Due to continuing issues with VOP_READ/VOP_WRITE ops being called without
[dragonfly.git] / sys / vfs / ufs / ffs_inode.c
... / ...
CommitLineData
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. 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 * @(#)ffs_inode.c 8.13 (Berkeley) 4/21/95
34 * $FreeBSD: src/sys/ufs/ffs/ffs_inode.c,v 1.56.2.5 2002/02/05 18:35:03 dillon Exp $
35 * $DragonFly: src/sys/vfs/ufs/ffs_inode.c,v 1.19 2006/04/07 06:38:33 dillon Exp $
36 */
37
38#include "opt_quota.h"
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/mount.h>
43#include <sys/proc.h>
44#include <sys/buf.h>
45#include <sys/vnode.h>
46#include <sys/kernel.h>
47#include <sys/malloc.h>
48#include <sys/resourcevar.h>
49#include <sys/vmmeter.h>
50
51#include <vm/vm.h>
52#include <vm/vm_extern.h>
53
54#include "quota.h"
55#include "ufsmount.h"
56#include "inode.h"
57#include "ufs_extern.h"
58
59#include "fs.h"
60#include "ffs_extern.h"
61
62#include <vm/vm_page2.h>
63
64static int ffs_indirtrunc (struct inode *, ufs_daddr_t, ufs_daddr_t,
65 ufs_daddr_t, int, long *);
66
67/*
68 * Update the access, modified, and inode change times as specified by the
69 * IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively. Write the inode
70 * to disk if the IN_MODIFIED flag is set (it may be set initially, or by
71 * the timestamp update). The IN_LAZYMOD flag is set to force a write
72 * later if not now. If we write now, then clear both IN_MODIFIED and
73 * IN_LAZYMOD to reflect the presumably successful write, and if waitfor is
74 * set, then wait for the write to complete.
75 */
76int
77ffs_update(struct vnode *vp, int waitfor)
78{
79 struct fs *fs;
80 struct buf *bp;
81 struct inode *ip;
82 int error;
83
84 ufs_itimes(vp);
85 ip = VTOI(vp);
86 if ((ip->i_flag & IN_MODIFIED) == 0 && waitfor == 0)
87 return (0);
88 ip->i_flag &= ~(IN_LAZYMOD | IN_MODIFIED);
89 fs = ip->i_fs;
90 if (fs->fs_ronly)
91 return (0);
92 /*
93 * Ensure that uid and gid are correct. This is a temporary
94 * fix until fsck has been changed to do the update.
95 */
96 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
97 ip->i_din.di_ouid = ip->i_uid; /* XXX */
98 ip->i_din.di_ogid = ip->i_gid; /* XXX */
99 } /* XXX */
100 error = bread(ip->i_devvp,
101 fsbtodoff(fs, ino_to_fsba(fs, ip->i_number)),
102 (int)fs->fs_bsize, &bp);
103 if (error) {
104 brelse(bp);
105 return (error);
106 }
107 if (DOINGSOFTDEP(vp))
108 softdep_update_inodeblock(ip, bp, waitfor);
109 else if (ip->i_effnlink != ip->i_nlink)
110 panic("ffs_update: bad link cnt");
111 *((struct ufs1_dinode *)bp->b_data +
112 ino_to_fsbo(fs, ip->i_number)) = ip->i_din;
113 if (waitfor && !DOINGASYNC(vp)) {
114 return (bwrite(bp));
115 } else if (vm_page_count_severe() || buf_dirty_count_severe()) {
116 return (bwrite(bp));
117 } else {
118 if (bp->b_bufsize == fs->fs_bsize)
119 bp->b_flags |= B_CLUSTEROK;
120 bdwrite(bp);
121 return (0);
122 }
123}
124
125#define SINGLE 0 /* index of single indirect block */
126#define DOUBLE 1 /* index of double indirect block */
127#define TRIPLE 2 /* index of triple indirect block */
128/*
129 * Truncate the inode oip to at most length size, freeing the
130 * disk blocks.
131 */
132int
133ffs_truncate(struct vnode *vp, off_t length, int flags, struct ucred *cred,
134 struct thread *td)
135{
136 struct vnode *ovp = vp;
137 ufs_daddr_t lastblock;
138 struct inode *oip;
139 ufs_daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
140 ufs_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
141 struct fs *fs;
142 struct buf *bp;
143 int offset, size, level;
144 long count, nblocks, blocksreleased = 0;
145 int i;
146 int aflags, error, allerror;
147 off_t osize;
148
149 oip = VTOI(ovp);
150 fs = oip->i_fs;
151 if (length < 0)
152 return (EINVAL);
153 if (length > fs->fs_maxfilesize)
154 return (EFBIG);
155 if (ovp->v_type == VLNK &&
156 (oip->i_size < ovp->v_mount->mnt_maxsymlinklen || oip->i_din.di_blocks == 0)) {
157#ifdef DIAGNOSTIC
158 if (length != 0)
159 panic("ffs_truncate: partial truncate of symlink");
160#endif /* DIAGNOSTIC */
161 bzero((char *)&oip->i_shortlink, (uint)oip->i_size);
162 oip->i_size = 0;
163 oip->i_flag |= IN_CHANGE | IN_UPDATE;
164 return (UFS_UPDATE(ovp, 1));
165 }
166 if (oip->i_size == length) {
167 oip->i_flag |= IN_CHANGE | IN_UPDATE;
168 return (UFS_UPDATE(ovp, 0));
169 }
170 if (fs->fs_ronly)
171 panic("ffs_truncate: read-only filesystem");
172#ifdef QUOTA
173 error = ufs_getinoquota(oip);
174 if (error)
175 return (error);
176#endif
177 ovp->v_lasta = ovp->v_clen = ovp->v_cstart = ovp->v_lastw = 0;
178 if (DOINGSOFTDEP(ovp)) {
179 if (length > 0 || softdep_slowdown(ovp)) {
180 /*
181 * If a file is only partially truncated, then
182 * we have to clean up the data structures
183 * describing the allocation past the truncation
184 * point. Finding and deallocating those structures
185 * is a lot of work. Since partial truncation occurs
186 * rarely, we solve the problem by syncing the file
187 * so that it will have no data structures left.
188 */
189 if ((error = VOP_FSYNC(ovp, MNT_WAIT, td)) != 0)
190 return (error);
191 } else {
192#ifdef QUOTA
193 (void) ufs_chkdq(oip, -oip->i_blocks, NOCRED, 0);
194#endif
195 softdep_setup_freeblocks(oip, length);
196 vinvalbuf(ovp, 0, td, 0, 0);
197 vnode_pager_setsize(ovp, 0);
198 oip->i_flag |= IN_CHANGE | IN_UPDATE;
199 return (ffs_update(ovp, 0));
200 }
201 }
202 osize = oip->i_size;
203 /*
204 * Lengthen the size of the file. We must ensure that the
205 * last byte of the file is allocated. Since the smallest
206 * value of osize is 0, length will be at least 1.
207 */
208 if (osize < length) {
209 vnode_pager_setsize(ovp, length);
210 aflags = B_CLRBUF;
211 if (flags & IO_SYNC)
212 aflags |= B_SYNC;
213 error = VOP_BALLOC(ovp, length - 1, 1,
214 cred, aflags, &bp);
215 if (error)
216 return (error);
217 oip->i_size = length;
218 if (bp->b_bufsize == fs->fs_bsize)
219 bp->b_flags |= B_CLUSTEROK;
220 if (aflags & B_SYNC)
221 bwrite(bp);
222 else
223 bawrite(bp);
224 oip->i_flag |= IN_CHANGE | IN_UPDATE;
225 return (UFS_UPDATE(ovp, 1));
226 }
227 /*
228 * Shorten the size of the file. If the file is not being
229 * truncated to a block boundary, the contents of the
230 * partial block following the end of the file must be
231 * zero'ed in case it ever becomes accessible again because
232 * of subsequent file growth. Directories however are not
233 * zero'ed as they should grow back initialized to empty.
234 */
235 offset = blkoff(fs, length);
236 if (offset == 0) {
237 oip->i_size = length;
238 } else {
239 lbn = lblkno(fs, length);
240 aflags = B_CLRBUF;
241 if (flags & IO_SYNC)
242 aflags |= B_SYNC;
243 error = VOP_BALLOC(ovp, length - 1, 1, cred, aflags, &bp);
244 if (error) {
245 return (error);
246 }
247 /*
248 * When we are doing soft updates and the UFS_BALLOC
249 * above fills in a direct block hole with a full sized
250 * block that will be truncated down to a fragment below,
251 * we must flush out the block dependency with an FSYNC
252 * so that we do not get a soft updates inconsistency
253 * when we create the fragment below.
254 */
255 if (DOINGSOFTDEP(ovp) && lbn < NDADDR &&
256 fragroundup(fs, blkoff(fs, length)) < fs->fs_bsize &&
257 (error = VOP_FSYNC(ovp, MNT_WAIT, td)) != 0) {
258 return (error);
259 }
260 oip->i_size = length;
261 size = blksize(fs, oip, lbn);
262 if (ovp->v_type != VDIR)
263 bzero((char *)bp->b_data + offset,
264 (uint)(size - offset));
265 /* Kirk's code has reallocbuf(bp, size, 1) here */
266 allocbuf(bp, size);
267 if (bp->b_bufsize == fs->fs_bsize)
268 bp->b_flags |= B_CLUSTEROK;
269 if (aflags & B_SYNC)
270 bwrite(bp);
271 else
272 bawrite(bp);
273 }
274 /*
275 * Calculate index into inode's block list of
276 * last direct and indirect blocks (if any)
277 * which we want to keep. Lastblock is -1 when
278 * the file is truncated to 0.
279 */
280 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
281 lastiblock[SINGLE] = lastblock - NDADDR;
282 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
283 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
284 nblocks = btodb(fs->fs_bsize);
285
286 /*
287 * Update file and block pointers on disk before we start freeing
288 * blocks. If we crash before free'ing blocks below, the blocks
289 * will be returned to the free list. lastiblock values are also
290 * normalized to -1 for calls to ffs_indirtrunc below.
291 */
292 bcopy((caddr_t)&oip->i_db[0], (caddr_t)oldblks, sizeof oldblks);
293 for (level = TRIPLE; level >= SINGLE; level--)
294 if (lastiblock[level] < 0) {
295 oip->i_ib[level] = 0;
296 lastiblock[level] = -1;
297 }
298 for (i = NDADDR - 1; i > lastblock; i--)
299 oip->i_db[i] = 0;
300 oip->i_flag |= IN_CHANGE | IN_UPDATE;
301 allerror = UFS_UPDATE(ovp, 1);
302
303 /*
304 * Having written the new inode to disk, save its new configuration
305 * and put back the old block pointers long enough to process them.
306 * Note that we save the new block configuration so we can check it
307 * when we are done.
308 */
309 bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof newblks);
310 bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof oldblks);
311 oip->i_size = osize;
312
313 error = vtruncbuf(ovp, td, length, fs->fs_bsize);
314 if (error && (allerror == 0))
315 allerror = error;
316
317 /*
318 * Indirect blocks first.
319 */
320 indir_lbn[SINGLE] = -NDADDR;
321 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
322 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
323 for (level = TRIPLE; level >= SINGLE; level--) {
324 bn = oip->i_ib[level];
325 if (bn != 0) {
326 error = ffs_indirtrunc(oip, indir_lbn[level],
327 fsbtodb(fs, bn), lastiblock[level], level, &count);
328 if (error)
329 allerror = error;
330 blocksreleased += count;
331 if (lastiblock[level] < 0) {
332 oip->i_ib[level] = 0;
333 ffs_blkfree(oip, bn, fs->fs_bsize);
334 blocksreleased += nblocks;
335 }
336 }
337 if (lastiblock[level] >= 0)
338 goto done;
339 }
340
341 /*
342 * All whole direct blocks or frags.
343 */
344 for (i = NDADDR - 1; i > lastblock; i--) {
345 long bsize;
346
347 bn = oip->i_db[i];
348 if (bn == 0)
349 continue;
350 oip->i_db[i] = 0;
351 bsize = blksize(fs, oip, i);
352 ffs_blkfree(oip, bn, bsize);
353 blocksreleased += btodb(bsize);
354 }
355 if (lastblock < 0)
356 goto done;
357
358 /*
359 * Finally, look for a change in size of the
360 * last direct block; release any frags.
361 */
362 bn = oip->i_db[lastblock];
363 if (bn != 0) {
364 long oldspace, newspace;
365
366 /*
367 * Calculate amount of space we're giving
368 * back as old block size minus new block size.
369 */
370 oldspace = blksize(fs, oip, lastblock);
371 oip->i_size = length;
372 newspace = blksize(fs, oip, lastblock);
373 if (newspace == 0)
374 panic("ffs_truncate: newspace");
375 if (oldspace - newspace > 0) {
376 /*
377 * Block number of space to be free'd is
378 * the old block # plus the number of frags
379 * required for the storage we're keeping.
380 */
381 bn += numfrags(fs, newspace);
382 ffs_blkfree(oip, bn, oldspace - newspace);
383 blocksreleased += btodb(oldspace - newspace);
384 }
385 }
386done:
387#ifdef DIAGNOSTIC
388 for (level = SINGLE; level <= TRIPLE; level++)
389 if (newblks[NDADDR + level] != oip->i_ib[level])
390 panic("ffs_truncate1");
391 for (i = 0; i < NDADDR; i++)
392 if (newblks[i] != oip->i_db[i])
393 panic("ffs_truncate2");
394 if (length == 0 &&
395 (!RB_EMPTY(&ovp->v_rbdirty_tree) ||
396 !RB_EMPTY(&ovp->v_rbclean_tree)))
397 panic("ffs_truncate3");
398#endif /* DIAGNOSTIC */
399 /*
400 * Put back the real size.
401 */
402 oip->i_size = length;
403 oip->i_blocks -= blocksreleased;
404
405 if (oip->i_blocks < 0) /* sanity */
406 oip->i_blocks = 0;
407 oip->i_flag |= IN_CHANGE;
408#ifdef QUOTA
409 (void) ufs_chkdq(oip, -blocksreleased, NOCRED, 0);
410#endif
411 return (allerror);
412}
413
414/*
415 * Release blocks associated with the inode ip and stored in the indirect
416 * block bn. Blocks are free'd in LIFO order up to (but not including)
417 * lastbn. If level is greater than SINGLE, the block is an indirect block
418 * and recursive calls to indirtrunc must be used to cleanse other indirect
419 * blocks.
420 *
421 * NB: triple indirect blocks are untested.
422 */
423static int
424ffs_indirtrunc(struct inode *ip, ufs_daddr_t lbn, ufs_daddr_t dbn,
425 ufs_daddr_t lastbn, int level, long *countp)
426{
427 int i;
428 struct buf *bp;
429 struct fs *fs = ip->i_fs;
430 ufs_daddr_t *bap;
431 struct vnode *vp;
432 ufs_daddr_t *copy = NULL, nb, nlbn, last;
433 long blkcount, factor;
434 int nblocks, blocksreleased = 0;
435 int error = 0, allerror = 0;
436
437 /*
438 * Calculate index in current block of last
439 * block to be kept. -1 indicates the entire
440 * block so we need not calculate the index.
441 */
442 factor = 1;
443 for (i = SINGLE; i < level; i++)
444 factor *= NINDIR(fs);
445 last = lastbn;
446 if (lastbn > 0)
447 last /= factor;
448 nblocks = btodb(fs->fs_bsize);
449 /*
450 * Get buffer of block pointers, zero those entries corresponding
451 * to blocks to be free'd, and update on disk copy first. Since
452 * double(triple) indirect before single(double) indirect, calls
453 * to bmap on these blocks will fail. However, we already have
454 * the on disk address, so we have to set the bio_offset field
455 * explicitly instead of letting bread do everything for us.
456 */
457 vp = ITOV(ip);
458 bp = getblk(vp, lblktodoff(fs, lbn), (int)fs->fs_bsize, 0, 0);
459 if ((bp->b_flags & B_CACHE) == 0) {
460 bp->b_flags |= B_READ;
461 bp->b_flags &= ~(B_ERROR|B_INVAL);
462 if (bp->b_bcount > bp->b_bufsize)
463 panic("ffs_indirtrunc: bad buffer size");
464 bp->b_bio2.bio_offset = dbtodoff(fs, dbn);
465 vfs_busy_pages(bp, 0);
466 /*
467 * Access the block device layer using the device vnode
468 * and the translated block number (bio2) instead of the
469 * file vnode (vp) and logical block number (bio1).
470 *
471 * Even though we are bypassing the vnode layer, we still
472 * want the vnode state to indicate that an I/O on its behalf
473 * is in progress.
474 */
475 bio_start_transaction(&bp->b_bio1, &vp->v_track_read);
476 vn_strategy(ip->i_devvp, &bp->b_bio2);
477 error = biowait(bp);
478 }
479 if (error) {
480 brelse(bp);
481 *countp = 0;
482 return (error);
483 }
484
485 bap = (ufs_daddr_t *)bp->b_data;
486 if (lastbn != -1) {
487 MALLOC(copy, ufs_daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
488 bcopy((caddr_t)bap, (caddr_t)copy, (uint)fs->fs_bsize);
489 bzero((caddr_t)&bap[last + 1],
490 (uint)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t));
491 if (DOINGASYNC(vp)) {
492 bawrite(bp);
493 } else {
494 error = bwrite(bp);
495 if (error)
496 allerror = error;
497 }
498 bap = copy;
499 }
500
501 /*
502 * Recursively free totally unused blocks.
503 */
504 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
505 i--, nlbn += factor) {
506 nb = bap[i];
507 if (nb == 0)
508 continue;
509 if (level > SINGLE) {
510 if ((error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
511 (ufs_daddr_t)-1, level - 1, &blkcount)) != 0)
512 allerror = error;
513 blocksreleased += blkcount;
514 }
515 ffs_blkfree(ip, nb, fs->fs_bsize);
516 blocksreleased += nblocks;
517 }
518
519 /*
520 * Recursively free last partial block.
521 */
522 if (level > SINGLE && lastbn >= 0) {
523 last = lastbn % factor;
524 nb = bap[i];
525 if (nb != 0) {
526 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
527 last, level - 1, &blkcount);
528 if (error)
529 allerror = error;
530 blocksreleased += blkcount;
531 }
532 }
533 if (copy != NULL) {
534 FREE(copy, M_TEMP);
535 } else {
536 bp->b_flags |= B_INVAL | B_NOCACHE;
537 brelse(bp);
538 }
539
540 *countp = blocksreleased;
541 return (allerror);
542}