1237d1b14SEd Maste /* $NetBSD: msdosfs_vnops.c,v 1.19 2017/04/13 17:10:12 christos Exp $ */
2237d1b14SEd Maste
3237d1b14SEd Maste /*-
42037e988SEd Maste * SPDX-License-Identifier: BSD-4-Clause
52037e988SEd Maste *
6237d1b14SEd Maste * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
7237d1b14SEd Maste * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
8237d1b14SEd Maste * All rights reserved.
9237d1b14SEd Maste * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
10237d1b14SEd Maste *
11237d1b14SEd Maste * Redistribution and use in source and binary forms, with or without
12237d1b14SEd Maste * modification, are permitted provided that the following conditions
13237d1b14SEd Maste * are met:
14237d1b14SEd Maste * 1. Redistributions of source code must retain the above copyright
15237d1b14SEd Maste * notice, this list of conditions and the following disclaimer.
16237d1b14SEd Maste * 2. Redistributions in binary form must reproduce the above copyright
17237d1b14SEd Maste * notice, this list of conditions and the following disclaimer in the
18237d1b14SEd Maste * documentation and/or other materials provided with the distribution.
19237d1b14SEd Maste * 3. All advertising materials mentioning features or use of this software
20237d1b14SEd Maste * must display the following acknowledgement:
21237d1b14SEd Maste * This product includes software developed by TooLs GmbH.
22237d1b14SEd Maste * 4. The name of TooLs GmbH may not be used to endorse or promote products
23237d1b14SEd Maste * derived from this software without specific prior written permission.
24237d1b14SEd Maste *
25237d1b14SEd Maste * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
26237d1b14SEd Maste * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27237d1b14SEd Maste * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28237d1b14SEd Maste * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29237d1b14SEd Maste * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30237d1b14SEd Maste * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
31237d1b14SEd Maste * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32237d1b14SEd Maste * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33237d1b14SEd Maste * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34237d1b14SEd Maste * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35237d1b14SEd Maste */
362037e988SEd Maste /*-
37237d1b14SEd Maste * Written by Paul Popelka (paulp@uts.amdahl.com)
38237d1b14SEd Maste *
39237d1b14SEd Maste * You can do anything you want with this software, just don't say you wrote
40237d1b14SEd Maste * it, and don't remove this notice.
41237d1b14SEd Maste *
42237d1b14SEd Maste * This software is provided "as is".
43237d1b14SEd Maste *
44237d1b14SEd Maste * The author supplies this software to be publicly redistributed on the
45237d1b14SEd Maste * understanding that the author is not responsible for the correct
46237d1b14SEd Maste * functioning of this software in any circumstances and is not liable for
47237d1b14SEd Maste * any damages caused by this software.
48237d1b14SEd Maste *
49237d1b14SEd Maste * October 1992
50237d1b14SEd Maste */
51237d1b14SEd Maste
52237d1b14SEd Maste #include <sys/param.h>
5398dc8da5SEd Maste #include <sys/errno.h>
54237d1b14SEd Maste #include <sys/mman.h>
5598dc8da5SEd Maste #include <sys/time.h>
5698dc8da5SEd Maste
57237d1b14SEd Maste #include <fcntl.h>
58aaa38524SConrad Meyer #include <stdbool.h>
5998dc8da5SEd Maste #include <stdio.h>
6098dc8da5SEd Maste #include <string.h>
6198dc8da5SEd Maste #include <time.h>
62237d1b14SEd Maste #include <unistd.h>
63237d1b14SEd Maste
64237d1b14SEd Maste #include <fs/msdosfs/bpb.h>
65ba2cfa80SAlex Richardson #include "msdos/denode.h"
6651e79affSEd Maste #include <fs/msdosfs/fat.h>
67840aca28SEd Maste #include <fs/msdosfs/msdosfsmount.h>
68237d1b14SEd Maste
69237d1b14SEd Maste #include "makefs.h"
70237d1b14SEd Maste #include "msdos.h"
71237d1b14SEd Maste
72237d1b14SEd Maste /*
73237d1b14SEd Maste * Some general notes:
74237d1b14SEd Maste *
75237d1b14SEd Maste * In the ufs filesystem the inodes, superblocks, and indirect blocks are
76237d1b14SEd Maste * read/written using the vnode for the filesystem. Blocks that represent
77237d1b14SEd Maste * the contents of a file are read/written using the vnode for the file
78237d1b14SEd Maste * (including directories when they are read/written as files). This
79237d1b14SEd Maste * presents problems for the dos filesystem because data that should be in
80237d1b14SEd Maste * an inode (if dos had them) resides in the directory itself. Since we
81237d1b14SEd Maste * must update directory entries without the benefit of having the vnode
82237d1b14SEd Maste * for the directory we must use the vnode for the filesystem. This means
83237d1b14SEd Maste * that when a directory is actually read/written (via read, write, or
84237d1b14SEd Maste * readdir, or seek) we must use the vnode for the filesystem instead of
85237d1b14SEd Maste * the vnode for the directory as would happen in ufs. This is to insure we
86237d1b14SEd Maste * retrieve the correct block from the buffer cache since the hash value is
87237d1b14SEd Maste * based upon the vnode address and the desired block number.
88237d1b14SEd Maste */
89237d1b14SEd Maste
90237d1b14SEd Maste static int msdosfs_wfile(const char *, struct denode *, fsnode *);
9198dc8da5SEd Maste static void unix2fattime(const struct timespec *tsp, uint16_t *ddp,
9298dc8da5SEd Maste uint16_t *dtp);
93237d1b14SEd Maste
94237d1b14SEd Maste static void
msdosfs_times(struct denode * dep,const struct stat * st)9598dc8da5SEd Maste msdosfs_times(struct denode *dep, const struct stat *st)
96237d1b14SEd Maste {
97237d1b14SEd Maste if (stampst.st_ino)
98237d1b14SEd Maste st = &stampst;
99237d1b14SEd Maste
100162ae9c8SAlex Richardson #ifdef HAVE_STRUCT_STAT_BIRTHTIME
10198dc8da5SEd Maste unix2fattime(&st->st_birthtim, &dep->de_CDate, &dep->de_CTime);
102162ae9c8SAlex Richardson #else
103162ae9c8SAlex Richardson unix2fattime(&st->st_ctim, &dep->de_CDate, &dep->de_CTime);
104162ae9c8SAlex Richardson #endif
10598dc8da5SEd Maste unix2fattime(&st->st_atim, &dep->de_ADate, NULL);
10698dc8da5SEd Maste unix2fattime(&st->st_mtim, &dep->de_MDate, &dep->de_MTime);
10798dc8da5SEd Maste }
10898dc8da5SEd Maste
10998dc8da5SEd Maste static void
unix2fattime(const struct timespec * tsp,uint16_t * ddp,uint16_t * dtp)11098dc8da5SEd Maste unix2fattime(const struct timespec *tsp, uint16_t *ddp, uint16_t *dtp)
11198dc8da5SEd Maste {
11298dc8da5SEd Maste time_t t1;
11398dc8da5SEd Maste struct tm lt = {0};
11498dc8da5SEd Maste
11598dc8da5SEd Maste t1 = tsp->tv_sec;
11698dc8da5SEd Maste localtime_r(&t1, <);
11798dc8da5SEd Maste
11898dc8da5SEd Maste unsigned long fat_time = ((lt.tm_year - 80) << 25) |
11998dc8da5SEd Maste ((lt.tm_mon + 1) << 21) |
12098dc8da5SEd Maste (lt.tm_mday << 16) |
12198dc8da5SEd Maste (lt.tm_hour << 11) |
12298dc8da5SEd Maste (lt.tm_min << 5) |
12398dc8da5SEd Maste (lt.tm_sec >> 1);
12498dc8da5SEd Maste
12598dc8da5SEd Maste if (ddp != NULL)
12698dc8da5SEd Maste *ddp = (uint16_t)(fat_time >> 16);
12798dc8da5SEd Maste if (dtp != NULL)
12898dc8da5SEd Maste *dtp = (uint16_t)fat_time;
129237d1b14SEd Maste }
130237d1b14SEd Maste
131237d1b14SEd Maste /*
132237d1b14SEd Maste * When we search a directory the blocks containing directory entries are
133237d1b14SEd Maste * read and examined. The directory entries contain information that would
134237d1b14SEd Maste * normally be in the inode of a unix filesystem. This means that some of
135237d1b14SEd Maste * a directory's contents may also be in memory resident denodes (sort of
136237d1b14SEd Maste * an inode). This can cause problems if we are searching while some other
137237d1b14SEd Maste * process is modifying a directory. To prevent one process from accessing
138237d1b14SEd Maste * incompletely modified directory information we depend upon being the
139237d1b14SEd Maste * sole owner of a directory block. bread/brelse provide this service.
140237d1b14SEd Maste * This being the case, when a process modifies a directory it must first
141237d1b14SEd Maste * acquire the disk block that contains the directory entry to be modified.
142237d1b14SEd Maste * Then update the disk block and the denode, and then write the disk block
143237d1b14SEd Maste * out to disk. This way disk blocks containing directory entries and in
144237d1b14SEd Maste * memory denode's will be in synch.
145237d1b14SEd Maste */
146237d1b14SEd Maste static int
msdosfs_findslot(struct denode * dp,struct componentname * cnp)147237d1b14SEd Maste msdosfs_findslot(struct denode *dp, struct componentname *cnp)
148237d1b14SEd Maste {
149237d1b14SEd Maste daddr_t bn;
150237d1b14SEd Maste int error;
151237d1b14SEd Maste int slotcount;
152237d1b14SEd Maste int slotoffset = 0;
153237d1b14SEd Maste int frcn;
154237d1b14SEd Maste u_long cluster;
155237d1b14SEd Maste int blkoff;
156237d1b14SEd Maste u_int diroff;
157237d1b14SEd Maste int blsize;
158237d1b14SEd Maste struct msdosfsmount *pmp;
159d485c77fSKonstantin Belousov struct m_buf *bp = 0;
160237d1b14SEd Maste struct direntry *dep;
161237d1b14SEd Maste u_char dosfilename[12];
162237d1b14SEd Maste int wincnt = 1;
163237d1b14SEd Maste int chksum = -1, chksum_ok;
164237d1b14SEd Maste int olddos = 1;
165237d1b14SEd Maste
166237d1b14SEd Maste pmp = dp->de_pmp;
167237d1b14SEd Maste
168237d1b14SEd Maste switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename,
169237d1b14SEd Maste cnp->cn_namelen, 0)) {
170237d1b14SEd Maste case 0:
171237d1b14SEd Maste return (EINVAL);
172237d1b14SEd Maste case 1:
173237d1b14SEd Maste break;
174237d1b14SEd Maste case 2:
175237d1b14SEd Maste wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
17698dc8da5SEd Maste cnp->cn_namelen) + 1;
177237d1b14SEd Maste break;
178237d1b14SEd Maste case 3:
179237d1b14SEd Maste olddos = 0;
180237d1b14SEd Maste wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
18198dc8da5SEd Maste cnp->cn_namelen) + 1;
182237d1b14SEd Maste break;
183237d1b14SEd Maste }
184237d1b14SEd Maste
185237d1b14SEd Maste if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
186237d1b14SEd Maste wincnt = 1;
187237d1b14SEd Maste
188237d1b14SEd Maste /*
189237d1b14SEd Maste * Suppress search for slots unless creating
190237d1b14SEd Maste * file and at end of pathname, in which case
191237d1b14SEd Maste * we watch for a place to put the new file in
192237d1b14SEd Maste * case it doesn't already exist.
193237d1b14SEd Maste */
194237d1b14SEd Maste slotcount = 0;
19598dc8da5SEd Maste MSDOSFS_DPRINTF(("%s(): dos filename: %s\n", __func__, dosfilename));
196237d1b14SEd Maste /*
197237d1b14SEd Maste * Search the directory pointed at by vdp for the name pointed at
198237d1b14SEd Maste * by cnp->cn_nameptr.
199237d1b14SEd Maste */
200237d1b14SEd Maste /*
201237d1b14SEd Maste * The outer loop ranges over the clusters that make up the
202237d1b14SEd Maste * directory. Note that the root directory is different from all
203237d1b14SEd Maste * other directories. It has a fixed number of blocks that are not
204237d1b14SEd Maste * part of the pool of allocatable clusters. So, we treat it a
205237d1b14SEd Maste * little differently. The root directory starts at "cluster" 0.
206237d1b14SEd Maste */
207237d1b14SEd Maste diroff = 0;
208237d1b14SEd Maste for (frcn = 0; diroff < dp->de_FileSize; frcn++) {
209237d1b14SEd Maste if ((error = pcbmap(dp, frcn, &bn, &cluster, &blsize)) != 0) {
210237d1b14SEd Maste if (error == E2BIG)
211237d1b14SEd Maste break;
212237d1b14SEd Maste return (error);
213237d1b14SEd Maste }
214d485c77fSKonstantin Belousov error = bread((void *)pmp->pm_devvp, bn, blsize, 0, &bp);
215237d1b14SEd Maste if (error) {
216237d1b14SEd Maste return (error);
217237d1b14SEd Maste }
218237d1b14SEd Maste for (blkoff = 0; blkoff < blsize;
219237d1b14SEd Maste blkoff += sizeof(struct direntry),
220237d1b14SEd Maste diroff += sizeof(struct direntry)) {
2212037e988SEd Maste dep = (struct direntry *)(bp->b_data + blkoff);
222237d1b14SEd Maste /*
223237d1b14SEd Maste * If the slot is empty and we are still looking
224237d1b14SEd Maste * for an empty then remember this one. If the
225237d1b14SEd Maste * slot is not empty then check to see if it
226237d1b14SEd Maste * matches what we are looking for. If the slot
227237d1b14SEd Maste * has never been filled with anything, then the
228237d1b14SEd Maste * remainder of the directory has never been used,
229237d1b14SEd Maste * so there is no point in searching it.
230237d1b14SEd Maste */
231237d1b14SEd Maste if (dep->deName[0] == SLOT_EMPTY ||
232237d1b14SEd Maste dep->deName[0] == SLOT_DELETED) {
233237d1b14SEd Maste /*
234237d1b14SEd Maste * Drop memory of previous long matches
235237d1b14SEd Maste */
236237d1b14SEd Maste chksum = -1;
237237d1b14SEd Maste
238237d1b14SEd Maste if (slotcount < wincnt) {
239237d1b14SEd Maste slotcount++;
240237d1b14SEd Maste slotoffset = diroff;
241237d1b14SEd Maste }
242237d1b14SEd Maste if (dep->deName[0] == SLOT_EMPTY) {
2435b292f9aSEd Maste brelse(bp);
244237d1b14SEd Maste goto notfound;
245237d1b14SEd Maste }
246237d1b14SEd Maste } else {
247237d1b14SEd Maste /*
248237d1b14SEd Maste * If there wasn't enough space for our
249237d1b14SEd Maste * winentries, forget about the empty space
250237d1b14SEd Maste */
251237d1b14SEd Maste if (slotcount < wincnt)
252237d1b14SEd Maste slotcount = 0;
253237d1b14SEd Maste
254237d1b14SEd Maste /*
255237d1b14SEd Maste * Check for Win95 long filename entry
256237d1b14SEd Maste */
257237d1b14SEd Maste if (dep->deAttributes == ATTR_WIN95) {
258237d1b14SEd Maste if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
259237d1b14SEd Maste continue;
260237d1b14SEd Maste
26198dc8da5SEd Maste chksum = winChkName(
26298dc8da5SEd Maste (const u_char *)cnp->cn_nameptr,
263237d1b14SEd Maste cnp->cn_namelen,
26498dc8da5SEd Maste (struct winentry *)dep, chksum);
265237d1b14SEd Maste continue;
266237d1b14SEd Maste }
267237d1b14SEd Maste
268237d1b14SEd Maste /*
269237d1b14SEd Maste * Ignore volume labels (anywhere, not just
270237d1b14SEd Maste * the root directory).
271237d1b14SEd Maste */
272237d1b14SEd Maste if (dep->deAttributes & ATTR_VOLUME) {
273237d1b14SEd Maste chksum = -1;
274237d1b14SEd Maste continue;
275237d1b14SEd Maste }
276237d1b14SEd Maste
277237d1b14SEd Maste /*
278237d1b14SEd Maste * Check for a checksum or name match
279237d1b14SEd Maste */
280237d1b14SEd Maste chksum_ok = (chksum == winChksum(dep->deName));
281237d1b14SEd Maste if (!chksum_ok
282237d1b14SEd Maste && (!olddos || memcmp(dosfilename, dep->deName, 11))) {
283237d1b14SEd Maste chksum = -1;
284237d1b14SEd Maste continue;
285237d1b14SEd Maste }
28698dc8da5SEd Maste MSDOSFS_DPRINTF(("%s(): match blkoff %d, diroff %u\n",
287237d1b14SEd Maste __func__, blkoff, diroff));
288237d1b14SEd Maste /*
289237d1b14SEd Maste * Remember where this directory
290237d1b14SEd Maste * entry came from for whoever did
291237d1b14SEd Maste * this lookup.
292237d1b14SEd Maste */
293237d1b14SEd Maste dp->de_fndoffset = diroff;
294237d1b14SEd Maste dp->de_fndcnt = 0;
295237d1b14SEd Maste
296237d1b14SEd Maste return EEXIST;
297237d1b14SEd Maste }
298237d1b14SEd Maste } /* for (blkoff = 0; .... */
299237d1b14SEd Maste /*
300237d1b14SEd Maste * Release the buffer holding the directory cluster just
301237d1b14SEd Maste * searched.
302237d1b14SEd Maste */
3035b292f9aSEd Maste brelse(bp);
304237d1b14SEd Maste } /* for (frcn = 0; ; frcn++) */
305237d1b14SEd Maste
306237d1b14SEd Maste notfound:
307237d1b14SEd Maste /*
308237d1b14SEd Maste * We hold no disk buffers at this point.
309237d1b14SEd Maste */
310237d1b14SEd Maste
311237d1b14SEd Maste /*
312237d1b14SEd Maste * If we get here we didn't find the entry we were looking for. But
313237d1b14SEd Maste * that's ok if we are creating or renaming and are at the end of
314237d1b14SEd Maste * the pathname and the directory hasn't been removed.
315237d1b14SEd Maste */
31698dc8da5SEd Maste MSDOSFS_DPRINTF(("%s(): refcnt %ld, slotcount %d, slotoffset %d\n",
317237d1b14SEd Maste __func__, dp->de_refcnt, slotcount, slotoffset));
318237d1b14SEd Maste /*
319237d1b14SEd Maste * Fixup the slot description to point to the place where
320237d1b14SEd Maste * we might put the new DOS direntry (putting the Win95
321237d1b14SEd Maste * long name entries before that)
322237d1b14SEd Maste */
323237d1b14SEd Maste if (!slotcount) {
324237d1b14SEd Maste slotcount = 1;
325237d1b14SEd Maste slotoffset = diroff;
326237d1b14SEd Maste }
327237d1b14SEd Maste if (wincnt > slotcount) {
328237d1b14SEd Maste slotoffset += sizeof(struct direntry) * (wincnt - slotcount);
329237d1b14SEd Maste }
330237d1b14SEd Maste
331237d1b14SEd Maste /*
332237d1b14SEd Maste * Return an indication of where the new directory
333237d1b14SEd Maste * entry should be put.
334237d1b14SEd Maste */
335237d1b14SEd Maste dp->de_fndoffset = slotoffset;
336237d1b14SEd Maste dp->de_fndcnt = wincnt - 1;
337237d1b14SEd Maste
338237d1b14SEd Maste /*
339237d1b14SEd Maste * We return with the directory locked, so that
340237d1b14SEd Maste * the parameters we set up above will still be
341237d1b14SEd Maste * valid if we actually decide to do a direnter().
342237d1b14SEd Maste * We return ni_vp == NULL to indicate that the entry
343237d1b14SEd Maste * does not currently exist; we leave a pointer to
344237d1b14SEd Maste * the (locked) directory inode in ndp->ni_dvp.
345237d1b14SEd Maste *
346237d1b14SEd Maste * NB - if the directory is unlocked, then this
347237d1b14SEd Maste * information cannot be used.
348237d1b14SEd Maste */
349237d1b14SEd Maste return 0;
350237d1b14SEd Maste }
351237d1b14SEd Maste
352237d1b14SEd Maste /*
353237d1b14SEd Maste * Create a regular file. On entry the directory to contain the file being
354237d1b14SEd Maste * created is locked. We must release before we return.
355237d1b14SEd Maste */
356237d1b14SEd Maste struct denode *
msdosfs_mkfile(const char * path,struct denode * pdep,fsnode * node)357237d1b14SEd Maste msdosfs_mkfile(const char *path, struct denode *pdep, fsnode *node)
358237d1b14SEd Maste {
359237d1b14SEd Maste struct componentname cn;
360237d1b14SEd Maste struct denode ndirent;
361237d1b14SEd Maste struct denode *dep;
362237d1b14SEd Maste int error;
363237d1b14SEd Maste struct stat *st = &node->inode->st;
364237d1b14SEd Maste
365237d1b14SEd Maste cn.cn_nameptr = node->name;
366237d1b14SEd Maste cn.cn_namelen = strlen(node->name);
367237d1b14SEd Maste
36898dc8da5SEd Maste MSDOSFS_DPRINTF(("%s(name %s, mode 0%o size %zu)\n",
36998dc8da5SEd Maste __func__, node->name, st->st_mode, (size_t)st->st_size));
370237d1b14SEd Maste
371237d1b14SEd Maste /*
372237d1b14SEd Maste * If this is the root directory and there is no space left we
373237d1b14SEd Maste * can't do anything. This is because the root directory can not
374237d1b14SEd Maste * change size.
375237d1b14SEd Maste */
376237d1b14SEd Maste if (pdep->de_StartCluster == MSDOSFSROOT
377237d1b14SEd Maste && pdep->de_fndoffset >= pdep->de_FileSize) {
378237d1b14SEd Maste error = ENOSPC;
379237d1b14SEd Maste goto bad;
380237d1b14SEd Maste }
381237d1b14SEd Maste
382237d1b14SEd Maste /*
383237d1b14SEd Maste * Create a directory entry for the file, then call createde() to
384237d1b14SEd Maste * have it installed. NOTE: DOS files are always executable. We
385237d1b14SEd Maste * use the absence of the owner write bit to make the file
386237d1b14SEd Maste * readonly.
387237d1b14SEd Maste */
388237d1b14SEd Maste memset(&ndirent, 0, sizeof(ndirent));
389237d1b14SEd Maste if ((error = uniqdosname(pdep, &cn, ndirent.de_Name)) != 0)
390237d1b14SEd Maste goto bad;
391237d1b14SEd Maste
392237d1b14SEd Maste ndirent.de_Attributes = (st->st_mode & S_IWUSR) ?
393237d1b14SEd Maste ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
394237d1b14SEd Maste ndirent.de_StartCluster = 0;
395237d1b14SEd Maste ndirent.de_FileSize = 0;
396237d1b14SEd Maste ndirent.de_pmp = pdep->de_pmp;
397237d1b14SEd Maste ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
39898dc8da5SEd Maste msdosfs_times(&ndirent, &node->inode->st);
39998dc8da5SEd Maste
400237d1b14SEd Maste if ((error = msdosfs_findslot(pdep, &cn)) != 0)
401237d1b14SEd Maste goto bad;
402237d1b14SEd Maste if ((error = createde(&ndirent, pdep, &dep, &cn)) != 0)
403237d1b14SEd Maste goto bad;
404237d1b14SEd Maste if ((error = msdosfs_wfile(path, dep, node)) != 0)
405237d1b14SEd Maste goto bad;
406237d1b14SEd Maste return dep;
407237d1b14SEd Maste
408237d1b14SEd Maste bad:
409237d1b14SEd Maste errno = error;
410237d1b14SEd Maste return NULL;
411237d1b14SEd Maste }
412237d1b14SEd Maste static int
msdosfs_updatede(struct denode * dep)413237d1b14SEd Maste msdosfs_updatede(struct denode *dep)
414237d1b14SEd Maste {
415d485c77fSKonstantin Belousov struct m_buf *bp;
416237d1b14SEd Maste struct direntry *dirp;
417237d1b14SEd Maste int error;
418237d1b14SEd Maste
419237d1b14SEd Maste dep->de_flag &= ~DE_MODIFIED;
420d485c77fSKonstantin Belousov error = m_readde(dep, &bp, &dirp);
421237d1b14SEd Maste if (error)
422237d1b14SEd Maste return error;
423237d1b14SEd Maste DE_EXTERNALIZE(dirp, dep);
424237d1b14SEd Maste error = bwrite(bp);
425237d1b14SEd Maste return error;
426237d1b14SEd Maste }
427237d1b14SEd Maste
428237d1b14SEd Maste /*
429237d1b14SEd Maste * Write data to a file or directory.
430237d1b14SEd Maste */
431237d1b14SEd Maste static int
msdosfs_wfile(const char * path,struct denode * dep,fsnode * node)432237d1b14SEd Maste msdosfs_wfile(const char *path, struct denode *dep, fsnode *node)
433237d1b14SEd Maste {
434237d1b14SEd Maste int error, fd;
435237d1b14SEd Maste size_t osize = dep->de_FileSize;
436237d1b14SEd Maste struct stat *st = &node->inode->st;
437237d1b14SEd Maste size_t nsize, offs;
438237d1b14SEd Maste struct msdosfsmount *pmp = dep->de_pmp;
439d485c77fSKonstantin Belousov struct m_buf *bp;
440237d1b14SEd Maste char *dat;
441237d1b14SEd Maste u_long cn = 0;
442237d1b14SEd Maste
443237d1b14SEd Maste error = 0; /* XXX: gcc/vax */
44498dc8da5SEd Maste MSDOSFS_DPRINTF(("%s(diroff %lu, dirclust %lu, startcluster %lu)\n",
44598dc8da5SEd Maste __func__, dep->de_diroffset, dep->de_dirclust,
44698dc8da5SEd Maste dep->de_StartCluster));
447237d1b14SEd Maste if (st->st_size == 0)
448237d1b14SEd Maste return 0;
449237d1b14SEd Maste
450237d1b14SEd Maste /* Don't bother to try to write files larger than the fs limit */
451237d1b14SEd Maste if (st->st_size > MSDOSFS_FILESIZE_MAX)
452237d1b14SEd Maste return EFBIG;
453237d1b14SEd Maste
454237d1b14SEd Maste nsize = st->st_size;
45598dc8da5SEd Maste MSDOSFS_DPRINTF(("%s(nsize=%zu, osize=%zu)\n", __func__, nsize, osize));
456237d1b14SEd Maste if (nsize > osize) {
457476b0ab7SEd Maste if ((error = deextend(dep, nsize, NULL)) != 0)
458237d1b14SEd Maste return error;
459237d1b14SEd Maste if ((error = msdosfs_updatede(dep)) != 0)
460237d1b14SEd Maste return error;
461237d1b14SEd Maste }
462237d1b14SEd Maste
463237d1b14SEd Maste if ((fd = open(path, O_RDONLY)) == -1) {
464237d1b14SEd Maste error = errno;
46506e20d1bSAlex Richardson fprintf(stderr, "open %s: %s\n", path, strerror(error));
466237d1b14SEd Maste return error;
467237d1b14SEd Maste }
468237d1b14SEd Maste
469237d1b14SEd Maste if ((dat = mmap(0, nsize, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0))
470237d1b14SEd Maste == MAP_FAILED) {
471237d1b14SEd Maste error = errno;
47206e20d1bSAlex Richardson fprintf(stderr, "%s: mmap %s: %s\n", __func__, node->name,
47306e20d1bSAlex Richardson strerror(error));
474237d1b14SEd Maste close(fd);
475237d1b14SEd Maste goto out;
476237d1b14SEd Maste }
477237d1b14SEd Maste close(fd);
478237d1b14SEd Maste
479237d1b14SEd Maste for (offs = 0; offs < nsize;) {
480237d1b14SEd Maste int blsize, cpsize;
481237d1b14SEd Maste daddr_t bn;
482237d1b14SEd Maste u_long on = offs & pmp->pm_crbomask;
48398dc8da5SEd Maste
484237d1b14SEd Maste if ((error = pcbmap(dep, cn++, &bn, NULL, &blsize)) != 0) {
48598dc8da5SEd Maste MSDOSFS_DPRINTF(("%s: pcbmap %lu",
48698dc8da5SEd Maste __func__, (unsigned long)bn));
487237d1b14SEd Maste goto out;
488237d1b14SEd Maste }
48998dc8da5SEd Maste
49098dc8da5SEd Maste MSDOSFS_DPRINTF(("%s(cn=%lu, bn=%llu, blsize=%d)\n",
49198dc8da5SEd Maste __func__, cn, (unsigned long long)bn, blsize));
492d485c77fSKonstantin Belousov if ((error = bread((void *)pmp->pm_devvp, bn, blsize, 0,
493d485c77fSKonstantin Belousov &bp)) != 0) {
49498dc8da5SEd Maste MSDOSFS_DPRINTF(("bread %d\n", error));
495237d1b14SEd Maste goto out;
496237d1b14SEd Maste }
497237d1b14SEd Maste cpsize = MIN((nsize - offs), blsize - on);
4982037e988SEd Maste memcpy(bp->b_data + on, dat + offs, cpsize);
499237d1b14SEd Maste bwrite(bp);
500237d1b14SEd Maste offs += cpsize;
501237d1b14SEd Maste }
502237d1b14SEd Maste
503237d1b14SEd Maste munmap(dat, nsize);
504237d1b14SEd Maste return 0;
505237d1b14SEd Maste out:
506237d1b14SEd Maste munmap(dat, nsize);
507237d1b14SEd Maste return error;
508237d1b14SEd Maste }
509237d1b14SEd Maste
510237d1b14SEd Maste static const struct {
511237d1b14SEd Maste struct direntry dot;
512237d1b14SEd Maste struct direntry dotdot;
513237d1b14SEd Maste } dosdirtemplate = {
51498dc8da5SEd Maste { ". ", /* the . entry */
515237d1b14SEd Maste ATTR_DIRECTORY, /* file attribute */
516237d1b14SEd Maste 0, /* reserved */
517237d1b14SEd Maste 0, { 0, 0 }, { 0, 0 }, /* create time & date */
518237d1b14SEd Maste { 0, 0 }, /* access date */
519237d1b14SEd Maste { 0, 0 }, /* high bits of start cluster */
520237d1b14SEd Maste { 210, 4 }, { 210, 4 }, /* modify time & date */
521237d1b14SEd Maste { 0, 0 }, /* startcluster */
522237d1b14SEd Maste { 0, 0, 0, 0 } /* filesize */
523237d1b14SEd Maste },
52498dc8da5SEd Maste { ".. ", /* the .. entry */
525237d1b14SEd Maste ATTR_DIRECTORY, /* file attribute */
526237d1b14SEd Maste 0, /* reserved */
527237d1b14SEd Maste 0, { 0, 0 }, { 0, 0 }, /* create time & date */
528237d1b14SEd Maste { 0, 0 }, /* access date */
529237d1b14SEd Maste { 0, 0 }, /* high bits of start cluster */
530237d1b14SEd Maste { 210, 4 }, { 210, 4 }, /* modify time & date */
531237d1b14SEd Maste { 0, 0 }, /* startcluster */
532237d1b14SEd Maste { 0, 0, 0, 0 } /* filesize */
533237d1b14SEd Maste }
534237d1b14SEd Maste };
535237d1b14SEd Maste
536237d1b14SEd Maste struct denode *
msdosfs_mkdire(const char * path __unused,struct denode * pdep,fsnode * node)537*cc1a53bcSMark Johnston msdosfs_mkdire(const char *path __unused, struct denode *pdep, fsnode *node)
538*cc1a53bcSMark Johnston {
539237d1b14SEd Maste struct denode ndirent;
540237d1b14SEd Maste struct denode *dep;
541237d1b14SEd Maste struct componentname cn;
542237d1b14SEd Maste struct msdosfsmount *pmp = pdep->de_pmp;
543237d1b14SEd Maste int error;
544237d1b14SEd Maste u_long newcluster, pcl, bn;
545237d1b14SEd Maste struct direntry *denp;
546d485c77fSKonstantin Belousov struct m_buf *bp;
547237d1b14SEd Maste
548237d1b14SEd Maste cn.cn_nameptr = node->name;
549237d1b14SEd Maste cn.cn_namelen = strlen(node->name);
550237d1b14SEd Maste /*
551237d1b14SEd Maste * If this is the root directory and there is no space left we
552237d1b14SEd Maste * can't do anything. This is because the root directory can not
553237d1b14SEd Maste * change size.
554237d1b14SEd Maste */
555237d1b14SEd Maste if (pdep->de_StartCluster == MSDOSFSROOT
556237d1b14SEd Maste && pdep->de_fndoffset >= pdep->de_FileSize) {
557237d1b14SEd Maste error = ENOSPC;
558237d1b14SEd Maste goto bad2;
559237d1b14SEd Maste }
560237d1b14SEd Maste
561237d1b14SEd Maste /*
562237d1b14SEd Maste * Allocate a cluster to hold the about to be created directory.
563237d1b14SEd Maste */
56498dc8da5SEd Maste error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
565237d1b14SEd Maste if (error)
566237d1b14SEd Maste goto bad2;
567237d1b14SEd Maste
568237d1b14SEd Maste memset(&ndirent, 0, sizeof(ndirent));
569237d1b14SEd Maste ndirent.de_pmp = pmp;
570237d1b14SEd Maste ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
57198dc8da5SEd Maste msdosfs_times(&ndirent, &node->inode->st);
572237d1b14SEd Maste
573237d1b14SEd Maste /*
574237d1b14SEd Maste * Now fill the cluster with the "." and ".." entries. And write
575237d1b14SEd Maste * the cluster to disk. This way it is there for the parent
576237d1b14SEd Maste * directory to be pointing at if there were a crash.
577237d1b14SEd Maste */
578237d1b14SEd Maste bn = cntobn(pmp, newcluster);
57998dc8da5SEd Maste MSDOSFS_DPRINTF(("%s(newcluster %lu, bn=%lu)\n",
58098dc8da5SEd Maste __func__, newcluster, bn));
581237d1b14SEd Maste /* always succeeds */
582d485c77fSKonstantin Belousov bp = getblk((void *)pmp->pm_devvp, bn, pmp->pm_bpcluster, 0, 0, 0);
583237d1b14SEd Maste memset(bp->b_data, 0, pmp->pm_bpcluster);
584237d1b14SEd Maste memcpy(bp->b_data, &dosdirtemplate, sizeof dosdirtemplate);
585237d1b14SEd Maste denp = (struct direntry *)bp->b_data;
586237d1b14SEd Maste putushort(denp[0].deStartCluster, newcluster);
587237d1b14SEd Maste putushort(denp[0].deCDate, ndirent.de_CDate);
588237d1b14SEd Maste putushort(denp[0].deCTime, ndirent.de_CTime);
589237d1b14SEd Maste denp[0].deCHundredth = ndirent.de_CHun;
590237d1b14SEd Maste putushort(denp[0].deADate, ndirent.de_ADate);
591237d1b14SEd Maste putushort(denp[0].deMDate, ndirent.de_MDate);
592237d1b14SEd Maste putushort(denp[0].deMTime, ndirent.de_MTime);
593237d1b14SEd Maste pcl = pdep->de_StartCluster;
59498dc8da5SEd Maste MSDOSFS_DPRINTF(("%s(pcl %lu, rootdirblk=%lu)\n", __func__, pcl,
595237d1b14SEd Maste pmp->pm_rootdirblk));
596237d1b14SEd Maste if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
597237d1b14SEd Maste pcl = 0;
598237d1b14SEd Maste putushort(denp[1].deStartCluster, pcl);
599237d1b14SEd Maste putushort(denp[1].deCDate, ndirent.de_CDate);
600237d1b14SEd Maste putushort(denp[1].deCTime, ndirent.de_CTime);
601237d1b14SEd Maste denp[1].deCHundredth = ndirent.de_CHun;
602237d1b14SEd Maste putushort(denp[1].deADate, ndirent.de_ADate);
603237d1b14SEd Maste putushort(denp[1].deMDate, ndirent.de_MDate);
604237d1b14SEd Maste putushort(denp[1].deMTime, ndirent.de_MTime);
605237d1b14SEd Maste if (FAT32(pmp)) {
606237d1b14SEd Maste putushort(denp[0].deHighClust, newcluster >> 16);
607237d1b14SEd Maste putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
608237d1b14SEd Maste } else {
609237d1b14SEd Maste putushort(denp[0].deHighClust, 0);
610237d1b14SEd Maste putushort(denp[1].deHighClust, 0);
611237d1b14SEd Maste }
612237d1b14SEd Maste
613237d1b14SEd Maste if ((error = bwrite(bp)) != 0)
614237d1b14SEd Maste goto bad;
615237d1b14SEd Maste
616237d1b14SEd Maste /*
617237d1b14SEd Maste * Now build up a directory entry pointing to the newly allocated
618237d1b14SEd Maste * cluster. This will be written to an empty slot in the parent
619237d1b14SEd Maste * directory.
620237d1b14SEd Maste */
621237d1b14SEd Maste if ((error = uniqdosname(pdep, &cn, ndirent.de_Name)) != 0)
622237d1b14SEd Maste goto bad;
623237d1b14SEd Maste
624237d1b14SEd Maste ndirent.de_Attributes = ATTR_DIRECTORY;
625237d1b14SEd Maste ndirent.de_StartCluster = newcluster;
626237d1b14SEd Maste ndirent.de_FileSize = 0;
627237d1b14SEd Maste ndirent.de_pmp = pdep->de_pmp;
628237d1b14SEd Maste if ((error = msdosfs_findslot(pdep, &cn)) != 0)
629237d1b14SEd Maste goto bad;
630237d1b14SEd Maste if ((error = createde(&ndirent, pdep, &dep, &cn)) != 0)
631237d1b14SEd Maste goto bad;
632237d1b14SEd Maste if ((error = msdosfs_updatede(dep)) != 0)
633237d1b14SEd Maste goto bad;
634237d1b14SEd Maste return dep;
635237d1b14SEd Maste
636237d1b14SEd Maste bad:
63765990b68SKonstantin Belousov clusterfree(pmp, newcluster);
638237d1b14SEd Maste bad2:
639237d1b14SEd Maste errno = error;
640237d1b14SEd Maste return NULL;
641237d1b14SEd Maste }
642