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