Bug Summary

File:msdosfs/msdosfs_vnops.c
Warning:line 1360, column 2
Value stored to 'error' 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 msdosfs_vnops.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/msdosfs/msdosfs_vnops.c
1/* $OpenBSD: msdosfs_vnops.c,v 1.140 2023/09/08 20:00:28 mvs Exp $ */
2/* $NetBSD: msdosfs_vnops.c,v 1.63 1997/10/17 11:24:19 ws Exp $ */
3
4/*-
5 * Copyright (C) 2005 Thomas Wang.
6 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
7 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
8 * All rights reserved.
9 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by TooLs GmbH.
22 * 4. The name of TooLs GmbH may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
31 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36/*
37 * Written by Paul Popelka (paulp@uts.amdahl.com)
38 *
39 * You can do anything you want with this software, just don't say you wrote
40 * it, and don't remove this notice.
41 *
42 * This software is provided "as is".
43 *
44 * The author supplies this software to be publicly redistributed on the
45 * understanding that the author is not responsible for the correct
46 * functioning of this software in any circumstances and is not liable for
47 * any damages caused by this software.
48 *
49 * October 1992
50 */
51
52#include <sys/param.h>
53#include <sys/systm.h>
54#include <sys/namei.h>
55#include <sys/resourcevar.h> /* defines plimit structure in proc struct */
56#include <sys/fcntl.h> /* define FWRITE ... */
57#include <sys/file.h>
58#include <sys/stat.h>
59#include <sys/buf.h>
60#include <sys/proc.h>
61#include <sys/mount.h>
62#include <sys/vnode.h>
63#include <sys/lock.h>
64#include <sys/signalvar.h>
65#include <sys/specdev.h> /* XXX */ /* defines v_rdev */
66#include <sys/malloc.h>
67#include <sys/pool.h>
68#include <sys/dirent.h> /* defines dirent structure */
69#include <sys/lockf.h>
70#include <sys/unistd.h>
71
72#include <msdosfs/bpb.h>
73#include <msdosfs/direntry.h>
74#include <msdosfs/denode.h>
75#include <msdosfs/msdosfsmount.h>
76#include <msdosfs/fat.h>
77
78static uint32_t fileidhash(uint64_t);
79
80int msdosfs_bmaparray(struct vnode *, uint32_t, daddr_t *, int *);
81int msdosfs_kqfilter(void *);
82int filt_msdosfsread(struct knote *, long);
83int filt_msdosfswrite(struct knote *, long);
84int filt_msdosfsvnode(struct knote *, long);
85void filt_msdosfsdetach(struct knote *);
86
87/*
88 * Some general notes:
89 *
90 * In the ufs filesystem the inodes, superblocks, and indirect blocks are
91 * read/written using the vnode for the filesystem. Blocks that represent
92 * the contents of a file are read/written using the vnode for the file
93 * (including directories when they are read/written as files). This
94 * presents problems for the dos filesystem because data that should be in
95 * an inode (if dos had them) resides in the directory itself. Since we
96 * must update directory entries without the benefit of having the vnode
97 * for the directory we must use the vnode for the filesystem. This means
98 * that when a directory is actually read/written (via read, write, or
99 * readdir, or seek) we must use the vnode for the filesystem instead of
100 * the vnode for the directory as would happen in ufs. This is to insure we
101 * retrieve the correct block from the buffer cache since the hash value is
102 * based upon the vnode address and the desired block number.
103 */
104
105/*
106 * Create a regular file. On entry the directory to contain the file being
107 * created is locked. We must release before we return. We must also free
108 * the pathname buffer pointed at by cnp->cn_pnbuf, always on error, or
109 * only if the SAVESTART bit in cn_flags is clear on success.
110 */
111int
112msdosfs_create(void *v)
113{
114 struct vop_create_args *ap = v;
115 struct componentname *cnp = ap->a_cnp;
116 struct denode ndirent;
117 struct denode *dep;
118 struct denode *pdep = VTODE(ap->a_dvp)((struct denode *)(ap->a_dvp)->v_data);
119 int error;
120 struct timespec ts;
121
122#ifdef MSDOSFS_DEBUG
123 printf("msdosfs_create(cnp %p, vap %p\n", cnp, ap->a_vap);
124#endif
125
126 /*
127 * If this is the root directory and there is no space left we
128 * can't do anything. This is because the root directory can not
129 * change size.
130 */
131 if (pdep->de_StartCluster == MSDOSFSROOT0
132 && pdep->de_fndoffset >= pdep->de_FileSize) {
133 error = ENOSPC28;
134 goto bad;
135 }
136
137 /*
138 * Create a directory entry for the file, then call createde() to
139 * have it installed. NOTE: DOS files are always executable. We
140 * use the absence of the owner write bit to make the file
141 * readonly.
142 */
143#ifdef DIAGNOSTIC1
144 if ((cnp->cn_flags & HASBUF0x000400) == 0)
145 panic("msdosfs_create: no name");
146#endif
147 bzero(&ndirent, sizeof(ndirent))__builtin_bzero((&ndirent), (sizeof(ndirent)));
148 if ((error = uniqdosname(pdep, cnp, ndirent.de_Name)) != 0)
149 goto bad;
150
151 ndirent.de_Attributes = (ap->a_vap->va_mode & VWRITE00200) ?
152 ATTR_ARCHIVE0x20 : ATTR_ARCHIVE0x20 | ATTR_READONLY0x01;
153 ndirent.de_StartCluster = 0;
154 ndirent.de_FileSize = 0;
155 ndirent.de_dev = pdep->de_dev;
156 ndirent.de_devvp = pdep->de_devvp;
157 ndirent.de_pmp = pdep->de_pmp;
158 ndirent.de_flag = DE_ACCESS0x0010 | DE_CREATE0x0008 | DE_UPDATE0x0004;
159 getnanotime(&ts);
160 DETIMES(&ndirent, &ts, &ts, &ts)if ((&ndirent)->de_flag & (0x0004 | 0x0008 | 0x0010
)) { (&ndirent)->de_flag |= 0x0020; if ((&ndirent)
->de_flag & 0x0004) { unix2dostime((&ts), &(&
ndirent)->de_MDate, &(&ndirent)->de_MTime, ((void
*)0)); (&ndirent)->de_Attributes |= 0x20; } if (!((&
ndirent)->de_pmp->pm_flags & 0x04)) { if ((&ndirent
)->de_flag & 0x0010) unix2dostime((&ts), &(&
ndirent)->de_ADate, ((void *)0), ((void *)0)); if ((&ndirent
)->de_flag & 0x0008) unix2dostime((&ts), &(&
ndirent)->de_CDate, &(&ndirent)->de_CTime, &
(&ndirent)->de_CTimeHundredth); } (&ndirent)->de_flag
&= ~(0x0004 | 0x0008 | 0x0010); }
;
161 if ((error = createde(&ndirent, pdep, &dep, cnp)) != 0)
162 goto bad;
163 if ((cnp->cn_flags & SAVESTART0x001000) == 0)
164 pool_put(&namei_pool, cnp->cn_pnbuf);
165 VN_KNOTE(ap->a_dvp, NOTE_WRITE)knote_locked(&ap->a_dvp->v_klist, (0x0002));
166 *ap->a_vpp = DETOV(dep)((dep)->de_vnode);
167 return (0);
168
169bad:
170 pool_put(&namei_pool, cnp->cn_pnbuf);
171 return (error);
172}
173
174int
175msdosfs_mknod(void *v)
176{
177 struct vop_mknod_args *ap = v;
178
179 pool_put(&namei_pool, ap->a_cnp->cn_pnbuf);
180 VN_KNOTE(ap->a_dvp, NOTE_WRITE)knote_locked(&ap->a_dvp->v_klist, (0x0002));
181 return (EINVAL22);
182}
183
184int
185msdosfs_open(void *v)
186{
187 return (0);
188}
189
190int
191msdosfs_close(void *v)
192{
193 struct vop_close_args *ap = v;
194 struct vnode *vp = ap->a_vp;
195 struct denode *dep = VTODE(vp)((struct denode *)(vp)->v_data);
196 struct timespec ts;
197
198 if (vp->v_usecount > 1 && !VOP_ISLOCKED(vp)) {
199 getnanotime(&ts);
200 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); }
;
201 }
202 return (0);
203}
204
205int
206msdosfs_access(void *v)
207{
208 struct vop_access_args *ap = v;
209 struct denode *dep = VTODE(ap->a_vp)((struct denode *)(ap->a_vp)->v_data);
210 struct msdosfsmount *pmp = dep->de_pmp;
211 mode_t dosmode;
212
213 dosmode = (S_IRUSR0000400 | S_IRGRP0000040 | S_IROTH0000004);
214 if ((dep->de_Attributes & ATTR_READONLY0x01) == 0)
215 dosmode |= (S_IWUSR0000200 | S_IWGRP0000020 | S_IWOTH0000002);
216 if (dep->de_Attributes & ATTR_DIRECTORY0x10)
217 dosmode |= (S_IXUSR0000100 | S_IXGRP0000010 | S_IXOTH0000001);
218 dosmode &= pmp->pm_mask;
219
220 return (vaccess(ap->a_vp->v_type, dosmode, pmp->pm_uid, pmp->pm_gid,
221 ap->a_mode, ap->a_cred));
222}
223
224int
225msdosfs_getattr(void *v)
226{
227 struct vop_getattr_args *ap = v;
228 struct denode *dep = VTODE(ap->a_vp)((struct denode *)(ap->a_vp)->v_data);
229 struct msdosfsmount *pmp = dep->de_pmp;
230 struct vattr *vap = ap->a_vap;
231 struct timespec ts;
232 uint32_t fileid;
233
234 getnanotime(&ts);
235 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); }
;
236 vap->va_fsid = dep->de_dev;
237
238 /*
239 * The following computation of the fileid must be the same as
240 * that used in msdosfs_readdir() to compute d_fileno. If not,
241 * pwd doesn't work.
242 *
243 * We now use the starting cluster number as the fileid/fileno.
244 * This works for both files and directories (including the root
245 * directory, on FAT32). Even on FAT32, this will at most be a
246 * 28-bit number, as the high 4 bits of FAT32 cluster numbers
247 * are reserved.
248 *
249 * However, we do need to do something for 0-length files, which
250 * will not have a starting cluster number.
251 *
252 * These files cannot be directories, since (except for /, which
253 * is special-cased anyway) directories contain entries for . and
254 * .., so must have non-zero length.
255 *
256 * In this case, we just create a non-cryptographic hash of the
257 * original fileid calculation, and set the top bit.
258 *
259 * This algorithm has the benefit that all directories, and all
260 * non-zero-length files, will have fileids that are persistent
261 * across mounts and reboots, and that cannot collide (as long
262 * as the filesystem is not corrupt). Zero-length files will
263 * have fileids that are persistent, but that may collide. We
264 * will just have to live with that.
265 */
266 fileid = dep->de_StartCluster;
267
268 if (dep->de_Attributes & ATTR_DIRECTORY0x10) {
269 /* Special-case root */
270 if (dep->de_StartCluster == MSDOSFSROOT0)
271 fileid = FAT32(pmp)(pmp->pm_fatmask == 0x0fffffff) ? pmp->pm_rootdirblk : 1;
272 } else {
273 if (dep->de_FileSize == 0) {
274 uint32_t dirsperblk;
275 uint64_t fileid64;
276
277 dirsperblk = pmp->pm_BytesPerSecpm_bpb.bpbBytesPerSec /
278 sizeof(struct direntry);
279
280 fileid64 = (dep->de_dirclust == MSDOSFSROOT0) ?
281 roottobn(pmp, 0)((((((((0))) >> (((pmp)))->pm_cnshift)) << (((
pmp))->pm_cnshift - ((pmp))->pm_bnshift))) + (pmp)->
pm_rootdirblk)
: cntobn(pmp, dep->de_dirclust)((((dep->de_dirclust)-2) << (((pmp))->pm_cnshift -
((pmp))->pm_bnshift)) + (pmp)->pm_firstcluster)
;
282 fileid64 *= dirsperblk;
283 fileid64 += dep->de_diroffset / sizeof(struct direntry);
284
285 fileid = fileidhash(fileid64);
286 }
287 }
288
289 vap->va_fileid = fileid;
290 vap->va_mode = (S_IRUSR0000400|S_IRGRP0000040|S_IROTH0000004);
291 if ((dep->de_Attributes & ATTR_READONLY0x01) == 0)
292 vap->va_mode |= (S_IWUSR0000200|S_IWGRP0000020|S_IWOTH0000002);
293 if (dep->de_Attributes & ATTR_DIRECTORY0x10) {
294 vap->va_mode |= S_IFDIR0040000;
295 vap->va_mode |= (vap->va_mode & S_IRUSR0000400) ? S_IXUSR0000100 : 0;
296 vap->va_mode |= (vap->va_mode & S_IRGRP0000040) ? S_IXGRP0000010 : 0;
297 vap->va_mode |= (vap->va_mode & S_IROTH0000004) ? S_IXOTH0000001 : 0;
298 }
299 vap->va_mode &= dep->de_pmp->pm_mask;
300 vap->va_nlink = 1;
301 vap->va_gid = dep->de_pmp->pm_gid;
302 vap->va_uid = dep->de_pmp->pm_uid;
303 vap->va_rdev = 0;
304 vap->va_size = dep->de_FileSize;
305 dos2unixtime(dep->de_MDate, dep->de_MTime, 0, &vap->va_mtime);
306 if (dep->de_pmp->pm_flags & MSDOSFSMNT_LONGNAME0x02) {
307 dos2unixtime(dep->de_ADate, 0, 0, &vap->va_atime);
308 dos2unixtime(dep->de_CDate, dep->de_CTime, dep->de_CTimeHundredth, &vap->va_ctime);
309 } else {
310 vap->va_atime = vap->va_mtime;
311 vap->va_ctime = vap->va_mtime;
312 }
313 vap->va_flags = 0;
314 if ((dep->de_Attributes & ATTR_ARCHIVE0x20) == 0)
315 vap->va_flags |= SF_ARCHIVED0x00010000;
316 vap->va_gen = 0;
317 vap->va_blocksize = dep->de_pmp->pm_bpcluster;
318 vap->va_bytes = (dep->de_FileSize + dep->de_pmp->pm_crbomask) &
319 ~(dep->de_pmp->pm_crbomask);
320 vap->va_type = ap->a_vp->v_type;
321 return (0);
322}
323
324int
325msdosfs_setattr(void *v)
326{
327 struct vop_setattr_args *ap = v;
328 struct vnode *vp = ap->a_vp;
329 struct denode *dep = VTODE(ap->a_vp)((struct denode *)(ap->a_vp)->v_data);
330 struct msdosfsmount *pmp = dep->de_pmp;
331 struct vattr *vap = ap->a_vap;
332 struct ucred *cred = ap->a_cred;
333 int error = 0;
334
335#ifdef MSDOSFS_DEBUG
336 printf("msdosfs_setattr(): vp %p, vap %p, cred %p, p %p\n",
337 ap->a_vp, vap, cred, ap->a_p);
338#endif
339 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL(-1)) ||
340 (vap->va_fsid != VNOVAL(-1)) || (vap->va_fileid != VNOVAL(-1)) ||
341 (vap->va_blocksize != VNOVAL(-1)) || (vap->va_rdev != VNOVAL(-1)) ||
342 (vap->va_bytes != VNOVAL(-1)) || (vap->va_gen != VNOVAL(-1))) {
343#ifdef MSDOSFS_DEBUG
344 printf("msdosfs_setattr(): returning EINVAL\n");
345 printf(" va_type %d, va_nlink %x, va_fsid %ld, "
346 "va_fileid %llx\n", vap->va_type, vap->va_nlink,
347 vap->va_fsid, (unsigned long long)vap->va_fileid);
348 printf(" va_blocksize %lx, va_rdev %x, va_bytes %llx, "
349 "va_gen %lx\n", vap->va_blocksize,
350 (unsigned int)vap->va_rdev,
351 (unsigned long long)vap->va_bytes, vap->va_gen);
352#endif
353 return (EINVAL22);
354 }
355 if (vap->va_flags != VNOVAL(-1)) {
356 if (vp->v_mount->mnt_flag & MNT_RDONLY0x00000001)
357 return (EINVAL22);
358 if (cred->cr_uid != pmp->pm_uid) {
359 error = suser_ucred(cred);
360 if (error)
361 return (error);
362 }
363 /*
364 * We are very inconsistent about handling unsupported
365 * attributes. We ignored the access time and the
366 * read and execute bits. We were strict for the other
367 * attributes.
368 *
369 * Here we are strict, stricter than ufs in not allowing
370 * users to attempt to set SF_SETTABLE bits or anyone to
371 * set unsupported bits. However, we ignore attempts to
372 * set ATTR_ARCHIVE for directories `cp -pr' from a more
373 * sensible filesystem attempts it a lot.
374 */
375 if (vap->va_flags & SF_SETTABLE0xffff0000) {
376 error = suser_ucred(cred);
377 if (error)
378 return (error);
379 }
380 if (vap->va_flags & ~SF_ARCHIVED0x00010000)
381 return EOPNOTSUPP45;
382 if (vap->va_flags & SF_ARCHIVED0x00010000)
383 dep->de_Attributes &= ~ATTR_ARCHIVE0x20;
384 else if (!(dep->de_Attributes & ATTR_DIRECTORY0x10))
385 dep->de_Attributes |= ATTR_ARCHIVE0x20;
386 dep->de_flag |= DE_MODIFIED0x0020;
387 }
388
389 if (vap->va_uid != (uid_t)VNOVAL(-1) || vap->va_gid != (gid_t)VNOVAL(-1)) {
390 uid_t uid;
391 gid_t gid;
392
393 if (vp->v_mount->mnt_flag & MNT_RDONLY0x00000001)
394 return (EINVAL22);
395 uid = vap->va_uid;
396 if (uid == (uid_t)VNOVAL(-1))
397 uid = pmp->pm_uid;
398 gid = vap->va_gid;
399 if (gid == (gid_t)VNOVAL(-1))
400 gid = pmp->pm_gid;
401 if (cred->cr_uid != pmp->pm_uid || uid != pmp->pm_uid ||
402 (gid != pmp->pm_gid && !groupmember(gid, cred))) {
403 error = suser_ucred(cred);
404 if (error)
405 return (error);
406 }
407 if (uid != pmp->pm_uid || gid != pmp->pm_gid)
408 return EINVAL22;
409 }
410
411 if (vap->va_size != VNOVAL(-1)) {
412 switch (vp->v_type) {
413 case VDIR:
414 return (EISDIR21);
415 case VREG:
416 /*
417 * Truncation is only supported for regular files,
418 * Disallow it if the filesystem is read-only.
419 */
420 if (vp->v_mount->mnt_flag & MNT_RDONLY0x00000001)
421 return (EINVAL22);
422 break;
423 default:
424 /*
425 * According to POSIX, the result is unspecified
426 * for file types other than regular files,
427 * directories and shared memory objects. We
428 * don't support any file types except regular
429 * files and directories in this file system, so
430 * this (default) case is unreachable and can do
431 * anything. Keep falling through to detrunc()
432 * for now.
433 */
434 break;
435 }
436 error = detrunc(dep, vap->va_size, 0, cred, ap->a_p);
437 if (error)
438 return error;
439 }
440 if ((vap->va_vaflags & VA_UTIMES_CHANGE0x04) ||
441 vap->va_atime.tv_nsec != VNOVAL(-1) ||
442 vap->va_mtime.tv_nsec != VNOVAL(-1)) {
443 if (vp->v_mount->mnt_flag & MNT_RDONLY0x00000001)
444 return (EINVAL22);
445 if (cred->cr_uid != pmp->pm_uid &&
446 (error = suser_ucred(cred)) &&
447 ((vap->va_vaflags & VA_UTIMES_NULL0x01) == 0 ||
448 (error = VOP_ACCESS(ap->a_vp, VWRITE00200, cred, ap->a_p))))
449 return (error);
450 if (vp->v_type != VDIR) {
451 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN950x04) == 0 &&
452 vap->va_atime.tv_nsec != VNOVAL(-1)) {
453 dep->de_flag &= ~DE_ACCESS0x0010;
454 unix2dostime(&vap->va_atime, &dep->de_ADate,
455 NULL((void *)0), NULL((void *)0));
456 }
457 if (vap->va_mtime.tv_nsec != VNOVAL(-1)) {
458 dep->de_flag &= ~DE_UPDATE0x0004;
459 unix2dostime(&vap->va_mtime, &dep->de_MDate,
460 &dep->de_MTime, NULL((void *)0));
461 }
462 dep->de_Attributes |= ATTR_ARCHIVE0x20;
463 dep->de_flag |= DE_MODIFIED0x0020;
464 }
465 }
466 /*
467 * DOS files only have the ability to have their writability
468 * attribute set, so we use the owner write bit to set the readonly
469 * attribute.
470 */
471 if (vap->va_mode != (mode_t)VNOVAL(-1)) {
472 if (vp->v_mount->mnt_flag & MNT_RDONLY0x00000001)
473 return (EINVAL22);
474 if (cred->cr_uid != pmp->pm_uid) {
475 error = suser_ucred(cred);
476 if (error)
477 return (error);
478 }
479 if (vp->v_type != VDIR) {
480 /* We ignore the read and execute bits. */
481 if (vap->va_mode & VWRITE00200)
482 dep->de_Attributes &= ~ATTR_READONLY0x01;
483 else
484 dep->de_Attributes |= ATTR_READONLY0x01;
485 dep->de_Attributes |= ATTR_ARCHIVE0x20;
486 dep->de_flag |= DE_MODIFIED0x0020;
487 }
488 }
489 VN_KNOTE(ap->a_vp, NOTE_ATTRIB)knote_locked(&ap->a_vp->v_klist, (0x0008));
490 return (deupdat(dep, 1));
491}
492
493int
494msdosfs_read(void *v)
495{
496 struct vop_read_args *ap = v;
497 struct vnode *vp = ap->a_vp;
498 struct denode *dep = VTODE(vp)((struct denode *)(vp)->v_data);
499 struct msdosfsmount *pmp = dep->de_pmp;
500 struct uio *uio = ap->a_uio;
501 int isadir, error = 0;
502 uint32_t n, diff, size, on;
503 struct buf *bp;
504 uint32_t cn;
505 daddr_t bn;
506
507 /*
508 * If they didn't ask for any data, then we are done.
509 */
510 if (uio->uio_resid == 0)
511 return (0);
512 if (uio->uio_offset < 0)
513 return (EINVAL22);
514
515 isadir = dep->de_Attributes & ATTR_DIRECTORY0x10;
516 do {
517 if (uio->uio_offset >= dep->de_FileSize)
518 return (0);
519
520 cn = de_cluster(pmp, uio->uio_offset)((uio->uio_offset) >> (pmp)->pm_cnshift);
521 size = pmp->pm_bpcluster;
522 on = uio->uio_offset & pmp->pm_crbomask;
523 n = ulmin(pmp->pm_bpcluster - on, uio->uio_resid);
524
525 /*
526 * de_FileSize is uint32_t, and we know that uio_offset <
527 * de_FileSize, so uio->uio_offset < 2^32. Therefore
528 * the cast to uint32_t on the next line is safe.
529 */
530 diff = dep->de_FileSize - (uint32_t)uio->uio_offset;
531 if (diff < n)
532 n = diff;
533
534 /*
535 * If we are operating on a directory file then be sure to
536 * do i/o with the vnode for the filesystem instead of the
537 * vnode for the directory.
538 */
539 if (isadir) {
540 /* convert cluster # to block # */
541 error = pcbmap(dep, cn, &bn, 0, &size);
542 if (error)
543 return (error);
544 error = bread(pmp->pm_devvp, bn, size, &bp);
545 } else {
546 if (de_cn2off(pmp, cn + 1)((cn + 1) << (pmp)->pm_cnshift) >= dep->de_FileSize)
547 error = bread(vp, cn, size, &bp);
548 else
549 error = bread_cluster(vp, cn, size, &bp);
550 }
551 n = min(n, pmp->pm_bpcluster - bp->b_resid);
552 if (error) {
553 brelse(bp);
554 return (error);
555 }
556 error = uiomove(bp->b_data + on, n, uio);
557 brelse(bp);
558 } while (error == 0 && uio->uio_resid > 0 && n != 0);
559 if (!isadir && !(vp->v_mount->mnt_flag & MNT_NOATIME0x00008000))
560 dep->de_flag |= DE_ACCESS0x0010;
561 return (error);
562}
563
564/*
565 * Write data to a file or directory.
566 */
567int
568msdosfs_write(void *v)
569{
570 struct vop_write_args *ap = v;
571 uint32_t n, croffset;
572 size_t resid;
573 ssize_t overrun;
574 int extended = 0;
575 uint32_t osize;
576 int error = 0;
577 uint32_t count, lastcn, cn;
578 struct buf *bp;
579 int ioflag = ap->a_ioflag;
580 struct uio *uio = ap->a_uio;
581 struct vnode *vp = ap->a_vp;
582 struct vnode *thisvp;
583 struct denode *dep = VTODE(vp)((struct denode *)(vp)->v_data);
584 struct msdosfsmount *pmp = dep->de_pmp;
585 struct ucred *cred = ap->a_cred;
586
587#ifdef MSDOSFS_DEBUG
588 printf("msdosfs_write(vp %p, uio %p, ioflag %08x, cred %p\n",
589 vp, uio, ioflag, cred);
590 printf("msdosfs_write(): diroff %d, dirclust %d, startcluster %d\n",
591 dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
592#endif
593
594 switch (vp->v_type) {
595 case VREG:
596 if (ioflag & IO_APPEND0x02)
597 uio->uio_offset = dep->de_FileSize;
598 thisvp = vp;
599 break;
600 case VDIR:
601 return EISDIR21;
602 default:
603 panic("msdosfs_write(): bad file type");
604 }
605
606 if (uio->uio_offset < 0)
607 return (EINVAL22);
608
609 if (uio->uio_resid == 0)
610 return (0);
611
612 /* Don't bother to try to write files larger than the f/s limit */
613 if (uio->uio_offset > MSDOSFS_FILESIZE_MAX0xFFFFFFFFLL ||
614 uio->uio_resid > (MSDOSFS_FILESIZE_MAX0xFFFFFFFFLL - uio->uio_offset))
615 return (EFBIG27);
616
617 /* do the filesize rlimit check */
618 if ((error = vn_fsizechk(vp, uio, ioflag, &overrun)))
619 return (error);
620
621 /*
622 * If the offset we are starting the write at is beyond the end of
623 * the file, then they've done a seek. Unix filesystems allow
624 * files with holes in them, DOS doesn't so we must fill the hole
625 * with zeroed blocks.
626 */
627 if (uio->uio_offset > dep->de_FileSize) {
628 if ((error = deextend(dep, uio->uio_offset, cred)) != 0)
629 goto out;
630 }
631
632 /*
633 * Remember some values in case the write fails.
634 */
635 resid = uio->uio_resid;
636 osize = dep->de_FileSize;
637
638 /*
639 * If we write beyond the end of the file, extend it to its ultimate
640 * size ahead of the time to hopefully get a contiguous area.
641 */
642 if (uio->uio_offset + resid > osize) {
643 extended = 1;
644 count = de_clcount(pmp, uio->uio_offset + resid)(((uio->uio_offset + resid) + (pmp)->pm_bpcluster - 1) >>
(pmp)->pm_cnshift)
-
645 de_clcount(pmp, osize)(((osize) + (pmp)->pm_bpcluster - 1) >> (pmp)->pm_cnshift
)
;
646 if ((error = extendfile(dep, count, NULL((void *)0), NULL((void *)0), 0)) &&
647 (error != ENOSPC28 || (ioflag & IO_UNIT0x01)))
648 goto errexit;
649 lastcn = dep->de_fc[FC_LASTFC1].fc_frcn;
650 } else
651 lastcn = de_clcount(pmp, osize)(((osize) + (pmp)->pm_bpcluster - 1) >> (pmp)->pm_cnshift
)
- 1;
652
653 do {
654 croffset = uio->uio_offset & pmp->pm_crbomask;
655 cn = de_cluster(pmp, uio->uio_offset)((uio->uio_offset) >> (pmp)->pm_cnshift);
656
657 if (cn > lastcn) {
658 error = ENOSPC28;
659 break;
660 }
661
662 if (croffset == 0 &&
663 (de_cluster(pmp, uio->uio_offset + uio->uio_resid)((uio->uio_offset + uio->uio_resid) >> (pmp)->
pm_cnshift)
> cn ||
664 (uio->uio_offset + uio->uio_resid) >= dep->de_FileSize)) {
665 /*
666 * If either the whole cluster gets written,
667 * or we write the cluster from its start beyond EOF,
668 * then no need to read data from disk.
669 */
670 bp = getblk(thisvp, cn, pmp->pm_bpcluster, 0, INFSLP0xffffffffffffffffULL);
671 clrbuf(bp){ __builtin_bzero(((bp)->b_data), ((bp)->b_bcount)); (bp
)->b_resid = 0; }
;
672 /*
673 * Do the bmap now, since pcbmap needs buffers
674 * for the fat table. (see msdosfs_strategy)
675 */
676 if (bp->b_blkno == bp->b_lblkno) {
677 error = pcbmap(dep, bp->b_lblkno, &bp->b_blkno, 0, 0);
678 if (error)
679 bp->b_blkno = -1;
680 }
681 if (bp->b_blkno == -1) {
682 brelse(bp);
683 if (!error)
684 error = EIO5; /* XXX */
685 break;
686 }
687 } else {
688 /*
689 * The block we need to write into exists, so
690 * read it in.
691 */
692 error = bread(thisvp, cn, pmp->pm_bpcluster, &bp);
693 if (error) {
694 brelse(bp);
695 break;
696 }
697 }
698
699 n = ulmin(uio->uio_resid, pmp->pm_bpcluster - croffset);
700 if (uio->uio_offset + n > dep->de_FileSize) {
701 dep->de_FileSize = uio->uio_offset + n;
702 uvm_vnp_setsize(vp, dep->de_FileSize);
703 }
704 uvm_vnp_uncache(vp);
705 /*
706 * Should these vnode_pager_* functions be done on dir
707 * files?
708 */
709
710 /*
711 * Copy the data from user space into the buf header.
712 */
713 error = uiomove(bp->b_data + croffset, n, uio);
714
715 /*
716 * If they want this synchronous then write it and wait for
717 * it. Otherwise, if on a cluster boundary write it
718 * asynchronously so we can move on to the next block
719 * without delay. Otherwise do a delayed write because we
720 * may want to write somemore into the block later.
721 */
722#if 0
723 if (ioflag & IO_NOCACHE0x40)
724 bp->b_flags |= B_NOCACHE0x00001000;
725#endif
726 if (ioflag & IO_SYNC0x04)
727 (void) bwrite(bp);
728 else if (n + croffset == pmp->pm_bpcluster)
729 bawrite(bp);
730 else
731 bdwrite(bp);
732 dep->de_flag |= DE_UPDATE0x0004;
733 } while (error == 0 && uio->uio_resid > 0);
734
735 if (resid > uio->uio_resid)
736 VN_KNOTE(ap->a_vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0))knote_locked(&ap->a_vp->v_klist, (0x0002 | (extended
? 0x0004 : 0)))
;
737
738 if (dep->de_FileSize < osize)
739 VN_KNOTE(ap->a_vp, NOTE_TRUNCATE)knote_locked(&ap->a_vp->v_klist, (0x0080));
740
741 /*
742 * If the write failed and they want us to, truncate the file back
743 * to the size it was before the write was attempted.
744 */
745errexit:
746 if (error) {
747 if (ioflag & IO_UNIT0x01) {
748 detrunc(dep, osize, ioflag & IO_SYNC0x04, NOCRED((struct ucred *)-1), curproc({struct cpu_info *__ci; asm volatile("movq %%gs:%P1,%0" : "=r"
(__ci) :"n" (__builtin_offsetof(struct cpu_info, ci_self)));
__ci;})->ci_curproc
);
749 uio->uio_offset -= resid - uio->uio_resid;
750 uio->uio_resid = resid;
751 } else {
752 detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC0x04, NOCRED((struct ucred *)-1), curproc({struct cpu_info *__ci; asm volatile("movq %%gs:%P1,%0" : "=r"
(__ci) :"n" (__builtin_offsetof(struct cpu_info, ci_self)));
__ci;})->ci_curproc
);
753 if (uio->uio_resid != resid)
754 error = 0;
755 }
756 } else if (ioflag & IO_SYNC0x04)
757 error = deupdat(dep, 1);
758
759out:
760 /* correct the result for writes clamped by vn_fsizechk() */
761 uio->uio_resid += overrun;
762 return (error);
763}
764
765int
766msdosfs_ioctl(void *v)
767{
768 return (ENOTTY25);
769}
770
771/*
772 * Flush the blocks of a file to disk.
773 *
774 * This function is worthless for vnodes that represent directories. Maybe we
775 * could just do a sync if they try an fsync on a directory file.
776 */
777int
778msdosfs_fsync(void *v)
779{
780 struct vop_fsync_args *ap = v;
781 struct vnode *vp = ap->a_vp;
782
783 vflushbuf(vp, ap->a_waitfor == MNT_WAIT1);
784 return (deupdat(VTODE(vp)((struct denode *)(vp)->v_data), ap->a_waitfor == MNT_WAIT1));
785}
786
787/*
788 * Flush the blocks of a file to disk.
789 *
790 * This function is worthless for vnodes that represent directories. Maybe we
791 * could just do a sync if they try an fsync on a directory file.
792 */
793int
794msdosfs_remove(void *v)
795{
796 struct vop_remove_args *ap = v;
797 struct denode *dep = VTODE(ap->a_vp)((struct denode *)(ap->a_vp)->v_data);
798 struct denode *ddep = VTODE(ap->a_dvp)((struct denode *)(ap->a_dvp)->v_data);
799 int error;
800
801 if (ap->a_vp->v_type == VDIR)
802 error = EPERM1;
803 else
804 error = removede(ddep, dep);
805
806 VN_KNOTE(ap->a_vp, NOTE_DELETE)knote_locked(&ap->a_vp->v_klist, (0x0001));
807 VN_KNOTE(ap->a_dvp, NOTE_WRITE)knote_locked(&ap->a_dvp->v_klist, (0x0002));
808
809#ifdef MSDOSFS_DEBUG
810 printf("msdosfs_remove(), dep %p, v_usecount %d\n", dep,
811 ap->a_vp->v_usecount);
812#endif
813 if (ddep == dep)
814 vrele(ap->a_vp);
815 else
816 vput(ap->a_vp); /* causes msdosfs_inactive() to be called
817 * via vrele() */
818 vput(ap->a_dvp);
819 return (error);
820}
821
822/*
823 * DOS filesystems don't know what links are. But since we already called
824 * msdosfs_lookup() with create and lockparent, the parent is locked so we
825 * have to free it before we return the error.
826 */
827int
828msdosfs_link(void *v)
829{
830 struct vop_link_args *ap = v;
831
832 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
833 vput(ap->a_dvp);
834 return (EOPNOTSUPP45);
835}
836
837/*
838 * Renames on files require moving the denode to a new hash queue since the
839 * denode's location is used to compute which hash queue to put the file
840 * in. Unless it is a rename in place. For example "mv a b".
841 *
842 * What follows is the basic algorithm:
843 *
844 * if (file move) {
845 * if (dest file exists) {
846 * remove dest file
847 * }
848 * if (dest and src in same directory) {
849 * rewrite name in existing directory slot
850 * } else {
851 * write new entry in dest directory
852 * update offset and dirclust in denode
853 * move denode to new hash chain
854 * clear old directory entry
855 * }
856 * } else {
857 * directory move
858 * if (dest directory exists) {
859 * if (dest is not empty) {
860 * return ENOTEMPTY
861 * }
862 * remove dest directory
863 * }
864 * if (dest and src in same directory) {
865 * rewrite name in existing entry
866 * } else {
867 * be sure dest is not a child of src directory
868 * write entry in dest directory
869 * update "." and ".." in moved directory
870 * update offset and dirclust in denode
871 * move denode to new hash chain
872 * clear old directory entry for moved directory
873 * }
874 * }
875 *
876 * On entry:
877 * source's parent directory is unlocked
878 * source file or directory is unlocked
879 * destination's parent directory is locked
880 * destination file or directory is locked if it exists
881 *
882 * On exit:
883 * all denodes should be released
884 *
885 * Notes:
886 * I'm not sure how the memory containing the pathnames pointed at by the
887 * componentname structures is freed, there may be some memory bleeding
888 * for each rename done.
889 */
890int
891msdosfs_rename(void *v)
892{
893 struct vop_rename_args *ap = v;
894 struct vnode *tvp = ap->a_tvp;
895 struct vnode *tdvp = ap->a_tdvp;
896 struct vnode *fvp = ap->a_fvp;
897 struct vnode *fdvp = ap->a_fdvp;
898 struct componentname *tcnp = ap->a_tcnp;
899 struct componentname *fcnp = ap->a_fcnp;
900 struct denode *ip, *xp, *dp, *zp;
901 u_char toname[11], oldname[11];
902 uint32_t from_diroffset, to_diroffset;
903 u_char to_count;
904 int doingdirectory = 0, newparent = 0;
905 int error;
906 uint32_t cn, pcl;
907 daddr_t bn;
908 struct msdosfsmount *pmp;
909 struct direntry *dotdotp;
910 struct buf *bp;
911
912 pmp = VFSTOMSDOSFS(fdvp->v_mount)((struct msdosfsmount *)fdvp->v_mount->mnt_data);
913
914#ifdef DIAGNOSTIC1
915 if ((tcnp->cn_flags & HASBUF0x000400) == 0 ||
916 (fcnp->cn_flags & HASBUF0x000400) == 0)
917 panic("msdosfs_rename: no name");
918#endif
919 /*
920 * Check for cross-device rename.
921 */
922 if ((fvp->v_mount != tdvp->v_mount) ||
923 (tvp && (fvp->v_mount != tvp->v_mount))) {
924 error = EXDEV18;
925abortit:
926 VOP_ABORTOP(tdvp, tcnp);
927 if (tdvp == tvp)
928 vrele(tdvp);
929 else
930 vput(tdvp);
931 if (tvp)
932 vput(tvp);
933 VOP_ABORTOP(fdvp, fcnp);
934 vrele(fdvp);
935 vrele(fvp);
936 return (error);
937 }
938
939 /*
940 * If source and dest are the same, do nothing.
941 */
942 if (tvp == fvp) {
943 error = 0;
944 goto abortit;
945 }
946
947 /* */
948 if ((error = vn_lock(fvp, LK_EXCLUSIVE0x0001UL | LK_RETRY0x2000UL)) != 0)
949 goto abortit;
950 dp = VTODE(fdvp)((struct denode *)(fdvp)->v_data);
951 ip = VTODE(fvp)((struct denode *)(fvp)->v_data);
952
953 /*
954 * Be sure we are not renaming ".", "..", or an alias of ".". This
955 * leads to a crippled directory tree. It's pretty tough to do a
956 * "ls" or "pwd" with the "." directory entry missing, and "cd .."
957 * doesn't work if the ".." entry is missing.
958 */
959 if (ip->de_Attributes & ATTR_DIRECTORY0x10) {
960 /*
961 * Avoid ".", "..", and aliases of "." for obvious reasons.
962 */
963 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
964 dp == ip ||
965 (fcnp->cn_flags & ISDOTDOT0x002000) ||
966 (tcnp->cn_flags & ISDOTDOT0x002000) ||
967 (ip->de_flag & DE_RENAME0x0040)) {
968 VOP_UNLOCK(fvp);
969 error = EINVAL22;
970 goto abortit;
971 }
972 ip->de_flag |= DE_RENAME0x0040;
973 doingdirectory++;
974 }
975 VN_KNOTE(fdvp, NOTE_WRITE)knote_locked(&fdvp->v_klist, (0x0002)); /* XXX right place? */
976
977 /*
978 * When the target exists, both the directory
979 * and target vnodes are returned locked.
980 */
981 dp = VTODE(tdvp)((struct denode *)(tdvp)->v_data);
982 xp = tvp ? VTODE(tvp)((struct denode *)(tvp)->v_data) : NULL((void *)0);
983 /*
984 * Remember direntry place to use for destination
985 */
986 to_diroffset = dp->de_fndoffset;
987 to_count = dp->de_fndcnt;
988
989 /*
990 * If ".." must be changed (ie the directory gets a new
991 * parent) then the source directory must not be in the
992 * directory hierarchy above the target, as this would
993 * orphan everything below the source directory. Also
994 * the user must have write permission in the source so
995 * as to be able to change "..". We must repeat the call
996 * to namei, as the parent directory is unlocked by the
997 * call to doscheckpath().
998 */
999 error = VOP_ACCESS(fvp, VWRITE00200, tcnp->cn_cred, tcnp->cn_proc);
1000 VOP_UNLOCK(fvp);
1001 if (VTODE(fdvp)((struct denode *)(fdvp)->v_data)->de_StartCluster != VTODE(tdvp)((struct denode *)(tdvp)->v_data)->de_StartCluster)
1002 newparent = 1;
1003 vrele(fdvp);
1004 if (doingdirectory && newparent) {
1005 if (error) /* write access check above */
1006 goto bad1;
1007 if (xp != NULL((void *)0))
1008 vput(tvp);
1009 /*
1010 * doscheckpath() vput()'s dp,
1011 * so we have to do a relookup afterwards
1012 */
1013 if ((error = doscheckpath(ip, dp)) != 0)
1014 goto out;
1015 if ((tcnp->cn_flags & SAVESTART0x001000) == 0)
1016 panic("msdosfs_rename: lost to startdir");
1017 if ((error = vfs_relookup(tdvp, &tvp, tcnp)) != 0)
1018 goto out;
1019 dp = VTODE(tdvp)((struct denode *)(tdvp)->v_data);
1020 xp = tvp ? VTODE(tvp)((struct denode *)(tvp)->v_data) : NULL((void *)0);
1021 }
1022
1023 VN_KNOTE(tdvp, NOTE_WRITE)knote_locked(&tdvp->v_klist, (0x0002));
1024
1025 if (xp != NULL((void *)0)) {
1026 /*
1027 * Target must be empty if a directory and have no links
1028 * to it. Also, ensure source and target are compatible
1029 * (both directories, or both not directories).
1030 */
1031 if (xp->de_Attributes & ATTR_DIRECTORY0x10) {
1032 if (!dosdirempty(xp)) {
1033 error = ENOTEMPTY66;
1034 goto bad1;
1035 }
1036 if (!doingdirectory) {
1037 error = ENOTDIR20;
1038 goto bad1;
1039 }
1040 cache_purge(tdvp);
1041 } else if (doingdirectory) {
1042 error = EISDIR21;
1043 goto bad1;
1044 }
1045 if ((error = removede(dp, xp)) != 0)
1046 goto bad1;
1047 VN_KNOTE(tvp, NOTE_DELETE)knote_locked(&tvp->v_klist, (0x0001));
1048 vput(tvp);
1049 xp = NULL((void *)0);
1050 }
1051
1052 /*
1053 * Convert the filename in tcnp into a dos filename. We copy this
1054 * into the denode and directory entry for the destination
1055 * file/directory.
1056 */
1057 if ((error = uniqdosname(VTODE(tdvp)((struct denode *)(tdvp)->v_data), tcnp, toname)) != 0)
1058 goto bad1;
1059
1060 /*
1061 * Since from wasn't locked at various places above,
1062 * have to do a relookup here.
1063 */
1064 fcnp->cn_flags &= ~MODMASK0x00fc;
1065 fcnp->cn_flags |= LOCKPARENT0x0008 | LOCKLEAF0x0004;
1066 if ((fcnp->cn_flags & SAVESTART0x001000) == 0)
1067 panic("msdosfs_rename: lost from startdir");
1068 if (!newparent)
1069 VOP_UNLOCK(tdvp);
1070 (void) vfs_relookup(fdvp, &fvp, fcnp);
1071 if (fvp == NULL((void *)0)) {
1072 /*
1073 * From name has disappeared.
1074 */
1075 if (doingdirectory)
1076 panic("rename: lost dir entry");
1077 vrele(ap->a_fvp);
1078 if (newparent)
1079 VOP_UNLOCK(tdvp);
1080 vrele(tdvp);
1081 return 0;
1082 }
1083 xp = VTODE(fvp)((struct denode *)(fvp)->v_data);
1084 zp = VTODE(fdvp)((struct denode *)(fdvp)->v_data);
1085 from_diroffset = zp->de_fndoffset;
1086
1087 /*
1088 * Ensure that the directory entry still exists and has not
1089 * changed till now. If the source is a file the entry may
1090 * have been unlinked or renamed. In either case there is
1091 * no further work to be done. If the source is a directory
1092 * then it cannot have been rmdir'ed or renamed; this is
1093 * prohibited by the DE_RENAME flag.
1094 */
1095 if (xp != ip) {
1096 if (doingdirectory)
1097 panic("rename: lost dir entry");
1098 vrele(ap->a_fvp);
1099 if (newparent)
1100 VOP_UNLOCK(fdvp);
1101 xp = NULL((void *)0);
1102 } else {
1103 vrele(fvp);
1104 xp = NULL((void *)0);
1105
1106 /*
1107 * First write a new entry in the destination
1108 * directory and mark the entry in the source directory
1109 * as deleted. Then move the denode to the correct hash
1110 * chain for its new location in the filesystem. And, if
1111 * we moved a directory, then update its .. entry to point
1112 * to the new parent directory.
1113 */
1114 bcopy(ip->de_Name, oldname, 11);
1115 bcopy(toname, ip->de_Name, 11); /* update denode */
1116 dp->de_fndoffset = to_diroffset;
1117 dp->de_fndcnt = to_count;
1118 error = createde(ip, dp, NULL((void *)0), tcnp);
1119 if (error) {
1120 bcopy(oldname, ip->de_Name, 11);
1121 if (newparent)
1122 VOP_UNLOCK(fdvp);
1123 goto bad;
1124 }
1125 ip->de_refcnt++;
1126 zp->de_fndoffset = from_diroffset;
1127 if ((error = removede(zp, ip)) != 0) {
1128 /* XXX should really panic here, fs is corrupt */
1129 if (newparent)
1130 VOP_UNLOCK(fdvp);
1131 goto bad;
1132 }
1133
1134 cache_purge(fvp);
1135
1136 if (!doingdirectory) {
1137 error = pcbmap(dp, de_cluster(pmp, to_diroffset)((to_diroffset) >> (pmp)->pm_cnshift), 0,
1138 &ip->de_dirclust, 0);
1139 if (error) {
1140 /* XXX should really panic here, fs is corrupt */
1141 if (newparent)
1142 VOP_UNLOCK(fdvp);
1143 goto bad;
1144 }
1145 ip->de_diroffset = to_diroffset;
1146 if (ip->de_dirclust != MSDOSFSROOT0)
1147 ip->de_diroffset &= pmp->pm_crbomask;
1148 }
1149 reinsert(ip);
1150 if (newparent)
1151 VOP_UNLOCK(fdvp);
1152 }
1153
1154 /*
1155 * If we moved a directory to a new parent directory, then we must
1156 * fixup the ".." entry in the moved directory.
1157 */
1158 if (doingdirectory && newparent) {
1159 cn = ip->de_StartCluster;
1160 if (cn == MSDOSFSROOT0) {
1161 /* this should never happen */
1162 panic("msdosfs_rename: updating .. in root directory?");
1163 } else
1164 bn = cntobn(pmp, cn)((((cn)-2) << (((pmp))->pm_cnshift - ((pmp))->pm_bnshift
)) + (pmp)->pm_firstcluster)
;
1165 error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster, &bp);
1166 if (error) {
1167 /* XXX should really panic here, fs is corrupt */
1168 brelse(bp);
1169 goto bad;
1170 }
1171 dotdotp = (struct direntry *)bp->b_data;
1172 putushort(dotdotp[0].deStartCluster, cn)(*((u_int16_t *)(dotdotp[0].deStartCluster)) = (cn));
1173 pcl = dp->de_StartCluster;
1174 if (FAT32(pmp)(pmp->pm_fatmask == 0x0fffffff) && pcl == pmp->pm_rootdirblk)
1175 pcl = 0;
1176 putushort(dotdotp[1].deStartCluster, pcl)(*((u_int16_t *)(dotdotp[1].deStartCluster)) = (pcl));
1177 if (FAT32(pmp)(pmp->pm_fatmask == 0x0fffffff)) {
1178 putushort(dotdotp[0].deHighClust, cn >> 16)(*((u_int16_t *)(dotdotp[0].deHighClust)) = (cn >> 16));
1179 putushort(dotdotp[1].deHighClust, pcl >> 16)(*((u_int16_t *)(dotdotp[1].deHighClust)) = (pcl >> 16)
)
;
1180 }
1181 if ((error = bwrite(bp)) != 0) {
1182 /* XXX should really panic here, fs is corrupt */
1183 goto bad;
1184 }
1185 }
1186
1187 VN_KNOTE(fvp, NOTE_RENAME)knote_locked(&fvp->v_klist, (0x0020));
1188
1189bad:
1190 VOP_UNLOCK(fvp);
1191 vrele(fdvp);
1192bad1:
1193 if (xp)
1194 vput(tvp);
1195 vput(tdvp);
1196out:
1197 ip->de_flag &= ~DE_RENAME0x0040;
1198 vrele(fvp);
1199 return (error);
1200
1201}
1202
1203struct {
1204 struct direntry dot;
1205 struct direntry dotdot;
1206} dosdirtemplate = {
1207 { ". ", " ", /* the . entry */
1208 ATTR_DIRECTORY0x10, /* file attribute */
1209 CASE_LOWER_BASE0x08 | CASE_LOWER_EXT0x10, /* lower case */
1210 0, /* create time 100ths */
1211 { 0, 0 }, { 0, 0 }, /* create time & date */
1212 { 0, 0 }, /* access date */
1213 { 0, 0 }, /* high bits of start cluster */
1214 { 210, 4 }, { 210, 4 }, /* modify time & date */
1215 { 0, 0 }, /* startcluster */
1216 { 0, 0, 0, 0 } /* filesize */
1217 },
1218 { ".. ", " ", /* the .. entry */
1219 ATTR_DIRECTORY0x10, /* file attribute */
1220 CASE_LOWER_BASE0x08 | CASE_LOWER_EXT0x10, /* lower case */
1221 0, /* create time 100ths */
1222 { 0, 0 }, { 0, 0 }, /* create time & date */
1223 { 0, 0 }, /* access date */
1224 { 0, 0 }, /* high bits of start cluster */
1225 { 210, 4 }, { 210, 4 }, /* modify time & date */
1226 { 0, 0 }, /* startcluster */
1227 { 0, 0, 0, 0 } /* filesize */
1228 }
1229};
1230
1231int
1232msdosfs_mkdir(void *v)
1233{
1234 struct vop_mkdir_args *ap = v;
1235 struct componentname *cnp = ap->a_cnp;
1236 struct denode ndirent;
1237 struct denode *dep;
1238 struct denode *pdep = VTODE(ap->a_dvp)((struct denode *)(ap->a_dvp)->v_data);
1239 int error;
1240 daddr_t bn;
1241 uint32_t newcluster, pcl;
1242 struct direntry *denp;
1243 struct msdosfsmount *pmp = pdep->de_pmp;
1244 struct buf *bp;
1245 struct timespec ts;
1246
1247 /*
1248 * If this is the root directory and there is no space left we
1249 * can't do anything. This is because the root directory can not
1250 * change size.
1251 */
1252 if (pdep->de_StartCluster == MSDOSFSROOT0
1253 && pdep->de_fndoffset >= pdep->de_FileSize) {
1254 error = ENOSPC28;
1255 goto bad2;
1256 }
1257
1258 /*
1259 * Allocate a cluster to hold the about to be created directory.
1260 */
1261 error = clusteralloc(pmp, 0, 1, &newcluster, NULL((void *)0));
1262 if (error)
1263 goto bad2;
1264
1265 bzero(&ndirent, sizeof(ndirent))__builtin_bzero((&ndirent), (sizeof(ndirent)));
1266 ndirent.de_pmp = pmp;
1267 ndirent.de_flag = DE_ACCESS0x0010 | DE_CREATE0x0008 | DE_UPDATE0x0004;
1268 getnanotime(&ts);
1269 DETIMES(&ndirent, &ts, &ts, &ts)if ((&ndirent)->de_flag & (0x0004 | 0x0008 | 0x0010
)) { (&ndirent)->de_flag |= 0x0020; if ((&ndirent)
->de_flag & 0x0004) { unix2dostime((&ts), &(&
ndirent)->de_MDate, &(&ndirent)->de_MTime, ((void
*)0)); (&ndirent)->de_Attributes |= 0x20; } if (!((&
ndirent)->de_pmp->pm_flags & 0x04)) { if ((&ndirent
)->de_flag & 0x0010) unix2dostime((&ts), &(&
ndirent)->de_ADate, ((void *)0), ((void *)0)); if ((&ndirent
)->de_flag & 0x0008) unix2dostime((&ts), &(&
ndirent)->de_CDate, &(&ndirent)->de_CTime, &
(&ndirent)->de_CTimeHundredth); } (&ndirent)->de_flag
&= ~(0x0004 | 0x0008 | 0x0010); }
;
1270
1271 /*
1272 * Now fill the cluster with the "." and ".." entries. And write
1273 * the cluster to disk. This way it is there for the parent
1274 * directory to be pointing at if there were a crash.
1275 */
1276 bn = cntobn(pmp, newcluster)((((newcluster)-2) << (((pmp))->pm_cnshift - ((pmp))
->pm_bnshift)) + (pmp)->pm_firstcluster)
;
1277 /* always succeeds */
1278 bp = getblk(pmp->pm_devvp, bn, pmp->pm_bpcluster, 0, INFSLP0xffffffffffffffffULL);
1279 bzero(bp->b_data, pmp->pm_bpcluster)__builtin_bzero((bp->b_data), (pmp->pm_bpcluster));
1280 bcopy(&dosdirtemplate, bp->b_data, sizeof dosdirtemplate);
1281 denp = (struct direntry *)bp->b_data;
1282 putushort(denp[0].deStartCluster, newcluster)(*((u_int16_t *)(denp[0].deStartCluster)) = (newcluster));
1283 putushort(denp[0].deCDate, ndirent.de_CDate)(*((u_int16_t *)(denp[0].deCDate)) = (ndirent.de_CDate));
1284 putushort(denp[0].deCTime, ndirent.de_CTime)(*((u_int16_t *)(denp[0].deCTime)) = (ndirent.de_CTime));
1285 denp[0].deCTimeHundredth = ndirent.de_CTimeHundredth;
1286 putushort(denp[0].deADate, ndirent.de_ADate)(*((u_int16_t *)(denp[0].deADate)) = (ndirent.de_ADate));
1287 putushort(denp[0].deMDate, ndirent.de_MDate)(*((u_int16_t *)(denp[0].deMDate)) = (ndirent.de_MDate));
1288 putushort(denp[0].deMTime, ndirent.de_MTime)(*((u_int16_t *)(denp[0].deMTime)) = (ndirent.de_MTime));
1289 pcl = pdep->de_StartCluster;
1290 if (FAT32(pmp)(pmp->pm_fatmask == 0x0fffffff) && pcl == pmp->pm_rootdirblk)
1291 pcl = 0;
1292 putushort(denp[1].deStartCluster, pcl)(*((u_int16_t *)(denp[1].deStartCluster)) = (pcl));
1293 putushort(denp[1].deCDate, ndirent.de_CDate)(*((u_int16_t *)(denp[1].deCDate)) = (ndirent.de_CDate));
1294 putushort(denp[1].deCTime, ndirent.de_CTime)(*((u_int16_t *)(denp[1].deCTime)) = (ndirent.de_CTime));
1295 denp[1].deCTimeHundredth = ndirent.de_CTimeHundredth;
1296 putushort(denp[1].deADate, ndirent.de_ADate)(*((u_int16_t *)(denp[1].deADate)) = (ndirent.de_ADate));
1297 putushort(denp[1].deMDate, ndirent.de_MDate)(*((u_int16_t *)(denp[1].deMDate)) = (ndirent.de_MDate));
1298 putushort(denp[1].deMTime, ndirent.de_MTime)(*((u_int16_t *)(denp[1].deMTime)) = (ndirent.de_MTime));
1299 if (FAT32(pmp)(pmp->pm_fatmask == 0x0fffffff)) {
1300 putushort(denp[0].deHighClust, newcluster >> 16)(*((u_int16_t *)(denp[0].deHighClust)) = (newcluster >>
16))
;
1301 putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16)(*((u_int16_t *)(denp[1].deHighClust)) = (pdep->de_StartCluster
>> 16))
;
1302 }
1303
1304 if ((error = bwrite(bp)) != 0)
1305 goto bad;
1306
1307 /*
1308 * Now build up a directory entry pointing to the newly allocated
1309 * cluster. This will be written to an empty slot in the parent
1310 * directory.
1311 */
1312#ifdef DIAGNOSTIC1
1313 if ((cnp->cn_flags & HASBUF0x000400) == 0)
1314 panic("msdosfs_mkdir: no name");
1315#endif
1316 if ((error = uniqdosname(pdep, cnp, ndirent.de_Name)) != 0)
1317 goto bad;
1318
1319 ndirent.de_Attributes = ATTR_DIRECTORY0x10;
1320 ndirent.de_StartCluster = newcluster;
1321 ndirent.de_FileSize = 0;
1322 ndirent.de_dev = pdep->de_dev;
1323 ndirent.de_devvp = pdep->de_devvp;
1324 if ((error = createde(&ndirent, pdep, &dep, cnp)) != 0)
1325 goto bad;
1326 if ((cnp->cn_flags & SAVESTART0x001000) == 0)
1327 pool_put(&namei_pool, cnp->cn_pnbuf);
1328 VN_KNOTE(ap->a_dvp, NOTE_WRITE | NOTE_LINK)knote_locked(&ap->a_dvp->v_klist, (0x0002 | 0x0010)
)
;
1329 vput(ap->a_dvp);
1330 *ap->a_vpp = DETOV(dep)((dep)->de_vnode);
1331 return (0);
1332
1333bad:
1334 clusterfree(pmp, newcluster, NULL((void *)0));
1335bad2:
1336 pool_put(&namei_pool, cnp->cn_pnbuf);
1337 vput(ap->a_dvp);
1338 return (error);
1339}
1340
1341int
1342msdosfs_rmdir(void *v)
1343{
1344 struct vop_rmdir_args *ap = v;
1345 struct vnode *vp = ap->a_vp;
1346 struct vnode *dvp = ap->a_dvp;
1347 struct componentname *cnp = ap->a_cnp;
1348 struct denode *ip, *dp;
1349 int error;
1350
1351 ip = VTODE(vp)((struct denode *)(vp)->v_data);
1352 dp = VTODE(dvp)((struct denode *)(dvp)->v_data);
1353 /*
1354 * Verify the directory is empty (and valid).
1355 * (Rmdir ".." won't be valid since
1356 * ".." will contain a reference to
1357 * the current directory and thus be
1358 * non-empty.)
1359 */
1360 error = 0;
Value stored to 'error' is never read
1361 if (!dosdirempty(ip) || ip->de_flag & DE_RENAME0x0040) {
1362 error = ENOTEMPTY66;
1363 goto out;
1364 }
1365
1366 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK)knote_locked(&dvp->v_klist, (0x0002 | 0x0010));
1367
1368 /*
1369 * Delete the entry from the directory. For dos filesystems this
1370 * gets rid of the directory entry on disk, the in memory copy
1371 * still exists but the de_refcnt is <= 0. This prevents it from
1372 * being found by deget(). When the vput() on dep is done we give
1373 * up access and eventually msdosfs_reclaim() will be called which
1374 * will remove it from the denode cache.
1375 */
1376 if ((error = removede(dp, ip)) != 0)
1377 goto out;
1378 /*
1379 * This is where we decrement the link count in the parent
1380 * directory. Since dos filesystems don't do this we just purge
1381 * the name cache and let go of the parent directory denode.
1382 */
1383 cache_purge(dvp);
1384 vput(dvp);
1385 dvp = NULL((void *)0);
1386 /*
1387 * Truncate the directory that is being deleted.
1388 */
1389 error = detrunc(ip, (uint32_t)0, IO_SYNC0x04, cnp->cn_cred, cnp->cn_proc);
1390 cache_purge(vp);
1391out:
1392 if (dvp)
1393 vput(dvp);
1394 VN_KNOTE(vp, NOTE_DELETE)knote_locked(&vp->v_klist, (0x0001));
1395 vput(vp);
1396 return (error);
1397}
1398
1399/*
1400 * DOS filesystems don't know what symlinks are.
1401 */
1402int
1403msdosfs_symlink(void *v)
1404{
1405 struct vop_symlink_args *ap = v;
1406
1407 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
1408 vput(ap->a_dvp);
1409 return (EOPNOTSUPP45);
1410}
1411
1412int
1413msdosfs_readdir(void *v)
1414{
1415 struct vop_readdir_args *ap = v;
1416 int error = 0;
1417 int diff;
1418 long n;
1419 int blsize;
1420 long on;
1421 long lost;
1422 long count;
1423 uint32_t dirsperblk;
1424 uint32_t cn, lbn;
1425 uint32_t fileno;
1426 long bias = 0;
1427 daddr_t bn;
1428 struct buf *bp;
1429 struct denode *dep = VTODE(ap->a_vp)((struct denode *)(ap->a_vp)->v_data);
1430 struct msdosfsmount *pmp = dep->de_pmp;
1431 struct direntry *dentp;
1432 struct dirent dirbuf;
1433 struct uio *uio = ap->a_uio;
1434 off_t offset, wlast = -1;
1435 int chksum = -1;
1436
1437#ifdef MSDOSFS_DEBUG
1438 printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1439 ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1440#endif
1441
1442 /*
1443 * msdosfs_readdir() won't operate properly on regular files since
1444 * it does i/o only with the filesystem vnode, and hence can
1445 * retrieve the wrong block from the buffer cache for a plain file.
1446 * So, fail attempts to readdir() on a plain file.
1447 */
1448 if ((dep->de_Attributes & ATTR_DIRECTORY0x10) == 0)
1449 return (ENOTDIR20);
1450
1451 /*
1452 * To be safe, initialize dirbuf
1453 */
1454 bzero(&dirbuf, sizeof(dirbuf))__builtin_bzero((&dirbuf), (sizeof(dirbuf)));
1455
1456 /*
1457 * If the user buffer is smaller than the size of one dos directory
1458 * entry or the file offset is not a multiple of the size of a
1459 * directory entry, then we fail the read.
1460 */
1461 count = uio->uio_resid & ~(sizeof(struct direntry) - 1);
1462 offset = uio->uio_offset;
1463 if (count < sizeof(struct direntry) ||
1464 (offset & (sizeof(struct direntry) - 1)))
1465 return (EINVAL22);
1466 lost = uio->uio_resid - count;
1467 uio->uio_resid = count;
1468
1469 dirsperblk = pmp->pm_BytesPerSecpm_bpb.bpbBytesPerSec / sizeof(struct direntry);
1470
1471 /*
1472 * If they are reading from the root directory then, we simulate
1473 * the . and .. entries since these don't exist in the root
1474 * directory. We also set the offset bias to make up for having to
1475 * simulate these entries. By this I mean that at file offset 64 we
1476 * read the first entry in the root directory that lives on disk.
1477 */
1478 if (dep->de_StartCluster == MSDOSFSROOT0
1479 || (FAT32(pmp)(pmp->pm_fatmask == 0x0fffffff) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1480#if 0
1481 printf("msdosfs_readdir(): going after . or .. in root dir, offset %d\n",
1482 offset);
1483#endif
1484 bias = 2 * sizeof(struct direntry);
1485 if (offset < bias) {
1486 for (n = (int)offset / sizeof(struct direntry);
1487 n < 2; n++) {
1488 if (FAT32(pmp)(pmp->pm_fatmask == 0x0fffffff))
1489 dirbuf.d_fileno = pmp->pm_rootdirblk;
1490 else
1491 dirbuf.d_fileno = 1;
1492 dirbuf.d_type = DT_DIR4;
1493 switch (n) {
1494 case 0:
1495 dirbuf.d_namlen = 1;
1496 strlcpy(dirbuf.d_name, ".",
1497 sizeof dirbuf.d_name);
1498 break;
1499 case 1:
1500 dirbuf.d_namlen = 2;
1501 strlcpy(dirbuf.d_name, "..",
1502 sizeof dirbuf.d_name);
1503 break;
1504 }
1505 dirbuf.d_reclen = DIRENT_SIZE(&dirbuf)((__builtin_offsetof(struct dirent, d_name) + ((&dirbuf)->
d_namlen) + 1 + 7) &~ 7)
;
1506 dirbuf.d_off = offset +
1507 sizeof(struct direntry);
1508 if (uio->uio_resid < dirbuf.d_reclen)
1509 goto out;
1510 error = uiomove(&dirbuf, dirbuf.d_reclen, uio);
1511 if (error)
1512 goto out;
1513 offset = dirbuf.d_off;
1514 }
1515 }
1516 }
1517
1518 while (uio->uio_resid > 0) {
1519 lbn = de_cluster(pmp, offset - bias)((offset - bias) >> (pmp)->pm_cnshift);
1520 on = (offset - bias) & pmp->pm_crbomask;
1521 n = min(pmp->pm_bpcluster - on, uio->uio_resid);
1522 diff = dep->de_FileSize - (offset - bias);
1523 if (diff <= 0)
1524 break;
1525 n = min(n, diff);
1526 if ((error = pcbmap(dep, lbn, &bn, &cn, &blsize)) != 0)
1527 break;
1528 error = bread(pmp->pm_devvp, bn, blsize, &bp);
1529 if (error) {
1530 brelse(bp);
1531 return (error);
1532 }
1533 n = min(n, blsize - bp->b_resid);
1534
1535 /*
1536 * Convert from dos directory entries to fs-independent
1537 * directory entries.
1538 */
1539 for (dentp = (struct direntry *)(bp->b_data + on);
1540 (char *)dentp < bp->b_data + on + n;
1541 dentp++, offset += sizeof(struct direntry)) {
1542#if 0
1543 printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1544 dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1545#endif
1546 /*
1547 * If this is an unused entry, we can stop.
1548 */
1549 if (dentp->deName[0] == SLOT_EMPTY0x00) {
1550 brelse(bp);
1551 goto out;
1552 }
1553 /*
1554 * Skip deleted entries.
1555 */
1556 if (dentp->deName[0] == SLOT_DELETED0xe5) {
1557 chksum = -1;
1558 wlast = -1;
1559 continue;
1560 }
1561
1562 /*
1563 * Handle Win95 long directory entries
1564 */
1565 if (dentp->deAttributes == ATTR_WIN950x0f) {
1566 struct winentry *wep;
1567 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME0x01)
1568 continue;
1569 wep = (struct winentry *)dentp;
1570 chksum = win2unixfn(wep, &dirbuf, chksum);
1571 if (wep->weCnt & WIN_LAST0x40)
1572 wlast = offset;
1573 continue;
1574 }
1575
1576 /*
1577 * Skip volume labels
1578 */
1579 if (dentp->deAttributes & ATTR_VOLUME0x08) {
1580 chksum = -1;
1581 wlast = -1;
1582 continue;
1583 }
1584
1585 /*
1586 * This computation of d_fileno must match
1587 * the computation of va_fileid in
1588 * msdosfs_getattr.
1589 */
1590 fileno = getushort(dentp->deStartCluster)*((u_int16_t *)(dentp->deStartCluster));
1591 if (FAT32(pmp)(pmp->pm_fatmask == 0x0fffffff))
1592 fileno |= getushort(dentp->deHighClust)*((u_int16_t *)(dentp->deHighClust)) << 16;
1593
1594 if (dentp->deAttributes & ATTR_DIRECTORY0x10) {
1595 /* Special-case root */
1596 if (fileno == MSDOSFSROOT0) {
1597 fileno = FAT32(pmp)(pmp->pm_fatmask == 0x0fffffff) ?
1598 pmp->pm_rootdirblk : 1;
1599 }
1600
1601 dirbuf.d_fileno = fileno;
1602 dirbuf.d_type = DT_DIR4;
1603 } else {
1604 if (getulong(dentp->deFileSize)*((u_int32_t *)(dentp->deFileSize)) == 0) {
1605 uint64_t fileno64;
1606
1607 fileno64 = (cn == MSDOSFSROOT0) ?
1608 roottobn(pmp, 0)((((((((0))) >> (((pmp)))->pm_cnshift)) << (((
pmp))->pm_cnshift - ((pmp))->pm_bnshift))) + (pmp)->
pm_rootdirblk)
: cntobn(pmp, cn)((((cn)-2) << (((pmp))->pm_cnshift - ((pmp))->pm_bnshift
)) + (pmp)->pm_firstcluster)
;
1609
1610 fileno64 *= dirsperblk;
1611 fileno64 += dentp -
1612 (struct direntry *)bp->b_data;
1613
1614 fileno = fileidhash(fileno64);
1615 }
1616
1617 dirbuf.d_fileno = fileno;
1618 dirbuf.d_type = DT_REG8;
1619 }
1620
1621 if (chksum != winChksum(dentp->deName))
1622 dirbuf.d_namlen = dos2unixfn(dentp->deName,
1623 (u_char *)dirbuf.d_name,
1624 pmp->pm_flags & MSDOSFSMNT_SHORTNAME0x01);
1625 else
1626 dirbuf.d_name[dirbuf.d_namlen] = 0;
1627 chksum = -1;
1628 dirbuf.d_reclen = DIRENT_SIZE(&dirbuf)((__builtin_offsetof(struct dirent, d_name) + ((&dirbuf)->
d_namlen) + 1 + 7) &~ 7)
;
1629 dirbuf.d_off = offset + sizeof(struct direntry);
1630 if (uio->uio_resid < dirbuf.d_reclen) {
1631 brelse(bp);
1632 /* Remember long-name offset. */
1633 if (wlast != -1)
1634 offset = wlast;
1635 goto out;
1636 }
1637 wlast = -1;
1638 error = uiomove(&dirbuf, dirbuf.d_reclen, uio);
1639 if (error) {
1640 brelse(bp);
1641 goto out;
1642 }
1643 }
1644 brelse(bp);
1645 }
1646
1647out:
1648 uio->uio_offset = offset;
1649 uio->uio_resid += lost;
1650 if (dep->de_FileSize - (offset - bias) <= 0)
1651 *ap->a_eofflag = 1;
1652 else
1653 *ap->a_eofflag = 0;
1654 return (error);
1655}
1656
1657/*
1658 * DOS filesystems don't know what symlinks are.
1659 */
1660int
1661msdosfs_readlink(void *v)
1662{
1663#if 0
1664 struct vop_readlink_args /* {
1665 struct vnode *a_vp;
1666 struct uio *a_uio;
1667 struct ucred *a_cred;
1668 } */ *ap;
1669#endif
1670
1671 return (EINVAL22);
1672}
1673
1674int
1675msdosfs_lock(void *v)
1676{
1677 struct vop_lock_args *ap = v;
1678 struct vnode *vp = ap->a_vp;
1679
1680 return rrw_enter(&VTODE(vp)((struct denode *)(vp)->v_data)->de_lock, ap->a_flags & LK_RWFLAGS(0x0001UL|0x0002UL|0x0040UL|0x0080UL|0x0100UL));
1681}
1682
1683int
1684msdosfs_unlock(void *v)
1685{
1686 struct vop_unlock_args *ap = v;
1687 struct vnode *vp = ap->a_vp;
1688
1689 rrw_exit(&VTODE(vp)((struct denode *)(vp)->v_data)->de_lock);
1690 return 0;
1691}
1692
1693int
1694msdosfs_islocked(void *v)
1695{
1696 struct vop_islocked_args *ap = v;
1697
1698 return rrw_status(&VTODE(ap->a_vp)((struct denode *)(ap->a_vp)->v_data)->de_lock);
1699}
1700
1701/*
1702 * vp - address of vnode file the file
1703 * bn - which cluster we are interested in mapping to a filesystem block number
1704 * vpp - returns the vnode for the block special file holding the filesystem
1705 * containing the file of interest
1706 * bnp - address of where to return the filesystem relative block number
1707 */
1708int
1709msdosfs_bmap(void *v)
1710{
1711 struct vop_bmap_args *ap = v;
1712 struct denode *dep = VTODE(ap->a_vp)((struct denode *)(ap->a_vp)->v_data);
1713 uint32_t cn;
1714
1715 if (ap->a_vpp != NULL((void *)0))
1716 *ap->a_vpp = dep->de_devvp;
1717 if (ap->a_bnp == NULL((void *)0))
1718 return (0);
1719
1720 cn = ap->a_bn;
1721 if (cn != ap->a_bn)
1722 return (EFBIG27);
1723
1724 return (msdosfs_bmaparray(ap->a_vp, cn, ap->a_bnp, ap->a_runp));
1725}
1726
1727int
1728msdosfs_bmaparray(struct vnode *vp, uint32_t cn, daddr_t *bnp, int *runp)
1729{
1730 struct denode *dep = VTODE(vp)((struct denode *)(vp)->v_data);
1731 struct msdosfsmount *pmp = dep->de_pmp;
1732 struct mount *mp;
1733 int error, maxrun = 0, run;
1734 daddr_t runbn;
1735
1736 mp = vp->v_mount;
1737
1738 if (runp) {
1739 /*
1740 * XXX
1741 * If MAXBSIZE is the largest transfer the disks can handle,
1742 * we probably want maxrun to be 1 block less so that we
1743 * don't create a block larger than the device can handle.
1744 */
1745 *runp = 0;
1746 maxrun = min(MAXBSIZE(64 * 1024) / mp->mnt_stat.f_iosize - 1,
1747 pmp->pm_maxcluster - cn);
1748 }
1749
1750 if ((error = pcbmap(dep, cn, bnp, 0, 0)) != 0)
1751 return (error);
1752
1753 for (run = 1; run <= maxrun; run++) {
1754 error = pcbmap(dep, cn + run, &runbn, 0, 0);
1755 if (error != 0 || (runbn != *bnp + de_cn2bn(pmp, run)((run) << ((pmp)->pm_cnshift - (pmp)->pm_bnshift)
)
))
1756 break;
1757 }
1758
1759 if (runp)
1760 *runp = run - 1;
1761
1762 return (0);
1763}
1764
1765int
1766msdosfs_strategy(void *v)
1767{
1768 struct vop_strategy_args *ap = v;
1769 struct buf *bp = ap->a_bp;
1770 struct denode *dep = VTODE(bp->b_vp)((struct denode *)(bp->b_vp)->v_data);
1771 struct vnode *vp;
1772 int error = 0;
1773 int s;
1774
1775 if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR)
1776 panic("msdosfs_strategy: spec");
1777 /*
1778 * If we don't already know the filesystem relative block number
1779 * then get it using pcbmap(). If pcbmap() returns the block
1780 * number as -1 then we've got a hole in the file. DOS filesystems
1781 * don't allow files with holes, so we shouldn't ever see this.
1782 */
1783 if (bp->b_blkno == bp->b_lblkno) {
1784 error = pcbmap(dep, bp->b_lblkno, &bp->b_blkno, 0, 0);
1785 if (error)
1786 bp->b_blkno = -1;
1787 if (bp->b_blkno == -1)
1788 clrbuf(bp){ __builtin_bzero(((bp)->b_data), ((bp)->b_bcount)); (bp
)->b_resid = 0; }
;
1789 }
1790 if (bp->b_blkno == -1) {
1791 s = splbio()splraise(0x3);
1792 biodone(bp);
1793 splx(s)spllower(s);
1794 return (error);
1795 }
1796
1797 /*
1798 * Read/write the block from/to the disk that contains the desired
1799 * file block.
1800 */
1801
1802 vp = dep->de_devvp;
1803 bp->b_dev = vp->v_rdevv_un.vu_specinfo->si_rdev;
1804 VOP_STRATEGY(vp, bp);
1805 return (0);
1806}
1807
1808int
1809msdosfs_print(void *v)
1810{
1811 struct vop_print_args *ap = v;
1812 struct denode *dep = VTODE(ap->a_vp)((struct denode *)(ap->a_vp)->v_data);
1813
1814 printf(
1815 "tag VT_MSDOSFS, startcluster %u, dircluster %u, diroffset %u ",
1816 dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1817 printf(" dev %d, %d, %s\n",
1818 major(dep->de_dev)(((unsigned)(dep->de_dev) >> 8) & 0xff), minor(dep->de_dev)((unsigned)((dep->de_dev) & 0xff) | (((dep->de_dev)
& 0xffff0000) >> 8))
,
1819 VOP_ISLOCKED(ap->a_vp) ? "(LOCKED)" : "");
1820#ifdef DIAGNOSTIC1
1821 printf("\n");
1822#endif
1823
1824 return (0);
1825}
1826
1827int
1828msdosfs_advlock(void *v)
1829{
1830 struct vop_advlock_args *ap = v;
1831 struct denode *dep = VTODE(ap->a_vp)((struct denode *)(ap->a_vp)->v_data);
1832
1833 return (lf_advlock(&dep->de_lockf, dep->de_FileSize, ap->a_id, ap->a_op,
1834 ap->a_fl, ap->a_flags));
1835}
1836
1837int
1838msdosfs_pathconf(void *v)
1839{
1840 struct vop_pathconf_args *ap = v;
1841 struct msdosfsmount *pmp = VTODE(ap->a_vp)((struct denode *)(ap->a_vp)->v_data)->de_pmp;
1842 int error = 0;
1843
1844 switch (ap->a_name) {
1845 case _PC_LINK_MAX1:
1846 *ap->a_retval = 1;
1847 break;
1848 case _PC_NAME_MAX4:
1849 *ap->a_retval = pmp->pm_flags & MSDOSFSMNT_LONGNAME0x02 ? WIN_MAXLEN255 : 12;
1850 break;
1851 case _PC_CHOWN_RESTRICTED7:
1852 *ap->a_retval = 1;
1853 break;
1854 case _PC_NO_TRUNC8:
1855 *ap->a_retval = 0;
1856 break;
1857 case _PC_TIMESTAMP_RESOLUTION21:
1858 *ap->a_retval = 2000000000; /* 2 billion nanoseconds */
1859 break;
1860 default:
1861 error = EINVAL22;
1862 break;
1863 }
1864
1865 return (error);
1866}
1867
1868/*
1869 * Thomas Wang's hash function, severely hacked to always set the high
1870 * bit on the number it returns (so no longer a proper hash function).
1871 */
1872static uint32_t
1873fileidhash(uint64_t fileid)
1874{
1875 uint64_t c1 = 0x6e5ea73858134343LL;
1876 uint64_t c2 = 0xb34e8f99a2ec9ef5LL;
1877
1878 /*
1879 * We now have the original fileid value, as 64-bit value.
1880 * We need to reduce it to 32-bits, with the top bit set.
1881 */
1882 fileid ^= ((c1 ^ fileid) >> 32);
1883 fileid *= c1;
1884 fileid ^= ((c2 ^ fileid) >> 31);
1885 fileid *= c2;
1886 fileid ^= ((c1 ^ fileid) >> 32);
1887
1888 return (uint32_t)(fileid | 0x80000000);
1889}
1890
1891/* Global vfs data structures for msdosfs */
1892const struct vops msdosfs_vops = {
1893 .vop_lookup = msdosfs_lookup,
1894 .vop_create = msdosfs_create,
1895 .vop_mknod = msdosfs_mknod,
1896 .vop_open = msdosfs_open,
1897 .vop_close = msdosfs_close,
1898 .vop_access = msdosfs_access,
1899 .vop_getattr = msdosfs_getattr,
1900 .vop_setattr = msdosfs_setattr,
1901 .vop_read = msdosfs_read,
1902 .vop_write = msdosfs_write,
1903 .vop_ioctl = msdosfs_ioctl,
1904 .vop_kqfilter = msdosfs_kqfilter,
1905 .vop_fsync = msdosfs_fsync,
1906 .vop_remove = msdosfs_remove,
1907 .vop_link = msdosfs_link,
1908 .vop_rename = msdosfs_rename,
1909 .vop_mkdir = msdosfs_mkdir,
1910 .vop_rmdir = msdosfs_rmdir,
1911 .vop_symlink = msdosfs_symlink,
1912 .vop_readdir = msdosfs_readdir,
1913 .vop_readlink = msdosfs_readlink,
1914 .vop_abortop = vop_generic_abortop,
1915 .vop_inactive = msdosfs_inactive,
1916 .vop_reclaim = msdosfs_reclaim,
1917 .vop_lock = msdosfs_lock,
1918 .vop_unlock = msdosfs_unlock,
1919 .vop_bmap = msdosfs_bmap,
1920 .vop_strategy = msdosfs_strategy,
1921 .vop_print = msdosfs_print,
1922 .vop_islocked = msdosfs_islocked,
1923 .vop_pathconf = msdosfs_pathconf,
1924 .vop_advlock = msdosfs_advlock,
1925 .vop_bwrite = vop_generic_bwrite,
1926 .vop_revoke = vop_generic_revoke,
1927};
1928
1929const struct filterops msdosfsread_filtops = {
1930 .f_flags = FILTEROP_ISFD0x00000001,
1931 .f_attach = NULL((void *)0),
1932 .f_detach = filt_msdosfsdetach,
1933 .f_event = filt_msdosfsread,
1934};
1935
1936const struct filterops msdosfswrite_filtops = {
1937 .f_flags = FILTEROP_ISFD0x00000001,
1938 .f_attach = NULL((void *)0),
1939 .f_detach = filt_msdosfsdetach,
1940 .f_event = filt_msdosfswrite,
1941};
1942
1943const struct filterops msdosfsvnode_filtops = {
1944 .f_flags = FILTEROP_ISFD0x00000001,
1945 .f_attach = NULL((void *)0),
1946 .f_detach = filt_msdosfsdetach,
1947 .f_event = filt_msdosfsvnode,
1948};
1949
1950int
1951msdosfs_kqfilter(void *v)
1952{
1953 struct vop_kqfilter_args *ap = v;
1954 struct vnode *vp = ap->a_vp;
1955 struct knote *kn = ap->a_kn;
1956
1957 switch (kn->kn_filterkn_kevent.filter) {
1958 case EVFILT_READ(-1):
1959 kn->kn_fop = &msdosfsread_filtops;
1960 break;
1961 case EVFILT_WRITE(-2):
1962 kn->kn_fop = &msdosfswrite_filtops;
1963 break;
1964 case EVFILT_VNODE(-4):
1965 kn->kn_fop = &msdosfsvnode_filtops;
1966 break;
1967 default:
1968 return (EINVAL22);
1969 }
1970
1971 kn->kn_hook = (caddr_t)vp;
1972
1973 klist_insert_locked(&vp->v_klist, kn);
1974
1975 return (0);
1976}
1977
1978void
1979filt_msdosfsdetach(struct knote *kn)
1980{
1981 struct vnode *vp = (struct vnode *)kn->kn_hook;
1982
1983 klist_remove_locked(&vp->v_klist, kn);
1984}
1985
1986int
1987filt_msdosfsread(struct knote *kn, long hint)
1988{
1989 struct vnode *vp = (struct vnode *)kn->kn_hook;
1990 struct denode *dep = VTODE(vp)((struct denode *)(vp)->v_data);
1991
1992 /*
1993 * filesystem is gone, so set the EOF flag and schedule
1994 * the knote for deletion.
1995 */
1996 if (hint == NOTE_REVOKE0x0040) {
1997 kn->kn_flagskn_kevent.flags |= (EV_EOF0x8000 | EV_ONESHOT0x0010);
1998 return (1);
1999 }
2000
2001 kn->kn_datakn_kevent.data = dep->de_FileSize - foffset(kn->kn_fpkn_ptr.p_fp);
2002 if (kn->kn_datakn_kevent.data == 0 && kn->kn_sfflags & NOTE_EOF0x0002) {
2003 kn->kn_fflagskn_kevent.fflags |= NOTE_EOF0x0002;
2004 return (1);
2005 }
2006
2007 if (kn->kn_flagskn_kevent.flags & (__EV_POLL0x1000 | __EV_SELECT0x0800))
2008 return (1);
2009
2010 return (kn->kn_datakn_kevent.data != 0);
2011}
2012
2013int
2014filt_msdosfswrite(struct knote *kn, long hint)
2015{
2016 /*
2017 * filesystem is gone, so set the EOF flag and schedule
2018 * the knote for deletion.
2019 */
2020 if (hint == NOTE_REVOKE0x0040) {
2021 kn->kn_flagskn_kevent.flags |= (EV_EOF0x8000 | EV_ONESHOT0x0010);
2022 return (1);
2023 }
2024
2025 kn->kn_datakn_kevent.data = 0;
2026 return (1);
2027}
2028
2029int
2030filt_msdosfsvnode(struct knote *kn, long hint)
2031{
2032 if (kn->kn_sfflags & hint)
2033 kn->kn_fflagskn_kevent.fflags |= hint;
2034 if (hint == NOTE_REVOKE0x0040) {
2035 kn->kn_flagskn_kevent.flags |= EV_EOF0x8000;
2036 return (1);
2037 }
2038 return (kn->kn_fflagskn_kevent.fflags != 0);
2039}