1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1982, 1986, 1989, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include <sys/cdefs.h>
38 #include "opt_quota.h"
39 #include "opt_suiddir.h"
40 #include "opt_ufs.h"
41 #include "opt_ffs.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 #include <sys/namei.h>
47 #include <sys/kernel.h>
48 #include <sys/fcntl.h>
49 #include <sys/filio.h>
50 #include <sys/stat.h>
51 #include <sys/bio.h>
52 #include <sys/buf.h>
53 #include <sys/mount.h>
54 #include <sys/priv.h>
55 #include <sys/refcount.h>
56 #include <sys/unistd.h>
57 #include <sys/vnode.h>
58 #include <sys/dirent.h>
59 #include <sys/lockf.h>
60 #include <sys/conf.h>
61 #include <sys/acl.h>
62 #include <sys/smr.h>
63
64 #include <security/audit/audit.h>
65 #include <security/mac/mac_framework.h>
66
67 #include <sys/file.h> /* XXX */
68
69 #include <vm/vm.h>
70 #include <vm/vm_extern.h>
71
72 #include <ufs/ufs/acl.h>
73 #include <ufs/ufs/extattr.h>
74 #include <ufs/ufs/quota.h>
75 #include <ufs/ufs/inode.h>
76 #include <ufs/ufs/dir.h>
77 #include <ufs/ufs/ufsmount.h>
78 #include <ufs/ufs/ufs_extern.h>
79 #ifdef UFS_DIRHASH
80 #include <ufs/ufs/dirhash.h>
81 #endif
82 #ifdef UFS_GJOURNAL
83 #include <ufs/ufs/gjournal.h>
84 FEATURE(ufs_gjournal, "Journaling support through GEOM for UFS");
85 #endif
86
87 #ifdef QUOTA
88 FEATURE(ufs_quota, "UFS disk quotas support");
89 FEATURE(ufs_quota64, "64bit UFS disk quotas support");
90 #endif
91
92 #ifdef SUIDDIR
93 FEATURE(suiddir,
94 "Give all new files in directory the same ownership as the directory");
95 #endif
96
97 VFS_SMR_DECLARE;
98
99 #include <ufs/ffs/ffs_extern.h>
100
101 static vop_accessx_t ufs_accessx;
102 vop_fplookup_vexec_t ufs_fplookup_vexec;
103 static int ufs_chmod(struct vnode *, int, struct ucred *, struct thread *);
104 static int ufs_chown(struct vnode *, uid_t, gid_t, struct ucred *,
105 struct thread *);
106 static vop_close_t ufs_close;
107 static vop_create_t ufs_create;
108 static vop_stat_t ufs_stat;
109 static vop_getattr_t ufs_getattr;
110 static vop_ioctl_t ufs_ioctl;
111 static vop_link_t ufs_link;
112 static int ufs_makeinode(int mode, struct vnode *, struct vnode **,
113 struct componentname *, const char *);
114 static vop_mmapped_t ufs_mmapped;
115 static vop_mkdir_t ufs_mkdir;
116 static vop_mknod_t ufs_mknod;
117 static vop_open_t ufs_open;
118 static vop_pathconf_t ufs_pathconf;
119 static vop_print_t ufs_print;
120 static vop_readlink_t ufs_readlink;
121 static vop_remove_t ufs_remove;
122 static vop_rename_t ufs_rename;
123 static vop_rmdir_t ufs_rmdir;
124 static vop_setattr_t ufs_setattr;
125 static vop_strategy_t ufs_strategy;
126 static vop_symlink_t ufs_symlink;
127 static vop_whiteout_t ufs_whiteout;
128 static vop_close_t ufsfifo_close;
129
130 SYSCTL_NODE(_vfs, OID_AUTO, ufs, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
131 "UFS filesystem");
132
133 /*
134 * A virgin directory (no blushing please).
135 */
136 static struct dirtemplate mastertemplate = {
137 0, 12, DT_DIR, 1, ".",
138 0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
139 };
140 static struct odirtemplate omastertemplate = {
141 0, 12, 1, ".",
142 0, DIRBLKSIZ - 12, 2, ".."
143 };
144
145 static void
ufs_itimes_locked(struct vnode * vp)146 ufs_itimes_locked(struct vnode *vp)
147 {
148 struct inode *ip;
149 struct timespec ts;
150
151 ASSERT_VI_LOCKED(vp, __func__);
152
153 ip = VTOI(vp);
154 if (UFS_RDONLY(ip))
155 goto out;
156 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
157 return;
158
159 if (VN_ISDEV(vp) && !DOINGSOFTDEP(vp))
160 UFS_INODE_SET_FLAG(ip, IN_LAZYMOD);
161 else if (((vp->v_mount->mnt_kern_flag &
162 (MNTK_SUSPENDED | MNTK_SUSPEND)) == 0) ||
163 (ip->i_flag & (IN_CHANGE | IN_UPDATE)) != 0)
164 UFS_INODE_SET_FLAG(ip, IN_MODIFIED);
165 else if ((ip->i_flag & IN_ACCESS) != 0)
166 UFS_INODE_SET_FLAG(ip, IN_LAZYACCESS);
167 vfs_timestamp(&ts);
168 if ((ip->i_flag & IN_ACCESS) != 0) {
169 DIP_SET(ip, i_atime, ts.tv_sec);
170 DIP_SET(ip, i_atimensec, ts.tv_nsec);
171 }
172 if ((ip->i_flag & IN_UPDATE) != 0) {
173 DIP_SET(ip, i_mtime, ts.tv_sec);
174 DIP_SET(ip, i_mtimensec, ts.tv_nsec);
175 }
176 if ((ip->i_flag & IN_CHANGE) != 0) {
177 DIP_SET(ip, i_ctime, ts.tv_sec);
178 DIP_SET(ip, i_ctimensec, ts.tv_nsec);
179 DIP_SET(ip, i_modrev, DIP(ip, i_modrev) + 1);
180 }
181
182 out:
183 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
184 }
185
186 void
ufs_itimes(struct vnode * vp)187 ufs_itimes(struct vnode *vp)
188 {
189 struct inode *ip;
190
191 ip = VTOI(vp);
192 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
193 return;
194
195 VI_LOCK(vp);
196 ufs_itimes_locked(vp);
197 VI_UNLOCK(vp);
198 }
199
200 static int
ufs_sync_nlink1(struct mount * mp)201 ufs_sync_nlink1(struct mount *mp)
202 {
203 int error;
204
205 error = vfs_busy(mp, 0);
206 if (error == 0) {
207 VFS_SYNC(mp, MNT_WAIT);
208 vfs_unbusy(mp);
209 error = ERELOOKUP;
210 }
211 vfs_rel(mp);
212 return (error);
213 }
214
215 static int
ufs_sync_nlink(struct vnode * vp,struct vnode * vp1)216 ufs_sync_nlink(struct vnode *vp, struct vnode *vp1)
217 {
218 struct inode *ip;
219 struct mount *mp;
220 int error;
221
222 ip = VTOI(vp);
223 if (ip->i_nlink < UFS_LINK_MAX)
224 return (0);
225 if (!DOINGSOFTDEP(vp) || ip->i_effnlink >= UFS_LINK_MAX)
226 return (EMLINK);
227
228 mp = vp->v_mount;
229 vfs_ref(mp);
230 VOP_UNLOCK(vp);
231 if (vp1 != NULL)
232 VOP_UNLOCK(vp1);
233 error = ufs_sync_nlink1(mp);
234 vn_lock_pair(vp, false, LK_EXCLUSIVE, vp1, false, LK_EXCLUSIVE);
235 return (error);
236 }
237
238 /*
239 * Create a regular file
240 */
241 static int
ufs_create(struct vop_create_args * ap)242 ufs_create(
243 struct vop_create_args /* {
244 struct vnode *a_dvp;
245 struct vnode **a_vpp;
246 struct componentname *a_cnp;
247 struct vattr *a_vap;
248 } */ *ap)
249 {
250 int error;
251
252 error =
253 ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
254 ap->a_dvp, ap->a_vpp, ap->a_cnp, "ufs_create");
255 if (error != 0)
256 return (error);
257 if ((ap->a_cnp->cn_flags & MAKEENTRY) != 0)
258 cache_enter(ap->a_dvp, *ap->a_vpp, ap->a_cnp);
259 return (0);
260 }
261
262 /*
263 * Mknod vnode call
264 */
265 /* ARGSUSED */
266 static int
ufs_mknod(struct vop_mknod_args * ap)267 ufs_mknod(
268 struct vop_mknod_args /* {
269 struct vnode *a_dvp;
270 struct vnode **a_vpp;
271 struct componentname *a_cnp;
272 struct vattr *a_vap;
273 } */ *ap)
274 {
275 struct vattr *vap = ap->a_vap;
276 struct vnode **vpp = ap->a_vpp;
277 struct inode *ip;
278 ino_t ino;
279 int error;
280
281 error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
282 ap->a_dvp, vpp, ap->a_cnp, "ufs_mknod");
283 if (error)
284 return (error);
285 ip = VTOI(*vpp);
286 UFS_INODE_SET_FLAG(ip, IN_ACCESS | IN_CHANGE | IN_UPDATE);
287 if (vap->va_rdev != VNOVAL) {
288 /*
289 * Want to be able to use this to make badblock
290 * inodes, so don't truncate the dev number.
291 */
292 DIP_SET(ip, i_rdev, vap->va_rdev);
293 }
294 /*
295 * Remove inode, then reload it through VFS_VGET(). This is
296 * needed to do further inode initialization, for instance
297 * fifo, which was too early for VFS_VGET() done as part of
298 * UFS_VALLOC().
299 */
300 (*vpp)->v_type = VNON;
301 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
302 vgone(*vpp);
303 vput(*vpp);
304 error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp);
305 if (error) {
306 *vpp = NULL;
307 return (error);
308 }
309 return (0);
310 }
311
312 /*
313 * Open called.
314 */
315 /* ARGSUSED */
316 static int
ufs_open(struct vop_open_args * ap)317 ufs_open(struct vop_open_args *ap)
318 {
319 struct vnode *vp = ap->a_vp;
320 struct inode *ip;
321
322 if (VN_ISDEV(vp))
323 return (EOPNOTSUPP);
324
325 ip = VTOI(vp);
326 vnode_create_vobject(vp, DIP(ip, i_size), ap->a_td);
327 if (vp->v_type == VREG && (vn_irflag_read(vp) & VIRF_PGREAD) == 0 &&
328 ip->i_ump->um_bsize >= PAGE_SIZE) {
329 vn_irflag_set_cond(vp, VIRF_PGREAD);
330 }
331
332 /*
333 * Files marked append-only must be opened for appending.
334 */
335 if ((ip->i_flags & APPEND) &&
336 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
337 return (EPERM);
338
339 return (0);
340 }
341
342 /*
343 * Close called.
344 *
345 * Update the times on the inode.
346 */
347 /* ARGSUSED */
348 static int
ufs_close(struct vop_close_args * ap)349 ufs_close(
350 struct vop_close_args /* {
351 struct vnode *a_vp;
352 int a_fflag;
353 struct ucred *a_cred;
354 struct thread *a_td;
355 } */ *ap)
356 {
357 struct vnode *vp = ap->a_vp;
358
359 ufs_itimes(vp);
360 return (0);
361 }
362
363 static int
ufs_accessx(struct vop_accessx_args * ap)364 ufs_accessx(
365 struct vop_accessx_args /* {
366 struct vnode *a_vp;
367 accmode_t a_accmode;
368 struct ucred *a_cred;
369 struct thread *a_td;
370 } */ *ap)
371 {
372 struct vnode *vp = ap->a_vp;
373 struct inode *ip = VTOI(vp);
374 accmode_t accmode = ap->a_accmode;
375 int error;
376 #ifdef UFS_ACL
377 struct acl *acl;
378 acl_type_t type;
379 #endif
380
381 /*
382 * Disallow write attempts on read-only filesystems;
383 * unless the file is a socket, fifo, or a block or
384 * character device resident on the filesystem.
385 */
386 if (accmode & VMODIFY_PERMS) {
387 switch (vp->v_type) {
388 case VDIR:
389 case VLNK:
390 case VREG:
391 if (vp->v_mount->mnt_flag & MNT_RDONLY)
392 return (EROFS);
393 #ifdef QUOTA
394 /*
395 * Inode is accounted in the quotas only if struct
396 * dquot is attached to it. VOP_ACCESS() is called
397 * from vn_open_cred() and provides a convenient
398 * point to call getinoquota(). The lock mode is
399 * exclusive when the file is opening for write.
400 */
401 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE) {
402 error = getinoquota(ip);
403 if (error != 0)
404 return (error);
405 }
406 #endif
407 break;
408 default:
409 break;
410 }
411 }
412
413 /*
414 * If immutable bit set, nobody gets to write it. "& ~VADMIN_PERMS"
415 * permits the owner of the file to remove the IMMUTABLE flag.
416 */
417 if ((accmode & (VMODIFY_PERMS & ~VADMIN_PERMS)) &&
418 (ip->i_flags & (IMMUTABLE | SF_SNAPSHOT)))
419 return (EPERM);
420
421 #ifdef UFS_ACL
422 if ((vp->v_mount->mnt_flag & (MNT_ACLS | MNT_NFS4ACLS)) != 0) {
423 if (vp->v_mount->mnt_flag & MNT_NFS4ACLS)
424 type = ACL_TYPE_NFS4;
425 else
426 type = ACL_TYPE_ACCESS;
427
428 acl = acl_alloc(M_WAITOK);
429 if (type == ACL_TYPE_NFS4)
430 error = ufs_getacl_nfs4_internal(vp, acl, ap->a_td);
431 else
432 error = VOP_GETACL(vp, type, acl, ap->a_cred, ap->a_td);
433 switch (error) {
434 case 0:
435 if (type == ACL_TYPE_NFS4) {
436 error = vaccess_acl_nfs4(vp->v_type, ip->i_uid,
437 ip->i_gid, acl, accmode, ap->a_cred);
438 } else {
439 error = vfs_unixify_accmode(&accmode);
440 if (error == 0)
441 error = vaccess_acl_posix1e(vp->v_type, ip->i_uid,
442 ip->i_gid, acl, accmode, ap->a_cred);
443 }
444 break;
445 default:
446 if (error != EOPNOTSUPP)
447 printf(
448 "ufs_accessx(): Error retrieving ACL on object (%d).\n",
449 error);
450 /*
451 * XXX: Fall back until debugged. Should
452 * eventually possibly log an error, and return
453 * EPERM for safety.
454 */
455 error = vfs_unixify_accmode(&accmode);
456 if (error == 0)
457 error = vaccess(vp->v_type, ip->i_mode,
458 ip->i_uid, ip->i_gid, accmode, ap->a_cred);
459 }
460 acl_free(acl);
461
462 return (error);
463 }
464 #endif /* !UFS_ACL */
465 error = vfs_unixify_accmode(&accmode);
466 if (error == 0)
467 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
468 accmode, ap->a_cred);
469 return (error);
470 }
471
472 /*
473 * VOP_FPLOOKUP_VEXEC routines are subject to special circumstances, see
474 * the comment above cache_fplookup for details.
475 */
476 int
ufs_fplookup_vexec(struct vop_fplookup_vexec_args * ap)477 ufs_fplookup_vexec(
478 struct vop_fplookup_vexec_args /* {
479 struct vnode *a_vp;
480 struct ucred *a_cred;
481 struct thread *a_td;
482 } */ *ap)
483 {
484 struct vnode *vp;
485 struct inode *ip;
486 struct ucred *cred;
487 mode_t all_x, mode;
488
489 vp = ap->a_vp;
490 ip = VTOI_SMR(vp);
491 if (__predict_false(ip == NULL))
492 return (EAGAIN);
493
494 /*
495 * XXX ACL race
496 *
497 * ACLs are not supported and UFS clears/sets this flag on mount and
498 * remount. However, we may still be racing with seeing them and there
499 * is no provision to make sure they were accounted for. This matches
500 * the behavior of the locked case, since the lookup there is also
501 * racy: mount takes no measures to block anyone from progressing.
502 */
503 all_x = S_IXUSR | S_IXGRP | S_IXOTH;
504 mode = atomic_load_short(&ip->i_mode);
505 if (__predict_true((mode & all_x) == all_x))
506 return (0);
507
508 cred = ap->a_cred;
509 return (vaccess_vexec_smr(mode, ip->i_uid, ip->i_gid, cred));
510 }
511
512 /* ARGSUSED */
513 static int
ufs_stat(struct vop_stat_args * ap)514 ufs_stat(struct vop_stat_args *ap)
515 {
516 struct vnode *vp = ap->a_vp;
517 struct inode *ip = VTOI(vp);
518 struct stat *sb = ap->a_sb;
519 int error;
520
521 error = vop_stat_helper_pre(ap);
522 if (__predict_false(error))
523 return (error);
524
525 VI_LOCK(vp);
526 ufs_itimes_locked(vp);
527 if (I_IS_UFS1(ip)) {
528 sb->st_atim.tv_sec = ip->i_din1->di_atime;
529 sb->st_atim.tv_nsec = ip->i_din1->di_atimensec;
530 } else {
531 sb->st_atim.tv_sec = ip->i_din2->di_atime;
532 sb->st_atim.tv_nsec = ip->i_din2->di_atimensec;
533 }
534 VI_UNLOCK(vp);
535
536 sb->st_dev = dev2udev(ITOUMP(ip)->um_dev);
537 sb->st_ino = ip->i_number;
538 sb->st_mode = (ip->i_mode & ~IFMT) | VTTOIF(vp->v_type);
539 sb->st_nlink = ip->i_effnlink;
540 sb->st_uid = ip->i_uid;
541 sb->st_gid = ip->i_gid;
542 if (I_IS_UFS1(ip)) {
543 sb->st_rdev = VN_ISDEV(vp) ? ip->i_din1->di_rdev : NODEV;
544 sb->st_size = ip->i_din1->di_size;
545 sb->st_mtim.tv_sec = ip->i_din1->di_mtime;
546 sb->st_mtim.tv_nsec = ip->i_din1->di_mtimensec;
547 sb->st_ctim.tv_sec = ip->i_din1->di_ctime;
548 sb->st_ctim.tv_nsec = ip->i_din1->di_ctimensec;
549 sb->st_birthtim.tv_sec = -1;
550 sb->st_birthtim.tv_nsec = 0;
551 sb->st_blocks = dbtob((uint64_t)ip->i_din1->di_blocks) / S_BLKSIZE;
552 sb->st_filerev = ip->i_din1->di_modrev;
553 } else {
554 sb->st_rdev = VN_ISDEV(vp) ? ip->i_din2->di_rdev : NODEV;
555 sb->st_size = ip->i_din2->di_size;
556 sb->st_mtim.tv_sec = ip->i_din2->di_mtime;
557 sb->st_mtim.tv_nsec = ip->i_din2->di_mtimensec;
558 sb->st_ctim.tv_sec = ip->i_din2->di_ctime;
559 sb->st_ctim.tv_nsec = ip->i_din2->di_ctimensec;
560 sb->st_birthtim.tv_sec = ip->i_din2->di_birthtime;
561 sb->st_birthtim.tv_nsec = ip->i_din2->di_birthnsec;
562 sb->st_blocks = dbtob((uint64_t)ip->i_din2->di_blocks) / S_BLKSIZE;
563 sb->st_filerev = ip->i_din2->di_modrev;
564 }
565
566 sb->st_blksize = max(PAGE_SIZE, vp->v_mount->mnt_stat.f_iosize);
567 sb->st_flags = ip->i_flags;
568 sb->st_gen = ip->i_gen;
569
570 return (vop_stat_helper_post(ap, error));
571 }
572
573 /* ARGSUSED */
574 static int
ufs_getattr(struct vop_getattr_args * ap)575 ufs_getattr(
576 struct vop_getattr_args /* {
577 struct vnode *a_vp;
578 struct vattr *a_vap;
579 struct ucred *a_cred;
580 } */ *ap)
581 {
582 struct vnode *vp = ap->a_vp;
583 struct inode *ip = VTOI(vp);
584 struct vattr *vap = ap->a_vap;
585
586 VI_LOCK(vp);
587 ufs_itimes_locked(vp);
588 if (I_IS_UFS1(ip)) {
589 vap->va_atime.tv_sec = ip->i_din1->di_atime;
590 vap->va_atime.tv_nsec = ip->i_din1->di_atimensec;
591 } else {
592 vap->va_atime.tv_sec = ip->i_din2->di_atime;
593 vap->va_atime.tv_nsec = ip->i_din2->di_atimensec;
594 }
595 VI_UNLOCK(vp);
596 /*
597 * Copy from inode table
598 */
599 vap->va_fsid = dev2udev(ITOUMP(ip)->um_dev);
600 vap->va_fileid = ip->i_number;
601 vap->va_mode = ip->i_mode & ~IFMT;
602 vap->va_nlink = ip->i_effnlink;
603 vap->va_uid = ip->i_uid;
604 vap->va_gid = ip->i_gid;
605 if (I_IS_UFS1(ip)) {
606 vap->va_rdev = VN_ISDEV(vp) ? ip->i_din1->di_rdev : NODEV;
607 vap->va_size = ip->i_din1->di_size;
608 vap->va_mtime.tv_sec = ip->i_din1->di_mtime;
609 vap->va_mtime.tv_nsec = ip->i_din1->di_mtimensec;
610 vap->va_ctime.tv_sec = ip->i_din1->di_ctime;
611 vap->va_ctime.tv_nsec = ip->i_din1->di_ctimensec;
612 vap->va_bytes = dbtob((uint64_t)ip->i_din1->di_blocks);
613 vap->va_filerev = ip->i_din1->di_modrev;
614 } else {
615 vap->va_rdev = VN_ISDEV(vp) ? ip->i_din2->di_rdev : NODEV;
616 vap->va_size = ip->i_din2->di_size;
617 vap->va_mtime.tv_sec = ip->i_din2->di_mtime;
618 vap->va_mtime.tv_nsec = ip->i_din2->di_mtimensec;
619 vap->va_ctime.tv_sec = ip->i_din2->di_ctime;
620 vap->va_ctime.tv_nsec = ip->i_din2->di_ctimensec;
621 vap->va_birthtime.tv_sec = ip->i_din2->di_birthtime;
622 vap->va_birthtime.tv_nsec = ip->i_din2->di_birthnsec;
623 vap->va_bytes = dbtob((uint64_t)ip->i_din2->di_blocks);
624 vap->va_filerev = ip->i_din2->di_modrev;
625 }
626 vap->va_flags = ip->i_flags;
627 vap->va_gen = ip->i_gen;
628 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
629 vap->va_type = IFTOVT(ip->i_mode);
630 return (0);
631 }
632
633 /*
634 * Set attribute vnode op. called from several syscalls
635 */
636 static int
ufs_setattr(struct vop_setattr_args * ap)637 ufs_setattr(
638 struct vop_setattr_args /* {
639 struct vnode *a_vp;
640 struct vattr *a_vap;
641 struct ucred *a_cred;
642 } */ *ap)
643 {
644 struct vattr *vap = ap->a_vap;
645 struct vnode *vp = ap->a_vp;
646 struct inode *ip = VTOI(vp);
647 struct ucred *cred = ap->a_cred;
648 struct thread *td = curthread;
649 int error;
650
651 /*
652 * Check for unsettable attributes.
653 */
654 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
655 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
656 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
657 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
658 return (EINVAL);
659 }
660 if (vap->va_flags != VNOVAL) {
661 if ((vap->va_flags & ~(SF_APPEND | SF_ARCHIVED | SF_IMMUTABLE |
662 SF_NOUNLINK | SF_SNAPSHOT | UF_APPEND | UF_ARCHIVE |
663 UF_HIDDEN | UF_IMMUTABLE | UF_NODUMP | UF_NOUNLINK |
664 UF_OFFLINE | UF_OPAQUE | UF_READONLY | UF_REPARSE |
665 UF_SPARSE | UF_SYSTEM)) != 0)
666 return (EOPNOTSUPP);
667 if (vp->v_mount->mnt_flag & MNT_RDONLY)
668 return (EROFS);
669 /*
670 * Callers may only modify the file flags on objects they
671 * have VADMIN rights for.
672 */
673 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
674 return (error);
675 /*
676 * Unprivileged processes are not permitted to unset system
677 * flags, or modify flags if any system flags are set.
678 * Privileged non-jail processes may not modify system flags
679 * if securelevel > 0 and any existing system flags are set.
680 * Privileged jail processes behave like privileged non-jail
681 * processes if the PR_ALLOW_CHFLAGS permission bit is set;
682 * otherwise, they behave like unprivileged processes.
683 */
684 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS)) {
685 if (ip->i_flags &
686 (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
687 error = securelevel_gt(cred, 0);
688 if (error)
689 return (error);
690 }
691 /* The snapshot flag cannot be toggled. */
692 if ((vap->va_flags ^ ip->i_flags) & SF_SNAPSHOT)
693 return (EPERM);
694 } else {
695 if (ip->i_flags &
696 (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
697 ((vap->va_flags ^ ip->i_flags) & SF_SETTABLE))
698 return (EPERM);
699 }
700 ip->i_flags = vap->va_flags;
701 DIP_SET(ip, i_flags, vap->va_flags);
702 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
703 error = UFS_UPDATE(vp, 0);
704 if (ip->i_flags & (IMMUTABLE | APPEND))
705 return (error);
706 }
707 /*
708 * If immutable or append, no one can change any of its attributes
709 * except the ones already handled (in some cases, file flags
710 * including the immutability flags themselves for the superuser).
711 */
712 if (ip->i_flags & (IMMUTABLE | APPEND))
713 return (EPERM);
714 /*
715 * Go through the fields and update iff not VNOVAL.
716 */
717 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
718 if (vp->v_mount->mnt_flag & MNT_RDONLY)
719 return (EROFS);
720 if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred,
721 td)) != 0)
722 return (error);
723 }
724 if (vap->va_size != VNOVAL) {
725 /*
726 * XXX most of the following special cases should be in
727 * callers instead of in N filesystems. The VDIR check
728 * mostly already is.
729 */
730 switch (vp->v_type) {
731 case VDIR:
732 return (EISDIR);
733 case VLNK:
734 case VREG:
735 /*
736 * Truncation should have an effect in these cases.
737 * Disallow it if the filesystem is read-only or
738 * the file is being snapshotted.
739 */
740 if (vp->v_mount->mnt_flag & MNT_RDONLY)
741 return (EROFS);
742 if (IS_SNAPSHOT(ip))
743 return (EPERM);
744 break;
745 default:
746 /*
747 * According to POSIX, the result is unspecified
748 * for file types other than regular files,
749 * directories and shared memory objects. We
750 * don't support shared memory objects in the file
751 * system, and have dubious support for truncating
752 * symlinks. Just ignore the request in other cases.
753 */
754 return (0);
755 }
756 error = vn_rlimit_trunc(vap->va_size, td);
757 if (error != 0)
758 return (error);
759 if ((error = UFS_TRUNCATE(vp, vap->va_size, IO_NORMAL |
760 ((vap->va_vaflags & VA_SYNC) != 0 ? IO_SYNC : 0),
761 cred)) != 0)
762 return (error);
763 }
764 if (vap->va_atime.tv_sec != VNOVAL ||
765 vap->va_mtime.tv_sec != VNOVAL ||
766 vap->va_birthtime.tv_sec != VNOVAL) {
767 if (vp->v_mount->mnt_flag & MNT_RDONLY)
768 return (EROFS);
769 if (IS_SNAPSHOT(ip))
770 return (EPERM);
771 error = vn_utimes_perm(vp, vap, cred, td);
772 if (error != 0)
773 return (error);
774 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_MODIFIED);
775 if (vap->va_atime.tv_sec != VNOVAL) {
776 ip->i_flag &= ~IN_ACCESS;
777 DIP_SET(ip, i_atime, vap->va_atime.tv_sec);
778 DIP_SET(ip, i_atimensec, vap->va_atime.tv_nsec);
779 }
780 if (vap->va_mtime.tv_sec != VNOVAL) {
781 ip->i_flag &= ~IN_UPDATE;
782 DIP_SET(ip, i_mtime, vap->va_mtime.tv_sec);
783 DIP_SET(ip, i_mtimensec, vap->va_mtime.tv_nsec);
784 }
785 if (vap->va_birthtime.tv_sec != VNOVAL && I_IS_UFS2(ip)) {
786 ip->i_din2->di_birthtime = vap->va_birthtime.tv_sec;
787 ip->i_din2->di_birthnsec = vap->va_birthtime.tv_nsec;
788 }
789 error = UFS_UPDATE(vp, 0);
790 if (error)
791 return (error);
792 }
793 error = 0;
794 if (vap->va_mode != (mode_t)VNOVAL) {
795 if (vp->v_mount->mnt_flag & MNT_RDONLY)
796 return (EROFS);
797 if (IS_SNAPSHOT(ip) && (vap->va_mode & (S_IXUSR | S_IWUSR |
798 S_IXGRP | S_IWGRP | S_IXOTH | S_IWOTH)) != 0)
799 return (EPERM);
800 error = ufs_chmod(vp, (int)vap->va_mode, cred, td);
801 }
802 return (error);
803 }
804
805 #ifdef UFS_ACL
806 static int
ufs_update_nfs4_acl_after_mode_change(struct vnode * vp,int mode,int file_owner_id,struct ucred * cred,struct thread * td)807 ufs_update_nfs4_acl_after_mode_change(struct vnode *vp, int mode,
808 int file_owner_id, struct ucred *cred, struct thread *td)
809 {
810 int error;
811 struct acl *aclp;
812
813 aclp = acl_alloc(M_WAITOK);
814 error = ufs_getacl_nfs4_internal(vp, aclp, td);
815 /*
816 * We don't have to handle EOPNOTSUPP here, as the filesystem claims
817 * it supports ACLs.
818 */
819 if (error)
820 goto out;
821
822 acl_nfs4_sync_acl_from_mode(aclp, mode, file_owner_id);
823 error = ufs_setacl_nfs4_internal(vp, aclp, td);
824
825 out:
826 acl_free(aclp);
827 return (error);
828 }
829 #endif /* UFS_ACL */
830
831 static int
ufs_mmapped(struct vop_mmapped_args * ap)832 ufs_mmapped(
833 struct vop_mmapped_args /* {
834 struct vnode *a_vp;
835 } */ *ap)
836 {
837 struct vnode *vp;
838 struct inode *ip;
839 struct mount *mp;
840
841 vp = ap->a_vp;
842 ip = VTOI(vp);
843 mp = vp->v_mount;
844
845 if ((mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
846 UFS_INODE_SET_FLAG_SHARED(ip, IN_ACCESS);
847 /*
848 * XXXKIB No UFS_UPDATE(ap->a_vp, 0) there.
849 */
850 return (0);
851 }
852
853 /*
854 * Change the mode on a file.
855 * Inode must be locked before calling.
856 */
857 static int
ufs_chmod(struct vnode * vp,int mode,struct ucred * cred,struct thread * td)858 ufs_chmod(struct vnode *vp, int mode, struct ucred *cred, struct thread *td)
859 {
860 struct inode *ip = VTOI(vp);
861 int newmode, error;
862
863 /*
864 * To modify the permissions on a file, must possess VADMIN
865 * for that file.
866 */
867 if ((error = VOP_ACCESSX(vp, VWRITE_ACL, cred, td)))
868 return (error);
869 /*
870 * Privileged processes may set the sticky bit on non-directories,
871 * as well as set the setgid bit on a file with a group that the
872 * process is not a member of. Both of these are allowed in
873 * jail(8).
874 */
875 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
876 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE))
877 return (EFTYPE);
878 }
879 if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) {
880 error = priv_check_cred(cred, PRIV_VFS_SETGID);
881 if (error)
882 return (error);
883 }
884
885 /*
886 * Deny setting setuid if we are not the file owner.
887 */
888 if ((mode & ISUID) && ip->i_uid != cred->cr_uid) {
889 error = priv_check_cred(cred, PRIV_VFS_ADMIN);
890 if (error)
891 return (error);
892 }
893
894 newmode = ip->i_mode & ~ALLPERMS;
895 newmode |= (mode & ALLPERMS);
896 UFS_INODE_SET_MODE(ip, newmode);
897 DIP_SET(ip, i_mode, ip->i_mode);
898 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
899 #ifdef UFS_ACL
900 if ((vp->v_mount->mnt_flag & MNT_NFS4ACLS) != 0)
901 error = ufs_update_nfs4_acl_after_mode_change(vp, mode, ip->i_uid, cred, td);
902 #endif
903 if (error == 0 && (ip->i_flag & IN_CHANGE) != 0)
904 error = UFS_UPDATE(vp, 0);
905
906 return (error);
907 }
908
909 /*
910 * Perform chown operation on inode ip;
911 * inode must be locked prior to call.
912 */
913 static int
ufs_chown(struct vnode * vp,uid_t uid,gid_t gid,struct ucred * cred,struct thread * td)914 ufs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
915 struct thread *td)
916 {
917 struct inode *ip = VTOI(vp);
918 uid_t ouid;
919 gid_t ogid;
920 int error = 0;
921 #ifdef QUOTA
922 int i;
923 ufs2_daddr_t change;
924 #endif
925
926 if (uid == (uid_t)VNOVAL)
927 uid = ip->i_uid;
928 if (gid == (gid_t)VNOVAL)
929 gid = ip->i_gid;
930 /*
931 * To modify the ownership of a file, must possess VADMIN for that
932 * file.
933 */
934 if ((error = VOP_ACCESSX(vp, VWRITE_OWNER, cred, td)))
935 return (error);
936 /*
937 * To change the owner of a file, or change the group of a file to a
938 * group of which we are not a member, the caller must have
939 * privilege.
940 */
941 if (((uid != ip->i_uid && uid != cred->cr_uid) ||
942 (gid != ip->i_gid && !groupmember(gid, cred))) &&
943 (error = priv_check_cred(cred, PRIV_VFS_CHOWN)))
944 return (error);
945 ogid = ip->i_gid;
946 ouid = ip->i_uid;
947 #ifdef QUOTA
948 if ((error = getinoquota(ip)) != 0)
949 return (error);
950 if (ouid == uid) {
951 dqrele(vp, ip->i_dquot[USRQUOTA]);
952 ip->i_dquot[USRQUOTA] = NODQUOT;
953 }
954 if (ogid == gid) {
955 dqrele(vp, ip->i_dquot[GRPQUOTA]);
956 ip->i_dquot[GRPQUOTA] = NODQUOT;
957 }
958 change = DIP(ip, i_blocks);
959 (void) chkdq(ip, -change, cred, CHOWN|FORCE);
960 (void) chkiq(ip, -1, cred, CHOWN|FORCE);
961 for (i = 0; i < MAXQUOTAS; i++) {
962 dqrele(vp, ip->i_dquot[i]);
963 ip->i_dquot[i] = NODQUOT;
964 }
965 #endif
966 ip->i_gid = gid;
967 DIP_SET(ip, i_gid, gid);
968 ip->i_uid = uid;
969 DIP_SET(ip, i_uid, uid);
970 #ifdef QUOTA
971 if ((error = getinoquota(ip)) == 0) {
972 if (ouid == uid) {
973 dqrele(vp, ip->i_dquot[USRQUOTA]);
974 ip->i_dquot[USRQUOTA] = NODQUOT;
975 }
976 if (ogid == gid) {
977 dqrele(vp, ip->i_dquot[GRPQUOTA]);
978 ip->i_dquot[GRPQUOTA] = NODQUOT;
979 }
980 if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
981 if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
982 goto good;
983 else
984 (void) chkdq(ip, -change, cred, CHOWN|FORCE);
985 }
986 for (i = 0; i < MAXQUOTAS; i++) {
987 dqrele(vp, ip->i_dquot[i]);
988 ip->i_dquot[i] = NODQUOT;
989 }
990 }
991 ip->i_gid = ogid;
992 DIP_SET(ip, i_gid, ogid);
993 ip->i_uid = ouid;
994 DIP_SET(ip, i_uid, ouid);
995 if (getinoquota(ip) == 0) {
996 if (ouid == uid) {
997 dqrele(vp, ip->i_dquot[USRQUOTA]);
998 ip->i_dquot[USRQUOTA] = NODQUOT;
999 }
1000 if (ogid == gid) {
1001 dqrele(vp, ip->i_dquot[GRPQUOTA]);
1002 ip->i_dquot[GRPQUOTA] = NODQUOT;
1003 }
1004 (void) chkdq(ip, change, cred, FORCE|CHOWN);
1005 (void) chkiq(ip, 1, cred, FORCE|CHOWN);
1006 (void) getinoquota(ip);
1007 }
1008 return (error);
1009 good:
1010 if (getinoquota(ip))
1011 panic("ufs_chown: lost quota");
1012 #endif /* QUOTA */
1013 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
1014 if ((ip->i_mode & (ISUID | ISGID)) && (ouid != uid || ogid != gid)) {
1015 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID)) {
1016 UFS_INODE_SET_MODE(ip, ip->i_mode & ~(ISUID | ISGID));
1017 DIP_SET(ip, i_mode, ip->i_mode);
1018 }
1019 }
1020 error = UFS_UPDATE(vp, 0);
1021 return (error);
1022 }
1023
1024 static int
ufs_remove(struct vop_remove_args * ap)1025 ufs_remove(
1026 struct vop_remove_args /* {
1027 struct vnode *a_dvp;
1028 struct vnode *a_vp;
1029 struct componentname *a_cnp;
1030 } */ *ap)
1031 {
1032 struct inode *ip;
1033 struct vnode *vp = ap->a_vp;
1034 struct vnode *dvp = ap->a_dvp;
1035 int error;
1036 struct thread *td;
1037
1038 td = curthread;
1039 ip = VTOI(vp);
1040 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1041 (VTOI(dvp)->i_flags & APPEND))
1042 return (EPERM);
1043 if (DOINGSUJ(dvp)) {
1044 error = softdep_prelink(dvp, vp, ap->a_cnp);
1045 if (error != 0) {
1046 MPASS(error == ERELOOKUP);
1047 return (error);
1048 }
1049 }
1050
1051 #ifdef UFS_GJOURNAL
1052 ufs_gjournal_orphan(vp);
1053 #endif
1054 error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, false);
1055 if (ip->i_nlink <= 0)
1056 vp->v_vflag |= VV_NOSYNC;
1057 if (IS_SNAPSHOT(ip)) {
1058 /*
1059 * Avoid deadlock where another thread is trying to
1060 * update the inodeblock for dvp and is waiting on
1061 * snaplk. Temporary unlock the vnode lock for the
1062 * unlinked file and sync the directory. This should
1063 * allow vput() of the directory to not block later on
1064 * while holding the snapshot vnode locked, assuming
1065 * that the directory hasn't been unlinked too.
1066 */
1067 VOP_UNLOCK(vp);
1068 (void) VOP_FSYNC(dvp, MNT_WAIT, td);
1069 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1070 }
1071 return (error);
1072 }
1073
1074 static void
print_bad_link_count(const char * funcname,struct vnode * dvp)1075 print_bad_link_count(const char *funcname, struct vnode *dvp)
1076 {
1077 struct inode *dip;
1078
1079 dip = VTOI(dvp);
1080 uprintf("%s: Bad link count %d on parent inode %jd in file system %s\n",
1081 funcname, dip->i_effnlink, (intmax_t)dip->i_number,
1082 dvp->v_mount->mnt_stat.f_mntonname);
1083 }
1084
1085 /*
1086 * link vnode call
1087 */
1088 static int
ufs_link(struct vop_link_args * ap)1089 ufs_link(
1090 struct vop_link_args /* {
1091 struct vnode *a_tdvp;
1092 struct vnode *a_vp;
1093 struct componentname *a_cnp;
1094 } */ *ap)
1095 {
1096 struct vnode *vp = ap->a_vp;
1097 struct vnode *tdvp = ap->a_tdvp;
1098 struct componentname *cnp = ap->a_cnp;
1099 struct inode *ip;
1100 struct direct newdir;
1101 int error;
1102
1103 if (DOINGSUJ(tdvp)) {
1104 error = softdep_prelink(tdvp, vp, cnp);
1105 if (error != 0) {
1106 MPASS(error == ERELOOKUP);
1107 return (error);
1108 }
1109 }
1110
1111 if (VTOI(tdvp)->i_effnlink < 2) {
1112 print_bad_link_count("ufs_link", tdvp);
1113 error = EINVAL;
1114 goto out;
1115 }
1116 error = ufs_sync_nlink(vp, tdvp);
1117 if (error != 0)
1118 goto out;
1119 ip = VTOI(vp);
1120
1121 /*
1122 * The file may have been removed after namei dropped the original
1123 * lock.
1124 */
1125 if (ip->i_effnlink == 0) {
1126 error = ENOENT;
1127 goto out;
1128 }
1129 if (ip->i_flags & (IMMUTABLE | APPEND)) {
1130 error = EPERM;
1131 goto out;
1132 }
1133
1134 ip->i_effnlink++;
1135 ip->i_nlink++;
1136 DIP_SET_NLINK(ip, ip->i_nlink);
1137 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
1138 if (DOINGSOFTDEP(vp))
1139 softdep_setup_link(VTOI(tdvp), ip);
1140 error = UFS_UPDATE(vp, !DOINGSOFTDEP(vp) && !DOINGASYNC(vp));
1141 if (!error) {
1142 ufs_makedirentry(ip, cnp, &newdir);
1143 error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL);
1144 }
1145
1146 if (error) {
1147 ip->i_effnlink--;
1148 ip->i_nlink--;
1149 DIP_SET_NLINK(ip, ip->i_nlink);
1150 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
1151 if (DOINGSOFTDEP(vp))
1152 softdep_revert_link(VTOI(tdvp), ip);
1153 }
1154 out:
1155 return (error);
1156 }
1157
1158 /*
1159 * whiteout vnode call
1160 */
1161 static int
ufs_whiteout(struct vop_whiteout_args * ap)1162 ufs_whiteout(
1163 struct vop_whiteout_args /* {
1164 struct vnode *a_dvp;
1165 struct componentname *a_cnp;
1166 int a_flags;
1167 } */ *ap)
1168 {
1169 struct vnode *dvp = ap->a_dvp;
1170 struct componentname *cnp = ap->a_cnp;
1171 struct direct newdir;
1172 int error = 0;
1173
1174 if (DOINGSUJ(dvp) && (ap->a_flags == CREATE ||
1175 ap->a_flags == DELETE)) {
1176 error = softdep_prelink(dvp, NULL, cnp);
1177 if (error != 0) {
1178 MPASS(error == ERELOOKUP);
1179 return (error);
1180 }
1181 }
1182
1183 switch (ap->a_flags) {
1184 case LOOKUP:
1185 /* 4.4 format directories support whiteout operations */
1186 if (!OFSFMT(dvp))
1187 return (0);
1188 return (EOPNOTSUPP);
1189
1190 case CREATE:
1191 /* create a new directory whiteout */
1192 #ifdef INVARIANTS
1193 if (OFSFMT(dvp))
1194 panic("ufs_whiteout: old format filesystem");
1195 #endif
1196
1197 newdir.d_ino = UFS_WINO;
1198 newdir.d_namlen = cnp->cn_namelen;
1199 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
1200 newdir.d_type = DT_WHT;
1201 error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL);
1202 break;
1203
1204 case DELETE:
1205 /* remove an existing directory whiteout */
1206 #ifdef INVARIANTS
1207 if (OFSFMT(dvp))
1208 panic("ufs_whiteout: old format filesystem");
1209 #endif
1210
1211 cnp->cn_flags &= ~DOWHITEOUT;
1212 error = ufs_dirremove(dvp, NULL, cnp->cn_flags, false);
1213 break;
1214 default:
1215 panic("ufs_whiteout: unknown op");
1216 }
1217 return (error);
1218 }
1219
1220 static volatile int rename_restarts;
1221 SYSCTL_INT(_vfs_ufs, OID_AUTO, rename_restarts, CTLFLAG_RD,
1222 __DEVOLATILE(int *, &rename_restarts), 0,
1223 "Times rename had to restart due to lock contention");
1224
1225 /*
1226 * Rename system call.
1227 * rename("foo", "bar");
1228 * is essentially
1229 * unlink("bar");
1230 * link("foo", "bar");
1231 * unlink("foo");
1232 * but ``atomically''. Can't do full commit without saving state in the
1233 * inode on disk which isn't feasible at this time. Best we can do is
1234 * always guarantee the target exists.
1235 *
1236 * Basic algorithm is:
1237 *
1238 * 1) Bump link count on source while we're linking it to the
1239 * target. This also ensure the inode won't be deleted out
1240 * from underneath us while we work (it may be truncated by
1241 * a concurrent `trunc' or `open' for creation).
1242 * 2) Link source to destination. If destination already exists,
1243 * delete it first.
1244 * 3) Unlink source reference to inode if still around. If a
1245 * directory was moved and the parent of the destination
1246 * is different from the source, patch the ".." entry in the
1247 * directory.
1248 */
1249 static int
ufs_rename(struct vop_rename_args * ap)1250 ufs_rename(
1251 struct vop_rename_args /* {
1252 struct vnode *a_fdvp;
1253 struct vnode *a_fvp;
1254 struct componentname *a_fcnp;
1255 struct vnode *a_tdvp;
1256 struct vnode *a_tvp;
1257 struct componentname *a_tcnp;
1258 u_int a_flags;
1259 } */ *ap)
1260 {
1261 struct vnode *tvp = ap->a_tvp;
1262 struct vnode *tdvp = ap->a_tdvp;
1263 struct vnode *fvp = ap->a_fvp;
1264 struct vnode *fdvp = ap->a_fdvp;
1265 struct vnode *nvp;
1266 struct componentname *tcnp = ap->a_tcnp;
1267 struct componentname *fcnp = ap->a_fcnp;
1268 struct thread *td = curthread;
1269 struct inode *fip, *tip, *tdp, *fdp;
1270 struct direct newdir;
1271 off_t endoff;
1272 int doingdirectory;
1273 u_int newparent;
1274 int error = 0;
1275 struct mount *mp;
1276 ino_t ino;
1277 seqc_t fdvp_s, fvp_s, tdvp_s, tvp_s;
1278 bool want_seqc_end;
1279
1280 want_seqc_end = false;
1281
1282 endoff = 0;
1283 mp = tdvp->v_mount;
1284 VOP_UNLOCK(tdvp);
1285 if (tvp && tvp != tdvp)
1286 VOP_UNLOCK(tvp);
1287 /*
1288 * Check for cross-device rename.
1289 */
1290 if ((fvp->v_mount != tdvp->v_mount) ||
1291 (tvp && (fvp->v_mount != tvp->v_mount))) {
1292 error = EXDEV;
1293 mp = NULL;
1294 goto releout;
1295 }
1296
1297 if ((ap->a_flags & ~(AT_RENAME_NOREPLACE)) != 0) {
1298 error = EOPNOTSUPP;
1299 mp = NULL;
1300 goto releout;
1301 }
1302
1303 fdvp_s = fvp_s = tdvp_s = tvp_s = SEQC_MOD;
1304 relock:
1305 /*
1306 * We need to acquire 2 to 4 locks depending on whether tvp is NULL
1307 * and fdvp and tdvp are the same directory. Subsequently we need
1308 * to double-check all paths and in the directory rename case we
1309 * need to verify that we are not creating a directory loop. To
1310 * handle this we acquire all but fdvp using non-blocking
1311 * acquisitions. If we fail to acquire any lock in the path we will
1312 * drop all held locks, acquire the new lock in a blocking fashion,
1313 * and then release it and restart the rename. This acquire/release
1314 * step ensures that we do not spin on a lock waiting for release.
1315 */
1316 error = vn_lock(fdvp, LK_EXCLUSIVE);
1317 if (error)
1318 goto releout;
1319 if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1320 VOP_UNLOCK(fdvp);
1321 error = vn_lock(tdvp, LK_EXCLUSIVE);
1322 if (error)
1323 goto releout;
1324 VOP_UNLOCK(tdvp);
1325 atomic_add_int(&rename_restarts, 1);
1326 goto relock;
1327 }
1328 /*
1329 * Re-resolve fvp to be certain it still exists and fetch the
1330 * correct vnode.
1331 */
1332 error = ufs_lookup_ino(fdvp, NULL, fcnp, &ino);
1333 if (error) {
1334 VOP_UNLOCK(fdvp);
1335 VOP_UNLOCK(tdvp);
1336 goto releout;
1337 }
1338 error = VFS_VGET(mp, ino, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
1339 if (error) {
1340 VOP_UNLOCK(fdvp);
1341 VOP_UNLOCK(tdvp);
1342 if (error != EBUSY)
1343 goto releout;
1344 error = VFS_VGET(mp, ino, LK_EXCLUSIVE, &nvp);
1345 if (error != 0)
1346 goto releout;
1347 VOP_UNLOCK(nvp);
1348 vrele(fvp);
1349 fvp = nvp;
1350 atomic_add_int(&rename_restarts, 1);
1351 goto relock;
1352 }
1353 vrele(fvp);
1354 fvp = nvp;
1355 /*
1356 * Re-resolve tvp and acquire the vnode lock if present.
1357 */
1358 error = ufs_lookup_ino(tdvp, NULL, tcnp, &ino);
1359 if (error != 0 && error != EJUSTRETURN) {
1360 VOP_UNLOCK(fdvp);
1361 VOP_UNLOCK(tdvp);
1362 VOP_UNLOCK(fvp);
1363 goto releout;
1364 }
1365 /*
1366 * If tvp disappeared we just carry on.
1367 */
1368 if (error == EJUSTRETURN && tvp != NULL) {
1369 vrele(tvp);
1370 tvp = NULL;
1371 }
1372 /*
1373 * Get the tvp ino if the lookup succeeded. We may have to restart
1374 * if the non-blocking acquire fails.
1375 */
1376 if (error == 0) {
1377 nvp = NULL;
1378 error = VFS_VGET(mp, ino, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
1379 if (tvp)
1380 vrele(tvp);
1381 tvp = nvp;
1382 if (error) {
1383 VOP_UNLOCK(fdvp);
1384 VOP_UNLOCK(tdvp);
1385 VOP_UNLOCK(fvp);
1386 if (error != EBUSY)
1387 goto releout;
1388 error = VFS_VGET(mp, ino, LK_EXCLUSIVE, &nvp);
1389 if (error != 0)
1390 goto releout;
1391 vput(nvp);
1392 atomic_add_int(&rename_restarts, 1);
1393 goto relock;
1394 }
1395 }
1396
1397 if (tvp != NULL && (ap->a_flags & AT_RENAME_NOREPLACE) != 0) {
1398 error = EEXIST;
1399 goto unlockout;
1400 }
1401
1402 if (DOINGSUJ(fdvp) &&
1403 (seqc_in_modify(fdvp_s) || !vn_seqc_consistent(fdvp, fdvp_s) ||
1404 seqc_in_modify(fvp_s) || !vn_seqc_consistent(fvp, fvp_s) ||
1405 seqc_in_modify(tdvp_s) || !vn_seqc_consistent(tdvp, tdvp_s) ||
1406 (tvp != NULL && (seqc_in_modify(tvp_s) ||
1407 !vn_seqc_consistent(tvp, tvp_s))))) {
1408 error = softdep_prerename(fdvp, fvp, tdvp, tvp);
1409 if (error != 0)
1410 goto releout;
1411 }
1412
1413 fdp = VTOI(fdvp);
1414 fip = VTOI(fvp);
1415 tdp = VTOI(tdvp);
1416 tip = NULL;
1417 if (tvp)
1418 tip = VTOI(tvp);
1419 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1420 (VTOI(tdvp)->i_flags & APPEND))) {
1421 error = EPERM;
1422 goto unlockout;
1423 }
1424 /*
1425 * Renaming a file to itself has no effect. The upper layers should
1426 * not call us in that case. However, things could change after
1427 * we drop the locks above.
1428 */
1429 if (fvp == tvp) {
1430 error = 0;
1431 goto unlockout;
1432 }
1433 doingdirectory = 0;
1434 newparent = 0;
1435 ino = fip->i_number;
1436 if (fip->i_nlink >= UFS_LINK_MAX) {
1437 if (!DOINGSOFTDEP(fvp) || fip->i_effnlink >= UFS_LINK_MAX) {
1438 error = EMLINK;
1439 goto unlockout;
1440 }
1441 vfs_ref(mp);
1442 MPASS(!want_seqc_end);
1443 VOP_UNLOCK(fdvp);
1444 VOP_UNLOCK(fvp);
1445 vref(tdvp);
1446 if (tvp != NULL)
1447 vref(tvp);
1448 VOP_VPUT_PAIR(tdvp, &tvp, true);
1449 error = ufs_sync_nlink1(mp);
1450 vrele(fdvp);
1451 vrele(fvp);
1452 vrele(tdvp);
1453 if (tvp != NULL)
1454 vrele(tvp);
1455 return (error);
1456 }
1457 if ((fip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
1458 || (fdp->i_flags & APPEND)) {
1459 error = EPERM;
1460 goto unlockout;
1461 }
1462 if ((fip->i_mode & IFMT) == IFDIR) {
1463 /*
1464 * Avoid ".", "..", and aliases of "." for obvious reasons.
1465 */
1466 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1467 fdp == fip ||
1468 (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT) {
1469 error = EINVAL;
1470 goto unlockout;
1471 }
1472 if (fdp->i_number != tdp->i_number)
1473 newparent = tdp->i_number;
1474 doingdirectory = 1;
1475 }
1476 if ((fvp->v_type == VDIR && fvp->v_mountedhere != NULL) ||
1477 (tvp != NULL && tvp->v_type == VDIR &&
1478 tvp->v_mountedhere != NULL)) {
1479 error = EXDEV;
1480 goto unlockout;
1481 }
1482
1483 /*
1484 * If ".." must be changed (ie the directory gets a new
1485 * parent) then the source directory must not be in the
1486 * directory hierarchy above the target, as this would
1487 * orphan everything below the source directory. Also
1488 * the user must have write permission in the source so
1489 * as to be able to change "..".
1490 */
1491 if (doingdirectory && newparent != 0) {
1492 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, curthread);
1493 if (error)
1494 goto unlockout;
1495
1496 error = ufs_checkpath(ino, fdp->i_number, tdp, tcnp->cn_cred,
1497 &ino);
1498 /*
1499 * We encountered a lock that we have to wait for. Unlock
1500 * everything else and VGET before restarting.
1501 */
1502 if (ino) {
1503 VOP_UNLOCK(fdvp);
1504 VOP_UNLOCK(fvp);
1505 VOP_UNLOCK(tdvp);
1506 if (tvp)
1507 VOP_UNLOCK(tvp);
1508 error = VFS_VGET(mp, ino, LK_SHARED, &nvp);
1509 if (error == 0)
1510 vput(nvp);
1511 atomic_add_int(&rename_restarts, 1);
1512 goto relock;
1513 }
1514 if (error)
1515 goto unlockout;
1516 }
1517 if (fip->i_effnlink == 0 || fdp->i_effnlink == 0 ||
1518 tdp->i_effnlink == 0)
1519 panic("Bad effnlink fip %p, fdp %p, tdp %p", fip, fdp, tdp);
1520
1521 if (tvp != NULL)
1522 vn_seqc_write_begin(tvp);
1523 vn_seqc_write_begin(tdvp);
1524 vn_seqc_write_begin(fvp);
1525 vn_seqc_write_begin(fdvp);
1526 want_seqc_end = true;
1527
1528 /*
1529 * 1) Bump link count while we're moving stuff
1530 * around. If we crash somewhere before
1531 * completing our work, the link count
1532 * may be wrong, but correctable.
1533 */
1534 fip->i_effnlink++;
1535 fip->i_nlink++;
1536 DIP_SET_NLINK(fip, fip->i_nlink);
1537 UFS_INODE_SET_FLAG(fip, IN_CHANGE);
1538 if (DOINGSOFTDEP(fvp))
1539 softdep_setup_link(tdp, fip);
1540 error = UFS_UPDATE(fvp, !DOINGSOFTDEP(fvp) && !DOINGASYNC(fvp));
1541 if (error)
1542 goto bad;
1543
1544 /*
1545 * 2) If target doesn't exist, link the target
1546 * to the source and unlink the source.
1547 * Otherwise, rewrite the target directory
1548 * entry to reference the source inode and
1549 * expunge the original entry's existence.
1550 */
1551 if (tip == NULL) {
1552 if (ITODEV(tdp) != ITODEV(fip))
1553 panic("ufs_rename: EXDEV");
1554 if (doingdirectory && newparent != 0) {
1555 /*
1556 * Account for ".." in new directory.
1557 * When source and destination have the same
1558 * parent we don't adjust the link count. The
1559 * actual link modification is completed when
1560 * .. is rewritten below.
1561 */
1562 if (tdp->i_nlink >= UFS_LINK_MAX) {
1563 fip->i_effnlink--;
1564 fip->i_nlink--;
1565 DIP_SET_NLINK(fip, fip->i_nlink);
1566 UFS_INODE_SET_FLAG(fip, IN_CHANGE);
1567 if (DOINGSOFTDEP(fvp))
1568 softdep_revert_link(tdp, fip);
1569 if (!DOINGSOFTDEP(tdvp) ||
1570 tdp->i_effnlink >= UFS_LINK_MAX) {
1571 error = EMLINK;
1572 goto unlockout;
1573 }
1574 MPASS(want_seqc_end);
1575 if (tvp != NULL)
1576 vn_seqc_write_end(tvp);
1577 vn_seqc_write_end(tdvp);
1578 vn_seqc_write_end(fvp);
1579 vn_seqc_write_end(fdvp);
1580 want_seqc_end = false;
1581 vfs_ref(mp);
1582 VOP_UNLOCK(fdvp);
1583 VOP_UNLOCK(fvp);
1584 vref(tdvp);
1585 if (tvp != NULL)
1586 vref(tvp);
1587 VOP_VPUT_PAIR(tdvp, &tvp, true);
1588 error = ufs_sync_nlink1(mp);
1589 vrele(fdvp);
1590 vrele(fvp);
1591 vrele(tdvp);
1592 if (tvp != NULL)
1593 vrele(tvp);
1594 return (error);
1595 }
1596 }
1597 ufs_makedirentry(fip, tcnp, &newdir);
1598 error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL);
1599 if (error)
1600 goto bad;
1601 /* Setup tdvp for directory compaction if needed. */
1602 if (I_COUNT(tdp) != 0 && I_ENDOFF(tdp) != 0 &&
1603 I_ENDOFF(tdp) < tdp->i_size)
1604 endoff = I_ENDOFF(tdp);
1605 } else {
1606 if (ITODEV(tip) != ITODEV(tdp) || ITODEV(tip) != ITODEV(fip))
1607 panic("ufs_rename: EXDEV");
1608 /*
1609 * Short circuit rename(foo, foo).
1610 */
1611 if (tip->i_number == fip->i_number)
1612 panic("ufs_rename: same file");
1613 /*
1614 * If the parent directory is "sticky", then the caller
1615 * must possess VADMIN for the parent directory, or the
1616 * destination of the rename. This implements append-only
1617 * directories.
1618 */
1619 if ((tdp->i_mode & S_ISTXT) &&
1620 VOP_ACCESS(tdvp, VADMIN, tcnp->cn_cred, td) &&
1621 VOP_ACCESS(tvp, VADMIN, tcnp->cn_cred, td)) {
1622 error = EPERM;
1623 goto bad;
1624 }
1625 /*
1626 * Target must be empty if a directory and have no links
1627 * to it. Also, ensure source and target are compatible
1628 * (both directories, or both not directories).
1629 */
1630 if ((tip->i_mode & IFMT) == IFDIR) {
1631 if ((tip->i_effnlink > 2) ||
1632 !ufs_dirempty(tip, tdp->i_number, tcnp->cn_cred,
1633 (tcnp->cn_flags & IGNOREWHITEOUT) != 0)) {
1634 error = ENOTEMPTY;
1635 goto bad;
1636 }
1637 if (!doingdirectory) {
1638 error = ENOTDIR;
1639 goto bad;
1640 }
1641 cache_purge(tdvp);
1642 } else if (doingdirectory) {
1643 error = EISDIR;
1644 goto bad;
1645 }
1646 if (doingdirectory) {
1647 if (newparent == 0) {
1648 tdp->i_effnlink--;
1649 if (DOINGSOFTDEP(tdvp))
1650 softdep_change_linkcnt(tdp);
1651 }
1652 tip->i_effnlink--;
1653 if (DOINGSOFTDEP(tvp))
1654 softdep_change_linkcnt(tip);
1655 }
1656 error = ufs_dirrewrite(tdp, tip, fip->i_number,
1657 IFTODT(fip->i_mode), (doingdirectory && newparent != 0) ?
1658 newparent : doingdirectory);
1659 if (error) {
1660 if (doingdirectory) {
1661 if (newparent == 0) {
1662 tdp->i_effnlink++;
1663 if (DOINGSOFTDEP(tdvp))
1664 softdep_change_linkcnt(tdp);
1665 }
1666 tip->i_effnlink++;
1667 if (DOINGSOFTDEP(tvp))
1668 softdep_change_linkcnt(tip);
1669 }
1670 goto bad;
1671 }
1672 if (doingdirectory && !DOINGSOFTDEP(tvp)) {
1673 /*
1674 * The only stuff left in the directory is "."
1675 * and "..". The "." reference is inconsequential
1676 * since we are quashing it. We have removed the "."
1677 * reference and the reference in the parent directory,
1678 * but there may be other hard links. The soft
1679 * dependency code will arrange to do these operations
1680 * after the parent directory entry has been deleted on
1681 * disk, so when running with that code we avoid doing
1682 * them now.
1683 */
1684 if (newparent == 0) {
1685 tdp->i_nlink--;
1686 DIP_SET_NLINK(tdp, tdp->i_nlink);
1687 UFS_INODE_SET_FLAG(tdp, IN_CHANGE);
1688 }
1689 tip->i_nlink--;
1690 DIP_SET_NLINK(tip, tip->i_nlink);
1691 UFS_INODE_SET_FLAG(tip, IN_CHANGE);
1692 }
1693 }
1694
1695 /*
1696 * 3) Unlink the source. We have to resolve the path again to
1697 * fixup the directory offset and count for ufs_dirremove.
1698 */
1699 if (fdvp == tdvp) {
1700 error = ufs_lookup_ino(fdvp, NULL, fcnp, &ino);
1701 if (error)
1702 panic("ufs_rename: from entry went away!");
1703 if (ino != fip->i_number)
1704 panic("ufs_rename: ino mismatch %ju != %ju\n",
1705 (uintmax_t)ino, (uintmax_t)fip->i_number);
1706 }
1707 /*
1708 * If the source is a directory with a
1709 * new parent, the link count of the old
1710 * parent directory must be decremented
1711 * and ".." set to point to the new parent.
1712 */
1713 if (doingdirectory && newparent != 0) {
1714 /*
1715 * Set the directory depth based on its new parent.
1716 */
1717 DIP_SET(fip, i_dirdepth, DIP(tdp, i_dirdepth) + 1);
1718 /*
1719 * If tip exists we simply use its link, otherwise we must
1720 * add a new one.
1721 */
1722 if (tip == NULL) {
1723 tdp->i_effnlink++;
1724 tdp->i_nlink++;
1725 DIP_SET_NLINK(tdp, tdp->i_nlink);
1726 UFS_INODE_SET_FLAG(tdp, IN_CHANGE);
1727 if (DOINGSOFTDEP(tdvp))
1728 softdep_setup_dotdot_link(tdp, fip);
1729 error = UFS_UPDATE(tdvp, !DOINGSOFTDEP(tdvp) &&
1730 !DOINGASYNC(tdvp));
1731 /* Don't go to bad here as the new link exists. */
1732 if (error)
1733 goto unlockout;
1734 } else if (DOINGSUJ(tdvp))
1735 /* Journal must account for each new link. */
1736 softdep_setup_dotdot_link(tdp, fip);
1737 SET_I_OFFSET(fip, mastertemplate.dot_reclen);
1738 if (ufs_dirrewrite(fip, fdp, newparent, DT_DIR, 0) != 0)
1739 ufs_dirbad(fip, mastertemplate.dot_reclen,
1740 "rename: missing .. entry");
1741 cache_purge(fdvp);
1742 }
1743 error = ufs_dirremove(fdvp, fip, fcnp->cn_flags, false);
1744 /*
1745 * The kern_renameat() looks up the fvp using the DELETE flag, which
1746 * causes the removal of the name cache entry for fvp.
1747 * As the relookup of the fvp is done in two steps:
1748 * ufs_lookup_ino() and then VFS_VGET(), another thread might do a
1749 * normal lookup of the from name just before the VFS_VGET() call,
1750 * causing the cache entry to be re-instantiated.
1751 *
1752 * The same issue also applies to tvp if it exists as
1753 * otherwise we may have a stale name cache entry for the new
1754 * name that references the old i-node if it has other links
1755 * or open file descriptors.
1756 */
1757 cache_vop_rename(fdvp, fvp, tdvp, tvp, fcnp, tcnp);
1758
1759 unlockout:
1760 if (want_seqc_end) {
1761 if (tvp != NULL)
1762 vn_seqc_write_end(tvp);
1763 vn_seqc_write_end(tdvp);
1764 vn_seqc_write_end(fvp);
1765 vn_seqc_write_end(fdvp);
1766 }
1767
1768 vput(fdvp);
1769 vput(fvp);
1770
1771 /*
1772 * If compaction or fsync was requested do it in
1773 * ffs_vput_pair() now that other locks are no longer needed.
1774 */
1775 if (error == 0 && endoff != 0) {
1776 UFS_INODE_SET_FLAG(tdp, IN_ENDOFF);
1777 SET_I_ENDOFF(tdp, endoff);
1778 }
1779 VOP_VPUT_PAIR(tdvp, &tvp, true);
1780 return (error);
1781
1782 bad:
1783 fip->i_effnlink--;
1784 fip->i_nlink--;
1785 DIP_SET_NLINK(fip, fip->i_nlink);
1786 UFS_INODE_SET_FLAG(fip, IN_CHANGE);
1787 if (DOINGSOFTDEP(fvp))
1788 softdep_revert_link(tdp, fip);
1789 goto unlockout;
1790
1791 releout:
1792 if (want_seqc_end) {
1793 if (tvp != NULL)
1794 vn_seqc_write_end(tvp);
1795 vn_seqc_write_end(tdvp);
1796 vn_seqc_write_end(fvp);
1797 vn_seqc_write_end(fdvp);
1798 }
1799
1800 vrele(fdvp);
1801 vrele(fvp);
1802 vrele(tdvp);
1803 if (tvp)
1804 vrele(tvp);
1805
1806 return (error);
1807 }
1808
1809 #ifdef UFS_ACL
1810 static int
ufs_do_posix1e_acl_inheritance_dir(struct vnode * dvp,struct vnode * tvp,mode_t dmode,struct ucred * cred,struct thread * td)1811 ufs_do_posix1e_acl_inheritance_dir(struct vnode *dvp, struct vnode *tvp,
1812 mode_t dmode, struct ucred *cred, struct thread *td)
1813 {
1814 int error;
1815 struct inode *ip = VTOI(tvp);
1816 struct acl *dacl, *acl;
1817
1818 acl = acl_alloc(M_WAITOK);
1819 dacl = acl_alloc(M_WAITOK);
1820
1821 /*
1822 * Retrieve default ACL from parent, if any.
1823 */
1824 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cred, td);
1825 switch (error) {
1826 case 0:
1827 /*
1828 * Retrieved a default ACL, so merge mode and ACL if
1829 * necessary. If the ACL is empty, fall through to
1830 * the "not defined or available" case.
1831 */
1832 if (acl->acl_cnt != 0) {
1833 dmode = acl_posix1e_newfilemode(dmode, acl);
1834 UFS_INODE_SET_MODE(ip, dmode);
1835 DIP_SET(ip, i_mode, dmode);
1836 *dacl = *acl;
1837 ufs_sync_acl_from_inode(ip, acl);
1838 break;
1839 }
1840 /* FALLTHROUGH */
1841
1842 case EOPNOTSUPP:
1843 /*
1844 * Just use the mode as-is.
1845 */
1846 UFS_INODE_SET_MODE(ip, dmode);
1847 DIP_SET(ip, i_mode, dmode);
1848 error = 0;
1849 goto out;
1850
1851 default:
1852 goto out;
1853 }
1854
1855 /*
1856 * XXX: If we abort now, will Soft Updates notify the extattr
1857 * code that the EAs for the file need to be released?
1858 */
1859 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cred, td);
1860 if (error == 0)
1861 error = VOP_SETACL(tvp, ACL_TYPE_DEFAULT, dacl, cred, td);
1862 switch (error) {
1863 case 0:
1864 break;
1865
1866 case EOPNOTSUPP:
1867 /*
1868 * XXX: This should not happen, as EOPNOTSUPP above
1869 * was supposed to free acl.
1870 */
1871 printf("ufs_mkdir: VOP_GETACL() but no VOP_SETACL()\n");
1872 /*
1873 panic("ufs_mkdir: VOP_GETACL() but no VOP_SETACL()");
1874 */
1875 break;
1876
1877 default:
1878 goto out;
1879 }
1880
1881 out:
1882 acl_free(acl);
1883 acl_free(dacl);
1884
1885 return (error);
1886 }
1887
1888 static int
ufs_do_posix1e_acl_inheritance_file(struct vnode * dvp,struct vnode * tvp,mode_t mode,struct ucred * cred,struct thread * td)1889 ufs_do_posix1e_acl_inheritance_file(struct vnode *dvp, struct vnode *tvp,
1890 mode_t mode, struct ucred *cred, struct thread *td)
1891 {
1892 int error;
1893 struct inode *ip = VTOI(tvp);
1894 struct acl *acl;
1895
1896 acl = acl_alloc(M_WAITOK);
1897
1898 /*
1899 * Retrieve default ACL for parent, if any.
1900 */
1901 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cred, td);
1902 switch (error) {
1903 case 0:
1904 /*
1905 * Retrieved a default ACL, so merge mode and ACL if
1906 * necessary.
1907 */
1908 if (acl->acl_cnt != 0) {
1909 /*
1910 * Two possible ways for default ACL to not
1911 * be present. First, the EA can be
1912 * undefined, or second, the default ACL can
1913 * be blank. If it's blank, fall through to
1914 * the it's not defined case.
1915 */
1916 mode = acl_posix1e_newfilemode(mode, acl);
1917 UFS_INODE_SET_MODE(ip, mode);
1918 DIP_SET(ip, i_mode, mode);
1919 ufs_sync_acl_from_inode(ip, acl);
1920 break;
1921 }
1922 /* FALLTHROUGH */
1923
1924 case EOPNOTSUPP:
1925 /*
1926 * Just use the mode as-is.
1927 */
1928 UFS_INODE_SET_MODE(ip, mode);
1929 DIP_SET(ip, i_mode, mode);
1930 error = 0;
1931 goto out;
1932
1933 default:
1934 goto out;
1935 }
1936
1937 /*
1938 * XXX: If we abort now, will Soft Updates notify the extattr
1939 * code that the EAs for the file need to be released?
1940 */
1941 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cred, td);
1942 switch (error) {
1943 case 0:
1944 break;
1945
1946 case EOPNOTSUPP:
1947 /*
1948 * XXX: This should not happen, as EOPNOTSUPP above was
1949 * supposed to free acl.
1950 */
1951 printf("ufs_do_posix1e_acl_inheritance_file: VOP_GETACL() "
1952 "but no VOP_SETACL()\n");
1953 /* panic("ufs_do_posix1e_acl_inheritance_file: VOP_GETACL() "
1954 "but no VOP_SETACL()"); */
1955 break;
1956
1957 default:
1958 goto out;
1959 }
1960
1961 out:
1962 acl_free(acl);
1963
1964 return (error);
1965 }
1966
1967 static int
ufs_do_nfs4_acl_inheritance(struct vnode * dvp,struct vnode * tvp,mode_t child_mode,struct ucred * cred,struct thread * td)1968 ufs_do_nfs4_acl_inheritance(struct vnode *dvp, struct vnode *tvp,
1969 mode_t child_mode, struct ucred *cred, struct thread *td)
1970 {
1971 int error;
1972 struct acl *parent_aclp, *child_aclp;
1973
1974 parent_aclp = acl_alloc(M_WAITOK);
1975 child_aclp = acl_alloc(M_WAITOK | M_ZERO);
1976
1977 error = ufs_getacl_nfs4_internal(dvp, parent_aclp, td);
1978 if (error)
1979 goto out;
1980 acl_nfs4_compute_inherited_acl(parent_aclp, child_aclp,
1981 child_mode, VTOI(tvp)->i_uid, tvp->v_type == VDIR);
1982 error = ufs_setacl_nfs4_internal(tvp, child_aclp, td);
1983 if (error)
1984 goto out;
1985 out:
1986 acl_free(parent_aclp);
1987 acl_free(child_aclp);
1988
1989 return (error);
1990 }
1991 #endif
1992
1993 /*
1994 * Mkdir system call
1995 */
1996 static int
ufs_mkdir(struct vop_mkdir_args * ap)1997 ufs_mkdir(
1998 struct vop_mkdir_args /* {
1999 struct vnode *a_dvp;
2000 struct vnode **a_vpp;
2001 struct componentname *a_cnp;
2002 struct vattr *a_vap;
2003 } */ *ap)
2004 {
2005 struct vnode *dvp = ap->a_dvp;
2006 struct vattr *vap = ap->a_vap;
2007 struct componentname *cnp = ap->a_cnp;
2008 struct inode *ip, *dp;
2009 struct vnode *tvp;
2010 struct buf *bp;
2011 struct dirtemplate dirtemplate, *dtp;
2012 struct direct newdir;
2013 int error, dmode;
2014 long blkoff;
2015
2016 dp = VTOI(dvp);
2017 error = ufs_sync_nlink(dvp, NULL);
2018 if (error != 0)
2019 goto out;
2020 dmode = vap->va_mode & 0777;
2021 dmode |= IFDIR;
2022
2023 /*
2024 * Must simulate part of ufs_makeinode here to acquire the inode,
2025 * but not have it entered in the parent directory. The entry is
2026 * made later after writing "." and ".." entries.
2027 */
2028 if (dp->i_effnlink < 2) {
2029 print_bad_link_count("ufs_mkdir", dvp);
2030 error = EINVAL;
2031 goto out;
2032 }
2033
2034 if (DOINGSUJ(dvp)) {
2035 error = softdep_prelink(dvp, NULL, cnp);
2036 if (error != 0) {
2037 MPASS(error == ERELOOKUP);
2038 return (error);
2039 }
2040 }
2041
2042 error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, &tvp);
2043 if (error)
2044 goto out;
2045 vn_seqc_write_begin(tvp);
2046 ip = VTOI(tvp);
2047 ip->i_gid = dp->i_gid;
2048 DIP_SET(ip, i_gid, dp->i_gid);
2049 #ifdef SUIDDIR
2050 {
2051 #ifdef QUOTA
2052 struct ucred ucred, *ucp;
2053 ucp = cnp->cn_cred;
2054 #endif
2055 /*
2056 * If we are hacking owners here, (only do this where told to)
2057 * and we are not giving it TO root, (would subvert quotas)
2058 * then go ahead and give it to the other user.
2059 * The new directory also inherits the SUID bit.
2060 * If user's UID and dir UID are the same,
2061 * 'give it away' so that the SUID is still forced on.
2062 */
2063 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2064 (dp->i_mode & ISUID) && dp->i_uid) {
2065 dmode |= ISUID;
2066 ip->i_uid = dp->i_uid;
2067 DIP_SET(ip, i_uid, dp->i_uid);
2068 #ifdef QUOTA
2069 if (dp->i_uid != cnp->cn_cred->cr_uid) {
2070 /*
2071 * Make sure the correct user gets charged
2072 * for the space.
2073 * Make a dummy credential for the victim.
2074 * XXX This seems to never be accessed out of
2075 * our context so a stack variable is ok.
2076 */
2077 ucred.cr_ref = 1;
2078 ucred.cr_uid = ip->i_uid;
2079 ucred.cr_gid = dp->i_gid;
2080 ucred.cr_ngroups = 0;
2081 ucp = &ucred;
2082 }
2083 #endif
2084 } else {
2085 ip->i_uid = cnp->cn_cred->cr_uid;
2086 DIP_SET(ip, i_uid, ip->i_uid);
2087 }
2088 #ifdef QUOTA
2089 if ((error = getinoquota(ip)) ||
2090 (error = chkiq(ip, 1, ucp, 0))) {
2091 if (DOINGSOFTDEP(tvp))
2092 softdep_revert_link(dp, ip);
2093 UFS_VFREE(tvp, ip->i_number, dmode);
2094 vn_seqc_write_end(tvp);
2095 vgone(tvp);
2096 vput(tvp);
2097 return (error);
2098 }
2099 #endif
2100 }
2101 #else /* !SUIDDIR */
2102 ip->i_uid = cnp->cn_cred->cr_uid;
2103 DIP_SET(ip, i_uid, ip->i_uid);
2104 #ifdef QUOTA
2105 if ((error = getinoquota(ip)) ||
2106 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2107 if (DOINGSOFTDEP(tvp))
2108 softdep_revert_link(dp, ip);
2109 UFS_VFREE(tvp, ip->i_number, dmode);
2110 vn_seqc_write_end(tvp);
2111 vgone(tvp);
2112 vput(tvp);
2113 return (error);
2114 }
2115 #endif
2116 #endif /* !SUIDDIR */
2117 UFS_INODE_SET_FLAG(ip, IN_ACCESS | IN_CHANGE | IN_UPDATE);
2118 UFS_INODE_SET_MODE(ip, dmode);
2119 DIP_SET(ip, i_mode, dmode);
2120 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
2121 ip->i_effnlink = 2;
2122 ip->i_nlink = 2;
2123 DIP_SET_NLINK(ip, 2);
2124 DIP_SET(ip, i_dirdepth, DIP(dp,i_dirdepth) + 1);
2125
2126 if (cnp->cn_flags & ISWHITEOUT) {
2127 ip->i_flags |= UF_OPAQUE;
2128 DIP_SET(ip, i_flags, ip->i_flags);
2129 }
2130
2131 /*
2132 * Bump link count in parent directory to reflect work done below.
2133 * Should be done before reference is created so cleanup is
2134 * possible if we crash.
2135 */
2136 dp->i_effnlink++;
2137 dp->i_nlink++;
2138 DIP_SET_NLINK(dp, dp->i_nlink);
2139 UFS_INODE_SET_FLAG(dp, IN_CHANGE);
2140 if (DOINGSOFTDEP(dvp))
2141 softdep_setup_mkdir(dp, ip);
2142 error = UFS_UPDATE(dvp, !DOINGSOFTDEP(dvp) && !DOINGASYNC(dvp));
2143 if (error)
2144 goto bad;
2145 #ifdef MAC
2146 if (dvp->v_mount->mnt_flag & MNT_MULTILABEL) {
2147 error = mac_vnode_create_extattr(cnp->cn_cred, dvp->v_mount,
2148 dvp, tvp, cnp);
2149 if (error)
2150 goto bad;
2151 }
2152 #endif
2153 #ifdef UFS_ACL
2154 if (dvp->v_mount->mnt_flag & MNT_ACLS) {
2155 error = ufs_do_posix1e_acl_inheritance_dir(dvp, tvp, dmode,
2156 cnp->cn_cred, curthread);
2157 if (error)
2158 goto bad;
2159 } else if (dvp->v_mount->mnt_flag & MNT_NFS4ACLS) {
2160 error = ufs_do_nfs4_acl_inheritance(dvp, tvp, dmode,
2161 cnp->cn_cred, curthread);
2162 if (error)
2163 goto bad;
2164 }
2165 #endif /* !UFS_ACL */
2166
2167 /*
2168 * Initialize directory with "." and ".." from static template.
2169 */
2170 if (!OFSFMT(dvp))
2171 dtp = &mastertemplate;
2172 else
2173 dtp = (struct dirtemplate *)&omastertemplate;
2174 dirtemplate = *dtp;
2175 dirtemplate.dot_ino = ip->i_number;
2176 dirtemplate.dotdot_ino = dp->i_number;
2177 vnode_pager_setsize(tvp, DIRBLKSIZ);
2178 if ((error = UFS_BALLOC(tvp, (off_t)0, DIRBLKSIZ, cnp->cn_cred,
2179 BA_CLRBUF, &bp)) != 0)
2180 goto bad;
2181 ip->i_size = DIRBLKSIZ;
2182 DIP_SET(ip, i_size, DIRBLKSIZ);
2183 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
2184 bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
2185 if (DOINGSOFTDEP(tvp)) {
2186 /*
2187 * Ensure that the entire newly allocated block is a
2188 * valid directory so that future growth within the
2189 * block does not have to ensure that the block is
2190 * written before the inode.
2191 */
2192 blkoff = DIRBLKSIZ;
2193 while (blkoff < bp->b_bcount) {
2194 ((struct direct *)
2195 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
2196 blkoff += DIRBLKSIZ;
2197 }
2198 }
2199 if ((error = UFS_UPDATE(tvp, !DOINGSOFTDEP(tvp) &&
2200 !DOINGASYNC(tvp))) != 0) {
2201 (void)bwrite(bp);
2202 goto bad;
2203 }
2204 /*
2205 * Directory set up, now install its entry in the parent directory.
2206 *
2207 * If we are not doing soft dependencies, then we must write out the
2208 * buffer containing the new directory body before entering the new
2209 * name in the parent. If we are doing soft dependencies, then the
2210 * buffer containing the new directory body will be passed to and
2211 * released in the soft dependency code after the code has attached
2212 * an appropriate ordering dependency to the buffer which ensures that
2213 * the buffer is written before the new name is written in the parent.
2214 */
2215 if (DOINGASYNC(dvp))
2216 bdwrite(bp);
2217 else if (!DOINGSOFTDEP(dvp) && ((error = bwrite(bp))))
2218 goto bad;
2219 ufs_makedirentry(ip, cnp, &newdir);
2220 error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
2221
2222 bad:
2223 if (error == 0) {
2224 *ap->a_vpp = tvp;
2225 vn_seqc_write_end(tvp);
2226 } else {
2227 dp->i_effnlink--;
2228 dp->i_nlink--;
2229 DIP_SET_NLINK(dp, dp->i_nlink);
2230 UFS_INODE_SET_FLAG(dp, IN_CHANGE);
2231 /*
2232 * No need to do an explicit VOP_TRUNCATE here, vrele will
2233 * do this for us because we set the link count to 0.
2234 */
2235 ip->i_effnlink = 0;
2236 ip->i_nlink = 0;
2237 DIP_SET_NLINK(ip, 0);
2238 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
2239 if (DOINGSOFTDEP(tvp))
2240 softdep_revert_mkdir(dp, ip);
2241 vn_seqc_write_end(tvp);
2242 vgone(tvp);
2243 vput(tvp);
2244 }
2245 out:
2246 return (error);
2247 }
2248
2249 /*
2250 * Rmdir system call.
2251 */
2252 static int
ufs_rmdir(struct vop_rmdir_args * ap)2253 ufs_rmdir(
2254 struct vop_rmdir_args /* {
2255 struct vnode *a_dvp;
2256 struct vnode *a_vp;
2257 struct componentname *a_cnp;
2258 } */ *ap)
2259 {
2260 struct vnode *vp = ap->a_vp;
2261 struct vnode *dvp = ap->a_dvp;
2262 struct componentname *cnp = ap->a_cnp;
2263 struct inode *ip, *dp;
2264 int error;
2265
2266 ip = VTOI(vp);
2267 dp = VTOI(dvp);
2268
2269 /*
2270 * Do not remove a directory that is in the process of being renamed.
2271 * Verify the directory is empty (and valid). Rmdir ".." will not be
2272 * valid since ".." will contain a reference to the current directory
2273 * and thus be non-empty. Do not allow the removal of mounted on
2274 * directories (this can happen when an NFS exported filesystem
2275 * tries to remove a locally mounted on directory).
2276 */
2277 error = 0;
2278 if (dp->i_effnlink <= 2) {
2279 if (dp->i_effnlink == 2)
2280 print_bad_link_count("ufs_rmdir", dvp);
2281 error = EINVAL;
2282 goto out;
2283 }
2284 if (!ufs_dirempty(ip, dp->i_number, cnp->cn_cred,
2285 (cnp->cn_flags & IGNOREWHITEOUT) != 0)) {
2286 error = ENOTEMPTY;
2287 goto out;
2288 }
2289 if ((dp->i_flags & APPEND)
2290 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
2291 error = EPERM;
2292 goto out;
2293 }
2294 if (vp->v_mountedhere != 0) {
2295 error = EINVAL;
2296 goto out;
2297 }
2298 if (DOINGSUJ(dvp)) {
2299 error = softdep_prelink(dvp, vp, cnp);
2300 if (error != 0) {
2301 MPASS(error == ERELOOKUP);
2302 return (error);
2303 }
2304 }
2305
2306 #ifdef UFS_GJOURNAL
2307 ufs_gjournal_orphan(vp);
2308 #endif
2309 /*
2310 * Delete reference to directory before purging
2311 * inode. If we crash in between, the directory
2312 * will be reattached to lost+found,
2313 */
2314 dp->i_effnlink--;
2315 ip->i_effnlink--;
2316 if (DOINGSOFTDEP(vp))
2317 softdep_setup_rmdir(dp, ip);
2318 error = ufs_dirremove(dvp, ip, cnp->cn_flags, true);
2319 if (error) {
2320 dp->i_effnlink++;
2321 ip->i_effnlink++;
2322 if (DOINGSOFTDEP(vp))
2323 softdep_revert_rmdir(dp, ip);
2324 goto out;
2325 }
2326 /*
2327 * The only stuff left in the directory is "." and "..". The "."
2328 * reference is inconsequential since we are quashing it. The soft
2329 * dependency code will arrange to do these operations after
2330 * the parent directory entry has been deleted on disk, so
2331 * when running with that code we avoid doing them now.
2332 */
2333 if (!DOINGSOFTDEP(vp)) {
2334 dp->i_nlink--;
2335 DIP_SET_NLINK(dp, dp->i_nlink);
2336 UFS_INODE_SET_FLAG(dp, IN_CHANGE);
2337 error = UFS_UPDATE(dvp, 0);
2338 ip->i_nlink--;
2339 DIP_SET_NLINK(ip, ip->i_nlink);
2340 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
2341 }
2342 cache_vop_rmdir(dvp, vp);
2343 #ifdef UFS_DIRHASH
2344 /* Kill any active hash; i_effnlink == 0, so it will not come back. */
2345 if (ip->i_dirhash != NULL)
2346 ufsdirhash_free(ip);
2347 #endif
2348 out:
2349 return (error);
2350 }
2351
2352 /*
2353 * symlink -- make a symbolic link
2354 */
2355 static int
ufs_symlink(struct vop_symlink_args * ap)2356 ufs_symlink(
2357 struct vop_symlink_args /* {
2358 struct vnode *a_dvp;
2359 struct vnode **a_vpp;
2360 struct componentname *a_cnp;
2361 struct vattr *a_vap;
2362 const char *a_target;
2363 } */ *ap)
2364 {
2365 struct vnode *vp, **vpp = ap->a_vpp;
2366 struct inode *ip;
2367 int len, error;
2368
2369 error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
2370 vpp, ap->a_cnp, "ufs_symlink");
2371 if (error)
2372 return (error);
2373 vp = *vpp;
2374 len = strlen(ap->a_target);
2375 if (len < VFSTOUFS(vp->v_mount)->um_maxsymlinklen) {
2376 ip = VTOI(vp);
2377 bcopy(ap->a_target, DIP(ip, i_shortlink), len);
2378 ip->i_size = len;
2379 DIP_SET(ip, i_size, len);
2380 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
2381 error = UFS_UPDATE(vp, 0);
2382 } else
2383 error = vn_rdwr(UIO_WRITE, vp, __DECONST(void *, ap->a_target),
2384 len, (off_t)0, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
2385 ap->a_cnp->cn_cred, NOCRED, NULL, NULL);
2386 if (error)
2387 vput(vp);
2388 return (error);
2389 }
2390
2391 /*
2392 * Vnode op for reading directories.
2393 */
2394 int
ufs_readdir(struct vop_readdir_args * ap)2395 ufs_readdir(
2396 struct vop_readdir_args /* {
2397 struct vnode *a_vp;
2398 struct uio *a_uio;
2399 struct ucred *a_cred;
2400 int *a_eofflag;
2401 int *a_ncookies;
2402 uint64_t **a_cookies;
2403 } */ *ap)
2404 {
2405 struct vnode *vp = ap->a_vp;
2406 struct uio *uio = ap->a_uio;
2407 struct buf *bp;
2408 struct inode *ip;
2409 struct direct *dp, *edp;
2410 uint64_t *cookies;
2411 struct dirent dstdp;
2412 off_t offset, startoffset;
2413 size_t readcnt, skipcnt;
2414 ssize_t startresid;
2415 uint64_t ncookies;
2416 int error;
2417
2418 if (uio->uio_offset < 0)
2419 return (EINVAL);
2420 ip = VTOI(vp);
2421 if (ip->i_effnlink == 0) {
2422 *ap->a_eofflag = 1;
2423 return (0);
2424 }
2425 if (ap->a_ncookies != NULL) {
2426 if (uio->uio_resid < 0)
2427 ncookies = 0;
2428 else
2429 ncookies = uio->uio_resid;
2430 if (uio->uio_offset >= ip->i_size)
2431 ncookies = 0;
2432 else if (ip->i_size - uio->uio_offset < ncookies)
2433 ncookies = ip->i_size - uio->uio_offset;
2434 ncookies = ncookies / (offsetof(struct direct, d_name) + 4) + 1;
2435 cookies = malloc(ncookies * sizeof(*cookies), M_TEMP, M_WAITOK);
2436 *ap->a_ncookies = ncookies;
2437 *ap->a_cookies = cookies;
2438 } else {
2439 ncookies = 0;
2440 cookies = NULL;
2441 }
2442 offset = startoffset = uio->uio_offset;
2443 startresid = uio->uio_resid;
2444 error = 0;
2445 while (error == 0 && uio->uio_resid > 0 &&
2446 uio->uio_offset < ip->i_size) {
2447 error = UFS_BLKATOFF(vp, uio->uio_offset, NULL, &bp);
2448 if (error)
2449 break;
2450 if (bp->b_offset + bp->b_bcount > ip->i_size)
2451 readcnt = ip->i_size - bp->b_offset;
2452 else
2453 readcnt = bp->b_bcount;
2454 skipcnt = (size_t)(uio->uio_offset - bp->b_offset) &
2455 ~(size_t)(DIRBLKSIZ - 1);
2456 offset = bp->b_offset + skipcnt;
2457 dp = (struct direct *)&bp->b_data[skipcnt];
2458 edp = (struct direct *)&bp->b_data[readcnt];
2459 while (error == 0 && uio->uio_resid > 0 && dp < edp) {
2460 if (dp->d_reclen <= offsetof(struct direct, d_name) ||
2461 (caddr_t)dp + dp->d_reclen > (caddr_t)edp) {
2462 error = EIO;
2463 break;
2464 }
2465 #if BYTE_ORDER == LITTLE_ENDIAN
2466 /* Old filesystem format. */
2467 if (OFSFMT(vp)) {
2468 dstdp.d_namlen = dp->d_type;
2469 dstdp.d_type = dp->d_namlen;
2470 } else
2471 #endif
2472 {
2473 dstdp.d_namlen = dp->d_namlen;
2474 dstdp.d_type = dp->d_type;
2475 }
2476 if (offsetof(struct direct, d_name) + dstdp.d_namlen >
2477 dp->d_reclen) {
2478 error = EIO;
2479 break;
2480 }
2481 if (offset < startoffset || dp->d_ino == 0)
2482 goto nextentry;
2483 dstdp.d_fileno = dp->d_ino;
2484 dstdp.d_reclen = GENERIC_DIRSIZ(&dstdp);
2485 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
2486 /* NOTE: d_off is the offset of the *next* entry. */
2487 dstdp.d_off = offset + dp->d_reclen;
2488 dirent_terminate(&dstdp);
2489 if (dstdp.d_reclen > uio->uio_resid) {
2490 if (uio->uio_resid == startresid)
2491 error = EINVAL;
2492 else
2493 error = EJUSTRETURN;
2494 break;
2495 }
2496 /* Advance dp. */
2497 error = uiomove((caddr_t)&dstdp, dstdp.d_reclen, uio);
2498 if (error)
2499 break;
2500 if (cookies != NULL) {
2501 KASSERT(ncookies > 0,
2502 ("ufs_readdir: cookies buffer too small"));
2503 *cookies = offset + dp->d_reclen;
2504 cookies++;
2505 ncookies--;
2506 }
2507 nextentry:
2508 offset += dp->d_reclen;
2509 dp = (struct direct *)((caddr_t)dp + dp->d_reclen);
2510 }
2511 bqrelse(bp);
2512 uio->uio_offset = offset;
2513 }
2514 /* We need to correct uio_offset. */
2515 uio->uio_offset = offset;
2516 if (error == EJUSTRETURN)
2517 error = 0;
2518 if (ap->a_ncookies != NULL) {
2519 if (error == 0) {
2520 *ap->a_ncookies -= ncookies;
2521 } else {
2522 free(*ap->a_cookies, M_TEMP);
2523 *ap->a_ncookies = 0;
2524 *ap->a_cookies = NULL;
2525 }
2526 }
2527 if (error == 0 && ap->a_eofflag)
2528 *ap->a_eofflag = ip->i_size <= uio->uio_offset;
2529 return (error);
2530 }
2531
2532 /*
2533 * Return target name of a symbolic link
2534 */
2535 static int
ufs_readlink(struct vop_readlink_args * ap)2536 ufs_readlink(
2537 struct vop_readlink_args /* {
2538 struct vnode *a_vp;
2539 struct uio *a_uio;
2540 struct ucred *a_cred;
2541 } */ *ap)
2542 {
2543 struct vnode *vp = ap->a_vp;
2544 struct inode *ip = VTOI(vp);
2545 doff_t isize;
2546
2547 isize = ip->i_size;
2548 if (isize < VFSTOUFS(vp->v_mount)->um_maxsymlinklen)
2549 return (uiomove(DIP(ip, i_shortlink), isize, ap->a_uio));
2550 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
2551 }
2552
2553 /*
2554 * Calculate the logical to physical mapping if not done already,
2555 * then call the device strategy routine.
2556 *
2557 * In order to be able to swap to a file, the ufs_bmaparray() operation may not
2558 * deadlock on memory. See ufs_bmap() for details.
2559 */
2560 static int
ufs_strategy(struct vop_strategy_args * ap)2561 ufs_strategy(
2562 struct vop_strategy_args /* {
2563 struct vnode *a_vp;
2564 struct buf *a_bp;
2565 } */ *ap)
2566 {
2567 struct buf *bp = ap->a_bp;
2568 struct vnode *vp = ap->a_vp;
2569 ufs2_daddr_t blkno;
2570 int error;
2571
2572 if (bp->b_blkno == bp->b_lblkno) {
2573 error = ufs_bmaparray(vp, bp->b_lblkno, &blkno, bp, NULL, NULL);
2574 bp->b_blkno = blkno;
2575 if (error) {
2576 bp->b_error = error;
2577 bp->b_ioflags |= BIO_ERROR;
2578 bufdone(bp);
2579 return (0);
2580 }
2581 if ((long)bp->b_blkno == -1)
2582 vfs_bio_clrbuf(bp);
2583 }
2584 if ((long)bp->b_blkno == -1) {
2585 bufdone(bp);
2586 return (0);
2587 }
2588 bp->b_iooffset = dbtob(bp->b_blkno);
2589 BO_STRATEGY(VFSTOUFS(vp->v_mount)->um_bo, bp);
2590 return (0);
2591 }
2592
2593 /*
2594 * Print out the contents of an inode.
2595 */
2596 static int
ufs_print(struct vop_print_args * ap)2597 ufs_print(
2598 struct vop_print_args /* {
2599 struct vnode *a_vp;
2600 } */ *ap)
2601 {
2602 struct vnode *vp = ap->a_vp;
2603 struct inode *ip = VTOI(vp);
2604
2605 printf("\tnlink=%d, effnlink=%d, size=%jd", ip->i_nlink,
2606 ip->i_effnlink, (intmax_t)ip->i_size);
2607 if (I_IS_UFS2(ip)) {
2608 if (ip->i_din2 == NULL)
2609 printf(", dinode=NULL (fields omitted)");
2610 else
2611 printf(", extsize=%d", ip->i_din2->di_extsize);
2612 }
2613 printf("\n\tgeneration=%jx, uid=%d, gid=%d, flags=0x%b\n",
2614 (uintmax_t)ip->i_gen, ip->i_uid, ip->i_gid,
2615 (uint32_t)ip->i_flags, PRINT_INODE_FLAGS);
2616 printf("\tino %ju, on dev %s", (intmax_t)ip->i_number,
2617 devtoname(ITODEV(ip)));
2618 if (vp->v_type == VFIFO)
2619 fifo_printinfo(vp);
2620 printf("\n");
2621 return (0);
2622 }
2623
2624 /*
2625 * Close wrapper for fifos.
2626 *
2627 * Update the times on the inode then do device close.
2628 */
2629 static int
ufsfifo_close(struct vop_close_args * ap)2630 ufsfifo_close(
2631 struct vop_close_args /* {
2632 struct vnode *a_vp;
2633 int a_fflag;
2634 struct ucred *a_cred;
2635 struct thread *a_td;
2636 } */ *ap)
2637 {
2638
2639 ufs_close(ap);
2640 return (fifo_specops.vop_close(ap));
2641 }
2642
2643 /*
2644 * Return POSIX pathconf information applicable to ufs filesystems.
2645 */
2646 static int
ufs_pathconf(struct vop_pathconf_args * ap)2647 ufs_pathconf(
2648 struct vop_pathconf_args /* {
2649 struct vnode *a_vp;
2650 int a_name;
2651 int *a_retval;
2652 } */ *ap)
2653 {
2654 int error;
2655
2656 error = 0;
2657 switch (ap->a_name) {
2658 case _PC_LINK_MAX:
2659 *ap->a_retval = UFS_LINK_MAX;
2660 break;
2661 case _PC_NAME_MAX:
2662 *ap->a_retval = UFS_MAXNAMLEN;
2663 break;
2664 case _PC_PIPE_BUF:
2665 if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO)
2666 *ap->a_retval = PIPE_BUF;
2667 else
2668 error = EINVAL;
2669 break;
2670 case _PC_CHOWN_RESTRICTED:
2671 *ap->a_retval = 1;
2672 break;
2673 case _PC_NO_TRUNC:
2674 *ap->a_retval = 1;
2675 break;
2676 #ifdef UFS_ACL
2677 case _PC_ACL_EXTENDED:
2678 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
2679 *ap->a_retval = 1;
2680 else
2681 *ap->a_retval = 0;
2682 break;
2683 case _PC_ACL_NFS4:
2684 if (ap->a_vp->v_mount->mnt_flag & MNT_NFS4ACLS)
2685 *ap->a_retval = 1;
2686 else
2687 *ap->a_retval = 0;
2688 break;
2689 #endif
2690 case _PC_ACL_PATH_MAX:
2691 #ifdef UFS_ACL
2692 if (ap->a_vp->v_mount->mnt_flag & (MNT_ACLS | MNT_NFS4ACLS))
2693 *ap->a_retval = ACL_MAX_ENTRIES;
2694 else
2695 *ap->a_retval = 3;
2696 #else
2697 *ap->a_retval = 3;
2698 #endif
2699 break;
2700 #ifdef MAC
2701 case _PC_MAC_PRESENT:
2702 if (ap->a_vp->v_mount->mnt_flag & MNT_MULTILABEL)
2703 *ap->a_retval = 1;
2704 else
2705 *ap->a_retval = 0;
2706 break;
2707 #endif
2708 case _PC_MIN_HOLE_SIZE:
2709 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2710 break;
2711 case _PC_PRIO_IO:
2712 *ap->a_retval = 0;
2713 break;
2714 case _PC_SYNC_IO:
2715 *ap->a_retval = 0;
2716 break;
2717 case _PC_ALLOC_SIZE_MIN:
2718 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize;
2719 break;
2720 case _PC_FILESIZEBITS:
2721 *ap->a_retval = 64;
2722 break;
2723 case _PC_REC_INCR_XFER_SIZE:
2724 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2725 break;
2726 case _PC_REC_MAX_XFER_SIZE:
2727 *ap->a_retval = -1; /* means ``unlimited'' */
2728 break;
2729 case _PC_REC_MIN_XFER_SIZE:
2730 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2731 break;
2732 case _PC_REC_XFER_ALIGN:
2733 *ap->a_retval = PAGE_SIZE;
2734 break;
2735 case _PC_SYMLINK_MAX:
2736 *ap->a_retval = MAXPATHLEN;
2737 break;
2738 case _PC_HAS_HIDDENSYSTEM:
2739 *ap->a_retval = 1;
2740 break;
2741
2742 default:
2743 error = vop_stdpathconf(ap);
2744 break;
2745 }
2746 return (error);
2747 }
2748
2749 /*
2750 * Initialize the vnode associated with a new inode, handle aliased
2751 * vnodes.
2752 */
2753 int
ufs_vinit(struct mount * mntp,struct vop_vector * fifoops,struct vnode ** vpp)2754 ufs_vinit(struct mount *mntp, struct vop_vector *fifoops, struct vnode **vpp)
2755 {
2756 struct inode *ip;
2757 struct vnode *vp;
2758
2759 vp = *vpp;
2760 ASSERT_VOP_LOCKED(vp, "ufs_vinit");
2761 ip = VTOI(vp);
2762 vp->v_type = IFTOVT(ip->i_mode);
2763 /*
2764 * Only unallocated inodes should be of type VNON.
2765 */
2766 if (ip->i_mode != 0 && vp->v_type == VNON)
2767 return (EINVAL);
2768 if (vp->v_type == VFIFO)
2769 vp->v_op = fifoops;
2770 if (ip->i_number == UFS_ROOTINO)
2771 vp->v_vflag |= VV_ROOT;
2772 *vpp = vp;
2773 return (0);
2774 }
2775
2776 /*
2777 * Allocate a new inode.
2778 * Vnode dvp must be locked.
2779 */
2780 static int
ufs_makeinode(int mode,struct vnode * dvp,struct vnode ** vpp,struct componentname * cnp,const char * callfunc)2781 ufs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
2782 struct componentname *cnp, const char *callfunc)
2783 {
2784 struct inode *ip, *pdir;
2785 struct direct newdir;
2786 struct vnode *tvp;
2787 int error;
2788
2789 pdir = VTOI(dvp);
2790 *vpp = NULL;
2791 if ((mode & IFMT) == 0)
2792 mode |= IFREG;
2793
2794 if (pdir->i_effnlink < 2) {
2795 print_bad_link_count(callfunc, dvp);
2796 return (EINVAL);
2797 }
2798 if (DOINGSUJ(dvp)) {
2799 error = softdep_prelink(dvp, NULL, cnp);
2800 if (error != 0) {
2801 MPASS(error == ERELOOKUP);
2802 return (error);
2803 }
2804 }
2805 error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
2806 if (error)
2807 return (error);
2808 ip = VTOI(tvp);
2809 ip->i_gid = pdir->i_gid;
2810 DIP_SET(ip, i_gid, pdir->i_gid);
2811 #ifdef SUIDDIR
2812 {
2813 #ifdef QUOTA
2814 struct ucred ucred, *ucp;
2815 ucp = cnp->cn_cred;
2816 #endif
2817 /*
2818 * If we are not the owner of the directory,
2819 * and we are hacking owners here, (only do this where told to)
2820 * and we are not giving it TO root, (would subvert quotas)
2821 * then go ahead and give it to the other user.
2822 * Note that this drops off the execute bits for security.
2823 */
2824 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2825 (pdir->i_mode & ISUID) &&
2826 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2827 ip->i_uid = pdir->i_uid;
2828 DIP_SET(ip, i_uid, ip->i_uid);
2829 mode &= ~07111;
2830 #ifdef QUOTA
2831 /*
2832 * Make sure the correct user gets charged
2833 * for the space.
2834 * Quickly knock up a dummy credential for the victim.
2835 * XXX This seems to never be accessed out of our
2836 * context so a stack variable is ok.
2837 */
2838 ucred.cr_ref = 1;
2839 ucred.cr_uid = ip->i_uid;
2840 ucred.cr_gid = pdir->i_gid;
2841 ucred.cr_ngroups = 0;
2842 ucp = &ucred;
2843 #endif
2844 } else {
2845 ip->i_uid = cnp->cn_cred->cr_uid;
2846 DIP_SET(ip, i_uid, ip->i_uid);
2847 }
2848
2849 #ifdef QUOTA
2850 if ((error = getinoquota(ip)) ||
2851 (error = chkiq(ip, 1, ucp, 0))) {
2852 if (DOINGSOFTDEP(tvp))
2853 softdep_revert_link(pdir, ip);
2854 UFS_VFREE(tvp, ip->i_number, mode);
2855 vgone(tvp);
2856 vput(tvp);
2857 return (error);
2858 }
2859 #endif
2860 }
2861 #else /* !SUIDDIR */
2862 ip->i_uid = cnp->cn_cred->cr_uid;
2863 DIP_SET(ip, i_uid, ip->i_uid);
2864 #ifdef QUOTA
2865 if ((error = getinoquota(ip)) ||
2866 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2867 if (DOINGSOFTDEP(tvp))
2868 softdep_revert_link(pdir, ip);
2869 UFS_VFREE(tvp, ip->i_number, mode);
2870 vgone(tvp);
2871 vput(tvp);
2872 return (error);
2873 }
2874 #endif
2875 #endif /* !SUIDDIR */
2876 vn_seqc_write_begin(tvp); /* Mostly to cover asserts */
2877 UFS_INODE_SET_FLAG(ip, IN_ACCESS | IN_CHANGE | IN_UPDATE);
2878 UFS_INODE_SET_MODE(ip, mode);
2879 DIP_SET(ip, i_mode, mode);
2880 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
2881 ip->i_effnlink = 1;
2882 ip->i_nlink = 1;
2883 DIP_SET_NLINK(ip, 1);
2884 if (DOINGSOFTDEP(tvp))
2885 softdep_setup_create(VTOI(dvp), ip);
2886 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2887 priv_check_cred(cnp->cn_cred, PRIV_VFS_SETGID)) {
2888 UFS_INODE_SET_MODE(ip, ip->i_mode & ~ISGID);
2889 DIP_SET(ip, i_mode, ip->i_mode);
2890 }
2891
2892 if (cnp->cn_flags & ISWHITEOUT) {
2893 ip->i_flags |= UF_OPAQUE;
2894 DIP_SET(ip, i_flags, ip->i_flags);
2895 }
2896
2897 /*
2898 * Make sure inode goes to disk before directory entry.
2899 */
2900 error = UFS_UPDATE(tvp, !DOINGSOFTDEP(tvp) && !DOINGASYNC(tvp));
2901 if (error)
2902 goto bad;
2903 #ifdef MAC
2904 if (dvp->v_mount->mnt_flag & MNT_MULTILABEL) {
2905 error = mac_vnode_create_extattr(cnp->cn_cred, dvp->v_mount,
2906 dvp, tvp, cnp);
2907 if (error)
2908 goto bad;
2909 }
2910 #endif
2911 #ifdef UFS_ACL
2912 if (dvp->v_mount->mnt_flag & MNT_ACLS) {
2913 error = ufs_do_posix1e_acl_inheritance_file(dvp, tvp, mode,
2914 cnp->cn_cred, curthread);
2915 if (error)
2916 goto bad;
2917 } else if (dvp->v_mount->mnt_flag & MNT_NFS4ACLS) {
2918 error = ufs_do_nfs4_acl_inheritance(dvp, tvp, mode,
2919 cnp->cn_cred, curthread);
2920 if (error)
2921 goto bad;
2922 }
2923 #endif /* !UFS_ACL */
2924 ufs_makedirentry(ip, cnp, &newdir);
2925 error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2926 if (error)
2927 goto bad;
2928 vn_seqc_write_end(tvp);
2929 *vpp = tvp;
2930 return (0);
2931
2932 bad:
2933 /*
2934 * Write error occurred trying to update the inode
2935 * or the directory so must deallocate the inode.
2936 */
2937 ip->i_effnlink = 0;
2938 ip->i_nlink = 0;
2939 DIP_SET_NLINK(ip, 0);
2940 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
2941 if (DOINGSOFTDEP(tvp))
2942 softdep_revert_create(VTOI(dvp), ip);
2943 vn_seqc_write_end(tvp);
2944 vgone(tvp);
2945 vput(tvp);
2946 return (error);
2947 }
2948
2949 static int
ufs_ioctl(struct vop_ioctl_args * ap)2950 ufs_ioctl(struct vop_ioctl_args *ap)
2951 {
2952 struct vnode *vp;
2953 int error;
2954
2955 vp = ap->a_vp;
2956 switch (ap->a_command) {
2957 case FIOSEEKDATA:
2958 error = vn_lock(vp, LK_EXCLUSIVE);
2959 if (error == 0) {
2960 error = ufs_bmap_seekdata(vp, (off_t *)ap->a_data);
2961 VOP_UNLOCK(vp);
2962 } else
2963 error = EBADF;
2964 return (error);
2965 case FIOSEEKHOLE:
2966 return (vn_bmap_seekhole(vp, ap->a_command, (off_t *)ap->a_data,
2967 ap->a_cred));
2968 default:
2969 return (ENOTTY);
2970 }
2971 }
2972
2973 static int
ufs_read_pgcache(struct vop_read_pgcache_args * ap)2974 ufs_read_pgcache(struct vop_read_pgcache_args *ap)
2975 {
2976 struct uio *uio;
2977 struct vnode *vp;
2978
2979 uio = ap->a_uio;
2980 vp = ap->a_vp;
2981 VNPASS((vn_irflag_read(vp) & VIRF_PGREAD) != 0, vp);
2982
2983 if (uio->uio_resid > ptoa(io_hold_cnt) || uio->uio_offset < 0 ||
2984 (ap->a_ioflag & IO_DIRECT) != 0)
2985 return (EJUSTRETURN);
2986 return (vn_read_from_obj(vp, uio));
2987 }
2988
2989 /* Global vfs data structures for ufs. */
2990 struct vop_vector ufs_vnodeops = {
2991 .vop_default = &default_vnodeops,
2992 .vop_fsync = VOP_PANIC,
2993 .vop_read = VOP_PANIC,
2994 .vop_reallocblks = VOP_PANIC,
2995 .vop_write = VOP_PANIC,
2996 .vop_accessx = ufs_accessx,
2997 .vop_bmap = ufs_bmap,
2998 .vop_fplookup_vexec = ufs_fplookup_vexec,
2999 .vop_fplookup_symlink = VOP_EAGAIN,
3000 .vop_cachedlookup = ufs_lookup,
3001 .vop_close = ufs_close,
3002 .vop_create = ufs_create,
3003 .vop_stat = ufs_stat,
3004 .vop_getattr = ufs_getattr,
3005 .vop_inactive = ufs_inactive,
3006 .vop_ioctl = ufs_ioctl,
3007 .vop_link = ufs_link,
3008 .vop_lookup = vfs_cache_lookup,
3009 .vop_mmapped = ufs_mmapped,
3010 .vop_mkdir = ufs_mkdir,
3011 .vop_mknod = ufs_mknod,
3012 .vop_need_inactive = ufs_need_inactive,
3013 .vop_open = ufs_open,
3014 .vop_pathconf = ufs_pathconf,
3015 .vop_poll = vop_stdpoll,
3016 .vop_print = ufs_print,
3017 .vop_read_pgcache = ufs_read_pgcache,
3018 .vop_readdir = ufs_readdir,
3019 .vop_readlink = ufs_readlink,
3020 .vop_reclaim = ufs_reclaim,
3021 .vop_remove = ufs_remove,
3022 .vop_rename = ufs_rename,
3023 .vop_rmdir = ufs_rmdir,
3024 .vop_setattr = ufs_setattr,
3025 #ifdef MAC
3026 .vop_setlabel = vop_stdsetlabel_ea,
3027 #endif
3028 .vop_strategy = ufs_strategy,
3029 .vop_symlink = ufs_symlink,
3030 .vop_whiteout = ufs_whiteout,
3031 #ifdef UFS_EXTATTR
3032 .vop_getextattr = ufs_getextattr,
3033 .vop_deleteextattr = ufs_deleteextattr,
3034 .vop_setextattr = ufs_setextattr,
3035 #endif
3036 #ifdef UFS_ACL
3037 .vop_getacl = ufs_getacl,
3038 .vop_setacl = ufs_setacl,
3039 .vop_aclcheck = ufs_aclcheck,
3040 #endif
3041 };
3042 VFS_VOP_VECTOR_REGISTER(ufs_vnodeops);
3043
3044 struct vop_vector ufs_fifoops = {
3045 .vop_default = &fifo_specops,
3046 .vop_fsync = VOP_PANIC,
3047 .vop_accessx = ufs_accessx,
3048 .vop_close = ufsfifo_close,
3049 .vop_getattr = ufs_getattr,
3050 .vop_inactive = ufs_inactive,
3051 .vop_pathconf = ufs_pathconf,
3052 .vop_print = ufs_print,
3053 .vop_read = VOP_PANIC,
3054 .vop_reclaim = ufs_reclaim,
3055 .vop_setattr = ufs_setattr,
3056 #ifdef MAC
3057 .vop_setlabel = vop_stdsetlabel_ea,
3058 #endif
3059 .vop_write = VOP_PANIC,
3060 #ifdef UFS_EXTATTR
3061 .vop_getextattr = ufs_getextattr,
3062 .vop_deleteextattr = ufs_deleteextattr,
3063 .vop_setextattr = ufs_setextattr,
3064 #endif
3065 #ifdef UFS_ACL
3066 .vop_getacl = ufs_getacl,
3067 .vop_setacl = ufs_setacl,
3068 .vop_aclcheck = ufs_aclcheck,
3069 #endif
3070 .vop_fplookup_vexec = VOP_EAGAIN,
3071 .vop_fplookup_symlink = VOP_EAGAIN,
3072 };
3073 VFS_VOP_VECTOR_REGISTER(ufs_fifoops);
3074