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