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