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