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