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