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