lfs_inode.c revision 1.1.1.2 1 /*
2 * Copyright (c) 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)lfs_inode.c 8.9 (Berkeley) 5/8/95
34 */
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/mount.h>
39 #include <sys/proc.h>
40 #include <sys/file.h>
41 #include <sys/buf.h>
42 #include <sys/vnode.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45
46 #include <vm/vm.h>
47
48 #include <ufs/ufs/quota.h>
49 #include <ufs/ufs/inode.h>
50 #include <ufs/ufs/ufsmount.h>
51 #include <ufs/ufs/ufs_extern.h>
52
53 #include <ufs/lfs/lfs.h>
54 #include <ufs/lfs/lfs_extern.h>
55
56 /* Search a block for a specific dinode. */
57 struct dinode *
58 lfs_ifind(fs, ino, dip)
59 struct lfs *fs;
60 ino_t ino;
61 register struct dinode *dip;
62 {
63 register int cnt;
64 register struct dinode *ldip;
65
66 for (cnt = INOPB(fs), ldip = dip + (cnt - 1); cnt--; --ldip)
67 if (ldip->di_inumber == ino)
68 return (ldip);
69
70 panic("lfs_ifind: dinode %u not found", ino);
71 /* NOTREACHED */
72 }
73
74 int
75 lfs_update(ap)
76 struct vop_update_args /* {
77 struct vnode *a_vp;
78 struct timeval *a_access;
79 struct timeval *a_modify;
80 int a_waitfor;
81 } */ *ap;
82 {
83 struct vnode *vp = ap->a_vp;
84 struct inode *ip;
85
86 if (vp->v_mount->mnt_flag & MNT_RDONLY)
87 return (0);
88 ip = VTOI(vp);
89 if ((ip->i_flag &
90 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0)
91 return (0);
92 if (ip->i_flag & IN_ACCESS)
93 ip->i_atime = ap->a_access->tv_sec;
94 if (ip->i_flag & IN_UPDATE) {
95 ip->i_mtime = ap->a_modify->tv_sec;
96 (ip)->i_modrev++;
97 }
98 if (ip->i_flag & IN_CHANGE)
99 ip->i_ctime = time.tv_sec;
100 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
101
102 if (!(ip->i_flag & IN_MODIFIED))
103 ++(VFSTOUFS(vp->v_mount)->um_lfs->lfs_uinodes);
104 ip->i_flag |= IN_MODIFIED;
105
106 /* If sync, push back the vnode and any dirty blocks it may have. */
107 return (ap->a_waitfor & LFS_SYNC ? lfs_vflush(vp) : 0);
108 }
109
110 /* Update segment usage information when removing a block. */
111 #define UPDATE_SEGUSE \
112 if (lastseg != -1) { \
113 LFS_SEGENTRY(sup, fs, lastseg, sup_bp); \
114 if (num > sup->su_nbytes) \
115 panic("lfs_truncate: negative bytes in segment %d\n", \
116 lastseg); \
117 sup->su_nbytes -= num; \
118 e1 = VOP_BWRITE(sup_bp); \
119 fragsreleased += numfrags(fs, num); \
120 }
121
122 #define SEGDEC(S) { \
123 if (daddr != 0) { \
124 if (lastseg != (seg = datosn(fs, daddr))) { \
125 UPDATE_SEGUSE; \
126 num = (S); \
127 lastseg = seg; \
128 } else \
129 num += (S); \
130 } \
131 }
132
133 /*
134 * Truncate the inode ip to at most length size. Update segment usage
135 * table information.
136 */
137 /* ARGSUSED */
138 int
139 lfs_truncate(ap)
140 struct vop_truncate_args /* {
141 struct vnode *a_vp;
142 off_t a_length;
143 int a_flags;
144 struct ucred *a_cred;
145 struct proc *a_p;
146 } */ *ap;
147 {
148 register struct indir *inp;
149 register int i;
150 register ufs_daddr_t *daddrp;
151 register struct vnode *vp = ap->a_vp;
152 off_t length = ap->a_length;
153 struct buf *bp, *sup_bp;
154 struct timeval tv;
155 struct ifile *ifp;
156 struct inode *ip;
157 struct lfs *fs;
158 struct indir a[NIADDR + 2], a_end[NIADDR + 2];
159 SEGUSE *sup;
160 ufs_daddr_t daddr, lastblock, lbn, olastblock;
161 ufs_daddr_t oldsize_lastblock, oldsize_newlast, newsize;
162 long off, a_released, fragsreleased, i_released;
163 int e1, e2, depth, lastseg, num, offset, seg, freesize;
164
165 ip = VTOI(vp);
166 tv = time;
167 if (vp->v_type == VLNK && vp->v_mount->mnt_maxsymlinklen > 0) {
168 #ifdef DIAGNOSTIC
169 if (length != 0)
170 panic("lfs_truncate: partial truncate of symlink");
171 #endif
172 bzero((char *)&ip->i_shortlink, (u_int)ip->i_size);
173 ip->i_size = 0;
174 ip->i_flag |= IN_CHANGE | IN_UPDATE;
175 return (VOP_UPDATE(vp, &tv, &tv, 0));
176 }
177 vnode_pager_setsize(vp, (u_long)length);
178
179 fs = ip->i_lfs;
180
181 /* If length is larger than the file, just update the times. */
182 if (ip->i_size <= length) {
183 ip->i_flag |= IN_CHANGE | IN_UPDATE;
184 return (VOP_UPDATE(vp, &tv, &tv, 0));
185 }
186
187 /*
188 * Calculate index into inode's block list of last direct and indirect
189 * blocks (if any) which we want to keep. Lastblock is 0 when the
190 * file is truncated to 0.
191 */
192 lastblock = lblkno(fs, length + fs->lfs_bsize - 1);
193 olastblock = lblkno(fs, ip->i_size + fs->lfs_bsize - 1) - 1;
194
195 /*
196 * Update the size of the file. If the file is not being truncated to
197 * a block boundry, the contents of the partial block following the end
198 * of the file must be zero'ed in case it ever become accessable again
199 * because of subsequent file growth. For this part of the code,
200 * oldsize_newlast refers to the old size of the new last block in the file.
201 */
202 offset = blkoff(fs, length);
203 lbn = lblkno(fs, length);
204 oldsize_newlast = blksize(fs, ip, lbn);
205
206 /* Now set oldsize to the current size of the current last block */
207 oldsize_lastblock = blksize(fs, ip, olastblock);
208 if (offset == 0)
209 ip->i_size = length;
210 else {
211 #ifdef QUOTA
212 if (e1 = getinoquota(ip))
213 return (e1);
214 #endif
215 if (e1 = bread(vp, lbn, oldsize_newlast, NOCRED, &bp))
216 return (e1);
217 ip->i_size = length;
218 (void)vnode_pager_uncache(vp);
219 newsize = blksize(fs, ip, lbn);
220 bzero((char *)bp->b_data + offset, (u_int)(newsize - offset));
221 allocbuf(bp, newsize);
222 if (e1 = VOP_BWRITE(bp))
223 return (e1);
224 }
225 /*
226 * Modify sup->su_nbyte counters for each deleted block; keep track
227 * of number of blocks removed for ip->i_blocks.
228 */
229 fragsreleased = 0;
230 num = 0;
231 lastseg = -1;
232
233 for (lbn = olastblock; lbn >= lastblock;) {
234 /* XXX use run length from bmap array to make this faster */
235 ufs_bmaparray(vp, lbn, &daddr, a, &depth, NULL);
236 if (lbn == olastblock) {
237 for (i = NIADDR + 2; i--;)
238 a_end[i] = a[i];
239 freesize = oldsize_lastblock;
240 } else
241 freesize = fs->lfs_bsize;
242
243 switch (depth) {
244 case 0: /* Direct block. */
245 daddr = ip->i_db[lbn];
246 SEGDEC(freesize);
247 ip->i_db[lbn] = 0;
248 --lbn;
249 break;
250 #ifdef DIAGNOSTIC
251 case 1: /* An indirect block. */
252 panic("lfs_truncate: ufs_bmaparray returned depth 1");
253 /* NOTREACHED */
254 #endif
255 default: /* Chain of indirect blocks. */
256 inp = a + --depth;
257 if (inp->in_off > 0 && lbn != lastblock) {
258 lbn -= inp->in_off < lbn - lastblock ?
259 inp->in_off : lbn - lastblock;
260 break;
261 }
262 for (; depth && (inp->in_off == 0 || lbn == lastblock);
263 --inp, --depth) {
264 if (bread(vp,
265 inp->in_lbn, fs->lfs_bsize, NOCRED, &bp))
266 panic("lfs_truncate: bread bno %d",
267 inp->in_lbn);
268 daddrp = (ufs_daddr_t *)bp->b_data +
269 inp->in_off;
270 for (i = inp->in_off;
271 i++ <= a_end[depth].in_off;) {
272 daddr = *daddrp++;
273 SEGDEC(freesize);
274 }
275 a_end[depth].in_off = NINDIR(fs) - 1;
276 if (inp->in_off == 0)
277 brelse (bp);
278 else {
279 bzero((ufs_daddr_t *)bp->b_data +
280 inp->in_off, fs->lfs_bsize -
281 inp->in_off * sizeof(ufs_daddr_t));
282 if (e1 = VOP_BWRITE(bp))
283 return (e1);
284 }
285 }
286 if (depth == 0 && a[1].in_off == 0) {
287 off = a[0].in_off;
288 daddr = ip->i_ib[off];
289 SEGDEC(freesize);
290 ip->i_ib[off] = 0;
291 }
292 if (lbn == lastblock || lbn <= NDADDR)
293 --lbn;
294 else {
295 lbn -= NINDIR(fs);
296 if (lbn < lastblock)
297 lbn = lastblock;
298 }
299 }
300 }
301 UPDATE_SEGUSE;
302
303 /* If truncating the file to 0, update the version number. */
304 if (length == 0) {
305 LFS_IENTRY(ifp, fs, ip->i_number, bp);
306 ++ifp->if_version;
307 (void) VOP_BWRITE(bp);
308 }
309
310 #ifdef DIAGNOSTIC
311 if (ip->i_blocks < fragstodb(fs, fragsreleased)) {
312 printf("lfs_truncate: frag count < 0\n");
313 fragsreleased = dbtofrags(fs, ip->i_blocks);
314 panic("lfs_truncate: frag count < 0\n");
315 }
316 #endif
317 ip->i_blocks -= fragstodb(fs, fragsreleased);
318 fs->lfs_bfree += fragstodb(fs, fragsreleased);
319 ip->i_flag |= IN_CHANGE | IN_UPDATE;
320 /*
321 * Traverse dirty block list counting number of dirty buffers
322 * that are being deleted out of the cache, so that the lfs_avail
323 * field can be updated.
324 */
325 a_released = 0;
326 i_released = 0;
327 for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next)
328 if (bp->b_flags & B_LOCKED) {
329 a_released += numfrags(fs, bp->b_bcount);
330 /*
331 * XXX
332 * When buffers are created in the cache, their block
333 * number is set equal to their logical block number.
334 * If that is still true, we are assuming that the
335 * blocks are new (not yet on disk) and weren't
336 * counted above. However, there is a slight chance
337 * that a block's disk address is equal to its logical
338 * block number in which case, we'll get an overcounting
339 * here.
340 */
341 if (bp->b_blkno == bp->b_lblkno)
342 i_released += numfrags(fs, bp->b_bcount);
343 }
344 fragsreleased = i_released;
345 #ifdef DIAGNOSTIC
346 if (fragsreleased > dbtofrags(fs, ip->i_blocks)) {
347 printf("lfs_inode: Warning! %s\n",
348 "more frags released from inode than are in inode");
349 fragsreleased = dbtofrags(fs, ip->i_blocks);
350 panic("lfs_inode: Warning. More frags released\n");
351 }
352 #endif
353 fs->lfs_bfree += fragstodb(fs, fragsreleased);
354 ip->i_blocks -= fragstodb(fs, fragsreleased);
355 #ifdef DIAGNOSTIC
356 if (length == 0 && ip->i_blocks != 0) {
357 printf("lfs_inode: Warning! %s%d%s\n",
358 "Truncation to zero, but ", ip->i_blocks,
359 " blocks left on inode");
360 panic("lfs_inode");
361 }
362 #endif
363 fs->lfs_avail += fragstodb(fs, a_released);
364 e1 = vinvalbuf(vp, (length > 0) ? V_SAVE : 0, ap->a_cred, ap->a_p,
365 0, 0);
366 e2 = VOP_UPDATE(vp, &tv, &tv, 0);
367 return (e1 ? e1 : e2 ? e2 : 0);
368 }
369