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