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