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