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