Deleted Added
sdiff udiff text old ( 34901 ) new ( 34961 )
full compact
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 * $Id: ffs_inode.c,v 1.39 1998/03/26 20:53:49 phk Exp $
35 */
36
37#include "opt_quota.h"
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/mount.h>
42#include <sys/proc.h>
43#include <sys/buf.h>
44#include <sys/vnode.h>
45#include <sys/kernel.h>
46#include <sys/malloc.h>
47#include <sys/resourcevar.h>
48
49#include <vm/vm.h>
50#include <vm/vm_extern.h>
51
52#include <ufs/ufs/quota.h>
53#include <ufs/ufs/ufsmount.h>
54#include <ufs/ufs/inode.h>
55
56#include <ufs/ffs/fs.h>
57#include <ufs/ffs/ffs_extern.h>
58
59static int ffs_indirtrunc __P((struct inode *, ufs_daddr_t, ufs_daddr_t,
60 ufs_daddr_t, int, long *));
61
62/*
63 * Update the access, modified, and inode change times as specified by the
64 * IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively. The IN_MODIFIED
65 * flag is used to specify that the inode needs to be updated even if none
66 * of the times needs to be updated. The access and modified times are taken
67 * from the second and third parameters; the inode change time is always
68 * taken from the current time. If waitfor is set, then wait for the disk
69 * write of the inode to complete.
70 */
71int
72ffs_update(vp, access, modify, waitfor)
73 struct vnode *vp;
74 struct timeval *access;
75 struct timeval *modify;
76 int waitfor;
77{
78 register struct fs *fs;
79 struct buf *bp;
80 struct inode *ip;
81 int error;
82
83 ip = VTOI(vp);
84 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
85 ip->i_flag &=
86 ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
87 return (0);
88 }
89 if (((ip->i_flag &
90 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0) &&
91 (waitfor != MNT_WAIT))
92 return (0);
93 /*
94 * XXX: Some callers make a copy too early (before the i/o has
95 * completed)...
96 */
97 if (ip->i_flag & IN_ACCESS)
98 ip->i_atime = access->tv_sec;
99 if (ip->i_flag & IN_UPDATE) {
100 ip->i_mtime = modify->tv_sec;
101 ip->i_modrev++;
102 }
103 if (ip->i_flag & IN_CHANGE)
104 ip->i_ctime = time_second;
105 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
106 fs = ip->i_fs;
107 /*
108 * Ensure that uid and gid are correct. This is a temporary
109 * fix until fsck has been changed to do the update.
110 */
111 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
112 ip->i_din.di_ouid = ip->i_uid; /* XXX */
113 ip->i_din.di_ogid = ip->i_gid; /* XXX */
114 } /* XXX */
115 error = bread(ip->i_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
116 (int)fs->fs_bsize, NOCRED, &bp);
117 if (error) {
118 brelse(bp);
119 return (error);
120 }
121 if (DOINGSOFTDEP(vp))
122 softdep_update_inodeblock(ip, bp, waitfor);
123 else if (ip->i_effnlink != ip->i_nlink)
124 panic("ffs_update: bad link cnt");
125 *((struct dinode *)bp->b_data +
126 ino_to_fsbo(fs, ip->i_number)) = ip->i_din;
127 if (waitfor && (vp->v_mount->mnt_flag & MNT_ASYNC) == 0) {
128 return (bwrite(bp));
129 } else {
130 if (bp->b_bufsize == fs->fs_bsize)
131 bp->b_flags |= B_CLUSTEROK;
132 bdwrite(bp);
133 return (0);
134 }
135}
136
137#define SINGLE 0 /* index of single indirect block */
138#define DOUBLE 1 /* index of double indirect block */
139#define TRIPLE 2 /* index of triple indirect block */
140/*
141 * Truncate the inode oip to at most length size, freeing the
142 * disk blocks.
143 */
144int
145ffs_truncate(vp, length, flags, cred, p)
146 struct vnode *vp;
147 off_t length;
148 int flags;
149 struct ucred *cred;
150 struct proc *p;
151{
152 register struct vnode *ovp = vp;
153 ufs_daddr_t lastblock;
154 register struct inode *oip;
155 ufs_daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
156 ufs_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
157 register struct fs *fs;
158 struct buf *bp;
159 int offset, size, level;
160 long count, nblocks, vflags, blocksreleased = 0;
161 struct timeval tv;
162 register int i;
163 int aflags, error, allerror;
164 off_t osize;
165
166 oip = VTOI(ovp);
167 if (oip->i_size == length)
168 return (0);
169 fs = oip->i_fs;
170 if (length < 0)
171 return (EINVAL);
172 if (length > fs->fs_maxfilesize)
173 return (EFBIG);
174 getmicrotime(&tv);
175 if (ovp->v_type == VLNK &&
176 (oip->i_size < ovp->v_mount->mnt_maxsymlinklen || oip->i_din.di_blocks == 0)) {
177#ifdef DIAGNOSTIC
178 if (length != 0)
179 panic("ffs_truncate: partial truncate of symlink");
180#endif
181 bzero((char *)&oip->i_shortlink, (u_int)oip->i_size);
182 oip->i_size = 0;
183 oip->i_flag |= IN_CHANGE | IN_UPDATE;
184 return (UFS_UPDATE(ovp, &tv, &tv, 1));
185 }
186 if (oip->i_size == length) {
187 oip->i_flag |= IN_CHANGE | IN_UPDATE;
188 return (UFS_UPDATE(ovp, &tv, &tv, 0));
189 }
190#ifdef QUOTA
191 error = getinoquota(oip);
192 if (error)
193 return (error);
194#endif
195 ovp->v_lasta = ovp->v_clen = ovp->v_cstart = ovp->v_lastw = 0;
196 if (DOINGSOFTDEP(ovp)) {
197 if (length > 0) {
198 /*
199 * If a file is only partially truncated, then
200 * we have to clean up the data structures
201 * describing the allocation past the truncation
202 * point. Finding and deallocating those structures
203 * is a lot of work. Since partial truncation occurs
204 * rarely, we solve the problem by syncing the file
205 * so that it will have no data structures left.
206 */
207 if ((error = VOP_FSYNC(ovp, cred, MNT_WAIT,
208 p)) != 0)
209 return (error);
210 } else {
211#ifdef QUOTA
212 (void) chkdq(oip, -oip->i_blocks, NOCRED, 0);
213#endif
214 softdep_setup_freeblocks(oip, length);
215 (void) vtruncbuf(ovp, cred, p, length, fs->fs_bsize);
216 oip->i_flag |= IN_CHANGE | IN_UPDATE;
217 return (ffs_update(ovp, &tv, &tv, 0));
218 }
219 }
220 osize = oip->i_size;
221 /*
222 * Lengthen the size of the file. We must ensure that the
223 * last byte of the file is allocated. Since the smallest
224 * value of osize is 0, length will be at least 1.
225 */
226 if (osize < length) {
227 vnode_pager_setsize(ovp, length);
228 aflags = B_CLRBUF;
229 if (flags & IO_SYNC)
230 aflags |= B_SYNC;
231 error = VOP_BALLOC(ovp, length - 1, 1,
232 cred, aflags, &bp);
233 if (error)
234 return (error);
235 oip->i_size = length;
236 if (bp->b_bufsize == fs->fs_bsize)
237 bp->b_flags |= B_CLUSTEROK;
238 if (aflags & B_SYNC)
239 bwrite(bp);
240 else if (ovp->v_mount->mnt_flag & MNT_ASYNC)
241 bdwrite(bp);
242 else
243 bawrite(bp);
244 oip->i_flag |= IN_CHANGE | IN_UPDATE;
245 return (UFS_UPDATE(ovp, &tv, &tv, 1));
246 }
247 /*
248 * Shorten the size of the file. If the file is not being
249 * truncated to a block boundry, the contents of the
250 * partial block following the end of the file must be
251 * zero'ed in case it ever become accessable again because
252 * of subsequent file growth.
253 */
254 offset = blkoff(fs, length);
255 if (offset == 0) {
256 oip->i_size = length;
257 } else {
258 lbn = lblkno(fs, length);
259 aflags = B_CLRBUF;
260 if (flags & IO_SYNC)
261 aflags |= B_SYNC;
262 error = VOP_BALLOC(ovp, length - 1, 1, cred, aflags, &bp);
263 if (error) {
264 return (error);
265 }
266 oip->i_size = length;
267 size = blksize(fs, oip, lbn);
268 bzero((char *)bp->b_data + offset, (u_int)(size - offset));
269 allocbuf(bp, size);
270 if (bp->b_bufsize == fs->fs_bsize)
271 bp->b_flags |= B_CLUSTEROK;
272 if (aflags & B_SYNC)
273 bwrite(bp);
274 else if (ovp->v_mount->mnt_flag & MNT_ASYNC)
275 bdwrite(bp);
276 else
277 bawrite(bp);
278 }
279 /*
280 * Calculate index into inode's block list of
281 * last direct and indirect blocks (if any)
282 * which we want to keep. Lastblock is -1 when
283 * the file is truncated to 0.
284 */
285 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
286 lastiblock[SINGLE] = lastblock - NDADDR;
287 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
288 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
289 nblocks = btodb(fs->fs_bsize);
290 /*
291 * Update file and block pointers on disk before we start freeing
292 * blocks. If we crash before free'ing blocks below, the blocks
293 * will be returned to the free list. lastiblock values are also
294 * normalized to -1 for calls to ffs_indirtrunc below.
295 */
296 bcopy((caddr_t)&oip->i_db[0], (caddr_t)oldblks, sizeof oldblks);
297 for (level = TRIPLE; level >= SINGLE; level--)
298 if (lastiblock[level] < 0) {
299 oip->i_ib[level] = 0;
300 lastiblock[level] = -1;
301 }
302 for (i = NDADDR - 1; i > lastblock; i--)
303 oip->i_db[i] = 0;
304 oip->i_flag |= IN_CHANGE | IN_UPDATE;
305 allerror = UFS_UPDATE(ovp, &tv, &tv, ((length > 0) ? 0 : 1));
306
307 /*
308 * Having written the new inode to disk, save its new configuration
309 * and put back the old block pointers long enough to process them.
310 * Note that we save the new block configuration so we can check it
311 * when we are done.
312 */
313 bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof newblks);
314 bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof oldblks);
315 oip->i_size = osize;
316
317 error = vtruncbuf(ovp, cred, p, length, fs->fs_bsize);
318 if (error && (allerror == 0))
319 allerror = error;
320
321 /*
322 * Indirect blocks first.
323 */
324 indir_lbn[SINGLE] = -NDADDR;
325 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
326 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
327 for (level = TRIPLE; level >= SINGLE; level--) {
328 bn = oip->i_ib[level];
329 if (bn != 0) {
330 error = ffs_indirtrunc(oip, indir_lbn[level],
331 fsbtodb(fs, bn), lastiblock[level], level, &count);
332 if (error)
333 allerror = error;
334 blocksreleased += count;
335 if (lastiblock[level] < 0) {
336 oip->i_ib[level] = 0;
337 ffs_blkfree(oip, bn, fs->fs_bsize);
338 blocksreleased += nblocks;
339 }
340 }
341 if (lastiblock[level] >= 0)
342 goto done;
343 }
344
345 /*
346 * All whole direct blocks or frags.
347 */
348 for (i = NDADDR - 1; i > lastblock; i--) {
349 register long bsize;
350
351 bn = oip->i_db[i];
352 if (bn == 0)
353 continue;
354 oip->i_db[i] = 0;
355 bsize = blksize(fs, oip, i);
356 ffs_blkfree(oip, bn, bsize);
357 blocksreleased += btodb(bsize);
358 }
359 if (lastblock < 0)
360 goto done;
361
362 /*
363 * Finally, look for a change in size of the
364 * last direct block; release any frags.
365 */
366 bn = oip->i_db[lastblock];
367 if (bn != 0) {
368 long oldspace, newspace;
369
370 /*
371 * Calculate amount of space we're giving
372 * back as old block size minus new block size.
373 */
374 oldspace = blksize(fs, oip, lastblock);
375 oip->i_size = length;
376 newspace = blksize(fs, oip, lastblock);
377 if (newspace == 0)
378 panic("ffs_truncate: newspace");
379 if (oldspace - newspace > 0) {
380 /*
381 * Block number of space to be free'd is
382 * the old block # plus the number of frags
383 * required for the storage we're keeping.
384 */
385 bn += numfrags(fs, newspace);
386 ffs_blkfree(oip, bn, oldspace - newspace);
387 blocksreleased += btodb(oldspace - newspace);
388 }
389 }
390done:
391#ifdef DIAGNOSTIC
392 for (level = SINGLE; level <= TRIPLE; level++)
393 if (newblks[NDADDR + level] != oip->i_ib[level])
394 panic("ffs_truncate1");
395 for (i = 0; i < NDADDR; i++)
396 if (newblks[i] != oip->i_db[i])
397 panic("ffs_truncate2");
398 if (length == 0 &&
399 (ovp->v_dirtyblkhd.lh_first || ovp->v_cleanblkhd.lh_first))
400 panic("ffs_truncate3");
401#endif /* DIAGNOSTIC */
402 /*
403 * Put back the real size.
404 */
405 oip->i_size = length;
406 oip->i_blocks -= blocksreleased;
407
408 if (oip->i_blocks < 0) /* sanity */
409 oip->i_blocks = 0;
410 oip->i_flag |= IN_CHANGE;
411#ifdef QUOTA
412 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
413#endif
414 return (allerror);
415}
416
417/*
418 * Release blocks associated with the inode ip and stored in the indirect
419 * block bn. Blocks are free'd in LIFO order up to (but not including)
420 * lastbn. If level is greater than SINGLE, the block is an indirect block
421 * and recursive calls to indirtrunc must be used to cleanse other indirect
422 * blocks.
423 *
424 * NB: triple indirect blocks are untested.
425 */
426static int
427ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
428 register struct inode *ip;
429 ufs_daddr_t lbn, lastbn;
430 ufs_daddr_t dbn;
431 int level;
432 long *countp;
433{
434 register int i;
435 struct buf *bp;
436 register struct fs *fs = ip->i_fs;
437 register ufs_daddr_t *bap;
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
461 * the on disk address, so we have to set the b_blkno field
462 * explicitly instead of letting bread do everything for us.
463 */
464 vp = ITOV(ip);
465 bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
466 if ((bp->b_flags & B_CACHE) == 0) {
467 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */
468 bp->b_flags |= B_READ;
469 if (bp->b_bcount > bp->b_bufsize)
470 panic("ffs_indirtrunc: bad buffer size");
471 bp->b_blkno = dbn;
472 vfs_busy_pages(bp, 0);
473 VOP_STRATEGY(bp);
474 error = biowait(bp);
475 }
476 if (error) {
477 brelse(bp);
478 *countp = 0;
479 return (error);
480 }
481
482 bap = (ufs_daddr_t *)bp->b_data;
483 if (lastbn != -1) {
484 MALLOC(copy, ufs_daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
485 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
486 bzero((caddr_t)&bap[last + 1],
487 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t));
488 if ((vp->v_mount->mnt_flag & MNT_ASYNC) == 0) {
489 error = bwrite(bp);
490 if (error)
491 allerror = error;
492 } else {
493 bawrite(bp);
494 }
495 bap = copy;
496 }
497
498 /*
499 * Recursively free totally unused blocks.
500 */
501 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
502 i--, nlbn += factor) {
503 nb = bap[i];
504 if (nb == 0)
505 continue;
506 if (level > SINGLE) {
507 if (error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
508 (ufs_daddr_t)-1, level - 1, &blkcount))
509 allerror = error;
510 blocksreleased += blkcount;
511 }
512 ffs_blkfree(ip, nb, fs->fs_bsize);
513 blocksreleased += nblocks;
514 }
515
516 /*
517 * Recursively free last partial block.
518 */
519 if (level > SINGLE && lastbn >= 0) {
520 last = lastbn % factor;
521 nb = bap[i];
522 if (nb != 0) {
523 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
524 last, level - 1, &blkcount);
525 if (error)
526 allerror = error;
527 blocksreleased += blkcount;
528 }
529 }
530 if (copy != NULL) {
531 FREE(copy, M_TEMP);
532 } else {
533 bp->b_flags |= B_INVAL | B_NOCACHE;
534 brelse(bp);
535 }
536
537 *countp = blocksreleased;
538 return (allerror);
539}