xref: /original-bsd/sys/ufs/ffs/ffs_vnops.c (revision ea3a8ee8)
1 /*
2  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)ffs_vnops.c	7.99 (Berkeley) 01/03/93
8  */
9 
10 #include <sys/param.h>
11 #include <sys/systm.h>
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/malloc.h>
22 
23 #include <vm/vm.h>
24 
25 #include <miscfs/specfs/specdev.h>
26 #include <miscfs/fifofs/fifo.h>
27 
28 #include <ufs/ufs/lockf.h>
29 #include <ufs/ufs/quota.h>
30 #include <ufs/ufs/inode.h>
31 #include <ufs/ufs/dir.h>
32 #include <ufs/ufs/ufs_extern.h>
33 
34 #include <ufs/ffs/fs.h>
35 #include <ufs/ffs/ffs_extern.h>
36 
37 /* Global vfs data structures for ufs. */
38 int (**ffs_vnodeop_p)();
39 struct vnodeopv_entry_desc ffs_vnodeop_entries[] = {
40 	{ &vop_default_desc, vn_default_error },
41 	{ &vop_lookup_desc, ufs_lookup },		/* lookup */
42 	{ &vop_create_desc, ufs_create },		/* create */
43 	{ &vop_mknod_desc, ufs_mknod },			/* mknod */
44 	{ &vop_open_desc, ufs_open },			/* open */
45 	{ &vop_close_desc, ufs_close },			/* close */
46 	{ &vop_access_desc, ufs_access },		/* access */
47 	{ &vop_getattr_desc, ufs_getattr },		/* getattr */
48 	{ &vop_setattr_desc, ufs_setattr },		/* setattr */
49 	{ &vop_read_desc, ffs_read },			/* read */
50 	{ &vop_write_desc, ffs_write },			/* write */
51 	{ &vop_ioctl_desc, ufs_ioctl },			/* ioctl */
52 	{ &vop_select_desc, ufs_select },		/* select */
53 	{ &vop_mmap_desc, ufs_mmap },			/* mmap */
54 	{ &vop_fsync_desc, ffs_fsync },			/* fsync */
55 	{ &vop_seek_desc, ufs_seek },			/* seek */
56 	{ &vop_remove_desc, ufs_remove },		/* remove */
57 	{ &vop_link_desc, ufs_link },			/* link */
58 	{ &vop_rename_desc, ufs_rename },		/* rename */
59 	{ &vop_mkdir_desc, ufs_mkdir },			/* mkdir */
60 	{ &vop_rmdir_desc, ufs_rmdir },			/* rmdir */
61 	{ &vop_symlink_desc, ufs_symlink },		/* symlink */
62 	{ &vop_readdir_desc, ufs_readdir },		/* readdir */
63 	{ &vop_readlink_desc, ufs_readlink },		/* readlink */
64 	{ &vop_abortop_desc, ufs_abortop },		/* abortop */
65 	{ &vop_inactive_desc, ufs_inactive },		/* inactive */
66 	{ &vop_reclaim_desc, ufs_reclaim },		/* reclaim */
67 	{ &vop_lock_desc, ufs_lock },			/* lock */
68 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
69 	{ &vop_bmap_desc, ufs_bmap },			/* bmap */
70 	{ &vop_strategy_desc, ufs_strategy },		/* strategy */
71 	{ &vop_print_desc, ufs_print },			/* print */
72 	{ &vop_islocked_desc, ufs_islocked },		/* islocked */
73 	{ &vop_advlock_desc, ufs_advlock },		/* advlock */
74 	{ &vop_blkatoff_desc, ffs_blkatoff },		/* blkatoff */
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 */
79 	{ &vop_bwrite_desc, vn_bwrite },
80 	{ (struct vnodeop_desc*)NULL, (int(*)())NULL }
81 };
82 struct vnodeopv_desc ffs_vnodeop_opv_desc =
83 	{ &ffs_vnodeop_p, ffs_vnodeop_entries };
84 
85 int (**ffs_specop_p)();
86 struct 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, ffs_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, ufs_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_valloc_desc, spec_valloc },		/* valloc */
123 	{ &vop_vfree_desc, ffs_vfree },			/* vfree */
124 	{ &vop_truncate_desc, spec_truncate },		/* truncate */
125 	{ &vop_update_desc, ffs_update },		/* update */
126 	{ &vop_bwrite_desc, vn_bwrite },
127 	{ (struct vnodeop_desc*)NULL, (int(*)())NULL }
128 };
129 struct vnodeopv_desc ffs_specop_opv_desc =
130 	{ &ffs_specop_p, ffs_specop_entries };
131 
132 #ifdef FIFO
133 int (**ffs_fifoop_p)();
134 struct vnodeopv_entry_desc ffs_fifoop_entries[] = {
135 	{ &vop_default_desc, vn_default_error },
136 	{ &vop_lookup_desc, fifo_lookup },		/* lookup */
137 	{ &vop_create_desc, fifo_create },		/* create */
138 	{ &vop_mknod_desc, fifo_mknod },		/* mknod */
139 	{ &vop_open_desc, fifo_open },			/* open */
140 	{ &vop_close_desc, ufsfifo_close },		/* close */
141 	{ &vop_access_desc, ufs_access },		/* access */
142 	{ &vop_getattr_desc, ufs_getattr },		/* getattr */
143 	{ &vop_setattr_desc, ufs_setattr },		/* setattr */
144 	{ &vop_read_desc, ufsfifo_read },		/* read */
145 	{ &vop_write_desc, ufsfifo_write },		/* write */
146 	{ &vop_ioctl_desc, fifo_ioctl },		/* ioctl */
147 	{ &vop_select_desc, fifo_select },		/* select */
148 	{ &vop_mmap_desc, fifo_mmap },			/* mmap */
149 	{ &vop_fsync_desc, ffs_fsync },			/* fsync */
150 	{ &vop_seek_desc, fifo_seek },			/* seek */
151 	{ &vop_remove_desc, fifo_remove },		/* remove */
152 	{ &vop_link_desc, fifo_link },			/* link */
153 	{ &vop_rename_desc, fifo_rename },		/* rename */
154 	{ &vop_mkdir_desc, fifo_mkdir },		/* mkdir */
155 	{ &vop_rmdir_desc, fifo_rmdir },		/* rmdir */
156 	{ &vop_symlink_desc, fifo_symlink },		/* symlink */
157 	{ &vop_readdir_desc, fifo_readdir },		/* readdir */
158 	{ &vop_readlink_desc, fifo_readlink },		/* readlink */
159 	{ &vop_abortop_desc, fifo_abortop },		/* abortop */
160 	{ &vop_inactive_desc, ufs_inactive },		/* inactive */
161 	{ &vop_reclaim_desc, ufs_reclaim },		/* reclaim */
162 	{ &vop_lock_desc, ufs_lock },			/* lock */
163 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
164 	{ &vop_bmap_desc, fifo_bmap },			/* bmap */
165 	{ &vop_strategy_desc, fifo_strategy },		/* strategy */
166 	{ &vop_print_desc, ufs_print },			/* print */
167 	{ &vop_islocked_desc, ufs_islocked },		/* islocked */
168 	{ &vop_advlock_desc, fifo_advlock },		/* advlock */
169 	{ &vop_blkatoff_desc, fifo_blkatoff },		/* blkatoff */
170 	{ &vop_valloc_desc, fifo_valloc },		/* valloc */
171 	{ &vop_vfree_desc, ffs_vfree },			/* vfree */
172 	{ &vop_truncate_desc, fifo_truncate },		/* truncate */
173 	{ &vop_update_desc, ffs_update },		/* update */
174 	{ &vop_bwrite_desc, vn_bwrite },
175 	{ (struct vnodeop_desc*)NULL, (int(*)())NULL }
176 };
177 struct vnodeopv_desc ffs_fifoop_opv_desc =
178 	{ &ffs_fifoop_p, ffs_fifoop_entries };
179 #endif /* FIFO */
180 
181 /*
182  * Enabling cluster read/write operations.
183  * Default is off until we trust them.
184  */
185 int doclusterread = 0;
186 int doclusterwrite = 0;
187 
188 /*
189  * Vnode op for reading.
190  */
191 /* ARGSUSED */
192 ffs_read(ap)
193 	struct vop_read_args /* {
194 		struct vnode *a_vp;
195 		struct uio *a_uio;
196 		int a_ioflag;
197 		struct ucred *a_cred;
198 	} */ *ap;
199 {
200 	register struct vnode *vp = ap->a_vp;
201 	register struct inode *ip = VTOI(vp);
202 	register struct uio *uio = ap->a_uio;
203 	register struct fs *fs;
204 	struct buf *bp;
205 	daddr_t lbn, bn, rablock;
206 	off_t diff;
207 	int type, rasize, error = 0;
208 	long size, n, on;
209 
210 	type = ip->i_mode & IFMT;
211 #ifdef DIAGNOSTIC
212 	if (uio->uio_rw != UIO_READ)
213 		panic("ffs_read mode");
214 	if (type != IFDIR && type != IFREG && type != IFLNK)
215 		panic("ffs_read type");
216 	if (type == IFLNK && (int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
217 		panic("read short symlink");
218 #endif
219 	if (uio->uio_resid == 0)
220 		return (0);
221 	fs = ip->i_fs;
222 	if ((u_quad_t)uio->uio_offset > fs->fs_maxfilesize)
223 		return (EFBIG);
224 	ip->i_flag |= IACC;
225 	do {
226 		lbn = lblkno(fs, uio->uio_offset);
227 		on = blkoff(fs, uio->uio_offset);
228 		n = min((unsigned)(fs->fs_bsize - on), uio->uio_resid);
229 		diff = ip->i_size - uio->uio_offset;
230 		if (diff <= 0)
231 			return (0);
232 		if (diff < n)
233 			n = diff;
234 		size = blksize(fs, ip, lbn);
235 		rablock = lbn + 1;
236 		if (doclusterread && lblktosize(fs, rablock) <= ip->i_size) {
237 			error = cluster_read(vp, ip->i_size, lbn, size,
238 			    NOCRED, &bp);
239 		} else if (vp->v_lastr + 1 == lbn &&
240 		    lblktosize(fs, rablock) < ip->i_size) {
241 			rasize = blksize(fs, ip, rablock);
242 			error = breadn(vp, lbn, size, &rablock,
243 				&rasize, 1, NOCRED, &bp);
244 		} else
245 			error = bread(vp, lbn, size, NOCRED, &bp);
246 		vp->v_lastr = lbn;
247 		n = min(n, size - bp->b_resid);
248 		if (error) {
249 			brelse(bp);
250 			return (error);
251 		}
252 		error = uiomove(bp->b_un.b_addr + on, (int)n, uio);
253 		if (type == IFREG &&
254 		    (n + on == fs->fs_bsize || uio->uio_offset == ip->i_size))
255 			bp->b_flags |= B_AGE;
256 		brelse(bp);
257 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
258 	return (error);
259 }
260 
261 /*
262  * Vnode op for writing.
263  */
264 ffs_write(ap)
265 	struct vop_write_args /* {
266 		struct vnode *a_vp;
267 		struct uio *a_uio;
268 		int a_ioflag;
269 		struct ucred *a_cred;
270 	} */ *ap;
271 {
272 	register struct vnode *vp = ap->a_vp;
273 	register struct uio *uio = ap->a_uio;
274 	register struct inode *ip = VTOI(vp);
275 	register struct fs *fs;
276 	struct proc *p = uio->uio_procp;
277 	int ioflag = ap->a_ioflag;
278 	struct timeval tv;
279 	struct buf *bp;
280 	daddr_t lbn, bn;
281 	off_t osize;
282 	int n, on, flags;
283 	int size, resid, error = 0;
284 
285 #ifdef DIAGNOSTIC
286 	if (uio->uio_rw != UIO_WRITE)
287 		panic("ffs_write mode");
288 #endif
289 	switch (vp->v_type) {
290 	case VREG:
291 		if (ioflag & IO_APPEND)
292 			uio->uio_offset = ip->i_size;
293 		/* fall through */
294 	case VLNK:
295 		break;
296 
297 	case VDIR:
298 		if ((ioflag & IO_SYNC) == 0)
299 			panic("ffs_write nonsync dir write");
300 		break;
301 
302 	default:
303 		panic("ffs_write type");
304 	}
305 	if (uio->uio_resid == 0)
306 		return (0);
307 	fs = ip->i_fs;
308 	if (uio->uio_offset < 0 ||
309 	    (u_quad_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize)
310 		return (EFBIG);
311 	/*
312 	 * Maybe this should be above the vnode op call, but so long as
313 	 * file servers have no limits, i don't think it matters
314 	 */
315 	if (vp->v_type == VREG && p &&
316 	    uio->uio_offset + uio->uio_resid >
317 	      p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
318 		psignal(p, SIGXFSZ);
319 		return (EFBIG);
320 	}
321 	resid = uio->uio_resid;
322 	osize = ip->i_size;
323 	flags = 0;
324 	if (ioflag & IO_SYNC)
325 		flags = B_SYNC;
326 	do {
327 		lbn = lblkno(fs, uio->uio_offset);
328 		on = blkoff(fs, uio->uio_offset);
329 		n = min((unsigned)(fs->fs_bsize - on), uio->uio_resid);
330 		if (n < fs->fs_bsize)
331 			flags |= B_CLRBUF;
332 		else
333 			flags &= ~B_CLRBUF;
334 		if (error = ffs_balloc(ip, lbn, on + n, ap->a_cred, &bp, flags))
335 			break;
336 		bn = bp->b_blkno;
337 		if (uio->uio_offset + n > ip->i_size) {
338 			ip->i_size = uio->uio_offset + n;
339 			vnode_pager_setsize(vp, (u_long)ip->i_size);
340 		}
341 		size = blksize(fs, ip, lbn);
342 		(void) vnode_pager_uncache(vp);
343 		n = min(n, size - bp->b_resid);
344 		error = uiomove(bp->b_un.b_addr + on, n, uio);
345 		if (ioflag & IO_SYNC)
346 			(void) bwrite(bp);
347 		else if (n + on == fs->fs_bsize) {
348 			if (doclusterwrite) {
349 				cluster_write(bp, ip->i_size);
350 			} else {
351 				bp->b_flags |= B_AGE;
352 				bawrite(bp);
353 			}
354 		} else
355 			bdwrite(bp);
356 		ip->i_flag |= IUPD|ICHG;
357 		if (ap->a_cred && ap->a_cred->cr_uid != 0)
358 			ip->i_mode &= ~(ISUID|ISGID);
359 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
360 	if (error && (ioflag & IO_UNIT)) {
361 		(void)VOP_TRUNCATE(vp, osize, ioflag & IO_SYNC, ap->a_cred,
362 		    uio->uio_procp);
363 		uio->uio_offset -= resid - uio->uio_resid;
364 		uio->uio_resid = resid;
365 	}
366 	if (!error && (ioflag & IO_SYNC)) {
367 		tv = time;
368 		error = VOP_UPDATE(vp, &tv, &tv, 1);
369 	}
370 	return (error);
371 }
372 
373 /*
374  * Synch an open file.
375  */
376 /* ARGSUSED */
377 int
378 ffs_fsync(ap)
379 	struct vop_fsync_args /* {
380 		struct vnode *a_vp;
381 		struct ucred *a_cred;
382 		int a_waitfor;
383 		struct proc *a_p;
384 	} */ *ap;
385 {
386 	register struct vnode *vp = ap->a_vp;
387 	struct inode *ip = VTOI(vp);
388 	register struct buf *bp;
389 	struct timeval tv;
390 	struct buf *nbp;
391 	int s;
392 
393 	/*
394 	 * Flush all dirty buffers associated with a vnode.
395 	 */
396 loop:
397 	s = splbio();
398 	for (bp = vp->v_dirtyblkhd.le_next; bp; bp = nbp) {
399 		nbp = bp->b_vnbufs.qe_next;
400 		if ((bp->b_flags & B_BUSY))
401 			continue;
402 		if ((bp->b_flags & B_DELWRI) == 0)
403 			panic("ffs_fsync: not dirty");
404 		bremfree(bp);
405 		bp->b_flags |= B_BUSY;
406 		splx(s);
407 		/*
408 		 * Wait for I/O associated with indirect blocks to complete,
409 		 * since there is no way to quickly wait for them below.
410 		 */
411 		if (bp->b_vp == vp || ap->a_waitfor == MNT_NOWAIT)
412 			(void) bawrite(bp);
413 		else
414 			(void) bwrite(bp);
415 		goto loop;
416 	}
417 	if (ap->a_waitfor == MNT_WAIT) {
418 		while (vp->v_numoutput) {
419 			vp->v_flag |= VBWAIT;
420 			sleep((caddr_t)&vp->v_numoutput, PRIBIO + 1);
421 		}
422 #ifdef DIAGNOSTIC
423 		if (vp->v_dirtyblkhd.le_next) {
424 			vprint("ffs_fsync: dirty", vp);
425 			goto loop;
426 		}
427 #endif
428 	}
429 	splx(s);
430 	tv = time;
431 	return (VOP_UPDATE(ap->a_vp, &tv, &tv, ap->a_waitfor == MNT_WAIT));
432 }
433 
434