1237d1b14SEd Maste /* $NetBSD: msdosfs_denode.c,v 1.7 2015/03/29 05:52:59 agc Exp $ */
2237d1b14SEd Maste
3237d1b14SEd Maste /*-
42037e988SEd Maste * SPDX-License-Identifier: BSD-4-Clause
52037e988SEd Maste *
6237d1b14SEd Maste * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
7237d1b14SEd Maste * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
8237d1b14SEd Maste * All rights reserved.
9237d1b14SEd Maste * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
10237d1b14SEd Maste *
11237d1b14SEd Maste * Redistribution and use in source and binary forms, with or without
12237d1b14SEd Maste * modification, are permitted provided that the following conditions
13237d1b14SEd Maste * are met:
14237d1b14SEd Maste * 1. Redistributions of source code must retain the above copyright
15237d1b14SEd Maste * notice, this list of conditions and the following disclaimer.
16237d1b14SEd Maste * 2. Redistributions in binary form must reproduce the above copyright
17237d1b14SEd Maste * notice, this list of conditions and the following disclaimer in the
18237d1b14SEd Maste * documentation and/or other materials provided with the distribution.
19237d1b14SEd Maste * 3. All advertising materials mentioning features or use of this software
20237d1b14SEd Maste * must display the following acknowledgement:
21237d1b14SEd Maste * This product includes software developed by TooLs GmbH.
22237d1b14SEd Maste * 4. The name of TooLs GmbH may not be used to endorse or promote products
23237d1b14SEd Maste * derived from this software without specific prior written permission.
24237d1b14SEd Maste *
25237d1b14SEd Maste * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
26237d1b14SEd Maste * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27237d1b14SEd Maste * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28237d1b14SEd Maste * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29237d1b14SEd Maste * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30237d1b14SEd Maste * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
31237d1b14SEd Maste * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32237d1b14SEd Maste * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33237d1b14SEd Maste * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34237d1b14SEd Maste * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35237d1b14SEd Maste */
362037e988SEd Maste /*-
37237d1b14SEd Maste * Written by Paul Popelka (paulp@uts.amdahl.com)
38237d1b14SEd Maste *
39237d1b14SEd Maste * You can do anything you want with this software, just don't say you wrote
40237d1b14SEd Maste * it, and don't remove this notice.
41237d1b14SEd Maste *
42237d1b14SEd Maste * This software is provided "as is".
43237d1b14SEd Maste *
44237d1b14SEd Maste * The author supplies this software to be publicly redistributed on the
45237d1b14SEd Maste * understanding that the author is not responsible for the correct
46237d1b14SEd Maste * functioning of this software in any circumstances and is not liable for
47237d1b14SEd Maste * any damages caused by this software.
48237d1b14SEd Maste *
49237d1b14SEd Maste * October 1992
50237d1b14SEd Maste */
51237d1b14SEd Maste
52237d1b14SEd Maste #include <sys/param.h>
5398dc8da5SEd Maste #include <sys/errno.h>
54237d1b14SEd Maste
55aaa38524SConrad Meyer #include <stdbool.h>
5698dc8da5SEd Maste #include <stdio.h>
5798dc8da5SEd Maste #include <string.h>
5898dc8da5SEd Maste #include <stdlib.h>
5998dc8da5SEd Maste #include <util.h>
60237d1b14SEd Maste
61237d1b14SEd Maste #include <fs/msdosfs/bpb.h>
62ba2cfa80SAlex Richardson #include "msdos/denode.h"
6351e79affSEd Maste #include <fs/msdosfs/fat.h>
64840aca28SEd Maste #include <fs/msdosfs/msdosfsmount.h>
65237d1b14SEd Maste
6698dc8da5SEd Maste #include "makefs.h"
6798dc8da5SEd Maste #include "msdos.h"
6898dc8da5SEd Maste
69237d1b14SEd Maste
70237d1b14SEd Maste /*
71237d1b14SEd Maste * If deget() succeeds it returns with the gotten denode locked().
72237d1b14SEd Maste *
73237d1b14SEd Maste * pmp - address of msdosfsmount structure of the filesystem containing
74237d1b14SEd Maste * the denode of interest. The pm_dev field and the address of
75237d1b14SEd Maste * the msdosfsmount structure are used.
76237d1b14SEd Maste * dirclust - which cluster bp contains, if dirclust is 0 (root directory)
77237d1b14SEd Maste * diroffset is relative to the beginning of the root directory,
78237d1b14SEd Maste * otherwise it is cluster relative.
79237d1b14SEd Maste * diroffset - offset past begin of cluster of denode we want
80237d1b14SEd Maste * depp - returns the address of the gotten denode.
81237d1b14SEd Maste */
82237d1b14SEd Maste int
deget(struct msdosfsmount * pmp,u_long dirclust,u_long diroffset,int lkflags __unused,struct denode ** depp)83237d1b14SEd Maste deget(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset,
84ae7e8a02SKonstantin Belousov int lkflags __unused, struct denode **depp)
85237d1b14SEd Maste {
86237d1b14SEd Maste int error;
8798dc8da5SEd Maste uint64_t inode;
88237d1b14SEd Maste struct direntry *direntptr;
89237d1b14SEd Maste struct denode *ldep;
90d485c77fSKonstantin Belousov struct m_buf *bp;
91237d1b14SEd Maste
9298dc8da5SEd Maste MSDOSFS_DPRINTF(("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n",
9398dc8da5SEd Maste pmp, dirclust, diroffset, depp));
94237d1b14SEd Maste
95237d1b14SEd Maste /*
96237d1b14SEd Maste * On FAT32 filesystems, root is a (more or less) normal
97237d1b14SEd Maste * directory
98237d1b14SEd Maste */
99237d1b14SEd Maste if (FAT32(pmp) && dirclust == MSDOSFSROOT)
100237d1b14SEd Maste dirclust = pmp->pm_rootdirblk;
101237d1b14SEd Maste
10298dc8da5SEd Maste inode = (uint64_t)pmp->pm_bpcluster * dirclust + diroffset;
10398dc8da5SEd Maste
104237d1b14SEd Maste ldep = ecalloc(1, sizeof(*ldep));
105237d1b14SEd Maste ldep->de_vnode = NULL;
106237d1b14SEd Maste ldep->de_flag = 0;
107237d1b14SEd Maste ldep->de_dirclust = dirclust;
108237d1b14SEd Maste ldep->de_diroffset = diroffset;
10998dc8da5SEd Maste ldep->de_inode = inode;
110237d1b14SEd Maste ldep->de_pmp = pmp;
111237d1b14SEd Maste ldep->de_refcnt = 1;
11298dc8da5SEd Maste fc_purge(ldep, 0); /* init the FAT cache for this denode */
113237d1b14SEd Maste /*
114237d1b14SEd Maste * Copy the directory entry into the denode area of the vnode.
115237d1b14SEd Maste */
116237d1b14SEd Maste if ((dirclust == MSDOSFSROOT
117237d1b14SEd Maste || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk))
118237d1b14SEd Maste && diroffset == MSDOSFSROOT_OFS) {
119237d1b14SEd Maste /*
120237d1b14SEd Maste * Directory entry for the root directory. There isn't one,
121237d1b14SEd Maste * so we manufacture one. We should probably rummage
122237d1b14SEd Maste * through the root directory and find a label entry (if it
123237d1b14SEd Maste * exists), and then use the time and date from that entry
124237d1b14SEd Maste * as the time and date for the root denode.
125237d1b14SEd Maste */
126237d1b14SEd Maste ldep->de_vnode = (struct vnode *)-1;
127237d1b14SEd Maste
128237d1b14SEd Maste ldep->de_Attributes = ATTR_DIRECTORY;
12998dc8da5SEd Maste ldep->de_LowerCase = 0;
130237d1b14SEd Maste if (FAT32(pmp))
131237d1b14SEd Maste ldep->de_StartCluster = pmp->pm_rootdirblk;
132237d1b14SEd Maste /* de_FileSize will be filled in further down */
133237d1b14SEd Maste else {
134237d1b14SEd Maste ldep->de_StartCluster = MSDOSFSROOT;
13598dc8da5SEd Maste ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE;
136237d1b14SEd Maste }
137237d1b14SEd Maste /*
138237d1b14SEd Maste * fill in time and date so that dos2unixtime() doesn't
139237d1b14SEd Maste * spit up when called from msdosfs_getattr() with root
140237d1b14SEd Maste * denode
141237d1b14SEd Maste */
142237d1b14SEd Maste ldep->de_CHun = 0;
143237d1b14SEd Maste ldep->de_CTime = 0x0000; /* 00:00:00 */
144237d1b14SEd Maste ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
145237d1b14SEd Maste | (1 << DD_DAY_SHIFT);
146237d1b14SEd Maste /* Jan 1, 1980 */
147237d1b14SEd Maste ldep->de_ADate = ldep->de_CDate;
148237d1b14SEd Maste ldep->de_MTime = ldep->de_CTime;
149237d1b14SEd Maste ldep->de_MDate = ldep->de_CDate;
150237d1b14SEd Maste /* leave the other fields as garbage */
151237d1b14SEd Maste } else {
152d485c77fSKonstantin Belousov error = m_readep(pmp, dirclust, diroffset, &bp, &direntptr);
153237d1b14SEd Maste if (error) {
154237d1b14SEd Maste ldep->de_Name[0] = SLOT_DELETED;
15598dc8da5SEd Maste
15698dc8da5SEd Maste *depp = NULL;
157237d1b14SEd Maste return (error);
158237d1b14SEd Maste }
15998dc8da5SEd Maste (void)DE_INTERNALIZE(ldep, direntptr);
1605b292f9aSEd Maste brelse(bp);
161237d1b14SEd Maste }
162237d1b14SEd Maste
163237d1b14SEd Maste /*
164237d1b14SEd Maste * Fill in a few fields of the vnode and finish filling in the
165237d1b14SEd Maste * denode. Then return the address of the found denode.
166237d1b14SEd Maste */
167237d1b14SEd Maste if (ldep->de_Attributes & ATTR_DIRECTORY) {
168237d1b14SEd Maste /*
169237d1b14SEd Maste * Since DOS directory entries that describe directories
170237d1b14SEd Maste * have 0 in the filesize field, we take this opportunity
171237d1b14SEd Maste * to find out the length of the directory and plug it into
172237d1b14SEd Maste * the denode structure.
173237d1b14SEd Maste */
174237d1b14SEd Maste u_long size;
175237d1b14SEd Maste
17698dc8da5SEd Maste /*
17798dc8da5SEd Maste * XXX it sometimes happens that the "." entry has cluster
17898dc8da5SEd Maste * number 0 when it shouldn't. Use the actual cluster number
17998dc8da5SEd Maste * instead of what is written in directory entry.
18098dc8da5SEd Maste */
18198dc8da5SEd Maste if (diroffset == 0 && ldep->de_StartCluster != dirclust) {
18298dc8da5SEd Maste MSDOSFS_DPRINTF(("deget(): \".\" entry at clust %lu != %lu\n",
18398dc8da5SEd Maste dirclust, ldep->de_StartCluster));
18498dc8da5SEd Maste
18598dc8da5SEd Maste ldep->de_StartCluster = dirclust;
18698dc8da5SEd Maste }
18798dc8da5SEd Maste
188237d1b14SEd Maste if (ldep->de_StartCluster != MSDOSFSROOT) {
18998dc8da5SEd Maste error = pcbmap(ldep, 0xffff, 0, &size, 0);
190237d1b14SEd Maste if (error == E2BIG) {
191237d1b14SEd Maste ldep->de_FileSize = de_cn2off(pmp, size);
192237d1b14SEd Maste error = 0;
19398dc8da5SEd Maste } else {
19498dc8da5SEd Maste MSDOSFS_DPRINTF(("deget(): pcbmap returned %d\n",
19598dc8da5SEd Maste error));
19698dc8da5SEd Maste }
197237d1b14SEd Maste }
198237d1b14SEd Maste }
199237d1b14SEd Maste *depp = ldep;
200237d1b14SEd Maste return (0);
201237d1b14SEd Maste }
202237d1b14SEd Maste
203237d1b14SEd Maste /*
204237d1b14SEd Maste * Truncate the file described by dep to the length specified by length.
205237d1b14SEd Maste */
206237d1b14SEd Maste int
detrunc(struct denode * dep,u_long length,int flags,struct ucred * cred)207476b0ab7SEd Maste detrunc(struct denode *dep, u_long length, int flags, struct ucred *cred)
208237d1b14SEd Maste {
209237d1b14SEd Maste int error;
210237d1b14SEd Maste u_long eofentry;
21198dc8da5SEd Maste u_long chaintofree;
21298dc8da5SEd Maste daddr_t bn;
213237d1b14SEd Maste int boff;
214237d1b14SEd Maste int isadir = dep->de_Attributes & ATTR_DIRECTORY;
215d485c77fSKonstantin Belousov struct m_buf *bp;
216237d1b14SEd Maste struct msdosfsmount *pmp = dep->de_pmp;
217237d1b14SEd Maste
21898dc8da5SEd Maste MSDOSFS_DPRINTF(("detrunc(): file %s, length %lu, flags %x\n",
21998dc8da5SEd Maste dep->de_Name, length, flags));
220237d1b14SEd Maste
221237d1b14SEd Maste /*
222237d1b14SEd Maste * Disallow attempts to truncate the root directory since it is of
223237d1b14SEd Maste * fixed size. That's just the way dos filesystems are. We use
224237d1b14SEd Maste * the VROOT bit in the vnode because checking for the directory
225237d1b14SEd Maste * bit and a startcluster of 0 in the denode is not adequate to
226237d1b14SEd Maste * recognize the root directory at this point in a file or
227237d1b14SEd Maste * directory's life.
228237d1b14SEd Maste */
229237d1b14SEd Maste if (dep->de_vnode != NULL && !FAT32(pmp)) {
23098dc8da5SEd Maste MSDOSFS_DPRINTF(("detrunc(): can't truncate root directory, "
23198dc8da5SEd Maste "clust %ld, offset %ld\n",
23298dc8da5SEd Maste dep->de_dirclust, dep->de_diroffset));
23398dc8da5SEd Maste
234237d1b14SEd Maste return (EINVAL);
235237d1b14SEd Maste }
236237d1b14SEd Maste
237237d1b14SEd Maste if (dep->de_FileSize < length)
238476b0ab7SEd Maste return deextend(dep, length, cred);
239237d1b14SEd Maste
240237d1b14SEd Maste /*
241237d1b14SEd Maste * If the desired length is 0 then remember the starting cluster of
242237d1b14SEd Maste * the file and set the StartCluster field in the directory entry
243237d1b14SEd Maste * to 0. If the desired length is not zero, then get the number of
244237d1b14SEd Maste * the last cluster in the shortened file. Then get the number of
245237d1b14SEd Maste * the first cluster in the part of the file that is to be freed.
246237d1b14SEd Maste * Then set the next cluster pointer in the last cluster of the
247237d1b14SEd Maste * file to CLUST_EOFE.
248237d1b14SEd Maste */
249237d1b14SEd Maste if (length == 0) {
250237d1b14SEd Maste chaintofree = dep->de_StartCluster;
251237d1b14SEd Maste dep->de_StartCluster = 0;
252*cc1a53bcSMark Johnston eofentry = ~0ul;
253237d1b14SEd Maste } else {
25498dc8da5SEd Maste error = pcbmap(dep, de_clcount(pmp, length) - 1, 0,
25598dc8da5SEd Maste &eofentry, 0);
256237d1b14SEd Maste if (error) {
25798dc8da5SEd Maste MSDOSFS_DPRINTF(("detrunc(): pcbmap fails %d\n",
25898dc8da5SEd Maste error));
2592037e988SEd Maste return (error);
260237d1b14SEd Maste }
261237d1b14SEd Maste }
262237d1b14SEd Maste
26398dc8da5SEd Maste fc_purge(dep, de_clcount(pmp, length));
26498dc8da5SEd Maste
265237d1b14SEd Maste /*
266237d1b14SEd Maste * If the new length is not a multiple of the cluster size then we
267237d1b14SEd Maste * must zero the tail end of the new last cluster in case it
268237d1b14SEd Maste * becomes part of the file again because of a seek.
269237d1b14SEd Maste */
270237d1b14SEd Maste if ((boff = length & pmp->pm_crbomask) != 0) {
271237d1b14SEd Maste if (isadir) {
272237d1b14SEd Maste bn = cntobn(pmp, eofentry);
273d485c77fSKonstantin Belousov error = bread((void *)pmp->pm_devvp, bn,
274d485c77fSKonstantin Belousov pmp->pm_bpcluster, 0, &bp);
275237d1b14SEd Maste if (error) {
27698dc8da5SEd Maste brelse(bp);
27798dc8da5SEd Maste MSDOSFS_DPRINTF(("detrunc(): bread fails %d\n",
27898dc8da5SEd Maste error));
27998dc8da5SEd Maste
2802037e988SEd Maste return (error);
281237d1b14SEd Maste }
2822037e988SEd Maste memset(bp->b_data + boff, 0, pmp->pm_bpcluster - boff);
283237d1b14SEd Maste bwrite(bp);
284237d1b14SEd Maste }
285237d1b14SEd Maste }
286237d1b14SEd Maste
287237d1b14SEd Maste /*
288237d1b14SEd Maste * Write out the updated directory entry. Even if the update fails
289237d1b14SEd Maste * we free the trailing clusters.
290237d1b14SEd Maste */
291237d1b14SEd Maste dep->de_FileSize = length;
292237d1b14SEd Maste if (!isadir)
293237d1b14SEd Maste dep->de_flag |= DE_UPDATE|DE_MODIFIED;
294*cc1a53bcSMark Johnston MSDOSFS_DPRINTF(("detrunc(): eofentry %lu\n", eofentry));
295237d1b14SEd Maste
296237d1b14SEd Maste /*
297237d1b14SEd Maste * If we need to break the cluster chain for the file then do it
298237d1b14SEd Maste * now.
299237d1b14SEd Maste */
300*cc1a53bcSMark Johnston if (eofentry != ~0ul) {
301237d1b14SEd Maste error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
302237d1b14SEd Maste &chaintofree, CLUST_EOFE);
303237d1b14SEd Maste if (error) {
30498dc8da5SEd Maste MSDOSFS_DPRINTF(("detrunc(): fatentry errors %d\n",
30598dc8da5SEd Maste error));
306237d1b14SEd Maste return (error);
307237d1b14SEd Maste }
30898dc8da5SEd Maste fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
30998dc8da5SEd Maste eofentry);
310237d1b14SEd Maste }
311237d1b14SEd Maste
312237d1b14SEd Maste /*
313237d1b14SEd Maste * Now free the clusters removed from the file because of the
314237d1b14SEd Maste * truncation.
315237d1b14SEd Maste */
31698dc8da5SEd Maste if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree))
317237d1b14SEd Maste freeclusterchain(pmp, chaintofree);
318237d1b14SEd Maste
319*cc1a53bcSMark Johnston return (0);
320237d1b14SEd Maste }
321237d1b14SEd Maste
322237d1b14SEd Maste /*
323237d1b14SEd Maste * Extend the file described by dep to length specified by length.
324237d1b14SEd Maste */
325237d1b14SEd Maste int
deextend(struct denode * dep,u_long length,struct ucred * cred)326476b0ab7SEd Maste deextend(struct denode *dep, u_long length, struct ucred *cred)
327237d1b14SEd Maste {
328237d1b14SEd Maste struct msdosfsmount *pmp = dep->de_pmp;
329237d1b14SEd Maste u_long count;
330237d1b14SEd Maste int error;
331237d1b14SEd Maste
332237d1b14SEd Maste /*
333237d1b14SEd Maste * The root of a DOS filesystem cannot be extended.
334237d1b14SEd Maste */
335237d1b14SEd Maste if (dep->de_vnode != NULL && !FAT32(pmp))
3362037e988SEd Maste return (EINVAL);
337237d1b14SEd Maste
338237d1b14SEd Maste /*
339237d1b14SEd Maste * Directories cannot be extended.
340237d1b14SEd Maste */
341237d1b14SEd Maste if (dep->de_Attributes & ATTR_DIRECTORY)
3422037e988SEd Maste return (EISDIR);
343237d1b14SEd Maste
344237d1b14SEd Maste if (length <= dep->de_FileSize)
3452037e988SEd Maste return (E2BIG);
346237d1b14SEd Maste
347237d1b14SEd Maste /*
348237d1b14SEd Maste * Compute the number of clusters to allocate.
349237d1b14SEd Maste */
350237d1b14SEd Maste count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
351237d1b14SEd Maste if (count > 0) {
352237d1b14SEd Maste if (count > pmp->pm_freeclustercount)
353237d1b14SEd Maste return (ENOSPC);
354d485c77fSKonstantin Belousov error = m_extendfile(dep, count, NULL, NULL, DE_CLEAR);
355237d1b14SEd Maste if (error) {
356237d1b14SEd Maste /* truncate the added clusters away again */
357476b0ab7SEd Maste (void) detrunc(dep, dep->de_FileSize, 0, cred);
358237d1b14SEd Maste return (error);
359237d1b14SEd Maste }
360237d1b14SEd Maste }
361237d1b14SEd Maste
362237d1b14SEd Maste /*
363237d1b14SEd Maste * Zero extend file range; ubc_zerorange() uses ubc_alloc() and a
364237d1b14SEd Maste * memset(); we set the write size so ubc won't read in file data that
365237d1b14SEd Maste * is zero'd later.
366237d1b14SEd Maste */
367237d1b14SEd Maste dep->de_FileSize = length;
368237d1b14SEd Maste dep->de_flag |= DE_UPDATE | DE_MODIFIED;
369237d1b14SEd Maste return 0;
370237d1b14SEd Maste }
371