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