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