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