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/refcount.h>
57 #include <sys/unistd.h>
58 #include <sys/vnode.h>
59 #include <sys/dirent.h>
60 #include <sys/lockf.h>
61 #include <sys/conf.h>
62 #include <sys/acl.h>
63 #include <sys/mac.h>
64 #include <sys/jail.h>
65
66 #include <machine/mutex.h>
67
68 #include <sys/file.h> /* XXX */
69
70 #include <vm/vm.h>
71 #include <vm/vm_extern.h>
72
73 #include <fs/fifofs/fifo.h>
74
75 #include <ufs/ufs/acl.h>
76 #include <ufs/ufs/extattr.h>
77 #include <ufs/ufs/quota.h>
78 #include <ufs/ufs/inode.h>
79 #include <ufs/ufs/dir.h>
80 #include <ufs/ufs/ufsmount.h>
81 #include <ufs/ufs/ufs_extern.h>
82 #ifdef UFS_DIRHASH
83 #include <ufs/ufs/dirhash.h>
84 #endif
85
86 #include <ufs/ffs/ffs_extern.h>
87
88 static vop_access_t ufs_access;
89 static vop_advlock_t ufs_advlock;
90 static vop_advlockasync_t ufs_advlockasync;
91 static int ufs_chmod(struct vnode *, int, struct ucred *, struct thread *);
92 static int ufs_chown(struct vnode *, uid_t, gid_t, struct ucred *, struct thread *);
93 static vop_close_t ufs_close;
94 static vop_create_t ufs_create;
95 static vop_getattr_t ufs_getattr;
96 static vop_link_t ufs_link;
97 static int ufs_makeinode(int mode, struct vnode *, struct vnode **, struct componentname *);
98 static vop_mkdir_t ufs_mkdir;
99 static vop_mknod_t ufs_mknod;
100 static vop_open_t ufs_open;
101 static vop_pathconf_t ufs_pathconf;
102 static vop_print_t ufs_print;
103 static vop_readlink_t ufs_readlink;
104 static vop_remove_t ufs_remove;
105 static vop_rename_t ufs_rename;
106 static vop_rmdir_t ufs_rmdir;
107 static vop_setattr_t ufs_setattr;
108 static vop_strategy_t ufs_strategy;
109 static vop_symlink_t ufs_symlink;
110 static vop_whiteout_t ufs_whiteout;
111 static vop_close_t ufsfifo_close;
112 static vop_kqfilter_t ufsfifo_kqfilter;
113
114 /*
115 * A virgin directory (no blushing please).
116 */
117 static struct dirtemplate mastertemplate = {
118 0, 12, DT_DIR, 1, ".",
119 0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
120 };
121 static struct odirtemplate omastertemplate = {
122 0, 12, 1, ".",
123 0, DIRBLKSIZ - 12, 2, ".."
124 };
125
126 void
127 ufs_itimes(vp)
128 struct vnode *vp;
129 {
130 struct inode *ip;
131 struct timespec ts;
132
133 ip = VTOI(vp);
134 VI_LOCK(vp);
135 if ((vp->v_mount->mnt_flag & MNT_RDONLY) != 0)
136 goto out;
137 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0) {
138 VI_UNLOCK(vp);
139 return;
140 }
141
142 if ((vp->v_type == VBLK || vp->v_type == VCHR) && !DOINGSOFTDEP(vp))
143 ip->i_flag |= IN_LAZYMOD;
144 else if (((vp->v_mount->mnt_kern_flag &
145 (MNTK_SUSPENDED | MNTK_SUSPEND)) == 0) ||
146 (ip->i_flag & (IN_CHANGE | IN_UPDATE)))
147 ip->i_flag |= IN_MODIFIED;
148 else if (ip->i_flag & IN_ACCESS)
149 ip->i_flag |= IN_LAZYACCESS;
150 vfs_timestamp(&ts);
151 if (ip->i_flag & IN_ACCESS) {
152 DIP_SET(ip, i_atime, ts.tv_sec);
153 DIP_SET(ip, i_atimensec, ts.tv_nsec);
154 }
155 if (ip->i_flag & IN_UPDATE) {
156 DIP_SET(ip, i_mtime, ts.tv_sec);
157 DIP_SET(ip, i_mtimensec, ts.tv_nsec);
158 ip->i_modrev++;
159 }
160 if (ip->i_flag & IN_CHANGE) {
161 DIP_SET(ip, i_ctime, ts.tv_sec);
162 DIP_SET(ip, i_ctimensec, ts.tv_nsec);
163 }
164
165 out:
166 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
167 VI_UNLOCK(vp);
168 }
169
170 /*
171 * Create a regular file
172 */
173 static int
174 ufs_create(ap)
175 struct vop_create_args /* {
176 struct vnode *a_dvp;
177 struct vnode **a_vpp;
178 struct componentname *a_cnp;
179 struct vattr *a_vap;
180 } */ *ap;
181 {
182 int error;
183
184 error =
185 ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
186 ap->a_dvp, ap->a_vpp, ap->a_cnp);
187 if (error)
188 return (error);
189 return (0);
190 }
191
192 /*
193 * Mknod vnode call
194 */
195 /* ARGSUSED */
196 static int
197 ufs_mknod(ap)
198 struct vop_mknod_args /* {
199 struct vnode *a_dvp;
200 struct vnode **a_vpp;
201 struct componentname *a_cnp;
202 struct vattr *a_vap;
203 } */ *ap;
204 {
205 struct vattr *vap = ap->a_vap;
206 struct vnode **vpp = ap->a_vpp;
207 struct inode *ip;
208 ino_t ino;
209 int error;
210
211 error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
212 ap->a_dvp, vpp, ap->a_cnp);
213 if (error)
214 return (error);
215 ip = VTOI(*vpp);
216 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
217 if (vap->va_rdev != VNOVAL) {
218 /*
219 * Want to be able to use this to make badblock
220 * inodes, so don't truncate the dev number.
221 */
222 DIP_SET(ip, i_rdev, vap->va_rdev);
223 }
224 /*
225 * Remove inode, then reload it through VFS_VGET so it is
226 * checked to see if it is an alias of an existing entry in
227 * the inode cache. XXX I don't believe this is necessary now.
228 */
229 (*vpp)->v_type = VNON;
230 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
231 vgone(*vpp);
232 vput(*vpp);
233 error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp);
234 if (error) {
235 *vpp = NULL;
236 return (error);
237 }
238 return (0);
239 }
240
241 /*
242 * Open called.
243 */
244 /* ARGSUSED */
245 static int
246 ufs_open(struct vop_open_args *ap)
247 {
248 struct vnode *vp = ap->a_vp;
249 struct inode *ip;
250
251 if (vp->v_type == VCHR || vp->v_type == VBLK)
252 return (EOPNOTSUPP);
253
254 ip = VTOI(vp);
255 /*
256 * Files marked append-only must be opened for appending.
257 */
258 if ((ip->i_flags & APPEND) &&
259 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
260 return (EPERM);
261 vnode_create_vobject_off(vp, DIP(ip, i_size), ap->a_td);
262 return (0);
263 }
264
265 /*
266 * Close called.
267 *
268 * Update the times on the inode.
269 */
270 /* ARGSUSED */
271 static int
272 ufs_close(ap)
273 struct vop_close_args /* {
274 struct vnode *a_vp;
275 int a_fflag;
276 struct ucred *a_cred;
277 struct thread *a_td;
278 } */ *ap;
279 {
280 struct vnode *vp = ap->a_vp;
281 int usecount;
282
283 VI_LOCK(vp);
284 usecount = vp->v_usecount;
285 VI_UNLOCK(vp);
286 if (usecount > 1)
287 ufs_itimes(vp);
288 return (0);
289 }
290
291 static int
292 ufs_access(ap)
293 struct vop_access_args /* {
294 struct vnode *a_vp;
295 int a_mode;
296 struct ucred *a_cred;
297 struct thread *a_td;
298 } */ *ap;
299 {
300 struct vnode *vp = ap->a_vp;
301 struct inode *ip = VTOI(vp);
302 mode_t mode = ap->a_mode;
303 int error;
304 #ifdef QUOTA
305 int relocked;
306 #endif
307 #ifdef UFS_ACL
308 struct acl *acl;
309 #endif
310
311 /*
312 * Disallow write attempts on read-only filesystems;
313 * unless the file is a socket, fifo, or a block or
314 * character device resident on the filesystem.
315 */
316 if (mode & VWRITE) {
317 switch (vp->v_type) {
318 case VDIR:
319 case VLNK:
320 case VREG:
321 if (vp->v_mount->mnt_flag & MNT_RDONLY)
322 return (EROFS);
323 #ifdef QUOTA
324 if (VOP_ISLOCKED(vp, ap->a_td) != LK_EXCLUSIVE) {
325 relocked = 1;
326 vhold(vp);
327 vn_lock(vp, LK_UPGRADE | LK_RETRY, ap->a_td);
328 VI_LOCK(vp);
329 if (vp->v_iflag & VI_DOOMED) {
330 vdropl(vp);
331 error = ENOENT;
332 goto relock;
333 }
334 vdropl(vp);
335 } else
336 relocked = 0;
337 error = getinoquota(ip);
338 relock:
339 if (relocked)
340 vn_lock(vp, LK_DOWNGRADE | LK_RETRY, ap->a_td);
341 if (error != 0)
342 return (error);
343 #endif
344 break;
345 default:
346 break;
347 }
348 }
349
350 /* If immutable bit set, nobody gets to write it. */
351 if ((mode & VWRITE) && (ip->i_flags & (IMMUTABLE | SF_SNAPSHOT)))
352 return (EPERM);
353
354 #ifdef UFS_ACL
355 if ((vp->v_mount->mnt_flag & MNT_ACLS) != 0) {
356 acl = uma_zalloc(acl_zone, M_WAITOK);
357 error = VOP_GETACL(vp, ACL_TYPE_ACCESS, acl, ap->a_cred,
358 ap->a_td);
359 switch (error) {
360 case EOPNOTSUPP:
361 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid,
362 ip->i_gid, ap->a_mode, ap->a_cred, NULL);
363 break;
364 case 0:
365 error = vaccess_acl_posix1e(vp->v_type, ip->i_uid,
366 ip->i_gid, acl, ap->a_mode, ap->a_cred, NULL);
367 break;
368 default:
369 printf(
370 "ufs_access(): Error retrieving ACL on object (%d).\n",
371 error);
372 /*
373 * XXX: Fall back until debugged. Should
374 * eventually possibly log an error, and return
375 * EPERM for safety.
376 */
377 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid,
378 ip->i_gid, ap->a_mode, ap->a_cred, NULL);
379 }
380 uma_zfree(acl_zone, acl);
381 } else
382 #endif /* !UFS_ACL */
383 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
384 ap->a_mode, ap->a_cred, NULL);
385 return (error);
386 }
387
388 /* ARGSUSED */
389 static int
390 ufs_getattr(ap)
391 struct vop_getattr_args /* {
392 struct vnode *a_vp;
393 struct vattr *a_vap;
394 struct ucred *a_cred;
395 struct thread *a_td;
396 } */ *ap;
397 {
398 struct vnode *vp = ap->a_vp;
399 struct inode *ip = VTOI(vp);
400 struct vattr *vap = ap->a_vap;
401
402 ufs_itimes(vp);
403 /*
404 * Copy from inode table
405 */
406 vap->va_fsid = dev2udev(ip->i_dev);
407 vap->va_fileid = ip->i_number;
408 vap->va_mode = ip->i_mode & ~IFMT;
409 vap->va_nlink = ip->i_effnlink;
410 vap->va_uid = ip->i_uid;
411 vap->va_gid = ip->i_gid;
412 if (ip->i_ump->um_fstype == UFS1) {
413 vap->va_rdev = ip->i_din1->di_rdev;
414 vap->va_size = ip->i_din1->di_size;
415 VI_LOCK(vp);
416 vap->va_atime.tv_sec = ip->i_din1->di_atime;
417 vap->va_atime.tv_nsec = ip->i_din1->di_atimensec;
418 VI_UNLOCK(vp);
419 vap->va_mtime.tv_sec = ip->i_din1->di_mtime;
420 vap->va_mtime.tv_nsec = ip->i_din1->di_mtimensec;
421 vap->va_ctime.tv_sec = ip->i_din1->di_ctime;
422 vap->va_ctime.tv_nsec = ip->i_din1->di_ctimensec;
423 vap->va_birthtime.tv_sec = 0;
424 vap->va_birthtime.tv_nsec = 0;
425 vap->va_bytes = dbtob((u_quad_t)ip->i_din1->di_blocks);
426 } else {
427 vap->va_rdev = ip->i_din2->di_rdev;
428 vap->va_size = ip->i_din2->di_size;
429 VI_LOCK(vp);
430 vap->va_atime.tv_sec = ip->i_din2->di_atime;
431 vap->va_atime.tv_nsec = ip->i_din2->di_atimensec;
432 VI_UNLOCK(vp);
433 vap->va_mtime.tv_sec = ip->i_din2->di_mtime;
434 vap->va_mtime.tv_nsec = ip->i_din2->di_mtimensec;
435 vap->va_ctime.tv_sec = ip->i_din2->di_ctime;
436 vap->va_ctime.tv_nsec = ip->i_din2->di_ctimensec;
437 vap->va_birthtime.tv_sec = ip->i_din2->di_birthtime;
438 vap->va_birthtime.tv_nsec = ip->i_din2->di_birthnsec;
439 vap->va_bytes = dbtob((u_quad_t)ip->i_din2->di_blocks);
440 }
441 vap->va_flags = ip->i_flags;
442 vap->va_gen = ip->i_gen;
443 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
444 vap->va_type = IFTOVT(ip->i_mode);
445 vap->va_filerev = ip->i_modrev;
446 return (0);
447 }
448
449 /*
450 * Set attribute vnode op. called from several syscalls
451 */
452 static int
453 ufs_setattr(ap)
454 struct vop_setattr_args /* {
455 struct vnode *a_vp;
456 struct vattr *a_vap;
457 struct ucred *a_cred;
458 struct thread *a_td;
459 } */ *ap;
460 {
461 struct vattr *vap = ap->a_vap;
462 struct vnode *vp = ap->a_vp;
463 struct inode *ip = VTOI(vp);
464 struct ucred *cred = ap->a_cred;
465 struct thread *td = ap->a_td;
466 int error;
467
468 /*
469 * Check for unsettable attributes.
470 */
471 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
472 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
473 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
474 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
475 return (EINVAL);
476 }
477 /*
478 * Mark for update the file's access time for vfs_mark_atime().
479 * We are doing this here to avoid some of the checks done
480 * below -- this operation is done by request of the kernel and
481 * should bypass some security checks. Things like read-only
482 * checks get handled by other levels (e.g., ffs_update()).
483 */
484 if (vap->va_vaflags & VA_MARK_ATIME) {
485 ip->i_flag |= IN_ACCESS;
486 return (0);
487 }
488 if (vap->va_flags != VNOVAL) {
489 if (vp->v_mount->mnt_flag & MNT_RDONLY)
490 return (EROFS);
491 /*
492 * Callers may only modify the file flags on objects they
493 * have VADMIN rights for.
494 */
495 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
496 return (error);
497 /*
498 * Unprivileged processes are not permitted to unset system
499 * flags, or modify flags if any system flags are set.
500 * Privileged non-jail processes may not modify system flags
501 * if securelevel > 0 and any existing system flags are set.
502 * Privileged jail processes behave like privileged non-jail
503 * processes if the security.jail.chflags_allowed sysctl is
504 * is non-zero; otherwise, they behave like unprivileged
505 * processes.
506 */
507 if (!suser_cred(cred,
508 jail_chflags_allowed ? SUSER_ALLOWJAIL : 0)) {
509 if (ip->i_flags
510 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
511 error = securelevel_gt(cred, 0);
512 if (error)
513 return (error);
514 }
515 /* Snapshot flag cannot be set or cleared */
516 if (((vap->va_flags & SF_SNAPSHOT) != 0 &&
517 (ip->i_flags & SF_SNAPSHOT) == 0) ||
518 ((vap->va_flags & SF_SNAPSHOT) == 0 &&
519 (ip->i_flags & SF_SNAPSHOT) != 0))
520 return (EPERM);
521 ip->i_flags = vap->va_flags;
522 DIP_SET(ip, i_flags, vap->va_flags);
523 } else {
524 if (ip->i_flags
525 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
526 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
527 return (EPERM);
528 ip->i_flags &= SF_SETTABLE;
529 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
530 DIP_SET(ip, i_flags, ip->i_flags);
531 }
532 ip->i_flag |= IN_CHANGE;
533 if (vap->va_flags & (IMMUTABLE | APPEND))
534 return (0);
535 }
536 if (ip->i_flags & (IMMUTABLE | APPEND))
537 return (EPERM);
538 /*
539 * Go through the fields and update iff not VNOVAL.
540 */
541 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
542 if (vp->v_mount->mnt_flag & MNT_RDONLY)
543 return (EROFS);
544 if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred,
545 td)) != 0)
546 return (error);
547 }
548 if (vap->va_size != VNOVAL) {
549 /*
550 * XXX most of the following special cases should be in
551 * callers instead of in N filesystems. The VDIR check
552 * mostly already is.
553 */
554 switch (vp->v_type) {
555 case VDIR:
556 return (EISDIR);
557 case VLNK:
558 case VREG:
559 /*
560 * Truncation should have an effect in these cases.
561 * Disallow it if the filesystem is read-only or
562 * the file is being snapshotted.
563 */
564 if (vp->v_mount->mnt_flag & MNT_RDONLY)
565 return (EROFS);
566 if ((ip->i_flags & SF_SNAPSHOT) != 0)
567 return (EPERM);
568 break;
569 default:
570 /*
571 * According to POSIX, the result is unspecified
572 * for file types other than regular files,
573 * directories and shared memory objects. We
574 * don't support shared memory objects in the file
575 * system, and have dubious support for truncating
576 * symlinks. Just ignore the request in other cases.
577 */
578 return (0);
579 }
580 if ((error = UFS_TRUNCATE(vp, vap->va_size, IO_NORMAL,
581 cred, td)) != 0)
582 return (error);
583 }
584 if (vap->va_atime.tv_sec != VNOVAL ||
585 vap->va_mtime.tv_sec != VNOVAL ||
586 vap->va_birthtime.tv_sec != VNOVAL) {
587 if (vp->v_mount->mnt_flag & MNT_RDONLY)
588 return (EROFS);
589 if ((ip->i_flags & SF_SNAPSHOT) != 0)
590 return (EPERM);
591 /*
592 * From utimes(2):
593 * If times is NULL, ... The caller must be the owner of
594 * the file, have permission to write the file, or be the
595 * super-user.
596 * If times is non-NULL, ... The caller must be the owner of
597 * the file or be the super-user.
598 */
599 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)) &&
600 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
601 (error = VOP_ACCESS(vp, VWRITE, cred, td))))
602 return (error);
603 if (vap->va_atime.tv_sec != VNOVAL)
604 ip->i_flag |= IN_ACCESS;
605 if (vap->va_mtime.tv_sec != VNOVAL)
606 ip->i_flag |= IN_CHANGE | IN_UPDATE;
607 if (vap->va_birthtime.tv_sec != VNOVAL &&
608 ip->i_ump->um_fstype == UFS2)
609 ip->i_flag |= IN_MODIFIED;
610 ufs_itimes(vp);
611 if (vap->va_atime.tv_sec != VNOVAL) {
612 DIP_SET(ip, i_atime, vap->va_atime.tv_sec);
613 DIP_SET(ip, i_atimensec, vap->va_atime.tv_nsec);
614 }
615 if (vap->va_mtime.tv_sec != VNOVAL) {
616 DIP_SET(ip, i_mtime, vap->va_mtime.tv_sec);
617 DIP_SET(ip, i_mtimensec, vap->va_mtime.tv_nsec);
618 }
619 if (vap->va_birthtime.tv_sec != VNOVAL &&
620 ip->i_ump->um_fstype == UFS2) {
621 ip->i_din2->di_birthtime = vap->va_birthtime.tv_sec;
622 ip->i_din2->di_birthnsec = vap->va_birthtime.tv_nsec;
623 }
624 error = UFS_UPDATE(vp, 0);
625 if (error)
626 return (error);
627 }
628 error = 0;
629 if (vap->va_mode != (mode_t)VNOVAL) {
630 if (vp->v_mount->mnt_flag & MNT_RDONLY)
631 return (EROFS);
632 if ((ip->i_flags & SF_SNAPSHOT) != 0 && (vap->va_mode &
633 (S_IXUSR | S_IWUSR | S_IXGRP | S_IWGRP | S_IXOTH | S_IWOTH)))
634 return (EPERM);
635 error = ufs_chmod(vp, (int)vap->va_mode, cred, td);
636 }
637 return (error);
638 }
639
640 /*
641 * Change the mode on a file.
642 * Inode must be locked before calling.
643 */
644 static int
645 ufs_chmod(vp, mode, cred, td)
646 struct vnode *vp;
647 int mode;
648 struct ucred *cred;
649 struct thread *td;
650 {
651 struct inode *ip = VTOI(vp);
652 int error;
653
654 /*
655 * To modify the ownership of a file, must possess VADMIN for that
656 * file.
657 */
658 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
659 return (error);
660 /*
661 * Privileged processes may set the sticky bit on non-directories,
662 * as well as set the setgid bit on a file with a group that the
663 * process is not a member of. Both of these are allowed in
664 * jail(8).
665 */
666 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
667 if (suser_cred(cred, SUSER_ALLOWJAIL))
668 return (EFTYPE);
669 }
670 if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) {
671 error = suser_cred(cred, SUSER_ALLOWJAIL);
672 if (error)
673 return (error);
674 }
675 ip->i_mode &= ~ALLPERMS;
676 ip->i_mode |= (mode & ALLPERMS);
677 DIP_SET(ip, i_mode, ip->i_mode);
678 ip->i_flag |= IN_CHANGE;
679 return (0);
680 }
681
682 /*
683 * Perform chown operation on inode ip;
684 * inode must be locked prior to call.
685 */
686 static int
687 ufs_chown(vp, uid, gid, cred, td)
688 struct vnode *vp;
689 uid_t uid;
690 gid_t gid;
691 struct ucred *cred;
692 struct thread *td;
693 {
694 struct inode *ip = VTOI(vp);
695 uid_t ouid;
696 gid_t ogid;
697 int error = 0;
698 #ifdef QUOTA
699 int i;
700 ufs2_daddr_t change;
701 #endif
702
703 if (uid == (uid_t)VNOVAL)
704 uid = ip->i_uid;
705 if (gid == (gid_t)VNOVAL)
706 gid = ip->i_gid;
707 /*
708 * To modify the ownership of a file, must possess VADMIN
709 * for that file.
710 */
711 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
712 return (error);
713 /*
714 * To change the owner of a file, or change the group of a file to a
715 * group of which we are not a member, the caller must have
716 * privilege.
717 */
718 if ((uid != ip->i_uid ||
719 (gid != ip->i_gid && !groupmember(gid, cred))) &&
720 (error = suser_cred(cred, SUSER_ALLOWJAIL)))
721 return (error);
722 ogid = ip->i_gid;
723 ouid = ip->i_uid;
724 #ifdef QUOTA
725 if ((error = getinoquota(ip)) != 0)
726 return (error);
727 if (ouid == uid) {
728 dqrele(vp, ip->i_dquot[USRQUOTA]);
729 ip->i_dquot[USRQUOTA] = NODQUOT;
730 }
731 if (ogid == gid) {
732 dqrele(vp, ip->i_dquot[GRPQUOTA]);
733 ip->i_dquot[GRPQUOTA] = NODQUOT;
734 }
735 change = DIP(ip, i_blocks);
736 (void) chkdq(ip, -change, cred, CHOWN);
737 (void) chkiq(ip, -1, cred, CHOWN);
738 for (i = 0; i < MAXQUOTAS; i++) {
739 dqrele(vp, ip->i_dquot[i]);
740 ip->i_dquot[i] = NODQUOT;
741 }
742 #endif
743 ip->i_gid = gid;
744 DIP_SET(ip, i_gid, gid);
745 ip->i_uid = uid;
746 DIP_SET(ip, i_uid, uid);
747 #ifdef QUOTA
748 if ((error = getinoquota(ip)) == 0) {
749 if (ouid == uid) {
750 dqrele(vp, ip->i_dquot[USRQUOTA]);
751 ip->i_dquot[USRQUOTA] = NODQUOT;
752 }
753 if (ogid == gid) {
754 dqrele(vp, ip->i_dquot[GRPQUOTA]);
755 ip->i_dquot[GRPQUOTA] = NODQUOT;
756 }
757 if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
758 if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
759 goto good;
760 else
761 (void) chkdq(ip, -change, cred, CHOWN|FORCE);
762 }
763 for (i = 0; i < MAXQUOTAS; i++) {
764 dqrele(vp, ip->i_dquot[i]);
765 ip->i_dquot[i] = NODQUOT;
766 }
767 }
768 ip->i_gid = ogid;
769 DIP_SET(ip, i_gid, ogid);
770 ip->i_uid = ouid;
771 DIP_SET(ip, i_uid, ouid);
772 if (getinoquota(ip) == 0) {
773 if (ouid == uid) {
774 dqrele(vp, ip->i_dquot[USRQUOTA]);
775 ip->i_dquot[USRQUOTA] = NODQUOT;
776 }
777 if (ogid == gid) {
778 dqrele(vp, ip->i_dquot[GRPQUOTA]);
779 ip->i_dquot[GRPQUOTA] = NODQUOT;
780 }
781 (void) chkdq(ip, change, cred, FORCE|CHOWN);
782 (void) chkiq(ip, 1, cred, FORCE|CHOWN);
783 (void) getinoquota(ip);
784 }
785 return (error);
786 good:
787 if (getinoquota(ip))
788 panic("ufs_chown: lost quota");
789 #endif /* QUOTA */
790 ip->i_flag |= IN_CHANGE;
791 if (suser_cred(cred, SUSER_ALLOWJAIL) && (ouid != uid || ogid != gid)) {
792 ip->i_mode &= ~(ISUID | ISGID);
793 DIP_SET(ip, i_mode, ip->i_mode);
794 }
795 return (0);
796 }
797
798 static int
799 ufs_remove(ap)
800 struct vop_remove_args /* {
801 struct vnode *a_dvp;
802 struct vnode *a_vp;
803 struct componentname *a_cnp;
804 } */ *ap;
805 {
806 struct inode *ip;
807 struct vnode *vp = ap->a_vp;
808 struct vnode *dvp = ap->a_dvp;
809 int error;
810 struct thread *td;
811
812 td = curthread;
813 ip = VTOI(vp);
814 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
815 (VTOI(dvp)->i_flags & APPEND)) {
816 error = EPERM;
817 goto out;
818 }
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 INVARIANTS
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 INVARIANTS
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 INVARIANTS
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 INVARIANTS
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 INVARIANTS
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 /*
1726 * Delete reference to directory before purging
1727 * inode. If we crash in between, the directory
1728 * will be reattached to lost+found,
1729 */
1730 dp->i_effnlink--;
1731 ip->i_effnlink--;
1732 if (DOINGSOFTDEP(vp)) {
1733 softdep_change_linkcnt(dp);
1734 softdep_change_linkcnt(ip);
1735 }
1736 error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
1737 if (error) {
1738 dp->i_effnlink++;
1739 ip->i_effnlink++;
1740 if (DOINGSOFTDEP(vp)) {
1741 softdep_change_linkcnt(dp);
1742 softdep_change_linkcnt(ip);
1743 }
1744 goto out;
1745 }
1746 cache_purge(dvp);
1747 /*
1748 * Truncate inode. The only stuff left in the directory is "." and
1749 * "..". The "." reference is inconsequential since we are quashing
1750 * it. The soft dependency code will arrange to do these operations
1751 * after the parent directory entry has been deleted on disk, so
1752 * when running with that code we avoid doing them now.
1753 */
1754 if (!DOINGSOFTDEP(vp)) {
1755 dp->i_nlink--;
1756 DIP_SET(dp, i_nlink, dp->i_nlink);
1757 dp->i_flag |= IN_CHANGE;
1758 ip->i_nlink--;
1759 DIP_SET(ip, i_nlink, ip->i_nlink);
1760 ip->i_flag |= IN_CHANGE;
1761 ioflag = IO_NORMAL;
1762 if (!DOINGASYNC(vp))
1763 ioflag |= IO_SYNC;
1764 error = UFS_TRUNCATE(vp, (off_t)0, ioflag, cnp->cn_cred,
1765 cnp->cn_thread);
1766 }
1767 cache_purge(vp);
1768 #ifdef UFS_DIRHASH
1769 /* Kill any active hash; i_effnlink == 0, so it will not come back. */
1770 if (ip->i_dirhash != NULL)
1771 ufsdirhash_free(ip);
1772 #endif
1773 out:
1774 return (error);
1775 }
1776
1777 /*
1778 * symlink -- make a symbolic link
1779 */
1780 static int
1781 ufs_symlink(ap)
1782 struct vop_symlink_args /* {
1783 struct vnode *a_dvp;
1784 struct vnode **a_vpp;
1785 struct componentname *a_cnp;
1786 struct vattr *a_vap;
1787 char *a_target;
1788 } */ *ap;
1789 {
1790 struct vnode *vp, **vpp = ap->a_vpp;
1791 struct inode *ip;
1792 int len, error;
1793
1794 error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1795 vpp, ap->a_cnp);
1796 if (error)
1797 return (error);
1798 vp = *vpp;
1799 len = strlen(ap->a_target);
1800 if (len < vp->v_mount->mnt_maxsymlinklen) {
1801 ip = VTOI(vp);
1802 bcopy(ap->a_target, SHORTLINK(ip), len);
1803 ip->i_size = len;
1804 DIP_SET(ip, i_size, len);
1805 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1806 } else
1807 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1808 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1809 ap->a_cnp->cn_cred, NOCRED, (int *)0, (struct thread *)0);
1810 if (error)
1811 vput(vp);
1812 return (error);
1813 }
1814
1815 /*
1816 * Vnode op for reading directories.
1817 *
1818 * The routine below assumes that the on-disk format of a directory
1819 * is the same as that defined by <sys/dirent.h>. If the on-disk
1820 * format changes, then it will be necessary to do a conversion
1821 * from the on-disk format that read returns to the format defined
1822 * by <sys/dirent.h>.
1823 */
1824 int
1825 ufs_readdir(ap)
1826 struct vop_readdir_args /* {
1827 struct vnode *a_vp;
1828 struct uio *a_uio;
1829 struct ucred *a_cred;
1830 int *a_eofflag;
1831 int *a_ncookies;
1832 u_long **a_cookies;
1833 } */ *ap;
1834 {
1835 struct uio *uio = ap->a_uio;
1836 int error;
1837 size_t count, lost;
1838 off_t off;
1839
1840 if (ap->a_ncookies != NULL)
1841 /*
1842 * Ensure that the block is aligned. The caller can use
1843 * the cookies to determine where in the block to start.
1844 */
1845 uio->uio_offset &= ~(DIRBLKSIZ - 1);
1846 off = uio->uio_offset;
1847 count = uio->uio_resid;
1848 /* Make sure we don't return partial entries. */
1849 if (count <= ((uio->uio_offset + count) & (DIRBLKSIZ -1)))
1850 return (EINVAL);
1851 count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
1852 lost = uio->uio_resid - count;
1853 uio->uio_resid = count;
1854 uio->uio_iov->iov_len = count;
1855 # if (BYTE_ORDER == LITTLE_ENDIAN)
1856 if (ap->a_vp->v_mount->mnt_maxsymlinklen > 0) {
1857 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1858 } else {
1859 struct dirent *dp, *edp;
1860 struct uio auio;
1861 struct iovec aiov;
1862 caddr_t dirbuf;
1863 int readcnt;
1864 u_char tmp;
1865
1866 auio = *uio;
1867 auio.uio_iov = &aiov;
1868 auio.uio_iovcnt = 1;
1869 auio.uio_segflg = UIO_SYSSPACE;
1870 aiov.iov_len = count;
1871 MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK);
1872 aiov.iov_base = dirbuf;
1873 error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
1874 if (error == 0) {
1875 readcnt = count - auio.uio_resid;
1876 edp = (struct dirent *)&dirbuf[readcnt];
1877 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
1878 tmp = dp->d_namlen;
1879 dp->d_namlen = dp->d_type;
1880 dp->d_type = tmp;
1881 if (dp->d_reclen > 0) {
1882 dp = (struct dirent *)
1883 ((char *)dp + dp->d_reclen);
1884 } else {
1885 error = EIO;
1886 break;
1887 }
1888 }
1889 if (dp >= edp)
1890 error = uiomove(dirbuf, readcnt, uio);
1891 }
1892 FREE(dirbuf, M_TEMP);
1893 }
1894 # else
1895 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1896 # endif
1897 if (!error && ap->a_ncookies != NULL) {
1898 struct dirent* dpStart;
1899 struct dirent* dpEnd;
1900 struct dirent* dp;
1901 int ncookies;
1902 u_long *cookies;
1903 u_long *cookiep;
1904
1905 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1906 panic("ufs_readdir: unexpected uio from NFS server");
1907 dpStart = (struct dirent *)
1908 ((char *)uio->uio_iov->iov_base - (uio->uio_offset - off));
1909 dpEnd = (struct dirent *) uio->uio_iov->iov_base;
1910 for (dp = dpStart, ncookies = 0;
1911 dp < dpEnd;
1912 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen))
1913 ncookies++;
1914 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1915 M_WAITOK);
1916 for (dp = dpStart, cookiep = cookies;
1917 dp < dpEnd;
1918 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) {
1919 off += dp->d_reclen;
1920 *cookiep++ = (u_long) off;
1921 }
1922 *ap->a_ncookies = ncookies;
1923 *ap->a_cookies = cookies;
1924 }
1925 uio->uio_resid += lost;
1926 if (ap->a_eofflag)
1927 *ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset;
1928 return (error);
1929 }
1930
1931 /*
1932 * Return target name of a symbolic link
1933 */
1934 static int
1935 ufs_readlink(ap)
1936 struct vop_readlink_args /* {
1937 struct vnode *a_vp;
1938 struct uio *a_uio;
1939 struct ucred *a_cred;
1940 } */ *ap;
1941 {
1942 struct vnode *vp = ap->a_vp;
1943 struct inode *ip = VTOI(vp);
1944 doff_t isize;
1945
1946 isize = ip->i_size;
1947 if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
1948 DIP(ip, i_blocks) == 0) { /* XXX - for old fastlink support */
1949 return (uiomove(SHORTLINK(ip), isize, ap->a_uio));
1950 }
1951 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1952 }
1953
1954 /*
1955 * Calculate the logical to physical mapping if not done already,
1956 * then call the device strategy routine.
1957 *
1958 * In order to be able to swap to a file, the ufs_bmaparray() operation may not
1959 * deadlock on memory. See ufs_bmap() for details.
1960 */
1961 static int
1962 ufs_strategy(ap)
1963 struct vop_strategy_args /* {
1964 struct vnode *a_vp;
1965 struct buf *a_bp;
1966 } */ *ap;
1967 {
1968 struct buf *bp = ap->a_bp;
1969 struct vnode *vp = ap->a_vp;
1970 struct bufobj *bo;
1971 struct inode *ip;
1972 ufs2_daddr_t blkno;
1973 int error;
1974
1975 ip = VTOI(vp);
1976 if (bp->b_blkno == bp->b_lblkno) {
1977 error = ufs_bmaparray(vp, bp->b_lblkno, &blkno, bp, NULL, NULL);
1978 bp->b_blkno = blkno;
1979 if (error) {
1980 bp->b_error = error;
1981 bp->b_ioflags |= BIO_ERROR;
1982 bufdone(bp);
1983 return (error);
1984 }
1985 if ((long)bp->b_blkno == -1)
1986 vfs_bio_clrbuf(bp);
1987 }
1988 if ((long)bp->b_blkno == -1) {
1989 bufdone(bp);
1990 return (0);
1991 }
1992 bp->b_iooffset = dbtob(bp->b_blkno);
1993 bo = ip->i_umbufobj;
1994 BO_STRATEGY(bo, bp);
1995 return (0);
1996 }
1997
1998 /*
1999 * Print out the contents of an inode.
2000 */
2001 static int
2002 ufs_print(ap)
2003 struct vop_print_args /* {
2004 struct vnode *a_vp;
2005 } */ *ap;
2006 {
2007 struct vnode *vp = ap->a_vp;
2008 struct inode *ip = VTOI(vp);
2009
2010 printf("\tino %lu, on dev %s", (u_long)ip->i_number,
2011 devtoname(ip->i_dev));
2012 if (vp->v_type == VFIFO)
2013 fifo_printinfo(vp);
2014 printf("\n");
2015 return (0);
2016 }
2017
2018 /*
2019 * Close wrapper for fifos.
2020 *
2021 * Update the times on the inode then do device close.
2022 */
2023 static int
2024 ufsfifo_close(ap)
2025 struct vop_close_args /* {
2026 struct vnode *a_vp;
2027 int a_fflag;
2028 struct ucred *a_cred;
2029 struct thread *a_td;
2030 } */ *ap;
2031 {
2032 struct vnode *vp = ap->a_vp;
2033 int usecount;
2034
2035 VI_LOCK(vp);
2036 usecount = vp->v_usecount;
2037 VI_UNLOCK(vp);
2038 if (usecount > 1)
2039 ufs_itimes(vp);
2040 return (fifo_specops.vop_close(ap));
2041 }
2042
2043 /*
2044 * Kqfilter wrapper for fifos.
2045 *
2046 * Fall through to ufs kqfilter routines if needed
2047 */
2048 static int
2049 ufsfifo_kqfilter(ap)
2050 struct vop_kqfilter_args *ap;
2051 {
2052 int error;
2053
2054 error = fifo_specops.vop_kqfilter(ap);
2055 if (error)
2056 error = vfs_kqfilter(ap);
2057 return (error);
2058 }
2059
2060 /*
2061 * Return POSIX pathconf information applicable to ufs filesystems.
2062 */
2063 static int
2064 ufs_pathconf(ap)
2065 struct vop_pathconf_args /* {
2066 struct vnode *a_vp;
2067 int a_name;
2068 int *a_retval;
2069 } */ *ap;
2070 {
2071 int error;
2072
2073 error = 0;
2074 switch (ap->a_name) {
2075 case _PC_LINK_MAX:
2076 *ap->a_retval = LINK_MAX;
2077 break;
2078 case _PC_NAME_MAX:
2079 *ap->a_retval = NAME_MAX;
2080 break;
2081 case _PC_PATH_MAX:
2082 *ap->a_retval = PATH_MAX;
2083 break;
2084 case _PC_PIPE_BUF:
2085 *ap->a_retval = PIPE_BUF;
2086 break;
2087 case _PC_CHOWN_RESTRICTED:
2088 *ap->a_retval = 1;
2089 break;
2090 case _PC_NO_TRUNC:
2091 *ap->a_retval = 1;
2092 break;
2093 case _PC_ACL_EXTENDED:
2094 #ifdef UFS_ACL
2095 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
2096 *ap->a_retval = 1;
2097 else
2098 *ap->a_retval = 0;
2099 #else
2100 *ap->a_retval = 0;
2101 #endif
2102 break;
2103 case _PC_ACL_PATH_MAX:
2104 #ifdef UFS_ACL
2105 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
2106 *ap->a_retval = ACL_MAX_ENTRIES;
2107 else
2108 *ap->a_retval = 3;
2109 #else
2110 *ap->a_retval = 3;
2111 #endif
2112 break;
2113 case _PC_MAC_PRESENT:
2114 #ifdef MAC
2115 if (ap->a_vp->v_mount->mnt_flag & MNT_MULTILABEL)
2116 *ap->a_retval = 1;
2117 else
2118 *ap->a_retval = 0;
2119 #else
2120 *ap->a_retval = 0;
2121 #endif
2122 break;
2123 case _PC_ASYNC_IO:
2124 /* _PC_ASYNC_IO should have been handled by upper layers. */
2125 KASSERT(0, ("_PC_ASYNC_IO should not get here"));
2126 error = EINVAL;
2127 break;
2128 case _PC_PRIO_IO:
2129 *ap->a_retval = 0;
2130 break;
2131 case _PC_SYNC_IO:
2132 *ap->a_retval = 0;
2133 break;
2134 case _PC_ALLOC_SIZE_MIN:
2135 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize;
2136 break;
2137 case _PC_FILESIZEBITS:
2138 *ap->a_retval = 64;
2139 break;
2140 case _PC_REC_INCR_XFER_SIZE:
2141 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2142 break;
2143 case _PC_REC_MAX_XFER_SIZE:
2144 *ap->a_retval = -1; /* means ``unlimited'' */
2145 break;
2146 case _PC_REC_MIN_XFER_SIZE:
2147 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2148 break;
2149 case _PC_REC_XFER_ALIGN:
2150 *ap->a_retval = PAGE_SIZE;
2151 break;
2152 case _PC_SYMLINK_MAX:
2153 *ap->a_retval = MAXPATHLEN;
2154 break;
2155
2156 default:
2157 error = EINVAL;
2158 break;
2159 }
2160 return (error);
2161 }
2162
2163 /*
2164 * Advisory record locking support
2165 */
2166 static int
2167 ufs_advlock(ap)
2168 struct vop_advlock_args /* {
2169 struct vnode *a_vp;
2170 caddr_t a_id;
2171 int a_op;
2172 struct flock *a_fl;
2173 int a_flags;
2174 } */ *ap;
2175 {
2176 struct inode *ip = VTOI(ap->a_vp);
2177
2178 return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
2179 }
2180
2181 /*
2182 * Advisory record locking support
2183 */
2184 static int
2185 ufs_advlockasync(ap)
2186 struct vop_advlockasync_args /* {
2187 struct vnode *a_vp;
2188 caddr_t a_id;
2189 int a_op;
2190 struct flock *a_fl;
2191 int a_flags;
2192 struct task *a_task;
2193 } */ *ap;
2194 {
2195 struct inode *ip = VTOI(ap->a_vp);
2196
2197 return (lf_advlockasync(ap, &(ip->i_lockf), ip->i_size));
2198 }
2199
2200 /*
2201 * Initialize the vnode associated with a new inode, handle aliased
2202 * vnodes.
2203 */
2204 int
2205 ufs_vinit(mntp, fifoops, vpp)
2206 struct mount *mntp;
2207 struct vop_vector *fifoops;
2208 struct vnode **vpp;
2209 {
2210 struct inode *ip;
2211 struct vnode *vp;
2212
2213 vp = *vpp;
2214 ip = VTOI(vp);
2215 vp->v_type = IFTOVT(ip->i_mode);
2216 if (vp->v_type == VFIFO)
2217 vp->v_op = fifoops;
2218 ASSERT_VOP_LOCKED(vp, "ufs_vinit");
2219 if (ip->i_number == ROOTINO)
2220 vp->v_vflag |= VV_ROOT;
2221 ip->i_modrev = init_va_filerev();
2222 *vpp = vp;
2223 return (0);
2224 }
2225
2226 /*
2227 * Allocate a new inode.
2228 * Vnode dvp must be locked.
2229 */
2230 static int
2231 ufs_makeinode(mode, dvp, vpp, cnp)
2232 int mode;
2233 struct vnode *dvp;
2234 struct vnode **vpp;
2235 struct componentname *cnp;
2236 {
2237 struct inode *ip, *pdir;
2238 struct direct newdir;
2239 struct vnode *tvp;
2240 #ifdef UFS_ACL
2241 struct acl *acl;
2242 #endif
2243 int error;
2244
2245 pdir = VTOI(dvp);
2246 #ifdef INVARIANTS
2247 if ((cnp->cn_flags & HASBUF) == 0)
2248 panic("ufs_makeinode: no name");
2249 #endif
2250 *vpp = NULL;
2251 if ((mode & IFMT) == 0)
2252 mode |= IFREG;
2253
2254 error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
2255 if (error)
2256 return (error);
2257 ip = VTOI(tvp);
2258 ip->i_gid = pdir->i_gid;
2259 DIP_SET(ip, i_gid, pdir->i_gid);
2260 #ifdef SUIDDIR
2261 {
2262 #ifdef QUOTA
2263 struct ucred ucred, *ucp;
2264 ucp = cnp->cn_cred;
2265 #endif
2266 /*
2267 * If we are not the owner of the directory,
2268 * and we are hacking owners here, (only do this where told to)
2269 * and we are not giving it TO root, (would subvert quotas)
2270 * then go ahead and give it to the other user.
2271 * Note that this drops off the execute bits for security.
2272 */
2273 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2274 (pdir->i_mode & ISUID) &&
2275 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2276 ip->i_uid = pdir->i_uid;
2277 DIP_SET(ip, i_uid, ip->i_uid);
2278 mode &= ~07111;
2279 #ifdef QUOTA
2280 /*
2281 * Make sure the correct user gets charged
2282 * for the space.
2283 * Quickly knock up a dummy credential for the victim.
2284 * XXX This seems to never be accessed out of our
2285 * context so a stack variable is ok.
2286 */
2287 refcount_init(&ucred.cr_ref, 1);
2288 ucred.cr_uid = ip->i_uid;
2289 ucred.cr_ngroups = 1;
2290 ucred.cr_groups[0] = pdir->i_gid;
2291 ucp = &ucred;
2292 #endif
2293 } else {
2294 ip->i_uid = cnp->cn_cred->cr_uid;
2295 DIP_SET(ip, i_uid, ip->i_uid);
2296 }
2297
2298 #ifdef QUOTA
2299 if ((error = getinoquota(ip)) ||
2300 (error = chkiq(ip, 1, ucp, 0))) {
2301 UFS_VFREE(tvp, ip->i_number, mode);
2302 vput(tvp);
2303 return (error);
2304 }
2305 #endif
2306 }
2307 #else /* !SUIDDIR */
2308 ip->i_uid = cnp->cn_cred->cr_uid;
2309 DIP_SET(ip, i_uid, ip->i_uid);
2310 #ifdef QUOTA
2311 if ((error = getinoquota(ip)) ||
2312 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2313 UFS_VFREE(tvp, ip->i_number, mode);
2314 vput(tvp);
2315 return (error);
2316 }
2317 #endif
2318 #endif /* !SUIDDIR */
2319 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2320 #ifdef UFS_ACL
2321 acl = NULL;
2322 if ((dvp->v_mount->mnt_flag & MNT_ACLS) != 0) {
2323 acl = uma_zalloc(acl_zone, M_WAITOK);
2324
2325 /*
2326 * Retrieve default ACL for parent, if any.
2327 */
2328 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cnp->cn_cred,
2329 cnp->cn_thread);
2330 switch (error) {
2331 case 0:
2332 /*
2333 * Retrieved a default ACL, so merge mode and ACL if
2334 * necessary.
2335 */
2336 if (acl->acl_cnt != 0) {
2337 /*
2338 * Two possible ways for default ACL to not
2339 * be present. First, the EA can be
2340 * undefined, or second, the default ACL can
2341 * be blank. If it's blank, fall through to
2342 * the it's not defined case.
2343 */
2344 mode = acl_posix1e_newfilemode(mode, acl);
2345 ip->i_mode = mode;
2346 DIP_SET(ip, i_mode, mode);
2347 ufs_sync_acl_from_inode(ip, acl);
2348 break;
2349 }
2350 /* FALLTHROUGH */
2351
2352 case EOPNOTSUPP:
2353 /*
2354 * Just use the mode as-is.
2355 */
2356 ip->i_mode = mode;
2357 DIP_SET(ip, i_mode, mode);
2358 uma_zfree(acl_zone, acl);
2359 acl = NULL;
2360 break;
2361
2362 default:
2363 UFS_VFREE(tvp, ip->i_number, mode);
2364 vput(tvp);
2365 uma_zfree(acl_zone, acl);
2366 acl = NULL;
2367 return (error);
2368 }
2369 } else {
2370 #endif
2371 ip->i_mode = mode;
2372 DIP_SET(ip, i_mode, mode);
2373 #ifdef UFS_ACL
2374 }
2375 #endif
2376 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
2377 ip->i_effnlink = 1;
2378 ip->i_nlink = 1;
2379 DIP_SET(ip, i_nlink, 1);
2380 if (DOINGSOFTDEP(tvp))
2381 softdep_change_linkcnt(ip);
2382 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
2383 suser_cred(cnp->cn_cred, SUSER_ALLOWJAIL)) {
2384 ip->i_mode &= ~ISGID;
2385 DIP_SET(ip, i_mode, ip->i_mode);
2386 }
2387
2388 if (cnp->cn_flags & ISWHITEOUT) {
2389 ip->i_flags |= UF_OPAQUE;
2390 DIP_SET(ip, i_flags, ip->i_flags);
2391 }
2392
2393 /*
2394 * Make sure inode goes to disk before directory entry.
2395 */
2396 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp)));
2397 if (error)
2398 goto bad;
2399 #ifdef MAC
2400 if (dvp->v_mount->mnt_flag & MNT_MULTILABEL) {
2401 error = mac_create_vnode_extattr(cnp->cn_cred, dvp->v_mount,
2402 dvp, tvp, cnp);
2403 if (error)
2404 goto bad;
2405 }
2406 #endif
2407 #ifdef UFS_ACL
2408 if (acl != NULL) {
2409 /*
2410 * XXX: If we abort now, will Soft Updates notify the extattr
2411 * code that the EAs for the file need to be released?
2412 */
2413 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cnp->cn_cred,
2414 cnp->cn_thread);
2415 switch (error) {
2416 case 0:
2417 break;
2418
2419 case EOPNOTSUPP:
2420 /*
2421 * XXX: This should not happen, as EOPNOTSUPP above was
2422 * supposed to free acl.
2423 */
2424 printf("ufs_makeinode: VOP_GETACL() but no "
2425 "VOP_SETACL()\n");
2426 /* panic("ufs_makeinode: VOP_GETACL() but no "
2427 "VOP_SETACL()"); */
2428 break;
2429
2430 default:
2431 uma_zfree(acl_zone, acl);
2432 goto bad;
2433 }
2434 uma_zfree(acl_zone, acl);
2435 }
2436 #endif /* !UFS_ACL */
2437 ufs_makedirentry(ip, cnp, &newdir);
2438 error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2439 if (error)
2440 goto bad;
2441 *vpp = tvp;
2442 return (0);
2443
2444 bad:
2445 /*
2446 * Write error occurred trying to update the inode
2447 * or the directory so must deallocate the inode.
2448 */
2449 ip->i_effnlink = 0;
2450 ip->i_nlink = 0;
2451 DIP_SET(ip, i_nlink, 0);
2452 ip->i_flag |= IN_CHANGE;
2453 if (DOINGSOFTDEP(tvp))
2454 softdep_change_linkcnt(ip);
2455 vput(tvp);
2456 return (error);
2457 }
2458
2459 /* Global vfs data structures for ufs. */
2460 struct vop_vector ufs_vnodeops = {
2461 .vop_default = &default_vnodeops,
2462 .vop_fsync = VOP_PANIC,
2463 .vop_read = VOP_PANIC,
2464 .vop_reallocblks = VOP_PANIC,
2465 .vop_write = VOP_PANIC,
2466 .vop_access = ufs_access,
2467 .vop_advlock = ufs_advlock,
2468 .vop_advlockasync = ufs_advlockasync,
2469 .vop_bmap = ufs_bmap,
2470 .vop_cachedlookup = ufs_lookup,
2471 .vop_close = ufs_close,
2472 .vop_create = ufs_create,
2473 .vop_getattr = ufs_getattr,
2474 .vop_inactive = ufs_inactive,
2475 .vop_link = ufs_link,
2476 .vop_lookup = vfs_cache_lookup,
2477 .vop_mkdir = ufs_mkdir,
2478 .vop_mknod = ufs_mknod,
2479 .vop_open = ufs_open,
2480 .vop_pathconf = ufs_pathconf,
2481 .vop_poll = vop_stdpoll,
2482 .vop_print = ufs_print,
2483 .vop_readdir = ufs_readdir,
2484 .vop_readlink = ufs_readlink,
2485 .vop_reclaim = ufs_reclaim,
2486 .vop_remove = ufs_remove,
2487 .vop_rename = ufs_rename,
2488 .vop_rmdir = ufs_rmdir,
2489 .vop_setattr = ufs_setattr,
2490 #ifdef MAC
2491 .vop_setlabel = vop_stdsetlabel_ea,
2492 #endif
2493 .vop_strategy = ufs_strategy,
2494 .vop_symlink = ufs_symlink,
2495 .vop_whiteout = ufs_whiteout,
2496 #ifdef UFS_EXTATTR
2497 .vop_getextattr = ufs_getextattr,
2498 .vop_deleteextattr = ufs_deleteextattr,
2499 .vop_setextattr = ufs_setextattr,
2500 #endif
2501 #ifdef UFS_ACL
2502 .vop_getacl = ufs_getacl,
2503 .vop_setacl = ufs_setacl,
2504 .vop_aclcheck = ufs_aclcheck,
2505 #endif
2506 };
2507
2508 struct vop_vector ufs_fifoops = {
2509 .vop_default = &fifo_specops,
2510 .vop_fsync = VOP_PANIC,
2511 .vop_access = ufs_access,
2512 .vop_close = ufsfifo_close,
2513 .vop_getattr = ufs_getattr,
2514 .vop_inactive = ufs_inactive,
2515 .vop_kqfilter = ufsfifo_kqfilter,
2516 .vop_print = ufs_print,
2517 .vop_read = VOP_PANIC,
2518 .vop_reclaim = ufs_reclaim,
2519 .vop_setattr = ufs_setattr,
2520 #ifdef MAC
2521 .vop_setlabel = vop_stdsetlabel_ea,
2522 #endif
2523 .vop_write = VOP_PANIC,
2524 #ifdef UFS_EXTATTR
2525 .vop_getextattr = ufs_getextattr,
2526 .vop_deleteextattr = ufs_deleteextattr,
2527 .vop_setextattr = ufs_setextattr,
2528 #endif
2529 #ifdef UFS_ACL
2530 .vop_getacl = ufs_getacl,
2531 .vop_setacl = ufs_setacl,
2532 .vop_aclcheck = ufs_aclcheck,
2533 #endif
2534 };
Cache object: 811811152163c6a33216b80764da4aa6
|