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