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