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