symlinks are OK if not being followed
[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 *
2021967e 7 * @(#)ufs_vnops.c 7.79 (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
1a3cb193
KM
36#ifdef _NOQUAD
37#define SETHIGH(q, h) (q).val[_QUAD_HIGHWORD] = (h)
38#define SETLOW(q, l) (q).val[_QUAD_LOWWORD] = (l)
39#else /* QUAD */
40union _qcvt {
41 quad_t qcvt;
42 long val[2];
43};
44#define SETHIGH(q, h) { \
45 union _qcvt tmp; \
46 tmp.qcvt = (q); \
47 tmp.val[_QUAD_HIGHWORD] = (h); \
48 (q) = tmp.qcvt; \
49}
50#define SETLOW(q, l) { \
51 union _qcvt tmp; \
52 tmp.qcvt = (q); \
53 tmp.val[_QUAD_LOWWORD] = (l); \
54 (q) = tmp.qcvt; \
55}
56#endif /* QUAD */
57
4f083fd7 58/*
7188ac27 59 * Create a regular file
4f083fd7 60 */
031fd966 61int
6e57e9ff 62ufs_create(dvp, vpp, cnp, vap)
cfef4373
JH
63 struct vnode *dvp;
64 struct vnode **vpp;
65 struct componentname *cnp;
7188ac27 66 struct vattr *vap;
3e78e260 67{
7188ac27 68 int error;
3e78e260 69
031fd966 70 if (error =
cfef4373 71 ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode), dvp, vpp, cnp))
7188ac27 72 return (error);
7188ac27 73 return (0);
3e78e260
BJ
74}
75
4f083fd7 76/*
7188ac27 77 * Mknod vnode call
4f083fd7 78 */
7188ac27 79/* ARGSUSED */
031fd966 80int
6e57e9ff 81ufs_mknod(dvp, vpp, cnp, vap)
cfef4373
JH
82 struct vnode *dvp;
83 struct vnode **vpp;
84 struct componentname *cnp;
7188ac27 85 struct vattr *vap;
3e78e260 86{
c59af027 87 register struct inode *ip;
7188ac27 88 int error;
3e78e260 89
031fd966 90 if (error =
cfef4373 91 ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode), dvp, vpp, cnp))
7188ac27 92 return (error);
cfef4373 93 ip = VTOI(*vpp);
d1c43d7f
KM
94 ip->i_flag |= IACC|IUPD|ICHG;
95 if (vap->va_rdev != VNOVAL) {
7188ac27
KM
96 /*
97 * Want to be able to use this to make badblock
98 * inodes, so don't truncate the dev number.
99 */
b373e060 100 ip->i_rdev = vap->va_rdev;
7188ac27 101 }
7188ac27
KM
102 /*
103 * Remove inode so that it will be reloaded by iget and
104 * checked to see if it is an alias of an existing entry
105 * in the inode cache.
106 */
cfef4373
JH
107 vput(*vpp);
108 (*vpp)->v_type = VNON;
109 vgone(*vpp);
110 *vpp = 0;
7188ac27 111 return (0);
3e78e260
BJ
112}
113
114/*
7188ac27
KM
115 * Open called.
116 *
117 * Nothing to do.
3e78e260 118 */
7188ac27 119/* ARGSUSED */
031fd966 120int
5b169cb7 121ufs_open(vp, mode, cred, p)
7188ac27
KM
122 struct vnode *vp;
123 int mode;
124 struct ucred *cred;
5b169cb7 125 struct proc *p;
3e78e260 126{
3e78e260 127
7188ac27 128 return (0);
3e78e260
BJ
129}
130
131/*
7188ac27
KM
132 * Close called
133 *
134 * Update the times on the inode.
3e78e260 135 */
7188ac27 136/* ARGSUSED */
031fd966 137int
5b169cb7 138ufs_close(vp, fflag, cred, p)
7188ac27
KM
139 struct vnode *vp;
140 int fflag;
141 struct ucred *cred;
5b169cb7 142 struct proc *p;
3e78e260 143{
031fd966 144 register struct inode *ip;
3e78e260 145
031fd966 146 ip = VTOI(vp);
de67eefc 147 if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
7188ac27
KM
148 ITIMES(ip, &time, &time);
149 return (0);
3e78e260
BJ
150}
151
4b61628b
KM
152/*
153 * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
154 * The mode is shifted to select the owner/group/other fields. The
155 * super user is granted all permissions.
156 */
031fd966 157int
5b169cb7 158ufs_access(vp, mode, cred, p)
7188ac27 159 struct vnode *vp;
4b61628b 160 register int mode;
7188ac27 161 struct ucred *cred;
5b169cb7 162 struct proc *p;
3e78e260 163{
4b61628b
KM
164 register struct inode *ip = VTOI(vp);
165 register gid_t *gp;
166 int i, error;
3e78e260 167
4b61628b
KM
168#ifdef DIAGNOSTIC
169 if (!VOP_ISLOCKED(vp)) {
170 vprint("ufs_access: not locked", vp);
171 panic("ufs_access: not locked");
172 }
173#endif
174#ifdef QUOTA
175 if (mode & VWRITE) {
176 switch (vp->v_type) {
177 case VREG: case VDIR: case VLNK:
178 if (error = getinoquota(ip))
179 return (error);
180 }
181 }
182#endif /* QUOTA */
183 /*
184 * If you're the super-user, you always get access.
185 */
186 if (cred->cr_uid == 0)
187 return (0);
188 /*
189 * Access check is based on only one of owner, group, public.
190 * If not owner, then check group. If not a member of the
191 * group, then check public access.
192 */
193 if (cred->cr_uid != ip->i_uid) {
194 mode >>= 3;
195 gp = cred->cr_groups;
196 for (i = 0; i < cred->cr_ngroups; i++, gp++)
197 if (ip->i_gid == *gp)
198 goto found;
199 mode >>= 3;
200found:
201 ;
202 }
203 if ((ip->i_mode & mode) != 0)
204 return (0);
205 return (EACCES);
3e78e260
BJ
206}
207
7188ac27 208/* ARGSUSED */
031fd966 209int
5b169cb7 210ufs_getattr(vp, vap, cred, p)
7188ac27
KM
211 struct vnode *vp;
212 register struct vattr *vap;
213 struct ucred *cred;
5b169cb7 214 struct proc *p;
3e78e260 215{
031fd966 216 register struct inode *ip;
3e78e260 217
031fd966 218 ip = VTOI(vp);
7188ac27 219 ITIMES(ip, &time, &time);
3e78e260 220 /*
7188ac27 221 * Copy from inode table
3e78e260 222 */
7188ac27
KM
223 vap->va_fsid = ip->i_dev;
224 vap->va_fileid = ip->i_number;
225 vap->va_mode = ip->i_mode & ~IFMT;
226 vap->va_nlink = ip->i_nlink;
227 vap->va_uid = ip->i_uid;
228 vap->va_gid = ip->i_gid;
229 vap->va_rdev = (dev_t)ip->i_rdev;
4b61628b
KM
230#ifdef tahoe
231 vap->va_size = ip->i_size;
232 vap->va_size_rsv = 0;
233#else
8fae943a 234 vap->va_qsize = ip->i_din.di_qsize;
4b61628b 235#endif
7188ac27 236 vap->va_atime.tv_sec = ip->i_atime;
6ef53d70 237 vap->va_atime.tv_usec = 0;
7188ac27 238 vap->va_mtime.tv_sec = ip->i_mtime;
6ef53d70 239 vap->va_mtime.tv_usec = 0;
7188ac27 240 vap->va_ctime.tv_sec = ip->i_ctime;
6ef53d70 241 vap->va_ctime.tv_usec = 0;
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 */
285 if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL)
031fd966 286 if (error = ufs_chown(vp, vap->va_uid, vap->va_gid, p))
7188ac27
KM
287 return (error);
288 if (vap->va_size != VNOVAL) {
289 if (vp->v_type == VDIR)
290 return (EISDIR);
c59af027 291 if (error = VOP_TRUNCATE(vp, vap->va_size, 0)) /* IO_SYNC? */
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;
7188ac27 308 if (vap->va_mode != (u_short)VNOVAL)
031fd966 309 error = ufs_chmod(vp, (int)vap->va_mode, 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
031fd966 330ufs_chmod(vp, mode, p)
7188ac27 331 register struct vnode *vp;
528f664c 332 register int mode;
c6f5111d 333 struct proc *p;
528f664c 334{
c6f5111d 335 register struct ucred *cred = p->p_ucred;
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
031fd966 361ufs_chown(vp, uid, gid, p)
7188ac27 362 register struct vnode *vp;
031fd966
KB
363 u_int uid;
364 u_int gid;
c6f5111d 365 struct proc *p;
528f664c 366{
7188ac27 367 register struct inode *ip = VTOI(vp);
c6f5111d 368 register struct ucred *cred = p->p_ucred;
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
7188ac27 377 if (uid == (u_short)VNOVAL)
bb1b75f4 378 uid = ip->i_uid;
7188ac27 379 if (gid == (u_short)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
6e57e9ff 557ufs_link(vp, tdvp, cnp)
cfef4373
JH
558 register struct vnode *vp; /* source vnode */
559 struct vnode *tdvp;
560 struct componentname *cnp;
4f083fd7 561{
031fd966 562 register struct inode *ip;
7188ac27 563 int error;
4f083fd7 564
655da945 565#ifdef DIANOSTIC
cfef4373 566 if ((cnp->cn_flags & HASBUF) == 0)
655da945
KM
567 panic("ufs_link: no name");
568#endif
031fd966 569 ip = VTOI(vp);
655da945 570 if ((unsigned short)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
JH
680#endif
681 if (cnp->cn_nameiop == LOOKUP || cnp->cn_nameiop == DELETE ||
d36023e3 682 error != ENOENT)
cfef4373
JH
683 goto bad;
684 /*
685 * If creating and at end of pathname, then can consider
686 * allowing file to be created.
687 */
688 if (rdonly || (dvp->v_mount->mnt_flag & MNT_RDONLY)) {
689 error = EROFS;
690 goto bad;
691 }
d36023e3
KM
692 /* ASSERT(dvp == ndp->ni_startdir) */
693 if (cnp->cn_flags & SAVESTART)
694 VREF(dvp);
cfef4373
JH
695 /*
696 * We return with ni_vp NULL to indicate that the entry
697 * doesn't currently exist, leaving a pointer to the
698 * (possibly locked) directory inode in ndp->ni_dvp.
699 */
cfef4373
JH
700 return (0);
701 }
cfef4373 702 dp = *vpp;
2021967e 703
6e57e9ff 704#ifdef DIAGNOSTIC
cfef4373
JH
705 /*
706 * Check for symbolic link
707 */
2021967e 708 if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW))
cfef4373 709 panic ("relookup: symlink found.\n");
cfef4373 710#endif
cfef4373 711
cfef4373 712nextname:
cfef4373
JH
713 /*
714 * Check for read-only file systems.
715 */
716 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) {
717 /*
718 * Disallow directory write attempts on read-only
719 * file systems.
720 */
721 if (rdonly || (dp->v_mount->mnt_flag & MNT_RDONLY) ||
722 (wantparent &&
723 (dvp->v_mount->mnt_flag & MNT_RDONLY))) {
724 error = EROFS;
725 goto bad2;
726 }
727 }
d36023e3
KM
728 /* ASSERT(dvp == ndp->ni_startdir) */
729 if (cnp->cn_flags & SAVESTART)
cfef4373 730 VREF(dvp);
cfef4373
JH
731
732 if (!wantparent)
733 vrele(dvp);
734 if ((cnp->cn_flags & LOCKLEAF) == 0)
735 VOP_UNLOCK(dp);
736 return (0);
737
738bad2:
739 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
740 VOP_UNLOCK(dvp);
741 vrele(dvp);
742bad:
743 vput(dp);
744 *vpp = NULL;
745 return (error);
746}
747
748
4f083fd7
SL
749/*
750 * Rename system call.
751 * rename("foo", "bar");
752 * is essentially
753 * unlink("bar");
754 * link("foo", "bar");
755 * unlink("foo");
756 * but ``atomically''. Can't do full commit without saving state in the
757 * inode on disk which isn't feasible at this time. Best we can do is
758 * always guarantee the target exists.
759 *
760 * Basic algorithm is:
761 *
762 * 1) Bump link count on source while we're linking it to the
7188ac27 763 * target. This also ensure the inode won't be deleted out
68f21562
KM
764 * from underneath us while we work (it may be truncated by
765 * a concurrent `trunc' or `open' for creation).
4f083fd7
SL
766 * 2) Link source to destination. If destination already exists,
767 * delete it first.
68f21562
KM
768 * 3) Unlink source reference to inode if still around. If a
769 * directory was moved and the parent of the destination
4f083fd7
SL
770 * is different from the source, patch the ".." entry in the
771 * directory.
4f083fd7 772 */
031fd966 773int
cfef4373 774ufs_rename(fdvp, fvp, fcnp,
6e57e9ff 775 tdvp, tvp, tcnp)
cfef4373
JH
776 struct vnode *fdvp, *fvp;
777 struct componentname *fcnp;
778 struct vnode *tdvp, *tvp;
779 struct componentname *tcnp;
528f664c 780{
4f083fd7 781 register struct inode *ip, *xp, *dp;
68f21562
KM
782 struct dirtemplate dirbuf;
783 int doingdirectory = 0, oldparent = 0, newparent = 0;
a5390dce 784 int error = 0;
cfef4373 785 int fdvpneedsrele = 1, tdvpneedsrele = 1;
4f083fd7 786
655da945 787#ifdef DIANOSTIC
cfef4373
JH
788 if ((tcnp->cn_flags & HASBUF) == 0 ||
789 (fcnp->cn_flags & HASBUF) == 0)
655da945
KM
790 panic("ufs_rename: no name");
791#endif
cfef4373
JH
792 dp = VTOI(fdvp);
793 ip = VTOI(fvp);
655da945
KM
794 /*
795 * Check if just deleting a link name.
796 */
cfef4373
JH
797 if (fvp == tvp) {
798 VOP_ABORTOP(tdvp, tcnp);
799 vput(tdvp);
800 vput(tvp);
801 vrele(fdvp);
655da945 802 if ((ip->i_mode&IFMT) == IFDIR) {
cfef4373
JH
803 VOP_ABORTOP(fdvp, fcnp);
804 vrele(fvp);
655da945
KM
805 return (EINVAL);
806 }
807 doingdirectory = 0;
808 goto unlinkit;
809 }
7188ac27 810 ILOCK(ip);
4f083fd7 811 if ((ip->i_mode&IFMT) == IFDIR) {
4f083fd7 812 /*
046f18d1 813 * Avoid ".", "..", and aliases of "." for obvious reasons.
4f083fd7 814 */
cfef4373
JH
815 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
816 dp == ip || (fcnp->cn_flags&ISDOTDOT) || (ip->i_flag & IRENAME)) {
817 VOP_ABORTOP(tdvp, tcnp);
818 vput(tdvp);
819 if (tvp)
820 vput(tvp);
821 VOP_ABORTOP(fdvp, fcnp);
822 vrele(fdvp);
823 vput(fvp);
7188ac27 824 return (EINVAL);
4f083fd7 825 }
68f21562 826 ip->i_flag |= IRENAME;
4f083fd7
SL
827 oldparent = dp->i_number;
828 doingdirectory++;
829 }
cfef4373 830 vrele(fdvp);
4f083fd7
SL
831
832 /*
833 * 1) Bump link count while we're moving stuff
834 * around. If we crash somewhere before
835 * completing our work, the link count
836 * may be wrong, but correctable.
837 */
838 ip->i_nlink++;
839 ip->i_flag |= ICHG;
cfef4373 840 error = VOP_UPDATE(fvp, &time, &time, 1);
a388503d 841 IUNLOCK(ip);
4f083fd7
SL
842
843 /*
844 * When the target exists, both the directory
7188ac27 845 * and target vnodes are returned locked.
4f083fd7 846 */
cfef4373 847 dp = VTOI(tdvp);
7188ac27 848 xp = NULL;
cfef4373
JH
849 if (tvp)
850 xp = VTOI(tvp);
046f18d1
SL
851 /*
852 * If ".." must be changed (ie the directory gets a new
81552f0f
KM
853 * parent) then the source directory must not be in the
854 * directory heirarchy above the target, as this would
855 * orphan everything below the source directory. Also
856 * the user must have write permission in the source so
857 * as to be able to change "..". We must repeat the call
858 * to namei, as the parent directory is unlocked by the
859 * call to checkpath().
046f18d1 860 */
68f21562
KM
861 if (oldparent != dp->i_number)
862 newparent = dp->i_number;
863 if (doingdirectory && newparent) {
cfef4373
JH
864 VOP_LOCK(fvp);
865 error = ufs_access(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
866 VOP_UNLOCK(fvp);
48c3b6e8 867 if (error)
81552f0f 868 goto bad;
655da945 869 if (xp != NULL)
031fd966 870 ufs_iput(xp);
cfef4373 871 if (error = ufs_checkpath(ip, dp, tcnp->cn_cred))
655da945 872 goto out;
cfef4373 873 if ((tcnp->cn_flags & SAVESTART) == 0)
655da945 874 panic("ufs_rename: lost to startdir");
5e03b55d 875 p->p_spare[1]--;
cfef4373 876 if (error = relookup(tdvp, &tvp, tcnp))
655da945 877 goto out;
cfef4373 878 dp = VTOI(tdvp);
655da945 879 xp = NULL;
cfef4373
JH
880 if (tvp)
881 xp = VTOI(tvp);
81552f0f 882 }
4f083fd7
SL
883 /*
884 * 2) If target doesn't exist, link the target
885 * to the source and unlink the source.
886 * Otherwise, rewrite the target directory
887 * entry to reference the source inode and
888 * expunge the original entry's existence.
889 */
4f083fd7 890 if (xp == NULL) {
7188ac27
KM
891 if (dp->i_dev != ip->i_dev)
892 panic("rename: EXDEV");
4f083fd7 893 /*
68f21562
KM
894 * Account for ".." in new directory.
895 * When source and destination have the same
896 * parent we don't fool with the link count.
4f083fd7 897 */
68f21562 898 if (doingdirectory && newparent) {
986aedaa
KM
899 if ((unsigned short)dp->i_nlink >= LINK_MAX) {
900 error = EMLINK;
901 goto bad;
902 }
4f083fd7
SL
903 dp->i_nlink++;
904 dp->i_flag |= ICHG;
c59af027 905 if (error = VOP_UPDATE(ITOV(dp), &time, &time, 1))
986aedaa 906 goto bad;
4f083fd7 907 }
cfef4373 908 if (error = ufs_direnter(ip, tdvp, tcnp)) {
394d67a8
KM
909 if (doingdirectory && newparent) {
910 dp->i_nlink--;
911 dp->i_flag |= ICHG;
c59af027 912 (void)VOP_UPDATE(ITOV(dp), &time, &time, 1);
394d67a8
KM
913 }
914 goto bad;
915 }
031fd966 916 ufs_iput(dp);
4f083fd7 917 } else {
7188ac27
KM
918 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
919 panic("rename: EXDEV");
e69c3c9c
SL
920 /*
921 * Short circuit rename(foo, foo).
922 */
923 if (xp->i_number == ip->i_number)
7188ac27 924 panic("rename: same file");
80cee150
JB
925 /*
926 * If the parent directory is "sticky", then the user must
927 * own the parent directory, or the destination of the rename,
928 * otherwise the destination may not be changed (except by
929 * root). This implements append-only directories.
930 */
cfef4373
JH
931 if ((dp->i_mode & ISVTX) && tcnp->cn_cred->cr_uid != 0 &&
932 tcnp->cn_cred->cr_uid != dp->i_uid &&
933 xp->i_uid != tcnp->cn_cred->cr_uid) {
80cee150
JB
934 error = EPERM;
935 goto bad;
936 }
4f083fd7 937 /*
655da945
KM
938 * Target must be empty if a directory and have no links
939 * to it. Also, ensure source and target are compatible
940 * (both directories, or both not directories).
4f083fd7
SL
941 */
942 if ((xp->i_mode&IFMT) == IFDIR) {
cfef4373 943 if (!ufs_dirempty(xp, dp->i_number, tcnp->cn_cred) ||
7188ac27 944 xp->i_nlink > 2) {
a5390dce 945 error = ENOTEMPTY;
4f083fd7
SL
946 goto bad;
947 }
948 if (!doingdirectory) {
a5390dce 949 error = ENOTDIR;
4f083fd7
SL
950 goto bad;
951 }
7188ac27 952 cache_purge(ITOV(dp));
4f083fd7 953 } else if (doingdirectory) {
a5390dce 954 error = EISDIR;
4f083fd7
SL
955 goto bad;
956 }
cfef4373 957 if (error = ufs_dirrewrite(dp, ip, tcnp))
7188ac27 958 goto bad;
a62786b9
KM
959 /*
960 * If the target directory is in the same
961 * directory as the source directory,
962 * decrement the link count on the parent
963 * of the target directory.
964 */
965 if (doingdirectory && !newparent) {
966 dp->i_nlink--;
967 dp->i_flag |= ICHG;
968 }
c59af027 969 ufs_iput(dp);
4f083fd7 970 /*
a5390dce
SL
971 * Adjust the link count of the target to
972 * reflect the dirrewrite above. If this is
973 * a directory it is empty and there are
974 * no links to it, so we can squash the inode and
975 * any space associated with it. We disallowed
976 * renaming over top of a directory with links to
68f21562
KM
977 * it above, as the remaining link would point to
978 * a directory without "." or ".." entries.
4f083fd7 979 */
a5390dce 980 xp->i_nlink--;
4f083fd7 981 if (doingdirectory) {
a5390dce
SL
982 if (--xp->i_nlink != 0)
983 panic("rename: linked directory");
c59af027 984 error = VOP_TRUNCATE(ITOV(xp), (u_long)0, IO_SYNC);
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] != '.') {
031fd966
KB
1044 ufs_dirbad(xp, 12,
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
655da945 1108#ifdef DIANOSTIC
cfef4373 1109 if ((cnp->cn_flags & HASBUF) == 0)
655da945
KM
1110 panic("ufs_mkdir: no name");
1111#endif
7188ac27 1112 dp = VTOI(dvp);
986aedaa 1113 if ((unsigned short)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;
cfef4373 1259 error = VOP_TRUNCATE(vp, (u_long)0, IO_SYNC);
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 */
4f1ff475 1397int checkoverlap = 0;
e16fa59e 1398
031fd966 1399int
7188ac27
KM
1400ufs_strategy(bp)
1401 register struct buf *bp;
88a7a62a 1402{
031fd966 1403 register struct inode *ip;
e16fa59e 1404 struct vnode *vp;
e16fa59e
KM
1405 int error;
1406
031fd966 1407 ip = VTOI(bp->b_vp);
e16fa59e
KM
1408 if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR)
1409 panic("ufs_strategy: spec");
1410 if (bp->b_blkno == bp->b_lblkno) {
c59af027
KM
1411 if (error =
1412 VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno))
e16fa59e 1413 return (error);
20aa076b 1414 if ((long)bp->b_blkno == -1)
e16fa59e 1415 clrbuf(bp);
e16fa59e 1416 }
20aa076b
KM
1417 if ((long)bp->b_blkno == -1) {
1418 biodone(bp);
e16fa59e 1419 return (0);
20aa076b 1420 }
4f1ff475 1421#ifdef DIAGNOSTIC
031fd966
KB
1422 if (checkoverlap && bp->b_vp->v_mount->mnt_stat.f_type == MOUNT_UFS)
1423 ffs_checkoverlap(bp, ip);
1424#endif
1425
e16fa59e
KM
1426 vp = ip->i_devvp;
1427 bp->b_dev = vp->v_rdev;
031fd966 1428 (vp->v_op->vop_strategy)(bp);
7188ac27
KM
1429 return (0);
1430}
88a7a62a 1431
e16fa59e
KM
1432/*
1433 * Print out the contents of an inode.
1434 */
031fd966 1435int
e16fa59e
KM
1436ufs_print(vp)
1437 struct vnode *vp;
1438{
1439 register struct inode *ip = VTOI(vp);
1440
a8f829ed
KM
1441 printf("tag VT_UFS, ino %d, on dev %d, %d", ip->i_number,
1442 major(ip->i_dev), minor(ip->i_dev));
1443#ifdef FIFO
1444 if (vp->v_type == VFIFO)
1445 fifo_printinfo(vp);
1446#endif /* FIFO */
1447 printf("%s\n", (ip->i_flag & ILOCKED) ? " (LOCKED)" : "");
4a8fb1bb 1448 if (ip->i_lockholder == 0)
031fd966 1449 return (0);
4a8fb1bb
KM
1450 printf("\towner pid %d", ip->i_lockholder);
1451 if (ip->i_lockwaiter)
1452 printf(" waiting pid %d", ip->i_lockwaiter);
96bae3e0 1453 printf("\n");
031fd966 1454 return (0);
e16fa59e
KM
1455}
1456
24a31b70
KM
1457/*
1458 * Read wrapper for special devices.
1459 */
031fd966 1460int
24a31b70
KM
1461ufsspec_read(vp, uio, ioflag, cred)
1462 struct vnode *vp;
1463 struct uio *uio;
1464 int ioflag;
1465 struct ucred *cred;
1466{
1467
1468 /*
1469 * Set access flag.
1470 */
1471 VTOI(vp)->i_flag |= IACC;
1472 return (spec_read(vp, uio, ioflag, cred));
1473}
1474
1475/*
1476 * Write wrapper for special devices.
1477 */
031fd966 1478int
24a31b70
KM
1479ufsspec_write(vp, uio, ioflag, cred)
1480 struct vnode *vp;
1481 struct uio *uio;
1482 int ioflag;
1483 struct ucred *cred;
1484{
1485
1486 /*
1487 * Set update and change flags.
1488 */
1489 VTOI(vp)->i_flag |= IUPD|ICHG;
1490 return (spec_write(vp, uio, ioflag, cred));
1491}
1492
1493/*
1494 * Close wrapper for special devices.
1495 *
1496 * Update the times on the inode then do device close.
1497 */
031fd966 1498int
5b169cb7 1499ufsspec_close(vp, fflag, cred, p)
24a31b70
KM
1500 struct vnode *vp;
1501 int fflag;
1502 struct ucred *cred;
5b169cb7 1503 struct proc *p;
24a31b70
KM
1504{
1505 register struct inode *ip = VTOI(vp);
1506
de67eefc 1507 if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
24a31b70 1508 ITIMES(ip, &time, &time);
5b169cb7 1509 return (spec_close(vp, fflag, cred, p));
24a31b70
KM
1510}
1511
d1c43d7f
KM
1512#ifdef FIFO
1513/*
1514 * Read wrapper for fifo's
1515 */
031fd966 1516int
d1c43d7f
KM
1517ufsfifo_read(vp, uio, ioflag, cred)
1518 struct vnode *vp;
1519 struct uio *uio;
1520 int ioflag;
1521 struct ucred *cred;
1522{
1523
1524 /*
1525 * Set access flag.
1526 */
1527 VTOI(vp)->i_flag |= IACC;
1528 return (fifo_read(vp, uio, ioflag, cred));
1529}
1530
1531/*
1532 * Write wrapper for fifo's.
1533 */
031fd966 1534int
d1c43d7f
KM
1535ufsfifo_write(vp, uio, ioflag, cred)
1536 struct vnode *vp;
1537 struct uio *uio;
1538 int ioflag;
1539 struct ucred *cred;
1540{
1541
1542 /*
1543 * Set update and change flags.
1544 */
1545 VTOI(vp)->i_flag |= IUPD|ICHG;
1546 return (fifo_write(vp, uio, ioflag, cred));
1547}
1548
1549/*
1550 * Close wrapper for fifo's.
1551 *
1552 * Update the times on the inode then do device close.
1553 */
5b169cb7 1554ufsfifo_close(vp, fflag, cred, p)
d1c43d7f
KM
1555 struct vnode *vp;
1556 int fflag;
1557 struct ucred *cred;
5b169cb7 1558 struct proc *p;
d1c43d7f
KM
1559{
1560 register struct inode *ip = VTOI(vp);
1561
1562 if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
1563 ITIMES(ip, &time, &time);
5b169cb7 1564 return (fifo_close(vp, fflag, cred, p));
d1c43d7f
KM
1565}
1566#endif /* FIFO */
1567
0b0bf936
KM
1568/*
1569 * Advisory record locking support
1570 */
031fd966 1571int
0b0bf936
KM
1572ufs_advlock(vp, id, op, fl, flags)
1573 struct vnode *vp;
1574 caddr_t id;
1575 int op;
1576 register struct flock *fl;
1577 int flags;
1578{
1579 register struct inode *ip = VTOI(vp);
1580 register struct lockf *lock;
1581 off_t start, end;
1582 int error;
1583
1584 /*
1585 * Avoid the common case of unlocking when inode has no locks.
1586 */
1587 if (ip->i_lockf == (struct lockf *)0) {
1588 if (op != F_SETLK) {
1589 fl->l_type = F_UNLCK;
1590 return (0);
1591 }
1592 }
1593 /*
1594 * Convert the flock structure into a start and end.
1595 */
1596 switch (fl->l_whence) {
1597
1598 case SEEK_SET:
1599 case SEEK_CUR:
1600 /*
1601 * Caller is responsible for adding any necessary offset
1602 * when SEEK_CUR is used.
1603 */
1604 start = fl->l_start;
1605 break;
1606
1607 case SEEK_END:
1608 start = ip->i_size + fl->l_start;
1609 break;
1610
1611 default:
1612 return (EINVAL);
1613 }
1614 if (start < 0)
1615 return (EINVAL);
1616 if (fl->l_len == 0)
1617 end = -1;
1618 else
c412cb68 1619 end = start + fl->l_len - 1;
0b0bf936
KM
1620 /*
1621 * Create the lockf structure
1622 */
1623 MALLOC(lock, struct lockf *, sizeof *lock, M_LOCKF, M_WAITOK);
1624 lock->lf_start = start;
1625 lock->lf_end = end;
1626 lock->lf_id = id;
1627 lock->lf_inode = ip;
1628 lock->lf_type = fl->l_type;
1629 lock->lf_next = (struct lockf *)0;
1630 lock->lf_block = (struct lockf *)0;
1631 lock->lf_flags = flags;
1632 /*
1633 * Do the requested operation.
1634 */
1635 switch(op) {
1636 case F_SETLK:
b9fc1077 1637 return (lf_setlock(lock));
0b0bf936
KM
1638
1639 case F_UNLCK:
b9fc1077
KM
1640 error = lf_clearlock(lock);
1641 FREE(lock, M_LOCKF);
1642 return (error);
0b0bf936
KM
1643
1644 case F_GETLK:
b9fc1077
KM
1645 error = lf_getlock(lock, fl);
1646 FREE(lock, M_LOCKF);
1647 return (error);
0b0bf936
KM
1648
1649 default:
1650 free(lock, M_LOCKF);
1651 return (EINVAL);
1652 }
1653 /* NOTREACHED */
1654}
5b169cb7
KM
1655
1656/*
031fd966
KB
1657 * Initialize the vnode associated with a new inode, handle aliased
1658 * vnodes.
5b169cb7 1659 */
031fd966 1660int
c59af027 1661ufs_vinit(mntp, specops, fifoops, vpp)
031fd966 1662 struct mount *mntp;
c59af027 1663 struct vnodeops *specops, *fifoops;
031fd966
KB
1664 struct vnode **vpp;
1665{
1666 struct inode *ip, *nip;
1667 struct vnode *vp, *nvp;
4a8fb1bb 1668 extern struct vnodeops spec_vnodeops;
031fd966
KB
1669
1670 vp = *vpp;
1671 ip = VTOI(vp);
1672 switch(vp->v_type = IFTOVT(ip->i_mode)) {
1673 case VCHR:
1674 case VBLK:
c59af027 1675 vp->v_op = specops;
031fd966 1676 if (nvp = checkalias(vp, ip->i_rdev, mntp)) {
c59af027 1677 /*
4a8fb1bb 1678 * Discard unneeded vnode, but save its inode.
c59af027 1679 */
4a8fb1bb
KM
1680 remque(ip);
1681 IUNLOCK(ip);
1682 nvp->v_data = vp->v_data;
1683 vp->v_data = NULL;
1684 vp->v_op = &spec_vnodeops;
1685 vrele(vp);
1686 vgone(vp);
c59af027 1687 /*
4a8fb1bb 1688 * Reinitialize aliased inode.
c59af027 1689 */
4a8fb1bb
KM
1690 vp = nvp;
1691 ip->i_vnode = vp;
1692 ufs_ihashins(ip);
031fd966
KB
1693 }
1694 break;
1695 case VFIFO:
1696#ifdef FIFO
c59af027 1697 vp->v_op = fifoops;
031fd966
KB
1698 break;
1699#else
1700 return (EOPNOTSUPP);
1701#endif
1702 }
031fd966
KB
1703 if (ip->i_number == ROOTINO)
1704 vp->v_flag |= VROOT;
1a3cb193
KM
1705 /*
1706 * Initialize modrev times
1707 */
1708 SETHIGH(ip->i_modrev, mono_time.tv_sec);
1709 SETLOW(ip->i_modrev, mono_time.tv_usec * 4294);
031fd966
KB
1710 *vpp = vp;
1711 return (0);
1712}
5b169cb7 1713
031fd966
KB
1714/*
1715 * Allocate a new inode.
1716 */
1717int
6e57e9ff 1718ufs_makeinode(mode, dvp, vpp, cnp)
031fd966 1719 int mode;
cfef4373 1720 struct vnode *dvp;
c59af027 1721 struct vnode **vpp;
cfef4373 1722 struct componentname *cnp;
031fd966
KB
1723{
1724 register struct inode *ip, *pdir;
c59af027 1725 struct vnode *tvp;
031fd966
KB
1726 int error;
1727
cfef4373 1728 pdir = VTOI(dvp);
031fd966 1729#ifdef DIANOSTIC
cfef4373 1730 if ((cnp->cn_flags & HASBUF) == 0)
031fd966
KB
1731 panic("ufs_makeinode: no name");
1732#endif
c59af027 1733 *vpp = NULL;
031fd966
KB
1734 if ((mode & IFMT) == 0)
1735 mode |= IFREG;
1736
cfef4373
JH
1737 if (error = VOP_VALLOC(dvp, mode, cnp->cn_cred, &tvp)) {
1738 free(cnp->cn_pnbuf, M_NAMEI);
031fd966
KB
1739 ufs_iput(pdir);
1740 return (error);
1741 }
c59af027 1742 ip = VTOI(tvp);
cfef4373 1743 ip->i_uid = cnp->cn_cred->cr_uid;
031fd966
KB
1744 ip->i_gid = pdir->i_gid;
1745#ifdef QUOTA
1746 if ((error = getinoquota(ip)) ||
cfef4373
JH
1747 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
1748 free(cnp->cn_pnbuf, M_NAMEI);
c59af027 1749 VOP_VFREE(tvp, ip->i_number, mode);
031fd966
KB
1750 ufs_iput(ip);
1751 ufs_iput(pdir);
1752 return (error);
1753 }
1754#endif
1755 ip->i_flag |= IACC|IUPD|ICHG;
1756 ip->i_mode = mode;
c59af027 1757 tvp->v_type = IFTOVT(mode); /* Rest init'd in iget() */
031fd966 1758 ip->i_nlink = 1;
cfef4373
JH
1759 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
1760 suser(cnp->cn_cred, NULL))
031fd966
KB
1761 ip->i_mode &= ~ISGID;
1762
1763 /*
1764 * Make sure inode goes to disk before directory entry.
1765 */
c59af027 1766 if (error = VOP_UPDATE(tvp, &time, &time, 1))
031fd966 1767 goto bad;
cfef4373 1768 if (error = ufs_direnter(ip, dvp, cnp))
031fd966 1769 goto bad;
cfef4373
JH
1770 if ((cnp->cn_flags & SAVESTART) == 0)
1771 FREE(cnp->cn_pnbuf, M_NAMEI);
031fd966 1772 ufs_iput(pdir);
c59af027 1773 *vpp = tvp;
031fd966
KB
1774 return (0);
1775
1776bad:
1777 /*
1778 * Write error occurred trying to update the inode
1779 * or the directory so must deallocate the inode.
1780 */
cfef4373 1781 free(cnp->cn_pnbuf, M_NAMEI);
031fd966
KB
1782 ufs_iput(pdir);
1783 ip->i_nlink = 0;
1784 ip->i_flag |= ICHG;
1785 ufs_iput(ip);
1786 return (error);
1787}