msdosfs_denode.c revision 1.2 1 1.2 darrenr /* $NetBSD: msdosfs_denode.c,v 1.2 2003/06/28 14:21:49 darrenr 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.2 darrenr __KERNEL_RCSID(0, "$NetBSD: msdosfs_denode.c,v 1.2 2003/06/28 14:21:49 darrenr 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.1 jdolecek #include <sys/malloc.h>
57 1.1 jdolecek #include <sys/pool.h>
58 1.1 jdolecek #include <sys/proc.h>
59 1.1 jdolecek #include <sys/buf.h>
60 1.1 jdolecek #include <sys/vnode.h>
61 1.1 jdolecek #include <sys/kernel.h> /* defines "time" */
62 1.1 jdolecek #include <sys/dirent.h>
63 1.1 jdolecek #include <sys/namei.h>
64 1.1 jdolecek
65 1.1 jdolecek #include <uvm/uvm_extern.h>
66 1.1 jdolecek
67 1.1 jdolecek #include <fs/msdosfs/bpb.h>
68 1.1 jdolecek #include <fs/msdosfs/msdosfsmount.h>
69 1.1 jdolecek #include <fs/msdosfs/direntry.h>
70 1.1 jdolecek #include <fs/msdosfs/denode.h>
71 1.1 jdolecek #include <fs/msdosfs/fat.h>
72 1.1 jdolecek
73 1.1 jdolecek LIST_HEAD(ihashhead, denode) *dehashtbl;
74 1.1 jdolecek u_long dehash; /* size of hash table - 1 */
75 1.1 jdolecek #define DEHASH(dev, dcl, doff) \
76 1.1 jdolecek (((dev) + (dcl) + (doff) / sizeof(struct direntry)) & dehash)
77 1.1 jdolecek
78 1.1 jdolecek struct simplelock msdosfs_ihash_slock;
79 1.1 jdolecek
80 1.1 jdolecek struct pool msdosfs_denode_pool;
81 1.1 jdolecek
82 1.1 jdolecek extern int prtactive;
83 1.1 jdolecek
84 1.1 jdolecek struct genfs_ops msdosfs_genfsops = {
85 1.1 jdolecek genfs_size,
86 1.1 jdolecek msdosfs_gop_alloc,
87 1.1 jdolecek genfs_gop_write,
88 1.1 jdolecek };
89 1.1 jdolecek
90 1.2 darrenr static struct denode *msdosfs_hashget __P((dev_t, u_long, u_long, struct lwp *));
91 1.1 jdolecek static void msdosfs_hashins __P((struct denode *));
92 1.1 jdolecek static void msdosfs_hashrem __P((struct denode *));
93 1.1 jdolecek
94 1.1 jdolecek void
95 1.1 jdolecek msdosfs_init()
96 1.1 jdolecek {
97 1.1 jdolecek dehashtbl = hashinit(desiredvnodes / 2, HASH_LIST, M_MSDOSFSMNT,
98 1.1 jdolecek M_WAITOK, &dehash);
99 1.1 jdolecek simple_lock_init(&msdosfs_ihash_slock);
100 1.1 jdolecek pool_init(&msdosfs_denode_pool, sizeof(struct denode), 0, 0, 0,
101 1.1 jdolecek "msdosnopl", &pool_allocator_nointr);
102 1.1 jdolecek }
103 1.1 jdolecek
104 1.1 jdolecek /*
105 1.1 jdolecek * Reinitialize inode hash table.
106 1.1 jdolecek */
107 1.1 jdolecek
108 1.1 jdolecek void
109 1.1 jdolecek msdosfs_reinit()
110 1.1 jdolecek {
111 1.1 jdolecek struct denode *dep;
112 1.1 jdolecek struct ihashhead *oldhash, *hash;
113 1.1 jdolecek u_long oldmask, mask, val;
114 1.1 jdolecek int i;
115 1.1 jdolecek
116 1.1 jdolecek hash = hashinit(desiredvnodes / 2, HASH_LIST, M_MSDOSFSMNT, M_WAITOK,
117 1.1 jdolecek &mask);
118 1.1 jdolecek
119 1.1 jdolecek simple_lock(&msdosfs_ihash_slock);
120 1.1 jdolecek oldhash = dehashtbl;
121 1.1 jdolecek oldmask = dehash;
122 1.1 jdolecek dehashtbl = hash;
123 1.1 jdolecek dehash = mask;
124 1.1 jdolecek for (i = 0; i <= oldmask; i++) {
125 1.1 jdolecek while ((dep = LIST_FIRST(&oldhash[i])) != NULL) {
126 1.1 jdolecek LIST_REMOVE(dep, de_hash);
127 1.1 jdolecek val = DEHASH(dep->de_dev, dep->de_dirclust,
128 1.1 jdolecek dep->de_diroffset);
129 1.1 jdolecek LIST_INSERT_HEAD(&hash[val], dep, de_hash);
130 1.1 jdolecek }
131 1.1 jdolecek }
132 1.1 jdolecek simple_unlock(&msdosfs_ihash_slock);
133 1.1 jdolecek hashdone(oldhash, M_MSDOSFSMNT);
134 1.1 jdolecek }
135 1.1 jdolecek
136 1.1 jdolecek void
137 1.1 jdolecek msdosfs_done()
138 1.1 jdolecek {
139 1.1 jdolecek hashdone(dehashtbl, M_MSDOSFSMNT);
140 1.1 jdolecek pool_destroy(&msdosfs_denode_pool);
141 1.1 jdolecek }
142 1.1 jdolecek
143 1.1 jdolecek static struct denode *
144 1.2 darrenr msdosfs_hashget(dev, dirclust, diroff, l)
145 1.1 jdolecek dev_t dev;
146 1.1 jdolecek u_long dirclust;
147 1.1 jdolecek u_long diroff;
148 1.2 darrenr struct lwp *l;
149 1.1 jdolecek {
150 1.1 jdolecek struct denode *dep;
151 1.1 jdolecek struct vnode *vp;
152 1.1 jdolecek
153 1.1 jdolecek loop:
154 1.1 jdolecek simple_lock(&msdosfs_ihash_slock);
155 1.1 jdolecek LIST_FOREACH(dep, &dehashtbl[DEHASH(dev, dirclust, diroff)], de_hash) {
156 1.1 jdolecek if (dirclust == dep->de_dirclust &&
157 1.1 jdolecek diroff == dep->de_diroffset &&
158 1.1 jdolecek dev == dep->de_dev &&
159 1.1 jdolecek dep->de_refcnt != 0) {
160 1.1 jdolecek vp = DETOV(dep);
161 1.1 jdolecek simple_lock(&vp->v_interlock);
162 1.1 jdolecek simple_unlock(&msdosfs_ihash_slock);
163 1.2 darrenr if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, l))
164 1.1 jdolecek goto loop;
165 1.1 jdolecek return (dep);
166 1.1 jdolecek }
167 1.1 jdolecek }
168 1.1 jdolecek simple_unlock(&msdosfs_ihash_slock);
169 1.1 jdolecek return (NULL);
170 1.1 jdolecek }
171 1.1 jdolecek
172 1.1 jdolecek static void
173 1.1 jdolecek msdosfs_hashins(dep)
174 1.1 jdolecek struct denode *dep;
175 1.1 jdolecek {
176 1.1 jdolecek struct ihashhead *depp;
177 1.1 jdolecek int val;
178 1.1 jdolecek
179 1.1 jdolecek simple_lock(&msdosfs_ihash_slock);
180 1.1 jdolecek val = DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset);
181 1.1 jdolecek depp = &dehashtbl[val];
182 1.1 jdolecek LIST_INSERT_HEAD(depp, dep, de_hash);
183 1.1 jdolecek simple_unlock(&msdosfs_ihash_slock);
184 1.1 jdolecek }
185 1.1 jdolecek
186 1.1 jdolecek static void
187 1.1 jdolecek msdosfs_hashrem(dep)
188 1.1 jdolecek struct denode *dep;
189 1.1 jdolecek {
190 1.1 jdolecek simple_lock(&msdosfs_ihash_slock);
191 1.1 jdolecek LIST_REMOVE(dep, de_hash);
192 1.1 jdolecek simple_unlock(&msdosfs_ihash_slock);
193 1.1 jdolecek }
194 1.1 jdolecek
195 1.1 jdolecek /*
196 1.1 jdolecek * If deget() succeeds it returns with the gotten denode locked().
197 1.1 jdolecek *
198 1.1 jdolecek * pmp - address of msdosfsmount structure of the filesystem containing
199 1.1 jdolecek * the denode of interest. The pm_dev field and the address of
200 1.1 jdolecek * the msdosfsmount structure are used.
201 1.1 jdolecek * dirclust - which cluster bp contains, if dirclust is 0 (root directory)
202 1.1 jdolecek * diroffset is relative to the beginning of the root directory,
203 1.1 jdolecek * otherwise it is cluster relative.
204 1.1 jdolecek * diroffset - offset past begin of cluster of denode we want
205 1.1 jdolecek * depp - returns the address of the gotten denode.
206 1.1 jdolecek */
207 1.1 jdolecek int
208 1.1 jdolecek deget(pmp, dirclust, diroffset, depp)
209 1.1 jdolecek struct msdosfsmount *pmp; /* so we know the maj/min number */
210 1.1 jdolecek u_long dirclust; /* cluster this dir entry came from */
211 1.1 jdolecek u_long diroffset; /* index of entry within the cluster */
212 1.1 jdolecek struct denode **depp; /* returns the addr of the gotten denode */
213 1.1 jdolecek {
214 1.1 jdolecek int error;
215 1.1 jdolecek extern int (**msdosfs_vnodeop_p) __P((void *));
216 1.1 jdolecek struct direntry *direntptr;
217 1.1 jdolecek struct denode *ldep;
218 1.1 jdolecek struct vnode *nvp;
219 1.1 jdolecek struct buf *bp;
220 1.1 jdolecek
221 1.1 jdolecek #ifdef MSDOSFS_DEBUG
222 1.1 jdolecek printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n",
223 1.1 jdolecek pmp, dirclust, diroffset, depp);
224 1.1 jdolecek #endif
225 1.1 jdolecek
226 1.1 jdolecek /*
227 1.1 jdolecek * On FAT32 filesystems, root is a (more or less) normal
228 1.1 jdolecek * directory
229 1.1 jdolecek */
230 1.1 jdolecek if (FAT32(pmp) && dirclust == MSDOSFSROOT)
231 1.1 jdolecek dirclust = pmp->pm_rootdirblk;
232 1.1 jdolecek
233 1.1 jdolecek /*
234 1.1 jdolecek * See if the denode is in the denode cache. Use the location of
235 1.1 jdolecek * the directory entry to compute the hash value. For subdir use
236 1.1 jdolecek * address of "." entry. For root dir (if not FAT32) use cluster
237 1.1 jdolecek * MSDOSFSROOT, offset MSDOSFSROOT_OFS
238 1.1 jdolecek *
239 1.1 jdolecek * NOTE: The check for de_refcnt > 0 below insures the denode being
240 1.1 jdolecek * examined does not represent an unlinked but still open file.
241 1.1 jdolecek * These files are not to be accessible even when the directory
242 1.1 jdolecek * entry that represented the file happens to be reused while the
243 1.1 jdolecek * deleted file is still open.
244 1.1 jdolecek */
245 1.2 darrenr ldep = msdosfs_hashget(pmp->pm_dev, dirclust, diroffset, curlwp);
246 1.1 jdolecek if (ldep) {
247 1.1 jdolecek *depp = ldep;
248 1.1 jdolecek return (0);
249 1.1 jdolecek }
250 1.1 jdolecek
251 1.1 jdolecek /*
252 1.1 jdolecek * Directory entry was not in cache, have to create a vnode and
253 1.1 jdolecek * copy it from the passed disk buffer.
254 1.1 jdolecek */
255 1.1 jdolecek /* getnewvnode() does a VREF() on the vnode */
256 1.1 jdolecek error = getnewvnode(VT_MSDOSFS, pmp->pm_mountp,
257 1.1 jdolecek msdosfs_vnodeop_p, &nvp);
258 1.1 jdolecek if (error) {
259 1.1 jdolecek *depp = 0;
260 1.1 jdolecek return (error);
261 1.1 jdolecek }
262 1.1 jdolecek ldep = pool_get(&msdosfs_denode_pool, PR_WAITOK);
263 1.1 jdolecek memset(ldep, 0, sizeof *ldep);
264 1.1 jdolecek nvp->v_data = ldep;
265 1.1 jdolecek ldep->de_vnode = nvp;
266 1.1 jdolecek ldep->de_flag = 0;
267 1.1 jdolecek ldep->de_devvp = 0;
268 1.1 jdolecek ldep->de_lockf = 0;
269 1.1 jdolecek ldep->de_dev = pmp->pm_dev;
270 1.1 jdolecek ldep->de_dirclust = dirclust;
271 1.1 jdolecek ldep->de_diroffset = diroffset;
272 1.1 jdolecek fc_purge(ldep, 0); /* init the fat cache for this denode */
273 1.1 jdolecek
274 1.1 jdolecek /*
275 1.1 jdolecek * Insert the denode into the hash queue and lock the denode so it
276 1.1 jdolecek * can't be accessed until we've read it in and have done what we
277 1.1 jdolecek * need to it.
278 1.1 jdolecek */
279 1.1 jdolecek vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY);
280 1.1 jdolecek msdosfs_hashins(ldep);
281 1.1 jdolecek
282 1.1 jdolecek ldep->de_pmp = pmp;
283 1.1 jdolecek ldep->de_devvp = pmp->pm_devvp;
284 1.1 jdolecek ldep->de_refcnt = 1;
285 1.1 jdolecek /*
286 1.1 jdolecek * Copy the directory entry into the denode area of the vnode.
287 1.1 jdolecek */
288 1.1 jdolecek if ((dirclust == MSDOSFSROOT
289 1.1 jdolecek || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk))
290 1.1 jdolecek && diroffset == MSDOSFSROOT_OFS) {
291 1.1 jdolecek /*
292 1.1 jdolecek * Directory entry for the root directory. There isn't one,
293 1.1 jdolecek * so we manufacture one. We should probably rummage
294 1.1 jdolecek * through the root directory and find a label entry (if it
295 1.1 jdolecek * exists), and then use the time and date from that entry
296 1.1 jdolecek * as the time and date for the root denode.
297 1.1 jdolecek */
298 1.1 jdolecek nvp->v_flag |= VROOT; /* should be further down XXX */
299 1.1 jdolecek
300 1.1 jdolecek ldep->de_Attributes = ATTR_DIRECTORY;
301 1.1 jdolecek if (FAT32(pmp))
302 1.1 jdolecek ldep->de_StartCluster = pmp->pm_rootdirblk;
303 1.1 jdolecek /* de_FileSize will be filled in further down */
304 1.1 jdolecek else {
305 1.1 jdolecek ldep->de_StartCluster = MSDOSFSROOT;
306 1.1 jdolecek ldep->de_FileSize = pmp->pm_rootdirsize * pmp->pm_BytesPerSec;
307 1.1 jdolecek }
308 1.1 jdolecek /*
309 1.1 jdolecek * fill in time and date so that dos2unixtime() doesn't
310 1.1 jdolecek * spit up when called from msdosfs_getattr() with root
311 1.1 jdolecek * denode
312 1.1 jdolecek */
313 1.1 jdolecek ldep->de_CHun = 0;
314 1.1 jdolecek ldep->de_CTime = 0x0000; /* 00:00:00 */
315 1.1 jdolecek ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
316 1.1 jdolecek | (1 << DD_DAY_SHIFT);
317 1.1 jdolecek /* Jan 1, 1980 */
318 1.1 jdolecek ldep->de_ADate = ldep->de_CDate;
319 1.1 jdolecek ldep->de_MTime = ldep->de_CTime;
320 1.1 jdolecek ldep->de_MDate = ldep->de_CDate;
321 1.1 jdolecek /* leave the other fields as garbage */
322 1.1 jdolecek } else {
323 1.1 jdolecek error = readep(pmp, dirclust, diroffset, &bp, &direntptr);
324 1.1 jdolecek if (error)
325 1.1 jdolecek return (error);
326 1.1 jdolecek DE_INTERNALIZE(ldep, direntptr);
327 1.1 jdolecek brelse(bp);
328 1.1 jdolecek }
329 1.1 jdolecek
330 1.1 jdolecek /*
331 1.1 jdolecek * Fill in a few fields of the vnode and finish filling in the
332 1.1 jdolecek * denode. Then return the address of the found denode.
333 1.1 jdolecek */
334 1.1 jdolecek if (ldep->de_Attributes & ATTR_DIRECTORY) {
335 1.1 jdolecek /*
336 1.1 jdolecek * Since DOS directory entries that describe directories
337 1.1 jdolecek * have 0 in the filesize field, we take this opportunity
338 1.1 jdolecek * to find out the length of the directory and plug it into
339 1.1 jdolecek * the denode structure.
340 1.1 jdolecek */
341 1.1 jdolecek u_long size;
342 1.1 jdolecek
343 1.1 jdolecek nvp->v_type = VDIR;
344 1.1 jdolecek if (ldep->de_StartCluster != MSDOSFSROOT) {
345 1.1 jdolecek error = pcbmap(ldep, CLUST_END, 0, &size, 0);
346 1.1 jdolecek if (error == E2BIG) {
347 1.1 jdolecek ldep->de_FileSize = de_cn2off(pmp, size);
348 1.1 jdolecek error = 0;
349 1.1 jdolecek } else
350 1.1 jdolecek printf("deget(): pcbmap returned %d\n", error);
351 1.1 jdolecek }
352 1.1 jdolecek } else
353 1.1 jdolecek nvp->v_type = VREG;
354 1.1 jdolecek genfs_node_init(nvp, &msdosfs_genfsops);
355 1.1 jdolecek VREF(ldep->de_devvp);
356 1.1 jdolecek *depp = ldep;
357 1.1 jdolecek nvp->v_size = ldep->de_FileSize;
358 1.1 jdolecek return (0);
359 1.1 jdolecek }
360 1.1 jdolecek
361 1.1 jdolecek int
362 1.1 jdolecek deupdat(dep, waitfor)
363 1.1 jdolecek struct denode *dep;
364 1.1 jdolecek int waitfor;
365 1.1 jdolecek {
366 1.1 jdolecek
367 1.1 jdolecek return (VOP_UPDATE(DETOV(dep), NULL, NULL, waitfor ? UPDATE_WAIT : 0));
368 1.1 jdolecek }
369 1.1 jdolecek
370 1.1 jdolecek /*
371 1.1 jdolecek * Truncate the file described by dep to the length specified by length.
372 1.1 jdolecek */
373 1.1 jdolecek int
374 1.2 darrenr detrunc(dep, length, flags, cred, l)
375 1.1 jdolecek struct denode *dep;
376 1.1 jdolecek u_long length;
377 1.1 jdolecek int flags;
378 1.1 jdolecek struct ucred *cred;
379 1.2 darrenr struct lwp *l;
380 1.1 jdolecek {
381 1.1 jdolecek int error;
382 1.1 jdolecek int allerror;
383 1.1 jdolecek u_long eofentry;
384 1.1 jdolecek u_long chaintofree;
385 1.1 jdolecek daddr_t bn, lastblock;
386 1.1 jdolecek int boff;
387 1.1 jdolecek int isadir = dep->de_Attributes & ATTR_DIRECTORY;
388 1.1 jdolecek struct buf *bp;
389 1.1 jdolecek struct msdosfsmount *pmp = dep->de_pmp;
390 1.1 jdolecek
391 1.1 jdolecek #ifdef MSDOSFS_DEBUG
392 1.1 jdolecek printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags);
393 1.1 jdolecek #endif
394 1.1 jdolecek
395 1.1 jdolecek /*
396 1.1 jdolecek * Disallow attempts to truncate the root directory since it is of
397 1.1 jdolecek * fixed size. That's just the way dos filesystems are. We use
398 1.1 jdolecek * the VROOT bit in the vnode because checking for the directory
399 1.1 jdolecek * bit and a startcluster of 0 in the denode is not adequate to
400 1.1 jdolecek * recognize the root directory at this point in a file or
401 1.1 jdolecek * directory's life.
402 1.1 jdolecek */
403 1.1 jdolecek if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) {
404 1.1 jdolecek printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
405 1.1 jdolecek dep->de_dirclust, dep->de_diroffset);
406 1.1 jdolecek return (EINVAL);
407 1.1 jdolecek }
408 1.1 jdolecek
409 1.1 jdolecek uvm_vnp_setsize(DETOV(dep), length);
410 1.1 jdolecek
411 1.1 jdolecek if (dep->de_FileSize < length)
412 1.1 jdolecek return (deextend(dep, length, cred));
413 1.1 jdolecek lastblock = de_clcount(pmp, length) - 1;
414 1.1 jdolecek
415 1.1 jdolecek /*
416 1.1 jdolecek * If the desired length is 0 then remember the starting cluster of
417 1.1 jdolecek * the file and set the StartCluster field in the directory entry
418 1.1 jdolecek * to 0. If the desired length is not zero, then get the number of
419 1.1 jdolecek * the last cluster in the shortened file. Then get the number of
420 1.1 jdolecek * the first cluster in the part of the file that is to be freed.
421 1.1 jdolecek * Then set the next cluster pointer in the last cluster of the
422 1.1 jdolecek * file to CLUST_EOFE.
423 1.1 jdolecek */
424 1.1 jdolecek if (length == 0) {
425 1.1 jdolecek chaintofree = dep->de_StartCluster;
426 1.1 jdolecek dep->de_StartCluster = 0;
427 1.1 jdolecek eofentry = ~0;
428 1.1 jdolecek } else {
429 1.1 jdolecek error = pcbmap(dep, lastblock, 0, &eofentry, 0);
430 1.1 jdolecek if (error) {
431 1.1 jdolecek #ifdef MSDOSFS_DEBUG
432 1.1 jdolecek printf("detrunc(): pcbmap fails %d\n", error);
433 1.1 jdolecek #endif
434 1.1 jdolecek return (error);
435 1.1 jdolecek }
436 1.1 jdolecek }
437 1.1 jdolecek
438 1.1 jdolecek fc_purge(dep, lastblock + 1);
439 1.1 jdolecek
440 1.1 jdolecek /*
441 1.1 jdolecek * If the new length is not a multiple of the cluster size then we
442 1.1 jdolecek * must zero the tail end of the new last cluster in case it
443 1.1 jdolecek * becomes part of the file again because of a seek.
444 1.1 jdolecek */
445 1.1 jdolecek if ((boff = length & pmp->pm_crbomask) != 0) {
446 1.1 jdolecek if (isadir) {
447 1.1 jdolecek bn = cntobn(pmp, eofentry);
448 1.1 jdolecek error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster,
449 1.1 jdolecek NOCRED, &bp);
450 1.1 jdolecek if (error) {
451 1.1 jdolecek brelse(bp);
452 1.1 jdolecek #ifdef MSDOSFS_DEBUG
453 1.1 jdolecek printf("detrunc(): bread fails %d\n", error);
454 1.1 jdolecek #endif
455 1.1 jdolecek return (error);
456 1.1 jdolecek }
457 1.1 jdolecek memset(bp->b_data + boff, 0, pmp->pm_bpcluster - boff);
458 1.1 jdolecek if (flags & IO_SYNC)
459 1.1 jdolecek bwrite(bp);
460 1.1 jdolecek else
461 1.1 jdolecek bdwrite(bp);
462 1.1 jdolecek } else {
463 1.1 jdolecek uvm_vnp_zerorange(DETOV(dep), length,
464 1.1 jdolecek pmp->pm_bpcluster - boff);
465 1.1 jdolecek }
466 1.1 jdolecek }
467 1.1 jdolecek
468 1.1 jdolecek /*
469 1.1 jdolecek * Write out the updated directory entry. Even if the update fails
470 1.1 jdolecek * we free the trailing clusters.
471 1.1 jdolecek */
472 1.1 jdolecek dep->de_FileSize = length;
473 1.1 jdolecek if (!isadir)
474 1.1 jdolecek dep->de_flag |= DE_UPDATE|DE_MODIFIED;
475 1.1 jdolecek vtruncbuf(DETOV(dep), lastblock + 1, 0, 0);
476 1.1 jdolecek allerror = deupdat(dep, 1);
477 1.1 jdolecek #ifdef MSDOSFS_DEBUG
478 1.1 jdolecek printf("detrunc(): allerror %d, eofentry %lu\n",
479 1.1 jdolecek allerror, eofentry);
480 1.1 jdolecek #endif
481 1.1 jdolecek
482 1.1 jdolecek /*
483 1.1 jdolecek * If we need to break the cluster chain for the file then do it
484 1.1 jdolecek * now.
485 1.1 jdolecek */
486 1.1 jdolecek if (eofentry != ~0) {
487 1.1 jdolecek error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
488 1.1 jdolecek &chaintofree, CLUST_EOFE);
489 1.1 jdolecek if (error) {
490 1.1 jdolecek #ifdef MSDOSFS_DEBUG
491 1.1 jdolecek printf("detrunc(): fatentry errors %d\n", error);
492 1.1 jdolecek #endif
493 1.1 jdolecek return (error);
494 1.1 jdolecek }
495 1.1 jdolecek fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
496 1.1 jdolecek eofentry);
497 1.1 jdolecek }
498 1.1 jdolecek
499 1.1 jdolecek /*
500 1.1 jdolecek * Now free the clusters removed from the file because of the
501 1.1 jdolecek * truncation.
502 1.1 jdolecek */
503 1.1 jdolecek if (chaintofree != 0 && !MSDOSFSEOF(chaintofree, pmp->pm_fatmask))
504 1.1 jdolecek freeclusterchain(pmp, chaintofree);
505 1.1 jdolecek
506 1.1 jdolecek return (allerror);
507 1.1 jdolecek }
508 1.1 jdolecek
509 1.1 jdolecek /*
510 1.1 jdolecek * Extend the file described by dep to length specified by length.
511 1.1 jdolecek */
512 1.1 jdolecek int
513 1.1 jdolecek deextend(dep, length, cred)
514 1.1 jdolecek struct denode *dep;
515 1.1 jdolecek u_long length;
516 1.1 jdolecek struct ucred *cred;
517 1.1 jdolecek {
518 1.1 jdolecek struct msdosfsmount *pmp = dep->de_pmp;
519 1.1 jdolecek u_long count, osize;
520 1.1 jdolecek int error;
521 1.1 jdolecek
522 1.1 jdolecek /*
523 1.1 jdolecek * The root of a DOS filesystem cannot be extended.
524 1.1 jdolecek */
525 1.1 jdolecek if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp))
526 1.1 jdolecek return (EINVAL);
527 1.1 jdolecek
528 1.1 jdolecek /*
529 1.1 jdolecek * Directories cannot be extended.
530 1.1 jdolecek */
531 1.1 jdolecek if (dep->de_Attributes & ATTR_DIRECTORY)
532 1.1 jdolecek return (EISDIR);
533 1.1 jdolecek
534 1.1 jdolecek if (length <= dep->de_FileSize)
535 1.1 jdolecek panic("deextend: file too large");
536 1.1 jdolecek
537 1.1 jdolecek /*
538 1.1 jdolecek * Compute the number of clusters to allocate.
539 1.1 jdolecek */
540 1.1 jdolecek count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
541 1.1 jdolecek if (count > 0) {
542 1.1 jdolecek if (count > pmp->pm_freeclustercount)
543 1.1 jdolecek return (ENOSPC);
544 1.1 jdolecek error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
545 1.1 jdolecek if (error) {
546 1.1 jdolecek /* truncate the added clusters away again */
547 1.1 jdolecek (void) detrunc(dep, dep->de_FileSize, 0, cred, NULL);
548 1.1 jdolecek return (error);
549 1.1 jdolecek }
550 1.1 jdolecek }
551 1.1 jdolecek
552 1.1 jdolecek osize = dep->de_FileSize;
553 1.1 jdolecek dep->de_FileSize = length;
554 1.1 jdolecek uvm_vnp_setsize(DETOV(dep), (voff_t)dep->de_FileSize);
555 1.1 jdolecek dep->de_flag |= DE_UPDATE|DE_MODIFIED;
556 1.1 jdolecek uvm_vnp_zerorange(DETOV(dep), (off_t)osize,
557 1.1 jdolecek (size_t)(dep->de_FileSize - osize));
558 1.1 jdolecek return (deupdat(dep, 1));
559 1.1 jdolecek }
560 1.1 jdolecek
561 1.1 jdolecek /*
562 1.1 jdolecek * Move a denode to its correct hash queue after the file it represents has
563 1.1 jdolecek * been moved to a new directory.
564 1.1 jdolecek */
565 1.1 jdolecek void
566 1.1 jdolecek reinsert(dep)
567 1.1 jdolecek struct denode *dep;
568 1.1 jdolecek {
569 1.1 jdolecek /*
570 1.1 jdolecek * Fix up the denode cache. If the denode is for a directory,
571 1.1 jdolecek * there is nothing to do since the hash is based on the starting
572 1.1 jdolecek * cluster of the directory file and that hasn't changed. If for a
573 1.1 jdolecek * file the hash is based on the location of the directory entry,
574 1.1 jdolecek * so we must remove it from the cache and re-enter it with the
575 1.1 jdolecek * hash based on the new location of the directory entry.
576 1.1 jdolecek */
577 1.1 jdolecek if (dep->de_Attributes & ATTR_DIRECTORY)
578 1.1 jdolecek return;
579 1.1 jdolecek msdosfs_hashrem(dep);
580 1.1 jdolecek msdosfs_hashins(dep);
581 1.1 jdolecek }
582 1.1 jdolecek
583 1.1 jdolecek int
584 1.1 jdolecek msdosfs_reclaim(v)
585 1.1 jdolecek void *v;
586 1.1 jdolecek {
587 1.1 jdolecek struct vop_reclaim_args /* {
588 1.1 jdolecek struct vnode *a_vp;
589 1.1 jdolecek } */ *ap = v;
590 1.1 jdolecek struct vnode *vp = ap->a_vp;
591 1.1 jdolecek struct denode *dep = VTODE(vp);
592 1.1 jdolecek
593 1.1 jdolecek #ifdef MSDOSFS_DEBUG
594 1.1 jdolecek printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n",
595 1.1 jdolecek dep, dep->de_Name, dep->de_refcnt);
596 1.1 jdolecek #endif
597 1.1 jdolecek
598 1.1 jdolecek if (prtactive && vp->v_usecount != 0)
599 1.1 jdolecek vprint("msdosfs_reclaim(): pushing active", vp);
600 1.1 jdolecek /*
601 1.1 jdolecek * Remove the denode from its hash chain.
602 1.1 jdolecek */
603 1.1 jdolecek msdosfs_hashrem(dep);
604 1.1 jdolecek /*
605 1.1 jdolecek * Purge old data structures associated with the denode.
606 1.1 jdolecek */
607 1.1 jdolecek cache_purge(vp);
608 1.1 jdolecek if (dep->de_devvp) {
609 1.1 jdolecek vrele(dep->de_devvp);
610 1.1 jdolecek dep->de_devvp = 0;
611 1.1 jdolecek }
612 1.1 jdolecek #if 0 /* XXX */
613 1.1 jdolecek dep->de_flag = 0;
614 1.1 jdolecek #endif
615 1.1 jdolecek pool_put(&msdosfs_denode_pool, dep);
616 1.1 jdolecek vp->v_data = NULL;
617 1.1 jdolecek return (0);
618 1.1 jdolecek }
619 1.1 jdolecek
620 1.1 jdolecek int
621 1.1 jdolecek msdosfs_inactive(v)
622 1.1 jdolecek void *v;
623 1.1 jdolecek {
624 1.1 jdolecek struct vop_inactive_args /* {
625 1.1 jdolecek struct vnode *a_vp;
626 1.1 jdolecek struct proc *a_p;
627 1.1 jdolecek } */ *ap = v;
628 1.2 darrenr struct lwp *l = ap->a_l;
629 1.1 jdolecek struct vnode *vp = ap->a_vp;
630 1.1 jdolecek struct denode *dep = VTODE(vp);
631 1.1 jdolecek int error = 0;
632 1.1 jdolecek
633 1.1 jdolecek #ifdef MSDOSFS_DEBUG
634 1.1 jdolecek printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]);
635 1.1 jdolecek #endif
636 1.1 jdolecek
637 1.1 jdolecek if (prtactive && vp->v_usecount != 0)
638 1.1 jdolecek vprint("msdosfs_inactive(): pushing active", vp);
639 1.1 jdolecek
640 1.1 jdolecek /*
641 1.1 jdolecek * Get rid of denodes related to stale file handles.
642 1.1 jdolecek */
643 1.1 jdolecek if (dep->de_Name[0] == SLOT_DELETED)
644 1.1 jdolecek goto out;
645 1.1 jdolecek
646 1.1 jdolecek /*
647 1.1 jdolecek * If the file has been deleted and it is on a read/write
648 1.1 jdolecek * filesystem, then truncate the file, and mark the directory slot
649 1.1 jdolecek * as empty. (This may not be necessary for the dos filesystem.)
650 1.1 jdolecek */
651 1.1 jdolecek #ifdef MSDOSFS_DEBUG
652 1.1 jdolecek printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x %s\n",
653 1.1 jdolecek dep, dep->de_refcnt, vp->v_mount->mnt_flag,
654 1.1 jdolecek (vp->v_mount->mnt_flag & MNT_RDONLY) ? "MNT_RDONLY" : "");
655 1.1 jdolecek #endif
656 1.1 jdolecek if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
657 1.1 jdolecek if (dep->de_FileSize != 0) {
658 1.1 jdolecek error = detrunc(dep, (u_long)0, 0, NOCRED, NULL);
659 1.1 jdolecek }
660 1.1 jdolecek dep->de_Name[0] = SLOT_DELETED;
661 1.1 jdolecek }
662 1.1 jdolecek deupdat(dep, 0);
663 1.1 jdolecek out:
664 1.1 jdolecek VOP_UNLOCK(vp, 0);
665 1.1 jdolecek /*
666 1.1 jdolecek * If we are done with the denode, reclaim it
667 1.1 jdolecek * so that it can be reused immediately.
668 1.1 jdolecek */
669 1.1 jdolecek #ifdef MSDOSFS_DEBUG
670 1.1 jdolecek printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n",
671 1.1 jdolecek vp->v_usecount, dep->de_Name[0]);
672 1.1 jdolecek #endif
673 1.1 jdolecek if (dep->de_Name[0] == SLOT_DELETED)
674 1.2 darrenr vrecycle(vp, (struct simplelock *)0, l);
675 1.1 jdolecek return (error);
676 1.1 jdolecek }
677 1.1 jdolecek
678 1.1 jdolecek int
679 1.1 jdolecek msdosfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
680 1.1 jdolecek struct ucred *cred)
681 1.1 jdolecek {
682 1.1 jdolecek return 0;
683 1.1 jdolecek }
684