must nullify the type to keep getnewbuf from looking for VBLK
[unix-history] / usr / src / sys / ufs / ffs / ufs_vnops.c
CommitLineData
da7c5cc6 1/*
7188ac27
KM
2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3 * All rights reserved.
da7c5cc6 4 *
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 *
9ed9b473 17 * @(#)ufs_vnops.c 7.17 (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
bb1b75f4
SL
374 ip->i_uid = uid;
375 ip->i_gid = gid;
3e78e260 376 ip->i_flag |= ICHG;
3088dd32 377 ip->i_mode &= ~(ISUID|ISGID);
528f664c 378#ifdef QUOTA
0a77f278 379 ip->i_dquot = inoquota(ip);
2e073567 380 (void) chkdq(ip, change, 1);
8011f5df 381 (void) chkiq(ip->i_dev, (struct inode *)NULL, (uid_t)uid, 1);
2e073567
SL
382 return (u.u_error); /* should == 0 ALWAYS !! */
383#else
bb1b75f4 384 return (0);
2e073567 385#endif
d67a03eb
BJ
386}
387
7188ac27
KM
388/* ARGSUSED */
389ufs_ioctl(vp, com, data, fflag, cred)
390 struct vnode *vp;
391 int com;
392 caddr_t data;
393 int fflag;
394 struct ucred *cred;
bb1b75f4 395{
bb1b75f4 396
7188ac27
KM
397 printf("ufs_ioctl called with type %d\n", vp->v_type);
398 return (ENOTTY);
399}
400
401/* ARGSUSED */
402ufs_select(vp, which, cred)
403 struct vnode *vp;
404 int which;
405 struct ucred *cred;
406{
407
408 printf("ufs_select called with type %d\n", vp->v_type);
409 return (1); /* XXX */
bb1b75f4 410}
d67a03eb 411
4f083fd7 412/*
7188ac27
KM
413 * Mmap a file
414 *
415 * NB Currently unsupported.
4f083fd7 416 */
7188ac27
KM
417/* ARGSUSED */
418ufs_mmap(vp, fflags, cred)
419 struct vnode *vp;
420 int fflags;
421 struct ucred *cred;
d67a03eb 422{
d67a03eb 423
7188ac27 424 return (EINVAL);
d67a03eb 425}
64d3a787 426
4f083fd7 427/*
7188ac27 428 * Synch an open file.
4f083fd7 429 */
7188ac27
KM
430/* ARGSUSED */
431ufs_fsync(vp, fflags, cred)
432 struct vnode *vp;
433 int fflags;
434 struct ucred *cred;
528f664c 435{
7188ac27
KM
436 register struct inode *ip = VTOI(vp);
437 int error;
438
439 ILOCK(ip);
440 if (fflags&FWRITE)
441 ip->i_flag |= ICHG;
442 error = syncip(ip);
443 IUNLOCK(ip);
444 return (error);
528f664c
SL
445}
446
4f083fd7 447/*
7188ac27
KM
448 * Seek on a file
449 *
450 * Nothing to do, so just return.
4f083fd7 451 */
7188ac27
KM
452/* ARGSUSED */
453ufs_seek(vp, oldoff, newoff, cred)
454 struct vnode *vp;
455 off_t oldoff, newoff;
456 struct ucred *cred;
528f664c 457{
7188ac27
KM
458
459 return (0);
460}
461
462/*
463 * ufs remove
464 * Hard to avoid races here, especially
465 * in unlinking directories.
466 */
467ufs_remove(ndp)
468 struct nameidata *ndp;
469{
470 register struct inode *ip, *dp;
471 int error;
472
473 ip = VTOI(ndp->ni_vp);
474 dp = VTOI(ndp->ni_dvp);
475 error = dirremove(ndp);
476 if (!error) {
477 ip->i_nlink--;
478 ip->i_flag |= ICHG;
528f664c 479 }
7188ac27
KM
480 if (dp == ip)
481 vrele(ITOV(ip));
482 else
483 iput(ip);
484 iput(dp);
485 return (error);
4f083fd7
SL
486}
487
488/*
7188ac27 489 * link vnode call
4f083fd7 490 */
7188ac27
KM
491ufs_link(vp, ndp)
492 register struct vnode *vp;
493 register struct nameidata *ndp;
4f083fd7 494{
7188ac27
KM
495 register struct inode *ip = VTOI(vp);
496 int error;
4f083fd7 497
7188ac27
KM
498 if (ndp->ni_dvp != vp)
499 ILOCK(ip);
500 if (ip->i_nlink == LINK_MAX - 1) {
501 error = EMLINK;
502 goto out;
503 }
504 ip->i_nlink++;
505 ip->i_flag |= ICHG;
506 error = iupdat(ip, &time, &time, 1);
507 if (!error)
508 error = direnter(ip, ndp);
509out:
510 if (ndp->ni_dvp != vp)
511 IUNLOCK(ip);
512 if (error) {
513 ip->i_nlink--;
82252d2b 514 ip->i_flag |= ICHG;
7188ac27
KM
515 }
516 return (error);
528f664c
SL
517}
518
4f083fd7
SL
519/*
520 * Rename system call.
521 * rename("foo", "bar");
522 * is essentially
523 * unlink("bar");
524 * link("foo", "bar");
525 * unlink("foo");
526 * but ``atomically''. Can't do full commit without saving state in the
527 * inode on disk which isn't feasible at this time. Best we can do is
528 * always guarantee the target exists.
529 *
530 * Basic algorithm is:
531 *
532 * 1) Bump link count on source while we're linking it to the
7188ac27 533 * target. This also ensure the inode won't be deleted out
68f21562
KM
534 * from underneath us while we work (it may be truncated by
535 * a concurrent `trunc' or `open' for creation).
4f083fd7
SL
536 * 2) Link source to destination. If destination already exists,
537 * delete it first.
68f21562
KM
538 * 3) Unlink source reference to inode if still around. If a
539 * directory was moved and the parent of the destination
4f083fd7
SL
540 * is different from the source, patch the ".." entry in the
541 * directory.
4f083fd7 542 */
7188ac27
KM
543ufs_rename(fndp, tndp)
544 register struct nameidata *fndp, *tndp;
528f664c 545{
4f083fd7 546 register struct inode *ip, *xp, *dp;
68f21562
KM
547 struct dirtemplate dirbuf;
548 int doingdirectory = 0, oldparent = 0, newparent = 0;
a5390dce 549 int error = 0;
4f083fd7 550
7188ac27
KM
551 dp = VTOI(fndp->ni_dvp);
552 ip = VTOI(fndp->ni_vp);
553 ILOCK(ip);
4f083fd7 554 if ((ip->i_mode&IFMT) == IFDIR) {
7188ac27 555 register struct direct *d = &fndp->ni_dent;
4f083fd7 556
4f083fd7 557 /*
046f18d1 558 * Avoid ".", "..", and aliases of "." for obvious reasons.
4f083fd7 559 */
7188ac27
KM
560 if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip ||
561 fndp->ni_isdotdot || (ip->i_flag & IRENAME)) {
562 IUNLOCK(ip);
563 ufs_abortop(fndp);
564 ufs_abortop(tndp);
565 return (EINVAL);
4f083fd7 566 }
68f21562 567 ip->i_flag |= IRENAME;
4f083fd7
SL
568 oldparent = dp->i_number;
569 doingdirectory++;
570 }
7188ac27 571 vrele(fndp->ni_dvp);
4f083fd7
SL
572
573 /*
574 * 1) Bump link count while we're moving stuff
575 * around. If we crash somewhere before
576 * completing our work, the link count
577 * may be wrong, but correctable.
578 */
579 ip->i_nlink++;
580 ip->i_flag |= ICHG;
7188ac27 581 error = iupdat(ip, &time, &time, 1);
a388503d 582 IUNLOCK(ip);
4f083fd7
SL
583
584 /*
585 * When the target exists, both the directory
7188ac27 586 * and target vnodes are returned locked.
4f083fd7 587 */
7188ac27
KM
588 dp = VTOI(tndp->ni_dvp);
589 xp = NULL;
590 if (tndp->ni_vp)
591 xp = VTOI(tndp->ni_vp);
046f18d1
SL
592 /*
593 * If ".." must be changed (ie the directory gets a new
81552f0f
KM
594 * parent) then the source directory must not be in the
595 * directory heirarchy above the target, as this would
596 * orphan everything below the source directory. Also
597 * the user must have write permission in the source so
598 * as to be able to change "..". We must repeat the call
599 * to namei, as the parent directory is unlocked by the
600 * call to checkpath().
046f18d1 601 */
68f21562
KM
602 if (oldparent != dp->i_number)
603 newparent = dp->i_number;
604 if (doingdirectory && newparent) {
7188ac27 605 if (error = iaccess(ip, IWRITE, tndp->ni_cred))
81552f0f 606 goto bad;
7188ac27 607 tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE;
81552f0f 608 do {
7188ac27 609 dp = VTOI(tndp->ni_dvp);
81552f0f 610 if (xp != NULL)
79cf21e2 611 iput(xp);
7188ac27 612 if (error = checkpath(ip, dp, tndp->ni_cred))
81552f0f 613 goto out;
7188ac27 614 if (error = namei(tndp))
81552f0f 615 goto out;
7188ac27
KM
616 xp = NULL;
617 if (tndp->ni_vp)
618 xp = VTOI(tndp->ni_vp);
619 } while (dp != VTOI(tndp->ni_dvp));
81552f0f 620 }
4f083fd7
SL
621 /*
622 * 2) If target doesn't exist, link the target
623 * to the source and unlink the source.
624 * Otherwise, rewrite the target directory
625 * entry to reference the source inode and
626 * expunge the original entry's existence.
627 */
4f083fd7 628 if (xp == NULL) {
7188ac27
KM
629 if (dp->i_dev != ip->i_dev)
630 panic("rename: EXDEV");
4f083fd7 631 /*
68f21562
KM
632 * Account for ".." in new directory.
633 * When source and destination have the same
634 * parent we don't fool with the link count.
4f083fd7 635 */
68f21562 636 if (doingdirectory && newparent) {
4f083fd7
SL
637 dp->i_nlink++;
638 dp->i_flag |= ICHG;
7188ac27 639 error = iupdat(dp, &time, &time, 1);
4f083fd7 640 }
7188ac27 641 if (error = direnter(ip, tndp))
4f083fd7
SL
642 goto out;
643 } else {
7188ac27
KM
644 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
645 panic("rename: EXDEV");
e69c3c9c
SL
646 /*
647 * Short circuit rename(foo, foo).
648 */
649 if (xp->i_number == ip->i_number)
7188ac27 650 panic("rename: same file");
80cee150
JB
651 /*
652 * If the parent directory is "sticky", then the user must
653 * own the parent directory, or the destination of the rename,
654 * otherwise the destination may not be changed (except by
655 * root). This implements append-only directories.
656 */
7188ac27
KM
657 if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 &&
658 tndp->ni_cred->cr_uid != dp->i_uid &&
659 xp->i_uid != tndp->ni_cred->cr_uid) {
80cee150
JB
660 error = EPERM;
661 goto bad;
662 }
4f083fd7 663 /*
a5390dce
SL
664 * Target must be empty if a directory
665 * and have no links to it.
4f083fd7
SL
666 * Also, insure source and target are
667 * compatible (both directories, or both
668 * not directories).
669 */
670 if ((xp->i_mode&IFMT) == IFDIR) {
7188ac27
KM
671 if (!dirempty(xp, dp->i_number, tndp->ni_cred) ||
672 xp->i_nlink > 2) {
a5390dce 673 error = ENOTEMPTY;
4f083fd7
SL
674 goto bad;
675 }
676 if (!doingdirectory) {
a5390dce 677 error = ENOTDIR;
4f083fd7
SL
678 goto bad;
679 }
7188ac27 680 cache_purge(ITOV(dp));
4f083fd7 681 } else if (doingdirectory) {
a5390dce 682 error = EISDIR;
4f083fd7
SL
683 goto bad;
684 }
7188ac27
KM
685 if (error = dirrewrite(dp, ip, tndp))
686 goto bad;
687 vput(ITOV(dp));
4f083fd7 688 /*
a5390dce
SL
689 * Adjust the link count of the target to
690 * reflect the dirrewrite above. If this is
691 * a directory it is empty and there are
692 * no links to it, so we can squash the inode and
693 * any space associated with it. We disallowed
694 * renaming over top of a directory with links to
68f21562
KM
695 * it above, as the remaining link would point to
696 * a directory without "." or ".." entries.
4f083fd7 697 */
a5390dce 698 xp->i_nlink--;
4f083fd7 699 if (doingdirectory) {
a5390dce
SL
700 if (--xp->i_nlink != 0)
701 panic("rename: linked directory");
7188ac27 702 error = itrunc(xp, (u_long)0);
a5390dce 703 }
4f083fd7 704 xp->i_flag |= ICHG;
88d931ba 705 iput(xp);
31db12cb 706 xp = NULL;
4f083fd7
SL
707 }
708
709 /*
710 * 3) Unlink the source.
711 */
7188ac27
KM
712 fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF;
713 (void)namei(fndp);
714 if (fndp->ni_vp != NULL) {
715 xp = VTOI(fndp->ni_vp);
716 dp = VTOI(fndp->ni_dvp);
717 } else {
79cf21e2
KM
718 if (fndp->ni_dvp != NULL)
719 vput(fndp->ni_dvp);
7188ac27 720 xp = NULL;
4f1a9037 721 dp = NULL;
7188ac27 722 }
4f083fd7 723 /*
7188ac27 724 * Ensure that the directory entry still exists and has not
68f21562
KM
725 * changed while the new name has been entered. If the source is
726 * a file then the entry may have been unlinked or renamed. In
727 * either case there is no further work to be done. If the source
728 * is a directory then it cannot have been rmdir'ed; its link
729 * count of three would cause a rmdir to fail with ENOTEMPTY.
7188ac27 730 * The IRENAME flag ensures that it cannot be moved by another
68f21562 731 * rename.
4f083fd7 732 */
4f1a9037 733 if (xp != ip) {
68f21562 734 if (doingdirectory)
4f1a9037 735 panic("rename: lost dir entry");
68f21562 736 } else {
4f083fd7 737 /*
68f21562
KM
738 * If the source is a directory with a
739 * new parent, the link count of the old
740 * parent directory must be decremented
741 * and ".." set to point to the new parent.
4f083fd7 742 */
68f21562 743 if (doingdirectory && newparent) {
4f083fd7
SL
744 dp->i_nlink--;
745 dp->i_flag |= ICHG;
68f21562 746 error = rdwri(UIO_READ, xp, (caddr_t)&dirbuf,
7188ac27 747 sizeof (struct dirtemplate), (off_t)0,
93e273b9 748 UIO_SYSSPACE, tndp->ni_cred, (int *)0);
68f21562
KM
749 if (error == 0) {
750 if (dirbuf.dotdot_namlen != 2 ||
751 dirbuf.dotdot_name[0] != '.' ||
752 dirbuf.dotdot_name[1] != '.') {
753 printf("rename: mangled dir\n");
754 } else {
755 dirbuf.dotdot_ino = newparent;
756 (void) rdwri(UIO_WRITE, xp,
757 (caddr_t)&dirbuf,
758 sizeof (struct dirtemplate),
93e273b9 759 (off_t)0, UIO_SYSSPACE,
7188ac27
KM
760 tndp->ni_cred, (int *)0);
761 cache_purge(ITOV(dp));
68f21562
KM
762 }
763 }
4f083fd7 764 }
7188ac27
KM
765 error = dirremove(fndp);
766 if (!error) {
68f21562
KM
767 xp->i_nlink--;
768 xp->i_flag |= ICHG;
4f083fd7 769 }
68f21562 770 xp->i_flag &= ~IRENAME;
4f083fd7 771 }
4f083fd7 772 if (dp)
7188ac27 773 vput(ITOV(dp));
68f21562 774 if (xp)
7188ac27
KM
775 vput(ITOV(xp));
776 vrele(ITOV(ip));
777 return (error);
a5390dce 778
4f083fd7 779bad:
4f083fd7 780 if (xp)
7188ac27
KM
781 vput(ITOV(xp));
782 vput(ITOV(dp));
4f083fd7
SL
783out:
784 ip->i_nlink--;
785 ip->i_flag |= ICHG;
7188ac27
KM
786 vrele(ITOV(ip));
787 return (error);
64d3a787 788}
88a7a62a
SL
789
790/*
791 * A virgin directory (no blushing please).
792 */
793struct dirtemplate mastertemplate = {
794 0, 12, 1, ".",
795 0, DIRBLKSIZ - 12, 2, ".."
796};
797
798/*
799 * Mkdir system call
800 */
7188ac27
KM
801ufs_mkdir(ndp, vap)
802 struct nameidata *ndp;
803 struct vattr *vap;
88a7a62a 804{
88a7a62a 805 register struct inode *ip, *dp;
7188ac27
KM
806 struct inode *tip;
807 struct vnode *dvp;
88a7a62a 808 struct dirtemplate dirtemplate;
7188ac27
KM
809 int error;
810 int dmode;
811
812 dvp = ndp->ni_dvp;
813 dp = VTOI(dvp);
814 dmode = vap->va_mode&0777;
815 dmode |= IFDIR;
88a7a62a
SL
816 /*
817 * Must simulate part of maknode here
818 * in order to acquire the inode, but
819 * not have it entered in the parent
820 * directory. The entry is made later
821 * after writing "." and ".." entries out.
822 */
7188ac27
KM
823 error = ialloc(dp, dirpref(dp->i_fs), dmode, &tip);
824 if (error) {
88a7a62a 825 iput(dp);
7188ac27 826 return (error);
88a7a62a 827 }
7188ac27 828 ip = tip;
88a7a62a
SL
829#ifdef QUOTA
830 if (ip->i_dquot != NODQUOT)
831 panic("mkdir: dquot");
832#endif
833 ip->i_flag |= IACC|IUPD|ICHG;
7188ac27
KM
834 ip->i_mode = dmode;
835 ITOV(ip)->v_type = VDIR; /* Rest init'd in iget() */
88a7a62a 836 ip->i_nlink = 2;
7188ac27 837 ip->i_uid = ndp->ni_cred->cr_uid;
88a7a62a
SL
838 ip->i_gid = dp->i_gid;
839#ifdef QUOTA
840 ip->i_dquot = inoquota(ip);
841#endif
7188ac27 842 error = iupdat(ip, &time, &time, 1);
88a7a62a
SL
843
844 /*
845 * Bump link count in parent directory
846 * to reflect work done below. Should
847 * be done before reference is created
848 * so reparation is possible if we crash.
849 */
850 dp->i_nlink++;
851 dp->i_flag |= ICHG;
7188ac27 852 error = iupdat(dp, &time, &time, 1);
88a7a62a
SL
853
854 /*
855 * Initialize directory with "."
856 * and ".." from static template.
857 */
858 dirtemplate = mastertemplate;
859 dirtemplate.dot_ino = ip->i_number;
860 dirtemplate.dotdot_ino = dp->i_number;
7188ac27
KM
861 error = rdwri(UIO_WRITE, ip, (caddr_t)&dirtemplate,
862 sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
863 ndp->ni_cred, (int *)0);
864 if (error) {
88a7a62a
SL
865 dp->i_nlink--;
866 dp->i_flag |= ICHG;
867 goto bad;
868 }
7188ac27
KM
869 if (DIRBLKSIZ > dp->i_fs->fs_fsize)
870 panic("mkdir: blksize"); /* XXX - should grow w/balloc() */
23de9f20
KM
871 else
872 ip->i_size = DIRBLKSIZ;
88a7a62a
SL
873 /*
874 * Directory all set up, now
875 * install the entry for it in
876 * the parent directory.
877 */
7188ac27 878 error = direnter(ip, ndp);
88a7a62a 879 dp = NULL;
7188ac27 880 if (error) {
715baff1 881 ndp->ni_nameiop = LOOKUP | NOCACHE;
7188ac27
KM
882 error = namei(ndp);
883 if (!error) {
884 dp = VTOI(ndp->ni_vp);
88a7a62a
SL
885 dp->i_nlink--;
886 dp->i_flag |= ICHG;
887 }
888 }
889bad:
890 /*
891 * No need to do an explicit itrunc here,
7188ac27 892 * vrele will do this for us because we set
88a7a62a
SL
893 * the link count to 0.
894 */
7188ac27 895 if (error) {
88a7a62a
SL
896 ip->i_nlink = 0;
897 ip->i_flag |= ICHG;
cbcdacd6
KM
898 iput(ip);
899 } else
900 ndp->ni_vp = ITOV(ip);
88a7a62a
SL
901 if (dp)
902 iput(dp);
7188ac27 903 return (error);
88a7a62a
SL
904}
905
906/*
907 * Rmdir system call.
908 */
7188ac27
KM
909ufs_rmdir(ndp)
910 register struct nameidata *ndp;
88a7a62a 911{
88a7a62a 912 register struct inode *ip, *dp;
7188ac27
KM
913 int error = 0;
914
915 ip = VTOI(ndp->ni_vp);
916 dp = VTOI(ndp->ni_dvp);
88a7a62a
SL
917 /*
918 * No rmdir "." please.
919 */
920 if (dp == ip) {
7188ac27 921 vrele(ITOV(dp));
88a7a62a 922 iput(ip);
7188ac27 923 return (EINVAL);
88a7a62a
SL
924 }
925 /*
926 * Verify the directory is empty (and valid).
927 * (Rmdir ".." won't be valid since
928 * ".." will contain a reference to
929 * the current directory and thus be
930 * non-empty.)
931 */
7188ac27
KM
932 if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) {
933 error = ENOTEMPTY;
88a7a62a
SL
934 goto out;
935 }
936 /*
937 * Delete reference to directory before purging
938 * inode. If we crash in between, the directory
939 * will be reattached to lost+found,
940 */
7188ac27 941 if (error = dirremove(ndp))
88a7a62a
SL
942 goto out;
943 dp->i_nlink--;
944 dp->i_flag |= ICHG;
7188ac27 945 cache_purge(ITOV(dp));
88a7a62a 946 iput(dp);
7188ac27 947 ndp->ni_dvp = NULL;
88a7a62a
SL
948 /*
949 * Truncate inode. The only stuff left
950 * in the directory is "." and "..". The
951 * "." reference is inconsequential since
952 * we're quashing it. The ".." reference
953 * has already been adjusted above. We've
954 * removed the "." reference and the reference
955 * in the parent directory, but there may be
956 * other hard links so decrement by 2 and
957 * worry about them later.
958 */
959 ip->i_nlink -= 2;
7188ac27
KM
960 error = itrunc(ip, (u_long)0);
961 cache_purge(ITOV(ip));
88a7a62a 962out:
7188ac27 963 if (ndp->ni_dvp)
88a7a62a
SL
964 iput(dp);
965 iput(ip);
7188ac27 966 return (error);
88a7a62a
SL
967}
968
7188ac27
KM
969/*
970 * symlink -- make a symbolic link
971 */
972ufs_symlink(ndp, vap, target)
973 struct nameidata *ndp;
974 struct vattr *vap;
975 char *target;
976{
977 struct inode *ip;
978 int error;
979
980 error = maknode(IFLNK | vap->va_mode, ndp, &ip);
981 if (error)
982 return (error);
983 error = rdwri(UIO_WRITE, ip, target, strlen(target), (off_t)0,
984 UIO_SYSSPACE, ndp->ni_cred, (int *)0);
985 iput(ip);
986 return (error);
987}
988
989/*
990 * Vnode op for read and write
991 */
992ufs_readdir(vp, uio, offp, cred)
993 struct vnode *vp;
994 register struct uio *uio;
995 off_t *offp;
996 struct ucred *cred;
88a7a62a 997{
7188ac27
KM
998 register struct inode *ip = VTOI(vp);
999 int count, error;
88a7a62a 1000
7188ac27
KM
1001 ILOCK(ip);
1002 uio->uio_offset = *offp;
1003 count = uio->uio_resid;
1004 count &= ~(DIRBLKSIZ - 1);
1005 if (vp->v_type != VDIR || uio->uio_iovcnt != 1 ||
1006 (count < DIRBLKSIZ) || (uio->uio_offset & (DIRBLKSIZ -1))) {
1007 IUNLOCK(ip);
1008 return (EINVAL);
1009 }
1010 uio->uio_resid = count;
1011 uio->uio_iov->iov_len = count;
1012 error = readip(ip, uio, cred);
1013 *offp += count - uio->uio_resid;
1014 IUNLOCK(ip);
1015 return (error);
1016}
1017
1018/*
1019 * Return target name of a symbolic link
1020 */
1021ufs_readlink(vp, uiop, cred)
1022 struct vnode *vp;
1023 struct uio *uiop;
1024 struct ucred *cred;
1025{
1026
1027 return (readip(VTOI(vp), uiop, cred));
1028}
1029
1030/*
1031 * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually
1032 * done. Iff ni_vp/ni_dvp not null and locked, unlock.
1033 */
1034ufs_abortop(ndp)
1035 register struct nameidata *ndp;
1036{
1037 register struct inode *ip;
1038
1039 if (ndp->ni_vp) {
1040 ip = VTOI(ndp->ni_vp);
1041 if (ip->i_flag & ILOCKED)
1042 IUNLOCK(ip);
1043 vrele(ndp->ni_vp);
8462a185 1044 }
7188ac27
KM
1045 if (ndp->ni_dvp) {
1046 ip = VTOI(ndp->ni_dvp);
1047 if (ip->i_flag & ILOCKED)
1048 IUNLOCK(ip);
1049 vrele(ndp->ni_dvp);
88a7a62a 1050 }
7188ac27
KM
1051 return;
1052}
1053
1054ufs_lock(vp)
1055 struct vnode *vp;
1056{
1057 register struct inode *ip = VTOI(vp);
1058
1059 ILOCK(ip);
1060 return (0);
1061}
1062
1063ufs_unlock(vp)
1064 struct vnode *vp;
1065{
1066 register struct inode *ip = VTOI(vp);
1067
1068 if (!(ip->i_flag & ILOCKED))
1069 panic("ufs_unlock NOT LOCKED");
1070 IUNLOCK(ip);
1071 return (0);
1072}
1073
1074/*
1075 * Get access to bmap
1076 */
1077ufs_bmap(vp, bn, vpp, bnp)
1078 struct vnode *vp;
1079 daddr_t bn;
1080 struct vnode **vpp;
1081 daddr_t *bnp;
1082{
1083 struct inode *ip = VTOI(vp);
1084
1085 if (vpp != NULL)
1086 *vpp = ip->i_devvp;
1087 if (bnp == NULL)
1088 return (0);
1089 return (bmap(ip, bn, bnp, (daddr_t *)0, (int *)0));
88a7a62a
SL
1090}
1091
1092/*
7188ac27 1093 * Just call the device strategy routine
88a7a62a 1094 */
7188ac27
KM
1095ufs_strategy(bp)
1096 register struct buf *bp;
88a7a62a 1097{
7188ac27
KM
1098 (*bdevsw[major(bp->b_dev)].d_strategy)(bp);
1099 return (0);
1100}
88a7a62a 1101
7188ac27
KM
1102/*
1103 * Make a new file.
1104 */
1105maknode(mode, ndp, ipp)
1106 int mode;
1107 register struct nameidata *ndp;
1108 struct inode **ipp;
1109{
1110 register struct inode *ip;
1111 struct inode *tip;
1112 register struct inode *pdir = VTOI(ndp->ni_dvp);
1113 ino_t ipref;
1114 int error;
1115
1116 *ipp = 0;
1117 if ((mode & IFMT) == IFDIR)
1118 ipref = dirpref(pdir->i_fs);
1119 else
1120 ipref = pdir->i_number;
1121 error = ialloc(pdir, ipref, mode, &tip);
1122 if (error) {
1123 iput(pdir);
1124 return (error);
1125 }
1126 ip = tip;
1127#ifdef QUOTA
1128 if (ip->i_dquot != NODQUOT)
1129 panic("maknode: dquot");
1130#endif
1131 ip->i_flag |= IACC|IUPD|ICHG;
1132 if ((mode & IFMT) == 0)
1133 mode |= IFREG;
1134 ip->i_mode = mode;
1135 ITOV(ip)->v_type = IFTOVT(mode); /* Rest init'd in iget() */
1136 ip->i_nlink = 1;
1137 ip->i_uid = ndp->ni_cred->cr_uid;
1138 ip->i_gid = pdir->i_gid;
1139 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) &&
1140 suser(ndp->ni_cred, NULL))
1141 ip->i_mode &= ~ISGID;
1142#ifdef QUOTA
1143 ip->i_dquot = inoquota(ip);
1144#endif
1145
1146 /*
1147 * Make sure inode goes to disk before directory entry.
1148 */
1149 if ((error = iupdat(ip, &time, &time, 1)) ||
1150 (error = direnter(ip, ndp))) {
1151 /*
1152 * Write error occurred trying to update the inode
1153 * or the directory so must deallocate the inode.
1154 */
1155 ip->i_nlink = 0;
1156 ip->i_flag |= ICHG;
1157 iput(ip);
1158 return (error);
1159 }
1160 *ipp = ip;
1161 return (0);
88a7a62a 1162}