msdosfs_denode.c revision 1.1 1 1.1 christos /* $NetBSD: msdosfs_denode.c,v 1.1 2013/01/26 00:20:40 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
5 1.1 christos * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
6 1.1 christos * All rights reserved.
7 1.1 christos * Original code by Paul Popelka (paulp (at) uts.amdahl.com) (see below).
8 1.1 christos *
9 1.1 christos * Redistribution and use in source and binary forms, with or without
10 1.1 christos * modification, are permitted provided that the following conditions
11 1.1 christos * are met:
12 1.1 christos * 1. Redistributions of source code must retain the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer.
14 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 christos * notice, this list of conditions and the following disclaimer in the
16 1.1 christos * documentation and/or other materials provided with the distribution.
17 1.1 christos * 3. All advertising materials mentioning features or use of this software
18 1.1 christos * must display the following acknowledgement:
19 1.1 christos * This product includes software developed by TooLs GmbH.
20 1.1 christos * 4. The name of TooLs GmbH may not be used to endorse or promote products
21 1.1 christos * derived from this software without specific prior written permission.
22 1.1 christos *
23 1.1 christos * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.1 christos * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 1.1 christos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 1.1 christos * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 1.1 christos * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 1.1 christos * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 1.1 christos * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 1.1 christos * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 christos */
34 1.1 christos /*
35 1.1 christos * Written by Paul Popelka (paulp (at) uts.amdahl.com)
36 1.1 christos *
37 1.1 christos * You can do anything you want with this software, just don't say you wrote
38 1.1 christos * it, and don't remove this notice.
39 1.1 christos *
40 1.1 christos * This software is provided "as is".
41 1.1 christos *
42 1.1 christos * The author supplies this software to be publicly redistributed on the
43 1.1 christos * understanding that the author is not responsible for the correct
44 1.1 christos * functioning of this software in any circumstances and is not liable for
45 1.1 christos * any damages caused by this software.
46 1.1 christos *
47 1.1 christos * October 1992
48 1.1 christos */
49 1.1 christos
50 1.1 christos #if HAVE_NBTOOL_CONFIG_H
51 1.1 christos #include "nbtool_config.h"
52 1.1 christos #endif
53 1.1 christos
54 1.1 christos #include <sys/cdefs.h>
55 1.1 christos __KERNEL_RCSID(0, "$NetBSD: msdosfs_denode.c,v 1.1 2013/01/26 00:20:40 christos Exp $");
56 1.1 christos
57 1.1 christos #include <sys/param.h>
58 1.1 christos #include <sys/systm.h>
59 1.1 christos #include <sys/mount.h>
60 1.1 christos #include <sys/malloc.h>
61 1.1 christos #include <sys/pool.h>
62 1.1 christos #include <sys/proc.h>
63 1.1 christos #include <sys/kernel.h> /* defines "time" */
64 1.1 christos #include <sys/dirent.h>
65 1.1 christos #include <sys/namei.h>
66 1.1 christos #include <sys/kauth.h>
67 1.1 christos #include <sys/rbtree.h>
68 1.1 christos
69 1.1 christos #ifdef _KERNEL
70 1.1 christos #include <sys/fstrans.h>
71 1.1 christos #include <uvm/uvm_extern.h>
72 1.1 christos #include <sys/buf.h>
73 1.1 christos #include <sys/vnode.h>
74 1.1 christos #else
75 1.1 christos #include <ffs/buf.h>
76 1.1 christos #endif
77 1.1 christos
78 1.1 christos #include <fs/msdosfs/bpb.h>
79 1.1 christos #include <fs/msdosfs/msdosfsmount.h>
80 1.1 christos #include <fs/msdosfs/direntry.h>
81 1.1 christos #include <fs/msdosfs/denode.h>
82 1.1 christos #include <fs/msdosfs/fat.h>
83 1.1 christos
84 1.1 christos /*
85 1.1 christos * If deget() succeeds it returns with the gotten denode locked().
86 1.1 christos *
87 1.1 christos * pmp - address of msdosfsmount structure of the filesystem containing
88 1.1 christos * the denode of interest. The pm_dev field and the address of
89 1.1 christos * the msdosfsmount structure are used.
90 1.1 christos * dirclust - which cluster bp contains, if dirclust is 0 (root directory)
91 1.1 christos * diroffset is relative to the beginning of the root directory,
92 1.1 christos * otherwise it is cluster relative.
93 1.1 christos * diroffset - offset past begin of cluster of denode we want
94 1.1 christos * depp - returns the address of the gotten denode.
95 1.1 christos */
96 1.1 christos int
97 1.1 christos deget(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset, struct denode **depp)
98 1.1 christos /* pmp: so we know the maj/min number */
99 1.1 christos /* dirclust: cluster this dir entry came from */
100 1.1 christos /* diroffset: index of entry within the cluster */
101 1.1 christos /* depp: returns the addr of the gotten denode */
102 1.1 christos {
103 1.1 christos int error;
104 1.1 christos struct direntry *direntptr;
105 1.1 christos struct denode *ldep;
106 1.1 christos struct buf *bp;
107 1.1 christos
108 1.1 christos #ifdef MSDOSFS_DEBUG
109 1.1 christos printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n",
110 1.1 christos pmp, dirclust, diroffset, depp);
111 1.1 christos #endif
112 1.1 christos
113 1.1 christos /*
114 1.1 christos * On FAT32 filesystems, root is a (more or less) normal
115 1.1 christos * directory
116 1.1 christos */
117 1.1 christos if (FAT32(pmp) && dirclust == MSDOSFSROOT)
118 1.1 christos dirclust = pmp->pm_rootdirblk;
119 1.1 christos
120 1.1 christos ldep = calloc(1, sizeof(*ldep));
121 1.1 christos if (ldep == NULL)
122 1.1 christos err(1, "calloc");
123 1.1 christos ldep->de_vnode = NULL;
124 1.1 christos ldep->de_flag = 0;
125 1.1 christos ldep->de_devvp = 0;
126 1.1 christos ldep->de_lockf = 0;
127 1.1 christos ldep->de_dev = pmp->pm_dev;
128 1.1 christos ldep->de_dirclust = dirclust;
129 1.1 christos ldep->de_diroffset = diroffset;
130 1.1 christos ldep->de_pmp = pmp;
131 1.1 christos ldep->de_devvp = pmp->pm_devvp;
132 1.1 christos ldep->de_refcnt = 1;
133 1.1 christos /*
134 1.1 christos * Copy the directory entry into the denode area of the vnode.
135 1.1 christos */
136 1.1 christos if ((dirclust == MSDOSFSROOT
137 1.1 christos || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk))
138 1.1 christos && diroffset == MSDOSFSROOT_OFS) {
139 1.1 christos /*
140 1.1 christos * Directory entry for the root directory. There isn't one,
141 1.1 christos * so we manufacture one. We should probably rummage
142 1.1 christos * through the root directory and find a label entry (if it
143 1.1 christos * exists), and then use the time and date from that entry
144 1.1 christos * as the time and date for the root denode.
145 1.1 christos */
146 1.1 christos ldep->de_vnode = (struct vnode *)-1;
147 1.1 christos
148 1.1 christos ldep->de_Attributes = ATTR_DIRECTORY;
149 1.1 christos if (FAT32(pmp))
150 1.1 christos ldep->de_StartCluster = pmp->pm_rootdirblk;
151 1.1 christos /* de_FileSize will be filled in further down */
152 1.1 christos else {
153 1.1 christos ldep->de_StartCluster = MSDOSFSROOT;
154 1.1 christos ldep->de_FileSize = pmp->pm_rootdirsize * pmp->pm_BytesPerSec;
155 1.1 christos }
156 1.1 christos /*
157 1.1 christos * fill in time and date so that dos2unixtime() doesn't
158 1.1 christos * spit up when called from msdosfs_getattr() with root
159 1.1 christos * denode
160 1.1 christos */
161 1.1 christos ldep->de_CHun = 0;
162 1.1 christos ldep->de_CTime = 0x0000; /* 00:00:00 */
163 1.1 christos ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
164 1.1 christos | (1 << DD_DAY_SHIFT);
165 1.1 christos /* Jan 1, 1980 */
166 1.1 christos ldep->de_ADate = ldep->de_CDate;
167 1.1 christos ldep->de_MTime = ldep->de_CTime;
168 1.1 christos ldep->de_MDate = ldep->de_CDate;
169 1.1 christos /* leave the other fields as garbage */
170 1.1 christos } else {
171 1.1 christos error = readep(pmp, dirclust, diroffset, &bp, &direntptr);
172 1.1 christos if (error) {
173 1.1 christos ldep->de_devvp = NULL;
174 1.1 christos ldep->de_Name[0] = SLOT_DELETED;
175 1.1 christos return (error);
176 1.1 christos }
177 1.1 christos DE_INTERNALIZE(ldep, direntptr);
178 1.1 christos brelse(bp, 0);
179 1.1 christos }
180 1.1 christos
181 1.1 christos /*
182 1.1 christos * Fill in a few fields of the vnode and finish filling in the
183 1.1 christos * denode. Then return the address of the found denode.
184 1.1 christos */
185 1.1 christos if (ldep->de_Attributes & ATTR_DIRECTORY) {
186 1.1 christos /*
187 1.1 christos * Since DOS directory entries that describe directories
188 1.1 christos * have 0 in the filesize field, we take this opportunity
189 1.1 christos * to find out the length of the directory and plug it into
190 1.1 christos * the denode structure.
191 1.1 christos */
192 1.1 christos u_long size;
193 1.1 christos
194 1.1 christos if (ldep->de_StartCluster != MSDOSFSROOT) {
195 1.1 christos error = pcbmap(ldep, CLUST_END, 0, &size, 0);
196 1.1 christos if (error == E2BIG) {
197 1.1 christos ldep->de_FileSize = de_cn2off(pmp, size);
198 1.1 christos error = 0;
199 1.1 christos } else
200 1.1 christos printf("deget(): pcbmap returned %d\n", error);
201 1.1 christos }
202 1.1 christos }
203 1.1 christos *depp = ldep;
204 1.1 christos return (0);
205 1.1 christos }
206 1.1 christos
207 1.1 christos /*
208 1.1 christos * Truncate the file described by dep to the length specified by length.
209 1.1 christos */
210 1.1 christos int
211 1.1 christos detrunc(struct denode *dep, u_long length, int flags, kauth_cred_t cred)
212 1.1 christos {
213 1.1 christos int error;
214 1.1 christos int allerror = 0;
215 1.1 christos u_long eofentry;
216 1.1 christos u_long chaintofree = 0;
217 1.1 christos daddr_t bn, lastblock;
218 1.1 christos int boff;
219 1.1 christos int isadir = dep->de_Attributes & ATTR_DIRECTORY;
220 1.1 christos struct buf *bp;
221 1.1 christos struct msdosfsmount *pmp = dep->de_pmp;
222 1.1 christos
223 1.1 christos #ifdef MSDOSFS_DEBUG
224 1.1 christos printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags);
225 1.1 christos #endif
226 1.1 christos
227 1.1 christos /*
228 1.1 christos * Disallow attempts to truncate the root directory since it is of
229 1.1 christos * fixed size. That's just the way dos filesystems are. We use
230 1.1 christos * the VROOT bit in the vnode because checking for the directory
231 1.1 christos * bit and a startcluster of 0 in the denode is not adequate to
232 1.1 christos * recognize the root directory at this point in a file or
233 1.1 christos * directory's life.
234 1.1 christos */
235 1.1 christos if ((DETOV(dep) == (struct vnode *)-1) && !FAT32(pmp)) {
236 1.1 christos printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
237 1.1 christos dep->de_dirclust, dep->de_diroffset);
238 1.1 christos return (EINVAL);
239 1.1 christos }
240 1.1 christos
241 1.1 christos if (dep->de_FileSize < length)
242 1.1 christos return (deextend(dep, length, cred));
243 1.1 christos lastblock = de_clcount(pmp, length) - 1;
244 1.1 christos
245 1.1 christos /*
246 1.1 christos * If the desired length is 0 then remember the starting cluster of
247 1.1 christos * the file and set the StartCluster field in the directory entry
248 1.1 christos * to 0. If the desired length is not zero, then get the number of
249 1.1 christos * the last cluster in the shortened file. Then get the number of
250 1.1 christos * the first cluster in the part of the file that is to be freed.
251 1.1 christos * Then set the next cluster pointer in the last cluster of the
252 1.1 christos * file to CLUST_EOFE.
253 1.1 christos */
254 1.1 christos if (length == 0) {
255 1.1 christos chaintofree = dep->de_StartCluster;
256 1.1 christos dep->de_StartCluster = 0;
257 1.1 christos eofentry = ~0;
258 1.1 christos } else {
259 1.1 christos error = pcbmap(dep, lastblock, 0, &eofentry, 0);
260 1.1 christos if (error) {
261 1.1 christos #ifdef MSDOSFS_DEBUG
262 1.1 christos printf("detrunc(): pcbmap fails %d\n", error);
263 1.1 christos #endif
264 1.1 christos return (error);
265 1.1 christos }
266 1.1 christos }
267 1.1 christos
268 1.1 christos /*
269 1.1 christos * If the new length is not a multiple of the cluster size then we
270 1.1 christos * must zero the tail end of the new last cluster in case it
271 1.1 christos * becomes part of the file again because of a seek.
272 1.1 christos */
273 1.1 christos if ((boff = length & pmp->pm_crbomask) != 0) {
274 1.1 christos if (isadir) {
275 1.1 christos bn = cntobn(pmp, eofentry);
276 1.1 christos error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn),
277 1.1 christos pmp->pm_bpcluster, NOCRED, B_MODIFY, &bp);
278 1.1 christos if (error) {
279 1.1 christos #ifdef MSDOSFS_DEBUG
280 1.1 christos printf("detrunc(): bread fails %d\n", error);
281 1.1 christos #endif
282 1.1 christos return (error);
283 1.1 christos }
284 1.1 christos memset((char *)bp->b_data + boff, 0,
285 1.1 christos pmp->pm_bpcluster - boff);
286 1.1 christos if (flags & IO_SYNC)
287 1.1 christos bwrite(bp);
288 1.1 christos else
289 1.1 christos bdwrite(bp);
290 1.1 christos }
291 1.1 christos }
292 1.1 christos
293 1.1 christos /*
294 1.1 christos * Write out the updated directory entry. Even if the update fails
295 1.1 christos * we free the trailing clusters.
296 1.1 christos */
297 1.1 christos dep->de_FileSize = length;
298 1.1 christos if (!isadir)
299 1.1 christos dep->de_flag |= DE_UPDATE|DE_MODIFIED;
300 1.1 christos #ifdef MSDOSFS_DEBUG
301 1.1 christos printf("detrunc(): allerror %d, eofentry %lu\n",
302 1.1 christos allerror, eofentry);
303 1.1 christos #endif
304 1.1 christos
305 1.1 christos /*
306 1.1 christos * If we need to break the cluster chain for the file then do it
307 1.1 christos * now.
308 1.1 christos */
309 1.1 christos if (eofentry != (u_long)~0) {
310 1.1 christos error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
311 1.1 christos &chaintofree, CLUST_EOFE);
312 1.1 christos if (error) {
313 1.1 christos #ifdef MSDOSFS_DEBUG
314 1.1 christos printf("detrunc(): fatentry errors %d\n", error);
315 1.1 christos #endif
316 1.1 christos return (error);
317 1.1 christos }
318 1.1 christos }
319 1.1 christos
320 1.1 christos /*
321 1.1 christos * Now free the clusters removed from the file because of the
322 1.1 christos * truncation.
323 1.1 christos */
324 1.1 christos if (chaintofree != 0 && !MSDOSFSEOF(chaintofree, pmp->pm_fatmask))
325 1.1 christos freeclusterchain(pmp, chaintofree);
326 1.1 christos
327 1.1 christos return (allerror);
328 1.1 christos }
329 1.1 christos
330 1.1 christos /*
331 1.1 christos * Extend the file described by dep to length specified by length.
332 1.1 christos */
333 1.1 christos int
334 1.1 christos deextend(struct denode *dep, u_long length, kauth_cred_t cred)
335 1.1 christos {
336 1.1 christos struct msdosfsmount *pmp = dep->de_pmp;
337 1.1 christos u_long count, osize;
338 1.1 christos int error;
339 1.1 christos
340 1.1 christos /*
341 1.1 christos * The root of a DOS filesystem cannot be extended.
342 1.1 christos */
343 1.1 christos if ((DETOV(dep) == (struct vnode *)-1) && !FAT32(pmp))
344 1.1 christos return (EINVAL);
345 1.1 christos
346 1.1 christos /*
347 1.1 christos * Directories cannot be extended.
348 1.1 christos */
349 1.1 christos if (dep->de_Attributes & ATTR_DIRECTORY)
350 1.1 christos return (EISDIR);
351 1.1 christos
352 1.1 christos if (length <= dep->de_FileSize)
353 1.1 christos panic("deextend: file too large");
354 1.1 christos
355 1.1 christos /*
356 1.1 christos * Compute the number of clusters to allocate.
357 1.1 christos */
358 1.1 christos count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
359 1.1 christos if (count > 0) {
360 1.1 christos if (count > pmp->pm_freeclustercount)
361 1.1 christos return (ENOSPC);
362 1.1 christos error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
363 1.1 christos if (error) {
364 1.1 christos /* truncate the added clusters away again */
365 1.1 christos (void) detrunc(dep, dep->de_FileSize, 0, cred);
366 1.1 christos return (error);
367 1.1 christos }
368 1.1 christos }
369 1.1 christos
370 1.1 christos /*
371 1.1 christos * Zero extend file range; ubc_zerorange() uses ubc_alloc() and a
372 1.1 christos * memset(); we set the write size so ubc won't read in file data that
373 1.1 christos * is zero'd later.
374 1.1 christos */
375 1.1 christos osize = dep->de_FileSize;
376 1.1 christos dep->de_FileSize = length;
377 1.1 christos dep->de_flag |= DE_UPDATE|DE_MODIFIED;
378 1.1 christos return 0;
379 1.1 christos }
380