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