Bug Summary

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