ffs_inode.c revision 1.78 1 /* $NetBSD: ffs_inode.c,v 1.78 2005/11/11 15:50:57 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.78 2005/11/11 15:50:57 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 (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 #ifdef QUOTA
207 if ((error = getinoquota(oip)) != 0)
208 return (error);
209 #endif
210 fs = oip->i_fs;
211 if (length > ump->um_maxfilesize)
212 return (EFBIG);
213
214 if ((oip->i_flags & SF_SNAPSHOT) != 0)
215 ffs_snapremove(ovp);
216
217 osize = oip->i_size;
218 aflag = ioflag & IO_SYNC ? B_SYNC : 0;
219
220 /*
221 * Lengthen the size of the file. We must ensure that the
222 * last byte of the file is allocated. Since the smallest
223 * value of osize is 0, length will be at least 1.
224 */
225
226 if (osize < length) {
227 if (lblkno(fs, osize) < NDADDR &&
228 lblkno(fs, osize) != lblkno(fs, length) &&
229 blkroundup(fs, osize) != osize) {
230 off_t eob;
231
232 eob = blkroundup(fs, osize);
233 error = ufs_balloc_range(ovp, osize, eob - osize,
234 cred, aflag);
235 if (error)
236 return error;
237 if (ioflag & IO_SYNC) {
238 ovp->v_size = eob;
239 simple_lock(&ovp->v_interlock);
240 VOP_PUTPAGES(ovp,
241 trunc_page(osize & fs->fs_bmask),
242 round_page(eob), PGO_CLEANIT | PGO_SYNCIO);
243 }
244 }
245 error = ufs_balloc_range(ovp, length - 1, 1, cred, aflag);
246 if (error) {
247 (void) ffs_truncate(ovp, osize, ioflag & IO_SYNC,
248 cred, p);
249 return (error);
250 }
251 uvm_vnp_setsize(ovp, length);
252 oip->i_flag |= IN_CHANGE | IN_UPDATE;
253 KASSERT(ovp->v_size == oip->i_size);
254 return (ffs_update(ovp, NULL, NULL, 0));
255 }
256
257 /*
258 * When truncating a regular file down to a non-block-aligned size,
259 * we must zero the part of last block which is past the new EOF.
260 * We must synchronously flush the zeroed pages to disk
261 * since the new pages will be invalidated as soon as we
262 * inform the VM system of the new, smaller size.
263 * We must do this before acquiring the GLOCK, since fetching
264 * the pages will acquire the GLOCK internally.
265 * So there is a window where another thread could see a whole
266 * zeroed page past EOF, but that's life.
267 */
268
269 offset = blkoff(fs, length);
270 if (ovp->v_type == VREG && offset != 0 && osize > length) {
271 daddr_t lbn;
272 voff_t eoz;
273
274 error = ufs_balloc_range(ovp, length - 1, 1, cred, aflag);
275 if (error)
276 return error;
277 lbn = lblkno(fs, length);
278 size = blksize(fs, oip, lbn);
279 eoz = MIN(lblktosize(fs, lbn) + size, osize);
280 uvm_vnp_zerorange(ovp, length, eoz - length);
281 if (round_page(eoz) > round_page(length)) {
282 simple_lock(&ovp->v_interlock);
283 error = VOP_PUTPAGES(ovp, round_page(length),
284 round_page(eoz),
285 PGO_CLEANIT | PGO_DEACTIVATE |
286 ((ioflag & IO_SYNC) ? PGO_SYNCIO : 0));
287 if (error)
288 return error;
289 }
290 }
291
292 lockmgr(&gp->g_glock, LK_EXCLUSIVE, NULL);
293
294 if (DOINGSOFTDEP(ovp)) {
295 if (length > 0) {
296 /*
297 * If a file is only partially truncated, then
298 * we have to clean up the data structures
299 * describing the allocation past the truncation
300 * point. Finding and deallocating those structures
301 * is a lot of work. Since partial truncation occurs
302 * rarely, we solve the problem by syncing the file
303 * so that it will have no data structures left.
304 */
305 if ((error = VOP_FSYNC(ovp, cred, FSYNC_WAIT,
306 0, 0, p)) != 0) {
307 lockmgr(&gp->g_glock, LK_RELEASE, NULL);
308 return (error);
309 }
310 if (oip->i_flag & IN_SPACECOUNTED)
311 fs->fs_pendingblocks -= DIP(oip, blocks);
312 } else {
313 uvm_vnp_setsize(ovp, length);
314 #ifdef QUOTA
315 (void) chkdq(oip, -DIP(oip, blocks), NOCRED, 0);
316 #endif
317 softdep_setup_freeblocks(oip, length, 0);
318 (void) vinvalbuf(ovp, 0, cred, p, 0, 0);
319 lockmgr(&gp->g_glock, LK_RELEASE, NULL);
320 oip->i_flag |= IN_CHANGE | IN_UPDATE;
321 return (ffs_update(ovp, NULL, NULL, 0));
322 }
323 }
324 oip->i_size = length;
325 DIP_ASSIGN(oip, size, length);
326 uvm_vnp_setsize(ovp, length);
327 /*
328 * Calculate index into inode's block list of
329 * last direct and indirect blocks (if any)
330 * which we want to keep. Lastblock is -1 when
331 * the file is truncated to 0.
332 */
333 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
334 lastiblock[SINGLE] = lastblock - NDADDR;
335 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
336 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
337 nblocks = btodb(fs->fs_bsize);
338 /*
339 * Update file and block pointers on disk before we start freeing
340 * blocks. If we crash before free'ing blocks below, the blocks
341 * will be returned to the free list. lastiblock values are also
342 * normalized to -1 for calls to ffs_indirtrunc below.
343 */
344 sync = 0;
345 for (level = TRIPLE; level >= SINGLE; level--) {
346 blks[NDADDR + level] = DIP(oip, ib[level]);
347 if (lastiblock[level] < 0 && blks[NDADDR + level] != 0) {
348 sync = 1;
349 DIP_ASSIGN(oip, ib[level], 0);
350 lastiblock[level] = -1;
351 }
352 }
353 for (i = 0; i < NDADDR; i++) {
354 blks[i] = DIP(oip, db[i]);
355 if (i > lastblock && blks[i] != 0) {
356 sync = 1;
357 DIP_ASSIGN(oip, db[i], 0);
358 }
359 }
360 oip->i_flag |= IN_CHANGE | IN_UPDATE;
361 if (sync) {
362 error = ffs_update(ovp, NULL, NULL, UPDATE_WAIT);
363 if (error && !allerror)
364 allerror = error;
365 }
366
367 /*
368 * Having written the new inode to disk, save its new configuration
369 * and put back the old block pointers long enough to process them.
370 * Note that we save the new block configuration so we can check it
371 * when we are done.
372 */
373 for (i = 0; i < NDADDR; i++) {
374 bn = DIP(oip, db[i]);
375 DIP_ASSIGN(oip, db[i], blks[i]);
376 blks[i] = bn;
377 }
378 for (i = 0; i < NIADDR; i++) {
379 bn = DIP(oip, ib[i]);
380 DIP_ASSIGN(oip, ib[i], blks[NDADDR + i]);
381 blks[NDADDR + i] = bn;
382 }
383
384 oip->i_size = osize;
385 DIP_ASSIGN(oip, size, osize);
386 error = vtruncbuf(ovp, lastblock + 1, 0, 0);
387 if (error && !allerror)
388 allerror = error;
389
390 /*
391 * Indirect blocks first.
392 */
393 indir_lbn[SINGLE] = -NDADDR;
394 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
395 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
396 for (level = TRIPLE; level >= SINGLE; level--) {
397 if (oip->i_ump->um_fstype == UFS1)
398 bn = ufs_rw32(oip->i_ffs1_ib[level],UFS_FSNEEDSWAP(fs));
399 else
400 bn = ufs_rw64(oip->i_ffs2_ib[level],UFS_FSNEEDSWAP(fs));
401 if (bn != 0) {
402 error = ffs_indirtrunc(oip, indir_lbn[level],
403 fsbtodb(fs, bn), lastiblock[level], level, &count);
404 if (error)
405 allerror = error;
406 blocksreleased += count;
407 if (lastiblock[level] < 0) {
408 DIP_ASSIGN(oip, ib[level], 0);
409 ffs_blkfree(fs, oip->i_devvp, bn, fs->fs_bsize,
410 oip->i_number);
411 blocksreleased += nblocks;
412 }
413 }
414 if (lastiblock[level] >= 0)
415 goto done;
416 }
417
418 /*
419 * All whole direct blocks or frags.
420 */
421 for (i = NDADDR - 1; i > lastblock; i--) {
422 long bsize;
423
424 if (oip->i_ump->um_fstype == UFS1)
425 bn = ufs_rw32(oip->i_ffs1_db[i], UFS_FSNEEDSWAP(fs));
426 else
427 bn = ufs_rw64(oip->i_ffs2_db[i], UFS_FSNEEDSWAP(fs));
428 if (bn == 0)
429 continue;
430 DIP_ASSIGN(oip, db[i], 0);
431 bsize = blksize(fs, oip, i);
432 ffs_blkfree(fs, oip->i_devvp, bn, bsize, oip->i_number);
433 blocksreleased += btodb(bsize);
434 }
435 if (lastblock < 0)
436 goto done;
437
438 /*
439 * Finally, look for a change in size of the
440 * last direct block; release any frags.
441 */
442 if (oip->i_ump->um_fstype == UFS1)
443 bn = ufs_rw32(oip->i_ffs1_db[lastblock], UFS_FSNEEDSWAP(fs));
444 else
445 bn = ufs_rw64(oip->i_ffs2_db[lastblock], UFS_FSNEEDSWAP(fs));
446 if (bn != 0) {
447 long oldspace, newspace;
448
449 /*
450 * Calculate amount of space we're giving
451 * back as old block size minus new block size.
452 */
453 oldspace = blksize(fs, oip, lastblock);
454 oip->i_size = length;
455 DIP_ASSIGN(oip, size, length);
456 newspace = blksize(fs, oip, lastblock);
457 if (newspace == 0)
458 panic("itrunc: newspace");
459 if (oldspace - newspace > 0) {
460 /*
461 * Block number of space to be free'd is
462 * the old block # plus the number of frags
463 * required for the storage we're keeping.
464 */
465 bn += numfrags(fs, newspace);
466 ffs_blkfree(fs, oip->i_devvp, bn, oldspace - newspace,
467 oip->i_number);
468 blocksreleased += btodb(oldspace - newspace);
469 }
470 }
471
472 done:
473 #ifdef DIAGNOSTIC
474 for (level = SINGLE; level <= TRIPLE; level++)
475 if (blks[NDADDR + level] != DIP(oip, ib[level]))
476 panic("itrunc1");
477 for (i = 0; i < NDADDR; i++)
478 if (blks[i] != DIP(oip, db[i]))
479 panic("itrunc2");
480 if (length == 0 &&
481 (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
482 panic("itrunc3");
483 #endif /* DIAGNOSTIC */
484 /*
485 * Put back the real size.
486 */
487 oip->i_size = length;
488 DIP_ASSIGN(oip, size, length);
489 DIP_ADD(oip, blocks, -blocksreleased);
490 lockmgr(&gp->g_glock, LK_RELEASE, NULL);
491 oip->i_flag |= IN_CHANGE;
492 #ifdef QUOTA
493 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
494 #endif
495 KASSERT(ovp->v_type != VREG || ovp->v_size == oip->i_size);
496 return (allerror);
497 }
498
499 /*
500 * Release blocks associated with the inode ip and stored in the indirect
501 * block bn. Blocks are free'd in LIFO order up to (but not including)
502 * lastbn. If level is greater than SINGLE, the block is an indirect block
503 * and recursive calls to indirtrunc must be used to cleanse other indirect
504 * blocks.
505 *
506 * NB: triple indirect blocks are untested.
507 */
508 static int
509 ffs_indirtrunc(struct inode *ip, daddr_t lbn, daddr_t dbn, daddr_t lastbn,
510 int level, int64_t *countp)
511 {
512 int i;
513 struct buf *bp;
514 struct fs *fs = ip->i_fs;
515 int32_t *bap1 = NULL;
516 int64_t *bap2 = NULL;
517 struct vnode *vp;
518 daddr_t nb, nlbn, last;
519 char *copy = NULL;
520 int64_t blkcount, factor, blocksreleased = 0;
521 int nblocks;
522 int error = 0, allerror = 0;
523 #ifdef FFS_EI
524 const int needswap = UFS_FSNEEDSWAP(fs);
525 #endif
526 #define RBAP(ip, i) (((ip)->i_ump->um_fstype == UFS1) ? \
527 ufs_rw32(bap1[i], needswap) : ufs_rw64(bap2[i], needswap))
528 #define BAP_ASSIGN(ip, i, value) \
529 do { \
530 if ((ip)->i_ump->um_fstype == UFS1) \
531 bap1[i] = (value); \
532 else \
533 bap2[i] = (value); \
534 } while(0)
535
536 /*
537 * Calculate index in current block of last
538 * block to be kept. -1 indicates the entire
539 * block so we need not calculate the index.
540 */
541 factor = 1;
542 for (i = SINGLE; i < level; i++)
543 factor *= NINDIR(fs);
544 last = lastbn;
545 if (lastbn > 0)
546 last /= factor;
547 nblocks = btodb(fs->fs_bsize);
548 /*
549 * Get buffer of block pointers, zero those entries corresponding
550 * to blocks to be free'd, and update on disk copy first. Since
551 * double(triple) indirect before single(double) indirect, calls
552 * to bmap on these blocks will fail. However, we already have
553 * the on disk address, so we have to set the b_blkno field
554 * explicitly instead of letting bread do everything for us.
555 */
556 vp = ITOV(ip);
557 bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
558 if (bp->b_flags & (B_DONE | B_DELWRI)) {
559 /* Braces must be here in case trace evaluates to nothing. */
560 trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
561 } else {
562 trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
563 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */
564 bp->b_flags |= B_READ;
565 if (bp->b_bcount > bp->b_bufsize)
566 panic("ffs_indirtrunc: bad buffer size");
567 bp->b_blkno = dbn;
568 BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
569 VOP_STRATEGY(vp, bp);
570 error = biowait(bp);
571 }
572 if (error) {
573 brelse(bp);
574 *countp = 0;
575 return (error);
576 }
577
578 if (ip->i_ump->um_fstype == UFS1)
579 bap1 = (int32_t *)bp->b_data;
580 else
581 bap2 = (int64_t *)bp->b_data;
582 if (lastbn >= 0) {
583 copy = malloc(fs->fs_bsize, M_TEMP, M_WAITOK);
584 memcpy((caddr_t)copy, bp->b_data, (u_int)fs->fs_bsize);
585 for (i = last + 1; i < NINDIR(fs); i++)
586 BAP_ASSIGN(ip, i, 0);
587 error = bwrite(bp);
588 if (error)
589 allerror = error;
590 if (ip->i_ump->um_fstype == UFS1)
591 bap1 = (int32_t *)copy;
592 else
593 bap2 = (int64_t *)copy;
594 }
595
596 /*
597 * Recursively free totally unused blocks.
598 */
599 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
600 i--, nlbn += factor) {
601 nb = RBAP(ip, i);
602 if (nb == 0)
603 continue;
604 if (level > SINGLE) {
605 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
606 (daddr_t)-1, level - 1,
607 &blkcount);
608 if (error)
609 allerror = error;
610 blocksreleased += blkcount;
611 }
612 ffs_blkfree(fs, ip->i_devvp, nb, fs->fs_bsize, ip->i_number);
613 blocksreleased += nblocks;
614 }
615
616 /*
617 * Recursively free last partial block.
618 */
619 if (level > SINGLE && lastbn >= 0) {
620 last = lastbn % factor;
621 nb = RBAP(ip, i);
622 if (nb != 0) {
623 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
624 last, level - 1, &blkcount);
625 if (error)
626 allerror = error;
627 blocksreleased += blkcount;
628 }
629 }
630
631 if (copy != NULL) {
632 FREE(copy, M_TEMP);
633 } else {
634 bp->b_flags |= B_INVAL;
635 brelse(bp);
636 }
637
638 *countp = blocksreleased;
639 return (allerror);
640 }
641
642 void
643 ffs_itimes(struct inode *ip, const struct timespec *acc,
644 const struct timespec *mod, const struct timespec *cre)
645 {
646 struct timespec *ts = NULL, tsb;
647
648 if (!(ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY))) {
649 return;
650 }
651
652 if (ip->i_flag & IN_ACCESS) {
653 if (acc == NULL)
654 acc = ts == NULL ? (ts = nanotime(&tsb)) : ts;
655 DIP_ASSIGN(ip, atime, acc->tv_sec);
656 DIP_ASSIGN(ip, atimensec, acc->tv_nsec);
657 }
658 if (ip->i_flag & (IN_UPDATE | IN_MODIFY)) {
659 if ((ip->i_flags & SF_SNAPSHOT) == 0) {
660 if (mod == NULL)
661 mod = ts == NULL ? (ts = nanotime(&tsb)) : ts;
662 DIP_ASSIGN(ip, mtime, mod->tv_sec);
663 DIP_ASSIGN(ip, mtimensec, mod->tv_nsec);
664 }
665 ip->i_modrev++;
666 }
667 if (ip->i_flag & (IN_CHANGE | IN_MODIFY)) {
668 if (cre == NULL)
669 cre = ts == NULL ? (ts = nanotime(&tsb)) : ts;
670 DIP_ASSIGN(ip, ctime, cre->tv_sec);
671 DIP_ASSIGN(ip, ctimensec, cre->tv_nsec);
672 }
673 if (ip->i_flag & (IN_ACCESS | IN_MODIFY))
674 ip->i_flag |= IN_ACCESSED;
675 if (ip->i_flag & (IN_UPDATE | IN_CHANGE))
676 ip->i_flag |= IN_MODIFIED;
677 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY);
678 }
679