kludge to force DC's on certain errors
[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 *
66955caf 17 * @(#)lfs_vnops.c 7.42 (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 "socket.h"
29#include "socketvar.h"
7188ac27 30#include "conf.h"
94368568 31#include "mount.h"
7188ac27 32#include "vnode.h"
0f93ba7b 33#include "specdev.h"
4b61628b 34#include "../ufs/quota.h"
7188ac27
KM
35#include "../ufs/inode.h"
36#include "../ufs/fs.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(),
e16fa59e 71 ufs_strategy(),
1c3ebc10
KM
72 ufs_print(),
73 ufs_islocked();
7188ac27
KM
74
75struct vnodeops ufs_vnodeops = {
e16fa59e
KM
76 ufs_lookup, /* lookup */
77 ufs_create, /* create */
78 ufs_mknod, /* mknod */
79 ufs_open, /* open */
80 ufs_close, /* close */
81 ufs_access, /* access */
82 ufs_getattr, /* getattr */
83 ufs_setattr, /* setattr */
84 ufs_read, /* read */
85 ufs_write, /* write */
86 ufs_ioctl, /* ioctl */
87 ufs_select, /* select */
88 ufs_mmap, /* mmap */
89 ufs_fsync, /* fsync */
90 ufs_seek, /* seek */
91 ufs_remove, /* remove */
92 ufs_link, /* link */
93 ufs_rename, /* rename */
94 ufs_mkdir, /* mkdir */
95 ufs_rmdir, /* rmdir */
96 ufs_symlink, /* symlink */
97 ufs_readdir, /* readdir */
98 ufs_readlink, /* readlink */
99 ufs_abortop, /* abortop */
100 ufs_inactive, /* inactive */
101 ufs_reclaim, /* reclaim */
102 ufs_lock, /* lock */
103 ufs_unlock, /* unlock */
104 ufs_bmap, /* bmap */
105 ufs_strategy, /* strategy */
106 ufs_print, /* print */
1c3ebc10 107 ufs_islocked, /* islocked */
7188ac27
KM
108};
109
f09fe6d3
KM
110int spec_lookup(),
111 spec_open(),
24a31b70
KM
112 ufsspec_read(),
113 ufsspec_write(),
f09fe6d3 114 spec_strategy(),
e16fa59e 115 spec_bmap(),
f09fe6d3
KM
116 spec_ioctl(),
117 spec_select(),
24a31b70 118 ufsspec_close(),
f09fe6d3
KM
119 spec_badop(),
120 spec_nullop();
121
122struct vnodeops spec_inodeops = {
86cdabf6
KM
123 spec_lookup, /* lookup */
124 spec_badop, /* create */
125 spec_badop, /* mknod */
126 spec_open, /* open */
24a31b70 127 ufsspec_close, /* close */
86cdabf6
KM
128 ufs_access, /* access */
129 ufs_getattr, /* getattr */
130 ufs_setattr, /* setattr */
24a31b70
KM
131 ufsspec_read, /* read */
132 ufsspec_write, /* write */
86cdabf6
KM
133 spec_ioctl, /* ioctl */
134 spec_select, /* select */
135 spec_badop, /* mmap */
136 spec_nullop, /* fsync */
137 spec_badop, /* seek */
138 spec_badop, /* remove */
139 spec_badop, /* link */
140 spec_badop, /* rename */
141 spec_badop, /* mkdir */
142 spec_badop, /* rmdir */
143 spec_badop, /* symlink */
144 spec_badop, /* readdir */
145 spec_badop, /* readlink */
146 spec_badop, /* abortop */
147 ufs_inactive, /* inactive */
148 ufs_reclaim, /* reclaim */
149 ufs_lock, /* lock */
150 ufs_unlock, /* unlock */
e16fa59e 151 spec_bmap, /* bmap */
86cdabf6 152 spec_strategy, /* strategy */
e16fa59e 153 ufs_print, /* print */
1c3ebc10 154 ufs_islocked, /* islocked */
f09fe6d3
KM
155};
156
d1c43d7f
KM
157#ifdef FIFO
158int fifo_lookup(),
159 fifo_open(),
160 ufsfifo_read(),
161 ufsfifo_write(),
162 fifo_bmap(),
163 fifo_ioctl(),
164 fifo_select(),
165 ufsfifo_close(),
166 fifo_print(),
167 fifo_badop(),
168 fifo_nullop();
169
170struct vnodeops fifo_inodeops = {
171 fifo_lookup, /* lookup */
172 fifo_badop, /* create */
173 fifo_badop, /* mknod */
174 fifo_open, /* open */
175 ufsfifo_close, /* close */
176 ufs_access, /* access */
177 ufs_getattr, /* getattr */
178 ufs_setattr, /* setattr */
179 ufsfifo_read, /* read */
180 ufsfifo_write, /* write */
181 fifo_ioctl, /* ioctl */
182 fifo_select, /* select */
183 fifo_badop, /* mmap */
184 fifo_nullop, /* fsync */
185 fifo_badop, /* seek */
186 fifo_badop, /* remove */
187 fifo_badop, /* link */
188 fifo_badop, /* rename */
189 fifo_badop, /* mkdir */
190 fifo_badop, /* rmdir */
191 fifo_badop, /* symlink */
192 fifo_badop, /* readdir */
193 fifo_badop, /* readlink */
194 fifo_badop, /* abortop */
195 ufs_inactive, /* inactive */
196 ufs_reclaim, /* reclaim */
197 ufs_lock, /* lock */
198 ufs_unlock, /* unlock */
199 fifo_bmap, /* bmap */
200 fifo_badop, /* strategy */
201 ufs_print, /* print */
202 ufs_islocked, /* islocked */
203};
204#endif /* FIFO */
205
206enum vtype iftovt_tab[16] = {
207 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
208 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
7188ac27 209};
d1c43d7f
KM
210int vttoif_tab[9] = {
211 0, IFREG, IFDIR, IFBLK, IFCHR, IFLNK, IFSOCK, IFIFO, IFMT,
7188ac27 212};
3e78e260 213
4f083fd7 214/*
7188ac27 215 * Create a regular file
4f083fd7 216 */
7188ac27
KM
217ufs_create(ndp, vap)
218 struct nameidata *ndp;
219 struct vattr *vap;
3e78e260 220{
7188ac27
KM
221 struct inode *ip;
222 int error;
3e78e260 223
7188ac27
KM
224 if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip))
225 return (error);
226 ndp->ni_vp = ITOV(ip);
227 return (0);
3e78e260
BJ
228}
229
4f083fd7 230/*
7188ac27 231 * Mknod vnode call
4f083fd7 232 */
7188ac27
KM
233/* ARGSUSED */
234ufs_mknod(ndp, vap, cred)
235 struct nameidata *ndp;
236 struct ucred *cred;
237 struct vattr *vap;
3e78e260 238{
f09fe6d3 239 register struct vnode *vp;
7188ac27
KM
240 struct inode *ip;
241 int error;
3e78e260 242
7188ac27
KM
243 if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip))
244 return (error);
d1c43d7f
KM
245 ip->i_flag |= IACC|IUPD|ICHG;
246 if (vap->va_rdev != VNOVAL) {
7188ac27
KM
247 /*
248 * Want to be able to use this to make badblock
249 * inodes, so don't truncate the dev number.
250 */
b373e060 251 ip->i_rdev = vap->va_rdev;
7188ac27 252 }
7188ac27
KM
253 /*
254 * Remove inode so that it will be reloaded by iget and
255 * checked to see if it is an alias of an existing entry
256 * in the inode cache.
257 */
d1c43d7f
KM
258 vp = ITOV(ip);
259 vput(vp);
f09fe6d3
KM
260 vp->v_type = VNON;
261 vgone(vp);
7188ac27 262 return (0);
3e78e260
BJ
263}
264
265/*
7188ac27
KM
266 * Open called.
267 *
268 * Nothing to do.
3e78e260 269 */
7188ac27
KM
270/* ARGSUSED */
271ufs_open(vp, mode, cred)
272 struct vnode *vp;
273 int mode;
274 struct ucred *cred;
3e78e260 275{
3e78e260 276
7188ac27 277 return (0);
3e78e260
BJ
278}
279
280/*
7188ac27
KM
281 * Close called
282 *
283 * Update the times on the inode.
3e78e260 284 */
7188ac27
KM
285/* ARGSUSED */
286ufs_close(vp, fflag, cred)
287 struct vnode *vp;
288 int fflag;
289 struct ucred *cred;
3e78e260 290{
7188ac27 291 register struct inode *ip = VTOI(vp);
3e78e260 292
de67eefc 293 if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
7188ac27
KM
294 ITIMES(ip, &time, &time);
295 return (0);
3e78e260
BJ
296}
297
4b61628b
KM
298/*
299 * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
300 * The mode is shifted to select the owner/group/other fields. The
301 * super user is granted all permissions.
302 */
7188ac27
KM
303ufs_access(vp, mode, cred)
304 struct vnode *vp;
4b61628b 305 register int mode;
7188ac27 306 struct ucred *cred;
3e78e260 307{
4b61628b
KM
308 register struct inode *ip = VTOI(vp);
309 register gid_t *gp;
310 int i, error;
3e78e260 311
4b61628b
KM
312#ifdef DIAGNOSTIC
313 if (!VOP_ISLOCKED(vp)) {
314 vprint("ufs_access: not locked", vp);
315 panic("ufs_access: not locked");
316 }
317#endif
318#ifdef QUOTA
319 if (mode & VWRITE) {
320 switch (vp->v_type) {
321 case VREG: case VDIR: case VLNK:
322 if (error = getinoquota(ip))
323 return (error);
324 }
325 }
326#endif /* QUOTA */
327 /*
328 * If you're the super-user, you always get access.
329 */
330 if (cred->cr_uid == 0)
331 return (0);
332 /*
333 * Access check is based on only one of owner, group, public.
334 * If not owner, then check group. If not a member of the
335 * group, then check public access.
336 */
337 if (cred->cr_uid != ip->i_uid) {
338 mode >>= 3;
339 gp = cred->cr_groups;
340 for (i = 0; i < cred->cr_ngroups; i++, gp++)
341 if (ip->i_gid == *gp)
342 goto found;
343 mode >>= 3;
344found:
345 ;
346 }
347 if ((ip->i_mode & mode) != 0)
348 return (0);
349 return (EACCES);
3e78e260
BJ
350}
351
7188ac27
KM
352/* ARGSUSED */
353ufs_getattr(vp, vap, cred)
354 struct vnode *vp;
355 register struct vattr *vap;
356 struct ucred *cred;
3e78e260 357{
7188ac27 358 register struct inode *ip = VTOI(vp);
3e78e260 359
7188ac27 360 ITIMES(ip, &time, &time);
3e78e260 361 /*
7188ac27 362 * Copy from inode table
3e78e260 363 */
7188ac27
KM
364 vap->va_fsid = ip->i_dev;
365 vap->va_fileid = ip->i_number;
366 vap->va_mode = ip->i_mode & ~IFMT;
367 vap->va_nlink = ip->i_nlink;
368 vap->va_uid = ip->i_uid;
369 vap->va_gid = ip->i_gid;
370 vap->va_rdev = (dev_t)ip->i_rdev;
4b61628b
KM
371#ifdef tahoe
372 vap->va_size = ip->i_size;
373 vap->va_size_rsv = 0;
374#else
8fae943a 375 vap->va_qsize = ip->i_din.di_qsize;
4b61628b 376#endif
7188ac27 377 vap->va_atime.tv_sec = ip->i_atime;
6ef53d70 378 vap->va_atime.tv_usec = 0;
7188ac27 379 vap->va_mtime.tv_sec = ip->i_mtime;
6ef53d70 380 vap->va_mtime.tv_usec = 0;
7188ac27 381 vap->va_ctime.tv_sec = ip->i_ctime;
6ef53d70 382 vap->va_ctime.tv_usec = 0;
d07fc92a
KM
383 vap->va_flags = ip->i_flags;
384 vap->va_gen = ip->i_gen;
7188ac27
KM
385 /* this doesn't belong here */
386 if (vp->v_type == VBLK)
387 vap->va_blocksize = BLKDEV_IOSIZE;
388 else if (vp->v_type == VCHR)
389 vap->va_blocksize = MAXBSIZE;
8eee8525 390 else
7188ac27 391 vap->va_blocksize = ip->i_fs->fs_bsize;
a61a68d6 392 vap->va_bytes = dbtob(ip->i_blocks);
8fae943a 393 vap->va_bytes_rsv = 0;
7188ac27
KM
394 vap->va_type = vp->v_type;
395 return (0);
3e78e260
BJ
396}
397
398/*
7188ac27 399 * Set attribute vnode op. called from several syscalls
3e78e260 400 */
7188ac27
KM
401ufs_setattr(vp, vap, cred)
402 register struct vnode *vp;
403 register struct vattr *vap;
404 register struct ucred *cred;
3e78e260 405{
7188ac27
KM
406 register struct inode *ip = VTOI(vp);
407 int error = 0;
b4d1aee9 408
7188ac27
KM
409 /*
410 * Check for unsetable attributes.
411 */
412 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
413 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
414 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
d07fc92a 415 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
7188ac27 416 return (EINVAL);
b4d1aee9 417 }
7188ac27
KM
418 /*
419 * Go through the fields and update iff not VNOVAL.
420 */
421 if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL)
422 if (error = chown1(vp, vap->va_uid, vap->va_gid, cred))
423 return (error);
424 if (vap->va_size != VNOVAL) {
425 if (vp->v_type == VDIR)
426 return (EISDIR);
e16fa59e 427 if (error = itrunc(ip, vap->va_size, 0)) /* XXX IO_SYNC? */
7188ac27 428 return (error);
3e78e260 429 }
7188ac27 430 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
de412887
KM
431 if (cred->cr_uid != ip->i_uid &&
432 (error = suser(cred, &u.u_acflag)))
433 return (error);
7188ac27
KM
434 if (vap->va_atime.tv_sec != VNOVAL)
435 ip->i_flag |= IACC;
436 if (vap->va_mtime.tv_sec != VNOVAL)
437 ip->i_flag |= IUPD;
438 ip->i_flag |= ICHG;
439 if (error = iupdat(ip, &vap->va_atime, &vap->va_mtime, 1))
440 return (error);
5485e062 441 }
7188ac27
KM
442 if (vap->va_mode != (u_short)VNOVAL)
443 error = chmod1(vp, (int)vap->va_mode, cred);
d07fc92a
KM
444 if (vap->va_flags != VNOVAL) {
445 if (cred->cr_uid != ip->i_uid &&
446 (error = suser(cred, &u.u_acflag)))
447 return (error);
448 if (cred->cr_uid == 0) {
449 ip->i_flags = vap->va_flags;
450 } else {
451 ip->i_flags &= 0xffff0000;
452 ip->i_flags |= (vap->va_flags & 0xffff);
453 }
454 ip->i_flag |= ICHG;
455 }
7188ac27 456 return (error);
528f664c
SL
457}
458
4f083fd7
SL
459/*
460 * Change the mode on a file.
461 * Inode must be locked before calling.
462 */
7188ac27
KM
463chmod1(vp, mode, cred)
464 register struct vnode *vp;
528f664c 465 register int mode;
7188ac27 466 struct ucred *cred;
528f664c 467{
7188ac27 468 register struct inode *ip = VTOI(vp);
de412887 469 int error;
197da11b 470
de412887
KM
471 if (cred->cr_uid != ip->i_uid &&
472 (error = suser(cred, &u.u_acflag)))
473 return (error);
3e78e260 474 ip->i_mode &= ~07777;
7188ac27
KM
475 if (cred->cr_uid) {
476 if (vp->v_type != VDIR)
47af7174 477 mode &= ~ISVTX;
7188ac27 478 if (!groupmember(ip->i_gid, cred))
bb1b75f4 479 mode &= ~ISGID;
f94ceb3b 480 }
7188ac27 481 ip->i_mode |= mode & 07777;
3e78e260 482 ip->i_flag |= ICHG;
7188ac27
KM
483 if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0)
484 xrele(vp);
47af7174 485 return (0);
5485e062
BJ
486}
487
528f664c
SL
488/*
489 * Perform chown operation on inode ip;
490 * inode must be locked prior to call.
491 */
7188ac27
KM
492chown1(vp, uid, gid, cred)
493 register struct vnode *vp;
494 uid_t uid;
495 gid_t gid;
496 struct ucred *cred;
528f664c 497{
7188ac27 498 register struct inode *ip = VTOI(vp);
4b61628b
KM
499 uid_t ouid;
500 gid_t ogid;
501 int error = 0;
528f664c 502#ifdef QUOTA
4b61628b
KM
503 register int i;
504 long change;
bb1b75f4 505#endif
528f664c 506
7188ac27 507 if (uid == (u_short)VNOVAL)
bb1b75f4 508 uid = ip->i_uid;
7188ac27 509 if (gid == (u_short)VNOVAL)
bb1b75f4 510 gid = ip->i_gid;
18b0bce6
KB
511 /*
512 * If we don't own the file, are trying to change the owner
513 * of the file, or are not a member of the target group,
514 * the caller must be superuser or the call fails.
515 */
7188ac27
KM
516 if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
517 !groupmember((gid_t)gid, cred)) &&
518 (error = suser(cred, &u.u_acflag)))
519 return (error);
4b61628b
KM
520 ouid = ip->i_uid;
521 ogid = ip->i_gid;
bb1b75f4 522#ifdef QUOTA
4b61628b
KM
523 if (error = getinoquota(ip))
524 return (error);
525 if (ouid == uid) {
526 dqrele(vp, ip->i_dquot[USRQUOTA]);
527 ip->i_dquot[USRQUOTA] = NODQUOT;
528 }
529 if (ogid == gid) {
530 dqrele(vp, ip->i_dquot[GRPQUOTA]);
531 ip->i_dquot[GRPQUOTA] = NODQUOT;
532 }
533 change = ip->i_blocks;
534 (void) chkdq(ip, -change, cred, CHOWN);
535 (void) chkiq(ip, -1, cred, CHOWN);
536 for (i = 0; i < MAXQUOTAS; i++) {
537 dqrele(vp, ip->i_dquot[i]);
538 ip->i_dquot[i] = NODQUOT;
539 }
f94ceb3b 540#endif
bb1b75f4
SL
541 ip->i_uid = uid;
542 ip->i_gid = gid;
528f664c 543#ifdef QUOTA
4b61628b
KM
544 if ((error = getinoquota(ip)) == 0) {
545 if (ouid == uid) {
546 dqrele(vp, ip->i_dquot[USRQUOTA]);
547 ip->i_dquot[USRQUOTA] = NODQUOT;
548 }
549 if (ogid == gid) {
550 dqrele(vp, ip->i_dquot[GRPQUOTA]);
551 ip->i_dquot[GRPQUOTA] = NODQUOT;
552 }
553 if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
554 if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
722a9b31 555 goto good;
4b61628b
KM
556 else
557 (void) chkdq(ip, -change, cred, CHOWN|FORCE);
558 }
559 for (i = 0; i < MAXQUOTAS; i++) {
560 dqrele(vp, ip->i_dquot[i]);
561 ip->i_dquot[i] = NODQUOT;
562 }
563 }
564 ip->i_uid = ouid;
565 ip->i_gid = ogid;
566 if (getinoquota(ip) == 0) {
567 if (ouid == uid) {
568 dqrele(vp, ip->i_dquot[USRQUOTA]);
569 ip->i_dquot[USRQUOTA] = NODQUOT;
570 }
571 if (ogid == gid) {
572 dqrele(vp, ip->i_dquot[GRPQUOTA]);
573 ip->i_dquot[GRPQUOTA] = NODQUOT;
574 }
722a9b31
KM
575 (void) chkdq(ip, change, cred, FORCE|CHOWN);
576 (void) chkiq(ip, 1, cred, FORCE|CHOWN);
6cb69bbe 577 (void) getinoquota(ip);
4b61628b 578 }
6cb69bbe 579 return (error);
722a9b31 580good:
6cb69bbe
KM
581 if (getinoquota(ip))
582 panic("chown: lost quota");
583#endif /* QUOTA */
4b61628b
KM
584 if (ouid != uid || ogid != gid)
585 ip->i_flag |= ICHG;
586 if (ouid != uid && cred->cr_uid != 0)
587 ip->i_mode &= ~ISUID;
588 if (ogid != gid && cred->cr_uid != 0)
589 ip->i_mode &= ~ISGID;
590 return (0);
d67a03eb
BJ
591}
592
b373e060
KM
593/*
594 * Vnode op for reading.
595 */
596/* ARGSUSED */
597ufs_read(vp, uio, ioflag, cred)
598 struct vnode *vp;
599 register struct uio *uio;
600 int ioflag;
601 struct ucred *cred;
602{
603 register struct inode *ip = VTOI(vp);
604 register struct fs *fs;
605 struct buf *bp;
606 daddr_t lbn, bn, rablock;
20aa076b 607 int size, diff, error = 0;
b373e060
KM
608 long n, on, type;
609
610 if (uio->uio_rw != UIO_READ)
611 panic("ufs_read mode");
612 type = ip->i_mode & IFMT;
613 if (type != IFDIR && type != IFREG && type != IFLNK)
614 panic("ufs_read type");
615 if (uio->uio_resid == 0)
616 return (0);
617 if (uio->uio_offset < 0)
618 return (EINVAL);
619 ip->i_flag |= IACC;
620 fs = ip->i_fs;
621 do {
622 lbn = lblkno(fs, uio->uio_offset);
623 on = blkoff(fs, uio->uio_offset);
624 n = MIN((unsigned)(fs->fs_bsize - on), uio->uio_resid);
625 diff = ip->i_size - uio->uio_offset;
626 if (diff <= 0)
627 return (0);
628 if (diff < n)
629 n = diff;
b373e060 630 size = blksize(fs, ip, lbn);
e16fa59e 631 rablock = lbn + 1;
20aa076b
KM
632 if (vp->v_lastr + 1 == lbn &&
633 lblktosize(fs, rablock) < ip->i_size)
634 error = breada(ITOV(ip), lbn, size, rablock,
635 blksize(fs, ip, rablock), NOCRED, &bp);
b373e060 636 else
e16fa59e 637 error = bread(ITOV(ip), lbn, size, NOCRED, &bp);
de67eefc 638 vp->v_lastr = lbn;
b373e060
KM
639 n = MIN(n, size - bp->b_resid);
640 if (error) {
641 brelse(bp);
642 return (error);
643 }
644 error = uiomove(bp->b_un.b_addr + on, (int)n, uio);
645 if (n + on == fs->fs_bsize || uio->uio_offset == ip->i_size)
646 bp->b_flags |= B_AGE;
647 brelse(bp);
648 } while (error == 0 && uio->uio_resid > 0 && n != 0);
649 return (error);
650}
651
652/*
653 * Vnode op for writing.
654 */
655ufs_write(vp, uio, ioflag, cred)
656 register struct vnode *vp;
657 struct uio *uio;
658 int ioflag;
659 struct ucred *cred;
660{
661 register struct inode *ip = VTOI(vp);
662 register struct fs *fs;
663 struct buf *bp;
664 daddr_t lbn, bn;
665 u_long osize;
666 int i, n, on, flags;
667 int count, size, resid, error = 0;
668
669 if (uio->uio_rw != UIO_WRITE)
670 panic("ufs_write mode");
671 switch (vp->v_type) {
672 case VREG:
673 if (ioflag & IO_APPEND)
674 uio->uio_offset = ip->i_size;
675 /* fall through */
676 case VLNK:
677 break;
678
679 case VDIR:
680 if ((ioflag & IO_SYNC) == 0)
681 panic("ufs_write nonsync dir write");
682 break;
683
684 default:
685 panic("ufs_write type");
686 }
687 if (uio->uio_offset < 0)
688 return (EINVAL);
689 if (uio->uio_resid == 0)
690 return (0);
691 /*
692 * Maybe this should be above the vnode op call, but so long as
693 * file servers have no limits, i don't think it matters
694 */
695 if (vp->v_type == VREG &&
696 uio->uio_offset + uio->uio_resid >
697 u.u_rlimit[RLIMIT_FSIZE].rlim_cur) {
698 psignal(u.u_procp, SIGXFSZ);
699 return (EFBIG);
700 }
701 resid = uio->uio_resid;
702 osize = ip->i_size;
703 fs = ip->i_fs;
e16fa59e
KM
704 flags = 0;
705 if (ioflag & IO_SYNC)
706 flags = B_SYNC;
b373e060
KM
707 do {
708 lbn = lblkno(fs, uio->uio_offset);
709 on = blkoff(fs, uio->uio_offset);
710 n = MIN((unsigned)(fs->fs_bsize - on), uio->uio_resid);
711 if (n < fs->fs_bsize)
e16fa59e 712 flags |= B_CLRBUF;
b373e060 713 else
e16fa59e
KM
714 flags &= ~B_CLRBUF;
715 if (error = balloc(ip, lbn, (int)(on + n), &bp, flags))
b373e060 716 break;
e16fa59e 717 bn = bp->b_blkno;
b373e060
KM
718 if (uio->uio_offset + n > ip->i_size)
719 ip->i_size = uio->uio_offset + n;
720 size = blksize(fs, ip, lbn);
721 count = howmany(size, CLBYTES);
722 for (i = 0; i < count; i++)
723 munhash(ip->i_devvp, bn + i * CLBYTES / DEV_BSIZE);
b373e060 724 n = MIN(n, size - bp->b_resid);
b373e060
KM
725 error = uiomove(bp->b_un.b_addr + on, n, uio);
726 if (ioflag & IO_SYNC)
727 (void) bwrite(bp);
728 else if (n + on == fs->fs_bsize) {
729 bp->b_flags |= B_AGE;
730 bawrite(bp);
731 } else
732 bdwrite(bp);
733 ip->i_flag |= IUPD|ICHG;
734 if (cred->cr_uid != 0)
735 ip->i_mode &= ~(ISUID|ISGID);
736 } while (error == 0 && uio->uio_resid > 0 && n != 0);
737 if (error && (ioflag & IO_UNIT)) {
e16fa59e 738 (void) itrunc(ip, osize, ioflag & IO_SYNC);
b373e060
KM
739 uio->uio_offset -= resid - uio->uio_resid;
740 uio->uio_resid = resid;
741 }
742 return (error);
743}
744
7188ac27
KM
745/* ARGSUSED */
746ufs_ioctl(vp, com, data, fflag, cred)
747 struct vnode *vp;
748 int com;
749 caddr_t data;
750 int fflag;
751 struct ucred *cred;
bb1b75f4 752{
bb1b75f4 753
7188ac27
KM
754 return (ENOTTY);
755}
756
757/* ARGSUSED */
d1c43d7f 758ufs_select(vp, which, fflags, cred)
7188ac27 759 struct vnode *vp;
d1c43d7f 760 int which, fflags;
7188ac27
KM
761 struct ucred *cred;
762{
763
7188ac27 764 return (1); /* XXX */
bb1b75f4 765}
d67a03eb 766
4f083fd7 767/*
7188ac27
KM
768 * Mmap a file
769 *
770 * NB Currently unsupported.
4f083fd7 771 */
7188ac27
KM
772/* ARGSUSED */
773ufs_mmap(vp, fflags, cred)
774 struct vnode *vp;
775 int fflags;
776 struct ucred *cred;
d67a03eb 777{
d67a03eb 778
7188ac27 779 return (EINVAL);
d67a03eb 780}
64d3a787 781
4f083fd7 782/*
7188ac27 783 * Synch an open file.
4f083fd7 784 */
7188ac27 785/* ARGSUSED */
86cdabf6 786ufs_fsync(vp, fflags, cred, waitfor)
7188ac27
KM
787 struct vnode *vp;
788 int fflags;
789 struct ucred *cred;
86cdabf6 790 int waitfor;
528f664c 791{
86cdabf6 792 struct inode *ip = VTOI(vp);
7188ac27 793
7188ac27
KM
794 if (fflags&FWRITE)
795 ip->i_flag |= ICHG;
e16fa59e
KM
796 vflushbuf(vp, waitfor == MNT_WAIT ? B_SYNC : 0);
797 return (iupdat(ip, &time, &time, waitfor == MNT_WAIT));
528f664c
SL
798}
799
4f083fd7 800/*
7188ac27
KM
801 * Seek on a file
802 *
803 * Nothing to do, so just return.
4f083fd7 804 */
7188ac27
KM
805/* ARGSUSED */
806ufs_seek(vp, oldoff, newoff, cred)
807 struct vnode *vp;
808 off_t oldoff, newoff;
809 struct ucred *cred;
528f664c 810{
7188ac27
KM
811
812 return (0);
813}
814
815/*
816 * ufs remove
817 * Hard to avoid races here, especially
818 * in unlinking directories.
819 */
820ufs_remove(ndp)
821 struct nameidata *ndp;
822{
823 register struct inode *ip, *dp;
824 int error;
825
826 ip = VTOI(ndp->ni_vp);
827 dp = VTOI(ndp->ni_dvp);
828 error = dirremove(ndp);
829 if (!error) {
830 ip->i_nlink--;
831 ip->i_flag |= ICHG;
528f664c 832 }
7188ac27
KM
833 if (dp == ip)
834 vrele(ITOV(ip));
835 else
836 iput(ip);
837 iput(dp);
838 return (error);
4f083fd7
SL
839}
840
841/*
7188ac27 842 * link vnode call
4f083fd7 843 */
7188ac27
KM
844ufs_link(vp, ndp)
845 register struct vnode *vp;
846 register struct nameidata *ndp;
4f083fd7 847{
7188ac27
KM
848 register struct inode *ip = VTOI(vp);
849 int error;
4f083fd7 850
7188ac27
KM
851 if (ndp->ni_dvp != vp)
852 ILOCK(ip);
853 if (ip->i_nlink == LINK_MAX - 1) {
854 error = EMLINK;
855 goto out;
856 }
857 ip->i_nlink++;
858 ip->i_flag |= ICHG;
859 error = iupdat(ip, &time, &time, 1);
860 if (!error)
861 error = direnter(ip, ndp);
862out:
863 if (ndp->ni_dvp != vp)
864 IUNLOCK(ip);
865 if (error) {
866 ip->i_nlink--;
82252d2b 867 ip->i_flag |= ICHG;
7188ac27
KM
868 }
869 return (error);
528f664c
SL
870}
871
4f083fd7
SL
872/*
873 * Rename system call.
874 * rename("foo", "bar");
875 * is essentially
876 * unlink("bar");
877 * link("foo", "bar");
878 * unlink("foo");
879 * but ``atomically''. Can't do full commit without saving state in the
880 * inode on disk which isn't feasible at this time. Best we can do is
881 * always guarantee the target exists.
882 *
883 * Basic algorithm is:
884 *
885 * 1) Bump link count on source while we're linking it to the
7188ac27 886 * target. This also ensure the inode won't be deleted out
68f21562
KM
887 * from underneath us while we work (it may be truncated by
888 * a concurrent `trunc' or `open' for creation).
4f083fd7
SL
889 * 2) Link source to destination. If destination already exists,
890 * delete it first.
68f21562
KM
891 * 3) Unlink source reference to inode if still around. If a
892 * directory was moved and the parent of the destination
4f083fd7
SL
893 * is different from the source, patch the ".." entry in the
894 * directory.
4f083fd7 895 */
7188ac27
KM
896ufs_rename(fndp, tndp)
897 register struct nameidata *fndp, *tndp;
528f664c 898{
4f083fd7 899 register struct inode *ip, *xp, *dp;
68f21562
KM
900 struct dirtemplate dirbuf;
901 int doingdirectory = 0, oldparent = 0, newparent = 0;
a5390dce 902 int error = 0;
4f083fd7 903
7188ac27
KM
904 dp = VTOI(fndp->ni_dvp);
905 ip = VTOI(fndp->ni_vp);
906 ILOCK(ip);
4f083fd7 907 if ((ip->i_mode&IFMT) == IFDIR) {
7188ac27 908 register struct direct *d = &fndp->ni_dent;
4f083fd7 909
4f083fd7 910 /*
046f18d1 911 * Avoid ".", "..", and aliases of "." for obvious reasons.
4f083fd7 912 */
7188ac27
KM
913 if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip ||
914 fndp->ni_isdotdot || (ip->i_flag & IRENAME)) {
66955caf
KM
915 VOP_ABORTOP(tndp);
916 vput(tndp->ni_dvp);
917 if (tndp->ni_vp)
918 vput(tndp->ni_vp);
919 VOP_ABORTOP(fndp);
920 vrele(fndp->ni_dvp);
921 vput(fndp->ni_vp);
7188ac27 922 return (EINVAL);
4f083fd7 923 }
68f21562 924 ip->i_flag |= IRENAME;
4f083fd7
SL
925 oldparent = dp->i_number;
926 doingdirectory++;
927 }
7188ac27 928 vrele(fndp->ni_dvp);
4f083fd7
SL
929
930 /*
931 * 1) Bump link count while we're moving stuff
932 * around. If we crash somewhere before
933 * completing our work, the link count
934 * may be wrong, but correctable.
935 */
936 ip->i_nlink++;
937 ip->i_flag |= ICHG;
7188ac27 938 error = iupdat(ip, &time, &time, 1);
a388503d 939 IUNLOCK(ip);
4f083fd7
SL
940
941 /*
942 * When the target exists, both the directory
7188ac27 943 * and target vnodes are returned locked.
4f083fd7 944 */
7188ac27
KM
945 dp = VTOI(tndp->ni_dvp);
946 xp = NULL;
947 if (tndp->ni_vp)
948 xp = VTOI(tndp->ni_vp);
046f18d1
SL
949 /*
950 * If ".." must be changed (ie the directory gets a new
81552f0f
KM
951 * parent) then the source directory must not be in the
952 * directory heirarchy above the target, as this would
953 * orphan everything below the source directory. Also
954 * the user must have write permission in the source so
955 * as to be able to change "..". We must repeat the call
956 * to namei, as the parent directory is unlocked by the
957 * call to checkpath().
046f18d1 958 */
68f21562
KM
959 if (oldparent != dp->i_number)
960 newparent = dp->i_number;
961 if (doingdirectory && newparent) {
48c3b6e8
KM
962 VOP_LOCK(fndp->ni_vp);
963 error = ufs_access(fndp->ni_vp, VWRITE, tndp->ni_cred);
964 VOP_UNLOCK(fndp->ni_vp);
965 if (error)
81552f0f 966 goto bad;
7188ac27 967 tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE;
81552f0f 968 do {
7188ac27 969 dp = VTOI(tndp->ni_dvp);
81552f0f 970 if (xp != NULL)
79cf21e2 971 iput(xp);
7188ac27 972 if (error = checkpath(ip, dp, tndp->ni_cred))
81552f0f 973 goto out;
7188ac27 974 if (error = namei(tndp))
81552f0f 975 goto out;
7188ac27
KM
976 xp = NULL;
977 if (tndp->ni_vp)
978 xp = VTOI(tndp->ni_vp);
979 } while (dp != VTOI(tndp->ni_dvp));
81552f0f 980 }
4f083fd7
SL
981 /*
982 * 2) If target doesn't exist, link the target
983 * to the source and unlink the source.
984 * Otherwise, rewrite the target directory
985 * entry to reference the source inode and
986 * expunge the original entry's existence.
987 */
4f083fd7 988 if (xp == NULL) {
7188ac27
KM
989 if (dp->i_dev != ip->i_dev)
990 panic("rename: EXDEV");
4f083fd7 991 /*
68f21562
KM
992 * Account for ".." in new directory.
993 * When source and destination have the same
994 * parent we don't fool with the link count.
4f083fd7 995 */
68f21562 996 if (doingdirectory && newparent) {
4f083fd7
SL
997 dp->i_nlink++;
998 dp->i_flag |= ICHG;
7188ac27 999 error = iupdat(dp, &time, &time, 1);
4f083fd7 1000 }
7188ac27 1001 if (error = direnter(ip, tndp))
4f083fd7
SL
1002 goto out;
1003 } else {
7188ac27
KM
1004 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1005 panic("rename: EXDEV");
e69c3c9c
SL
1006 /*
1007 * Short circuit rename(foo, foo).
1008 */
1009 if (xp->i_number == ip->i_number)
7188ac27 1010 panic("rename: same file");
80cee150
JB
1011 /*
1012 * If the parent directory is "sticky", then the user must
1013 * own the parent directory, or the destination of the rename,
1014 * otherwise the destination may not be changed (except by
1015 * root). This implements append-only directories.
1016 */
7188ac27
KM
1017 if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 &&
1018 tndp->ni_cred->cr_uid != dp->i_uid &&
1019 xp->i_uid != tndp->ni_cred->cr_uid) {
80cee150
JB
1020 error = EPERM;
1021 goto bad;
1022 }
4f083fd7 1023 /*
a5390dce
SL
1024 * Target must be empty if a directory
1025 * and have no links to it.
4f083fd7
SL
1026 * Also, insure source and target are
1027 * compatible (both directories, or both
1028 * not directories).
1029 */
1030 if ((xp->i_mode&IFMT) == IFDIR) {
7188ac27
KM
1031 if (!dirempty(xp, dp->i_number, tndp->ni_cred) ||
1032 xp->i_nlink > 2) {
a5390dce 1033 error = ENOTEMPTY;
4f083fd7
SL
1034 goto bad;
1035 }
1036 if (!doingdirectory) {
a5390dce 1037 error = ENOTDIR;
4f083fd7
SL
1038 goto bad;
1039 }
7188ac27 1040 cache_purge(ITOV(dp));
4f083fd7 1041 } else if (doingdirectory) {
a5390dce 1042 error = EISDIR;
4f083fd7
SL
1043 goto bad;
1044 }
7188ac27
KM
1045 if (error = dirrewrite(dp, ip, tndp))
1046 goto bad;
1047 vput(ITOV(dp));
4f083fd7 1048 /*
a5390dce
SL
1049 * Adjust the link count of the target to
1050 * reflect the dirrewrite above. If this is
1051 * a directory it is empty and there are
1052 * no links to it, so we can squash the inode and
1053 * any space associated with it. We disallowed
1054 * renaming over top of a directory with links to
68f21562
KM
1055 * it above, as the remaining link would point to
1056 * a directory without "." or ".." entries.
4f083fd7 1057 */
a5390dce 1058 xp->i_nlink--;
4f083fd7 1059 if (doingdirectory) {
a5390dce
SL
1060 if (--xp->i_nlink != 0)
1061 panic("rename: linked directory");
e16fa59e 1062 error = itrunc(xp, (u_long)0, IO_SYNC);
a5390dce 1063 }
4f083fd7 1064 xp->i_flag |= ICHG;
88d931ba 1065 iput(xp);
31db12cb 1066 xp = NULL;
4f083fd7
SL
1067 }
1068
1069 /*
1070 * 3) Unlink the source.
1071 */
7188ac27
KM
1072 fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF;
1073 (void)namei(fndp);
1074 if (fndp->ni_vp != NULL) {
1075 xp = VTOI(fndp->ni_vp);
1076 dp = VTOI(fndp->ni_dvp);
1077 } else {
79cf21e2
KM
1078 if (fndp->ni_dvp != NULL)
1079 vput(fndp->ni_dvp);
7188ac27 1080 xp = NULL;
4f1a9037 1081 dp = NULL;
7188ac27 1082 }
4f083fd7 1083 /*
7188ac27 1084 * Ensure that the directory entry still exists and has not
68f21562
KM
1085 * changed while the new name has been entered. If the source is
1086 * a file then the entry may have been unlinked or renamed. In
1087 * either case there is no further work to be done. If the source
1088 * is a directory then it cannot have been rmdir'ed; its link
1089 * count of three would cause a rmdir to fail with ENOTEMPTY.
7188ac27 1090 * The IRENAME flag ensures that it cannot be moved by another
68f21562 1091 * rename.
4f083fd7 1092 */
4f1a9037 1093 if (xp != ip) {
68f21562 1094 if (doingdirectory)
4f1a9037 1095 panic("rename: lost dir entry");
68f21562 1096 } else {
4f083fd7 1097 /*
68f21562
KM
1098 * If the source is a directory with a
1099 * new parent, the link count of the old
1100 * parent directory must be decremented
1101 * and ".." set to point to the new parent.
4f083fd7 1102 */
68f21562 1103 if (doingdirectory && newparent) {
4f083fd7
SL
1104 dp->i_nlink--;
1105 dp->i_flag |= ICHG;
86cdabf6 1106 error = vn_rdwr(UIO_READ, ITOV(xp), (caddr_t)&dirbuf,
7188ac27 1107 sizeof (struct dirtemplate), (off_t)0,
86cdabf6
KM
1108 UIO_SYSSPACE, IO_NODELOCKED,
1109 tndp->ni_cred, (int *)0);
68f21562
KM
1110 if (error == 0) {
1111 if (dirbuf.dotdot_namlen != 2 ||
1112 dirbuf.dotdot_name[0] != '.' ||
1113 dirbuf.dotdot_name[1] != '.') {
8ad54d9e 1114 dirbad(xp, 12, "rename: mangled dir");
68f21562
KM
1115 } else {
1116 dirbuf.dotdot_ino = newparent;
86cdabf6 1117 (void) vn_rdwr(UIO_WRITE, ITOV(xp),
68f21562
KM
1118 (caddr_t)&dirbuf,
1119 sizeof (struct dirtemplate),
93e273b9 1120 (off_t)0, UIO_SYSSPACE,
86cdabf6 1121 IO_NODELOCKED|IO_SYNC,
7188ac27
KM
1122 tndp->ni_cred, (int *)0);
1123 cache_purge(ITOV(dp));
68f21562
KM
1124 }
1125 }
4f083fd7 1126 }
7188ac27
KM
1127 error = dirremove(fndp);
1128 if (!error) {
68f21562
KM
1129 xp->i_nlink--;
1130 xp->i_flag |= ICHG;
4f083fd7 1131 }
68f21562 1132 xp->i_flag &= ~IRENAME;
4f083fd7 1133 }
4f083fd7 1134 if (dp)
7188ac27 1135 vput(ITOV(dp));
68f21562 1136 if (xp)
7188ac27
KM
1137 vput(ITOV(xp));
1138 vrele(ITOV(ip));
1139 return (error);
a5390dce 1140
4f083fd7 1141bad:
4f083fd7 1142 if (xp)
7188ac27
KM
1143 vput(ITOV(xp));
1144 vput(ITOV(dp));
4f083fd7
SL
1145out:
1146 ip->i_nlink--;
1147 ip->i_flag |= ICHG;
7188ac27
KM
1148 vrele(ITOV(ip));
1149 return (error);
64d3a787 1150}
88a7a62a
SL
1151
1152/*
1153 * A virgin directory (no blushing please).
1154 */
1155struct dirtemplate mastertemplate = {
1156 0, 12, 1, ".",
1157 0, DIRBLKSIZ - 12, 2, ".."
1158};
1159
1160/*
1161 * Mkdir system call
1162 */
7188ac27
KM
1163ufs_mkdir(ndp, vap)
1164 struct nameidata *ndp;
1165 struct vattr *vap;
88a7a62a 1166{
88a7a62a 1167 register struct inode *ip, *dp;
7188ac27
KM
1168 struct inode *tip;
1169 struct vnode *dvp;
88a7a62a 1170 struct dirtemplate dirtemplate;
7188ac27
KM
1171 int error;
1172 int dmode;
1173
1174 dvp = ndp->ni_dvp;
1175 dp = VTOI(dvp);
1176 dmode = vap->va_mode&0777;
1177 dmode |= IFDIR;
88a7a62a
SL
1178 /*
1179 * Must simulate part of maknode here
1180 * in order to acquire the inode, but
1181 * not have it entered in the parent
1182 * directory. The entry is made later
1183 * after writing "." and ".." entries out.
1184 */
4b61628b 1185 if (error = ialloc(dp, dirpref(dp->i_fs), dmode, ndp->ni_cred, &tip)) {
88a7a62a 1186 iput(dp);
7188ac27 1187 return (error);
88a7a62a 1188 }
7188ac27 1189 ip = tip;
4b61628b
KM
1190 ip->i_uid = ndp->ni_cred->cr_uid;
1191 ip->i_gid = dp->i_gid;
88a7a62a 1192#ifdef QUOTA
4b61628b
KM
1193 if ((error = getinoquota(ip)) ||
1194 (error = chkiq(ip, 1, ndp->ni_cred, 0))) {
1195 ifree(ip, ip->i_number, dmode);
1196 iput(ip);
1197 iput(dp);
1198 return (error);
1199 }
88a7a62a
SL
1200#endif
1201 ip->i_flag |= IACC|IUPD|ICHG;
7188ac27
KM
1202 ip->i_mode = dmode;
1203 ITOV(ip)->v_type = VDIR; /* Rest init'd in iget() */
88a7a62a 1204 ip->i_nlink = 2;
7188ac27 1205 error = iupdat(ip, &time, &time, 1);
88a7a62a
SL
1206
1207 /*
1208 * Bump link count in parent directory
1209 * to reflect work done below. Should
1210 * be done before reference is created
1211 * so reparation is possible if we crash.
1212 */
1213 dp->i_nlink++;
1214 dp->i_flag |= ICHG;
7188ac27 1215 error = iupdat(dp, &time, &time, 1);
88a7a62a
SL
1216
1217 /*
1218 * Initialize directory with "."
1219 * and ".." from static template.
1220 */
1221 dirtemplate = mastertemplate;
1222 dirtemplate.dot_ino = ip->i_number;
1223 dirtemplate.dotdot_ino = dp->i_number;
86cdabf6 1224 error = vn_rdwr(UIO_WRITE, ITOV(ip), (caddr_t)&dirtemplate,
7188ac27 1225 sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
86cdabf6 1226 IO_NODELOCKED|IO_SYNC, ndp->ni_cred, (int *)0);
7188ac27 1227 if (error) {
88a7a62a
SL
1228 dp->i_nlink--;
1229 dp->i_flag |= ICHG;
1230 goto bad;
1231 }
7188ac27
KM
1232 if (DIRBLKSIZ > dp->i_fs->fs_fsize)
1233 panic("mkdir: blksize"); /* XXX - should grow w/balloc() */
23de9f20
KM
1234 else
1235 ip->i_size = DIRBLKSIZ;
88a7a62a
SL
1236 /*
1237 * Directory all set up, now
1238 * install the entry for it in
1239 * the parent directory.
1240 */
7188ac27 1241 error = direnter(ip, ndp);
88a7a62a 1242 dp = NULL;
7188ac27 1243 if (error) {
715baff1 1244 ndp->ni_nameiop = LOOKUP | NOCACHE;
7188ac27
KM
1245 error = namei(ndp);
1246 if (!error) {
1247 dp = VTOI(ndp->ni_vp);
88a7a62a
SL
1248 dp->i_nlink--;
1249 dp->i_flag |= ICHG;
1250 }
1251 }
1252bad:
1253 /*
1254 * No need to do an explicit itrunc here,
7188ac27 1255 * vrele will do this for us because we set
88a7a62a
SL
1256 * the link count to 0.
1257 */
7188ac27 1258 if (error) {
88a7a62a
SL
1259 ip->i_nlink = 0;
1260 ip->i_flag |= ICHG;
cbcdacd6
KM
1261 iput(ip);
1262 } else
1263 ndp->ni_vp = ITOV(ip);
88a7a62a
SL
1264 if (dp)
1265 iput(dp);
7188ac27 1266 return (error);
88a7a62a
SL
1267}
1268
1269/*
1270 * Rmdir system call.
1271 */
7188ac27
KM
1272ufs_rmdir(ndp)
1273 register struct nameidata *ndp;
88a7a62a 1274{
88a7a62a 1275 register struct inode *ip, *dp;
7188ac27
KM
1276 int error = 0;
1277
1278 ip = VTOI(ndp->ni_vp);
1279 dp = VTOI(ndp->ni_dvp);
88a7a62a
SL
1280 /*
1281 * No rmdir "." please.
1282 */
1283 if (dp == ip) {
7188ac27 1284 vrele(ITOV(dp));
88a7a62a 1285 iput(ip);
7188ac27 1286 return (EINVAL);
88a7a62a
SL
1287 }
1288 /*
1289 * Verify the directory is empty (and valid).
1290 * (Rmdir ".." won't be valid since
1291 * ".." will contain a reference to
1292 * the current directory and thus be
1293 * non-empty.)
1294 */
7188ac27
KM
1295 if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) {
1296 error = ENOTEMPTY;
88a7a62a
SL
1297 goto out;
1298 }
1299 /*
1300 * Delete reference to directory before purging
1301 * inode. If we crash in between, the directory
1302 * will be reattached to lost+found,
1303 */
7188ac27 1304 if (error = dirremove(ndp))
88a7a62a
SL
1305 goto out;
1306 dp->i_nlink--;
1307 dp->i_flag |= ICHG;
7188ac27 1308 cache_purge(ITOV(dp));
88a7a62a 1309 iput(dp);
7188ac27 1310 ndp->ni_dvp = NULL;
88a7a62a
SL
1311 /*
1312 * Truncate inode. The only stuff left
1313 * in the directory is "." and "..". The
1314 * "." reference is inconsequential since
1315 * we're quashing it. The ".." reference
1316 * has already been adjusted above. We've
1317 * removed the "." reference and the reference
1318 * in the parent directory, but there may be
1319 * other hard links so decrement by 2 and
1320 * worry about them later.
1321 */
1322 ip->i_nlink -= 2;
e16fa59e 1323 error = itrunc(ip, (u_long)0, IO_SYNC);
7188ac27 1324 cache_purge(ITOV(ip));
88a7a62a 1325out:
7188ac27 1326 if (ndp->ni_dvp)
88a7a62a
SL
1327 iput(dp);
1328 iput(ip);
7188ac27 1329 return (error);
88a7a62a
SL
1330}
1331
7188ac27
KM
1332/*
1333 * symlink -- make a symbolic link
1334 */
1335ufs_symlink(ndp, vap, target)
1336 struct nameidata *ndp;
1337 struct vattr *vap;
1338 char *target;
1339{
1340 struct inode *ip;
1341 int error;
1342
1343 error = maknode(IFLNK | vap->va_mode, ndp, &ip);
1344 if (error)
1345 return (error);
86cdabf6
KM
1346 error = vn_rdwr(UIO_WRITE, ITOV(ip), target, strlen(target), (off_t)0,
1347 UIO_SYSSPACE, IO_NODELOCKED, ndp->ni_cred, (int *)0);
7188ac27
KM
1348 iput(ip);
1349 return (error);
1350}
1351
1352/*
1353 * Vnode op for read and write
1354 */
930730fd 1355ufs_readdir(vp, uio, cred, eofflagp)
7188ac27
KM
1356 struct vnode *vp;
1357 register struct uio *uio;
7188ac27 1358 struct ucred *cred;
930730fd 1359 int *eofflagp;
88a7a62a 1360{
86cdabf6 1361 int count, lost, error;
88a7a62a 1362
7188ac27
KM
1363 count = uio->uio_resid;
1364 count &= ~(DIRBLKSIZ - 1);
86cdabf6
KM
1365 lost = uio->uio_resid - count;
1366 if (count < DIRBLKSIZ || (uio->uio_offset & (DIRBLKSIZ -1)))
7188ac27 1367 return (EINVAL);
7188ac27
KM
1368 uio->uio_resid = count;
1369 uio->uio_iov->iov_len = count;
86cdabf6
KM
1370 error = ufs_read(vp, uio, 0, cred);
1371 uio->uio_resid += lost;
930730fd
KM
1372 if ((VTOI(vp)->i_size - uio->uio_offset) <= 0)
1373 *eofflagp = 1;
1374 else
1375 *eofflagp = 0;
7188ac27
KM
1376 return (error);
1377}
1378
1379/*
1380 * Return target name of a symbolic link
1381 */
1382ufs_readlink(vp, uiop, cred)
1383 struct vnode *vp;
1384 struct uio *uiop;
1385 struct ucred *cred;
1386{
1387
86cdabf6 1388 return (ufs_read(vp, uiop, 0, cred));
7188ac27
KM
1389}
1390
1391/*
1392 * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually
66955caf 1393 * done. Nothing to do at the moment.
7188ac27 1394 */
66955caf 1395/* ARGSUSED */
7188ac27 1396ufs_abortop(ndp)
66955caf 1397 struct nameidata *ndp;
7188ac27 1398{
7188ac27 1399
66955caf 1400 return (0);
7188ac27
KM
1401}
1402
1c3ebc10
KM
1403/*
1404 * Lock an inode.
1405 */
7188ac27
KM
1406ufs_lock(vp)
1407 struct vnode *vp;
1408{
1409 register struct inode *ip = VTOI(vp);
1410
1411 ILOCK(ip);
1412 return (0);
1413}
1414
1c3ebc10
KM
1415/*
1416 * Unlock an inode.
1417 */
7188ac27
KM
1418ufs_unlock(vp)
1419 struct vnode *vp;
1420{
1421 register struct inode *ip = VTOI(vp);
1422
1423 if (!(ip->i_flag & ILOCKED))
1424 panic("ufs_unlock NOT LOCKED");
1425 IUNLOCK(ip);
1426 return (0);
1427}
1428
1c3ebc10
KM
1429/*
1430 * Check for a locked inode.
1431 */
1432ufs_islocked(vp)
1433 struct vnode *vp;
1434{
1435
1436 if (VTOI(vp)->i_flag & ILOCKED)
1437 return (1);
1438 return (0);
1439}
1440
7188ac27
KM
1441/*
1442 * Get access to bmap
1443 */
1444ufs_bmap(vp, bn, vpp, bnp)
1445 struct vnode *vp;
1446 daddr_t bn;
1447 struct vnode **vpp;
1448 daddr_t *bnp;
1449{
1450 struct inode *ip = VTOI(vp);
1451
1452 if (vpp != NULL)
1453 *vpp = ip->i_devvp;
1454 if (bnp == NULL)
1455 return (0);
4f1ff475 1456 return (bmap(ip, bn, bnp));
88a7a62a
SL
1457}
1458
1459/*
4f1ff475
KM
1460 * Calculate the logical to physical mapping if not done already,
1461 * then call the device strategy routine.
88a7a62a 1462 */
4f1ff475 1463int checkoverlap = 0;
e16fa59e 1464
7188ac27
KM
1465ufs_strategy(bp)
1466 register struct buf *bp;
88a7a62a 1467{
e16fa59e 1468 register struct inode *ip = VTOI(bp->b_vp);
e16fa59e 1469 struct vnode *vp;
e16fa59e
KM
1470 int error;
1471
1472 if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR)
1473 panic("ufs_strategy: spec");
1474 if (bp->b_blkno == bp->b_lblkno) {
1475 if (error = bmap(ip, bp->b_lblkno, &bp->b_blkno))
1476 return (error);
20aa076b 1477 if ((long)bp->b_blkno == -1)
e16fa59e 1478 clrbuf(bp);
e16fa59e 1479 }
20aa076b
KM
1480 if ((long)bp->b_blkno == -1) {
1481 biodone(bp);
e16fa59e 1482 return (0);
20aa076b 1483 }
4f1ff475 1484#ifdef DIAGNOSTIC
e16fa59e 1485 if (checkoverlap) {
4f1ff475
KM
1486 register struct buf *ep;
1487 struct buf *ebp;
1488 daddr_t start, last;
1489
e16fa59e
KM
1490 ebp = &buf[nbuf];
1491 start = bp->b_blkno;
1492 last = start + btodb(bp->b_bcount) - 1;
1493 for (ep = buf; ep < ebp; ep++) {
1494 if (ep == bp || (ep->b_flags & B_INVAL) ||
fcd4f15a 1495 ep->b_vp == NULLVP)
e16fa59e
KM
1496 continue;
1497 if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0))
1498 continue;
1499 if (vp != ip->i_devvp)
1500 continue;
1501 /* look for overlap */
1502 if (ep->b_bcount == 0 || ep->b_blkno > last ||
1503 ep->b_blkno + btodb(ep->b_bcount) <= start)
1504 continue;
20aa076b
KM
1505 vprint("Disk overlap", vp);
1506 printf("\tstart %d, end %d overlap start %d, end %d\n",
1507 start, last, ep->b_blkno,
1508 ep->b_blkno + btodb(ep->b_bcount) - 1);
4f1ff475 1509 panic("Disk buffer overlap");
e16fa59e
KM
1510 }
1511 }
4f1ff475 1512#endif /* DIAGNOSTIC */
e16fa59e
KM
1513 vp = ip->i_devvp;
1514 bp->b_dev = vp->v_rdev;
1515 (*(vp->v_op->vn_strategy))(bp);
7188ac27
KM
1516 return (0);
1517}
88a7a62a 1518
e16fa59e
KM
1519/*
1520 * Print out the contents of an inode.
1521 */
1522ufs_print(vp)
1523 struct vnode *vp;
1524{
1525 register struct inode *ip = VTOI(vp);
1526
a8f829ed
KM
1527 printf("tag VT_UFS, ino %d, on dev %d, %d", ip->i_number,
1528 major(ip->i_dev), minor(ip->i_dev));
1529#ifdef FIFO
1530 if (vp->v_type == VFIFO)
1531 fifo_printinfo(vp);
1532#endif /* FIFO */
1533 printf("%s\n", (ip->i_flag & ILOCKED) ? " (LOCKED)" : "");
96bae3e0
KM
1534 if (ip->i_spare0 == 0)
1535 return;
1536 printf("\towner pid %d", ip->i_spare0);
1537 if (ip->i_spare1)
1538 printf(" waiting pid %d", ip->i_spare1);
1539 printf("\n");
e16fa59e
KM
1540}
1541
24a31b70
KM
1542/*
1543 * Read wrapper for special devices.
1544 */
1545ufsspec_read(vp, uio, ioflag, cred)
1546 struct vnode *vp;
1547 struct uio *uio;
1548 int ioflag;
1549 struct ucred *cred;
1550{
1551
1552 /*
1553 * Set access flag.
1554 */
1555 VTOI(vp)->i_flag |= IACC;
1556 return (spec_read(vp, uio, ioflag, cred));
1557}
1558
1559/*
1560 * Write wrapper for special devices.
1561 */
1562ufsspec_write(vp, uio, ioflag, cred)
1563 struct vnode *vp;
1564 struct uio *uio;
1565 int ioflag;
1566 struct ucred *cred;
1567{
1568
1569 /*
1570 * Set update and change flags.
1571 */
1572 VTOI(vp)->i_flag |= IUPD|ICHG;
1573 return (spec_write(vp, uio, ioflag, cred));
1574}
1575
1576/*
1577 * Close wrapper for special devices.
1578 *
1579 * Update the times on the inode then do device close.
1580 */
1581ufsspec_close(vp, fflag, cred)
1582 struct vnode *vp;
1583 int fflag;
1584 struct ucred *cred;
1585{
1586 register struct inode *ip = VTOI(vp);
1587
de67eefc 1588 if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
24a31b70
KM
1589 ITIMES(ip, &time, &time);
1590 return (spec_close(vp, fflag, cred));
1591}
1592
d1c43d7f
KM
1593#ifdef FIFO
1594/*
1595 * Read wrapper for fifo's
1596 */
1597ufsfifo_read(vp, uio, ioflag, cred)
1598 struct vnode *vp;
1599 struct uio *uio;
1600 int ioflag;
1601 struct ucred *cred;
1602{
1603
1604 /*
1605 * Set access flag.
1606 */
1607 VTOI(vp)->i_flag |= IACC;
1608 return (fifo_read(vp, uio, ioflag, cred));
1609}
1610
1611/*
1612 * Write wrapper for fifo's.
1613 */
1614ufsfifo_write(vp, uio, ioflag, cred)
1615 struct vnode *vp;
1616 struct uio *uio;
1617 int ioflag;
1618 struct ucred *cred;
1619{
1620
1621 /*
1622 * Set update and change flags.
1623 */
1624 VTOI(vp)->i_flag |= IUPD|ICHG;
1625 return (fifo_write(vp, uio, ioflag, cred));
1626}
1627
1628/*
1629 * Close wrapper for fifo's.
1630 *
1631 * Update the times on the inode then do device close.
1632 */
1633ufsfifo_close(vp, fflag, cred)
1634 struct vnode *vp;
1635 int fflag;
1636 struct ucred *cred;
1637{
1638 register struct inode *ip = VTOI(vp);
1639
1640 if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
1641 ITIMES(ip, &time, &time);
1642 return (fifo_close(vp, fflag, cred));
1643}
1644#endif /* FIFO */
1645
7188ac27
KM
1646/*
1647 * Make a new file.
1648 */
1649maknode(mode, ndp, ipp)
1650 int mode;
1651 register struct nameidata *ndp;
1652 struct inode **ipp;
1653{
1654 register struct inode *ip;
1655 struct inode *tip;
1656 register struct inode *pdir = VTOI(ndp->ni_dvp);
1657 ino_t ipref;
1658 int error;
1659
1660 *ipp = 0;
4b61628b
KM
1661 if ((mode & IFMT) == 0)
1662 mode |= IFREG;
7188ac27
KM
1663 if ((mode & IFMT) == IFDIR)
1664 ipref = dirpref(pdir->i_fs);
1665 else
1666 ipref = pdir->i_number;
4b61628b 1667 if (error = ialloc(pdir, ipref, mode, ndp->ni_cred, &tip)) {
7188ac27
KM
1668 iput(pdir);
1669 return (error);
1670 }
1671 ip = tip;
4b61628b
KM
1672 ip->i_uid = ndp->ni_cred->cr_uid;
1673 ip->i_gid = pdir->i_gid;
7188ac27 1674#ifdef QUOTA
4b61628b
KM
1675 if ((error = getinoquota(ip)) ||
1676 (error = chkiq(ip, 1, ndp->ni_cred, 0))) {
1677 ifree(ip, ip->i_number, mode);
1678 iput(ip);
1679 iput(pdir);
1680 return (error);
1681 }
7188ac27
KM
1682#endif
1683 ip->i_flag |= IACC|IUPD|ICHG;
7188ac27
KM
1684 ip->i_mode = mode;
1685 ITOV(ip)->v_type = IFTOVT(mode); /* Rest init'd in iget() */
1686 ip->i_nlink = 1;
7188ac27
KM
1687 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) &&
1688 suser(ndp->ni_cred, NULL))
1689 ip->i_mode &= ~ISGID;
7188ac27
KM
1690
1691 /*
1692 * Make sure inode goes to disk before directory entry.
1693 */
4b61628b
KM
1694 if (error = iupdat(ip, &time, &time, 1))
1695 goto bad;
1696 if (error = direnter(ip, ndp)) {
1697 pdir = NULL;
1698 goto bad;
7188ac27
KM
1699 }
1700 *ipp = ip;
1701 return (0);
4b61628b
KM
1702
1703bad:
1704 /*
1705 * Write error occurred trying to update the inode
1706 * or the directory so must deallocate the inode.
1707 */
1708 if (pdir)
1709 iput(pdir);
1710 ip->i_nlink = 0;
1711 ip->i_flag |= ICHG;
1712 iput(ip);
1713 return (error);
88a7a62a 1714}