xref: /original-bsd/sys/ufs/lfs/lfs_inode.c (revision 0d869007)
1 /*
2  * Copyright (c) 1986, 1989, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)lfs_inode.c	8.8 (Berkeley) 03/30/95
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 /* Search a block for a specific dinode. */
31 struct dinode *
32 lfs_ifind(fs, ino, dip)
33 	struct lfs *fs;
34 	ino_t ino;
35 	register struct dinode *dip;
36 {
37 	register int cnt;
38 	register struct dinode *ldip;
39 
40 	for (cnt = INOPB(fs), ldip = dip + (cnt - 1); cnt--; --ldip)
41 		if (ldip->di_inumber == ino)
42 			return (ldip);
43 
44 	panic("lfs_ifind: dinode %u not found", ino);
45 	/* NOTREACHED */
46 }
47 
48 int
49 lfs_update(ap)
50 	struct vop_update_args /* {
51 		struct vnode *a_vp;
52 		struct timeval *a_access;
53 		struct timeval *a_modify;
54 		int a_waitfor;
55 	} */ *ap;
56 {
57 	struct vnode *vp = ap->a_vp;
58 	struct inode *ip;
59 
60 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
61 		return (0);
62 	ip = VTOI(vp);
63 	if ((ip->i_flag &
64 	    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0)
65 		return (0);
66 	if (ip->i_flag & IN_ACCESS)
67 		ip->i_atime = ap->a_access->tv_sec;
68 	if (ip->i_flag & IN_UPDATE) {
69 		ip->i_mtime = ap->a_modify->tv_sec;
70 		(ip)->i_modrev++;
71 	}
72 	if (ip->i_flag & IN_CHANGE)
73 		ip->i_ctime = time.tv_sec;
74 	ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
75 
76 	if (!(ip->i_flag & IN_MODIFIED))
77 		++(VFSTOUFS(vp->v_mount)->um_lfs->lfs_uinodes);
78 	ip->i_flag |= IN_MODIFIED;
79 
80 	/* If sync, push back the vnode and any dirty blocks it may have. */
81 	return (ap->a_waitfor & LFS_SYNC ? lfs_vflush(vp) : 0);
82 }
83 
84 /* Update segment usage information when removing a block. */
85 #define UPDATE_SEGUSE \
86 	if (lastseg != -1) { \
87 		LFS_SEGENTRY(sup, fs, lastseg, sup_bp); \
88 		if ((num << fs->lfs_bshift) > sup->su_nbytes) \
89 			panic("lfs_truncate: negative bytes in segment %d\n", \
90 			    lastseg); \
91 		sup->su_nbytes -= num << fs->lfs_bshift; \
92 		e1 = VOP_BWRITE(sup_bp); \
93 		blocksreleased += num; \
94 	}
95 
96 #define SEGDEC { \
97 	if (daddr != 0) { \
98 		if (lastseg != (seg = datosn(fs, daddr))) { \
99 			UPDATE_SEGUSE; \
100 			num = 1; \
101 			lastseg = seg; \
102 		} else \
103 			++num; \
104 	} \
105 }
106 
107 /*
108  * Truncate the inode ip to at most length size.  Update segment usage
109  * table information.
110  */
111 /* ARGSUSED */
112 int
113 lfs_truncate(ap)
114 	struct vop_truncate_args /* {
115 		struct vnode *a_vp;
116 		off_t a_length;
117 		int a_flags;
118 		struct ucred *a_cred;
119 		struct proc *a_p;
120 	} */ *ap;
121 {
122 	register struct indir *inp;
123 	register int i;
124 	register ufs_daddr_t *daddrp;
125 	register struct vnode *vp = ap->a_vp;
126 	off_t length = ap->a_length;
127 	struct buf *bp, *sup_bp;
128 	struct timeval tv;
129 	struct ifile *ifp;
130 	struct inode *ip;
131 	struct lfs *fs;
132 	struct indir a[NIADDR + 2], a_end[NIADDR + 2];
133 	SEGUSE *sup;
134 	ufs_daddr_t daddr, lastblock, lbn, olastblock;
135 	long off, a_released, blocksreleased, i_released;
136 	int e1, e2, depth, lastseg, num, offset, seg, size;
137 
138 	ip = VTOI(vp);
139 	tv = time;
140 	if (vp->v_type == VLNK && vp->v_mount->mnt_maxsymlinklen > 0) {
141 #ifdef DIAGNOSTIC
142 		if (length != 0)
143 			panic("lfs_truncate: partial truncate of symlink");
144 #endif
145 		bzero((char *)&ip->i_shortlink, (u_int)ip->i_size);
146 		ip->i_size = 0;
147 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
148 		return (VOP_UPDATE(vp, &tv, &tv, 0));
149 	}
150 	vnode_pager_setsize(vp, (u_long)length);
151 
152 	fs = ip->i_lfs;
153 
154 	/* If length is larger than the file, just update the times. */
155 	if (ip->i_size <= length) {
156 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
157 		return (VOP_UPDATE(vp, &tv, &tv, 0));
158 	}
159 
160 	/*
161 	 * Calculate index into inode's block list of last direct and indirect
162 	 * blocks (if any) which we want to keep.  Lastblock is 0 when the
163 	 * file is truncated to 0.
164 	 */
165 	lastblock = lblkno(fs, length + fs->lfs_bsize - 1);
166 	olastblock = lblkno(fs, ip->i_size + fs->lfs_bsize - 1) - 1;
167 
168 	/*
169 	 * Update the size of the file. If the file is not being truncated to
170 	 * a block boundry, the contents of the partial block following the end
171 	 * of the file must be zero'ed in case it ever become accessable again
172 	 * because of subsequent file growth.
173 	 */
174 	offset = blkoff(fs, length);
175 	if (offset == 0)
176 		ip->i_size = length;
177 	else {
178 		lbn = lblkno(fs, length);
179 #ifdef QUOTA
180 		if (e1 = getinoquota(ip))
181 			return (e1);
182 #endif
183 		if (e1 = bread(vp, lbn, fs->lfs_bsize, NOCRED, &bp))
184 			return (e1);
185 		ip->i_size = length;
186 		size = blksize(fs);
187 		(void)vnode_pager_uncache(vp);
188 		bzero((char *)bp->b_data + offset, (u_int)(size - offset));
189 		allocbuf(bp, size);
190 		if (e1 = VOP_BWRITE(bp))
191 			return (e1);
192 	}
193 	/*
194 	 * Modify sup->su_nbyte counters for each deleted block; keep track
195 	 * of number of blocks removed for ip->i_blocks.
196 	 */
197 	blocksreleased = 0;
198 	num = 0;
199 	lastseg = -1;
200 
201 	for (lbn = olastblock; lbn >= lastblock;) {
202 		/* XXX use run length from bmap array to make this faster */
203 		ufs_bmaparray(vp, lbn, &daddr, a, &depth, NULL);
204 		if (lbn == olastblock)
205 			for (i = NIADDR + 2; i--;)
206 				a_end[i] = a[i];
207 		switch (depth) {
208 		case 0:				/* Direct block. */
209 			daddr = ip->i_db[lbn];
210 			SEGDEC;
211 			ip->i_db[lbn] = 0;
212 			--lbn;
213 			break;
214 #ifdef DIAGNOSTIC
215 		case 1:				/* An indirect block. */
216 			panic("lfs_truncate: ufs_bmaparray returned depth 1");
217 			/* NOTREACHED */
218 #endif
219 		default:			/* Chain of indirect blocks. */
220 			inp = a + --depth;
221 			if (inp->in_off > 0 && lbn != lastblock) {
222 				lbn -= inp->in_off < lbn - lastblock ?
223 				    inp->in_off : lbn - lastblock;
224 				break;
225 			}
226 			for (; depth && (inp->in_off == 0 || lbn == lastblock);
227 			    --inp, --depth) {
228 				if (bread(vp,
229 				    inp->in_lbn, fs->lfs_bsize, NOCRED, &bp))
230 					panic("lfs_truncate: bread bno %d",
231 					    inp->in_lbn);
232 				daddrp = (ufs_daddr_t *)bp->b_data +
233 				    inp->in_off;
234 				for (i = inp->in_off;
235 				    i++ <= a_end[depth].in_off;) {
236 					daddr = *daddrp++;
237 					SEGDEC;
238 				}
239 				a_end[depth].in_off = NINDIR(fs) - 1;
240 				if (inp->in_off == 0)
241 					brelse (bp);
242 				else {
243 					bzero((ufs_daddr_t *)bp->b_data +
244 					    inp->in_off, fs->lfs_bsize -
245 					    inp->in_off * sizeof(ufs_daddr_t));
246 					if (e1 = VOP_BWRITE(bp))
247 						return (e1);
248 				}
249 			}
250 			if (depth == 0 && a[1].in_off == 0) {
251 				off = a[0].in_off;
252 				daddr = ip->i_ib[off];
253 				SEGDEC;
254 				ip->i_ib[off] = 0;
255 			}
256 			if (lbn == lastblock || lbn <= NDADDR)
257 				--lbn;
258 			else {
259 				lbn -= NINDIR(fs);
260 				if (lbn < lastblock)
261 					lbn = lastblock;
262 			}
263 		}
264 	}
265 	UPDATE_SEGUSE;
266 
267 	/* If truncating the file to 0, update the version number. */
268 	if (length == 0) {
269 		LFS_IENTRY(ifp, fs, ip->i_number, bp);
270 		++ifp->if_version;
271 		(void) VOP_BWRITE(bp);
272 	}
273 
274 #ifdef DIAGNOSTIC
275 	if (ip->i_blocks < fsbtodb(fs, blocksreleased)) {
276 		printf("lfs_truncate: block count < 0\n");
277 		blocksreleased = ip->i_blocks;
278 	}
279 #endif
280 	ip->i_blocks -= fsbtodb(fs, blocksreleased);
281 	fs->lfs_bfree +=  fsbtodb(fs, blocksreleased);
282 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
283 	/*
284 	 * Traverse dirty block list counting number of dirty buffers
285 	 * that are being deleted out of the cache, so that the lfs_avail
286 	 * field can be updated.
287 	 */
288 	a_released = 0;
289 	i_released = 0;
290 	for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next)
291 		if (bp->b_flags & B_LOCKED) {
292 			++a_released;
293 			/*
294 			 * XXX
295 			 * When buffers are created in the cache, their block
296 			 * number is set equal to their logical block number.
297 			 * If that is still true, we are assuming that the
298 			 * blocks are new (not yet on disk) and weren't
299 			 * counted above.  However, there is a slight chance
300 			 * that a block's disk address is equal to its logical
301 			 * block number in which case, we'll get an overcounting
302 			 * here.
303 			 */
304 			if (bp->b_blkno == bp->b_lblkno)
305 				++i_released;
306 		}
307 	blocksreleased = fsbtodb(fs, i_released);
308 #ifdef DIAGNOSTIC
309 	if (blocksreleased > ip->i_blocks) {
310 		printf("lfs_inode: Warning! %s\n",
311 		    "more blocks released from inode than are in inode");
312 		blocksreleased = ip->i_blocks;
313 	}
314 #endif
315 	fs->lfs_bfree += blocksreleased;
316 	ip->i_blocks -= blocksreleased;
317 #ifdef DIAGNOSTIC
318 	if (length == 0 && ip->i_blocks != 0)
319 		printf("lfs_inode: Warning! %s%d%s\n",
320 		    "Truncation to zero, but ", ip->i_blocks,
321 		    " blocks left on inode");
322 #endif
323 	fs->lfs_avail += fsbtodb(fs, a_released);
324 	e1 = vinvalbuf(vp, (length > 0) ? V_SAVE : 0, ap->a_cred, ap->a_p,
325 	    0, 0);
326 	e2 = VOP_UPDATE(vp, &tv, &tv, 0);
327 	return (e1 ? e1 : e2 ? e2 : 0);
328 }
329