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