/
ufs_vnops.c
2425 lines (2222 loc) · 57.8 KB
/
ufs_vnops.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $NetBSD: ufs_vnops.c,v 1.259 2020/09/05 16:30:13 riastradh Exp $ */
/*-
* Copyright (c) 2008, 2020 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Wasabi Systems, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1982, 1986, 1989, 1993, 1995
* The Regents of the University of California. All rights reserved.
* (c) UNIX System Laboratories, Inc.
* All or some portions of this file are derived from material licensed
* to the University of California by American Telephone and Telegraph
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
* the permission of UNIX System Laboratories, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)ufs_vnops.c 8.28 (Berkeley) 7/31/95
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ufs_vnops.c,v 1.259 2020/09/05 16:30:13 riastradh Exp $");
#if defined(_KERNEL_OPT)
#include "opt_ffs.h"
#include "opt_quota.h"
#include "opt_uvmhist.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/namei.h>
#include <sys/resourcevar.h>
#include <sys/kernel.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/buf.h>
#include <sys/proc.h>
#include <sys/mount.h>
#include <sys/vnode.h>
#include <sys/fstrans.h>
#include <sys/kmem.h>
#include <sys/malloc.h>
#include <sys/dirent.h>
#include <sys/lockf.h>
#include <sys/kauth.h>
#include <sys/wapbl.h>
#include <miscfs/specfs/specdev.h>
#include <miscfs/fifofs/fifo.h>
#include <miscfs/genfs/genfs.h>
#include <ufs/ufs/acl.h>
#include <ufs/ufs/inode.h>
#include <ufs/ufs/dir.h>
#include <ufs/ufs/ufsmount.h>
#include <ufs/ufs/ufs_bswap.h>
#include <ufs/ufs/ufs_extern.h>
#include <ufs/ufs/ufs_wapbl.h>
#ifdef UFS_DIRHASH
#include <ufs/ufs/dirhash.h>
#endif
#include <ufs/ext2fs/ext2fs_extern.h>
#include <ufs/ext2fs/ext2fs_dir.h>
#include <ufs/ffs/ffs_extern.h>
#include <ufs/lfs/lfs_extern.h>
#include <ufs/lfs/lfs.h>
#ifdef UVMHIST
#include <uvm/uvm.h>
#endif
#include <uvm/uvm_extern.h>
#include <uvm/uvm_stat.h>
__CTASSERT(EXT2FS_MAXNAMLEN == FFS_MAXNAMLEN);
__CTASSERT(LFS_MAXNAMLEN == FFS_MAXNAMLEN);
static int ufs_chmod(struct vnode *, int, kauth_cred_t, struct lwp *);
static int ufs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t,
struct lwp *);
static int ufs_makeinode(struct vattr *, struct vnode *,
const struct ufs_lookup_results *, struct vnode **, struct componentname *);
/*
* A virgin directory (no blushing please).
*/
static const struct dirtemplate mastertemplate = {
0, 12, DT_DIR, 1, ".",
0, UFS_DIRBLKSIZ - 12, DT_DIR, 2, ".."
};
/*
* Create a regular file
*/
int
ufs_create(void *v)
{
struct vop_create_v3_args /* {
struct vnode *a_dvp;
struct vnode **a_vpp;
struct componentname *a_cnp;
struct vattr *a_vap;
} */ *ap = v;
int error;
struct vnode *dvp = ap->a_dvp;
struct ufs_lookup_results *ulr;
/* XXX should handle this material another way */
ulr = &VTOI(dvp)->i_crap;
UFS_CHECK_CRAPCOUNTER(VTOI(dvp));
/*
* UFS_WAPBL_BEGIN(dvp->v_mount) performed by successful
* ufs_makeinode
*/
error = ufs_makeinode(ap->a_vap, dvp, ulr, ap->a_vpp, ap->a_cnp);
if (error) {
return (error);
}
UFS_WAPBL_END(dvp->v_mount);
VN_KNOTE(dvp, NOTE_WRITE);
VOP_UNLOCK(*ap->a_vpp);
return (0);
}
/*
* Mknod vnode call
*/
/* ARGSUSED */
int
ufs_mknod(void *v)
{
struct vop_mknod_v3_args /* {
struct vnode *a_dvp;
struct vnode **a_vpp;
struct componentname *a_cnp;
struct vattr *a_vap;
} */ *ap = v;
struct vattr *vap;
struct vnode **vpp;
struct inode *ip;
int error;
struct ufs_lookup_results *ulr;
vap = ap->a_vap;
vpp = ap->a_vpp;
/* XXX should handle this material another way */
ulr = &VTOI(ap->a_dvp)->i_crap;
UFS_CHECK_CRAPCOUNTER(VTOI(ap->a_dvp));
/*
* UFS_WAPBL_BEGIN(dvp->v_mount) performed by successful
* ufs_makeinode
*/
if ((error = ufs_makeinode(vap, ap->a_dvp, ulr, vpp, ap->a_cnp)) != 0)
goto out;
VN_KNOTE(ap->a_dvp, NOTE_WRITE);
ip = VTOI(*vpp);
ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
UFS_WAPBL_UPDATE(*vpp, NULL, NULL, 0);
UFS_WAPBL_END(ap->a_dvp->v_mount);
VOP_UNLOCK(*vpp);
out:
if (error != 0) {
*vpp = NULL;
return (error);
}
return (0);
}
/*
* Open called.
*
* Nothing to do.
*/
/* ARGSUSED */
int
ufs_open(void *v)
{
struct vop_open_args /* {
struct vnode *a_vp;
int a_mode;
kauth_cred_t a_cred;
} */ *ap = v;
/*
* Files marked append-only must be opened for appending.
*/
if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
(ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
return (EPERM);
return (0);
}
/*
* Close called.
*
* Update the times on the inode.
*/
/* ARGSUSED */
int
ufs_close(void *v)
{
struct vop_close_args /* {
struct vnode *a_vp;
int a_fflag;
kauth_cred_t a_cred;
} */ *ap = v;
struct vnode *vp;
vp = ap->a_vp;
if (vrefcnt(vp) > 1)
UFS_ITIMES(vp, NULL, NULL, NULL);
return (0);
}
static int
ufs_check_possible(struct vnode *vp, struct inode *ip, accmode_t accmode,
kauth_cred_t cred)
{
#if defined(QUOTA) || defined(QUOTA2)
int error;
#endif
/*
* Disallow write attempts on read-only file systems;
* unless the file is a socket, fifo, or a block or
* character device resident on the file system.
*/
if (accmode & VMODIFY_PERMS) {
switch (vp->v_type) {
case VDIR:
case VLNK:
case VREG:
if (vp->v_mount->mnt_flag & MNT_RDONLY)
return EROFS;
#if defined(QUOTA) || defined(QUOTA2)
error = chkdq(ip, 0, cred, 0);
if (error != 0)
return error;
#endif
break;
case VBAD:
case VBLK:
case VCHR:
case VSOCK:
case VFIFO:
case VNON:
default:
break;
}
}
/* If it is a snapshot, nobody gets access to it. */
if ((ip->i_flags & SF_SNAPSHOT))
return EPERM;
/*
* If immutable bit set, nobody gets to write it. "& ~VADMIN_PERMS"
* permits the owner of the file to remove the IMMUTABLE flag.
*/
if ((accmode & (VMODIFY_PERMS & ~VADMIN_PERMS)) &&
(ip->i_flags & IMMUTABLE))
return EPERM;
return 0;
}
static int
ufs_check_permitted(struct vnode *vp, struct inode *ip,
struct acl *acl, accmode_t accmode, kauth_cred_t cred,
int (*func)(struct vnode *, kauth_cred_t, uid_t, gid_t, mode_t,
struct acl *, accmode_t))
{
return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(accmode,
vp->v_type, ip->i_mode & ALLPERMS), vp, NULL, (*func)(vp, cred,
ip->i_uid, ip->i_gid, ip->i_mode & ALLPERMS, acl, accmode));
}
int
ufs_accessx(void *v)
{
struct vop_accessx_args /* {
struct vnode *a_vp;
accmode_t a_accmode;
kauth_cred_t a_cred;
} */ *ap = v;
struct vnode *vp = ap->a_vp;
struct inode *ip = VTOI(vp);
accmode_t accmode = ap->a_accmode;
int error;
#ifdef UFS_ACL
struct acl *acl;
acl_type_t type;
#endif
error = ufs_check_possible(vp, ip, accmode, ap->a_cred);
if (error)
return error;
#ifdef UFS_ACL
if ((vp->v_mount->mnt_flag & (MNT_POSIX1EACLS | MNT_ACLS)) != 0) {
if (vp->v_mount->mnt_flag & MNT_ACLS)
type = ACL_TYPE_NFS4;
else
type = ACL_TYPE_ACCESS;
acl = acl_alloc(KM_SLEEP);
if (type == ACL_TYPE_NFS4)
error = ufs_getacl_nfs4_internal(vp, acl, curlwp);
else
error = VOP_GETACL(vp, type, acl, ap->a_cred);
if (!error) {
if (type == ACL_TYPE_NFS4) {
error = ufs_check_permitted(vp,
ip, acl, accmode, ap->a_cred,
genfs_can_access_acl_nfs4);
} else {
error = vfs_unixify_accmode(&accmode);
if (error == 0)
error = ufs_check_permitted(vp,
ip, acl, accmode, ap->a_cred,
genfs_can_access_acl_posix1e);
}
acl_free(acl);
return error;
}
if (error != EOPNOTSUPP)
printf("%s: Error retrieving ACL: %d\n",
__func__, error);
/*
* XXX: Fall back until debugged. Should
* eventually possibly log an error, and return
* EPERM for safety.
*/
acl_free(acl);
}
#endif /* !UFS_ACL */
error = vfs_unixify_accmode(&accmode);
if (error)
return error;
return ufs_check_permitted(vp, ip,
NULL, accmode, ap->a_cred, genfs_can_access);
}
/* ARGSUSED */
int
ufs_getattr(void *v)
{
struct vop_getattr_args /* {
struct vnode *a_vp;
struct vattr *a_vap;
kauth_cred_t a_cred;
} */ *ap = v;
struct vnode *vp;
struct inode *ip;
struct vattr *vap;
vp = ap->a_vp;
ip = VTOI(vp);
vap = ap->a_vap;
UFS_ITIMES(vp, NULL, NULL, NULL);
/*
* Copy from inode table
*/
vap->va_fsid = ip->i_dev;
vap->va_fileid = ip->i_number;
vap->va_mode = ip->i_mode & ALLPERMS;
vap->va_nlink = ip->i_nlink;
vap->va_uid = ip->i_uid;
vap->va_gid = ip->i_gid;
vap->va_size = vp->v_size;
if (ip->i_ump->um_fstype == UFS1) {
switch (vp->v_type) {
case VBLK:
case VCHR:
vap->va_rdev = (dev_t)ufs_rw32(ip->i_ffs1_rdev,
UFS_MPNEEDSWAP(ip->i_ump));
break;
default:
vap->va_rdev = NODEV;
break;
}
vap->va_atime.tv_sec = ip->i_ffs1_atime;
vap->va_atime.tv_nsec = ip->i_ffs1_atimensec;
vap->va_mtime.tv_sec = ip->i_ffs1_mtime;
vap->va_mtime.tv_nsec = ip->i_ffs1_mtimensec;
vap->va_ctime.tv_sec = ip->i_ffs1_ctime;
vap->va_ctime.tv_nsec = ip->i_ffs1_ctimensec;
vap->va_birthtime.tv_sec = 0;
vap->va_birthtime.tv_nsec = 0;
vap->va_bytes = dbtob((u_quad_t)ip->i_ffs1_blocks);
} else {
switch (vp->v_type) {
case VBLK:
case VCHR:
vap->va_rdev = (dev_t)ufs_rw64(ip->i_ffs2_rdev,
UFS_MPNEEDSWAP(ip->i_ump));
break;
default:
vap->va_rdev = NODEV;
break;
}
vap->va_atime.tv_sec = ip->i_ffs2_atime;
vap->va_atime.tv_nsec = ip->i_ffs2_atimensec;
vap->va_mtime.tv_sec = ip->i_ffs2_mtime;
vap->va_mtime.tv_nsec = ip->i_ffs2_mtimensec;
vap->va_ctime.tv_sec = ip->i_ffs2_ctime;
vap->va_ctime.tv_nsec = ip->i_ffs2_ctimensec;
vap->va_birthtime.tv_sec = ip->i_ffs2_birthtime;
vap->va_birthtime.tv_nsec = ip->i_ffs2_birthnsec;
vap->va_bytes = dbtob(ip->i_ffs2_blocks);
}
vap->va_gen = ip->i_gen;
vap->va_flags = ip->i_flags;
/* this doesn't belong here */
if (vp->v_type == VBLK)
vap->va_blocksize = BLKDEV_IOSIZE;
else if (vp->v_type == VCHR)
vap->va_blocksize = MAXBSIZE;
else
vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
vap->va_type = vp->v_type;
vap->va_filerev = ip->i_modrev;
return (0);
}
/*
* Set attribute vnode op. called from several syscalls
*/
int
ufs_setattr(void *v)
{
struct vop_setattr_args /* {
struct vnode *a_vp;
struct vattr *a_vap;
kauth_cred_t a_cred;
} */ *ap = v;
struct vattr *vap;
struct vnode *vp;
struct inode *ip;
kauth_cred_t cred;
struct lwp *l;
int error;
kauth_action_t action;
bool changing_sysflags;
vap = ap->a_vap;
vp = ap->a_vp;
ip = VTOI(vp);
cred = ap->a_cred;
l = curlwp;
action = KAUTH_VNODE_WRITE_FLAGS;
changing_sysflags = false;
/*
* Check for unsettable attributes.
*/
if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
(vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
(vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
return (EINVAL);
}
UFS_WAPBL_JUNLOCK_ASSERT(vp->v_mount);
if (vap->va_flags != VNOVAL) {
if (vp->v_mount->mnt_flag & MNT_RDONLY) {
error = EROFS;
goto out;
}
/* Snapshot flag cannot be set or cleared */
if ((vap->va_flags & (SF_SNAPSHOT | SF_SNAPINVAL)) !=
(ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL))) {
error = EPERM;
goto out;
}
if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND)) {
action |= KAUTH_VNODE_HAS_SYSFLAGS;
}
if ((vap->va_flags & SF_SETTABLE) !=
(ip->i_flags & SF_SETTABLE)) {
action |= KAUTH_VNODE_WRITE_SYSFLAGS;
changing_sysflags = true;
}
error = kauth_authorize_vnode(cred, action, vp, NULL,
genfs_can_chflags(vp, cred, ip->i_uid, changing_sysflags));
if (error)
goto out;
if (changing_sysflags) {
error = UFS_WAPBL_BEGIN(vp->v_mount);
if (error)
goto out;
ip->i_flags = vap->va_flags;
DIP_ASSIGN(ip, flags, ip->i_flags);
} else {
error = UFS_WAPBL_BEGIN(vp->v_mount);
if (error)
goto out;
ip->i_flags &= SF_SETTABLE;
ip->i_flags |= (vap->va_flags & UF_SETTABLE);
DIP_ASSIGN(ip, flags, ip->i_flags);
}
ip->i_flag |= IN_CHANGE;
UFS_WAPBL_UPDATE(vp, NULL, NULL, 0);
UFS_WAPBL_END(vp->v_mount);
if (vap->va_flags & (IMMUTABLE | APPEND)) {
error = 0;
goto out;
}
}
if (ip->i_flags & (IMMUTABLE | APPEND)) {
error = EPERM;
goto out;
}
/*
* Go through the fields and update iff not VNOVAL.
*/
if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
if (vp->v_mount->mnt_flag & MNT_RDONLY) {
error = EROFS;
goto out;
}
error = UFS_WAPBL_BEGIN(vp->v_mount);
if (error)
goto out;
error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
UFS_WAPBL_END(vp->v_mount);
if (error)
goto out;
}
if (vap->va_size != VNOVAL) {
/*
* Disallow write attempts on read-only file systems;
* unless the file is a socket, fifo, or a block or
* character device resident on the file system.
*/
switch (vp->v_type) {
case VDIR:
error = EISDIR;
goto out;
case VCHR:
case VBLK:
case VFIFO:
break;
case VREG:
if (vp->v_mount->mnt_flag & MNT_RDONLY) {
error = EROFS;
goto out;
}
if ((ip->i_flags & SF_SNAPSHOT) != 0) {
error = EPERM;
goto out;
}
error = ufs_truncate_retry(vp, 0, vap->va_size, cred);
if (error)
goto out;
break;
default:
error = EOPNOTSUPP;
goto out;
}
}
ip = VTOI(vp);
if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
vap->va_birthtime.tv_sec != VNOVAL) {
if (vp->v_mount->mnt_flag & MNT_RDONLY) {
error = EROFS;
goto out;
}
if ((ip->i_flags & SF_SNAPSHOT) != 0) {
error = EPERM;
goto out;
}
error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp,
NULL, genfs_can_chtimes(vp, cred, ip->i_uid,
vap->va_vaflags));
if (error)
goto out;
error = UFS_WAPBL_BEGIN(vp->v_mount);
if (error)
goto out;
if (vap->va_atime.tv_sec != VNOVAL)
if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
ip->i_flag |= IN_ACCESS;
if (vap->va_mtime.tv_sec != VNOVAL) {
ip->i_flag |= IN_CHANGE | IN_UPDATE;
if (vp->v_mount->mnt_flag & MNT_RELATIME)
ip->i_flag |= IN_ACCESS;
}
if (vap->va_birthtime.tv_sec != VNOVAL &&
ip->i_ump->um_fstype == UFS2) {
ip->i_ffs2_birthtime = vap->va_birthtime.tv_sec;
ip->i_ffs2_birthnsec = vap->va_birthtime.tv_nsec;
}
error = UFS_UPDATE(vp, &vap->va_atime, &vap->va_mtime, 0);
UFS_WAPBL_END(vp->v_mount);
if (error)
goto out;
}
error = 0;
if (vap->va_mode != (mode_t)VNOVAL) {
if (vp->v_mount->mnt_flag & MNT_RDONLY) {
error = EROFS;
goto out;
}
if ((ip->i_flags & SF_SNAPSHOT) != 0 &&
(vap->va_mode & (S_IXUSR | S_IWUSR | S_IXGRP | S_IWGRP |
S_IXOTH | S_IWOTH))) {
error = EPERM;
goto out;
}
error = UFS_WAPBL_BEGIN(vp->v_mount);
if (error)
goto out;
error = ufs_chmod(vp, (int)vap->va_mode, cred, l);
UFS_WAPBL_END(vp->v_mount);
}
VN_KNOTE(vp, NOTE_ATTRIB);
out:
cache_enter_id(vp, ip->i_mode, ip->i_uid, ip->i_gid, !HAS_ACLS(ip));
return (error);
}
#ifdef UFS_ACL
static int
ufs_update_nfs4_acl_after_mode_change(struct vnode *vp, int mode,
int file_owner_id, kauth_cred_t cred, struct lwp *l)
{
int error;
struct acl *aclp;
aclp = acl_alloc(KM_SLEEP);
error = ufs_getacl_nfs4_internal(vp, aclp, l);
/*
* We don't have to handle EOPNOTSUPP here, as the filesystem claims
* it supports ACLs.
*/
if (error)
goto out;
acl_nfs4_sync_acl_from_mode(aclp, mode, file_owner_id);
error = ufs_setacl_nfs4_internal(vp, aclp, l, false);
out:
acl_free(aclp);
return (error);
}
#endif /* UFS_ACL */
/*
* Change the mode on a file.
* Inode must be locked before calling.
*/
static int
ufs_chmod(struct vnode *vp, int mode, kauth_cred_t cred, struct lwp *l)
{
struct inode *ip;
int error;
UFS_WAPBL_JLOCK_ASSERT(vp->v_mount);
ip = VTOI(vp);
#ifdef UFS_ACL
/*
* To modify the permissions on a file, must possess VADMIN
* for that file.
*/
if ((error = VOP_ACCESSX(vp, VWRITE_ACL, cred)) != 0)
return error;
#endif
error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp,
NULL, genfs_can_chmod(vp, cred, ip->i_uid, ip->i_gid, mode));
if (error)
return (error);
#ifdef UFS_ACL
if ((vp->v_mount->mnt_flag & MNT_ACLS) != 0) {
error = ufs_update_nfs4_acl_after_mode_change(vp, mode,
ip->i_uid, cred, l);
if (error)
return error;
}
#endif
ip->i_mode &= ~ALLPERMS;
ip->i_mode |= (mode & ALLPERMS);
ip->i_flag |= IN_CHANGE;
DIP_ASSIGN(ip, mode, ip->i_mode);
UFS_WAPBL_UPDATE(vp, NULL, NULL, 0);
cache_enter_id(vp, ip->i_mode, ip->i_uid, ip->i_gid, !HAS_ACLS(ip));
return (0);
}
/*
* Perform chown operation on inode ip;
* inode must be locked prior to call.
*/
static int
ufs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred,
struct lwp *l)
{
struct inode *ip;
int error = 0;
#if defined(QUOTA) || defined(QUOTA2)
uid_t ouid;
gid_t ogid;
int64_t change;
#endif
ip = VTOI(vp);
error = 0;
if (uid == (uid_t)VNOVAL)
uid = ip->i_uid;
if (gid == (gid_t)VNOVAL)
gid = ip->i_gid;
#ifdef UFS_ACL
/*
* To modify the ownership of a file, must possess VADMIN for that
* file.
*/
if ((error = VOP_ACCESSX(vp, VWRITE_OWNER, cred)) != 0)
return error;
#endif
error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp,
NULL, genfs_can_chown(vp, cred, ip->i_uid, ip->i_gid, uid, gid));
if (error)
return (error);
#if defined(QUOTA) || defined(QUOTA2)
ogid = ip->i_gid;
ouid = ip->i_uid;
change = DIP(ip, blocks);
(void) chkdq(ip, -change, cred, 0);
(void) chkiq(ip, -1, cred, 0);
#endif
ip->i_gid = gid;
DIP_ASSIGN(ip, gid, gid);
ip->i_uid = uid;
DIP_ASSIGN(ip, uid, uid);
#if defined(QUOTA) || defined(QUOTA2)
if ((error = chkdq(ip, change, cred, 0)) == 0) {
if ((error = chkiq(ip, 1, cred, 0)) == 0)
goto good;
else
(void) chkdq(ip, -change, cred, FORCE);
}
ip->i_gid = ogid;
DIP_ASSIGN(ip, gid, ogid);
ip->i_uid = ouid;
DIP_ASSIGN(ip, uid, ouid);
(void) chkdq(ip, change, cred, FORCE);
(void) chkiq(ip, 1, cred, FORCE);
return (error);
good:
#endif /* QUOTA || QUOTA2 */
ip->i_flag |= IN_CHANGE;
UFS_WAPBL_UPDATE(vp, NULL, NULL, 0);
cache_enter_id(vp, ip->i_mode, ip->i_uid, ip->i_gid, !HAS_ACLS(ip));
return (0);
}
int
ufs_remove(void *v)
{
struct vop_remove_v2_args /* {
struct vnode *a_dvp;
struct vnode *a_vp;
struct componentname *a_cnp;
} */ *ap = v;
struct vnode *vp, *dvp;
struct inode *ip;
struct mount *mp;
int error;
struct ufs_lookup_results *ulr;
vp = ap->a_vp;
dvp = ap->a_dvp;
ip = VTOI(vp);
mp = dvp->v_mount;
KASSERT(mp == vp->v_mount); /* XXX Not stable without lock. */
#ifdef UFS_ACL
#ifdef notyet
/* We don't do this because if the filesystem is mounted without ACLs
* this goes through vfs_unixify_accmode() and we get EPERM.
*/
error = VOP_ACCESSX(vp, VDELETE, ap->a_cnp->cn_cred);
if (error)
goto err;
#endif
#endif
/* XXX should handle this material another way */
ulr = &VTOI(dvp)->i_crap;
UFS_CHECK_CRAPCOUNTER(VTOI(dvp));
if (vp->v_type == VDIR || (ip->i_flags & (IMMUTABLE | APPEND)) ||
(VTOI(dvp)->i_flags & APPEND))
error = EPERM;
else {
error = UFS_WAPBL_BEGIN(mp);
if (error == 0) {
error = ufs_dirremove(dvp, ulr,
ip, ap->a_cnp->cn_flags, 0);
UFS_WAPBL_END(mp);
}
}
VN_KNOTE(vp, NOTE_DELETE);
VN_KNOTE(dvp, NOTE_WRITE);
#ifdef notyet
err:
#endif
if (dvp == vp)
vrele(vp);
else
vput(vp);
return (error);
}
/*
* ufs_link: create hard link.
*/
int
ufs_link(void *v)
{
struct vop_link_v2_args /* {
struct vnode *a_dvp;
struct vnode *a_vp;
struct componentname *a_cnp;
} */ *ap = v;
struct vnode *dvp = ap->a_dvp;
struct vnode *vp = ap->a_vp;
struct componentname *cnp = ap->a_cnp;
struct mount *mp = dvp->v_mount;
struct inode *ip;
struct direct *newdir;
int error;
struct ufs_lookup_results *ulr;
KASSERT(dvp != vp);
KASSERT(vp->v_type != VDIR);
KASSERT(mp == vp->v_mount); /* XXX Not stable without lock. */
/* XXX should handle this material another way */
ulr = &VTOI(dvp)->i_crap;
UFS_CHECK_CRAPCOUNTER(VTOI(dvp));
error = vn_lock(vp, LK_EXCLUSIVE);
if (error) {
VOP_ABORTOP(dvp, cnp);
goto out2;
}
ip = VTOI(vp);
if ((nlink_t)ip->i_nlink >= LINK_MAX) {
VOP_ABORTOP(dvp, cnp);
error = EMLINK;
goto out1;
}
if (ip->i_flags & (IMMUTABLE | APPEND)) {
VOP_ABORTOP(dvp, cnp);
error = EPERM;
goto out1;
}
error = UFS_WAPBL_BEGIN(mp);
if (error) {
VOP_ABORTOP(dvp, cnp);
goto out1;
}
ip->i_nlink++;
DIP_ASSIGN(ip, nlink, ip->i_nlink);
ip->i_flag |= IN_CHANGE;
error = UFS_UPDATE(vp, NULL, NULL, UPDATE_DIROP);
if (!error) {
newdir = pool_cache_get(ufs_direct_cache, PR_WAITOK);
ufs_makedirentry(ip, cnp, newdir);
error = ufs_direnter(dvp, ulr, vp, newdir, cnp, NULL);
pool_cache_put(ufs_direct_cache, newdir);
}
if (error) {
ip->i_nlink--;
DIP_ASSIGN(ip, nlink, ip->i_nlink);
ip->i_flag |= IN_CHANGE;
UFS_WAPBL_UPDATE(vp, NULL, NULL, UPDATE_DIROP);
}
UFS_WAPBL_END(mp);
out1:
VOP_UNLOCK(vp);
out2:
VN_KNOTE(vp, NOTE_LINK);
VN_KNOTE(dvp, NOTE_WRITE);
return (error);
}
/*
* whiteout vnode call
*/
int
ufs_whiteout(void *v)
{
struct vop_whiteout_args /* {
struct vnode *a_dvp;
struct componentname *a_cnp;
int a_flags;
} */ *ap = v;
struct vnode *dvp = ap->a_dvp;
struct componentname *cnp = ap->a_cnp;
struct direct *newdir;
int error;
struct ufsmount *ump = VFSTOUFS(dvp->v_mount);
struct ufs_lookup_results *ulr;
/* XXX should handle this material another way */
ulr = &VTOI(dvp)->i_crap;
UFS_CHECK_CRAPCOUNTER(VTOI(dvp));
error = 0;
switch (ap->a_flags) {
case LOOKUP:
/* 4.4 format directories support whiteout operations */
if (ump->um_maxsymlinklen > 0)
return (0);
return (EOPNOTSUPP);
case CREATE:
/* create a new directory whiteout */
error = UFS_WAPBL_BEGIN(dvp->v_mount);
if (error)
break;
KASSERTMSG((ump->um_maxsymlinklen > 0),
"ufs_whiteout: old format filesystem");
newdir = pool_cache_get(ufs_direct_cache, PR_WAITOK);
newdir->d_ino = UFS_WINO;
newdir->d_namlen = cnp->cn_namelen;
memcpy(newdir->d_name, cnp->cn_nameptr,
(size_t)cnp->cn_namelen);
/* NUL terminate and zero out padding */
memset(&newdir->d_name[cnp->cn_namelen], 0,