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