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