msdosfs_vnops.c revision 1.7 1 /* $NetBSD: msdosfs_vnops.c,v 1.7 2013/01/27 15:35:45 christos Exp $ */
2
3 /*-
4 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
5 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
6 * All rights reserved.
7 * Original code by Paul Popelka (paulp (at) uts.amdahl.com) (see below).
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by TooLs GmbH.
20 * 4. The name of TooLs GmbH may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34 /*
35 * Written by Paul Popelka (paulp (at) uts.amdahl.com)
36 *
37 * You can do anything you want with this software, just don't say you wrote
38 * it, and don't remove this notice.
39 *
40 * This software is provided "as is".
41 *
42 * The author supplies this software to be publicly redistributed on the
43 * understanding that the author is not responsible for the correct
44 * functioning of this software in any circumstances and is not liable for
45 * any damages caused by this software.
46 *
47 * October 1992
48 */
49 #if HAVE_NBTOOL_CONFIG_H
50 #include "nbtool_config.h"
51 #endif
52
53 #include <sys/cdefs.h>
54 __KERNEL_RCSID(0, "$NetBSD: msdosfs_vnops.c,v 1.7 2013/01/27 15:35:45 christos Exp $");
55
56 #include <sys/param.h>
57 #include <sys/mman.h>
58 #include <sys/mount.h>
59 #include <fcntl.h>
60 #include <unistd.h>
61
62 #include <ffs/buf.h>
63
64 #include <fs/msdosfs/bpb.h>
65 #include <fs/msdosfs/direntry.h>
66 #include <fs/msdosfs/denode.h>
67 #include <fs/msdosfs/msdosfsmount.h>
68 #include <fs/msdosfs/fat.h>
69
70 #include "makefs.h"
71 #include "msdos.h"
72
73 /*
74 * Some general notes:
75 *
76 * In the ufs filesystem the inodes, superblocks, and indirect blocks are
77 * read/written using the vnode for the filesystem. Blocks that represent
78 * the contents of a file are read/written using the vnode for the file
79 * (including directories when they are read/written as files). This
80 * presents problems for the dos filesystem because data that should be in
81 * an inode (if dos had them) resides in the directory itself. Since we
82 * must update directory entries without the benefit of having the vnode
83 * for the directory we must use the vnode for the filesystem. This means
84 * that when a directory is actually read/written (via read, write, or
85 * readdir, or seek) we must use the vnode for the filesystem instead of
86 * the vnode for the directory as would happen in ufs. This is to insure we
87 * retrieve the correct block from the buffer cache since the hash value is
88 * based upon the vnode address and the desired block number.
89 */
90
91 static int msdosfs_wfile(const char *, struct denode *, fsnode *);
92
93 static void
94 msdosfs_times(struct msdosfsmount *pmp, struct denode *dep,
95 const struct stat *st)
96 {
97 #ifndef HAVE_NBTOOL_CONFIG_H
98 struct timespec at = st->st_atimespec;
99 struct timespec mt = st->st_mtimespec;
100 #else
101 struct timespec at = { st->st_atime, 0 };
102 struct timespec mt = { st->st_mtime, 0 };
103 #endif
104 unix2dostime(&at, pmp->pm_gmtoff, &dep->de_ADate, NULL, NULL);
105 unix2dostime(&mt, pmp->pm_gmtoff, &dep->de_MDate, &dep->de_MTime, NULL);
106 }
107
108 /*
109 * When we search a directory the blocks containing directory entries are
110 * read and examined. The directory entries contain information that would
111 * normally be in the inode of a unix filesystem. This means that some of
112 * a directory's contents may also be in memory resident denodes (sort of
113 * an inode). This can cause problems if we are searching while some other
114 * process is modifying a directory. To prevent one process from accessing
115 * incompletely modified directory information we depend upon being the
116 * sole owner of a directory block. bread/brelse provide this service.
117 * This being the case, when a process modifies a directory it must first
118 * acquire the disk block that contains the directory entry to be modified.
119 * Then update the disk block and the denode, and then write the disk block
120 * out to disk. This way disk blocks containing directory entries and in
121 * memory denode's will be in synch.
122 */
123 static int
124 msdosfs_findslot(struct denode *dp, struct componentname *cnp)
125 {
126 daddr_t bn;
127 int error;
128 int slotcount;
129 int slotoffset = 0;
130 int frcn;
131 u_long cluster;
132 int blkoff;
133 u_int diroff;
134 int blsize;
135 struct denode *tdp;
136 struct msdosfsmount *pmp;
137 struct buf *bp = 0;
138 struct direntry *dep;
139 u_char dosfilename[12];
140 int wincnt = 1;
141 int chksum = -1, chksum_ok;
142 int olddos = 1;
143
144 pmp = dp->de_pmp;
145 switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename,
146 cnp->cn_namelen, 0)) {
147 case 0:
148 return (EINVAL);
149 case 1:
150 break;
151 case 2:
152 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
153 cnp->cn_namelen) + 1;
154 break;
155 case 3:
156 olddos = 0;
157 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
158 cnp->cn_namelen) + 1;
159 break;
160 }
161
162 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
163 wincnt = 1;
164
165 /*
166 * Suppress search for slots unless creating
167 * file and at end of pathname, in which case
168 * we watch for a place to put the new file in
169 * case it doesn't already exist.
170 */
171 slotcount = 0;
172 #ifdef MSDOSFS_DEBUG
173 printf("%s(): dos filename: %s\n", __func__, dosfilename);
174 #endif
175 /*
176 * Search the directory pointed at by vdp for the name pointed at
177 * by cnp->cn_nameptr.
178 */
179 tdp = NULL;
180 /*
181 * The outer loop ranges over the clusters that make up the
182 * directory. Note that the root directory is different from all
183 * other directories. It has a fixed number of blocks that are not
184 * part of the pool of allocatable clusters. So, we treat it a
185 * little differently. The root directory starts at "cluster" 0.
186 */
187 diroff = 0;
188 for (frcn = 0; diroff < dp->de_FileSize; frcn++) {
189 if ((error = pcbmap(dp, frcn, &bn, &cluster, &blsize)) != 0) {
190 if (error == E2BIG)
191 break;
192 return (error);
193 }
194 error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize, NOCRED,
195 0, &bp);
196 if (error) {
197 return (error);
198 }
199 for (blkoff = 0; blkoff < blsize;
200 blkoff += sizeof(struct direntry),
201 diroff += sizeof(struct direntry)) {
202 dep = (struct direntry *)((char *)bp->b_data + blkoff);
203 /*
204 * If the slot is empty and we are still looking
205 * for an empty then remember this one. If the
206 * slot is not empty then check to see if it
207 * matches what we are looking for. If the slot
208 * has never been filled with anything, then the
209 * remainder of the directory has never been used,
210 * so there is no point in searching it.
211 */
212 if (dep->deName[0] == SLOT_EMPTY ||
213 dep->deName[0] == SLOT_DELETED) {
214 /*
215 * Drop memory of previous long matches
216 */
217 chksum = -1;
218
219 if (slotcount < wincnt) {
220 slotcount++;
221 slotoffset = diroff;
222 }
223 if (dep->deName[0] == SLOT_EMPTY) {
224 brelse(bp, 0);
225 goto notfound;
226 }
227 } else {
228 /*
229 * If there wasn't enough space for our
230 * winentries, forget about the empty space
231 */
232 if (slotcount < wincnt)
233 slotcount = 0;
234
235 /*
236 * Check for Win95 long filename entry
237 */
238 if (dep->deAttributes == ATTR_WIN95) {
239 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
240 continue;
241
242 chksum = winChkName((const u_char *)cnp->cn_nameptr,
243 cnp->cn_namelen,
244 (struct winentry *)dep,
245 chksum);
246 continue;
247 }
248
249 /*
250 * Ignore volume labels (anywhere, not just
251 * the root directory).
252 */
253 if (dep->deAttributes & ATTR_VOLUME) {
254 chksum = -1;
255 continue;
256 }
257
258 /*
259 * Check for a checksum or name match
260 */
261 chksum_ok = (chksum == winChksum(dep->deName));
262 if (!chksum_ok
263 && (!olddos || memcmp(dosfilename, dep->deName, 11))) {
264 chksum = -1;
265 continue;
266 }
267 #ifdef MSDOSFS_DEBUG
268 printf("%s(): match blkoff %d, diroff %d\n",
269 __func__, blkoff, diroff);
270 #endif
271 /*
272 * Remember where this directory
273 * entry came from for whoever did
274 * this lookup.
275 */
276 dp->de_fndoffset = diroff;
277 dp->de_fndcnt = 0;
278
279 return EEXIST;
280 }
281 } /* for (blkoff = 0; .... */
282 /*
283 * Release the buffer holding the directory cluster just
284 * searched.
285 */
286 brelse(bp, 0);
287 } /* for (frcn = 0; ; frcn++) */
288
289 notfound:
290 /*
291 * We hold no disk buffers at this point.
292 */
293
294 /*
295 * If we get here we didn't find the entry we were looking for. But
296 * that's ok if we are creating or renaming and are at the end of
297 * the pathname and the directory hasn't been removed.
298 */
299 #ifdef MSDOSFS_DEBUG
300 printf("%s(): refcnt %ld, slotcount %d, slotoffset %d\n",
301 __func__, dp->de_refcnt, slotcount, slotoffset);
302 #endif
303 /*
304 * Fixup the slot description to point to the place where
305 * we might put the new DOS direntry (putting the Win95
306 * long name entries before that)
307 */
308 if (!slotcount) {
309 slotcount = 1;
310 slotoffset = diroff;
311 }
312 if (wincnt > slotcount) {
313 slotoffset += sizeof(struct direntry) * (wincnt - slotcount);
314 }
315
316 /*
317 * Return an indication of where the new directory
318 * entry should be put.
319 */
320 dp->de_fndoffset = slotoffset;
321 dp->de_fndcnt = wincnt - 1;
322
323 /*
324 * We return with the directory locked, so that
325 * the parameters we set up above will still be
326 * valid if we actually decide to do a direnter().
327 * We return ni_vp == NULL to indicate that the entry
328 * does not currently exist; we leave a pointer to
329 * the (locked) directory inode in ndp->ni_dvp.
330 *
331 * NB - if the directory is unlocked, then this
332 * information cannot be used.
333 */
334 return 0;
335 }
336
337 /*
338 * Create a regular file. On entry the directory to contain the file being
339 * created is locked. We must release before we return.
340 */
341 struct denode *
342 msdosfs_mkfile(const char *path, struct denode *pdep, fsnode *node)
343 {
344 struct componentname cn;
345 struct denode ndirent;
346 struct denode *dep;
347 int error;
348 struct stat *st = &node->inode->st;
349 struct msdosfsmount *pmp = pdep->de_pmp;
350
351 cn.cn_nameptr = node->name;
352 cn.cn_namelen = strlen(node->name);
353
354 #ifdef MSDOSFS_DEBUG
355 printf("msdosfs_create(name %s, mode 0%o size %zu\n", node->name,
356 st->st_mode, (size_t)st->st_size);
357 #endif
358
359 /*
360 * If this is the root directory and there is no space left we
361 * can't do anything. This is because the root directory can not
362 * change size.
363 */
364 if (pdep->de_StartCluster == MSDOSFSROOT
365 && pdep->de_fndoffset >= pdep->de_FileSize) {
366 error = ENOSPC;
367 goto bad;
368 }
369
370 /*
371 * Create a directory entry for the file, then call createde() to
372 * have it installed. NOTE: DOS files are always executable. We
373 * use the absence of the owner write bit to make the file
374 * readonly.
375 */
376 memset(&ndirent, 0, sizeof(ndirent));
377 if ((error = uniqdosname(pdep, &cn, ndirent.de_Name)) != 0)
378 goto bad;
379
380 ndirent.de_Attributes = (st->st_mode & S_IWUSR) ?
381 ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
382 ndirent.de_StartCluster = 0;
383 ndirent.de_FileSize = 0;
384 ndirent.de_dev = pdep->de_dev;
385 ndirent.de_devvp = pdep->de_devvp;
386 ndirent.de_pmp = pdep->de_pmp;
387 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
388 msdosfs_times(pmp, &ndirent, st);
389 if ((error = msdosfs_findslot(pdep, &cn)) != 0)
390 goto bad;
391 if ((error = createde(&ndirent, pdep, &dep, &cn)) != 0)
392 goto bad;
393 if ((error = msdosfs_wfile(path, dep, node)) != 0)
394 goto bad;
395 return dep;
396
397 bad:
398 errno = error;
399 return NULL;
400 }
401
402 /*
403 * Write data to a file or directory.
404 */
405 static int
406 msdosfs_wfile(const char *path, struct denode *dep, fsnode *node)
407 {
408 int error, fd;
409 size_t osize = dep->de_FileSize;
410 struct stat *st = &node->inode->st;
411 size_t nsize;
412 size_t offs = 0;
413 struct msdosfsmount *pmp = dep->de_pmp;
414 struct buf *bp;
415 char *dat;
416 u_long cn;
417
418 #ifdef MSDOSFS_DEBUG
419 printf("%s(diroff %lu, dirclust %lu, startcluster %lu)\n", __func__,
420 dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
421 #endif
422 if (st->st_size == 0)
423 return 0;
424
425 /* Don't bother to try to write files larger than the fs limit */
426 if (st->st_size > (off_t)min(MSDOSFS_FILESIZE_MAX,__SIZE_MAX__)) {
427 errno = EFBIG;
428 return -1;
429 }
430
431 nsize = st->st_size;
432 if (nsize > osize) {
433 if ((error = deextend(dep, nsize, NULL)) != 0) {
434 errno = error;
435 return -1;
436 }
437 }
438
439 if ((fd = open(path, O_RDONLY)) == -1)
440 err(1, "open %s", path);
441
442 if ((dat = mmap(0, nsize, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0))
443 == MAP_FAILED) {
444 fprintf(stderr, "mmap %s", node->name);
445 goto out;
446 }
447 close(fd);
448
449 for (cn = 0;; cn++) {
450 int blsize, cpsize;
451 daddr_t bn;
452 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
453 fprintf(stderr, "pcbmap %ld\n", cn);
454 goto out;
455 }
456 #ifdef MSDOSFS_DEBUG
457 printf("%s(cn=%lu, bn=%llu/%llu, blsize=%d)\n", __func__, cn,
458 (unsigned long long)bn,
459 (unsigned long long)de_bn2kb(pmp, bn), blsize);
460 #endif
461 if ((error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
462 NULL, 0, &bp)) != 0) {
463 fprintf(stderr, "bread %d\n", error);
464 goto out;
465 }
466 cpsize = MIN((int)(nsize - offs), blsize);
467 memcpy(bp->b_data, dat + offs, cpsize);
468 bwrite(bp);
469 brelse(bp, 0);
470 offs += blsize;
471 if (offs >= nsize)
472 break;
473 }
474
475 munmap(dat, nsize);
476 return 0;
477 out:
478 munmap(dat, nsize);
479 return error;
480 }
481
482
483 static const struct {
484 struct direntry dot;
485 struct direntry dotdot;
486 } dosdirtemplate = {
487 { ". ", " ", /* the . entry */
488 ATTR_DIRECTORY, /* file attribute */
489 0, /* reserved */
490 0, { 0, 0 }, { 0, 0 }, /* create time & date */
491 { 0, 0 }, /* access date */
492 { 0, 0 }, /* high bits of start cluster */
493 { 210, 4 }, { 210, 4 }, /* modify time & date */
494 { 0, 0 }, /* startcluster */
495 { 0, 0, 0, 0 } /* filesize */
496 },
497 { ".. ", " ", /* the .. entry */
498 ATTR_DIRECTORY, /* file attribute */
499 0, /* reserved */
500 0, { 0, 0 }, { 0, 0 }, /* create time & date */
501 { 0, 0 }, /* access date */
502 { 0, 0 }, /* high bits of start cluster */
503 { 210, 4 }, { 210, 4 }, /* modify time & date */
504 { 0, 0 }, /* startcluster */
505 { 0, 0, 0, 0 } /* filesize */
506 }
507 };
508
509 struct denode *
510 msdosfs_mkdire(const char *path, struct denode *pdep, fsnode *node) {
511 struct denode ndirent;
512 struct denode *dep;
513 struct componentname cn;
514 struct stat *st = &node->inode->st;
515 struct msdosfsmount *pmp = pdep->de_pmp;
516 int error;
517 u_long newcluster, pcl, bn;
518 daddr_t lbn;
519 struct direntry *denp;
520 struct buf *bp;
521
522 cn.cn_nameptr = node->name;
523 cn.cn_namelen = strlen(node->name);
524 /*
525 * If this is the root directory and there is no space left we
526 * can't do anything. This is because the root directory can not
527 * change size.
528 */
529 if (pdep->de_StartCluster == MSDOSFSROOT
530 && pdep->de_fndoffset >= pdep->de_FileSize) {
531 error = ENOSPC;
532 goto bad2;
533 }
534
535 /*
536 * Allocate a cluster to hold the about to be created directory.
537 */
538 error = clusteralloc(pmp, 0, 1, &newcluster, NULL);
539 if (error)
540 goto bad2;
541
542 memset(&ndirent, 0, sizeof(ndirent));
543 ndirent.de_pmp = pmp;
544 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
545 msdosfs_times(pmp, &ndirent, st);
546
547 /*
548 * Now fill the cluster with the "." and ".." entries. And write
549 * the cluster to disk. This way it is there for the parent
550 * directory to be pointing at if there were a crash.
551 */
552 bn = cntobn(pmp, newcluster);
553 lbn = de_bn2kb(pmp, bn);
554 /* always succeeds */
555 bp = getblk(pmp->pm_devvp, lbn, pmp->pm_bpcluster, 0, 0);
556 memset(bp->b_data, 0, pmp->pm_bpcluster);
557 memcpy(bp->b_data, &dosdirtemplate, sizeof dosdirtemplate);
558 denp = (struct direntry *)bp->b_data;
559 putushort(denp[0].deStartCluster, newcluster);
560 putushort(denp[0].deCDate, ndirent.de_CDate);
561 putushort(denp[0].deCTime, ndirent.de_CTime);
562 denp[0].deCHundredth = ndirent.de_CHun;
563 putushort(denp[0].deADate, ndirent.de_ADate);
564 putushort(denp[0].deMDate, ndirent.de_MDate);
565 putushort(denp[0].deMTime, ndirent.de_MTime);
566 pcl = pdep->de_StartCluster;
567 if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
568 pcl = 0;
569 putushort(denp[1].deStartCluster, pcl);
570 putushort(denp[1].deCDate, ndirent.de_CDate);
571 putushort(denp[1].deCTime, ndirent.de_CTime);
572 denp[1].deCHundredth = ndirent.de_CHun;
573 putushort(denp[1].deADate, ndirent.de_ADate);
574 putushort(denp[1].deMDate, ndirent.de_MDate);
575 putushort(denp[1].deMTime, ndirent.de_MTime);
576 if (FAT32(pmp)) {
577 putushort(denp[0].deHighClust, newcluster >> 16);
578 putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
579 } else {
580 putushort(denp[0].deHighClust, 0);
581 putushort(denp[1].deHighClust, 0);
582 }
583
584 if ((error = bwrite(bp)) != 0)
585 goto bad;
586
587 /*
588 * Now build up a directory entry pointing to the newly allocated
589 * cluster. This will be written to an empty slot in the parent
590 * directory.
591 */
592 if ((error = uniqdosname(pdep, &cn, ndirent.de_Name)) != 0)
593 goto bad;
594
595 ndirent.de_Attributes = ATTR_DIRECTORY;
596 ndirent.de_StartCluster = newcluster;
597 ndirent.de_FileSize = 0;
598 ndirent.de_dev = pdep->de_dev;
599 ndirent.de_devvp = pdep->de_devvp;
600 if ((error = msdosfs_findslot(pdep, &cn)) != 0)
601 goto bad;
602 if ((error = createde(&ndirent, pdep, &dep, &cn)) != 0)
603 goto bad;
604 return dep;
605
606 bad:
607 clusterfree(pmp, newcluster, NULL);
608 bad2:
609 errno = error;
610 return NULL;
611 }
612