ffs_inode.c revision 1.83 1 /* $NetBSD: ffs_inode.c,v 1.83 2006/06/23 14:13:02 yamt Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)ffs_inode.c 8.13 (Berkeley) 4/21/95
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: ffs_inode.c,v 1.83 2006/06/23 14:13:02 yamt Exp $");
36
37 #if defined(_KERNEL_OPT)
38 #include "opt_ffs.h"
39 #include "opt_quota.h"
40 #endif
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/mount.h>
45 #include <sys/proc.h>
46 #include <sys/file.h>
47 #include <sys/buf.h>
48 #include <sys/vnode.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/trace.h>
52 #include <sys/resourcevar.h>
53 #include <sys/kauth.h>
54
55 #include <ufs/ufs/quota.h>
56 #include <ufs/ufs/inode.h>
57 #include <ufs/ufs/ufsmount.h>
58 #include <ufs/ufs/ufs_extern.h>
59 #include <ufs/ufs/ufs_bswap.h>
60
61 #include <ufs/ffs/fs.h>
62 #include <ufs/ffs/ffs_extern.h>
63
64 static int ffs_indirtrunc(struct inode *, daddr_t, daddr_t, daddr_t, int,
65 int64_t *);
66
67 /*
68 * Update the access, modified, and inode change times as specified
69 * by the IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively.
70 * The IN_MODIFIED flag is used to specify that the inode needs to be
71 * updated but that the times have already been set. The access
72 * and modified times are taken from the second and third parameters;
73 * the inode change time is always taken from the current time. If
74 * UPDATE_WAIT flag is set, or UPDATE_DIROP is set and we are not doing
75 * softupdates, then wait for the disk write of the inode to complete.
76 */
77
78 int
79 ffs_update(struct vnode *vp, const struct timespec *acc,
80 const struct timespec *mod, int updflags)
81 {
82 struct fs *fs;
83 struct buf *bp;
84 struct inode *ip;
85 int error;
86 caddr_t cp;
87 int waitfor, flags;
88
89 if (vp->v_mount->mnt_flag & MNT_RDONLY)
90 return (0);
91 ip = VTOI(vp);
92 FFS_ITIMES(ip, acc, mod, NULL);
93 if (updflags & UPDATE_CLOSE)
94 flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED);
95 else
96 flags = ip->i_flag & IN_MODIFIED;
97 if (flags == 0)
98 return (0);
99 fs = ip->i_fs;
100
101 if ((flags & IN_MODIFIED) != 0 &&
102 (vp->v_mount->mnt_flag & MNT_ASYNC) == 0) {
103 waitfor = updflags & UPDATE_WAIT;
104 if ((updflags & UPDATE_DIROP) && !DOINGSOFTDEP(vp))
105 waitfor |= UPDATE_WAIT;
106 } else
107 waitfor = 0;
108
109 /*
110 * Ensure that uid and gid are correct. This is a temporary
111 * fix until fsck has been changed to do the update.
112 */
113 if (fs->fs_magic == FS_UFS1_MAGIC && /* XXX */
114 fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */
115 ip->i_ffs1_ouid = ip->i_uid; /* XXX */
116 ip->i_ffs1_ogid = ip->i_gid; /* XXX */
117 } /* XXX */
118 error = bread(ip->i_devvp,
119 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
120 (int)fs->fs_bsize, NOCRED, &bp);
121 if (error) {
122 brelse(bp);
123 return (error);
124 }
125 ip->i_flag &= ~(IN_MODIFIED | IN_ACCESSED);
126 if (DOINGSOFTDEP(vp))
127 softdep_update_inodeblock(ip, bp, waitfor);
128 else if (ip->i_ffs_effnlink != ip->i_nlink)
129 panic("ffs_update: bad link cnt");
130 if (fs->fs_magic == FS_UFS1_MAGIC) {
131 cp = (caddr_t)bp->b_data +
132 (ino_to_fsbo(fs, ip->i_number) * DINODE1_SIZE);
133 #ifdef FFS_EI
134 if (UFS_FSNEEDSWAP(fs))
135 ffs_dinode1_swap(ip->i_din.ffs1_din,
136 (struct ufs1_dinode *)cp);
137 else
138 #endif
139 memcpy(cp, ip->i_din.ffs1_din, DINODE1_SIZE);
140 } else {
141 cp = (caddr_t)bp->b_data +
142 (ino_to_fsbo(fs, ip->i_number) * DINODE2_SIZE);
143 #ifdef FFS_EI
144 if (UFS_FSNEEDSWAP(fs))
145 ffs_dinode2_swap(ip->i_din.ffs2_din,
146 (struct ufs2_dinode *)cp);
147 else
148 #endif
149 memcpy(cp, ip->i_din.ffs2_din, DINODE2_SIZE);
150 }
151 if (waitfor) {
152 return (bwrite(bp));
153 } else {
154 bdwrite(bp);
155 return (0);
156 }
157 }
158
159 #define SINGLE 0 /* index of single indirect block */
160 #define DOUBLE 1 /* index of double indirect block */
161 #define TRIPLE 2 /* index of triple indirect block */
162 /*
163 * Truncate the inode oip to at most length size, freeing the
164 * disk blocks.
165 */
166 int
167 ffs_truncate(struct vnode *ovp, off_t length, int ioflag, kauth_cred_t cred,
168 struct lwp *l)
169 {
170 struct genfs_node *gp = VTOG(ovp);
171 daddr_t lastblock;
172 struct inode *oip = VTOI(ovp);
173 daddr_t bn, lastiblock[NIADDR], indir_lbn[NIADDR];
174 daddr_t blks[NDADDR + NIADDR];
175 struct fs *fs;
176 int offset, size, level;
177 int64_t count, blocksreleased = 0;
178 int i, aflag, nblocks;
179 int error, allerror = 0;
180 off_t osize;
181 int sync;
182 struct ufsmount *ump = oip->i_ump;
183
184 if (ovp->v_type == VCHR || ovp->v_type == VBLK ||
185 ovp->v_type == VFIFO || ovp->v_type == VSOCK) {
186 KASSERT(oip->i_size == 0);
187 return 0;
188 }
189
190 if (length < 0)
191 return (EINVAL);
192
193 if (ovp->v_type == VLNK &&
194 (oip->i_size < ump->um_maxsymlinklen ||
195 (ump->um_maxsymlinklen == 0 && DIP(oip, blocks) == 0))) {
196 KDASSERT(length == 0);
197 memset(SHORTLINK(oip), 0, (size_t)oip->i_size);
198 oip->i_size = 0;
199 DIP_ASSIGN(oip, size, 0);
200 oip->i_flag |= IN_CHANGE | IN_UPDATE;
201 return (ffs_update(ovp, NULL, NULL, 0));
202 }
203 if (oip->i_size == length) {
204 oip->i_flag |= IN_CHANGE | IN_UPDATE;
205 return (ffs_update(ovp, NULL, NULL, 0));
206 }
207 #ifdef QUOTA
208 if ((error = getinoquota(oip)) != 0)
209 return (error);
210 #endif
211 fs = oip->i_fs;
212 if (length > ump->um_maxfilesize)
213 return (EFBIG);
214
215 if ((oip->i_flags & SF_SNAPSHOT) != 0)
216 ffs_snapremove(ovp);
217
218 osize = oip->i_size;
219 aflag = ioflag & IO_SYNC ? B_SYNC : 0;
220
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
227 if (osize < length) {
228 if (lblkno(fs, osize) < NDADDR &&
229 lblkno(fs, osize) != lblkno(fs, length) &&
230 blkroundup(fs, osize) != osize) {
231 off_t eob;
232
233 eob = blkroundup(fs, osize);
234 error = ufs_balloc_range(ovp, osize, eob - osize,
235 cred, aflag);
236 if (error)
237 return error;
238 if (ioflag & IO_SYNC) {
239 ovp->v_size = eob;
240 simple_lock(&ovp->v_interlock);
241 VOP_PUTPAGES(ovp,
242 trunc_page(osize & fs->fs_bmask),
243 round_page(eob), PGO_CLEANIT | PGO_SYNCIO);
244 }
245 }
246 error = ufs_balloc_range(ovp, length - 1, 1, cred, aflag);
247 if (error) {
248 (void) ffs_truncate(ovp, osize, ioflag & IO_SYNC,
249 cred, l);
250 return (error);
251 }
252 uvm_vnp_setsize(ovp, length);
253 oip->i_flag |= IN_CHANGE | IN_UPDATE;
254 KASSERT(ovp->v_size == oip->i_size);
255 return (ffs_update(ovp, NULL, NULL, 0));
256 }
257
258 /*
259 * When truncating a regular file down to a non-block-aligned size,
260 * we must zero the part of last block which is past the new EOF.
261 * We must synchronously flush the zeroed pages to disk
262 * since the new pages will be invalidated as soon as we
263 * inform the VM system of the new, smaller size.
264 * We must do this before acquiring the GLOCK, since fetching
265 * the pages will acquire the GLOCK internally.
266 * So there is a window where another thread could see a whole
267 * zeroed page past EOF, but that's life.
268 */
269
270 offset = blkoff(fs, length);
271 if (ovp->v_type == VREG && offset != 0 && osize > length) {
272 daddr_t lbn;
273 voff_t eoz;
274
275 error = ufs_balloc_range(ovp, length - 1, 1, cred, aflag);
276 if (error)
277 return error;
278 lbn = lblkno(fs, length);
279 size = blksize(fs, oip, lbn);
280 eoz = MIN(lblktosize(fs, lbn) + size, osize);
281 uvm_vnp_zerorange(ovp, length, eoz - length);
282 if (round_page(eoz) > round_page(length)) {
283 simple_lock(&ovp->v_interlock);
284 error = VOP_PUTPAGES(ovp, round_page(length),
285 round_page(eoz),
286 PGO_CLEANIT | PGO_DEACTIVATE |
287 ((ioflag & IO_SYNC) ? PGO_SYNCIO : 0));
288 if (error)
289 return error;
290 }
291 }
292
293 lockmgr(&gp->g_glock, LK_EXCLUSIVE, NULL);
294
295 if (DOINGSOFTDEP(ovp)) {
296 if (length > 0) {
297 /*
298 * If a file is only partially truncated, then
299 * we have to clean up the data structures
300 * describing the allocation past the truncation
301 * point. Finding and deallocating those structures
302 * is a lot of work. Since partial truncation occurs
303 * rarely, we solve the problem by syncing the file
304 * so that it will have no data structures left.
305 */
306 if ((error = VOP_FSYNC(ovp, cred, FSYNC_WAIT,
307 0, 0, l)) != 0) {
308 lockmgr(&gp->g_glock, LK_RELEASE, NULL);
309 return (error);
310 }
311 if (oip->i_flag & IN_SPACECOUNTED)
312 fs->fs_pendingblocks -= DIP(oip, blocks);
313 } else {
314 uvm_vnp_setsize(ovp, length);
315 #ifdef QUOTA
316 (void) chkdq(oip, -DIP(oip, blocks), NOCRED, 0);
317 #endif
318 softdep_setup_freeblocks(oip, length, 0);
319 (void) vinvalbuf(ovp, 0, cred, l, 0, 0);
320 lockmgr(&gp->g_glock, LK_RELEASE, NULL);
321 oip->i_flag |= IN_CHANGE | IN_UPDATE;
322 return (ffs_update(ovp, NULL, NULL, 0));
323 }
324 }
325 oip->i_size = length;
326 DIP_ASSIGN(oip, size, length);
327 uvm_vnp_setsize(ovp, length);
328 /*
329 * Calculate index into inode's block list of
330 * last direct and indirect blocks (if any)
331 * which we want to keep. Lastblock is -1 when
332 * the file is truncated to 0.
333 */
334 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
335 lastiblock[SINGLE] = lastblock - NDADDR;
336 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
337 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
338 nblocks = btodb(fs->fs_bsize);
339 /*
340 * Update file and block pointers on disk before we start freeing
341 * blocks. If we crash before free'ing blocks below, the blocks
342 * will be returned to the free list. lastiblock values are also
343 * normalized to -1 for calls to ffs_indirtrunc below.
344 */
345 sync = 0;
346 for (level = TRIPLE; level >= SINGLE; level--) {
347 blks[NDADDR + level] = DIP(oip, ib[level]);
348 if (lastiblock[level] < 0 && blks[NDADDR + level] != 0) {
349 sync = 1;
350 DIP_ASSIGN(oip, ib[level], 0);
351 lastiblock[level] = -1;
352 }
353 }
354 for (i = 0; i < NDADDR; i++) {
355 blks[i] = DIP(oip, db[i]);
356 if (i > lastblock && blks[i] != 0) {
357 sync = 1;
358 DIP_ASSIGN(oip, db[i], 0);
359 }
360 }
361 oip->i_flag |= IN_CHANGE | IN_UPDATE;
362 if (sync) {
363 error = ffs_update(ovp, NULL, NULL, UPDATE_WAIT);
364 if (error && !allerror)
365 allerror = error;
366 }
367
368 /*
369 * Having written the new inode to disk, save its new configuration
370 * and put back the old block pointers long enough to process them.
371 * Note that we save the new block configuration so we can check it
372 * when we are done.
373 */
374 for (i = 0; i < NDADDR; i++) {
375 bn = DIP(oip, db[i]);
376 DIP_ASSIGN(oip, db[i], blks[i]);
377 blks[i] = bn;
378 }
379 for (i = 0; i < NIADDR; i++) {
380 bn = DIP(oip, ib[i]);
381 DIP_ASSIGN(oip, ib[i], blks[NDADDR + i]);
382 blks[NDADDR + i] = bn;
383 }
384
385 oip->i_size = osize;
386 DIP_ASSIGN(oip, size, osize);
387 error = vtruncbuf(ovp, lastblock + 1, 0, 0);
388 if (error && !allerror)
389 allerror = error;
390
391 /*
392 * Indirect blocks first.
393 */
394 indir_lbn[SINGLE] = -NDADDR;
395 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
396 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
397 for (level = TRIPLE; level >= SINGLE; level--) {
398 if (oip->i_ump->um_fstype == UFS1)
399 bn = ufs_rw32(oip->i_ffs1_ib[level],UFS_FSNEEDSWAP(fs));
400 else
401 bn = ufs_rw64(oip->i_ffs2_ib[level],UFS_FSNEEDSWAP(fs));
402 if (bn != 0) {
403 error = ffs_indirtrunc(oip, indir_lbn[level],
404 fsbtodb(fs, bn), lastiblock[level], level, &count);
405 if (error)
406 allerror = error;
407 blocksreleased += count;
408 if (lastiblock[level] < 0) {
409 DIP_ASSIGN(oip, ib[level], 0);
410 ffs_blkfree(fs, oip->i_devvp, bn, fs->fs_bsize,
411 oip->i_number);
412 blocksreleased += nblocks;
413 }
414 }
415 if (lastiblock[level] >= 0)
416 goto done;
417 }
418
419 /*
420 * All whole direct blocks or frags.
421 */
422 for (i = NDADDR - 1; i > lastblock; i--) {
423 long bsize;
424
425 if (oip->i_ump->um_fstype == UFS1)
426 bn = ufs_rw32(oip->i_ffs1_db[i], UFS_FSNEEDSWAP(fs));
427 else
428 bn = ufs_rw64(oip->i_ffs2_db[i], UFS_FSNEEDSWAP(fs));
429 if (bn == 0)
430 continue;
431 DIP_ASSIGN(oip, db[i], 0);
432 bsize = blksize(fs, oip, i);
433 ffs_blkfree(fs, oip->i_devvp, bn, bsize, oip->i_number);
434 blocksreleased += btodb(bsize);
435 }
436 if (lastblock < 0)
437 goto done;
438
439 /*
440 * Finally, look for a change in size of the
441 * last direct block; release any frags.
442 */
443 if (oip->i_ump->um_fstype == UFS1)
444 bn = ufs_rw32(oip->i_ffs1_db[lastblock], UFS_FSNEEDSWAP(fs));
445 else
446 bn = ufs_rw64(oip->i_ffs2_db[lastblock], UFS_FSNEEDSWAP(fs));
447 if (bn != 0) {
448 long oldspace, newspace;
449
450 /*
451 * Calculate amount of space we're giving
452 * back as old block size minus new block size.
453 */
454 oldspace = blksize(fs, oip, lastblock);
455 oip->i_size = length;
456 DIP_ASSIGN(oip, size, length);
457 newspace = blksize(fs, oip, lastblock);
458 if (newspace == 0)
459 panic("itrunc: newspace");
460 if (oldspace - newspace > 0) {
461 /*
462 * Block number of space to be free'd is
463 * the old block # plus the number of frags
464 * required for the storage we're keeping.
465 */
466 bn += numfrags(fs, newspace);
467 ffs_blkfree(fs, oip->i_devvp, bn, oldspace - newspace,
468 oip->i_number);
469 blocksreleased += btodb(oldspace - newspace);
470 }
471 }
472
473 done:
474 #ifdef DIAGNOSTIC
475 for (level = SINGLE; level <= TRIPLE; level++)
476 if (blks[NDADDR + level] != DIP(oip, ib[level]))
477 panic("itrunc1");
478 for (i = 0; i < NDADDR; i++)
479 if (blks[i] != DIP(oip, db[i]))
480 panic("itrunc2");
481 if (length == 0 &&
482 (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
483 panic("itrunc3");
484 #endif /* DIAGNOSTIC */
485 /*
486 * Put back the real size.
487 */
488 oip->i_size = length;
489 DIP_ASSIGN(oip, size, length);
490 DIP_ADD(oip, blocks, -blocksreleased);
491 lockmgr(&gp->g_glock, LK_RELEASE, NULL);
492 oip->i_flag |= IN_CHANGE;
493 #ifdef QUOTA
494 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
495 #endif
496 KASSERT(ovp->v_type != VREG || ovp->v_size == oip->i_size);
497 return (allerror);
498 }
499
500 /*
501 * Release blocks associated with the inode ip and stored in the indirect
502 * block bn. Blocks are free'd in LIFO order up to (but not including)
503 * lastbn. If level is greater than SINGLE, the block is an indirect block
504 * and recursive calls to indirtrunc must be used to cleanse other indirect
505 * blocks.
506 *
507 * NB: triple indirect blocks are untested.
508 */
509 static int
510 ffs_indirtrunc(struct inode *ip, daddr_t lbn, daddr_t dbn, daddr_t lastbn,
511 int level, int64_t *countp)
512 {
513 int i;
514 struct buf *bp;
515 struct fs *fs = ip->i_fs;
516 int32_t *bap1 = NULL;
517 int64_t *bap2 = NULL;
518 struct vnode *vp;
519 daddr_t nb, nlbn, last;
520 char *copy = NULL;
521 int64_t blkcount, factor, blocksreleased = 0;
522 int nblocks;
523 int error = 0, allerror = 0;
524 #ifdef FFS_EI
525 const int needswap = UFS_FSNEEDSWAP(fs);
526 #endif
527 #define RBAP(ip, i) (((ip)->i_ump->um_fstype == UFS1) ? \
528 ufs_rw32(bap1[i], needswap) : ufs_rw64(bap2[i], needswap))
529 #define BAP_ASSIGN(ip, i, value) \
530 do { \
531 if ((ip)->i_ump->um_fstype == UFS1) \
532 bap1[i] = (value); \
533 else \
534 bap2[i] = (value); \
535 } while(0)
536
537 /*
538 * Calculate index in current block of last
539 * block to be kept. -1 indicates the entire
540 * block so we need not calculate the index.
541 */
542 factor = 1;
543 for (i = SINGLE; i < level; i++)
544 factor *= NINDIR(fs);
545 last = lastbn;
546 if (lastbn > 0)
547 last /= factor;
548 nblocks = btodb(fs->fs_bsize);
549 /*
550 * Get buffer of block pointers, zero those entries corresponding
551 * to blocks to be free'd, and update on disk copy first. Since
552 * double(triple) indirect before single(double) indirect, calls
553 * to bmap on these blocks will fail. However, we already have
554 * the on disk address, so we have to set the b_blkno field
555 * explicitly instead of letting bread do everything for us.
556 */
557 vp = ITOV(ip);
558 bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
559 if (bp->b_flags & (B_DONE | B_DELWRI)) {
560 /* Braces must be here in case trace evaluates to nothing. */
561 trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
562 } else {
563 trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
564 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */
565 bp->b_flags |= B_READ;
566 if (bp->b_bcount > bp->b_bufsize)
567 panic("ffs_indirtrunc: bad buffer size");
568 bp->b_blkno = dbn;
569 BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
570 VOP_STRATEGY(vp, bp);
571 error = biowait(bp);
572 }
573 if (error) {
574 brelse(bp);
575 *countp = 0;
576 return (error);
577 }
578
579 if (ip->i_ump->um_fstype == UFS1)
580 bap1 = (int32_t *)bp->b_data;
581 else
582 bap2 = (int64_t *)bp->b_data;
583 if (lastbn >= 0) {
584 copy = malloc(fs->fs_bsize, M_TEMP, M_WAITOK);
585 memcpy((caddr_t)copy, bp->b_data, (u_int)fs->fs_bsize);
586 for (i = last + 1; i < NINDIR(fs); i++)
587 BAP_ASSIGN(ip, i, 0);
588 error = bwrite(bp);
589 if (error)
590 allerror = error;
591 if (ip->i_ump->um_fstype == UFS1)
592 bap1 = (int32_t *)copy;
593 else
594 bap2 = (int64_t *)copy;
595 }
596
597 /*
598 * Recursively free totally unused blocks.
599 */
600 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
601 i--, nlbn += factor) {
602 nb = RBAP(ip, i);
603 if (nb == 0)
604 continue;
605 if (level > SINGLE) {
606 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
607 (daddr_t)-1, level - 1,
608 &blkcount);
609 if (error)
610 allerror = error;
611 blocksreleased += blkcount;
612 }
613 ffs_blkfree(fs, ip->i_devvp, nb, fs->fs_bsize, ip->i_number);
614 blocksreleased += nblocks;
615 }
616
617 /*
618 * Recursively free last partial block.
619 */
620 if (level > SINGLE && lastbn >= 0) {
621 last = lastbn % factor;
622 nb = RBAP(ip, i);
623 if (nb != 0) {
624 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
625 last, level - 1, &blkcount);
626 if (error)
627 allerror = error;
628 blocksreleased += blkcount;
629 }
630 }
631
632 if (copy != NULL) {
633 FREE(copy, M_TEMP);
634 } else {
635 bp->b_flags |= B_INVAL;
636 brelse(bp);
637 }
638
639 *countp = blocksreleased;
640 return (allerror);
641 }
642
643 void
644 ffs_itimes(struct inode *ip, const struct timespec *acc,
645 const struct timespec *mod, const struct timespec *cre)
646 {
647 struct timespec now;
648
649 if (!(ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY))) {
650 return;
651 }
652
653 vfs_timestamp(&now);
654 if (ip->i_flag & IN_ACCESS) {
655 if (acc == NULL)
656 acc = &now;
657 DIP_ASSIGN(ip, atime, acc->tv_sec);
658 DIP_ASSIGN(ip, atimensec, acc->tv_nsec);
659 }
660 if (ip->i_flag & (IN_UPDATE | IN_MODIFY)) {
661 if ((ip->i_flags & SF_SNAPSHOT) == 0) {
662 if (mod == NULL)
663 mod = &now;
664 DIP_ASSIGN(ip, mtime, mod->tv_sec);
665 DIP_ASSIGN(ip, mtimensec, mod->tv_nsec);
666 }
667 ip->i_modrev++;
668 }
669 if (ip->i_flag & (IN_CHANGE | IN_MODIFY)) {
670 if (cre == NULL)
671 cre = &now;
672 DIP_ASSIGN(ip, ctime, cre->tv_sec);
673 DIP_ASSIGN(ip, ctimensec, cre->tv_nsec);
674 }
675 if (ip->i_flag & (IN_ACCESS | IN_MODIFY))
676 ip->i_flag |= IN_ACCESSED;
677 if (ip->i_flag & (IN_UPDATE | IN_CHANGE))
678 ip->i_flag |= IN_MODIFIED;
679 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY);
680 }
681