Bug Summary

File:msdosfs/msdosfs_denode.c
Warning:line 328, column 5
Value stored to 'error' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name msdosfs_denode.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -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 -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 -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/lib/clang/13.0.0 -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/swsmu -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/powerplay -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/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 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 -D CONFIG_DRM_AMD_DC_DCN3_0 -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 -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 /usr/obj/sys/arch/amd64/compile/GENERIC.MP/scan-build/2022-01-12-131800-47421-1 -x c /usr/src/sys/msdosfs/msdosfs_denode.c
1/* $OpenBSD: msdosfs_denode.c,v 1.66 2020/02/27 09:10:31 mpi Exp $ */
2/* $NetBSD: msdosfs_denode.c,v 1.23 1997/10/17 11:23:58 ws Exp $ */
3
4/*-
5 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
7 * All rights reserved.
8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by TooLs GmbH.
21 * 4. The name of TooLs GmbH may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35/*
36 * Written by Paul Popelka (paulp@uts.amdahl.com)
37 *
38 * You can do anything you want with this software, just don't say you wrote
39 * it, and don't remove this notice.
40 *
41 * This software is provided "as is".
42 *
43 * The author supplies this software to be publicly redistributed on the
44 * understanding that the author is not responsible for the correct
45 * functioning of this software in any circumstances and is not liable for
46 * any damages caused by this software.
47 *
48 * October 1992
49 */
50
51#include <sys/param.h>
52#include <sys/systm.h>
53#include <sys/mount.h>
54#include <sys/malloc.h>
55#include <sys/buf.h>
56#include <sys/vnode.h>
57#include <sys/lock.h>
58#include <sys/kernel.h> /* defines "time" */
59#include <sys/dirent.h>
60#include <sys/namei.h>
61
62#include <crypto/siphash.h>
63
64#include <msdosfs/bpb.h>
65#include <msdosfs/msdosfsmount.h>
66#include <msdosfs/direntry.h>
67#include <msdosfs/denode.h>
68#include <msdosfs/fat.h>
69
70u_int msdosfs_dehash(dev_t, uint32_t, uint32_t);
71
72struct denode **dehashtbl;
73SIPHASH_KEY dehashkey;
74u_long dehash; /* size of hash table - 1 */
75#define DEHASH(dev, dcl, doff)msdosfs_dehash((dev), (dcl), (doff)) msdosfs_dehash((dev), (dcl), (doff))
76
77static struct denode *msdosfs_hashget(dev_t, uint32_t, uint32_t);
78static int msdosfs_hashins(struct denode *);
79static void msdosfs_hashrem(struct denode *);
80
81/*ARGSUSED*/
82int
83msdosfs_init(struct vfsconf *vfsp)
84{
85 dehashtbl = hashinit(initialvnodes / 2, M_MSDOSFSMNT59, M_WAITOK0x0001, &dehash);
86 arc4random_buf(&dehashkey, sizeof(dehashkey));
87 return (0);
88}
89
90u_int
91msdosfs_dehash(dev_t dev, uint32_t dirclust, uint32_t diroff)
92{
93 SIPHASH_CTX ctx;
94
95 SipHash24_Init(&ctx, &dehashkey)SipHash_Init((&ctx), (&dehashkey));
96 SipHash24_Update(&ctx, &dev, sizeof(dev))SipHash_Update((&ctx), 2, 4, (&dev), (sizeof(dev)));
97 SipHash24_Update(&ctx, &dirclust, sizeof(dirclust))SipHash_Update((&ctx), 2, 4, (&dirclust), (sizeof(dirclust
)))
;
98 SipHash24_Update(&ctx, &diroff, sizeof(diroff))SipHash_Update((&ctx), 2, 4, (&diroff), (sizeof(diroff
)))
;
99
100 return (SipHash24_End(&ctx)SipHash_End((&ctx), 2, 4) & dehash);
101}
102
103static struct denode *
104msdosfs_hashget(dev_t dev, uint32_t dirclust, uint32_t diroff)
105{
106 struct denode *dep;
107
108 for (;;)
109 for (dep = dehashtbl[DEHASH(dev, dirclust, diroff)msdosfs_dehash((dev), (dirclust), (diroff))]; ;
110 dep = dep->de_next) {
111 if (dep == NULL((void *)0))
112 return (NULL((void *)0));
113 if (dirclust == dep->de_dirclust &&
114 diroff == dep->de_diroffset &&
115 dev == dep->de_dev &&
116 dep->de_refcnt != 0) {
117 struct vnode *vp = DETOV(dep)((dep)->de_vnode);
118
119 if (!vget(vp, LK_EXCLUSIVE0x0001UL))
120 return (dep);
121 break;
122 }
123 }
124 /* NOTREACHED */
125}
126
127static int
128msdosfs_hashins(struct denode *dep)
129{
130 struct denode **depp, *deq;
131
132 depp = &dehashtbl[DEHASH(dep->de_dev, dep->de_dirclust,msdosfs_dehash((dep->de_dev), (dep->de_dirclust), (dep->
de_diroffset))
133 dep->de_diroffset)msdosfs_dehash((dep->de_dev), (dep->de_dirclust), (dep->
de_diroffset))
];
134
135 for (deq = *depp; deq; deq = deq->de_next) {
136 if (dep->de_dirclust == deq->de_dirclust &&
137 dep->de_diroffset == deq->de_diroffset &&
138 dep->de_dev == deq->de_dev &&
139 deq->de_refcnt != 0) {
140 return (EEXIST17);
141 }
142 }
143
144 if ((deq = *depp) != NULL((void *)0))
145 deq->de_prev = &dep->de_next;
146 dep->de_next = deq;
147 dep->de_prev = depp;
148 *depp = dep;
149 return (0);
150}
151
152static void
153msdosfs_hashrem(struct denode *dep)
154{
155 struct denode *deq;
156
157 if (dep->de_prev == NULL((void *)0))
158 return;
159
160 if ((deq = dep->de_next) != NULL((void *)0))
161 deq->de_prev = dep->de_prev;
162 *dep->de_prev = deq;
163#ifdef DIAGNOSTIC1
164 dep->de_next = NULL((void *)0);
165 dep->de_prev = NULL((void *)0);
166#endif
167}
168
169/*
170 * If deget() succeeds it returns with the gotten denode locked().
171 *
172 * pmp - address of msdosfsmount structure of the filesystem containing
173 * the denode of interest. The pm_dev field and the address of
174 * the msdosfsmount structure are used.
175 * dirclust - which cluster bp contains, if dirclust is 0 (root directory)
176 * diroffset is relative to the beginning of the root directory,
177 * otherwise it is cluster relative.
178 * diroffset - offset past begin of cluster of denode we want
179 * depp - returns the address of the gotten denode.
180 */
181int
182deget(struct msdosfsmount *pmp, uint32_t dirclust, uint32_t diroffset,
183 struct denode **depp)
184{
185 int error;
186 extern const struct vops msdosfs_vops;
187 struct direntry *direntptr;
188 struct denode *ldep;
189 struct vnode *nvp;
190 struct buf *bp;
191
192#ifdef MSDOSFS_DEBUG
193 printf("deget(pmp %p, dirclust %d, diroffset %x, depp %p)\n",
194 pmp, dirclust, diroffset, depp);
195#endif
196
197 /*
198 * On FAT32 filesystems, root is a (more or less) normal
199 * directory
200 */
201 if (FAT32(pmp)(pmp->pm_fatmask == 0x0fffffff) && dirclust == MSDOSFSROOT0)
202 dirclust = pmp->pm_rootdirblk;
203
204 /*
205 * See if the denode is in the denode cache. Use the location of
206 * the directory entry to compute the hash value. For subdir use
207 * address of "." entry. For root dir (if not FAT32) use cluster
208 * MSDOSFSROOT, offset MSDOSFSROOT_OFS
209 *
210 * NOTE: The check for de_refcnt > 0 below insures the denode being
211 * examined does not represent an unlinked but still open file.
212 * These files are not to be accessible even when the directory
213 * entry that represented the file happens to be reused while the
214 * deleted file is still open.
215 */
216retry:
217 ldep = msdosfs_hashget(pmp->pm_dev, dirclust, diroffset);
218 if (ldep) {
219 *depp = ldep;
220 return (0);
221 }
222
223 /*
224 * Directory entry was not in cache, have to create a vnode and
225 * copy it from the passed disk buffer.
226 */
227 /* getnewvnode() does a vref() on the vnode */
228 error = getnewvnode(VT_MSDOSFS, pmp->pm_mountp, &msdosfs_vops, &nvp);
229 if (error) {
230 *depp = 0;
231 return (error);
232 }
233 ldep = malloc(sizeof(*ldep), M_MSDOSFSNODE61, M_WAITOK0x0001 | M_ZERO0x0008);
234 rrw_init_flags(&ldep->de_lock, "denode", RWL_DUPOK | RWL_IS_VNODE)_rrw_init_flags(&ldep->de_lock, "denode", 0, ((void *)
0))
;
235 nvp->v_data = ldep;
236 ldep->de_vnode = nvp;
237 ldep->de_flag = 0;
238 ldep->de_devvp = 0;
239 ldep->de_lockf = 0;
240 ldep->de_dev = pmp->pm_dev;
241 ldep->de_dirclust = dirclust;
242 ldep->de_diroffset = diroffset;
243 fc_purge(ldep, 0); /* init the fat cache for this denode */
244
245 /*
246 * Insert the denode into the hash queue and lock the denode so it
247 * can't be accessed until we've read it in and have done what we
248 * need to it.
249 */
250 vn_lock(nvp, LK_EXCLUSIVE0x0001UL | LK_RETRY0x2000UL);
251 error = msdosfs_hashins(ldep);
252
253 if (error) {
254 vput (nvp);
255
256 if (error == EEXIST17)
257 goto retry;
258
259 return (error);
260 }
261
262 ldep->de_pmp = pmp;
263 ldep->de_devvp = pmp->pm_devvp;
264 ldep->de_refcnt = 1;
265 /*
266 * Copy the directory entry into the denode area of the vnode.
267 */
268 if ((dirclust == MSDOSFSROOT0
269 || (FAT32(pmp)(pmp->pm_fatmask == 0x0fffffff) && dirclust == pmp->pm_rootdirblk))
270 && diroffset == MSDOSFSROOT_OFS0x1fffffff) {
271 /*
272 * Directory entry for the root directory. There isn't one,
273 * so we manufacture one. We should probably rummage
274 * through the root directory and find a label entry (if it
275 * exists), and then use the time and date from that entry
276 * as the time and date for the root denode.
277 */
278 nvp->v_flag |= VROOT0x0001; /* should be further down XXX */
279
280 ldep->de_Attributes = ATTR_DIRECTORY0x10;
281 if (FAT32(pmp)(pmp->pm_fatmask == 0x0fffffff))
282 ldep->de_StartCluster = pmp->pm_rootdirblk;
283 /* de_FileSize will be filled in further down */
284 else {
285 ldep->de_StartCluster = MSDOSFSROOT0;
286 ldep->de_FileSize = pmp->pm_rootdirsize * pmp->pm_BytesPerSecpm_bpb.bpbBytesPerSec;
287 }
288 /*
289 * fill in time and date so that dos2unixtime() doesn't
290 * spit up when called from msdosfs_getattr() with root
291 * denode
292 */
293 ldep->de_CTime = 0x0000; /* 00:00:00 */
294 ldep->de_CTimeHundredth = 0;
295 ldep->de_CDate = (0 << DD_YEAR_SHIFT9) | (1 << DD_MONTH_SHIFT5)
296 | (1 << DD_DAY_SHIFT0);
297 /* Jan 1, 1980 */
298 ldep->de_ADate = ldep->de_CDate;
299 ldep->de_MTime = ldep->de_CTime;
300 ldep->de_MDate = ldep->de_CDate;
301 /* leave the other fields as garbage */
302 } else {
303 error = readep(pmp, dirclust, diroffset, &bp, &direntptr);
304 if (error)
305 return (error);
306 DE_INTERNALIZE(ldep, direntptr)(bcopy((direntptr)->deName, (ldep)->de_Name, 8), bcopy(
(direntptr)->deExtension, (ldep)->de_Name + 8, 3), (ldep
)->de_Attributes = (direntptr)->deAttributes, (ldep)->
de_CTimeHundredth = (direntptr)->deCTimeHundredth, (ldep)->
de_CTime = *((u_int16_t *)((direntptr)->deCTime)), (ldep)->
de_CDate = *((u_int16_t *)((direntptr)->deCDate)), (ldep)->
de_ADate = *((u_int16_t *)((direntptr)->deADate)), (ldep)->
de_MTime = *((u_int16_t *)((direntptr)->deMTime)), (ldep)->
de_MDate = *((u_int16_t *)((direntptr)->deMDate)), (ldep)->
de_StartCluster = *((u_int16_t *)((direntptr)->deStartCluster
)), (ldep)->de_FileSize = *((u_int32_t *)((direntptr)->
deFileSize)), (((ldep)->de_pmp->pm_fatmask == 0x0fffffff
) ? (((ldep))->de_StartCluster |= *((u_int16_t *)(((direntptr
))->deHighClust)) << 16) : 0))
;
307 brelse(bp);
308 }
309
310 /*
311 * Fill in a few fields of the vnode and finish filling in the
312 * denode. Then return the address of the found denode.
313 */
314 if (ldep->de_Attributes & ATTR_DIRECTORY0x10) {
315 /*
316 * Since DOS directory entries that describe directories
317 * have 0 in the filesize field, we take this opportunity
318 * to find out the length of the directory and plug it into
319 * the denode structure.
320 */
321 uint32_t size;
322
323 nvp->v_type = VDIR;
324 if (ldep->de_StartCluster != MSDOSFSROOT0) {
325 error = pcbmap(ldep, CLUST_END0xffffffff, 0, &size, 0);
326 if (error == E2BIG7) {
327 ldep->de_FileSize = de_cn2off(pmp, size)((size) << (pmp)->pm_cnshift);
328 error = 0;
Value stored to 'error' is never read
329 } else if (error) {
330 printf("deget(): pcbmap returned %d\n", error);
331 return (error);
332 }
333 }
334 } else
335 nvp->v_type = VREG;
336 vref(ldep->de_devvp);
337 *depp = ldep;
338 return (0);
339}
340
341int
342deupdat(struct denode *dep, int waitfor)
343{
344 struct buf *bp;
345 struct direntry *dirp;
346 int error;
347 struct timespec ts;
348
349 if (DETOV(dep)((dep)->de_vnode)->v_mount->mnt_flag & MNT_RDONLY0x00000001)
350 return (0);
351 getnanotime(&ts);
352 DETIMES(dep, &ts, &ts, &ts)if ((dep)->de_flag & (0x0004 | 0x0008 | 0x0010)) { (dep
)->de_flag |= 0x0020; if ((dep)->de_flag & 0x0004) {
unix2dostime((&ts), &(dep)->de_MDate, &(dep)->
de_MTime, ((void *)0)); (dep)->de_Attributes |= 0x20; } if
(!((dep)->de_pmp->pm_flags & 0x04)) { if ((dep)->
de_flag & 0x0010) unix2dostime((&ts), &(dep)->
de_ADate, ((void *)0), ((void *)0)); if ((dep)->de_flag &
0x0008) unix2dostime((&ts), &(dep)->de_CDate, &
(dep)->de_CTime, &(dep)->de_CTimeHundredth); } (dep
)->de_flag &= ~(0x0004 | 0x0008 | 0x0010); }
;
353 if ((dep->de_flag & DE_MODIFIED0x0020) == 0)
354 return (0);
355 dep->de_flag &= ~DE_MODIFIED0x0020;
356 if (dep->de_Attributes & ATTR_DIRECTORY0x10)
357 return (0);
358 if (dep->de_refcnt <= 0)
359 return (0);
360 error = readde(dep, &bp, &dirp);
361 if (error)
362 return (error);
363 DE_EXTERNALIZE(dirp, dep)(bcopy((dep)->de_Name, (dirp)->deName, 8), bcopy((dep)->
de_Name + 8, (dirp)->deExtension, 3), (dirp)->deAttributes
= (dep)->de_Attributes, (dirp)->deLowerCase = 0x08 | 0x10
, (dirp)->deCTimeHundredth = (dep)->de_CTimeHundredth, (
*((u_int16_t *)((dirp)->deCTime)) = ((dep)->de_CTime)),
(*((u_int16_t *)((dirp)->deCDate)) = ((dep)->de_CDate)
), (*((u_int16_t *)((dirp)->deADate)) = ((dep)->de_ADate
)), (*((u_int16_t *)((dirp)->deMTime)) = ((dep)->de_MTime
)), (*((u_int16_t *)((dirp)->deMDate)) = ((dep)->de_MDate
)), (*((u_int16_t *)((dirp)->deStartCluster)) = ((dep)->
de_StartCluster)), (*((u_int32_t *)((dirp)->deFileSize)) =
(((dep)->de_Attributes & 0x10) ? 0 : (dep)->de_FileSize
)), (*((u_int16_t *)((dirp)->deHighClust)) = (((dep)->de_pmp
->pm_fatmask == 0x0fffffff) ? (dep)->de_StartCluster >>
16 : 0)))
;
364 if (waitfor)
365 return (bwrite(bp));
366 else {
367 bdwrite(bp);
368 return (0);
369 }
370}
371
372/*
373 * Truncate the file described by dep to the length specified by length.
374 */
375int
376detrunc(struct denode *dep, uint32_t length, int flags, struct ucred *cred,
377 struct proc *p)
378{
379 int error;
380 int allerror;
381 int vflags;
382 uint32_t eofentry;
383 uint32_t chaintofree = 0;
384 daddr_t bn;
385 int boff;
386 int isadir = dep->de_Attributes & ATTR_DIRECTORY0x10;
387 struct buf *bp;
388 struct msdosfsmount *pmp = dep->de_pmp;
389
390#ifdef MSDOSFS_DEBUG
391 printf("detrunc(): file %.11s, length %u, flags %d\n",
392 dep->de_Name, length, flags);
393#endif
394
395 /*
396 * Disallow attempts to truncate the root directory since it is of
397 * fixed size. That's just the way dos filesystems are. We use
398 * the VROOT bit in the vnode because checking for the directory
399 * bit and a startcluster of 0 in the denode is not adequate to
400 * recognize the root directory at this point in a file or
401 * directory's life.
402 */
403 if ((DETOV(dep)((dep)->de_vnode)->v_flag & VROOT0x0001) && !FAT32(pmp)(pmp->pm_fatmask == 0x0fffffff)) {
404 printf("detrunc(): can't truncate root directory, clust %u, offset %u\n",
405 dep->de_dirclust, dep->de_diroffset);
406 return (EINVAL22);
407 }
408
409 uvm_vnp_setsize(DETOV(dep)((dep)->de_vnode), length);
410
411 if (dep->de_FileSize < length)
412 return (deextend(dep, length, cred));
413
414 /*
415 * If the desired length is 0 then remember the starting cluster of
416 * the file and set the StartCluster field in the directory entry
417 * to 0. If the desired length is not zero, then get the number of
418 * the last cluster in the shortened file. Then get the number of
419 * the first cluster in the part of the file that is to be freed.
420 * Then set the next cluster pointer in the last cluster of the
421 * file to CLUST_EOFE.
422 */
423 if (length == 0) {
424 chaintofree = dep->de_StartCluster;
425 dep->de_StartCluster = 0;
426 eofentry = ~0;
427 } else {
428 error = pcbmap(dep, de_clcount(pmp, length)(((length) + (pmp)->pm_bpcluster - 1) >> (pmp)->pm_cnshift
)
- 1, 0,
429 &eofentry, 0);
430 if (error) {
431#ifdef MSDOSFS_DEBUG
432 printf("detrunc(): pcbmap fails %d\n", error);
433#endif
434 return (error);
435 }
436 }
437
438 fc_purge(dep, de_clcount(pmp, length)(((length) + (pmp)->pm_bpcluster - 1) >> (pmp)->pm_cnshift
)
);
439
440 /*
441 * If the new length is not a multiple of the cluster size then we
442 * must zero the tail end of the new last cluster in case it
443 * becomes part of the file again because of a seek.
444 */
445 if ((boff = length & pmp->pm_crbomask) != 0) {
446 if (isadir) {
447 bn = cntobn(pmp, eofentry)((((eofentry)-2) << (((pmp))->pm_cnshift - ((pmp))->
pm_bnshift)) + (pmp)->pm_firstcluster)
;
448 error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster, &bp);
449 } else {
450 bn = de_blk(pmp, length)((((((length)) >> ((pmp))->pm_cnshift)) << ((pmp
)->pm_cnshift - (pmp)->pm_bnshift)))
;
451 error = bread(DETOV(dep)((dep)->de_vnode), bn, pmp->pm_bpcluster, &bp);
452 }
453 if (error) {
454 brelse(bp);
455#ifdef MSDOSFS_DEBUG
456 printf("detrunc(): bread fails %d\n", error);
457#endif
458 return (error);
459 }
460 uvm_vnp_uncache(DETOV(dep)((dep)->de_vnode));
461 /*
462 * is this the right place for it?
463 */
464 bzero(bp->b_data + boff, pmp->pm_bpcluster - boff)__builtin_bzero((bp->b_data + boff), (pmp->pm_bpcluster
- boff))
;
465 if (flags & IO_SYNC0x04)
466 bwrite(bp);
467 else
468 bdwrite(bp);
469 }
470
471 /*
472 * Write out the updated directory entry. Even if the update fails
473 * we free the trailing clusters.
474 */
475 dep->de_FileSize = length;
476 if (!isadir)
477 dep->de_flag |= DE_UPDATE0x0004|DE_MODIFIED0x0020;
478 vflags = (length > 0 ? V_SAVE0x0001 : 0) | V_SAVEMETA0x0002;
479 vinvalbuf(DETOV(dep)((dep)->de_vnode), vflags, cred, p, 0, INFSLP0xffffffffffffffffULL);
480 allerror = deupdat(dep, 1);
481#ifdef MSDOSFS_DEBUG
482 printf("detrunc(): allerror %d, eofentry %d\n",
483 allerror, eofentry);
484#endif
485
486 /*
487 * If we need to break the cluster chain for the file then do it
488 * now.
489 */
490 if (eofentry != ~0) {
491 error = fatentry(FAT_GET_AND_SET(0x0001 | 0x0002), pmp, eofentry,
492 &chaintofree, CLUST_EOFE0xffffffff);
493 if (error) {
494#ifdef MSDOSFS_DEBUG
495 printf("detrunc(): fatentry errors %d\n", error);
496#endif
497 return (error);
498 }
499 fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),(dep)->de_fc[1].fc_frcn = ((length - 1) >> (pmp)->
pm_cnshift); (dep)->de_fc[1].fc_fsrcn = eofentry;
500 eofentry)(dep)->de_fc[1].fc_frcn = ((length - 1) >> (pmp)->
pm_cnshift); (dep)->de_fc[1].fc_fsrcn = eofentry;
;
501 }
502
503 /*
504 * Now free the clusters removed from the file because of the
505 * truncation.
506 */
507 if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree)((((chaintofree) | ~(pmp)->pm_fatmask) & 0xfffffff8) ==
0xfffffff8)
)
508 freeclusterchain(pmp, chaintofree);
509
510 return (allerror);
511}
512
513/*
514 * Extend the file described by dep to length specified by length.
515 */
516int
517deextend(struct denode *dep, uint32_t length, struct ucred *cred)
518{
519 struct msdosfsmount *pmp = dep->de_pmp;
520 uint32_t count;
521 int error;
522
523 /*
524 * The root of a DOS filesystem cannot be extended.
525 */
526 if ((DETOV(dep)((dep)->de_vnode)->v_flag & VROOT0x0001) && !FAT32(pmp)(pmp->pm_fatmask == 0x0fffffff))
527 return (EINVAL22);
528
529 /*
530 * Directories cannot be extended.
531 */
532 if (dep->de_Attributes & ATTR_DIRECTORY0x10)
533 return (EISDIR21);
534
535 if (length <= dep->de_FileSize)
536 panic("deextend: file too large");
537
538 /*
539 * Compute the number of clusters to allocate.
540 */
541 count = de_clcount(pmp, length)(((length) + (pmp)->pm_bpcluster - 1) >> (pmp)->pm_cnshift
)
- de_clcount(pmp, dep->de_FileSize)(((dep->de_FileSize) + (pmp)->pm_bpcluster - 1) >>
(pmp)->pm_cnshift)
;
542 if (count > 0) {
543 if (count > pmp->pm_freeclustercount)
544 return (ENOSPC28);
545 error = extendfile(dep, count, NULL((void *)0), NULL((void *)0), DE_CLEAR1);
546 if (error) {
547 /* truncate the added clusters away again */
548 (void) detrunc(dep, dep->de_FileSize, 0, cred, NULL((void *)0));
549 return (error);
550 }
551 }
552
553 dep->de_FileSize = length;
554 dep->de_flag |= DE_UPDATE0x0004|DE_MODIFIED0x0020;
555 return (deupdat(dep, 1));
556}
557
558/*
559 * Move a denode to its correct hash queue after the file it represents has
560 * been moved to a new directory.
561 */
562void
563reinsert(struct denode *dep)
564{
565 /*
566 * Fix up the denode cache. If the denode is for a directory,
567 * there is nothing to do since the hash is based on the starting
568 * cluster of the directory file and that hasn't changed. If for a
569 * file the hash is based on the location of the directory entry,
570 * so we must remove it from the cache and re-enter it with the
571 * hash based on the new location of the directory entry.
572 */
573 if (dep->de_Attributes & ATTR_DIRECTORY0x10)
574 return;
575 msdosfs_hashrem(dep);
576 msdosfs_hashins(dep);
577}
578
579int
580msdosfs_reclaim(void *v)
581{
582 struct vop_reclaim_args *ap = v;
583 struct vnode *vp = ap->a_vp;
584 struct denode *dep = VTODE(vp)((struct denode *)(vp)->v_data);
585#ifdef DIAGNOSTIC1
586 extern int prtactive;
587
588 if (prtactive && vp->v_usecount != 0)
589 vprint("msdosfs_reclaim(): pushing active", vp);
590#endif
591
592#ifdef MSDOSFS_DEBUG
593 printf("msdosfs_reclaim(): dep %p, file %.11s, refcnt %ld\n",
594 dep, dep->de_Name, dep->de_refcnt);
595#endif
596
597 /*
598 * Remove the denode from its hash chain.
599 */
600 msdosfs_hashrem(dep);
601 /*
602 * Purge old data structures associated with the denode.
603 */
604 cache_purge(vp);
605 if (dep->de_devvp) {
606 vrele(dep->de_devvp);
607 dep->de_devvp = 0;
608 }
609#if 0 /* XXX */
610 dep->de_flag = 0;
611#endif
612 free(dep, M_MSDOSFSNODE61, 0);
613 vp->v_data = NULL((void *)0);
614 return (0);
615}
616
617int
618msdosfs_inactive(void *v)
619{
620 struct vop_inactive_args *ap = v;
621 struct vnode *vp = ap->a_vp;
622 struct denode *dep = VTODE(vp)((struct denode *)(vp)->v_data);
623 int error;
624#ifdef DIAGNOSTIC1
625 extern int prtactive;
626
627 if (prtactive && vp->v_usecount != 0)
628 vprint("msdosfs_inactive(): pushing active", vp);
629#endif
630
631#ifdef MSDOSFS_DEBUG
632 printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep,
633 dep->de_Name[0]);
634#endif
635
636 error = 0;
637
638 /*
639 * Get rid of denodes related to stale file handles.
640 */
641 if (dep->de_Name[0] == SLOT_DELETED0xe5)
642 goto out;
643
644 /*
645 * If the file has been deleted and it is on a read/write
646 * filesystem, then truncate the file, and mark the directory slot
647 * as empty. (This may not be necessary for the dos filesystem.)
648 */
649#ifdef MSDOSFS_DEBUG
650 printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x, "
651 "MNT_RDONLY %x\n", dep, dep->de_refcnt, vp->v_mount->mnt_flag,
652 MNT_RDONLY0x00000001);
653#endif
654 if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY0x00000001) == 0) {
655 error = detrunc(dep, (uint32_t)0, 0, NOCRED((struct ucred *)-1), NULL((void *)0));
656 dep->de_Name[0] = SLOT_DELETED0xe5;
657 }
658 deupdat(dep, 0);
659
660out:
661 VOP_UNLOCK(vp);
662 /*
663 * If we are done with the denode, reclaim it
664 * so that it can be reused immediately.
665 */
666#ifdef MSDOSFS_DEBUG
667 printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n",
668 vp->v_usecount, dep->de_Name[0]);
669#endif
670 if (dep->de_Name[0] == SLOT_DELETED0xe5)
671 vrecycle(vp, ap->a_p);
672 return (error);
673}