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