new namei calling convention
[unix-history] / usr / src / sys / ufs / ufs / ufs_vnops.c
CommitLineData
da7c5cc6 1/*
7188ac27
KM
2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3 * All rights reserved.
da7c5cc6 4 *
b702c21d 5 * %sccs.include.redist.c%
7188ac27 6 *
d7961b1d 7 * @(#)ufs_vnops.c 7.74 (Berkeley) %G%
da7c5cc6 8 */
6459ebe0 9
031fd966
KB
10#include <sys/param.h>
11#include <sys/systm.h>
12#include <sys/namei.h>
13#include <sys/resourcevar.h>
14#include <sys/kernel.h>
15#include <sys/file.h>
16#include <sys/stat.h>
17#include <sys/buf.h>
18#include <sys/proc.h>
19#include <sys/conf.h>
20#include <sys/mount.h>
21#include <sys/vnode.h>
22#include <sys/specdev.h>
23#include <sys/fifo.h>
24#include <sys/malloc.h>
25
26#include <ufs/ufs/lockf.h>
27#include <ufs/ufs/quota.h>
28#include <ufs/ufs/inode.h>
29#include <ufs/ufs/dir.h>
30#include <ufs/ufs/ufsmount.h>
31#include <ufs/ufs/ufs_extern.h>
32
1a3cb193
KM
33int ufs_chmod __P((struct vnode *, int, struct proc *));
34int ufs_chown __P((struct vnode *, u_int, u_int, struct proc *));
031fd966 35
031fd966
KB
36enum vtype iftovt_tab[16] = {
37 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
38 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
39};
40int vttoif_tab[9] = {
41 0, IFREG, IFDIR, IFBLK, IFCHR, IFLNK, IFSOCK, IFIFO, IFMT,
42};
3e78e260 43
1a3cb193
KM
44#ifdef _NOQUAD
45#define SETHIGH(q, h) (q).val[_QUAD_HIGHWORD] = (h)
46#define SETLOW(q, l) (q).val[_QUAD_LOWWORD] = (l)
47#else /* QUAD */
48union _qcvt {
49 quad_t qcvt;
50 long val[2];
51};
52#define SETHIGH(q, h) { \
53 union _qcvt tmp; \
54 tmp.qcvt = (q); \
55 tmp.val[_QUAD_HIGHWORD] = (h); \
56 (q) = tmp.qcvt; \
57}
58#define SETLOW(q, l) { \
59 union _qcvt tmp; \
60 tmp.qcvt = (q); \
61 tmp.val[_QUAD_LOWWORD] = (l); \
62 (q) = tmp.qcvt; \
63}
64#endif /* QUAD */
65
4f083fd7 66/*
7188ac27 67 * Create a regular file
4f083fd7 68 */
031fd966 69int
6e57e9ff 70ufs_create(dvp, vpp, cnp, vap)
cfef4373
JH
71 struct vnode *dvp;
72 struct vnode **vpp;
73 struct componentname *cnp;
7188ac27 74 struct vattr *vap;
3e78e260 75{
7188ac27 76 int error;
3e78e260 77
031fd966 78 if (error =
cfef4373 79 ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode), dvp, vpp, cnp))
7188ac27 80 return (error);
7188ac27 81 return (0);
3e78e260
BJ
82}
83
4f083fd7 84/*
7188ac27 85 * Mknod vnode call
4f083fd7 86 */
7188ac27 87/* ARGSUSED */
031fd966 88int
6e57e9ff 89ufs_mknod(dvp, vpp, cnp, vap)
cfef4373
JH
90 struct vnode *dvp;
91 struct vnode **vpp;
92 struct componentname *cnp;
7188ac27 93 struct vattr *vap;
3e78e260 94{
c59af027 95 register struct inode *ip;
7188ac27 96 int error;
3e78e260 97
031fd966 98 if (error =
cfef4373 99 ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode), dvp, vpp, cnp))
7188ac27 100 return (error);
cfef4373 101 ip = VTOI(*vpp);
d1c43d7f
KM
102 ip->i_flag |= IACC|IUPD|ICHG;
103 if (vap->va_rdev != VNOVAL) {
7188ac27
KM
104 /*
105 * Want to be able to use this to make badblock
106 * inodes, so don't truncate the dev number.
107 */
b373e060 108 ip->i_rdev = vap->va_rdev;
7188ac27 109 }
7188ac27
KM
110 /*
111 * Remove inode so that it will be reloaded by iget and
112 * checked to see if it is an alias of an existing entry
113 * in the inode cache.
114 */
cfef4373
JH
115 vput(*vpp);
116 (*vpp)->v_type = VNON;
117 vgone(*vpp);
118 *vpp = 0;
7188ac27 119 return (0);
3e78e260
BJ
120}
121
122/*
7188ac27
KM
123 * Open called.
124 *
125 * Nothing to do.
3e78e260 126 */
7188ac27 127/* ARGSUSED */
031fd966 128int
5b169cb7 129ufs_open(vp, mode, cred, p)
7188ac27
KM
130 struct vnode *vp;
131 int mode;
132 struct ucred *cred;
5b169cb7 133 struct proc *p;
3e78e260 134{
3e78e260 135
7188ac27 136 return (0);
3e78e260
BJ
137}
138
139/*
7188ac27
KM
140 * Close called
141 *
142 * Update the times on the inode.
3e78e260 143 */
7188ac27 144/* ARGSUSED */
031fd966 145int
5b169cb7 146ufs_close(vp, fflag, cred, p)
7188ac27
KM
147 struct vnode *vp;
148 int fflag;
149 struct ucred *cred;
5b169cb7 150 struct proc *p;
3e78e260 151{
031fd966 152 register struct inode *ip;
3e78e260 153
031fd966 154 ip = VTOI(vp);
de67eefc 155 if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
7188ac27
KM
156 ITIMES(ip, &time, &time);
157 return (0);
3e78e260
BJ
158}
159
4b61628b
KM
160/*
161 * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
162 * The mode is shifted to select the owner/group/other fields. The
163 * super user is granted all permissions.
164 */
031fd966 165int
5b169cb7 166ufs_access(vp, mode, cred, p)
7188ac27 167 struct vnode *vp;
4b61628b 168 register int mode;
7188ac27 169 struct ucred *cred;
5b169cb7 170 struct proc *p;
3e78e260 171{
4b61628b
KM
172 register struct inode *ip = VTOI(vp);
173 register gid_t *gp;
174 int i, error;
3e78e260 175
4b61628b
KM
176#ifdef DIAGNOSTIC
177 if (!VOP_ISLOCKED(vp)) {
178 vprint("ufs_access: not locked", vp);
179 panic("ufs_access: not locked");
180 }
181#endif
182#ifdef QUOTA
183 if (mode & VWRITE) {
184 switch (vp->v_type) {
185 case VREG: case VDIR: case VLNK:
186 if (error = getinoquota(ip))
187 return (error);
188 }
189 }
190#endif /* QUOTA */
191 /*
192 * If you're the super-user, you always get access.
193 */
194 if (cred->cr_uid == 0)
195 return (0);
196 /*
197 * Access check is based on only one of owner, group, public.
198 * If not owner, then check group. If not a member of the
199 * group, then check public access.
200 */
201 if (cred->cr_uid != ip->i_uid) {
202 mode >>= 3;
203 gp = cred->cr_groups;
204 for (i = 0; i < cred->cr_ngroups; i++, gp++)
205 if (ip->i_gid == *gp)
206 goto found;
207 mode >>= 3;
208found:
209 ;
210 }
211 if ((ip->i_mode & mode) != 0)
212 return (0);
213 return (EACCES);
3e78e260
BJ
214}
215
7188ac27 216/* ARGSUSED */
031fd966 217int
5b169cb7 218ufs_getattr(vp, vap, cred, p)
7188ac27
KM
219 struct vnode *vp;
220 register struct vattr *vap;
221 struct ucred *cred;
5b169cb7 222 struct proc *p;
3e78e260 223{
031fd966 224 register struct inode *ip;
3e78e260 225
031fd966 226 ip = VTOI(vp);
7188ac27 227 ITIMES(ip, &time, &time);
3e78e260 228 /*
7188ac27 229 * Copy from inode table
3e78e260 230 */
7188ac27
KM
231 vap->va_fsid = ip->i_dev;
232 vap->va_fileid = ip->i_number;
233 vap->va_mode = ip->i_mode & ~IFMT;
234 vap->va_nlink = ip->i_nlink;
235 vap->va_uid = ip->i_uid;
236 vap->va_gid = ip->i_gid;
237 vap->va_rdev = (dev_t)ip->i_rdev;
4b61628b
KM
238#ifdef tahoe
239 vap->va_size = ip->i_size;
240 vap->va_size_rsv = 0;
241#else
8fae943a 242 vap->va_qsize = ip->i_din.di_qsize;
4b61628b 243#endif
7188ac27 244 vap->va_atime.tv_sec = ip->i_atime;
6ef53d70 245 vap->va_atime.tv_usec = 0;
7188ac27 246 vap->va_mtime.tv_sec = ip->i_mtime;
6ef53d70 247 vap->va_mtime.tv_usec = 0;
7188ac27 248 vap->va_ctime.tv_sec = ip->i_ctime;
6ef53d70 249 vap->va_ctime.tv_usec = 0;
d07fc92a
KM
250 vap->va_flags = ip->i_flags;
251 vap->va_gen = ip->i_gen;
7188ac27
KM
252 /* this doesn't belong here */
253 if (vp->v_type == VBLK)
254 vap->va_blocksize = BLKDEV_IOSIZE;
255 else if (vp->v_type == VCHR)
256 vap->va_blocksize = MAXBSIZE;
8eee8525 257 else
cc173077 258 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
a61a68d6 259 vap->va_bytes = dbtob(ip->i_blocks);
1a3cb193 260#ifdef _NOQUAD
8fae943a 261 vap->va_bytes_rsv = 0;
1a3cb193 262#endif
7188ac27 263 vap->va_type = vp->v_type;
1a3cb193 264 vap->va_filerev = ip->i_modrev;
7188ac27 265 return (0);
3e78e260
BJ
266}
267
268/*
7188ac27 269 * Set attribute vnode op. called from several syscalls
3e78e260 270 */
031fd966 271int
5b169cb7 272ufs_setattr(vp, vap, cred, p)
7188ac27
KM
273 register struct vnode *vp;
274 register struct vattr *vap;
275 register struct ucred *cred;
5b169cb7 276 struct proc *p;
3e78e260 277{
031fd966 278 register struct inode *ip;
031fd966 279 int error;
b4d1aee9 280
7188ac27 281 /*
031fd966 282 * Check for unsettable attributes.
7188ac27
KM
283 */
284 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
285 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
286 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
d07fc92a 287 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
7188ac27 288 return (EINVAL);
b4d1aee9 289 }
7188ac27
KM
290 /*
291 * Go through the fields and update iff not VNOVAL.
292 */
293 if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL)
031fd966 294 if (error = ufs_chown(vp, vap->va_uid, vap->va_gid, p))
7188ac27
KM
295 return (error);
296 if (vap->va_size != VNOVAL) {
297 if (vp->v_type == VDIR)
298 return (EISDIR);
c59af027 299 if (error = VOP_TRUNCATE(vp, vap->va_size, 0)) /* IO_SYNC? */
7188ac27 300 return (error);
3e78e260 301 }
c59af027 302 ip = VTOI(vp);
7188ac27 303 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
de412887 304 if (cred->cr_uid != ip->i_uid &&
c6f5111d 305 (error = suser(cred, &p->p_acflag)))
de412887 306 return (error);
7188ac27
KM
307 if (vap->va_atime.tv_sec != VNOVAL)
308 ip->i_flag |= IACC;
309 if (vap->va_mtime.tv_sec != VNOVAL)
310 ip->i_flag |= IUPD;
311 ip->i_flag |= ICHG;
c59af027 312 if (error = VOP_UPDATE(vp, &vap->va_atime, &vap->va_mtime, 1))
7188ac27 313 return (error);
5485e062 314 }
031fd966 315 error = 0;
7188ac27 316 if (vap->va_mode != (u_short)VNOVAL)
031fd966 317 error = ufs_chmod(vp, (int)vap->va_mode, p);
d07fc92a
KM
318 if (vap->va_flags != VNOVAL) {
319 if (cred->cr_uid != ip->i_uid &&
c6f5111d 320 (error = suser(cred, &p->p_acflag)))
d07fc92a
KM
321 return (error);
322 if (cred->cr_uid == 0) {
323 ip->i_flags = vap->va_flags;
324 } else {
325 ip->i_flags &= 0xffff0000;
326 ip->i_flags |= (vap->va_flags & 0xffff);
327 }
328 ip->i_flag |= ICHG;
329 }
7188ac27 330 return (error);
528f664c
SL
331}
332
4f083fd7
SL
333/*
334 * Change the mode on a file.
335 * Inode must be locked before calling.
336 */
ca3e8b68 337static int
031fd966 338ufs_chmod(vp, mode, p)
7188ac27 339 register struct vnode *vp;
528f664c 340 register int mode;
c6f5111d 341 struct proc *p;
528f664c 342{
c6f5111d 343 register struct ucred *cred = p->p_ucred;
7188ac27 344 register struct inode *ip = VTOI(vp);
de412887 345 int error;
197da11b 346
de412887 347 if (cred->cr_uid != ip->i_uid &&
c6f5111d 348 (error = suser(cred, &p->p_acflag)))
de412887 349 return (error);
7188ac27 350 if (cred->cr_uid) {
e57b6138 351 if (vp->v_type != VDIR && (mode & ISVTX))
39b5be4c 352 return (EFTYPE);
e57b6138 353 if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
39b5be4c 354 return (EPERM);
f94ceb3b 355 }
39b5be4c 356 ip->i_mode &= ~07777;
7188ac27 357 ip->i_mode |= mode & 07777;
3e78e260 358 ip->i_flag |= ICHG;
7188ac27 359 if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0)
8986c97c 360 (void) vnode_pager_uncache(vp);
47af7174 361 return (0);
5485e062
BJ
362}
363
528f664c
SL
364/*
365 * Perform chown operation on inode ip;
366 * inode must be locked prior to call.
367 */
ca3e8b68 368static int
031fd966 369ufs_chown(vp, uid, gid, p)
7188ac27 370 register struct vnode *vp;
031fd966
KB
371 u_int uid;
372 u_int gid;
c6f5111d 373 struct proc *p;
528f664c 374{
7188ac27 375 register struct inode *ip = VTOI(vp);
c6f5111d 376 register struct ucred *cred = p->p_ucred;
4b61628b
KM
377 uid_t ouid;
378 gid_t ogid;
379 int error = 0;
528f664c 380#ifdef QUOTA
4b61628b
KM
381 register int i;
382 long change;
bb1b75f4 383#endif
528f664c 384
7188ac27 385 if (uid == (u_short)VNOVAL)
bb1b75f4 386 uid = ip->i_uid;
7188ac27 387 if (gid == (u_short)VNOVAL)
bb1b75f4 388 gid = ip->i_gid;
18b0bce6
KB
389 /*
390 * If we don't own the file, are trying to change the owner
391 * of the file, or are not a member of the target group,
392 * the caller must be superuser or the call fails.
393 */
7188ac27
KM
394 if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
395 !groupmember((gid_t)gid, cred)) &&
c6f5111d 396 (error = suser(cred, &p->p_acflag)))
7188ac27 397 return (error);
4b61628b
KM
398 ouid = ip->i_uid;
399 ogid = ip->i_gid;
bb1b75f4 400#ifdef QUOTA
4b61628b
KM
401 if (error = getinoquota(ip))
402 return (error);
403 if (ouid == uid) {
404 dqrele(vp, ip->i_dquot[USRQUOTA]);
405 ip->i_dquot[USRQUOTA] = NODQUOT;
406 }
407 if (ogid == gid) {
408 dqrele(vp, ip->i_dquot[GRPQUOTA]);
409 ip->i_dquot[GRPQUOTA] = NODQUOT;
410 }
411 change = ip->i_blocks;
412 (void) chkdq(ip, -change, cred, CHOWN);
413 (void) chkiq(ip, -1, cred, CHOWN);
414 for (i = 0; i < MAXQUOTAS; i++) {
415 dqrele(vp, ip->i_dquot[i]);
416 ip->i_dquot[i] = NODQUOT;
417 }
f94ceb3b 418#endif
bb1b75f4
SL
419 ip->i_uid = uid;
420 ip->i_gid = gid;
528f664c 421#ifdef QUOTA
4b61628b
KM
422 if ((error = getinoquota(ip)) == 0) {
423 if (ouid == uid) {
424 dqrele(vp, ip->i_dquot[USRQUOTA]);
425 ip->i_dquot[USRQUOTA] = NODQUOT;
426 }
427 if (ogid == gid) {
428 dqrele(vp, ip->i_dquot[GRPQUOTA]);
429 ip->i_dquot[GRPQUOTA] = NODQUOT;
430 }
431 if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
432 if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
722a9b31 433 goto good;
4b61628b
KM
434 else
435 (void) chkdq(ip, -change, cred, CHOWN|FORCE);
436 }
437 for (i = 0; i < MAXQUOTAS; i++) {
438 dqrele(vp, ip->i_dquot[i]);
439 ip->i_dquot[i] = NODQUOT;
440 }
441 }
442 ip->i_uid = ouid;
443 ip->i_gid = ogid;
444 if (getinoquota(ip) == 0) {
445 if (ouid == uid) {
446 dqrele(vp, ip->i_dquot[USRQUOTA]);
447 ip->i_dquot[USRQUOTA] = NODQUOT;
448 }
449 if (ogid == gid) {
450 dqrele(vp, ip->i_dquot[GRPQUOTA]);
451 ip->i_dquot[GRPQUOTA] = NODQUOT;
452 }
722a9b31
KM
453 (void) chkdq(ip, change, cred, FORCE|CHOWN);
454 (void) chkiq(ip, 1, cred, FORCE|CHOWN);
6cb69bbe 455 (void) getinoquota(ip);
4b61628b 456 }
6cb69bbe 457 return (error);
722a9b31 458good:
6cb69bbe
KM
459 if (getinoquota(ip))
460 panic("chown: lost quota");
461#endif /* QUOTA */
4b61628b
KM
462 if (ouid != uid || ogid != gid)
463 ip->i_flag |= ICHG;
464 if (ouid != uid && cred->cr_uid != 0)
465 ip->i_mode &= ~ISUID;
466 if (ogid != gid && cred->cr_uid != 0)
467 ip->i_mode &= ~ISGID;
468 return (0);
d67a03eb
BJ
469}
470
7188ac27 471/* ARGSUSED */
031fd966 472int
5b169cb7 473ufs_ioctl(vp, com, data, fflag, cred, p)
7188ac27
KM
474 struct vnode *vp;
475 int com;
476 caddr_t data;
477 int fflag;
478 struct ucred *cred;
5b169cb7 479 struct proc *p;
bb1b75f4 480{
bb1b75f4 481
7188ac27
KM
482 return (ENOTTY);
483}
484
485/* ARGSUSED */
031fd966 486int
5b169cb7 487ufs_select(vp, which, fflags, cred, p)
7188ac27 488 struct vnode *vp;
d1c43d7f 489 int which, fflags;
7188ac27 490 struct ucred *cred;
5b169cb7 491 struct proc *p;
7188ac27
KM
492{
493
5b169cb7
KM
494 /*
495 * We should really check to see if I/O is possible.
496 */
497 return (1);
bb1b75f4 498}
d67a03eb 499
4f083fd7 500/*
7188ac27
KM
501 * Mmap a file
502 *
503 * NB Currently unsupported.
4f083fd7 504 */
7188ac27 505/* ARGSUSED */
031fd966 506int
5b169cb7 507ufs_mmap(vp, fflags, cred, p)
7188ac27
KM
508 struct vnode *vp;
509 int fflags;
510 struct ucred *cred;
5b169cb7 511 struct proc *p;
d67a03eb 512{
d67a03eb 513
7188ac27 514 return (EINVAL);
d67a03eb 515}
64d3a787 516
4f083fd7 517/*
7188ac27
KM
518 * Seek on a file
519 *
520 * Nothing to do, so just return.
4f083fd7 521 */
7188ac27 522/* ARGSUSED */
031fd966 523int
7188ac27
KM
524ufs_seek(vp, oldoff, newoff, cred)
525 struct vnode *vp;
526 off_t oldoff, newoff;
527 struct ucred *cred;
528f664c 528{
7188ac27
KM
529
530 return (0);
531}
532
533/*
534 * ufs remove
535 * Hard to avoid races here, especially
536 * in unlinking directories.
537 */
031fd966 538int
6e57e9ff 539ufs_remove(dvp, vp, cnp)
cfef4373
JH
540 struct vnode *dvp, *vp;
541 struct componentname *cnp;
7188ac27
KM
542{
543 register struct inode *ip, *dp;
544 int error;
545
cfef4373
JH
546 ip = VTOI(vp);
547 dp = VTOI(dvp);
548 error = ufs_dirremove(dvp, cnp);
7188ac27
KM
549 if (!error) {
550 ip->i_nlink--;
551 ip->i_flag |= ICHG;
528f664c 552 }
7188ac27
KM
553 if (dp == ip)
554 vrele(ITOV(ip));
555 else
031fd966
KB
556 ufs_iput(ip);
557 ufs_iput(dp);
7188ac27 558 return (error);
4f083fd7
SL
559}
560
561/*
7188ac27 562 * link vnode call
4f083fd7 563 */
031fd966 564int
6e57e9ff 565ufs_link(vp, tdvp, cnp)
cfef4373
JH
566 register struct vnode *vp; /* source vnode */
567 struct vnode *tdvp;
568 struct componentname *cnp;
4f083fd7 569{
031fd966 570 register struct inode *ip;
7188ac27 571 int error;
4f083fd7 572
655da945 573#ifdef DIANOSTIC
cfef4373 574 if ((cnp->cn_flags & HASBUF) == 0)
655da945
KM
575 panic("ufs_link: no name");
576#endif
031fd966 577 ip = VTOI(vp);
655da945 578 if ((unsigned short)ip->i_nlink >= LINK_MAX) {
cfef4373 579 free(cnp->cn_pnbuf, M_NAMEI);
986aedaa 580 return (EMLINK);
655da945 581 }
cfef4373 582 if (tdvp != vp)
7188ac27 583 ILOCK(ip);
7188ac27
KM
584 ip->i_nlink++;
585 ip->i_flag |= ICHG;
c59af027 586 error = VOP_UPDATE(vp, &time, &time, 1);
7188ac27 587 if (!error)
cfef4373
JH
588 error = ufs_direnter(ip, tdvp, cnp);
589 if (tdvp != vp)
7188ac27 590 IUNLOCK(ip);
cfef4373
JH
591 FREE(cnp->cn_pnbuf, M_NAMEI);
592 vput(tdvp);
7188ac27
KM
593 if (error) {
594 ip->i_nlink--;
82252d2b 595 ip->i_flag |= ICHG;
7188ac27
KM
596 }
597 return (error);
528f664c
SL
598}
599
cfef4373
JH
600
601
602/*
603 * relookup - lookup a path name component
604 * Used by lookup to re-aquire things.
605 */
606int
6e57e9ff 607relookup(dvp, vpp, cnp)
cfef4373
JH
608 struct vnode *dvp, **vpp;
609 struct componentname *cnp;
610{
611 register char *cp; /* pointer into pathname argument */
612 register struct vnode *dp = 0; /* the directory we are searching */
613 struct vnode *tdp; /* saved dp */
614 struct mount *mp; /* mount table entry */
615 int docache; /* == 0 do not cache last component */
616 int wantparent; /* 1 => wantparent or lockparent flag */
617 int rdonly; /* lookup read-only flag bit */
618 int error = 0;
619 int newhash;
620
621 /*
622 * Setup: break out flag bits into variables.
623 */
624 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
625 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
d7961b1d
JH
626 if (cnp->cn_nameiop == DELETE ||
627 (wantparent && cnp->cn_nameiop != CREATE))
cfef4373
JH
628 docache = 0;
629 rdonly = cnp->cn_flags & RDONLY;
630 cnp->cn_flags &= ~ISSYMLINK;
631 dp = dvp;
632 VOP_LOCK(dp);
633
634/* dirloop: */
635 /*
636 * Search a new directory.
637 *
638 * The cn_hash value is for use by vfs_cache.
639 * The last component of the filename is left accessible via
640 * cnp->cn_nameptr for callers that need the name. Callers needing
641 * the name set the SAVENAME flag. When done, they assume
642 * responsibility for freeing the pathname buffer.
643 */
6e57e9ff 644#ifdef NAMEI_DIAGNOSTIC
cfef4373
JH
645 newhash = 0;
646 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
647 newhash += (unsigned char)*cp;
648 if (newhash != cnp->cn_hash)
649 panic("relookup: bad hash");
650 if (cnp->cn_namelen != cp - cnp->cn_nameptr)
651 panic ("relookup: bad len");
cfef4373
JH
652 { char c = *cp;
653 *cp = '\0';
654 printf("{%s}: ", cnp->cn_nameptr);
655 *cp = c; }
656#endif
cfef4373
JH
657
658 /*
659 * Check for degenerate name (e.g. / or "")
660 * which is a way of talking about a directory,
661 * e.g. like "/." or ".".
662 */
663 if (cnp->cn_nameptr[0] == '\0') {
664 if (cnp->cn_nameiop != LOOKUP || wantparent) {
665 error = EISDIR;
666 goto bad;
667 }
668 if (dp->v_type != VDIR) {
669 error = ENOTDIR;
670 goto bad;
671 }
672 if (!(cnp->cn_flags & LOCKLEAF))
673 VOP_UNLOCK(dp);
674 *vpp = dp;
675 if (cnp->cn_flags & SAVESTART)
676 panic("lookup: SAVESTART");
677 return (0);
678 }
679
cfef4373
JH
680 if (cnp->cn_flags & ISDOTDOT)
681 panic ("relookup: lookup on dot-dot");
682
683 /*
684 * We now have a segment name to search for, and a directory to search.
685 */
686 if (error = VOP_LOOKUP(dp, vpp, cnp)) {
687#ifdef DIAGNOSTIC
688 if (*vpp != NULL)
689 panic("leaf should be empty");
690#endif
691#ifdef NAMEI_DIAGNOSTIC
692 printf("not found\n");
693#endif
694 if (cnp->cn_nameiop == LOOKUP || cnp->cn_nameiop == DELETE ||
695 error != ENOENT || *cp != 0)
696 goto bad;
697 /*
698 * If creating and at end of pathname, then can consider
699 * allowing file to be created.
700 */
701 if (rdonly || (dvp->v_mount->mnt_flag & MNT_RDONLY)) {
702 error = EROFS;
703 goto bad;
704 }
705 /*
706 * We return with ni_vp NULL to indicate that the entry
707 * doesn't currently exist, leaving a pointer to the
708 * (possibly locked) directory inode in ndp->ni_dvp.
709 */
cfef4373 710 if (cnp->cn_flags & SAVESTART) {
cfef4373
JH
711 /*
712 * startdir == dvp, always
713 */
714 VREF(dvp);
cfef4373 715 }
cfef4373
JH
716 return (0);
717 }
718#ifdef NAMEI_DIAGNOSTIC
719 printf("found\n");
720#endif
721
722 dp = *vpp;
6e57e9ff 723#ifdef DIAGNOSTIC
cfef4373
JH
724 /*
725 * Check for symbolic link
726 */
cfef4373
JH
727 if (dp->v_type == VLNK) {
728 panic ("relookup: symlink found.\n");
729 };
730
731 /*
732 * Check to see if the vnode has been mounted on;
733 * if so find the root of the mounted file system.
734 */
cfef4373 735#endif
cfef4373
JH
736
737
738nextname:
cfef4373
JH
739 /*
740 * Check for read-only file systems.
741 */
742 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) {
743 /*
744 * Disallow directory write attempts on read-only
745 * file systems.
746 */
747 if (rdonly || (dp->v_mount->mnt_flag & MNT_RDONLY) ||
748 (wantparent &&
749 (dvp->v_mount->mnt_flag & MNT_RDONLY))) {
750 error = EROFS;
751 goto bad2;
752 }
753 }
754 if (cnp->cn_flags & SAVESTART) {
cfef4373
JH
755 /* ASSERT(dvp==ndp->ni_startdir) */
756 VREF(dvp);
757 }
758
759 if (!wantparent)
760 vrele(dvp);
761 if ((cnp->cn_flags & LOCKLEAF) == 0)
762 VOP_UNLOCK(dp);
763 return (0);
764
765bad2:
766 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
767 VOP_UNLOCK(dvp);
768 vrele(dvp);
769bad:
770 vput(dp);
771 *vpp = NULL;
772 return (error);
773}
774
775
4f083fd7
SL
776/*
777 * Rename system call.
778 * rename("foo", "bar");
779 * is essentially
780 * unlink("bar");
781 * link("foo", "bar");
782 * unlink("foo");
783 * but ``atomically''. Can't do full commit without saving state in the
784 * inode on disk which isn't feasible at this time. Best we can do is
785 * always guarantee the target exists.
786 *
787 * Basic algorithm is:
788 *
789 * 1) Bump link count on source while we're linking it to the
7188ac27 790 * target. This also ensure the inode won't be deleted out
68f21562
KM
791 * from underneath us while we work (it may be truncated by
792 * a concurrent `trunc' or `open' for creation).
4f083fd7
SL
793 * 2) Link source to destination. If destination already exists,
794 * delete it first.
68f21562
KM
795 * 3) Unlink source reference to inode if still around. If a
796 * directory was moved and the parent of the destination
4f083fd7
SL
797 * is different from the source, patch the ".." entry in the
798 * directory.
4f083fd7 799 */
031fd966 800int
cfef4373 801ufs_rename(fdvp, fvp, fcnp,
6e57e9ff 802 tdvp, tvp, tcnp)
cfef4373
JH
803 struct vnode *fdvp, *fvp;
804 struct componentname *fcnp;
805 struct vnode *tdvp, *tvp;
806 struct componentname *tcnp;
528f664c 807{
4f083fd7 808 register struct inode *ip, *xp, *dp;
68f21562
KM
809 struct dirtemplate dirbuf;
810 int doingdirectory = 0, oldparent = 0, newparent = 0;
a5390dce 811 int error = 0;
cfef4373 812 int fdvpneedsrele = 1, tdvpneedsrele = 1;
4f083fd7 813
655da945 814#ifdef DIANOSTIC
cfef4373
JH
815 if ((tcnp->cn_flags & HASBUF) == 0 ||
816 (fcnp->cn_flags & HASBUF) == 0)
655da945
KM
817 panic("ufs_rename: no name");
818#endif
cfef4373
JH
819 dp = VTOI(fdvp);
820 ip = VTOI(fvp);
655da945
KM
821 /*
822 * Check if just deleting a link name.
823 */
cfef4373
JH
824 if (fvp == tvp) {
825 VOP_ABORTOP(tdvp, tcnp);
826 vput(tdvp);
827 vput(tvp);
828 vrele(fdvp);
655da945 829 if ((ip->i_mode&IFMT) == IFDIR) {
cfef4373
JH
830 VOP_ABORTOP(fdvp, fcnp);
831 vrele(fvp);
655da945
KM
832 return (EINVAL);
833 }
834 doingdirectory = 0;
835 goto unlinkit;
836 }
7188ac27 837 ILOCK(ip);
4f083fd7 838 if ((ip->i_mode&IFMT) == IFDIR) {
4f083fd7 839 /*
046f18d1 840 * Avoid ".", "..", and aliases of "." for obvious reasons.
4f083fd7 841 */
cfef4373
JH
842 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
843 dp == ip || (fcnp->cn_flags&ISDOTDOT) || (ip->i_flag & IRENAME)) {
844 VOP_ABORTOP(tdvp, tcnp);
845 vput(tdvp);
846 if (tvp)
847 vput(tvp);
848 VOP_ABORTOP(fdvp, fcnp);
849 vrele(fdvp);
850 vput(fvp);
7188ac27 851 return (EINVAL);
4f083fd7 852 }
68f21562 853 ip->i_flag |= IRENAME;
4f083fd7
SL
854 oldparent = dp->i_number;
855 doingdirectory++;
856 }
cfef4373 857 vrele(fdvp);
4f083fd7
SL
858
859 /*
860 * 1) Bump link count while we're moving stuff
861 * around. If we crash somewhere before
862 * completing our work, the link count
863 * may be wrong, but correctable.
864 */
865 ip->i_nlink++;
866 ip->i_flag |= ICHG;
cfef4373 867 error = VOP_UPDATE(fvp, &time, &time, 1);
a388503d 868 IUNLOCK(ip);
4f083fd7
SL
869
870 /*
871 * When the target exists, both the directory
7188ac27 872 * and target vnodes are returned locked.
4f083fd7 873 */
cfef4373 874 dp = VTOI(tdvp);
7188ac27 875 xp = NULL;
cfef4373
JH
876 if (tvp)
877 xp = VTOI(tvp);
046f18d1
SL
878 /*
879 * If ".." must be changed (ie the directory gets a new
81552f0f
KM
880 * parent) then the source directory must not be in the
881 * directory heirarchy above the target, as this would
882 * orphan everything below the source directory. Also
883 * the user must have write permission in the source so
884 * as to be able to change "..". We must repeat the call
885 * to namei, as the parent directory is unlocked by the
886 * call to checkpath().
046f18d1 887 */
68f21562
KM
888 if (oldparent != dp->i_number)
889 newparent = dp->i_number;
890 if (doingdirectory && newparent) {
cfef4373
JH
891 VOP_LOCK(fvp);
892 error = ufs_access(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
893 VOP_UNLOCK(fvp);
48c3b6e8 894 if (error)
81552f0f 895 goto bad;
655da945 896 if (xp != NULL)
031fd966 897 ufs_iput(xp);
cfef4373 898 if (error = ufs_checkpath(ip, dp, tcnp->cn_cred))
655da945 899 goto out;
cfef4373 900 if ((tcnp->cn_flags & SAVESTART) == 0)
655da945 901 panic("ufs_rename: lost to startdir");
5e03b55d 902 p->p_spare[1]--;
cfef4373 903 if (error = relookup(tdvp, &tvp, tcnp))
655da945 904 goto out;
cfef4373 905 dp = VTOI(tdvp);
655da945 906 xp = NULL;
cfef4373
JH
907 if (tvp)
908 xp = VTOI(tvp);
81552f0f 909 }
4f083fd7
SL
910 /*
911 * 2) If target doesn't exist, link the target
912 * to the source and unlink the source.
913 * Otherwise, rewrite the target directory
914 * entry to reference the source inode and
915 * expunge the original entry's existence.
916 */
4f083fd7 917 if (xp == NULL) {
7188ac27
KM
918 if (dp->i_dev != ip->i_dev)
919 panic("rename: EXDEV");
4f083fd7 920 /*
68f21562
KM
921 * Account for ".." in new directory.
922 * When source and destination have the same
923 * parent we don't fool with the link count.
4f083fd7 924 */
68f21562 925 if (doingdirectory && newparent) {
986aedaa
KM
926 if ((unsigned short)dp->i_nlink >= LINK_MAX) {
927 error = EMLINK;
928 goto bad;
929 }
4f083fd7
SL
930 dp->i_nlink++;
931 dp->i_flag |= ICHG;
c59af027 932 if (error = VOP_UPDATE(ITOV(dp), &time, &time, 1))
986aedaa 933 goto bad;
4f083fd7 934 }
cfef4373 935 if (error = ufs_direnter(ip, tdvp, tcnp)) {
394d67a8
KM
936 if (doingdirectory && newparent) {
937 dp->i_nlink--;
938 dp->i_flag |= ICHG;
c59af027 939 (void)VOP_UPDATE(ITOV(dp), &time, &time, 1);
394d67a8
KM
940 }
941 goto bad;
942 }
031fd966 943 ufs_iput(dp);
4f083fd7 944 } else {
7188ac27
KM
945 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
946 panic("rename: EXDEV");
e69c3c9c
SL
947 /*
948 * Short circuit rename(foo, foo).
949 */
950 if (xp->i_number == ip->i_number)
7188ac27 951 panic("rename: same file");
80cee150
JB
952 /*
953 * If the parent directory is "sticky", then the user must
954 * own the parent directory, or the destination of the rename,
955 * otherwise the destination may not be changed (except by
956 * root). This implements append-only directories.
957 */
cfef4373
JH
958 if ((dp->i_mode & ISVTX) && tcnp->cn_cred->cr_uid != 0 &&
959 tcnp->cn_cred->cr_uid != dp->i_uid &&
960 xp->i_uid != tcnp->cn_cred->cr_uid) {
80cee150
JB
961 error = EPERM;
962 goto bad;
963 }
4f083fd7 964 /*
655da945
KM
965 * Target must be empty if a directory and have no links
966 * to it. Also, ensure source and target are compatible
967 * (both directories, or both not directories).
4f083fd7
SL
968 */
969 if ((xp->i_mode&IFMT) == IFDIR) {
cfef4373 970 if (!ufs_dirempty(xp, dp->i_number, tcnp->cn_cred) ||
7188ac27 971 xp->i_nlink > 2) {
a5390dce 972 error = ENOTEMPTY;
4f083fd7
SL
973 goto bad;
974 }
975 if (!doingdirectory) {
a5390dce 976 error = ENOTDIR;
4f083fd7
SL
977 goto bad;
978 }
7188ac27 979 cache_purge(ITOV(dp));
4f083fd7 980 } else if (doingdirectory) {
a5390dce 981 error = EISDIR;
4f083fd7
SL
982 goto bad;
983 }
cfef4373 984 if (error = ufs_dirrewrite(dp, ip, tcnp))
7188ac27 985 goto bad;
a62786b9
KM
986 /*
987 * If the target directory is in the same
988 * directory as the source directory,
989 * decrement the link count on the parent
990 * of the target directory.
991 */
992 if (doingdirectory && !newparent) {
993 dp->i_nlink--;
994 dp->i_flag |= ICHG;
995 }
c59af027 996 ufs_iput(dp);
4f083fd7 997 /*
a5390dce
SL
998 * Adjust the link count of the target to
999 * reflect the dirrewrite above. If this is
1000 * a directory it is empty and there are
1001 * no links to it, so we can squash the inode and
1002 * any space associated with it. We disallowed
1003 * renaming over top of a directory with links to
68f21562
KM
1004 * it above, as the remaining link would point to
1005 * a directory without "." or ".." entries.
4f083fd7 1006 */
a5390dce 1007 xp->i_nlink--;
4f083fd7 1008 if (doingdirectory) {
a5390dce
SL
1009 if (--xp->i_nlink != 0)
1010 panic("rename: linked directory");
c59af027 1011 error = VOP_TRUNCATE(ITOV(xp), (u_long)0, IO_SYNC);
a5390dce 1012 }
4f083fd7 1013 xp->i_flag |= ICHG;
031fd966 1014 ufs_iput(xp);
31db12cb 1015 xp = NULL;
4f083fd7
SL
1016 }
1017
1018 /*
1019 * 3) Unlink the source.
1020 */
655da945 1021unlinkit:
cfef4373
JH
1022 fcnp->cn_flags &= ~MODMASK;
1023 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1024 if ((fcnp->cn_flags & SAVESTART) == 0)
655da945 1025 panic("ufs_rename: lost from startdir");
5e03b55d 1026 p->p_spare[1]--;
cfef4373
JH
1027 (void) relookup(fdvp, &fvp, fcnp); /* NEEDSWORK: startdir stuff */
1028 if (fvp != NULL) {
1029 xp = VTOI(fvp);
1030 dp = VTOI(fdvp);
7188ac27 1031 } else {
d9d75b8f
KM
1032 /*
1033 * From name has disappeared.
1034 */
1035 if (doingdirectory)
1036 panic("rename: lost dir entry");
1037 vrele(ITOV(ip));
1038 return (0);
7188ac27 1039 }
4f083fd7 1040 /*
7188ac27 1041 * Ensure that the directory entry still exists and has not
68f21562
KM
1042 * changed while the new name has been entered. If the source is
1043 * a file then the entry may have been unlinked or renamed. In
1044 * either case there is no further work to be done. If the source
1045 * is a directory then it cannot have been rmdir'ed; its link
1046 * count of three would cause a rmdir to fail with ENOTEMPTY.
7188ac27 1047 * The IRENAME flag ensures that it cannot be moved by another
68f21562 1048 * rename.
4f083fd7 1049 */
4f1a9037 1050 if (xp != ip) {
68f21562 1051 if (doingdirectory)
4f1a9037 1052 panic("rename: lost dir entry");
68f21562 1053 } else {
4f083fd7 1054 /*
68f21562
KM
1055 * If the source is a directory with a
1056 * new parent, the link count of the old
1057 * parent directory must be decremented
1058 * and ".." set to point to the new parent.
4f083fd7 1059 */
68f21562 1060 if (doingdirectory && newparent) {
4f083fd7
SL
1061 dp->i_nlink--;
1062 dp->i_flag |= ICHG;
86cdabf6 1063 error = vn_rdwr(UIO_READ, ITOV(xp), (caddr_t)&dirbuf,
7188ac27 1064 sizeof (struct dirtemplate), (off_t)0,
86cdabf6 1065 UIO_SYSSPACE, IO_NODELOCKED,
cfef4373 1066 tcnp->cn_cred, (int *)0, (struct proc *)0);
68f21562
KM
1067 if (error == 0) {
1068 if (dirbuf.dotdot_namlen != 2 ||
1069 dirbuf.dotdot_name[0] != '.' ||
1070 dirbuf.dotdot_name[1] != '.') {
031fd966
KB
1071 ufs_dirbad(xp, 12,
1072 "rename: mangled dir");
68f21562
KM
1073 } else {
1074 dirbuf.dotdot_ino = newparent;
86cdabf6 1075 (void) vn_rdwr(UIO_WRITE, ITOV(xp),
68f21562
KM
1076 (caddr_t)&dirbuf,
1077 sizeof (struct dirtemplate),
93e273b9 1078 (off_t)0, UIO_SYSSPACE,
86cdabf6 1079 IO_NODELOCKED|IO_SYNC,
cfef4373 1080 tcnp->cn_cred, (int *)0,
5b169cb7 1081 (struct proc *)0);
7188ac27 1082 cache_purge(ITOV(dp));
68f21562
KM
1083 }
1084 }
4f083fd7 1085 }
cfef4373 1086 error = ufs_dirremove(fdvp, fcnp);
7188ac27 1087 if (!error) {
68f21562
KM
1088 xp->i_nlink--;
1089 xp->i_flag |= ICHG;
4f083fd7 1090 }
68f21562 1091 xp->i_flag &= ~IRENAME;
4f083fd7 1092 }
4f083fd7 1093 if (dp)
7188ac27 1094 vput(ITOV(dp));
68f21562 1095 if (xp)
7188ac27
KM
1096 vput(ITOV(xp));
1097 vrele(ITOV(ip));
1098 return (error);
a5390dce 1099
4f083fd7 1100bad:
4f083fd7 1101 if (xp)
7188ac27
KM
1102 vput(ITOV(xp));
1103 vput(ITOV(dp));
4f083fd7
SL
1104out:
1105 ip->i_nlink--;
1106 ip->i_flag |= ICHG;
7188ac27
KM
1107 vrele(ITOV(ip));
1108 return (error);
64d3a787 1109}
88a7a62a
SL
1110
1111/*
1112 * A virgin directory (no blushing please).
1113 */
031fd966 1114static struct dirtemplate mastertemplate = {
88a7a62a
SL
1115 0, 12, 1, ".",
1116 0, DIRBLKSIZ - 12, 2, ".."
1117};
1118
1119/*
1120 * Mkdir system call
1121 */
031fd966 1122int
6e57e9ff 1123ufs_mkdir(dvp, vpp, cnp, vap)
cfef4373
JH
1124 struct vnode *dvp;
1125 struct vnode **vpp;
1126 struct componentname *cnp;
7188ac27 1127 struct vattr *vap;
88a7a62a 1128{
88a7a62a 1129 register struct inode *ip, *dp;
c59af027 1130 struct vnode *tvp;
88a7a62a 1131 struct dirtemplate dirtemplate;
7188ac27
KM
1132 int error;
1133 int dmode;
1134
655da945 1135#ifdef DIANOSTIC
cfef4373 1136 if ((cnp->cn_flags & HASBUF) == 0)
655da945
KM
1137 panic("ufs_mkdir: no name");
1138#endif
7188ac27 1139 dp = VTOI(dvp);
986aedaa 1140 if ((unsigned short)dp->i_nlink >= LINK_MAX) {
cfef4373 1141 free(cnp->cn_pnbuf, M_NAMEI);
031fd966 1142 ufs_iput(dp);
986aedaa
KM
1143 return (EMLINK);
1144 }
7188ac27
KM
1145 dmode = vap->va_mode&0777;
1146 dmode |= IFDIR;
88a7a62a 1147 /*
655da945
KM
1148 * Must simulate part of maknode here to acquire the inode, but
1149 * not have it entered in the parent directory. The entry is made
1150 * later after writing "." and ".." entries.
88a7a62a 1151 */
cfef4373
JH
1152 if (error = VOP_VALLOC(dvp, dmode, cnp->cn_cred, &tvp)) {
1153 free(cnp->cn_pnbuf, M_NAMEI);
031fd966 1154 ufs_iput(dp);
7188ac27 1155 return (error);
88a7a62a 1156 }
c59af027 1157 ip = VTOI(tvp);
cfef4373 1158 ip->i_uid = cnp->cn_cred->cr_uid;
4b61628b 1159 ip->i_gid = dp->i_gid;
88a7a62a 1160#ifdef QUOTA
4b61628b 1161 if ((error = getinoquota(ip)) ||
cfef4373
JH
1162 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
1163 free(cnp->cn_pnbuf, M_NAMEI);
c59af027 1164 VOP_VFREE(tvp, ip->i_number, dmode);
031fd966
KB
1165 ufs_iput(ip);
1166 ufs_iput(dp);
4b61628b
KM
1167 return (error);
1168 }
88a7a62a
SL
1169#endif
1170 ip->i_flag |= IACC|IUPD|ICHG;
7188ac27
KM
1171 ip->i_mode = dmode;
1172 ITOV(ip)->v_type = VDIR; /* Rest init'd in iget() */
88a7a62a 1173 ip->i_nlink = 2;
c59af027 1174 error = VOP_UPDATE(ITOV(ip), &time, &time, 1);
88a7a62a
SL
1175
1176 /*
1177 * Bump link count in parent directory
1178 * to reflect work done below. Should
1179 * be done before reference is created
1180 * so reparation is possible if we crash.
1181 */
1182 dp->i_nlink++;
1183 dp->i_flag |= ICHG;
c59af027 1184 if (error = VOP_UPDATE(ITOV(dp), &time, &time, 1))
394d67a8 1185 goto bad;
88a7a62a 1186
031fd966 1187 /* Initialize directory with "." and ".." from static template. */
88a7a62a
SL
1188 dirtemplate = mastertemplate;
1189 dirtemplate.dot_ino = ip->i_number;
1190 dirtemplate.dotdot_ino = dp->i_number;
86cdabf6 1191 error = vn_rdwr(UIO_WRITE, ITOV(ip), (caddr_t)&dirtemplate,
5b169cb7 1192 sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
cfef4373 1193 IO_NODELOCKED|IO_SYNC, cnp->cn_cred, (int *)0, (struct proc *)0);
7188ac27 1194 if (error) {
88a7a62a
SL
1195 dp->i_nlink--;
1196 dp->i_flag |= ICHG;
1197 goto bad;
1198 }
cc173077 1199 if (DIRBLKSIZ > VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
031fd966
KB
1200 panic("ufs_mkdir: blksize"); /* XXX should grow with balloc() */
1201 else {
23de9f20 1202 ip->i_size = DIRBLKSIZ;
15365e07
KM
1203 ip->i_flag |= ICHG;
1204 }
031fd966
KB
1205
1206 /* Directory set up, now install it's entry in the parent directory. */
cfef4373 1207 if (error = ufs_direnter(ip, dvp, cnp)) {
2a5d2f56
KM
1208 dp->i_nlink--;
1209 dp->i_flag |= ICHG;
88a7a62a
SL
1210 }
1211bad:
1212 /*
c59af027
KM
1213 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
1214 * for us because we set the link count to 0.
88a7a62a 1215 */
7188ac27 1216 if (error) {
88a7a62a
SL
1217 ip->i_nlink = 0;
1218 ip->i_flag |= ICHG;
031fd966 1219 ufs_iput(ip);
cbcdacd6 1220 } else
cfef4373
JH
1221 *vpp = ITOV(ip);
1222 FREE(cnp->cn_pnbuf, M_NAMEI);
031fd966 1223 ufs_iput(dp);
7188ac27 1224 return (error);
88a7a62a
SL
1225}
1226
1227/*
1228 * Rmdir system call.
1229 */
031fd966 1230int
6e57e9ff
JH
1231ufs_rmdir(dvp, vp, cnp)
1232 struct vnode *dvp;
1233 struct vnode *vp;
cfef4373 1234 struct componentname *cnp;
88a7a62a 1235{
88a7a62a 1236 register struct inode *ip, *dp;
031fd966 1237 int error;
7188ac27 1238
cfef4373
JH
1239 ip = VTOI(vp);
1240 dp = VTOI(dvp);
88a7a62a
SL
1241 /*
1242 * No rmdir "." please.
1243 */
1244 if (dp == ip) {
cfef4373 1245 vrele(dvp);
031fd966 1246 ufs_iput(ip);
7188ac27 1247 return (EINVAL);
88a7a62a
SL
1248 }
1249 /*
1250 * Verify the directory is empty (and valid).
1251 * (Rmdir ".." won't be valid since
1252 * ".." will contain a reference to
1253 * the current directory and thus be
1254 * non-empty.)
1255 */
031fd966
KB
1256 error = 0;
1257 if (ip->i_nlink != 2 ||
cfef4373 1258 !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
7188ac27 1259 error = ENOTEMPTY;
88a7a62a
SL
1260 goto out;
1261 }
1262 /*
1263 * Delete reference to directory before purging
1264 * inode. If we crash in between, the directory
1265 * will be reattached to lost+found,
1266 */
cfef4373 1267 if (error = ufs_dirremove(dvp, cnp))
88a7a62a
SL
1268 goto out;
1269 dp->i_nlink--;
1270 dp->i_flag |= ICHG;
cfef4373 1271 cache_purge(dvp);
031fd966 1272 ufs_iput(dp);
cfef4373 1273 dvp = NULL;
88a7a62a
SL
1274 /*
1275 * Truncate inode. The only stuff left
1276 * in the directory is "." and "..". The
1277 * "." reference is inconsequential since
1278 * we're quashing it. The ".." reference
1279 * has already been adjusted above. We've
1280 * removed the "." reference and the reference
1281 * in the parent directory, but there may be
1282 * other hard links so decrement by 2 and
1283 * worry about them later.
1284 */
1285 ip->i_nlink -= 2;
cfef4373 1286 error = VOP_TRUNCATE(vp, (u_long)0, IO_SYNC);
7188ac27 1287 cache_purge(ITOV(ip));
88a7a62a 1288out:
cfef4373 1289 if (dvp)
031fd966
KB
1290 ufs_iput(dp);
1291 ufs_iput(ip);
7188ac27 1292 return (error);
88a7a62a
SL
1293}
1294
7188ac27
KM
1295/*
1296 * symlink -- make a symbolic link
1297 */
031fd966 1298int
6e57e9ff 1299ufs_symlink(dvp, vpp, cnp, vap, target)
cfef4373
JH
1300 struct vnode *dvp;
1301 struct vnode **vpp;
1302 struct componentname *cnp;
7188ac27
KM
1303 struct vattr *vap;
1304 char *target;
1305{
7188ac27
KM
1306 int error;
1307
cfef4373 1308 if (error = ufs_makeinode(IFLNK | vap->va_mode, dvp, vpp, cnp))
7188ac27 1309 return (error);
cfef4373
JH
1310 error = vn_rdwr(UIO_WRITE, *vpp, target, strlen(target), (off_t)0,
1311 UIO_SYSSPACE, IO_NODELOCKED, cnp->cn_cred, (int *)0,
5b169cb7 1312 (struct proc *)0);
cfef4373 1313 vput(*vpp);
7188ac27
KM
1314 return (error);
1315}
1316
1317/*
1318 * Vnode op for read and write
1319 */
031fd966 1320int
930730fd 1321ufs_readdir(vp, uio, cred, eofflagp)
7188ac27
KM
1322 struct vnode *vp;
1323 register struct uio *uio;
7188ac27 1324 struct ucred *cred;
930730fd 1325 int *eofflagp;
88a7a62a 1326{
86cdabf6 1327 int count, lost, error;
88a7a62a 1328
7188ac27
KM
1329 count = uio->uio_resid;
1330 count &= ~(DIRBLKSIZ - 1);
86cdabf6
KM
1331 lost = uio->uio_resid - count;
1332 if (count < DIRBLKSIZ || (uio->uio_offset & (DIRBLKSIZ -1)))
7188ac27 1333 return (EINVAL);
7188ac27
KM
1334 uio->uio_resid = count;
1335 uio->uio_iov->iov_len = count;
031fd966 1336 error = VOP_READ(vp, uio, 0, cred);
86cdabf6 1337 uio->uio_resid += lost;
930730fd
KM
1338 if ((VTOI(vp)->i_size - uio->uio_offset) <= 0)
1339 *eofflagp = 1;
1340 else
1341 *eofflagp = 0;
7188ac27
KM
1342 return (error);
1343}
1344
1345/*
1346 * Return target name of a symbolic link
1347 */
031fd966 1348int
7188ac27
KM
1349ufs_readlink(vp, uiop, cred)
1350 struct vnode *vp;
1351 struct uio *uiop;
1352 struct ucred *cred;
1353{
1354
031fd966 1355 return (VOP_READ(vp, uiop, 0, cred));
7188ac27
KM
1356}
1357
1358/*
1359 * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually
655da945 1360 * done. If a buffer has been saved in anticipation of a CREATE, delete it.
7188ac27 1361 */
66955caf 1362/* ARGSUSED */
031fd966 1363int
6e57e9ff 1364ufs_abortop(dvp, cnp)
cfef4373
JH
1365 struct vnode *dvp;
1366 struct componentname *cnp;
7188ac27 1367{
cfef4373
JH
1368 if ((cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
1369 FREE(cnp->cn_pnbuf, M_NAMEI);
66955caf 1370 return (0);
7188ac27
KM
1371}
1372
1c3ebc10
KM
1373/*
1374 * Lock an inode.
1375 */
031fd966 1376int
7188ac27
KM
1377ufs_lock(vp)
1378 struct vnode *vp;
1379{
1380 register struct inode *ip = VTOI(vp);
1381
1382 ILOCK(ip);
1383 return (0);
1384}
1385
1c3ebc10
KM
1386/*
1387 * Unlock an inode.
1388 */
031fd966 1389int
7188ac27
KM
1390ufs_unlock(vp)
1391 struct vnode *vp;
1392{
1393 register struct inode *ip = VTOI(vp);
1394
1395 if (!(ip->i_flag & ILOCKED))
1396 panic("ufs_unlock NOT LOCKED");
1397 IUNLOCK(ip);
1398 return (0);
1399}
1400
1c3ebc10
KM
1401/*
1402 * Check for a locked inode.
1403 */
031fd966 1404int
1c3ebc10
KM
1405ufs_islocked(vp)
1406 struct vnode *vp;
1407{
1408
1409 if (VTOI(vp)->i_flag & ILOCKED)
1410 return (1);
1411 return (0);
1412}
1413
88a7a62a 1414/*
4f1ff475
KM
1415 * Calculate the logical to physical mapping if not done already,
1416 * then call the device strategy routine.
88a7a62a 1417 */
4f1ff475 1418int checkoverlap = 0;
e16fa59e 1419
031fd966 1420int
7188ac27
KM
1421ufs_strategy(bp)
1422 register struct buf *bp;
88a7a62a 1423{
031fd966 1424 register struct inode *ip;
e16fa59e 1425 struct vnode *vp;
e16fa59e
KM
1426 int error;
1427
031fd966 1428 ip = VTOI(bp->b_vp);
e16fa59e
KM
1429 if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR)
1430 panic("ufs_strategy: spec");
1431 if (bp->b_blkno == bp->b_lblkno) {
c59af027
KM
1432 if (error =
1433 VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno))
e16fa59e 1434 return (error);
20aa076b 1435 if ((long)bp->b_blkno == -1)
e16fa59e 1436 clrbuf(bp);
e16fa59e 1437 }
20aa076b
KM
1438 if ((long)bp->b_blkno == -1) {
1439 biodone(bp);
e16fa59e 1440 return (0);
20aa076b 1441 }
4f1ff475 1442#ifdef DIAGNOSTIC
031fd966
KB
1443 if (checkoverlap && bp->b_vp->v_mount->mnt_stat.f_type == MOUNT_UFS)
1444 ffs_checkoverlap(bp, ip);
1445#endif
1446
e16fa59e
KM
1447 vp = ip->i_devvp;
1448 bp->b_dev = vp->v_rdev;
031fd966 1449 (vp->v_op->vop_strategy)(bp);
7188ac27
KM
1450 return (0);
1451}
88a7a62a 1452
e16fa59e
KM
1453/*
1454 * Print out the contents of an inode.
1455 */
031fd966 1456int
e16fa59e
KM
1457ufs_print(vp)
1458 struct vnode *vp;
1459{
1460 register struct inode *ip = VTOI(vp);
1461
a8f829ed
KM
1462 printf("tag VT_UFS, ino %d, on dev %d, %d", ip->i_number,
1463 major(ip->i_dev), minor(ip->i_dev));
1464#ifdef FIFO
1465 if (vp->v_type == VFIFO)
1466 fifo_printinfo(vp);
1467#endif /* FIFO */
1468 printf("%s\n", (ip->i_flag & ILOCKED) ? " (LOCKED)" : "");
4a8fb1bb 1469 if (ip->i_lockholder == 0)
031fd966 1470 return (0);
4a8fb1bb
KM
1471 printf("\towner pid %d", ip->i_lockholder);
1472 if (ip->i_lockwaiter)
1473 printf(" waiting pid %d", ip->i_lockwaiter);
96bae3e0 1474 printf("\n");
031fd966 1475 return (0);
e16fa59e
KM
1476}
1477
24a31b70
KM
1478/*
1479 * Read wrapper for special devices.
1480 */
031fd966 1481int
24a31b70
KM
1482ufsspec_read(vp, uio, ioflag, cred)
1483 struct vnode *vp;
1484 struct uio *uio;
1485 int ioflag;
1486 struct ucred *cred;
1487{
1488
1489 /*
1490 * Set access flag.
1491 */
1492 VTOI(vp)->i_flag |= IACC;
1493 return (spec_read(vp, uio, ioflag, cred));
1494}
1495
1496/*
1497 * Write wrapper for special devices.
1498 */
031fd966 1499int
24a31b70
KM
1500ufsspec_write(vp, uio, ioflag, cred)
1501 struct vnode *vp;
1502 struct uio *uio;
1503 int ioflag;
1504 struct ucred *cred;
1505{
1506
1507 /*
1508 * Set update and change flags.
1509 */
1510 VTOI(vp)->i_flag |= IUPD|ICHG;
1511 return (spec_write(vp, uio, ioflag, cred));
1512}
1513
1514/*
1515 * Close wrapper for special devices.
1516 *
1517 * Update the times on the inode then do device close.
1518 */
031fd966 1519int
5b169cb7 1520ufsspec_close(vp, fflag, cred, p)
24a31b70
KM
1521 struct vnode *vp;
1522 int fflag;
1523 struct ucred *cred;
5b169cb7 1524 struct proc *p;
24a31b70
KM
1525{
1526 register struct inode *ip = VTOI(vp);
1527
de67eefc 1528 if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
24a31b70 1529 ITIMES(ip, &time, &time);
5b169cb7 1530 return (spec_close(vp, fflag, cred, p));
24a31b70
KM
1531}
1532
d1c43d7f
KM
1533#ifdef FIFO
1534/*
1535 * Read wrapper for fifo's
1536 */
031fd966 1537int
d1c43d7f
KM
1538ufsfifo_read(vp, uio, ioflag, cred)
1539 struct vnode *vp;
1540 struct uio *uio;
1541 int ioflag;
1542 struct ucred *cred;
1543{
1544
1545 /*
1546 * Set access flag.
1547 */
1548 VTOI(vp)->i_flag |= IACC;
1549 return (fifo_read(vp, uio, ioflag, cred));
1550}
1551
1552/*
1553 * Write wrapper for fifo's.
1554 */
031fd966 1555int
d1c43d7f
KM
1556ufsfifo_write(vp, uio, ioflag, cred)
1557 struct vnode *vp;
1558 struct uio *uio;
1559 int ioflag;
1560 struct ucred *cred;
1561{
1562
1563 /*
1564 * Set update and change flags.
1565 */
1566 VTOI(vp)->i_flag |= IUPD|ICHG;
1567 return (fifo_write(vp, uio, ioflag, cred));
1568}
1569
1570/*
1571 * Close wrapper for fifo's.
1572 *
1573 * Update the times on the inode then do device close.
1574 */
5b169cb7 1575ufsfifo_close(vp, fflag, cred, p)
d1c43d7f
KM
1576 struct vnode *vp;
1577 int fflag;
1578 struct ucred *cred;
5b169cb7 1579 struct proc *p;
d1c43d7f
KM
1580{
1581 register struct inode *ip = VTOI(vp);
1582
1583 if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
1584 ITIMES(ip, &time, &time);
5b169cb7 1585 return (fifo_close(vp, fflag, cred, p));
d1c43d7f
KM
1586}
1587#endif /* FIFO */
1588
0b0bf936
KM
1589/*
1590 * Advisory record locking support
1591 */
031fd966 1592int
0b0bf936
KM
1593ufs_advlock(vp, id, op, fl, flags)
1594 struct vnode *vp;
1595 caddr_t id;
1596 int op;
1597 register struct flock *fl;
1598 int flags;
1599{
1600 register struct inode *ip = VTOI(vp);
1601 register struct lockf *lock;
1602 off_t start, end;
1603 int error;
1604
1605 /*
1606 * Avoid the common case of unlocking when inode has no locks.
1607 */
1608 if (ip->i_lockf == (struct lockf *)0) {
1609 if (op != F_SETLK) {
1610 fl->l_type = F_UNLCK;
1611 return (0);
1612 }
1613 }
1614 /*
1615 * Convert the flock structure into a start and end.
1616 */
1617 switch (fl->l_whence) {
1618
1619 case SEEK_SET:
1620 case SEEK_CUR:
1621 /*
1622 * Caller is responsible for adding any necessary offset
1623 * when SEEK_CUR is used.
1624 */
1625 start = fl->l_start;
1626 break;
1627
1628 case SEEK_END:
1629 start = ip->i_size + fl->l_start;
1630 break;
1631
1632 default:
1633 return (EINVAL);
1634 }
1635 if (start < 0)
1636 return (EINVAL);
1637 if (fl->l_len == 0)
1638 end = -1;
1639 else
c412cb68 1640 end = start + fl->l_len - 1;
0b0bf936
KM
1641 /*
1642 * Create the lockf structure
1643 */
1644 MALLOC(lock, struct lockf *, sizeof *lock, M_LOCKF, M_WAITOK);
1645 lock->lf_start = start;
1646 lock->lf_end = end;
1647 lock->lf_id = id;
1648 lock->lf_inode = ip;
1649 lock->lf_type = fl->l_type;
1650 lock->lf_next = (struct lockf *)0;
1651 lock->lf_block = (struct lockf *)0;
1652 lock->lf_flags = flags;
1653 /*
1654 * Do the requested operation.
1655 */
1656 switch(op) {
1657 case F_SETLK:
b9fc1077 1658 return (lf_setlock(lock));
0b0bf936
KM
1659
1660 case F_UNLCK:
b9fc1077
KM
1661 error = lf_clearlock(lock);
1662 FREE(lock, M_LOCKF);
1663 return (error);
0b0bf936
KM
1664
1665 case F_GETLK:
b9fc1077
KM
1666 error = lf_getlock(lock, fl);
1667 FREE(lock, M_LOCKF);
1668 return (error);
0b0bf936
KM
1669
1670 default:
1671 free(lock, M_LOCKF);
1672 return (EINVAL);
1673 }
1674 /* NOTREACHED */
1675}
5b169cb7
KM
1676
1677/*
031fd966
KB
1678 * Initialize the vnode associated with a new inode, handle aliased
1679 * vnodes.
5b169cb7 1680 */
031fd966 1681int
c59af027 1682ufs_vinit(mntp, specops, fifoops, vpp)
031fd966 1683 struct mount *mntp;
c59af027 1684 struct vnodeops *specops, *fifoops;
031fd966
KB
1685 struct vnode **vpp;
1686{
1687 struct inode *ip, *nip;
1688 struct vnode *vp, *nvp;
4a8fb1bb 1689 extern struct vnodeops spec_vnodeops;
031fd966
KB
1690
1691 vp = *vpp;
1692 ip = VTOI(vp);
1693 switch(vp->v_type = IFTOVT(ip->i_mode)) {
1694 case VCHR:
1695 case VBLK:
c59af027 1696 vp->v_op = specops;
031fd966 1697 if (nvp = checkalias(vp, ip->i_rdev, mntp)) {
c59af027 1698 /*
4a8fb1bb 1699 * Discard unneeded vnode, but save its inode.
c59af027 1700 */
4a8fb1bb
KM
1701 remque(ip);
1702 IUNLOCK(ip);
1703 nvp->v_data = vp->v_data;
1704 vp->v_data = NULL;
1705 vp->v_op = &spec_vnodeops;
1706 vrele(vp);
1707 vgone(vp);
c59af027 1708 /*
4a8fb1bb 1709 * Reinitialize aliased inode.
c59af027 1710 */
4a8fb1bb
KM
1711 vp = nvp;
1712 ip->i_vnode = vp;
1713 ufs_ihashins(ip);
031fd966
KB
1714 }
1715 break;
1716 case VFIFO:
1717#ifdef FIFO
c59af027 1718 vp->v_op = fifoops;
031fd966
KB
1719 break;
1720#else
1721 return (EOPNOTSUPP);
1722#endif
1723 }
031fd966
KB
1724 if (ip->i_number == ROOTINO)
1725 vp->v_flag |= VROOT;
1a3cb193
KM
1726 /*
1727 * Initialize modrev times
1728 */
1729 SETHIGH(ip->i_modrev, mono_time.tv_sec);
1730 SETLOW(ip->i_modrev, mono_time.tv_usec * 4294);
031fd966
KB
1731 *vpp = vp;
1732 return (0);
1733}
5b169cb7 1734
031fd966
KB
1735/*
1736 * Allocate a new inode.
1737 */
1738int
6e57e9ff 1739ufs_makeinode(mode, dvp, vpp, cnp)
031fd966 1740 int mode;
cfef4373 1741 struct vnode *dvp;
c59af027 1742 struct vnode **vpp;
cfef4373 1743 struct componentname *cnp;
031fd966
KB
1744{
1745 register struct inode *ip, *pdir;
c59af027 1746 struct vnode *tvp;
031fd966
KB
1747 int error;
1748
cfef4373 1749 pdir = VTOI(dvp);
031fd966 1750#ifdef DIANOSTIC
cfef4373 1751 if ((cnp->cn_flags & HASBUF) == 0)
031fd966
KB
1752 panic("ufs_makeinode: no name");
1753#endif
c59af027 1754 *vpp = NULL;
031fd966
KB
1755 if ((mode & IFMT) == 0)
1756 mode |= IFREG;
1757
cfef4373
JH
1758 if (error = VOP_VALLOC(dvp, mode, cnp->cn_cred, &tvp)) {
1759 free(cnp->cn_pnbuf, M_NAMEI);
031fd966
KB
1760 ufs_iput(pdir);
1761 return (error);
1762 }
c59af027 1763 ip = VTOI(tvp);
cfef4373 1764 ip->i_uid = cnp->cn_cred->cr_uid;
031fd966
KB
1765 ip->i_gid = pdir->i_gid;
1766#ifdef QUOTA
1767 if ((error = getinoquota(ip)) ||
cfef4373
JH
1768 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
1769 free(cnp->cn_pnbuf, M_NAMEI);
c59af027 1770 VOP_VFREE(tvp, ip->i_number, mode);
031fd966
KB
1771 ufs_iput(ip);
1772 ufs_iput(pdir);
1773 return (error);
1774 }
1775#endif
1776 ip->i_flag |= IACC|IUPD|ICHG;
1777 ip->i_mode = mode;
c59af027 1778 tvp->v_type = IFTOVT(mode); /* Rest init'd in iget() */
031fd966 1779 ip->i_nlink = 1;
cfef4373
JH
1780 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
1781 suser(cnp->cn_cred, NULL))
031fd966
KB
1782 ip->i_mode &= ~ISGID;
1783
1784 /*
1785 * Make sure inode goes to disk before directory entry.
1786 */
c59af027 1787 if (error = VOP_UPDATE(tvp, &time, &time, 1))
031fd966 1788 goto bad;
cfef4373 1789 if (error = ufs_direnter(ip, dvp, cnp))
031fd966 1790 goto bad;
cfef4373
JH
1791 if ((cnp->cn_flags & SAVESTART) == 0)
1792 FREE(cnp->cn_pnbuf, M_NAMEI);
031fd966 1793 ufs_iput(pdir);
c59af027 1794 *vpp = tvp;
031fd966
KB
1795 return (0);
1796
1797bad:
1798 /*
1799 * Write error occurred trying to update the inode
1800 * or the directory so must deallocate the inode.
1801 */
cfef4373 1802 free(cnp->cn_pnbuf, M_NAMEI);
031fd966
KB
1803 ufs_iput(pdir);
1804 ip->i_nlink = 0;
1805 ip->i_flag |= ICHG;
1806 ufs_iput(ip);
1807 return (error);
1808}
cfef4373
JH
1809
1810
1811#if defined(JOHNH) && 0
1812/*
1813 * A hack to get the kernel to compile.
1814 */
1815int
1816hang_addrlist()
1817{
1818 return 0;
1819}
1820int
1821free_addrlist()
1822{
1823 return 0;
1824}
1825#endif
1826