msdosfs_denode.c revision 1.48.10.1 1 1.48.10.1 tls /* $NetBSD: msdosfs_denode.c,v 1.48.10.1 2014/08/10 06:55:53 tls 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.1 jdolecek #include <sys/cdefs.h>
51 1.48.10.1 tls __KERNEL_RCSID(0, "$NetBSD: msdosfs_denode.c,v 1.48.10.1 2014/08/10 06:55:53 tls Exp $");
52 1.1 jdolecek
53 1.1 jdolecek #include <sys/param.h>
54 1.1 jdolecek #include <sys/systm.h>
55 1.1 jdolecek #include <sys/mount.h>
56 1.38 hannken #include <sys/fstrans.h>
57 1.1 jdolecek #include <sys/malloc.h>
58 1.1 jdolecek #include <sys/pool.h>
59 1.1 jdolecek #include <sys/proc.h>
60 1.1 jdolecek #include <sys/buf.h>
61 1.1 jdolecek #include <sys/vnode.h>
62 1.1 jdolecek #include <sys/kernel.h> /* defines "time" */
63 1.1 jdolecek #include <sys/dirent.h>
64 1.1 jdolecek #include <sys/namei.h>
65 1.14 elad #include <sys/kauth.h>
66 1.1 jdolecek
67 1.1 jdolecek #include <uvm/uvm_extern.h>
68 1.1 jdolecek
69 1.1 jdolecek #include <fs/msdosfs/bpb.h>
70 1.1 jdolecek #include <fs/msdosfs/msdosfsmount.h>
71 1.1 jdolecek #include <fs/msdosfs/direntry.h>
72 1.1 jdolecek #include <fs/msdosfs/denode.h>
73 1.1 jdolecek #include <fs/msdosfs/fat.h>
74 1.1 jdolecek
75 1.21 pooka struct pool msdosfs_denode_pool;
76 1.1 jdolecek
77 1.1 jdolecek extern int prtactive;
78 1.1 jdolecek
79 1.43 hannken struct fh_key {
80 1.43 hannken struct msdosfsmount *fhk_mount;
81 1.43 hannken uint32_t fhk_dircluster;
82 1.43 hannken uint32_t fhk_diroffset;
83 1.43 hannken };
84 1.43 hannken struct fh_node {
85 1.43 hannken struct rb_node fh_rbnode;
86 1.43 hannken struct fh_key fh_key;
87 1.43 hannken #define fh_mount fh_key.fhk_mount
88 1.43 hannken #define fh_dircluster fh_key.fhk_dircluster
89 1.43 hannken #define fh_diroffset fh_key.fhk_diroffset
90 1.43 hannken uint32_t fh_gen;
91 1.43 hannken };
92 1.43 hannken
93 1.43 hannken static int
94 1.43 hannken fh_compare_node_fh(void *ctx, const void *b, const void *key)
95 1.43 hannken {
96 1.43 hannken const struct fh_node * const pnp = b;
97 1.43 hannken const struct fh_key * const fhp = key;
98 1.43 hannken
99 1.43 hannken /* msdosfs_fh_destroy() below depends on first sorting on fh_mount. */
100 1.43 hannken if (pnp->fh_mount != fhp->fhk_mount)
101 1.43 hannken return (intptr_t)pnp->fh_mount - (intptr_t)fhp->fhk_mount;
102 1.43 hannken if (pnp->fh_dircluster != fhp->fhk_dircluster)
103 1.43 hannken return pnp->fh_dircluster - fhp->fhk_dircluster;
104 1.43 hannken return pnp->fh_diroffset - fhp->fhk_diroffset;
105 1.43 hannken }
106 1.43 hannken
107 1.43 hannken static int
108 1.43 hannken fh_compare_nodes(void *ctx, const void *parent, const void *node)
109 1.43 hannken {
110 1.43 hannken const struct fh_node * const np = node;
111 1.43 hannken
112 1.43 hannken return fh_compare_node_fh(ctx, parent, &np->fh_key);
113 1.43 hannken }
114 1.43 hannken
115 1.43 hannken static uint32_t fh_generation;
116 1.43 hannken static kmutex_t fh_lock;
117 1.43 hannken static struct pool fh_pool;
118 1.43 hannken static rb_tree_t fh_rbtree;
119 1.43 hannken static const rb_tree_ops_t fh_rbtree_ops = {
120 1.43 hannken .rbto_compare_nodes = fh_compare_nodes,
121 1.43 hannken .rbto_compare_key = fh_compare_node_fh,
122 1.43 hannken .rbto_node_offset = offsetof(struct fh_node, fh_rbnode),
123 1.43 hannken .rbto_context = NULL
124 1.43 hannken };
125 1.43 hannken
126 1.8 yamt static const struct genfs_ops msdosfs_genfsops = {
127 1.8 yamt .gop_size = genfs_size,
128 1.8 yamt .gop_alloc = msdosfs_gop_alloc,
129 1.8 yamt .gop_write = genfs_gop_write,
130 1.9 yamt .gop_markupdate = msdosfs_gop_markupdate,
131 1.1 jdolecek };
132 1.1 jdolecek
133 1.5 atatat MALLOC_DECLARE(M_MSDOSFSFAT);
134 1.5 atatat
135 1.1 jdolecek void
136 1.36 cegger msdosfs_init(void)
137 1.1 jdolecek {
138 1.21 pooka
139 1.5 atatat malloc_type_attach(M_MSDOSFSMNT);
140 1.5 atatat malloc_type_attach(M_MSDOSFSFAT);
141 1.23 rumble malloc_type_attach(M_MSDOSFSTMP);
142 1.7 atatat pool_init(&msdosfs_denode_pool, sizeof(struct denode), 0, 0, 0,
143 1.20 ad "msdosnopl", &pool_allocator_nointr, IPL_NONE);
144 1.43 hannken pool_init(&fh_pool, sizeof(struct fh_node), 0, 0, 0,
145 1.43 hannken "msdosfhpl", &pool_allocator_nointr, IPL_NONE);
146 1.43 hannken rb_tree_init(&fh_rbtree, &fh_rbtree_ops);
147 1.43 hannken mutex_init(&fh_lock, MUTEX_DEFAULT, IPL_NONE);
148 1.1 jdolecek }
149 1.1 jdolecek
150 1.1 jdolecek /*
151 1.48.10.1 tls * Reinitialize.
152 1.1 jdolecek */
153 1.1 jdolecek
154 1.1 jdolecek void
155 1.36 cegger msdosfs_reinit(void)
156 1.1 jdolecek {
157 1.48.10.1 tls
158 1.1 jdolecek }
159 1.1 jdolecek
160 1.1 jdolecek void
161 1.36 cegger msdosfs_done(void)
162 1.1 jdolecek {
163 1.1 jdolecek pool_destroy(&msdosfs_denode_pool);
164 1.43 hannken pool_destroy(&fh_pool);
165 1.43 hannken mutex_destroy(&fh_lock);
166 1.23 rumble malloc_type_detach(M_MSDOSFSTMP);
167 1.5 atatat malloc_type_detach(M_MSDOSFSFAT);
168 1.5 atatat malloc_type_detach(M_MSDOSFSMNT);
169 1.1 jdolecek }
170 1.1 jdolecek
171 1.1 jdolecek /*
172 1.48.10.1 tls * If deget() succeeds it returns with the gotten denode unlocked.
173 1.1 jdolecek *
174 1.1 jdolecek * pmp - address of msdosfsmount structure of the filesystem containing
175 1.1 jdolecek * the denode of interest. The pm_dev field and the address of
176 1.1 jdolecek * the msdosfsmount structure are used.
177 1.1 jdolecek * dirclust - which cluster bp contains, if dirclust is 0 (root directory)
178 1.1 jdolecek * diroffset is relative to the beginning of the root directory,
179 1.1 jdolecek * otherwise it is cluster relative.
180 1.1 jdolecek * diroffset - offset past begin of cluster of denode we want
181 1.48.10.1 tls * vpp - returns the address of the gotten vnode.
182 1.1 jdolecek */
183 1.1 jdolecek int
184 1.48.10.1 tls deget(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset,
185 1.48.10.1 tls struct vnode **vpp)
186 1.35 dsl /* pmp: so we know the maj/min number */
187 1.35 dsl /* dirclust: cluster this dir entry came from */
188 1.35 dsl /* diroffset: index of entry within the cluster */
189 1.48.10.1 tls /* vpp: returns the addr of the gotten vnode */
190 1.1 jdolecek {
191 1.1 jdolecek int error;
192 1.48.10.1 tls struct denode_key key;
193 1.1 jdolecek
194 1.1 jdolecek /*
195 1.1 jdolecek * On FAT32 filesystems, root is a (more or less) normal
196 1.1 jdolecek * directory
197 1.1 jdolecek */
198 1.1 jdolecek if (FAT32(pmp) && dirclust == MSDOSFSROOT)
199 1.1 jdolecek dirclust = pmp->pm_rootdirblk;
200 1.1 jdolecek
201 1.48.10.1 tls memset(&key, 0, sizeof(key));
202 1.48.10.1 tls key.dk_dirclust = dirclust;
203 1.48.10.1 tls key.dk_diroffset = diroffset;
204 1.48.10.1 tls /* key.dk_dirgen = NULL; */
205 1.1 jdolecek
206 1.48.10.1 tls error = vcache_get(pmp->pm_mountp, &key, sizeof(key), vpp);
207 1.48.10.1 tls return error;
208 1.48.10.1 tls }
209 1.28 ad
210 1.48.10.1 tls int
211 1.48.10.1 tls msdosfs_loadvnode(struct mount *mp, struct vnode *vp,
212 1.48.10.1 tls const void *key, size_t key_len, const void **new_key)
213 1.48.10.1 tls {
214 1.48.10.1 tls bool is_root;
215 1.48.10.1 tls int error;
216 1.48.10.1 tls extern int (**msdosfs_vnodeop_p)(void *);
217 1.48.10.1 tls struct msdosfsmount *pmp;
218 1.48.10.1 tls struct direntry *direntptr;
219 1.48.10.1 tls struct denode *ldep;
220 1.48.10.1 tls struct buf *bp;
221 1.48.10.1 tls struct denode_key dkey;
222 1.1 jdolecek
223 1.48.10.1 tls KASSERT(key_len == sizeof(dkey));
224 1.48.10.1 tls memcpy(&dkey, key, key_len);
225 1.48.10.1 tls KASSERT(dkey.dk_dirgen == NULL);
226 1.48.10.1 tls
227 1.48.10.1 tls pmp = VFSTOMSDOSFS(mp);
228 1.48.10.1 tls is_root = ((dkey.dk_dirclust == MSDOSFSROOT ||
229 1.48.10.1 tls (FAT32(pmp) && dkey.dk_dirclust == pmp->pm_rootdirblk)) &&
230 1.48.10.1 tls dkey.dk_diroffset == MSDOSFSROOT_OFS);
231 1.1 jdolecek
232 1.48.10.1 tls #ifdef MSDOSFS_DEBUG
233 1.48.10.1 tls printf("loadvnode(pmp %p, dirclust %lu, diroffset %lx, vp %p)\n",
234 1.48.10.1 tls pmp, dkey.dk_dirclust, dkey.dk_diroffset, vp);
235 1.48.10.1 tls #endif
236 1.48.10.1 tls
237 1.48.10.1 tls ldep = pool_get(&msdosfs_denode_pool, PR_WAITOK);
238 1.48.10.1 tls memset(ldep, 0, sizeof *ldep);
239 1.48.10.1 tls /* ldep->de_flag = 0; */
240 1.48.10.1 tls /* ldep->de_devvp = 0; */
241 1.48.10.1 tls /* ldep->de_lockf = 0; */
242 1.48.10.1 tls ldep->de_dev = pmp->pm_dev;
243 1.48.10.1 tls ldep->de_dirclust = dkey.dk_dirclust;
244 1.48.10.1 tls ldep->de_diroffset = dkey.dk_diroffset;
245 1.1 jdolecek ldep->de_pmp = pmp;
246 1.1 jdolecek ldep->de_devvp = pmp->pm_devvp;
247 1.1 jdolecek ldep->de_refcnt = 1;
248 1.48.10.1 tls fc_purge(ldep, 0); /* init the FAT cache for this denode */
249 1.48.10.1 tls
250 1.1 jdolecek /*
251 1.1 jdolecek * Copy the directory entry into the denode area of the vnode.
252 1.1 jdolecek */
253 1.48.10.1 tls if (is_root) {
254 1.1 jdolecek /*
255 1.1 jdolecek * Directory entry for the root directory. There isn't one,
256 1.1 jdolecek * so we manufacture one. We should probably rummage
257 1.1 jdolecek * through the root directory and find a label entry (if it
258 1.1 jdolecek * exists), and then use the time and date from that entry
259 1.1 jdolecek * as the time and date for the root denode.
260 1.1 jdolecek */
261 1.1 jdolecek ldep->de_Attributes = ATTR_DIRECTORY;
262 1.1 jdolecek if (FAT32(pmp))
263 1.1 jdolecek ldep->de_StartCluster = pmp->pm_rootdirblk;
264 1.1 jdolecek /* de_FileSize will be filled in further down */
265 1.1 jdolecek else {
266 1.1 jdolecek ldep->de_StartCluster = MSDOSFSROOT;
267 1.48.10.1 tls ldep->de_FileSize = pmp->pm_rootdirsize *
268 1.48.10.1 tls pmp->pm_BytesPerSec;
269 1.1 jdolecek }
270 1.1 jdolecek /*
271 1.1 jdolecek * fill in time and date so that dos2unixtime() doesn't
272 1.1 jdolecek * spit up when called from msdosfs_getattr() with root
273 1.1 jdolecek * denode
274 1.1 jdolecek */
275 1.1 jdolecek ldep->de_CHun = 0;
276 1.1 jdolecek ldep->de_CTime = 0x0000; /* 00:00:00 */
277 1.1 jdolecek ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
278 1.1 jdolecek | (1 << DD_DAY_SHIFT);
279 1.1 jdolecek /* Jan 1, 1980 */
280 1.1 jdolecek ldep->de_ADate = ldep->de_CDate;
281 1.1 jdolecek ldep->de_MTime = ldep->de_CTime;
282 1.1 jdolecek ldep->de_MDate = ldep->de_CDate;
283 1.1 jdolecek /* leave the other fields as garbage */
284 1.1 jdolecek } else {
285 1.48.10.1 tls error = readep(pmp, ldep->de_dirclust, ldep->de_diroffset,
286 1.48.10.1 tls &bp, &direntptr);
287 1.24 pooka if (error) {
288 1.48.10.1 tls pool_put(&msdosfs_denode_pool, ldep);
289 1.48.10.1 tls return error;
290 1.24 pooka }
291 1.1 jdolecek DE_INTERNALIZE(ldep, direntptr);
292 1.25 ad brelse(bp, 0);
293 1.1 jdolecek }
294 1.1 jdolecek
295 1.1 jdolecek /*
296 1.1 jdolecek * Fill in a few fields of the vnode and finish filling in the
297 1.48.10.1 tls * denode.
298 1.1 jdolecek */
299 1.1 jdolecek if (ldep->de_Attributes & ATTR_DIRECTORY) {
300 1.1 jdolecek /*
301 1.1 jdolecek * Since DOS directory entries that describe directories
302 1.1 jdolecek * have 0 in the filesize field, we take this opportunity
303 1.1 jdolecek * to find out the length of the directory and plug it into
304 1.1 jdolecek * the denode structure.
305 1.1 jdolecek */
306 1.1 jdolecek u_long size;
307 1.1 jdolecek
308 1.48.10.1 tls vp->v_type = VDIR;
309 1.1 jdolecek if (ldep->de_StartCluster != MSDOSFSROOT) {
310 1.1 jdolecek error = pcbmap(ldep, CLUST_END, 0, &size, 0);
311 1.1 jdolecek if (error == E2BIG) {
312 1.1 jdolecek ldep->de_FileSize = de_cn2off(pmp, size);
313 1.1 jdolecek error = 0;
314 1.1 jdolecek } else
315 1.48.10.1 tls printf("loadvnode(): pcbmap returned %d\n",
316 1.48.10.1 tls error);
317 1.1 jdolecek }
318 1.1 jdolecek } else
319 1.48.10.1 tls vp->v_type = VREG;
320 1.37 pooka vref(ldep->de_devvp);
321 1.48.10.1 tls if (is_root)
322 1.48.10.1 tls vp->v_vflag |= VV_ROOT;
323 1.48.10.1 tls vp->v_tag = VT_MSDOSFS;
324 1.48.10.1 tls vp->v_op = msdosfs_vnodeop_p;
325 1.48.10.1 tls vp->v_data = ldep;
326 1.48.10.1 tls ldep->de_vnode = vp;
327 1.48.10.1 tls genfs_node_init(vp, &msdosfs_genfsops);
328 1.48.10.1 tls uvm_vnp_setsize(vp, ldep->de_FileSize);
329 1.48.10.1 tls *new_key = &ldep->de_key;
330 1.48.10.1 tls
331 1.48.10.1 tls return 0;
332 1.1 jdolecek }
333 1.1 jdolecek
334 1.1 jdolecek int
335 1.34 dsl deupdat(struct denode *dep, int waitfor)
336 1.1 jdolecek {
337 1.1 jdolecek
338 1.11 yamt return (msdosfs_update(DETOV(dep), NULL, NULL,
339 1.11 yamt waitfor ? UPDATE_WAIT : 0));
340 1.1 jdolecek }
341 1.1 jdolecek
342 1.1 jdolecek /*
343 1.1 jdolecek * Truncate the file described by dep to the length specified by length.
344 1.1 jdolecek */
345 1.1 jdolecek int
346 1.27 pooka detrunc(struct denode *dep, u_long length, int flags, kauth_cred_t cred)
347 1.1 jdolecek {
348 1.1 jdolecek int error;
349 1.1 jdolecek int allerror;
350 1.1 jdolecek u_long eofentry;
351 1.13 christos u_long chaintofree = 0;
352 1.1 jdolecek daddr_t bn, lastblock;
353 1.1 jdolecek int boff;
354 1.1 jdolecek int isadir = dep->de_Attributes & ATTR_DIRECTORY;
355 1.1 jdolecek struct buf *bp;
356 1.1 jdolecek struct msdosfsmount *pmp = dep->de_pmp;
357 1.1 jdolecek
358 1.1 jdolecek #ifdef MSDOSFS_DEBUG
359 1.1 jdolecek printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags);
360 1.1 jdolecek #endif
361 1.1 jdolecek
362 1.1 jdolecek /*
363 1.1 jdolecek * Disallow attempts to truncate the root directory since it is of
364 1.1 jdolecek * fixed size. That's just the way dos filesystems are. We use
365 1.1 jdolecek * the VROOT bit in the vnode because checking for the directory
366 1.1 jdolecek * bit and a startcluster of 0 in the denode is not adequate to
367 1.1 jdolecek * recognize the root directory at this point in a file or
368 1.1 jdolecek * directory's life.
369 1.1 jdolecek */
370 1.26 ad if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) {
371 1.1 jdolecek printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
372 1.1 jdolecek dep->de_dirclust, dep->de_diroffset);
373 1.1 jdolecek return (EINVAL);
374 1.1 jdolecek }
375 1.1 jdolecek
376 1.1 jdolecek uvm_vnp_setsize(DETOV(dep), length);
377 1.1 jdolecek
378 1.1 jdolecek if (dep->de_FileSize < length)
379 1.1 jdolecek return (deextend(dep, length, cred));
380 1.1 jdolecek lastblock = de_clcount(pmp, length) - 1;
381 1.1 jdolecek
382 1.1 jdolecek /*
383 1.1 jdolecek * If the desired length is 0 then remember the starting cluster of
384 1.1 jdolecek * the file and set the StartCluster field in the directory entry
385 1.1 jdolecek * to 0. If the desired length is not zero, then get the number of
386 1.1 jdolecek * the last cluster in the shortened file. Then get the number of
387 1.1 jdolecek * the first cluster in the part of the file that is to be freed.
388 1.1 jdolecek * Then set the next cluster pointer in the last cluster of the
389 1.1 jdolecek * file to CLUST_EOFE.
390 1.1 jdolecek */
391 1.1 jdolecek if (length == 0) {
392 1.1 jdolecek chaintofree = dep->de_StartCluster;
393 1.1 jdolecek dep->de_StartCluster = 0;
394 1.1 jdolecek eofentry = ~0;
395 1.1 jdolecek } else {
396 1.1 jdolecek error = pcbmap(dep, lastblock, 0, &eofentry, 0);
397 1.1 jdolecek if (error) {
398 1.1 jdolecek #ifdef MSDOSFS_DEBUG
399 1.1 jdolecek printf("detrunc(): pcbmap fails %d\n", error);
400 1.1 jdolecek #endif
401 1.1 jdolecek return (error);
402 1.1 jdolecek }
403 1.1 jdolecek }
404 1.1 jdolecek
405 1.1 jdolecek /*
406 1.1 jdolecek * If the new length is not a multiple of the cluster size then we
407 1.1 jdolecek * must zero the tail end of the new last cluster in case it
408 1.1 jdolecek * becomes part of the file again because of a seek.
409 1.1 jdolecek */
410 1.1 jdolecek if ((boff = length & pmp->pm_crbomask) != 0) {
411 1.1 jdolecek if (isadir) {
412 1.1 jdolecek bn = cntobn(pmp, eofentry);
413 1.17 scw error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn),
414 1.33 hannken pmp->pm_bpcluster, NOCRED, B_MODIFY, &bp);
415 1.1 jdolecek if (error) {
416 1.1 jdolecek #ifdef MSDOSFS_DEBUG
417 1.1 jdolecek printf("detrunc(): bread fails %d\n", error);
418 1.1 jdolecek #endif
419 1.1 jdolecek return (error);
420 1.1 jdolecek }
421 1.19 christos memset((char *)bp->b_data + boff, 0,
422 1.19 christos pmp->pm_bpcluster - boff);
423 1.1 jdolecek if (flags & IO_SYNC)
424 1.1 jdolecek bwrite(bp);
425 1.1 jdolecek else
426 1.1 jdolecek bdwrite(bp);
427 1.1 jdolecek } else {
428 1.46 hannken ubc_zerorange(&DETOV(dep)->v_uobj, length,
429 1.46 hannken pmp->pm_bpcluster - boff,
430 1.46 hannken UBC_UNMAP_FLAG(DETOV(dep)));
431 1.1 jdolecek }
432 1.1 jdolecek }
433 1.1 jdolecek
434 1.1 jdolecek /*
435 1.1 jdolecek * Write out the updated directory entry. Even if the update fails
436 1.1 jdolecek * we free the trailing clusters.
437 1.1 jdolecek */
438 1.1 jdolecek dep->de_FileSize = length;
439 1.1 jdolecek if (!isadir)
440 1.1 jdolecek dep->de_flag |= DE_UPDATE|DE_MODIFIED;
441 1.1 jdolecek vtruncbuf(DETOV(dep), lastblock + 1, 0, 0);
442 1.1 jdolecek allerror = deupdat(dep, 1);
443 1.1 jdolecek #ifdef MSDOSFS_DEBUG
444 1.1 jdolecek printf("detrunc(): allerror %d, eofentry %lu\n",
445 1.1 jdolecek allerror, eofentry);
446 1.1 jdolecek #endif
447 1.1 jdolecek
448 1.42 hannken fc_purge(dep, lastblock + 1);
449 1.42 hannken
450 1.1 jdolecek /*
451 1.1 jdolecek * If we need to break the cluster chain for the file then do it
452 1.1 jdolecek * now.
453 1.1 jdolecek */
454 1.1 jdolecek if (eofentry != ~0) {
455 1.1 jdolecek error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
456 1.1 jdolecek &chaintofree, CLUST_EOFE);
457 1.1 jdolecek if (error) {
458 1.1 jdolecek #ifdef MSDOSFS_DEBUG
459 1.1 jdolecek printf("detrunc(): fatentry errors %d\n", error);
460 1.1 jdolecek #endif
461 1.1 jdolecek return (error);
462 1.1 jdolecek }
463 1.1 jdolecek fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
464 1.1 jdolecek eofentry);
465 1.1 jdolecek }
466 1.1 jdolecek
467 1.1 jdolecek /*
468 1.1 jdolecek * Now free the clusters removed from the file because of the
469 1.1 jdolecek * truncation.
470 1.1 jdolecek */
471 1.1 jdolecek if (chaintofree != 0 && !MSDOSFSEOF(chaintofree, pmp->pm_fatmask))
472 1.1 jdolecek freeclusterchain(pmp, chaintofree);
473 1.1 jdolecek
474 1.1 jdolecek return (allerror);
475 1.1 jdolecek }
476 1.1 jdolecek
477 1.1 jdolecek /*
478 1.1 jdolecek * Extend the file described by dep to length specified by length.
479 1.1 jdolecek */
480 1.1 jdolecek int
481 1.34 dsl deextend(struct denode *dep, u_long length, kauth_cred_t cred)
482 1.1 jdolecek {
483 1.1 jdolecek struct msdosfsmount *pmp = dep->de_pmp;
484 1.1 jdolecek u_long count, osize;
485 1.1 jdolecek int error;
486 1.1 jdolecek
487 1.1 jdolecek /*
488 1.1 jdolecek * The root of a DOS filesystem cannot be extended.
489 1.1 jdolecek */
490 1.26 ad if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp))
491 1.1 jdolecek return (EINVAL);
492 1.1 jdolecek
493 1.1 jdolecek /*
494 1.1 jdolecek * Directories cannot be extended.
495 1.1 jdolecek */
496 1.1 jdolecek if (dep->de_Attributes & ATTR_DIRECTORY)
497 1.1 jdolecek return (EISDIR);
498 1.1 jdolecek
499 1.1 jdolecek if (length <= dep->de_FileSize)
500 1.1 jdolecek panic("deextend: file too large");
501 1.1 jdolecek
502 1.1 jdolecek /*
503 1.1 jdolecek * Compute the number of clusters to allocate.
504 1.1 jdolecek */
505 1.1 jdolecek count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
506 1.1 jdolecek if (count > 0) {
507 1.1 jdolecek if (count > pmp->pm_freeclustercount)
508 1.1 jdolecek return (ENOSPC);
509 1.1 jdolecek error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
510 1.1 jdolecek if (error) {
511 1.1 jdolecek /* truncate the added clusters away again */
512 1.27 pooka (void) detrunc(dep, dep->de_FileSize, 0, cred);
513 1.1 jdolecek return (error);
514 1.1 jdolecek }
515 1.1 jdolecek }
516 1.1 jdolecek
517 1.29 reinoud /*
518 1.46 hannken * Zero extend file range; ubc_zerorange() uses ubc_alloc() and a
519 1.29 reinoud * memset(); we set the write size so ubc won't read in file data that
520 1.29 reinoud * is zero'd later.
521 1.29 reinoud */
522 1.1 jdolecek osize = dep->de_FileSize;
523 1.1 jdolecek dep->de_FileSize = length;
524 1.29 reinoud uvm_vnp_setwritesize(DETOV(dep), (voff_t)dep->de_FileSize);
525 1.1 jdolecek dep->de_flag |= DE_UPDATE|DE_MODIFIED;
526 1.46 hannken ubc_zerorange(&DETOV(dep)->v_uobj, (off_t)osize,
527 1.46 hannken (size_t)(round_page(dep->de_FileSize) - osize),
528 1.46 hannken UBC_UNMAP_FLAG(DETOV(dep)));
529 1.29 reinoud uvm_vnp_setsize(DETOV(dep), (voff_t)dep->de_FileSize);
530 1.1 jdolecek return (deupdat(dep, 1));
531 1.1 jdolecek }
532 1.1 jdolecek
533 1.1 jdolecek int
534 1.34 dsl msdosfs_reclaim(void *v)
535 1.1 jdolecek {
536 1.1 jdolecek struct vop_reclaim_args /* {
537 1.1 jdolecek struct vnode *a_vp;
538 1.1 jdolecek } */ *ap = v;
539 1.1 jdolecek struct vnode *vp = ap->a_vp;
540 1.48.10.1 tls struct mount *mp = vp->v_mount;
541 1.1 jdolecek struct denode *dep = VTODE(vp);
542 1.1 jdolecek
543 1.48.10.1 tls fstrans_start(mp, FSTRANS_LAZY);
544 1.1 jdolecek #ifdef MSDOSFS_DEBUG
545 1.1 jdolecek printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n",
546 1.1 jdolecek dep, dep->de_Name, dep->de_refcnt);
547 1.1 jdolecek #endif
548 1.1 jdolecek
549 1.31 ad if (prtactive && vp->v_usecount > 1)
550 1.1 jdolecek vprint("msdosfs_reclaim(): pushing active", vp);
551 1.1 jdolecek /*
552 1.48.10.1 tls * Remove the denode from the vnode cache.
553 1.1 jdolecek */
554 1.48.10.1 tls vcache_remove(vp->v_mount, &dep->de_key, sizeof(dep->de_key));
555 1.1 jdolecek /*
556 1.1 jdolecek * Purge old data structures associated with the denode.
557 1.1 jdolecek */
558 1.1 jdolecek if (dep->de_devvp) {
559 1.1 jdolecek vrele(dep->de_devvp);
560 1.1 jdolecek dep->de_devvp = 0;
561 1.1 jdolecek }
562 1.1 jdolecek #if 0 /* XXX */
563 1.1 jdolecek dep->de_flag = 0;
564 1.1 jdolecek #endif
565 1.48.10.1 tls /*
566 1.48.10.1 tls * To interlock with msdosfs_sync().
567 1.48.10.1 tls */
568 1.18 ad genfs_node_destroy(vp);
569 1.48.10.1 tls mutex_enter(vp->v_interlock);
570 1.1 jdolecek vp->v_data = NULL;
571 1.48.10.1 tls mutex_exit(vp->v_interlock);
572 1.48.10.1 tls pool_put(&msdosfs_denode_pool, dep);
573 1.48.10.1 tls fstrans_done(mp);
574 1.1 jdolecek return (0);
575 1.1 jdolecek }
576 1.1 jdolecek
577 1.1 jdolecek int
578 1.34 dsl msdosfs_inactive(void *v)
579 1.1 jdolecek {
580 1.1 jdolecek struct vop_inactive_args /* {
581 1.1 jdolecek struct vnode *a_vp;
582 1.30 ad bool *a_recycle;
583 1.1 jdolecek } */ *ap = v;
584 1.1 jdolecek struct vnode *vp = ap->a_vp;
585 1.38 hannken struct mount *mp = vp->v_mount;
586 1.1 jdolecek struct denode *dep = VTODE(vp);
587 1.1 jdolecek int error = 0;
588 1.1 jdolecek
589 1.1 jdolecek #ifdef MSDOSFS_DEBUG
590 1.1 jdolecek printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]);
591 1.1 jdolecek #endif
592 1.1 jdolecek
593 1.38 hannken fstrans_start(mp, FSTRANS_LAZY);
594 1.1 jdolecek /*
595 1.1 jdolecek * Get rid of denodes related to stale file handles.
596 1.1 jdolecek */
597 1.1 jdolecek if (dep->de_Name[0] == SLOT_DELETED)
598 1.1 jdolecek goto out;
599 1.1 jdolecek
600 1.1 jdolecek /*
601 1.1 jdolecek * If the file has been deleted and it is on a read/write
602 1.1 jdolecek * filesystem, then truncate the file, and mark the directory slot
603 1.1 jdolecek * as empty. (This may not be necessary for the dos filesystem.)
604 1.1 jdolecek */
605 1.1 jdolecek #ifdef MSDOSFS_DEBUG
606 1.1 jdolecek printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x %s\n",
607 1.1 jdolecek dep, dep->de_refcnt, vp->v_mount->mnt_flag,
608 1.1 jdolecek (vp->v_mount->mnt_flag & MNT_RDONLY) ? "MNT_RDONLY" : "");
609 1.1 jdolecek #endif
610 1.1 jdolecek if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
611 1.1 jdolecek if (dep->de_FileSize != 0) {
612 1.27 pooka error = detrunc(dep, (u_long)0, 0, NOCRED);
613 1.1 jdolecek }
614 1.1 jdolecek dep->de_Name[0] = SLOT_DELETED;
615 1.43 hannken msdosfs_fh_remove(dep->de_pmp,
616 1.43 hannken dep->de_dirclust, dep->de_diroffset);
617 1.1 jdolecek }
618 1.1 jdolecek deupdat(dep, 0);
619 1.1 jdolecek out:
620 1.1 jdolecek /*
621 1.1 jdolecek * If we are done with the denode, reclaim it
622 1.1 jdolecek * so that it can be reused immediately.
623 1.1 jdolecek */
624 1.1 jdolecek #ifdef MSDOSFS_DEBUG
625 1.1 jdolecek printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n",
626 1.1 jdolecek vp->v_usecount, dep->de_Name[0]);
627 1.1 jdolecek #endif
628 1.30 ad *ap->a_recycle = (dep->de_Name[0] == SLOT_DELETED);
629 1.39 hannken VOP_UNLOCK(vp);
630 1.38 hannken fstrans_done(mp);
631 1.1 jdolecek return (error);
632 1.1 jdolecek }
633 1.1 jdolecek
634 1.1 jdolecek int
635 1.16 christos msdosfs_gop_alloc(struct vnode *vp, off_t off,
636 1.16 christos off_t len, int flags, kauth_cred_t cred)
637 1.1 jdolecek {
638 1.1 jdolecek return 0;
639 1.1 jdolecek }
640 1.9 yamt
641 1.9 yamt void
642 1.9 yamt msdosfs_gop_markupdate(struct vnode *vp, int flags)
643 1.9 yamt {
644 1.9 yamt u_long mask = 0;
645 1.9 yamt
646 1.9 yamt if ((flags & GOP_UPDATE_ACCESSED) != 0) {
647 1.9 yamt mask = DE_ACCESS;
648 1.9 yamt }
649 1.9 yamt if ((flags & GOP_UPDATE_MODIFIED) != 0) {
650 1.9 yamt mask |= DE_UPDATE;
651 1.9 yamt }
652 1.9 yamt if (mask) {
653 1.9 yamt struct denode *dep = VTODE(vp);
654 1.9 yamt
655 1.9 yamt dep->de_flag |= mask;
656 1.9 yamt }
657 1.9 yamt }
658 1.43 hannken
659 1.43 hannken int
660 1.43 hannken msdosfs_fh_enter(struct msdosfsmount *pmp,
661 1.43 hannken uint32_t dircluster, uint32_t diroffset, uint32_t *genp)
662 1.43 hannken {
663 1.43 hannken struct fh_key fhkey;
664 1.43 hannken struct fh_node *fhp;
665 1.43 hannken
666 1.43 hannken fhkey.fhk_mount = pmp;
667 1.43 hannken fhkey.fhk_dircluster = dircluster;
668 1.43 hannken fhkey.fhk_diroffset = diroffset;
669 1.43 hannken
670 1.43 hannken mutex_enter(&fh_lock);
671 1.43 hannken fhp = rb_tree_find_node(&fh_rbtree, &fhkey);
672 1.43 hannken if (fhp == NULL) {
673 1.43 hannken mutex_exit(&fh_lock);
674 1.43 hannken fhp = pool_get(&fh_pool, PR_WAITOK);
675 1.43 hannken mutex_enter(&fh_lock);
676 1.43 hannken fhp->fh_key = fhkey;
677 1.43 hannken fhp->fh_gen = fh_generation++;
678 1.43 hannken rb_tree_insert_node(&fh_rbtree, fhp);
679 1.43 hannken }
680 1.43 hannken *genp = fhp->fh_gen;
681 1.43 hannken mutex_exit(&fh_lock);
682 1.43 hannken return 0;
683 1.43 hannken }
684 1.43 hannken
685 1.43 hannken int
686 1.43 hannken msdosfs_fh_remove(struct msdosfsmount *pmp,
687 1.43 hannken uint32_t dircluster, uint32_t diroffset)
688 1.43 hannken {
689 1.43 hannken struct fh_key fhkey;
690 1.43 hannken struct fh_node *fhp;
691 1.43 hannken
692 1.43 hannken fhkey.fhk_mount = pmp;
693 1.43 hannken fhkey.fhk_dircluster = dircluster;
694 1.43 hannken fhkey.fhk_diroffset = diroffset;
695 1.43 hannken
696 1.43 hannken mutex_enter(&fh_lock);
697 1.43 hannken fhp = rb_tree_find_node(&fh_rbtree, &fhkey);
698 1.43 hannken if (fhp == NULL) {
699 1.43 hannken mutex_exit(&fh_lock);
700 1.43 hannken return ENOENT;
701 1.43 hannken }
702 1.43 hannken rb_tree_remove_node(&fh_rbtree, fhp);
703 1.43 hannken mutex_exit(&fh_lock);
704 1.43 hannken pool_put(&fh_pool, fhp);
705 1.43 hannken return 0;
706 1.43 hannken }
707 1.43 hannken
708 1.43 hannken int
709 1.43 hannken msdosfs_fh_lookup(struct msdosfsmount *pmp,
710 1.43 hannken uint32_t dircluster, uint32_t diroffset, uint32_t *genp)
711 1.43 hannken {
712 1.43 hannken struct fh_key fhkey;
713 1.43 hannken struct fh_node *fhp;
714 1.43 hannken
715 1.43 hannken fhkey.fhk_mount = pmp;
716 1.43 hannken fhkey.fhk_dircluster = dircluster;
717 1.43 hannken fhkey.fhk_diroffset = diroffset;
718 1.43 hannken
719 1.43 hannken mutex_enter(&fh_lock);
720 1.43 hannken fhp = rb_tree_find_node(&fh_rbtree, &fhkey);
721 1.43 hannken if (fhp == NULL) {
722 1.43 hannken mutex_exit(&fh_lock);
723 1.43 hannken return ESTALE;
724 1.43 hannken }
725 1.43 hannken *genp = fhp->fh_gen;
726 1.43 hannken mutex_exit(&fh_lock);
727 1.43 hannken return 0;
728 1.43 hannken }
729 1.43 hannken
730 1.43 hannken void
731 1.43 hannken msdosfs_fh_destroy(struct msdosfsmount *pmp)
732 1.43 hannken {
733 1.43 hannken struct fh_key fhkey;
734 1.43 hannken struct fh_node *fhp, *nfhp;
735 1.43 hannken
736 1.43 hannken fhkey.fhk_mount = pmp;
737 1.43 hannken fhkey.fhk_dircluster = 0;
738 1.43 hannken fhkey.fhk_diroffset = 0;
739 1.43 hannken
740 1.43 hannken mutex_enter(&fh_lock);
741 1.43 hannken for (fhp = rb_tree_find_node_geq(&fh_rbtree, &fhkey);
742 1.43 hannken fhp != NULL && fhp->fh_mount == pmp; fhp = nfhp) {
743 1.43 hannken nfhp = rb_tree_iterate(&fh_rbtree, fhp, RB_DIR_RIGHT);
744 1.43 hannken rb_tree_remove_node(&fh_rbtree, fhp);
745 1.43 hannken pool_put(&fh_pool, fhp);
746 1.43 hannken }
747 1.43 hannken #ifdef DIAGNOSTIC
748 1.43 hannken RB_TREE_FOREACH(fhp, &fh_rbtree) {
749 1.43 hannken KASSERT(fhp->fh_mount != pmp);
750 1.43 hannken }
751 1.43 hannken #endif
752 1.43 hannken mutex_exit(&fh_lock);
753 1.43 hannken }
754