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