ufs_chown and ufs_chmod now take cred pointers
[unix-history] / usr / src / sys / ufs / ffs / 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 *
8e232de3 7 * @(#)ufs_vnops.c 7.84 (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
8e232de3
KM
33int ufs_chmod __P((struct vnode *, int, struct ucred *, struct proc *));
34int ufs_chown
35 __P((struct vnode *, u_int, u_int, struct ucred *, struct proc *));
031fd966 36
1a3cb193
KM
37#ifdef _NOQUAD
38#define SETHIGH(q, h) (q).val[_QUAD_HIGHWORD] = (h)
39#define SETLOW(q, l) (q).val[_QUAD_LOWWORD] = (l)
40#else /* QUAD */
41union _qcvt {
42 quad_t qcvt;
43 long val[2];
44};
45#define SETHIGH(q, h) { \
46 union _qcvt tmp; \
47 tmp.qcvt = (q); \
48 tmp.val[_QUAD_HIGHWORD] = (h); \
49 (q) = tmp.qcvt; \
50}
51#define SETLOW(q, l) { \
52 union _qcvt tmp; \
53 tmp.qcvt = (q); \
54 tmp.val[_QUAD_LOWWORD] = (l); \
55 (q) = tmp.qcvt; \
56}
57#endif /* QUAD */
58
4f083fd7 59/*
7188ac27 60 * Create a regular file
4f083fd7 61 */
031fd966 62int
6e57e9ff 63ufs_create(dvp, vpp, cnp, vap)
cfef4373
JH
64 struct vnode *dvp;
65 struct vnode **vpp;
66 struct componentname *cnp;
7188ac27 67 struct vattr *vap;
3e78e260 68{
7188ac27 69 int error;
3e78e260 70
031fd966 71 if (error =
cfef4373 72 ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode), dvp, vpp, cnp))
7188ac27 73 return (error);
7188ac27 74 return (0);
3e78e260
BJ
75}
76
4f083fd7 77/*
7188ac27 78 * Mknod vnode call
4f083fd7 79 */
7188ac27 80/* ARGSUSED */
031fd966 81int
6e57e9ff 82ufs_mknod(dvp, vpp, cnp, vap)
cfef4373
JH
83 struct vnode *dvp;
84 struct vnode **vpp;
85 struct componentname *cnp;
7188ac27 86 struct vattr *vap;
3e78e260 87{
c59af027 88 register struct inode *ip;
7188ac27 89 int error;
3e78e260 90
031fd966 91 if (error =
cfef4373 92 ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode), dvp, vpp, cnp))
7188ac27 93 return (error);
cfef4373 94 ip = VTOI(*vpp);
d1c43d7f
KM
95 ip->i_flag |= IACC|IUPD|ICHG;
96 if (vap->va_rdev != VNOVAL) {
7188ac27
KM
97 /*
98 * Want to be able to use this to make badblock
99 * inodes, so don't truncate the dev number.
100 */
b373e060 101 ip->i_rdev = vap->va_rdev;
7188ac27 102 }
7188ac27
KM
103 /*
104 * Remove inode so that it will be reloaded by iget and
105 * checked to see if it is an alias of an existing entry
106 * in the inode cache.
107 */
cfef4373
JH
108 vput(*vpp);
109 (*vpp)->v_type = VNON;
110 vgone(*vpp);
111 *vpp = 0;
7188ac27 112 return (0);
3e78e260
BJ
113}
114
115/*
7188ac27
KM
116 * Open called.
117 *
118 * Nothing to do.
3e78e260 119 */
7188ac27 120/* ARGSUSED */
031fd966 121int
5b169cb7 122ufs_open(vp, mode, cred, p)
7188ac27
KM
123 struct vnode *vp;
124 int mode;
125 struct ucred *cred;
5b169cb7 126 struct proc *p;
3e78e260 127{
3e78e260 128
7188ac27 129 return (0);
3e78e260
BJ
130}
131
132/*
7188ac27
KM
133 * Close called
134 *
135 * Update the times on the inode.
3e78e260 136 */
7188ac27 137/* ARGSUSED */
031fd966 138int
5b169cb7 139ufs_close(vp, fflag, cred, p)
7188ac27
KM
140 struct vnode *vp;
141 int fflag;
142 struct ucred *cred;
5b169cb7 143 struct proc *p;
3e78e260 144{
031fd966 145 register struct inode *ip;
3e78e260 146
031fd966 147 ip = VTOI(vp);
de67eefc 148 if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
7188ac27
KM
149 ITIMES(ip, &time, &time);
150 return (0);
3e78e260
BJ
151}
152
4b61628b
KM
153/*
154 * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
155 * The mode is shifted to select the owner/group/other fields. The
156 * super user is granted all permissions.
157 */
031fd966 158int
5b169cb7 159ufs_access(vp, mode, cred, p)
7188ac27 160 struct vnode *vp;
4b61628b 161 register int mode;
7188ac27 162 struct ucred *cred;
5b169cb7 163 struct proc *p;
3e78e260 164{
4b61628b
KM
165 register struct inode *ip = VTOI(vp);
166 register gid_t *gp;
167 int i, error;
3e78e260 168
4b61628b
KM
169#ifdef DIAGNOSTIC
170 if (!VOP_ISLOCKED(vp)) {
171 vprint("ufs_access: not locked", vp);
172 panic("ufs_access: not locked");
173 }
174#endif
175#ifdef QUOTA
176 if (mode & VWRITE) {
177 switch (vp->v_type) {
178 case VREG: case VDIR: case VLNK:
179 if (error = getinoquota(ip))
180 return (error);
181 }
182 }
183#endif /* QUOTA */
184 /*
185 * If you're the super-user, you always get access.
186 */
187 if (cred->cr_uid == 0)
188 return (0);
189 /*
190 * Access check is based on only one of owner, group, public.
191 * If not owner, then check group. If not a member of the
192 * group, then check public access.
193 */
194 if (cred->cr_uid != ip->i_uid) {
195 mode >>= 3;
196 gp = cred->cr_groups;
197 for (i = 0; i < cred->cr_ngroups; i++, gp++)
198 if (ip->i_gid == *gp)
199 goto found;
200 mode >>= 3;
201found:
202 ;
203 }
204 if ((ip->i_mode & mode) != 0)
205 return (0);
206 return (EACCES);
3e78e260
BJ
207}
208
7188ac27 209/* ARGSUSED */
031fd966 210int
5b169cb7 211ufs_getattr(vp, vap, cred, p)
7188ac27
KM
212 struct vnode *vp;
213 register struct vattr *vap;
214 struct ucred *cred;
5b169cb7 215 struct proc *p;
3e78e260 216{
031fd966 217 register struct inode *ip;
3e78e260 218
031fd966 219 ip = VTOI(vp);
7188ac27 220 ITIMES(ip, &time, &time);
3e78e260 221 /*
7188ac27 222 * Copy from inode table
3e78e260 223 */
7188ac27
KM
224 vap->va_fsid = ip->i_dev;
225 vap->va_fileid = ip->i_number;
226 vap->va_mode = ip->i_mode & ~IFMT;
227 vap->va_nlink = ip->i_nlink;
228 vap->va_uid = ip->i_uid;
229 vap->va_gid = ip->i_gid;
230 vap->va_rdev = (dev_t)ip->i_rdev;
4b61628b
KM
231#ifdef tahoe
232 vap->va_size = ip->i_size;
233 vap->va_size_rsv = 0;
234#else
8fae943a 235 vap->va_qsize = ip->i_din.di_qsize;
4b61628b 236#endif
7188ac27 237 vap->va_atime.tv_sec = ip->i_atime;
6ef53d70 238 vap->va_atime.tv_usec = 0;
7188ac27 239 vap->va_mtime.tv_sec = ip->i_mtime;
6ef53d70 240 vap->va_mtime.tv_usec = 0;
7188ac27 241 vap->va_ctime.tv_sec = ip->i_ctime;
6ef53d70 242 vap->va_ctime.tv_usec = 0;
d07fc92a
KM
243 vap->va_flags = ip->i_flags;
244 vap->va_gen = ip->i_gen;
7188ac27
KM
245 /* this doesn't belong here */
246 if (vp->v_type == VBLK)
247 vap->va_blocksize = BLKDEV_IOSIZE;
248 else if (vp->v_type == VCHR)
249 vap->va_blocksize = MAXBSIZE;
8eee8525 250 else
cc173077 251 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
a61a68d6 252 vap->va_bytes = dbtob(ip->i_blocks);
1a3cb193 253#ifdef _NOQUAD
8fae943a 254 vap->va_bytes_rsv = 0;
1a3cb193 255#endif
7188ac27 256 vap->va_type = vp->v_type;
1a3cb193 257 vap->va_filerev = ip->i_modrev;
7188ac27 258 return (0);
3e78e260
BJ
259}
260
261/*
7188ac27 262 * Set attribute vnode op. called from several syscalls
3e78e260 263 */
031fd966 264int
5b169cb7 265ufs_setattr(vp, vap, cred, p)
7188ac27
KM
266 register struct vnode *vp;
267 register struct vattr *vap;
268 register struct ucred *cred;
5b169cb7 269 struct proc *p;
3e78e260 270{
031fd966 271 register struct inode *ip;
031fd966 272 int error;
b4d1aee9 273
7188ac27 274 /*
031fd966 275 * Check for unsettable attributes.
7188ac27
KM
276 */
277 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
278 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
279 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
d07fc92a 280 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
7188ac27 281 return (EINVAL);
b4d1aee9 282 }
7188ac27
KM
283 /*
284 * Go through the fields and update iff not VNOVAL.
285 */
286 if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL)
8e232de3 287 if (error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred, p))
7188ac27
KM
288 return (error);
289 if (vap->va_size != VNOVAL) {
290 if (vp->v_type == VDIR)
291 return (EISDIR);
c59af027 292 if (error = VOP_TRUNCATE(vp, vap->va_size, 0)) /* IO_SYNC? */
7188ac27 293 return (error);
3e78e260 294 }
c59af027 295 ip = VTOI(vp);
7188ac27 296 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
de412887 297 if (cred->cr_uid != ip->i_uid &&
c6f5111d 298 (error = suser(cred, &p->p_acflag)))
de412887 299 return (error);
7188ac27
KM
300 if (vap->va_atime.tv_sec != VNOVAL)
301 ip->i_flag |= IACC;
302 if (vap->va_mtime.tv_sec != VNOVAL)
303 ip->i_flag |= IUPD;
304 ip->i_flag |= ICHG;
c59af027 305 if (error = VOP_UPDATE(vp, &vap->va_atime, &vap->va_mtime, 1))
7188ac27 306 return (error);
5485e062 307 }
031fd966 308 error = 0;
7188ac27 309 if (vap->va_mode != (u_short)VNOVAL)
8e232de3 310 error = ufs_chmod(vp, (int)vap->va_mode, cred, p);
d07fc92a
KM
311 if (vap->va_flags != VNOVAL) {
312 if (cred->cr_uid != ip->i_uid &&
c6f5111d 313 (error = suser(cred, &p->p_acflag)))
d07fc92a
KM
314 return (error);
315 if (cred->cr_uid == 0) {
316 ip->i_flags = vap->va_flags;
317 } else {
318 ip->i_flags &= 0xffff0000;
319 ip->i_flags |= (vap->va_flags & 0xffff);
320 }
321 ip->i_flag |= ICHG;
322 }
7188ac27 323 return (error);
528f664c
SL
324}
325
4f083fd7
SL
326/*
327 * Change the mode on a file.
328 * Inode must be locked before calling.
329 */
ca3e8b68 330static int
8e232de3 331ufs_chmod(vp, mode, cred, p)
7188ac27 332 register struct vnode *vp;
528f664c 333 register int mode;
8e232de3 334 register struct ucred *cred;
c6f5111d 335 struct proc *p;
528f664c 336{
7188ac27 337 register struct inode *ip = VTOI(vp);
de412887 338 int error;
197da11b 339
de412887 340 if (cred->cr_uid != ip->i_uid &&
c6f5111d 341 (error = suser(cred, &p->p_acflag)))
de412887 342 return (error);
7188ac27 343 if (cred->cr_uid) {
e57b6138 344 if (vp->v_type != VDIR && (mode & ISVTX))
39b5be4c 345 return (EFTYPE);
e57b6138 346 if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
39b5be4c 347 return (EPERM);
f94ceb3b 348 }
39b5be4c 349 ip->i_mode &= ~07777;
7188ac27 350 ip->i_mode |= mode & 07777;
3e78e260 351 ip->i_flag |= ICHG;
7188ac27 352 if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0)
8986c97c 353 (void) vnode_pager_uncache(vp);
47af7174 354 return (0);
5485e062
BJ
355}
356
528f664c
SL
357/*
358 * Perform chown operation on inode ip;
359 * inode must be locked prior to call.
360 */
ca3e8b68 361static int
8e232de3 362ufs_chown(vp, uid, gid, cred, p)
7188ac27 363 register struct vnode *vp;
031fd966
KB
364 u_int uid;
365 u_int gid;
8e232de3 366 struct ucred *cred;
c6f5111d 367 struct proc *p;
528f664c 368{
7188ac27 369 register struct inode *ip = VTOI(vp);
4b61628b
KM
370 uid_t ouid;
371 gid_t ogid;
372 int error = 0;
528f664c 373#ifdef QUOTA
4b61628b
KM
374 register int i;
375 long change;
bb1b75f4 376#endif
528f664c 377
7188ac27 378 if (uid == (u_short)VNOVAL)
bb1b75f4 379 uid = ip->i_uid;
7188ac27 380 if (gid == (u_short)VNOVAL)
bb1b75f4 381 gid = ip->i_gid;
18b0bce6
KB
382 /*
383 * If we don't own the file, are trying to change the owner
384 * of the file, or are not a member of the target group,
385 * the caller must be superuser or the call fails.
386 */
7188ac27
KM
387 if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
388 !groupmember((gid_t)gid, cred)) &&
c6f5111d 389 (error = suser(cred, &p->p_acflag)))
7188ac27 390 return (error);
4b61628b
KM
391 ouid = ip->i_uid;
392 ogid = ip->i_gid;
bb1b75f4 393#ifdef QUOTA
4b61628b
KM
394 if (error = getinoquota(ip))
395 return (error);
396 if (ouid == uid) {
397 dqrele(vp, ip->i_dquot[USRQUOTA]);
398 ip->i_dquot[USRQUOTA] = NODQUOT;
399 }
400 if (ogid == gid) {
401 dqrele(vp, ip->i_dquot[GRPQUOTA]);
402 ip->i_dquot[GRPQUOTA] = NODQUOT;
403 }
404 change = ip->i_blocks;
405 (void) chkdq(ip, -change, cred, CHOWN);
406 (void) chkiq(ip, -1, cred, CHOWN);
407 for (i = 0; i < MAXQUOTAS; i++) {
408 dqrele(vp, ip->i_dquot[i]);
409 ip->i_dquot[i] = NODQUOT;
410 }
f94ceb3b 411#endif
bb1b75f4
SL
412 ip->i_uid = uid;
413 ip->i_gid = gid;
528f664c 414#ifdef QUOTA
4b61628b
KM
415 if ((error = getinoquota(ip)) == 0) {
416 if (ouid == uid) {
417 dqrele(vp, ip->i_dquot[USRQUOTA]);
418 ip->i_dquot[USRQUOTA] = NODQUOT;
419 }
420 if (ogid == gid) {
421 dqrele(vp, ip->i_dquot[GRPQUOTA]);
422 ip->i_dquot[GRPQUOTA] = NODQUOT;
423 }
424 if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
425 if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
722a9b31 426 goto good;
4b61628b
KM
427 else
428 (void) chkdq(ip, -change, cred, CHOWN|FORCE);
429 }
430 for (i = 0; i < MAXQUOTAS; i++) {
431 dqrele(vp, ip->i_dquot[i]);
432 ip->i_dquot[i] = NODQUOT;
433 }
434 }
435 ip->i_uid = ouid;
436 ip->i_gid = ogid;
437 if (getinoquota(ip) == 0) {
438 if (ouid == uid) {
439 dqrele(vp, ip->i_dquot[USRQUOTA]);
440 ip->i_dquot[USRQUOTA] = NODQUOT;
441 }
442 if (ogid == gid) {
443 dqrele(vp, ip->i_dquot[GRPQUOTA]);
444 ip->i_dquot[GRPQUOTA] = NODQUOT;
445 }
722a9b31
KM
446 (void) chkdq(ip, change, cred, FORCE|CHOWN);
447 (void) chkiq(ip, 1, cred, FORCE|CHOWN);
6cb69bbe 448 (void) getinoquota(ip);
4b61628b 449 }
6cb69bbe 450 return (error);
722a9b31 451good:
6cb69bbe
KM
452 if (getinoquota(ip))
453 panic("chown: lost quota");
454#endif /* QUOTA */
4b61628b
KM
455 if (ouid != uid || ogid != gid)
456 ip->i_flag |= ICHG;
457 if (ouid != uid && cred->cr_uid != 0)
458 ip->i_mode &= ~ISUID;
459 if (ogid != gid && cred->cr_uid != 0)
460 ip->i_mode &= ~ISGID;
461 return (0);
d67a03eb
BJ
462}
463
7188ac27 464/* ARGSUSED */
031fd966 465int
5b169cb7 466ufs_ioctl(vp, com, data, fflag, cred, p)
7188ac27
KM
467 struct vnode *vp;
468 int com;
469 caddr_t data;
470 int fflag;
471 struct ucred *cred;
5b169cb7 472 struct proc *p;
bb1b75f4 473{
bb1b75f4 474
7188ac27
KM
475 return (ENOTTY);
476}
477
478/* ARGSUSED */
031fd966 479int
5b169cb7 480ufs_select(vp, which, fflags, cred, p)
7188ac27 481 struct vnode *vp;
d1c43d7f 482 int which, fflags;
7188ac27 483 struct ucred *cred;
5b169cb7 484 struct proc *p;
7188ac27
KM
485{
486
5b169cb7
KM
487 /*
488 * We should really check to see if I/O is possible.
489 */
490 return (1);
bb1b75f4 491}
d67a03eb 492
4f083fd7 493/*
7188ac27
KM
494 * Mmap a file
495 *
496 * NB Currently unsupported.
4f083fd7 497 */
7188ac27 498/* ARGSUSED */
031fd966 499int
5b169cb7 500ufs_mmap(vp, fflags, cred, p)
7188ac27
KM
501 struct vnode *vp;
502 int fflags;
503 struct ucred *cred;
5b169cb7 504 struct proc *p;
d67a03eb 505{
d67a03eb 506
7188ac27 507 return (EINVAL);
d67a03eb 508}
64d3a787 509
4f083fd7 510/*
7188ac27
KM
511 * Seek on a file
512 *
513 * Nothing to do, so just return.
4f083fd7 514 */
7188ac27 515/* ARGSUSED */
031fd966 516int
7188ac27
KM
517ufs_seek(vp, oldoff, newoff, cred)
518 struct vnode *vp;
519 off_t oldoff, newoff;
520 struct ucred *cred;
528f664c 521{
7188ac27
KM
522
523 return (0);
524}
525
526/*
527 * ufs remove
528 * Hard to avoid races here, especially
529 * in unlinking directories.
530 */
031fd966 531int
6e57e9ff 532ufs_remove(dvp, vp, cnp)
cfef4373
JH
533 struct vnode *dvp, *vp;
534 struct componentname *cnp;
7188ac27
KM
535{
536 register struct inode *ip, *dp;
537 int error;
538
cfef4373
JH
539 ip = VTOI(vp);
540 dp = VTOI(dvp);
541 error = ufs_dirremove(dvp, cnp);
7188ac27
KM
542 if (!error) {
543 ip->i_nlink--;
544 ip->i_flag |= ICHG;
528f664c 545 }
7188ac27
KM
546 if (dp == ip)
547 vrele(ITOV(ip));
548 else
031fd966
KB
549 ufs_iput(ip);
550 ufs_iput(dp);
7188ac27 551 return (error);
4f083fd7
SL
552}
553
554/*
7188ac27 555 * link vnode call
4f083fd7 556 */
031fd966 557int
532a2b44 558ufs_link(tdvp, vp, cnp)
cfef4373 559 struct vnode *tdvp;
532a2b44 560 register struct vnode *vp; /* source vnode */
cfef4373 561 struct componentname *cnp;
4f083fd7 562{
031fd966 563 register struct inode *ip;
7188ac27 564 int error;
4f083fd7 565
655da945 566#ifdef DIANOSTIC
cfef4373 567 if ((cnp->cn_flags & HASBUF) == 0)
655da945
KM
568 panic("ufs_link: no name");
569#endif
031fd966 570 ip = VTOI(vp);
655da945 571 if ((unsigned short)ip->i_nlink >= LINK_MAX) {
cfef4373 572 free(cnp->cn_pnbuf, M_NAMEI);
986aedaa 573 return (EMLINK);
655da945 574 }
cfef4373 575 if (tdvp != vp)
7188ac27 576 ILOCK(ip);
7188ac27
KM
577 ip->i_nlink++;
578 ip->i_flag |= ICHG;
c59af027 579 error = VOP_UPDATE(vp, &time, &time, 1);
7188ac27 580 if (!error)
cfef4373
JH
581 error = ufs_direnter(ip, tdvp, cnp);
582 if (tdvp != vp)
7188ac27 583 IUNLOCK(ip);
cfef4373
JH
584 FREE(cnp->cn_pnbuf, M_NAMEI);
585 vput(tdvp);
7188ac27
KM
586 if (error) {
587 ip->i_nlink--;
82252d2b 588 ip->i_flag |= ICHG;
7188ac27
KM
589 }
590 return (error);
528f664c
SL
591}
592
cfef4373
JH
593
594
595/*
596 * relookup - lookup a path name component
597 * Used by lookup to re-aquire things.
598 */
599int
6e57e9ff 600relookup(dvp, vpp, cnp)
cfef4373
JH
601 struct vnode *dvp, **vpp;
602 struct componentname *cnp;
603{
cfef4373
JH
604 register struct vnode *dp = 0; /* the directory we are searching */
605 struct vnode *tdp; /* saved dp */
606 struct mount *mp; /* mount table entry */
607 int docache; /* == 0 do not cache last component */
608 int wantparent; /* 1 => wantparent or lockparent flag */
609 int rdonly; /* lookup read-only flag bit */
d36023e3
KM
610 char *cp; /* DEBUG: check name ptr/len */
611 int newhash; /* DEBUG: check name hash */
cfef4373 612 int error = 0;
cfef4373
JH
613
614 /*
615 * Setup: break out flag bits into variables.
616 */
617 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
618 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
d7961b1d
JH
619 if (cnp->cn_nameiop == DELETE ||
620 (wantparent && cnp->cn_nameiop != CREATE))
cfef4373
JH
621 docache = 0;
622 rdonly = cnp->cn_flags & RDONLY;
623 cnp->cn_flags &= ~ISSYMLINK;
624 dp = dvp;
625 VOP_LOCK(dp);
626
627/* dirloop: */
628 /*
629 * Search a new directory.
630 *
631 * The cn_hash value is for use by vfs_cache.
632 * The last component of the filename is left accessible via
633 * cnp->cn_nameptr for callers that need the name. Callers needing
634 * the name set the SAVENAME flag. When done, they assume
635 * responsibility for freeing the pathname buffer.
636 */
6e57e9ff 637#ifdef NAMEI_DIAGNOSTIC
d36023e3 638 for (newhash = 0, cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
cfef4373
JH
639 newhash += (unsigned char)*cp;
640 if (newhash != cnp->cn_hash)
641 panic("relookup: bad hash");
642 if (cnp->cn_namelen != cp - cnp->cn_nameptr)
643 panic ("relookup: bad len");
d36023e3
KM
644 if (*cp != 0)
645 panic("relookup: not last component");
cfef4373 646 printf("{%s}: ", cnp->cn_nameptr);
cfef4373 647#endif
cfef4373
JH
648
649 /*
650 * Check for degenerate name (e.g. / or "")
651 * which is a way of talking about a directory,
652 * e.g. like "/." or ".".
653 */
654 if (cnp->cn_nameptr[0] == '\0') {
655 if (cnp->cn_nameiop != LOOKUP || wantparent) {
656 error = EISDIR;
657 goto bad;
658 }
659 if (dp->v_type != VDIR) {
660 error = ENOTDIR;
661 goto bad;
662 }
663 if (!(cnp->cn_flags & LOCKLEAF))
664 VOP_UNLOCK(dp);
665 *vpp = dp;
666 if (cnp->cn_flags & SAVESTART)
667 panic("lookup: SAVESTART");
668 return (0);
669 }
670
cfef4373
JH
671 if (cnp->cn_flags & ISDOTDOT)
672 panic ("relookup: lookup on dot-dot");
673
674 /*
675 * We now have a segment name to search for, and a directory to search.
676 */
677 if (error = VOP_LOOKUP(dp, vpp, cnp)) {
678#ifdef DIAGNOSTIC
679 if (*vpp != NULL)
680 panic("leaf should be empty");
cfef4373 681#endif
39a892ba 682 if (error != EJUSTRETURN)
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] != '.') {
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
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 */
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;
c59af027 1660 struct vnodeops *specops, *fifoops;
031fd966
KB
1661 struct vnode **vpp;
1662{
1663 struct inode *ip, *nip;
1664 struct vnode *vp, *nvp;
4a8fb1bb 1665 extern struct vnodeops spec_vnodeops;
031fd966
KB
1666
1667 vp = *vpp;
1668 ip = VTOI(vp);
1669 switch(vp->v_type = IFTOVT(ip->i_mode)) {
1670 case VCHR:
1671 case VBLK:
c59af027 1672 vp->v_op = specops;
031fd966 1673 if (nvp = checkalias(vp, ip->i_rdev, mntp)) {
c59af027 1674 /*
4a8fb1bb 1675 * Discard unneeded vnode, but save its inode.
c59af027 1676 */
4a8fb1bb
KM
1677 remque(ip);
1678 IUNLOCK(ip);
1679 nvp->v_data = vp->v_data;
1680 vp->v_data = NULL;
1681 vp->v_op = &spec_vnodeops;
1682 vrele(vp);
1683 vgone(vp);
c59af027 1684 /*
4a8fb1bb 1685 * Reinitialize aliased inode.
c59af027 1686 */
4a8fb1bb
KM
1687 vp = nvp;
1688 ip->i_vnode = vp;
1689 ufs_ihashins(ip);
031fd966
KB
1690 }
1691 break;
1692 case VFIFO:
1693#ifdef FIFO
c59af027 1694 vp->v_op = fifoops;
031fd966
KB
1695 break;
1696#else
1697 return (EOPNOTSUPP);
1698#endif
1699 }
031fd966
KB
1700 if (ip->i_number == ROOTINO)
1701 vp->v_flag |= VROOT;
1a3cb193
KM
1702 /*
1703 * Initialize modrev times
1704 */
1705 SETHIGH(ip->i_modrev, mono_time.tv_sec);
1706 SETLOW(ip->i_modrev, mono_time.tv_usec * 4294);
031fd966
KB
1707 *vpp = vp;
1708 return (0);
1709}
5b169cb7 1710
031fd966
KB
1711/*
1712 * Allocate a new inode.
1713 */
1714int
6e57e9ff 1715ufs_makeinode(mode, dvp, vpp, cnp)
031fd966 1716 int mode;
cfef4373 1717 struct vnode *dvp;
c59af027 1718 struct vnode **vpp;
cfef4373 1719 struct componentname *cnp;
031fd966
KB
1720{
1721 register struct inode *ip, *pdir;
c59af027 1722 struct vnode *tvp;
031fd966
KB
1723 int error;
1724
cfef4373 1725 pdir = VTOI(dvp);
031fd966 1726#ifdef DIANOSTIC
cfef4373 1727 if ((cnp->cn_flags & HASBUF) == 0)
031fd966
KB
1728 panic("ufs_makeinode: no name");
1729#endif
c59af027 1730 *vpp = NULL;
031fd966
KB
1731 if ((mode & IFMT) == 0)
1732 mode |= IFREG;
1733
cfef4373
JH
1734 if (error = VOP_VALLOC(dvp, mode, cnp->cn_cred, &tvp)) {
1735 free(cnp->cn_pnbuf, M_NAMEI);
031fd966
KB
1736 ufs_iput(pdir);
1737 return (error);
1738 }
c59af027 1739 ip = VTOI(tvp);
cfef4373 1740 ip->i_uid = cnp->cn_cred->cr_uid;
031fd966
KB
1741 ip->i_gid = pdir->i_gid;
1742#ifdef QUOTA
1743 if ((error = getinoquota(ip)) ||
cfef4373
JH
1744 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
1745 free(cnp->cn_pnbuf, M_NAMEI);
c59af027 1746 VOP_VFREE(tvp, ip->i_number, mode);
031fd966
KB
1747 ufs_iput(ip);
1748 ufs_iput(pdir);
1749 return (error);
1750 }
1751#endif
1752 ip->i_flag |= IACC|IUPD|ICHG;
1753 ip->i_mode = mode;
c59af027 1754 tvp->v_type = IFTOVT(mode); /* Rest init'd in iget() */
031fd966 1755 ip->i_nlink = 1;
cfef4373
JH
1756 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
1757 suser(cnp->cn_cred, NULL))
031fd966
KB
1758 ip->i_mode &= ~ISGID;
1759
1760 /*
1761 * Make sure inode goes to disk before directory entry.
1762 */
c59af027 1763 if (error = VOP_UPDATE(tvp, &time, &time, 1))
031fd966 1764 goto bad;
cfef4373 1765 if (error = ufs_direnter(ip, dvp, cnp))
031fd966 1766 goto bad;
cfef4373
JH
1767 if ((cnp->cn_flags & SAVESTART) == 0)
1768 FREE(cnp->cn_pnbuf, M_NAMEI);
031fd966 1769 ufs_iput(pdir);
c59af027 1770 *vpp = tvp;
031fd966
KB
1771 return (0);
1772
1773bad:
1774 /*
1775 * Write error occurred trying to update the inode
1776 * or the directory so must deallocate the inode.
1777 */
cfef4373 1778 free(cnp->cn_pnbuf, M_NAMEI);
031fd966
KB
1779 ufs_iput(pdir);
1780 ip->i_nlink = 0;
1781 ip->i_flag |= ICHG;
1782 ufs_iput(ip);
1783 return (error);
1784}