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