msdosfs_lookup.c revision 1.41 1 1.41 andvar /* $NetBSD: msdosfs_lookup.c,v 1.41 2022/08/06 18:26:42 andvar Exp $ */
2 1.1 jdolecek
3 1.1 jdolecek /*-
4 1.1 jdolecek * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
5 1.1 jdolecek * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
6 1.1 jdolecek * All rights reserved.
7 1.1 jdolecek * Original code by Paul Popelka (paulp (at) uts.amdahl.com) (see below).
8 1.1 jdolecek *
9 1.1 jdolecek * Redistribution and use in source and binary forms, with or without
10 1.1 jdolecek * modification, are permitted provided that the following conditions
11 1.1 jdolecek * are met:
12 1.1 jdolecek * 1. Redistributions of source code must retain the above copyright
13 1.1 jdolecek * notice, this list of conditions and the following disclaimer.
14 1.1 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 jdolecek * notice, this list of conditions and the following disclaimer in the
16 1.1 jdolecek * documentation and/or other materials provided with the distribution.
17 1.1 jdolecek * 3. All advertising materials mentioning features or use of this software
18 1.1 jdolecek * must display the following acknowledgement:
19 1.1 jdolecek * This product includes software developed by TooLs GmbH.
20 1.1 jdolecek * 4. The name of TooLs GmbH may not be used to endorse or promote products
21 1.1 jdolecek * derived from this software without specific prior written permission.
22 1.1 jdolecek *
23 1.1 jdolecek * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24 1.1 jdolecek * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.1 jdolecek * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.1 jdolecek * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 1.1 jdolecek * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 1.1 jdolecek * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 1.1 jdolecek * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 1.1 jdolecek * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 1.1 jdolecek * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 1.1 jdolecek * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 jdolecek */
34 1.1 jdolecek /*
35 1.1 jdolecek * Written by Paul Popelka (paulp (at) uts.amdahl.com)
36 1.1 jdolecek *
37 1.1 jdolecek * You can do anything you want with this software, just don't say you wrote
38 1.1 jdolecek * it, and don't remove this notice.
39 1.1 jdolecek *
40 1.1 jdolecek * This software is provided "as is".
41 1.1 jdolecek *
42 1.1 jdolecek * The author supplies this software to be publicly redistributed on the
43 1.1 jdolecek * understanding that the author is not responsible for the correct
44 1.1 jdolecek * functioning of this software in any circumstances and is not liable for
45 1.1 jdolecek * any damages caused by this software.
46 1.1 jdolecek *
47 1.1 jdolecek * October 1992
48 1.1 jdolecek */
49 1.1 jdolecek
50 1.28 christos #if HAVE_NBTOOL_CONFIG_H
51 1.28 christos #include "nbtool_config.h"
52 1.28 christos #endif
53 1.28 christos
54 1.1 jdolecek #include <sys/cdefs.h>
55 1.41 andvar __KERNEL_RCSID(0, "$NetBSD: msdosfs_lookup.c,v 1.41 2022/08/06 18:26:42 andvar Exp $");
56 1.1 jdolecek
57 1.1 jdolecek #include <sys/param.h>
58 1.29 christos
59 1.29 christos #ifdef _KERNEL
60 1.1 jdolecek #include <sys/systm.h>
61 1.28 christos #include <sys/mount.h>
62 1.28 christos #include <sys/kauth.h>
63 1.1 jdolecek #include <sys/namei.h>
64 1.28 christos #include <sys/dirent.h>
65 1.1 jdolecek #include <sys/buf.h>
66 1.1 jdolecek #include <sys/vnode.h>
67 1.33 hannken #include <sys/atomic.h>
68 1.28 christos #else
69 1.28 christos #include <ffs/buf.h>
70 1.28 christos #endif /* _KERNEL */
71 1.1 jdolecek
72 1.1 jdolecek #include <fs/msdosfs/bpb.h>
73 1.1 jdolecek #include <fs/msdosfs/direntry.h>
74 1.1 jdolecek #include <fs/msdosfs/denode.h>
75 1.1 jdolecek #include <fs/msdosfs/msdosfsmount.h>
76 1.1 jdolecek #include <fs/msdosfs/fat.h>
77 1.1 jdolecek
78 1.28 christos
79 1.28 christos #ifdef _KERNEL
80 1.1 jdolecek /*
81 1.1 jdolecek * When we search a directory the blocks containing directory entries are
82 1.1 jdolecek * read and examined. The directory entries contain information that would
83 1.1 jdolecek * normally be in the inode of a unix filesystem. This means that some of
84 1.1 jdolecek * a directory's contents may also be in memory resident denodes (sort of
85 1.1 jdolecek * an inode). This can cause problems if we are searching while some other
86 1.1 jdolecek * process is modifying a directory. To prevent one process from accessing
87 1.1 jdolecek * incompletely modified directory information we depend upon being the
88 1.1 jdolecek * sole owner of a directory block. bread/brelse provide this service.
89 1.1 jdolecek * This being the case, when a process modifies a directory it must first
90 1.1 jdolecek * acquire the disk block that contains the directory entry to be modified.
91 1.1 jdolecek * Then update the disk block and the denode, and then write the disk block
92 1.1 jdolecek * out to disk. This way disk blocks containing directory entries and in
93 1.1 jdolecek * memory denode's will be in synch.
94 1.1 jdolecek */
95 1.1 jdolecek int
96 1.18 dsl msdosfs_lookup(void *v)
97 1.1 jdolecek {
98 1.32 hannken struct vop_lookup_v2_args /* {
99 1.1 jdolecek struct vnode *a_dvp;
100 1.1 jdolecek struct vnode **a_vpp;
101 1.1 jdolecek struct componentname *a_cnp;
102 1.1 jdolecek } */ *ap = v;
103 1.1 jdolecek struct vnode *vdp = ap->a_dvp;
104 1.1 jdolecek struct vnode **vpp = ap->a_vpp;
105 1.1 jdolecek struct componentname *cnp = ap->a_cnp;
106 1.1 jdolecek daddr_t bn;
107 1.1 jdolecek int error;
108 1.1 jdolecek int slotcount;
109 1.1 jdolecek int slotoffset = 0;
110 1.1 jdolecek int frcn;
111 1.1 jdolecek u_long cluster;
112 1.1 jdolecek int blkoff;
113 1.1 jdolecek int diroff;
114 1.1 jdolecek int blsize;
115 1.1 jdolecek int isadir; /* ~0 if found direntry is a directory */
116 1.1 jdolecek u_long scn; /* starting cluster number */
117 1.1 jdolecek struct denode *dp;
118 1.1 jdolecek struct msdosfsmount *pmp;
119 1.1 jdolecek struct buf *bp = 0;
120 1.1 jdolecek struct direntry *dep;
121 1.1 jdolecek u_char dosfilename[12];
122 1.1 jdolecek int flags;
123 1.1 jdolecek int nameiop = cnp->cn_nameiop;
124 1.1 jdolecek int wincnt = 1;
125 1.1 jdolecek int chksum = -1, chksum_ok;
126 1.1 jdolecek int olddos = 1;
127 1.1 jdolecek
128 1.1 jdolecek flags = cnp->cn_flags;
129 1.1 jdolecek
130 1.1 jdolecek #ifdef MSDOSFS_DEBUG
131 1.1 jdolecek printf("msdosfs_lookup(): looking for %.*s\n",
132 1.1 jdolecek (int)cnp->cn_namelen, cnp->cn_nameptr);
133 1.1 jdolecek #endif
134 1.1 jdolecek dp = VTODE(vdp);
135 1.1 jdolecek pmp = dp->de_pmp;
136 1.1 jdolecek *vpp = NULL;
137 1.1 jdolecek #ifdef MSDOSFS_DEBUG
138 1.1 jdolecek printf("msdosfs_lookup(): vdp %p, dp %p, Attr %02x\n",
139 1.1 jdolecek vdp, dp, dp->de_Attributes);
140 1.1 jdolecek #endif
141 1.1 jdolecek
142 1.1 jdolecek /*
143 1.41 andvar * Check accessibility of directory.
144 1.1 jdolecek */
145 1.15 pooka if ((error = VOP_ACCESS(vdp, VEXEC, cnp->cn_cred)) != 0)
146 1.1 jdolecek return (error);
147 1.1 jdolecek
148 1.1 jdolecek if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
149 1.1 jdolecek (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
150 1.1 jdolecek return (EROFS);
151 1.1 jdolecek
152 1.1 jdolecek /*
153 1.1 jdolecek * We now have a segment name to search for, and a directory to search.
154 1.1 jdolecek *
155 1.1 jdolecek * Before tediously performing a linear scan of the directory,
156 1.1 jdolecek * check the name cache to see if the directory/name pair
157 1.1 jdolecek * we are looking for is known already.
158 1.1 jdolecek */
159 1.26 dholland if (cache_lookup(vdp, cnp->cn_nameptr, cnp->cn_namelen,
160 1.26 dholland cnp->cn_nameiop, cnp->cn_flags, NULL, vpp)) {
161 1.25 dholland return *vpp == NULLVP ? ENOENT: 0;
162 1.25 dholland }
163 1.1 jdolecek
164 1.36 ad /* May need to restart the lookup with an exclusive lock. */
165 1.36 ad if (VOP_ISLOCKED(vdp) != LK_EXCLUSIVE)
166 1.36 ad return ENOLCK;
167 1.36 ad
168 1.1 jdolecek /*
169 1.1 jdolecek * If they are going after the . or .. entry in the root directory,
170 1.1 jdolecek * they won't find it. DOS filesystems don't have them in the root
171 1.1 jdolecek * directory. So, we fake it. deget() is in on this scam too.
172 1.1 jdolecek */
173 1.14 ad if ((vdp->v_vflag & VV_ROOT) && cnp->cn_nameptr[0] == '.' &&
174 1.1 jdolecek (cnp->cn_namelen == 1 ||
175 1.1 jdolecek (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.'))) {
176 1.1 jdolecek isadir = ATTR_DIRECTORY;
177 1.1 jdolecek scn = MSDOSFSROOT;
178 1.1 jdolecek #ifdef MSDOSFS_DEBUG
179 1.1 jdolecek printf("msdosfs_lookup(): looking for . or .. in root directory\n");
180 1.1 jdolecek #endif
181 1.1 jdolecek cluster = MSDOSFSROOT;
182 1.1 jdolecek blkoff = MSDOSFSROOT_OFS;
183 1.1 jdolecek goto foundroot;
184 1.1 jdolecek }
185 1.1 jdolecek
186 1.40 thorpej switch (msdosfs_unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename,
187 1.1 jdolecek cnp->cn_namelen, 0)) {
188 1.1 jdolecek case 0:
189 1.1 jdolecek return (EINVAL);
190 1.1 jdolecek case 1:
191 1.1 jdolecek break;
192 1.1 jdolecek case 2:
193 1.40 thorpej wincnt = msdosfs_winSlotCnt((const u_char *)cnp->cn_nameptr,
194 1.35 mlelstv cnp->cn_namelen, pmp->pm_flags & MSDOSFSMNT_UTF8) + 1;
195 1.1 jdolecek break;
196 1.1 jdolecek case 3:
197 1.1 jdolecek olddos = 0;
198 1.40 thorpej wincnt = msdosfs_winSlotCnt((const u_char *)cnp->cn_nameptr,
199 1.35 mlelstv cnp->cn_namelen, pmp->pm_flags & MSDOSFSMNT_UTF8) + 1;
200 1.1 jdolecek break;
201 1.1 jdolecek }
202 1.1 jdolecek if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
203 1.1 jdolecek wincnt = 1;
204 1.1 jdolecek
205 1.1 jdolecek /*
206 1.1 jdolecek * Suppress search for slots unless creating
207 1.1 jdolecek * file and at end of pathname, in which case
208 1.1 jdolecek * we watch for a place to put the new file in
209 1.1 jdolecek * case it doesn't already exist.
210 1.1 jdolecek */
211 1.1 jdolecek slotcount = wincnt;
212 1.1 jdolecek if ((nameiop == CREATE || nameiop == RENAME) &&
213 1.1 jdolecek (flags & ISLASTCN))
214 1.1 jdolecek slotcount = 0;
215 1.1 jdolecek
216 1.1 jdolecek #ifdef MSDOSFS_DEBUG
217 1.1 jdolecek printf("msdosfs_lookup(): dos filename: %s\n", dosfilename);
218 1.1 jdolecek #endif
219 1.1 jdolecek /*
220 1.1 jdolecek * Search the directory pointed at by vdp for the name pointed at
221 1.1 jdolecek * by cnp->cn_nameptr.
222 1.1 jdolecek */
223 1.33 hannken
224 1.1 jdolecek /*
225 1.1 jdolecek * The outer loop ranges over the clusters that make up the
226 1.1 jdolecek * directory. Note that the root directory is different from all
227 1.1 jdolecek * other directories. It has a fixed number of blocks that are not
228 1.1 jdolecek * part of the pool of allocatable clusters. So, we treat it a
229 1.1 jdolecek * little differently. The root directory starts at "cluster" 0.
230 1.1 jdolecek */
231 1.1 jdolecek diroff = 0;
232 1.1 jdolecek for (frcn = 0; diroff < dp->de_FileSize; frcn++) {
233 1.40 thorpej if ((error = msdosfs_pcbmap(dp, frcn, &bn, &cluster,
234 1.40 thorpej &blsize)) != 0) {
235 1.1 jdolecek if (error == E2BIG)
236 1.1 jdolecek break;
237 1.1 jdolecek return (error);
238 1.1 jdolecek }
239 1.34 maxv error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
240 1.16 hannken 0, &bp);
241 1.1 jdolecek if (error) {
242 1.1 jdolecek return (error);
243 1.1 jdolecek }
244 1.1 jdolecek for (blkoff = 0; blkoff < blsize;
245 1.1 jdolecek blkoff += sizeof(struct direntry),
246 1.1 jdolecek diroff += sizeof(struct direntry)) {
247 1.12 christos dep = (struct direntry *)((char *)bp->b_data + blkoff);
248 1.1 jdolecek /*
249 1.1 jdolecek * If the slot is empty and we are still looking
250 1.1 jdolecek * for an empty then remember this one. If the
251 1.1 jdolecek * slot is not empty then check to see if it
252 1.1 jdolecek * matches what we are looking for. If the slot
253 1.1 jdolecek * has never been filled with anything, then the
254 1.1 jdolecek * remainder of the directory has never been used,
255 1.1 jdolecek * so there is no point in searching it.
256 1.1 jdolecek */
257 1.1 jdolecek if (dep->deName[0] == SLOT_EMPTY ||
258 1.1 jdolecek dep->deName[0] == SLOT_DELETED) {
259 1.1 jdolecek /*
260 1.1 jdolecek * Drop memory of previous long matches
261 1.1 jdolecek */
262 1.1 jdolecek chksum = -1;
263 1.1 jdolecek
264 1.1 jdolecek if (slotcount < wincnt) {
265 1.1 jdolecek slotcount++;
266 1.1 jdolecek slotoffset = diroff;
267 1.1 jdolecek }
268 1.1 jdolecek if (dep->deName[0] == SLOT_EMPTY) {
269 1.13 ad brelse(bp, 0);
270 1.1 jdolecek goto notfound;
271 1.1 jdolecek }
272 1.1 jdolecek } else {
273 1.1 jdolecek /*
274 1.1 jdolecek * If there wasn't enough space for our
275 1.1 jdolecek * winentries, forget about the empty space
276 1.1 jdolecek */
277 1.1 jdolecek if (slotcount < wincnt)
278 1.1 jdolecek slotcount = 0;
279 1.1 jdolecek
280 1.1 jdolecek /*
281 1.1 jdolecek * Check for Win95 long filename entry
282 1.1 jdolecek */
283 1.1 jdolecek if (dep->deAttributes == ATTR_WIN95) {
284 1.1 jdolecek if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
285 1.1 jdolecek continue;
286 1.1 jdolecek
287 1.40 thorpej chksum = msdosfs_winChkName((const u_char *)cnp->cn_nameptr,
288 1.1 jdolecek cnp->cn_namelen,
289 1.1 jdolecek (struct winentry *)dep,
290 1.35 mlelstv chksum,
291 1.35 mlelstv pmp->pm_flags & MSDOSFSMNT_UTF8);
292 1.1 jdolecek continue;
293 1.1 jdolecek }
294 1.1 jdolecek
295 1.1 jdolecek /*
296 1.1 jdolecek * Ignore volume labels (anywhere, not just
297 1.1 jdolecek * the root directory).
298 1.1 jdolecek */
299 1.1 jdolecek if (dep->deAttributes & ATTR_VOLUME) {
300 1.1 jdolecek chksum = -1;
301 1.1 jdolecek continue;
302 1.1 jdolecek }
303 1.1 jdolecek
304 1.1 jdolecek /*
305 1.1 jdolecek * Check for a checksum or name match
306 1.1 jdolecek */
307 1.40 thorpej chksum_ok =
308 1.40 thorpej (chksum == msdosfs_winChksum(dep->deName));
309 1.30 mlelstv if (!chksum_ok && (
310 1.30 mlelstv !olddos ||
311 1.30 mlelstv memcmp(&dosfilename[0],dep->deName,8) ||
312 1.30 mlelstv memcmp(&dosfilename[8],dep->deExtension,3))) {
313 1.1 jdolecek chksum = -1;
314 1.1 jdolecek continue;
315 1.1 jdolecek }
316 1.1 jdolecek #ifdef MSDOSFS_DEBUG
317 1.1 jdolecek printf("msdosfs_lookup(): match blkoff %d, diroff %d\n",
318 1.1 jdolecek blkoff, diroff);
319 1.1 jdolecek #endif
320 1.1 jdolecek /*
321 1.1 jdolecek * Remember where this directory
322 1.1 jdolecek * entry came from for whoever did
323 1.1 jdolecek * this lookup.
324 1.1 jdolecek */
325 1.38 hannken dp->de_crap.mlr_fndoffset = diroff;
326 1.1 jdolecek if (chksum_ok && nameiop == RENAME) {
327 1.1 jdolecek /*
328 1.1 jdolecek * Target had correct long name
329 1.1 jdolecek * directory entries, reuse them
330 1.1 jdolecek * as needed.
331 1.1 jdolecek */
332 1.38 hannken dp->de_crap.mlr_fndcnt = wincnt - 1;
333 1.1 jdolecek } else {
334 1.1 jdolecek /*
335 1.1 jdolecek * Long name directory entries
336 1.1 jdolecek * not present or corrupt, can only
337 1.1 jdolecek * reuse dos directory entry.
338 1.1 jdolecek */
339 1.38 hannken dp->de_crap.mlr_fndcnt = 0;
340 1.1 jdolecek }
341 1.1 jdolecek
342 1.1 jdolecek goto found;
343 1.1 jdolecek }
344 1.1 jdolecek } /* for (blkoff = 0; .... */
345 1.1 jdolecek /*
346 1.1 jdolecek * Release the buffer holding the directory cluster just
347 1.1 jdolecek * searched.
348 1.1 jdolecek */
349 1.13 ad brelse(bp, 0);
350 1.1 jdolecek } /* for (frcn = 0; ; frcn++) */
351 1.1 jdolecek
352 1.1 jdolecek notfound:
353 1.1 jdolecek /*
354 1.1 jdolecek * We hold no disk buffers at this point.
355 1.1 jdolecek */
356 1.1 jdolecek
357 1.1 jdolecek /*
358 1.1 jdolecek * If we get here we didn't find the entry we were looking for. But
359 1.1 jdolecek * that's ok if we are creating or renaming and are at the end of
360 1.1 jdolecek * the pathname and the directory hasn't been removed.
361 1.1 jdolecek */
362 1.1 jdolecek #ifdef MSDOSFS_DEBUG
363 1.1 jdolecek printf("msdosfs_lookup(): op %d, refcnt %ld, slotcount %d, slotoffset %d\n",
364 1.1 jdolecek nameiop, dp->de_refcnt, slotcount, slotoffset);
365 1.1 jdolecek #endif
366 1.1 jdolecek if ((nameiop == CREATE || nameiop == RENAME) &&
367 1.1 jdolecek (flags & ISLASTCN) && dp->de_refcnt != 0) {
368 1.1 jdolecek /*
369 1.1 jdolecek * Access for write is interpreted as allowing
370 1.1 jdolecek * creation of files in the directory.
371 1.1 jdolecek */
372 1.15 pooka error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred);
373 1.1 jdolecek if (error)
374 1.1 jdolecek return (error);
375 1.1 jdolecek
376 1.1 jdolecek /*
377 1.1 jdolecek * Fixup the slot description to point to the place where
378 1.1 jdolecek * we might put the new DOS direntry (putting the Win95
379 1.1 jdolecek * long name entries before that)
380 1.1 jdolecek */
381 1.1 jdolecek if (!slotcount) {
382 1.1 jdolecek slotcount = 1;
383 1.1 jdolecek slotoffset = diroff;
384 1.1 jdolecek }
385 1.1 jdolecek if (wincnt > slotcount) {
386 1.1 jdolecek slotoffset +=
387 1.1 jdolecek sizeof(struct direntry) * (wincnt - slotcount);
388 1.1 jdolecek }
389 1.4 perry
390 1.1 jdolecek /*
391 1.1 jdolecek * Return an indication of where the new directory
392 1.1 jdolecek * entry should be put.
393 1.1 jdolecek */
394 1.38 hannken dp->de_crap.mlr_fndoffset = slotoffset;
395 1.38 hannken dp->de_crap.mlr_fndcnt = wincnt - 1;
396 1.1 jdolecek
397 1.1 jdolecek /*
398 1.1 jdolecek * We return with the directory locked, so that
399 1.1 jdolecek * the parameters we set up above will still be
400 1.1 jdolecek * valid if we actually decide to do a direnter().
401 1.1 jdolecek * We return ni_vp == NULL to indicate that the entry
402 1.1 jdolecek * does not currently exist; we leave a pointer to
403 1.1 jdolecek * the (locked) directory inode in ndp->ni_dvp.
404 1.1 jdolecek *
405 1.1 jdolecek * NB - if the directory is unlocked, then this
406 1.1 jdolecek * information cannot be used.
407 1.1 jdolecek */
408 1.1 jdolecek return (EJUSTRETURN);
409 1.1 jdolecek }
410 1.1 jdolecek
411 1.6 christos #if 0
412 1.1 jdolecek /*
413 1.1 jdolecek * Insert name into cache (as non-existent) if appropriate.
414 1.6 christos *
415 1.6 christos * XXX Negative caching is broken for msdosfs because the name
416 1.6 christos * cache doesn't understand peculiarities such as case insensitivity
417 1.6 christos * and 8.3 filenames. Hence, it may not invalidate all negative
418 1.6 christos * entries if a file with this name is later created.
419 1.7 soda * e.g. creating a file 'foo' won't invalidate a negative entry
420 1.7 soda * for 'FOO'.
421 1.1 jdolecek */
422 1.24 rmind if (nameiop != CREATE)
423 1.26 dholland cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
424 1.26 dholland cnp->cn_flags);
425 1.6 christos #endif
426 1.1 jdolecek
427 1.1 jdolecek return (ENOENT);
428 1.1 jdolecek
429 1.1 jdolecek found:
430 1.1 jdolecek /*
431 1.1 jdolecek * NOTE: We still have the buffer with matched directory entry at
432 1.1 jdolecek * this point.
433 1.1 jdolecek */
434 1.1 jdolecek isadir = dep->deAttributes & ATTR_DIRECTORY;
435 1.1 jdolecek scn = getushort(dep->deStartCluster);
436 1.1 jdolecek if (FAT32(pmp)) {
437 1.1 jdolecek scn |= getushort(dep->deHighClust) << 16;
438 1.1 jdolecek if (scn == pmp->pm_rootdirblk) {
439 1.1 jdolecek /*
440 1.1 jdolecek * There should actually be 0 here.
441 1.1 jdolecek * Just ignore the error.
442 1.1 jdolecek */
443 1.1 jdolecek scn = MSDOSFSROOT;
444 1.1 jdolecek }
445 1.1 jdolecek }
446 1.1 jdolecek
447 1.1 jdolecek if (isadir) {
448 1.1 jdolecek cluster = scn;
449 1.1 jdolecek if (cluster == MSDOSFSROOT)
450 1.1 jdolecek blkoff = MSDOSFSROOT_OFS;
451 1.1 jdolecek else
452 1.1 jdolecek blkoff = 0;
453 1.1 jdolecek } else if (cluster == MSDOSFSROOT)
454 1.1 jdolecek blkoff = diroff;
455 1.1 jdolecek
456 1.1 jdolecek /*
457 1.1 jdolecek * Now release buf to allow deget to read the entry again.
458 1.1 jdolecek * Reserving it here and giving it to deget could result
459 1.1 jdolecek * in a deadlock.
460 1.1 jdolecek */
461 1.13 ad brelse(bp, 0);
462 1.1 jdolecek
463 1.1 jdolecek foundroot:
464 1.1 jdolecek /*
465 1.1 jdolecek * If we entered at foundroot, then we are looking for the . or ..
466 1.1 jdolecek * entry of the filesystems root directory. isadir and scn were
467 1.1 jdolecek * setup before jumping here. And, bp is already null.
468 1.1 jdolecek */
469 1.1 jdolecek if (FAT32(pmp) && scn == MSDOSFSROOT)
470 1.1 jdolecek scn = pmp->pm_rootdirblk;
471 1.1 jdolecek
472 1.1 jdolecek /*
473 1.1 jdolecek * If deleting, and at end of pathname, return
474 1.1 jdolecek * parameters which can be used to remove file.
475 1.11 chs * Lock the inode, being careful with ".".
476 1.1 jdolecek */
477 1.1 jdolecek if (nameiop == DELETE && (flags & ISLASTCN)) {
478 1.1 jdolecek /*
479 1.1 jdolecek * Don't allow deleting the root.
480 1.1 jdolecek */
481 1.1 jdolecek if (blkoff == MSDOSFSROOT_OFS)
482 1.22 mlelstv return EINVAL;
483 1.1 jdolecek
484 1.1 jdolecek /*
485 1.1 jdolecek * Write access to directory required to delete files.
486 1.1 jdolecek */
487 1.15 pooka error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred);
488 1.1 jdolecek if (error)
489 1.1 jdolecek return (error);
490 1.1 jdolecek
491 1.1 jdolecek /*
492 1.1 jdolecek * Return pointer to current entry in dp->i_offset.
493 1.1 jdolecek * Save directory inode pointer in ndp->ni_dvp for dirremove().
494 1.1 jdolecek */
495 1.1 jdolecek if (dp->de_StartCluster == scn && isadir) { /* "." */
496 1.20 pooka vref(vdp);
497 1.1 jdolecek *vpp = vdp;
498 1.1 jdolecek return (0);
499 1.1 jdolecek }
500 1.40 thorpej error = msdosfs_deget(pmp, cluster, blkoff, vpp);
501 1.33 hannken return error;
502 1.1 jdolecek }
503 1.1 jdolecek
504 1.1 jdolecek /*
505 1.1 jdolecek * If rewriting (RENAME), return the inode and the
506 1.1 jdolecek * information required to rewrite the present directory
507 1.1 jdolecek * Must get inode of directory entry to verify it's a
508 1.1 jdolecek * regular file, or empty directory.
509 1.1 jdolecek */
510 1.11 chs if (nameiop == RENAME && (flags & ISLASTCN)) {
511 1.1 jdolecek
512 1.1 jdolecek if (vdp->v_mount->mnt_flag & MNT_RDONLY)
513 1.1 jdolecek return (EROFS);
514 1.1 jdolecek
515 1.1 jdolecek if (blkoff == MSDOSFSROOT_OFS)
516 1.22 mlelstv return EINVAL;
517 1.1 jdolecek
518 1.15 pooka error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred);
519 1.1 jdolecek if (error)
520 1.1 jdolecek return (error);
521 1.1 jdolecek
522 1.1 jdolecek /*
523 1.1 jdolecek * Careful about locking second inode.
524 1.1 jdolecek * This can only occur if the target is ".".
525 1.1 jdolecek */
526 1.1 jdolecek if (dp->de_StartCluster == scn && isadir)
527 1.1 jdolecek return (EISDIR);
528 1.1 jdolecek
529 1.40 thorpej error = msdosfs_deget(pmp, cluster, blkoff, vpp);
530 1.33 hannken return error;
531 1.1 jdolecek }
532 1.1 jdolecek
533 1.33 hannken if (dp->de_StartCluster == scn && isadir) {
534 1.20 pooka vref(vdp); /* we want ourself, ie "." */
535 1.1 jdolecek *vpp = vdp;
536 1.40 thorpej } else if ((error = msdosfs_deget(pmp, cluster, blkoff, vpp)) != 0) {
537 1.33 hannken return error;
538 1.1 jdolecek }
539 1.1 jdolecek
540 1.1 jdolecek /*
541 1.1 jdolecek * Insert name into cache if appropriate.
542 1.1 jdolecek */
543 1.26 dholland cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_flags);
544 1.1 jdolecek
545 1.24 rmind return 0;
546 1.1 jdolecek }
547 1.28 christos #endif /* _KERNEL */
548 1.1 jdolecek
549 1.1 jdolecek /*
550 1.1 jdolecek * dep - directory entry to copy into the directory
551 1.1 jdolecek * ddep - directory to add to
552 1.1 jdolecek * depp - return the address of the denode for the created directory entry
553 1.1 jdolecek * if depp != 0
554 1.1 jdolecek * cnp - componentname needed for Win95 long filenames
555 1.1 jdolecek */
556 1.1 jdolecek int
557 1.40 thorpej msdosfs_createde(struct denode *dep, struct denode *ddep,
558 1.38 hannken const struct msdosfs_lookup_results *mlr,
559 1.38 hannken struct denode **depp, struct componentname *cnp)
560 1.1 jdolecek {
561 1.1 jdolecek int error, rberror;
562 1.1 jdolecek u_long dirclust, clusoffset;
563 1.28 christos u_long fndoffset, havecnt = 0, wcnt = 1, i;
564 1.1 jdolecek struct direntry *ndep;
565 1.1 jdolecek struct msdosfsmount *pmp = ddep->de_pmp;
566 1.1 jdolecek struct buf *bp;
567 1.1 jdolecek daddr_t bn;
568 1.28 christos int blsize;
569 1.28 christos #ifdef _KERNEL
570 1.1 jdolecek int async = ddep->de_pmp->pm_mountp->mnt_flag & MNT_ASYNC;
571 1.28 christos #else
572 1.28 christos #define async 0
573 1.28 christos #endif
574 1.1 jdolecek
575 1.1 jdolecek #ifdef MSDOSFS_DEBUG
576 1.1 jdolecek printf("createde(dep %p, ddep %p, depp %p, cnp %p)\n",
577 1.1 jdolecek dep, ddep, depp, cnp);
578 1.1 jdolecek #endif
579 1.1 jdolecek
580 1.1 jdolecek /*
581 1.1 jdolecek * If no space left in the directory then allocate another cluster
582 1.1 jdolecek * and chain it onto the end of the file. There is one exception
583 1.1 jdolecek * to this. That is, if the root directory has no more space it
584 1.1 jdolecek * can NOT be expanded. extendfile() checks for and fails attempts
585 1.1 jdolecek * to extend the root directory. We just return an error in that
586 1.1 jdolecek * case.
587 1.1 jdolecek */
588 1.38 hannken if (mlr->mlr_fndoffset >= ddep->de_FileSize) {
589 1.38 hannken u_long needlen = ddep->de_crap.mlr_fndoffset
590 1.38 hannken + sizeof(struct direntry) - ddep->de_FileSize;
591 1.1 jdolecek dirclust = de_clcount(pmp, needlen);
592 1.40 thorpej if ((error = msdosfs_extendfile(ddep, dirclust, 0, 0,
593 1.40 thorpej DE_CLEAR)) != 0) {
594 1.40 thorpej (void)msdosfs_detrunc(ddep, ddep->de_FileSize, 0,
595 1.40 thorpej NOCRED);
596 1.1 jdolecek goto err_norollback;
597 1.1 jdolecek }
598 1.1 jdolecek
599 1.1 jdolecek /*
600 1.1 jdolecek * Update the size of the directory
601 1.1 jdolecek */
602 1.1 jdolecek ddep->de_FileSize += de_cn2off(pmp, dirclust);
603 1.1 jdolecek }
604 1.1 jdolecek
605 1.1 jdolecek /*
606 1.1 jdolecek * We just read in the cluster with space. Copy the new directory
607 1.1 jdolecek * entry in. Then write it to disk. NOTE: DOS directories
608 1.1 jdolecek * do not get smaller as clusters are emptied.
609 1.1 jdolecek */
610 1.40 thorpej error = msdosfs_pcbmap(ddep, de_cluster(pmp, mlr->mlr_fndoffset),
611 1.1 jdolecek &bn, &dirclust, &blsize);
612 1.1 jdolecek if (error)
613 1.1 jdolecek goto err_norollback;
614 1.38 hannken clusoffset = mlr->mlr_fndoffset;
615 1.1 jdolecek if (dirclust != MSDOSFSROOT)
616 1.1 jdolecek clusoffset &= pmp->pm_crbomask;
617 1.34 maxv if ((error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
618 1.16 hannken B_MODIFY, &bp)) != 0) {
619 1.1 jdolecek goto err_norollback;
620 1.1 jdolecek }
621 1.1 jdolecek ndep = bptoep(pmp, bp, clusoffset);
622 1.1 jdolecek
623 1.1 jdolecek DE_EXTERNALIZE(ndep, dep);
624 1.1 jdolecek
625 1.1 jdolecek /*
626 1.1 jdolecek * Now write the Win95 long name
627 1.1 jdolecek */
628 1.38 hannken if (mlr->mlr_fndcnt > 0) {
629 1.40 thorpej u_int8_t chksum = msdosfs_winChksum(ndep->deName);
630 1.1 jdolecek const u_char *un = (const u_char *)cnp->cn_nameptr;
631 1.1 jdolecek int unlen = cnp->cn_namelen;
632 1.5 christos u_long xhavecnt;
633 1.1 jdolecek
634 1.38 hannken fndoffset = mlr->mlr_fndoffset;
635 1.38 hannken xhavecnt = mlr->mlr_fndcnt + 1;
636 1.1 jdolecek
637 1.5 christos for(; wcnt < xhavecnt; wcnt++) {
638 1.1 jdolecek if ((fndoffset & pmp->pm_crbomask) == 0) {
639 1.1 jdolecek /* we should never get here if ddep is root
640 1.1 jdolecek * directory */
641 1.1 jdolecek
642 1.1 jdolecek if (async)
643 1.1 jdolecek (void) bdwrite(bp);
644 1.1 jdolecek else if ((error = bwrite(bp)) != 0)
645 1.1 jdolecek goto rollback;
646 1.1 jdolecek
647 1.1 jdolecek fndoffset -= sizeof(struct direntry);
648 1.40 thorpej error = msdosfs_pcbmap(ddep,
649 1.1 jdolecek de_cluster(pmp, fndoffset),
650 1.1 jdolecek &bn, 0, &blsize);
651 1.1 jdolecek if (error)
652 1.1 jdolecek goto rollback;
653 1.1 jdolecek
654 1.10 scw error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn),
655 1.34 maxv blsize, B_MODIFY, &bp);
656 1.1 jdolecek if (error) {
657 1.1 jdolecek goto rollback;
658 1.1 jdolecek }
659 1.1 jdolecek ndep = bptoep(pmp, bp,
660 1.1 jdolecek fndoffset & pmp->pm_crbomask);
661 1.1 jdolecek } else {
662 1.1 jdolecek ndep--;
663 1.1 jdolecek fndoffset -= sizeof(struct direntry);
664 1.1 jdolecek }
665 1.40 thorpej if (!msdosfs_unix2winfn(un, unlen,
666 1.40 thorpej (struct winentry *)ndep,
667 1.35 mlelstv wcnt, chksum,
668 1.35 mlelstv ddep->de_pmp->pm_flags & MSDOSFSMNT_UTF8))
669 1.1 jdolecek break;
670 1.1 jdolecek }
671 1.1 jdolecek }
672 1.1 jdolecek
673 1.1 jdolecek if (async)
674 1.1 jdolecek bdwrite(bp);
675 1.1 jdolecek else if ((error = bwrite(bp)) != 0)
676 1.1 jdolecek goto rollback;
677 1.1 jdolecek
678 1.1 jdolecek /*
679 1.1 jdolecek * If they want us to return with the denode gotten.
680 1.1 jdolecek */
681 1.1 jdolecek if (depp) {
682 1.1 jdolecek u_long diroffset = clusoffset;
683 1.33 hannken
684 1.1 jdolecek if (dep->de_Attributes & ATTR_DIRECTORY) {
685 1.1 jdolecek dirclust = dep->de_StartCluster;
686 1.1 jdolecek if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)
687 1.1 jdolecek dirclust = MSDOSFSROOT;
688 1.1 jdolecek if (dirclust == MSDOSFSROOT)
689 1.1 jdolecek diroffset = MSDOSFSROOT_OFS;
690 1.1 jdolecek else
691 1.1 jdolecek diroffset = 0;
692 1.1 jdolecek }
693 1.33 hannken #ifdef MAKEFS
694 1.40 thorpej error = msdosfs_deget(pmp, dirclust, diroffset, depp);
695 1.33 hannken #else
696 1.33 hannken struct vnode *vp;
697 1.33 hannken
698 1.40 thorpej error = msdosfs_deget(pmp, dirclust, diroffset, &vp);
699 1.31 hannken if (error == 0)
700 1.33 hannken *depp = VTODE(vp);
701 1.33 hannken else
702 1.33 hannken *depp = NULL;
703 1.31 hannken #endif
704 1.31 hannken return error;
705 1.1 jdolecek }
706 1.1 jdolecek
707 1.1 jdolecek return 0;
708 1.1 jdolecek
709 1.1 jdolecek rollback:
710 1.1 jdolecek /*
711 1.1 jdolecek * Mark all slots modified so far as deleted. Note that we
712 1.1 jdolecek * can't just call removede(), since directory is not in
713 1.1 jdolecek * consistent state.
714 1.1 jdolecek */
715 1.38 hannken fndoffset = mlr->mlr_fndoffset;
716 1.40 thorpej rberror = msdosfs_pcbmap(ddep, de_cluster(pmp, fndoffset),
717 1.1 jdolecek &bn, NULL, &blsize);
718 1.1 jdolecek if (rberror)
719 1.1 jdolecek goto err_norollback;
720 1.34 maxv if ((rberror = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
721 1.16 hannken B_MODIFY, &bp)) != 0) {
722 1.1 jdolecek goto err_norollback;
723 1.1 jdolecek }
724 1.1 jdolecek ndep = bptoep(pmp, bp, clusoffset);
725 1.1 jdolecek
726 1.38 hannken havecnt = mlr->mlr_fndcnt + 1;
727 1.28 christos for(i = wcnt; i <= havecnt; i++) {
728 1.1 jdolecek /* mark entry as deleted */
729 1.1 jdolecek ndep->deName[0] = SLOT_DELETED;
730 1.1 jdolecek
731 1.1 jdolecek if ((fndoffset & pmp->pm_crbomask) == 0) {
732 1.1 jdolecek /* we should never get here if ddep is root
733 1.1 jdolecek * directory */
734 1.1 jdolecek
735 1.1 jdolecek if (async)
736 1.1 jdolecek bdwrite(bp);
737 1.1 jdolecek else if ((rberror = bwrite(bp)) != 0)
738 1.1 jdolecek goto err_norollback;
739 1.1 jdolecek
740 1.1 jdolecek fndoffset -= sizeof(struct direntry);
741 1.40 thorpej rberror = msdosfs_pcbmap(ddep,
742 1.1 jdolecek de_cluster(pmp, fndoffset),
743 1.1 jdolecek &bn, 0, &blsize);
744 1.1 jdolecek if (rberror)
745 1.1 jdolecek goto err_norollback;
746 1.1 jdolecek
747 1.10 scw rberror = bread(pmp->pm_devvp, de_bn2kb(pmp, bn),
748 1.34 maxv blsize, B_MODIFY, &bp);
749 1.1 jdolecek if (rberror) {
750 1.1 jdolecek goto err_norollback;
751 1.1 jdolecek }
752 1.1 jdolecek ndep = bptoep(pmp, bp, fndoffset);
753 1.1 jdolecek } else {
754 1.1 jdolecek ndep--;
755 1.1 jdolecek fndoffset -= sizeof(struct direntry);
756 1.1 jdolecek }
757 1.1 jdolecek }
758 1.1 jdolecek
759 1.1 jdolecek /* ignore any further error */
760 1.1 jdolecek if (async)
761 1.1 jdolecek (void) bdwrite(bp);
762 1.1 jdolecek else
763 1.1 jdolecek (void) bwrite(bp);
764 1.1 jdolecek
765 1.1 jdolecek err_norollback:
766 1.1 jdolecek return error;
767 1.1 jdolecek }
768 1.1 jdolecek
769 1.1 jdolecek /*
770 1.1 jdolecek * Be sure a directory is empty except for "." and "..". Return 1 if empty,
771 1.1 jdolecek * return 0 if not empty or error.
772 1.1 jdolecek */
773 1.1 jdolecek int
774 1.40 thorpej msdosfs_dosdirempty(struct denode *dep)
775 1.1 jdolecek {
776 1.1 jdolecek int blsize;
777 1.1 jdolecek int error;
778 1.1 jdolecek u_long cn;
779 1.1 jdolecek daddr_t bn;
780 1.1 jdolecek struct buf *bp;
781 1.1 jdolecek struct msdosfsmount *pmp = dep->de_pmp;
782 1.1 jdolecek struct direntry *dentp;
783 1.1 jdolecek
784 1.1 jdolecek /*
785 1.1 jdolecek * Since the filesize field in directory entries for a directory is
786 1.1 jdolecek * zero, we just have to feel our way through the directory until
787 1.1 jdolecek * we hit end of file.
788 1.1 jdolecek */
789 1.1 jdolecek for (cn = 0;; cn++) {
790 1.40 thorpej if ((error = msdosfs_pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
791 1.1 jdolecek if (error == E2BIG)
792 1.1 jdolecek return (1); /* it's empty */
793 1.1 jdolecek return (0);
794 1.1 jdolecek }
795 1.34 maxv error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
796 1.16 hannken 0, &bp);
797 1.1 jdolecek if (error) {
798 1.1 jdolecek return (0);
799 1.1 jdolecek }
800 1.1 jdolecek for (dentp = (struct direntry *)bp->b_data;
801 1.12 christos (char *)dentp < (char *)bp->b_data + blsize;
802 1.1 jdolecek dentp++) {
803 1.1 jdolecek if (dentp->deName[0] != SLOT_DELETED &&
804 1.1 jdolecek (dentp->deAttributes & ATTR_VOLUME) == 0) {
805 1.1 jdolecek /*
806 1.1 jdolecek * In dos directories an entry whose name
807 1.1 jdolecek * starts with SLOT_EMPTY (0) starts the
808 1.1 jdolecek * beginning of the unused part of the
809 1.1 jdolecek * directory, so we can just return that it
810 1.1 jdolecek * is empty.
811 1.1 jdolecek */
812 1.1 jdolecek if (dentp->deName[0] == SLOT_EMPTY) {
813 1.13 ad brelse(bp, 0);
814 1.1 jdolecek return (1);
815 1.1 jdolecek }
816 1.1 jdolecek /*
817 1.1 jdolecek * Any names other than "." and ".." in a
818 1.1 jdolecek * directory mean it is not empty.
819 1.1 jdolecek */
820 1.1 jdolecek if (memcmp(dentp->deName, ". ", 11) &&
821 1.1 jdolecek memcmp(dentp->deName, ".. ", 11)) {
822 1.13 ad brelse(bp, 0);
823 1.1 jdolecek #ifdef MSDOSFS_DEBUG
824 1.1 jdolecek printf("dosdirempty(): found %.11s, %d, %d\n",
825 1.1 jdolecek dentp->deName, dentp->deName[0],
826 1.1 jdolecek dentp->deName[1]);
827 1.1 jdolecek #endif
828 1.1 jdolecek return (0); /* not empty */
829 1.1 jdolecek }
830 1.1 jdolecek }
831 1.1 jdolecek }
832 1.13 ad brelse(bp, 0);
833 1.1 jdolecek }
834 1.1 jdolecek /* NOTREACHED */
835 1.1 jdolecek }
836 1.1 jdolecek
837 1.1 jdolecek /*
838 1.1 jdolecek * Read in the disk block containing the directory entry (dirclu, dirofs)
839 1.1 jdolecek * and return the address of the buf header, and the address of the
840 1.1 jdolecek * directory entry within the block.
841 1.1 jdolecek */
842 1.1 jdolecek int
843 1.40 thorpej msdosfs_readep(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset,
844 1.40 thorpej struct buf **bpp, struct direntry **epp)
845 1.1 jdolecek {
846 1.1 jdolecek int error;
847 1.1 jdolecek daddr_t bn;
848 1.1 jdolecek int blsize;
849 1.1 jdolecek
850 1.1 jdolecek blsize = pmp->pm_bpcluster;
851 1.1 jdolecek if (dirclust == MSDOSFSROOT
852 1.1 jdolecek && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize)
853 1.1 jdolecek blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask;
854 1.1 jdolecek bn = detobn(pmp, dirclust, diroffset);
855 1.34 maxv if ((error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
856 1.16 hannken 0, bpp)) != 0) {
857 1.1 jdolecek *bpp = NULL;
858 1.1 jdolecek return (error);
859 1.1 jdolecek }
860 1.1 jdolecek if (epp)
861 1.1 jdolecek *epp = bptoep(pmp, *bpp, diroffset);
862 1.1 jdolecek return (0);
863 1.1 jdolecek }
864 1.1 jdolecek
865 1.1 jdolecek /*
866 1.1 jdolecek * Read in the disk block containing the directory entry dep came from and
867 1.1 jdolecek * return the address of the buf header, and the address of the directory
868 1.1 jdolecek * entry within the block.
869 1.1 jdolecek */
870 1.1 jdolecek int
871 1.40 thorpej msdosfs_readde(struct denode *dep, struct buf **bpp, struct direntry **epp)
872 1.1 jdolecek {
873 1.40 thorpej return (msdosfs_readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset,
874 1.1 jdolecek bpp, epp));
875 1.1 jdolecek }
876 1.1 jdolecek
877 1.1 jdolecek /*
878 1.1 jdolecek * Remove a directory entry. At this point the file represented by the
879 1.1 jdolecek * directory entry to be removed is still full length until noone has it
880 1.1 jdolecek * open. When the file no longer being used msdosfs_inactive() is called
881 1.1 jdolecek * and will truncate the file to 0 length. When the vnode containing the
882 1.1 jdolecek * denode is needed for some other purpose by VFS it will call
883 1.1 jdolecek * msdosfs_reclaim() which will remove the denode from the denode cache.
884 1.1 jdolecek */
885 1.1 jdolecek int
886 1.40 thorpej msdosfs_removede(struct denode *pdep, struct denode *dep,
887 1.38 hannken const struct msdosfs_lookup_results *mlr)
888 1.19 dsl /* pdep: directory where the entry is removed */
889 1.19 dsl /* dep: file to be removed */
890 1.38 hannken /* mlr: position of dep in pdep from lookup */
891 1.1 jdolecek {
892 1.1 jdolecek int error;
893 1.1 jdolecek struct direntry *ep;
894 1.1 jdolecek struct buf *bp;
895 1.1 jdolecek daddr_t bn;
896 1.1 jdolecek int blsize;
897 1.1 jdolecek struct msdosfsmount *pmp = pdep->de_pmp;
898 1.38 hannken u_long offset = mlr->mlr_fndoffset;
899 1.28 christos #ifdef _KERNEL
900 1.1 jdolecek int async = pdep->de_pmp->pm_mountp->mnt_flag & MNT_ASYNC;
901 1.28 christos #else
902 1.28 christos #define async 0
903 1.28 christos #endif
904 1.1 jdolecek
905 1.1 jdolecek #ifdef MSDOSFS_DEBUG
906 1.1 jdolecek printf("removede(): filename %s, dep %p, offset %08lx\n",
907 1.1 jdolecek dep->de_Name, dep, offset);
908 1.1 jdolecek #endif
909 1.1 jdolecek
910 1.33 hannken if (--dep->de_refcnt == 0) {
911 1.33 hannken #ifndef MAKEFS
912 1.33 hannken struct denode_key old_key = dep->de_key;
913 1.33 hannken struct denode_key new_key = dep->de_key;
914 1.33 hannken
915 1.33 hannken KASSERT(new_key.dk_dirgen == NULL);
916 1.33 hannken new_key.dk_dirgen = dep;
917 1.33 hannken vcache_rekey_enter(pmp->pm_mountp, DETOV(dep), &old_key,
918 1.33 hannken sizeof(old_key), &new_key, sizeof(new_key));
919 1.33 hannken dep->de_key = new_key;
920 1.33 hannken vcache_rekey_exit(pmp->pm_mountp, DETOV(dep), &old_key,
921 1.33 hannken sizeof(old_key), &dep->de_key, sizeof(dep->de_key));
922 1.33 hannken #endif
923 1.33 hannken }
924 1.1 jdolecek offset += sizeof(struct direntry);
925 1.1 jdolecek do {
926 1.1 jdolecek offset -= sizeof(struct direntry);
927 1.40 thorpej error = msdosfs_pcbmap(pdep, de_cluster(pmp, offset), &bn, 0,
928 1.40 thorpej &blsize);
929 1.1 jdolecek if (error)
930 1.1 jdolecek return error;
931 1.34 maxv error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
932 1.16 hannken B_MODIFY, &bp);
933 1.1 jdolecek if (error) {
934 1.1 jdolecek return error;
935 1.1 jdolecek }
936 1.1 jdolecek ep = bptoep(pmp, bp, offset);
937 1.1 jdolecek /*
938 1.1 jdolecek * Check whether, if we came here the second time, i.e.
939 1.1 jdolecek * when underflowing into the previous block, the last
940 1.1 jdolecek * entry in this block is a longfilename entry, too.
941 1.1 jdolecek */
942 1.1 jdolecek if (ep->deAttributes != ATTR_WIN95
943 1.38 hannken && offset != mlr->mlr_fndoffset) {
944 1.13 ad brelse(bp, 0);
945 1.1 jdolecek break;
946 1.1 jdolecek }
947 1.1 jdolecek offset += sizeof(struct direntry);
948 1.1 jdolecek while (1) {
949 1.1 jdolecek /*
950 1.37 andvar * We are a bit aggressive here in that we delete any Win95
951 1.1 jdolecek * entries preceding this entry, not just the ones we "own".
952 1.1 jdolecek * Since these presumably aren't valid anyway,
953 1.1 jdolecek * there should be no harm.
954 1.1 jdolecek */
955 1.1 jdolecek offset -= sizeof(struct direntry);
956 1.1 jdolecek ep--->deName[0] = SLOT_DELETED;
957 1.1 jdolecek if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95)
958 1.1 jdolecek || !(offset & pmp->pm_crbomask)
959 1.1 jdolecek || ep->deAttributes != ATTR_WIN95)
960 1.1 jdolecek break;
961 1.1 jdolecek }
962 1.1 jdolecek if (async)
963 1.1 jdolecek bdwrite(bp);
964 1.1 jdolecek else if ((error = bwrite(bp)) != 0)
965 1.1 jdolecek return error;
966 1.1 jdolecek } while (!(pmp->pm_flags & MSDOSFSMNT_NOWIN95)
967 1.1 jdolecek && !(offset & pmp->pm_crbomask)
968 1.1 jdolecek && offset);
969 1.1 jdolecek return 0;
970 1.1 jdolecek }
971 1.1 jdolecek
972 1.1 jdolecek /*
973 1.1 jdolecek * Create a unique DOS name in dvp
974 1.1 jdolecek */
975 1.1 jdolecek int
976 1.40 thorpej msdosfs_uniqdosname(struct denode *dep, struct componentname *cnp, u_char *cp)
977 1.1 jdolecek {
978 1.1 jdolecek struct msdosfsmount *pmp = dep->de_pmp;
979 1.1 jdolecek struct direntry *dentp;
980 1.1 jdolecek int gen;
981 1.1 jdolecek int blsize;
982 1.1 jdolecek u_long cn;
983 1.1 jdolecek daddr_t bn;
984 1.1 jdolecek struct buf *bp;
985 1.1 jdolecek int error;
986 1.1 jdolecek
987 1.1 jdolecek for (gen = 1;; gen++) {
988 1.1 jdolecek /*
989 1.1 jdolecek * Generate DOS name with generation number
990 1.1 jdolecek */
991 1.40 thorpej if (!msdosfs_unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
992 1.1 jdolecek cnp->cn_namelen, gen))
993 1.1 jdolecek return gen == 1 ? EINVAL : EEXIST;
994 1.1 jdolecek
995 1.1 jdolecek /*
996 1.1 jdolecek * Now look for a dir entry with this exact name
997 1.1 jdolecek */
998 1.1 jdolecek for (cn = error = 0; !error; cn++) {
999 1.40 thorpej if ((error = msdosfs_pcbmap(dep, cn, &bn, 0,
1000 1.40 thorpej &blsize)) != 0) {
1001 1.1 jdolecek if (error == E2BIG) /* EOF reached and not found */
1002 1.1 jdolecek return 0;
1003 1.1 jdolecek return error;
1004 1.1 jdolecek }
1005 1.10 scw error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
1006 1.34 maxv 0, &bp);
1007 1.1 jdolecek if (error) {
1008 1.1 jdolecek return error;
1009 1.1 jdolecek }
1010 1.1 jdolecek for (dentp = (struct direntry *)bp->b_data;
1011 1.12 christos (char *)dentp < (char *)bp->b_data + blsize;
1012 1.1 jdolecek dentp++) {
1013 1.1 jdolecek if (dentp->deName[0] == SLOT_EMPTY) {
1014 1.1 jdolecek /*
1015 1.1 jdolecek * Last used entry and not found
1016 1.1 jdolecek */
1017 1.13 ad brelse(bp, 0);
1018 1.1 jdolecek return 0;
1019 1.1 jdolecek }
1020 1.1 jdolecek /*
1021 1.1 jdolecek * Ignore volume labels and Win95 entries
1022 1.1 jdolecek */
1023 1.1 jdolecek if (dentp->deAttributes & ATTR_VOLUME)
1024 1.1 jdolecek continue;
1025 1.1 jdolecek if (!memcmp(dentp->deName, cp, 11)) {
1026 1.1 jdolecek error = EEXIST;
1027 1.1 jdolecek break;
1028 1.1 jdolecek }
1029 1.1 jdolecek }
1030 1.13 ad brelse(bp, 0);
1031 1.1 jdolecek }
1032 1.1 jdolecek }
1033 1.1 jdolecek }
1034 1.1 jdolecek
1035 1.1 jdolecek /*
1036 1.1 jdolecek * Find any Win'95 long filename entry in directory dep
1037 1.1 jdolecek */
1038 1.1 jdolecek int
1039 1.40 thorpej msdosfs_findwin95(struct denode *dep)
1040 1.1 jdolecek {
1041 1.1 jdolecek struct msdosfsmount *pmp = dep->de_pmp;
1042 1.1 jdolecek struct direntry *dentp;
1043 1.17 jmcneill int blsize, win95;
1044 1.1 jdolecek u_long cn;
1045 1.1 jdolecek daddr_t bn;
1046 1.1 jdolecek struct buf *bp;
1047 1.1 jdolecek
1048 1.17 jmcneill win95 = 1;
1049 1.1 jdolecek /*
1050 1.1 jdolecek * Read through the directory looking for Win'95 entries
1051 1.1 jdolecek * XXX Note: Error currently handled just as EOF
1052 1.1 jdolecek */
1053 1.1 jdolecek for (cn = 0;; cn++) {
1054 1.40 thorpej if (msdosfs_pcbmap(dep, cn, &bn, 0, &blsize))
1055 1.17 jmcneill return win95;
1056 1.34 maxv if (bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
1057 1.16 hannken 0, &bp)) {
1058 1.17 jmcneill return win95;
1059 1.1 jdolecek }
1060 1.1 jdolecek for (dentp = (struct direntry *)bp->b_data;
1061 1.12 christos (char *)dentp < (char *)bp->b_data + blsize;
1062 1.1 jdolecek dentp++) {
1063 1.1 jdolecek if (dentp->deName[0] == SLOT_EMPTY) {
1064 1.1 jdolecek /*
1065 1.1 jdolecek * Last used entry and not found
1066 1.1 jdolecek */
1067 1.13 ad brelse(bp, 0);
1068 1.17 jmcneill return win95;
1069 1.1 jdolecek }
1070 1.1 jdolecek if (dentp->deName[0] == SLOT_DELETED) {
1071 1.1 jdolecek /*
1072 1.1 jdolecek * Ignore deleted files
1073 1.1 jdolecek * Note: might be an indication of Win'95
1074 1.1 jdolecek * anyway XXX
1075 1.1 jdolecek */
1076 1.1 jdolecek continue;
1077 1.1 jdolecek }
1078 1.1 jdolecek if (dentp->deAttributes == ATTR_WIN95) {
1079 1.13 ad brelse(bp, 0);
1080 1.1 jdolecek return 1;
1081 1.1 jdolecek }
1082 1.17 jmcneill win95 = 0;
1083 1.1 jdolecek }
1084 1.13 ad brelse(bp, 0);
1085 1.1 jdolecek }
1086 1.1 jdolecek }
1087