File: | ufs/ext2fs/ext2fs_inode.c |
Warning: | line 332, column 3 Value stored to 'allerror' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* $OpenBSD: ext2fs_inode.c,v 1.65 2021/12/12 09:14:59 visa Exp $ */ |
2 | /* $NetBSD: ext2fs_inode.c,v 1.24 2001/06/19 12:59:18 wiz Exp $ */ |
3 | |
4 | /* |
5 | * Copyright (c) 1997 Manuel Bouyer. |
6 | * Copyright (c) 1982, 1986, 1989, 1993 |
7 | * The Regents of the University of California. All rights reserved. |
8 | * |
9 | * Redistribution and use in source and binary forms, with or without |
10 | * modification, are permitted provided that the following conditions |
11 | * are met: |
12 | * 1. Redistributions of source code must retain the above copyright |
13 | * notice, this list of conditions and the following disclaimer. |
14 | * 2. Redistributions in binary form must reproduce the above copyright |
15 | * notice, this list of conditions and the following disclaimer in the |
16 | * documentation and/or other materials provided with the distribution. |
17 | * 3. Neither the name of the University nor the names of its contributors |
18 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. |
20 | * |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
31 | * SUCH DAMAGE. |
32 | * |
33 | * @(#)ffs_inode.c 8.8 (Berkeley) 10/19/94 |
34 | * Modified for ext2fs by Manuel Bouyer. |
35 | */ |
36 | |
37 | #include <sys/param.h> |
38 | #include <sys/systm.h> |
39 | #include <sys/mount.h> |
40 | #include <sys/proc.h> |
41 | #include <sys/buf.h> |
42 | #include <sys/vnode.h> |
43 | #include <sys/kernel.h> |
44 | #include <sys/malloc.h> |
45 | #include <sys/resourcevar.h> |
46 | |
47 | #include <ufs/ufs/quota.h> |
48 | #include <ufs/ufs/inode.h> |
49 | #include <ufs/ufs/ufsmount.h> |
50 | #include <ufs/ufs/ufs_extern.h> |
51 | |
52 | #include <ufs/ext2fs/ext2fs.h> |
53 | #include <ufs/ext2fs/ext2fs_extern.h> |
54 | |
55 | static int ext2fs_indirtrunc(struct inode *, int32_t, int32_t, |
56 | int32_t, int, long *); |
57 | |
58 | /* |
59 | * Get the size of an inode. |
60 | */ |
61 | u_int64_t |
62 | ext2fs_size(struct inode *ip) |
63 | { |
64 | u_int64_t size = ip->i_e2fs_sizedinode_u.e2fs_din->e2di_size; |
65 | |
66 | if ((ip->i_e2fs_modedinode_u.e2fs_din->e2di_mode & IFMT0170000) == IFREG0100000) |
67 | size |= (u_int64_t)ip->i_e2fs_size_hidinode_u.e2fs_din->e2di_size_hi << 32; |
68 | |
69 | return (size); |
70 | } |
71 | |
72 | int |
73 | ext2fs_setsize(struct inode *ip, u_int64_t size) |
74 | { |
75 | struct m_ext2fs *fs = ip->i_e2fsinode_u.e2fs; |
76 | |
77 | if (size <= fs->e2fs_maxfilesize) { |
78 | /* If HUGE_FILEs are off, e2fs_maxfilesize will protect us. */ |
79 | if ((ip->i_e2fs_modedinode_u.e2fs_din->e2di_mode & IFMT0170000) == IFREG0100000 || ip->i_e2fs_modedinode_u.e2fs_din->e2di_mode == 0) |
80 | ip->i_e2fs_size_hidinode_u.e2fs_din->e2di_size_hi = size >> 32; |
81 | |
82 | ip->i_e2fs_sizedinode_u.e2fs_din->e2di_size = size; |
83 | return (0); |
84 | } |
85 | |
86 | /* Linux automagically upgrades to REV1 here! */ |
87 | if (fs->e2fs.e2fs_rev <= E2FS_REV00) |
88 | return (EFBIG27); |
89 | |
90 | if (!(fs->e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_LARGE_FILE0x0002)) { |
91 | fs->e2fs.e2fs_features_rocompat |= EXT2F_ROCOMPAT_LARGE_FILE0x0002; |
92 | fs->e2fs_fmod = 1; |
93 | } |
94 | return (EFBIG27); |
95 | } |
96 | |
97 | |
98 | /* |
99 | * Last reference to an inode. If necessary, write or delete it. |
100 | */ |
101 | int |
102 | ext2fs_inactive(void *v) |
103 | { |
104 | struct vop_inactive_args *ap = v; |
105 | struct vnode *vp = ap->a_vp; |
106 | struct inode *ip = VTOI(vp)((struct inode *)(vp)->v_data); |
107 | struct timespec ts; |
108 | int error = 0; |
109 | #ifdef DIAGNOSTIC1 |
110 | extern int prtactive; |
111 | |
112 | if (prtactive && vp->v_usecount != 0) |
113 | vprint("ext2fs_inactive: pushing active", vp); |
114 | #endif |
115 | |
116 | /* Get rid of inodes related to stale file handles. */ |
117 | if (ip->i_e2dindinode_u.e2fs_din == NULL((void *)0) || ip->i_e2fs_modedinode_u.e2fs_din->e2di_mode == 0 || ip->i_e2fs_dtimedinode_u.e2fs_din->e2di_dtime) |
118 | goto out; |
119 | |
120 | error = 0; |
121 | if (ip->i_e2fs_nlinkdinode_u.e2fs_din->e2di_nlink == 0 && (vp->v_mount->mnt_flag & MNT_RDONLY0x00000001) == 0) { |
122 | if (ext2fs_size(ip) != 0) { |
123 | error = ext2fs_truncate(ip, (off_t)0, 0, NOCRED((struct ucred *)-1)); |
124 | } |
125 | getnanotime(&ts); |
126 | ip->i_e2fs_dtimedinode_u.e2fs_din->e2di_dtime = ts.tv_sec; |
127 | ip->i_flag |= IN_CHANGE0x0002 | IN_UPDATE0x0004; |
128 | ext2fs_inode_free(ip, ip->i_number, ip->i_e2fs_modedinode_u.e2fs_din->e2di_mode); |
129 | } |
130 | if (ip->i_flag & (IN_ACCESS0x0001 | IN_CHANGE0x0002 | IN_MODIFIED0x0008 | IN_UPDATE0x0004)) { |
131 | ext2fs_update(ip, 0); |
132 | } |
133 | out: |
134 | VOP_UNLOCK(vp); |
135 | /* |
136 | * If we are done with the inode, reclaim it |
137 | * so that it can be reused immediately. |
138 | */ |
139 | if (ip->i_e2dindinode_u.e2fs_din == NULL((void *)0) || ip->i_e2fs_dtimedinode_u.e2fs_din->e2di_dtime != 0) |
140 | vrecycle(vp, ap->a_p); |
141 | return (error); |
142 | } |
143 | |
144 | |
145 | /* |
146 | * Update the access, modified, and inode change times as specified by the |
147 | * IACCESS, IUPDATE, and ICHANGE flags respectively. The IMODIFIED flag is |
148 | * used to specify that the inode needs to be updated but that the times have |
149 | * already been set. The access and modified times are taken from the second |
150 | * and third parameters; the inode change time is always taken from the current |
151 | * time. If waitfor is set, then wait for the disk write of the inode to |
152 | * complete. |
153 | */ |
154 | int |
155 | ext2fs_update(struct inode *ip, int waitfor) |
156 | { |
157 | struct m_ext2fs *fs; |
158 | struct buf *bp; |
159 | int error; |
160 | caddr_t cp; |
161 | |
162 | if (ITOV(ip)((ip)->i_vnode)->v_mount->mnt_flag & MNT_RDONLY0x00000001) |
163 | return (0); |
164 | EXT2FS_ITIMES(ip)do { if ((ip)->i_flag & (0x0001 | 0x0002 | 0x0004)) { ( ip)->i_flag |= 0x0008; if ((ip)->i_flag & 0x0001) ( ip)->dinode_u.e2fs_din->e2di_atime = gettime(); if ((ip )->i_flag & 0x0004) (ip)->dinode_u.e2fs_din->e2di_mtime = gettime(); if ((ip)->i_flag & 0x0002) { (ip)->dinode_u .e2fs_din->e2di_ctime = gettime(); (ip)->i_modrev++; } ( ip)->i_flag &= ~(0x0001 | 0x0002 | 0x0004); } } while ( 0); |
165 | if ((ip->i_flag & IN_MODIFIED0x0008) == 0) |
166 | return (0); |
167 | ip->i_flag &= ~IN_MODIFIED0x0008; |
168 | fs = ip->i_e2fsinode_u.e2fs; |
169 | error = bread(ip->i_devvpi_ump->um_devvp, |
170 | fsbtodb(fs, ino_to_fsba(fs, ip->i_number))((((fs)->e2fs_gd[(((ip->i_number) - 1) / (fs)->e2fs. e2fs_ipg)].ext2bgd_i_tables + (((ip->i_number)-1) % (fs)-> e2fs.e2fs_ipg)/(fs)->e2fs_ipb)) << (fs)->e2fs_fsbtodb ), |
171 | (int)fs->e2fs_bsize, &bp); |
172 | if (error) { |
173 | brelse(bp); |
174 | return (error); |
175 | } |
176 | ip->i_flag &= ~(IN_MODIFIED0x0008); |
177 | cp = (caddr_t)bp->b_data + |
178 | (ino_to_fsbo(fs, ip->i_number)(((ip->i_number)-1) % (fs)->e2fs_ipb) * EXT2_DINODE_SIZE(fs)((fs)->e2fs.e2fs_rev > 0 ? (fs)->e2fs.e2fs_inode_size : 128)); |
179 | |
180 | /* |
181 | * See note about 16-bit UID/GID limitation in ext2fs_vget(). Now |
182 | * that we are about to write the inode, construct the split UID and |
183 | * GID fields out of the two 32-bit fields we kept in memory. |
184 | */ |
185 | ip->i_e2fs_uid_lowdinode_u.e2fs_din->e2di_uid_low = (u_int16_t)ip->i_e2fs_uidinode_ext.e2fs.ext2fs_effective_uid; |
186 | ip->i_e2fs_gid_lowdinode_u.e2fs_din->e2di_gid_low = (u_int16_t)ip->i_e2fs_gidinode_ext.e2fs.ext2fs_effective_gid; |
187 | ip->i_e2fs_uid_highdinode_u.e2fs_din->e2di_uid_high = ip->i_e2fs_uidinode_ext.e2fs.ext2fs_effective_uid >> 16; |
188 | ip->i_e2fs_gid_highdinode_u.e2fs_din->e2di_gid_high = ip->i_e2fs_gidinode_ext.e2fs.ext2fs_effective_gid >> 16; |
189 | |
190 | e2fs_isave(fs, ip->i_e2din, (struct ext2fs_dinode *)cp)__builtin_memcpy((((struct ext2fs_dinode *)cp)), ((ip->dinode_u .e2fs_din)), ((((((fs)->e2fs.e2fs_rev > 0 ? (fs)->e2fs .e2fs_inode_size : 128))<(sizeof(*(struct ext2fs_dinode *) cp)))?(((fs)->e2fs.e2fs_rev > 0 ? (fs)->e2fs.e2fs_inode_size : 128)):(sizeof(*(struct ext2fs_dinode *)cp))))); |
191 | if (waitfor) |
192 | return (bwrite(bp)); |
193 | else { |
194 | bdwrite(bp); |
195 | return (0); |
196 | } |
197 | } |
198 | |
199 | #define SINGLE0 0 /* index of single indirect block */ |
200 | #define DOUBLE1 1 /* index of double indirect block */ |
201 | #define TRIPLE2 2 /* index of triple indirect block */ |
202 | /* |
203 | * Truncate the inode oip to at most length size, freeing the |
204 | * disk blocks. |
205 | */ |
206 | int |
207 | ext2fs_truncate(struct inode *oip, off_t length, int flags, struct ucred *cred) |
208 | { |
209 | struct vnode *ovp = ITOV(oip)((oip)->i_vnode); |
210 | int32_t lastblock; |
211 | int32_t bn, lbn, lastiblock[NIADDR3], indir_lbn[NIADDR3]; |
212 | int32_t oldblks[NDADDR12 + NIADDR3], newblks[NDADDR12 + NIADDR3]; |
213 | struct m_ext2fs *fs; |
214 | struct buf *bp; |
215 | int offset, size, level; |
216 | long count, nblocks, vflags, blocksreleased = 0; |
217 | int i; |
218 | int aflags, error, allerror; |
219 | off_t osize; |
220 | |
221 | if (length < 0) |
222 | return (EINVAL22); |
223 | |
224 | if (ovp->v_type != VREG && |
225 | ovp->v_type != VDIR && |
226 | ovp->v_type != VLNK) |
227 | return (0); |
228 | |
229 | if (ovp->v_type == VLNK && ext2fs_size(oip) < EXT2_MAXSYMLINKLEN((12 +3) * sizeof (u_int32_t))) { |
230 | #ifdef DIAGNOSTIC1 |
231 | if (length != 0) |
232 | panic("ext2fs_truncate: partial truncate of symlink"); |
233 | #endif |
234 | memset(&oip->i_e2din->e2di_shortlink, 0, ext2fs_size(oip))__builtin_memset((&oip->dinode_u.e2fs_din->e2di_blocks ), (0), (ext2fs_size(oip))); |
235 | (void)ext2fs_setsize(oip, 0); |
236 | oip->i_flag |= IN_CHANGE0x0002 | IN_UPDATE0x0004; |
237 | return (ext2fs_update(oip, 1)); |
238 | } |
239 | |
240 | if (ext2fs_size(oip) == length) { |
241 | oip->i_flag |= IN_CHANGE0x0002 | IN_UPDATE0x0004; |
242 | return (ext2fs_update(oip, 0)); |
243 | } |
244 | fs = oip->i_e2fsinode_u.e2fs; |
245 | osize = ext2fs_size(oip); |
246 | /* |
247 | * Lengthen the size of the file. We must ensure that the |
248 | * last byte of the file is allocated. Since the smallest |
249 | * value of osize is 0, length will be at least 1. |
250 | */ |
251 | if (osize < length) { |
252 | #if 0 /* XXX */ |
253 | if (length > fs->fs_maxfilesize) |
254 | return (EFBIG27); |
255 | #endif |
256 | offset = blkoff(fs, length - 1)((length - 1) & (fs)->e2fs_qbmask); |
257 | lbn = lblkno(fs, length - 1)((length - 1) >> (fs)->e2fs_bshift); |
258 | aflags = B_CLRBUF0x01; |
259 | if (flags & IO_SYNC0x04) |
260 | aflags |= B_SYNC0x02; |
261 | error = ext2fs_buf_alloc(oip, lbn, offset + 1, cred, &bp, |
262 | aflags); |
263 | if (error) |
264 | return (error); |
265 | (void)ext2fs_setsize(oip, length); |
266 | uvm_vnp_setsize(ovp, length); |
267 | uvm_vnp_uncache(ovp); |
268 | if (aflags & B_SYNC0x02) |
269 | bwrite(bp); |
270 | else |
271 | bawrite(bp); |
272 | oip->i_flag |= IN_CHANGE0x0002 | IN_UPDATE0x0004; |
273 | return (ext2fs_update(oip, 1)); |
274 | } |
275 | /* |
276 | * Shorten the size of the file. If the file is not being |
277 | * truncated to a block boundary, the contents of the |
278 | * partial block following the end of the file must be |
279 | * zero'ed in case it ever become accessible again because |
280 | * of subsequent file growth. |
281 | */ |
282 | offset = blkoff(fs, length)((length) & (fs)->e2fs_qbmask); |
283 | if (offset == 0) { |
284 | (void)ext2fs_setsize(oip, length); |
285 | } else { |
286 | lbn = lblkno(fs, length)((length) >> (fs)->e2fs_bshift); |
287 | aflags = B_CLRBUF0x01; |
288 | if (flags & IO_SYNC0x04) |
289 | aflags |= B_SYNC0x02; |
290 | error = ext2fs_buf_alloc(oip, lbn, offset, cred, &bp, |
291 | aflags); |
292 | if (error) |
293 | return (error); |
294 | (void)ext2fs_setsize(oip, length); |
295 | size = fs->e2fs_bsize; |
296 | uvm_vnp_setsize(ovp, length); |
297 | uvm_vnp_uncache(ovp); |
298 | memset(bp->b_data + offset, 0, size - offset)__builtin_memset((bp->b_data + offset), (0), (size - offset )); |
299 | bp->b_bcount = size; |
300 | if (aflags & B_SYNC0x02) |
301 | bwrite(bp); |
302 | else |
303 | bawrite(bp); |
304 | } |
305 | /* |
306 | * Calculate index into inode's block list of |
307 | * last direct and indirect blocks (if any) |
308 | * which we want to keep. Lastblock is -1 when |
309 | * the file is truncated to 0. |
310 | */ |
311 | lastblock = lblkno(fs, length + fs->e2fs_bsize - 1)((length + fs->e2fs_bsize - 1) >> (fs)->e2fs_bshift ) - 1; |
312 | lastiblock[SINGLE0] = lastblock - NDADDR12; |
313 | lastiblock[DOUBLE1] = lastiblock[SINGLE0] - NINDIR(fs)((fs)->e2fs_bsize / sizeof(u_int32_t)); |
314 | lastiblock[TRIPLE2] = lastiblock[DOUBLE1] - NINDIR(fs)((fs)->e2fs_bsize / sizeof(u_int32_t)) * NINDIR(fs)((fs)->e2fs_bsize / sizeof(u_int32_t)); |
315 | nblocks = btodb(fs->e2fs_bsize)((fs->e2fs_bsize) >> 9); |
316 | /* |
317 | * Update file and block pointers on disk before we start freeing |
318 | * blocks. If we crash before free'ing blocks below, the blocks |
319 | * will be returned to the free list. lastiblock values are also |
320 | * normalized to -1 for calls to ext2fs_indirtrunc below. |
321 | */ |
322 | memcpy(oldblks, &oip->i_e2fs_blocks[0], sizeof(oldblks))__builtin_memcpy((oldblks), (&oip->dinode_u.e2fs_din-> e2di_blocks[0]), (sizeof(oldblks))); |
323 | for (level = TRIPLE2; level >= SINGLE0; level--) |
324 | if (lastiblock[level] < 0) { |
325 | oip->i_e2fs_blocksdinode_u.e2fs_din->e2di_blocks[NDADDR12 + level] = 0; |
326 | lastiblock[level] = -1; |
327 | } |
328 | for (i = NDADDR12 - 1; i > lastblock; i--) |
329 | oip->i_e2fs_blocksdinode_u.e2fs_din->e2di_blocks[i] = 0; |
330 | oip->i_flag |= IN_CHANGE0x0002 | IN_UPDATE0x0004; |
331 | if ((error = ext2fs_update(oip, 1)) != 0) |
332 | allerror = error; |
Value stored to 'allerror' is never read | |
333 | /* |
334 | * Having written the new inode to disk, save its new configuration |
335 | * and put back the old block pointers long enough to process them. |
336 | * Note that we save the new block configuration so we can check it |
337 | * when we are done. |
338 | */ |
339 | memcpy(newblks, &oip->i_e2fs_blocks[0], sizeof(newblks))__builtin_memcpy((newblks), (&oip->dinode_u.e2fs_din-> e2di_blocks[0]), (sizeof(newblks))); |
340 | memcpy(&oip->i_e2fs_blocks[0], oldblks, sizeof(oldblks))__builtin_memcpy((&oip->dinode_u.e2fs_din->e2di_blocks [0]), (oldblks), (sizeof(oldblks))); |
341 | (void)ext2fs_setsize(oip, osize); |
342 | vflags = ((length > 0) ? V_SAVE0x0001 : 0) | V_SAVEMETA0x0002; |
343 | allerror = vinvalbuf(ovp, vflags, cred, curproc({struct cpu_info *__ci; asm volatile("movq %%gs:%P1,%0" : "=r" (__ci) :"n" (__builtin_offsetof(struct cpu_info, ci_self))); __ci;})->ci_curproc, 0, INFSLP0xffffffffffffffffULL); |
344 | |
345 | /* |
346 | * Indirect blocks first. |
347 | */ |
348 | indir_lbn[SINGLE0] = -NDADDR12; |
349 | indir_lbn[DOUBLE1] = indir_lbn[SINGLE0] - NINDIR(fs)((fs)->e2fs_bsize / sizeof(u_int32_t)) -1; |
350 | indir_lbn[TRIPLE2] = indir_lbn[DOUBLE1] - NINDIR(fs)((fs)->e2fs_bsize / sizeof(u_int32_t)) * NINDIR(fs)((fs)->e2fs_bsize / sizeof(u_int32_t)) - 1; |
351 | for (level = TRIPLE2; level >= SINGLE0; level--) { |
352 | bn = letoh32(oip->i_e2fs_blocks[NDADDR + level])((__uint32_t)(oip->dinode_u.e2fs_din->e2di_blocks[12 + level ])); |
353 | if (bn != 0) { |
354 | error = ext2fs_indirtrunc(oip, indir_lbn[level], |
355 | fsbtodb(fs, bn)((bn) << (fs)->e2fs_fsbtodb), lastiblock[level], level, &count); |
356 | if (error) |
357 | allerror = error; |
358 | blocksreleased += count; |
359 | if (lastiblock[level] < 0) { |
360 | oip->i_e2fs_blocksdinode_u.e2fs_din->e2di_blocks[NDADDR12 + level] = 0; |
361 | ext2fs_blkfree(oip, bn); |
362 | blocksreleased += nblocks; |
363 | } |
364 | } |
365 | if (lastiblock[level] >= 0) |
366 | goto done; |
367 | } |
368 | |
369 | /* |
370 | * All whole direct blocks or frags. |
371 | */ |
372 | for (i = NDADDR12 - 1; i > lastblock; i--) { |
373 | bn = letoh32(oip->i_e2fs_blocks[i])((__uint32_t)(oip->dinode_u.e2fs_din->e2di_blocks[i])); |
374 | if (bn == 0) |
375 | continue; |
376 | oip->i_e2fs_blocksdinode_u.e2fs_din->e2di_blocks[i] = 0; |
377 | ext2fs_blkfree(oip, bn); |
378 | blocksreleased += btodb(fs->e2fs_bsize)((fs->e2fs_bsize) >> 9); |
379 | } |
380 | |
381 | done: |
382 | #ifdef DIAGNOSTIC1 |
383 | for (level = SINGLE0; level <= TRIPLE2; level++) |
384 | if (newblks[NDADDR12 + level] != |
385 | oip->i_e2fs_blocksdinode_u.e2fs_din->e2di_blocks[NDADDR12 + level]) |
386 | panic("ext2fs_truncate1"); |
387 | for (i = 0; i < NDADDR12; i++) |
388 | if (newblks[i] != oip->i_e2fs_blocksdinode_u.e2fs_din->e2di_blocks[i]) |
389 | panic("ext2fs_truncate2"); |
390 | if (length == 0 && |
391 | (!LIST_EMPTY(&ovp->v_cleanblkhd)(((&ovp->v_cleanblkhd)->lh_first) == ((void *)0)) || |
392 | !LIST_EMPTY(&ovp->v_dirtyblkhd)(((&ovp->v_dirtyblkhd)->lh_first) == ((void *)0)))) |
393 | panic("ext2fs_truncate3"); |
394 | #endif /* DIAGNOSTIC */ |
395 | /* |
396 | * Put back the real size. |
397 | */ |
398 | (void)ext2fs_setsize(oip, length); |
399 | if (blocksreleased >= oip->i_e2fs_nblockdinode_u.e2fs_din->e2di_nblock) |
400 | oip->i_e2fs_nblockdinode_u.e2fs_din->e2di_nblock = 0; |
401 | else |
402 | oip->i_e2fs_nblockdinode_u.e2fs_din->e2di_nblock -= blocksreleased; |
403 | oip->i_flag |= IN_CHANGE0x0002; |
404 | return (allerror); |
405 | } |
406 | |
407 | /* |
408 | * Release blocks associated with the inode ip and stored in the indirect |
409 | * block bn. Blocks are free'd in LIFO order up to (but not including) |
410 | * lastbn. If level is greater than SINGLE, the block is an indirect block |
411 | * and recursive calls to indirtrunc must be used to cleanse other indirect |
412 | * blocks. |
413 | * |
414 | * NB: triple indirect blocks are untested. |
415 | */ |
416 | static int |
417 | ext2fs_indirtrunc(struct inode *ip, int32_t lbn, int32_t dbn, int32_t lastbn, int level, long *countp) |
418 | { |
419 | int i; |
420 | struct buf *bp; |
421 | struct m_ext2fs *fs = ip->i_e2fsinode_u.e2fs; |
422 | int32_t *bap; |
423 | struct vnode *vp; |
424 | int32_t *copy = NULL((void *)0), nb, nlbn, last; |
425 | long blkcount, factor; |
426 | int nblocks, blocksreleased = 0; |
427 | int error = 0, allerror = 0; |
428 | |
429 | /* |
430 | * Calculate index in current block of last |
431 | * block to be kept. -1 indicates the entire |
432 | * block so we need not calculate the index. |
433 | */ |
434 | factor = 1; |
435 | for (i = SINGLE0; i < level; i++) |
436 | factor *= NINDIR(fs)((fs)->e2fs_bsize / sizeof(u_int32_t)); |
437 | last = lastbn; |
438 | if (lastbn > 0) |
439 | last /= factor; |
440 | nblocks = btodb(fs->e2fs_bsize)((fs->e2fs_bsize) >> 9); |
441 | /* |
442 | * Get buffer of block pointers, zero those entries corresponding |
443 | * to blocks to be free'd, and update on disk copy first. Since |
444 | * double(triple) indirect before single(double) indirect, calls |
445 | * to bmap on these blocks will fail. However, we already have |
446 | * the on disk address, so we have to set the b_blkno field |
447 | * explicitly instead of letting bread do everything for us. |
448 | */ |
449 | vp = ITOV(ip)((ip)->i_vnode); |
450 | bp = getblk(vp, lbn, (int)fs->e2fs_bsize, 0, INFSLP0xffffffffffffffffULL); |
451 | if (!(bp->b_flags & (B_DONE0x00000100 | B_DELWRI0x00000080))) { |
452 | curproc({struct cpu_info *__ci; asm volatile("movq %%gs:%P1,%0" : "=r" (__ci) :"n" (__builtin_offsetof(struct cpu_info, ci_self))); __ci;})->ci_curproc->p_ru.ru_inblock++; /* pay for read */ |
453 | bcstats.pendingreads++; |
454 | bcstats.numreads++; |
455 | bp->b_flags |= B_READ0x00008000; |
456 | if (bp->b_bcount > bp->b_bufsize) |
457 | panic("ext2fs_indirtrunc: bad buffer size"); |
458 | bp->b_blkno = dbn; |
459 | VOP_STRATEGY(bp->b_vp, bp); |
460 | error = biowait(bp); |
461 | } |
462 | if (error) { |
463 | brelse(bp); |
464 | *countp = 0; |
465 | return (error); |
466 | } |
467 | |
468 | bap = (int32_t *)bp->b_data; |
469 | if (lastbn >= 0) { |
470 | copy = malloc(fs->e2fs_bsize, M_TEMP127, M_WAITOK0x0001); |
471 | memcpy(copy, bap, fs->e2fs_bsize)__builtin_memcpy((copy), (bap), (fs->e2fs_bsize)); |
472 | memset(&bap[last + 1], 0,__builtin_memset((&bap[last + 1]), (0), ((((fs)->e2fs_bsize / sizeof(u_int32_t)) - (last + 1)) * sizeof(u_int32_t))) |
473 | (NINDIR(fs) - (last + 1)) * sizeof(u_int32_t))__builtin_memset((&bap[last + 1]), (0), ((((fs)->e2fs_bsize / sizeof(u_int32_t)) - (last + 1)) * sizeof(u_int32_t))); |
474 | error = bwrite(bp); |
475 | if (error) |
476 | allerror = error; |
477 | bap = copy; |
478 | } |
479 | |
480 | /* |
481 | * Recursively free totally unused blocks. |
482 | */ |
483 | for (i = NINDIR(fs)((fs)->e2fs_bsize / sizeof(u_int32_t)) - 1, |
484 | nlbn = lbn + 1 - i * factor; i > last; |
485 | i--, nlbn += factor) { |
486 | nb = letoh32(bap[i])((__uint32_t)(bap[i])); |
487 | if (nb == 0) |
488 | continue; |
489 | if (level > SINGLE0) { |
490 | error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb)((nb) << (fs)->e2fs_fsbtodb), |
491 | (int32_t)-1, level - 1, |
492 | &blkcount); |
493 | if (error) |
494 | allerror = error; |
495 | blocksreleased += blkcount; |
496 | } |
497 | ext2fs_blkfree(ip, nb); |
498 | blocksreleased += nblocks; |
499 | } |
500 | |
501 | /* |
502 | * Recursively free last partial block. |
503 | */ |
504 | if (level > SINGLE0 && lastbn >= 0) { |
505 | last = lastbn % factor; |
506 | nb = letoh32(bap[i])((__uint32_t)(bap[i])); |
507 | if (nb != 0) { |
508 | error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb)((nb) << (fs)->e2fs_fsbtodb), |
509 | last, level - 1, &blkcount); |
510 | if (error) |
511 | allerror = error; |
512 | blocksreleased += blkcount; |
513 | } |
514 | } |
515 | |
516 | if (copy != NULL((void *)0)) { |
517 | free(copy, M_TEMP127, fs->e2fs_bsize); |
518 | } else { |
519 | bp->b_flags |= B_INVAL0x00000800; |
520 | brelse(bp); |
521 | } |
522 | |
523 | *countp = blocksreleased; |
524 | return (allerror); |
525 | } |