registerized vnodeop ops after vnode interface conversion
[unix-history] / usr / src / sys / ufs / ffs / ffs_vnops.c
CommitLineData
da7c5cc6 1/*
7188ac27
KM
2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3 * All rights reserved.
da7c5cc6 4 *
b702c21d 5 * %sccs.include.redist.c%
7188ac27 6 *
406c9a0d 7 * @(#)ffs_vnops.c 7.79 (Berkeley) %G%
a6673c09
KB
8 */
9
10#include <sys/param.h>
11#include <sys/systm.h>
a6673c09
KB
12#include <sys/resourcevar.h>
13#include <sys/kernel.h>
14#include <sys/file.h>
15#include <sys/stat.h>
16#include <sys/buf.h>
17#include <sys/proc.h>
18#include <sys/conf.h>
19#include <sys/mount.h>
20#include <sys/vnode.h>
21#include <sys/specdev.h>
22#include <sys/fifo.h>
23#include <sys/malloc.h>
24
80e9de4e
KM
25#include <vm/vm.h>
26
a6673c09
KB
27#include <ufs/ufs/lockf.h>
28#include <ufs/ufs/quota.h>
29#include <ufs/ufs/inode.h>
30#include <ufs/ufs/dir.h>
31#include <ufs/ufs/ufs_extern.h>
32
33#include <ufs/ffs/fs.h>
34#include <ufs/ffs/ffs_extern.h>
35
36/* Global vfs data structures for ufs. */
9342689a
JH
37int (**ffs_vnodeop_p)();
38struct vnodeopv_entry_desc ffs_vnodeop_entries[] = {
39 { &vop_default_desc, vn_default_error },
40 { &vop_lookup_desc, ufs_lookup }, /* lookup */
41 { &vop_create_desc, ufs_create }, /* create */
42 { &vop_mknod_desc, ufs_mknod }, /* mknod */
43 { &vop_open_desc, ufs_open }, /* open */
44 { &vop_close_desc, ufs_close }, /* close */
45 { &vop_access_desc, ufs_access }, /* access */
46 { &vop_getattr_desc, ufs_getattr }, /* getattr */
47 { &vop_setattr_desc, ufs_setattr }, /* setattr */
48 { &vop_read_desc, ffs_read }, /* read */
49 { &vop_write_desc, ffs_write }, /* write */
50 { &vop_ioctl_desc, ufs_ioctl }, /* ioctl */
51 { &vop_select_desc, ufs_select }, /* select */
52 { &vop_mmap_desc, ufs_mmap }, /* mmap */
53 { &vop_fsync_desc, ffs_fsync }, /* fsync */
54 { &vop_seek_desc, ufs_seek }, /* seek */
55 { &vop_remove_desc, ufs_remove }, /* remove */
56 { &vop_link_desc, ufs_link }, /* link */
57 { &vop_rename_desc, ufs_rename }, /* rename */
58 { &vop_mkdir_desc, ufs_mkdir }, /* mkdir */
59 { &vop_rmdir_desc, ufs_rmdir }, /* rmdir */
60 { &vop_symlink_desc, ufs_symlink }, /* symlink */
61 { &vop_readdir_desc, ufs_readdir }, /* readdir */
62 { &vop_readlink_desc, ufs_readlink }, /* readlink */
63 { &vop_abortop_desc, ufs_abortop }, /* abortop */
64 { &vop_inactive_desc, ffs_inactive }, /* inactive */
65 { &vop_reclaim_desc, ufs_reclaim }, /* reclaim */
66 { &vop_lock_desc, ufs_lock }, /* lock */
67 { &vop_unlock_desc, ufs_unlock }, /* unlock */
68 { &vop_bmap_desc, ffs_bmap }, /* bmap */
69 { &vop_strategy_desc, ufs_strategy }, /* strategy */
70 { &vop_print_desc, ufs_print }, /* print */
71 { &vop_islocked_desc, ufs_islocked }, /* islocked */
72 { &vop_advlock_desc, ufs_advlock }, /* advlock */
73 { &vop_blkatoff_desc, ffs_blkatoff }, /* blkatoff */
74 { &vop_vget_desc, ffs_vget }, /* vget */
75 { &vop_valloc_desc, ffs_valloc }, /* valloc */
76 { &vop_vfree_desc, ffs_vfree }, /* vfree */
77 { &vop_truncate_desc, ffs_truncate }, /* truncate */
78 { &vop_update_desc, ffs_update }, /* update */
36c34e72 79 { &vop_bwrite_desc, vn_bwrite },
9342689a 80 { (struct vnodeop_desc*)NULL, (int(*)())NULL }
a6673c09 81};
9342689a
JH
82struct vnodeopv_desc ffs_vnodeop_opv_desc =
83 { &ffs_vnodeop_p, ffs_vnodeop_entries };
528f664c 84
9342689a
JH
85int (**ffs_specop_p)();
86struct vnodeopv_entry_desc ffs_specop_entries[] = {
87 { &vop_default_desc, vn_default_error },
88 { &vop_lookup_desc, spec_lookup }, /* lookup */
89 { &vop_create_desc, spec_create }, /* create */
90 { &vop_mknod_desc, spec_mknod }, /* mknod */
91 { &vop_open_desc, spec_open }, /* open */
92 { &vop_close_desc, ufsspec_close }, /* close */
93 { &vop_access_desc, ufs_access }, /* access */
94 { &vop_getattr_desc, ufs_getattr }, /* getattr */
95 { &vop_setattr_desc, ufs_setattr }, /* setattr */
96 { &vop_read_desc, ufsspec_read }, /* read */
97 { &vop_write_desc, ufsspec_write }, /* write */
98 { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
99 { &vop_select_desc, spec_select }, /* select */
100 { &vop_mmap_desc, spec_mmap }, /* mmap */
101 { &vop_fsync_desc, spec_fsync }, /* fsync */
102 { &vop_seek_desc, spec_seek }, /* seek */
103 { &vop_remove_desc, spec_remove }, /* remove */
104 { &vop_link_desc, spec_link }, /* link */
105 { &vop_rename_desc, spec_rename }, /* rename */
106 { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
107 { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
108 { &vop_symlink_desc, spec_symlink }, /* symlink */
109 { &vop_readdir_desc, spec_readdir }, /* readdir */
110 { &vop_readlink_desc, spec_readlink }, /* readlink */
111 { &vop_abortop_desc, spec_abortop }, /* abortop */
112 { &vop_inactive_desc, ffs_inactive }, /* inactive */
113 { &vop_reclaim_desc, ufs_reclaim }, /* reclaim */
114 { &vop_lock_desc, ufs_lock }, /* lock */
115 { &vop_unlock_desc, ufs_unlock }, /* unlock */
116 { &vop_bmap_desc, spec_bmap }, /* bmap */
117 { &vop_strategy_desc, spec_strategy }, /* strategy */
118 { &vop_print_desc, ufs_print }, /* print */
119 { &vop_islocked_desc, ufs_islocked }, /* islocked */
120 { &vop_advlock_desc, spec_advlock }, /* advlock */
121 { &vop_blkatoff_desc, spec_blkatoff }, /* blkatoff */
122 { &vop_vget_desc, spec_vget }, /* vget */
123 { &vop_valloc_desc, spec_valloc }, /* valloc */
124 { &vop_vfree_desc, spec_vfree }, /* vfree */
125 { &vop_truncate_desc, spec_truncate }, /* truncate */
126 { &vop_update_desc, ffs_update }, /* update */
36c34e72 127 { &vop_bwrite_desc, vn_bwrite },
9342689a 128 { (struct vnodeop_desc*)NULL, (int(*)())NULL }
9d5097aa 129};
9342689a
JH
130struct vnodeopv_desc ffs_specop_opv_desc =
131 { &ffs_specop_p, ffs_specop_entries };
9d5097aa
KM
132
133#ifdef FIFO
9342689a
JH
134int (**ffs_fifoop_p)();
135struct vnodeopv_entry_desc ffs_fifoop_entries[] = {
136 { &vop_default_desc, vn_default_error },
137 { &vop_lookup_desc, fifo_lookup }, /* lookup */
138 { &vop_create_desc, fifo_create }, /* create */
139 { &vop_mknod_desc, fifo_mknod }, /* mknod */
140 { &vop_open_desc, fifo_open }, /* open */
141 { &vop_close_desc, ufsfifo_close }, /* close */
142 { &vop_access_desc, ufs_access }, /* access */
143 { &vop_getattr_desc, ufs_getattr }, /* getattr */
144 { &vop_setattr_desc, ufs_setattr }, /* setattr */
145 { &vop_read_desc, ufsfifo_read }, /* read */
146 { &vop_write_desc, ufsfifo_write }, /* write */
147 { &vop_ioctl_desc, fifo_ioctl }, /* ioctl */
148 { &vop_select_desc, fifo_select }, /* select */
149 { &vop_mmap_desc, fifo_mmap }, /* mmap */
150 { &vop_fsync_desc, fifo_fsync }, /* fsync */
151 { &vop_seek_desc, fifo_seek }, /* seek */
152 { &vop_remove_desc, fifo_remove }, /* remove */
153 { &vop_link_desc, fifo_link }, /* link */
154 { &vop_rename_desc, fifo_rename }, /* rename */
155 { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */
156 { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */
157 { &vop_symlink_desc, fifo_symlink }, /* symlink */
158 { &vop_readdir_desc, fifo_readdir }, /* readdir */
159 { &vop_readlink_desc, fifo_readlink }, /* readlink */
160 { &vop_abortop_desc, fifo_abortop }, /* abortop */
161 { &vop_inactive_desc, ffs_inactive }, /* inactive */
162 { &vop_reclaim_desc, ufs_reclaim }, /* reclaim */
163 { &vop_lock_desc, ufs_lock }, /* lock */
164 { &vop_unlock_desc, ufs_unlock }, /* unlock */
165 { &vop_bmap_desc, fifo_bmap }, /* bmap */
166 { &vop_strategy_desc, fifo_strategy }, /* strategy */
167 { &vop_print_desc, ufs_print }, /* print */
168 { &vop_islocked_desc, ufs_islocked }, /* islocked */
169 { &vop_advlock_desc, fifo_advlock }, /* advlock */
170 { &vop_blkatoff_desc, fifo_blkatoff }, /* blkatoff */
171 { &vop_vget_desc, fifo_vget }, /* vget */
172 { &vop_valloc_desc, fifo_valloc }, /* valloc */
173 { &vop_vfree_desc, fifo_vfree }, /* vfree */
174 { &vop_truncate_desc, fifo_truncate }, /* truncate */
175 { &vop_update_desc, ffs_update }, /* update */
36c34e72 176 { &vop_bwrite_desc, vn_bwrite },
9342689a 177 { (struct vnodeop_desc*)NULL, (int(*)())NULL }
9d5097aa 178};
9342689a
JH
179struct vnodeopv_desc ffs_fifoop_opv_desc =
180 { &ffs_fifoop_p, ffs_fifoop_entries };
9d5097aa
KM
181#endif /* FIFO */
182
d67a03eb 183
b373e060
KM
184/*
185 * Vnode op for reading.
186 */
187/* ARGSUSED */
9342689a
JH
188ffs_read (ap)
189 struct vop_read_args *ap;
b373e060 190{
e1b76915 191 register struct inode *ip = VTOI(ap->a_vp);
b373e060
KM
192 register struct fs *fs;
193 struct buf *bp;
194 daddr_t lbn, bn, rablock;
b7077901 195 off_t diff;
1ca49178
KM
196 int rasize, error = 0;
197 long size, n, on;
b373e060 198
5b169cb7 199#ifdef DIAGNOSTIC
b7077901 200 int type;
e1b76915 201 if (ap->a_uio->uio_rw != UIO_READ)
a6673c09 202 panic("ffs_read mode");
b373e060
KM
203 type = ip->i_mode & IFMT;
204 if (type != IFDIR && type != IFREG && type != IFLNK)
a6673c09 205 panic("ffs_read type");
5b169cb7 206#endif
e1b76915 207 if (ap->a_uio->uio_resid == 0)
b373e060 208 return (0);
e1b76915 209 if (ap->a_uio->uio_offset < 0)
b373e060
KM
210 return (EINVAL);
211 ip->i_flag |= IACC;
212 fs = ip->i_fs;
213 do {
e1b76915
JH
214 lbn = lblkno(fs, ap->a_uio->uio_offset);
215 on = blkoff(fs, ap->a_uio->uio_offset);
216 n = MIN((unsigned)(fs->fs_bsize - on), ap->a_uio->uio_resid);
217 diff = ip->i_size - ap->a_uio->uio_offset;
b373e060
KM
218 if (diff <= 0)
219 return (0);
220 if (diff < n)
221 n = diff;
b373e060 222 size = blksize(fs, ip, lbn);
e16fa59e 223 rablock = lbn + 1;
e1b76915 224 if (ap->a_vp->v_lastr + 1 == lbn &&
38f21582
KM
225 lblktosize(fs, rablock) < ip->i_size) {
226 rasize = blksize(fs, ip, rablock);
e1b76915 227 error = breadn(ap->a_vp, lbn, size, &rablock,
38f21582
KM
228 &rasize, 1, NOCRED, &bp);
229 } else
e1b76915
JH
230 error = bread(ap->a_vp, lbn, size, NOCRED, &bp);
231 ap->a_vp->v_lastr = lbn;
b373e060
KM
232 n = MIN(n, size - bp->b_resid);
233 if (error) {
234 brelse(bp);
235 return (error);
236 }
e1b76915
JH
237 error = uiomove(bp->b_un.b_addr + on, (int)n, ap->a_uio);
238 if (n + on == fs->fs_bsize || ap->a_uio->uio_offset == ip->i_size)
b373e060
KM
239 bp->b_flags |= B_AGE;
240 brelse(bp);
e1b76915 241 } while (error == 0 && ap->a_uio->uio_resid > 0 && n != 0);
b373e060
KM
242 return (error);
243}
244
245/*
246 * Vnode op for writing.
247 */
9342689a
JH
248ffs_write (ap)
249 struct vop_write_args *ap;
b373e060 250{
9342689a
JH
251 USES_VOP_TRUNCATE;
252 USES_VOP_UPDATE;
406c9a0d 253 register struct vnode *vp = ap->a_vp;
e1b76915 254 struct proc *p = ap->a_uio->uio_procp;
406c9a0d 255 register struct inode *ip = VTOI(vp);
b373e060
KM
256 register struct fs *fs;
257 struct buf *bp;
258 daddr_t lbn, bn;
0308fc84 259 off_t osize;
8986c97c
KM
260 int n, on, flags;
261 int size, resid, error = 0;
b373e060 262
5b169cb7 263#ifdef DIAGNOSTIC
e1b76915 264 if (ap->a_uio->uio_rw != UIO_WRITE)
a6673c09 265 panic("ffs_write mode");
5b169cb7 266#endif
406c9a0d 267 switch (vp->v_type) {
b373e060 268 case VREG:
e1b76915
JH
269 if (ap->a_ioflag & IO_APPEND)
270 ap->a_uio->uio_offset = ip->i_size;
b373e060
KM
271 /* fall through */
272 case VLNK:
273 break;
274
275 case VDIR:
e1b76915 276 if ((ap->a_ioflag & IO_SYNC) == 0)
a6673c09 277 panic("ffs_write nonsync dir write");
b373e060
KM
278 break;
279
280 default:
a6673c09 281 panic("ffs_write type");
b373e060 282 }
e1b76915 283 if (ap->a_uio->uio_offset < 0)
b373e060 284 return (EINVAL);
e1b76915 285 if (ap->a_uio->uio_resid == 0)
b373e060
KM
286 return (0);
287 /*
288 * Maybe this should be above the vnode op call, but so long as
289 * file servers have no limits, i don't think it matters
290 */
406c9a0d 291 if (vp->v_type == VREG && p &&
e1b76915 292 ap->a_uio->uio_offset + ap->a_uio->uio_resid >
c6f5111d
MK
293 p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
294 psignal(p, SIGXFSZ);
b373e060
KM
295 return (EFBIG);
296 }
e1b76915 297 resid = ap->a_uio->uio_resid;
b373e060
KM
298 osize = ip->i_size;
299 fs = ip->i_fs;
e16fa59e 300 flags = 0;
e1b76915 301 if (ap->a_ioflag & IO_SYNC)
e16fa59e 302 flags = B_SYNC;
b373e060 303 do {
e1b76915
JH
304 lbn = lblkno(fs, ap->a_uio->uio_offset);
305 on = blkoff(fs, ap->a_uio->uio_offset);
306 n = MIN((unsigned)(fs->fs_bsize - on), ap->a_uio->uio_resid);
b373e060 307 if (n < fs->fs_bsize)
e16fa59e 308 flags |= B_CLRBUF;
b373e060 309 else
e16fa59e 310 flags &= ~B_CLRBUF;
e1b76915 311 if (error = ffs_balloc(ip, lbn, on + n, ap->a_cred, &bp, flags))
b373e060 312 break;
e16fa59e 313 bn = bp->b_blkno;
e1b76915
JH
314 if (ap->a_uio->uio_offset + n > ip->i_size) {
315 ip->i_size = ap->a_uio->uio_offset + n;
406c9a0d 316 vnode_pager_setsize(vp, (u_long)ip->i_size);
8986c97c 317 }
b373e060 318 size = blksize(fs, ip, lbn);
406c9a0d 319 (void) vnode_pager_uncache(vp);
b373e060 320 n = MIN(n, size - bp->b_resid);
e1b76915
JH
321 error = uiomove(bp->b_un.b_addr + on, n, ap->a_uio);
322 if (ap->a_ioflag & IO_SYNC)
b373e060
KM
323 (void) bwrite(bp);
324 else if (n + on == fs->fs_bsize) {
325 bp->b_flags |= B_AGE;
326 bawrite(bp);
327 } else
328 bdwrite(bp);
329 ip->i_flag |= IUPD|ICHG;
e1b76915 330 if (ap->a_cred->cr_uid != 0)
b373e060 331 ip->i_mode &= ~(ISUID|ISGID);
e1b76915
JH
332 } while (error == 0 && ap->a_uio->uio_resid > 0 && n != 0);
333 if (error && (ap->a_ioflag & IO_UNIT)) {
406c9a0d 334 (void)VOP_TRUNCATE(vp, osize, ap->a_ioflag & IO_SYNC, ap->a_cred);
e1b76915
JH
335 ap->a_uio->uio_offset -= resid - ap->a_uio->uio_resid;
336 ap->a_uio->uio_resid = resid;
b373e060 337 }
e1b76915 338 if (!error && (ap->a_ioflag & IO_SYNC))
406c9a0d 339 error = VOP_UPDATE(vp, &time, &time, 1);
b373e060
KM
340 return (error);
341}
342
4f083fd7 343/*
7188ac27 344 * Synch an open file.
4f083fd7 345 */
7188ac27 346/* ARGSUSED */
a6673c09 347int
9342689a
JH
348ffs_fsync (ap)
349 struct vop_fsync_args *ap;
528f664c 350{
9342689a 351 USES_VOP_UPDATE;
e1b76915 352 struct inode *ip = VTOI(ap->a_vp);
7188ac27 353
e1b76915 354 if (ap->a_fflags & FWRITE)
7188ac27 355 ip->i_flag |= ICHG;
e1b76915
JH
356 vflushbuf(ap->a_vp, ap->a_waitfor == MNT_WAIT ? B_SYNC : 0);
357 return (VOP_UPDATE(ap->a_vp, &time, &time, ap->a_waitfor == MNT_WAIT));
9d5097aa
KM
358}
359
360/*
361 * Last reference to an inode, write the inode out and if necessary,
362 * truncate and deallocate the file.
363 */
364int
9342689a
JH
365ffs_inactive (ap)
366 struct vop_inactive_args *ap;
9d5097aa 367{
9342689a
JH
368 USES_VOP_TRUNCATE;
369 USES_VOP_UPDATE;
370 USES_VOP_VFREE;
9d5097aa
KM
371 register struct inode *ip;
372 int mode, error;
373 extern int prtactive;
374
e1b76915
JH
375 if (prtactive && ap->a_vp->v_usecount != 0)
376 vprint("ffs_inactive: pushing active", ap->a_vp);
9d5097aa
KM
377
378 /* Get rid of inodes related to stale file handles. */
e1b76915 379 ip = VTOI(ap->a_vp);
9d5097aa 380 if (ip->i_mode == 0) {
e1b76915
JH
381 if ((ap->a_vp->v_flag & VXLOCK) == 0)
382 vgone(ap->a_vp);
9d5097aa
KM
383 return (0);
384 }
385
386 error = 0;
387 ILOCK(ip);
e1b76915 388 if (ip->i_nlink <= 0 && (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
9d5097aa
KM
389#ifdef QUOTA
390 if (!getinoquota(ip))
391 (void)chkiq(ip, -1, NOCRED, 0);
392#endif
e1b76915 393 error = VOP_TRUNCATE(ap->a_vp, (off_t)0, 0, NOCRED);
9d5097aa
KM
394 mode = ip->i_mode;
395 ip->i_mode = 0;
396 ip->i_rdev = 0;
397 ip->i_flag |= IUPD|ICHG;
e1b76915 398 VOP_VFREE(ap->a_vp, ip->i_number, mode);
9d5097aa
KM
399 }
400 if (ip->i_flag&(IUPD|IACC|ICHG|IMOD))
e1b76915 401 VOP_UPDATE(ap->a_vp, &time, &time, 0);
9d5097aa
KM
402 IUNLOCK(ip);
403 ip->i_flag = 0;
404 /*
405 * If we are done with the inode, reclaim it
406 * so that it can be reused immediately.
407 */
e1b76915
JH
408 if (ap->a_vp->v_usecount == 0 && ip->i_mode == 0)
409 vgone(ap->a_vp);
9d5097aa 410 return (error);
4f083fd7 411}