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