xref: /titanic_51/usr/src/uts/common/sys/fs/pc_dir.h (revision bbaa8b60dd95d714741fc474adad3cf710ef4efd)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_SYS_FS_PC_DIR_H
27 #define	_SYS_FS_PC_DIR_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <sys/dirent.h>
32 
33 #ifdef	__cplusplus
34 extern "C" {
35 #endif
36 
37 #define	PCFNAMESIZE	8
38 #define	PCFEXTSIZE	3
39 #define	PCMAXNAMLEN	255
40 #define	PCMAXNAM_UTF16	(256 * sizeof (uint16_t))	/* for UTF-16 */
41 #define	PCLFNCHUNKSIZE	13
42 
43 struct pctime {
44 	ushort_t pct_time;		/* hh:mm:ss (little endian) */
45 	ushort_t pct_date;		/* yy:mm:dd (little endian) */
46 };
47 
48 /*
49  * Shifts and masks for time and date fields, in host byte order.
50  */
51 #define	SECSHIFT	0
52 #define	SECMASK		0x1F
53 #define	MINSHIFT	5
54 #define	MINMASK		0x3F
55 #define	HOURSHIFT	11
56 #define	HOURMASK	0x1F
57 
58 #define	DAYSHIFT	0
59 #define	DAYMASK		0x1F
60 #define	MONSHIFT	5
61 #define	MONMASK		0x0F
62 #define	YEARSHIFT	9
63 #define	YEARMASK	0x7F
64 
65 struct pcdir {
66 	char pcd_filename[PCFNAMESIZE];	/* file name */
67 	char pcd_ext[PCFEXTSIZE];	/* file extension */
68 	uchar_t	pcd_attr;		/* file attributes */
69 	uchar_t	pcd_ntattr;		/* reserved for NT attributes */
70 	uchar_t	pcd_crtime_msec;	/* milliseconds after the minute */
71 	struct pctime pcd_crtime;	/* creation time/date */
72 	ushort_t pcd_ladate;		/* last-access date */
73 	union {
74 		uint16_t pcd_eattr;	/* OS/2 extended attribute */
75 		pc_cluster16_t pcd_scluster_hi;
76 	} un;
77 	struct pctime pcd_mtime;	/* last modified time/date */
78 	pc_cluster16_t pcd_scluster_lo;	/* starting cluster (little endian) */
79 	uint_t	pcd_size;		/* file size (little endian) */
80 };
81 
82 #ifdef	__cplusplus
83 }
84 #endif
85 
86 #include <sys/fs/pc_node.h>
87 
88 #ifdef	__cplusplus
89 extern "C" {
90 #endif
91 
92 /*
93  * Long filename support (introduced by Windows 95) is an interesting
94  * exercise in compatibility. Basically, it is now no longer the case
95  * that an entry in a directory (as described by the 'pcdir' structure above)
96  * contains the entire name of a file. Now, a directory entry can consist of
97  * a long filename component (a series of 'pcdir'-like structures) followed
98  * by a short filename (the old form). Each long filename component is
99  * identified by having it's Read-Only, Hidden, System, and Volume Label
100  * attributes set.  Each can store 13 Unicode characters (16-bits, of
101  * which we only look at the lower 8 for now), broken into (gak) three
102  * sections of the entry (since that's the way the available bits fall out).
103  * In addition, each long filename entry has a sequence number (starting
104  * from 1). The first entry has bit 7 (0x40) set in the sequence number, and
105  * has the maximum value in the sequence. This may seem a bit backwards, and
106  * it gets worse: the first entry stores the last component of
107  * the name. So the directory entry for a file named
108  * "This is a very long filename indeed" might look like this:
109  *
110  * Offset  Sequence      Component         Attributes    Cluster    Size
111  *    0      0x43       "me indeed"          RSHV           0         0
112  *   32      0x02       "y long filena"      RSHV           0         0
113  *   64      0x01       "This is a ver"      RSHV           0         0
114  *   96      ----       "THISIS~1.TXT"       <whatever>  2122       110
115  *
116  * The last entry is for the short filename, which stores actual information
117  * about the file (like type, permissions, cluster, and size). The short name
118  * is also used by non-long-filename aware applications (like Windows 3.X and
119  * DOS). This name is generated by Windows 95 (and now Solaris) from the
120  * long name, and must (of course) be unique within the directory.
121  * Solaris continues to this entry to actually identify the file and its
122  * attributes (filenames only really matter when names are used, like at
123  * lookup/readdir/remove/create/rename time - for general access to the file
124  * they aren't used).
125  *
126  * Long filenames can also be broken by applications that don't
127  * understand them (for example, a Solaris 2.5.1 user could rename
128  * "THISIS~1.TXT" to "test.exe"). This can be detected because each long
129  * filename component has a checksum which is based on the short filename.
130  * After reading the long filename entry, if the checksum doesn't match the
131  * short name that follows, we simply ignore it and use the short name.
132  *
133  * One subtle thing - though long file names are case-sensitive,
134  * searches for them are not.
135  *
136  * Another _very_ subtle thing. The number of characters in the
137  * last long filename chunk (the first entry, with the 0x40 bit set) is
138  * either all the characters (if there is no null, '\0'), or all the
139  * characters up to the null. _However_, if the remaining characters are
140  * null, Norton Disk Doctor and Microsoft ScanDisk will claim
141  * that the filename entry is damaged. The remaining bytes must actually
142  * contain 0xff (discovered with Disk Doctor).
143  *
144  * Some information about long filename support can be found in the
145  * book "Inside Windows 95" by Adrian King.
146  */
147 
148 /*
149  * The number of bytes in each section of the name in a long filename
150  * entry. This is _bytes_, not characters: each character is actually
151  * 16 bits.
152  */
153 #define	PCLF_FIRSTNAMESIZE	10
154 #define	PCLF_SECONDNAMESIZE	12
155 #define	PCLF_THIRDNAMESIZE	4
156 
157 /*
158  * A long filename entry. It must match the 'pcdir' structure in size,
159  * and pcdl_attr must overlap pcd_attr.
160  */
161 struct pcdir_lfn {
162 	uchar_t pcdl_ordinal;	/* lfn order. First is 01, next 02, */
163 				/* last has bit 7 (0x40) set */
164 	uchar_t pcdl_firstfilename[PCLF_FIRSTNAMESIZE];
165 	uchar_t pcdl_attr;
166 	uchar_t pcdl_type;	/* type - always contains 0 for an LFN entry */
167 	uchar_t pcdl_checksum;	/* checksum to validate the LFN entry - */
168 				/* based on the short name */
169 	uchar_t pcdl_secondfilename[PCLF_SECONDNAMESIZE];
170 	pc_cluster16_t pcd_scluster;	/* (not used, always 0) */
171 	uchar_t pcdl_thirdfilename[PCLF_THIRDNAMESIZE];
172 };
173 
174 /*
175  * FAT LFN entries are consecutively numbered downwards, and the last
176  * entry of a LFN chain will have the 0x40 'termination' marker logically
177  * or'ed in. The entry immediately preceeding the short name has number 1,
178  * consecutively increasing. Since the filename length limit on FAT is
179  * 255 unicode characters and every LFN entry contributes 13 characters,
180  * the maximum sequence number is 255/13 + 1 == 20.
181  */
182 #define	PCDL_IS_LAST_LFN(x) ((x->pcdl_ordinal) & 0x40)
183 #define	PCDL_LFN_BITS (PCA_RDONLY | PCA_HIDDEN | PCA_SYSTEM | PCA_LABEL)
184 #define	PCDL_LFN_MASK (PCDL_LFN_BITS | PCA_DIR | PCA_ARCH)
185 #define	PCDL_LFN_VALID_ORD(x)						\
186 	(((((struct pcdir_lfn *)(x))->pcdl_ordinal & ~0x40) > 0) &&	\
187 	((((struct pcdir_lfn *)(x))->pcdl_ordinal & ~0x40) <= 20))
188 #define	PCDL_IS_LFN(x)							\
189 	(enable_long_filenames &&					\
190 	(((x)->pcd_attr & PCDL_LFN_MASK) == PCDL_LFN_BITS) &&		\
191 	PCDL_LFN_VALID_ORD((x)))
192 
193 /*
194  * The first char of the file name has special meaning as follows:
195  */
196 #define	PCD_UNUSED	((char)0x00)	/* entry has never been used */
197 #define	PCD_ERASED	((char)0xE5)	/* entry was erased */
198 
199 /*
200  * File attributes.
201  */
202 #define	PCA_RDONLY	0x01	/* file is read only */
203 #define	PCA_HIDDEN	0x02	/* file is hidden */
204 #define	PCA_SYSTEM	0x04	/* system file */
205 #define	PCA_LABEL	0x08	/* entry contains the volume label */
206 #define	PCA_DIR		0x10	/* subdirectory */
207 #define	PCA_ARCH	0x20	/* file has been modified since last backup */
208 
209 /*
210  * Avoid hidden files unless the private variable is set.
211  * Always avoid the label.
212  */
213 #define	PCA_IS_HIDDEN(fsp, attr) \
214 	((((attr) & PCA_LABEL) == PCA_LABEL) || \
215 	((((fsp)->pcfs_flags & PCFS_HIDDEN) == 0) && \
216 	    ((attr) & (PCA_HIDDEN | PCA_SYSTEM))))
217 
218 #define	PC_NAME_IS_DOT(namep) \
219 	(((namep)[0] == '.') && ((namep)[1] == '\0'))
220 #define	PC_NAME_IS_DOTDOT(namep) \
221 	(((namep)[0] == '.') && ((namep)[1] == '.') && ((namep)[2] == '\0'))
222 #define	PC_SHORTNAME_IS_DOT(namep) \
223 	(((namep)[0] == '.') && ((namep)[1] == ' '))
224 #define	PC_SHORTNAME_IS_DOTDOT(namep) \
225 	(((namep)[0] == '.') && ((namep)[1] == '.') && ((namep)[2] == ' '))
226 /*
227  * slot structure is used by the directory search routine to return
228  * the results of the search.  If the search is successful sl_blkno and
229  * sl_offset reflect the disk address of the entry and sl_ep points to
230  * the actual entry data in buffer sl_bp. sl_flags is set to whether the
231  * entry is dot or dotdot. If the search is unsuccessful sl_blkno and
232  * sl_offset points to an empty directory slot if there are any. Otherwise
233  * it is set to -1.
234  */
235 struct pcslot {
236 	enum {SL_NONE, SL_FOUND, SL_EXTEND} sl_status;	/* slot status */
237 	daddr_t		sl_blkno;	/* disk block number which has entry */
238 	int		sl_offset;	/* offset of entry within block */
239 	struct buf	*sl_bp;		/* buffer containing entry data */
240 	struct pcdir	*sl_ep;		/* pointer to entry data */
241 	int		sl_flags;	/* flags (see below) */
242 };
243 #define	SL_DOT		1	/* entry point to self */
244 #define	SL_DOTDOT	2	/* entry points to parent */
245 
246 /*
247  * A pcfs directory entry. Directory entries are actually variable
248  * length, but this is the maximum size.
249  *
250  * This _must_ match a dirent64 structure in format.
251  * d_name is 512 bytes long to accomodate 256 UTF-16 characters.
252  */
253 struct pc_dirent {
254 	ino64_t		d_ino;		/* "inode number" of entry */
255 	off64_t		d_off;		/* offset of disk directory entry */
256 	unsigned short	d_reclen;	/* length of this record */
257 	char		d_name[PCMAXNAM_UTF16];
258 };
259 
260 /*
261  * Check FAT 8.3 filename characters for validity.
262  * Lacking a kernel iconv, codepage support for short filenames
263  * is not provided.
264  * Short names must be uppercase ASCII (no support for MSDOS
265  * codepages right now, sorry) and may not contain any of
266  * *+=|\[];:",<>.?/ which are explicitly forbidden by the
267  * FAT specifications.
268  */
269 #define	pc_invalchar(c)						\
270 	(((c) >= 'a' && (c) <= 'z') ||				\
271 	(c) == '"' || (c) == '*' || (c) == '+' || (c) == ',' || \
272 	(c) == '.' || (c) == '/' || (c) == ':' || (c) == ';' || \
273 	(c) == '<' || (c) == '=' || (c) == '>' || (c) == '?' || \
274 	(c) == '[' || (c) == '|' || (c) == ']' || (c) == '\\')
275 
276 #define	pc_validchar(c)	(((c) >= ' ' && !((c) & ~0177)) && !pc_invalchar(c))
277 
278 
279 #ifdef _KERNEL
280 
281 /*
282  * macros for converting ASCII to/from upper or lower case.
283  * users may give and get names in lower case, but they are stored on the
284  * disk in upper case to be PCDOS compatible.
285  * These would better come from some shared source in <sys/...> but
286  * there is no such place yet.
287  */
288 #define	toupper(C)	(((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C))
289 #define	tolower(C)	(((C) >= 'A' && (C) <= 'Z') ? (C) - 'A' + 'a' : (C))
290 
291 extern int pc_tvtopct(timestruc_t *, struct pctime *);	/* timeval to pctime */
292 extern void pc_pcttotv(struct pctime *, int64_t *);	/* pctime to timeval */
293 extern int pc_valid_lfn_char(char);		/* valid long filename ch */
294 
295 extern int pc_read_long_fn(struct vnode *, struct uio *,
296     struct pc_dirent *, struct pcdir **, offset_t *, struct buf **);
297 extern int pc_read_short_fn(struct vnode *, struct uio *,
298     struct pc_dirent *, struct pcdir **, offset_t *, struct buf **);
299 extern int pc_match_long_fn(struct pcnode *, char *, struct pcdir **,
300     struct pcslot *, offset_t *);
301 extern int pc_match_short_fn(struct pcnode *, char *,
302     struct pcdir **, struct pcslot *, offset_t *);
303 extern uchar_t pc_checksum_long_fn(char *, char *);
304 extern void set_long_fn_chunk(struct pcdir_lfn *, char *, int);
305 extern int pc_valid_long_fn(char *, int);
306 extern int pc_extract_long_fn(struct pcnode *, char *,
307     struct pcdir **, offset_t *offset, struct buf **);
308 extern int pc_fname_ext_to_name(char *, char *, char *, int);
309 
310 extern pc_cluster32_t pc_getstartcluster(struct pcfs *, struct pcdir *);
311 extern void pc_setstartcluster(struct pcfs *, struct pcdir *, pc_cluster32_t);
312 
313 /*
314  * Private tunables
315  */
316 
317 /*
318  * Use long filenames (Windows 95). Disabling this causes pcfs
319  * to not recognize long filenames at all, which may cause it to
320  * break associations between the short and long names. This is likely
321  * to leave unused long filename entries  in directories (which may make
322  * apparently empty directories unremovable), and would require a fsck_pcfs
323  * to find and fix (or a Windows utility like Norton Disk Doctor or
324  * Microsoft ScanDisk).
325  */
326 extern int enable_long_filenames;	/* default: on */
327 
328 #endif
329 
330 #ifdef	__cplusplus
331 }
332 #endif
333 
334 #endif	/* _SYS_FS_PC_DIR_H */
335