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