Deleted Added
sdiff udiff text old ( 125781 ) new ( 125962 )
full compact
1/*
2 * modified for Lites 1.1
3 *
4 * Aug 1995, Godmar Back (gback@cs.utah.edu)
5 * University of Utah, Department of Computer Science
6 */
7/*
8 * Copyright (c) 1982, 1986, 1989, 1993
9 * The Regents of the University of California. All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * @(#)ffs_inode.c 8.5 (Berkeley) 12/30/93
40 * $FreeBSD: head/sys/gnu/fs/ext2fs/ext2_inode.c 125781 2004-02-13 17:49:03Z bde $
41 */
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/mount.h>
46#include <sys/bio.h>
47#include <sys/buf.h>
48#include <sys/vnode.h>
49#include <sys/malloc.h>
50
51#include <vm/vm.h>
52#include <vm/vm_extern.h>
53
54#include <gnu/ext2fs/inode.h>
55#include <gnu/ext2fs/ext2_mount.h>
56#include <gnu/ext2fs/ext2_fs.h>
57#include <gnu/ext2fs/ext2_fs_sb.h>
58#include <gnu/ext2fs/fs.h>
59#include <gnu/ext2fs/ext2_extern.h>
60
61static int ext2_indirtrunc(struct inode *, int32_t, int32_t, int32_t, int,
62 long *);
63
64/*
65 * Update the access, modified, and inode change times as specified by the
66 * IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively. Write the inode
67 * to disk if the IN_MODIFIED flag is set (it may be set initially, or by
68 * the timestamp update). The IN_LAZYMOD flag is set to force a write
69 * later if not now. If we write now, then clear both IN_MODIFIED and
70 * IN_LAZYMOD to reflect the presumably successful write, and if waitfor is
71 * set, then wait for the write to complete.
72 */
73int
74ext2_update(vp, waitfor)
75 struct vnode *vp;
76 int waitfor;
77{
78 struct ext2_sb_info *fs;
79 struct buf *bp;
80 struct inode *ip;
81 int error;
82
83 ext2_itimes(vp);
84 ip = VTOI(vp);
85 if ((ip->i_flag & IN_MODIFIED) == 0)
86 return (0);
87 ip->i_flag &= ~(IN_LAZYMOD | IN_MODIFIED);
88 if (vp->v_mount->mnt_flag & MNT_RDONLY)
89 return (0);
90 fs = ip->i_e2fs;
91 if ((error = bread(ip->i_devvp,
92 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
93 (int)fs->s_blocksize, NOCRED, &bp)) != 0) {
94 brelse(bp);
95 return (error);
96 }
97 ext2_i2ei(ip, (struct ext2_inode *)((char *)bp->b_data +
98 EXT2_INODE_SIZE * ino_to_fsbo(fs, ip->i_number)));
99 if (waitfor && (vp->v_mount->mnt_flag & MNT_ASYNC) == 0)
100 return (bwrite(bp));
101 else {
102 bdwrite(bp);
103 return (0);
104 }
105}
106
107#define SINGLE 0 /* index of single indirect block */
108#define DOUBLE 1 /* index of double indirect block */
109#define TRIPLE 2 /* index of triple indirect block */
110/*
111 * Truncate the inode oip to at most length size, freeing the
112 * disk blocks.
113 */
114int
115ext2_truncate(vp, length, flags, cred, td)
116 struct vnode *vp;
117 off_t length;
118 int flags;
119 struct ucred *cred;
120 struct thread *td;
121{
122 struct vnode *ovp = vp;
123 int32_t lastblock;
124 struct inode *oip;
125 int32_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
126 int32_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
127 struct ext2_sb_info *fs;
128 struct buf *bp;
129 int offset, size, level;
130 long count, nblocks, blocksreleased = 0;
131 int aflags, error, i, allerror;
132 off_t osize;
133/*
134printf("ext2_truncate called %d to %d\n", VTOI(ovp)->i_number, length);
135*/ /*
136 * negative file sizes will totally break the code below and
137 * are not meaningful anyways.
138 */
139 if (length < 0)
140 return EFBIG;
141
142 oip = VTOI(ovp);
143 if (ovp->v_type == VLNK &&
144 oip->i_size < ovp->v_mount->mnt_maxsymlinklen) {
145#if DIAGNOSTIC
146 if (length != 0)
147 panic("ext2_truncate: partial truncate of symlink");
148#endif
149 bzero((char *)&oip->i_shortlink, (u_int)oip->i_size);
150 oip->i_size = 0;
151 oip->i_flag |= IN_CHANGE | IN_UPDATE;
152 return (ext2_update(ovp, 1));
153 }
154 if (oip->i_size == length) {
155 oip->i_flag |= IN_CHANGE | IN_UPDATE;
156 return (ext2_update(ovp, 0));
157 }
158 fs = oip->i_e2fs;
159 osize = oip->i_size;
160 ext2_discard_prealloc(oip);
161 /*
162 * Lengthen the size of the file. We must ensure that the
163 * last byte of the file is allocated. Since the smallest
164 * value of oszie is 0, length will be at least 1.
165 */
166 if (osize < length) {
167 offset = blkoff(fs, length - 1);
168 lbn = lblkno(fs, length - 1);
169 aflags = B_CLRBUF;
170 if (flags & IO_SYNC)
171 aflags |= B_SYNC;
172 vnode_pager_setsize(ovp, length);
173 if ((error = ext2_balloc(oip, lbn, offset + 1, cred, &bp,
174 aflags)) != 0)
175 return (error);
176 oip->i_size = length;
177 if (aflags & IO_SYNC)
178 bwrite(bp);
179 else
180 bawrite(bp);
181 oip->i_flag |= IN_CHANGE | IN_UPDATE;
182 return (ext2_update(ovp, 1));
183 }
184 /*
185 * Shorten the size of the file. If the file is not being
186 * truncated to a block boundry, the contents of the
187 * partial block following the end of the file must be
188 * zero'ed in case it ever become accessible again because
189 * of subsequent file growth.
190 */
191 /* I don't understand the comment above */
192 offset = blkoff(fs, length);
193 if (offset == 0) {
194 oip->i_size = length;
195 } else {
196 lbn = lblkno(fs, length);
197 aflags = B_CLRBUF;
198 if (flags & IO_SYNC)
199 aflags |= B_SYNC;
200 if ((error = ext2_balloc(oip, lbn, offset, cred, &bp,
201 aflags)) != 0)
202 return (error);
203 oip->i_size = length;
204 size = blksize(fs, oip, lbn);
205 bzero((char *)bp->b_data + offset, (u_int)(size - offset));
206 allocbuf(bp, size);
207 if (aflags & IO_SYNC)
208 bwrite(bp);
209 else
210 bawrite(bp);
211 }
212 /*
213 * Calculate index into inode's block list of
214 * last direct and indirect blocks (if any)
215 * which we want to keep. Lastblock is -1 when
216 * the file is truncated to 0.
217 */
218 lastblock = lblkno(fs, length + fs->s_blocksize - 1) - 1;
219 lastiblock[SINGLE] = lastblock - NDADDR;
220 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
221 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
222 nblocks = btodb(fs->s_blocksize);
223 /*
224 * Update file and block pointers on disk before we start freeing
225 * blocks. If we crash before free'ing blocks below, the blocks
226 * will be returned to the free list. lastiblock values are also
227 * normalized to -1 for calls to ext2_indirtrunc below.
228 */
229 bcopy((caddr_t)&oip->i_db[0], (caddr_t)oldblks, sizeof oldblks);
230 for (level = TRIPLE; level >= SINGLE; level--)
231 if (lastiblock[level] < 0) {
232 oip->i_ib[level] = 0;
233 lastiblock[level] = -1;
234 }
235 for (i = NDADDR - 1; i > lastblock; i--)
236 oip->i_db[i] = 0;
237 oip->i_flag |= IN_CHANGE | IN_UPDATE;
238 allerror = ext2_update(ovp, 1);
239
240 /*
241 * Having written the new inode to disk, save its new configuration
242 * and put back the old block pointers long enough to process them.
243 * Note that we save the new block configuration so we can check it
244 * when we are done.
245 */
246 bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof newblks);
247 bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof oldblks);
248 oip->i_size = osize;
249 error = vtruncbuf(ovp, cred, td, length, (int)fs->s_blocksize);
250 if (error && (allerror == 0))
251 allerror = error;
252
253 /*
254 * Indirect blocks first.
255 */
256 indir_lbn[SINGLE] = -NDADDR;
257 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
258 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
259 for (level = TRIPLE; level >= SINGLE; level--) {
260 bn = oip->i_ib[level];
261 if (bn != 0) {
262 error = ext2_indirtrunc(oip, indir_lbn[level],
263 fsbtodb(fs, bn), lastiblock[level], level, &count);
264 if (error)
265 allerror = error;
266 blocksreleased += count;
267 if (lastiblock[level] < 0) {
268 oip->i_ib[level] = 0;
269 ext2_blkfree(oip, bn, fs->s_frag_size);
270 blocksreleased += nblocks;
271 }
272 }
273 if (lastiblock[level] >= 0)
274 goto done;
275 }
276
277 /*
278 * All whole direct blocks or frags.
279 */
280 for (i = NDADDR - 1; i > lastblock; i--) {
281 long bsize;
282
283 bn = oip->i_db[i];
284 if (bn == 0)
285 continue;
286 oip->i_db[i] = 0;
287 bsize = blksize(fs, oip, i);
288 ext2_blkfree(oip, bn, bsize);
289 blocksreleased += btodb(bsize);
290 }
291 if (lastblock < 0)
292 goto done;
293
294 /*
295 * Finally, look for a change in size of the
296 * last direct block; release any frags.
297 */
298 bn = oip->i_db[lastblock];
299 if (bn != 0) {
300 long oldspace, newspace;
301
302 /*
303 * Calculate amount of space we're giving
304 * back as old block size minus new block size.
305 */
306 oldspace = blksize(fs, oip, lastblock);
307 oip->i_size = length;
308 newspace = blksize(fs, oip, lastblock);
309 if (newspace == 0)
310 panic("itrunc: newspace");
311 if (oldspace - newspace > 0) {
312 /*
313 * Block number of space to be free'd is
314 * the old block # plus the number of frags
315 * required for the storage we're keeping.
316 */
317 bn += numfrags(fs, newspace);
318 ext2_blkfree(oip, bn, oldspace - newspace);
319 blocksreleased += btodb(oldspace - newspace);
320 }
321 }
322done:
323#if DIAGNOSTIC
324 for (level = SINGLE; level <= TRIPLE; level++)
325 if (newblks[NDADDR + level] != oip->i_ib[level])
326 panic("itrunc1");
327 for (i = 0; i < NDADDR; i++)
328 if (newblks[i] != oip->i_db[i])
329 panic("itrunc2");
330 VI_LOCK(ovp);
331 if (length == 0 && (!TAILQ_EMPTY(&ovp->v_dirtyblkhd) ||
332 !TAILQ_EMPTY(&ovp->v_cleanblkhd)))
333 panic("itrunc3");
334 VI_UNLOCK(ovp);
335#endif /* DIAGNOSTIC */
336 /*
337 * Put back the real size.
338 */
339 oip->i_size = length;
340 oip->i_blocks -= blocksreleased;
341 if (oip->i_blocks < 0) /* sanity */
342 oip->i_blocks = 0;
343 oip->i_flag |= IN_CHANGE;
344 vnode_pager_setsize(ovp, length);
345 return (allerror);
346}
347
348/*
349 * Release blocks associated with the inode ip and stored in the indirect
350 * block bn. Blocks are free'd in LIFO order up to (but not including)
351 * lastbn. If level is greater than SINGLE, the block is an indirect block
352 * and recursive calls to indirtrunc must be used to cleanse other indirect
353 * blocks.
354 *
355 * NB: triple indirect blocks are untested.
356 */
357
358static int
359ext2_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
360 struct inode *ip;
361 int32_t lbn, lastbn;
362 int32_t dbn;
363 int level;
364 long *countp;
365{
366 struct buf *bp;
367 struct ext2_sb_info *fs = ip->i_e2fs;
368 struct vnode *vp;
369 int32_t *bap, *copy, nb, nlbn, last;
370 long blkcount, factor;
371 int i, nblocks, blocksreleased = 0;
372 int error = 0, allerror = 0;
373
374 /*
375 * Calculate index in current block of last
376 * block to be kept. -1 indicates the entire
377 * block so we need not calculate the index.
378 */
379 factor = 1;
380 for (i = SINGLE; i < level; i++)
381 factor *= NINDIR(fs);
382 last = lastbn;
383 if (lastbn > 0)
384 last /= factor;
385 nblocks = btodb(fs->s_blocksize);
386 /*
387 * Get buffer of block pointers, zero those entries corresponding
388 * to blocks to be free'd, and update on disk copy first. Since
389 * double(triple) indirect before single(double) indirect, calls
390 * to bmap on these blocks will fail. However, we already have
391 * the on disk address, so we have to set the b_blkno field
392 * explicitly instead of letting bread do everything for us.
393 */
394 vp = ITOV(ip);
395 bp = getblk(vp, lbn, (int)fs->s_blocksize, 0, 0, 0);
396 if (bp->b_flags & (B_DONE | B_DELWRI)) {
397 } else {
398 bp->b_iocmd = BIO_READ;
399 if (bp->b_bcount > bp->b_bufsize)
400 panic("ext2_indirtrunc: bad buffer size");
401 bp->b_blkno = dbn;
402 vfs_busy_pages(bp, 0);
403 bp->b_iooffset = dbtob(bp->b_blkno);
404 VOP_STRATEGY(vp, bp);
405 error = bufwait(bp);
406 }
407 if (error) {
408 brelse(bp);
409 *countp = 0;
410 return (error);
411 }
412
413 bap = (int32_t *)bp->b_data;
414 MALLOC(copy, int32_t *, fs->s_blocksize, M_TEMP, M_WAITOK);
415 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->s_blocksize);
416 bzero((caddr_t)&bap[last + 1],
417 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (int32_t));
418 if (last == -1)
419 bp->b_flags |= B_INVAL;
420 error = bwrite(bp);
421 if (error)
422 allerror = error;
423 bap = copy;
424
425 /*
426 * Recursively free totally unused blocks.
427 */
428 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
429 i--, nlbn += factor) {
430 nb = bap[i];
431 if (nb == 0)
432 continue;
433 if (level > SINGLE) {
434 if ((error = ext2_indirtrunc(ip, nlbn,
435 fsbtodb(fs, nb), (int32_t)-1, level - 1, &blkcount)) != 0)
436 allerror = error;
437 blocksreleased += blkcount;
438 }
439 ext2_blkfree(ip, nb, fs->s_blocksize);
440 blocksreleased += nblocks;
441 }
442
443 /*
444 * Recursively free last partial block.
445 */
446 if (level > SINGLE && lastbn >= 0) {
447 last = lastbn % factor;
448 nb = bap[i];
449 if (nb != 0) {
450 if ((error = ext2_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
451 last, level - 1, &blkcount)) != 0)
452 allerror = error;
453 blocksreleased += blkcount;
454 }
455 }
456 FREE(copy, M_TEMP);
457 *countp = blocksreleased;
458 return (allerror);
459}
460
461/*
462 * discard preallocated blocks
463 */
464int
465ext2_inactive(ap)
466 struct vop_inactive_args /* {
467 struct vnode *a_vp;
468 struct thread *a_td;
469 } */ *ap;
470{
471 struct vnode *vp = ap->a_vp;
472 struct inode *ip = VTOI(vp);
473 struct thread *td = ap->a_td;
474 int mode, error = 0;
475
476 ext2_discard_prealloc(ip);
477 if (prtactive && vrefcnt(vp) != 0)
478 vprint("ext2_inactive: pushing active", vp);
479
480 /*
481 * Ignore inodes related to stale file handles.
482 */
483 if (ip->i_mode == 0)
484 goto out;
485 if (ip->i_nlink <= 0) {
486 (void) vn_write_suspend_wait(vp, NULL, V_WAIT);
487 error = ext2_truncate(vp, (off_t)0, 0, NOCRED, td);
488 ip->i_rdev = 0;
489 mode = ip->i_mode;
490 ip->i_mode = 0;
491 ip->i_flag |= IN_CHANGE | IN_UPDATE;
492 ext2_vfree(vp, ip->i_number, mode);
493 }
494 if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) {
495 if ((ip->i_flag & (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
496 vn_write_suspend_wait(vp, NULL, V_NOWAIT)) {
497 ip->i_flag &= ~IN_ACCESS;
498 } else {
499 (void) vn_write_suspend_wait(vp, NULL, V_WAIT);
500 ext2_update(vp, 0);
501 }
502 }
503out:
504 VOP_UNLOCK(vp, 0, td);
505 /*
506 * If we are done with the inode, reclaim it
507 * so that it can be reused immediately.
508 */
509 if (ip->i_mode == 0)
510 vrecycle(vp, NULL, td);
511 return (error);
512}
513
514/*
515 * Reclaim an inode so that it can be used for other purposes.
516 */
517int
518ext2_reclaim(ap)
519 struct vop_reclaim_args /* {
520 struct vnode *a_vp;
521 struct thread *a_td;
522 } */ *ap;
523{
524 struct inode *ip;
525 struct vnode *vp = ap->a_vp;
526
527 if (prtactive && vrefcnt(vp) != 0)
528 vprint("ufs_reclaim: pushing active", vp);
529 ip = VTOI(vp);
530 if (ip->i_flag & IN_LAZYMOD) {
531 ip->i_flag |= IN_MODIFIED;
532 ext2_update(vp, 0);
533 }
534 /*
535 * Remove the inode from its hash chain.
536 */
537 ext2_ihashrem(ip);
538 /*
539 * Purge old data structures associated with the inode.
540 */
541 if (ip->i_devvp) {
542 vrele(ip->i_devvp);
543 ip->i_devvp = 0;
544 }
545 FREE(vp->v_data, M_EXT2NODE);
546 vp->v_data = 0;
547 return (0);
548}