Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / vfs / gnu / ext2fs / ext2_inode.c
CommitLineData
984263bc
MD
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 * @(#)ext2_inode.c 8.5 (Berkeley) 12/30/93
40 * $FreeBSD: src/sys/gnu/ext2fs/ext2_inode.c,v 1.24.2.1 2000/08/03 00:52:57 peter Exp $
41 */
42
43#include "opt_quota.h"
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/mount.h>
48#include <sys/buf.h>
49#include <sys/vnode.h>
50#include <sys/malloc.h>
51
52#include <vm/vm.h>
53#include <vm/vm_extern.h>
54
55#include <ufs/ufs/quota.h>
56#include <ufs/ufs/inode.h>
57#include <ufs/ufs/ufsmount.h>
58#include <ufs/ufs/ufs_extern.h>
59
60#include <gnu/ext2fs/ext2_fs.h>
61#include <gnu/ext2fs/ext2_fs_sb.h>
62#include <gnu/ext2fs/fs.h>
63#include <gnu/ext2fs/ext2_extern.h>
64
65static int ext2_indirtrunc __P((struct inode *, daddr_t, daddr_t, daddr_t, int,
66 long *));
67
68int
69ext2_init(struct vfsconf *vfsp)
70{
71 return (ufs_init(vfsp));
72}
73
74/*
75 * Update the access, modified, and inode change times as specified by the
76 * IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively. Write the inode
77 * to disk if the IN_MODIFIED flag is set (it may be set initially, or by
78 * the timestamp update). The IN_LAZYMOD flag is set to force a write
79 * later if not now. If we write now, then clear both IN_MODIFIED and
80 * IN_LAZYMOD to reflect the presumably successful write, and if waitfor is
81 * set, then wait for the write to complete.
82 */
83int
84ext2_update(vp, waitfor)
85 struct vnode *vp;
86 int waitfor;
87{
88 register struct ext2_sb_info *fs;
89 struct buf *bp;
90 struct inode *ip;
91 int error;
92
93 ufs_itimes(vp);
94 ip = VTOI(vp);
95 if ((ip->i_flag & IN_MODIFIED) == 0)
96 return (0);
97 ip->i_flag &= ~(IN_LAZYMOD | IN_MODIFIED);
98 if (vp->v_mount->mnt_flag & MNT_RDONLY)
99 return (0);
100 fs = ip->i_e2fs;
101 if ((error = bread(ip->i_devvp,
102 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
103 (int)fs->s_blocksize, NOCRED, &bp)) != 0) {
104 brelse(bp);
105 return (error);
106 }
107 ext2_di2ei( &ip->i_din, (struct ext2_inode *) ((char *)bp->b_data + EXT2_INODE_SIZE *
108 ino_to_fsbo(fs, ip->i_number)));
109/*
110 if (waitfor && (vp->v_mount->mnt_flag & MNT_ASYNC) == 0)
111 return (bwrite(bp));
112 else {
113*/
114 bdwrite(bp);
115 return (0);
116/*
117 }
118*/
119}
120
121#define SINGLE 0 /* index of single indirect block */
122#define DOUBLE 1 /* index of double indirect block */
123#define TRIPLE 2 /* index of triple indirect block */
124/*
125 * Truncate the inode oip to at most length size, freeing the
126 * disk blocks.
127 */
128int
129ext2_truncate(vp, length, flags, cred, p)
130 struct vnode *vp;
131 off_t length;
132 int flags;
133 struct ucred *cred;
134 struct proc *p;
135{
136 register struct vnode *ovp = vp;
137 register daddr_t lastblock;
138 register struct inode *oip;
139 daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
140 daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
141 register struct ext2_sb_info *fs;
142 struct buf *bp;
143 int offset, size, level;
144 long count, nblocks, blocksreleased = 0;
145 register int i;
146 int aflags, error, allerror;
147 off_t osize;
148/*
149printf("ext2_truncate called %d to %d\n", VTOI(ovp)->i_number, length);
150*/ /*
151 * negative file sizes will totally break the code below and
152 * are not meaningful anyways.
153 */
154 if (length < 0)
155 return EFBIG;
156
157 oip = VTOI(ovp);
158 if (ovp->v_type == VLNK &&
159 oip->i_size < ovp->v_mount->mnt_maxsymlinklen) {
160#if DIAGNOSTIC
161 if (length != 0)
162 panic("ext2_truncate: partial truncate of symlink");
163#endif
164 bzero((char *)&oip->i_shortlink, (u_int)oip->i_size);
165 oip->i_size = 0;
166 oip->i_flag |= IN_CHANGE | IN_UPDATE;
167 return (UFS_UPDATE(ovp, 1));
168 }
169 if (oip->i_size == length) {
170 oip->i_flag |= IN_CHANGE | IN_UPDATE;
171 return (UFS_UPDATE(ovp, 0));
172 }
173#if QUOTA
174 if ((error = getinoquota(oip)) != 0)
175 return (error);
176#endif
177 fs = oip->i_e2fs;
178 osize = oip->i_size;
179 ext2_discard_prealloc(oip);
180 /*
181 * Lengthen the size of the file. We must ensure that the
182 * last byte of the file is allocated. Since the smallest
183 * value of oszie is 0, length will be at least 1.
184 */
185 if (osize < length) {
186 offset = blkoff(fs, length - 1);
187 lbn = lblkno(fs, length - 1);
188 aflags = B_CLRBUF;
189 if (flags & IO_SYNC)
190 aflags |= B_SYNC;
191 vnode_pager_setsize(ovp, length);
192 if ((error = ext2_balloc(oip, lbn, offset + 1, cred, &bp,
193 aflags)) != 0)
194 return (error);
195 oip->i_size = length;
196 if (aflags & IO_SYNC)
197 bwrite(bp);
198 else
199 bawrite(bp);
200 oip->i_flag |= IN_CHANGE | IN_UPDATE;
201 return (UFS_UPDATE(ovp, 1));
202 }
203 /*
204 * Shorten the size of the file. If the file is not being
205 * truncated to a block boundry, the contents of the
206 * partial block following the end of the file must be
207 * zero'ed in case it ever become accessable again because
208 * of subsequent file growth.
209 */
210 /* I don't understand the comment above */
211 offset = blkoff(fs, length);
212 if (offset == 0) {
213 oip->i_size = length;
214 } else {
215 lbn = lblkno(fs, length);
216 aflags = B_CLRBUF;
217 if (flags & IO_SYNC)
218 aflags |= B_SYNC;
219 if ((error = ext2_balloc(oip, lbn, offset, cred, &bp,
220 aflags)) != 0)
221 return (error);
222 oip->i_size = length;
223 size = blksize(fs, oip, lbn);
224 bzero((char *)bp->b_data + offset, (u_int)(size - offset));
225 allocbuf(bp, size);
226 if (aflags & IO_SYNC)
227 bwrite(bp);
228 else
229 bawrite(bp);
230 }
231 /*
232 * Calculate index into inode's block list of
233 * last direct and indirect blocks (if any)
234 * which we want to keep. Lastblock is -1 when
235 * the file is truncated to 0.
236 */
237 lastblock = lblkno(fs, length + fs->s_blocksize - 1) - 1;
238 lastiblock[SINGLE] = lastblock - NDADDR;
239 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
240 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
241 nblocks = btodb(fs->s_blocksize);
242 /*
243 * Update file and block pointers on disk before we start freeing
244 * blocks. If we crash before free'ing blocks below, the blocks
245 * will be returned to the free list. lastiblock values are also
246 * normalized to -1 for calls to ext2_indirtrunc below.
247 */
248 bcopy((caddr_t)&oip->i_db[0], (caddr_t)oldblks, sizeof oldblks);
249 for (level = TRIPLE; level >= SINGLE; level--)
250 if (lastiblock[level] < 0) {
251 oip->i_ib[level] = 0;
252 lastiblock[level] = -1;
253 }
254 for (i = NDADDR - 1; i > lastblock; i--)
255 oip->i_db[i] = 0;
256 oip->i_flag |= IN_CHANGE | IN_UPDATE;
257 allerror = UFS_UPDATE(ovp, 1);
258
259 /*
260 * Having written the new inode to disk, save its new configuration
261 * and put back the old block pointers long enough to process them.
262 * Note that we save the new block configuration so we can check it
263 * when we are done.
264 */
265 bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof newblks);
266 bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof oldblks);
267 oip->i_size = osize;
268 error = vtruncbuf(ovp, cred, p, length, (int)fs->s_blocksize);
269 if (error && (allerror == 0))
270 allerror = error;
271
272 /*
273 * Indirect blocks first.
274 */
275 indir_lbn[SINGLE] = -NDADDR;
276 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
277 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
278 for (level = TRIPLE; level >= SINGLE; level--) {
279 bn = oip->i_ib[level];
280 if (bn != 0) {
281 error = ext2_indirtrunc(oip, indir_lbn[level],
282 fsbtodb(fs, bn), lastiblock[level], level, &count);
283 if (error)
284 allerror = error;
285 blocksreleased += count;
286 if (lastiblock[level] < 0) {
287 oip->i_ib[level] = 0;
288 ext2_blkfree(oip, bn, fs->s_frag_size);
289 blocksreleased += nblocks;
290 }
291 }
292 if (lastiblock[level] >= 0)
293 goto done;
294 }
295
296 /*
297 * All whole direct blocks or frags.
298 */
299 for (i = NDADDR - 1; i > lastblock; i--) {
300 register long bsize;
301
302 bn = oip->i_db[i];
303 if (bn == 0)
304 continue;
305 oip->i_db[i] = 0;
306 bsize = blksize(fs, oip, i);
307 ext2_blkfree(oip, bn, bsize);
308 blocksreleased += btodb(bsize);
309 }
310 if (lastblock < 0)
311 goto done;
312
313 /*
314 * Finally, look for a change in size of the
315 * last direct block; release any frags.
316 */
317 bn = oip->i_db[lastblock];
318 if (bn != 0) {
319 long oldspace, newspace;
320
321 /*
322 * Calculate amount of space we're giving
323 * back as old block size minus new block size.
324 */
325 oldspace = blksize(fs, oip, lastblock);
326 oip->i_size = length;
327 newspace = blksize(fs, oip, lastblock);
328 if (newspace == 0)
329 panic("itrunc: newspace");
330 if (oldspace - newspace > 0) {
331 /*
332 * Block number of space to be free'd is
333 * the old block # plus the number of frags
334 * required for the storage we're keeping.
335 */
336 bn += numfrags(fs, newspace);
337 ext2_blkfree(oip, bn, oldspace - newspace);
338 blocksreleased += btodb(oldspace - newspace);
339 }
340 }
341done:
342#if DIAGNOSTIC
343 for (level = SINGLE; level <= TRIPLE; level++)
344 if (newblks[NDADDR + level] != oip->i_ib[level])
345 panic("itrunc1");
346 for (i = 0; i < NDADDR; i++)
347 if (newblks[i] != oip->i_db[i])
348 panic("itrunc2");
349 if (length == 0 && (!TAILQ_EMPTY(&ovp->v_dirtyblkhd) ||
350 !TAILQ_EMPTY(&ovp->v_cleanblkhd)))
351 panic("itrunc3");
352#endif /* DIAGNOSTIC */
353 /*
354 * Put back the real size.
355 */
356 oip->i_size = length;
357 oip->i_blocks -= blocksreleased;
358 if (oip->i_blocks < 0) /* sanity */
359 oip->i_blocks = 0;
360 oip->i_flag |= IN_CHANGE;
361 vnode_pager_setsize(ovp, length);
362#if QUOTA
363 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
364#endif
365 return (allerror);
366}
367
368/*
369 * Release blocks associated with the inode ip and stored in the indirect
370 * block bn. Blocks are free'd in LIFO order up to (but not including)
371 * lastbn. If level is greater than SINGLE, the block is an indirect block
372 * and recursive calls to indirtrunc must be used to cleanse other indirect
373 * blocks.
374 *
375 * NB: triple indirect blocks are untested.
376 */
377
378static int
379ext2_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
380 register struct inode *ip;
381 daddr_t lbn, lastbn;
382 daddr_t dbn;
383 int level;
384 long *countp;
385{
386 register int i;
387 struct buf *bp;
388 register struct ext2_sb_info *fs = ip->i_e2fs;
389 register daddr_t *bap;
390 struct vnode *vp;
391 daddr_t *copy, nb, nlbn, last;
392 long blkcount, factor;
393 int nblocks, blocksreleased = 0;
394 int error = 0, allerror = 0;
395
396 /*
397 * Calculate index in current block of last
398 * block to be kept. -1 indicates the entire
399 * block so we need not calculate the index.
400 */
401 factor = 1;
402 for (i = SINGLE; i < level; i++)
403 factor *= NINDIR(fs);
404 last = lastbn;
405 if (lastbn > 0)
406 last /= factor;
407 nblocks = btodb(fs->s_blocksize);
408 /*
409 * Get buffer of block pointers, zero those entries corresponding
410 * to blocks to be free'd, and update on disk copy first. Since
411 * double(triple) indirect before single(double) indirect, calls
412 * to bmap on these blocks will fail. However, we already have
413 * the on disk address, so we have to set the b_blkno field
414 * explicitly instead of letting bread do everything for us.
415 */
416 vp = ITOV(ip);
417 bp = getblk(vp, lbn, (int)fs->s_blocksize, 0, 0);
418 if (bp->b_flags & (B_DONE | B_DELWRI)) {
419 } else {
420 bp->b_flags |= B_READ;
421 if (bp->b_bcount > bp->b_bufsize)
422 panic("ext2_indirtrunc: bad buffer size");
423 bp->b_blkno = dbn;
424 vfs_busy_pages(bp, 0);
425 VOP_STRATEGY(vp, bp);
426 error = biowait(bp);
427 }
428 if (error) {
429 brelse(bp);
430 *countp = 0;
431 return (error);
432 }
433
434 bap = (daddr_t *)bp->b_data;
435 MALLOC(copy, daddr_t *, fs->s_blocksize, M_TEMP, M_WAITOK);
436 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->s_blocksize);
437 bzero((caddr_t)&bap[last + 1],
438 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
439 if (last == -1)
440 bp->b_flags |= B_INVAL;
441 error = bwrite(bp);
442 if (error)
443 allerror = error;
444 bap = copy;
445
446 /*
447 * Recursively free totally unused blocks.
448 */
449 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
450 i--, nlbn += factor) {
451 nb = bap[i];
452 if (nb == 0)
453 continue;
454 if (level > SINGLE) {
455 if ((error = ext2_indirtrunc(ip, nlbn,
456 fsbtodb(fs, nb), (daddr_t)-1, level - 1, &blkcount)) != 0)
457 allerror = error;
458 blocksreleased += blkcount;
459 }
460 ext2_blkfree(ip, nb, fs->s_blocksize);
461 blocksreleased += nblocks;
462 }
463
464 /*
465 * Recursively free last partial block.
466 */
467 if (level > SINGLE && lastbn >= 0) {
468 last = lastbn % factor;
469 nb = bap[i];
470 if (nb != 0) {
471 if ((error = ext2_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
472 last, level - 1, &blkcount)) != 0)
473 allerror = error;
474 blocksreleased += blkcount;
475 }
476 }
477 FREE(copy, M_TEMP);
478 *countp = blocksreleased;
479 return (allerror);
480}
481
482/*
483 * discard preallocated blocks
484 */
485int
486ext2_inactive(ap)
487 struct vop_inactive_args /* {
488 struct vnode *a_vp;
489 } */ *ap;
490{
491 ext2_discard_prealloc(VTOI(ap->a_vp));
492 return ufs_inactive(ap);
493}
494