readdir now returns an eof indication
[unix-history] / usr / src / sys / ufs / ufs / 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 *
930730fd 17 * @(#)ufs_vnops.c 7.33 (Berkeley) %G%
da7c5cc6 18 */
6459ebe0 19
94368568
JB
20#include "param.h"
21#include "systm.h"
94368568
JB
22#include "user.h"
23#include "kernel.h"
24#include "file.h"
25#include "stat.h"
94368568
JB
26#include "buf.h"
27#include "proc.h"
94368568
JB
28#include "uio.h"
29#include "socket.h"
30#include "socketvar.h"
7188ac27 31#include "conf.h"
94368568 32#include "mount.h"
7188ac27
KM
33#include "vnode.h"
34#include "../ufs/inode.h"
35#include "../ufs/fs.h"
36#include "../ufs/quota.h"
3e78e260 37
4f083fd7 38/*
7188ac27 39 * Global vfs data structures for ufs
4f083fd7 40 */
3e78e260 41
7188ac27
KM
42int ufs_lookup(),
43 ufs_create(),
44 ufs_mknod(),
45 ufs_open(),
46 ufs_close(),
47 ufs_access(),
48 ufs_getattr(),
49 ufs_setattr(),
50 ufs_read(),
51 ufs_write(),
52 ufs_ioctl(),
53 ufs_select(),
54 ufs_mmap(),
55 ufs_fsync(),
56 ufs_seek(),
57 ufs_remove(),
58 ufs_link(),
59 ufs_rename(),
60 ufs_mkdir(),
61 ufs_rmdir(),
62 ufs_symlink(),
63 ufs_readdir(),
64 ufs_readlink(),
65 ufs_abortop(),
66 ufs_inactive(),
e8a3816c 67 ufs_reclaim(),
7188ac27
KM
68 ufs_lock(),
69 ufs_unlock(),
70 ufs_bmap(),
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
7188ac27
KM
298ufs_access(vp, mode, cred)
299 struct vnode *vp;
300 int mode;
301 struct ucred *cred;
3e78e260 302{
3e78e260 303
7188ac27 304 return (iaccess(VTOI(vp), mode, cred));
3e78e260
BJ
305}
306
7188ac27
KM
307/* ARGSUSED */
308ufs_getattr(vp, vap, cred)
309 struct vnode *vp;
310 register struct vattr *vap;
311 struct ucred *cred;
3e78e260 312{
7188ac27 313 register struct inode *ip = VTOI(vp);
3e78e260 314
7188ac27 315 ITIMES(ip, &time, &time);
3e78e260 316 /*
7188ac27 317 * Copy from inode table
3e78e260 318 */
7188ac27
KM
319 vap->va_fsid = ip->i_dev;
320 vap->va_fileid = ip->i_number;
321 vap->va_mode = ip->i_mode & ~IFMT;
322 vap->va_nlink = ip->i_nlink;
323 vap->va_uid = ip->i_uid;
324 vap->va_gid = ip->i_gid;
325 vap->va_rdev = (dev_t)ip->i_rdev;
e8a3816c
KM
326 vap->va_size = ip->i_din.di_qsize.val[0];
327 vap->va_size1 = ip->i_din.di_qsize.val[1];
7188ac27 328 vap->va_atime.tv_sec = ip->i_atime;
6ef53d70 329 vap->va_atime.tv_usec = 0;
7188ac27 330 vap->va_mtime.tv_sec = ip->i_mtime;
6ef53d70 331 vap->va_mtime.tv_usec = 0;
7188ac27 332 vap->va_ctime.tv_sec = ip->i_ctime;
6ef53d70 333 vap->va_ctime.tv_usec = 0;
d07fc92a
KM
334 vap->va_flags = ip->i_flags;
335 vap->va_gen = ip->i_gen;
7188ac27
KM
336 /* this doesn't belong here */
337 if (vp->v_type == VBLK)
338 vap->va_blocksize = BLKDEV_IOSIZE;
339 else if (vp->v_type == VCHR)
340 vap->va_blocksize = MAXBSIZE;
8eee8525 341 else
7188ac27 342 vap->va_blocksize = ip->i_fs->fs_bsize;
a61a68d6 343 vap->va_bytes = dbtob(ip->i_blocks);
7188ac27
KM
344 vap->va_bytes1 = -1;
345 vap->va_type = vp->v_type;
346 return (0);
3e78e260
BJ
347}
348
349/*
7188ac27 350 * Set attribute vnode op. called from several syscalls
3e78e260 351 */
7188ac27
KM
352ufs_setattr(vp, vap, cred)
353 register struct vnode *vp;
354 register struct vattr *vap;
355 register struct ucred *cred;
3e78e260 356{
7188ac27
KM
357 register struct inode *ip = VTOI(vp);
358 int error = 0;
b4d1aee9 359
7188ac27
KM
360 /*
361 * Check for unsetable attributes.
362 */
363 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
364 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
365 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
d07fc92a 366 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
7188ac27 367 return (EINVAL);
b4d1aee9 368 }
7188ac27
KM
369 /*
370 * Go through the fields and update iff not VNOVAL.
371 */
372 if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL)
373 if (error = chown1(vp, vap->va_uid, vap->va_gid, cred))
374 return (error);
375 if (vap->va_size != VNOVAL) {
376 if (vp->v_type == VDIR)
377 return (EISDIR);
e16fa59e 378 if (error = itrunc(ip, vap->va_size, 0)) /* XXX IO_SYNC? */
7188ac27 379 return (error);
3e78e260 380 }
7188ac27 381 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
de412887
KM
382 if (cred->cr_uid != ip->i_uid &&
383 (error = suser(cred, &u.u_acflag)))
384 return (error);
7188ac27
KM
385 if (vap->va_atime.tv_sec != VNOVAL)
386 ip->i_flag |= IACC;
387 if (vap->va_mtime.tv_sec != VNOVAL)
388 ip->i_flag |= IUPD;
389 ip->i_flag |= ICHG;
390 if (error = iupdat(ip, &vap->va_atime, &vap->va_mtime, 1))
391 return (error);
5485e062 392 }
7188ac27
KM
393 if (vap->va_mode != (u_short)VNOVAL)
394 error = chmod1(vp, (int)vap->va_mode, cred);
d07fc92a
KM
395 if (vap->va_flags != VNOVAL) {
396 if (cred->cr_uid != ip->i_uid &&
397 (error = suser(cred, &u.u_acflag)))
398 return (error);
399 if (cred->cr_uid == 0) {
400 ip->i_flags = vap->va_flags;
401 } else {
402 ip->i_flags &= 0xffff0000;
403 ip->i_flags |= (vap->va_flags & 0xffff);
404 }
405 ip->i_flag |= ICHG;
406 }
7188ac27 407 return (error);
528f664c
SL
408}
409
4f083fd7
SL
410/*
411 * Change the mode on a file.
412 * Inode must be locked before calling.
413 */
7188ac27
KM
414chmod1(vp, mode, cred)
415 register struct vnode *vp;
528f664c 416 register int mode;
7188ac27 417 struct ucred *cred;
528f664c 418{
7188ac27 419 register struct inode *ip = VTOI(vp);
de412887 420 int error;
197da11b 421
de412887
KM
422 if (cred->cr_uid != ip->i_uid &&
423 (error = suser(cred, &u.u_acflag)))
424 return (error);
3e78e260 425 ip->i_mode &= ~07777;
7188ac27
KM
426 if (cred->cr_uid) {
427 if (vp->v_type != VDIR)
47af7174 428 mode &= ~ISVTX;
7188ac27 429 if (!groupmember(ip->i_gid, cred))
bb1b75f4 430 mode &= ~ISGID;
f94ceb3b 431 }
7188ac27 432 ip->i_mode |= mode & 07777;
3e78e260 433 ip->i_flag |= ICHG;
7188ac27
KM
434 if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0)
435 xrele(vp);
47af7174 436 return (0);
5485e062
BJ
437}
438
528f664c
SL
439/*
440 * Perform chown operation on inode ip;
441 * inode must be locked prior to call.
442 */
7188ac27
KM
443chown1(vp, uid, gid, cred)
444 register struct vnode *vp;
445 uid_t uid;
446 gid_t gid;
447 struct ucred *cred;
528f664c 448{
7188ac27 449 register struct inode *ip = VTOI(vp);
528f664c
SL
450#ifdef QUOTA
451 register long change;
bb1b75f4 452#endif
7188ac27 453 int error;
528f664c 454
7188ac27 455 if (uid == (u_short)VNOVAL)
bb1b75f4 456 uid = ip->i_uid;
7188ac27 457 if (gid == (u_short)VNOVAL)
bb1b75f4 458 gid = ip->i_gid;
18b0bce6
KB
459 /*
460 * If we don't own the file, are trying to change the owner
461 * of the file, or are not a member of the target group,
462 * the caller must be superuser or the call fails.
463 */
7188ac27
KM
464 if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
465 !groupmember((gid_t)gid, cred)) &&
466 (error = suser(cred, &u.u_acflag)))
467 return (error);
bb1b75f4 468#ifdef QUOTA
3809bf69 469 if (ip->i_uid == uid) /* this just speeds things a little */
0a77f278 470 change = 0;
2e073567
SL
471 else
472 change = ip->i_blocks;
473 (void) chkdq(ip, -change, 1);
474 (void) chkiq(ip->i_dev, ip, ip->i_uid, 1);
0a77f278 475 dqrele(ip->i_dquot);
f94ceb3b 476#endif
b440c2b9
KM
477 if (ip->i_uid != uid && cred->cr_uid != 0)
478 ip->i_mode &= ~ISUID;
479 if (ip->i_gid != gid && cred->cr_uid != 0)
480 ip->i_mode &= ~ISGID;
bb1b75f4
SL
481 ip->i_uid = uid;
482 ip->i_gid = gid;
3e78e260 483 ip->i_flag |= ICHG;
528f664c 484#ifdef QUOTA
0a77f278 485 ip->i_dquot = inoquota(ip);
2e073567 486 (void) chkdq(ip, change, 1);
8011f5df 487 (void) chkiq(ip->i_dev, (struct inode *)NULL, (uid_t)uid, 1);
2e073567
SL
488 return (u.u_error); /* should == 0 ALWAYS !! */
489#else
bb1b75f4 490 return (0);
2e073567 491#endif
d67a03eb
BJ
492}
493
b373e060
KM
494/*
495 * Vnode op for reading.
496 */
497/* ARGSUSED */
498ufs_read(vp, uio, ioflag, cred)
499 struct vnode *vp;
500 register struct uio *uio;
501 int ioflag;
502 struct ucred *cred;
503{
504 register struct inode *ip = VTOI(vp);
505 register struct fs *fs;
506 struct buf *bp;
507 daddr_t lbn, bn, rablock;
20aa076b 508 int size, diff, error = 0;
b373e060
KM
509 long n, on, type;
510
511 if (uio->uio_rw != UIO_READ)
512 panic("ufs_read mode");
513 type = ip->i_mode & IFMT;
514 if (type != IFDIR && type != IFREG && type != IFLNK)
515 panic("ufs_read type");
516 if (uio->uio_resid == 0)
517 return (0);
518 if (uio->uio_offset < 0)
519 return (EINVAL);
520 ip->i_flag |= IACC;
521 fs = ip->i_fs;
522 do {
523 lbn = lblkno(fs, uio->uio_offset);
524 on = blkoff(fs, uio->uio_offset);
525 n = MIN((unsigned)(fs->fs_bsize - on), uio->uio_resid);
526 diff = ip->i_size - uio->uio_offset;
527 if (diff <= 0)
528 return (0);
529 if (diff < n)
530 n = diff;
b373e060 531 size = blksize(fs, ip, lbn);
e16fa59e 532 rablock = lbn + 1;
20aa076b
KM
533 if (vp->v_lastr + 1 == lbn &&
534 lblktosize(fs, rablock) < ip->i_size)
535 error = breada(ITOV(ip), lbn, size, rablock,
536 blksize(fs, ip, rablock), NOCRED, &bp);
b373e060 537 else
e16fa59e 538 error = bread(ITOV(ip), lbn, size, NOCRED, &bp);
de67eefc 539 vp->v_lastr = lbn;
b373e060
KM
540 n = MIN(n, size - bp->b_resid);
541 if (error) {
542 brelse(bp);
543 return (error);
544 }
545 error = uiomove(bp->b_un.b_addr + on, (int)n, uio);
546 if (n + on == fs->fs_bsize || uio->uio_offset == ip->i_size)
547 bp->b_flags |= B_AGE;
548 brelse(bp);
549 } while (error == 0 && uio->uio_resid > 0 && n != 0);
550 return (error);
551}
552
553/*
554 * Vnode op for writing.
555 */
556ufs_write(vp, uio, ioflag, cred)
557 register struct vnode *vp;
558 struct uio *uio;
559 int ioflag;
560 struct ucred *cred;
561{
562 register struct inode *ip = VTOI(vp);
563 register struct fs *fs;
564 struct buf *bp;
565 daddr_t lbn, bn;
566 u_long osize;
567 int i, n, on, flags;
568 int count, size, resid, error = 0;
569
570 if (uio->uio_rw != UIO_WRITE)
571 panic("ufs_write mode");
572 switch (vp->v_type) {
573 case VREG:
574 if (ioflag & IO_APPEND)
575 uio->uio_offset = ip->i_size;
576 /* fall through */
577 case VLNK:
578 break;
579
580 case VDIR:
581 if ((ioflag & IO_SYNC) == 0)
582 panic("ufs_write nonsync dir write");
583 break;
584
585 default:
586 panic("ufs_write type");
587 }
588 if (uio->uio_offset < 0)
589 return (EINVAL);
590 if (uio->uio_resid == 0)
591 return (0);
592 /*
593 * Maybe this should be above the vnode op call, but so long as
594 * file servers have no limits, i don't think it matters
595 */
596 if (vp->v_type == VREG &&
597 uio->uio_offset + uio->uio_resid >
598 u.u_rlimit[RLIMIT_FSIZE].rlim_cur) {
599 psignal(u.u_procp, SIGXFSZ);
600 return (EFBIG);
601 }
602 resid = uio->uio_resid;
603 osize = ip->i_size;
604 fs = ip->i_fs;
e16fa59e
KM
605 flags = 0;
606 if (ioflag & IO_SYNC)
607 flags = B_SYNC;
b373e060
KM
608 do {
609 lbn = lblkno(fs, uio->uio_offset);
610 on = blkoff(fs, uio->uio_offset);
611 n = MIN((unsigned)(fs->fs_bsize - on), uio->uio_resid);
612 if (n < fs->fs_bsize)
e16fa59e 613 flags |= B_CLRBUF;
b373e060 614 else
e16fa59e
KM
615 flags &= ~B_CLRBUF;
616 if (error = balloc(ip, lbn, (int)(on + n), &bp, flags))
b373e060 617 break;
e16fa59e 618 bn = bp->b_blkno;
b373e060
KM
619 if (uio->uio_offset + n > ip->i_size)
620 ip->i_size = uio->uio_offset + n;
621 size = blksize(fs, ip, lbn);
622 count = howmany(size, CLBYTES);
623 for (i = 0; i < count; i++)
624 munhash(ip->i_devvp, bn + i * CLBYTES / DEV_BSIZE);
b373e060 625 n = MIN(n, size - bp->b_resid);
b373e060
KM
626 error = uiomove(bp->b_un.b_addr + on, n, uio);
627 if (ioflag & IO_SYNC)
628 (void) bwrite(bp);
629 else if (n + on == fs->fs_bsize) {
630 bp->b_flags |= B_AGE;
631 bawrite(bp);
632 } else
633 bdwrite(bp);
634 ip->i_flag |= IUPD|ICHG;
635 if (cred->cr_uid != 0)
636 ip->i_mode &= ~(ISUID|ISGID);
637 } while (error == 0 && uio->uio_resid > 0 && n != 0);
638 if (error && (ioflag & IO_UNIT)) {
e16fa59e 639 (void) itrunc(ip, osize, ioflag & IO_SYNC);
b373e060
KM
640 uio->uio_offset -= resid - uio->uio_resid;
641 uio->uio_resid = resid;
642 }
643 return (error);
644}
645
7188ac27
KM
646/* ARGSUSED */
647ufs_ioctl(vp, com, data, fflag, cred)
648 struct vnode *vp;
649 int com;
650 caddr_t data;
651 int fflag;
652 struct ucred *cred;
bb1b75f4 653{
bb1b75f4 654
7188ac27
KM
655 return (ENOTTY);
656}
657
658/* ARGSUSED */
d1c43d7f 659ufs_select(vp, which, fflags, cred)
7188ac27 660 struct vnode *vp;
d1c43d7f 661 int which, fflags;
7188ac27
KM
662 struct ucred *cred;
663{
664
7188ac27 665 return (1); /* XXX */
bb1b75f4 666}
d67a03eb 667
4f083fd7 668/*
7188ac27
KM
669 * Mmap a file
670 *
671 * NB Currently unsupported.
4f083fd7 672 */
7188ac27
KM
673/* ARGSUSED */
674ufs_mmap(vp, fflags, cred)
675 struct vnode *vp;
676 int fflags;
677 struct ucred *cred;
d67a03eb 678{
d67a03eb 679
7188ac27 680 return (EINVAL);
d67a03eb 681}
64d3a787 682
4f083fd7 683/*
7188ac27 684 * Synch an open file.
4f083fd7 685 */
7188ac27 686/* ARGSUSED */
86cdabf6 687ufs_fsync(vp, fflags, cred, waitfor)
7188ac27
KM
688 struct vnode *vp;
689 int fflags;
690 struct ucred *cred;
86cdabf6 691 int waitfor;
528f664c 692{
86cdabf6 693 struct inode *ip = VTOI(vp);
7188ac27 694
7188ac27
KM
695 if (fflags&FWRITE)
696 ip->i_flag |= ICHG;
e16fa59e
KM
697 vflushbuf(vp, waitfor == MNT_WAIT ? B_SYNC : 0);
698 return (iupdat(ip, &time, &time, waitfor == MNT_WAIT));
528f664c
SL
699}
700
4f083fd7 701/*
7188ac27
KM
702 * Seek on a file
703 *
704 * Nothing to do, so just return.
4f083fd7 705 */
7188ac27
KM
706/* ARGSUSED */
707ufs_seek(vp, oldoff, newoff, cred)
708 struct vnode *vp;
709 off_t oldoff, newoff;
710 struct ucred *cred;
528f664c 711{
7188ac27
KM
712
713 return (0);
714}
715
716/*
717 * ufs remove
718 * Hard to avoid races here, especially
719 * in unlinking directories.
720 */
721ufs_remove(ndp)
722 struct nameidata *ndp;
723{
724 register struct inode *ip, *dp;
725 int error;
726
727 ip = VTOI(ndp->ni_vp);
728 dp = VTOI(ndp->ni_dvp);
729 error = dirremove(ndp);
730 if (!error) {
731 ip->i_nlink--;
732 ip->i_flag |= ICHG;
528f664c 733 }
7188ac27
KM
734 if (dp == ip)
735 vrele(ITOV(ip));
736 else
737 iput(ip);
738 iput(dp);
739 return (error);
4f083fd7
SL
740}
741
742/*
7188ac27 743 * link vnode call
4f083fd7 744 */
7188ac27
KM
745ufs_link(vp, ndp)
746 register struct vnode *vp;
747 register struct nameidata *ndp;
4f083fd7 748{
7188ac27
KM
749 register struct inode *ip = VTOI(vp);
750 int error;
4f083fd7 751
7188ac27
KM
752 if (ndp->ni_dvp != vp)
753 ILOCK(ip);
754 if (ip->i_nlink == LINK_MAX - 1) {
755 error = EMLINK;
756 goto out;
757 }
758 ip->i_nlink++;
759 ip->i_flag |= ICHG;
760 error = iupdat(ip, &time, &time, 1);
761 if (!error)
762 error = direnter(ip, ndp);
763out:
764 if (ndp->ni_dvp != vp)
765 IUNLOCK(ip);
766 if (error) {
767 ip->i_nlink--;
82252d2b 768 ip->i_flag |= ICHG;
7188ac27
KM
769 }
770 return (error);
528f664c
SL
771}
772
4f083fd7
SL
773/*
774 * Rename system call.
775 * rename("foo", "bar");
776 * is essentially
777 * unlink("bar");
778 * link("foo", "bar");
779 * unlink("foo");
780 * but ``atomically''. Can't do full commit without saving state in the
781 * inode on disk which isn't feasible at this time. Best we can do is
782 * always guarantee the target exists.
783 *
784 * Basic algorithm is:
785 *
786 * 1) Bump link count on source while we're linking it to the
7188ac27 787 * target. This also ensure the inode won't be deleted out
68f21562
KM
788 * from underneath us while we work (it may be truncated by
789 * a concurrent `trunc' or `open' for creation).
4f083fd7
SL
790 * 2) Link source to destination. If destination already exists,
791 * delete it first.
68f21562
KM
792 * 3) Unlink source reference to inode if still around. If a
793 * directory was moved and the parent of the destination
4f083fd7
SL
794 * is different from the source, patch the ".." entry in the
795 * directory.
4f083fd7 796 */
7188ac27
KM
797ufs_rename(fndp, tndp)
798 register struct nameidata *fndp, *tndp;
528f664c 799{
4f083fd7 800 register struct inode *ip, *xp, *dp;
68f21562
KM
801 struct dirtemplate dirbuf;
802 int doingdirectory = 0, oldparent = 0, newparent = 0;
a5390dce 803 int error = 0;
4f083fd7 804
7188ac27
KM
805 dp = VTOI(fndp->ni_dvp);
806 ip = VTOI(fndp->ni_vp);
807 ILOCK(ip);
4f083fd7 808 if ((ip->i_mode&IFMT) == IFDIR) {
7188ac27 809 register struct direct *d = &fndp->ni_dent;
4f083fd7 810
4f083fd7 811 /*
046f18d1 812 * Avoid ".", "..", and aliases of "." for obvious reasons.
4f083fd7 813 */
7188ac27
KM
814 if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip ||
815 fndp->ni_isdotdot || (ip->i_flag & IRENAME)) {
816 IUNLOCK(ip);
817 ufs_abortop(fndp);
818 ufs_abortop(tndp);
819 return (EINVAL);
4f083fd7 820 }
68f21562 821 ip->i_flag |= IRENAME;
4f083fd7
SL
822 oldparent = dp->i_number;
823 doingdirectory++;
824 }
7188ac27 825 vrele(fndp->ni_dvp);
4f083fd7
SL
826
827 /*
828 * 1) Bump link count while we're moving stuff
829 * around. If we crash somewhere before
830 * completing our work, the link count
831 * may be wrong, but correctable.
832 */
833 ip->i_nlink++;
834 ip->i_flag |= ICHG;
7188ac27 835 error = iupdat(ip, &time, &time, 1);
a388503d 836 IUNLOCK(ip);
4f083fd7
SL
837
838 /*
839 * When the target exists, both the directory
7188ac27 840 * and target vnodes are returned locked.
4f083fd7 841 */
7188ac27
KM
842 dp = VTOI(tndp->ni_dvp);
843 xp = NULL;
844 if (tndp->ni_vp)
845 xp = VTOI(tndp->ni_vp);
046f18d1
SL
846 /*
847 * If ".." must be changed (ie the directory gets a new
81552f0f
KM
848 * parent) then the source directory must not be in the
849 * directory heirarchy above the target, as this would
850 * orphan everything below the source directory. Also
851 * the user must have write permission in the source so
852 * as to be able to change "..". We must repeat the call
853 * to namei, as the parent directory is unlocked by the
854 * call to checkpath().
046f18d1 855 */
68f21562
KM
856 if (oldparent != dp->i_number)
857 newparent = dp->i_number;
858 if (doingdirectory && newparent) {
7188ac27 859 if (error = iaccess(ip, IWRITE, tndp->ni_cred))
81552f0f 860 goto bad;
7188ac27 861 tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE;
81552f0f 862 do {
7188ac27 863 dp = VTOI(tndp->ni_dvp);
81552f0f 864 if (xp != NULL)
79cf21e2 865 iput(xp);
7188ac27 866 if (error = checkpath(ip, dp, tndp->ni_cred))
81552f0f 867 goto out;
7188ac27 868 if (error = namei(tndp))
81552f0f 869 goto out;
7188ac27
KM
870 xp = NULL;
871 if (tndp->ni_vp)
872 xp = VTOI(tndp->ni_vp);
873 } while (dp != VTOI(tndp->ni_dvp));
81552f0f 874 }
4f083fd7
SL
875 /*
876 * 2) If target doesn't exist, link the target
877 * to the source and unlink the source.
878 * Otherwise, rewrite the target directory
879 * entry to reference the source inode and
880 * expunge the original entry's existence.
881 */
4f083fd7 882 if (xp == NULL) {
7188ac27
KM
883 if (dp->i_dev != ip->i_dev)
884 panic("rename: EXDEV");
4f083fd7 885 /*
68f21562
KM
886 * Account for ".." in new directory.
887 * When source and destination have the same
888 * parent we don't fool with the link count.
4f083fd7 889 */
68f21562 890 if (doingdirectory && newparent) {
4f083fd7
SL
891 dp->i_nlink++;
892 dp->i_flag |= ICHG;
7188ac27 893 error = iupdat(dp, &time, &time, 1);
4f083fd7 894 }
7188ac27 895 if (error = direnter(ip, tndp))
4f083fd7
SL
896 goto out;
897 } else {
7188ac27
KM
898 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
899 panic("rename: EXDEV");
e69c3c9c
SL
900 /*
901 * Short circuit rename(foo, foo).
902 */
903 if (xp->i_number == ip->i_number)
7188ac27 904 panic("rename: same file");
80cee150
JB
905 /*
906 * If the parent directory is "sticky", then the user must
907 * own the parent directory, or the destination of the rename,
908 * otherwise the destination may not be changed (except by
909 * root). This implements append-only directories.
910 */
7188ac27
KM
911 if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 &&
912 tndp->ni_cred->cr_uid != dp->i_uid &&
913 xp->i_uid != tndp->ni_cred->cr_uid) {
80cee150
JB
914 error = EPERM;
915 goto bad;
916 }
4f083fd7 917 /*
a5390dce
SL
918 * Target must be empty if a directory
919 * and have no links to it.
4f083fd7
SL
920 * Also, insure source and target are
921 * compatible (both directories, or both
922 * not directories).
923 */
924 if ((xp->i_mode&IFMT) == IFDIR) {
7188ac27
KM
925 if (!dirempty(xp, dp->i_number, tndp->ni_cred) ||
926 xp->i_nlink > 2) {
a5390dce 927 error = ENOTEMPTY;
4f083fd7
SL
928 goto bad;
929 }
930 if (!doingdirectory) {
a5390dce 931 error = ENOTDIR;
4f083fd7
SL
932 goto bad;
933 }
7188ac27 934 cache_purge(ITOV(dp));
4f083fd7 935 } else if (doingdirectory) {
a5390dce 936 error = EISDIR;
4f083fd7
SL
937 goto bad;
938 }
7188ac27
KM
939 if (error = dirrewrite(dp, ip, tndp))
940 goto bad;
941 vput(ITOV(dp));
4f083fd7 942 /*
a5390dce
SL
943 * Adjust the link count of the target to
944 * reflect the dirrewrite above. If this is
945 * a directory it is empty and there are
946 * no links to it, so we can squash the inode and
947 * any space associated with it. We disallowed
948 * renaming over top of a directory with links to
68f21562
KM
949 * it above, as the remaining link would point to
950 * a directory without "." or ".." entries.
4f083fd7 951 */
a5390dce 952 xp->i_nlink--;
4f083fd7 953 if (doingdirectory) {
a5390dce
SL
954 if (--xp->i_nlink != 0)
955 panic("rename: linked directory");
e16fa59e 956 error = itrunc(xp, (u_long)0, IO_SYNC);
a5390dce 957 }
4f083fd7 958 xp->i_flag |= ICHG;
88d931ba 959 iput(xp);
31db12cb 960 xp = NULL;
4f083fd7
SL
961 }
962
963 /*
964 * 3) Unlink the source.
965 */
7188ac27
KM
966 fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF;
967 (void)namei(fndp);
968 if (fndp->ni_vp != NULL) {
969 xp = VTOI(fndp->ni_vp);
970 dp = VTOI(fndp->ni_dvp);
971 } else {
79cf21e2
KM
972 if (fndp->ni_dvp != NULL)
973 vput(fndp->ni_dvp);
7188ac27 974 xp = NULL;
4f1a9037 975 dp = NULL;
7188ac27 976 }
4f083fd7 977 /*
7188ac27 978 * Ensure that the directory entry still exists and has not
68f21562
KM
979 * changed while the new name has been entered. If the source is
980 * a file then the entry may have been unlinked or renamed. In
981 * either case there is no further work to be done. If the source
982 * is a directory then it cannot have been rmdir'ed; its link
983 * count of three would cause a rmdir to fail with ENOTEMPTY.
7188ac27 984 * The IRENAME flag ensures that it cannot be moved by another
68f21562 985 * rename.
4f083fd7 986 */
4f1a9037 987 if (xp != ip) {
68f21562 988 if (doingdirectory)
4f1a9037 989 panic("rename: lost dir entry");
68f21562 990 } else {
4f083fd7 991 /*
68f21562
KM
992 * If the source is a directory with a
993 * new parent, the link count of the old
994 * parent directory must be decremented
995 * and ".." set to point to the new parent.
4f083fd7 996 */
68f21562 997 if (doingdirectory && newparent) {
4f083fd7
SL
998 dp->i_nlink--;
999 dp->i_flag |= ICHG;
86cdabf6 1000 error = vn_rdwr(UIO_READ, ITOV(xp), (caddr_t)&dirbuf,
7188ac27 1001 sizeof (struct dirtemplate), (off_t)0,
86cdabf6
KM
1002 UIO_SYSSPACE, IO_NODELOCKED,
1003 tndp->ni_cred, (int *)0);
68f21562
KM
1004 if (error == 0) {
1005 if (dirbuf.dotdot_namlen != 2 ||
1006 dirbuf.dotdot_name[0] != '.' ||
1007 dirbuf.dotdot_name[1] != '.') {
8ad54d9e 1008 dirbad(xp, 12, "rename: mangled dir");
68f21562
KM
1009 } else {
1010 dirbuf.dotdot_ino = newparent;
86cdabf6 1011 (void) vn_rdwr(UIO_WRITE, ITOV(xp),
68f21562
KM
1012 (caddr_t)&dirbuf,
1013 sizeof (struct dirtemplate),
93e273b9 1014 (off_t)0, UIO_SYSSPACE,
86cdabf6 1015 IO_NODELOCKED|IO_SYNC,
7188ac27
KM
1016 tndp->ni_cred, (int *)0);
1017 cache_purge(ITOV(dp));
68f21562
KM
1018 }
1019 }
4f083fd7 1020 }
7188ac27
KM
1021 error = dirremove(fndp);
1022 if (!error) {
68f21562
KM
1023 xp->i_nlink--;
1024 xp->i_flag |= ICHG;
4f083fd7 1025 }
68f21562 1026 xp->i_flag &= ~IRENAME;
4f083fd7 1027 }
4f083fd7 1028 if (dp)
7188ac27 1029 vput(ITOV(dp));
68f21562 1030 if (xp)
7188ac27
KM
1031 vput(ITOV(xp));
1032 vrele(ITOV(ip));
1033 return (error);
a5390dce 1034
4f083fd7 1035bad:
4f083fd7 1036 if (xp)
7188ac27
KM
1037 vput(ITOV(xp));
1038 vput(ITOV(dp));
4f083fd7
SL
1039out:
1040 ip->i_nlink--;
1041 ip->i_flag |= ICHG;
7188ac27
KM
1042 vrele(ITOV(ip));
1043 return (error);
64d3a787 1044}
88a7a62a
SL
1045
1046/*
1047 * A virgin directory (no blushing please).
1048 */
1049struct dirtemplate mastertemplate = {
1050 0, 12, 1, ".",
1051 0, DIRBLKSIZ - 12, 2, ".."
1052};
1053
1054/*
1055 * Mkdir system call
1056 */
7188ac27
KM
1057ufs_mkdir(ndp, vap)
1058 struct nameidata *ndp;
1059 struct vattr *vap;
88a7a62a 1060{
88a7a62a 1061 register struct inode *ip, *dp;
7188ac27
KM
1062 struct inode *tip;
1063 struct vnode *dvp;
88a7a62a 1064 struct dirtemplate dirtemplate;
7188ac27
KM
1065 int error;
1066 int dmode;
1067
1068 dvp = ndp->ni_dvp;
1069 dp = VTOI(dvp);
1070 dmode = vap->va_mode&0777;
1071 dmode |= IFDIR;
88a7a62a
SL
1072 /*
1073 * Must simulate part of maknode here
1074 * in order to acquire the inode, but
1075 * not have it entered in the parent
1076 * directory. The entry is made later
1077 * after writing "." and ".." entries out.
1078 */
7188ac27
KM
1079 error = ialloc(dp, dirpref(dp->i_fs), dmode, &tip);
1080 if (error) {
88a7a62a 1081 iput(dp);
7188ac27 1082 return (error);
88a7a62a 1083 }
7188ac27 1084 ip = tip;
88a7a62a
SL
1085#ifdef QUOTA
1086 if (ip->i_dquot != NODQUOT)
1087 panic("mkdir: dquot");
1088#endif
1089 ip->i_flag |= IACC|IUPD|ICHG;
7188ac27
KM
1090 ip->i_mode = dmode;
1091 ITOV(ip)->v_type = VDIR; /* Rest init'd in iget() */
88a7a62a 1092 ip->i_nlink = 2;
7188ac27 1093 ip->i_uid = ndp->ni_cred->cr_uid;
88a7a62a
SL
1094 ip->i_gid = dp->i_gid;
1095#ifdef QUOTA
1096 ip->i_dquot = inoquota(ip);
1097#endif
7188ac27 1098 error = iupdat(ip, &time, &time, 1);
88a7a62a
SL
1099
1100 /*
1101 * Bump link count in parent directory
1102 * to reflect work done below. Should
1103 * be done before reference is created
1104 * so reparation is possible if we crash.
1105 */
1106 dp->i_nlink++;
1107 dp->i_flag |= ICHG;
7188ac27 1108 error = iupdat(dp, &time, &time, 1);
88a7a62a
SL
1109
1110 /*
1111 * Initialize directory with "."
1112 * and ".." from static template.
1113 */
1114 dirtemplate = mastertemplate;
1115 dirtemplate.dot_ino = ip->i_number;
1116 dirtemplate.dotdot_ino = dp->i_number;
86cdabf6 1117 error = vn_rdwr(UIO_WRITE, ITOV(ip), (caddr_t)&dirtemplate,
7188ac27 1118 sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
86cdabf6 1119 IO_NODELOCKED|IO_SYNC, ndp->ni_cred, (int *)0);
7188ac27 1120 if (error) {
88a7a62a
SL
1121 dp->i_nlink--;
1122 dp->i_flag |= ICHG;
1123 goto bad;
1124 }
7188ac27
KM
1125 if (DIRBLKSIZ > dp->i_fs->fs_fsize)
1126 panic("mkdir: blksize"); /* XXX - should grow w/balloc() */
23de9f20
KM
1127 else
1128 ip->i_size = DIRBLKSIZ;
88a7a62a
SL
1129 /*
1130 * Directory all set up, now
1131 * install the entry for it in
1132 * the parent directory.
1133 */
7188ac27 1134 error = direnter(ip, ndp);
88a7a62a 1135 dp = NULL;
7188ac27 1136 if (error) {
715baff1 1137 ndp->ni_nameiop = LOOKUP | NOCACHE;
7188ac27
KM
1138 error = namei(ndp);
1139 if (!error) {
1140 dp = VTOI(ndp->ni_vp);
88a7a62a
SL
1141 dp->i_nlink--;
1142 dp->i_flag |= ICHG;
1143 }
1144 }
1145bad:
1146 /*
1147 * No need to do an explicit itrunc here,
7188ac27 1148 * vrele will do this for us because we set
88a7a62a
SL
1149 * the link count to 0.
1150 */
7188ac27 1151 if (error) {
88a7a62a
SL
1152 ip->i_nlink = 0;
1153 ip->i_flag |= ICHG;
cbcdacd6
KM
1154 iput(ip);
1155 } else
1156 ndp->ni_vp = ITOV(ip);
88a7a62a
SL
1157 if (dp)
1158 iput(dp);
7188ac27 1159 return (error);
88a7a62a
SL
1160}
1161
1162/*
1163 * Rmdir system call.
1164 */
7188ac27
KM
1165ufs_rmdir(ndp)
1166 register struct nameidata *ndp;
88a7a62a 1167{
88a7a62a 1168 register struct inode *ip, *dp;
7188ac27
KM
1169 int error = 0;
1170
1171 ip = VTOI(ndp->ni_vp);
1172 dp = VTOI(ndp->ni_dvp);
88a7a62a
SL
1173 /*
1174 * No rmdir "." please.
1175 */
1176 if (dp == ip) {
7188ac27 1177 vrele(ITOV(dp));
88a7a62a 1178 iput(ip);
7188ac27 1179 return (EINVAL);
88a7a62a
SL
1180 }
1181 /*
1182 * Verify the directory is empty (and valid).
1183 * (Rmdir ".." won't be valid since
1184 * ".." will contain a reference to
1185 * the current directory and thus be
1186 * non-empty.)
1187 */
7188ac27
KM
1188 if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) {
1189 error = ENOTEMPTY;
88a7a62a
SL
1190 goto out;
1191 }
1192 /*
1193 * Delete reference to directory before purging
1194 * inode. If we crash in between, the directory
1195 * will be reattached to lost+found,
1196 */
7188ac27 1197 if (error = dirremove(ndp))
88a7a62a
SL
1198 goto out;
1199 dp->i_nlink--;
1200 dp->i_flag |= ICHG;
7188ac27 1201 cache_purge(ITOV(dp));
88a7a62a 1202 iput(dp);
7188ac27 1203 ndp->ni_dvp = NULL;
88a7a62a
SL
1204 /*
1205 * Truncate inode. The only stuff left
1206 * in the directory is "." and "..". The
1207 * "." reference is inconsequential since
1208 * we're quashing it. The ".." reference
1209 * has already been adjusted above. We've
1210 * removed the "." reference and the reference
1211 * in the parent directory, but there may be
1212 * other hard links so decrement by 2 and
1213 * worry about them later.
1214 */
1215 ip->i_nlink -= 2;
e16fa59e 1216 error = itrunc(ip, (u_long)0, IO_SYNC);
7188ac27 1217 cache_purge(ITOV(ip));
88a7a62a 1218out:
7188ac27 1219 if (ndp->ni_dvp)
88a7a62a
SL
1220 iput(dp);
1221 iput(ip);
7188ac27 1222 return (error);
88a7a62a
SL
1223}
1224
7188ac27
KM
1225/*
1226 * symlink -- make a symbolic link
1227 */
1228ufs_symlink(ndp, vap, target)
1229 struct nameidata *ndp;
1230 struct vattr *vap;
1231 char *target;
1232{
1233 struct inode *ip;
1234 int error;
1235
1236 error = maknode(IFLNK | vap->va_mode, ndp, &ip);
1237 if (error)
1238 return (error);
86cdabf6
KM
1239 error = vn_rdwr(UIO_WRITE, ITOV(ip), target, strlen(target), (off_t)0,
1240 UIO_SYSSPACE, IO_NODELOCKED, ndp->ni_cred, (int *)0);
7188ac27
KM
1241 iput(ip);
1242 return (error);
1243}
1244
1245/*
1246 * Vnode op for read and write
1247 */
930730fd 1248ufs_readdir(vp, uio, cred, eofflagp)
7188ac27
KM
1249 struct vnode *vp;
1250 register struct uio *uio;
7188ac27 1251 struct ucred *cred;
930730fd 1252 int *eofflagp;
88a7a62a 1253{
86cdabf6 1254 int count, lost, error;
88a7a62a 1255
7188ac27
KM
1256 count = uio->uio_resid;
1257 count &= ~(DIRBLKSIZ - 1);
86cdabf6
KM
1258 lost = uio->uio_resid - count;
1259 if (count < DIRBLKSIZ || (uio->uio_offset & (DIRBLKSIZ -1)))
7188ac27 1260 return (EINVAL);
7188ac27
KM
1261 uio->uio_resid = count;
1262 uio->uio_iov->iov_len = count;
86cdabf6
KM
1263 error = ufs_read(vp, uio, 0, cred);
1264 uio->uio_resid += lost;
930730fd
KM
1265 if ((VTOI(vp)->i_size - uio->uio_offset) <= 0)
1266 *eofflagp = 1;
1267 else
1268 *eofflagp = 0;
7188ac27
KM
1269 return (error);
1270}
1271
1272/*
1273 * Return target name of a symbolic link
1274 */
1275ufs_readlink(vp, uiop, cred)
1276 struct vnode *vp;
1277 struct uio *uiop;
1278 struct ucred *cred;
1279{
1280
86cdabf6 1281 return (ufs_read(vp, uiop, 0, cred));
7188ac27
KM
1282}
1283
1284/*
1285 * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually
1286 * done. Iff ni_vp/ni_dvp not null and locked, unlock.
1287 */
1288ufs_abortop(ndp)
1289 register struct nameidata *ndp;
1290{
7188ac27 1291
7188ac27 1292 if (ndp->ni_dvp) {
7359b026
KM
1293 if (VOP_ISLOCKED(ndp->ni_dvp))
1294 VOP_UNLOCK(ndp->ni_dvp);
7188ac27 1295 vrele(ndp->ni_dvp);
88a7a62a 1296 }
7359b026
KM
1297 if (ndp->ni_vp) {
1298 if (VOP_ISLOCKED(ndp->ni_vp))
1299 VOP_UNLOCK(ndp->ni_vp);
1300 vrele(ndp->ni_vp);
1301 }
7188ac27
KM
1302 return;
1303}
1304
1c3ebc10
KM
1305/*
1306 * Lock an inode.
1307 */
7188ac27
KM
1308ufs_lock(vp)
1309 struct vnode *vp;
1310{
1311 register struct inode *ip = VTOI(vp);
1312
1313 ILOCK(ip);
1314 return (0);
1315}
1316
1c3ebc10
KM
1317/*
1318 * Unlock an inode.
1319 */
7188ac27
KM
1320ufs_unlock(vp)
1321 struct vnode *vp;
1322{
1323 register struct inode *ip = VTOI(vp);
1324
1325 if (!(ip->i_flag & ILOCKED))
1326 panic("ufs_unlock NOT LOCKED");
1327 IUNLOCK(ip);
1328 return (0);
1329}
1330
1c3ebc10
KM
1331/*
1332 * Check for a locked inode.
1333 */
1334ufs_islocked(vp)
1335 struct vnode *vp;
1336{
1337
1338 if (VTOI(vp)->i_flag & ILOCKED)
1339 return (1);
1340 return (0);
1341}
1342
7188ac27
KM
1343/*
1344 * Get access to bmap
1345 */
1346ufs_bmap(vp, bn, vpp, bnp)
1347 struct vnode *vp;
1348 daddr_t bn;
1349 struct vnode **vpp;
1350 daddr_t *bnp;
1351{
1352 struct inode *ip = VTOI(vp);
1353
1354 if (vpp != NULL)
1355 *vpp = ip->i_devvp;
1356 if (bnp == NULL)
1357 return (0);
1358 return (bmap(ip, bn, bnp, (daddr_t *)0, (int *)0));
88a7a62a
SL
1359}
1360
1361/*
7188ac27 1362 * Just call the device strategy routine
88a7a62a 1363 */
e16fa59e
KM
1364int checkoverlap = 1;
1365
7188ac27
KM
1366ufs_strategy(bp)
1367 register struct buf *bp;
88a7a62a 1368{
e16fa59e
KM
1369 register struct inode *ip = VTOI(bp->b_vp);
1370 register struct buf *ep;
1371 struct vnode *vp;
1372 struct buf *ebp;
1373 daddr_t start, last;
1374 int error;
1375
1376 if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR)
1377 panic("ufs_strategy: spec");
1378 if (bp->b_blkno == bp->b_lblkno) {
1379 if (error = bmap(ip, bp->b_lblkno, &bp->b_blkno))
1380 return (error);
20aa076b 1381 if ((long)bp->b_blkno == -1)
e16fa59e 1382 clrbuf(bp);
e16fa59e 1383 }
20aa076b
KM
1384 if ((long)bp->b_blkno == -1) {
1385 biodone(bp);
e16fa59e 1386 return (0);
20aa076b 1387 }
e16fa59e
KM
1388 if (checkoverlap) {
1389 ebp = &buf[nbuf];
1390 start = bp->b_blkno;
1391 last = start + btodb(bp->b_bcount) - 1;
1392 for (ep = buf; ep < ebp; ep++) {
1393 if (ep == bp || (ep->b_flags & B_INVAL) ||
1394 ep->b_vp == (struct vnode *)0)
1395 continue;
1396 if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0))
1397 continue;
1398 if (vp != ip->i_devvp)
1399 continue;
1400 /* look for overlap */
1401 if (ep->b_bcount == 0 || ep->b_blkno > last ||
1402 ep->b_blkno + btodb(ep->b_bcount) <= start)
1403 continue;
20aa076b
KM
1404 vprint("Disk overlap", vp);
1405 printf("\tstart %d, end %d overlap start %d, end %d\n",
1406 start, last, ep->b_blkno,
1407 ep->b_blkno + btodb(ep->b_bcount) - 1);
e16fa59e
KM
1408 }
1409 }
1410 vp = ip->i_devvp;
1411 bp->b_dev = vp->v_rdev;
1412 (*(vp->v_op->vn_strategy))(bp);
7188ac27
KM
1413 return (0);
1414}
88a7a62a 1415
e16fa59e
KM
1416/*
1417 * Print out the contents of an inode.
1418 */
1419ufs_print(vp)
1420 struct vnode *vp;
1421{
1422 register struct inode *ip = VTOI(vp);
1423
a8f829ed
KM
1424 printf("tag VT_UFS, ino %d, on dev %d, %d", ip->i_number,
1425 major(ip->i_dev), minor(ip->i_dev));
1426#ifdef FIFO
1427 if (vp->v_type == VFIFO)
1428 fifo_printinfo(vp);
1429#endif /* FIFO */
1430 printf("%s\n", (ip->i_flag & ILOCKED) ? " (LOCKED)" : "");
96bae3e0
KM
1431 if (ip->i_spare0 == 0)
1432 return;
1433 printf("\towner pid %d", ip->i_spare0);
1434 if (ip->i_spare1)
1435 printf(" waiting pid %d", ip->i_spare1);
1436 printf("\n");
e16fa59e
KM
1437}
1438
24a31b70
KM
1439/*
1440 * Read wrapper for special devices.
1441 */
1442ufsspec_read(vp, uio, ioflag, cred)
1443 struct vnode *vp;
1444 struct uio *uio;
1445 int ioflag;
1446 struct ucred *cred;
1447{
1448
1449 /*
1450 * Set access flag.
1451 */
1452 VTOI(vp)->i_flag |= IACC;
1453 return (spec_read(vp, uio, ioflag, cred));
1454}
1455
1456/*
1457 * Write wrapper for special devices.
1458 */
1459ufsspec_write(vp, uio, ioflag, cred)
1460 struct vnode *vp;
1461 struct uio *uio;
1462 int ioflag;
1463 struct ucred *cred;
1464{
1465
1466 /*
1467 * Set update and change flags.
1468 */
1469 VTOI(vp)->i_flag |= IUPD|ICHG;
1470 return (spec_write(vp, uio, ioflag, cred));
1471}
1472
1473/*
1474 * Close wrapper for special devices.
1475 *
1476 * Update the times on the inode then do device close.
1477 */
1478ufsspec_close(vp, fflag, cred)
1479 struct vnode *vp;
1480 int fflag;
1481 struct ucred *cred;
1482{
1483 register struct inode *ip = VTOI(vp);
1484
de67eefc 1485 if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
24a31b70
KM
1486 ITIMES(ip, &time, &time);
1487 return (spec_close(vp, fflag, cred));
1488}
1489
d1c43d7f
KM
1490#ifdef FIFO
1491/*
1492 * Read wrapper for fifo's
1493 */
1494ufsfifo_read(vp, uio, ioflag, cred)
1495 struct vnode *vp;
1496 struct uio *uio;
1497 int ioflag;
1498 struct ucred *cred;
1499{
1500
1501 /*
1502 * Set access flag.
1503 */
1504 VTOI(vp)->i_flag |= IACC;
1505 return (fifo_read(vp, uio, ioflag, cred));
1506}
1507
1508/*
1509 * Write wrapper for fifo's.
1510 */
1511ufsfifo_write(vp, uio, ioflag, cred)
1512 struct vnode *vp;
1513 struct uio *uio;
1514 int ioflag;
1515 struct ucred *cred;
1516{
1517
1518 /*
1519 * Set update and change flags.
1520 */
1521 VTOI(vp)->i_flag |= IUPD|ICHG;
1522 return (fifo_write(vp, uio, ioflag, cred));
1523}
1524
1525/*
1526 * Close wrapper for fifo's.
1527 *
1528 * Update the times on the inode then do device close.
1529 */
1530ufsfifo_close(vp, fflag, cred)
1531 struct vnode *vp;
1532 int fflag;
1533 struct ucred *cred;
1534{
1535 register struct inode *ip = VTOI(vp);
1536
1537 if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
1538 ITIMES(ip, &time, &time);
1539 return (fifo_close(vp, fflag, cred));
1540}
1541#endif /* FIFO */
1542
7188ac27
KM
1543/*
1544 * Make a new file.
1545 */
1546maknode(mode, ndp, ipp)
1547 int mode;
1548 register struct nameidata *ndp;
1549 struct inode **ipp;
1550{
1551 register struct inode *ip;
1552 struct inode *tip;
1553 register struct inode *pdir = VTOI(ndp->ni_dvp);
1554 ino_t ipref;
1555 int error;
1556
1557 *ipp = 0;
1558 if ((mode & IFMT) == IFDIR)
1559 ipref = dirpref(pdir->i_fs);
1560 else
1561 ipref = pdir->i_number;
1562 error = ialloc(pdir, ipref, mode, &tip);
1563 if (error) {
1564 iput(pdir);
1565 return (error);
1566 }
1567 ip = tip;
1568#ifdef QUOTA
1569 if (ip->i_dquot != NODQUOT)
1570 panic("maknode: dquot");
1571#endif
1572 ip->i_flag |= IACC|IUPD|ICHG;
1573 if ((mode & IFMT) == 0)
1574 mode |= IFREG;
1575 ip->i_mode = mode;
1576 ITOV(ip)->v_type = IFTOVT(mode); /* Rest init'd in iget() */
1577 ip->i_nlink = 1;
1578 ip->i_uid = ndp->ni_cred->cr_uid;
1579 ip->i_gid = pdir->i_gid;
1580 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) &&
1581 suser(ndp->ni_cred, NULL))
1582 ip->i_mode &= ~ISGID;
1583#ifdef QUOTA
1584 ip->i_dquot = inoquota(ip);
1585#endif
1586
1587 /*
1588 * Make sure inode goes to disk before directory entry.
1589 */
1590 if ((error = iupdat(ip, &time, &time, 1)) ||
1591 (error = direnter(ip, ndp))) {
1592 /*
1593 * Write error occurred trying to update the inode
1594 * or the directory so must deallocate the inode.
1595 */
1596 ip->i_nlink = 0;
1597 ip->i_flag |= ICHG;
1598 iput(ip);
1599 return (error);
1600 }
1601 *ipp = ip;
1602 return (0);
88a7a62a 1603}