1 /*	$OpenBSD: ufs_vnops.c,v 1.67 2005/07/24 05:43:36 millert Exp $	*/
2 /*	$NetBSD: ufs_vnops.c,v 1.18 1996/05/11 18:28:04 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  * (c) UNIX System Laboratories, Inc.
8  * All or some portions of this file are derived from material licensed
9  * to the University of California by American Telephone and Telegraph
10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11  * the permission of UNIX System Laboratories, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)ufs_vnops.c	8.14 (Berkeley) 10/26/94
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/namei.h>
43 #include <sys/resourcevar.h>
44 #include <sys/kernel.h>
45 #include <sys/file.h>
46 #include <sys/stat.h>
47 #include <sys/buf.h>
48 #include <sys/proc.h>
49 #include <sys/conf.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
52 #include <sys/malloc.h>
53 #include <sys/pool.h>
54 #include <sys/dirent.h>
55 #include <sys/lockf.h>
56 #include <sys/event.h>
57 #include <sys/poll.h>
58 
59 #include <uvm/uvm_extern.h>
60 
61 #include <miscfs/specfs/specdev.h>
62 #include <miscfs/fifofs/fifo.h>
63 
64 #include <ufs/ufs/quota.h>
65 #include <ufs/ufs/inode.h>
66 #include <ufs/ufs/dir.h>
67 #include <ufs/ufs/ufsmount.h>
68 #include <ufs/ufs/ufs_extern.h>
69 #ifdef UFS_DIRHASH
70 #include <ufs/ufs/dirhash.h>
71 #endif
72 #include <ufs/ext2fs/ext2fs_extern.h>
73 
74 static int ufs_chmod(struct vnode *, int, struct ucred *, struct proc *);
75 static int ufs_chown(struct vnode *, uid_t, gid_t, struct ucred *, struct proc *);
76 int filt_ufsread(struct knote *kn, long hint);
77 int filt_ufswrite(struct knote *kn, long hint);
78 int filt_ufsvnode(struct knote *kn, long hint);
79 void filt_ufsdetach(struct knote *kn);
80 
81 union _qcvt {
82 	int64_t	qcvt;
83 	int32_t val[2];
84 };
85 #define SETHIGH(q, h) { \
86 	union _qcvt tmp; \
87 	tmp.qcvt = (q); \
88 	tmp.val[_QUAD_HIGHWORD] = (h); \
89 	(q) = tmp.qcvt; \
90 }
91 #define SETLOW(q, l) { \
92 	union _qcvt tmp; \
93 	tmp.qcvt = (q); \
94 	tmp.val[_QUAD_LOWWORD] = (l); \
95 	(q) = tmp.qcvt; \
96 }
97 #define VN_KNOTE(vp, b) \
98 	KNOTE(&vp->v_selectinfo.vsi_selinfo.si_note, (b))
99 
100 
101 /*
102  * A virgin directory (no blushing please).
103  */
104 static struct dirtemplate mastertemplate = {
105 	0, 12, DT_DIR, 1, ".",
106 	0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
107 };
108 static struct odirtemplate omastertemplate = {
109 	0, 12, 1, ".",
110 	0, DIRBLKSIZ - 12, 2, ".."
111 };
112 
113 /*
114  * Create a regular file
115  */
116 int
ufs_create(v)117 ufs_create(v)
118 	void *v;
119 {
120 	struct vop_create_args /* {
121 		struct vnode *a_dvp;
122 		struct vnode **a_vpp;
123 		struct componentname *a_cnp;
124 		struct vattr *a_vap;
125 	} */ *ap = v;
126 	int error;
127 
128 	error =
129 	    ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
130 			  ap->a_dvp, ap->a_vpp, ap->a_cnp);
131 	if (error)
132 		return (error);
133 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
134 	return (0);
135 }
136 
137 /*
138  * Mknod vnode call
139  */
140 /* ARGSUSED */
141 int
ufs_mknod(v)142 ufs_mknod(v)
143 	void *v;
144 {
145 	struct vop_mknod_args /* {
146 				 struct vnode *a_dvp;
147 				 struct vnode **a_vpp;
148 				 struct componentname *a_cnp;
149 				 struct vattr *a_vap;
150 				 } */ *ap = v;
151 	struct vattr *vap = ap->a_vap;
152         struct vnode **vpp = ap->a_vpp;
153 	struct inode *ip;
154 	int error;
155 
156 	if ((error =
157 	     ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
158 			   ap->a_dvp, vpp, ap->a_cnp)) != 0)
159 		return (error);
160 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
161 	ip = VTOI(*vpp);
162 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
163 	if (vap->va_rdev != VNOVAL) {
164 		/*
165 		 * Want to be able to use this to make badblock
166 		 * inodes, so don't truncate the dev number.
167 		 */
168 		ip->i_ffs_rdev = vap->va_rdev;
169 	}
170 	/*
171 	 * Remove inode so that it will be reloaded by VFS_VGET and
172 	 * checked to see if it is an alias of an existing entry in
173 	 * the inode cache.
174 	 */
175 	vput(*vpp);
176 	(*vpp)->v_type = VNON;
177 	vgone(*vpp);
178 	*vpp = 0;
179 	return (0);
180 }
181 
182 /*
183  * Open called.
184  *
185  * Nothing to do.
186  */
187 /* ARGSUSED */
188 int
ufs_open(v)189 ufs_open(v)
190 	void *v;
191 {
192 	struct vop_open_args /* {
193 				struct vnode *a_vp;
194 				int  a_mode;
195 				struct ucred *a_cred;
196 				struct proc *a_p;
197 	} */ *ap = v;
198 	struct inode *ip = VTOI(ap->a_vp);
199 
200 	/*
201 	 * Files marked append-only must be opened for appending.
202 	 */
203 	if ((ip->i_ffs_flags & APPEND) &&
204 	    (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
205 		return (EPERM);
206 
207 	if (ap->a_mode & O_TRUNC)
208 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
209 
210 	return (0);
211 }
212 
213 /*
214  * Close called.
215  *
216  * Update the times on the inode.
217  */
218 /* ARGSUSED */
219 int
ufs_close(v)220 ufs_close(v)
221 	void *v;
222 {
223 	struct vop_close_args /* {
224 		struct vnode *a_vp;
225 		int  a_fflag;
226 		struct ucred *a_cred;
227 		struct proc *a_p;
228 	} */ *ap = v;
229 	struct vnode *vp = ap->a_vp;
230 	struct inode *ip = VTOI(vp);
231 
232 	simple_lock(&vp->v_interlock);
233 	if (vp->v_usecount > 1)
234 		ITIMES(ip, &time, &time);
235 	simple_unlock(&vp->v_interlock);
236 	return (0);
237 }
238 
239 int
ufs_access(v)240 ufs_access(v)
241 	void *v;
242 {
243 	struct vop_access_args /* {
244 		struct vnode *a_vp;
245 		int  a_mode;
246 		struct ucred *a_cred;
247 		struct proc *a_p;
248 	} */ *ap = v;
249 	struct vnode *vp = ap->a_vp;
250 	struct inode *ip = VTOI(vp);
251 	mode_t mode = ap->a_mode;
252 
253 	/*
254 	 * Disallow write attempts on read-only file systems;
255 	 * unless the file is a socket, fifo, or a block or
256 	 * character device resident on the file system.
257 	 */
258 	if (mode & VWRITE) {
259 		switch (vp->v_type) {
260 			int error;
261 		case VDIR:
262 		case VLNK:
263 		case VREG:
264 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
265 				return (EROFS);
266 
267 			if ((error = getinoquota(ip)) != 0)
268 				return (error);
269 			break;
270 		case VBAD:
271 		case VBLK:
272 		case VCHR:
273 		case VSOCK:
274 		case VFIFO:
275 		case VNON:
276 			break;
277 
278 		}
279 	}
280 
281 	/* If immutable bit set, nobody gets to write it. */
282 	if ((mode & VWRITE) && (ip->i_ffs_flags & IMMUTABLE))
283 		return (EPERM);
284 
285 	return (vaccess(ip->i_ffs_mode, ip->i_ffs_uid, ip->i_ffs_gid,
286 					mode, ap->a_cred));
287 }
288 
289 /* ARGSUSED */
290 int
ufs_getattr(v)291 ufs_getattr(v)
292 	void *v;
293 {
294 	struct vop_getattr_args /* {
295 		struct vnode *a_vp;
296 		struct vattr *a_vap;
297 		struct ucred *a_cred;
298 		struct proc *a_p;
299 	} */ *ap = v;
300 	struct vnode *vp = ap->a_vp;
301 	struct inode *ip = VTOI(vp);
302 	struct vattr *vap = ap->a_vap;
303 
304 	ITIMES(ip, &time, &time);
305 	/*
306 	 * Copy from inode table
307 	 */
308 	vap->va_fsid = ip->i_dev;
309 	vap->va_fileid = ip->i_number;
310 	vap->va_mode = ip->i_ffs_mode & ~IFMT;
311 	vap->va_nlink = ip->i_effnlink;
312 	vap->va_uid = ip->i_ffs_uid;
313 	vap->va_gid = ip->i_ffs_gid;
314 	vap->va_rdev = (dev_t)ip->i_ffs_rdev;
315 	vap->va_size = ip->i_ffs_size;
316 	vap->va_atime.tv_sec = ip->i_ffs_atime;
317 	vap->va_atime.tv_nsec = ip->i_ffs_atimensec;
318 	vap->va_mtime.tv_sec = ip->i_ffs_mtime;
319 	vap->va_mtime.tv_nsec = ip->i_ffs_mtimensec;
320 	vap->va_ctime.tv_sec = ip->i_ffs_ctime;
321 	vap->va_ctime.tv_nsec = ip->i_ffs_ctimensec;
322 	vap->va_flags = ip->i_ffs_flags;
323 	vap->va_gen = ip->i_ffs_gen;
324 	/* this doesn't belong here */
325 	if (vp->v_type == VBLK)
326 		vap->va_blocksize = BLKDEV_IOSIZE;
327 	else if (vp->v_type == VCHR)
328 		vap->va_blocksize = MAXBSIZE;
329 	else
330 		vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
331 	vap->va_bytes = dbtob((u_quad_t)ip->i_ffs_blocks);
332 	vap->va_type = vp->v_type;
333 	vap->va_filerev = ip->i_modrev;
334 	return (0);
335 }
336 
337 /*
338  * Set attribute vnode op. called from several syscalls
339  */
340 int
ufs_setattr(v)341 ufs_setattr(v)
342 	void *v;
343 {
344 	struct vop_setattr_args /* {
345 		struct vnode *a_vp;
346 		struct vattr *a_vap;
347 		struct ucred *a_cred;
348 		struct proc *a_p;
349 	} */ *ap = v;
350 	struct vattr *vap = ap->a_vap;
351 	struct vnode *vp = ap->a_vp;
352 	struct inode *ip = VTOI(vp);
353 	struct ucred *cred = ap->a_cred;
354 	struct proc *p = ap->a_p;
355 	int error;
356 	long hint = NOTE_ATTRIB;
357 	u_quad_t oldsize;
358 
359 	/*
360 	 * Check for unsettable attributes.
361 	 */
362 	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
363 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
364 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
365 	    ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
366 		return (EINVAL);
367 	}
368 	if (vap->va_flags != VNOVAL) {
369 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
370 			return (EROFS);
371 		if (cred->cr_uid != ip->i_ffs_uid &&
372 		    (error = suser_ucred(cred)))
373 			return (error);
374 		if (cred->cr_uid == 0) {
375 			if ((ip->i_ffs_flags & (SF_IMMUTABLE | SF_APPEND)) &&
376 			    securelevel > 0)
377 				return (EPERM);
378 			ip->i_ffs_flags = vap->va_flags;
379 		} else {
380 			if (ip->i_ffs_flags & (SF_IMMUTABLE | SF_APPEND) ||
381 			    (vap->va_flags & UF_SETTABLE) != vap->va_flags)
382 				return (EPERM);
383 			ip->i_ffs_flags &= SF_SETTABLE;
384 			ip->i_ffs_flags |= (vap->va_flags & UF_SETTABLE);
385 		}
386 		ip->i_flag |= IN_CHANGE;
387 		if (vap->va_flags & (IMMUTABLE | APPEND))
388 			return (0);
389 	}
390 	if (ip->i_ffs_flags & (IMMUTABLE | APPEND))
391 		return (EPERM);
392 	/*
393 	 * Go through the fields and update if not VNOVAL.
394 	 */
395 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
396 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
397 			return (EROFS);
398 		error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred, p);
399 		if (error)
400 			return (error);
401 	}
402 	if (vap->va_size != VNOVAL) {
403 		oldsize = ip->i_ffs_size;
404 		/*
405 		 * Disallow write attempts on read-only file systems;
406 		 * unless the file is a socket, fifo, or a block or
407 		 * character device resident on the file system.
408 		 */
409 		switch (vp->v_type) {
410 		case VDIR:
411  			return (EISDIR);
412 		case VLNK:
413 		case VREG:
414 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
415 				return (EROFS);
416 			break;
417 		default:
418 			break;
419 		}
420  		if ((error = UFS_TRUNCATE(ip, vap->va_size, 0, cred)) != 0)
421  			return (error);
422 		if (vap->va_size < oldsize)
423 			hint |= NOTE_TRUNCATE;
424 	}
425 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
426 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
427 			return (EROFS);
428 		if (cred->cr_uid != ip->i_ffs_uid &&
429 		    (error = suser_ucred(cred)) &&
430 		    ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
431 		    (error = VOP_ACCESS(vp, VWRITE, cred, p))))
432 			return (error);
433 		if (vap->va_atime.tv_sec != VNOVAL)
434 			ip->i_flag |= IN_ACCESS;
435 		if (vap->va_mtime.tv_sec != VNOVAL)
436 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
437 		error = UFS_UPDATE2(ip, &vap->va_atime, &vap->va_mtime, 0);
438 		if (error)
439 			return (error);
440 	}
441 	error = 0;
442 	if (vap->va_mode != (mode_t)VNOVAL) {
443 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
444 			return (EROFS);
445 		error = ufs_chmod(vp, (int)vap->va_mode, cred, p);
446 	}
447 	VN_KNOTE(vp, hint);
448 	return (error);
449 }
450 
451 /*
452  * Change the mode on a file.
453  * Inode must be locked before calling.
454  */
455 static int
ufs_chmod(vp,mode,cred,p)456 ufs_chmod(vp, mode, cred, p)
457 	struct vnode *vp;
458 	int mode;
459 	struct ucred *cred;
460 	struct proc *p;
461 {
462 	struct inode *ip = VTOI(vp);
463 	int error;
464 
465 	if (cred->cr_uid != ip->i_ffs_uid &&
466 	    (error = suser_ucred(cred)))
467 		return (error);
468 	if (cred->cr_uid) {
469 		if (vp->v_type != VDIR && (mode & S_ISTXT))
470 			return (EFTYPE);
471 		if (!groupmember(ip->i_ffs_gid, cred) && (mode & ISGID))
472 			return (EPERM);
473 	}
474 	ip->i_ffs_mode &= ~ALLPERMS;
475 	ip->i_ffs_mode |= (mode & ALLPERMS);
476 	ip->i_flag |= IN_CHANGE;
477 	if ((vp->v_flag & VTEXT) && (ip->i_ffs_mode & S_ISTXT) == 0)
478 		(void) uvm_vnp_uncache(vp);
479 	return (0);
480 }
481 
482 /*
483  * Perform chown operation on inode ip;
484  * inode must be locked prior to call.
485  */
486 static int
ufs_chown(vp,uid,gid,cred,p)487 ufs_chown(vp, uid, gid, cred, p)
488 	struct vnode *vp;
489 	uid_t uid;
490 	gid_t gid;
491 	struct ucred *cred;
492 	struct proc *p;
493 {
494 	struct inode *ip = VTOI(vp);
495 	uid_t ouid;
496 	gid_t ogid;
497 	int error = 0;
498 	daddr_t change;
499 	enum ufs_quota_flags quota_flags = 0;
500 
501 	if (uid == (uid_t)VNOVAL)
502 		uid = ip->i_ffs_uid;
503 	if (gid == (gid_t)VNOVAL)
504 		gid = ip->i_ffs_gid;
505 	/*
506 	 * If we don't own the file, are trying to change the owner
507 	 * of the file, or are not a member of the target group,
508 	 * the caller must be superuser or the call fails.
509 	 */
510 	if ((cred->cr_uid != ip->i_ffs_uid || uid != ip->i_ffs_uid ||
511 	    (gid != ip->i_ffs_gid && !groupmember((gid_t)gid, cred))) &&
512 	    (error = suser_ucred(cred)))
513 		return (error);
514 	ogid = ip->i_ffs_gid;
515 	ouid = ip->i_ffs_uid;
516 	change = ip->i_ffs_blocks;
517 
518 	if (ouid == uid)
519 		quota_flags |= UFS_QUOTA_NOUID;
520 
521 	if (ogid == gid)
522 		quota_flags |= UFS_QUOTA_NOGID;
523 
524 	if ((error = getinoquota(ip)) != 0)
525 		return (error);
526 	(void) ufs_quota_free_blocks2(ip, change, cred, quota_flags);
527 	(void) ufs_quota_free_inode2(ip, cred, quota_flags);
528 	(void) ufs_quota_delete(ip);
529 
530 	ip->i_ffs_gid = gid;
531 	ip->i_ffs_uid = uid;
532 
533 	if ((error = getinoquota(ip)) != 0)
534 		goto error;
535 
536 	if ((error = ufs_quota_alloc_blocks2(ip, change, cred,
537 		 quota_flags)) != 0)
538 		goto error;
539 
540 	if ((error = ufs_quota_alloc_inode2(ip, cred ,
541 		 quota_flags)) != 0) {
542 		(void)ufs_quota_free_blocks2(ip, change, cred,
543 		    quota_flags);
544 		goto error;
545 	}
546 
547 	if (getinoquota(ip))
548 		panic("chown: lost quota");
549 
550 	if (ouid != uid || ogid != gid)
551 		ip->i_flag |= IN_CHANGE;
552 	if (ouid != uid && cred->cr_uid != 0)
553 		ip->i_ffs_mode &= ~ISUID;
554 	if (ogid != gid && cred->cr_uid != 0)
555 		ip->i_ffs_mode &= ~ISGID;
556 	return (0);
557 
558 error:
559 	(void) ufs_quota_delete(ip);
560 
561 	ip->i_ffs_gid = ogid;
562 	ip->i_ffs_uid = ouid;
563 	if (getinoquota(ip) == 0) {
564 		(void) ufs_quota_alloc_blocks2(ip, change, cred,
565 		    quota_flags | UFS_QUOTA_FORCE);
566 		(void) ufs_quota_alloc_inode2(ip, cred,
567 		    quota_flags | UFS_QUOTA_FORCE);
568 		(void) getinoquota(ip);
569 	}
570 	return (error);
571 
572 }
573 
574 /* ARGSUSED */
575 int
ufs_ioctl(v)576 ufs_ioctl(v)
577 	void *v;
578 {
579 #if 0
580 	struct vop_ioctl_args /* {
581 		struct vnode *a_vp;
582 		u_long a_command;
583 		caddr_t  a_data;
584 		int  a_fflag;
585 		struct ucred *a_cred;
586 		struct proc *a_p;
587 	} */ *ap = v;
588 #endif
589 	return (ENOTTY);
590 }
591 
592 /* ARGSUSED */
593 int
ufs_poll(v)594 ufs_poll(v)
595 	void *v;
596 {
597 	struct vop_poll_args /* {
598 		struct vnode *a_vp;
599 		int  a_events;
600 		struct proc *a_p;
601 	} */ *ap = v;
602 
603 	/*
604 	 * We should really check to see if I/O is possible.
605 	 */
606 	return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
607 }
608 
609 /*
610  * Seek on a file
611  *
612  * Nothing to do, so just return.
613  */
614 /* ARGSUSED */
615 int
ufs_seek(v)616 ufs_seek(v)
617 	void *v;
618 {
619 #if 0
620 	struct vop_seek_args /* {
621 		struct vnode *a_vp;
622 		off_t  a_oldoff;
623 		off_t  a_newoff;
624 		struct ucred *a_cred;
625 	} */ *ap = v;
626 #endif
627 
628 	return (0);
629 }
630 
631 int
ufs_remove(v)632 ufs_remove(v)
633 	void *v;
634 {
635 	struct vop_remove_args /* {
636 		struct vnode *a_dvp;
637 		struct vnode *a_vp;
638 		struct componentname *a_cnp;
639 	} */ *ap = v;
640 	struct inode *ip;
641 	struct vnode *vp = ap->a_vp;
642 	struct vnode *dvp = ap->a_dvp;
643 	int error;
644 
645 	ip = VTOI(vp);
646 	if (vp->v_type == VDIR || (ip->i_ffs_flags & (IMMUTABLE | APPEND)) ||
647 	    (VTOI(dvp)->i_ffs_flags & APPEND)) {
648 		error = EPERM;
649 		goto out;
650 	}
651 	error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0);
652 	VN_KNOTE(vp, NOTE_DELETE);
653 	VN_KNOTE(dvp, NOTE_WRITE);
654  out:
655 	if (dvp == vp)
656 		vrele(vp);
657 	else
658 		vput(vp);
659 	vput(dvp);
660 	return (error);
661 }
662 
663 /*
664  * link vnode call
665  */
666 int
ufs_link(v)667 ufs_link(v)
668 	void *v;
669 {
670 	struct vop_link_args /* {
671 		struct vnode *a_dvp;
672 		struct vnode *a_vp;
673 		struct componentname *a_cnp;
674 	} */ *ap = v;
675 	struct vnode *dvp = ap->a_dvp;
676 	struct vnode *vp = ap->a_vp;
677 	struct componentname *cnp = ap->a_cnp;
678 	struct proc *p = cnp->cn_proc;
679 	struct inode *ip;
680 	struct direct newdir;
681 	int error;
682 
683 #ifdef DIAGNOSTIC
684 	if ((cnp->cn_flags & HASBUF) == 0)
685 		panic("ufs_link: no name");
686 #endif
687 	if (vp->v_type == VDIR) {
688 		VOP_ABORTOP(dvp, cnp);
689 		error = EPERM;
690 		goto out2;
691 	}
692 	if (dvp->v_mount != vp->v_mount) {
693 		VOP_ABORTOP(dvp, cnp);
694 		error = EXDEV;
695 		goto out2;
696 	}
697 	if (dvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE, p))) {
698 		VOP_ABORTOP(dvp, cnp);
699 		goto out2;
700 	}
701 	ip = VTOI(vp);
702 	if ((nlink_t)ip->i_ffs_nlink >= LINK_MAX) {
703 		VOP_ABORTOP(dvp, cnp);
704 		error = EMLINK;
705 		goto out1;
706 	}
707 	if (ip->i_ffs_flags & (IMMUTABLE | APPEND)) {
708 		VOP_ABORTOP(dvp, cnp);
709 		error = EPERM;
710 		goto out1;
711 	}
712 	ip->i_effnlink++;
713 	ip->i_ffs_nlink++;
714 	ip->i_flag |= IN_CHANGE;
715 	if (DOINGSOFTDEP(vp))
716 		softdep_change_linkcnt(ip, 0);
717 	if ((error = UFS_UPDATE(ip, !DOINGSOFTDEP(vp))) == 0) {
718 		ufs_makedirentry(ip, cnp, &newdir);
719 		error = ufs_direnter(dvp, vp, &newdir, cnp, NULL);
720 	}
721 	if (error) {
722 		ip->i_effnlink--;
723 		ip->i_ffs_nlink--;
724 		ip->i_flag |= IN_CHANGE;
725 		if (DOINGSOFTDEP(vp))
726 			softdep_change_linkcnt(ip, 0);
727 	}
728 	pool_put(&namei_pool, cnp->cn_pnbuf);
729 	VN_KNOTE(vp, NOTE_LINK);
730 	VN_KNOTE(dvp, NOTE_WRITE);
731 out1:
732 	if (dvp != vp)
733 		VOP_UNLOCK(vp, 0, p);
734 out2:
735 	vput(dvp);
736 	return (error);
737 }
738 
739 /*
740  * Rename system call.
741  * 	rename("foo", "bar");
742  * is essentially
743  *	unlink("bar");
744  *	link("foo", "bar");
745  *	unlink("foo");
746  * but ``atomically''.  Can't do full commit without saving state in the
747  * inode on disk which isn't feasible at this time.  Best we can do is
748  * always guarantee the target exists.
749  *
750  * Basic algorithm is:
751  *
752  * 1) Bump link count on source while we're linking it to the
753  *    target.  This also ensure the inode won't be deleted out
754  *    from underneath us while we work (it may be truncated by
755  *    a concurrent `trunc' or `open' for creation).
756  * 2) Link source to destination.  If destination already exists,
757  *    delete it first.
758  * 3) Unlink source reference to inode if still around. If a
759  *    directory was moved and the parent of the destination
760  *    is different from the source, patch the ".." entry in the
761  *    directory.
762  */
763 int
ufs_rename(v)764 ufs_rename(v)
765 	void *v;
766 {
767 	struct vop_rename_args  /* {
768 		struct vnode *a_fdvp;
769 		struct vnode *a_fvp;
770 		struct componentname *a_fcnp;
771 		struct vnode *a_tdvp;
772 		struct vnode *a_tvp;
773 		struct componentname *a_tcnp;
774 	} */ *ap = v;
775 	struct vnode *tvp = ap->a_tvp;
776 	struct vnode *tdvp = ap->a_tdvp;
777 	struct vnode *fvp = ap->a_fvp;
778 	struct vnode *fdvp = ap->a_fdvp;
779 	struct componentname *tcnp = ap->a_tcnp;
780 	struct componentname *fcnp = ap->a_fcnp;
781 	struct proc *p = fcnp->cn_proc;
782 	struct inode *ip, *xp, *dp;
783 	struct direct newdir;
784 	int doingdirectory = 0, oldparent = 0, newparent = 0;
785 	int error = 0;
786 
787 #ifdef DIAGNOSTIC
788 	if ((tcnp->cn_flags & HASBUF) == 0 ||
789 	    (fcnp->cn_flags & HASBUF) == 0)
790 		panic("ufs_rename: no name");
791 #endif
792 	/*
793 	 * Check for cross-device rename.
794 	 */
795 	if ((fvp->v_mount != tdvp->v_mount) ||
796 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
797 		error = EXDEV;
798 abortit:
799 		VOP_ABORTOP(tdvp, tcnp); /* XXX, why not in NFS? */
800 		if (tdvp == tvp)
801 			vrele(tdvp);
802 		else
803 			vput(tdvp);
804 		if (tvp)
805 			vput(tvp);
806 		VOP_ABORTOP(fdvp, fcnp); /* XXX, why not in NFS? */
807 		vrele(fdvp);
808 		vrele(fvp);
809 		return (error);
810 	}
811 
812 	if (tvp && ((VTOI(tvp)->i_ffs_flags & (IMMUTABLE | APPEND)) ||
813 	    (VTOI(tdvp)->i_ffs_flags & APPEND))) {
814 		error = EPERM;
815 		goto abortit;
816 	}
817 
818 	/*
819 	 * Check if just deleting a link name or if we've lost a race.
820 	 * If another process completes the same rename after we've looked
821 	 * up the source and have blocked looking up the target, then the
822 	 * source and target inodes may be identical now although the
823 	 * names were never linked.
824 	 */
825 	if (fvp == tvp) {
826 		if (fvp->v_type == VDIR) {
827 			/*
828 			 * Linked directories are impossible, so we must
829 			 * have lost the race.  Pretend that the rename
830 			 * completed before the lookup.
831 			 */
832 			error = ENOENT;
833 			goto abortit;
834 		}
835 
836 		/* Release destination completely. */
837 		VOP_ABORTOP(tdvp, tcnp);
838 		vput(tdvp);
839 		vput(tvp);
840 
841 		/*
842 		 * Delete source.  There is another race now that everything
843 		 * is unlocked, but this doesn't cause any new complications.
844 		 * relookup() may find a file that is unrelated to the
845 		 * original one, or it may fail.  Too bad.
846 		 */
847 		vrele(fvp);
848 		fcnp->cn_flags &= ~MODMASK;
849 		fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
850 		if ((fcnp->cn_flags & SAVESTART) == 0)
851 			panic("ufs_rename: lost from startdir");
852 		fcnp->cn_nameiop = DELETE;
853 		if ((error = relookup(fdvp, &fvp, fcnp)) != 0)
854 			return (error);		/* relookup did vrele() */
855 		vrele(fdvp);
856 		return (VOP_REMOVE(fdvp, fvp, fcnp));
857 	}
858 
859 	if ((error = vn_lock(fvp, LK_EXCLUSIVE, p)) != 0)
860 		goto abortit;
861 
862 	/* fvp, tdvp, tvp now locked */
863 	dp = VTOI(fdvp);
864 	ip = VTOI(fvp);
865 	if ((nlink_t)ip->i_ffs_nlink >= LINK_MAX) {
866 		VOP_UNLOCK(fvp, 0, p);
867 		error = EMLINK;
868 		goto abortit;
869 	}
870 	if ((ip->i_ffs_flags & (IMMUTABLE | APPEND)) ||
871 	    (dp->i_ffs_flags & APPEND)) {
872 		VOP_UNLOCK(fvp, 0, p);
873 		error = EPERM;
874 		goto abortit;
875 	}
876 	if ((ip->i_ffs_mode & IFMT) == IFDIR) {
877 		error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
878 		if (!error && tvp)
879 			error = VOP_ACCESS(tvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
880 		if (error) {
881 			VOP_UNLOCK(fvp, 0, p);
882 			error = EACCES;
883 			goto abortit;
884 		}
885 		/*
886 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
887 		 */
888 		if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
889 		    dp == ip ||
890 		    (fcnp->cn_flags & ISDOTDOT) ||
891 		    (tcnp->cn_flags & ISDOTDOT) ||
892 		    (ip->i_flag & IN_RENAME)) {
893 			VOP_UNLOCK(fvp, 0, p);
894 			error = EINVAL;
895 			goto abortit;
896 		}
897 		ip->i_flag |= IN_RENAME;
898 		oldparent = dp->i_number;
899 		doingdirectory = 1;
900 	}
901 	VN_KNOTE(fdvp, NOTE_WRITE);		/* XXX right place? */
902 
903 	/*
904 	 * When the target exists, both the directory
905 	 * and target vnodes are returned locked.
906 	 */
907 	dp = VTOI(tdvp);
908 	xp = NULL;
909 	if (tvp)
910 		xp = VTOI(tvp);
911 
912 	/*
913 	 * 1) Bump link count while we're moving stuff
914 	 *    around.  If we crash somewhere before
915 	 *    completing our work, the link count
916 	 *    may be wrong, but correctable.
917 	 */
918 	ip->i_effnlink++;
919 	ip->i_ffs_nlink++;
920 	ip->i_flag |= IN_CHANGE;
921 	if (DOINGSOFTDEP(fvp))
922 		softdep_change_linkcnt(ip, 0);
923 	if ((error = UFS_UPDATE(ip, !DOINGSOFTDEP(fvp))) != 0) {
924 		VOP_UNLOCK(fvp, 0, p);
925 		goto bad;
926 	}
927 
928 	/*
929 	 * If ".." must be changed (ie the directory gets a new
930 	 * parent) then the source directory must not be in the
931 	 * directory hierarchy above the target, as this would
932 	 * orphan everything below the source directory. Also
933 	 * the user must have write permission in the source so
934 	 * as to be able to change "..". We must repeat the call
935 	 * to namei, as the parent directory is unlocked by the
936 	 * call to checkpath().
937 	 */
938 	error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
939 	VOP_UNLOCK(fvp, 0, p);
940 
941 	/* tdvp and tvp locked */
942 	if (oldparent != dp->i_number)
943 		newparent = dp->i_number;
944 	if (doingdirectory && newparent) {
945 		if (error)	/* write access check above */
946 			goto bad;
947 		if (xp != NULL)
948 			vput(tvp);
949 		/*
950 		 * Compensate for the reference ufs_checkpath() loses.
951 		 */
952 		vref(tdvp);
953 		/* Only tdvp is locked */
954 		if ((error = ufs_checkpath(ip, dp, tcnp->cn_cred)) != 0)
955 			goto out;
956 		if ((tcnp->cn_flags & SAVESTART) == 0)
957 			panic("ufs_rename: lost to startdir");
958 		if ((error = relookup(tdvp, &tvp, tcnp)) != 0)
959 			goto out;
960 		dp = VTOI(tdvp);
961 		xp = NULL;
962 		if (tvp)
963 			xp = VTOI(tvp);
964 	}
965 	/*
966 	 * 2) If target doesn't exist, link the target
967 	 *    to the source and unlink the source.
968 	 *    Otherwise, rewrite the target directory
969 	 *    entry to reference the source inode and
970 	 *    expunge the original entry's existence.
971 	 */
972 	if (xp == NULL) {
973 		if (dp->i_dev != ip->i_dev)
974 			panic("rename: EXDEV");
975 		/*
976 		 * Account for ".." in new directory.
977 		 * When source and destination have the same
978 		 * parent we don't fool with the link count.
979 		 */
980 		if (doingdirectory && newparent) {
981 			if ((nlink_t)dp->i_ffs_nlink >= LINK_MAX) {
982 				error = EMLINK;
983 				goto bad;
984 			}
985 			dp->i_effnlink++;
986 			dp->i_ffs_nlink++;
987 			dp->i_flag |= IN_CHANGE;
988 			if (DOINGSOFTDEP(tdvp))
989                                softdep_change_linkcnt(dp, 0);
990 			if ((error = UFS_UPDATE(dp, !DOINGSOFTDEP(tdvp)))
991 			    != 0) {
992 				dp->i_effnlink--;
993 				dp->i_ffs_nlink--;
994 				dp->i_flag |= IN_CHANGE;
995 				if (DOINGSOFTDEP(tdvp))
996 					softdep_change_linkcnt(dp, 0);
997 				goto bad;
998 			}
999 		}
1000 		ufs_makedirentry(ip, tcnp, &newdir);
1001 		if ((error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL)) != 0) {
1002 			if (doingdirectory && newparent) {
1003 				dp->i_effnlink--;
1004 				dp->i_ffs_nlink--;
1005 				dp->i_flag |= IN_CHANGE;
1006 				if (DOINGSOFTDEP(tdvp))
1007 					softdep_change_linkcnt(dp, 0);
1008 				(void)UFS_UPDATE(dp, 1);
1009 			}
1010 			goto bad;
1011 		}
1012 		VN_KNOTE(tdvp, NOTE_WRITE);
1013 		vput(tdvp);
1014 	} else {
1015 		if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1016 			panic("rename: EXDEV");
1017 		/*
1018 		 * Short circuit rename(foo, foo).
1019 		 */
1020 		if (xp->i_number == ip->i_number)
1021 			panic("ufs_rename: same file");
1022 		/*
1023 		 * If the parent directory is "sticky", then the user must
1024 		 * own the parent directory, or the destination of the rename,
1025 		 * otherwise the destination may not be changed (except by
1026 		 * root). This implements append-only directories.
1027 		 */
1028 		if ((dp->i_ffs_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
1029 		    tcnp->cn_cred->cr_uid != dp->i_ffs_uid &&
1030 		    xp->i_ffs_uid != tcnp->cn_cred->cr_uid) {
1031 			error = EPERM;
1032 			goto bad;
1033 		}
1034 		/*
1035 		 * Target must be empty if a directory and have no links
1036 		 * to it. Also, ensure source and target are compatible
1037 		 * (both directories, or both not directories).
1038 		 */
1039 		if ((xp->i_ffs_mode & IFMT) == IFDIR) {
1040 			if (xp->i_effnlink > 2 ||
1041 			    !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
1042 				error = ENOTEMPTY;
1043 				goto bad;
1044 			}
1045 			if (!doingdirectory) {
1046 				error = ENOTDIR;
1047 				goto bad;
1048 			}
1049 			cache_purge(tdvp);
1050 		} else if (doingdirectory) {
1051 			error = EISDIR;
1052 			goto bad;
1053 		}
1054 
1055 		if ((error = ufs_dirrewrite(dp, xp, ip->i_number,
1056                    IFTODT(ip->i_ffs_mode), (doingdirectory && newparent) ?
1057 		   newparent : doingdirectory)) != 0)
1058                         goto bad;
1059 		if (doingdirectory) {
1060 			if (!newparent) {
1061 				dp->i_effnlink--;
1062 				if (DOINGSOFTDEP(tdvp))
1063 					softdep_change_linkcnt(dp, 0);
1064 			}
1065 			xp->i_effnlink--;
1066 			if (DOINGSOFTDEP(tvp))
1067 				softdep_change_linkcnt(xp, 0);
1068 		}
1069 		if (doingdirectory && !DOINGSOFTDEP(tvp)) {
1070 		       /*
1071 			* Truncate inode. The only stuff left in the directory
1072 			* is "." and "..". The "." reference is inconsequential
1073                         * since we are quashing it. We have removed the "."
1074                         * reference and the reference in the parent directory,
1075                         * but there may be other hard links. The soft
1076                         * dependency code will arrange to do these operations
1077                         * after the parent directory entry has been deleted on
1078                         * disk, so when running with that code we avoid doing
1079                         * them now.
1080                         */
1081 			if (!newparent) {
1082 				dp->i_ffs_nlink--;
1083 				dp->i_flag |= IN_CHANGE;
1084 			}
1085 
1086 			xp->i_ffs_nlink--;
1087 			xp->i_flag |= IN_CHANGE;
1088 			if ((error = UFS_TRUNCATE(VTOI(tvp), (off_t)0, IO_SYNC,
1089 			        tcnp->cn_cred)) != 0)
1090 				goto bad;
1091                 }
1092 		VN_KNOTE(tdvp, NOTE_WRITE);
1093 	        vput(tdvp);
1094 		VN_KNOTE(tvp, NOTE_DELETE);
1095 		vput(tvp);
1096 		xp = NULL;
1097 	}
1098 
1099 	/*
1100 	 * 3) Unlink the source.
1101 	 */
1102 	fcnp->cn_flags &= ~MODMASK;
1103 	fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1104 	if ((fcnp->cn_flags & SAVESTART) == 0)
1105 		panic("ufs_rename: lost from startdir");
1106 	if ((error = relookup(fdvp, &fvp, fcnp)) != 0) {
1107 		vrele(ap->a_fvp);
1108 		return (error);
1109 	}
1110 	vrele(fdvp);
1111 	if (fvp == NULL) {
1112 		/*
1113 		 * From name has disappeared.
1114 		 */
1115 		if (doingdirectory)
1116 			panic("ufs_rename: lost dir entry");
1117 		vrele(ap->a_fvp);
1118 		return (0);
1119 	}
1120 
1121 	xp = VTOI(fvp);
1122 	dp = VTOI(fdvp);
1123 
1124 	/*
1125 	 * Ensure that the directory entry still exists and has not
1126 	 * changed while the new name has been entered. If the source is
1127 	 * a file then the entry may have been unlinked or renamed. In
1128 	 * either case there is no further work to be done. If the source
1129 	 * is a directory then it cannot have been rmdir'ed; the IN_RENAME
1130 	 * flag ensures that it cannot be moved by another rename or removed
1131 	 * by a rmdir.
1132 	 */
1133 	if (xp != ip) {
1134 		if (doingdirectory)
1135 			panic("ufs_rename: lost dir entry");
1136 	} else {
1137 		/*
1138 		 * If the source is a directory with a
1139 		 * new parent, the link count of the old
1140 		 * parent directory must be decremented
1141 		 * and ".." set to point to the new parent.
1142 		 */
1143 		if (doingdirectory && newparent) {
1144 			xp->i_offset = mastertemplate.dot_reclen;
1145 			ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0);
1146 			cache_purge(fdvp);
1147 		}
1148 		error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0);
1149 		xp->i_flag &= ~IN_RENAME;
1150 	}
1151 	VN_KNOTE(fvp, NOTE_RENAME);
1152 	if (dp)
1153 		vput(fdvp);
1154 	if (xp)
1155 		vput(fvp);
1156 	vrele(ap->a_fvp);
1157 	return (error);
1158 
1159 bad:
1160 	if (xp)
1161 		vput(ITOV(xp));
1162 	vput(ITOV(dp));
1163 out:
1164 	vrele(fdvp);
1165 	if (doingdirectory)
1166 		ip->i_flag &= ~IN_RENAME;
1167 	if (vn_lock(fvp, LK_EXCLUSIVE, p) == 0) {
1168 		ip->i_effnlink--;
1169 		ip->i_ffs_nlink--;
1170 		ip->i_flag |= IN_CHANGE;
1171 		ip->i_flag &= ~IN_RENAME;
1172 		if (DOINGSOFTDEP(fvp))
1173 			softdep_change_linkcnt(ip, 0);
1174 		vput(fvp);
1175 	} else
1176 		vrele(fvp);
1177 	return (error);
1178 }
1179 
1180 /*
1181  * Mkdir system call
1182  */
1183 int
ufs_mkdir(v)1184 ufs_mkdir(v)
1185 	void *v;
1186 {
1187 	struct vop_mkdir_args /* {
1188 		struct vnode *a_dvp;
1189 		struct vnode **a_vpp;
1190 		struct componentname *a_cnp;
1191 		struct vattr *a_vap;
1192 	} */ *ap = v;
1193 	struct vnode *dvp = ap->a_dvp;
1194 	struct vattr *vap = ap->a_vap;
1195 	struct componentname *cnp = ap->a_cnp;
1196 	struct inode *ip, *dp;
1197 	struct vnode *tvp;
1198 	struct buf *bp;
1199 	struct direct newdir;
1200 	struct dirtemplate dirtemplate, *dtp;
1201 	int error, dmode, blkoff;
1202 
1203 #ifdef DIAGNOSTIC
1204 	if ((cnp->cn_flags & HASBUF) == 0)
1205 		panic("ufs_mkdir: no name");
1206 #endif
1207 	dp = VTOI(dvp);
1208 	if ((nlink_t)dp->i_ffs_nlink >= LINK_MAX) {
1209 		error = EMLINK;
1210 		goto out;
1211 	}
1212 	dmode = vap->va_mode & 0777;
1213 	dmode |= IFDIR;
1214 	/*
1215 	 * Must simulate part of ufs_makeinode here to acquire the inode,
1216 	 * but not have it entered in the parent directory. The entry is
1217 	 * made later after writing "." and ".." entries.
1218 	 */
1219 	if ((error = UFS_INODE_ALLOC(dp, dmode, cnp->cn_cred, &tvp)) != 0)
1220 		goto out;
1221 	ip = VTOI(tvp);
1222 	ip->i_ffs_uid = cnp->cn_cred->cr_uid;
1223 	ip->i_ffs_gid = dp->i_ffs_gid;
1224 
1225 	if ((error = getinoquota(ip)) ||
1226 	    (error = ufs_quota_alloc_inode(ip, cnp->cn_cred))) {
1227 		pool_put(&namei_pool, cnp->cn_pnbuf);
1228 		UFS_INODE_FREE(ip, ip->i_number, dmode);
1229 		vput(tvp);
1230 		vput(dvp);
1231 		return (error);
1232 	}
1233 
1234 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1235 	ip->i_ffs_mode = dmode;
1236 	tvp->v_type = VDIR;	/* Rest init'd in getnewvnode(). */
1237 	ip->i_effnlink = 2;
1238 	ip->i_ffs_nlink = 2;
1239 	if (DOINGSOFTDEP(tvp))
1240 		softdep_change_linkcnt(ip, 0);
1241 
1242 	/*
1243 	 * Bump link count in parent directory to reflect work done below.
1244 	 * Should be done before reference is create so cleanup is
1245 	 * possible if we crash.
1246 	 */
1247 	dp->i_effnlink++;
1248 	dp->i_ffs_nlink++;
1249 	dp->i_flag |= IN_CHANGE;
1250 	if (DOINGSOFTDEP(dvp))
1251 		softdep_change_linkcnt(dp, 0);
1252 	if ((error = UFS_UPDATE(dp, !DOINGSOFTDEP(dvp))) != 0)
1253 		goto bad;
1254 
1255 	/*
1256 	 * Initialize directory with "." and ".." from static template.
1257 	 */
1258 	if (dvp->v_mount->mnt_maxsymlinklen > 0)
1259 		dtp = &mastertemplate;
1260 	else
1261 		dtp = (struct dirtemplate *)&omastertemplate;
1262 	dirtemplate = *dtp;
1263 	dirtemplate.dot_ino = ip->i_number;
1264 	dirtemplate.dotdot_ino = dp->i_number;
1265 
1266 	if ((error = UFS_BUF_ALLOC(ip, (off_t)0, DIRBLKSIZ, cnp->cn_cred,
1267             B_CLRBUF, &bp)) != 0)
1268 		goto bad;
1269 	ip->i_ffs_size = DIRBLKSIZ;
1270 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
1271 	uvm_vnp_setsize(tvp, ip->i_ffs_size);
1272 	bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
1273 	if (DOINGSOFTDEP(tvp)) {
1274 		/*
1275 		 * Ensure that the entire newly allocated block is a
1276 		 * valid directory so that future growth within the
1277 		 * block does not have to ensure that the block is
1278 		 * written before the inode
1279 		 */
1280 		blkoff = DIRBLKSIZ;
1281 		while (blkoff < bp->b_bcount) {
1282 			((struct direct *)
1283 			 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
1284 			blkoff += DIRBLKSIZ;
1285 		}
1286 	}
1287 	if ((error = UFS_UPDATE(ip, !DOINGSOFTDEP(tvp))) != 0) {
1288 		(void)VOP_BWRITE(bp);
1289 		goto bad;
1290 	}
1291 
1292 	/*
1293          * Directory set up, now install it's entry in the parent directory.
1294          *
1295          * If we are not doing soft dependencies, then we must write out the
1296          * buffer containing the new directory body before entering the new
1297          * name in the parent. If we are doing soft dependencies, then the
1298          * buffer containing the new directory body will be passed to and
1299          * released in the soft dependency code after the code has attached
1300          * an appropriate ordering dependency to the buffer which ensures that
1301          * the buffer is written before the new name is written in the parent.
1302 	 */
1303         if (!DOINGSOFTDEP(dvp) && ((error = VOP_BWRITE(bp)) != 0))
1304                 goto bad;
1305         ufs_makedirentry(ip, cnp, &newdir);
1306         error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
1307 
1308 bad:
1309         if (error == 0) {
1310 		VN_KNOTE(dvp, NOTE_WRITE);
1311                 *ap->a_vpp = tvp;
1312         } else {
1313                 dp->i_effnlink--;
1314                 dp->i_ffs_nlink--;
1315                 dp->i_flag |= IN_CHANGE;
1316 		if (DOINGSOFTDEP(dvp))
1317 			softdep_change_linkcnt(dp, 0);
1318                 /*
1319                  * No need to do an explicit VOP_TRUNCATE here, vrele will
1320                  * do this for us because we set the link count to 0.
1321                  */
1322                 ip->i_effnlink = 0;
1323                 ip->i_ffs_nlink = 0;
1324                 ip->i_flag |= IN_CHANGE;
1325 		if (DOINGSOFTDEP(tvp))
1326 			softdep_change_linkcnt(ip, 0);
1327 		vput(tvp);
1328 	}
1329 out:
1330 	pool_put(&namei_pool, cnp->cn_pnbuf);
1331 	vput(dvp);
1332 
1333 	return (error);
1334 }
1335 
1336 /*
1337  * Rmdir system call.
1338  */
1339 int
ufs_rmdir(v)1340 ufs_rmdir(v)
1341 	void *v;
1342 {
1343 	struct vop_rmdir_args /* {
1344 		struct vnode *a_dvp;
1345 		struct vnode *a_vp;
1346 		struct componentname *a_cnp;
1347 	} */ *ap = v;
1348 	struct vnode *vp = ap->a_vp;
1349 	struct vnode *dvp = ap->a_dvp;
1350 	struct componentname *cnp = ap->a_cnp;
1351 	struct inode *ip, *dp;
1352 	int error;
1353 
1354 	ip = VTOI(vp);
1355 	dp = VTOI(dvp);
1356 	/*
1357 	 * No rmdir "." or of mounted on directories.
1358 	 */
1359 	if (dp == ip || vp->v_mountedhere != 0) {
1360 		if (dp == ip)
1361 			vrele(dvp);
1362 		else
1363 			vput(dvp);
1364 		vput(vp);
1365 		return (EINVAL);
1366 	}
1367 	/*
1368          * Do not remove a directory that is in the process of being renamed.
1369          * Verify the directory is empty (and valid). Rmdir ".." will not be
1370          * valid since ".." will contain a reference to the current directory
1371          * and thus be non-empty.
1372 	 */
1373 	error = 0;
1374 	if (ip->i_flag & IN_RENAME) {
1375 		error = EINVAL;
1376 		goto out;
1377 	}
1378 	if (ip->i_effnlink != 2 ||
1379 	    !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1380 		error = ENOTEMPTY;
1381 		goto out;
1382 	}
1383 	if ((dp->i_ffs_flags & APPEND) ||
1384 		(ip->i_ffs_flags & (IMMUTABLE | APPEND))) {
1385 		error = EPERM;
1386 		goto out;
1387 	}
1388 	/*
1389 	 * Delete reference to directory before purging
1390 	 * inode.  If we crash in between, the directory
1391 	 * will be reattached to lost+found,
1392 	 */
1393 	dp->i_effnlink--;
1394 	ip->i_effnlink--;
1395 	if (DOINGSOFTDEP(vp)) {
1396 		softdep_change_linkcnt(dp, 0);
1397 		softdep_change_linkcnt(ip, 0);
1398 	}
1399 	if ((error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1)) != 0) {
1400 		dp->i_effnlink++;
1401 		ip->i_effnlink++;
1402 		if (DOINGSOFTDEP(vp)) {
1403 			softdep_change_linkcnt(dp, 0);
1404 			softdep_change_linkcnt(ip, 0);
1405 		}
1406 		goto out;
1407 	}
1408 
1409 	VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1410 	cache_purge(dvp);
1411         /*
1412 	 * Truncate inode. The only stuff left in the directory is "." and
1413 	 * "..". The "." reference is inconsequential since we are quashing
1414 	 * it. The soft dependency code will arrange to do these operations
1415 	 * after the parent directory entry has been deleted on disk, so
1416 	 * when running with that code we avoid doing them now.
1417 	 */
1418 	if (!DOINGSOFTDEP(vp)) {
1419 		int ioflag;
1420 
1421 		dp->i_ffs_nlink--;
1422 		dp->i_flag |= IN_CHANGE;
1423 		ip->i_ffs_nlink--;
1424 		ip->i_flag |= IN_CHANGE;
1425 		ioflag = DOINGASYNC(vp) ? 0 : IO_SYNC;
1426 		error = UFS_TRUNCATE(ip, (off_t)0, ioflag, cnp->cn_cred);
1427 	}
1428 	cache_purge(vp);
1429 #ifdef UFS_DIRHASH
1430 	/* Kill any active hash; i_effnlink == 0, so it will not come back. */
1431 	if (ip->i_dirhash != NULL)
1432 		ufsdirhash_free(ip);
1433 #endif
1434 
1435 out:
1436 	VN_KNOTE(vp, NOTE_DELETE);
1437         vput(dvp);
1438 	vput(vp);
1439 	return (error);
1440 }
1441 
1442 /*
1443  * symlink -- make a symbolic link
1444  */
1445 int
ufs_symlink(v)1446 ufs_symlink(v)
1447 	void *v;
1448 {
1449 	struct vop_symlink_args /* {
1450 		struct vnode *a_dvp;
1451 		struct vnode **a_vpp;
1452 		struct componentname *a_cnp;
1453 		struct vattr *a_vap;
1454 		char *a_target;
1455 	} */ *ap = v;
1456 	struct vnode *vp, **vpp = ap->a_vpp;
1457 	struct inode *ip;
1458 	int len, error;
1459 
1460 	error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1461 			      vpp, ap->a_cnp);
1462 	if (error)
1463 		return (error);
1464 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
1465 	vp = *vpp;
1466 	len = strlen(ap->a_target);
1467 	if (len < vp->v_mount->mnt_maxsymlinklen) {
1468 		ip = VTOI(vp);
1469 		bcopy(ap->a_target, (char *)ip->i_ffs_shortlink, len);
1470 		ip->i_ffs_size = len;
1471 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
1472 	} else
1473 		error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1474 		    UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred, NULL,
1475 		    (struct proc *)0);
1476 	vput(vp);
1477 	return (error);
1478 }
1479 
1480 /*
1481  * Vnode op for reading directories.
1482  *
1483  * The routine below assumes that the on-disk format of a directory
1484  * is the same as that defined by <sys/dirent.h>. If the on-disk
1485  * format changes, then it will be necessary to do a conversion
1486  * from the on-disk format that read returns to the format defined
1487  * by <sys/dirent.h>.
1488  */
1489 int
ufs_readdir(v)1490 ufs_readdir(v)
1491 	void *v;
1492 {
1493 	struct vop_readdir_args /* {
1494 		struct vnode *a_vp;
1495 		struct uio *a_uio;
1496 		struct ucred *a_cred;
1497 		int *a_eofflag;
1498 		u_long **a_cookies;
1499 		int *ncookies;
1500 	} */ *ap = v;
1501 	struct uio *uio = ap->a_uio;
1502 	int error;
1503 	size_t count, lost;
1504 	off_t off = uio->uio_offset;
1505 
1506 	count = uio->uio_resid;
1507 	/* Make sure we don't return partial entries. */
1508 	count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
1509 	if (count <= 0)
1510 		return (EINVAL);
1511 	lost = uio->uio_resid - count;
1512 	uio->uio_resid = count;
1513 	uio->uio_iov->iov_len = count;
1514 #	if (BYTE_ORDER == LITTLE_ENDIAN)
1515 		if (ap->a_vp->v_mount->mnt_maxsymlinklen > 0) {
1516 			error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1517 		} else {
1518 			struct dirent *dp, *edp;
1519 			struct uio auio;
1520 			struct iovec aiov;
1521 			caddr_t dirbuf;
1522 			int readcnt;
1523 			u_char tmp;
1524 
1525 			auio = *uio;
1526 			auio.uio_iov = &aiov;
1527 			auio.uio_iovcnt = 1;
1528 			auio.uio_segflg = UIO_SYSSPACE;
1529 			aiov.iov_len = count;
1530 			MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK);
1531 			aiov.iov_base = dirbuf;
1532 			error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
1533 			if (error == 0) {
1534 				readcnt = count - auio.uio_resid;
1535 				edp = (struct dirent *)&dirbuf[readcnt];
1536 				for (dp = (struct dirent *)dirbuf; dp < edp; ) {
1537 					tmp = dp->d_namlen;
1538 					dp->d_namlen = dp->d_type;
1539 					dp->d_type = tmp;
1540 					if (dp->d_reclen > 0) {
1541 						dp = (struct dirent *)
1542 						    ((char *)dp + dp->d_reclen);
1543 					} else {
1544 						error = EIO;
1545 						break;
1546 					}
1547 				}
1548 				if (dp >= edp)
1549 					error = uiomove(dirbuf, readcnt, uio);
1550 			}
1551 			FREE(dirbuf, M_TEMP);
1552 		}
1553 #	else
1554 		error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1555 #	endif
1556 	if (!error && ap->a_ncookies) {
1557 		struct dirent *dp, *dpstart;
1558 		off_t offstart;
1559 		u_long *cookies;
1560 		int ncookies;
1561 
1562 		/*
1563 		 * Only the NFS server and emulations use cookies, and they
1564 		 * load the directory block into system space, so we can
1565 		 * just look at it directly.
1566 		 */
1567 		if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1568 			panic("ufs_readdir: lost in space");
1569 
1570 		dpstart = (struct dirent *)
1571 			(uio->uio_iov->iov_base - (uio->uio_offset - off));
1572                 offstart = off;
1573                 for (dp = dpstart, ncookies = 0; off < uio->uio_offset; ) {
1574                         if (dp->d_reclen == 0)
1575                                 break;
1576                         off += dp->d_reclen;
1577                         ncookies++;
1578                         dp = (struct dirent *)((caddr_t)dp + dp->d_reclen);
1579                 }
1580                 lost += uio->uio_offset - off;
1581                 uio->uio_offset = off;
1582                 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1583                     M_WAITOK);
1584                 *ap->a_ncookies = ncookies;
1585                 *ap->a_cookies = cookies;
1586                 for (off = offstart, dp = dpstart; off < uio->uio_offset; ) {
1587 			off += dp->d_reclen;
1588                         *cookies = off;
1589 			cookies++;
1590                         dp = (struct dirent *)((caddr_t)dp + dp->d_reclen);
1591 		}
1592 	}
1593 	uio->uio_resid += lost;
1594 	*ap->a_eofflag = VTOI(ap->a_vp)->i_ffs_size <= uio->uio_offset;
1595 	return (error);
1596 }
1597 
1598 /*
1599  * Return target name of a symbolic link
1600  */
1601 int
ufs_readlink(v)1602 ufs_readlink(v)
1603 	void *v;
1604 {
1605 	struct vop_readlink_args /* {
1606 		struct vnode *a_vp;
1607 		struct uio *a_uio;
1608 		struct ucred *a_cred;
1609 	} */ *ap = v;
1610 	struct vnode *vp = ap->a_vp;
1611 	struct inode *ip = VTOI(vp);
1612 	int isize;
1613 
1614 	isize = ip->i_ffs_size;
1615 	if (isize < vp->v_mount->mnt_maxsymlinklen ||
1616 	    (vp->v_mount->mnt_maxsymlinklen == 0 && ip->i_ffs_blocks == 0)) {
1617 		uiomove((char *)ip->i_ffs_shortlink, isize, ap->a_uio);
1618 		return (0);
1619 	}
1620 	return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1621 }
1622 
1623 /*
1624  * Lock an inode. If its already locked, set the WANT bit and sleep.
1625  */
1626 int
ufs_lock(v)1627 ufs_lock(v)
1628 	void *v;
1629 {
1630 	struct vop_lock_args /* {
1631 		struct vnode *a_vp;
1632 		int a_flags;
1633 		struct proc *a_p;
1634 	} */ *ap = v;
1635 	struct vnode *vp = ap->a_vp;
1636 
1637 	return (lockmgr(&VTOI(vp)->i_lock, ap->a_flags, &vp->v_interlock,
1638 		ap->a_p));
1639 }
1640 
1641 /*
1642  * Unlock an inode.  If WANT bit is on, wakeup.
1643  */
1644 int
ufs_unlock(v)1645 ufs_unlock(v)
1646 	void *v;
1647 {
1648 	struct vop_unlock_args /* {
1649 		struct vnode *a_vp;
1650 		int a_flags;
1651 		struct proc *a_p;
1652 	} */ *ap = v;
1653 	struct vnode *vp = ap->a_vp;
1654 
1655 	return (lockmgr(&VTOI(vp)->i_lock, ap->a_flags | LK_RELEASE,
1656 		&vp->v_interlock, ap->a_p));
1657 }
1658 
1659 /*
1660  * Check for a locked inode.
1661  */
1662 int
ufs_islocked(v)1663 ufs_islocked(v)
1664 	void *v;
1665 {
1666 	struct vop_islocked_args /* {
1667 		struct vnode *a_vp;
1668 	} */ *ap = v;
1669 
1670 	return (lockstatus(&VTOI(ap->a_vp)->i_lock));
1671 }
1672 
1673 /*
1674  * Calculate the logical to physical mapping if not done already,
1675  * then call the device strategy routine.
1676  */
1677 int
ufs_strategy(v)1678 ufs_strategy(v)
1679 	void *v;
1680 {
1681 	struct vop_strategy_args /* {
1682 		struct buf *a_bp;
1683 	} */ *ap = v;
1684 	struct buf *bp = ap->a_bp;
1685 	struct vnode *vp = bp->b_vp;
1686 	struct inode *ip;
1687 	int error;
1688 	int s;
1689 
1690 	ip = VTOI(vp);
1691 	if (vp->v_type == VBLK || vp->v_type == VCHR)
1692 		panic("ufs_strategy: spec");
1693 	if (bp->b_blkno == bp->b_lblkno) {
1694 		error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
1695 				 NULL);
1696 		if (error) {
1697 			bp->b_error = error;
1698 			bp->b_flags |= B_ERROR;
1699 			s = splbio();
1700 			biodone(bp);
1701 			splx(s);
1702 			return (error);
1703 		}
1704 		if ((long)bp->b_blkno == -1)
1705 			clrbuf(bp);
1706 	}
1707 	if ((long)bp->b_blkno == -1) {
1708 		s = splbio();
1709 		biodone(bp);
1710 		splx(s);
1711 		return (0);
1712 	}
1713 	vp = ip->i_devvp;
1714 	bp->b_dev = vp->v_rdev;
1715 	VOCALL (vp->v_op, VOFFSET(vop_strategy), ap);
1716 	return (0);
1717 }
1718 
1719 /*
1720  * Print out the contents of an inode.
1721  */
1722 int
ufs_print(v)1723 ufs_print(v)
1724 	void *v;
1725 {
1726 	struct vop_print_args /* {
1727 		struct vnode *a_vp;
1728 	} */ *ap = v;
1729 	struct vnode *vp = ap->a_vp;
1730 	struct inode *ip = VTOI(vp);
1731 
1732 	printf("tag VT_UFS, ino %d, on dev %d, %d", ip->i_number,
1733 		major(ip->i_dev), minor(ip->i_dev));
1734 	printf(" flags 0x%x, effnlink %d, nlink %d\n",
1735 	       ip->i_flag, ip->i_effnlink, ip->i_ffs_nlink);
1736 	printf("\tmode 0%o, owner %d, group %d, size %lld",
1737 	       ip->i_ffs_mode, ip->i_ffs_uid, ip->i_ffs_gid, ip->i_ffs_size);
1738 
1739 #ifdef FIFO
1740 	if (vp->v_type == VFIFO)
1741 		fifo_printinfo(vp);
1742 #endif /* FIFO */
1743 	lockmgr_printinfo(&ip->i_lock);
1744 	printf("\n");
1745 	return (0);
1746 }
1747 
1748 /*
1749  * Read wrapper for special devices.
1750  */
1751 int
ufsspec_read(v)1752 ufsspec_read(v)
1753 	void *v;
1754 {
1755 	struct vop_read_args /* {
1756 		struct vnode *a_vp;
1757 		struct uio *a_uio;
1758 		int  a_ioflag;
1759 		struct ucred *a_cred;
1760 	} */ *ap = v;
1761 
1762 	/*
1763 	 * Set access flag.
1764 	 */
1765 	VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1766 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_read), ap));
1767 }
1768 
1769 /*
1770  * Write wrapper for special devices.
1771  */
1772 int
ufsspec_write(v)1773 ufsspec_write(v)
1774 	void *v;
1775 {
1776 	struct vop_write_args /* {
1777 		struct vnode *a_vp;
1778 		struct uio *a_uio;
1779 		int  a_ioflag;
1780 		struct ucred *a_cred;
1781 	} */ *ap = v;
1782 
1783 	/*
1784 	 * Set update and change flags.
1785 	 */
1786 	VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1787 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_write), ap));
1788 }
1789 
1790 /*
1791  * Close wrapper for special devices.
1792  *
1793  * Update the times on the inode then do device close.
1794  */
1795 int
ufsspec_close(v)1796 ufsspec_close(v)
1797 	void *v;
1798 {
1799 	struct vop_close_args /* {
1800 		struct vnode *a_vp;
1801 		int  a_fflag;
1802 		struct ucred *a_cred;
1803 		struct proc *a_p;
1804 	} */ *ap = v;
1805 	struct vnode *vp = ap->a_vp;
1806 	struct inode *ip = VTOI(vp);
1807 
1808 	simple_lock(&vp->v_interlock);
1809 	if (ap->a_vp->v_usecount > 1)
1810 		ITIMES(ip, &time, &time);
1811 	simple_unlock(&vp->v_interlock);
1812 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
1813 }
1814 
1815 #ifdef FIFO
1816 /*
1817  * Read wrapper for fifo's
1818  */
1819 int
ufsfifo_read(v)1820 ufsfifo_read(v)
1821 	void *v;
1822 {
1823 	struct vop_read_args /* {
1824 		struct vnode *a_vp;
1825 		struct uio *a_uio;
1826 		int  a_ioflag;
1827 		struct ucred *a_cred;
1828 	} */ *ap = v;
1829 	extern int (**fifo_vnodeop_p)(void *);
1830 
1831 	/*
1832 	 * Set access flag.
1833 	 */
1834 	VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1835 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_read), ap));
1836 }
1837 
1838 /*
1839  * Write wrapper for fifo's.
1840  */
1841 int
ufsfifo_write(v)1842 ufsfifo_write(v)
1843 	void *v;
1844 {
1845 	struct vop_write_args /* {
1846 		struct vnode *a_vp;
1847 		struct uio *a_uio;
1848 		int  a_ioflag;
1849 		struct ucred *a_cred;
1850 	} */ *ap = v;
1851 	extern int (**fifo_vnodeop_p)(void *);
1852 
1853 	/*
1854 	 * Set update and change flags.
1855 	 */
1856 	VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1857 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_write), ap));
1858 }
1859 
1860 /*
1861  * Close wrapper for fifo's.
1862  *
1863  * Update the times on the inode then do device close.
1864  */
1865 int
ufsfifo_close(v)1866 ufsfifo_close(v)
1867 	void *v;
1868 {
1869 	struct vop_close_args /* {
1870 		struct vnode *a_vp;
1871 		int  a_fflag;
1872 		struct ucred *a_cred;
1873 		struct proc *a_p;
1874 	} */ *ap = v;
1875 	extern int (**fifo_vnodeop_p)(void *);
1876 	struct vnode *vp = ap->a_vp;
1877 	struct inode *ip = VTOI(vp);
1878 
1879 	simple_lock(&vp->v_interlock);
1880 	if (ap->a_vp->v_usecount > 1)
1881 		ITIMES(ip, &time, &time);
1882 	simple_unlock(&vp->v_interlock);
1883 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
1884 }
1885 #endif /* FIFO */
1886 
1887 /*
1888  * Return POSIX pathconf information applicable to ufs filesystems.
1889  */
1890 int
ufs_pathconf(v)1891 ufs_pathconf(v)
1892 	void *v;
1893 {
1894 	struct vop_pathconf_args /* {
1895 		struct vnode *a_vp;
1896 		int a_name;
1897 		register_t *a_retval;
1898 	} */ *ap = v;
1899 
1900 	switch (ap->a_name) {
1901 	case _PC_LINK_MAX:
1902 		*ap->a_retval = LINK_MAX;
1903 		return (0);
1904 	case _PC_NAME_MAX:
1905 		*ap->a_retval = NAME_MAX;
1906 		return (0);
1907 	case _PC_PATH_MAX:
1908 		*ap->a_retval = PATH_MAX;
1909 		return (0);
1910 	case _PC_PIPE_BUF:
1911 		*ap->a_retval = PIPE_BUF;
1912 		return (0);
1913 	case _PC_CHOWN_RESTRICTED:
1914 		*ap->a_retval = 1;
1915 		return (0);
1916 	case _PC_NO_TRUNC:
1917 		*ap->a_retval = 1;
1918 		return (0);
1919 	default:
1920 		return (EINVAL);
1921 	}
1922 	/* NOTREACHED */
1923 }
1924 
1925 /*
1926  * Advisory record locking support
1927  */
1928 int
ufs_advlock(v)1929 ufs_advlock(v)
1930 	void *v;
1931 {
1932 	struct vop_advlock_args /* {
1933 		struct vnode *a_vp;
1934 		caddr_t  a_id;
1935 		int  a_op;
1936 		struct flock *a_fl;
1937 		int  a_flags;
1938 	} */ *ap = v;
1939 	struct inode *ip = VTOI(ap->a_vp);
1940 
1941 	return (lf_advlock(&ip->i_lockf, ip->i_ffs_size, ap->a_id, ap->a_op,
1942 	    ap->a_fl, ap->a_flags));
1943 }
1944 
1945 /*
1946  * Initialize the vnode associated with a new inode, handle aliased
1947  * vnodes.
1948  */
1949 int
ufs_vinit(mntp,specops,fifoops,vpp)1950 ufs_vinit(mntp, specops, fifoops, vpp)
1951 	struct mount *mntp;
1952 	int (**specops)(void *);
1953 	int (**fifoops)(void *);
1954 	struct vnode **vpp;
1955 {
1956 	struct inode *ip;
1957 	struct vnode *vp, *nvp;
1958 
1959 	vp = *vpp;
1960 	ip = VTOI(vp);
1961 	switch(vp->v_type = IFTOVT(ip->i_ffs_mode)) {
1962 	case VCHR:
1963 	case VBLK:
1964 		vp->v_op = specops;
1965 		if ((nvp = checkalias(vp, ip->i_ffs_rdev, mntp)) != NULL) {
1966 			/*
1967 			 * Discard unneeded vnode, but save its inode.
1968 			 * Note that the lock is carried over in the inode
1969 			 * to the replacement vnode.
1970 			 */
1971 			nvp->v_data = vp->v_data;
1972 			vp->v_data = NULL;
1973 			vp->v_op = spec_vnodeop_p;
1974 #ifdef VFSDEBUG
1975 			vp->v_flag &= ~VLOCKSWORK;
1976 #endif
1977 			vrele(vp);
1978 			vgone(vp);
1979 			/*
1980 			 * Reinitialize aliased inode.
1981 			 */
1982 			vp = nvp;
1983 			ip->i_vnode = vp;
1984 		}
1985 		break;
1986 	case VFIFO:
1987 #ifdef FIFO
1988 		vp->v_op = fifoops;
1989 		break;
1990 #else
1991 		return (EOPNOTSUPP);
1992 #endif
1993 	case VNON:
1994 	case VBAD:
1995 	case VSOCK:
1996 	case VLNK:
1997 	case VDIR:
1998 	case VREG:
1999 		break;
2000 	}
2001 	if (ip->i_number == ROOTINO)
2002                 vp->v_flag |= VROOT;
2003 	/*
2004 	 * Initialize modrev times
2005 	 */
2006 	SETHIGH(ip->i_modrev, mono_time.tv_sec);
2007 	SETLOW(ip->i_modrev, mono_time.tv_usec * 4294);
2008 	*vpp = vp;
2009 	return (0);
2010 }
2011 
2012 /*
2013  * Allocate a new inode.
2014  */
2015 int
ufs_makeinode(mode,dvp,vpp,cnp)2016 ufs_makeinode(mode, dvp, vpp, cnp)
2017 	int mode;
2018 	struct vnode *dvp;
2019 	struct vnode **vpp;
2020 	struct componentname *cnp;
2021 {
2022 	struct inode *ip, *pdir;
2023 	struct direct newdir;
2024 	struct vnode *tvp;
2025 	int error;
2026 
2027 	pdir = VTOI(dvp);
2028 #ifdef DIAGNOSTIC
2029 	if ((cnp->cn_flags & HASBUF) == 0)
2030 		panic("ufs_makeinode: no name");
2031 #endif
2032 	*vpp = NULL;
2033 	if ((mode & IFMT) == 0)
2034 		mode |= IFREG;
2035 
2036 	if ((error = UFS_INODE_ALLOC(pdir, mode, cnp->cn_cred, &tvp)) != 0) {
2037 		pool_put(&namei_pool, cnp->cn_pnbuf);
2038 		vput(dvp);
2039 		return (error);
2040 	}
2041 	ip = VTOI(tvp);
2042 	ip->i_ffs_gid = pdir->i_ffs_gid;
2043 	ip->i_ffs_uid = cnp->cn_cred->cr_uid;
2044 
2045 	if ((error = getinoquota(ip)) ||
2046 	    (error = ufs_quota_alloc_inode(ip, cnp->cn_cred))) {
2047 		pool_put(&namei_pool, cnp->cn_pnbuf);
2048 		UFS_INODE_FREE(ip, ip->i_number, mode);
2049 		vput(tvp);
2050 		vput(dvp);
2051 		return (error);
2052 	}
2053 
2054 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2055 	ip->i_ffs_mode = mode;
2056 	tvp->v_type = IFTOVT(mode);	/* Rest init'd in getnewvnode(). */
2057 	ip->i_effnlink = 1;
2058 	ip->i_ffs_nlink = 1;
2059 	if (DOINGSOFTDEP(tvp))
2060 		softdep_change_linkcnt(ip, 0);
2061 	if ((ip->i_ffs_mode & ISGID) &&
2062 		!groupmember(ip->i_ffs_gid, cnp->cn_cred) &&
2063 	    suser_ucred(cnp->cn_cred))
2064 		ip->i_ffs_mode &= ~ISGID;
2065 
2066 	/*
2067 	 * Make sure inode goes to disk before directory entry.
2068 	 */
2069 	if ((error = UFS_UPDATE(ip, !DOINGSOFTDEP(tvp))) != 0)
2070 		goto bad;
2071 
2072 	ufs_makedirentry(ip, cnp, &newdir);
2073 	if ((error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL)) != 0)
2074 		goto bad;
2075 
2076 	if ((cnp->cn_flags & SAVESTART) == 0)
2077 		pool_put(&namei_pool, cnp->cn_pnbuf);
2078 	vput(dvp);
2079 	*vpp = tvp;
2080 	return (0);
2081 
2082 bad:
2083 	/*
2084 	 * Write error occurred trying to update the inode
2085 	 * or the directory so must deallocate the inode.
2086 	 */
2087 	pool_put(&namei_pool, cnp->cn_pnbuf);
2088 	vput(dvp);
2089 	ip->i_effnlink = 0;
2090 	ip->i_ffs_nlink = 0;
2091 	ip->i_flag |= IN_CHANGE;
2092 	if (DOINGSOFTDEP(tvp))
2093 		softdep_change_linkcnt(ip, 0);
2094 	tvp->v_type = VNON;
2095 	vput(tvp);
2096 
2097 	return (error);
2098 }
2099 
2100 struct filterops ufsread_filtops =
2101 	{ 1, NULL, filt_ufsdetach, filt_ufsread };
2102 struct filterops ufswrite_filtops =
2103 	{ 1, NULL, filt_ufsdetach, filt_ufswrite };
2104 struct filterops ufsvnode_filtops =
2105 	{ 1, NULL, filt_ufsdetach, filt_ufsvnode };
2106 
2107 int
ufs_kqfilter(v)2108 ufs_kqfilter(v)
2109 	void *v;
2110 {
2111 	struct vop_kqfilter_args /* {
2112 		struct vnode *a_vp;
2113 		struct knote *a_kn;
2114 	} */ *ap = v;
2115 	struct vnode *vp = ap->a_vp;
2116 	struct knote *kn = ap->a_kn;
2117 
2118 	switch (kn->kn_filter) {
2119 	case EVFILT_READ:
2120 		kn->kn_fop = &ufsread_filtops;
2121 		break;
2122 	case EVFILT_WRITE:
2123 		kn->kn_fop = &ufswrite_filtops;
2124 		break;
2125 	case EVFILT_VNODE:
2126 		kn->kn_fop = &ufsvnode_filtops;
2127 		break;
2128 	default:
2129 		return (1);
2130 	}
2131 
2132 	kn->kn_hook = (caddr_t)vp;
2133 
2134 	simple_lock(&vp->v_selectinfo.vsi_lock);
2135 	SLIST_INSERT_HEAD(&vp->v_selectinfo.vsi_selinfo.si_note, kn, kn_selnext);
2136 	simple_unlock(&vp->v_selectinfo.vsi_lock);
2137 
2138 	return (0);
2139 }
2140 
2141 void
filt_ufsdetach(struct knote * kn)2142 filt_ufsdetach(struct knote *kn)
2143 {
2144 	struct vnode *vp = (struct vnode *)kn->kn_hook;
2145 
2146 	simple_lock(&vp->v_selectinfo.vsi_lock);
2147 	SLIST_REMOVE(&vp->v_selectinfo.vsi_selinfo.si_note,
2148 	    kn, knote, kn_selnext);
2149 	simple_unlock(&vp->v_selectinfo.vsi_lock);
2150 }
2151 
2152 /*ARGSUSED*/
2153 int
filt_ufsread(struct knote * kn,long hint)2154 filt_ufsread(struct knote *kn, long hint)
2155 {
2156 	struct vnode *vp = (struct vnode *)kn->kn_hook;
2157 	struct inode *ip = VTOI(vp);
2158 
2159 	/*
2160 	 * filesystem is gone, so set the EOF flag and schedule
2161 	 * the knote for deletion.
2162 	 */
2163 	if (hint == NOTE_REVOKE) {
2164 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2165 		return (1);
2166 	}
2167 
2168         kn->kn_data = ip->i_ffs_size - kn->kn_fp->f_offset;
2169 	if (kn->kn_data == 0 && kn->kn_sfflags & NOTE_EOF) {
2170 		kn->kn_fflags |= NOTE_EOF;
2171 		return (1);
2172 	}
2173 
2174         return (kn->kn_data != 0);
2175 }
2176 
2177 int
filt_ufswrite(struct knote * kn,long hint)2178 filt_ufswrite(struct knote *kn, long hint)
2179 {
2180 	/*
2181 	 * filesystem is gone, so set the EOF flag and schedule
2182 	 * the knote for deletion.
2183 	 */
2184 	if (hint == NOTE_REVOKE) {
2185 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2186 		return (1);
2187 	}
2188 
2189         kn->kn_data = 0;
2190         return (1);
2191 }
2192 
2193 int
filt_ufsvnode(struct knote * kn,long hint)2194 filt_ufsvnode(struct knote *kn, long hint)
2195 {
2196 	if (kn->kn_sfflags & hint)
2197 		kn->kn_fflags |= hint;
2198 	if (hint == NOTE_REVOKE) {
2199 		kn->kn_flags |= EV_EOF;
2200 		return (1);
2201 	}
2202 	return (kn->kn_fflags != 0);
2203 }
2204