1 /*
2 * Copyright (c) 1982, 1986, 1989, 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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)ffs_inode.c 8.13 (Berkeley) 4/21/95
30 * $FreeBSD: src/sys/ufs/ffs/ffs_inode.c,v 1.56.2.5 2002/02/05 18:35:03 dillon Exp $
31 */
32
33 #include "opt_quota.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/mount.h>
38 #include <sys/proc.h>
39 #include <sys/buf.h>
40 #include <sys/vnode.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/resourcevar.h>
44 #include <sys/vmmeter.h>
45
46 #include <vm/vm.h>
47 #include <vm/vm_extern.h>
48
49 #include "quota.h"
50 #include "ufsmount.h"
51 #include "inode.h"
52 #include "ufs_extern.h"
53
54 #include "fs.h"
55 #include "ffs_extern.h"
56
57 #include <vm/vm_page2.h>
58 #include <sys/buf2.h>
59
60 static int ffs_indirtrunc (struct inode *, ufs_daddr_t, ufs_daddr_t,
61 ufs_daddr_t, int, long *);
62
63 /*
64 * Update the access, modified, and inode change times as specified by the
65 * IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively. Write the inode
66 * to disk if the IN_MODIFIED flag is set (it may be set initially, or by
67 * the timestamp update). The IN_LAZYMOD flag is set to force a write
68 * later if not now. If we write now, then clear both IN_MODIFIED and
69 * IN_LAZYMOD to reflect the presumably successful write, and if waitfor is
70 * set, then wait for the write to complete.
71 */
72 int
ffs_update(struct vnode * vp,int waitfor)73 ffs_update(struct vnode *vp, int waitfor)
74 {
75 struct fs *fs;
76 struct buf *bp;
77 struct inode *ip;
78 int error;
79
80 ufs_itimes(vp);
81 ip = VTOI(vp);
82 if ((ip->i_flag & IN_MODIFIED) == 0 && waitfor == 0)
83 return (0);
84 ip->i_flag &= ~(IN_LAZYMOD | IN_MODIFIED);
85 fs = ip->i_fs;
86 if (fs->fs_ronly)
87 return (0);
88
89 /*
90 * The vnode type is usually set to VBAD if an unrecoverable I/O
91 * error has occured (such as when reading the inode). Clear the
92 * modified bits but do not write anything out in this case.
93 */
94 if (vp->v_type == VBAD)
95 return (0);
96 /*
97 * Ensure that uid and gid are correct. This is a temporary
98 * fix until fsck has been changed to do the update.
99 */
100 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
101 ip->i_din.di_ouid = ip->i_uid; /* XXX */
102 ip->i_din.di_ogid = ip->i_gid; /* XXX */
103 } /* XXX */
104 error = bread(ip->i_devvp,
105 fsbtodoff(fs, ino_to_fsba(fs, ip->i_number)),
106 (int)fs->fs_bsize, &bp);
107 if (error) {
108 brelse(bp);
109 return (error);
110 }
111 if (DOINGSOFTDEP(vp))
112 softdep_update_inodeblock(ip, bp, waitfor);
113 else if (ip->i_effnlink != ip->i_nlink)
114 panic("ffs_update: bad link cnt");
115 *((struct ufs1_dinode *)bp->b_data +
116 ino_to_fsbo(fs, ip->i_number)) = ip->i_din;
117 if (waitfor && !DOINGASYNC(vp)) {
118 return (bwrite(bp));
119 } else if (vm_paging_severe() || buf_dirty_count_severe()) {
120 return (bwrite(bp));
121 } else {
122 if (bp->b_bufsize == fs->fs_bsize)
123 bp->b_flags |= B_CLUSTEROK;
124 bdwrite(bp);
125 return (0);
126 }
127 }
128
129 #define SINGLE 0 /* index of single indirect block */
130 #define DOUBLE 1 /* index of double indirect block */
131 #define TRIPLE 2 /* index of triple indirect block */
132 /*
133 * Truncate the inode oip to at most length size, freeing the
134 * disk blocks.
135 */
136 int
ffs_truncate(struct vnode * vp,off_t length,int flags,struct ucred * cred)137 ffs_truncate(struct vnode *vp, off_t length, int flags, struct ucred *cred)
138 {
139 struct vnode *ovp = vp;
140 ufs_daddr_t lastblock;
141 struct inode *oip;
142 ufs_daddr_t bn, lbn, lastiblock[UFS_NIADDR], indir_lbn[UFS_NIADDR];
143 ufs_daddr_t oldblks[UFS_NDADDR + UFS_NIADDR];
144 ufs_daddr_t newblks[UFS_NDADDR + UFS_NIADDR];
145 struct fs *fs;
146 struct buf *bp;
147 int offset, size, level;
148 long count, nblocks, blocksreleased = 0;
149 int i;
150 int aflags, error, allerror;
151 off_t osize;
152
153 oip = VTOI(ovp);
154 fs = oip->i_fs;
155 if (length < 0)
156 return (EINVAL);
157 if (length > fs->fs_maxfilesize)
158 return (EFBIG);
159 if (ovp->v_type == VLNK &&
160 (oip->i_size < ovp->v_mount->mnt_maxsymlinklen || oip->i_din.di_blocks == 0)) {
161 #ifdef DIAGNOSTIC
162 if (length != 0)
163 panic("ffs_truncate: partial truncate of symlink");
164 #endif /* DIAGNOSTIC */
165 bzero((char *)&oip->i_shortlink, (uint)oip->i_size);
166 oip->i_size = 0;
167 oip->i_flag |= IN_CHANGE | IN_UPDATE;
168 return (ffs_update(ovp, 1));
169 }
170 if (oip->i_size == length) {
171 oip->i_flag |= IN_CHANGE | IN_UPDATE;
172 return (ffs_update(ovp, 0));
173 }
174 if (fs->fs_ronly)
175 panic("ffs_truncate: read-only filesystem");
176 #ifdef QUOTA
177 error = ufs_getinoquota(oip);
178 if (error)
179 return (error);
180 #endif
181 if (DOINGSOFTDEP(ovp)) {
182 if (length > 0 || softdep_slowdown(ovp)) {
183 /*
184 * If a file is only partially truncated, then
185 * we have to clean up the data structures
186 * describing the allocation past the truncation
187 * point. Finding and deallocating those structures
188 * is a lot of work. Since partial truncation occurs
189 * rarely, we solve the problem by syncing the file
190 * so that it will have no data structures left.
191 */
192 if ((error = VOP_FSYNC(ovp, MNT_WAIT, 0)) != 0)
193 return (error);
194 } else {
195 #ifdef QUOTA
196 (void) ufs_chkdq(oip, -oip->i_blocks, NOCRED, 0);
197 #endif
198 softdep_setup_freeblocks(oip, length);
199 vinvalbuf(ovp, 0, 0, 0);
200 nvnode_pager_setsize(ovp, 0, fs->fs_bsize, 0);
201 oip->i_flag |= IN_CHANGE | IN_UPDATE;
202 return (ffs_update(ovp, 0));
203 }
204 }
205 osize = oip->i_size;
206
207 /*
208 * Lengthen the size of the file. We must ensure that the
209 * last byte of the file is allocated. Since the smallest
210 * value of osize is 0, length will be at least 1.
211 *
212 * nvextendbuf() only breads the old buffer. The blocksize
213 * of the new buffer must be specified so it knows how large
214 * to make the VM object.
215 */
216 if (osize < length) {
217 nvextendbuf(vp, osize, length,
218 blkoffsize(fs, oip, osize), /* oblksize */
219 blkoffresize(fs, length), /* nblksize */
220 blkoff(fs, osize),
221 blkoff(fs, length),
222 0);
223
224 aflags = B_CLRBUF;
225 if (flags & IO_SYNC)
226 aflags |= B_SYNC;
227 /* BALLOC will reallocate the fragment at the old EOF */
228 error = VOP_BALLOC(ovp, length - 1, 1, cred, aflags, &bp);
229 if (error)
230 return (error);
231 oip->i_size = length;
232 if (bp->b_bufsize == fs->fs_bsize)
233 bp->b_flags |= B_CLUSTEROK;
234 if (aflags & B_SYNC)
235 bwrite(bp);
236 else
237 bawrite(bp);
238 oip->i_flag |= IN_CHANGE | IN_UPDATE;
239 return (ffs_update(ovp, 1));
240 }
241
242 /*
243 * Shorten the size of the file.
244 *
245 * NOTE: The block size specified in nvtruncbuf() is the blocksize
246 * of the buffer containing length prior to any reallocation
247 * of the block.
248 */
249 allerror = nvtruncbuf(ovp, length, blkoffsize(fs, oip, length),
250 blkoff(fs, length), 0);
251 offset = blkoff(fs, length);
252 if (offset == 0) {
253 oip->i_size = length;
254 } else {
255 lbn = lblkno(fs, length);
256 aflags = B_CLRBUF;
257 if (flags & IO_SYNC)
258 aflags |= B_SYNC;
259 error = VOP_BALLOC(ovp, length - 1, 1, cred, aflags, &bp);
260 if (error)
261 return (error);
262
263 /*
264 * When we are doing soft updates and the UFS_BALLOC
265 * above fills in a direct block hole with a full sized
266 * block that will be truncated down to a fragment below,
267 * we must flush out the block dependency with an FSYNC
268 * so that we do not get a soft updates inconsistency
269 * when we create the fragment below.
270 *
271 * nvtruncbuf() may have re-dirtied the underlying block
272 * as part of its truncation zeroing code. To avoid a
273 * 'locking against myself' panic in the second fsync we
274 * can simply undirty the bp since the redirtying was
275 * related to areas of the buffer that we are going to
276 * throw away anyway, and we will b*write() the remainder
277 * anyway down below.
278 */
279 if (DOINGSOFTDEP(ovp) && lbn < UFS_NDADDR &&
280 fragroundup(fs, blkoff(fs, length)) < fs->fs_bsize) {
281 bundirty(bp);
282 error = VOP_FSYNC(ovp, MNT_WAIT, 0);
283 if (error) {
284 bdwrite(bp);
285 return (error);
286 }
287 }
288 oip->i_size = length;
289 size = blksize(fs, oip, lbn);
290 #if 0
291 /* remove - nvtruncbuf deals with this */
292 if (ovp->v_type != VDIR)
293 bzero((char *)bp->b_data + offset,
294 (uint)(size - offset));
295 #endif
296 /* Kirk's code has reallocbuf(bp, size, 1) here */
297 allocbuf(bp, size);
298 if (bp->b_bufsize == fs->fs_bsize)
299 bp->b_flags |= B_CLUSTEROK;
300 if (aflags & B_SYNC)
301 bwrite(bp);
302 else
303 bawrite(bp);
304 }
305 /*
306 * Calculate index into inode's block list of
307 * last direct and indirect blocks (if any)
308 * which we want to keep. Lastblock is -1 when
309 * the file is truncated to 0.
310 */
311 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
312 lastiblock[SINGLE] = lastblock - UFS_NDADDR;
313 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
314 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
315 nblocks = btodb(fs->fs_bsize);
316
317 /*
318 * Update file and block pointers on disk before we start freeing
319 * blocks. If we crash before free'ing blocks below, the blocks
320 * will be returned to the free list. lastiblock values are also
321 * normalized to -1 for calls to ffs_indirtrunc below.
322 */
323 for (level = TRIPLE; level >= SINGLE; level--) {
324 oldblks[UFS_NDADDR + level] = oip->i_ib[level];
325 if (lastiblock[level] < 0) {
326 oip->i_ib[level] = 0;
327 lastiblock[level] = -1;
328 }
329 }
330 for (i = 0; i < UFS_NDADDR; i++) {
331 oldblks[i] = oip->i_db[i];
332 if (i > lastblock)
333 oip->i_db[i] = 0;
334 }
335 oip->i_flag |= IN_CHANGE | IN_UPDATE;
336 error = ffs_update(ovp, 1);
337 if (error && allerror == 0)
338 allerror = error;
339
340 /*
341 * Having written the new inode to disk, save its new configuration
342 * and put back the old block pointers long enough to process them.
343 * Note that we save the new block configuration so we can check it
344 * when we are done.
345 */
346 for (i = 0; i < UFS_NDADDR; i++) {
347 newblks[i] = oip->i_db[i];
348 oip->i_db[i] = oldblks[i];
349 }
350 for (i = 0; i < UFS_NIADDR; i++) {
351 newblks[UFS_NDADDR + i] = oip->i_ib[i];
352 oip->i_ib[i] = oldblks[UFS_NDADDR + i];
353 }
354 oip->i_size = osize;
355
356 if (error && allerror == 0)
357 allerror = error;
358
359 /*
360 * Indirect blocks first.
361 */
362 indir_lbn[SINGLE] = -UFS_NDADDR;
363 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
364 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
365 for (level = TRIPLE; level >= SINGLE; level--) {
366 bn = oip->i_ib[level];
367 if (bn != 0) {
368 error = ffs_indirtrunc(oip, indir_lbn[level],
369 fsbtodb(fs, bn), lastiblock[level], level, &count);
370 if (error)
371 allerror = error;
372 blocksreleased += count;
373 if (lastiblock[level] < 0) {
374 oip->i_ib[level] = 0;
375 ffs_blkfree(oip, bn, fs->fs_bsize);
376 blocksreleased += nblocks;
377 }
378 }
379 if (lastiblock[level] >= 0)
380 goto done;
381 }
382
383 /*
384 * All whole direct blocks or frags.
385 */
386 for (i = UFS_NDADDR - 1; i > lastblock; i--) {
387 long bsize;
388
389 bn = oip->i_db[i];
390 if (bn == 0)
391 continue;
392 oip->i_db[i] = 0;
393 bsize = blksize(fs, oip, i);
394 ffs_blkfree(oip, bn, bsize);
395 blocksreleased += btodb(bsize);
396 }
397 if (lastblock < 0)
398 goto done;
399
400 /*
401 * Finally, look for a change in size of the
402 * last direct block; release any frags.
403 */
404 bn = oip->i_db[lastblock];
405 if (bn != 0) {
406 long oldspace, newspace;
407
408 /*
409 * Calculate amount of space we're giving
410 * back as old block size minus new block size.
411 */
412 oldspace = blksize(fs, oip, lastblock);
413 oip->i_size = length;
414 newspace = blksize(fs, oip, lastblock);
415 if (newspace == 0)
416 panic("ffs_truncate: newspace");
417 if (oldspace - newspace > 0) {
418 /*
419 * Block number of space to be free'd is
420 * the old block # plus the number of frags
421 * required for the storage we're keeping.
422 */
423 bn += numfrags(fs, newspace);
424 ffs_blkfree(oip, bn, oldspace - newspace);
425 blocksreleased += btodb(oldspace - newspace);
426 }
427 }
428 done:
429 #ifdef DIAGNOSTIC
430 for (level = SINGLE; level <= TRIPLE; level++)
431 if (newblks[UFS_NDADDR + level] != oip->i_ib[level])
432 panic("ffs_truncate1");
433 for (i = 0; i < UFS_NDADDR; i++)
434 if (newblks[i] != oip->i_db[i])
435 panic("ffs_truncate2");
436 if (length == 0 && !RB_EMPTY(&ovp->v_rbdirty_tree))
437 panic("ffs_truncate3");
438 #endif /* DIAGNOSTIC */
439 /*
440 * Put back the real size.
441 */
442 oip->i_size = length;
443 oip->i_blocks -= blocksreleased;
444
445 if (oip->i_blocks < 0) /* sanity */
446 oip->i_blocks = 0;
447 oip->i_flag |= IN_CHANGE;
448 #ifdef QUOTA
449 (void) ufs_chkdq(oip, -blocksreleased, NOCRED, 0);
450 #endif
451 return (allerror);
452 }
453
454 /*
455 * Release blocks associated with the inode ip and stored in the indirect
456 * block bn. Blocks are free'd in LIFO order up to (but not including)
457 * lastbn. If level is greater than SINGLE, the block is an indirect block
458 * and recursive calls to indirtrunc must be used to cleanse other indirect
459 * blocks.
460 *
461 * NB: triple indirect blocks are untested.
462 */
463 static int
ffs_indirtrunc(struct inode * ip,ufs_daddr_t lbn,ufs_daddr_t dbn,ufs_daddr_t lastbn,int level,long * countp)464 ffs_indirtrunc(struct inode *ip, ufs_daddr_t lbn, ufs_daddr_t dbn,
465 ufs_daddr_t lastbn, int level, long *countp)
466 {
467 int i;
468 struct buf *bp;
469 struct fs *fs = ip->i_fs;
470 ufs_daddr_t *bap;
471 struct vnode *vp;
472 ufs_daddr_t *copy = NULL, nb, nlbn, last;
473 long blkcount, factor;
474 int nblocks, blocksreleased = 0;
475 int error = 0, allerror = 0;
476
477 /*
478 * Calculate index in current block of last
479 * block to be kept. -1 indicates the entire
480 * block so we need not calculate the index.
481 */
482 factor = 1;
483 for (i = SINGLE; i < level; i++)
484 factor *= NINDIR(fs);
485 last = lastbn;
486 if (lastbn > 0)
487 last /= factor;
488 nblocks = btodb(fs->fs_bsize);
489 /*
490 * Get buffer of block pointers, zero those entries corresponding
491 * to blocks to be free'd, and update on disk copy first. Since
492 * double(triple) indirect before single(double) indirect, calls
493 * to bmap on these blocks will fail. However, we already have
494 * the on disk address, so we have to set the bio_offset field
495 * explicitly instead of letting bread do everything for us.
496 */
497 vp = ITOV(ip);
498 bp = getblk(vp, lblktodoff(fs, lbn), (int)fs->fs_bsize, 0, 0);
499 if ((bp->b_flags & B_CACHE) == 0) {
500 bp->b_flags &= ~(B_ERROR|B_INVAL);
501 bp->b_cmd = BUF_CMD_READ;
502 if (bp->b_bcount > bp->b_bufsize)
503 panic("ffs_indirtrunc: bad buffer size");
504 /*
505 * BIO is bio2 which chains back to bio1. We wait
506 * on bio1.
507 */
508 bp->b_bio2.bio_offset = dbtodoff(fs, dbn);
509 bp->b_bio1.bio_done = biodone_sync;
510 bp->b_bio1.bio_flags |= BIO_SYNC;
511 vfs_busy_pages(vp, bp);
512 /*
513 * Access the block device layer using the device vnode
514 * and the translated block number (bio2) instead of the
515 * file vnode (vp) and logical block number (bio1).
516 *
517 * Even though we are bypassing the vnode layer, we still
518 * want the vnode state to indicate that an I/O on its behalf
519 * is in progress.
520 */
521 bio_start_transaction(&bp->b_bio1, &vp->v_track_read);
522 vn_strategy(ip->i_devvp, &bp->b_bio2);
523 error = biowait(&bp->b_bio1, "biord");
524 }
525 if (error) {
526 brelse(bp);
527 *countp = 0;
528 return (error);
529 }
530
531 bap = (ufs_daddr_t *)bp->b_data;
532 if (lastbn != -1) {
533 copy = kmalloc(fs->fs_bsize, M_TEMP, M_WAITOK);
534 bcopy((caddr_t)bap, (caddr_t)copy, (uint)fs->fs_bsize);
535 bzero((caddr_t)&bap[last + 1],
536 (uint)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t));
537 if (DOINGASYNC(vp)) {
538 bawrite(bp);
539 } else {
540 error = bwrite(bp);
541 if (error)
542 allerror = error;
543 }
544 bap = copy;
545 }
546
547 /*
548 * Recursively free totally unused blocks.
549 */
550 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
551 i--, nlbn += factor) {
552 nb = bap[i];
553 if (nb == 0)
554 continue;
555 if (level > SINGLE) {
556 if ((error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
557 (ufs_daddr_t)-1, level - 1, &blkcount)) != 0)
558 allerror = error;
559 blocksreleased += blkcount;
560 }
561 ffs_blkfree(ip, nb, fs->fs_bsize);
562 blocksreleased += nblocks;
563 }
564
565 /*
566 * Recursively free last partial block.
567 */
568 if (level > SINGLE && lastbn >= 0) {
569 last = lastbn % factor;
570 nb = bap[i];
571 if (nb != 0) {
572 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
573 last, level - 1, &blkcount);
574 if (error)
575 allerror = error;
576 blocksreleased += blkcount;
577 }
578 }
579 if (copy != NULL) {
580 kfree(copy, M_TEMP);
581 } else {
582 bp->b_flags |= B_INVAL | B_NOCACHE;
583 brelse(bp);
584 }
585
586 *countp = blocksreleased;
587 return (allerror);
588 }
589