Bug Summary

File:ufs/ext2fs/ext2fs_inode.c
Warning:line 332, column 3
Value stored to 'allerror' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.4 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name ext2fs_inode.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -ffp-contract=on -fno-rounding-math -mconstructor-aliases -ffreestanding -mcmodel=kernel -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -target-feature -sse2 -target-feature -sse -target-feature -3dnow -target-feature -mmx -target-feature +save-args -target-feature +retpoline-external-thunk -disable-red-zone -no-implicit-float -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -nostdsysteminc -nobuiltininc -resource-dir /usr/local/llvm16/lib/clang/16 -I /usr/src/sys -I /usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -I /usr/src/sys/arch -I /usr/src/sys/dev/pci/drm/include -I /usr/src/sys/dev/pci/drm/include/uapi -I /usr/src/sys/dev/pci/drm/amd/include/asic_reg -I /usr/src/sys/dev/pci/drm/amd/include -I /usr/src/sys/dev/pci/drm/amd/amdgpu -I /usr/src/sys/dev/pci/drm/amd/display -I /usr/src/sys/dev/pci/drm/amd/display/include -I /usr/src/sys/dev/pci/drm/amd/display/dc -I /usr/src/sys/dev/pci/drm/amd/display/amdgpu_dm -I /usr/src/sys/dev/pci/drm/amd/pm/inc -I /usr/src/sys/dev/pci/drm/amd/pm/legacy-dpm -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/inc -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu11 -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu12 -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu13 -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/inc -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/hwmgr -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/smumgr -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/inc -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/inc/pmfw_if -I /usr/src/sys/dev/pci/drm/amd/display/dc/inc -I /usr/src/sys/dev/pci/drm/amd/display/dc/inc/hw -I /usr/src/sys/dev/pci/drm/amd/display/dc/clk_mgr -I /usr/src/sys/dev/pci/drm/amd/display/modules/inc -I /usr/src/sys/dev/pci/drm/amd/display/modules/hdcp -I /usr/src/sys/dev/pci/drm/amd/display/dmub/inc -I /usr/src/sys/dev/pci/drm/i915 -D DDB -D DIAGNOSTIC -D KTRACE -D ACCOUNTING -D KMEMSTATS -D PTRACE -D POOL_DEBUG -D CRYPTO -D SYSVMSG -D SYSVSEM -D SYSVSHM -D UVM_SWAP_ENCRYPT -D FFS -D FFS2 -D FFS_SOFTUPDATES -D UFS_DIRHASH -D QUOTA -D EXT2FS -D MFS -D NFSCLIENT -D NFSSERVER -D CD9660 -D UDF -D MSDOSFS -D FIFO -D FUSE -D SOCKET_SPLICE -D TCP_ECN -D TCP_SIGNATURE -D INET6 -D IPSEC -D PPP_BSDCOMP -D PPP_DEFLATE -D PIPEX -D MROUTING -D MPLS -D BOOT_CONFIG -D USER_PCICONF -D APERTURE -D MTRR -D NTFS -D SUSPEND -D HIBERNATE -D PCIVERBOSE -D USBVERBOSE -D WSDISPLAY_COMPAT_USL -D WSDISPLAY_COMPAT_RAWKBD -D WSDISPLAY_DEFAULTSCREENS=6 -D X86EMU -D ONEWIREVERBOSE -D MULTIPROCESSOR -D MAXUSERS=80 -D _KERNEL -O2 -Wno-pointer-sign -Wno-address-of-packed-member -Wno-constant-conversion -Wno-unused-but-set-variable -Wno-gnu-folding-constant -fdebug-compilation-dir=/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fcf-protection=branch -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -o /home/ben/Projects/scan/2024-01-11-110808-61670-1 -x c /usr/src/sys/ufs/ext2fs/ext2fs_inode.c
1/* $OpenBSD: ext2fs_inode.c,v 1.66 2022/08/12 14:30:53 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
55static int ext2fs_indirtrunc(struct inode *, int32_t, int32_t,
56 int32_t, int, long *);
57
58/*
59 * Get the size of an inode.
60 */
61u_int64_t
62ext2fs_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
72int
73ext2fs_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 */
101int
102ext2fs_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 }
133out:
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 */
154int
155ext2fs_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 */
206int
207ext2fs_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
381done:
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 int s;
392
393 s = splbio()splraise(0x3);
394 if (!LIST_EMPTY(&ovp->v_cleanblkhd)(((&ovp->v_cleanblkhd)->lh_first) == ((void *)0)) ||
395 !LIST_EMPTY(&ovp->v_dirtyblkhd)(((&ovp->v_dirtyblkhd)->lh_first) == ((void *)0)))
396 panic("ext2fs_truncate3");
397 splx(s)spllower(s);
398 }
399#endif /* DIAGNOSTIC */
400 /*
401 * Put back the real size.
402 */
403 (void)ext2fs_setsize(oip, length);
404 if (blocksreleased >= oip->i_e2fs_nblockdinode_u.e2fs_din->e2di_nblock)
405 oip->i_e2fs_nblockdinode_u.e2fs_din->e2di_nblock = 0;
406 else
407 oip->i_e2fs_nblockdinode_u.e2fs_din->e2di_nblock -= blocksreleased;
408 oip->i_flag |= IN_CHANGE0x0002;
409 return (allerror);
410}
411
412/*
413 * Release blocks associated with the inode ip and stored in the indirect
414 * block bn. Blocks are free'd in LIFO order up to (but not including)
415 * lastbn. If level is greater than SINGLE, the block is an indirect block
416 * and recursive calls to indirtrunc must be used to cleanse other indirect
417 * blocks.
418 *
419 * NB: triple indirect blocks are untested.
420 */
421static int
422ext2fs_indirtrunc(struct inode *ip, int32_t lbn, int32_t dbn, int32_t lastbn, int level, long *countp)
423{
424 int i;
425 struct buf *bp;
426 struct m_ext2fs *fs = ip->i_e2fsinode_u.e2fs;
427 int32_t *bap;
428 struct vnode *vp;
429 int32_t *copy = NULL((void *)0), nb, nlbn, last;
430 long blkcount, factor;
431 int nblocks, blocksreleased = 0;
432 int error = 0, allerror = 0;
433
434 /*
435 * Calculate index in current block of last
436 * block to be kept. -1 indicates the entire
437 * block so we need not calculate the index.
438 */
439 factor = 1;
440 for (i = SINGLE0; i < level; i++)
441 factor *= NINDIR(fs)((fs)->e2fs_bsize / sizeof(u_int32_t));
442 last = lastbn;
443 if (lastbn > 0)
444 last /= factor;
445 nblocks = btodb(fs->e2fs_bsize)((fs->e2fs_bsize) >> 9);
446 /*
447 * Get buffer of block pointers, zero those entries corresponding
448 * to blocks to be free'd, and update on disk copy first. Since
449 * double(triple) indirect before single(double) indirect, calls
450 * to bmap on these blocks will fail. However, we already have
451 * the on disk address, so we have to set the b_blkno field
452 * explicitly instead of letting bread do everything for us.
453 */
454 vp = ITOV(ip)((ip)->i_vnode);
455 bp = getblk(vp, lbn, (int)fs->e2fs_bsize, 0, INFSLP0xffffffffffffffffULL);
456 if (!(bp->b_flags & (B_DONE0x00000100 | B_DELWRI0x00000080))) {
457 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 */
458 bcstats.pendingreads++;
459 bcstats.numreads++;
460 bp->b_flags |= B_READ0x00008000;
461 if (bp->b_bcount > bp->b_bufsize)
462 panic("ext2fs_indirtrunc: bad buffer size");
463 bp->b_blkno = dbn;
464 VOP_STRATEGY(bp->b_vp, bp);
465 error = biowait(bp);
466 }
467 if (error) {
468 brelse(bp);
469 *countp = 0;
470 return (error);
471 }
472
473 bap = (int32_t *)bp->b_data;
474 if (lastbn >= 0) {
475 copy = malloc(fs->e2fs_bsize, M_TEMP127, M_WAITOK0x0001);
476 memcpy(copy, bap, fs->e2fs_bsize)__builtin_memcpy((copy), (bap), (fs->e2fs_bsize));
477 memset(&bap[last + 1], 0,__builtin_memset((&bap[last + 1]), (0), ((((fs)->e2fs_bsize
/ sizeof(u_int32_t)) - (last + 1)) * sizeof(u_int32_t)))
478 (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)))
;
479 error = bwrite(bp);
480 if (error)
481 allerror = error;
482 bap = copy;
483 }
484
485 /*
486 * Recursively free totally unused blocks.
487 */
488 for (i = NINDIR(fs)((fs)->e2fs_bsize / sizeof(u_int32_t)) - 1,
489 nlbn = lbn + 1 - i * factor; i > last;
490 i--, nlbn += factor) {
491 nb = letoh32(bap[i])((__uint32_t)(bap[i]));
492 if (nb == 0)
493 continue;
494 if (level > SINGLE0) {
495 error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb)((nb) << (fs)->e2fs_fsbtodb),
496 (int32_t)-1, level - 1,
497 &blkcount);
498 if (error)
499 allerror = error;
500 blocksreleased += blkcount;
501 }
502 ext2fs_blkfree(ip, nb);
503 blocksreleased += nblocks;
504 }
505
506 /*
507 * Recursively free last partial block.
508 */
509 if (level > SINGLE0 && lastbn >= 0) {
510 last = lastbn % factor;
511 nb = letoh32(bap[i])((__uint32_t)(bap[i]));
512 if (nb != 0) {
513 error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb)((nb) << (fs)->e2fs_fsbtodb),
514 last, level - 1, &blkcount);
515 if (error)
516 allerror = error;
517 blocksreleased += blkcount;
518 }
519 }
520
521 if (copy != NULL((void *)0)) {
522 free(copy, M_TEMP127, fs->e2fs_bsize);
523 } else {
524 bp->b_flags |= B_INVAL0x00000800;
525 brelse(bp);
526 }
527
528 *countp = blocksreleased;
529 return (allerror);
530}