xref: /original-bsd/sys/ufs/ffs/ffs_vnops.c (revision 753853ba)
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.71 (Berkeley) 03/26/92
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/specdev.h>
22 #include <sys/fifo.h>
23 #include <sys/malloc.h>
24 
25 #include <ufs/ufs/lockf.h>
26 #include <ufs/ufs/quota.h>
27 #include <ufs/ufs/inode.h>
28 #include <ufs/ufs/dir.h>
29 #include <ufs/ufs/ufs_extern.h>
30 
31 #include <ufs/ffs/fs.h>
32 #include <ufs/ffs/ffs_extern.h>
33 
34 /* Global vfs data structures for ufs. */
35 struct vnodeops ffs_vnodeops = {
36 	ufs_lookup,		/* lookup */
37 	ufs_create,		/* create */
38 	ufs_mknod,		/* mknod */
39 	ufs_open,		/* open */
40 	ufs_close,		/* close */
41 	ufs_access,		/* access */
42 	ufs_getattr,		/* getattr */
43 	ufs_setattr,		/* setattr */
44 	ffs_read,		/* read */
45 	ffs_write,		/* write */
46 	ufs_ioctl,		/* ioctl */
47 	ufs_select,		/* select */
48 	ufs_mmap,		/* mmap */
49 	ffs_fsync,		/* fsync */
50 	ufs_seek,		/* seek */
51 	ufs_remove,		/* remove */
52 	ufs_link,		/* link */
53 	ufs_rename,		/* rename */
54 	ufs_mkdir,		/* mkdir */
55 	ufs_rmdir,		/* rmdir */
56 	ufs_symlink,		/* symlink */
57 	ufs_readdir,		/* readdir */
58 	ufs_readlink,		/* readlink */
59 	ufs_abortop,		/* abortop */
60 	ffs_inactive,		/* inactive */
61 	ufs_reclaim,		/* reclaim */
62 	ufs_lock,		/* lock */
63 	ufs_unlock,		/* unlock */
64 	ffs_bmap,		/* bmap */
65 	ufs_strategy,		/* strategy */
66 	ufs_print,		/* print */
67 	ufs_islocked,		/* islocked */
68 	ufs_advlock,		/* advlock */
69 	ffs_blkatoff,		/* blkatoff */
70 	ffs_vget,		/* vget */
71 	ffs_valloc,		/* valloc */
72 	ffs_vfree,		/* vfree */
73 	ffs_truncate,		/* truncate */
74 	ffs_update,		/* update */
75 	bwrite,			/* bwrite */
76 };
77 
78 struct vnodeops ffs_specops = {
79 	spec_lookup,		/* lookup */
80 	spec_create,		/* create */
81 	spec_mknod,		/* mknod */
82 	spec_open,		/* open */
83 	ufsspec_close,		/* close */
84 	ufs_access,		/* access */
85 	ufs_getattr,		/* getattr */
86 	ufs_setattr,		/* setattr */
87 	ufsspec_read,		/* read */
88 	ufsspec_write,		/* write */
89 	spec_ioctl,		/* ioctl */
90 	spec_select,		/* select */
91 	spec_mmap,		/* mmap */
92 	spec_fsync,		/* fsync */
93 	spec_seek,		/* seek */
94 	spec_remove,		/* remove */
95 	spec_link,		/* link */
96 	spec_rename,		/* rename */
97 	spec_mkdir,		/* mkdir */
98 	spec_rmdir,		/* rmdir */
99 	spec_symlink,		/* symlink */
100 	spec_readdir,		/* readdir */
101 	spec_readlink,		/* readlink */
102 	spec_abortop,		/* abortop */
103 	ffs_inactive,		/* inactive */
104 	ufs_reclaim,		/* reclaim */
105 	ufs_lock,		/* lock */
106 	ufs_unlock,		/* unlock */
107 	spec_bmap,		/* bmap */
108 	spec_strategy,		/* strategy */
109 	ufs_print,		/* print */
110 	ufs_islocked,		/* islocked */
111 	spec_advlock,		/* advlock */
112 	spec_blkatoff,		/* blkatoff */
113 	spec_vget,		/* vget */
114 	spec_valloc,		/* valloc */
115 	spec_vfree,		/* vfree */
116 	spec_truncate,		/* truncate */
117 	ffs_update,		/* update */
118 	bwrite,			/* bwrite */
119 };
120 
121 #ifdef FIFO
122 struct vnodeops ffs_fifoops = {
123 	fifo_lookup,		/* lookup */
124 	fifo_create,		/* create */
125 	fifo_mknod,		/* mknod */
126 	fifo_open,		/* open */
127 	ufsfifo_close,		/* close */
128 	ufs_access,		/* access */
129 	ufs_getattr,		/* getattr */
130 	ufs_setattr,		/* setattr */
131 	ufsfifo_read,		/* read */
132 	ufsfifo_write,		/* write */
133 	fifo_ioctl,		/* ioctl */
134 	fifo_select,		/* select */
135 	fifo_mmap,		/* mmap */
136 	fifo_fsync,		/* fsync */
137 	fifo_seek,		/* seek */
138 	fifo_remove,		/* remove */
139 	fifo_link,		/* link */
140 	fifo_rename,		/* rename */
141 	fifo_mkdir,		/* mkdir */
142 	fifo_rmdir,		/* rmdir */
143 	fifo_symlink,		/* symlink */
144 	fifo_readdir,		/* readdir */
145 	fifo_readlink,		/* readlink */
146 	fifo_abortop,		/* abortop */
147 	ffs_inactive,		/* inactive */
148 	ufs_reclaim,		/* reclaim */
149 	ufs_lock,		/* lock */
150 	ufs_unlock,		/* unlock */
151 	fifo_bmap,		/* bmap */
152 	fifo_strategy,		/* strategy */
153 	ufs_print,		/* print */
154 	ufs_islocked,		/* islocked */
155 	fifo_advlock,		/* advlock */
156 	fifo_blkatoff,		/* blkatoff */
157 	fifo_vget,		/* vget */
158 	fifo_valloc,		/* valloc */
159 	fifo_vfree,		/* vfree */
160 	fifo_truncate,		/* truncate */
161 	ffs_update,		/* update */
162 	bwrite,			/* bwrite */
163 };
164 #endif /* FIFO */
165 
166 
167 /*
168  * Vnode op for reading.
169  */
170 /* ARGSUSED */
171 ffs_read(vp, uio, ioflag, cred)
172 	struct vnode *vp;
173 	register struct uio *uio;
174 	int ioflag;
175 	struct ucred *cred;
176 {
177 	register struct inode *ip = VTOI(vp);
178 	register struct fs *fs;
179 	struct buf *bp;
180 	daddr_t lbn, bn, rablock;
181 	off_t diff;
182 	int error = 0;
183 	long size, rasize, n, on;
184 
185 #ifdef DIAGNOSTIC
186 	int type;
187 	if (uio->uio_rw != UIO_READ)
188 		panic("ffs_read mode");
189 	type = ip->i_mode & IFMT;
190 	if (type != IFDIR && type != IFREG && type != IFLNK)
191 		panic("ffs_read type");
192 #endif
193 	if (uio->uio_resid == 0)
194 		return (0);
195 	if (uio->uio_offset < 0)
196 		return (EINVAL);
197 	ip->i_flag |= IACC;
198 	fs = ip->i_fs;
199 	do {
200 		lbn = lblkno(fs, uio->uio_offset);
201 		on = blkoff(fs, uio->uio_offset);
202 		n = MIN((unsigned)(fs->fs_bsize - on), uio->uio_resid);
203 		diff = ip->i_size - uio->uio_offset;
204 		if (diff <= 0)
205 			return (0);
206 		if (diff < n)
207 			n = diff;
208 		size = blksize(fs, ip, lbn);
209 		rablock = lbn + 1;
210 		if (vp->v_lastr + 1 == lbn &&
211 		    lblktosize(fs, rablock) < ip->i_size) {
212 			rasize = blksize(fs, ip, rablock);
213 			error = breadn(vp, lbn, size, &rablock,
214 				&rasize, 1, NOCRED, &bp);
215 		} else
216 			error = bread(vp, lbn, size, NOCRED, &bp);
217 		vp->v_lastr = lbn;
218 		n = MIN(n, size - bp->b_resid);
219 		if (error) {
220 			brelse(bp);
221 			return (error);
222 		}
223 		error = uiomove(bp->b_un.b_addr + on, (int)n, uio);
224 		if (n + on == fs->fs_bsize || uio->uio_offset == ip->i_size)
225 			bp->b_flags |= B_AGE;
226 		brelse(bp);
227 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
228 	return (error);
229 }
230 
231 /*
232  * Vnode op for writing.
233  */
234 ffs_write(vp, uio, ioflag, cred)
235 	register struct vnode *vp;
236 	struct uio *uio;
237 	int ioflag;
238 	struct ucred *cred;
239 {
240 	struct proc *p = uio->uio_procp;
241 	register struct inode *ip = VTOI(vp);
242 	register struct fs *fs;
243 	struct buf *bp;
244 	daddr_t lbn, bn;
245 	u_long osize;
246 	int n, on, flags;
247 	int size, resid, error = 0;
248 
249 #ifdef DIAGNOSTIC
250 	if (uio->uio_rw != UIO_WRITE)
251 		panic("ffs_write mode");
252 #endif
253 	switch (vp->v_type) {
254 	case VREG:
255 		if (ioflag & IO_APPEND)
256 			uio->uio_offset = ip->i_size;
257 		/* fall through */
258 	case VLNK:
259 		break;
260 
261 	case VDIR:
262 		if ((ioflag & IO_SYNC) == 0)
263 			panic("ffs_write nonsync dir write");
264 		break;
265 
266 	default:
267 		panic("ffs_write type");
268 	}
269 	if (uio->uio_offset < 0)
270 		return (EINVAL);
271 	if (uio->uio_resid == 0)
272 		return (0);
273 	/*
274 	 * Maybe this should be above the vnode op call, but so long as
275 	 * file servers have no limits, i don't think it matters
276 	 */
277 	if (vp->v_type == VREG && p &&
278 	    uio->uio_offset + uio->uio_resid >
279 	      p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
280 		psignal(p, SIGXFSZ);
281 		return (EFBIG);
282 	}
283 	resid = uio->uio_resid;
284 	osize = ip->i_size;
285 	fs = ip->i_fs;
286 	flags = 0;
287 	if (ioflag & IO_SYNC)
288 		flags = B_SYNC;
289 	do {
290 		lbn = lblkno(fs, uio->uio_offset);
291 		on = blkoff(fs, uio->uio_offset);
292 		n = MIN((unsigned)(fs->fs_bsize - on), uio->uio_resid);
293 		if (n < fs->fs_bsize)
294 			flags |= B_CLRBUF;
295 		else
296 			flags &= ~B_CLRBUF;
297 		if (error = ffs_balloc(ip, lbn, (int)(on + n), &bp, flags))
298 			break;
299 		bn = bp->b_blkno;
300 		if (uio->uio_offset + n > ip->i_size) {
301 			ip->i_size = uio->uio_offset + n;
302 			vnode_pager_setsize(vp, (u_long)ip->i_size);
303 		}
304 		size = blksize(fs, ip, lbn);
305 		(void) vnode_pager_uncache(vp);
306 		n = MIN(n, size - bp->b_resid);
307 		error = uiomove(bp->b_un.b_addr + on, n, uio);
308 		if (ioflag & IO_SYNC)
309 			(void) bwrite(bp);
310 		else if (n + on == fs->fs_bsize) {
311 			bp->b_flags |= B_AGE;
312 			bawrite(bp);
313 		} else
314 			bdwrite(bp);
315 		ip->i_flag |= IUPD|ICHG;
316 		if (cred->cr_uid != 0)
317 			ip->i_mode &= ~(ISUID|ISGID);
318 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
319 	if (error && (ioflag & IO_UNIT)) {
320 		(void)ffs_truncate(vp, osize, ioflag & IO_SYNC);
321 		uio->uio_offset -= resid - uio->uio_resid;
322 		uio->uio_resid = resid;
323 	}
324 	if (!error && (ioflag & IO_SYNC))
325 		error = ffs_update(vp, &time, &time, 1);
326 	return (error);
327 }
328 
329 /*
330  * Synch an open file.
331  */
332 /* ARGSUSED */
333 int
334 ffs_fsync(vp, fflags, cred, waitfor, p)
335 	struct vnode *vp;
336 	int fflags;
337 	struct ucred *cred;
338 	int waitfor;
339 	struct proc *p;
340 {
341 	struct inode *ip = VTOI(vp);
342 
343 	if (fflags & FWRITE)
344 		ip->i_flag |= ICHG;
345 	vflushbuf(vp, waitfor == MNT_WAIT ? B_SYNC : 0);
346 	return (ffs_update(vp, &time, &time, waitfor == MNT_WAIT));
347 }
348 
349 /*
350  * Last reference to an inode, write the inode out and if necessary,
351  * truncate and deallocate the file.
352  */
353 int
354 ffs_inactive(vp, p)
355 	struct vnode *vp;
356 	struct proc *p;
357 {
358 	register struct inode *ip;
359 	int mode, error;
360 	extern int prtactive;
361 
362 	if (prtactive && vp->v_usecount != 0)
363 		vprint("ffs_inactive: pushing active", vp);
364 
365 	/* Get rid of inodes related to stale file handles. */
366 	ip = VTOI(vp);
367 	if (ip->i_mode == 0) {
368 		if ((vp->v_flag & VXLOCK) == 0)
369 			vgone(vp);
370 		return (0);
371 	}
372 
373 	error = 0;
374 	ILOCK(ip);
375 	if (ip->i_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
376 #ifdef QUOTA
377 		if (!getinoquota(ip))
378 			(void)chkiq(ip, -1, NOCRED, 0);
379 #endif
380 		error = ffs_truncate(vp, (u_long)0, 0);
381 		mode = ip->i_mode;
382 		ip->i_mode = 0;
383 		ip->i_rdev = 0;
384 		ip->i_flag |= IUPD|ICHG;
385 		ffs_vfree(vp, ip->i_number, mode);
386 	}
387 	if (ip->i_flag&(IUPD|IACC|ICHG|IMOD))
388 		ffs_update(vp, &time, &time, 0);
389 	IUNLOCK(ip);
390 	ip->i_flag = 0;
391 	/*
392 	 * If we are done with the inode, reclaim it
393 	 * so that it can be reused immediately.
394 	 */
395 	if (vp->v_usecount == 0 && ip->i_mode == 0)
396 		vgone(vp);
397 	return (error);
398 }
399