ext2fs_inode.c revision 1.80 1 1.80 dholland /* $NetBSD: ext2fs_inode.c,v 1.80 2013/06/23 02:06:05 dholland Exp $ */
2 1.1 bouyer
3 1.1 bouyer /*
4 1.1 bouyer * Copyright (c) 1982, 1986, 1989, 1993
5 1.1 bouyer * The Regents of the University of California. All rights reserved.
6 1.1 bouyer *
7 1.1 bouyer * Redistribution and use in source and binary forms, with or without
8 1.1 bouyer * modification, are permitted provided that the following conditions
9 1.1 bouyer * are met:
10 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
11 1.1 bouyer * notice, this list of conditions and the following disclaimer.
12 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
14 1.1 bouyer * documentation and/or other materials provided with the distribution.
15 1.35 agc * 3. Neither the name of the University nor the names of its contributors
16 1.35 agc * may be used to endorse or promote products derived from this software
17 1.35 agc * without specific prior written permission.
18 1.35 agc *
19 1.35 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.35 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.35 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.35 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.35 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.35 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.35 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.35 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.35 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.35 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.35 agc * SUCH DAMAGE.
30 1.35 agc *
31 1.35 agc * @(#)ffs_inode.c 8.8 (Berkeley) 10/19/94
32 1.35 agc * Modified for ext2fs by Manuel Bouyer.
33 1.35 agc */
34 1.35 agc
35 1.35 agc /*
36 1.35 agc * Copyright (c) 1997 Manuel Bouyer.
37 1.35 agc *
38 1.35 agc * Redistribution and use in source and binary forms, with or without
39 1.35 agc * modification, are permitted provided that the following conditions
40 1.35 agc * are met:
41 1.35 agc * 1. Redistributions of source code must retain the above copyright
42 1.35 agc * notice, this list of conditions and the following disclaimer.
43 1.35 agc * 2. Redistributions in binary form must reproduce the above copyright
44 1.35 agc * notice, this list of conditions and the following disclaimer in the
45 1.35 agc * documentation and/or other materials provided with the distribution.
46 1.1 bouyer *
47 1.40 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48 1.40 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 1.40 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 1.40 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51 1.40 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52 1.40 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53 1.40 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54 1.40 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55 1.40 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56 1.40 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 1.1 bouyer *
58 1.1 bouyer * @(#)ffs_inode.c 8.8 (Berkeley) 10/19/94
59 1.1 bouyer * Modified for ext2fs by Manuel Bouyer.
60 1.1 bouyer */
61 1.27 lukem
62 1.27 lukem #include <sys/cdefs.h>
63 1.80 dholland __KERNEL_RCSID(0, "$NetBSD: ext2fs_inode.c,v 1.80 2013/06/23 02:06:05 dholland Exp $");
64 1.5 mrg
65 1.1 bouyer #include <sys/param.h>
66 1.1 bouyer #include <sys/systm.h>
67 1.1 bouyer #include <sys/mount.h>
68 1.1 bouyer #include <sys/proc.h>
69 1.1 bouyer #include <sys/file.h>
70 1.1 bouyer #include <sys/buf.h>
71 1.1 bouyer #include <sys/vnode.h>
72 1.1 bouyer #include <sys/kernel.h>
73 1.75 para #include <sys/kmem.h>
74 1.1 bouyer #include <sys/trace.h>
75 1.1 bouyer #include <sys/resourcevar.h>
76 1.54 elad #include <sys/kauth.h>
77 1.1 bouyer
78 1.1 bouyer #include <ufs/ufs/inode.h>
79 1.1 bouyer #include <ufs/ufs/ufsmount.h>
80 1.1 bouyer #include <ufs/ufs/ufs_extern.h>
81 1.1 bouyer
82 1.1 bouyer #include <ufs/ext2fs/ext2fs.h>
83 1.1 bouyer #include <ufs/ext2fs/ext2fs_extern.h>
84 1.1 bouyer
85 1.22 tsutsui extern int prtactive;
86 1.22 tsutsui
87 1.47 xtraeme static int ext2fs_indirtrunc(struct inode *, daddr_t, daddr_t,
88 1.47 xtraeme daddr_t, int, long *);
89 1.1 bouyer
90 1.1 bouyer /*
91 1.78 dholland * These are fortunately the same values; it is likely that there is
92 1.78 dholland * code that assumes they're equal. In any event, neither ought to
93 1.78 dholland * ever change because it's a property of the on-disk formats.
94 1.78 dholland */
95 1.78 dholland CTASSERT(EXT2FS_NDADDR == UFS_NDADDR);
96 1.78 dholland CTASSERT(EXT2FS_NIADDR == UFS_NIADDR);
97 1.78 dholland
98 1.78 dholland /*
99 1.44 ws * Get the size of an inode.
100 1.44 ws */
101 1.69 tsutsui uint64_t
102 1.44 ws ext2fs_size(struct inode *ip)
103 1.44 ws {
104 1.69 tsutsui uint64_t size = ip->i_e2fs_size;
105 1.44 ws
106 1.44 ws if ((ip->i_e2fs_mode & IFMT) == IFREG)
107 1.69 tsutsui size |= (uint64_t)ip->i_e2fs_dacl << 32;
108 1.44 ws return size;
109 1.44 ws }
110 1.44 ws
111 1.44 ws int
112 1.69 tsutsui ext2fs_setsize(struct inode *ip, uint64_t size)
113 1.44 ws {
114 1.44 ws if ((ip->i_e2fs_mode & IFMT) == IFREG ||
115 1.44 ws ip->i_e2fs_mode == 0) {
116 1.44 ws ip->i_e2fs_dacl = size >> 32;
117 1.44 ws if (size >= 0x80000000U) {
118 1.44 ws struct m_ext2fs *fs = ip->i_e2fs;
119 1.44 ws
120 1.44 ws if (fs->e2fs.e2fs_rev <= E2FS_REV0) {
121 1.44 ws /* Linux automagically upgrades to REV1 here! */
122 1.44 ws return EFBIG;
123 1.44 ws }
124 1.44 ws if (!(fs->e2fs.e2fs_features_rocompat
125 1.44 ws & EXT2F_ROCOMPAT_LARGEFILE)) {
126 1.44 ws fs->e2fs.e2fs_features_rocompat |=
127 1.44 ws EXT2F_ROCOMPAT_LARGEFILE;
128 1.44 ws fs->e2fs_fmod = 1;
129 1.44 ws }
130 1.44 ws }
131 1.44 ws } else if (size >= 0x80000000U)
132 1.44 ws return EFBIG;
133 1.44 ws
134 1.44 ws ip->i_e2fs_size = size;
135 1.44 ws
136 1.44 ws return 0;
137 1.44 ws }
138 1.44 ws
139 1.76 jakllsch uint64_t
140 1.76 jakllsch ext2fs_nblock(struct inode *ip)
141 1.76 jakllsch {
142 1.76 jakllsch uint64_t nblock = ip->i_e2fs_nblock;
143 1.76 jakllsch struct m_ext2fs * const fs = ip->i_e2fs;
144 1.76 jakllsch
145 1.76 jakllsch if (fs->e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_HUGE_FILE) {
146 1.76 jakllsch nblock |= (uint64_t)ip->i_e2fs_nblock_high << 32;
147 1.76 jakllsch
148 1.76 jakllsch if ((ip->i_e2fs_flags & EXT2_HUGE_FILE)) {
149 1.80 dholland nblock = EXT2_FSBTODB(fs, nblock);
150 1.76 jakllsch }
151 1.76 jakllsch }
152 1.76 jakllsch
153 1.76 jakllsch return nblock;
154 1.76 jakllsch }
155 1.76 jakllsch
156 1.76 jakllsch int
157 1.76 jakllsch ext2fs_setnblock(struct inode *ip, uint64_t nblock)
158 1.76 jakllsch {
159 1.76 jakllsch struct m_ext2fs * const fs = ip->i_e2fs;
160 1.76 jakllsch
161 1.76 jakllsch if (nblock <= 0xffffffffULL) {
162 1.76 jakllsch CLR(ip->i_e2fs_flags, EXT2_HUGE_FILE);
163 1.76 jakllsch ip->i_e2fs_nblock = nblock;
164 1.76 jakllsch return 0;
165 1.76 jakllsch }
166 1.76 jakllsch
167 1.76 jakllsch if (!ISSET(fs->e2fs.e2fs_features_rocompat, EXT2F_ROCOMPAT_HUGE_FILE))
168 1.76 jakllsch return EFBIG;
169 1.76 jakllsch
170 1.76 jakllsch if (nblock <= 0xffffffffffffULL) {
171 1.76 jakllsch CLR(ip->i_e2fs_flags, EXT2_HUGE_FILE);
172 1.76 jakllsch ip->i_e2fs_nblock = nblock & 0xffffffff;
173 1.76 jakllsch ip->i_e2fs_nblock_high = (nblock >> 32) & 0xffff;
174 1.76 jakllsch return 0;
175 1.76 jakllsch }
176 1.76 jakllsch
177 1.80 dholland if (EXT2_DBTOFSB(fs, nblock) <= 0xffffffffffffULL) {
178 1.76 jakllsch SET(ip->i_e2fs_flags, EXT2_HUGE_FILE);
179 1.80 dholland ip->i_e2fs_nblock = EXT2_DBTOFSB(fs, nblock) & 0xffffffff;
180 1.80 dholland ip->i_e2fs_nblock_high = (EXT2_DBTOFSB(fs, nblock) >> 32) & 0xffff;
181 1.76 jakllsch return 0;
182 1.76 jakllsch }
183 1.76 jakllsch
184 1.76 jakllsch return EFBIG;
185 1.76 jakllsch }
186 1.76 jakllsch
187 1.44 ws /*
188 1.1 bouyer * Last reference to an inode. If necessary, write or delete it.
189 1.1 bouyer */
190 1.1 bouyer int
191 1.47 xtraeme ext2fs_inactive(void *v)
192 1.45 perry {
193 1.1 bouyer struct vop_inactive_args /* {
194 1.1 bouyer struct vnode *a_vp;
195 1.63 ad bool *a_recycle;
196 1.1 bouyer } */ *ap = v;
197 1.6 fvdl struct vnode *vp = ap->a_vp;
198 1.6 fvdl struct inode *ip = VTOI(vp);
199 1.6 fvdl int error = 0;
200 1.45 perry
201 1.1 bouyer if (prtactive && vp->v_usecount != 0)
202 1.7 mikel vprint("ext2fs_inactive: pushing active", vp);
203 1.1 bouyer /* Get rid of inodes related to stale file handles. */
204 1.6 fvdl if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0)
205 1.6 fvdl goto out;
206 1.1 bouyer
207 1.1 bouyer error = 0;
208 1.1 bouyer if (ip->i_e2fs_nlink == 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
209 1.63 ad /* Defer final inode free and update to reclaim.*/
210 1.44 ws if (ext2fs_size(ip) != 0) {
211 1.62 pooka error = ext2fs_truncate(vp, (off_t)0, 0, NOCRED);
212 1.23 chs }
213 1.56 chs ip->i_e2fs_dtime = time_second;
214 1.64 ad ip->i_flag |= IN_CHANGE | IN_UPDATE;
215 1.73 hannken ip->i_omode = 1;
216 1.64 ad }
217 1.64 ad if (ip->i_flag & (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) {
218 1.50 yamt ext2fs_update(vp, NULL, NULL, 0);
219 1.37 hannken }
220 1.6 fvdl out:
221 1.1 bouyer /*
222 1.1 bouyer * If we are done with the inode, reclaim it
223 1.1 bouyer * so that it can be reused immediately.
224 1.1 bouyer */
225 1.63 ad *ap->a_recycle = (ip->i_e2fs_dtime != 0);
226 1.72 hannken VOP_UNLOCK(vp);
227 1.1 bouyer return (error);
228 1.45 perry }
229 1.1 bouyer
230 1.1 bouyer
231 1.1 bouyer /*
232 1.1 bouyer * Update the access, modified, and inode change times as specified by the
233 1.1 bouyer * IACCESS, IUPDATE, and ICHANGE flags respectively. The IMODIFIED flag is
234 1.1 bouyer * used to specify that the inode needs to be updated but that the times have
235 1.1 bouyer * already been set. The access and modified times are taken from the second
236 1.1 bouyer * and third parameters; the inode change time is always taken from the current
237 1.15 perseant * time. If UPDATE_WAIT or UPDATE_DIROP is set, then wait for the disk
238 1.15 perseant * write of the inode to complete.
239 1.1 bouyer */
240 1.1 bouyer int
241 1.50 yamt ext2fs_update(struct vnode *vp, const struct timespec *acc,
242 1.50 yamt const struct timespec *mod, int updflags)
243 1.1 bouyer {
244 1.14 augustss struct m_ext2fs *fs;
245 1.1 bouyer struct buf *bp;
246 1.1 bouyer struct inode *ip;
247 1.1 bouyer int error;
248 1.57 christos void *cp;
249 1.18 mycroft int flags;
250 1.1 bouyer
251 1.50 yamt if (vp->v_mount->mnt_flag & MNT_RDONLY)
252 1.1 bouyer return (0);
253 1.50 yamt ip = VTOI(vp);
254 1.50 yamt EXT2FS_ITIMES(ip, acc, mod, NULL);
255 1.50 yamt if (updflags & UPDATE_CLOSE)
256 1.41 mycroft flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED);
257 1.41 mycroft else
258 1.41 mycroft flags = ip->i_flag & IN_MODIFIED;
259 1.18 mycroft if (flags == 0)
260 1.1 bouyer return (0);
261 1.1 bouyer fs = ip->i_e2fs;
262 1.18 mycroft
263 1.1 bouyer error = bread(ip->i_devvp,
264 1.80 dholland EXT2_FSBTODB(fs, ino_to_fsba(fs, ip->i_number)),
265 1.66 hannken (int)fs->e2fs_bsize, NOCRED, B_MODIFY, &bp);
266 1.1 bouyer if (error) {
267 1.1 bouyer return (error);
268 1.1 bouyer }
269 1.19 mycroft ip->i_flag &= ~(IN_MODIFIED | IN_ACCESSED);
270 1.57 christos cp = (char *)bp->b_data +
271 1.68 christos (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE(fs));
272 1.32 he e2fs_isave(ip->i_din.e2fs_din, (struct ext2fs_dinode *)cp);
273 1.50 yamt if ((updflags & (UPDATE_WAIT|UPDATE_DIROP)) != 0 &&
274 1.18 mycroft (flags & IN_MODIFIED) != 0 &&
275 1.50 yamt (vp->v_mount->mnt_flag & MNT_ASYNC) == 0)
276 1.1 bouyer return (bwrite(bp));
277 1.1 bouyer else {
278 1.1 bouyer bdwrite(bp);
279 1.1 bouyer return (0);
280 1.1 bouyer }
281 1.1 bouyer }
282 1.1 bouyer
283 1.1 bouyer #define SINGLE 0 /* index of single indirect block */
284 1.1 bouyer #define DOUBLE 1 /* index of double indirect block */
285 1.1 bouyer #define TRIPLE 2 /* index of triple indirect block */
286 1.1 bouyer /*
287 1.1 bouyer * Truncate the inode oip to at most length size, freeing the
288 1.1 bouyer * disk blocks.
289 1.1 bouyer */
290 1.1 bouyer int
291 1.50 yamt ext2fs_truncate(struct vnode *ovp, off_t length, int ioflag,
292 1.62 pooka kauth_cred_t cred)
293 1.1 bouyer {
294 1.29 fvdl daddr_t lastblock;
295 1.43 mycroft struct inode *oip = VTOI(ovp);
296 1.78 dholland daddr_t bn, lastiblock[EXT2FS_NIADDR], indir_lbn[EXT2FS_NIADDR];
297 1.30 fvdl /* XXX ondisk32 */
298 1.78 dholland int32_t oldblks[EXT2FS_NDADDR + EXT2FS_NIADDR], newblks[EXT2FS_NDADDR + EXT2FS_NIADDR];
299 1.14 augustss struct m_ext2fs *fs;
300 1.1 bouyer int offset, size, level;
301 1.43 mycroft long count, blocksreleased = 0;
302 1.50 yamt int i, nblocks;
303 1.21 chs int error, allerror = 0;
304 1.1 bouyer off_t osize;
305 1.43 mycroft int sync;
306 1.43 mycroft struct ufsmount *ump = oip->i_ump;
307 1.1 bouyer
308 1.51 yamt if (ovp->v_type == VCHR || ovp->v_type == VBLK ||
309 1.51 yamt ovp->v_type == VFIFO || ovp->v_type == VSOCK) {
310 1.51 yamt return 0;
311 1.51 yamt }
312 1.51 yamt
313 1.1 bouyer if (length < 0)
314 1.1 bouyer return (EINVAL);
315 1.1 bouyer
316 1.1 bouyer if (ovp->v_type == VLNK &&
317 1.44 ws (ext2fs_size(oip) < ump->um_maxsymlinklen ||
318 1.76 jakllsch (ump->um_maxsymlinklen == 0 && ext2fs_nblock(oip) == 0))) {
319 1.43 mycroft KDASSERT(length == 0);
320 1.31 fvdl memset((char *)&oip->i_din.e2fs_din->e2di_shortlink, 0,
321 1.44 ws (u_int)ext2fs_size(oip));
322 1.44 ws (void)ext2fs_setsize(oip, 0);
323 1.1 bouyer oip->i_flag |= IN_CHANGE | IN_UPDATE;
324 1.50 yamt return (ext2fs_update(ovp, NULL, NULL, 0));
325 1.1 bouyer }
326 1.44 ws if (ext2fs_size(oip) == length) {
327 1.71 bouyer /* still do a uvm_vnp_setsize() as writesize may be larger */
328 1.71 bouyer uvm_vnp_setsize(ovp, length);
329 1.1 bouyer oip->i_flag |= IN_CHANGE | IN_UPDATE;
330 1.50 yamt return (ext2fs_update(ovp, NULL, NULL, 0));
331 1.1 bouyer }
332 1.1 bouyer fs = oip->i_e2fs;
333 1.43 mycroft if (length > ump->um_maxfilesize)
334 1.43 mycroft return (EFBIG);
335 1.43 mycroft
336 1.44 ws osize = ext2fs_size(oip);
337 1.43 mycroft
338 1.1 bouyer /*
339 1.1 bouyer * Lengthen the size of the file. We must ensure that the
340 1.1 bouyer * last byte of the file is allocated. Since the smallest
341 1.1 bouyer * value of osize is 0, length will be at least 1.
342 1.1 bouyer */
343 1.1 bouyer if (osize < length) {
344 1.59 yamt uvm_vnp_setwritesize(ovp, length);
345 1.50 yamt error = ufs_balloc_range(ovp, length - 1, 1, cred,
346 1.43 mycroft ioflag & IO_SYNC ? B_SYNC : 0);
347 1.43 mycroft if (error) {
348 1.50 yamt (void) ext2fs_truncate(ovp, osize, ioflag & IO_SYNC,
349 1.62 pooka cred);
350 1.43 mycroft return (error);
351 1.43 mycroft }
352 1.43 mycroft uvm_vnp_setsize(ovp, length);
353 1.1 bouyer oip->i_flag |= IN_CHANGE | IN_UPDATE;
354 1.46 kml KASSERT(error || ovp->v_size == ext2fs_size(oip));
355 1.50 yamt return (ext2fs_update(ovp, NULL, NULL, 0));
356 1.1 bouyer }
357 1.1 bouyer /*
358 1.1 bouyer * Shorten the size of the file. If the file is not being
359 1.1 bouyer * truncated to a block boundry, the contents of the
360 1.1 bouyer * partial block following the end of the file must be
361 1.24 wiz * zero'ed in case it ever become accessible again because
362 1.1 bouyer * of subsequent file growth.
363 1.1 bouyer */
364 1.79 dholland offset = ext2_blkoff(fs, length);
365 1.21 chs if (offset != 0) {
366 1.1 bouyer size = fs->e2fs_bsize;
367 1.21 chs
368 1.21 chs /* XXXUBC we should handle more than just VREG */
369 1.74 hannken ubc_zerorange(&ovp->v_uobj, length, size - offset,
370 1.74 hannken UBC_UNMAP_FLAG(ovp));
371 1.1 bouyer }
372 1.44 ws (void)ext2fs_setsize(oip, length);
373 1.6 fvdl uvm_vnp_setsize(ovp, length);
374 1.1 bouyer /*
375 1.1 bouyer * Calculate index into inode's block list of
376 1.1 bouyer * last direct and indirect blocks (if any)
377 1.1 bouyer * which we want to keep. Lastblock is -1 when
378 1.1 bouyer * the file is truncated to 0.
379 1.1 bouyer */
380 1.1 bouyer lastblock = lblkno(fs, length + fs->e2fs_bsize - 1) - 1;
381 1.78 dholland lastiblock[SINGLE] = lastblock - EXT2FS_NDADDR;
382 1.79 dholland lastiblock[DOUBLE] = lastiblock[SINGLE] - EXT2_NINDIR(fs);
383 1.79 dholland lastiblock[TRIPLE] = lastiblock[DOUBLE] - EXT2_NINDIR(fs) * EXT2_NINDIR(fs);
384 1.1 bouyer nblocks = btodb(fs->e2fs_bsize);
385 1.1 bouyer /*
386 1.1 bouyer * Update file and block pointers on disk before we start freeing
387 1.1 bouyer * blocks. If we crash before free'ing blocks below, the blocks
388 1.1 bouyer * will be returned to the free list. lastiblock values are also
389 1.1 bouyer * normalized to -1 for calls to ext2fs_indirtrunc below.
390 1.1 bouyer */
391 1.57 christos memcpy((void *)oldblks, (void *)&oip->i_e2fs_blocks[0], sizeof oldblks);
392 1.43 mycroft sync = 0;
393 1.43 mycroft for (level = TRIPLE; level >= SINGLE; level--) {
394 1.78 dholland if (lastiblock[level] < 0 && oldblks[EXT2FS_NDADDR + level] != 0) {
395 1.43 mycroft sync = 1;
396 1.78 dholland oip->i_e2fs_blocks[EXT2FS_NDADDR + level] = 0;
397 1.1 bouyer lastiblock[level] = -1;
398 1.1 bouyer }
399 1.43 mycroft }
400 1.78 dholland for (i = 0; i < EXT2FS_NDADDR; i++) {
401 1.43 mycroft if (i > lastblock && oldblks[i] != 0) {
402 1.43 mycroft sync = 1;
403 1.43 mycroft oip->i_e2fs_blocks[i] = 0;
404 1.43 mycroft }
405 1.43 mycroft }
406 1.1 bouyer oip->i_flag |= IN_CHANGE | IN_UPDATE;
407 1.43 mycroft if (sync) {
408 1.50 yamt error = ext2fs_update(ovp, NULL, NULL, UPDATE_WAIT);
409 1.43 mycroft if (error && !allerror)
410 1.43 mycroft allerror = error;
411 1.43 mycroft }
412 1.16 mycroft
413 1.1 bouyer /*
414 1.1 bouyer * Having written the new inode to disk, save its new configuration
415 1.1 bouyer * and put back the old block pointers long enough to process them.
416 1.1 bouyer * Note that we save the new block configuration so we can check it
417 1.1 bouyer * when we are done.
418 1.1 bouyer */
419 1.57 christos memcpy((void *)newblks, (void *)&oip->i_e2fs_blocks[0], sizeof newblks);
420 1.57 christos memcpy((void *)&oip->i_e2fs_blocks[0], (void *)oldblks, sizeof oldblks);
421 1.43 mycroft
422 1.44 ws (void)ext2fs_setsize(oip, osize);
423 1.16 mycroft error = vtruncbuf(ovp, lastblock + 1, 0, 0);
424 1.16 mycroft if (error && !allerror)
425 1.16 mycroft allerror = error;
426 1.1 bouyer
427 1.1 bouyer /*
428 1.1 bouyer * Indirect blocks first.
429 1.1 bouyer */
430 1.78 dholland indir_lbn[SINGLE] = -EXT2FS_NDADDR;
431 1.79 dholland indir_lbn[DOUBLE] = indir_lbn[SINGLE] - EXT2_NINDIR(fs) -1;
432 1.79 dholland indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - EXT2_NINDIR(fs) * EXT2_NINDIR(fs) - 1;
433 1.1 bouyer for (level = TRIPLE; level >= SINGLE; level--) {
434 1.29 fvdl /* XXX ondisk32 */
435 1.78 dholland bn = fs2h32(oip->i_e2fs_blocks[EXT2FS_NDADDR + level]);
436 1.1 bouyer if (bn != 0) {
437 1.1 bouyer error = ext2fs_indirtrunc(oip, indir_lbn[level],
438 1.80 dholland EXT2_FSBTODB(fs, bn), lastiblock[level], level, &count);
439 1.1 bouyer if (error)
440 1.1 bouyer allerror = error;
441 1.1 bouyer blocksreleased += count;
442 1.1 bouyer if (lastiblock[level] < 0) {
443 1.78 dholland oip->i_e2fs_blocks[EXT2FS_NDADDR + level] = 0;
444 1.1 bouyer ext2fs_blkfree(oip, bn);
445 1.1 bouyer blocksreleased += nblocks;
446 1.1 bouyer }
447 1.1 bouyer }
448 1.1 bouyer if (lastiblock[level] >= 0)
449 1.1 bouyer goto done;
450 1.1 bouyer }
451 1.1 bouyer
452 1.1 bouyer /*
453 1.1 bouyer * All whole direct blocks or frags.
454 1.1 bouyer */
455 1.78 dholland for (i = EXT2FS_NDADDR - 1; i > lastblock; i--) {
456 1.29 fvdl /* XXX ondisk32 */
457 1.3 bouyer bn = fs2h32(oip->i_e2fs_blocks[i]);
458 1.1 bouyer if (bn == 0)
459 1.1 bouyer continue;
460 1.1 bouyer oip->i_e2fs_blocks[i] = 0;
461 1.1 bouyer ext2fs_blkfree(oip, bn);
462 1.1 bouyer blocksreleased += btodb(fs->e2fs_bsize);
463 1.1 bouyer }
464 1.1 bouyer
465 1.1 bouyer done:
466 1.1 bouyer #ifdef DIAGNOSTIC
467 1.1 bouyer for (level = SINGLE; level <= TRIPLE; level++)
468 1.78 dholland if (newblks[EXT2FS_NDADDR + level] !=
469 1.78 dholland oip->i_e2fs_blocks[EXT2FS_NDADDR + level])
470 1.21 chs panic("ext2fs_truncate1");
471 1.78 dholland for (i = 0; i < EXT2FS_NDADDR; i++)
472 1.16 mycroft if (newblks[i] != oip->i_e2fs_blocks[i])
473 1.21 chs panic("ext2fs_truncate2");
474 1.1 bouyer if (length == 0 &&
475 1.21 chs (!LIST_EMPTY(&ovp->v_cleanblkhd) ||
476 1.21 chs !LIST_EMPTY(&ovp->v_dirtyblkhd)))
477 1.21 chs panic("ext2fs_truncate3");
478 1.1 bouyer #endif /* DIAGNOSTIC */
479 1.1 bouyer /*
480 1.1 bouyer * Put back the real size.
481 1.1 bouyer */
482 1.44 ws (void)ext2fs_setsize(oip, length);
483 1.76 jakllsch error = ext2fs_setnblock(oip, ext2fs_nblock(oip) - blocksreleased);
484 1.76 jakllsch if (error != 0)
485 1.76 jakllsch allerror = error;
486 1.1 bouyer oip->i_flag |= IN_CHANGE;
487 1.46 kml KASSERT(ovp->v_type != VREG || ovp->v_size == ext2fs_size(oip));
488 1.1 bouyer return (allerror);
489 1.1 bouyer }
490 1.1 bouyer
491 1.1 bouyer /*
492 1.1 bouyer * Release blocks associated with the inode ip and stored in the indirect
493 1.1 bouyer * block bn. Blocks are free'd in LIFO order up to (but not including)
494 1.1 bouyer * lastbn. If level is greater than SINGLE, the block is an indirect block
495 1.1 bouyer * and recursive calls to indirtrunc must be used to cleanse other indirect
496 1.1 bouyer * blocks.
497 1.1 bouyer *
498 1.1 bouyer * NB: triple indirect blocks are untested.
499 1.1 bouyer */
500 1.1 bouyer static int
501 1.47 xtraeme ext2fs_indirtrunc(struct inode *ip, daddr_t lbn, daddr_t dbn, daddr_t lastbn,
502 1.47 xtraeme int level, long *countp)
503 1.1 bouyer {
504 1.14 augustss int i;
505 1.1 bouyer struct buf *bp;
506 1.14 augustss struct m_ext2fs *fs = ip->i_e2fs;
507 1.29 fvdl int32_t *bap; /* XXX ondisk32 */
508 1.1 bouyer struct vnode *vp;
509 1.29 fvdl daddr_t nb, nlbn, last;
510 1.29 fvdl int32_t *copy = NULL; /* XXX ondisk32 */
511 1.1 bouyer long blkcount, factor;
512 1.1 bouyer int nblocks, blocksreleased = 0;
513 1.1 bouyer int error = 0, allerror = 0;
514 1.1 bouyer
515 1.1 bouyer /*
516 1.1 bouyer * Calculate index in current block of last
517 1.1 bouyer * block to be kept. -1 indicates the entire
518 1.1 bouyer * block so we need not calculate the index.
519 1.1 bouyer */
520 1.1 bouyer factor = 1;
521 1.1 bouyer for (i = SINGLE; i < level; i++)
522 1.79 dholland factor *= EXT2_NINDIR(fs);
523 1.1 bouyer last = lastbn;
524 1.1 bouyer if (lastbn > 0)
525 1.1 bouyer last /= factor;
526 1.1 bouyer nblocks = btodb(fs->e2fs_bsize);
527 1.1 bouyer /*
528 1.1 bouyer * Get buffer of block pointers, zero those entries corresponding
529 1.1 bouyer * to blocks to be free'd, and update on disk copy first. Since
530 1.1 bouyer * double(triple) indirect before single(double) indirect, calls
531 1.1 bouyer * to bmap on these blocks will fail. However, we already have
532 1.1 bouyer * the on disk address, so we have to set the b_blkno field
533 1.1 bouyer * explicitly instead of letting bread do everything for us.
534 1.1 bouyer */
535 1.1 bouyer vp = ITOV(ip);
536 1.1 bouyer bp = getblk(vp, lbn, (int)fs->e2fs_bsize, 0, 0);
537 1.63 ad if (bp->b_oflags & (BO_DONE | BO_DELWRI)) {
538 1.1 bouyer /* Braces must be here in case trace evaluates to nothing. */
539 1.1 bouyer trace(TR_BREADHIT, pack(vp, fs->e2fs_bsize), lbn);
540 1.1 bouyer } else {
541 1.1 bouyer trace(TR_BREADMISS, pack(vp, fs->e2fs_bsize), lbn);
542 1.65 ad curlwp->l_ru.ru_inblock++; /* pay for read */
543 1.1 bouyer bp->b_flags |= B_READ;
544 1.1 bouyer if (bp->b_bcount > bp->b_bufsize)
545 1.1 bouyer panic("ext2fs_indirtrunc: bad buffer size");
546 1.1 bouyer bp->b_blkno = dbn;
547 1.39 hannken VOP_STRATEGY(vp, bp);
548 1.1 bouyer error = biowait(bp);
549 1.1 bouyer }
550 1.1 bouyer if (error) {
551 1.60 ad brelse(bp, 0);
552 1.1 bouyer *countp = 0;
553 1.1 bouyer return (error);
554 1.1 bouyer }
555 1.1 bouyer
556 1.29 fvdl bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
557 1.17 mycroft if (lastbn >= 0) {
558 1.29 fvdl /* XXX ondisk32 */
559 1.75 para copy = kmem_alloc(fs->e2fs_bsize, KM_SLEEP);
560 1.57 christos memcpy((void *)copy, (void *)bap, (u_int)fs->e2fs_bsize);
561 1.57 christos memset((void *)&bap[last + 1], 0,
562 1.79 dholland (u_int)(EXT2_NINDIR(fs) - (last + 1)) * sizeof (uint32_t));
563 1.1 bouyer error = bwrite(bp);
564 1.1 bouyer if (error)
565 1.1 bouyer allerror = error;
566 1.1 bouyer bap = copy;
567 1.1 bouyer }
568 1.1 bouyer
569 1.1 bouyer /*
570 1.1 bouyer * Recursively free totally unused blocks.
571 1.1 bouyer */
572 1.79 dholland for (i = EXT2_NINDIR(fs) - 1,
573 1.1 bouyer nlbn = lbn + 1 - i * factor; i > last;
574 1.1 bouyer i--, nlbn += factor) {
575 1.29 fvdl /* XXX ondisk32 */
576 1.3 bouyer nb = fs2h32(bap[i]);
577 1.1 bouyer if (nb == 0)
578 1.1 bouyer continue;
579 1.1 bouyer if (level > SINGLE) {
580 1.80 dholland error = ext2fs_indirtrunc(ip, nlbn, EXT2_FSBTODB(fs, nb),
581 1.29 fvdl (daddr_t)-1, level - 1,
582 1.1 bouyer &blkcount);
583 1.1 bouyer if (error)
584 1.1 bouyer allerror = error;
585 1.1 bouyer blocksreleased += blkcount;
586 1.1 bouyer }
587 1.1 bouyer ext2fs_blkfree(ip, nb);
588 1.1 bouyer blocksreleased += nblocks;
589 1.1 bouyer }
590 1.1 bouyer
591 1.1 bouyer /*
592 1.1 bouyer * Recursively free last partial block.
593 1.1 bouyer */
594 1.1 bouyer if (level > SINGLE && lastbn >= 0) {
595 1.1 bouyer last = lastbn % factor;
596 1.29 fvdl /* XXX ondisk32 */
597 1.3 bouyer nb = fs2h32(bap[i]);
598 1.1 bouyer if (nb != 0) {
599 1.80 dholland error = ext2fs_indirtrunc(ip, nlbn, EXT2_FSBTODB(fs, nb),
600 1.1 bouyer last, level - 1, &blkcount);
601 1.1 bouyer if (error)
602 1.1 bouyer allerror = error;
603 1.1 bouyer blocksreleased += blkcount;
604 1.1 bouyer }
605 1.1 bouyer }
606 1.1 bouyer
607 1.1 bouyer if (copy != NULL) {
608 1.75 para kmem_free(copy, fs->e2fs_bsize);
609 1.1 bouyer } else {
610 1.60 ad brelse(bp, BC_INVAL);
611 1.1 bouyer }
612 1.1 bouyer
613 1.1 bouyer *countp = blocksreleased;
614 1.1 bouyer return (allerror);
615 1.1 bouyer }
616