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
98fb2e9656SJessica Clarke #if HAVE_STRUCT_STAT_BIRTHTIME
9998dc8da5SEd Maste unix2fattime(&st->st_birthtim, &dep->de_CDate, &dep->de_CTime);
100162ae9c8SAlex Richardson #else
101162ae9c8SAlex Richardson unix2fattime(&st->st_ctim, &dep->de_CDate, &dep->de_CTime);
102162ae9c8SAlex Richardson #endif
10398dc8da5SEd Maste unix2fattime(&st->st_atim, &dep->de_ADate, NULL);
10498dc8da5SEd Maste unix2fattime(&st->st_mtim, &dep->de_MDate, &dep->de_MTime);
10598dc8da5SEd Maste }
10698dc8da5SEd Maste
10798dc8da5SEd Maste static void
unix2fattime(const struct timespec * tsp,uint16_t * ddp,uint16_t * dtp)10898dc8da5SEd Maste unix2fattime(const struct timespec *tsp, uint16_t *ddp, uint16_t *dtp)
10998dc8da5SEd Maste {
11098dc8da5SEd Maste time_t t1;
11198dc8da5SEd Maste struct tm lt = {0};
11298dc8da5SEd Maste
11398dc8da5SEd Maste t1 = tsp->tv_sec;
114*018493d3SMark Johnston gmtime_r(&t1, <);
11598dc8da5SEd Maste
11698dc8da5SEd Maste unsigned long fat_time = ((lt.tm_year - 80) << 25) |
11798dc8da5SEd Maste ((lt.tm_mon + 1) << 21) |
11898dc8da5SEd Maste (lt.tm_mday << 16) |
11998dc8da5SEd Maste (lt.tm_hour << 11) |
12098dc8da5SEd Maste (lt.tm_min << 5) |
12198dc8da5SEd Maste (lt.tm_sec >> 1);
12298dc8da5SEd Maste
12398dc8da5SEd Maste if (ddp != NULL)
12498dc8da5SEd Maste *ddp = (uint16_t)(fat_time >> 16);
12598dc8da5SEd Maste if (dtp != NULL)
12698dc8da5SEd Maste *dtp = (uint16_t)fat_time;
127237d1b14SEd Maste }
128237d1b14SEd Maste
129237d1b14SEd Maste /*
130237d1b14SEd Maste * When we search a directory the blocks containing directory entries are
131237d1b14SEd Maste * read and examined. The directory entries contain information that would
132237d1b14SEd Maste * normally be in the inode of a unix filesystem. This means that some of
133237d1b14SEd Maste * a directory's contents may also be in memory resident denodes (sort of
134237d1b14SEd Maste * an inode). This can cause problems if we are searching while some other
135237d1b14SEd Maste * process is modifying a directory. To prevent one process from accessing
136237d1b14SEd Maste * incompletely modified directory information we depend upon being the
137237d1b14SEd Maste * sole owner of a directory block. bread/brelse provide this service.
138237d1b14SEd Maste * This being the case, when a process modifies a directory it must first
139237d1b14SEd Maste * acquire the disk block that contains the directory entry to be modified.
140237d1b14SEd Maste * Then update the disk block and the denode, and then write the disk block
141237d1b14SEd Maste * out to disk. This way disk blocks containing directory entries and in
142237d1b14SEd Maste * memory denode's will be in synch.
143237d1b14SEd Maste */
144237d1b14SEd Maste static int
msdosfs_findslot(struct denode * dp,struct componentname * cnp)145237d1b14SEd Maste msdosfs_findslot(struct denode *dp, struct componentname *cnp)
146237d1b14SEd Maste {
147237d1b14SEd Maste daddr_t bn;
148237d1b14SEd Maste int error;
149237d1b14SEd Maste int slotcount;
150237d1b14SEd Maste int slotoffset = 0;
151237d1b14SEd Maste int frcn;
152237d1b14SEd Maste u_long cluster;
153237d1b14SEd Maste int blkoff;
154237d1b14SEd Maste u_int diroff;
155237d1b14SEd Maste int blsize;
156237d1b14SEd Maste struct msdosfsmount *pmp;
157d485c77fSKonstantin Belousov struct m_buf *bp = 0;
158237d1b14SEd Maste struct direntry *dep;
159237d1b14SEd Maste u_char dosfilename[12];
160237d1b14SEd Maste int wincnt = 1;
161237d1b14SEd Maste int chksum = -1, chksum_ok;
162237d1b14SEd Maste int olddos = 1;
163237d1b14SEd Maste
164237d1b14SEd Maste pmp = dp->de_pmp;
165237d1b14SEd Maste
166237d1b14SEd Maste switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename,
167237d1b14SEd Maste cnp->cn_namelen, 0)) {
168237d1b14SEd Maste case 0:
169237d1b14SEd Maste return (EINVAL);
170237d1b14SEd Maste case 1:
171237d1b14SEd Maste break;
172237d1b14SEd Maste case 2:
173237d1b14SEd Maste wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
17498dc8da5SEd Maste cnp->cn_namelen) + 1;
175237d1b14SEd Maste break;
176237d1b14SEd Maste case 3:
177237d1b14SEd Maste olddos = 0;
178237d1b14SEd Maste wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
17998dc8da5SEd Maste cnp->cn_namelen) + 1;
180237d1b14SEd Maste break;
181237d1b14SEd Maste }
182237d1b14SEd Maste
183237d1b14SEd Maste if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
184237d1b14SEd Maste wincnt = 1;
185237d1b14SEd Maste
186237d1b14SEd Maste /*
187237d1b14SEd Maste * Suppress search for slots unless creating
188237d1b14SEd Maste * file and at end of pathname, in which case
189237d1b14SEd Maste * we watch for a place to put the new file in
190237d1b14SEd Maste * case it doesn't already exist.
191237d1b14SEd Maste */
192237d1b14SEd Maste slotcount = 0;
19398dc8da5SEd Maste MSDOSFS_DPRINTF(("%s(): dos filename: %s\n", __func__, dosfilename));
194237d1b14SEd Maste /*
195237d1b14SEd Maste * Search the directory pointed at by vdp for the name pointed at
196237d1b14SEd Maste * by cnp->cn_nameptr.
197237d1b14SEd Maste */
198237d1b14SEd Maste /*
199237d1b14SEd Maste * The outer loop ranges over the clusters that make up the
200237d1b14SEd Maste * directory. Note that the root directory is different from all
201237d1b14SEd Maste * other directories. It has a fixed number of blocks that are not
202237d1b14SEd Maste * part of the pool of allocatable clusters. So, we treat it a
203237d1b14SEd Maste * little differently. The root directory starts at "cluster" 0.
204237d1b14SEd Maste */
205237d1b14SEd Maste diroff = 0;
206237d1b14SEd Maste for (frcn = 0; diroff < dp->de_FileSize; frcn++) {
207237d1b14SEd Maste if ((error = pcbmap(dp, frcn, &bn, &cluster, &blsize)) != 0) {
208237d1b14SEd Maste if (error == E2BIG)
209237d1b14SEd Maste break;
210237d1b14SEd Maste return (error);
211237d1b14SEd Maste }
212d485c77fSKonstantin Belousov error = bread((void *)pmp->pm_devvp, bn, blsize, 0, &bp);
213237d1b14SEd Maste if (error) {
214237d1b14SEd Maste return (error);
215237d1b14SEd Maste }
216237d1b14SEd Maste for (blkoff = 0; blkoff < blsize;
217237d1b14SEd Maste blkoff += sizeof(struct direntry),
218237d1b14SEd Maste diroff += sizeof(struct direntry)) {
2192037e988SEd Maste dep = (struct direntry *)(bp->b_data + blkoff);
220237d1b14SEd Maste /*
221237d1b14SEd Maste * If the slot is empty and we are still looking
222237d1b14SEd Maste * for an empty then remember this one. If the
223237d1b14SEd Maste * slot is not empty then check to see if it
224237d1b14SEd Maste * matches what we are looking for. If the slot
225237d1b14SEd Maste * has never been filled with anything, then the
226237d1b14SEd Maste * remainder of the directory has never been used,
227237d1b14SEd Maste * so there is no point in searching it.
228237d1b14SEd Maste */
229237d1b14SEd Maste if (dep->deName[0] == SLOT_EMPTY ||
230237d1b14SEd Maste dep->deName[0] == SLOT_DELETED) {
231237d1b14SEd Maste /*
232237d1b14SEd Maste * Drop memory of previous long matches
233237d1b14SEd Maste */
234237d1b14SEd Maste chksum = -1;
235237d1b14SEd Maste
236237d1b14SEd Maste if (slotcount < wincnt) {
237237d1b14SEd Maste slotcount++;
238237d1b14SEd Maste slotoffset = diroff;
239237d1b14SEd Maste }
240237d1b14SEd Maste if (dep->deName[0] == SLOT_EMPTY) {
2415b292f9aSEd Maste brelse(bp);
242237d1b14SEd Maste goto notfound;
243237d1b14SEd Maste }
244237d1b14SEd Maste } else {
245237d1b14SEd Maste /*
246237d1b14SEd Maste * If there wasn't enough space for our
247237d1b14SEd Maste * winentries, forget about the empty space
248237d1b14SEd Maste */
249237d1b14SEd Maste if (slotcount < wincnt)
250237d1b14SEd Maste slotcount = 0;
251237d1b14SEd Maste
252237d1b14SEd Maste /*
253237d1b14SEd Maste * Check for Win95 long filename entry
254237d1b14SEd Maste */
255237d1b14SEd Maste if (dep->deAttributes == ATTR_WIN95) {
256237d1b14SEd Maste if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
257237d1b14SEd Maste continue;
258237d1b14SEd Maste
25998dc8da5SEd Maste chksum = winChkName(
26098dc8da5SEd Maste (const u_char *)cnp->cn_nameptr,
261237d1b14SEd Maste cnp->cn_namelen,
26298dc8da5SEd Maste (struct winentry *)dep, chksum);
263237d1b14SEd Maste continue;
264237d1b14SEd Maste }
265237d1b14SEd Maste
266237d1b14SEd Maste /*
267237d1b14SEd Maste * Ignore volume labels (anywhere, not just
268237d1b14SEd Maste * the root directory).
269237d1b14SEd Maste */
270237d1b14SEd Maste if (dep->deAttributes & ATTR_VOLUME) {
271237d1b14SEd Maste chksum = -1;
272237d1b14SEd Maste continue;
273237d1b14SEd Maste }
274237d1b14SEd Maste
275237d1b14SEd Maste /*
276237d1b14SEd Maste * Check for a checksum or name match
277237d1b14SEd Maste */
278237d1b14SEd Maste chksum_ok = (chksum == winChksum(dep->deName));
279237d1b14SEd Maste if (!chksum_ok
280237d1b14SEd Maste && (!olddos || memcmp(dosfilename, dep->deName, 11))) {
281237d1b14SEd Maste chksum = -1;
282237d1b14SEd Maste continue;
283237d1b14SEd Maste }
28498dc8da5SEd Maste MSDOSFS_DPRINTF(("%s(): match blkoff %d, diroff %u\n",
285237d1b14SEd Maste __func__, blkoff, diroff));
286237d1b14SEd Maste /*
287237d1b14SEd Maste * Remember where this directory
288237d1b14SEd Maste * entry came from for whoever did
289237d1b14SEd Maste * this lookup.
290237d1b14SEd Maste */
291237d1b14SEd Maste dp->de_fndoffset = diroff;
292237d1b14SEd Maste dp->de_fndcnt = 0;
293237d1b14SEd Maste
294237d1b14SEd Maste return EEXIST;
295237d1b14SEd Maste }
296237d1b14SEd Maste } /* for (blkoff = 0; .... */
297237d1b14SEd Maste /*
298237d1b14SEd Maste * Release the buffer holding the directory cluster just
299237d1b14SEd Maste * searched.
300237d1b14SEd Maste */
3015b292f9aSEd Maste brelse(bp);
302237d1b14SEd Maste } /* for (frcn = 0; ; frcn++) */
303237d1b14SEd Maste
304237d1b14SEd Maste notfound:
305237d1b14SEd Maste /*
306237d1b14SEd Maste * We hold no disk buffers at this point.
307237d1b14SEd Maste */
308237d1b14SEd Maste
309237d1b14SEd Maste /*
310237d1b14SEd Maste * If we get here we didn't find the entry we were looking for. But
311237d1b14SEd Maste * that's ok if we are creating or renaming and are at the end of
312237d1b14SEd Maste * the pathname and the directory hasn't been removed.
313237d1b14SEd Maste */
31498dc8da5SEd Maste MSDOSFS_DPRINTF(("%s(): refcnt %ld, slotcount %d, slotoffset %d\n",
315237d1b14SEd Maste __func__, dp->de_refcnt, slotcount, slotoffset));
316237d1b14SEd Maste /*
317237d1b14SEd Maste * Fixup the slot description to point to the place where
318237d1b14SEd Maste * we might put the new DOS direntry (putting the Win95
319237d1b14SEd Maste * long name entries before that)
320237d1b14SEd Maste */
321237d1b14SEd Maste if (!slotcount) {
322237d1b14SEd Maste slotcount = 1;
323237d1b14SEd Maste slotoffset = diroff;
324237d1b14SEd Maste }
325237d1b14SEd Maste if (wincnt > slotcount) {
326237d1b14SEd Maste slotoffset += sizeof(struct direntry) * (wincnt - slotcount);
327237d1b14SEd Maste }
328237d1b14SEd Maste
329237d1b14SEd Maste /*
330237d1b14SEd Maste * Return an indication of where the new directory
331237d1b14SEd Maste * entry should be put.
332237d1b14SEd Maste */
333237d1b14SEd Maste dp->de_fndoffset = slotoffset;
334237d1b14SEd Maste dp->de_fndcnt = wincnt - 1;
335237d1b14SEd Maste
336237d1b14SEd Maste /*
337237d1b14SEd Maste * We return with the directory locked, so that
338237d1b14SEd Maste * the parameters we set up above will still be
339237d1b14SEd Maste * valid if we actually decide to do a direnter().
340237d1b14SEd Maste * We return ni_vp == NULL to indicate that the entry
341237d1b14SEd Maste * does not currently exist; we leave a pointer to
342237d1b14SEd Maste * the (locked) directory inode in ndp->ni_dvp.
343237d1b14SEd Maste *
344237d1b14SEd Maste * NB - if the directory is unlocked, then this
345237d1b14SEd Maste * information cannot be used.
346237d1b14SEd Maste */
347237d1b14SEd Maste return 0;
348237d1b14SEd Maste }
349237d1b14SEd Maste
350237d1b14SEd Maste /*
351237d1b14SEd Maste * Create a regular file. On entry the directory to contain the file being
352237d1b14SEd Maste * created is locked. We must release before we return.
353237d1b14SEd Maste */
354237d1b14SEd Maste struct denode *
msdosfs_mkfile(const char * path,struct denode * pdep,fsnode * node)355237d1b14SEd Maste msdosfs_mkfile(const char *path, struct denode *pdep, fsnode *node)
356237d1b14SEd Maste {
357237d1b14SEd Maste struct componentname cn;
358237d1b14SEd Maste struct denode ndirent;
359237d1b14SEd Maste struct denode *dep;
360237d1b14SEd Maste int error;
361237d1b14SEd Maste struct stat *st = &node->inode->st;
362237d1b14SEd Maste
363237d1b14SEd Maste cn.cn_nameptr = node->name;
364237d1b14SEd Maste cn.cn_namelen = strlen(node->name);
365237d1b14SEd Maste
36698dc8da5SEd Maste MSDOSFS_DPRINTF(("%s(name %s, mode 0%o size %zu)\n",
36798dc8da5SEd Maste __func__, node->name, st->st_mode, (size_t)st->st_size));
368237d1b14SEd Maste
369237d1b14SEd Maste /*
370237d1b14SEd Maste * If this is the root directory and there is no space left we
371237d1b14SEd Maste * can't do anything. This is because the root directory can not
372237d1b14SEd Maste * change size.
373237d1b14SEd Maste */
374237d1b14SEd Maste if (pdep->de_StartCluster == MSDOSFSROOT
375237d1b14SEd Maste && pdep->de_fndoffset >= pdep->de_FileSize) {
376237d1b14SEd Maste error = ENOSPC;
377237d1b14SEd Maste goto bad;
378237d1b14SEd Maste }
379237d1b14SEd Maste
380237d1b14SEd Maste /*
381237d1b14SEd Maste * Create a directory entry for the file, then call createde() to
382237d1b14SEd Maste * have it installed. NOTE: DOS files are always executable. We
383237d1b14SEd Maste * use the absence of the owner write bit to make the file
384237d1b14SEd Maste * readonly.
385237d1b14SEd Maste */
386237d1b14SEd Maste memset(&ndirent, 0, sizeof(ndirent));
387237d1b14SEd Maste if ((error = uniqdosname(pdep, &cn, ndirent.de_Name)) != 0)
388237d1b14SEd Maste goto bad;
389237d1b14SEd Maste
390237d1b14SEd Maste ndirent.de_Attributes = (st->st_mode & S_IWUSR) ?
391237d1b14SEd Maste ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
392237d1b14SEd Maste ndirent.de_StartCluster = 0;
393237d1b14SEd Maste ndirent.de_FileSize = 0;
394237d1b14SEd Maste ndirent.de_pmp = pdep->de_pmp;
395237d1b14SEd Maste ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
39698dc8da5SEd Maste msdosfs_times(&ndirent, &node->inode->st);
39798dc8da5SEd Maste
398237d1b14SEd Maste if ((error = msdosfs_findslot(pdep, &cn)) != 0)
399237d1b14SEd Maste goto bad;
400237d1b14SEd Maste if ((error = createde(&ndirent, pdep, &dep, &cn)) != 0)
401237d1b14SEd Maste goto bad;
402237d1b14SEd Maste if ((error = msdosfs_wfile(path, dep, node)) != 0)
403237d1b14SEd Maste goto bad;
404237d1b14SEd Maste return dep;
405237d1b14SEd Maste
406237d1b14SEd Maste bad:
407237d1b14SEd Maste errno = error;
408237d1b14SEd Maste return NULL;
409237d1b14SEd Maste }
410237d1b14SEd Maste static int
msdosfs_updatede(struct denode * dep)411237d1b14SEd Maste msdosfs_updatede(struct denode *dep)
412237d1b14SEd Maste {
413d485c77fSKonstantin Belousov struct m_buf *bp;
414237d1b14SEd Maste struct direntry *dirp;
415237d1b14SEd Maste int error;
416237d1b14SEd Maste
417237d1b14SEd Maste dep->de_flag &= ~DE_MODIFIED;
418d485c77fSKonstantin Belousov error = m_readde(dep, &bp, &dirp);
419237d1b14SEd Maste if (error)
420237d1b14SEd Maste return error;
421237d1b14SEd Maste DE_EXTERNALIZE(dirp, dep);
422237d1b14SEd Maste error = bwrite(bp);
423237d1b14SEd Maste return error;
424237d1b14SEd Maste }
425237d1b14SEd Maste
426237d1b14SEd Maste /*
427237d1b14SEd Maste * Write data to a file or directory.
428237d1b14SEd Maste */
429237d1b14SEd Maste static int
msdosfs_wfile(const char * path,struct denode * dep,fsnode * node)430237d1b14SEd Maste msdosfs_wfile(const char *path, struct denode *dep, fsnode *node)
431237d1b14SEd Maste {
432237d1b14SEd Maste int error, fd;
433237d1b14SEd Maste size_t osize = dep->de_FileSize;
434237d1b14SEd Maste struct stat *st = &node->inode->st;
435237d1b14SEd Maste size_t nsize, offs;
436237d1b14SEd Maste struct msdosfsmount *pmp = dep->de_pmp;
437d485c77fSKonstantin Belousov struct m_buf *bp;
438237d1b14SEd Maste char *dat;
439237d1b14SEd Maste u_long cn = 0;
440237d1b14SEd Maste
441237d1b14SEd Maste error = 0; /* XXX: gcc/vax */
44298dc8da5SEd Maste MSDOSFS_DPRINTF(("%s(diroff %lu, dirclust %lu, startcluster %lu)\n",
44398dc8da5SEd Maste __func__, dep->de_diroffset, dep->de_dirclust,
44498dc8da5SEd Maste dep->de_StartCluster));
445237d1b14SEd Maste if (st->st_size == 0)
446237d1b14SEd Maste return 0;
447237d1b14SEd Maste
448237d1b14SEd Maste /* Don't bother to try to write files larger than the fs limit */
449237d1b14SEd Maste if (st->st_size > MSDOSFS_FILESIZE_MAX)
450237d1b14SEd Maste return EFBIG;
451237d1b14SEd Maste
452237d1b14SEd Maste nsize = st->st_size;
45398dc8da5SEd Maste MSDOSFS_DPRINTF(("%s(nsize=%zu, osize=%zu)\n", __func__, nsize, osize));
454237d1b14SEd Maste if (nsize > osize) {
455476b0ab7SEd Maste if ((error = deextend(dep, nsize, NULL)) != 0)
456237d1b14SEd Maste return error;
457237d1b14SEd Maste if ((error = msdosfs_updatede(dep)) != 0)
458237d1b14SEd Maste return error;
459237d1b14SEd Maste }
460237d1b14SEd Maste
461237d1b14SEd Maste if ((fd = open(path, O_RDONLY)) == -1) {
462237d1b14SEd Maste error = errno;
46306e20d1bSAlex Richardson fprintf(stderr, "open %s: %s\n", path, strerror(error));
464237d1b14SEd Maste return error;
465237d1b14SEd Maste }
466237d1b14SEd Maste
467237d1b14SEd Maste if ((dat = mmap(0, nsize, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0))
468237d1b14SEd Maste == MAP_FAILED) {
469237d1b14SEd Maste error = errno;
47006e20d1bSAlex Richardson fprintf(stderr, "%s: mmap %s: %s\n", __func__, node->name,
47106e20d1bSAlex Richardson strerror(error));
472237d1b14SEd Maste close(fd);
473237d1b14SEd Maste goto out;
474237d1b14SEd Maste }
475237d1b14SEd Maste close(fd);
476237d1b14SEd Maste
477237d1b14SEd Maste for (offs = 0; offs < nsize;) {
478237d1b14SEd Maste int blsize, cpsize;
479237d1b14SEd Maste daddr_t bn;
480237d1b14SEd Maste u_long on = offs & pmp->pm_crbomask;
48198dc8da5SEd Maste
482237d1b14SEd Maste if ((error = pcbmap(dep, cn++, &bn, NULL, &blsize)) != 0) {
48398dc8da5SEd Maste MSDOSFS_DPRINTF(("%s: pcbmap %lu",
48498dc8da5SEd Maste __func__, (unsigned long)bn));
485237d1b14SEd Maste goto out;
486237d1b14SEd Maste }
48798dc8da5SEd Maste
48898dc8da5SEd Maste MSDOSFS_DPRINTF(("%s(cn=%lu, bn=%llu, blsize=%d)\n",
48998dc8da5SEd Maste __func__, cn, (unsigned long long)bn, blsize));
490d485c77fSKonstantin Belousov if ((error = bread((void *)pmp->pm_devvp, bn, blsize, 0,
491d485c77fSKonstantin Belousov &bp)) != 0) {
49298dc8da5SEd Maste MSDOSFS_DPRINTF(("bread %d\n", error));
493237d1b14SEd Maste goto out;
494237d1b14SEd Maste }
495237d1b14SEd Maste cpsize = MIN((nsize - offs), blsize - on);
4962037e988SEd Maste memcpy(bp->b_data + on, dat + offs, cpsize);
497237d1b14SEd Maste bwrite(bp);
498237d1b14SEd Maste offs += cpsize;
499237d1b14SEd Maste }
500237d1b14SEd Maste
501237d1b14SEd Maste munmap(dat, nsize);
502237d1b14SEd Maste return 0;
503237d1b14SEd Maste out:
504237d1b14SEd Maste munmap(dat, nsize);
505237d1b14SEd Maste return error;
506237d1b14SEd Maste }
507237d1b14SEd Maste
508237d1b14SEd Maste static const struct {
509237d1b14SEd Maste struct direntry dot;
510237d1b14SEd Maste struct direntry dotdot;
511237d1b14SEd Maste } dosdirtemplate = {
51298dc8da5SEd Maste { ". ", /* the . entry */
513237d1b14SEd Maste ATTR_DIRECTORY, /* file attribute */
514237d1b14SEd Maste 0, /* reserved */
515237d1b14SEd Maste 0, { 0, 0 }, { 0, 0 }, /* create time & date */
516237d1b14SEd Maste { 0, 0 }, /* access date */
517237d1b14SEd Maste { 0, 0 }, /* high bits of start cluster */
518237d1b14SEd Maste { 210, 4 }, { 210, 4 }, /* modify time & date */
519237d1b14SEd Maste { 0, 0 }, /* startcluster */
520237d1b14SEd Maste { 0, 0, 0, 0 } /* filesize */
521237d1b14SEd Maste },
52298dc8da5SEd Maste { ".. ", /* the .. entry */
523237d1b14SEd Maste ATTR_DIRECTORY, /* file attribute */
524237d1b14SEd Maste 0, /* reserved */
525237d1b14SEd Maste 0, { 0, 0 }, { 0, 0 }, /* create time & date */
526237d1b14SEd Maste { 0, 0 }, /* access date */
527237d1b14SEd Maste { 0, 0 }, /* high bits of start cluster */
528237d1b14SEd Maste { 210, 4 }, { 210, 4 }, /* modify time & date */
529237d1b14SEd Maste { 0, 0 }, /* startcluster */
530237d1b14SEd Maste { 0, 0, 0, 0 } /* filesize */
531237d1b14SEd Maste }
532237d1b14SEd Maste };
533237d1b14SEd Maste
534237d1b14SEd Maste struct denode *
msdosfs_mkdire(const char * path __unused,struct denode * pdep,fsnode * node)535cc1a53bcSMark Johnston msdosfs_mkdire(const char *path __unused, struct denode *pdep, fsnode *node)
536cc1a53bcSMark Johnston {
537237d1b14SEd Maste struct denode ndirent;
538237d1b14SEd Maste struct denode *dep;
539237d1b14SEd Maste struct componentname cn;
540237d1b14SEd Maste struct msdosfsmount *pmp = pdep->de_pmp;
541237d1b14SEd Maste int error;
542237d1b14SEd Maste u_long newcluster, pcl, bn;
543237d1b14SEd Maste struct direntry *denp;
544d485c77fSKonstantin Belousov struct m_buf *bp;
545237d1b14SEd Maste
546237d1b14SEd Maste cn.cn_nameptr = node->name;
547237d1b14SEd Maste cn.cn_namelen = strlen(node->name);
548237d1b14SEd Maste /*
549237d1b14SEd Maste * If this is the root directory and there is no space left we
550237d1b14SEd Maste * can't do anything. This is because the root directory can not
551237d1b14SEd Maste * change size.
552237d1b14SEd Maste */
553237d1b14SEd Maste if (pdep->de_StartCluster == MSDOSFSROOT
554237d1b14SEd Maste && pdep->de_fndoffset >= pdep->de_FileSize) {
555237d1b14SEd Maste error = ENOSPC;
556237d1b14SEd Maste goto bad2;
557237d1b14SEd Maste }
558237d1b14SEd Maste
559237d1b14SEd Maste /*
560237d1b14SEd Maste * Allocate a cluster to hold the about to be created directory.
561237d1b14SEd Maste */
56298dc8da5SEd Maste error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
563237d1b14SEd Maste if (error)
564237d1b14SEd Maste goto bad2;
565237d1b14SEd Maste
566237d1b14SEd Maste memset(&ndirent, 0, sizeof(ndirent));
567237d1b14SEd Maste ndirent.de_pmp = pmp;
568237d1b14SEd Maste ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
56998dc8da5SEd Maste msdosfs_times(&ndirent, &node->inode->st);
570237d1b14SEd Maste
571237d1b14SEd Maste /*
572237d1b14SEd Maste * Now fill the cluster with the "." and ".." entries. And write
573237d1b14SEd Maste * the cluster to disk. This way it is there for the parent
574237d1b14SEd Maste * directory to be pointing at if there were a crash.
575237d1b14SEd Maste */
576237d1b14SEd Maste bn = cntobn(pmp, newcluster);
57798dc8da5SEd Maste MSDOSFS_DPRINTF(("%s(newcluster %lu, bn=%lu)\n",
57898dc8da5SEd Maste __func__, newcluster, bn));
579237d1b14SEd Maste /* always succeeds */
580d485c77fSKonstantin Belousov bp = getblk((void *)pmp->pm_devvp, bn, pmp->pm_bpcluster, 0, 0, 0);
581237d1b14SEd Maste memset(bp->b_data, 0, pmp->pm_bpcluster);
582237d1b14SEd Maste memcpy(bp->b_data, &dosdirtemplate, sizeof dosdirtemplate);
583237d1b14SEd Maste denp = (struct direntry *)bp->b_data;
584237d1b14SEd Maste putushort(denp[0].deStartCluster, newcluster);
585237d1b14SEd Maste putushort(denp[0].deCDate, ndirent.de_CDate);
586237d1b14SEd Maste putushort(denp[0].deCTime, ndirent.de_CTime);
587237d1b14SEd Maste denp[0].deCHundredth = ndirent.de_CHun;
588237d1b14SEd Maste putushort(denp[0].deADate, ndirent.de_ADate);
589237d1b14SEd Maste putushort(denp[0].deMDate, ndirent.de_MDate);
590237d1b14SEd Maste putushort(denp[0].deMTime, ndirent.de_MTime);
591237d1b14SEd Maste pcl = pdep->de_StartCluster;
59298dc8da5SEd Maste MSDOSFS_DPRINTF(("%s(pcl %lu, rootdirblk=%lu)\n", __func__, pcl,
593237d1b14SEd Maste pmp->pm_rootdirblk));
594237d1b14SEd Maste if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
595237d1b14SEd Maste pcl = 0;
596237d1b14SEd Maste putushort(denp[1].deStartCluster, pcl);
597237d1b14SEd Maste putushort(denp[1].deCDate, ndirent.de_CDate);
598237d1b14SEd Maste putushort(denp[1].deCTime, ndirent.de_CTime);
599237d1b14SEd Maste denp[1].deCHundredth = ndirent.de_CHun;
600237d1b14SEd Maste putushort(denp[1].deADate, ndirent.de_ADate);
601237d1b14SEd Maste putushort(denp[1].deMDate, ndirent.de_MDate);
602237d1b14SEd Maste putushort(denp[1].deMTime, ndirent.de_MTime);
603237d1b14SEd Maste if (FAT32(pmp)) {
604237d1b14SEd Maste putushort(denp[0].deHighClust, newcluster >> 16);
605237d1b14SEd Maste putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
606237d1b14SEd Maste } else {
607237d1b14SEd Maste putushort(denp[0].deHighClust, 0);
608237d1b14SEd Maste putushort(denp[1].deHighClust, 0);
609237d1b14SEd Maste }
610237d1b14SEd Maste
611237d1b14SEd Maste if ((error = bwrite(bp)) != 0)
612237d1b14SEd Maste goto bad;
613237d1b14SEd Maste
614237d1b14SEd Maste /*
615237d1b14SEd Maste * Now build up a directory entry pointing to the newly allocated
616237d1b14SEd Maste * cluster. This will be written to an empty slot in the parent
617237d1b14SEd Maste * directory.
618237d1b14SEd Maste */
619237d1b14SEd Maste if ((error = uniqdosname(pdep, &cn, ndirent.de_Name)) != 0)
620237d1b14SEd Maste goto bad;
621237d1b14SEd Maste
622237d1b14SEd Maste ndirent.de_Attributes = ATTR_DIRECTORY;
623237d1b14SEd Maste ndirent.de_StartCluster = newcluster;
624237d1b14SEd Maste ndirent.de_FileSize = 0;
625237d1b14SEd Maste ndirent.de_pmp = pdep->de_pmp;
626237d1b14SEd Maste if ((error = msdosfs_findslot(pdep, &cn)) != 0)
627237d1b14SEd Maste goto bad;
628237d1b14SEd Maste if ((error = createde(&ndirent, pdep, &dep, &cn)) != 0)
629237d1b14SEd Maste goto bad;
630237d1b14SEd Maste if ((error = msdosfs_updatede(dep)) != 0)
631237d1b14SEd Maste goto bad;
632237d1b14SEd Maste return dep;
633237d1b14SEd Maste
634237d1b14SEd Maste bad:
63565990b68SKonstantin Belousov clusterfree(pmp, newcluster);
636237d1b14SEd Maste bad2:
637237d1b14SEd Maste errno = error;
638237d1b14SEd Maste return NULL;
639237d1b14SEd Maste }
640