xref: /original-bsd/sys/ufs/lfs/lfs_inode.c (revision a5a45b47)
1 /*
2  * Copyright (c) 1986, 1989, 1991 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)lfs_inode.c	7.65 (Berkeley) 05/15/92
8  */
9 
10 #include <sys/param.h>
11 #include <sys/systm.h>
12 #include <sys/mount.h>
13 #include <sys/proc.h>
14 #include <sys/file.h>
15 #include <sys/buf.h>
16 #include <sys/vnode.h>
17 #include <sys/kernel.h>
18 #include <sys/malloc.h>
19 
20 #include <vm/vm.h>
21 
22 #include <ufs/ufs/quota.h>
23 #include <ufs/ufs/inode.h>
24 #include <ufs/ufs/ufsmount.h>
25 #include <ufs/ufs/ufs_extern.h>
26 
27 #include <ufs/lfs/lfs.h>
28 #include <ufs/lfs/lfs_extern.h>
29 
30 static struct dinode *lfs_ifind __P((struct lfs *, ino_t, struct dinode *));
31 
32 int
33 lfs_init()
34 {
35 #ifdef VERBOSE
36 	printf("lfs_init\n");
37 #endif
38 	return (ufs_init());
39 }
40 
41 /*
42  * Look up an LFS dinode number to find its incore vnode.  If not already
43  * in core, read it in from the specified device.  Return the inode locked.
44  * Detection and handling of mount points must be done by the calling routine.
45  */
46 int
47 lfs_vget (ap)
48 	struct vop_vget_args *ap;
49 {
50 	register struct lfs *fs;
51 	register struct inode *ip;
52 	struct buf *bp;
53 	struct ifile *ifp;
54 	struct vnode *vp;
55 	struct ufsmount *ump;
56 	daddr_t daddr;
57 	dev_t dev;
58 	int error;
59 
60 #ifdef VERBOSE
61 	printf("lfs_vget\n");
62 #endif
63 	ump = VFSTOUFS(ap->a_mp);
64 	dev = ump->um_dev;
65 	if ((*ap->a_vpp = ufs_ihashget(dev, ap->a_ino)) != NULL)
66 		return (0);
67 
68 	/* Translate the inode number to a disk address. */
69 	fs = ump->um_lfs;
70 	if (ap->a_ino == LFS_IFILE_INUM)
71 		daddr = fs->lfs_idaddr;
72 	else {
73 		LFS_IENTRY(ifp, fs, ap->a_ino, bp);
74 		daddr = ifp->if_daddr;
75 		brelse(bp);
76 		if (daddr == LFS_UNUSED_DADDR)
77 			return (ENOENT);
78 	}
79 
80 	/* Allocate new vnode/inode. */
81 	if (error = lfs_vcreate(ap->a_mp, ap->a_ino, &vp)) {
82 		*ap->a_vpp = NULL;
83 		return (error);
84 	}
85 
86 	/*
87 	 * Put it onto its hash chain and lock it so that other requests for
88 	 * this inode will block if they arrive while we are sleeping waiting
89 	 * for old data structures to be purged or for the contents of the
90 	 * disk portion of this inode to be read.
91 	 */
92 	ip = VTOI(vp);
93 	ufs_ihashins(ip);
94 
95 	/*
96 	 * XXX
97 	 * This may not need to be here, logically it should go down with
98 	 * the i_devvp initialization.
99 	 * Ask Kirk.
100 	 */
101 	ip->i_lfs = ump->um_lfs;
102 
103 	/* Read in the disk contents for the inode, copy into the inode. */
104 	if (error =
105 	    bread(ump->um_devvp, daddr, (int)fs->lfs_bsize, NOCRED, &bp)) {
106 		/*
107 		 * The inode does not contain anything useful, so it
108 		 * would be misleading to leave it on its hash chain.
109 		 * Iput() will return it to the free list.
110 		 */
111 		remque(ip);
112 		ip->i_forw = ip;
113 		ip->i_back = ip;
114 
115 		/* Unlock and discard unneeded inode. */
116 		ufs_iput(ip);
117 		brelse(bp);
118 		*ap->a_vpp = NULL;
119 		return (error);
120 	}
121 	ip->i_din = *lfs_ifind(fs, ap->a_ino, bp->b_un.b_dino);
122 	brelse(bp);
123 
124 	/*
125 	 * Initialize the vnode from the inode, check for aliases.  In all
126 	 * cases re-init ip, the underlying vnode/inode may have changed.
127 	 */
128 	if (error = ufs_vinit(ap->a_mp, lfs_specop_p, LFS_FIFOOPS, &vp)) {
129 		ufs_iput(ip);
130 		*ap->a_vpp = NULL;
131 		return (error);
132 	}
133 	/*
134 	 * Finish inode initialization now that aliasing has been resolved.
135 	 */
136 	ip->i_devvp = ump->um_devvp;
137 	VREF(ip->i_devvp);
138 	*ap->a_vpp = vp;
139 	return (0);
140 }
141 
142 /* Search a block for a specific dinode. */
143 static struct dinode *
144 lfs_ifind(fs, ino, dip)
145 	struct lfs *fs;
146 	ino_t ino;
147 	register struct dinode *dip;
148 {
149 	register int cnt;
150 
151 #ifdef VERBOSE
152 	printf("lfs_ifind: inode %d\n", ino);
153 #endif
154 	for (cnt = INOPB(fs); cnt--; ++dip)
155 		if (dip->di_inum == ino)
156 			return (dip);
157 
158 	panic("lfs_ifind: dinode %u not found", ino);
159 	/* NOTREACHED */
160 }
161 
162 int
163 lfs_update (ap)
164 	struct vop_update_args *ap;
165 {
166 	struct inode *ip;
167 
168 #ifdef VERBOSE
169 	printf("lfs_update\n");
170 #endif
171 	if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
172 		return (0);
173 	ip = VTOI(ap->a_vp);
174 	if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0)
175 		return (0);
176 	if (ip->i_flag&IACC)
177 		ip->i_atime.tv_sec = ap->a_ta->tv_sec;
178 	if (ip->i_flag&IUPD) {
179 		ip->i_mtime.tv_sec = ap->a_tm->tv_sec;
180 		INCRQUAD((ip)->i_modrev);
181 	}
182 	if (ip->i_flag&ICHG)
183 		ip->i_ctime.tv_sec = time.tv_sec;
184 	ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
185 
186 	/* Push back the vnode and any dirty blocks it may have. */
187 	return (ap->a_waitfor ? lfs_vflush(ap->a_vp) : 0);
188 }
189 
190 /* Update segment usage information when removing a block. */
191 #define UPDATE_SEGUSE \
192 	if (lastseg != -1) { \
193 		LFS_SEGENTRY(sup, fs, lastseg, sup_bp); \
194 		sup->su_nbytes -= num << fs->lfs_bshift; \
195 		LFS_UBWRITE(sup_bp); \
196 		blocksreleased += num; \
197 	}
198 
199 #define SEGDEC { \
200 	if (daddr != UNASSIGNED) { \
201 		if (lastseg != (seg = datosn(fs, daddr))) { \
202 			UPDATE_SEGUSE; \
203 			num = 1; \
204 			lastseg = seg; \
205 		} else \
206 			++num; \
207 	} \
208 }
209 
210 /*
211  * Truncate the inode ip to at most length size.  Update segment usage
212  * table information.
213  */
214 /* ARGSUSED */
215 int
216 lfs_truncate (ap)
217 	struct vop_truncate_args *ap;
218 {
219 	USES_VOP_UPDATE;
220 	register INDIR *inp;
221 	register int i;
222 	register daddr_t *daddrp;
223 	struct buf *bp, *sup_bp;
224 	struct ifile *ifp;
225 	struct inode *ip;
226 	struct lfs *fs;
227 	INDIR a[NIADDR + 2], a_end[NIADDR + 2];
228 	SEGUSE *sup;
229 	daddr_t daddr, lastblock, lbn, olastblock;
230 	long off, blocksreleased;
231 	int error, depth, lastseg, num, offset, seg, size;
232 
233 #ifdef VERBOSE
234 	printf("lfs_truncate\n");
235 #endif
236 	vnode_pager_setsize(ap->a_vp, (u_long)ap->a_length);
237 
238 	ip = VTOI(ap->a_vp);
239 	fs = ip->i_lfs;
240 
241 	/* If truncating the file to 0, update the version number. */
242 	if (ap->a_length == 0) {
243 		LFS_IENTRY(ifp, fs, ip->i_number, bp);
244 		++ifp->if_version;
245 		LFS_UBWRITE(bp);
246 	}
247 
248 	/* If ap->a_length is larger than the file, just update the times. */
249 	if (ip->i_size <= ap->a_length) {
250 		ip->i_flag |= ICHG|IUPD;
251 		return (VOP_UPDATE(ap->a_vp, &time, &time, 1));
252 	}
253 
254 	/*
255 	 * Calculate index into inode's block list of last direct and indirect
256 	 * blocks (if any) which we want to keep.  Lastblock is 0 when the
257 	 * file is truncated to 0.
258 	 */
259 	lastblock = lblkno(fs, ap->a_length + fs->lfs_bsize - 1);
260 	olastblock = lblkno(fs, ip->i_size + fs->lfs_bsize - 1) - 1;
261 
262 	/*
263 	 * Update the size of the file. If the file is not being truncated to
264 	 * a block boundry, the contents of the partial block following the end
265 	 * of the file must be zero'ed in case it ever become accessable again
266 	 * because of subsequent file growth.
267 	 */
268 	offset = blkoff(fs, ap->a_length);
269 	if (offset == 0)
270 		ip->i_size = ap->a_length;
271 	else {
272 		lbn = lblkno(fs, ap->a_length);
273 #ifdef QUOTA
274 		if (error = getinoquota(ip))
275 			return (error);
276 #endif
277 		if (error = bread(ap->a_vp, lbn, fs->lfs_bsize, NOCRED, &bp))
278 			return (error);
279 		ip->i_size = ap->a_length;
280 		size = blksize(fs);
281 		(void)vnode_pager_uncache(ap->a_vp);
282 		bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset));
283 		allocbuf(bp, size);
284 		LFS_UBWRITE(bp);
285 	}
286 	/*
287 	 * Modify sup->su_nbyte counters for each deleted block; keep track
288 	 * of number of blocks removed for ip->i_blocks.
289 	 */
290 	blocksreleased = 0;
291 	num = 0;
292 	lastseg = -1;
293 
294 	for (lbn = olastblock; lbn >= lastblock;) {
295 		lfs_bmaparray(ap->a_vp, lbn, &daddr, a, &depth);
296 		if (lbn == olastblock)
297 			for (i = NIADDR + 2; i--;)
298 				a_end[i] = a[i];
299 		switch (depth) {
300 		case 0:				/* Direct block. */
301 			daddr = ip->i_db[lbn];
302 			SEGDEC;
303 			ip->i_db[lbn] = 0;
304 			--lbn;
305 			break;
306 #ifdef DIAGNOSTIC
307 		case 1:				/* An indirect block. */
308 			panic("lfs_truncate: lfs_bmaparray returned depth 1");
309 			/* NOTREACHED */
310 #endif
311 		default:			/* Chain of indirect blocks. */
312 			inp = a + --depth;
313 			if (inp->in_off > 0 && lbn != lastblock) {
314 				lbn -= inp->in_off < lbn - lastblock ?
315 				    inp->in_off : lbn - lastblock;
316 				break;
317 			}
318 			for (; depth && (inp->in_off == 0 || lbn == lastblock);
319 			    --inp, --depth) {
320 				/*
321 				 * XXX
322 				 * The indirect block may not yet exist, so
323 				 * bread will create one just so we can free
324 				 * it.
325 				 */
326 				if (bread(ap->a_vp,
327 				    inp->in_lbn, fs->lfs_bsize, NOCRED, &bp))
328 					panic("lfs_truncate: bread bno %d",
329 					    inp->in_lbn);
330 				daddrp = bp->b_un.b_daddr + inp->in_off;
331 				for (i = inp->in_off;
332 				    i++ <= a_end[depth].in_off;) {
333 					daddr = *daddrp++;
334 					SEGDEC;
335 				}
336 				a_end[depth].in_off = NINDIR(fs) - 1;
337 				if (inp->in_off == 0)
338 					brelse (bp);
339 				else {
340 					bzero(bp->b_un.b_daddr + inp->in_off,
341 					    fs->lfs_bsize -
342 					    inp->in_off * sizeof(daddr_t));
343 					LFS_UBWRITE(bp);
344 				}
345 			}
346 			if (depth == 0 && a[1].in_off == 0) {
347 				off = a[0].in_off;
348 				daddr = ip->i_ib[off];
349 				SEGDEC;
350 				ip->i_ib[off] = 0;
351 			}
352 			if (lbn == lastblock || lbn <= NDADDR)
353 				--lbn;
354 			else {
355 				lbn -= NINDIR(fs);
356 				if (lbn < lastblock)
357 					lbn = lastblock;
358 			}
359 		}
360 	}
361 	UPDATE_SEGUSE;
362 	ip->i_blocks -= blocksreleased;
363 	/*
364 	 * XXX
365 	 * Currently, we don't know when we allocate an indirect block, so
366 	 * ip->i_blocks isn't getting incremented appropriately.  As a result,
367 	 * when we delete any indirect blocks, we get a bad number here.
368 	 */
369 	if (ip->i_blocks < 0)
370 		ip->i_blocks = 0;
371 	ip->i_flag |= ICHG|IUPD;
372 	(void)vinvalbuf(ap->a_vp, ap->a_length > 0);
373 	error = VOP_UPDATE(ap->a_vp, &time, &time, MNT_WAIT);
374 	return (0);
375 }
376