ffs_inode.c revision 1.1 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.1 1994/06/08 11:42:04 mycroft 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 oip = VTOI(ovp);
166 tv = time;
167 if (ovp->v_type == VLNK &&
168 oip->i_size < ovp->v_mount->mnt_maxsymlinklen) {
169 #ifdef DIAGNOSTIC
170 if (length != 0)
171 panic("ffs_truncate: partial truncate of symlink");
172 #endif
173 bzero((char *)&oip->i_shortlink, (u_int)oip->i_size);
174 oip->i_size = 0;
175 oip->i_flag |= IN_CHANGE | IN_UPDATE;
176 return (VOP_UPDATE(ovp, &tv, &tv, 1));
177 }
178 if (oip->i_size == length) {
179 oip->i_flag |= IN_CHANGE | IN_UPDATE;
180 return (VOP_UPDATE(ovp, &tv, &tv, 0));
181 }
182 #ifdef QUOTA
183 if (error = getinoquota(oip))
184 return (error);
185 #endif
186 vnode_pager_setsize(ovp, (u_long)length);
187 fs = oip->i_fs;
188 osize = oip->i_size;
189 /*
190 * Lengthen the size of the file. We must ensure that the
191 * last byte of the file is allocated. Since the smallest
192 * value of oszie is 0, length will be at least 1.
193 */
194 if (osize < length) {
195 offset = blkoff(fs, length - 1);
196 lbn = lblkno(fs, length - 1);
197 aflags = B_CLRBUF;
198 if (ap->a_flags & IO_SYNC)
199 aflags |= B_SYNC;
200 if (error = ffs_balloc(oip, lbn, offset + 1, ap->a_cred, &bp,
201 aflags))
202 return (error);
203 oip->i_size = length;
204 (void) vnode_pager_uncache(ovp);
205 if (aflags & B_SYNC)
206 bwrite(bp);
207 else
208 bawrite(bp);
209 oip->i_flag |= IN_CHANGE | IN_UPDATE;
210 return (VOP_UPDATE(ovp, &tv, &tv, 1));
211 }
212 /*
213 * Shorten the size of the file. If the file is not being
214 * truncated to a block boundry, the contents of the
215 * partial block following the end of the file must be
216 * zero'ed in case it ever become accessable again because
217 * of subsequent file growth.
218 */
219 offset = blkoff(fs, length);
220 if (offset == 0) {
221 oip->i_size = length;
222 } else {
223 lbn = lblkno(fs, length);
224 aflags = B_CLRBUF;
225 if (ap->a_flags & IO_SYNC)
226 aflags |= B_SYNC;
227 if (error = ffs_balloc(oip, lbn, offset, ap->a_cred, &bp,
228 aflags))
229 return (error);
230 oip->i_size = length;
231 size = blksize(fs, oip, lbn);
232 (void) vnode_pager_uncache(ovp);
233 bzero((char *)bp->b_data + offset, (u_int)(size - offset));
234 allocbuf(bp, size);
235 if (aflags & B_SYNC)
236 bwrite(bp);
237 else
238 bawrite(bp);
239 }
240 /*
241 * Calculate index into inode's block list of
242 * last direct and indirect blocks (if any)
243 * which we want to keep. Lastblock is -1 when
244 * the file is truncated to 0.
245 */
246 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
247 lastiblock[SINGLE] = lastblock - NDADDR;
248 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
249 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
250 nblocks = btodb(fs->fs_bsize);
251 /*
252 * Update file and block pointers on disk before we start freeing
253 * blocks. If we crash before free'ing blocks below, the blocks
254 * will be returned to the free list. lastiblock values are also
255 * normalized to -1 for calls to ffs_indirtrunc below.
256 */
257 bcopy((caddr_t)&oip->i_db[0], (caddr_t)oldblks, sizeof oldblks);
258 for (level = TRIPLE; level >= SINGLE; level--)
259 if (lastiblock[level] < 0) {
260 oip->i_ib[level] = 0;
261 lastiblock[level] = -1;
262 }
263 for (i = NDADDR - 1; i > lastblock; i--)
264 oip->i_db[i] = 0;
265 oip->i_flag |= IN_CHANGE | IN_UPDATE;
266 if (error = VOP_UPDATE(ovp, &tv, &tv, MNT_WAIT))
267 allerror = error;
268 /*
269 * Having written the new inode to disk, save its new configuration
270 * and put back the old block pointers long enough to process them.
271 * Note that we save the new block configuration so we can check it
272 * when we are done.
273 */
274 bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof newblks);
275 bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof oldblks);
276 oip->i_size = osize;
277 vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA;
278 allerror = vinvalbuf(ovp, vflags, ap->a_cred, ap->a_p, 0, 0);
279
280 /*
281 * Indirect blocks first.
282 */
283 indir_lbn[SINGLE] = -NDADDR;
284 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
285 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
286 for (level = TRIPLE; level >= SINGLE; level--) {
287 bn = oip->i_ib[level];
288 if (bn != 0) {
289 error = ffs_indirtrunc(oip, indir_lbn[level],
290 fsbtodb(fs, bn), lastiblock[level], level, &count);
291 if (error)
292 allerror = error;
293 blocksreleased += count;
294 if (lastiblock[level] < 0) {
295 oip->i_ib[level] = 0;
296 ffs_blkfree(oip, bn, fs->fs_bsize);
297 blocksreleased += nblocks;
298 }
299 }
300 if (lastiblock[level] >= 0)
301 goto done;
302 }
303
304 /*
305 * All whole direct blocks or frags.
306 */
307 for (i = NDADDR - 1; i > lastblock; i--) {
308 register long bsize;
309
310 bn = oip->i_db[i];
311 if (bn == 0)
312 continue;
313 oip->i_db[i] = 0;
314 bsize = blksize(fs, oip, i);
315 ffs_blkfree(oip, bn, bsize);
316 blocksreleased += btodb(bsize);
317 }
318 if (lastblock < 0)
319 goto done;
320
321 /*
322 * Finally, look for a change in size of the
323 * last direct block; release any frags.
324 */
325 bn = oip->i_db[lastblock];
326 if (bn != 0) {
327 long oldspace, newspace;
328
329 /*
330 * Calculate amount of space we're giving
331 * back as old block size minus new block size.
332 */
333 oldspace = blksize(fs, oip, lastblock);
334 oip->i_size = length;
335 newspace = blksize(fs, oip, lastblock);
336 if (newspace == 0)
337 panic("itrunc: newspace");
338 if (oldspace - newspace > 0) {
339 /*
340 * Block number of space to be free'd is
341 * the old block # plus the number of frags
342 * required for the storage we're keeping.
343 */
344 bn += numfrags(fs, newspace);
345 ffs_blkfree(oip, bn, oldspace - newspace);
346 blocksreleased += btodb(oldspace - newspace);
347 }
348 }
349 done:
350 #ifdef DIAGNOSTIC
351 for (level = SINGLE; level <= TRIPLE; level++)
352 if (newblks[NDADDR + level] != oip->i_ib[level])
353 panic("itrunc1");
354 for (i = 0; i < NDADDR; i++)
355 if (newblks[i] != oip->i_db[i])
356 panic("itrunc2");
357 if (length == 0 &&
358 (ovp->v_dirtyblkhd.lh_first || ovp->v_cleanblkhd.lh_first))
359 panic("itrunc3");
360 #endif /* DIAGNOSTIC */
361 /*
362 * Put back the real size.
363 */
364 oip->i_size = length;
365 oip->i_blocks -= blocksreleased;
366 if (oip->i_blocks < 0) /* sanity */
367 oip->i_blocks = 0;
368 oip->i_flag |= IN_CHANGE;
369 #ifdef QUOTA
370 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
371 #endif
372 return (allerror);
373 }
374
375 /*
376 * Release blocks associated with the inode ip and stored in the indirect
377 * block bn. Blocks are free'd in LIFO order up to (but not including)
378 * lastbn. If level is greater than SINGLE, the block is an indirect block
379 * and recursive calls to indirtrunc must be used to cleanse other indirect
380 * blocks.
381 *
382 * NB: triple indirect blocks are untested.
383 */
384 static int
385 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
386 register struct inode *ip;
387 daddr_t lbn, lastbn;
388 daddr_t dbn;
389 int level;
390 long *countp;
391 {
392 register int i;
393 struct buf *bp;
394 register struct fs *fs = ip->i_fs;
395 register daddr_t *bap;
396 struct vnode *vp;
397 daddr_t *copy, nb, nlbn, last;
398 long blkcount, factor;
399 int nblocks, blocksreleased = 0;
400 int error = 0, allerror = 0;
401
402 /*
403 * Calculate index in current block of last
404 * block to be kept. -1 indicates the entire
405 * block so we need not calculate the index.
406 */
407 factor = 1;
408 for (i = SINGLE; i < level; i++)
409 factor *= NINDIR(fs);
410 last = lastbn;
411 if (lastbn > 0)
412 last /= factor;
413 nblocks = btodb(fs->fs_bsize);
414 /*
415 * Get buffer of block pointers, zero those entries corresponding
416 * to blocks to be free'd, and update on disk copy first. Since
417 * double(triple) indirect before single(double) indirect, calls
418 * to bmap on these blocks will fail. However, we already have
419 * the on disk address, so we have to set the b_blkno field
420 * explicitly instead of letting bread do everything for us.
421 */
422 vp = ITOV(ip);
423 bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
424 if (bp->b_flags & (B_DONE | B_DELWRI)) {
425 /* Braces must be here in case trace evaluates to nothing. */
426 trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
427 } else {
428 trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
429 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */
430 bp->b_flags |= B_READ;
431 if (bp->b_bcount > bp->b_bufsize)
432 panic("ffs_indirtrunc: bad buffer size");
433 bp->b_blkno = dbn;
434 VOP_STRATEGY(bp);
435 error = biowait(bp);
436 }
437 if (error) {
438 brelse(bp);
439 *countp = 0;
440 return (error);
441 }
442
443 bap = (daddr_t *)bp->b_data;
444 MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
445 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
446 bzero((caddr_t)&bap[last + 1],
447 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
448 if (last == -1)
449 bp->b_flags |= B_INVAL;
450 error = bwrite(bp);
451 if (error)
452 allerror = error;
453 bap = copy;
454
455 /*
456 * Recursively free totally unused blocks.
457 */
458 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
459 i--, nlbn += factor) {
460 nb = bap[i];
461 if (nb == 0)
462 continue;
463 if (level > SINGLE) {
464 if (error = ffs_indirtrunc(ip, nlbn,
465 fsbtodb(fs, nb), (daddr_t)-1, level - 1, &blkcount))
466 allerror = error;
467 blocksreleased += blkcount;
468 }
469 ffs_blkfree(ip, nb, fs->fs_bsize);
470 blocksreleased += nblocks;
471 }
472
473 /*
474 * Recursively free last partial block.
475 */
476 if (level > SINGLE && lastbn >= 0) {
477 last = lastbn % factor;
478 nb = bap[i];
479 if (nb != 0) {
480 if (error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
481 last, level - 1, &blkcount))
482 allerror = error;
483 blocksreleased += blkcount;
484 }
485 }
486 FREE(copy, M_TEMP);
487 *countp = blocksreleased;
488 return (allerror);
489 }
490