A number of structures related to UFS and QUOTAS have changed name.
[dragonfly.git] / sys / vfs / ufs / ffs_inode.c
CommitLineData
984263bc
MD
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 $
b461a088 35 * $DragonFly: src/sys/vfs/ufs/ffs_inode.c,v 1.17 2006/03/29 21:59:57 dillon Exp $
984263bc
MD
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
1f2de5d4
MD
54#include "quota.h"
55#include "ufsmount.h"
56#include "inode.h"
57#include "ufs_extern.h"
984263bc 58
1f2de5d4
MD
59#include "fs.h"
60#include "ffs_extern.h"
984263bc 61
12e4aaff
MD
62#include <vm/vm_page2.h>
63
a6ee311a
RG
64static int ffs_indirtrunc (struct inode *, ufs_daddr_t, ufs_daddr_t,
65 ufs_daddr_t, int, long *);
984263bc
MD
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
0973c589 77ffs_update(struct vnode *vp, int waitfor)
984263bc 78{
3ff2135f 79 struct fs *fs;
984263bc
MD
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 */
54078292
MD
100 error = bread(ip->i_devvp,
101 fsbtodoff(fs, ino_to_fsba(fs, ip->i_number)),
102 (int)fs->fs_bsize, &bp);
984263bc
MD
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 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
0973c589
CP
133ffs_truncate(struct vnode *vp, off_t length, int flags, struct ucred *cred,
134 struct thread *td)
984263bc 135{
3ff2135f 136 struct vnode *ovp = vp;
984263bc 137 ufs_daddr_t lastblock;
3ff2135f 138 struct inode *oip;
984263bc
MD
139 ufs_daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
140 ufs_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
3ff2135f 141 struct fs *fs;
984263bc
MD
142 struct buf *bp;
143 int offset, size, level;
144 long count, nblocks, blocksreleased = 0;
3ff2135f 145 int i;
984263bc
MD
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");
f719c866
DR
160#endif /* DIAGNOSTIC */
161 bzero((char *)&oip->i_shortlink, (uint)oip->i_size);
984263bc
MD
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 = getinoquota(oip);
174 if (error)
175 return (error);
176#endif
b461a088
MD
177 /*
178 * truncation can occur for a variety of reasons where an OPEN has
179 * not been performed. truncate(), rmdir(), and remove() being
180 * examples. Vnode-based buffer cache ops require a VM object.
181 */
182 if (vp->v_object == NULL)
183 vinitvmio(vp);
984263bc
MD
184 ovp->v_lasta = ovp->v_clen = ovp->v_cstart = ovp->v_lastw = 0;
185 if (DOINGSOFTDEP(ovp)) {
186 if (length > 0 || softdep_slowdown(ovp)) {
187 /*
188 * If a file is only partially truncated, then
189 * we have to clean up the data structures
190 * describing the allocation past the truncation
191 * point. Finding and deallocating those structures
192 * is a lot of work. Since partial truncation occurs
193 * rarely, we solve the problem by syncing the file
194 * so that it will have no data structures left.
195 */
3b568787 196 if ((error = VOP_FSYNC(ovp, MNT_WAIT, td)) != 0)
984263bc
MD
197 return (error);
198 } else {
199#ifdef QUOTA
200 (void) chkdq(oip, -oip->i_blocks, NOCRED, 0);
201#endif
202 softdep_setup_freeblocks(oip, length);
3b568787 203 vinvalbuf(ovp, 0, td, 0, 0);
0e8fa33c 204 vnode_pager_setsize(ovp, 0);
984263bc
MD
205 oip->i_flag |= IN_CHANGE | IN_UPDATE;
206 return (ffs_update(ovp, 0));
207 }
208 }
209 osize = oip->i_size;
210 /*
211 * Lengthen the size of the file. We must ensure that the
212 * last byte of the file is allocated. Since the smallest
213 * value of osize is 0, length will be at least 1.
214 */
215 if (osize < length) {
216 vnode_pager_setsize(ovp, length);
217 aflags = B_CLRBUF;
218 if (flags & IO_SYNC)
219 aflags |= B_SYNC;
220 error = VOP_BALLOC(ovp, length - 1, 1,
221 cred, aflags, &bp);
222 if (error)
223 return (error);
224 oip->i_size = length;
225 if (bp->b_bufsize == fs->fs_bsize)
226 bp->b_flags |= B_CLUSTEROK;
227 if (aflags & B_SYNC)
228 bwrite(bp);
229 else
230 bawrite(bp);
231 oip->i_flag |= IN_CHANGE | IN_UPDATE;
232 return (UFS_UPDATE(ovp, 1));
233 }
234 /*
235 * Shorten the size of the file. If the file is not being
236 * truncated to a block boundary, the contents of the
237 * partial block following the end of the file must be
238 * zero'ed in case it ever becomes accessible again because
239 * of subsequent file growth. Directories however are not
240 * zero'ed as they should grow back initialized to empty.
241 */
242 offset = blkoff(fs, length);
243 if (offset == 0) {
244 oip->i_size = length;
245 } else {
246 lbn = lblkno(fs, length);
247 aflags = B_CLRBUF;
248 if (flags & IO_SYNC)
249 aflags |= B_SYNC;
250 error = VOP_BALLOC(ovp, length - 1, 1, cred, aflags, &bp);
251 if (error) {
252 return (error);
253 }
254 /*
255 * When we are doing soft updates and the UFS_BALLOC
256 * above fills in a direct block hole with a full sized
257 * block that will be truncated down to a fragment below,
258 * we must flush out the block dependency with an FSYNC
259 * so that we do not get a soft updates inconsistency
260 * when we create the fragment below.
261 */
262 if (DOINGSOFTDEP(ovp) && lbn < NDADDR &&
263 fragroundup(fs, blkoff(fs, length)) < fs->fs_bsize &&
3b568787 264 (error = VOP_FSYNC(ovp, MNT_WAIT, td)) != 0) {
984263bc
MD
265 return (error);
266 }
267 oip->i_size = length;
268 size = blksize(fs, oip, lbn);
269 if (ovp->v_type != VDIR)
270 bzero((char *)bp->b_data + offset,
f719c866 271 (uint)(size - offset));
984263bc
MD
272 /* Kirk's code has reallocbuf(bp, size, 1) here */
273 allocbuf(bp, size);
274 if (bp->b_bufsize == fs->fs_bsize)
275 bp->b_flags |= B_CLUSTEROK;
276 if (aflags & B_SYNC)
277 bwrite(bp);
278 else
279 bawrite(bp);
280 }
281 /*
282 * Calculate index into inode's block list of
283 * last direct and indirect blocks (if any)
284 * which we want to keep. Lastblock is -1 when
285 * the file is truncated to 0.
286 */
287 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
288 lastiblock[SINGLE] = lastblock - NDADDR;
289 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
290 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
291 nblocks = btodb(fs->fs_bsize);
0f1737b3 292
984263bc
MD
293 /*
294 * Update file and block pointers on disk before we start freeing
295 * blocks. If we crash before free'ing blocks below, the blocks
296 * will be returned to the free list. lastiblock values are also
297 * normalized to -1 for calls to ffs_indirtrunc below.
298 */
299 bcopy((caddr_t)&oip->i_db[0], (caddr_t)oldblks, sizeof oldblks);
300 for (level = TRIPLE; level >= SINGLE; level--)
301 if (lastiblock[level] < 0) {
302 oip->i_ib[level] = 0;
303 lastiblock[level] = -1;
304 }
305 for (i = NDADDR - 1; i > lastblock; i--)
306 oip->i_db[i] = 0;
307 oip->i_flag |= IN_CHANGE | IN_UPDATE;
308 allerror = UFS_UPDATE(ovp, 1);
309
310 /*
311 * Having written the new inode to disk, save its new configuration
312 * and put back the old block pointers long enough to process them.
313 * Note that we save the new block configuration so we can check it
314 * when we are done.
315 */
316 bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof newblks);
317 bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof oldblks);
318 oip->i_size = osize;
319
3b568787 320 error = vtruncbuf(ovp, td, length, fs->fs_bsize);
984263bc
MD
321 if (error && (allerror == 0))
322 allerror = error;
323
324 /*
325 * Indirect blocks first.
326 */
327 indir_lbn[SINGLE] = -NDADDR;
328 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
329 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
330 for (level = TRIPLE; level >= SINGLE; level--) {
331 bn = oip->i_ib[level];
332 if (bn != 0) {
333 error = ffs_indirtrunc(oip, indir_lbn[level],
334 fsbtodb(fs, bn), lastiblock[level], level, &count);
335 if (error)
336 allerror = error;
337 blocksreleased += count;
338 if (lastiblock[level] < 0) {
339 oip->i_ib[level] = 0;
340 ffs_blkfree(oip, bn, fs->fs_bsize);
341 blocksreleased += nblocks;
342 }
343 }
344 if (lastiblock[level] >= 0)
345 goto done;
346 }
347
348 /*
349 * All whole direct blocks or frags.
350 */
351 for (i = NDADDR - 1; i > lastblock; i--) {
3ff2135f 352 long bsize;
984263bc
MD
353
354 bn = oip->i_db[i];
355 if (bn == 0)
356 continue;
357 oip->i_db[i] = 0;
358 bsize = blksize(fs, oip, i);
359 ffs_blkfree(oip, bn, bsize);
360 blocksreleased += btodb(bsize);
361 }
362 if (lastblock < 0)
363 goto done;
364
365 /*
366 * Finally, look for a change in size of the
367 * last direct block; release any frags.
368 */
369 bn = oip->i_db[lastblock];
370 if (bn != 0) {
371 long oldspace, newspace;
372
373 /*
374 * Calculate amount of space we're giving
375 * back as old block size minus new block size.
376 */
377 oldspace = blksize(fs, oip, lastblock);
378 oip->i_size = length;
379 newspace = blksize(fs, oip, lastblock);
380 if (newspace == 0)
381 panic("ffs_truncate: newspace");
382 if (oldspace - newspace > 0) {
383 /*
384 * Block number of space to be free'd is
385 * the old block # plus the number of frags
386 * required for the storage we're keeping.
387 */
388 bn += numfrags(fs, newspace);
389 ffs_blkfree(oip, bn, oldspace - newspace);
390 blocksreleased += btodb(oldspace - newspace);
391 }
392 }
393done:
394#ifdef DIAGNOSTIC
395 for (level = SINGLE; level <= TRIPLE; level++)
396 if (newblks[NDADDR + level] != oip->i_ib[level])
397 panic("ffs_truncate1");
398 for (i = 0; i < NDADDR; i++)
399 if (newblks[i] != oip->i_db[i])
400 panic("ffs_truncate2");
401 if (length == 0 &&
6bae6177
MD
402 (!RB_EMPTY(&ovp->v_rbdirty_tree) ||
403 !RB_EMPTY(&ovp->v_rbclean_tree)))
984263bc
MD
404 panic("ffs_truncate3");
405#endif /* DIAGNOSTIC */
406 /*
407 * Put back the real size.
408 */
409 oip->i_size = length;
410 oip->i_blocks -= blocksreleased;
411
412 if (oip->i_blocks < 0) /* sanity */
413 oip->i_blocks = 0;
414 oip->i_flag |= IN_CHANGE;
415#ifdef QUOTA
416 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
417#endif
418 return (allerror);
419}
420
421/*
422 * Release blocks associated with the inode ip and stored in the indirect
423 * block bn. Blocks are free'd in LIFO order up to (but not including)
424 * lastbn. If level is greater than SINGLE, the block is an indirect block
425 * and recursive calls to indirtrunc must be used to cleanse other indirect
426 * blocks.
427 *
428 * NB: triple indirect blocks are untested.
429 */
430static int
0973c589
CP
431ffs_indirtrunc(struct inode *ip, ufs_daddr_t lbn, ufs_daddr_t dbn,
432 ufs_daddr_t lastbn, int level, long *countp)
984263bc 433{
3ff2135f 434 int i;
984263bc 435 struct buf *bp;
3ff2135f
RG
436 struct fs *fs = ip->i_fs;
437 ufs_daddr_t *bap;
984263bc
MD
438 struct vnode *vp;
439 ufs_daddr_t *copy = NULL, nb, nlbn, last;
440 long blkcount, factor;
441 int nblocks, blocksreleased = 0;
442 int error = 0, allerror = 0;
443
444 /*
445 * Calculate index in current block of last
446 * block to be kept. -1 indicates the entire
447 * block so we need not calculate the index.
448 */
449 factor = 1;
450 for (i = SINGLE; i < level; i++)
451 factor *= NINDIR(fs);
452 last = lastbn;
453 if (lastbn > 0)
454 last /= factor;
455 nblocks = btodb(fs->fs_bsize);
456 /*
457 * Get buffer of block pointers, zero those entries corresponding
458 * to blocks to be free'd, and update on disk copy first. Since
459 * double(triple) indirect before single(double) indirect, calls
460 * to bmap on these blocks will fail. However, we already have
54078292 461 * the on disk address, so we have to set the bio_offset field
984263bc
MD
462 * explicitly instead of letting bread do everything for us.
463 */
464 vp = ITOV(ip);
54078292 465 bp = getblk(vp, lblktodoff(fs, lbn), (int)fs->fs_bsize, 0, 0);
984263bc 466 if ((bp->b_flags & B_CACHE) == 0) {
984263bc
MD
467 bp->b_flags |= B_READ;
468 bp->b_flags &= ~(B_ERROR|B_INVAL);
469 if (bp->b_bcount > bp->b_bufsize)
470 panic("ffs_indirtrunc: bad buffer size");
54078292 471 bp->b_bio2.bio_offset = dbtodoff(fs, dbn);
984263bc 472 vfs_busy_pages(bp, 0);
81b5c339
MD
473 /*
474 * Access the block device layer using the device vnode
475 * and the translated block number (bio2) instead of the
476 * file vnode (vp) and logical block number (bio1).
477 *
478 * Even though we are bypassing the vnode layer, we still
479 * want the vnode state to indicate that an I/O on its behalf
480 * is in progress.
481 */
482 bio_start_transaction(&bp->b_bio1, &vp->v_track_read);
483 vn_strategy(ip->i_devvp, &bp->b_bio2);
984263bc
MD
484 error = biowait(bp);
485 }
486 if (error) {
487 brelse(bp);
488 *countp = 0;
489 return (error);
490 }
491
492 bap = (ufs_daddr_t *)bp->b_data;
493 if (lastbn != -1) {
494 MALLOC(copy, ufs_daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
f719c866 495 bcopy((caddr_t)bap, (caddr_t)copy, (uint)fs->fs_bsize);
984263bc 496 bzero((caddr_t)&bap[last + 1],
f719c866 497 (uint)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t));
984263bc
MD
498 if (DOINGASYNC(vp)) {
499 bawrite(bp);
500 } else {
501 error = bwrite(bp);
502 if (error)
503 allerror = error;
504 }
505 bap = copy;
506 }
507
508 /*
509 * Recursively free totally unused blocks.
510 */
511 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
512 i--, nlbn += factor) {
513 nb = bap[i];
514 if (nb == 0)
515 continue;
516 if (level > SINGLE) {
517 if ((error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
518 (ufs_daddr_t)-1, level - 1, &blkcount)) != 0)
519 allerror = error;
520 blocksreleased += blkcount;
521 }
522 ffs_blkfree(ip, nb, fs->fs_bsize);
523 blocksreleased += nblocks;
524 }
525
526 /*
527 * Recursively free last partial block.
528 */
529 if (level > SINGLE && lastbn >= 0) {
530 last = lastbn % factor;
531 nb = bap[i];
532 if (nb != 0) {
533 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
534 last, level - 1, &blkcount);
535 if (error)
536 allerror = error;
537 blocksreleased += blkcount;
538 }
539 }
540 if (copy != NULL) {
541 FREE(copy, M_TEMP);
542 } else {
543 bp->b_flags |= B_INVAL | B_NOCACHE;
544 brelse(bp);
545 }
546
547 *countp = blocksreleased;
548 return (allerror);
549}