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