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