Deleted Added
full compact
msdosfs_vnops.c (67438) msdosfs_vnops.c (72200)
1/* $FreeBSD: head/sys/fs/msdosfs/msdosfs_vnops.c 67438 2000-10-22 14:24:30Z bp $ */
1/* $FreeBSD: head/sys/fs/msdosfs/msdosfs_vnops.c 72200 2001-02-09 06:11:45Z bmilekic $ */
2/* $NetBSD: msdosfs_vnops.c,v 1.68 1998/02/10 14:10:04 mrg Exp $ */
3
4/*-
5 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
7 * All rights reserved.
8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by TooLs GmbH.
21 * 4. The name of TooLs GmbH may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35/*
36 * Written by Paul Popelka (paulp@uts.amdahl.com)
37 *
38 * You can do anything you want with this software, just don't say you wrote
39 * it, and don't remove this notice.
40 *
41 * This software is provided "as is".
42 *
43 * The author supplies this software to be publicly redistributed on the
44 * understanding that the author is not responsible for the correct
45 * functioning of this software in any circumstances and is not liable for
46 * any damages caused by this software.
47 *
48 * October 1992
49 */
50
51#include <sys/param.h>
52#include <sys/systm.h>
53#include <sys/namei.h>
54#include <sys/resourcevar.h> /* defines plimit structure in proc struct */
55#include <sys/kernel.h>
56#include <sys/stat.h>
57#include <sys/bio.h>
58#include <sys/buf.h>
59#include <sys/proc.h>
60#include <sys/mount.h>
61#include <sys/unistd.h>
62#include <sys/vnode.h>
63#include <sys/malloc.h>
64#include <sys/dirent.h>
65#include <sys/signalvar.h>
66
67#include <vm/vm.h>
68#include <vm/vm_extern.h>
69#include <vm/vnode_pager.h>
70
71#include <machine/mutex.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
79#define DOS_FILESIZE_MAX 0xffffffff
80
81/*
82 * Prototypes for MSDOSFS vnode operations
83 */
84static int msdosfs_create __P((struct vop_create_args *));
85static int msdosfs_mknod __P((struct vop_mknod_args *));
86static int msdosfs_close __P((struct vop_close_args *));
87static int msdosfs_access __P((struct vop_access_args *));
88static int msdosfs_getattr __P((struct vop_getattr_args *));
89static int msdosfs_setattr __P((struct vop_setattr_args *));
90static int msdosfs_read __P((struct vop_read_args *));
91static int msdosfs_write __P((struct vop_write_args *));
92static int msdosfs_fsync __P((struct vop_fsync_args *));
93static int msdosfs_remove __P((struct vop_remove_args *));
94static int msdosfs_link __P((struct vop_link_args *));
95static int msdosfs_rename __P((struct vop_rename_args *));
96static int msdosfs_mkdir __P((struct vop_mkdir_args *));
97static int msdosfs_rmdir __P((struct vop_rmdir_args *));
98static int msdosfs_symlink __P((struct vop_symlink_args *));
99static int msdosfs_readdir __P((struct vop_readdir_args *));
100static int msdosfs_bmap __P((struct vop_bmap_args *));
101static int msdosfs_strategy __P((struct vop_strategy_args *));
102static int msdosfs_print __P((struct vop_print_args *));
103static int msdosfs_pathconf __P((struct vop_pathconf_args *ap));
104static int msdosfs_getpages __P((struct vop_getpages_args *));
105static int msdosfs_putpages __P((struct vop_putpages_args *));
106
107/*
108 * Some general notes:
109 *
110 * In the ufs filesystem the inodes, superblocks, and indirect blocks are
111 * read/written using the vnode for the filesystem. Blocks that represent
112 * the contents of a file are read/written using the vnode for the file
113 * (including directories when they are read/written as files). This
114 * presents problems for the dos filesystem because data that should be in
115 * an inode (if dos had them) resides in the directory itself. Since we
116 * must update directory entries without the benefit of having the vnode
117 * for the directory we must use the vnode for the filesystem. This means
118 * that when a directory is actually read/written (via read, write, or
119 * readdir, or seek) we must use the vnode for the filesystem instead of
120 * the vnode for the directory as would happen in ufs. This is to insure we
121 * retreive the correct block from the buffer cache since the hash value is
122 * based upon the vnode address and the desired block number.
123 */
124
125/*
126 * Create a regular file. On entry the directory to contain the file being
127 * created is locked. We must release before we return. We must also free
128 * the pathname buffer pointed at by cnp->cn_pnbuf, always on error, or
129 * only if the SAVESTART bit in cn_flags is clear on success.
130 */
131static int
132msdosfs_create(ap)
133 struct vop_create_args /* {
134 struct vnode *a_dvp;
135 struct vnode **a_vpp;
136 struct componentname *a_cnp;
137 struct vattr *a_vap;
138 } */ *ap;
139{
140 struct componentname *cnp = ap->a_cnp;
141 struct denode ndirent;
142 struct denode *dep;
143 struct denode *pdep = VTODE(ap->a_dvp);
144 struct timespec ts;
145 int error;
146
147#ifdef MSDOSFS_DEBUG
148 printf("msdosfs_create(cnp %p, vap %p\n", cnp, ap->a_vap);
149#endif
150
151 /*
152 * If this is the root directory and there is no space left we
153 * can't do anything. This is because the root directory can not
154 * change size.
155 */
156 if (pdep->de_StartCluster == MSDOSFSROOT
157 && pdep->de_fndoffset >= pdep->de_FileSize) {
158 error = ENOSPC;
159 goto bad;
160 }
161
162 /*
163 * Create a directory entry for the file, then call createde() to
164 * have it installed. NOTE: DOS files are always executable. We
165 * use the absence of the owner write bit to make the file
166 * readonly.
167 */
168#ifdef DIAGNOSTIC
169 if ((cnp->cn_flags & HASBUF) == 0)
170 panic("msdosfs_create: no name");
171#endif
172 bzero(&ndirent, sizeof(ndirent));
173 error = uniqdosname(pdep, cnp, ndirent.de_Name);
174 if (error)
175 goto bad;
176
177 ndirent.de_Attributes = (ap->a_vap->va_mode & VWRITE) ?
178 ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
179 ndirent.de_LowerCase = 0;
180 ndirent.de_StartCluster = 0;
181 ndirent.de_FileSize = 0;
182 ndirent.de_dev = pdep->de_dev;
183 ndirent.de_devvp = pdep->de_devvp;
184 ndirent.de_pmp = pdep->de_pmp;
185 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
186 getnanotime(&ts);
187 DETIMES(&ndirent, &ts, &ts, &ts);
188 error = createde(&ndirent, pdep, &dep, cnp);
189 if (error)
190 goto bad;
191 *ap->a_vpp = DETOV(dep);
192 return (0);
193
194bad:
195 return (error);
196}
197
198static int
199msdosfs_mknod(ap)
200 struct vop_mknod_args /* {
201 struct vnode *a_dvp;
202 struct vnode **a_vpp;
203 struct componentname *a_cnp;
204 struct vattr *a_vap;
205 } */ *ap;
206{
207
208 switch (ap->a_vap->va_type) {
209 case VDIR:
210 return (msdosfs_mkdir((struct vop_mkdir_args *)ap));
211 break;
212
213 case VREG:
214 return (msdosfs_create((struct vop_create_args *)ap));
215 break;
216
217 default:
218 return (EINVAL);
219 }
220 /* NOTREACHED */
221}
222
223static int
224msdosfs_close(ap)
225 struct vop_close_args /* {
226 struct vnode *a_vp;
227 int a_fflag;
228 struct ucred *a_cred;
229 struct proc *a_p;
230 } */ *ap;
231{
232 struct vnode *vp = ap->a_vp;
233 struct denode *dep = VTODE(vp);
234 struct timespec ts;
235
2/* $NetBSD: msdosfs_vnops.c,v 1.68 1998/02/10 14:10:04 mrg Exp $ */
3
4/*-
5 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
7 * All rights reserved.
8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by TooLs GmbH.
21 * 4. The name of TooLs GmbH may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35/*
36 * Written by Paul Popelka (paulp@uts.amdahl.com)
37 *
38 * You can do anything you want with this software, just don't say you wrote
39 * it, and don't remove this notice.
40 *
41 * This software is provided "as is".
42 *
43 * The author supplies this software to be publicly redistributed on the
44 * understanding that the author is not responsible for the correct
45 * functioning of this software in any circumstances and is not liable for
46 * any damages caused by this software.
47 *
48 * October 1992
49 */
50
51#include <sys/param.h>
52#include <sys/systm.h>
53#include <sys/namei.h>
54#include <sys/resourcevar.h> /* defines plimit structure in proc struct */
55#include <sys/kernel.h>
56#include <sys/stat.h>
57#include <sys/bio.h>
58#include <sys/buf.h>
59#include <sys/proc.h>
60#include <sys/mount.h>
61#include <sys/unistd.h>
62#include <sys/vnode.h>
63#include <sys/malloc.h>
64#include <sys/dirent.h>
65#include <sys/signalvar.h>
66
67#include <vm/vm.h>
68#include <vm/vm_extern.h>
69#include <vm/vnode_pager.h>
70
71#include <machine/mutex.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
79#define DOS_FILESIZE_MAX 0xffffffff
80
81/*
82 * Prototypes for MSDOSFS vnode operations
83 */
84static int msdosfs_create __P((struct vop_create_args *));
85static int msdosfs_mknod __P((struct vop_mknod_args *));
86static int msdosfs_close __P((struct vop_close_args *));
87static int msdosfs_access __P((struct vop_access_args *));
88static int msdosfs_getattr __P((struct vop_getattr_args *));
89static int msdosfs_setattr __P((struct vop_setattr_args *));
90static int msdosfs_read __P((struct vop_read_args *));
91static int msdosfs_write __P((struct vop_write_args *));
92static int msdosfs_fsync __P((struct vop_fsync_args *));
93static int msdosfs_remove __P((struct vop_remove_args *));
94static int msdosfs_link __P((struct vop_link_args *));
95static int msdosfs_rename __P((struct vop_rename_args *));
96static int msdosfs_mkdir __P((struct vop_mkdir_args *));
97static int msdosfs_rmdir __P((struct vop_rmdir_args *));
98static int msdosfs_symlink __P((struct vop_symlink_args *));
99static int msdosfs_readdir __P((struct vop_readdir_args *));
100static int msdosfs_bmap __P((struct vop_bmap_args *));
101static int msdosfs_strategy __P((struct vop_strategy_args *));
102static int msdosfs_print __P((struct vop_print_args *));
103static int msdosfs_pathconf __P((struct vop_pathconf_args *ap));
104static int msdosfs_getpages __P((struct vop_getpages_args *));
105static int msdosfs_putpages __P((struct vop_putpages_args *));
106
107/*
108 * Some general notes:
109 *
110 * In the ufs filesystem the inodes, superblocks, and indirect blocks are
111 * read/written using the vnode for the filesystem. Blocks that represent
112 * the contents of a file are read/written using the vnode for the file
113 * (including directories when they are read/written as files). This
114 * presents problems for the dos filesystem because data that should be in
115 * an inode (if dos had them) resides in the directory itself. Since we
116 * must update directory entries without the benefit of having the vnode
117 * for the directory we must use the vnode for the filesystem. This means
118 * that when a directory is actually read/written (via read, write, or
119 * readdir, or seek) we must use the vnode for the filesystem instead of
120 * the vnode for the directory as would happen in ufs. This is to insure we
121 * retreive the correct block from the buffer cache since the hash value is
122 * based upon the vnode address and the desired block number.
123 */
124
125/*
126 * Create a regular file. On entry the directory to contain the file being
127 * created is locked. We must release before we return. We must also free
128 * the pathname buffer pointed at by cnp->cn_pnbuf, always on error, or
129 * only if the SAVESTART bit in cn_flags is clear on success.
130 */
131static int
132msdosfs_create(ap)
133 struct vop_create_args /* {
134 struct vnode *a_dvp;
135 struct vnode **a_vpp;
136 struct componentname *a_cnp;
137 struct vattr *a_vap;
138 } */ *ap;
139{
140 struct componentname *cnp = ap->a_cnp;
141 struct denode ndirent;
142 struct denode *dep;
143 struct denode *pdep = VTODE(ap->a_dvp);
144 struct timespec ts;
145 int error;
146
147#ifdef MSDOSFS_DEBUG
148 printf("msdosfs_create(cnp %p, vap %p\n", cnp, ap->a_vap);
149#endif
150
151 /*
152 * If this is the root directory and there is no space left we
153 * can't do anything. This is because the root directory can not
154 * change size.
155 */
156 if (pdep->de_StartCluster == MSDOSFSROOT
157 && pdep->de_fndoffset >= pdep->de_FileSize) {
158 error = ENOSPC;
159 goto bad;
160 }
161
162 /*
163 * Create a directory entry for the file, then call createde() to
164 * have it installed. NOTE: DOS files are always executable. We
165 * use the absence of the owner write bit to make the file
166 * readonly.
167 */
168#ifdef DIAGNOSTIC
169 if ((cnp->cn_flags & HASBUF) == 0)
170 panic("msdosfs_create: no name");
171#endif
172 bzero(&ndirent, sizeof(ndirent));
173 error = uniqdosname(pdep, cnp, ndirent.de_Name);
174 if (error)
175 goto bad;
176
177 ndirent.de_Attributes = (ap->a_vap->va_mode & VWRITE) ?
178 ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
179 ndirent.de_LowerCase = 0;
180 ndirent.de_StartCluster = 0;
181 ndirent.de_FileSize = 0;
182 ndirent.de_dev = pdep->de_dev;
183 ndirent.de_devvp = pdep->de_devvp;
184 ndirent.de_pmp = pdep->de_pmp;
185 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
186 getnanotime(&ts);
187 DETIMES(&ndirent, &ts, &ts, &ts);
188 error = createde(&ndirent, pdep, &dep, cnp);
189 if (error)
190 goto bad;
191 *ap->a_vpp = DETOV(dep);
192 return (0);
193
194bad:
195 return (error);
196}
197
198static int
199msdosfs_mknod(ap)
200 struct vop_mknod_args /* {
201 struct vnode *a_dvp;
202 struct vnode **a_vpp;
203 struct componentname *a_cnp;
204 struct vattr *a_vap;
205 } */ *ap;
206{
207
208 switch (ap->a_vap->va_type) {
209 case VDIR:
210 return (msdosfs_mkdir((struct vop_mkdir_args *)ap));
211 break;
212
213 case VREG:
214 return (msdosfs_create((struct vop_create_args *)ap));
215 break;
216
217 default:
218 return (EINVAL);
219 }
220 /* NOTREACHED */
221}
222
223static int
224msdosfs_close(ap)
225 struct vop_close_args /* {
226 struct vnode *a_vp;
227 int a_fflag;
228 struct ucred *a_cred;
229 struct proc *a_p;
230 } */ *ap;
231{
232 struct vnode *vp = ap->a_vp;
233 struct denode *dep = VTODE(vp);
234 struct timespec ts;
235
236 mtx_enter(&vp->v_interlock, MTX_DEF);
236 mtx_lock(&vp->v_interlock);
237 if (vp->v_usecount > 1) {
238 getnanotime(&ts);
239 DETIMES(dep, &ts, &ts, &ts);
240 }
237 if (vp->v_usecount > 1) {
238 getnanotime(&ts);
239 DETIMES(dep, &ts, &ts, &ts);
240 }
241 mtx_exit(&vp->v_interlock, MTX_DEF);
241 mtx_unlock(&vp->v_interlock);
242 return 0;
243}
244
245static int
246msdosfs_access(ap)
247 struct vop_access_args /* {
248 struct vnode *a_vp;
249 int a_mode;
250 struct ucred *a_cred;
251 struct proc *a_p;
252 } */ *ap;
253{
254 struct vnode *vp = ap->a_vp;
255 struct denode *dep = VTODE(ap->a_vp);
256 struct msdosfsmount *pmp = dep->de_pmp;
257 mode_t file_mode, mode = ap->a_mode;
258
259 file_mode = (S_IXUSR|S_IXGRP|S_IXOTH) | (S_IRUSR|S_IRGRP|S_IROTH) |
260 ((dep->de_Attributes & ATTR_READONLY) ? 0 : (S_IWUSR|S_IWGRP|S_IWOTH));
261 file_mode &= pmp->pm_mask;
262
263 /*
264 * Disallow write attempts on read-only file systems;
265 * unless the file is a socket, fifo, or a block or
266 * character device resident on the file system.
267 */
268 if (mode & VWRITE) {
269 switch (vp->v_type) {
270 case VDIR:
271 case VLNK:
272 case VREG:
273 if (vp->v_mount->mnt_flag & MNT_RDONLY)
274 return (EROFS);
275 break;
276 default:
277 break;
278 }
279 }
280
281 return (vaccess(vp->v_type, file_mode, pmp->pm_uid, pmp->pm_gid,
282 ap->a_mode, ap->a_cred, NULL));
283}
284
285static int
286msdosfs_getattr(ap)
287 struct vop_getattr_args /* {
288 struct vnode *a_vp;
289 struct vattr *a_vap;
290 struct ucred *a_cred;
291 struct proc *a_p;
292 } */ *ap;
293{
294 struct denode *dep = VTODE(ap->a_vp);
295 struct msdosfsmount *pmp = dep->de_pmp;
296 struct vattr *vap = ap->a_vap;
297 mode_t mode;
298 struct timespec ts;
299 u_long dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
300 u_long fileid;
301
302 getnanotime(&ts);
303 DETIMES(dep, &ts, &ts, &ts);
304 vap->va_fsid = dev2udev(dep->de_dev);
305 /*
306 * The following computation of the fileid must be the same as that
307 * used in msdosfs_readdir() to compute d_fileno. If not, pwd
308 * doesn't work.
309 */
310 if (dep->de_Attributes & ATTR_DIRECTORY) {
311 fileid = cntobn(pmp, dep->de_StartCluster) * dirsperblk;
312 if (dep->de_StartCluster == MSDOSFSROOT)
313 fileid = 1;
314 } else {
315 fileid = cntobn(pmp, dep->de_dirclust) * dirsperblk;
316 if (dep->de_dirclust == MSDOSFSROOT)
317 fileid = roottobn(pmp, 0) * dirsperblk;
318 fileid += dep->de_diroffset / sizeof(struct direntry);
319 }
320 vap->va_fileid = fileid;
321 if ((dep->de_Attributes & ATTR_READONLY) == 0)
322 mode = S_IRWXU|S_IRWXG|S_IRWXO;
323 else
324 mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
325 vap->va_mode = mode & pmp->pm_mask;
326 vap->va_uid = pmp->pm_uid;
327 vap->va_gid = pmp->pm_gid;
328 vap->va_nlink = 1;
329 vap->va_rdev = 0;
330 vap->va_size = dep->de_FileSize;
331 dos2unixtime(dep->de_MDate, dep->de_MTime, 0, &vap->va_mtime);
332 if (pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
333 dos2unixtime(dep->de_ADate, 0, 0, &vap->va_atime);
334 dos2unixtime(dep->de_CDate, dep->de_CTime, dep->de_CHun, &vap->va_ctime);
335 } else {
336 vap->va_atime = vap->va_mtime;
337 vap->va_ctime = vap->va_mtime;
338 }
339 vap->va_flags = 0;
340 if ((dep->de_Attributes & ATTR_ARCHIVE) == 0)
341 vap->va_flags |= SF_ARCHIVED;
342 vap->va_gen = 0;
343 vap->va_blocksize = pmp->pm_bpcluster;
344 vap->va_bytes =
345 (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask;
346 vap->va_type = ap->a_vp->v_type;
347 vap->va_filerev = dep->de_modrev;
348 return (0);
349}
350
351static int
352msdosfs_setattr(ap)
353 struct vop_setattr_args /* {
354 struct vnode *a_vp;
355 struct vattr *a_vap;
356 struct ucred *a_cred;
357 struct proc *a_p;
358 } */ *ap;
359{
360 struct vnode *vp = ap->a_vp;
361 struct denode *dep = VTODE(ap->a_vp);
362 struct msdosfsmount *pmp = dep->de_pmp;
363 struct vattr *vap = ap->a_vap;
364 struct ucred *cred = ap->a_cred;
365 int error = 0;
366
367#ifdef MSDOSFS_DEBUG
368 printf("msdosfs_setattr(): vp %p, vap %p, cred %p, p %p\n",
369 ap->a_vp, vap, cred, ap->a_p);
370#endif
371
372 /*
373 * Check for unsettable attributes.
374 */
375 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
376 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
377 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
378 (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
379#ifdef MSDOSFS_DEBUG
380 printf("msdosfs_setattr(): returning EINVAL\n");
381 printf(" va_type %d, va_nlink %x, va_fsid %lx, va_fileid %lx\n",
382 vap->va_type, vap->va_nlink, vap->va_fsid, vap->va_fileid);
383 printf(" va_blocksize %lx, va_rdev %x, va_bytes %qx, va_gen %lx\n",
384 vap->va_blocksize, vap->va_rdev, vap->va_bytes, vap->va_gen);
385 printf(" va_uid %x, va_gid %x\n",
386 vap->va_uid, vap->va_gid);
387#endif
388 return (EINVAL);
389 }
390 if (vap->va_flags != VNOVAL) {
391 if (vp->v_mount->mnt_flag & MNT_RDONLY)
392 return (EROFS);
393 if (cred->cr_uid != pmp->pm_uid &&
394 (error = suser_xxx(cred, ap->a_p, PRISON_ROOT)))
395 return (error);
396 /*
397 * We are very inconsistent about handling unsupported
398 * attributes. We ignored the access time and the
399 * read and execute bits. We were strict for the other
400 * attributes.
401 *
402 * Here we are strict, stricter than ufs in not allowing
403 * users to attempt to set SF_SETTABLE bits or anyone to
404 * set unsupported bits. However, we ignore attempts to
405 * set ATTR_ARCHIVE for directories `cp -pr' from a more
406 * sensible file system attempts it a lot.
407 */
408 if (cred->cr_uid != 0) {
409 if (vap->va_flags & SF_SETTABLE)
410 return EPERM;
411 }
412 if (vap->va_flags & ~SF_ARCHIVED)
413 return EOPNOTSUPP;
414 if (vap->va_flags & SF_ARCHIVED)
415 dep->de_Attributes &= ~ATTR_ARCHIVE;
416 else if (!(dep->de_Attributes & ATTR_DIRECTORY))
417 dep->de_Attributes |= ATTR_ARCHIVE;
418 dep->de_flag |= DE_MODIFIED;
419 }
420
421 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
422 uid_t uid;
423 gid_t gid;
424
425 if (vp->v_mount->mnt_flag & MNT_RDONLY)
426 return (EROFS);
427 uid = vap->va_uid;
428 if (uid == (uid_t)VNOVAL)
429 uid = pmp->pm_uid;
430 gid = vap->va_gid;
431 if (gid == (gid_t)VNOVAL)
432 gid = pmp->pm_gid;
433 if ((cred->cr_uid != pmp->pm_uid || uid != pmp->pm_uid ||
434 (gid != pmp->pm_gid && !groupmember(gid, cred))) &&
435 (error = suser_xxx(cred, ap->a_p, PRISON_ROOT)))
436 return error;
437 if (uid != pmp->pm_uid || gid != pmp->pm_gid)
438 return EINVAL;
439 }
440
441 if (vap->va_size != VNOVAL) {
442 /*
443 * Disallow write attempts on read-only file systems;
444 * unless the file is a socket, fifo, or a block or
445 * character device resident on the file system.
446 */
447 switch (vp->v_type) {
448 case VDIR:
449 return (EISDIR);
450 /* NOT REACHED */
451 case VLNK:
452 case VREG:
453 if (vp->v_mount->mnt_flag & MNT_RDONLY)
454 return (EROFS);
455 break;
456 default:
457 break;
458 }
459 error = detrunc(dep, vap->va_size, 0, cred, ap->a_p);
460 if (error)
461 return error;
462 }
463 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
464 if (vp->v_mount->mnt_flag & MNT_RDONLY)
465 return (EROFS);
466 if (cred->cr_uid != pmp->pm_uid &&
467 (error = suser_xxx(cred, ap->a_p, PRISON_ROOT)) &&
468 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
469 (error = VOP_ACCESS(ap->a_vp, VWRITE, cred, ap->a_p))))
470 return (error);
471 if (vp->v_type != VDIR) {
472 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
473 vap->va_atime.tv_sec != VNOVAL)
474 unix2dostime(&vap->va_atime, &dep->de_ADate, NULL, NULL);
475 if (vap->va_mtime.tv_sec != VNOVAL)
476 unix2dostime(&vap->va_mtime, &dep->de_MDate, &dep->de_MTime, NULL);
477 dep->de_Attributes |= ATTR_ARCHIVE;
478 dep->de_flag |= DE_MODIFIED;
479 }
480 }
481 /*
482 * DOS files only have the ability to have their writability
483 * attribute set, so we use the owner write bit to set the readonly
484 * attribute.
485 */
486 if (vap->va_mode != (mode_t)VNOVAL) {
487 if (vp->v_mount->mnt_flag & MNT_RDONLY)
488 return (EROFS);
489 if (cred->cr_uid != pmp->pm_uid &&
490 (error = suser_xxx(cred, ap->a_p, PRISON_ROOT)))
491 return (error);
492 if (vp->v_type != VDIR) {
493 /* We ignore the read and execute bits. */
494 if (vap->va_mode & VWRITE)
495 dep->de_Attributes &= ~ATTR_READONLY;
496 else
497 dep->de_Attributes |= ATTR_READONLY;
498 dep->de_flag |= DE_MODIFIED;
499 }
500 }
501 return (deupdat(dep, 1));
502}
503
504static int
505msdosfs_read(ap)
506 struct vop_read_args /* {
507 struct vnode *a_vp;
508 struct uio *a_uio;
509 int a_ioflag;
510 struct ucred *a_cred;
511 } */ *ap;
512{
513 int error = 0;
514 int blsize;
515 int isadir;
516 int orig_resid;
517 u_int n;
518 u_long diff;
519 u_long on;
520 daddr_t lbn;
521 daddr_t rablock;
522 int rasize;
523 int seqcount;
524 struct buf *bp;
525 struct vnode *vp = ap->a_vp;
526 struct denode *dep = VTODE(vp);
527 struct msdosfsmount *pmp = dep->de_pmp;
528 struct uio *uio = ap->a_uio;
529
530 if (uio->uio_offset < 0)
531 return (EINVAL);
532
533 if ((uoff_t)uio->uio_offset > DOS_FILESIZE_MAX)
534 return (0);
535 /*
536 * If they didn't ask for any data, then we are done.
537 */
538 orig_resid = uio->uio_resid;
539 if (orig_resid <= 0)
540 return (0);
541
542 seqcount = ap->a_ioflag >> 16;
543
544 isadir = dep->de_Attributes & ATTR_DIRECTORY;
545 do {
546 if (uio->uio_offset >= dep->de_FileSize)
547 break;
548 lbn = de_cluster(pmp, uio->uio_offset);
549 /*
550 * If we are operating on a directory file then be sure to
551 * do i/o with the vnode for the filesystem instead of the
552 * vnode for the directory.
553 */
554 if (isadir) {
555 /* convert cluster # to block # */
556 error = pcbmap(dep, lbn, &lbn, 0, &blsize);
557 if (error == E2BIG) {
558 error = EINVAL;
559 break;
560 } else if (error)
561 break;
562 error = bread(pmp->pm_devvp, lbn, blsize, NOCRED, &bp);
563 } else {
564 blsize = pmp->pm_bpcluster;
565 rablock = lbn + 1;
566 if (seqcount > 1 &&
567 de_cn2off(pmp, rablock) < dep->de_FileSize) {
568 rasize = pmp->pm_bpcluster;
569 error = breadn(vp, lbn, blsize,
570 &rablock, &rasize, 1, NOCRED, &bp);
571 } else {
572 error = bread(vp, lbn, blsize, NOCRED, &bp);
573 }
574 }
575 if (error) {
576 brelse(bp);
577 break;
578 }
579 on = uio->uio_offset & pmp->pm_crbomask;
580 diff = pmp->pm_bpcluster - on;
581 n = diff > uio->uio_resid ? uio->uio_resid : diff;
582 diff = dep->de_FileSize - uio->uio_offset;
583 if (diff < n)
584 n = diff;
585 diff = blsize - bp->b_resid;
586 if (diff < n)
587 n = diff;
588 error = uiomove(bp->b_data + on, (int) n, uio);
589 brelse(bp);
590 } while (error == 0 && uio->uio_resid > 0 && n != 0);
591 if (!isadir && (error == 0 || uio->uio_resid != orig_resid) &&
592 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
593 dep->de_flag |= DE_ACCESS;
594 return (error);
595}
596
597/*
598 * Write data to a file or directory.
599 */
600static int
601msdosfs_write(ap)
602 struct vop_write_args /* {
603 struct vnode *a_vp;
604 struct uio *a_uio;
605 int a_ioflag;
606 struct ucred *a_cred;
607 } */ *ap;
608{
609 int n;
610 int croffset;
611 int resid;
612 u_long osize;
613 int error = 0;
614 u_long count;
615 daddr_t bn, lastcn;
616 struct buf *bp;
617 int ioflag = ap->a_ioflag;
618 struct uio *uio = ap->a_uio;
619 struct proc *p = uio->uio_procp;
620 struct vnode *vp = ap->a_vp;
621 struct vnode *thisvp;
622 struct denode *dep = VTODE(vp);
623 struct msdosfsmount *pmp = dep->de_pmp;
624 struct ucred *cred = ap->a_cred;
625
626#ifdef MSDOSFS_DEBUG
627 printf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
628 vp, uio, ioflag, cred);
629 printf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n",
630 dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
631#endif
632
633 switch (vp->v_type) {
634 case VREG:
635 if (ioflag & IO_APPEND)
636 uio->uio_offset = dep->de_FileSize;
637 thisvp = vp;
638 break;
639 case VDIR:
640 return EISDIR;
641 default:
642 panic("msdosfs_write(): bad file type");
643 }
644
645 if (uio->uio_offset < 0)
646 return (EFBIG);
647
648 if (uio->uio_resid == 0)
649 return (0);
650
651 /*
652 * If they've exceeded their filesize limit, tell them about it.
653 */
654 if (p &&
655 ((uoff_t)uio->uio_offset + uio->uio_resid >
656 p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
657 psignal(p, SIGXFSZ);
658 return (EFBIG);
659 }
660
661 if ((uoff_t)uio->uio_offset + uio->uio_resid > DOS_FILESIZE_MAX)
662 return (EFBIG);
663
664 /*
665 * If the offset we are starting the write at is beyond the end of
666 * the file, then they've done a seek. Unix filesystems allow
667 * files with holes in them, DOS doesn't so we must fill the hole
668 * with zeroed blocks.
669 */
670 if (uio->uio_offset > dep->de_FileSize) {
671 error = deextend(dep, uio->uio_offset, cred);
672 if (error)
673 return (error);
674 }
675
676 /*
677 * Remember some values in case the write fails.
678 */
679 resid = uio->uio_resid;
680 osize = dep->de_FileSize;
681
682 /*
683 * If we write beyond the end of the file, extend it to its ultimate
684 * size ahead of the time to hopefully get a contiguous area.
685 */
686 if (uio->uio_offset + resid > osize) {
687 count = de_clcount(pmp, uio->uio_offset + resid) -
688 de_clcount(pmp, osize);
689 error = extendfile(dep, count, NULL, NULL, 0);
690 if (error && (error != ENOSPC || (ioflag & IO_UNIT)))
691 goto errexit;
692 lastcn = dep->de_fc[FC_LASTFC].fc_frcn;
693 } else
694 lastcn = de_clcount(pmp, osize) - 1;
695
696 do {
697 if (de_cluster(pmp, uio->uio_offset) > lastcn) {
698 error = ENOSPC;
699 break;
700 }
701
702 croffset = uio->uio_offset & pmp->pm_crbomask;
703 n = min(uio->uio_resid, pmp->pm_bpcluster - croffset);
704 if (uio->uio_offset + n > dep->de_FileSize) {
705 dep->de_FileSize = uio->uio_offset + n;
706 /* The object size needs to be set before buffer is allocated */
707 vnode_pager_setsize(vp, dep->de_FileSize);
708 }
709
710 bn = de_cluster(pmp, uio->uio_offset);
711 if ((uio->uio_offset & pmp->pm_crbomask) == 0
712 && (de_cluster(pmp, uio->uio_offset + uio->uio_resid)
713 > de_cluster(pmp, uio->uio_offset)
714 || uio->uio_offset + uio->uio_resid >= dep->de_FileSize)) {
715 /*
716 * If either the whole cluster gets written,
717 * or we write the cluster from its start beyond EOF,
718 * then no need to read data from disk.
719 */
720 bp = getblk(thisvp, bn, pmp->pm_bpcluster, 0, 0);
721 clrbuf(bp);
722 /*
723 * Do the bmap now, since pcbmap needs buffers
724 * for the fat table. (see msdosfs_strategy)
725 */
726 if (bp->b_blkno == bp->b_lblkno) {
727 error = pcbmap(dep, bp->b_lblkno, &bp->b_blkno,
728 0, 0);
729 if (error)
730 bp->b_blkno = -1;
731 }
732 if (bp->b_blkno == -1) {
733 brelse(bp);
734 if (!error)
735 error = EIO; /* XXX */
736 break;
737 }
738 } else {
739 /*
740 * The block we need to write into exists, so read it in.
741 */
742 error = bread(thisvp, bn, pmp->pm_bpcluster, cred, &bp);
743 if (error) {
744 brelse(bp);
745 break;
746 }
747 }
748
749 /*
750 * Should these vnode_pager_* functions be done on dir
751 * files?
752 */
753
754 /*
755 * Copy the data from user space into the buf header.
756 */
757 error = uiomove(bp->b_data + croffset, n, uio);
758 if (error) {
759 brelse(bp);
760 break;
761 }
762
763 /*
764 * If they want this synchronous then write it and wait for
765 * it. Otherwise, if on a cluster boundary write it
766 * asynchronously so we can move on to the next block
767 * without delay. Otherwise do a delayed write because we
768 * may want to write somemore into the block later.
769 */
770 if (ioflag & IO_SYNC)
771 (void) bwrite(bp);
772 else if (n + croffset == pmp->pm_bpcluster)
773 bawrite(bp);
774 else
775 bdwrite(bp);
776 dep->de_flag |= DE_UPDATE;
777 } while (error == 0 && uio->uio_resid > 0);
778
779 /*
780 * If the write failed and they want us to, truncate the file back
781 * to the size it was before the write was attempted.
782 */
783errexit:
784 if (error) {
785 if (ioflag & IO_UNIT) {
786 detrunc(dep, osize, ioflag & IO_SYNC, NOCRED, NULL);
787 uio->uio_offset -= resid - uio->uio_resid;
788 uio->uio_resid = resid;
789 } else {
790 detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC, NOCRED, NULL);
791 if (uio->uio_resid != resid)
792 error = 0;
793 }
794 } else if (ioflag & IO_SYNC)
795 error = deupdat(dep, 1);
796 return (error);
797}
798
799/*
800 * Flush the blocks of a file to disk.
801 *
802 * This function is worthless for vnodes that represent directories. Maybe we
803 * could just do a sync if they try an fsync on a directory file.
804 */
805static int
806msdosfs_fsync(ap)
807 struct vop_fsync_args /* {
808 struct vnode *a_vp;
809 struct ucred *a_cred;
810 int a_waitfor;
811 struct proc *a_p;
812 } */ *ap;
813{
814 struct vnode *vp = ap->a_vp;
815 int s;
816 struct buf *bp, *nbp;
817
818 /*
819 * Flush all dirty buffers associated with a vnode.
820 */
821loop:
822 s = splbio();
823 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
824 nbp = TAILQ_NEXT(bp, b_vnbufs);
825 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
826 continue;
827 if ((bp->b_flags & B_DELWRI) == 0)
828 panic("msdosfs_fsync: not dirty");
829 bremfree(bp);
830 splx(s);
831 (void) bwrite(bp);
832 goto loop;
833 }
834 while (vp->v_numoutput) {
835 vp->v_flag |= VBWAIT;
836 (void) tsleep((caddr_t)&vp->v_numoutput, PRIBIO + 1, "msdosfsn", 0);
837 }
838#ifdef DIAGNOSTIC
839 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
840 vprint("msdosfs_fsync: dirty", vp);
841 goto loop;
842 }
843#endif
844 splx(s);
845 return (deupdat(VTODE(vp), ap->a_waitfor == MNT_WAIT));
846}
847
848static int
849msdosfs_remove(ap)
850 struct vop_remove_args /* {
851 struct vnode *a_dvp;
852 struct vnode *a_vp;
853 struct componentname *a_cnp;
854 } */ *ap;
855{
856 struct denode *dep = VTODE(ap->a_vp);
857 struct denode *ddep = VTODE(ap->a_dvp);
858 int error;
859
860 if (ap->a_vp->v_type == VDIR)
861 error = EPERM;
862 else
863 error = removede(ddep, dep);
864#ifdef MSDOSFS_DEBUG
865 printf("msdosfs_remove(), dep %p, v_usecount %d\n", dep, ap->a_vp->v_usecount);
866#endif
867 return (error);
868}
869
870/*
871 * DOS filesystems don't know what links are.
872 */
873static int
874msdosfs_link(ap)
875 struct vop_link_args /* {
876 struct vnode *a_tdvp;
877 struct vnode *a_vp;
878 struct componentname *a_cnp;
879 } */ *ap;
880{
881 return (EOPNOTSUPP);
882}
883
884/*
885 * Renames on files require moving the denode to a new hash queue since the
886 * denode's location is used to compute which hash queue to put the file
887 * in. Unless it is a rename in place. For example "mv a b".
888 *
889 * What follows is the basic algorithm:
890 *
891 * if (file move) {
892 * if (dest file exists) {
893 * remove dest file
894 * }
895 * if (dest and src in same directory) {
896 * rewrite name in existing directory slot
897 * } else {
898 * write new entry in dest directory
899 * update offset and dirclust in denode
900 * move denode to new hash chain
901 * clear old directory entry
902 * }
903 * } else {
904 * directory move
905 * if (dest directory exists) {
906 * if (dest is not empty) {
907 * return ENOTEMPTY
908 * }
909 * remove dest directory
910 * }
911 * if (dest and src in same directory) {
912 * rewrite name in existing entry
913 * } else {
914 * be sure dest is not a child of src directory
915 * write entry in dest directory
916 * update "." and ".." in moved directory
917 * clear old directory entry for moved directory
918 * }
919 * }
920 *
921 * On entry:
922 * source's parent directory is unlocked
923 * source file or directory is unlocked
924 * destination's parent directory is locked
925 * destination file or directory is locked if it exists
926 *
927 * On exit:
928 * all denodes should be released
929 */
930static int
931msdosfs_rename(ap)
932 struct vop_rename_args /* {
933 struct vnode *a_fdvp;
934 struct vnode *a_fvp;
935 struct componentname *a_fcnp;
936 struct vnode *a_tdvp;
937 struct vnode *a_tvp;
938 struct componentname *a_tcnp;
939 } */ *ap;
940{
941 struct vnode *tdvp = ap->a_tdvp;
942 struct vnode *fvp = ap->a_fvp;
943 struct vnode *fdvp = ap->a_fdvp;
944 struct vnode *tvp = ap->a_tvp;
945 struct componentname *tcnp = ap->a_tcnp;
946 struct componentname *fcnp = ap->a_fcnp;
947 struct proc *p = fcnp->cn_proc;
948 struct denode *ip, *xp, *dp, *zp;
949 u_char toname[11], oldname[11];
950 u_long from_diroffset, to_diroffset;
951 u_char to_count;
952 int doingdirectory = 0, newparent = 0;
953 int error;
954 u_long cn;
955 daddr_t bn;
956 struct denode *fddep; /* from file's parent directory */
957 struct denode *fdep; /* from file or directory */
958 struct denode *tddep; /* to file's parent directory */
959 struct denode *tdep; /* to file or directory */
960 struct msdosfsmount *pmp;
961 struct direntry *dotdotp;
962 struct buf *bp;
963
964 fddep = VTODE(ap->a_fdvp);
965 fdep = VTODE(ap->a_fvp);
966 tddep = VTODE(ap->a_tdvp);
967 tdep = tvp ? VTODE(tvp) : NULL;
968 pmp = fddep->de_pmp;
969
970 pmp = VFSTOMSDOSFS(fdvp->v_mount);
971
972#ifdef DIAGNOSTIC
973 if ((tcnp->cn_flags & HASBUF) == 0 ||
974 (fcnp->cn_flags & HASBUF) == 0)
975 panic("msdosfs_rename: no name");
976#endif
977 /*
978 * Check for cross-device rename.
979 */
980 if ((fvp->v_mount != tdvp->v_mount) ||
981 (tvp && (fvp->v_mount != tvp->v_mount))) {
982 error = EXDEV;
983abortit:
984 if (tdvp == tvp)
985 vrele(tdvp);
986 else
987 vput(tdvp);
988 if (tvp)
989 vput(tvp);
990 vrele(fdvp);
991 vrele(fvp);
992 return (error);
993 }
994
995 /*
996 * If source and dest are the same, do nothing.
997 */
998 if (tvp == fvp) {
999 error = 0;
1000 goto abortit;
1001 }
1002
1003 error = vn_lock(fvp, LK_EXCLUSIVE, p);
1004 if (error)
1005 goto abortit;
1006 dp = VTODE(fdvp);
1007 ip = VTODE(fvp);
1008
1009 /*
1010 * Be sure we are not renaming ".", "..", or an alias of ".". This
1011 * leads to a crippled directory tree. It's pretty tough to do a
1012 * "ls" or "pwd" with the "." directory entry missing, and "cd .."
1013 * doesn't work if the ".." entry is missing.
1014 */
1015 if (ip->de_Attributes & ATTR_DIRECTORY) {
1016 /*
1017 * Avoid ".", "..", and aliases of "." for obvious reasons.
1018 */
1019 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1020 dp == ip ||
1021 (fcnp->cn_flags & ISDOTDOT) ||
1022 (tcnp->cn_flags & ISDOTDOT) ||
1023 (ip->de_flag & DE_RENAME)) {
1024 VOP_UNLOCK(fvp, 0, p);
1025 error = EINVAL;
1026 goto abortit;
1027 }
1028 ip->de_flag |= DE_RENAME;
1029 doingdirectory++;
1030 }
1031
1032 /*
1033 * When the target exists, both the directory
1034 * and target vnodes are returned locked.
1035 */
1036 dp = VTODE(tdvp);
1037 xp = tvp ? VTODE(tvp) : NULL;
1038 /*
1039 * Remember direntry place to use for destination
1040 */
1041 to_diroffset = dp->de_fndoffset;
1042 to_count = dp->de_fndcnt;
1043
1044 /*
1045 * If ".." must be changed (ie the directory gets a new
1046 * parent) then the source directory must not be in the
1047 * directory heirarchy above the target, as this would
1048 * orphan everything below the source directory. Also
1049 * the user must have write permission in the source so
1050 * as to be able to change "..". We must repeat the call
1051 * to namei, as the parent directory is unlocked by the
1052 * call to doscheckpath().
1053 */
1054 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
1055 VOP_UNLOCK(fvp, 0, p);
1056 if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
1057 newparent = 1;
1058 if (doingdirectory && newparent) {
1059 if (error) /* write access check above */
1060 goto bad;
1061 if (xp != NULL)
1062 vput(tvp);
1063 /*
1064 * doscheckpath() vput()'s dp,
1065 * so we have to do a relookup afterwards
1066 */
1067 error = doscheckpath(ip, dp);
1068 if (error)
1069 goto out;
1070 if ((tcnp->cn_flags & SAVESTART) == 0)
1071 panic("msdosfs_rename: lost to startdir");
1072 error = relookup(tdvp, &tvp, tcnp);
1073 if (error)
1074 goto out;
1075 dp = VTODE(tdvp);
1076 xp = tvp ? VTODE(tvp) : NULL;
1077 }
1078
1079 if (xp != NULL) {
1080 /*
1081 * Target must be empty if a directory and have no links
1082 * to it. Also, ensure source and target are compatible
1083 * (both directories, or both not directories).
1084 */
1085 if (xp->de_Attributes & ATTR_DIRECTORY) {
1086 if (!dosdirempty(xp)) {
1087 error = ENOTEMPTY;
1088 goto bad;
1089 }
1090 if (!doingdirectory) {
1091 error = ENOTDIR;
1092 goto bad;
1093 }
1094 cache_purge(tdvp);
1095 } else if (doingdirectory) {
1096 error = EISDIR;
1097 goto bad;
1098 }
1099 error = removede(dp, xp);
1100 if (error)
1101 goto bad;
1102 vput(tvp);
1103 xp = NULL;
1104 }
1105
1106 /*
1107 * Convert the filename in tcnp into a dos filename. We copy this
1108 * into the denode and directory entry for the destination
1109 * file/directory.
1110 */
1111 error = uniqdosname(VTODE(tdvp), tcnp, toname);
1112 if (error)
1113 goto abortit;
1114
1115 /*
1116 * Since from wasn't locked at various places above,
1117 * have to do a relookup here.
1118 */
1119 fcnp->cn_flags &= ~MODMASK;
1120 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1121 if ((fcnp->cn_flags & SAVESTART) == 0)
1122 panic("msdosfs_rename: lost from startdir");
1123 if (!newparent)
1124 VOP_UNLOCK(tdvp, 0, p);
1125 if (relookup(fdvp, &fvp, fcnp) == 0)
1126 vrele(fdvp);
1127 if (fvp == NULL) {
1128 /*
1129 * From name has disappeared.
1130 */
1131 if (doingdirectory)
1132 panic("rename: lost dir entry");
1133 vrele(ap->a_fvp);
1134 if (newparent)
1135 VOP_UNLOCK(tdvp, 0, p);
1136 vrele(tdvp);
1137 return 0;
1138 }
1139 xp = VTODE(fvp);
1140 zp = VTODE(fdvp);
1141 from_diroffset = zp->de_fndoffset;
1142
1143 /*
1144 * Ensure that the directory entry still exists and has not
1145 * changed till now. If the source is a file the entry may
1146 * have been unlinked or renamed. In either case there is
1147 * no further work to be done. If the source is a directory
1148 * then it cannot have been rmdir'ed or renamed; this is
1149 * prohibited by the DE_RENAME flag.
1150 */
1151 if (xp != ip) {
1152 if (doingdirectory)
1153 panic("rename: lost dir entry");
1154 vrele(ap->a_fvp);
1155 VOP_UNLOCK(fvp, 0, p);
1156 if (newparent)
1157 VOP_UNLOCK(fdvp, 0, p);
1158 xp = NULL;
1159 } else {
1160 vrele(fvp);
1161 xp = NULL;
1162
1163 /*
1164 * First write a new entry in the destination
1165 * directory and mark the entry in the source directory
1166 * as deleted. Then move the denode to the correct hash
1167 * chain for its new location in the filesystem. And, if
1168 * we moved a directory, then update its .. entry to point
1169 * to the new parent directory.
1170 */
1171 bcopy(ip->de_Name, oldname, 11);
1172 bcopy(toname, ip->de_Name, 11); /* update denode */
1173 dp->de_fndoffset = to_diroffset;
1174 dp->de_fndcnt = to_count;
1175 error = createde(ip, dp, (struct denode **)0, tcnp);
1176 if (error) {
1177 bcopy(oldname, ip->de_Name, 11);
1178 if (newparent)
1179 VOP_UNLOCK(fdvp, 0, p);
1180 VOP_UNLOCK(fvp, 0, p);
1181 goto bad;
1182 }
1183 ip->de_refcnt++;
1184 zp->de_fndoffset = from_diroffset;
1185 error = removede(zp, ip);
1186 if (error) {
1187 /* XXX should really panic here, fs is corrupt */
1188 if (newparent)
1189 VOP_UNLOCK(fdvp, 0, p);
1190 VOP_UNLOCK(fvp, 0, p);
1191 goto bad;
1192 }
1193 if (!doingdirectory) {
1194 error = pcbmap(dp, de_cluster(pmp, to_diroffset), 0,
1195 &ip->de_dirclust, 0);
1196 if (error) {
1197 /* XXX should really panic here, fs is corrupt */
1198 if (newparent)
1199 VOP_UNLOCK(fdvp, 0, p);
1200 VOP_UNLOCK(fvp, 0, p);
1201 goto bad;
1202 }
1203 if (ip->de_dirclust == MSDOSFSROOT)
1204 ip->de_diroffset = to_diroffset;
1205 else
1206 ip->de_diroffset = to_diroffset & pmp->pm_crbomask;
1207 }
1208 reinsert(ip);
1209 if (newparent)
1210 VOP_UNLOCK(fdvp, 0, p);
1211 }
1212
1213 /*
1214 * If we moved a directory to a new parent directory, then we must
1215 * fixup the ".." entry in the moved directory.
1216 */
1217 if (doingdirectory && newparent) {
1218 cn = ip->de_StartCluster;
1219 if (cn == MSDOSFSROOT) {
1220 /* this should never happen */
1221 panic("msdosfs_rename(): updating .. in root directory?");
1222 } else
1223 bn = cntobn(pmp, cn);
1224 error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster,
1225 NOCRED, &bp);
1226 if (error) {
1227 /* XXX should really panic here, fs is corrupt */
1228 brelse(bp);
1229 VOP_UNLOCK(fvp, 0, p);
1230 goto bad;
1231 }
1232 dotdotp = (struct direntry *)bp->b_data + 1;
1233 putushort(dotdotp->deStartCluster, dp->de_StartCluster);
1234 if (FAT32(pmp))
1235 putushort(dotdotp->deHighClust, dp->de_StartCluster >> 16);
1236 error = bwrite(bp);
1237 if (error) {
1238 /* XXX should really panic here, fs is corrupt */
1239 VOP_UNLOCK(fvp, 0, p);
1240 goto bad;
1241 }
1242 }
1243
1244 VOP_UNLOCK(fvp, 0, p);
1245bad:
1246 if (xp)
1247 vput(tvp);
1248 vput(tdvp);
1249out:
1250 ip->de_flag &= ~DE_RENAME;
1251 vrele(fdvp);
1252 vrele(fvp);
1253 return (error);
1254
1255}
1256
1257static struct {
1258 struct direntry dot;
1259 struct direntry dotdot;
1260} dosdirtemplate = {
1261 { ". ", " ", /* the . entry */
1262 ATTR_DIRECTORY, /* file attribute */
1263 0, /* reserved */
1264 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1265 { 0, 0 }, /* access date */
1266 { 0, 0 }, /* high bits of start cluster */
1267 { 210, 4 }, { 210, 4 }, /* modify time & date */
1268 { 0, 0 }, /* startcluster */
1269 { 0, 0, 0, 0 } /* filesize */
1270 },
1271 { ".. ", " ", /* the .. entry */
1272 ATTR_DIRECTORY, /* file attribute */
1273 0, /* reserved */
1274 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1275 { 0, 0 }, /* access date */
1276 { 0, 0 }, /* high bits of start cluster */
1277 { 210, 4 }, { 210, 4 }, /* modify time & date */
1278 { 0, 0 }, /* startcluster */
1279 { 0, 0, 0, 0 } /* filesize */
1280 }
1281};
1282
1283static int
1284msdosfs_mkdir(ap)
1285 struct vop_mkdir_args /* {
1286 struct vnode *a_dvp;
1287 struvt vnode **a_vpp;
1288 struvt componentname *a_cnp;
1289 struct vattr *a_vap;
1290 } */ *ap;
1291{
1292 struct componentname *cnp = ap->a_cnp;
1293 struct denode *dep;
1294 struct denode *pdep = VTODE(ap->a_dvp);
1295 struct direntry *denp;
1296 struct msdosfsmount *pmp = pdep->de_pmp;
1297 struct buf *bp;
1298 u_long newcluster, pcl;
1299 int bn;
1300 int error;
1301 struct denode ndirent;
1302 struct timespec ts;
1303
1304 /*
1305 * If this is the root directory and there is no space left we
1306 * can't do anything. This is because the root directory can not
1307 * change size.
1308 */
1309 if (pdep->de_StartCluster == MSDOSFSROOT
1310 && pdep->de_fndoffset >= pdep->de_FileSize) {
1311 error = ENOSPC;
1312 goto bad2;
1313 }
1314
1315 /*
1316 * Allocate a cluster to hold the about to be created directory.
1317 */
1318 error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
1319 if (error)
1320 goto bad2;
1321
1322 bzero(&ndirent, sizeof(ndirent));
1323 ndirent.de_pmp = pmp;
1324 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1325 getnanotime(&ts);
1326 DETIMES(&ndirent, &ts, &ts, &ts);
1327
1328 /*
1329 * Now fill the cluster with the "." and ".." entries. And write
1330 * the cluster to disk. This way it is there for the parent
1331 * directory to be pointing at if there were a crash.
1332 */
1333 bn = cntobn(pmp, newcluster);
1334 /* always succeeds */
1335 bp = getblk(pmp->pm_devvp, bn, pmp->pm_bpcluster, 0, 0);
1336 bzero(bp->b_data, pmp->pm_bpcluster);
1337 bcopy(&dosdirtemplate, bp->b_data, sizeof dosdirtemplate);
1338 denp = (struct direntry *)bp->b_data;
1339 putushort(denp[0].deStartCluster, newcluster);
1340 putushort(denp[0].deCDate, ndirent.de_CDate);
1341 putushort(denp[0].deCTime, ndirent.de_CTime);
1342 denp[0].deCHundredth = ndirent.de_CHun;
1343 putushort(denp[0].deADate, ndirent.de_ADate);
1344 putushort(denp[0].deMDate, ndirent.de_MDate);
1345 putushort(denp[0].deMTime, ndirent.de_MTime);
1346 pcl = pdep->de_StartCluster;
1347 if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1348 pcl = 0;
1349 putushort(denp[1].deStartCluster, pcl);
1350 putushort(denp[1].deCDate, ndirent.de_CDate);
1351 putushort(denp[1].deCTime, ndirent.de_CTime);
1352 denp[1].deCHundredth = ndirent.de_CHun;
1353 putushort(denp[1].deADate, ndirent.de_ADate);
1354 putushort(denp[1].deMDate, ndirent.de_MDate);
1355 putushort(denp[1].deMTime, ndirent.de_MTime);
1356 if (FAT32(pmp)) {
1357 putushort(denp[0].deHighClust, newcluster >> 16);
1358 putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
1359 }
1360
1361 error = bwrite(bp);
1362 if (error)
1363 goto bad;
1364
1365 /*
1366 * Now build up a directory entry pointing to the newly allocated
1367 * cluster. This will be written to an empty slot in the parent
1368 * directory.
1369 */
1370#ifdef DIAGNOSTIC
1371 if ((cnp->cn_flags & HASBUF) == 0)
1372 panic("msdosfs_mkdir: no name");
1373#endif
1374 error = uniqdosname(pdep, cnp, ndirent.de_Name);
1375 if (error)
1376 goto bad;
1377
1378 ndirent.de_Attributes = ATTR_DIRECTORY;
1379 ndirent.de_LowerCase = 0;
1380 ndirent.de_StartCluster = newcluster;
1381 ndirent.de_FileSize = 0;
1382 ndirent.de_dev = pdep->de_dev;
1383 ndirent.de_devvp = pdep->de_devvp;
1384 error = createde(&ndirent, pdep, &dep, cnp);
1385 if (error)
1386 goto bad;
1387 *ap->a_vpp = DETOV(dep);
1388 return (0);
1389
1390bad:
1391 clusterfree(pmp, newcluster, NULL);
1392bad2:
1393 return (error);
1394}
1395
1396static int
1397msdosfs_rmdir(ap)
1398 struct vop_rmdir_args /* {
1399 struct vnode *a_dvp;
1400 struct vnode *a_vp;
1401 struct componentname *a_cnp;
1402 } */ *ap;
1403{
1404 register struct vnode *vp = ap->a_vp;
1405 register struct vnode *dvp = ap->a_dvp;
1406 register struct componentname *cnp = ap->a_cnp;
1407 register struct denode *ip, *dp;
1408 struct proc *p = cnp->cn_proc;
1409 int error;
1410
1411 ip = VTODE(vp);
1412 dp = VTODE(dvp);
1413
1414 /*
1415 * Verify the directory is empty (and valid).
1416 * (Rmdir ".." won't be valid since
1417 * ".." will contain a reference to
1418 * the current directory and thus be
1419 * non-empty.)
1420 */
1421 error = 0;
1422 if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
1423 error = ENOTEMPTY;
1424 goto out;
1425 }
1426 /*
1427 * Delete the entry from the directory. For dos filesystems this
1428 * gets rid of the directory entry on disk, the in memory copy
1429 * still exists but the de_refcnt is <= 0. This prevents it from
1430 * being found by deget(). When the vput() on dep is done we give
1431 * up access and eventually msdosfs_reclaim() will be called which
1432 * will remove it from the denode cache.
1433 */
1434 error = removede(dp, ip);
1435 if (error)
1436 goto out;
1437 /*
1438 * This is where we decrement the link count in the parent
1439 * directory. Since dos filesystems don't do this we just purge
1440 * the name cache.
1441 */
1442 cache_purge(dvp);
1443 VOP_UNLOCK(dvp, 0, p);
1444 /*
1445 * Truncate the directory that is being deleted.
1446 */
1447 error = detrunc(ip, (u_long)0, IO_SYNC, cnp->cn_cred, p);
1448 cache_purge(vp);
1449
1450 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p);
1451out:
1452 return (error);
1453}
1454
1455/*
1456 * DOS filesystems don't know what symlinks are.
1457 */
1458static int
1459msdosfs_symlink(ap)
1460 struct vop_symlink_args /* {
1461 struct vnode *a_dvp;
1462 struct vnode **a_vpp;
1463 struct componentname *a_cnp;
1464 struct vattr *a_vap;
1465 char *a_target;
1466 } */ *ap;
1467{
1468 return (EOPNOTSUPP);
1469}
1470
1471static int
1472msdosfs_readdir(ap)
1473 struct vop_readdir_args /* {
1474 struct vnode *a_vp;
1475 struct uio *a_uio;
1476 struct ucred *a_cred;
1477 int *a_eofflag;
1478 int *a_ncookies;
1479 u_long **a_cookies;
1480 } */ *ap;
1481{
1482 int error = 0;
1483 int diff;
1484 long n;
1485 int blsize;
1486 long on;
1487 u_long cn;
1488 u_long fileno;
1489 u_long dirsperblk;
1490 long bias = 0;
1491 daddr_t bn, lbn;
1492 struct buf *bp;
1493 struct denode *dep = VTODE(ap->a_vp);
1494 struct msdosfsmount *pmp = dep->de_pmp;
1495 struct direntry *dentp;
1496 struct dirent dirbuf;
1497 struct uio *uio = ap->a_uio;
1498 u_long *cookies = NULL;
1499 int ncookies = 0;
1500 off_t offset, off;
1501 int chksum = -1;
1502
1503#ifdef MSDOSFS_DEBUG
1504 printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1505 ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1506#endif
1507
1508 /*
1509 * msdosfs_readdir() won't operate properly on regular files since
1510 * it does i/o only with the the filesystem vnode, and hence can
1511 * retrieve the wrong block from the buffer cache for a plain file.
1512 * So, fail attempts to readdir() on a plain file.
1513 */
1514 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
1515 return (ENOTDIR);
1516
1517 /*
1518 * To be safe, initialize dirbuf
1519 */
1520 bzero(dirbuf.d_name, sizeof(dirbuf.d_name));
1521
1522 /*
1523 * If the user buffer is smaller than the size of one dos directory
1524 * entry or the file offset is not a multiple of the size of a
1525 * directory entry, then we fail the read.
1526 */
1527 off = offset = uio->uio_offset;
1528 if (uio->uio_resid < sizeof(struct direntry) ||
1529 (offset & (sizeof(struct direntry) - 1)))
1530 return (EINVAL);
1531
1532 if (ap->a_ncookies) {
1533 ncookies = uio->uio_resid / 16;
1534 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1535 M_WAITOK);
1536 *ap->a_cookies = cookies;
1537 *ap->a_ncookies = ncookies;
1538 }
1539
1540 dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1541
1542 /*
1543 * If they are reading from the root directory then, we simulate
1544 * the . and .. entries since these don't exist in the root
1545 * directory. We also set the offset bias to make up for having to
1546 * simulate these entries. By this I mean that at file offset 64 we
1547 * read the first entry in the root directory that lives on disk.
1548 */
1549 if (dep->de_StartCluster == MSDOSFSROOT
1550 || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1551#if 0
1552 printf("msdosfs_readdir(): going after . or .. in root dir, offset %d\n",
1553 offset);
1554#endif
1555 bias = 2 * sizeof(struct direntry);
1556 if (offset < bias) {
1557 for (n = (int)offset / sizeof(struct direntry);
1558 n < 2; n++) {
1559 if (FAT32(pmp))
1560 dirbuf.d_fileno = cntobn(pmp,
1561 pmp->pm_rootdirblk)
1562 * dirsperblk;
1563 else
1564 dirbuf.d_fileno = 1;
1565 dirbuf.d_type = DT_DIR;
1566 switch (n) {
1567 case 0:
1568 dirbuf.d_namlen = 1;
1569 strcpy(dirbuf.d_name, ".");
1570 break;
1571 case 1:
1572 dirbuf.d_namlen = 2;
1573 strcpy(dirbuf.d_name, "..");
1574 break;
1575 }
1576 dirbuf.d_reclen = GENERIC_DIRSIZ(&dirbuf);
1577 if (uio->uio_resid < dirbuf.d_reclen)
1578 goto out;
1579 error = uiomove((caddr_t) &dirbuf,
1580 dirbuf.d_reclen, uio);
1581 if (error)
1582 goto out;
1583 offset += sizeof(struct direntry);
1584 off = offset;
1585 if (cookies) {
1586 *cookies++ = offset;
1587 if (--ncookies <= 0)
1588 goto out;
1589 }
1590 }
1591 }
1592 }
1593
1594 off = offset;
1595 while (uio->uio_resid > 0) {
1596 lbn = de_cluster(pmp, offset - bias);
1597 on = (offset - bias) & pmp->pm_crbomask;
1598 n = min(pmp->pm_bpcluster - on, uio->uio_resid);
1599 diff = dep->de_FileSize - (offset - bias);
1600 if (diff <= 0)
1601 break;
1602 n = min(n, diff);
1603 error = pcbmap(dep, lbn, &bn, &cn, &blsize);
1604 if (error)
1605 break;
1606 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
1607 if (error) {
1608 brelse(bp);
1609 return (error);
1610 }
1611 n = min(n, blsize - bp->b_resid);
1612
1613 /*
1614 * Convert from dos directory entries to fs-independent
1615 * directory entries.
1616 */
1617 for (dentp = (struct direntry *)(bp->b_data + on);
1618 (char *)dentp < bp->b_data + on + n;
1619 dentp++, offset += sizeof(struct direntry)) {
1620#if 0
1621 printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1622 dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1623#endif
1624 /*
1625 * If this is an unused entry, we can stop.
1626 */
1627 if (dentp->deName[0] == SLOT_EMPTY) {
1628 brelse(bp);
1629 goto out;
1630 }
1631 /*
1632 * Skip deleted entries.
1633 */
1634 if (dentp->deName[0] == SLOT_DELETED) {
1635 chksum = -1;
1636 continue;
1637 }
1638
1639 /*
1640 * Handle Win95 long directory entries
1641 */
1642 if (dentp->deAttributes == ATTR_WIN95) {
1643 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1644 continue;
1645 chksum = win2unixfn((struct winentry *)dentp,
1646 &dirbuf, chksum,
1647 pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
1648 pmp->pm_u2w);
1649 continue;
1650 }
1651
1652 /*
1653 * Skip volume labels
1654 */
1655 if (dentp->deAttributes & ATTR_VOLUME) {
1656 chksum = -1;
1657 continue;
1658 }
1659 /*
1660 * This computation of d_fileno must match
1661 * the computation of va_fileid in
1662 * msdosfs_getattr.
1663 */
1664 if (dentp->deAttributes & ATTR_DIRECTORY) {
1665 fileno = getushort(dentp->deStartCluster);
1666 if (FAT32(pmp))
1667 fileno |= getushort(dentp->deHighClust) << 16;
1668 /* if this is the root directory */
1669 if (fileno == MSDOSFSROOT)
1670 if (FAT32(pmp))
1671 fileno = cntobn(pmp,
1672 pmp->pm_rootdirblk)
1673 * dirsperblk;
1674 else
1675 fileno = 1;
1676 else
1677 fileno = cntobn(pmp, fileno) * dirsperblk;
1678 dirbuf.d_fileno = fileno;
1679 dirbuf.d_type = DT_DIR;
1680 } else {
1681 dirbuf.d_fileno = offset / sizeof(struct direntry);
1682 dirbuf.d_type = DT_REG;
1683 }
1684 if (chksum != winChksum(dentp->deName))
1685 dirbuf.d_namlen = dos2unixfn(dentp->deName,
1686 (u_char *)dirbuf.d_name,
1687 dentp->deLowerCase |
1688 ((pmp->pm_flags & MSDOSFSMNT_SHORTNAME) ?
1689 (LCASE_BASE | LCASE_EXT) : 0),
1690 pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
1691 pmp->pm_d2u,
1692 pmp->pm_flags & MSDOSFSMNT_ULTABLE,
1693 pmp->pm_ul);
1694 else
1695 dirbuf.d_name[dirbuf.d_namlen] = 0;
1696 chksum = -1;
1697 dirbuf.d_reclen = GENERIC_DIRSIZ(&dirbuf);
1698 if (uio->uio_resid < dirbuf.d_reclen) {
1699 brelse(bp);
1700 goto out;
1701 }
1702 error = uiomove((caddr_t) &dirbuf,
1703 dirbuf.d_reclen, uio);
1704 if (error) {
1705 brelse(bp);
1706 goto out;
1707 }
1708 if (cookies) {
1709 *cookies++ = offset + sizeof(struct direntry);
1710 if (--ncookies <= 0) {
1711 brelse(bp);
1712 goto out;
1713 }
1714 }
1715 off = offset + sizeof(struct direntry);
1716 }
1717 brelse(bp);
1718 }
1719out:
1720 /* Subtract unused cookies */
1721 if (ap->a_ncookies)
1722 *ap->a_ncookies -= ncookies;
1723
1724 uio->uio_offset = off;
1725
1726 /*
1727 * Set the eofflag (NFS uses it)
1728 */
1729 if (ap->a_eofflag) {
1730 if (dep->de_FileSize - (offset - bias) <= 0)
1731 *ap->a_eofflag = 1;
1732 else
1733 *ap->a_eofflag = 0;
1734 }
1735 return (error);
1736}
1737
1738/*
1739 * vp - address of vnode file the file
1740 * bn - which cluster we are interested in mapping to a filesystem block number.
1741 * vpp - returns the vnode for the block special file holding the filesystem
1742 * containing the file of interest
1743 * bnp - address of where to return the filesystem relative block number
1744 */
1745static int
1746msdosfs_bmap(ap)
1747 struct vop_bmap_args /* {
1748 struct vnode *a_vp;
1749 daddr_t a_bn;
1750 struct vnode **a_vpp;
1751 daddr_t *a_bnp;
1752 int *a_runp;
1753 int *a_runb;
1754 } */ *ap;
1755{
1756 struct denode *dep = VTODE(ap->a_vp);
1757
1758 if (ap->a_vpp != NULL)
1759 *ap->a_vpp = dep->de_devvp;
1760 if (ap->a_bnp == NULL)
1761 return (0);
1762 if (ap->a_runp) {
1763 /*
1764 * Sequential clusters should be counted here.
1765 */
1766 *ap->a_runp = 0;
1767 }
1768 if (ap->a_runb) {
1769 *ap->a_runb = 0;
1770 }
1771 return (pcbmap(dep, ap->a_bn, ap->a_bnp, 0, 0));
1772}
1773
1774static int
1775msdosfs_strategy(ap)
1776 struct vop_strategy_args /* {
1777 struct vnode *a_vp;
1778 struct buf *a_bp;
1779 } */ *ap;
1780{
1781 struct buf *bp = ap->a_bp;
1782 struct denode *dep = VTODE(bp->b_vp);
1783 struct vnode *vp;
1784 int error = 0;
1785
1786 if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR)
1787 panic("msdosfs_strategy: spec");
1788 /*
1789 * If we don't already know the filesystem relative block number
1790 * then get it using pcbmap(). If pcbmap() returns the block
1791 * number as -1 then we've got a hole in the file. DOS filesystems
1792 * don't allow files with holes, so we shouldn't ever see this.
1793 */
1794 if (bp->b_blkno == bp->b_lblkno) {
1795 error = pcbmap(dep, bp->b_lblkno, &bp->b_blkno, 0, 0);
1796 if (error) {
1797 bp->b_error = error;
1798 bp->b_ioflags |= BIO_ERROR;
1799 bufdone(bp);
1800 return (error);
1801 }
1802 if ((long)bp->b_blkno == -1)
1803 vfs_bio_clrbuf(bp);
1804 }
1805 if (bp->b_blkno == -1) {
1806 bufdone(bp);
1807 return (0);
1808 }
1809 /*
1810 * Read/write the block from/to the disk that contains the desired
1811 * file block.
1812 */
1813 vp = dep->de_devvp;
1814 bp->b_dev = vp->v_rdev;
1815 VOP_STRATEGY(vp, bp);
1816 return (0);
1817}
1818
1819static int
1820msdosfs_print(ap)
1821 struct vop_print_args /* {
1822 struct vnode *vp;
1823 } */ *ap;
1824{
1825 struct denode *dep = VTODE(ap->a_vp);
1826
1827 printf(
1828 "tag VT_MSDOSFS, startcluster %lu, dircluster %lu, diroffset %lu ",
1829 dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1830 printf(" dev %d, %d", major(dep->de_dev), minor(dep->de_dev));
1831 lockmgr_printinfo(&ap->a_vp->v_lock);
1832 printf("\n");
1833 return (0);
1834}
1835
1836static int
1837msdosfs_pathconf(ap)
1838 struct vop_pathconf_args /* {
1839 struct vnode *a_vp;
1840 int a_name;
1841 int *a_retval;
1842 } */ *ap;
1843{
1844 struct msdosfsmount *pmp = VTODE(ap->a_vp)->de_pmp;
1845
1846 switch (ap->a_name) {
1847 case _PC_LINK_MAX:
1848 *ap->a_retval = 1;
1849 return (0);
1850 case _PC_NAME_MAX:
1851 *ap->a_retval = pmp->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12;
1852 return (0);
1853 case _PC_PATH_MAX:
1854 *ap->a_retval = PATH_MAX;
1855 return (0);
1856 case _PC_CHOWN_RESTRICTED:
1857 *ap->a_retval = 1;
1858 return (0);
1859 case _PC_NO_TRUNC:
1860 *ap->a_retval = 0;
1861 return (0);
1862 default:
1863 return (EINVAL);
1864 }
1865 /* NOTREACHED */
1866}
1867
1868/*
1869 * get page routine
1870 *
1871 * XXX By default, wimp out... note that a_offset is ignored (and always
1872 * XXX has been).
1873 */
1874int
1875msdosfs_getpages(ap)
1876 struct vop_getpages_args *ap;
1877{
1878 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
1879 ap->a_reqpage);
1880}
1881
1882/*
1883 * put page routine
1884 *
1885 * XXX By default, wimp out... note that a_offset is ignored (and always
1886 * XXX has been).
1887 */
1888int
1889msdosfs_putpages(ap)
1890 struct vop_putpages_args *ap;
1891{
1892 return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
1893 ap->a_sync, ap->a_rtvals);
1894}
1895
1896/* Global vfs data structures for msdosfs */
1897vop_t **msdosfs_vnodeop_p;
1898static struct vnodeopv_entry_desc msdosfs_vnodeop_entries[] = {
1899 { &vop_default_desc, (vop_t *) vop_defaultop },
1900 { &vop_access_desc, (vop_t *) msdosfs_access },
1901 { &vop_bmap_desc, (vop_t *) msdosfs_bmap },
1902 { &vop_cachedlookup_desc, (vop_t *) msdosfs_lookup },
1903 { &vop_close_desc, (vop_t *) msdosfs_close },
1904 { &vop_create_desc, (vop_t *) msdosfs_create },
1905 { &vop_fsync_desc, (vop_t *) msdosfs_fsync },
1906 { &vop_getattr_desc, (vop_t *) msdosfs_getattr },
1907 { &vop_inactive_desc, (vop_t *) msdosfs_inactive },
1908 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
1909 { &vop_link_desc, (vop_t *) msdosfs_link },
1910 { &vop_lock_desc, (vop_t *) vop_stdlock },
1911 { &vop_lookup_desc, (vop_t *) vfs_cache_lookup },
1912 { &vop_mkdir_desc, (vop_t *) msdosfs_mkdir },
1913 { &vop_mknod_desc, (vop_t *) msdosfs_mknod },
1914 { &vop_pathconf_desc, (vop_t *) msdosfs_pathconf },
1915 { &vop_print_desc, (vop_t *) msdosfs_print },
1916 { &vop_read_desc, (vop_t *) msdosfs_read },
1917 { &vop_readdir_desc, (vop_t *) msdosfs_readdir },
1918 { &vop_reclaim_desc, (vop_t *) msdosfs_reclaim },
1919 { &vop_remove_desc, (vop_t *) msdosfs_remove },
1920 { &vop_rename_desc, (vop_t *) msdosfs_rename },
1921 { &vop_rmdir_desc, (vop_t *) msdosfs_rmdir },
1922 { &vop_setattr_desc, (vop_t *) msdosfs_setattr },
1923 { &vop_strategy_desc, (vop_t *) msdosfs_strategy },
1924 { &vop_symlink_desc, (vop_t *) msdosfs_symlink },
1925 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
1926 { &vop_write_desc, (vop_t *) msdosfs_write },
1927 { &vop_getpages_desc, (vop_t *) msdosfs_getpages },
1928 { &vop_putpages_desc, (vop_t *) msdosfs_putpages },
1929 { NULL, NULL }
1930};
1931static struct vnodeopv_desc msdosfs_vnodeop_opv_desc =
1932 { &msdosfs_vnodeop_p, msdosfs_vnodeop_entries };
1933
1934VNODEOP_SET(msdosfs_vnodeop_opv_desc);
242 return 0;
243}
244
245static int
246msdosfs_access(ap)
247 struct vop_access_args /* {
248 struct vnode *a_vp;
249 int a_mode;
250 struct ucred *a_cred;
251 struct proc *a_p;
252 } */ *ap;
253{
254 struct vnode *vp = ap->a_vp;
255 struct denode *dep = VTODE(ap->a_vp);
256 struct msdosfsmount *pmp = dep->de_pmp;
257 mode_t file_mode, mode = ap->a_mode;
258
259 file_mode = (S_IXUSR|S_IXGRP|S_IXOTH) | (S_IRUSR|S_IRGRP|S_IROTH) |
260 ((dep->de_Attributes & ATTR_READONLY) ? 0 : (S_IWUSR|S_IWGRP|S_IWOTH));
261 file_mode &= pmp->pm_mask;
262
263 /*
264 * Disallow write attempts on read-only file systems;
265 * unless the file is a socket, fifo, or a block or
266 * character device resident on the file system.
267 */
268 if (mode & VWRITE) {
269 switch (vp->v_type) {
270 case VDIR:
271 case VLNK:
272 case VREG:
273 if (vp->v_mount->mnt_flag & MNT_RDONLY)
274 return (EROFS);
275 break;
276 default:
277 break;
278 }
279 }
280
281 return (vaccess(vp->v_type, file_mode, pmp->pm_uid, pmp->pm_gid,
282 ap->a_mode, ap->a_cred, NULL));
283}
284
285static int
286msdosfs_getattr(ap)
287 struct vop_getattr_args /* {
288 struct vnode *a_vp;
289 struct vattr *a_vap;
290 struct ucred *a_cred;
291 struct proc *a_p;
292 } */ *ap;
293{
294 struct denode *dep = VTODE(ap->a_vp);
295 struct msdosfsmount *pmp = dep->de_pmp;
296 struct vattr *vap = ap->a_vap;
297 mode_t mode;
298 struct timespec ts;
299 u_long dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
300 u_long fileid;
301
302 getnanotime(&ts);
303 DETIMES(dep, &ts, &ts, &ts);
304 vap->va_fsid = dev2udev(dep->de_dev);
305 /*
306 * The following computation of the fileid must be the same as that
307 * used in msdosfs_readdir() to compute d_fileno. If not, pwd
308 * doesn't work.
309 */
310 if (dep->de_Attributes & ATTR_DIRECTORY) {
311 fileid = cntobn(pmp, dep->de_StartCluster) * dirsperblk;
312 if (dep->de_StartCluster == MSDOSFSROOT)
313 fileid = 1;
314 } else {
315 fileid = cntobn(pmp, dep->de_dirclust) * dirsperblk;
316 if (dep->de_dirclust == MSDOSFSROOT)
317 fileid = roottobn(pmp, 0) * dirsperblk;
318 fileid += dep->de_diroffset / sizeof(struct direntry);
319 }
320 vap->va_fileid = fileid;
321 if ((dep->de_Attributes & ATTR_READONLY) == 0)
322 mode = S_IRWXU|S_IRWXG|S_IRWXO;
323 else
324 mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
325 vap->va_mode = mode & pmp->pm_mask;
326 vap->va_uid = pmp->pm_uid;
327 vap->va_gid = pmp->pm_gid;
328 vap->va_nlink = 1;
329 vap->va_rdev = 0;
330 vap->va_size = dep->de_FileSize;
331 dos2unixtime(dep->de_MDate, dep->de_MTime, 0, &vap->va_mtime);
332 if (pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
333 dos2unixtime(dep->de_ADate, 0, 0, &vap->va_atime);
334 dos2unixtime(dep->de_CDate, dep->de_CTime, dep->de_CHun, &vap->va_ctime);
335 } else {
336 vap->va_atime = vap->va_mtime;
337 vap->va_ctime = vap->va_mtime;
338 }
339 vap->va_flags = 0;
340 if ((dep->de_Attributes & ATTR_ARCHIVE) == 0)
341 vap->va_flags |= SF_ARCHIVED;
342 vap->va_gen = 0;
343 vap->va_blocksize = pmp->pm_bpcluster;
344 vap->va_bytes =
345 (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask;
346 vap->va_type = ap->a_vp->v_type;
347 vap->va_filerev = dep->de_modrev;
348 return (0);
349}
350
351static int
352msdosfs_setattr(ap)
353 struct vop_setattr_args /* {
354 struct vnode *a_vp;
355 struct vattr *a_vap;
356 struct ucred *a_cred;
357 struct proc *a_p;
358 } */ *ap;
359{
360 struct vnode *vp = ap->a_vp;
361 struct denode *dep = VTODE(ap->a_vp);
362 struct msdosfsmount *pmp = dep->de_pmp;
363 struct vattr *vap = ap->a_vap;
364 struct ucred *cred = ap->a_cred;
365 int error = 0;
366
367#ifdef MSDOSFS_DEBUG
368 printf("msdosfs_setattr(): vp %p, vap %p, cred %p, p %p\n",
369 ap->a_vp, vap, cred, ap->a_p);
370#endif
371
372 /*
373 * Check for unsettable attributes.
374 */
375 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
376 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
377 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
378 (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
379#ifdef MSDOSFS_DEBUG
380 printf("msdosfs_setattr(): returning EINVAL\n");
381 printf(" va_type %d, va_nlink %x, va_fsid %lx, va_fileid %lx\n",
382 vap->va_type, vap->va_nlink, vap->va_fsid, vap->va_fileid);
383 printf(" va_blocksize %lx, va_rdev %x, va_bytes %qx, va_gen %lx\n",
384 vap->va_blocksize, vap->va_rdev, vap->va_bytes, vap->va_gen);
385 printf(" va_uid %x, va_gid %x\n",
386 vap->va_uid, vap->va_gid);
387#endif
388 return (EINVAL);
389 }
390 if (vap->va_flags != VNOVAL) {
391 if (vp->v_mount->mnt_flag & MNT_RDONLY)
392 return (EROFS);
393 if (cred->cr_uid != pmp->pm_uid &&
394 (error = suser_xxx(cred, ap->a_p, PRISON_ROOT)))
395 return (error);
396 /*
397 * We are very inconsistent about handling unsupported
398 * attributes. We ignored the access time and the
399 * read and execute bits. We were strict for the other
400 * attributes.
401 *
402 * Here we are strict, stricter than ufs in not allowing
403 * users to attempt to set SF_SETTABLE bits or anyone to
404 * set unsupported bits. However, we ignore attempts to
405 * set ATTR_ARCHIVE for directories `cp -pr' from a more
406 * sensible file system attempts it a lot.
407 */
408 if (cred->cr_uid != 0) {
409 if (vap->va_flags & SF_SETTABLE)
410 return EPERM;
411 }
412 if (vap->va_flags & ~SF_ARCHIVED)
413 return EOPNOTSUPP;
414 if (vap->va_flags & SF_ARCHIVED)
415 dep->de_Attributes &= ~ATTR_ARCHIVE;
416 else if (!(dep->de_Attributes & ATTR_DIRECTORY))
417 dep->de_Attributes |= ATTR_ARCHIVE;
418 dep->de_flag |= DE_MODIFIED;
419 }
420
421 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
422 uid_t uid;
423 gid_t gid;
424
425 if (vp->v_mount->mnt_flag & MNT_RDONLY)
426 return (EROFS);
427 uid = vap->va_uid;
428 if (uid == (uid_t)VNOVAL)
429 uid = pmp->pm_uid;
430 gid = vap->va_gid;
431 if (gid == (gid_t)VNOVAL)
432 gid = pmp->pm_gid;
433 if ((cred->cr_uid != pmp->pm_uid || uid != pmp->pm_uid ||
434 (gid != pmp->pm_gid && !groupmember(gid, cred))) &&
435 (error = suser_xxx(cred, ap->a_p, PRISON_ROOT)))
436 return error;
437 if (uid != pmp->pm_uid || gid != pmp->pm_gid)
438 return EINVAL;
439 }
440
441 if (vap->va_size != VNOVAL) {
442 /*
443 * Disallow write attempts on read-only file systems;
444 * unless the file is a socket, fifo, or a block or
445 * character device resident on the file system.
446 */
447 switch (vp->v_type) {
448 case VDIR:
449 return (EISDIR);
450 /* NOT REACHED */
451 case VLNK:
452 case VREG:
453 if (vp->v_mount->mnt_flag & MNT_RDONLY)
454 return (EROFS);
455 break;
456 default:
457 break;
458 }
459 error = detrunc(dep, vap->va_size, 0, cred, ap->a_p);
460 if (error)
461 return error;
462 }
463 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
464 if (vp->v_mount->mnt_flag & MNT_RDONLY)
465 return (EROFS);
466 if (cred->cr_uid != pmp->pm_uid &&
467 (error = suser_xxx(cred, ap->a_p, PRISON_ROOT)) &&
468 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
469 (error = VOP_ACCESS(ap->a_vp, VWRITE, cred, ap->a_p))))
470 return (error);
471 if (vp->v_type != VDIR) {
472 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
473 vap->va_atime.tv_sec != VNOVAL)
474 unix2dostime(&vap->va_atime, &dep->de_ADate, NULL, NULL);
475 if (vap->va_mtime.tv_sec != VNOVAL)
476 unix2dostime(&vap->va_mtime, &dep->de_MDate, &dep->de_MTime, NULL);
477 dep->de_Attributes |= ATTR_ARCHIVE;
478 dep->de_flag |= DE_MODIFIED;
479 }
480 }
481 /*
482 * DOS files only have the ability to have their writability
483 * attribute set, so we use the owner write bit to set the readonly
484 * attribute.
485 */
486 if (vap->va_mode != (mode_t)VNOVAL) {
487 if (vp->v_mount->mnt_flag & MNT_RDONLY)
488 return (EROFS);
489 if (cred->cr_uid != pmp->pm_uid &&
490 (error = suser_xxx(cred, ap->a_p, PRISON_ROOT)))
491 return (error);
492 if (vp->v_type != VDIR) {
493 /* We ignore the read and execute bits. */
494 if (vap->va_mode & VWRITE)
495 dep->de_Attributes &= ~ATTR_READONLY;
496 else
497 dep->de_Attributes |= ATTR_READONLY;
498 dep->de_flag |= DE_MODIFIED;
499 }
500 }
501 return (deupdat(dep, 1));
502}
503
504static int
505msdosfs_read(ap)
506 struct vop_read_args /* {
507 struct vnode *a_vp;
508 struct uio *a_uio;
509 int a_ioflag;
510 struct ucred *a_cred;
511 } */ *ap;
512{
513 int error = 0;
514 int blsize;
515 int isadir;
516 int orig_resid;
517 u_int n;
518 u_long diff;
519 u_long on;
520 daddr_t lbn;
521 daddr_t rablock;
522 int rasize;
523 int seqcount;
524 struct buf *bp;
525 struct vnode *vp = ap->a_vp;
526 struct denode *dep = VTODE(vp);
527 struct msdosfsmount *pmp = dep->de_pmp;
528 struct uio *uio = ap->a_uio;
529
530 if (uio->uio_offset < 0)
531 return (EINVAL);
532
533 if ((uoff_t)uio->uio_offset > DOS_FILESIZE_MAX)
534 return (0);
535 /*
536 * If they didn't ask for any data, then we are done.
537 */
538 orig_resid = uio->uio_resid;
539 if (orig_resid <= 0)
540 return (0);
541
542 seqcount = ap->a_ioflag >> 16;
543
544 isadir = dep->de_Attributes & ATTR_DIRECTORY;
545 do {
546 if (uio->uio_offset >= dep->de_FileSize)
547 break;
548 lbn = de_cluster(pmp, uio->uio_offset);
549 /*
550 * If we are operating on a directory file then be sure to
551 * do i/o with the vnode for the filesystem instead of the
552 * vnode for the directory.
553 */
554 if (isadir) {
555 /* convert cluster # to block # */
556 error = pcbmap(dep, lbn, &lbn, 0, &blsize);
557 if (error == E2BIG) {
558 error = EINVAL;
559 break;
560 } else if (error)
561 break;
562 error = bread(pmp->pm_devvp, lbn, blsize, NOCRED, &bp);
563 } else {
564 blsize = pmp->pm_bpcluster;
565 rablock = lbn + 1;
566 if (seqcount > 1 &&
567 de_cn2off(pmp, rablock) < dep->de_FileSize) {
568 rasize = pmp->pm_bpcluster;
569 error = breadn(vp, lbn, blsize,
570 &rablock, &rasize, 1, NOCRED, &bp);
571 } else {
572 error = bread(vp, lbn, blsize, NOCRED, &bp);
573 }
574 }
575 if (error) {
576 brelse(bp);
577 break;
578 }
579 on = uio->uio_offset & pmp->pm_crbomask;
580 diff = pmp->pm_bpcluster - on;
581 n = diff > uio->uio_resid ? uio->uio_resid : diff;
582 diff = dep->de_FileSize - uio->uio_offset;
583 if (diff < n)
584 n = diff;
585 diff = blsize - bp->b_resid;
586 if (diff < n)
587 n = diff;
588 error = uiomove(bp->b_data + on, (int) n, uio);
589 brelse(bp);
590 } while (error == 0 && uio->uio_resid > 0 && n != 0);
591 if (!isadir && (error == 0 || uio->uio_resid != orig_resid) &&
592 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
593 dep->de_flag |= DE_ACCESS;
594 return (error);
595}
596
597/*
598 * Write data to a file or directory.
599 */
600static int
601msdosfs_write(ap)
602 struct vop_write_args /* {
603 struct vnode *a_vp;
604 struct uio *a_uio;
605 int a_ioflag;
606 struct ucred *a_cred;
607 } */ *ap;
608{
609 int n;
610 int croffset;
611 int resid;
612 u_long osize;
613 int error = 0;
614 u_long count;
615 daddr_t bn, lastcn;
616 struct buf *bp;
617 int ioflag = ap->a_ioflag;
618 struct uio *uio = ap->a_uio;
619 struct proc *p = uio->uio_procp;
620 struct vnode *vp = ap->a_vp;
621 struct vnode *thisvp;
622 struct denode *dep = VTODE(vp);
623 struct msdosfsmount *pmp = dep->de_pmp;
624 struct ucred *cred = ap->a_cred;
625
626#ifdef MSDOSFS_DEBUG
627 printf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
628 vp, uio, ioflag, cred);
629 printf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n",
630 dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
631#endif
632
633 switch (vp->v_type) {
634 case VREG:
635 if (ioflag & IO_APPEND)
636 uio->uio_offset = dep->de_FileSize;
637 thisvp = vp;
638 break;
639 case VDIR:
640 return EISDIR;
641 default:
642 panic("msdosfs_write(): bad file type");
643 }
644
645 if (uio->uio_offset < 0)
646 return (EFBIG);
647
648 if (uio->uio_resid == 0)
649 return (0);
650
651 /*
652 * If they've exceeded their filesize limit, tell them about it.
653 */
654 if (p &&
655 ((uoff_t)uio->uio_offset + uio->uio_resid >
656 p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
657 psignal(p, SIGXFSZ);
658 return (EFBIG);
659 }
660
661 if ((uoff_t)uio->uio_offset + uio->uio_resid > DOS_FILESIZE_MAX)
662 return (EFBIG);
663
664 /*
665 * If the offset we are starting the write at is beyond the end of
666 * the file, then they've done a seek. Unix filesystems allow
667 * files with holes in them, DOS doesn't so we must fill the hole
668 * with zeroed blocks.
669 */
670 if (uio->uio_offset > dep->de_FileSize) {
671 error = deextend(dep, uio->uio_offset, cred);
672 if (error)
673 return (error);
674 }
675
676 /*
677 * Remember some values in case the write fails.
678 */
679 resid = uio->uio_resid;
680 osize = dep->de_FileSize;
681
682 /*
683 * If we write beyond the end of the file, extend it to its ultimate
684 * size ahead of the time to hopefully get a contiguous area.
685 */
686 if (uio->uio_offset + resid > osize) {
687 count = de_clcount(pmp, uio->uio_offset + resid) -
688 de_clcount(pmp, osize);
689 error = extendfile(dep, count, NULL, NULL, 0);
690 if (error && (error != ENOSPC || (ioflag & IO_UNIT)))
691 goto errexit;
692 lastcn = dep->de_fc[FC_LASTFC].fc_frcn;
693 } else
694 lastcn = de_clcount(pmp, osize) - 1;
695
696 do {
697 if (de_cluster(pmp, uio->uio_offset) > lastcn) {
698 error = ENOSPC;
699 break;
700 }
701
702 croffset = uio->uio_offset & pmp->pm_crbomask;
703 n = min(uio->uio_resid, pmp->pm_bpcluster - croffset);
704 if (uio->uio_offset + n > dep->de_FileSize) {
705 dep->de_FileSize = uio->uio_offset + n;
706 /* The object size needs to be set before buffer is allocated */
707 vnode_pager_setsize(vp, dep->de_FileSize);
708 }
709
710 bn = de_cluster(pmp, uio->uio_offset);
711 if ((uio->uio_offset & pmp->pm_crbomask) == 0
712 && (de_cluster(pmp, uio->uio_offset + uio->uio_resid)
713 > de_cluster(pmp, uio->uio_offset)
714 || uio->uio_offset + uio->uio_resid >= dep->de_FileSize)) {
715 /*
716 * If either the whole cluster gets written,
717 * or we write the cluster from its start beyond EOF,
718 * then no need to read data from disk.
719 */
720 bp = getblk(thisvp, bn, pmp->pm_bpcluster, 0, 0);
721 clrbuf(bp);
722 /*
723 * Do the bmap now, since pcbmap needs buffers
724 * for the fat table. (see msdosfs_strategy)
725 */
726 if (bp->b_blkno == bp->b_lblkno) {
727 error = pcbmap(dep, bp->b_lblkno, &bp->b_blkno,
728 0, 0);
729 if (error)
730 bp->b_blkno = -1;
731 }
732 if (bp->b_blkno == -1) {
733 brelse(bp);
734 if (!error)
735 error = EIO; /* XXX */
736 break;
737 }
738 } else {
739 /*
740 * The block we need to write into exists, so read it in.
741 */
742 error = bread(thisvp, bn, pmp->pm_bpcluster, cred, &bp);
743 if (error) {
744 brelse(bp);
745 break;
746 }
747 }
748
749 /*
750 * Should these vnode_pager_* functions be done on dir
751 * files?
752 */
753
754 /*
755 * Copy the data from user space into the buf header.
756 */
757 error = uiomove(bp->b_data + croffset, n, uio);
758 if (error) {
759 brelse(bp);
760 break;
761 }
762
763 /*
764 * If they want this synchronous then write it and wait for
765 * it. Otherwise, if on a cluster boundary write it
766 * asynchronously so we can move on to the next block
767 * without delay. Otherwise do a delayed write because we
768 * may want to write somemore into the block later.
769 */
770 if (ioflag & IO_SYNC)
771 (void) bwrite(bp);
772 else if (n + croffset == pmp->pm_bpcluster)
773 bawrite(bp);
774 else
775 bdwrite(bp);
776 dep->de_flag |= DE_UPDATE;
777 } while (error == 0 && uio->uio_resid > 0);
778
779 /*
780 * If the write failed and they want us to, truncate the file back
781 * to the size it was before the write was attempted.
782 */
783errexit:
784 if (error) {
785 if (ioflag & IO_UNIT) {
786 detrunc(dep, osize, ioflag & IO_SYNC, NOCRED, NULL);
787 uio->uio_offset -= resid - uio->uio_resid;
788 uio->uio_resid = resid;
789 } else {
790 detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC, NOCRED, NULL);
791 if (uio->uio_resid != resid)
792 error = 0;
793 }
794 } else if (ioflag & IO_SYNC)
795 error = deupdat(dep, 1);
796 return (error);
797}
798
799/*
800 * Flush the blocks of a file to disk.
801 *
802 * This function is worthless for vnodes that represent directories. Maybe we
803 * could just do a sync if they try an fsync on a directory file.
804 */
805static int
806msdosfs_fsync(ap)
807 struct vop_fsync_args /* {
808 struct vnode *a_vp;
809 struct ucred *a_cred;
810 int a_waitfor;
811 struct proc *a_p;
812 } */ *ap;
813{
814 struct vnode *vp = ap->a_vp;
815 int s;
816 struct buf *bp, *nbp;
817
818 /*
819 * Flush all dirty buffers associated with a vnode.
820 */
821loop:
822 s = splbio();
823 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
824 nbp = TAILQ_NEXT(bp, b_vnbufs);
825 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
826 continue;
827 if ((bp->b_flags & B_DELWRI) == 0)
828 panic("msdosfs_fsync: not dirty");
829 bremfree(bp);
830 splx(s);
831 (void) bwrite(bp);
832 goto loop;
833 }
834 while (vp->v_numoutput) {
835 vp->v_flag |= VBWAIT;
836 (void) tsleep((caddr_t)&vp->v_numoutput, PRIBIO + 1, "msdosfsn", 0);
837 }
838#ifdef DIAGNOSTIC
839 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
840 vprint("msdosfs_fsync: dirty", vp);
841 goto loop;
842 }
843#endif
844 splx(s);
845 return (deupdat(VTODE(vp), ap->a_waitfor == MNT_WAIT));
846}
847
848static int
849msdosfs_remove(ap)
850 struct vop_remove_args /* {
851 struct vnode *a_dvp;
852 struct vnode *a_vp;
853 struct componentname *a_cnp;
854 } */ *ap;
855{
856 struct denode *dep = VTODE(ap->a_vp);
857 struct denode *ddep = VTODE(ap->a_dvp);
858 int error;
859
860 if (ap->a_vp->v_type == VDIR)
861 error = EPERM;
862 else
863 error = removede(ddep, dep);
864#ifdef MSDOSFS_DEBUG
865 printf("msdosfs_remove(), dep %p, v_usecount %d\n", dep, ap->a_vp->v_usecount);
866#endif
867 return (error);
868}
869
870/*
871 * DOS filesystems don't know what links are.
872 */
873static int
874msdosfs_link(ap)
875 struct vop_link_args /* {
876 struct vnode *a_tdvp;
877 struct vnode *a_vp;
878 struct componentname *a_cnp;
879 } */ *ap;
880{
881 return (EOPNOTSUPP);
882}
883
884/*
885 * Renames on files require moving the denode to a new hash queue since the
886 * denode's location is used to compute which hash queue to put the file
887 * in. Unless it is a rename in place. For example "mv a b".
888 *
889 * What follows is the basic algorithm:
890 *
891 * if (file move) {
892 * if (dest file exists) {
893 * remove dest file
894 * }
895 * if (dest and src in same directory) {
896 * rewrite name in existing directory slot
897 * } else {
898 * write new entry in dest directory
899 * update offset and dirclust in denode
900 * move denode to new hash chain
901 * clear old directory entry
902 * }
903 * } else {
904 * directory move
905 * if (dest directory exists) {
906 * if (dest is not empty) {
907 * return ENOTEMPTY
908 * }
909 * remove dest directory
910 * }
911 * if (dest and src in same directory) {
912 * rewrite name in existing entry
913 * } else {
914 * be sure dest is not a child of src directory
915 * write entry in dest directory
916 * update "." and ".." in moved directory
917 * clear old directory entry for moved directory
918 * }
919 * }
920 *
921 * On entry:
922 * source's parent directory is unlocked
923 * source file or directory is unlocked
924 * destination's parent directory is locked
925 * destination file or directory is locked if it exists
926 *
927 * On exit:
928 * all denodes should be released
929 */
930static int
931msdosfs_rename(ap)
932 struct vop_rename_args /* {
933 struct vnode *a_fdvp;
934 struct vnode *a_fvp;
935 struct componentname *a_fcnp;
936 struct vnode *a_tdvp;
937 struct vnode *a_tvp;
938 struct componentname *a_tcnp;
939 } */ *ap;
940{
941 struct vnode *tdvp = ap->a_tdvp;
942 struct vnode *fvp = ap->a_fvp;
943 struct vnode *fdvp = ap->a_fdvp;
944 struct vnode *tvp = ap->a_tvp;
945 struct componentname *tcnp = ap->a_tcnp;
946 struct componentname *fcnp = ap->a_fcnp;
947 struct proc *p = fcnp->cn_proc;
948 struct denode *ip, *xp, *dp, *zp;
949 u_char toname[11], oldname[11];
950 u_long from_diroffset, to_diroffset;
951 u_char to_count;
952 int doingdirectory = 0, newparent = 0;
953 int error;
954 u_long cn;
955 daddr_t bn;
956 struct denode *fddep; /* from file's parent directory */
957 struct denode *fdep; /* from file or directory */
958 struct denode *tddep; /* to file's parent directory */
959 struct denode *tdep; /* to file or directory */
960 struct msdosfsmount *pmp;
961 struct direntry *dotdotp;
962 struct buf *bp;
963
964 fddep = VTODE(ap->a_fdvp);
965 fdep = VTODE(ap->a_fvp);
966 tddep = VTODE(ap->a_tdvp);
967 tdep = tvp ? VTODE(tvp) : NULL;
968 pmp = fddep->de_pmp;
969
970 pmp = VFSTOMSDOSFS(fdvp->v_mount);
971
972#ifdef DIAGNOSTIC
973 if ((tcnp->cn_flags & HASBUF) == 0 ||
974 (fcnp->cn_flags & HASBUF) == 0)
975 panic("msdosfs_rename: no name");
976#endif
977 /*
978 * Check for cross-device rename.
979 */
980 if ((fvp->v_mount != tdvp->v_mount) ||
981 (tvp && (fvp->v_mount != tvp->v_mount))) {
982 error = EXDEV;
983abortit:
984 if (tdvp == tvp)
985 vrele(tdvp);
986 else
987 vput(tdvp);
988 if (tvp)
989 vput(tvp);
990 vrele(fdvp);
991 vrele(fvp);
992 return (error);
993 }
994
995 /*
996 * If source and dest are the same, do nothing.
997 */
998 if (tvp == fvp) {
999 error = 0;
1000 goto abortit;
1001 }
1002
1003 error = vn_lock(fvp, LK_EXCLUSIVE, p);
1004 if (error)
1005 goto abortit;
1006 dp = VTODE(fdvp);
1007 ip = VTODE(fvp);
1008
1009 /*
1010 * Be sure we are not renaming ".", "..", or an alias of ".". This
1011 * leads to a crippled directory tree. It's pretty tough to do a
1012 * "ls" or "pwd" with the "." directory entry missing, and "cd .."
1013 * doesn't work if the ".." entry is missing.
1014 */
1015 if (ip->de_Attributes & ATTR_DIRECTORY) {
1016 /*
1017 * Avoid ".", "..", and aliases of "." for obvious reasons.
1018 */
1019 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1020 dp == ip ||
1021 (fcnp->cn_flags & ISDOTDOT) ||
1022 (tcnp->cn_flags & ISDOTDOT) ||
1023 (ip->de_flag & DE_RENAME)) {
1024 VOP_UNLOCK(fvp, 0, p);
1025 error = EINVAL;
1026 goto abortit;
1027 }
1028 ip->de_flag |= DE_RENAME;
1029 doingdirectory++;
1030 }
1031
1032 /*
1033 * When the target exists, both the directory
1034 * and target vnodes are returned locked.
1035 */
1036 dp = VTODE(tdvp);
1037 xp = tvp ? VTODE(tvp) : NULL;
1038 /*
1039 * Remember direntry place to use for destination
1040 */
1041 to_diroffset = dp->de_fndoffset;
1042 to_count = dp->de_fndcnt;
1043
1044 /*
1045 * If ".." must be changed (ie the directory gets a new
1046 * parent) then the source directory must not be in the
1047 * directory heirarchy above the target, as this would
1048 * orphan everything below the source directory. Also
1049 * the user must have write permission in the source so
1050 * as to be able to change "..". We must repeat the call
1051 * to namei, as the parent directory is unlocked by the
1052 * call to doscheckpath().
1053 */
1054 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
1055 VOP_UNLOCK(fvp, 0, p);
1056 if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
1057 newparent = 1;
1058 if (doingdirectory && newparent) {
1059 if (error) /* write access check above */
1060 goto bad;
1061 if (xp != NULL)
1062 vput(tvp);
1063 /*
1064 * doscheckpath() vput()'s dp,
1065 * so we have to do a relookup afterwards
1066 */
1067 error = doscheckpath(ip, dp);
1068 if (error)
1069 goto out;
1070 if ((tcnp->cn_flags & SAVESTART) == 0)
1071 panic("msdosfs_rename: lost to startdir");
1072 error = relookup(tdvp, &tvp, tcnp);
1073 if (error)
1074 goto out;
1075 dp = VTODE(tdvp);
1076 xp = tvp ? VTODE(tvp) : NULL;
1077 }
1078
1079 if (xp != NULL) {
1080 /*
1081 * Target must be empty if a directory and have no links
1082 * to it. Also, ensure source and target are compatible
1083 * (both directories, or both not directories).
1084 */
1085 if (xp->de_Attributes & ATTR_DIRECTORY) {
1086 if (!dosdirempty(xp)) {
1087 error = ENOTEMPTY;
1088 goto bad;
1089 }
1090 if (!doingdirectory) {
1091 error = ENOTDIR;
1092 goto bad;
1093 }
1094 cache_purge(tdvp);
1095 } else if (doingdirectory) {
1096 error = EISDIR;
1097 goto bad;
1098 }
1099 error = removede(dp, xp);
1100 if (error)
1101 goto bad;
1102 vput(tvp);
1103 xp = NULL;
1104 }
1105
1106 /*
1107 * Convert the filename in tcnp into a dos filename. We copy this
1108 * into the denode and directory entry for the destination
1109 * file/directory.
1110 */
1111 error = uniqdosname(VTODE(tdvp), tcnp, toname);
1112 if (error)
1113 goto abortit;
1114
1115 /*
1116 * Since from wasn't locked at various places above,
1117 * have to do a relookup here.
1118 */
1119 fcnp->cn_flags &= ~MODMASK;
1120 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1121 if ((fcnp->cn_flags & SAVESTART) == 0)
1122 panic("msdosfs_rename: lost from startdir");
1123 if (!newparent)
1124 VOP_UNLOCK(tdvp, 0, p);
1125 if (relookup(fdvp, &fvp, fcnp) == 0)
1126 vrele(fdvp);
1127 if (fvp == NULL) {
1128 /*
1129 * From name has disappeared.
1130 */
1131 if (doingdirectory)
1132 panic("rename: lost dir entry");
1133 vrele(ap->a_fvp);
1134 if (newparent)
1135 VOP_UNLOCK(tdvp, 0, p);
1136 vrele(tdvp);
1137 return 0;
1138 }
1139 xp = VTODE(fvp);
1140 zp = VTODE(fdvp);
1141 from_diroffset = zp->de_fndoffset;
1142
1143 /*
1144 * Ensure that the directory entry still exists and has not
1145 * changed till now. If the source is a file the entry may
1146 * have been unlinked or renamed. In either case there is
1147 * no further work to be done. If the source is a directory
1148 * then it cannot have been rmdir'ed or renamed; this is
1149 * prohibited by the DE_RENAME flag.
1150 */
1151 if (xp != ip) {
1152 if (doingdirectory)
1153 panic("rename: lost dir entry");
1154 vrele(ap->a_fvp);
1155 VOP_UNLOCK(fvp, 0, p);
1156 if (newparent)
1157 VOP_UNLOCK(fdvp, 0, p);
1158 xp = NULL;
1159 } else {
1160 vrele(fvp);
1161 xp = NULL;
1162
1163 /*
1164 * First write a new entry in the destination
1165 * directory and mark the entry in the source directory
1166 * as deleted. Then move the denode to the correct hash
1167 * chain for its new location in the filesystem. And, if
1168 * we moved a directory, then update its .. entry to point
1169 * to the new parent directory.
1170 */
1171 bcopy(ip->de_Name, oldname, 11);
1172 bcopy(toname, ip->de_Name, 11); /* update denode */
1173 dp->de_fndoffset = to_diroffset;
1174 dp->de_fndcnt = to_count;
1175 error = createde(ip, dp, (struct denode **)0, tcnp);
1176 if (error) {
1177 bcopy(oldname, ip->de_Name, 11);
1178 if (newparent)
1179 VOP_UNLOCK(fdvp, 0, p);
1180 VOP_UNLOCK(fvp, 0, p);
1181 goto bad;
1182 }
1183 ip->de_refcnt++;
1184 zp->de_fndoffset = from_diroffset;
1185 error = removede(zp, ip);
1186 if (error) {
1187 /* XXX should really panic here, fs is corrupt */
1188 if (newparent)
1189 VOP_UNLOCK(fdvp, 0, p);
1190 VOP_UNLOCK(fvp, 0, p);
1191 goto bad;
1192 }
1193 if (!doingdirectory) {
1194 error = pcbmap(dp, de_cluster(pmp, to_diroffset), 0,
1195 &ip->de_dirclust, 0);
1196 if (error) {
1197 /* XXX should really panic here, fs is corrupt */
1198 if (newparent)
1199 VOP_UNLOCK(fdvp, 0, p);
1200 VOP_UNLOCK(fvp, 0, p);
1201 goto bad;
1202 }
1203 if (ip->de_dirclust == MSDOSFSROOT)
1204 ip->de_diroffset = to_diroffset;
1205 else
1206 ip->de_diroffset = to_diroffset & pmp->pm_crbomask;
1207 }
1208 reinsert(ip);
1209 if (newparent)
1210 VOP_UNLOCK(fdvp, 0, p);
1211 }
1212
1213 /*
1214 * If we moved a directory to a new parent directory, then we must
1215 * fixup the ".." entry in the moved directory.
1216 */
1217 if (doingdirectory && newparent) {
1218 cn = ip->de_StartCluster;
1219 if (cn == MSDOSFSROOT) {
1220 /* this should never happen */
1221 panic("msdosfs_rename(): updating .. in root directory?");
1222 } else
1223 bn = cntobn(pmp, cn);
1224 error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster,
1225 NOCRED, &bp);
1226 if (error) {
1227 /* XXX should really panic here, fs is corrupt */
1228 brelse(bp);
1229 VOP_UNLOCK(fvp, 0, p);
1230 goto bad;
1231 }
1232 dotdotp = (struct direntry *)bp->b_data + 1;
1233 putushort(dotdotp->deStartCluster, dp->de_StartCluster);
1234 if (FAT32(pmp))
1235 putushort(dotdotp->deHighClust, dp->de_StartCluster >> 16);
1236 error = bwrite(bp);
1237 if (error) {
1238 /* XXX should really panic here, fs is corrupt */
1239 VOP_UNLOCK(fvp, 0, p);
1240 goto bad;
1241 }
1242 }
1243
1244 VOP_UNLOCK(fvp, 0, p);
1245bad:
1246 if (xp)
1247 vput(tvp);
1248 vput(tdvp);
1249out:
1250 ip->de_flag &= ~DE_RENAME;
1251 vrele(fdvp);
1252 vrele(fvp);
1253 return (error);
1254
1255}
1256
1257static struct {
1258 struct direntry dot;
1259 struct direntry dotdot;
1260} dosdirtemplate = {
1261 { ". ", " ", /* the . entry */
1262 ATTR_DIRECTORY, /* file attribute */
1263 0, /* reserved */
1264 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1265 { 0, 0 }, /* access date */
1266 { 0, 0 }, /* high bits of start cluster */
1267 { 210, 4 }, { 210, 4 }, /* modify time & date */
1268 { 0, 0 }, /* startcluster */
1269 { 0, 0, 0, 0 } /* filesize */
1270 },
1271 { ".. ", " ", /* the .. entry */
1272 ATTR_DIRECTORY, /* file attribute */
1273 0, /* reserved */
1274 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1275 { 0, 0 }, /* access date */
1276 { 0, 0 }, /* high bits of start cluster */
1277 { 210, 4 }, { 210, 4 }, /* modify time & date */
1278 { 0, 0 }, /* startcluster */
1279 { 0, 0, 0, 0 } /* filesize */
1280 }
1281};
1282
1283static int
1284msdosfs_mkdir(ap)
1285 struct vop_mkdir_args /* {
1286 struct vnode *a_dvp;
1287 struvt vnode **a_vpp;
1288 struvt componentname *a_cnp;
1289 struct vattr *a_vap;
1290 } */ *ap;
1291{
1292 struct componentname *cnp = ap->a_cnp;
1293 struct denode *dep;
1294 struct denode *pdep = VTODE(ap->a_dvp);
1295 struct direntry *denp;
1296 struct msdosfsmount *pmp = pdep->de_pmp;
1297 struct buf *bp;
1298 u_long newcluster, pcl;
1299 int bn;
1300 int error;
1301 struct denode ndirent;
1302 struct timespec ts;
1303
1304 /*
1305 * If this is the root directory and there is no space left we
1306 * can't do anything. This is because the root directory can not
1307 * change size.
1308 */
1309 if (pdep->de_StartCluster == MSDOSFSROOT
1310 && pdep->de_fndoffset >= pdep->de_FileSize) {
1311 error = ENOSPC;
1312 goto bad2;
1313 }
1314
1315 /*
1316 * Allocate a cluster to hold the about to be created directory.
1317 */
1318 error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
1319 if (error)
1320 goto bad2;
1321
1322 bzero(&ndirent, sizeof(ndirent));
1323 ndirent.de_pmp = pmp;
1324 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1325 getnanotime(&ts);
1326 DETIMES(&ndirent, &ts, &ts, &ts);
1327
1328 /*
1329 * Now fill the cluster with the "." and ".." entries. And write
1330 * the cluster to disk. This way it is there for the parent
1331 * directory to be pointing at if there were a crash.
1332 */
1333 bn = cntobn(pmp, newcluster);
1334 /* always succeeds */
1335 bp = getblk(pmp->pm_devvp, bn, pmp->pm_bpcluster, 0, 0);
1336 bzero(bp->b_data, pmp->pm_bpcluster);
1337 bcopy(&dosdirtemplate, bp->b_data, sizeof dosdirtemplate);
1338 denp = (struct direntry *)bp->b_data;
1339 putushort(denp[0].deStartCluster, newcluster);
1340 putushort(denp[0].deCDate, ndirent.de_CDate);
1341 putushort(denp[0].deCTime, ndirent.de_CTime);
1342 denp[0].deCHundredth = ndirent.de_CHun;
1343 putushort(denp[0].deADate, ndirent.de_ADate);
1344 putushort(denp[0].deMDate, ndirent.de_MDate);
1345 putushort(denp[0].deMTime, ndirent.de_MTime);
1346 pcl = pdep->de_StartCluster;
1347 if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1348 pcl = 0;
1349 putushort(denp[1].deStartCluster, pcl);
1350 putushort(denp[1].deCDate, ndirent.de_CDate);
1351 putushort(denp[1].deCTime, ndirent.de_CTime);
1352 denp[1].deCHundredth = ndirent.de_CHun;
1353 putushort(denp[1].deADate, ndirent.de_ADate);
1354 putushort(denp[1].deMDate, ndirent.de_MDate);
1355 putushort(denp[1].deMTime, ndirent.de_MTime);
1356 if (FAT32(pmp)) {
1357 putushort(denp[0].deHighClust, newcluster >> 16);
1358 putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
1359 }
1360
1361 error = bwrite(bp);
1362 if (error)
1363 goto bad;
1364
1365 /*
1366 * Now build up a directory entry pointing to the newly allocated
1367 * cluster. This will be written to an empty slot in the parent
1368 * directory.
1369 */
1370#ifdef DIAGNOSTIC
1371 if ((cnp->cn_flags & HASBUF) == 0)
1372 panic("msdosfs_mkdir: no name");
1373#endif
1374 error = uniqdosname(pdep, cnp, ndirent.de_Name);
1375 if (error)
1376 goto bad;
1377
1378 ndirent.de_Attributes = ATTR_DIRECTORY;
1379 ndirent.de_LowerCase = 0;
1380 ndirent.de_StartCluster = newcluster;
1381 ndirent.de_FileSize = 0;
1382 ndirent.de_dev = pdep->de_dev;
1383 ndirent.de_devvp = pdep->de_devvp;
1384 error = createde(&ndirent, pdep, &dep, cnp);
1385 if (error)
1386 goto bad;
1387 *ap->a_vpp = DETOV(dep);
1388 return (0);
1389
1390bad:
1391 clusterfree(pmp, newcluster, NULL);
1392bad2:
1393 return (error);
1394}
1395
1396static int
1397msdosfs_rmdir(ap)
1398 struct vop_rmdir_args /* {
1399 struct vnode *a_dvp;
1400 struct vnode *a_vp;
1401 struct componentname *a_cnp;
1402 } */ *ap;
1403{
1404 register struct vnode *vp = ap->a_vp;
1405 register struct vnode *dvp = ap->a_dvp;
1406 register struct componentname *cnp = ap->a_cnp;
1407 register struct denode *ip, *dp;
1408 struct proc *p = cnp->cn_proc;
1409 int error;
1410
1411 ip = VTODE(vp);
1412 dp = VTODE(dvp);
1413
1414 /*
1415 * Verify the directory is empty (and valid).
1416 * (Rmdir ".." won't be valid since
1417 * ".." will contain a reference to
1418 * the current directory and thus be
1419 * non-empty.)
1420 */
1421 error = 0;
1422 if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
1423 error = ENOTEMPTY;
1424 goto out;
1425 }
1426 /*
1427 * Delete the entry from the directory. For dos filesystems this
1428 * gets rid of the directory entry on disk, the in memory copy
1429 * still exists but the de_refcnt is <= 0. This prevents it from
1430 * being found by deget(). When the vput() on dep is done we give
1431 * up access and eventually msdosfs_reclaim() will be called which
1432 * will remove it from the denode cache.
1433 */
1434 error = removede(dp, ip);
1435 if (error)
1436 goto out;
1437 /*
1438 * This is where we decrement the link count in the parent
1439 * directory. Since dos filesystems don't do this we just purge
1440 * the name cache.
1441 */
1442 cache_purge(dvp);
1443 VOP_UNLOCK(dvp, 0, p);
1444 /*
1445 * Truncate the directory that is being deleted.
1446 */
1447 error = detrunc(ip, (u_long)0, IO_SYNC, cnp->cn_cred, p);
1448 cache_purge(vp);
1449
1450 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p);
1451out:
1452 return (error);
1453}
1454
1455/*
1456 * DOS filesystems don't know what symlinks are.
1457 */
1458static int
1459msdosfs_symlink(ap)
1460 struct vop_symlink_args /* {
1461 struct vnode *a_dvp;
1462 struct vnode **a_vpp;
1463 struct componentname *a_cnp;
1464 struct vattr *a_vap;
1465 char *a_target;
1466 } */ *ap;
1467{
1468 return (EOPNOTSUPP);
1469}
1470
1471static int
1472msdosfs_readdir(ap)
1473 struct vop_readdir_args /* {
1474 struct vnode *a_vp;
1475 struct uio *a_uio;
1476 struct ucred *a_cred;
1477 int *a_eofflag;
1478 int *a_ncookies;
1479 u_long **a_cookies;
1480 } */ *ap;
1481{
1482 int error = 0;
1483 int diff;
1484 long n;
1485 int blsize;
1486 long on;
1487 u_long cn;
1488 u_long fileno;
1489 u_long dirsperblk;
1490 long bias = 0;
1491 daddr_t bn, lbn;
1492 struct buf *bp;
1493 struct denode *dep = VTODE(ap->a_vp);
1494 struct msdosfsmount *pmp = dep->de_pmp;
1495 struct direntry *dentp;
1496 struct dirent dirbuf;
1497 struct uio *uio = ap->a_uio;
1498 u_long *cookies = NULL;
1499 int ncookies = 0;
1500 off_t offset, off;
1501 int chksum = -1;
1502
1503#ifdef MSDOSFS_DEBUG
1504 printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1505 ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1506#endif
1507
1508 /*
1509 * msdosfs_readdir() won't operate properly on regular files since
1510 * it does i/o only with the the filesystem vnode, and hence can
1511 * retrieve the wrong block from the buffer cache for a plain file.
1512 * So, fail attempts to readdir() on a plain file.
1513 */
1514 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
1515 return (ENOTDIR);
1516
1517 /*
1518 * To be safe, initialize dirbuf
1519 */
1520 bzero(dirbuf.d_name, sizeof(dirbuf.d_name));
1521
1522 /*
1523 * If the user buffer is smaller than the size of one dos directory
1524 * entry or the file offset is not a multiple of the size of a
1525 * directory entry, then we fail the read.
1526 */
1527 off = offset = uio->uio_offset;
1528 if (uio->uio_resid < sizeof(struct direntry) ||
1529 (offset & (sizeof(struct direntry) - 1)))
1530 return (EINVAL);
1531
1532 if (ap->a_ncookies) {
1533 ncookies = uio->uio_resid / 16;
1534 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1535 M_WAITOK);
1536 *ap->a_cookies = cookies;
1537 *ap->a_ncookies = ncookies;
1538 }
1539
1540 dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1541
1542 /*
1543 * If they are reading from the root directory then, we simulate
1544 * the . and .. entries since these don't exist in the root
1545 * directory. We also set the offset bias to make up for having to
1546 * simulate these entries. By this I mean that at file offset 64 we
1547 * read the first entry in the root directory that lives on disk.
1548 */
1549 if (dep->de_StartCluster == MSDOSFSROOT
1550 || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1551#if 0
1552 printf("msdosfs_readdir(): going after . or .. in root dir, offset %d\n",
1553 offset);
1554#endif
1555 bias = 2 * sizeof(struct direntry);
1556 if (offset < bias) {
1557 for (n = (int)offset / sizeof(struct direntry);
1558 n < 2; n++) {
1559 if (FAT32(pmp))
1560 dirbuf.d_fileno = cntobn(pmp,
1561 pmp->pm_rootdirblk)
1562 * dirsperblk;
1563 else
1564 dirbuf.d_fileno = 1;
1565 dirbuf.d_type = DT_DIR;
1566 switch (n) {
1567 case 0:
1568 dirbuf.d_namlen = 1;
1569 strcpy(dirbuf.d_name, ".");
1570 break;
1571 case 1:
1572 dirbuf.d_namlen = 2;
1573 strcpy(dirbuf.d_name, "..");
1574 break;
1575 }
1576 dirbuf.d_reclen = GENERIC_DIRSIZ(&dirbuf);
1577 if (uio->uio_resid < dirbuf.d_reclen)
1578 goto out;
1579 error = uiomove((caddr_t) &dirbuf,
1580 dirbuf.d_reclen, uio);
1581 if (error)
1582 goto out;
1583 offset += sizeof(struct direntry);
1584 off = offset;
1585 if (cookies) {
1586 *cookies++ = offset;
1587 if (--ncookies <= 0)
1588 goto out;
1589 }
1590 }
1591 }
1592 }
1593
1594 off = offset;
1595 while (uio->uio_resid > 0) {
1596 lbn = de_cluster(pmp, offset - bias);
1597 on = (offset - bias) & pmp->pm_crbomask;
1598 n = min(pmp->pm_bpcluster - on, uio->uio_resid);
1599 diff = dep->de_FileSize - (offset - bias);
1600 if (diff <= 0)
1601 break;
1602 n = min(n, diff);
1603 error = pcbmap(dep, lbn, &bn, &cn, &blsize);
1604 if (error)
1605 break;
1606 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
1607 if (error) {
1608 brelse(bp);
1609 return (error);
1610 }
1611 n = min(n, blsize - bp->b_resid);
1612
1613 /*
1614 * Convert from dos directory entries to fs-independent
1615 * directory entries.
1616 */
1617 for (dentp = (struct direntry *)(bp->b_data + on);
1618 (char *)dentp < bp->b_data + on + n;
1619 dentp++, offset += sizeof(struct direntry)) {
1620#if 0
1621 printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1622 dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1623#endif
1624 /*
1625 * If this is an unused entry, we can stop.
1626 */
1627 if (dentp->deName[0] == SLOT_EMPTY) {
1628 brelse(bp);
1629 goto out;
1630 }
1631 /*
1632 * Skip deleted entries.
1633 */
1634 if (dentp->deName[0] == SLOT_DELETED) {
1635 chksum = -1;
1636 continue;
1637 }
1638
1639 /*
1640 * Handle Win95 long directory entries
1641 */
1642 if (dentp->deAttributes == ATTR_WIN95) {
1643 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1644 continue;
1645 chksum = win2unixfn((struct winentry *)dentp,
1646 &dirbuf, chksum,
1647 pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
1648 pmp->pm_u2w);
1649 continue;
1650 }
1651
1652 /*
1653 * Skip volume labels
1654 */
1655 if (dentp->deAttributes & ATTR_VOLUME) {
1656 chksum = -1;
1657 continue;
1658 }
1659 /*
1660 * This computation of d_fileno must match
1661 * the computation of va_fileid in
1662 * msdosfs_getattr.
1663 */
1664 if (dentp->deAttributes & ATTR_DIRECTORY) {
1665 fileno = getushort(dentp->deStartCluster);
1666 if (FAT32(pmp))
1667 fileno |= getushort(dentp->deHighClust) << 16;
1668 /* if this is the root directory */
1669 if (fileno == MSDOSFSROOT)
1670 if (FAT32(pmp))
1671 fileno = cntobn(pmp,
1672 pmp->pm_rootdirblk)
1673 * dirsperblk;
1674 else
1675 fileno = 1;
1676 else
1677 fileno = cntobn(pmp, fileno) * dirsperblk;
1678 dirbuf.d_fileno = fileno;
1679 dirbuf.d_type = DT_DIR;
1680 } else {
1681 dirbuf.d_fileno = offset / sizeof(struct direntry);
1682 dirbuf.d_type = DT_REG;
1683 }
1684 if (chksum != winChksum(dentp->deName))
1685 dirbuf.d_namlen = dos2unixfn(dentp->deName,
1686 (u_char *)dirbuf.d_name,
1687 dentp->deLowerCase |
1688 ((pmp->pm_flags & MSDOSFSMNT_SHORTNAME) ?
1689 (LCASE_BASE | LCASE_EXT) : 0),
1690 pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
1691 pmp->pm_d2u,
1692 pmp->pm_flags & MSDOSFSMNT_ULTABLE,
1693 pmp->pm_ul);
1694 else
1695 dirbuf.d_name[dirbuf.d_namlen] = 0;
1696 chksum = -1;
1697 dirbuf.d_reclen = GENERIC_DIRSIZ(&dirbuf);
1698 if (uio->uio_resid < dirbuf.d_reclen) {
1699 brelse(bp);
1700 goto out;
1701 }
1702 error = uiomove((caddr_t) &dirbuf,
1703 dirbuf.d_reclen, uio);
1704 if (error) {
1705 brelse(bp);
1706 goto out;
1707 }
1708 if (cookies) {
1709 *cookies++ = offset + sizeof(struct direntry);
1710 if (--ncookies <= 0) {
1711 brelse(bp);
1712 goto out;
1713 }
1714 }
1715 off = offset + sizeof(struct direntry);
1716 }
1717 brelse(bp);
1718 }
1719out:
1720 /* Subtract unused cookies */
1721 if (ap->a_ncookies)
1722 *ap->a_ncookies -= ncookies;
1723
1724 uio->uio_offset = off;
1725
1726 /*
1727 * Set the eofflag (NFS uses it)
1728 */
1729 if (ap->a_eofflag) {
1730 if (dep->de_FileSize - (offset - bias) <= 0)
1731 *ap->a_eofflag = 1;
1732 else
1733 *ap->a_eofflag = 0;
1734 }
1735 return (error);
1736}
1737
1738/*
1739 * vp - address of vnode file the file
1740 * bn - which cluster we are interested in mapping to a filesystem block number.
1741 * vpp - returns the vnode for the block special file holding the filesystem
1742 * containing the file of interest
1743 * bnp - address of where to return the filesystem relative block number
1744 */
1745static int
1746msdosfs_bmap(ap)
1747 struct vop_bmap_args /* {
1748 struct vnode *a_vp;
1749 daddr_t a_bn;
1750 struct vnode **a_vpp;
1751 daddr_t *a_bnp;
1752 int *a_runp;
1753 int *a_runb;
1754 } */ *ap;
1755{
1756 struct denode *dep = VTODE(ap->a_vp);
1757
1758 if (ap->a_vpp != NULL)
1759 *ap->a_vpp = dep->de_devvp;
1760 if (ap->a_bnp == NULL)
1761 return (0);
1762 if (ap->a_runp) {
1763 /*
1764 * Sequential clusters should be counted here.
1765 */
1766 *ap->a_runp = 0;
1767 }
1768 if (ap->a_runb) {
1769 *ap->a_runb = 0;
1770 }
1771 return (pcbmap(dep, ap->a_bn, ap->a_bnp, 0, 0));
1772}
1773
1774static int
1775msdosfs_strategy(ap)
1776 struct vop_strategy_args /* {
1777 struct vnode *a_vp;
1778 struct buf *a_bp;
1779 } */ *ap;
1780{
1781 struct buf *bp = ap->a_bp;
1782 struct denode *dep = VTODE(bp->b_vp);
1783 struct vnode *vp;
1784 int error = 0;
1785
1786 if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR)
1787 panic("msdosfs_strategy: spec");
1788 /*
1789 * If we don't already know the filesystem relative block number
1790 * then get it using pcbmap(). If pcbmap() returns the block
1791 * number as -1 then we've got a hole in the file. DOS filesystems
1792 * don't allow files with holes, so we shouldn't ever see this.
1793 */
1794 if (bp->b_blkno == bp->b_lblkno) {
1795 error = pcbmap(dep, bp->b_lblkno, &bp->b_blkno, 0, 0);
1796 if (error) {
1797 bp->b_error = error;
1798 bp->b_ioflags |= BIO_ERROR;
1799 bufdone(bp);
1800 return (error);
1801 }
1802 if ((long)bp->b_blkno == -1)
1803 vfs_bio_clrbuf(bp);
1804 }
1805 if (bp->b_blkno == -1) {
1806 bufdone(bp);
1807 return (0);
1808 }
1809 /*
1810 * Read/write the block from/to the disk that contains the desired
1811 * file block.
1812 */
1813 vp = dep->de_devvp;
1814 bp->b_dev = vp->v_rdev;
1815 VOP_STRATEGY(vp, bp);
1816 return (0);
1817}
1818
1819static int
1820msdosfs_print(ap)
1821 struct vop_print_args /* {
1822 struct vnode *vp;
1823 } */ *ap;
1824{
1825 struct denode *dep = VTODE(ap->a_vp);
1826
1827 printf(
1828 "tag VT_MSDOSFS, startcluster %lu, dircluster %lu, diroffset %lu ",
1829 dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1830 printf(" dev %d, %d", major(dep->de_dev), minor(dep->de_dev));
1831 lockmgr_printinfo(&ap->a_vp->v_lock);
1832 printf("\n");
1833 return (0);
1834}
1835
1836static int
1837msdosfs_pathconf(ap)
1838 struct vop_pathconf_args /* {
1839 struct vnode *a_vp;
1840 int a_name;
1841 int *a_retval;
1842 } */ *ap;
1843{
1844 struct msdosfsmount *pmp = VTODE(ap->a_vp)->de_pmp;
1845
1846 switch (ap->a_name) {
1847 case _PC_LINK_MAX:
1848 *ap->a_retval = 1;
1849 return (0);
1850 case _PC_NAME_MAX:
1851 *ap->a_retval = pmp->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12;
1852 return (0);
1853 case _PC_PATH_MAX:
1854 *ap->a_retval = PATH_MAX;
1855 return (0);
1856 case _PC_CHOWN_RESTRICTED:
1857 *ap->a_retval = 1;
1858 return (0);
1859 case _PC_NO_TRUNC:
1860 *ap->a_retval = 0;
1861 return (0);
1862 default:
1863 return (EINVAL);
1864 }
1865 /* NOTREACHED */
1866}
1867
1868/*
1869 * get page routine
1870 *
1871 * XXX By default, wimp out... note that a_offset is ignored (and always
1872 * XXX has been).
1873 */
1874int
1875msdosfs_getpages(ap)
1876 struct vop_getpages_args *ap;
1877{
1878 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
1879 ap->a_reqpage);
1880}
1881
1882/*
1883 * put page routine
1884 *
1885 * XXX By default, wimp out... note that a_offset is ignored (and always
1886 * XXX has been).
1887 */
1888int
1889msdosfs_putpages(ap)
1890 struct vop_putpages_args *ap;
1891{
1892 return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
1893 ap->a_sync, ap->a_rtvals);
1894}
1895
1896/* Global vfs data structures for msdosfs */
1897vop_t **msdosfs_vnodeop_p;
1898static struct vnodeopv_entry_desc msdosfs_vnodeop_entries[] = {
1899 { &vop_default_desc, (vop_t *) vop_defaultop },
1900 { &vop_access_desc, (vop_t *) msdosfs_access },
1901 { &vop_bmap_desc, (vop_t *) msdosfs_bmap },
1902 { &vop_cachedlookup_desc, (vop_t *) msdosfs_lookup },
1903 { &vop_close_desc, (vop_t *) msdosfs_close },
1904 { &vop_create_desc, (vop_t *) msdosfs_create },
1905 { &vop_fsync_desc, (vop_t *) msdosfs_fsync },
1906 { &vop_getattr_desc, (vop_t *) msdosfs_getattr },
1907 { &vop_inactive_desc, (vop_t *) msdosfs_inactive },
1908 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
1909 { &vop_link_desc, (vop_t *) msdosfs_link },
1910 { &vop_lock_desc, (vop_t *) vop_stdlock },
1911 { &vop_lookup_desc, (vop_t *) vfs_cache_lookup },
1912 { &vop_mkdir_desc, (vop_t *) msdosfs_mkdir },
1913 { &vop_mknod_desc, (vop_t *) msdosfs_mknod },
1914 { &vop_pathconf_desc, (vop_t *) msdosfs_pathconf },
1915 { &vop_print_desc, (vop_t *) msdosfs_print },
1916 { &vop_read_desc, (vop_t *) msdosfs_read },
1917 { &vop_readdir_desc, (vop_t *) msdosfs_readdir },
1918 { &vop_reclaim_desc, (vop_t *) msdosfs_reclaim },
1919 { &vop_remove_desc, (vop_t *) msdosfs_remove },
1920 { &vop_rename_desc, (vop_t *) msdosfs_rename },
1921 { &vop_rmdir_desc, (vop_t *) msdosfs_rmdir },
1922 { &vop_setattr_desc, (vop_t *) msdosfs_setattr },
1923 { &vop_strategy_desc, (vop_t *) msdosfs_strategy },
1924 { &vop_symlink_desc, (vop_t *) msdosfs_symlink },
1925 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
1926 { &vop_write_desc, (vop_t *) msdosfs_write },
1927 { &vop_getpages_desc, (vop_t *) msdosfs_getpages },
1928 { &vop_putpages_desc, (vop_t *) msdosfs_putpages },
1929 { NULL, NULL }
1930};
1931static struct vnodeopv_desc msdosfs_vnodeop_opv_desc =
1932 { &msdosfs_vnodeop_p, msdosfs_vnodeop_entries };
1933
1934VNODEOP_SET(msdosfs_vnodeop_opv_desc);