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