iflush takes a mount ptr instead of a dev;
[unix-history] / usr / src / sys / ufs / lfs / lfs_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 *
7188ac27
KM
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 *
b440c2b9 17 * @(#)lfs_vnops.c 7.18 (Berkeley) %G%
da7c5cc6 18 */
6459ebe0 19
94368568
JB
20#include "param.h"
21#include "systm.h"
94368568
JB
22#include "user.h"
23#include "kernel.h"
24#include "file.h"
25#include "stat.h"
94368568
JB
26#include "buf.h"
27#include "proc.h"
94368568
JB
28#include "uio.h"
29#include "socket.h"
30#include "socketvar.h"
7188ac27 31#include "conf.h"
94368568 32#include "mount.h"
7188ac27
KM
33#include "vnode.h"
34#include "../ufs/inode.h"
35#include "../ufs/fs.h"
36#include "../ufs/quota.h"
3e78e260 37
4f083fd7 38/*
7188ac27 39 * Global vfs data structures for ufs
4f083fd7 40 */
3e78e260 41
7188ac27
KM
42int ufs_lookup(),
43 ufs_create(),
44 ufs_mknod(),
45 ufs_open(),
46 ufs_close(),
47 ufs_access(),
48 ufs_getattr(),
49 ufs_setattr(),
50 ufs_read(),
51 ufs_write(),
52 ufs_ioctl(),
53 ufs_select(),
54 ufs_mmap(),
55 ufs_fsync(),
56 ufs_seek(),
57 ufs_remove(),
58 ufs_link(),
59 ufs_rename(),
60 ufs_mkdir(),
61 ufs_rmdir(),
62 ufs_symlink(),
63 ufs_readdir(),
64 ufs_readlink(),
65 ufs_abortop(),
66 ufs_inactive(),
67 ufs_lock(),
68 ufs_unlock(),
69 ufs_bmap(),
70 ufs_strategy();
71
72struct vnodeops ufs_vnodeops = {
73 ufs_lookup,
74 ufs_create,
75 ufs_mknod,
76 ufs_open,
77 ufs_close,
78 ufs_access,
79 ufs_getattr,
80 ufs_setattr,
81 ufs_read,
82 ufs_write,
83 ufs_ioctl,
84 ufs_select,
85 ufs_mmap,
86 ufs_fsync,
87 ufs_seek,
88 ufs_remove,
89 ufs_link,
90 ufs_rename,
91 ufs_mkdir,
92 ufs_rmdir,
93 ufs_symlink,
94 ufs_readdir,
95 ufs_readlink,
96 ufs_abortop,
97 ufs_inactive,
98 ufs_lock,
99 ufs_unlock,
100 ufs_bmap,
101 ufs_strategy,
102};
103
104enum vtype iftovt_tab[8] = {
105 VNON, VCHR, VDIR, VBLK, VREG, VLNK, VSOCK, VBAD,
106};
107int vttoif_tab[8] = {
108 0, IFREG, IFDIR, IFBLK, IFCHR, IFLNK, IFSOCK, IFMT,
109};
3e78e260 110
4f083fd7 111/*
7188ac27 112 * Create a regular file
4f083fd7 113 */
7188ac27
KM
114ufs_create(ndp, vap)
115 struct nameidata *ndp;
116 struct vattr *vap;
3e78e260 117{
7188ac27
KM
118 struct inode *ip;
119 int error;
3e78e260 120
7188ac27
KM
121 if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip))
122 return (error);
123 ndp->ni_vp = ITOV(ip);
124 return (0);
3e78e260
BJ
125}
126
4f083fd7 127/*
7188ac27 128 * Mknod vnode call
4f083fd7 129 */
7188ac27
KM
130/* ARGSUSED */
131ufs_mknod(ndp, vap, cred)
132 struct nameidata *ndp;
133 struct ucred *cred;
134 struct vattr *vap;
3e78e260 135{
7188ac27
KM
136 struct inode *ip;
137 int error;
3e78e260 138
7188ac27
KM
139 if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip))
140 return (error);
141 if (vap->va_rdev) {
142 /*
143 * Want to be able to use this to make badblock
144 * inodes, so don't truncate the dev number.
145 */
146 ITOV(ip)->v_rdev = ip->i_rdev = vap->va_rdev;
147 ip->i_flag |= IACC|IUPD|ICHG;
148 }
7188ac27
KM
149 /*
150 * Remove inode so that it will be reloaded by iget and
151 * checked to see if it is an alias of an existing entry
152 * in the inode cache.
153 */
154 remque(ip);
155 ip->i_forw = ip;
156 ip->i_back = ip;
9ed9b473
KM
157 ITOV(ip)->v_type = VNON;
158 iput(ip);
7188ac27 159 return (0);
3e78e260
BJ
160}
161
162/*
7188ac27
KM
163 * Open called.
164 *
165 * Nothing to do.
3e78e260 166 */
7188ac27
KM
167/* ARGSUSED */
168ufs_open(vp, mode, cred)
169 struct vnode *vp;
170 int mode;
171 struct ucred *cred;
3e78e260 172{
3e78e260 173
7188ac27 174 return (0);
3e78e260
BJ
175}
176
177/*
7188ac27
KM
178 * Close called
179 *
180 * Update the times on the inode.
3e78e260 181 */
7188ac27
KM
182/* ARGSUSED */
183ufs_close(vp, fflag, cred)
184 struct vnode *vp;
185 int fflag;
186 struct ucred *cred;
3e78e260 187{
7188ac27 188 register struct inode *ip = VTOI(vp);
3e78e260 189
7188ac27
KM
190 if (vp->v_count > 1 && !(ip->i_flag & ILOCKED))
191 ITIMES(ip, &time, &time);
192 return (0);
3e78e260
BJ
193}
194
7188ac27
KM
195ufs_access(vp, mode, cred)
196 struct vnode *vp;
197 int mode;
198 struct ucred *cred;
3e78e260 199{
3e78e260 200
7188ac27 201 return (iaccess(VTOI(vp), mode, cred));
3e78e260
BJ
202}
203
7188ac27
KM
204/* ARGSUSED */
205ufs_getattr(vp, vap, cred)
206 struct vnode *vp;
207 register struct vattr *vap;
208 struct ucred *cred;
3e78e260 209{
7188ac27 210 register struct inode *ip = VTOI(vp);
3e78e260 211
7188ac27 212 ITIMES(ip, &time, &time);
3e78e260 213 /*
7188ac27 214 * Copy from inode table
3e78e260 215 */
7188ac27
KM
216 vap->va_fsid = ip->i_dev;
217 vap->va_fileid = ip->i_number;
218 vap->va_mode = ip->i_mode & ~IFMT;
219 vap->va_nlink = ip->i_nlink;
220 vap->va_uid = ip->i_uid;
221 vap->va_gid = ip->i_gid;
222 vap->va_rdev = (dev_t)ip->i_rdev;
223 vap->va_size = ip->i_ic.ic_size.val[0];
224 vap->va_size1 = ip->i_ic.ic_size.val[1];
225 vap->va_atime.tv_sec = ip->i_atime;
6ef53d70 226 vap->va_atime.tv_usec = 0;
7188ac27 227 vap->va_mtime.tv_sec = ip->i_mtime;
6ef53d70 228 vap->va_mtime.tv_usec = 0;
7188ac27 229 vap->va_ctime.tv_sec = ip->i_ctime;
6ef53d70 230 vap->va_ctime.tv_usec = 0;
d07fc92a
KM
231 vap->va_flags = ip->i_flags;
232 vap->va_gen = ip->i_gen;
7188ac27
KM
233 /* this doesn't belong here */
234 if (vp->v_type == VBLK)
235 vap->va_blocksize = BLKDEV_IOSIZE;
236 else if (vp->v_type == VCHR)
237 vap->va_blocksize = MAXBSIZE;
8eee8525 238 else
7188ac27 239 vap->va_blocksize = ip->i_fs->fs_bsize;
a61a68d6 240 vap->va_bytes = dbtob(ip->i_blocks);
7188ac27
KM
241 vap->va_bytes1 = -1;
242 vap->va_type = vp->v_type;
243 return (0);
3e78e260
BJ
244}
245
246/*
7188ac27 247 * Set attribute vnode op. called from several syscalls
3e78e260 248 */
7188ac27
KM
249ufs_setattr(vp, vap, cred)
250 register struct vnode *vp;
251 register struct vattr *vap;
252 register struct ucred *cred;
3e78e260 253{
7188ac27
KM
254 register struct inode *ip = VTOI(vp);
255 int error = 0;
b4d1aee9 256
7188ac27
KM
257 /*
258 * Check for unsetable attributes.
259 */
260 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
261 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
262 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
d07fc92a 263 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
7188ac27 264 return (EINVAL);
b4d1aee9 265 }
7188ac27
KM
266 /*
267 * Go through the fields and update iff not VNOVAL.
268 */
269 if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL)
270 if (error = chown1(vp, vap->va_uid, vap->va_gid, cred))
271 return (error);
272 if (vap->va_size != VNOVAL) {
273 if (vp->v_type == VDIR)
274 return (EISDIR);
7188ac27
KM
275 if (error = itrunc(ip, vap->va_size))
276 return (error);
3e78e260 277 }
7188ac27 278 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
de412887
KM
279 if (cred->cr_uid != ip->i_uid &&
280 (error = suser(cred, &u.u_acflag)))
281 return (error);
7188ac27
KM
282 if (vap->va_atime.tv_sec != VNOVAL)
283 ip->i_flag |= IACC;
284 if (vap->va_mtime.tv_sec != VNOVAL)
285 ip->i_flag |= IUPD;
286 ip->i_flag |= ICHG;
287 if (error = iupdat(ip, &vap->va_atime, &vap->va_mtime, 1))
288 return (error);
5485e062 289 }
7188ac27
KM
290 if (vap->va_mode != (u_short)VNOVAL)
291 error = chmod1(vp, (int)vap->va_mode, cred);
d07fc92a
KM
292 if (vap->va_flags != VNOVAL) {
293 if (cred->cr_uid != ip->i_uid &&
294 (error = suser(cred, &u.u_acflag)))
295 return (error);
296 if (cred->cr_uid == 0) {
297 ip->i_flags = vap->va_flags;
298 } else {
299 ip->i_flags &= 0xffff0000;
300 ip->i_flags |= (vap->va_flags & 0xffff);
301 }
302 ip->i_flag |= ICHG;
303 }
7188ac27 304 return (error);
528f664c
SL
305}
306
4f083fd7
SL
307/*
308 * Change the mode on a file.
309 * Inode must be locked before calling.
310 */
7188ac27
KM
311chmod1(vp, mode, cred)
312 register struct vnode *vp;
528f664c 313 register int mode;
7188ac27 314 struct ucred *cred;
528f664c 315{
7188ac27 316 register struct inode *ip = VTOI(vp);
de412887 317 int error;
197da11b 318
de412887
KM
319 if (cred->cr_uid != ip->i_uid &&
320 (error = suser(cred, &u.u_acflag)))
321 return (error);
3e78e260 322 ip->i_mode &= ~07777;
7188ac27
KM
323 if (cred->cr_uid) {
324 if (vp->v_type != VDIR)
47af7174 325 mode &= ~ISVTX;
7188ac27 326 if (!groupmember(ip->i_gid, cred))
bb1b75f4 327 mode &= ~ISGID;
f94ceb3b 328 }
7188ac27 329 ip->i_mode |= mode & 07777;
3e78e260 330 ip->i_flag |= ICHG;
7188ac27
KM
331 if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0)
332 xrele(vp);
47af7174 333 return (0);
5485e062
BJ
334}
335
528f664c
SL
336/*
337 * Perform chown operation on inode ip;
338 * inode must be locked prior to call.
339 */
7188ac27
KM
340chown1(vp, uid, gid, cred)
341 register struct vnode *vp;
342 uid_t uid;
343 gid_t gid;
344 struct ucred *cred;
528f664c 345{
7188ac27 346 register struct inode *ip = VTOI(vp);
528f664c
SL
347#ifdef QUOTA
348 register long change;
bb1b75f4 349#endif
7188ac27 350 int error;
528f664c 351
7188ac27 352 if (uid == (u_short)VNOVAL)
bb1b75f4 353 uid = ip->i_uid;
7188ac27 354 if (gid == (u_short)VNOVAL)
bb1b75f4 355 gid = ip->i_gid;
18b0bce6
KB
356 /*
357 * If we don't own the file, are trying to change the owner
358 * of the file, or are not a member of the target group,
359 * the caller must be superuser or the call fails.
360 */
7188ac27
KM
361 if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
362 !groupmember((gid_t)gid, cred)) &&
363 (error = suser(cred, &u.u_acflag)))
364 return (error);
bb1b75f4 365#ifdef QUOTA
3809bf69 366 if (ip->i_uid == uid) /* this just speeds things a little */
0a77f278 367 change = 0;
2e073567
SL
368 else
369 change = ip->i_blocks;
370 (void) chkdq(ip, -change, 1);
371 (void) chkiq(ip->i_dev, ip, ip->i_uid, 1);
0a77f278 372 dqrele(ip->i_dquot);
f94ceb3b 373#endif
b440c2b9
KM
374 if (ip->i_uid != uid && cred->cr_uid != 0)
375 ip->i_mode &= ~ISUID;
376 if (ip->i_gid != gid && cred->cr_uid != 0)
377 ip->i_mode &= ~ISGID;
bb1b75f4
SL
378 ip->i_uid = uid;
379 ip->i_gid = gid;
3e78e260 380 ip->i_flag |= ICHG;
528f664c 381#ifdef QUOTA
0a77f278 382 ip->i_dquot = inoquota(ip);
2e073567 383 (void) chkdq(ip, change, 1);
8011f5df 384 (void) chkiq(ip->i_dev, (struct inode *)NULL, (uid_t)uid, 1);
2e073567
SL
385 return (u.u_error); /* should == 0 ALWAYS !! */
386#else
bb1b75f4 387 return (0);
2e073567 388#endif
d67a03eb
BJ
389}
390
7188ac27
KM
391/* ARGSUSED */
392ufs_ioctl(vp, com, data, fflag, cred)
393 struct vnode *vp;
394 int com;
395 caddr_t data;
396 int fflag;
397 struct ucred *cred;
bb1b75f4 398{
bb1b75f4 399
7188ac27
KM
400 printf("ufs_ioctl called with type %d\n", vp->v_type);
401 return (ENOTTY);
402}
403
404/* ARGSUSED */
405ufs_select(vp, which, cred)
406 struct vnode *vp;
407 int which;
408 struct ucred *cred;
409{
410
411 printf("ufs_select called with type %d\n", vp->v_type);
412 return (1); /* XXX */
bb1b75f4 413}
d67a03eb 414
4f083fd7 415/*
7188ac27
KM
416 * Mmap a file
417 *
418 * NB Currently unsupported.
4f083fd7 419 */
7188ac27
KM
420/* ARGSUSED */
421ufs_mmap(vp, fflags, cred)
422 struct vnode *vp;
423 int fflags;
424 struct ucred *cred;
d67a03eb 425{
d67a03eb 426
7188ac27 427 return (EINVAL);
d67a03eb 428}
64d3a787 429
4f083fd7 430/*
7188ac27 431 * Synch an open file.
4f083fd7 432 */
7188ac27
KM
433/* ARGSUSED */
434ufs_fsync(vp, fflags, cred)
435 struct vnode *vp;
436 int fflags;
437 struct ucred *cred;
528f664c 438{
7188ac27
KM
439 register struct inode *ip = VTOI(vp);
440 int error;
441
442 ILOCK(ip);
443 if (fflags&FWRITE)
444 ip->i_flag |= ICHG;
445 error = syncip(ip);
446 IUNLOCK(ip);
447 return (error);
528f664c
SL
448}
449
4f083fd7 450/*
7188ac27
KM
451 * Seek on a file
452 *
453 * Nothing to do, so just return.
4f083fd7 454 */
7188ac27
KM
455/* ARGSUSED */
456ufs_seek(vp, oldoff, newoff, cred)
457 struct vnode *vp;
458 off_t oldoff, newoff;
459 struct ucred *cred;
528f664c 460{
7188ac27
KM
461
462 return (0);
463}
464
465/*
466 * ufs remove
467 * Hard to avoid races here, especially
468 * in unlinking directories.
469 */
470ufs_remove(ndp)
471 struct nameidata *ndp;
472{
473 register struct inode *ip, *dp;
474 int error;
475
476 ip = VTOI(ndp->ni_vp);
477 dp = VTOI(ndp->ni_dvp);
478 error = dirremove(ndp);
479 if (!error) {
480 ip->i_nlink--;
481 ip->i_flag |= ICHG;
528f664c 482 }
7188ac27
KM
483 if (dp == ip)
484 vrele(ITOV(ip));
485 else
486 iput(ip);
487 iput(dp);
488 return (error);
4f083fd7
SL
489}
490
491/*
7188ac27 492 * link vnode call
4f083fd7 493 */
7188ac27
KM
494ufs_link(vp, ndp)
495 register struct vnode *vp;
496 register struct nameidata *ndp;
4f083fd7 497{
7188ac27
KM
498 register struct inode *ip = VTOI(vp);
499 int error;
4f083fd7 500
7188ac27
KM
501 if (ndp->ni_dvp != vp)
502 ILOCK(ip);
503 if (ip->i_nlink == LINK_MAX - 1) {
504 error = EMLINK;
505 goto out;
506 }
507 ip->i_nlink++;
508 ip->i_flag |= ICHG;
509 error = iupdat(ip, &time, &time, 1);
510 if (!error)
511 error = direnter(ip, ndp);
512out:
513 if (ndp->ni_dvp != vp)
514 IUNLOCK(ip);
515 if (error) {
516 ip->i_nlink--;
82252d2b 517 ip->i_flag |= ICHG;
7188ac27
KM
518 }
519 return (error);
528f664c
SL
520}
521
4f083fd7
SL
522/*
523 * Rename system call.
524 * rename("foo", "bar");
525 * is essentially
526 * unlink("bar");
527 * link("foo", "bar");
528 * unlink("foo");
529 * but ``atomically''. Can't do full commit without saving state in the
530 * inode on disk which isn't feasible at this time. Best we can do is
531 * always guarantee the target exists.
532 *
533 * Basic algorithm is:
534 *
535 * 1) Bump link count on source while we're linking it to the
7188ac27 536 * target. This also ensure the inode won't be deleted out
68f21562
KM
537 * from underneath us while we work (it may be truncated by
538 * a concurrent `trunc' or `open' for creation).
4f083fd7
SL
539 * 2) Link source to destination. If destination already exists,
540 * delete it first.
68f21562
KM
541 * 3) Unlink source reference to inode if still around. If a
542 * directory was moved and the parent of the destination
4f083fd7
SL
543 * is different from the source, patch the ".." entry in the
544 * directory.
4f083fd7 545 */
7188ac27
KM
546ufs_rename(fndp, tndp)
547 register struct nameidata *fndp, *tndp;
528f664c 548{
4f083fd7 549 register struct inode *ip, *xp, *dp;
68f21562
KM
550 struct dirtemplate dirbuf;
551 int doingdirectory = 0, oldparent = 0, newparent = 0;
a5390dce 552 int error = 0;
4f083fd7 553
7188ac27
KM
554 dp = VTOI(fndp->ni_dvp);
555 ip = VTOI(fndp->ni_vp);
556 ILOCK(ip);
4f083fd7 557 if ((ip->i_mode&IFMT) == IFDIR) {
7188ac27 558 register struct direct *d = &fndp->ni_dent;
4f083fd7 559
4f083fd7 560 /*
046f18d1 561 * Avoid ".", "..", and aliases of "." for obvious reasons.
4f083fd7 562 */
7188ac27
KM
563 if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip ||
564 fndp->ni_isdotdot || (ip->i_flag & IRENAME)) {
565 IUNLOCK(ip);
566 ufs_abortop(fndp);
567 ufs_abortop(tndp);
568 return (EINVAL);
4f083fd7 569 }
68f21562 570 ip->i_flag |= IRENAME;
4f083fd7
SL
571 oldparent = dp->i_number;
572 doingdirectory++;
573 }
7188ac27 574 vrele(fndp->ni_dvp);
4f083fd7
SL
575
576 /*
577 * 1) Bump link count while we're moving stuff
578 * around. If we crash somewhere before
579 * completing our work, the link count
580 * may be wrong, but correctable.
581 */
582 ip->i_nlink++;
583 ip->i_flag |= ICHG;
7188ac27 584 error = iupdat(ip, &time, &time, 1);
a388503d 585 IUNLOCK(ip);
4f083fd7
SL
586
587 /*
588 * When the target exists, both the directory
7188ac27 589 * and target vnodes are returned locked.
4f083fd7 590 */
7188ac27
KM
591 dp = VTOI(tndp->ni_dvp);
592 xp = NULL;
593 if (tndp->ni_vp)
594 xp = VTOI(tndp->ni_vp);
046f18d1
SL
595 /*
596 * If ".." must be changed (ie the directory gets a new
81552f0f
KM
597 * parent) then the source directory must not be in the
598 * directory heirarchy above the target, as this would
599 * orphan everything below the source directory. Also
600 * the user must have write permission in the source so
601 * as to be able to change "..". We must repeat the call
602 * to namei, as the parent directory is unlocked by the
603 * call to checkpath().
046f18d1 604 */
68f21562
KM
605 if (oldparent != dp->i_number)
606 newparent = dp->i_number;
607 if (doingdirectory && newparent) {
7188ac27 608 if (error = iaccess(ip, IWRITE, tndp->ni_cred))
81552f0f 609 goto bad;
7188ac27 610 tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE;
81552f0f 611 do {
7188ac27 612 dp = VTOI(tndp->ni_dvp);
81552f0f 613 if (xp != NULL)
79cf21e2 614 iput(xp);
7188ac27 615 if (error = checkpath(ip, dp, tndp->ni_cred))
81552f0f 616 goto out;
7188ac27 617 if (error = namei(tndp))
81552f0f 618 goto out;
7188ac27
KM
619 xp = NULL;
620 if (tndp->ni_vp)
621 xp = VTOI(tndp->ni_vp);
622 } while (dp != VTOI(tndp->ni_dvp));
81552f0f 623 }
4f083fd7
SL
624 /*
625 * 2) If target doesn't exist, link the target
626 * to the source and unlink the source.
627 * Otherwise, rewrite the target directory
628 * entry to reference the source inode and
629 * expunge the original entry's existence.
630 */
4f083fd7 631 if (xp == NULL) {
7188ac27
KM
632 if (dp->i_dev != ip->i_dev)
633 panic("rename: EXDEV");
4f083fd7 634 /*
68f21562
KM
635 * Account for ".." in new directory.
636 * When source and destination have the same
637 * parent we don't fool with the link count.
4f083fd7 638 */
68f21562 639 if (doingdirectory && newparent) {
4f083fd7
SL
640 dp->i_nlink++;
641 dp->i_flag |= ICHG;
7188ac27 642 error = iupdat(dp, &time, &time, 1);
4f083fd7 643 }
7188ac27 644 if (error = direnter(ip, tndp))
4f083fd7
SL
645 goto out;
646 } else {
7188ac27
KM
647 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
648 panic("rename: EXDEV");
e69c3c9c
SL
649 /*
650 * Short circuit rename(foo, foo).
651 */
652 if (xp->i_number == ip->i_number)
7188ac27 653 panic("rename: same file");
80cee150
JB
654 /*
655 * If the parent directory is "sticky", then the user must
656 * own the parent directory, or the destination of the rename,
657 * otherwise the destination may not be changed (except by
658 * root). This implements append-only directories.
659 */
7188ac27
KM
660 if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 &&
661 tndp->ni_cred->cr_uid != dp->i_uid &&
662 xp->i_uid != tndp->ni_cred->cr_uid) {
80cee150
JB
663 error = EPERM;
664 goto bad;
665 }
4f083fd7 666 /*
a5390dce
SL
667 * Target must be empty if a directory
668 * and have no links to it.
4f083fd7
SL
669 * Also, insure source and target are
670 * compatible (both directories, or both
671 * not directories).
672 */
673 if ((xp->i_mode&IFMT) == IFDIR) {
7188ac27
KM
674 if (!dirempty(xp, dp->i_number, tndp->ni_cred) ||
675 xp->i_nlink > 2) {
a5390dce 676 error = ENOTEMPTY;
4f083fd7
SL
677 goto bad;
678 }
679 if (!doingdirectory) {
a5390dce 680 error = ENOTDIR;
4f083fd7
SL
681 goto bad;
682 }
7188ac27 683 cache_purge(ITOV(dp));
4f083fd7 684 } else if (doingdirectory) {
a5390dce 685 error = EISDIR;
4f083fd7
SL
686 goto bad;
687 }
7188ac27
KM
688 if (error = dirrewrite(dp, ip, tndp))
689 goto bad;
690 vput(ITOV(dp));
4f083fd7 691 /*
a5390dce
SL
692 * Adjust the link count of the target to
693 * reflect the dirrewrite above. If this is
694 * a directory it is empty and there are
695 * no links to it, so we can squash the inode and
696 * any space associated with it. We disallowed
697 * renaming over top of a directory with links to
68f21562
KM
698 * it above, as the remaining link would point to
699 * a directory without "." or ".." entries.
4f083fd7 700 */
a5390dce 701 xp->i_nlink--;
4f083fd7 702 if (doingdirectory) {
a5390dce
SL
703 if (--xp->i_nlink != 0)
704 panic("rename: linked directory");
7188ac27 705 error = itrunc(xp, (u_long)0);
a5390dce 706 }
4f083fd7 707 xp->i_flag |= ICHG;
88d931ba 708 iput(xp);
31db12cb 709 xp = NULL;
4f083fd7
SL
710 }
711
712 /*
713 * 3) Unlink the source.
714 */
7188ac27
KM
715 fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF;
716 (void)namei(fndp);
717 if (fndp->ni_vp != NULL) {
718 xp = VTOI(fndp->ni_vp);
719 dp = VTOI(fndp->ni_dvp);
720 } else {
79cf21e2
KM
721 if (fndp->ni_dvp != NULL)
722 vput(fndp->ni_dvp);
7188ac27 723 xp = NULL;
4f1a9037 724 dp = NULL;
7188ac27 725 }
4f083fd7 726 /*
7188ac27 727 * Ensure that the directory entry still exists and has not
68f21562
KM
728 * changed while the new name has been entered. If the source is
729 * a file then the entry may have been unlinked or renamed. In
730 * either case there is no further work to be done. If the source
731 * is a directory then it cannot have been rmdir'ed; its link
732 * count of three would cause a rmdir to fail with ENOTEMPTY.
7188ac27 733 * The IRENAME flag ensures that it cannot be moved by another
68f21562 734 * rename.
4f083fd7 735 */
4f1a9037 736 if (xp != ip) {
68f21562 737 if (doingdirectory)
4f1a9037 738 panic("rename: lost dir entry");
68f21562 739 } else {
4f083fd7 740 /*
68f21562
KM
741 * If the source is a directory with a
742 * new parent, the link count of the old
743 * parent directory must be decremented
744 * and ".." set to point to the new parent.
4f083fd7 745 */
68f21562 746 if (doingdirectory && newparent) {
4f083fd7
SL
747 dp->i_nlink--;
748 dp->i_flag |= ICHG;
68f21562 749 error = rdwri(UIO_READ, xp, (caddr_t)&dirbuf,
7188ac27 750 sizeof (struct dirtemplate), (off_t)0,
93e273b9 751 UIO_SYSSPACE, tndp->ni_cred, (int *)0);
68f21562
KM
752 if (error == 0) {
753 if (dirbuf.dotdot_namlen != 2 ||
754 dirbuf.dotdot_name[0] != '.' ||
755 dirbuf.dotdot_name[1] != '.') {
756 printf("rename: mangled dir\n");
757 } else {
758 dirbuf.dotdot_ino = newparent;
759 (void) rdwri(UIO_WRITE, xp,
760 (caddr_t)&dirbuf,
761 sizeof (struct dirtemplate),
93e273b9 762 (off_t)0, UIO_SYSSPACE,
7188ac27
KM
763 tndp->ni_cred, (int *)0);
764 cache_purge(ITOV(dp));
68f21562
KM
765 }
766 }
4f083fd7 767 }
7188ac27
KM
768 error = dirremove(fndp);
769 if (!error) {
68f21562
KM
770 xp->i_nlink--;
771 xp->i_flag |= ICHG;
4f083fd7 772 }
68f21562 773 xp->i_flag &= ~IRENAME;
4f083fd7 774 }
4f083fd7 775 if (dp)
7188ac27 776 vput(ITOV(dp));
68f21562 777 if (xp)
7188ac27
KM
778 vput(ITOV(xp));
779 vrele(ITOV(ip));
780 return (error);
a5390dce 781
4f083fd7 782bad:
4f083fd7 783 if (xp)
7188ac27
KM
784 vput(ITOV(xp));
785 vput(ITOV(dp));
4f083fd7
SL
786out:
787 ip->i_nlink--;
788 ip->i_flag |= ICHG;
7188ac27
KM
789 vrele(ITOV(ip));
790 return (error);
64d3a787 791}
88a7a62a
SL
792
793/*
794 * A virgin directory (no blushing please).
795 */
796struct dirtemplate mastertemplate = {
797 0, 12, 1, ".",
798 0, DIRBLKSIZ - 12, 2, ".."
799};
800
801/*
802 * Mkdir system call
803 */
7188ac27
KM
804ufs_mkdir(ndp, vap)
805 struct nameidata *ndp;
806 struct vattr *vap;
88a7a62a 807{
88a7a62a 808 register struct inode *ip, *dp;
7188ac27
KM
809 struct inode *tip;
810 struct vnode *dvp;
88a7a62a 811 struct dirtemplate dirtemplate;
7188ac27
KM
812 int error;
813 int dmode;
814
815 dvp = ndp->ni_dvp;
816 dp = VTOI(dvp);
817 dmode = vap->va_mode&0777;
818 dmode |= IFDIR;
88a7a62a
SL
819 /*
820 * Must simulate part of maknode here
821 * in order to acquire the inode, but
822 * not have it entered in the parent
823 * directory. The entry is made later
824 * after writing "." and ".." entries out.
825 */
7188ac27
KM
826 error = ialloc(dp, dirpref(dp->i_fs), dmode, &tip);
827 if (error) {
88a7a62a 828 iput(dp);
7188ac27 829 return (error);
88a7a62a 830 }
7188ac27 831 ip = tip;
88a7a62a
SL
832#ifdef QUOTA
833 if (ip->i_dquot != NODQUOT)
834 panic("mkdir: dquot");
835#endif
836 ip->i_flag |= IACC|IUPD|ICHG;
7188ac27
KM
837 ip->i_mode = dmode;
838 ITOV(ip)->v_type = VDIR; /* Rest init'd in iget() */
88a7a62a 839 ip->i_nlink = 2;
7188ac27 840 ip->i_uid = ndp->ni_cred->cr_uid;
88a7a62a
SL
841 ip->i_gid = dp->i_gid;
842#ifdef QUOTA
843 ip->i_dquot = inoquota(ip);
844#endif
7188ac27 845 error = iupdat(ip, &time, &time, 1);
88a7a62a
SL
846
847 /*
848 * Bump link count in parent directory
849 * to reflect work done below. Should
850 * be done before reference is created
851 * so reparation is possible if we crash.
852 */
853 dp->i_nlink++;
854 dp->i_flag |= ICHG;
7188ac27 855 error = iupdat(dp, &time, &time, 1);
88a7a62a
SL
856
857 /*
858 * Initialize directory with "."
859 * and ".." from static template.
860 */
861 dirtemplate = mastertemplate;
862 dirtemplate.dot_ino = ip->i_number;
863 dirtemplate.dotdot_ino = dp->i_number;
7188ac27
KM
864 error = rdwri(UIO_WRITE, ip, (caddr_t)&dirtemplate,
865 sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
866 ndp->ni_cred, (int *)0);
867 if (error) {
88a7a62a
SL
868 dp->i_nlink--;
869 dp->i_flag |= ICHG;
870 goto bad;
871 }
7188ac27
KM
872 if (DIRBLKSIZ > dp->i_fs->fs_fsize)
873 panic("mkdir: blksize"); /* XXX - should grow w/balloc() */
23de9f20
KM
874 else
875 ip->i_size = DIRBLKSIZ;
88a7a62a
SL
876 /*
877 * Directory all set up, now
878 * install the entry for it in
879 * the parent directory.
880 */
7188ac27 881 error = direnter(ip, ndp);
88a7a62a 882 dp = NULL;
7188ac27 883 if (error) {
715baff1 884 ndp->ni_nameiop = LOOKUP | NOCACHE;
7188ac27
KM
885 error = namei(ndp);
886 if (!error) {
887 dp = VTOI(ndp->ni_vp);
88a7a62a
SL
888 dp->i_nlink--;
889 dp->i_flag |= ICHG;
890 }
891 }
892bad:
893 /*
894 * No need to do an explicit itrunc here,
7188ac27 895 * vrele will do this for us because we set
88a7a62a
SL
896 * the link count to 0.
897 */
7188ac27 898 if (error) {
88a7a62a
SL
899 ip->i_nlink = 0;
900 ip->i_flag |= ICHG;
cbcdacd6
KM
901 iput(ip);
902 } else
903 ndp->ni_vp = ITOV(ip);
88a7a62a
SL
904 if (dp)
905 iput(dp);
7188ac27 906 return (error);
88a7a62a
SL
907}
908
909/*
910 * Rmdir system call.
911 */
7188ac27
KM
912ufs_rmdir(ndp)
913 register struct nameidata *ndp;
88a7a62a 914{
88a7a62a 915 register struct inode *ip, *dp;
7188ac27
KM
916 int error = 0;
917
918 ip = VTOI(ndp->ni_vp);
919 dp = VTOI(ndp->ni_dvp);
88a7a62a
SL
920 /*
921 * No rmdir "." please.
922 */
923 if (dp == ip) {
7188ac27 924 vrele(ITOV(dp));
88a7a62a 925 iput(ip);
7188ac27 926 return (EINVAL);
88a7a62a
SL
927 }
928 /*
929 * Verify the directory is empty (and valid).
930 * (Rmdir ".." won't be valid since
931 * ".." will contain a reference to
932 * the current directory and thus be
933 * non-empty.)
934 */
7188ac27
KM
935 if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) {
936 error = ENOTEMPTY;
88a7a62a
SL
937 goto out;
938 }
939 /*
940 * Delete reference to directory before purging
941 * inode. If we crash in between, the directory
942 * will be reattached to lost+found,
943 */
7188ac27 944 if (error = dirremove(ndp))
88a7a62a
SL
945 goto out;
946 dp->i_nlink--;
947 dp->i_flag |= ICHG;
7188ac27 948 cache_purge(ITOV(dp));
88a7a62a 949 iput(dp);
7188ac27 950 ndp->ni_dvp = NULL;
88a7a62a
SL
951 /*
952 * Truncate inode. The only stuff left
953 * in the directory is "." and "..". The
954 * "." reference is inconsequential since
955 * we're quashing it. The ".." reference
956 * has already been adjusted above. We've
957 * removed the "." reference and the reference
958 * in the parent directory, but there may be
959 * other hard links so decrement by 2 and
960 * worry about them later.
961 */
962 ip->i_nlink -= 2;
7188ac27
KM
963 error = itrunc(ip, (u_long)0);
964 cache_purge(ITOV(ip));
88a7a62a 965out:
7188ac27 966 if (ndp->ni_dvp)
88a7a62a
SL
967 iput(dp);
968 iput(ip);
7188ac27 969 return (error);
88a7a62a
SL
970}
971
7188ac27
KM
972/*
973 * symlink -- make a symbolic link
974 */
975ufs_symlink(ndp, vap, target)
976 struct nameidata *ndp;
977 struct vattr *vap;
978 char *target;
979{
980 struct inode *ip;
981 int error;
982
983 error = maknode(IFLNK | vap->va_mode, ndp, &ip);
984 if (error)
985 return (error);
986 error = rdwri(UIO_WRITE, ip, target, strlen(target), (off_t)0,
987 UIO_SYSSPACE, ndp->ni_cred, (int *)0);
988 iput(ip);
989 return (error);
990}
991
992/*
993 * Vnode op for read and write
994 */
995ufs_readdir(vp, uio, offp, cred)
996 struct vnode *vp;
997 register struct uio *uio;
998 off_t *offp;
999 struct ucred *cred;
88a7a62a 1000{
7188ac27
KM
1001 register struct inode *ip = VTOI(vp);
1002 int count, error;
88a7a62a 1003
7188ac27
KM
1004 ILOCK(ip);
1005 uio->uio_offset = *offp;
1006 count = uio->uio_resid;
1007 count &= ~(DIRBLKSIZ - 1);
1008 if (vp->v_type != VDIR || uio->uio_iovcnt != 1 ||
1009 (count < DIRBLKSIZ) || (uio->uio_offset & (DIRBLKSIZ -1))) {
1010 IUNLOCK(ip);
1011 return (EINVAL);
1012 }
1013 uio->uio_resid = count;
1014 uio->uio_iov->iov_len = count;
1015 error = readip(ip, uio, cred);
1016 *offp += count - uio->uio_resid;
1017 IUNLOCK(ip);
1018 return (error);
1019}
1020
1021/*
1022 * Return target name of a symbolic link
1023 */
1024ufs_readlink(vp, uiop, cred)
1025 struct vnode *vp;
1026 struct uio *uiop;
1027 struct ucred *cred;
1028{
1029
1030 return (readip(VTOI(vp), uiop, cred));
1031}
1032
1033/*
1034 * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually
1035 * done. Iff ni_vp/ni_dvp not null and locked, unlock.
1036 */
1037ufs_abortop(ndp)
1038 register struct nameidata *ndp;
1039{
1040 register struct inode *ip;
1041
1042 if (ndp->ni_vp) {
1043 ip = VTOI(ndp->ni_vp);
1044 if (ip->i_flag & ILOCKED)
1045 IUNLOCK(ip);
1046 vrele(ndp->ni_vp);
8462a185 1047 }
7188ac27
KM
1048 if (ndp->ni_dvp) {
1049 ip = VTOI(ndp->ni_dvp);
1050 if (ip->i_flag & ILOCKED)
1051 IUNLOCK(ip);
1052 vrele(ndp->ni_dvp);
88a7a62a 1053 }
7188ac27
KM
1054 return;
1055}
1056
1057ufs_lock(vp)
1058 struct vnode *vp;
1059{
1060 register struct inode *ip = VTOI(vp);
1061
1062 ILOCK(ip);
1063 return (0);
1064}
1065
1066ufs_unlock(vp)
1067 struct vnode *vp;
1068{
1069 register struct inode *ip = VTOI(vp);
1070
1071 if (!(ip->i_flag & ILOCKED))
1072 panic("ufs_unlock NOT LOCKED");
1073 IUNLOCK(ip);
1074 return (0);
1075}
1076
1077/*
1078 * Get access to bmap
1079 */
1080ufs_bmap(vp, bn, vpp, bnp)
1081 struct vnode *vp;
1082 daddr_t bn;
1083 struct vnode **vpp;
1084 daddr_t *bnp;
1085{
1086 struct inode *ip = VTOI(vp);
1087
1088 if (vpp != NULL)
1089 *vpp = ip->i_devvp;
1090 if (bnp == NULL)
1091 return (0);
1092 return (bmap(ip, bn, bnp, (daddr_t *)0, (int *)0));
88a7a62a
SL
1093}
1094
1095/*
7188ac27 1096 * Just call the device strategy routine
88a7a62a 1097 */
7188ac27
KM
1098ufs_strategy(bp)
1099 register struct buf *bp;
88a7a62a 1100{
7188ac27
KM
1101 (*bdevsw[major(bp->b_dev)].d_strategy)(bp);
1102 return (0);
1103}
88a7a62a 1104
7188ac27
KM
1105/*
1106 * Make a new file.
1107 */
1108maknode(mode, ndp, ipp)
1109 int mode;
1110 register struct nameidata *ndp;
1111 struct inode **ipp;
1112{
1113 register struct inode *ip;
1114 struct inode *tip;
1115 register struct inode *pdir = VTOI(ndp->ni_dvp);
1116 ino_t ipref;
1117 int error;
1118
1119 *ipp = 0;
1120 if ((mode & IFMT) == IFDIR)
1121 ipref = dirpref(pdir->i_fs);
1122 else
1123 ipref = pdir->i_number;
1124 error = ialloc(pdir, ipref, mode, &tip);
1125 if (error) {
1126 iput(pdir);
1127 return (error);
1128 }
1129 ip = tip;
1130#ifdef QUOTA
1131 if (ip->i_dquot != NODQUOT)
1132 panic("maknode: dquot");
1133#endif
1134 ip->i_flag |= IACC|IUPD|ICHG;
1135 if ((mode & IFMT) == 0)
1136 mode |= IFREG;
1137 ip->i_mode = mode;
1138 ITOV(ip)->v_type = IFTOVT(mode); /* Rest init'd in iget() */
1139 ip->i_nlink = 1;
1140 ip->i_uid = ndp->ni_cred->cr_uid;
1141 ip->i_gid = pdir->i_gid;
1142 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) &&
1143 suser(ndp->ni_cred, NULL))
1144 ip->i_mode &= ~ISGID;
1145#ifdef QUOTA
1146 ip->i_dquot = inoquota(ip);
1147#endif
1148
1149 /*
1150 * Make sure inode goes to disk before directory entry.
1151 */
1152 if ((error = iupdat(ip, &time, &time, 1)) ||
1153 (error = direnter(ip, ndp))) {
1154 /*
1155 * Write error occurred trying to update the inode
1156 * or the directory so must deallocate the inode.
1157 */
1158 ip->i_nlink = 0;
1159 ip->i_flag |= ICHG;
1160 iput(ip);
1161 return (error);
1162 }
1163 *ipp = ip;
1164 return (0);
88a7a62a 1165}