xref: /original-bsd/sys/ufs/lfs/lfs_syscalls.c (revision 333da485)
1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)lfs_syscalls.c	8.4 (Berkeley) 01/13/94
8  */
9 
10 #include <sys/param.h>
11 #include <sys/proc.h>
12 #include <sys/buf.h>
13 #include <sys/mount.h>
14 #include <sys/vnode.h>
15 #include <sys/malloc.h>
16 #include <sys/kernel.h>
17 
18 #include <ufs/ufs/quota.h>
19 #include <ufs/ufs/inode.h>
20 #include <ufs/ufs/ufsmount.h>
21 #include <ufs/ufs/ufs_extern.h>
22 
23 #include <ufs/lfs/lfs.h>
24 #include <ufs/lfs/lfs_extern.h>
25 #define BUMP_FIP(SP) \
26 	(SP)->fip = (FINFO *) (&(SP)->fip->fi_blocks[(SP)->fip->fi_nblocks])
27 
28 #define INC_FINFO(SP) ++((SEGSUM *)((SP)->segsum))->ss_nfinfo
29 #define DEC_FINFO(SP) --((SEGSUM *)((SP)->segsum))->ss_nfinfo
30 
31 /*
32  * Before committing to add something to a segment summary, make sure there
33  * is enough room.  S is the bytes added to the summary.
34  */
35 #define	CHECK_SEG(s)			\
36 if (sp->sum_bytes_left < (s)) {		\
37 	(void) lfs_writeseg(fs, sp);	\
38 }
39 struct buf *lfs_fakebuf __P((struct vnode *, int, size_t, caddr_t));
40 
41 /*
42  * lfs_markv:
43  *
44  * This will mark inodes and blocks dirty, so they are written into the log.
45  * It will block until all the blocks have been written.  The segment create
46  * time passed in the block_info and inode_info structures is used to decide
47  * if the data is valid for each block (in case some process dirtied a block
48  * or inode that is being cleaned between the determination that a block is
49  * live and the lfs_markv call).
50  *
51  *  0 on success
52  * -1/errno is return on error.
53  */
54 struct lfs_markv_args {
55 	fsid_t *fsidp;		/* file system */
56 	BLOCK_INFO *blkiov;	/* block array */
57 	int blkcnt;		/* count of block array entries */
58 };
59 int
60 lfs_markv(p, uap, retval)
61 	struct proc *p;
62 	struct lfs_markv_args *uap;
63 	int *retval;
64 {
65 	struct segment *sp;
66 	BLOCK_INFO *blkp;
67 	IFILE *ifp;
68 	struct buf *bp, **bpp;
69 	struct inode *ip;
70 	struct lfs *fs;
71 	struct mount *mntp;
72 	struct vnode *vp;
73 	fsid_t fsid;
74 	void *start;
75 	ino_t lastino;
76 	daddr_t b_daddr, v_daddr;
77 	u_long bsize;
78 	int cnt, error;
79 
80 	if (error = suser(p->p_ucred, &p->p_acflag))
81 		return (error);
82 
83 	if (error = copyin(uap->fsidp, &fsid, sizeof(fsid_t)))
84 		return (error);
85 	if ((mntp = getvfs(&fsid)) == NULL)
86 		return (EINVAL);
87 
88 	cnt = uap->blkcnt;
89 	start = malloc(cnt * sizeof(BLOCK_INFO), M_SEGMENT, M_WAITOK);
90 	if (error = copyin(uap->blkiov, start, cnt * sizeof(BLOCK_INFO)))
91 		goto err1;
92 
93 	/* Mark blocks/inodes dirty.  */
94 	fs = VFSTOUFS(mntp)->um_lfs;
95 	bsize = fs->lfs_bsize;
96 	error = 0;
97 
98 	lfs_seglock(fs, SEGM_SYNC | SEGM_CLEAN);
99 	sp = fs->lfs_sp;
100 	for (v_daddr = LFS_UNUSED_DADDR, lastino = LFS_UNUSED_INUM,
101 	    blkp = start; cnt--; ++blkp) {
102 		/*
103 		 * Get the IFILE entry (only once) and see if the file still
104 		 * exists.
105 		 */
106 		if (lastino != blkp->bi_inode) {
107 			if (lastino != LFS_UNUSED_INUM) {
108 				/* Finish up last file */
109 				lfs_updatemeta(sp);
110 				lfs_writeinode(fs, sp, ip);
111 				lfs_vunref(vp);
112 				if (sp->fip->fi_nblocks)
113 					BUMP_FIP(sp);
114 				else  {
115 					DEC_FINFO(sp);
116 					sp->sum_bytes_left +=
117 						sizeof(FINFO) - sizeof(daddr_t);
118 
119 				}
120 			}
121 
122 			/* Start a new file */
123 			CHECK_SEG(sizeof(FINFO));
124 			sp->sum_bytes_left -= sizeof(FINFO) - sizeof(daddr_t);
125 			INC_FINFO(sp);
126 			sp->start_lbp = &sp->fip->fi_blocks[0];
127 			sp->vp = NULL;
128 			sp->fip->fi_version = blkp->bi_version;
129 			sp->fip->fi_nblocks = 0;
130 			sp->fip->fi_ino = blkp->bi_inode;
131 			lastino = blkp->bi_inode;
132 			if (blkp->bi_inode == LFS_IFILE_INUM)
133 				v_daddr = fs->lfs_idaddr;
134 			else {
135 				LFS_IENTRY(ifp, fs, blkp->bi_inode, bp);
136 				v_daddr = ifp->if_daddr;
137 				brelse(bp);
138 			}
139 			if (v_daddr == LFS_UNUSED_DADDR)
140 				continue;
141 
142 			/* Get the vnode/inode. */
143 			if (lfs_fastvget(mntp, blkp->bi_inode, v_daddr, &vp,
144 			    blkp->bi_lbn == LFS_UNUSED_LBN ?
145 			    blkp->bi_bp : NULL)) {
146 #ifdef DIAGNOSTIC
147 				printf("lfs_markv: VFS_VGET failed (%d)\n",
148 				    blkp->bi_inode);
149 #endif
150 				lastino = LFS_UNUSED_INUM;
151 				v_daddr = LFS_UNUSED_DADDR;
152 				continue;
153 			}
154 			sp->vp = vp;
155 			ip = VTOI(vp);
156 		} else if (v_daddr == LFS_UNUSED_DADDR)
157 			continue;
158 
159 		/* If this BLOCK_INFO didn't contain a block, keep going. */
160 		if (blkp->bi_lbn == LFS_UNUSED_LBN)
161 			continue;
162 		if (VOP_BMAP(vp, blkp->bi_lbn, NULL, &b_daddr, NULL) ||
163 		    b_daddr != blkp->bi_daddr)
164 			continue;
165 		/*
166 		 * If we got to here, then we are keeping the block.  If it
167 		 * is an indirect block, we want to actually put it in the
168 		 * buffer cache so that it can be updated in the finish_meta
169 		 * section.  If it's not, we need to allocate a fake buffer
170 		 * so that writeseg can perform the copyin and write the buffer.
171 		 */
172 		if (blkp->bi_lbn >= 0)	/* Data Block */
173 			bp = lfs_fakebuf(vp, blkp->bi_lbn, bsize,
174 			    blkp->bi_bp);
175 		else {
176 			bp = getblk(vp, blkp->bi_lbn, bsize, 0, 0);
177 			if (!(bp->b_flags & (B_DELWRI | B_DONE | B_CACHE)) &&
178 			    (error = copyin(blkp->bi_bp, bp->b_data,
179 			    bsize)))
180 				goto err2;
181 			if (error = VOP_BWRITE(bp))
182 				goto err2;
183 		}
184 		while (lfs_gatherblock(sp, bp, NULL));
185 	}
186 	if (sp->vp) {
187 		lfs_updatemeta(sp);
188 		lfs_writeinode(fs, sp, ip);
189 		lfs_vunref(vp);
190 		if (!sp->fip->fi_nblocks) {
191 			DEC_FINFO(sp);
192 			sp->sum_bytes_left += sizeof(FINFO) - sizeof(daddr_t);
193 		}
194 	}
195 	(void) lfs_writeseg(fs, sp);
196 	lfs_segunlock(fs);
197 	free(start, M_SEGMENT);
198 	return (error);
199 /*
200  * XXX If we come in to error 2, we might have indirect blocks that were
201  * updated and now have bad block pointers.  I don't know what to do
202  * about this.
203  */
204 
205 err2:	lfs_vunref(vp);
206 	/* Free up fakebuffers */
207 	for (bpp = --sp->cbpp; bpp >= sp->bpp; --bpp)
208 		if ((*bpp)->b_flags & B_CALL) {
209 			brelvp(*bpp);
210 			free(*bpp, M_SEGMENT);
211 		} else
212 			brelse(*bpp);
213 	lfs_segunlock(fs);
214 err1:
215 	free(start, M_SEGMENT);
216 	return(error);
217 }
218 
219 /*
220  * lfs_bmapv:
221  *
222  * This will fill in the current disk address for arrays of blocks.
223  *
224  *  0 on success
225  * -1/errno is return on error.
226  */
227 struct lfs_bmapv_args {
228 	fsid_t *fsidp;		/* file system */
229 	BLOCK_INFO *blkiov;	/* block array */
230 	int blkcnt;		/* count of block array entries */
231 };
232 int
233 lfs_bmapv(p, uap, retval)
234 	struct proc *p;
235 	struct lfs_bmapv_args *uap;
236 	int *retval;
237 {
238 	BLOCK_INFO *blkp;
239 	struct mount *mntp;
240 	struct vnode *vp;
241 	fsid_t fsid;
242 	void *start;
243 	daddr_t daddr;
244 	int cnt, error, step;
245 
246 	if (error = suser(p->p_ucred, &p->p_acflag))
247 		return (error);
248 
249 	if (error = copyin(uap->fsidp, &fsid, sizeof(fsid_t)))
250 		return (error);
251 	if ((mntp = getvfs(&fsid)) == NULL)
252 		return (EINVAL);
253 
254 	cnt = uap->blkcnt;
255 	start = blkp = malloc(cnt * sizeof(BLOCK_INFO), M_SEGMENT, M_WAITOK);
256 	if (error = copyin(uap->blkiov, blkp, cnt * sizeof(BLOCK_INFO))) {
257 		free(blkp, M_SEGMENT);
258 		return (error);
259 	}
260 
261 	for (step = cnt; step--; ++blkp) {
262 		if (blkp->bi_lbn == LFS_UNUSED_LBN)
263 			continue;
264 		/* Could be a deadlock ? */
265 		if (VFS_VGET(mntp, blkp->bi_inode, &vp))
266 			daddr = LFS_UNUSED_DADDR;
267 		else {
268 			if (VOP_BMAP(vp, blkp->bi_lbn, NULL, &daddr, NULL))
269 				daddr = LFS_UNUSED_DADDR;
270 			vput(vp);
271 		}
272 		blkp->bi_daddr = daddr;
273         }
274 	copyout(start, uap->blkiov, cnt * sizeof(BLOCK_INFO));
275 	free(start, M_SEGMENT);
276 	return (0);
277 }
278 
279 /*
280  * lfs_segclean:
281  *
282  * Mark the segment clean.
283  *
284  *  0 on success
285  * -1/errno is return on error.
286  */
287 struct lfs_segclean_args {
288 	fsid_t *fsidp;		/* file system */
289 	u_long segment;		/* segment number */
290 };
291 int
292 lfs_segclean(p, uap, retval)
293 	struct proc *p;
294 	struct lfs_segclean_args *uap;
295 	int *retval;
296 {
297 	CLEANERINFO *cip;
298 	SEGUSE *sup;
299 	struct buf *bp;
300 	struct mount *mntp;
301 	struct lfs *fs;
302 	fsid_t fsid;
303 	int error;
304 
305 	if (error = suser(p->p_ucred, &p->p_acflag))
306 		return (error);
307 
308 	if (error = copyin(uap->fsidp, &fsid, sizeof(fsid_t)))
309 		return (error);
310 	if ((mntp = getvfs(&fsid)) == NULL)
311 		return (EINVAL);
312 
313 	fs = VFSTOUFS(mntp)->um_lfs;
314 
315 	if (datosn(fs, fs->lfs_curseg) == uap->segment)
316 		return (EBUSY);
317 
318 	LFS_SEGENTRY(sup, fs, uap->segment, bp);
319 	if (sup->su_flags & SEGUSE_ACTIVE) {
320 		brelse(bp);
321 		return(EBUSY);
322 	}
323 	fs->lfs_avail += fsbtodb(fs, fs->lfs_ssize) - 1;
324 	fs->lfs_bfree += (sup->su_nsums * LFS_SUMMARY_SIZE / DEV_BSIZE) +
325 	    sup->su_ninos * btodb(fs->lfs_bsize);
326 	sup->su_flags &= ~SEGUSE_DIRTY;
327 	(void) VOP_BWRITE(bp);
328 
329 	LFS_CLEANERINFO(cip, fs, bp);
330 	++cip->clean;
331 	--cip->dirty;
332 	(void) VOP_BWRITE(bp);
333 	wakeup(&fs->lfs_avail);
334 	return (0);
335 }
336 
337 /*
338  * lfs_segwait:
339  *
340  * This will block until a segment in file system fsid is written.  A timeout
341  * in milliseconds may be specified which will awake the cleaner automatically.
342  * An fsid of -1 means any file system, and a timeout of 0 means forever.
343  *
344  *  0 on success
345  *  1 on timeout
346  * -1/errno is return on error.
347  */
348 struct lfs_segwait_args {
349 	fsid_t *fsidp;		/* file system */
350 	struct timeval *tv;	/* timeout */
351 };
352 int
353 lfs_segwait(p, uap, retval)
354 	struct proc *p;
355 	struct lfs_segwait_args *uap;
356 	int *retval;
357 {
358 	extern int lfs_allclean_wakeup;
359 	struct mount *mntp;
360 	struct timeval atv;
361 	fsid_t fsid;
362 	void *addr;
363 	u_long timeout;
364 	int error, s;
365 
366 	if (error = suser(p->p_ucred, &p->p_acflag)) {
367 		return (error);
368 }
369 #ifdef WHEN_QUADS_WORK
370 	if (error = copyin(uap->fsidp, &fsid, sizeof(fsid_t)))
371 		return (error);
372 	if (fsid == (fsid_t)-1)
373 		addr = &lfs_allclean_wakeup;
374 	else {
375 		if ((mntp = getvfs(&fsid)) == NULL)
376 			return (EINVAL);
377 		addr = &VFSTOUFS(mntp)->um_lfs->lfs_nextseg;
378 	}
379 #else
380 	if (error = copyin(uap->fsidp, &fsid, sizeof(fsid_t)))
381 		return (error);
382 	if ((mntp = getvfs(&fsid)) == NULL)
383 		addr = &lfs_allclean_wakeup;
384 	else
385 		addr = &VFSTOUFS(mntp)->um_lfs->lfs_nextseg;
386 #endif
387 
388 	if (uap->tv) {
389 		if (error = copyin(uap->tv, &atv, sizeof(struct timeval)))
390 			return (error);
391 		if (itimerfix(&atv))
392 			return (EINVAL);
393 		s = splclock();
394 		timevaladd(&atv, (struct timeval *)&time);
395 		timeout = hzto(&atv);
396 		splx(s);
397 	} else
398 		timeout = 0;
399 
400 	error = tsleep(addr, PCATCH | PUSER, "segment", timeout);
401 	return (error == ERESTART ? EINTR : 0);
402 }
403 
404 /*
405  * VFS_VGET call specialized for the cleaner.  The cleaner already knows the
406  * daddr from the ifile, so don't look it up again.  If the cleaner is
407  * processing IINFO structures, it may have the ondisk inode already, so
408  * don't go retrieving it again.
409  */
410 int
411 lfs_fastvget(mp, ino, daddr, vpp, dinp)
412 	struct mount *mp;
413 	ino_t ino;
414 	daddr_t daddr;
415 	struct vnode **vpp;
416 	struct dinode *dinp;
417 {
418 	register struct inode *ip;
419 	struct vnode *vp;
420 	struct ufsmount *ump;
421 	struct buf *bp;
422 	dev_t dev;
423 	int error;
424 
425 	ump = VFSTOUFS(mp);
426 	dev = ump->um_dev;
427 	/*
428 	 * This is playing fast and loose.  Someone may have the inode
429 	 * locked, in which case they are going to be distinctly unhappy
430 	 * if we trash something.
431 	 */
432 	if ((*vpp = ufs_ihashlookup(dev, ino)) != NULL) {
433 		lfs_vref(*vpp);
434 		if ((*vpp)->v_flag & VXLOCK)
435 			printf ("Cleaned vnode VXLOCKED\n");
436 		ip = VTOI(*vpp);
437 		if (ip->i_flags & IN_LOCKED)
438 			printf("cleaned vnode locked\n");
439 		if (!(ip->i_flag & IN_MODIFIED)) {
440 			++ump->um_lfs->lfs_uinodes;
441 			ip->i_flag |= IN_MODIFIED;
442 		}
443 		ip->i_flag |= IN_MODIFIED;
444 		return (0);
445 	}
446 
447 	/* Allocate new vnode/inode. */
448 	if (error = lfs_vcreate(mp, ino, &vp)) {
449 		*vpp = NULL;
450 		return (error);
451 	}
452 
453 	/*
454 	 * Put it onto its hash chain and lock it so that other requests for
455 	 * this inode will block if they arrive while we are sleeping waiting
456 	 * for old data structures to be purged or for the contents of the
457 	 * disk portion of this inode to be read.
458 	 */
459 	ip = VTOI(vp);
460 	ufs_ihashins(ip);
461 
462 	/*
463 	 * XXX
464 	 * This may not need to be here, logically it should go down with
465 	 * the i_devvp initialization.
466 	 * Ask Kirk.
467 	 */
468 	ip->i_lfs = ump->um_lfs;
469 
470 	/* Read in the disk contents for the inode, copy into the inode. */
471 	if (dinp)
472 		if (error = copyin(dinp, &ip->i_din, sizeof(struct dinode)))
473 			return (error);
474 	else {
475 		if (error = bread(ump->um_devvp, daddr,
476 		    (int)ump->um_lfs->lfs_bsize, NOCRED, &bp)) {
477 			/*
478 			 * The inode does not contain anything useful, so it
479 			 * would be misleading to leave it on its hash chain.
480 			 * Iput() will return it to the free list.
481 			 */
482 			ufs_ihashrem(ip);
483 
484 			/* Unlock and discard unneeded inode. */
485 			lfs_vunref(vp);
486 			brelse(bp);
487 			*vpp = NULL;
488 			return (error);
489 		}
490 		ip->i_din =
491 		    *lfs_ifind(ump->um_lfs, ino, (struct dinode *)bp->b_data);
492 		brelse(bp);
493 	}
494 
495 	/* Inode was just read from user space or disk, make sure it's locked */
496 	ip->i_flag |= IN_LOCKED;
497 
498 	/*
499 	 * Initialize the vnode from the inode, check for aliases.  In all
500 	 * cases re-init ip, the underlying vnode/inode may have changed.
501 	 */
502 	if (error = ufs_vinit(mp, lfs_specop_p, LFS_FIFOOPS, &vp)) {
503 		lfs_vunref(vp);
504 		*vpp = NULL;
505 		return (error);
506 	}
507 	/*
508 	 * Finish inode initialization now that aliasing has been resolved.
509 	 */
510 	ip->i_devvp = ump->um_devvp;
511 	ip->i_flag |= IN_MODIFIED;
512 	++ump->um_lfs->lfs_uinodes;
513 	VREF(ip->i_devvp);
514 	*vpp = vp;
515 	return (0);
516 }
517 struct buf *
518 lfs_fakebuf(vp, lbn, size, uaddr)
519 	struct vnode *vp;
520 	int lbn;
521 	size_t size;
522 	caddr_t uaddr;
523 {
524 	struct buf *bp;
525 
526 	bp = lfs_newbuf(vp, lbn, 0);
527 	bp->b_saveaddr = uaddr;
528 	bp->b_bufsize = size;
529 	bp->b_bcount = size;
530 	bp->b_flags |= B_INVAL;
531 	return(bp);
532 }
533