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