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