Commit dfc209c0064efef5590f608056a48b61a5cac09c
Committed by
Linus Torvalds
1 parent
9183482f5d
fat: Fix ATTR_RO for directory
FAT has the ATTR_RO (read-only) attribute. But on Windows, the ATTR_RO of the directory will be just ignored actually, and is used by only applications as flag. E.g. it's setted for the customized folder by Explorer. http://msdn2.microsoft.com/en-us/library/aa969337.aspx This adds "rodir" option. If user specified it, ATTR_RO is used as read-only flag even if it's the directory. Otherwise, inode->i_mode is not used to hold ATTR_RO (i.e. fat_mode_can_save_ro() returns 0). Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Showing 4 changed files with 43 additions and 12 deletions Inline Diff
Documentation/filesystems/vfat.txt
1 | USING VFAT | 1 | USING VFAT |
2 | ---------------------------------------------------------------------- | 2 | ---------------------------------------------------------------------- |
3 | To use the vfat filesystem, use the filesystem type 'vfat'. i.e. | 3 | To use the vfat filesystem, use the filesystem type 'vfat'. i.e. |
4 | mount -t vfat /dev/fd0 /mnt | 4 | mount -t vfat /dev/fd0 /mnt |
5 | 5 | ||
6 | No special partition formatter is required. mkdosfs will work fine | 6 | No special partition formatter is required. mkdosfs will work fine |
7 | if you want to format from within Linux. | 7 | if you want to format from within Linux. |
8 | 8 | ||
9 | VFAT MOUNT OPTIONS | 9 | VFAT MOUNT OPTIONS |
10 | ---------------------------------------------------------------------- | 10 | ---------------------------------------------------------------------- |
11 | uid=### -- Set the owner of all files on this filesystem. | 11 | uid=### -- Set the owner of all files on this filesystem. |
12 | The default is the uid of current process. | 12 | The default is the uid of current process. |
13 | 13 | ||
14 | gid=### -- Set the group of all files on this filesystem. | 14 | gid=### -- Set the group of all files on this filesystem. |
15 | The default is the gid of current process. | 15 | The default is the gid of current process. |
16 | 16 | ||
17 | umask=### -- The permission mask (for files and directories, see umask(1)). | 17 | umask=### -- The permission mask (for files and directories, see umask(1)). |
18 | The default is the umask of current process. | 18 | The default is the umask of current process. |
19 | 19 | ||
20 | dmask=### -- The permission mask for the directory. | 20 | dmask=### -- The permission mask for the directory. |
21 | The default is the umask of current process. | 21 | The default is the umask of current process. |
22 | 22 | ||
23 | fmask=### -- The permission mask for files. | 23 | fmask=### -- The permission mask for files. |
24 | The default is the umask of current process. | 24 | The default is the umask of current process. |
25 | 25 | ||
26 | allow_utime=### -- This option controls the permission check of mtime/atime. | 26 | allow_utime=### -- This option controls the permission check of mtime/atime. |
27 | 27 | ||
28 | 20 - If current process is in group of file's group ID, | 28 | 20 - If current process is in group of file's group ID, |
29 | you can change timestamp. | 29 | you can change timestamp. |
30 | 2 - Other users can change timestamp. | 30 | 2 - Other users can change timestamp. |
31 | 31 | ||
32 | The default is set from `dmask' option. (If the directory is | 32 | The default is set from `dmask' option. (If the directory is |
33 | writable, utime(2) is also allowed. I.e. ~dmask & 022) | 33 | writable, utime(2) is also allowed. I.e. ~dmask & 022) |
34 | 34 | ||
35 | Normally utime(2) checks current process is owner of | 35 | Normally utime(2) checks current process is owner of |
36 | the file, or it has CAP_FOWNER capability. But FAT | 36 | the file, or it has CAP_FOWNER capability. But FAT |
37 | filesystem doesn't have uid/gid on disk, so normal | 37 | filesystem doesn't have uid/gid on disk, so normal |
38 | check is too unflexible. With this option you can | 38 | check is too unflexible. With this option you can |
39 | relax it. | 39 | relax it. |
40 | 40 | ||
41 | codepage=### -- Sets the codepage number for converting to shortname | 41 | codepage=### -- Sets the codepage number for converting to shortname |
42 | characters on FAT filesystem. | 42 | characters on FAT filesystem. |
43 | By default, FAT_DEFAULT_CODEPAGE setting is used. | 43 | By default, FAT_DEFAULT_CODEPAGE setting is used. |
44 | 44 | ||
45 | iocharset=<name> -- Character set to use for converting between the | 45 | iocharset=<name> -- Character set to use for converting between the |
46 | encoding is used for user visible filename and 16 bit | 46 | encoding is used for user visible filename and 16 bit |
47 | Unicode characters. Long filenames are stored on disk | 47 | Unicode characters. Long filenames are stored on disk |
48 | in Unicode format, but Unix for the most part doesn't | 48 | in Unicode format, but Unix for the most part doesn't |
49 | know how to deal with Unicode. | 49 | know how to deal with Unicode. |
50 | By default, FAT_DEFAULT_IOCHARSET setting is used. | 50 | By default, FAT_DEFAULT_IOCHARSET setting is used. |
51 | 51 | ||
52 | There is also an option of doing UTF-8 translations | 52 | There is also an option of doing UTF-8 translations |
53 | with the utf8 option. | 53 | with the utf8 option. |
54 | 54 | ||
55 | NOTE: "iocharset=utf8" is not recommended. If unsure, | 55 | NOTE: "iocharset=utf8" is not recommended. If unsure, |
56 | you should consider the following option instead. | 56 | you should consider the following option instead. |
57 | 57 | ||
58 | utf8=<bool> -- UTF-8 is the filesystem safe version of Unicode that | 58 | utf8=<bool> -- UTF-8 is the filesystem safe version of Unicode that |
59 | is used by the console. It can be enabled for the | 59 | is used by the console. It can be enabled for the |
60 | filesystem with this option. If 'uni_xlate' gets set, | 60 | filesystem with this option. If 'uni_xlate' gets set, |
61 | UTF-8 gets disabled. | 61 | UTF-8 gets disabled. |
62 | 62 | ||
63 | uni_xlate=<bool> -- Translate unhandled Unicode characters to special | 63 | uni_xlate=<bool> -- Translate unhandled Unicode characters to special |
64 | escaped sequences. This would let you backup and | 64 | escaped sequences. This would let you backup and |
65 | restore filenames that are created with any Unicode | 65 | restore filenames that are created with any Unicode |
66 | characters. Until Linux supports Unicode for real, | 66 | characters. Until Linux supports Unicode for real, |
67 | this gives you an alternative. Without this option, | 67 | this gives you an alternative. Without this option, |
68 | a '?' is used when no translation is possible. The | 68 | a '?' is used when no translation is possible. The |
69 | escape character is ':' because it is otherwise | 69 | escape character is ':' because it is otherwise |
70 | illegal on the vfat filesystem. The escape sequence | 70 | illegal on the vfat filesystem. The escape sequence |
71 | that gets used is ':' and the four digits of hexadecimal | 71 | that gets used is ':' and the four digits of hexadecimal |
72 | unicode. | 72 | unicode. |
73 | 73 | ||
74 | nonumtail=<bool> -- When creating 8.3 aliases, normally the alias will | 74 | nonumtail=<bool> -- When creating 8.3 aliases, normally the alias will |
75 | end in '~1' or tilde followed by some number. If this | 75 | end in '~1' or tilde followed by some number. If this |
76 | option is set, then if the filename is | 76 | option is set, then if the filename is |
77 | "longfilename.txt" and "longfile.txt" does not | 77 | "longfilename.txt" and "longfile.txt" does not |
78 | currently exist in the directory, 'longfile.txt' will | 78 | currently exist in the directory, 'longfile.txt' will |
79 | be the short alias instead of 'longfi~1.txt'. | 79 | be the short alias instead of 'longfi~1.txt'. |
80 | 80 | ||
81 | usefree -- Use the "free clusters" value stored on FSINFO. It'll | 81 | usefree -- Use the "free clusters" value stored on FSINFO. It'll |
82 | be used to determine number of free clusters without | 82 | be used to determine number of free clusters without |
83 | scanning disk. But it's not used by default, because | 83 | scanning disk. But it's not used by default, because |
84 | recent Windows don't update it correctly in some | 84 | recent Windows don't update it correctly in some |
85 | case. If you are sure the "free clusters" on FSINFO is | 85 | case. If you are sure the "free clusters" on FSINFO is |
86 | correct, by this option you can avoid scanning disk. | 86 | correct, by this option you can avoid scanning disk. |
87 | 87 | ||
88 | quiet -- Stops printing certain warning messages. | 88 | quiet -- Stops printing certain warning messages. |
89 | 89 | ||
90 | check=s|r|n -- Case sensitivity checking setting. | 90 | check=s|r|n -- Case sensitivity checking setting. |
91 | s: strict, case sensitive | 91 | s: strict, case sensitive |
92 | r: relaxed, case insensitive | 92 | r: relaxed, case insensitive |
93 | n: normal, default setting, currently case insensitive | 93 | n: normal, default setting, currently case insensitive |
94 | 94 | ||
95 | nocase -- This was deprecated for vfat. Use shortname=win95 instead. | 95 | nocase -- This was deprecated for vfat. Use shortname=win95 instead. |
96 | 96 | ||
97 | shortname=lower|win95|winnt|mixed | 97 | shortname=lower|win95|winnt|mixed |
98 | -- Shortname display/create setting. | 98 | -- Shortname display/create setting. |
99 | lower: convert to lowercase for display, | 99 | lower: convert to lowercase for display, |
100 | emulate the Windows 95 rule for create. | 100 | emulate the Windows 95 rule for create. |
101 | win95: emulate the Windows 95 rule for display/create. | 101 | win95: emulate the Windows 95 rule for display/create. |
102 | winnt: emulate the Windows NT rule for display/create. | 102 | winnt: emulate the Windows NT rule for display/create. |
103 | mixed: emulate the Windows NT rule for display, | 103 | mixed: emulate the Windows NT rule for display, |
104 | emulate the Windows 95 rule for create. | 104 | emulate the Windows 95 rule for create. |
105 | Default setting is `lower'. | 105 | Default setting is `lower'. |
106 | 106 | ||
107 | tz=UTC -- Interpret timestamps as UTC rather than local time. | 107 | tz=UTC -- Interpret timestamps as UTC rather than local time. |
108 | This option disables the conversion of timestamps | 108 | This option disables the conversion of timestamps |
109 | between local time (as used by Windows on FAT) and UTC | 109 | between local time (as used by Windows on FAT) and UTC |
110 | (which Linux uses internally). This is particularly | 110 | (which Linux uses internally). This is particularly |
111 | useful when mounting devices (like digital cameras) | 111 | useful when mounting devices (like digital cameras) |
112 | that are set to UTC in order to avoid the pitfalls of | 112 | that are set to UTC in order to avoid the pitfalls of |
113 | local time. | 113 | local time. |
114 | 114 | ||
115 | showexec -- If set, the execute permission bits of the file will be | 115 | showexec -- If set, the execute permission bits of the file will be |
116 | allowed only if the extension part of the name is .EXE, | 116 | allowed only if the extension part of the name is .EXE, |
117 | .COM, or .BAT. Not set by default. | 117 | .COM, or .BAT. Not set by default. |
118 | 118 | ||
119 | debug -- Can be set, but unused by the current implementation. | 119 | debug -- Can be set, but unused by the current implementation. |
120 | 120 | ||
121 | sys_immutable -- If set, ATTR_SYS attribute on FAT is handled as | 121 | sys_immutable -- If set, ATTR_SYS attribute on FAT is handled as |
122 | IMMUTABLE flag on Linux. Not set by default. | 122 | IMMUTABLE flag on Linux. Not set by default. |
123 | 123 | ||
124 | flush -- If set, the filesystem will try to flush to disk more | 124 | flush -- If set, the filesystem will try to flush to disk more |
125 | early than normal. Not set by default. | 125 | early than normal. Not set by default. |
126 | 126 | ||
127 | rodir -- FAT has the ATTR_RO (read-only) attribute. But on Windows, | ||
128 | the ATTR_RO of the directory will be just ignored actually, | ||
129 | and is used by only applications as flag. E.g. it's setted | ||
130 | for the customized folder. | ||
131 | |||
132 | If you want to use ATTR_RO as read-only flag even for | ||
133 | the directory, set this option. | ||
134 | |||
127 | <bool>: 0,1,yes,no,true,false | 135 | <bool>: 0,1,yes,no,true,false |
128 | 136 | ||
129 | TODO | 137 | TODO |
130 | ---------------------------------------------------------------------- | 138 | ---------------------------------------------------------------------- |
131 | * Need to get rid of the raw scanning stuff. Instead, always use | 139 | * Need to get rid of the raw scanning stuff. Instead, always use |
132 | a get next directory entry approach. The only thing left that uses | 140 | a get next directory entry approach. The only thing left that uses |
133 | raw scanning is the directory renaming code. | 141 | raw scanning is the directory renaming code. |
134 | 142 | ||
135 | 143 | ||
136 | POSSIBLE PROBLEMS | 144 | POSSIBLE PROBLEMS |
137 | ---------------------------------------------------------------------- | 145 | ---------------------------------------------------------------------- |
138 | * vfat_valid_longname does not properly checked reserved names. | 146 | * vfat_valid_longname does not properly checked reserved names. |
139 | * When a volume name is the same as a directory name in the root | 147 | * When a volume name is the same as a directory name in the root |
140 | directory of the filesystem, the directory name sometimes shows | 148 | directory of the filesystem, the directory name sometimes shows |
141 | up as an empty file. | 149 | up as an empty file. |
142 | * autoconv option does not work correctly. | 150 | * autoconv option does not work correctly. |
143 | 151 | ||
144 | BUG REPORTS | 152 | BUG REPORTS |
145 | ---------------------------------------------------------------------- | 153 | ---------------------------------------------------------------------- |
146 | If you have trouble with the VFAT filesystem, mail bug reports to | 154 | If you have trouble with the VFAT filesystem, mail bug reports to |
147 | chaffee@bmrc.cs.berkeley.edu. Please specify the filename | 155 | chaffee@bmrc.cs.berkeley.edu. Please specify the filename |
148 | and the operation that gave you trouble. | 156 | and the operation that gave you trouble. |
149 | 157 | ||
150 | TEST SUITE | 158 | TEST SUITE |
151 | ---------------------------------------------------------------------- | 159 | ---------------------------------------------------------------------- |
152 | If you plan to make any modifications to the vfat filesystem, please | 160 | If you plan to make any modifications to the vfat filesystem, please |
153 | get the test suite that comes with the vfat distribution at | 161 | get the test suite that comes with the vfat distribution at |
154 | 162 | ||
155 | http://bmrc.berkeley.edu/people/chaffee/vfat.html | 163 | http://bmrc.berkeley.edu/people/chaffee/vfat.html |
156 | 164 | ||
157 | This tests quite a few parts of the vfat filesystem and additional | 165 | This tests quite a few parts of the vfat filesystem and additional |
158 | tests for new features or untested features would be appreciated. | 166 | tests for new features or untested features would be appreciated. |
159 | 167 | ||
160 | NOTES ON THE STRUCTURE OF THE VFAT FILESYSTEM | 168 | NOTES ON THE STRUCTURE OF THE VFAT FILESYSTEM |
161 | ---------------------------------------------------------------------- | 169 | ---------------------------------------------------------------------- |
162 | (This documentation was provided by Galen C. Hunt <gchunt@cs.rochester.edu> | 170 | (This documentation was provided by Galen C. Hunt <gchunt@cs.rochester.edu> |
163 | and lightly annotated by Gordon Chaffee). | 171 | and lightly annotated by Gordon Chaffee). |
164 | 172 | ||
165 | This document presents a very rough, technical overview of my | 173 | This document presents a very rough, technical overview of my |
166 | knowledge of the extended FAT file system used in Windows NT 3.5 and | 174 | knowledge of the extended FAT file system used in Windows NT 3.5 and |
167 | Windows 95. I don't guarantee that any of the following is correct, | 175 | Windows 95. I don't guarantee that any of the following is correct, |
168 | but it appears to be so. | 176 | but it appears to be so. |
169 | 177 | ||
170 | The extended FAT file system is almost identical to the FAT | 178 | The extended FAT file system is almost identical to the FAT |
171 | file system used in DOS versions up to and including 6.223410239847 | 179 | file system used in DOS versions up to and including 6.223410239847 |
172 | :-). The significant change has been the addition of long file names. | 180 | :-). The significant change has been the addition of long file names. |
173 | These names support up to 255 characters including spaces and lower | 181 | These names support up to 255 characters including spaces and lower |
174 | case characters as opposed to the traditional 8.3 short names. | 182 | case characters as opposed to the traditional 8.3 short names. |
175 | 183 | ||
176 | Here is the description of the traditional FAT entry in the current | 184 | Here is the description of the traditional FAT entry in the current |
177 | Windows 95 filesystem: | 185 | Windows 95 filesystem: |
178 | 186 | ||
179 | struct directory { // Short 8.3 names | 187 | struct directory { // Short 8.3 names |
180 | unsigned char name[8]; // file name | 188 | unsigned char name[8]; // file name |
181 | unsigned char ext[3]; // file extension | 189 | unsigned char ext[3]; // file extension |
182 | unsigned char attr; // attribute byte | 190 | unsigned char attr; // attribute byte |
183 | unsigned char lcase; // Case for base and extension | 191 | unsigned char lcase; // Case for base and extension |
184 | unsigned char ctime_ms; // Creation time, milliseconds | 192 | unsigned char ctime_ms; // Creation time, milliseconds |
185 | unsigned char ctime[2]; // Creation time | 193 | unsigned char ctime[2]; // Creation time |
186 | unsigned char cdate[2]; // Creation date | 194 | unsigned char cdate[2]; // Creation date |
187 | unsigned char adate[2]; // Last access date | 195 | unsigned char adate[2]; // Last access date |
188 | unsigned char reserved[2]; // reserved values (ignored) | 196 | unsigned char reserved[2]; // reserved values (ignored) |
189 | unsigned char time[2]; // time stamp | 197 | unsigned char time[2]; // time stamp |
190 | unsigned char date[2]; // date stamp | 198 | unsigned char date[2]; // date stamp |
191 | unsigned char start[2]; // starting cluster number | 199 | unsigned char start[2]; // starting cluster number |
192 | unsigned char size[4]; // size of the file | 200 | unsigned char size[4]; // size of the file |
193 | }; | 201 | }; |
194 | 202 | ||
195 | The lcase field specifies if the base and/or the extension of an 8.3 | 203 | The lcase field specifies if the base and/or the extension of an 8.3 |
196 | name should be capitalized. This field does not seem to be used by | 204 | name should be capitalized. This field does not seem to be used by |
197 | Windows 95 but it is used by Windows NT. The case of filenames is not | 205 | Windows 95 but it is used by Windows NT. The case of filenames is not |
198 | completely compatible from Windows NT to Windows 95. It is not completely | 206 | completely compatible from Windows NT to Windows 95. It is not completely |
199 | compatible in the reverse direction, however. Filenames that fit in | 207 | compatible in the reverse direction, however. Filenames that fit in |
200 | the 8.3 namespace and are written on Windows NT to be lowercase will | 208 | the 8.3 namespace and are written on Windows NT to be lowercase will |
201 | show up as uppercase on Windows 95. | 209 | show up as uppercase on Windows 95. |
202 | 210 | ||
203 | Note that the "start" and "size" values are actually little | 211 | Note that the "start" and "size" values are actually little |
204 | endian integer values. The descriptions of the fields in this | 212 | endian integer values. The descriptions of the fields in this |
205 | structure are public knowledge and can be found elsewhere. | 213 | structure are public knowledge and can be found elsewhere. |
206 | 214 | ||
207 | With the extended FAT system, Microsoft has inserted extra | 215 | With the extended FAT system, Microsoft has inserted extra |
208 | directory entries for any files with extended names. (Any name which | 216 | directory entries for any files with extended names. (Any name which |
209 | legally fits within the old 8.3 encoding scheme does not have extra | 217 | legally fits within the old 8.3 encoding scheme does not have extra |
210 | entries.) I call these extra entries slots. Basically, a slot is a | 218 | entries.) I call these extra entries slots. Basically, a slot is a |
211 | specially formatted directory entry which holds up to 13 characters of | 219 | specially formatted directory entry which holds up to 13 characters of |
212 | a file's extended name. Think of slots as additional labeling for the | 220 | a file's extended name. Think of slots as additional labeling for the |
213 | directory entry of the file to which they correspond. Microsoft | 221 | directory entry of the file to which they correspond. Microsoft |
214 | prefers to refer to the 8.3 entry for a file as its alias and the | 222 | prefers to refer to the 8.3 entry for a file as its alias and the |
215 | extended slot directory entries as the file name. | 223 | extended slot directory entries as the file name. |
216 | 224 | ||
217 | The C structure for a slot directory entry follows: | 225 | The C structure for a slot directory entry follows: |
218 | 226 | ||
219 | struct slot { // Up to 13 characters of a long name | 227 | struct slot { // Up to 13 characters of a long name |
220 | unsigned char id; // sequence number for slot | 228 | unsigned char id; // sequence number for slot |
221 | unsigned char name0_4[10]; // first 5 characters in name | 229 | unsigned char name0_4[10]; // first 5 characters in name |
222 | unsigned char attr; // attribute byte | 230 | unsigned char attr; // attribute byte |
223 | unsigned char reserved; // always 0 | 231 | unsigned char reserved; // always 0 |
224 | unsigned char alias_checksum; // checksum for 8.3 alias | 232 | unsigned char alias_checksum; // checksum for 8.3 alias |
225 | unsigned char name5_10[12]; // 6 more characters in name | 233 | unsigned char name5_10[12]; // 6 more characters in name |
226 | unsigned char start[2]; // starting cluster number | 234 | unsigned char start[2]; // starting cluster number |
227 | unsigned char name11_12[4]; // last 2 characters in name | 235 | unsigned char name11_12[4]; // last 2 characters in name |
228 | }; | 236 | }; |
229 | 237 | ||
230 | If the layout of the slots looks a little odd, it's only | 238 | If the layout of the slots looks a little odd, it's only |
231 | because of Microsoft's efforts to maintain compatibility with old | 239 | because of Microsoft's efforts to maintain compatibility with old |
232 | software. The slots must be disguised to prevent old software from | 240 | software. The slots must be disguised to prevent old software from |
233 | panicking. To this end, a number of measures are taken: | 241 | panicking. To this end, a number of measures are taken: |
234 | 242 | ||
235 | 1) The attribute byte for a slot directory entry is always set | 243 | 1) The attribute byte for a slot directory entry is always set |
236 | to 0x0f. This corresponds to an old directory entry with | 244 | to 0x0f. This corresponds to an old directory entry with |
237 | attributes of "hidden", "system", "read-only", and "volume | 245 | attributes of "hidden", "system", "read-only", and "volume |
238 | label". Most old software will ignore any directory | 246 | label". Most old software will ignore any directory |
239 | entries with the "volume label" bit set. Real volume label | 247 | entries with the "volume label" bit set. Real volume label |
240 | entries don't have the other three bits set. | 248 | entries don't have the other three bits set. |
241 | 249 | ||
242 | 2) The starting cluster is always set to 0, an impossible | 250 | 2) The starting cluster is always set to 0, an impossible |
243 | value for a DOS file. | 251 | value for a DOS file. |
244 | 252 | ||
245 | Because the extended FAT system is backward compatible, it is | 253 | Because the extended FAT system is backward compatible, it is |
246 | possible for old software to modify directory entries. Measures must | 254 | possible for old software to modify directory entries. Measures must |
247 | be taken to ensure the validity of slots. An extended FAT system can | 255 | be taken to ensure the validity of slots. An extended FAT system can |
248 | verify that a slot does in fact belong to an 8.3 directory entry by | 256 | verify that a slot does in fact belong to an 8.3 directory entry by |
249 | the following: | 257 | the following: |
250 | 258 | ||
251 | 1) Positioning. Slots for a file always immediately proceed | 259 | 1) Positioning. Slots for a file always immediately proceed |
252 | their corresponding 8.3 directory entry. In addition, each | 260 | their corresponding 8.3 directory entry. In addition, each |
253 | slot has an id which marks its order in the extended file | 261 | slot has an id which marks its order in the extended file |
254 | name. Here is a very abbreviated view of an 8.3 directory | 262 | name. Here is a very abbreviated view of an 8.3 directory |
255 | entry and its corresponding long name slots for the file | 263 | entry and its corresponding long name slots for the file |
256 | "My Big File.Extension which is long": | 264 | "My Big File.Extension which is long": |
257 | 265 | ||
258 | <proceeding files...> | 266 | <proceeding files...> |
259 | <slot #3, id = 0x43, characters = "h is long"> | 267 | <slot #3, id = 0x43, characters = "h is long"> |
260 | <slot #2, id = 0x02, characters = "xtension whic"> | 268 | <slot #2, id = 0x02, characters = "xtension whic"> |
261 | <slot #1, id = 0x01, characters = "My Big File.E"> | 269 | <slot #1, id = 0x01, characters = "My Big File.E"> |
262 | <directory entry, name = "MYBIGFIL.EXT"> | 270 | <directory entry, name = "MYBIGFIL.EXT"> |
263 | 271 | ||
264 | Note that the slots are stored from last to first. Slots | 272 | Note that the slots are stored from last to first. Slots |
265 | are numbered from 1 to N. The Nth slot is or'ed with 0x40 | 273 | are numbered from 1 to N. The Nth slot is or'ed with 0x40 |
266 | to mark it as the last one. | 274 | to mark it as the last one. |
267 | 275 | ||
268 | 2) Checksum. Each slot has an "alias_checksum" value. The | 276 | 2) Checksum. Each slot has an "alias_checksum" value. The |
269 | checksum is calculated from the 8.3 name using the | 277 | checksum is calculated from the 8.3 name using the |
270 | following algorithm: | 278 | following algorithm: |
271 | 279 | ||
272 | for (sum = i = 0; i < 11; i++) { | 280 | for (sum = i = 0; i < 11; i++) { |
273 | sum = (((sum&1)<<7)|((sum&0xfe)>>1)) + name[i] | 281 | sum = (((sum&1)<<7)|((sum&0xfe)>>1)) + name[i] |
274 | } | 282 | } |
275 | 283 | ||
276 | 3) If there is free space in the final slot, a Unicode NULL (0x0000) | 284 | 3) If there is free space in the final slot, a Unicode NULL (0x0000) |
277 | is stored after the final character. After that, all unused | 285 | is stored after the final character. After that, all unused |
278 | characters in the final slot are set to Unicode 0xFFFF. | 286 | characters in the final slot are set to Unicode 0xFFFF. |
279 | 287 | ||
280 | Finally, note that the extended name is stored in Unicode. Each Unicode | 288 | Finally, note that the extended name is stored in Unicode. Each Unicode |
281 | character takes two bytes. | 289 | character takes two bytes. |
282 | 290 |
fs/fat/fat.h
1 | #ifndef _FAT_H | 1 | #ifndef _FAT_H |
2 | #define _FAT_H | 2 | #define _FAT_H |
3 | 3 | ||
4 | #include <linux/buffer_head.h> | 4 | #include <linux/buffer_head.h> |
5 | #include <linux/string.h> | 5 | #include <linux/string.h> |
6 | #include <linux/nls.h> | 6 | #include <linux/nls.h> |
7 | #include <linux/fs.h> | 7 | #include <linux/fs.h> |
8 | #include <linux/mutex.h> | 8 | #include <linux/mutex.h> |
9 | #include <linux/msdos_fs.h> | 9 | #include <linux/msdos_fs.h> |
10 | 10 | ||
11 | /* | 11 | /* |
12 | * vfat shortname flags | 12 | * vfat shortname flags |
13 | */ | 13 | */ |
14 | #define VFAT_SFN_DISPLAY_LOWER 0x0001 /* convert to lowercase for display */ | 14 | #define VFAT_SFN_DISPLAY_LOWER 0x0001 /* convert to lowercase for display */ |
15 | #define VFAT_SFN_DISPLAY_WIN95 0x0002 /* emulate win95 rule for display */ | 15 | #define VFAT_SFN_DISPLAY_WIN95 0x0002 /* emulate win95 rule for display */ |
16 | #define VFAT_SFN_DISPLAY_WINNT 0x0004 /* emulate winnt rule for display */ | 16 | #define VFAT_SFN_DISPLAY_WINNT 0x0004 /* emulate winnt rule for display */ |
17 | #define VFAT_SFN_CREATE_WIN95 0x0100 /* emulate win95 rule for create */ | 17 | #define VFAT_SFN_CREATE_WIN95 0x0100 /* emulate win95 rule for create */ |
18 | #define VFAT_SFN_CREATE_WINNT 0x0200 /* emulate winnt rule for create */ | 18 | #define VFAT_SFN_CREATE_WINNT 0x0200 /* emulate winnt rule for create */ |
19 | 19 | ||
20 | struct fat_mount_options { | 20 | struct fat_mount_options { |
21 | uid_t fs_uid; | 21 | uid_t fs_uid; |
22 | gid_t fs_gid; | 22 | gid_t fs_gid; |
23 | unsigned short fs_fmask; | 23 | unsigned short fs_fmask; |
24 | unsigned short fs_dmask; | 24 | unsigned short fs_dmask; |
25 | unsigned short codepage; /* Codepage for shortname conversions */ | 25 | unsigned short codepage; /* Codepage for shortname conversions */ |
26 | char *iocharset; /* Charset used for filename input/display */ | 26 | char *iocharset; /* Charset used for filename input/display */ |
27 | unsigned short shortname; /* flags for shortname display/create rule */ | 27 | unsigned short shortname; /* flags for shortname display/create rule */ |
28 | unsigned char name_check; /* r = relaxed, n = normal, s = strict */ | 28 | unsigned char name_check; /* r = relaxed, n = normal, s = strict */ |
29 | unsigned short allow_utime;/* permission for setting the [am]time */ | 29 | unsigned short allow_utime;/* permission for setting the [am]time */ |
30 | unsigned quiet:1, /* set = fake successful chmods and chowns */ | 30 | unsigned quiet:1, /* set = fake successful chmods and chowns */ |
31 | showexec:1, /* set = only set x bit for com/exe/bat */ | 31 | showexec:1, /* set = only set x bit for com/exe/bat */ |
32 | sys_immutable:1, /* set = system files are immutable */ | 32 | sys_immutable:1, /* set = system files are immutable */ |
33 | dotsOK:1, /* set = hidden and system files are named '.filename' */ | 33 | dotsOK:1, /* set = hidden and system files are named '.filename' */ |
34 | isvfat:1, /* 0=no vfat long filename support, 1=vfat support */ | 34 | isvfat:1, /* 0=no vfat long filename support, 1=vfat support */ |
35 | utf8:1, /* Use of UTF-8 character set (Default) */ | 35 | utf8:1, /* Use of UTF-8 character set (Default) */ |
36 | unicode_xlate:1, /* create escape sequences for unhandled Unicode */ | 36 | unicode_xlate:1, /* create escape sequences for unhandled Unicode */ |
37 | numtail:1, /* Does first alias have a numeric '~1' type tail? */ | 37 | numtail:1, /* Does first alias have a numeric '~1' type tail? */ |
38 | flush:1, /* write things quickly */ | 38 | flush:1, /* write things quickly */ |
39 | nocase:1, /* Does this need case conversion? 0=need case conversion*/ | 39 | nocase:1, /* Does this need case conversion? 0=need case conversion*/ |
40 | usefree:1, /* Use free_clusters for FAT32 */ | 40 | usefree:1, /* Use free_clusters for FAT32 */ |
41 | tz_utc:1; /* Filesystem timestamps are in UTC */ | 41 | tz_utc:1, /* Filesystem timestamps are in UTC */ |
42 | rodir:1; /* allow ATTR_RO for directory */ | ||
42 | }; | 43 | }; |
43 | 44 | ||
44 | #define FAT_HASH_BITS 8 | 45 | #define FAT_HASH_BITS 8 |
45 | #define FAT_HASH_SIZE (1UL << FAT_HASH_BITS) | 46 | #define FAT_HASH_SIZE (1UL << FAT_HASH_BITS) |
46 | 47 | ||
47 | /* | 48 | /* |
48 | * MS-DOS file system in-core superblock data | 49 | * MS-DOS file system in-core superblock data |
49 | */ | 50 | */ |
50 | struct msdos_sb_info { | 51 | struct msdos_sb_info { |
51 | unsigned short sec_per_clus; /* sectors/cluster */ | 52 | unsigned short sec_per_clus; /* sectors/cluster */ |
52 | unsigned short cluster_bits; /* log2(cluster_size) */ | 53 | unsigned short cluster_bits; /* log2(cluster_size) */ |
53 | unsigned int cluster_size; /* cluster size */ | 54 | unsigned int cluster_size; /* cluster size */ |
54 | unsigned char fats,fat_bits; /* number of FATs, FAT bits (12 or 16) */ | 55 | unsigned char fats,fat_bits; /* number of FATs, FAT bits (12 or 16) */ |
55 | unsigned short fat_start; | 56 | unsigned short fat_start; |
56 | unsigned long fat_length; /* FAT start & length (sec.) */ | 57 | unsigned long fat_length; /* FAT start & length (sec.) */ |
57 | unsigned long dir_start; | 58 | unsigned long dir_start; |
58 | unsigned short dir_entries; /* root dir start & entries */ | 59 | unsigned short dir_entries; /* root dir start & entries */ |
59 | unsigned long data_start; /* first data sector */ | 60 | unsigned long data_start; /* first data sector */ |
60 | unsigned long max_cluster; /* maximum cluster number */ | 61 | unsigned long max_cluster; /* maximum cluster number */ |
61 | unsigned long root_cluster; /* first cluster of the root directory */ | 62 | unsigned long root_cluster; /* first cluster of the root directory */ |
62 | unsigned long fsinfo_sector; /* sector number of FAT32 fsinfo */ | 63 | unsigned long fsinfo_sector; /* sector number of FAT32 fsinfo */ |
63 | struct mutex fat_lock; | 64 | struct mutex fat_lock; |
64 | unsigned int prev_free; /* previously allocated cluster number */ | 65 | unsigned int prev_free; /* previously allocated cluster number */ |
65 | unsigned int free_clusters; /* -1 if undefined */ | 66 | unsigned int free_clusters; /* -1 if undefined */ |
66 | unsigned int free_clus_valid; /* is free_clusters valid? */ | 67 | unsigned int free_clus_valid; /* is free_clusters valid? */ |
67 | struct fat_mount_options options; | 68 | struct fat_mount_options options; |
68 | struct nls_table *nls_disk; /* Codepage used on disk */ | 69 | struct nls_table *nls_disk; /* Codepage used on disk */ |
69 | struct nls_table *nls_io; /* Charset used for input and display */ | 70 | struct nls_table *nls_io; /* Charset used for input and display */ |
70 | const void *dir_ops; /* Opaque; default directory operations */ | 71 | const void *dir_ops; /* Opaque; default directory operations */ |
71 | int dir_per_block; /* dir entries per block */ | 72 | int dir_per_block; /* dir entries per block */ |
72 | int dir_per_block_bits; /* log2(dir_per_block) */ | 73 | int dir_per_block_bits; /* log2(dir_per_block) */ |
73 | 74 | ||
74 | int fatent_shift; | 75 | int fatent_shift; |
75 | struct fatent_operations *fatent_ops; | 76 | struct fatent_operations *fatent_ops; |
76 | 77 | ||
77 | spinlock_t inode_hash_lock; | 78 | spinlock_t inode_hash_lock; |
78 | struct hlist_head inode_hashtable[FAT_HASH_SIZE]; | 79 | struct hlist_head inode_hashtable[FAT_HASH_SIZE]; |
79 | }; | 80 | }; |
80 | 81 | ||
81 | #define FAT_CACHE_VALID 0 /* special case for valid cache */ | 82 | #define FAT_CACHE_VALID 0 /* special case for valid cache */ |
82 | 83 | ||
83 | /* | 84 | /* |
84 | * MS-DOS file system inode data in memory | 85 | * MS-DOS file system inode data in memory |
85 | */ | 86 | */ |
86 | struct msdos_inode_info { | 87 | struct msdos_inode_info { |
87 | spinlock_t cache_lru_lock; | 88 | spinlock_t cache_lru_lock; |
88 | struct list_head cache_lru; | 89 | struct list_head cache_lru; |
89 | int nr_caches; | 90 | int nr_caches; |
90 | /* for avoiding the race between fat_free() and fat_get_cluster() */ | 91 | /* for avoiding the race between fat_free() and fat_get_cluster() */ |
91 | unsigned int cache_valid_id; | 92 | unsigned int cache_valid_id; |
92 | 93 | ||
93 | loff_t mmu_private; | 94 | loff_t mmu_private; |
94 | int i_start; /* first cluster or 0 */ | 95 | int i_start; /* first cluster or 0 */ |
95 | int i_logstart; /* logical first cluster */ | 96 | int i_logstart; /* logical first cluster */ |
96 | int i_attrs; /* unused attribute bits */ | 97 | int i_attrs; /* unused attribute bits */ |
97 | loff_t i_pos; /* on-disk position of directory entry or 0 */ | 98 | loff_t i_pos; /* on-disk position of directory entry or 0 */ |
98 | struct hlist_node i_fat_hash; /* hash by i_location */ | 99 | struct hlist_node i_fat_hash; /* hash by i_location */ |
99 | struct inode vfs_inode; | 100 | struct inode vfs_inode; |
100 | }; | 101 | }; |
101 | 102 | ||
102 | struct fat_slot_info { | 103 | struct fat_slot_info { |
103 | loff_t i_pos; /* on-disk position of directory entry */ | 104 | loff_t i_pos; /* on-disk position of directory entry */ |
104 | loff_t slot_off; /* offset for slot or de start */ | 105 | loff_t slot_off; /* offset for slot or de start */ |
105 | int nr_slots; /* number of slots + 1(de) in filename */ | 106 | int nr_slots; /* number of slots + 1(de) in filename */ |
106 | struct msdos_dir_entry *de; | 107 | struct msdos_dir_entry *de; |
107 | struct buffer_head *bh; | 108 | struct buffer_head *bh; |
108 | }; | 109 | }; |
109 | 110 | ||
110 | static inline struct msdos_sb_info *MSDOS_SB(struct super_block *sb) | 111 | static inline struct msdos_sb_info *MSDOS_SB(struct super_block *sb) |
111 | { | 112 | { |
112 | return sb->s_fs_info; | 113 | return sb->s_fs_info; |
113 | } | 114 | } |
114 | 115 | ||
115 | static inline struct msdos_inode_info *MSDOS_I(struct inode *inode) | 116 | static inline struct msdos_inode_info *MSDOS_I(struct inode *inode) |
116 | { | 117 | { |
117 | return container_of(inode, struct msdos_inode_info, vfs_inode); | 118 | return container_of(inode, struct msdos_inode_info, vfs_inode); |
118 | } | 119 | } |
119 | 120 | ||
120 | /* | 121 | /* |
121 | * If ->i_mode can't hold S_IWUGO (i.e. ATTR_RO), we use ->i_attrs to | 122 | * If ->i_mode can't hold S_IWUGO (i.e. ATTR_RO), we use ->i_attrs to |
122 | * save ATTR_RO instead of ->i_mode. | 123 | * save ATTR_RO instead of ->i_mode. |
124 | * | ||
125 | * If it's directory and !sbi->options.rodir, ATTR_RO isn't read-only | ||
126 | * bit, it's just used as flag for app. | ||
123 | */ | 127 | */ |
124 | static inline int fat_mode_can_hold_ro(struct inode *inode) | 128 | static inline int fat_mode_can_hold_ro(struct inode *inode) |
125 | { | 129 | { |
126 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); | 130 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); |
127 | mode_t mask; | 131 | mode_t mask; |
128 | 132 | ||
129 | if (S_ISDIR(inode->i_mode)) | 133 | if (S_ISDIR(inode->i_mode)) { |
134 | if (!sbi->options.rodir) | ||
135 | return 0; | ||
130 | mask = ~sbi->options.fs_dmask; | 136 | mask = ~sbi->options.fs_dmask; |
131 | else | 137 | } else |
132 | mask = ~sbi->options.fs_fmask; | 138 | mask = ~sbi->options.fs_fmask; |
133 | 139 | ||
134 | if (!(mask & S_IWUGO)) | 140 | if (!(mask & S_IWUGO)) |
135 | return 0; | 141 | return 0; |
136 | return 1; | 142 | return 1; |
137 | } | 143 | } |
138 | 144 | ||
139 | /* Convert attribute bits and a mask to the UNIX mode. */ | 145 | /* Convert attribute bits and a mask to the UNIX mode. */ |
140 | static inline mode_t fat_make_mode(struct msdos_sb_info *sbi, | 146 | static inline mode_t fat_make_mode(struct msdos_sb_info *sbi, |
141 | u8 attrs, mode_t mode) | 147 | u8 attrs, mode_t mode) |
142 | { | 148 | { |
143 | if (attrs & ATTR_RO) | 149 | if (attrs & ATTR_RO && !((attrs & ATTR_DIR) && !sbi->options.rodir)) |
144 | mode &= ~S_IWUGO; | 150 | mode &= ~S_IWUGO; |
145 | 151 | ||
146 | if (attrs & ATTR_DIR) | 152 | if (attrs & ATTR_DIR) |
147 | return (mode & ~sbi->options.fs_dmask) | S_IFDIR; | 153 | return (mode & ~sbi->options.fs_dmask) | S_IFDIR; |
148 | else | 154 | else |
149 | return (mode & ~sbi->options.fs_fmask) | S_IFREG; | 155 | return (mode & ~sbi->options.fs_fmask) | S_IFREG; |
150 | } | 156 | } |
151 | 157 | ||
152 | /* Return the FAT attribute byte for this inode */ | 158 | /* Return the FAT attribute byte for this inode */ |
153 | static inline u8 fat_make_attrs(struct inode *inode) | 159 | static inline u8 fat_make_attrs(struct inode *inode) |
154 | { | 160 | { |
155 | u8 attrs = MSDOS_I(inode)->i_attrs; | 161 | u8 attrs = MSDOS_I(inode)->i_attrs; |
156 | if (S_ISDIR(inode->i_mode)) | 162 | if (S_ISDIR(inode->i_mode)) |
157 | attrs |= ATTR_DIR; | 163 | attrs |= ATTR_DIR; |
158 | if (fat_mode_can_hold_ro(inode) && !(inode->i_mode & S_IWUGO)) | 164 | if (fat_mode_can_hold_ro(inode) && !(inode->i_mode & S_IWUGO)) |
159 | attrs |= ATTR_RO; | 165 | attrs |= ATTR_RO; |
160 | return attrs; | 166 | return attrs; |
161 | } | 167 | } |
162 | 168 | ||
163 | static inline void fat_save_attrs(struct inode *inode, u8 attrs) | 169 | static inline void fat_save_attrs(struct inode *inode, u8 attrs) |
164 | { | 170 | { |
165 | if (fat_mode_can_hold_ro(inode)) | 171 | if (fat_mode_can_hold_ro(inode)) |
166 | MSDOS_I(inode)->i_attrs = attrs & ATTR_UNUSED; | 172 | MSDOS_I(inode)->i_attrs = attrs & ATTR_UNUSED; |
167 | else | 173 | else |
168 | MSDOS_I(inode)->i_attrs = attrs & (ATTR_UNUSED | ATTR_RO); | 174 | MSDOS_I(inode)->i_attrs = attrs & (ATTR_UNUSED | ATTR_RO); |
169 | } | 175 | } |
170 | 176 | ||
171 | static inline unsigned char fat_checksum(const __u8 *name) | 177 | static inline unsigned char fat_checksum(const __u8 *name) |
172 | { | 178 | { |
173 | unsigned char s = name[0]; | 179 | unsigned char s = name[0]; |
174 | s = (s<<7) + (s>>1) + name[1]; s = (s<<7) + (s>>1) + name[2]; | 180 | s = (s<<7) + (s>>1) + name[1]; s = (s<<7) + (s>>1) + name[2]; |
175 | s = (s<<7) + (s>>1) + name[3]; s = (s<<7) + (s>>1) + name[4]; | 181 | s = (s<<7) + (s>>1) + name[3]; s = (s<<7) + (s>>1) + name[4]; |
176 | s = (s<<7) + (s>>1) + name[5]; s = (s<<7) + (s>>1) + name[6]; | 182 | s = (s<<7) + (s>>1) + name[5]; s = (s<<7) + (s>>1) + name[6]; |
177 | s = (s<<7) + (s>>1) + name[7]; s = (s<<7) + (s>>1) + name[8]; | 183 | s = (s<<7) + (s>>1) + name[7]; s = (s<<7) + (s>>1) + name[8]; |
178 | s = (s<<7) + (s>>1) + name[9]; s = (s<<7) + (s>>1) + name[10]; | 184 | s = (s<<7) + (s>>1) + name[9]; s = (s<<7) + (s>>1) + name[10]; |
179 | return s; | 185 | return s; |
180 | } | 186 | } |
181 | 187 | ||
182 | static inline sector_t fat_clus_to_blknr(struct msdos_sb_info *sbi, int clus) | 188 | static inline sector_t fat_clus_to_blknr(struct msdos_sb_info *sbi, int clus) |
183 | { | 189 | { |
184 | return ((sector_t)clus - FAT_START_ENT) * sbi->sec_per_clus | 190 | return ((sector_t)clus - FAT_START_ENT) * sbi->sec_per_clus |
185 | + sbi->data_start; | 191 | + sbi->data_start; |
186 | } | 192 | } |
187 | 193 | ||
188 | static inline void fat16_towchar(wchar_t *dst, const __u8 *src, size_t len) | 194 | static inline void fat16_towchar(wchar_t *dst, const __u8 *src, size_t len) |
189 | { | 195 | { |
190 | #ifdef __BIG_ENDIAN | 196 | #ifdef __BIG_ENDIAN |
191 | while (len--) { | 197 | while (len--) { |
192 | *dst++ = src[0] | (src[1] << 8); | 198 | *dst++ = src[0] | (src[1] << 8); |
193 | src += 2; | 199 | src += 2; |
194 | } | 200 | } |
195 | #else | 201 | #else |
196 | memcpy(dst, src, len * 2); | 202 | memcpy(dst, src, len * 2); |
197 | #endif | 203 | #endif |
198 | } | 204 | } |
199 | 205 | ||
200 | static inline void fatwchar_to16(__u8 *dst, const wchar_t *src, size_t len) | 206 | static inline void fatwchar_to16(__u8 *dst, const wchar_t *src, size_t len) |
201 | { | 207 | { |
202 | #ifdef __BIG_ENDIAN | 208 | #ifdef __BIG_ENDIAN |
203 | while (len--) { | 209 | while (len--) { |
204 | dst[0] = *src & 0x00FF; | 210 | dst[0] = *src & 0x00FF; |
205 | dst[1] = (*src & 0xFF00) >> 8; | 211 | dst[1] = (*src & 0xFF00) >> 8; |
206 | dst += 2; | 212 | dst += 2; |
207 | src++; | 213 | src++; |
208 | } | 214 | } |
209 | #else | 215 | #else |
210 | memcpy(dst, src, len * 2); | 216 | memcpy(dst, src, len * 2); |
211 | #endif | 217 | #endif |
212 | } | 218 | } |
213 | 219 | ||
214 | /* fat/cache.c */ | 220 | /* fat/cache.c */ |
215 | extern void fat_cache_inval_inode(struct inode *inode); | 221 | extern void fat_cache_inval_inode(struct inode *inode); |
216 | extern int fat_get_cluster(struct inode *inode, int cluster, | 222 | extern int fat_get_cluster(struct inode *inode, int cluster, |
217 | int *fclus, int *dclus); | 223 | int *fclus, int *dclus); |
218 | extern int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys, | 224 | extern int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys, |
219 | unsigned long *mapped_blocks); | 225 | unsigned long *mapped_blocks); |
220 | 226 | ||
221 | /* fat/dir.c */ | 227 | /* fat/dir.c */ |
222 | extern const struct file_operations fat_dir_operations; | 228 | extern const struct file_operations fat_dir_operations; |
223 | extern int fat_search_long(struct inode *inode, const unsigned char *name, | 229 | extern int fat_search_long(struct inode *inode, const unsigned char *name, |
224 | int name_len, struct fat_slot_info *sinfo); | 230 | int name_len, struct fat_slot_info *sinfo); |
225 | extern int fat_dir_empty(struct inode *dir); | 231 | extern int fat_dir_empty(struct inode *dir); |
226 | extern int fat_subdirs(struct inode *dir); | 232 | extern int fat_subdirs(struct inode *dir); |
227 | extern int fat_scan(struct inode *dir, const unsigned char *name, | 233 | extern int fat_scan(struct inode *dir, const unsigned char *name, |
228 | struct fat_slot_info *sinfo); | 234 | struct fat_slot_info *sinfo); |
229 | extern int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh, | 235 | extern int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh, |
230 | struct msdos_dir_entry **de, loff_t *i_pos); | 236 | struct msdos_dir_entry **de, loff_t *i_pos); |
231 | extern int fat_alloc_new_dir(struct inode *dir, struct timespec *ts); | 237 | extern int fat_alloc_new_dir(struct inode *dir, struct timespec *ts); |
232 | extern int fat_add_entries(struct inode *dir, void *slots, int nr_slots, | 238 | extern int fat_add_entries(struct inode *dir, void *slots, int nr_slots, |
233 | struct fat_slot_info *sinfo); | 239 | struct fat_slot_info *sinfo); |
234 | extern int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo); | 240 | extern int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo); |
235 | 241 | ||
236 | /* fat/fatent.c */ | 242 | /* fat/fatent.c */ |
237 | struct fat_entry { | 243 | struct fat_entry { |
238 | int entry; | 244 | int entry; |
239 | union { | 245 | union { |
240 | u8 *ent12_p[2]; | 246 | u8 *ent12_p[2]; |
241 | __le16 *ent16_p; | 247 | __le16 *ent16_p; |
242 | __le32 *ent32_p; | 248 | __le32 *ent32_p; |
243 | } u; | 249 | } u; |
244 | int nr_bhs; | 250 | int nr_bhs; |
245 | struct buffer_head *bhs[2]; | 251 | struct buffer_head *bhs[2]; |
246 | }; | 252 | }; |
247 | 253 | ||
248 | static inline void fatent_init(struct fat_entry *fatent) | 254 | static inline void fatent_init(struct fat_entry *fatent) |
249 | { | 255 | { |
250 | fatent->nr_bhs = 0; | 256 | fatent->nr_bhs = 0; |
251 | fatent->entry = 0; | 257 | fatent->entry = 0; |
252 | fatent->u.ent32_p = NULL; | 258 | fatent->u.ent32_p = NULL; |
253 | fatent->bhs[0] = fatent->bhs[1] = NULL; | 259 | fatent->bhs[0] = fatent->bhs[1] = NULL; |
254 | } | 260 | } |
255 | 261 | ||
256 | static inline void fatent_set_entry(struct fat_entry *fatent, int entry) | 262 | static inline void fatent_set_entry(struct fat_entry *fatent, int entry) |
257 | { | 263 | { |
258 | fatent->entry = entry; | 264 | fatent->entry = entry; |
259 | fatent->u.ent32_p = NULL; | 265 | fatent->u.ent32_p = NULL; |
260 | } | 266 | } |
261 | 267 | ||
262 | static inline void fatent_brelse(struct fat_entry *fatent) | 268 | static inline void fatent_brelse(struct fat_entry *fatent) |
263 | { | 269 | { |
264 | int i; | 270 | int i; |
265 | fatent->u.ent32_p = NULL; | 271 | fatent->u.ent32_p = NULL; |
266 | for (i = 0; i < fatent->nr_bhs; i++) | 272 | for (i = 0; i < fatent->nr_bhs; i++) |
267 | brelse(fatent->bhs[i]); | 273 | brelse(fatent->bhs[i]); |
268 | fatent->nr_bhs = 0; | 274 | fatent->nr_bhs = 0; |
269 | fatent->bhs[0] = fatent->bhs[1] = NULL; | 275 | fatent->bhs[0] = fatent->bhs[1] = NULL; |
270 | } | 276 | } |
271 | 277 | ||
272 | extern void fat_ent_access_init(struct super_block *sb); | 278 | extern void fat_ent_access_init(struct super_block *sb); |
273 | extern int fat_ent_read(struct inode *inode, struct fat_entry *fatent, | 279 | extern int fat_ent_read(struct inode *inode, struct fat_entry *fatent, |
274 | int entry); | 280 | int entry); |
275 | extern int fat_ent_write(struct inode *inode, struct fat_entry *fatent, | 281 | extern int fat_ent_write(struct inode *inode, struct fat_entry *fatent, |
276 | int new, int wait); | 282 | int new, int wait); |
277 | extern int fat_alloc_clusters(struct inode *inode, int *cluster, | 283 | extern int fat_alloc_clusters(struct inode *inode, int *cluster, |
278 | int nr_cluster); | 284 | int nr_cluster); |
279 | extern int fat_free_clusters(struct inode *inode, int cluster); | 285 | extern int fat_free_clusters(struct inode *inode, int cluster); |
280 | extern int fat_count_free_clusters(struct super_block *sb); | 286 | extern int fat_count_free_clusters(struct super_block *sb); |
281 | 287 | ||
282 | /* fat/file.c */ | 288 | /* fat/file.c */ |
283 | extern int fat_generic_ioctl(struct inode *inode, struct file *filp, | 289 | extern int fat_generic_ioctl(struct inode *inode, struct file *filp, |
284 | unsigned int cmd, unsigned long arg); | 290 | unsigned int cmd, unsigned long arg); |
285 | extern const struct file_operations fat_file_operations; | 291 | extern const struct file_operations fat_file_operations; |
286 | extern const struct inode_operations fat_file_inode_operations; | 292 | extern const struct inode_operations fat_file_inode_operations; |
287 | extern int fat_setattr(struct dentry * dentry, struct iattr * attr); | 293 | extern int fat_setattr(struct dentry * dentry, struct iattr * attr); |
288 | extern void fat_truncate(struct inode *inode); | 294 | extern void fat_truncate(struct inode *inode); |
289 | extern int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, | 295 | extern int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, |
290 | struct kstat *stat); | 296 | struct kstat *stat); |
291 | 297 | ||
292 | /* fat/inode.c */ | 298 | /* fat/inode.c */ |
293 | extern void fat_attach(struct inode *inode, loff_t i_pos); | 299 | extern void fat_attach(struct inode *inode, loff_t i_pos); |
294 | extern void fat_detach(struct inode *inode); | 300 | extern void fat_detach(struct inode *inode); |
295 | extern struct inode *fat_iget(struct super_block *sb, loff_t i_pos); | 301 | extern struct inode *fat_iget(struct super_block *sb, loff_t i_pos); |
296 | extern struct inode *fat_build_inode(struct super_block *sb, | 302 | extern struct inode *fat_build_inode(struct super_block *sb, |
297 | struct msdos_dir_entry *de, loff_t i_pos); | 303 | struct msdos_dir_entry *de, loff_t i_pos); |
298 | extern int fat_sync_inode(struct inode *inode); | 304 | extern int fat_sync_inode(struct inode *inode); |
299 | extern int fat_fill_super(struct super_block *sb, void *data, int silent, | 305 | extern int fat_fill_super(struct super_block *sb, void *data, int silent, |
300 | const struct inode_operations *fs_dir_inode_ops, int isvfat); | 306 | const struct inode_operations *fs_dir_inode_ops, int isvfat); |
301 | 307 | ||
302 | extern int fat_flush_inodes(struct super_block *sb, struct inode *i1, | 308 | extern int fat_flush_inodes(struct super_block *sb, struct inode *i1, |
303 | struct inode *i2); | 309 | struct inode *i2); |
304 | /* fat/misc.c */ | 310 | /* fat/misc.c */ |
305 | extern void fat_fs_panic(struct super_block *s, const char *fmt, ...); | 311 | extern void fat_fs_panic(struct super_block *s, const char *fmt, ...); |
306 | extern void fat_clusters_flush(struct super_block *sb); | 312 | extern void fat_clusters_flush(struct super_block *sb); |
307 | extern int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster); | 313 | extern int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster); |
308 | extern void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec *ts, | 314 | extern void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec *ts, |
309 | __le16 __time, __le16 __date, u8 time_cs); | 315 | __le16 __time, __le16 __date, u8 time_cs); |
310 | extern void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec *ts, | 316 | extern void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec *ts, |
311 | __le16 *time, __le16 *date, u8 *time_cs); | 317 | __le16 *time, __le16 *date, u8 *time_cs); |
312 | extern int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs); | 318 | extern int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs); |
313 | 319 | ||
314 | int fat_cache_init(void); | 320 | int fat_cache_init(void); |
315 | void fat_cache_destroy(void); | 321 | void fat_cache_destroy(void); |
316 | 322 | ||
317 | #endif /* !_FAT_H */ | 323 | #endif /* !_FAT_H */ |
318 | 324 |
fs/fat/file.c
1 | /* | 1 | /* |
2 | * linux/fs/fat/file.c | 2 | * linux/fs/fat/file.c |
3 | * | 3 | * |
4 | * Written 1992,1993 by Werner Almesberger | 4 | * Written 1992,1993 by Werner Almesberger |
5 | * | 5 | * |
6 | * regular file handling primitives for fat-based filesystems | 6 | * regular file handling primitives for fat-based filesystems |
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include <linux/capability.h> | 9 | #include <linux/capability.h> |
10 | #include <linux/module.h> | 10 | #include <linux/module.h> |
11 | #include <linux/mount.h> | 11 | #include <linux/mount.h> |
12 | #include <linux/time.h> | 12 | #include <linux/time.h> |
13 | #include <linux/buffer_head.h> | 13 | #include <linux/buffer_head.h> |
14 | #include <linux/writeback.h> | 14 | #include <linux/writeback.h> |
15 | #include <linux/backing-dev.h> | 15 | #include <linux/backing-dev.h> |
16 | #include <linux/blkdev.h> | 16 | #include <linux/blkdev.h> |
17 | #include <linux/fsnotify.h> | 17 | #include <linux/fsnotify.h> |
18 | #include <linux/security.h> | 18 | #include <linux/security.h> |
19 | #include "fat.h" | 19 | #include "fat.h" |
20 | 20 | ||
21 | int fat_generic_ioctl(struct inode *inode, struct file *filp, | 21 | int fat_generic_ioctl(struct inode *inode, struct file *filp, |
22 | unsigned int cmd, unsigned long arg) | 22 | unsigned int cmd, unsigned long arg) |
23 | { | 23 | { |
24 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); | 24 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); |
25 | u32 __user *user_attr = (u32 __user *)arg; | 25 | u32 __user *user_attr = (u32 __user *)arg; |
26 | 26 | ||
27 | switch (cmd) { | 27 | switch (cmd) { |
28 | case FAT_IOCTL_GET_ATTRIBUTES: | 28 | case FAT_IOCTL_GET_ATTRIBUTES: |
29 | { | 29 | { |
30 | u32 attr; | 30 | u32 attr; |
31 | 31 | ||
32 | mutex_lock(&inode->i_mutex); | 32 | mutex_lock(&inode->i_mutex); |
33 | attr = fat_make_attrs(inode); | 33 | attr = fat_make_attrs(inode); |
34 | mutex_unlock(&inode->i_mutex); | 34 | mutex_unlock(&inode->i_mutex); |
35 | 35 | ||
36 | return put_user(attr, user_attr); | 36 | return put_user(attr, user_attr); |
37 | } | 37 | } |
38 | case FAT_IOCTL_SET_ATTRIBUTES: | 38 | case FAT_IOCTL_SET_ATTRIBUTES: |
39 | { | 39 | { |
40 | u32 attr, oldattr; | 40 | u32 attr, oldattr; |
41 | int err, is_dir = S_ISDIR(inode->i_mode); | 41 | int err, is_dir = S_ISDIR(inode->i_mode); |
42 | struct iattr ia; | 42 | struct iattr ia; |
43 | 43 | ||
44 | err = get_user(attr, user_attr); | 44 | err = get_user(attr, user_attr); |
45 | if (err) | 45 | if (err) |
46 | return err; | 46 | return err; |
47 | 47 | ||
48 | mutex_lock(&inode->i_mutex); | 48 | mutex_lock(&inode->i_mutex); |
49 | 49 | ||
50 | err = mnt_want_write(filp->f_path.mnt); | 50 | err = mnt_want_write(filp->f_path.mnt); |
51 | if (err) | 51 | if (err) |
52 | goto up_no_drop_write; | 52 | goto up_no_drop_write; |
53 | 53 | ||
54 | /* | 54 | /* |
55 | * ATTR_VOLUME and ATTR_DIR cannot be changed; this also | 55 | * ATTR_VOLUME and ATTR_DIR cannot be changed; this also |
56 | * prevents the user from turning us into a VFAT | 56 | * prevents the user from turning us into a VFAT |
57 | * longname entry. Also, we obviously can't set | 57 | * longname entry. Also, we obviously can't set |
58 | * any of the NTFS attributes in the high 24 bits. | 58 | * any of the NTFS attributes in the high 24 bits. |
59 | */ | 59 | */ |
60 | attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR); | 60 | attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR); |
61 | /* Merge in ATTR_VOLUME and ATTR_DIR */ | 61 | /* Merge in ATTR_VOLUME and ATTR_DIR */ |
62 | attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) | | 62 | attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) | |
63 | (is_dir ? ATTR_DIR : 0); | 63 | (is_dir ? ATTR_DIR : 0); |
64 | oldattr = fat_make_attrs(inode); | 64 | oldattr = fat_make_attrs(inode); |
65 | 65 | ||
66 | /* Equivalent to a chmod() */ | 66 | /* Equivalent to a chmod() */ |
67 | ia.ia_valid = ATTR_MODE | ATTR_CTIME; | 67 | ia.ia_valid = ATTR_MODE | ATTR_CTIME; |
68 | ia.ia_ctime = current_fs_time(inode->i_sb); | 68 | ia.ia_ctime = current_fs_time(inode->i_sb); |
69 | if (is_dir) | 69 | if (is_dir) |
70 | ia.ia_mode = fat_make_mode(sbi, attr, S_IRWXUGO); | 70 | ia.ia_mode = fat_make_mode(sbi, attr, S_IRWXUGO); |
71 | else { | 71 | else { |
72 | ia.ia_mode = fat_make_mode(sbi, attr, | 72 | ia.ia_mode = fat_make_mode(sbi, attr, |
73 | S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO)); | 73 | S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO)); |
74 | } | 74 | } |
75 | 75 | ||
76 | /* The root directory has no attributes */ | 76 | /* The root directory has no attributes */ |
77 | if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) { | 77 | if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) { |
78 | err = -EINVAL; | 78 | err = -EINVAL; |
79 | goto up; | 79 | goto up; |
80 | } | 80 | } |
81 | 81 | ||
82 | if (sbi->options.sys_immutable) { | 82 | if (sbi->options.sys_immutable) { |
83 | if ((attr | oldattr) & ATTR_SYS) { | 83 | if ((attr | oldattr) & ATTR_SYS) { |
84 | if (!capable(CAP_LINUX_IMMUTABLE)) { | 84 | if (!capable(CAP_LINUX_IMMUTABLE)) { |
85 | err = -EPERM; | 85 | err = -EPERM; |
86 | goto up; | 86 | goto up; |
87 | } | 87 | } |
88 | } | 88 | } |
89 | } | 89 | } |
90 | 90 | ||
91 | /* | 91 | /* |
92 | * The security check is questionable... We single | 92 | * The security check is questionable... We single |
93 | * out the RO attribute for checking by the security | 93 | * out the RO attribute for checking by the security |
94 | * module, just because it maps to a file mode. | 94 | * module, just because it maps to a file mode. |
95 | */ | 95 | */ |
96 | err = security_inode_setattr(filp->f_path.dentry, &ia); | 96 | err = security_inode_setattr(filp->f_path.dentry, &ia); |
97 | if (err) | 97 | if (err) |
98 | goto up; | 98 | goto up; |
99 | 99 | ||
100 | /* This MUST be done before doing anything irreversible... */ | 100 | /* This MUST be done before doing anything irreversible... */ |
101 | err = fat_setattr(filp->f_path.dentry, &ia); | 101 | err = fat_setattr(filp->f_path.dentry, &ia); |
102 | if (err) | 102 | if (err) |
103 | goto up; | 103 | goto up; |
104 | 104 | ||
105 | fsnotify_change(filp->f_path.dentry, ia.ia_valid); | 105 | fsnotify_change(filp->f_path.dentry, ia.ia_valid); |
106 | if (sbi->options.sys_immutable) { | 106 | if (sbi->options.sys_immutable) { |
107 | if (attr & ATTR_SYS) | 107 | if (attr & ATTR_SYS) |
108 | inode->i_flags |= S_IMMUTABLE; | 108 | inode->i_flags |= S_IMMUTABLE; |
109 | else | 109 | else |
110 | inode->i_flags &= S_IMMUTABLE; | 110 | inode->i_flags &= S_IMMUTABLE; |
111 | } | 111 | } |
112 | 112 | ||
113 | fat_save_attrs(inode, attr); | 113 | fat_save_attrs(inode, attr); |
114 | mark_inode_dirty(inode); | 114 | mark_inode_dirty(inode); |
115 | up: | 115 | up: |
116 | mnt_drop_write(filp->f_path.mnt); | 116 | mnt_drop_write(filp->f_path.mnt); |
117 | up_no_drop_write: | 117 | up_no_drop_write: |
118 | mutex_unlock(&inode->i_mutex); | 118 | mutex_unlock(&inode->i_mutex); |
119 | return err; | 119 | return err; |
120 | } | 120 | } |
121 | default: | 121 | default: |
122 | return -ENOTTY; /* Inappropriate ioctl for device */ | 122 | return -ENOTTY; /* Inappropriate ioctl for device */ |
123 | } | 123 | } |
124 | } | 124 | } |
125 | 125 | ||
126 | static int fat_file_release(struct inode *inode, struct file *filp) | 126 | static int fat_file_release(struct inode *inode, struct file *filp) |
127 | { | 127 | { |
128 | if ((filp->f_mode & FMODE_WRITE) && | 128 | if ((filp->f_mode & FMODE_WRITE) && |
129 | MSDOS_SB(inode->i_sb)->options.flush) { | 129 | MSDOS_SB(inode->i_sb)->options.flush) { |
130 | fat_flush_inodes(inode->i_sb, inode, NULL); | 130 | fat_flush_inodes(inode->i_sb, inode, NULL); |
131 | congestion_wait(WRITE, HZ/10); | 131 | congestion_wait(WRITE, HZ/10); |
132 | } | 132 | } |
133 | return 0; | 133 | return 0; |
134 | } | 134 | } |
135 | 135 | ||
136 | const struct file_operations fat_file_operations = { | 136 | const struct file_operations fat_file_operations = { |
137 | .llseek = generic_file_llseek, | 137 | .llseek = generic_file_llseek, |
138 | .read = do_sync_read, | 138 | .read = do_sync_read, |
139 | .write = do_sync_write, | 139 | .write = do_sync_write, |
140 | .aio_read = generic_file_aio_read, | 140 | .aio_read = generic_file_aio_read, |
141 | .aio_write = generic_file_aio_write, | 141 | .aio_write = generic_file_aio_write, |
142 | .mmap = generic_file_mmap, | 142 | .mmap = generic_file_mmap, |
143 | .release = fat_file_release, | 143 | .release = fat_file_release, |
144 | .ioctl = fat_generic_ioctl, | 144 | .ioctl = fat_generic_ioctl, |
145 | .fsync = file_fsync, | 145 | .fsync = file_fsync, |
146 | .splice_read = generic_file_splice_read, | 146 | .splice_read = generic_file_splice_read, |
147 | }; | 147 | }; |
148 | 148 | ||
149 | static int fat_cont_expand(struct inode *inode, loff_t size) | 149 | static int fat_cont_expand(struct inode *inode, loff_t size) |
150 | { | 150 | { |
151 | struct address_space *mapping = inode->i_mapping; | 151 | struct address_space *mapping = inode->i_mapping; |
152 | loff_t start = inode->i_size, count = size - inode->i_size; | 152 | loff_t start = inode->i_size, count = size - inode->i_size; |
153 | int err; | 153 | int err; |
154 | 154 | ||
155 | err = generic_cont_expand_simple(inode, size); | 155 | err = generic_cont_expand_simple(inode, size); |
156 | if (err) | 156 | if (err) |
157 | goto out; | 157 | goto out; |
158 | 158 | ||
159 | inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC; | 159 | inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC; |
160 | mark_inode_dirty(inode); | 160 | mark_inode_dirty(inode); |
161 | if (IS_SYNC(inode)) | 161 | if (IS_SYNC(inode)) |
162 | err = sync_page_range_nolock(inode, mapping, start, count); | 162 | err = sync_page_range_nolock(inode, mapping, start, count); |
163 | out: | 163 | out: |
164 | return err; | 164 | return err; |
165 | } | 165 | } |
166 | 166 | ||
167 | /* Free all clusters after the skip'th cluster. */ | 167 | /* Free all clusters after the skip'th cluster. */ |
168 | static int fat_free(struct inode *inode, int skip) | 168 | static int fat_free(struct inode *inode, int skip) |
169 | { | 169 | { |
170 | struct super_block *sb = inode->i_sb; | 170 | struct super_block *sb = inode->i_sb; |
171 | int err, wait, free_start, i_start, i_logstart; | 171 | int err, wait, free_start, i_start, i_logstart; |
172 | 172 | ||
173 | if (MSDOS_I(inode)->i_start == 0) | 173 | if (MSDOS_I(inode)->i_start == 0) |
174 | return 0; | 174 | return 0; |
175 | 175 | ||
176 | fat_cache_inval_inode(inode); | 176 | fat_cache_inval_inode(inode); |
177 | 177 | ||
178 | wait = IS_DIRSYNC(inode); | 178 | wait = IS_DIRSYNC(inode); |
179 | i_start = free_start = MSDOS_I(inode)->i_start; | 179 | i_start = free_start = MSDOS_I(inode)->i_start; |
180 | i_logstart = MSDOS_I(inode)->i_logstart; | 180 | i_logstart = MSDOS_I(inode)->i_logstart; |
181 | 181 | ||
182 | /* First, we write the new file size. */ | 182 | /* First, we write the new file size. */ |
183 | if (!skip) { | 183 | if (!skip) { |
184 | MSDOS_I(inode)->i_start = 0; | 184 | MSDOS_I(inode)->i_start = 0; |
185 | MSDOS_I(inode)->i_logstart = 0; | 185 | MSDOS_I(inode)->i_logstart = 0; |
186 | } | 186 | } |
187 | MSDOS_I(inode)->i_attrs |= ATTR_ARCH; | 187 | MSDOS_I(inode)->i_attrs |= ATTR_ARCH; |
188 | inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC; | 188 | inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC; |
189 | if (wait) { | 189 | if (wait) { |
190 | err = fat_sync_inode(inode); | 190 | err = fat_sync_inode(inode); |
191 | if (err) { | 191 | if (err) { |
192 | MSDOS_I(inode)->i_start = i_start; | 192 | MSDOS_I(inode)->i_start = i_start; |
193 | MSDOS_I(inode)->i_logstart = i_logstart; | 193 | MSDOS_I(inode)->i_logstart = i_logstart; |
194 | return err; | 194 | return err; |
195 | } | 195 | } |
196 | } else | 196 | } else |
197 | mark_inode_dirty(inode); | 197 | mark_inode_dirty(inode); |
198 | 198 | ||
199 | /* Write a new EOF, and get the remaining cluster chain for freeing. */ | 199 | /* Write a new EOF, and get the remaining cluster chain for freeing. */ |
200 | if (skip) { | 200 | if (skip) { |
201 | struct fat_entry fatent; | 201 | struct fat_entry fatent; |
202 | int ret, fclus, dclus; | 202 | int ret, fclus, dclus; |
203 | 203 | ||
204 | ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus); | 204 | ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus); |
205 | if (ret < 0) | 205 | if (ret < 0) |
206 | return ret; | 206 | return ret; |
207 | else if (ret == FAT_ENT_EOF) | 207 | else if (ret == FAT_ENT_EOF) |
208 | return 0; | 208 | return 0; |
209 | 209 | ||
210 | fatent_init(&fatent); | 210 | fatent_init(&fatent); |
211 | ret = fat_ent_read(inode, &fatent, dclus); | 211 | ret = fat_ent_read(inode, &fatent, dclus); |
212 | if (ret == FAT_ENT_EOF) { | 212 | if (ret == FAT_ENT_EOF) { |
213 | fatent_brelse(&fatent); | 213 | fatent_brelse(&fatent); |
214 | return 0; | 214 | return 0; |
215 | } else if (ret == FAT_ENT_FREE) { | 215 | } else if (ret == FAT_ENT_FREE) { |
216 | fat_fs_panic(sb, | 216 | fat_fs_panic(sb, |
217 | "%s: invalid cluster chain (i_pos %lld)", | 217 | "%s: invalid cluster chain (i_pos %lld)", |
218 | __func__, MSDOS_I(inode)->i_pos); | 218 | __func__, MSDOS_I(inode)->i_pos); |
219 | ret = -EIO; | 219 | ret = -EIO; |
220 | } else if (ret > 0) { | 220 | } else if (ret > 0) { |
221 | err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait); | 221 | err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait); |
222 | if (err) | 222 | if (err) |
223 | ret = err; | 223 | ret = err; |
224 | } | 224 | } |
225 | fatent_brelse(&fatent); | 225 | fatent_brelse(&fatent); |
226 | if (ret < 0) | 226 | if (ret < 0) |
227 | return ret; | 227 | return ret; |
228 | 228 | ||
229 | free_start = ret; | 229 | free_start = ret; |
230 | } | 230 | } |
231 | inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9); | 231 | inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9); |
232 | 232 | ||
233 | /* Freeing the remained cluster chain */ | 233 | /* Freeing the remained cluster chain */ |
234 | return fat_free_clusters(inode, free_start); | 234 | return fat_free_clusters(inode, free_start); |
235 | } | 235 | } |
236 | 236 | ||
237 | void fat_truncate(struct inode *inode) | 237 | void fat_truncate(struct inode *inode) |
238 | { | 238 | { |
239 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); | 239 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); |
240 | const unsigned int cluster_size = sbi->cluster_size; | 240 | const unsigned int cluster_size = sbi->cluster_size; |
241 | int nr_clusters; | 241 | int nr_clusters; |
242 | 242 | ||
243 | /* | 243 | /* |
244 | * This protects against truncating a file bigger than it was then | 244 | * This protects against truncating a file bigger than it was then |
245 | * trying to write into the hole. | 245 | * trying to write into the hole. |
246 | */ | 246 | */ |
247 | if (MSDOS_I(inode)->mmu_private > inode->i_size) | 247 | if (MSDOS_I(inode)->mmu_private > inode->i_size) |
248 | MSDOS_I(inode)->mmu_private = inode->i_size; | 248 | MSDOS_I(inode)->mmu_private = inode->i_size; |
249 | 249 | ||
250 | nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits; | 250 | nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits; |
251 | 251 | ||
252 | fat_free(inode, nr_clusters); | 252 | fat_free(inode, nr_clusters); |
253 | fat_flush_inodes(inode->i_sb, inode, NULL); | 253 | fat_flush_inodes(inode->i_sb, inode, NULL); |
254 | } | 254 | } |
255 | 255 | ||
256 | int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) | 256 | int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) |
257 | { | 257 | { |
258 | struct inode *inode = dentry->d_inode; | 258 | struct inode *inode = dentry->d_inode; |
259 | generic_fillattr(inode, stat); | 259 | generic_fillattr(inode, stat); |
260 | stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size; | 260 | stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size; |
261 | return 0; | 261 | return 0; |
262 | } | 262 | } |
263 | EXPORT_SYMBOL_GPL(fat_getattr); | 263 | EXPORT_SYMBOL_GPL(fat_getattr); |
264 | 264 | ||
265 | static int fat_sanitize_mode(const struct msdos_sb_info *sbi, | 265 | static int fat_sanitize_mode(const struct msdos_sb_info *sbi, |
266 | struct inode *inode, umode_t *mode_ptr) | 266 | struct inode *inode, umode_t *mode_ptr) |
267 | { | 267 | { |
268 | mode_t mask, perm; | 268 | mode_t mask, perm; |
269 | 269 | ||
270 | /* | 270 | /* |
271 | * Note, the basic check is already done by a caller of | 271 | * Note, the basic check is already done by a caller of |
272 | * (attr->ia_mode & ~FAT_VALID_MODE) | 272 | * (attr->ia_mode & ~FAT_VALID_MODE) |
273 | */ | 273 | */ |
274 | 274 | ||
275 | if (S_ISREG(inode->i_mode)) | 275 | if (S_ISREG(inode->i_mode)) |
276 | mask = sbi->options.fs_fmask; | 276 | mask = sbi->options.fs_fmask; |
277 | else | 277 | else |
278 | mask = sbi->options.fs_dmask; | 278 | mask = sbi->options.fs_dmask; |
279 | 279 | ||
280 | perm = *mode_ptr & ~(S_IFMT | mask); | 280 | perm = *mode_ptr & ~(S_IFMT | mask); |
281 | 281 | ||
282 | /* | 282 | /* |
283 | * Of the r and x bits, all (subject to umask) must be present. Of the | 283 | * Of the r and x bits, all (subject to umask) must be present. Of the |
284 | * w bits, either all (subject to umask) or none must be present. | 284 | * w bits, either all (subject to umask) or none must be present. |
285 | * | ||
286 | * If fat_mode_can_hold_ro(inode) is false, can't change w bits. | ||
285 | */ | 287 | */ |
286 | if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO))) | 288 | if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO))) |
287 | return -EPERM; | 289 | return -EPERM; |
288 | if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask))) | 290 | if (fat_mode_can_hold_ro(inode)) { |
289 | return -EPERM; | 291 | if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask))) |
292 | return -EPERM; | ||
293 | } else { | ||
294 | if ((perm & S_IWUGO) != (S_IWUGO & ~mask)) | ||
295 | return -EPERM; | ||
296 | } | ||
290 | 297 | ||
291 | *mode_ptr &= S_IFMT | perm; | 298 | *mode_ptr &= S_IFMT | perm; |
292 | 299 | ||
293 | return 0; | 300 | return 0; |
294 | } | 301 | } |
295 | 302 | ||
296 | static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode) | 303 | static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode) |
297 | { | 304 | { |
298 | mode_t allow_utime = sbi->options.allow_utime; | 305 | mode_t allow_utime = sbi->options.allow_utime; |
299 | 306 | ||
300 | if (current->fsuid != inode->i_uid) { | 307 | if (current->fsuid != inode->i_uid) { |
301 | if (in_group_p(inode->i_gid)) | 308 | if (in_group_p(inode->i_gid)) |
302 | allow_utime >>= 3; | 309 | allow_utime >>= 3; |
303 | if (allow_utime & MAY_WRITE) | 310 | if (allow_utime & MAY_WRITE) |
304 | return 1; | 311 | return 1; |
305 | } | 312 | } |
306 | 313 | ||
307 | /* use a default check */ | 314 | /* use a default check */ |
308 | return 0; | 315 | return 0; |
309 | } | 316 | } |
310 | 317 | ||
311 | #define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET) | 318 | #define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET) |
312 | /* valid file mode bits */ | 319 | /* valid file mode bits */ |
313 | #define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO) | 320 | #define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO) |
314 | 321 | ||
315 | int fat_setattr(struct dentry *dentry, struct iattr *attr) | 322 | int fat_setattr(struct dentry *dentry, struct iattr *attr) |
316 | { | 323 | { |
317 | struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb); | 324 | struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb); |
318 | struct inode *inode = dentry->d_inode; | 325 | struct inode *inode = dentry->d_inode; |
319 | int error = 0; | ||
320 | unsigned int ia_valid; | 326 | unsigned int ia_valid; |
327 | int error; | ||
321 | 328 | ||
322 | /* | 329 | /* |
323 | * Expand the file. Since inode_setattr() updates ->i_size | 330 | * Expand the file. Since inode_setattr() updates ->i_size |
324 | * before calling the ->truncate(), but FAT needs to fill the | 331 | * before calling the ->truncate(), but FAT needs to fill the |
325 | * hole before it. | 332 | * hole before it. |
326 | */ | 333 | */ |
327 | if (attr->ia_valid & ATTR_SIZE) { | 334 | if (attr->ia_valid & ATTR_SIZE) { |
328 | if (attr->ia_size > inode->i_size) { | 335 | if (attr->ia_size > inode->i_size) { |
329 | error = fat_cont_expand(inode, attr->ia_size); | 336 | error = fat_cont_expand(inode, attr->ia_size); |
330 | if (error || attr->ia_valid == ATTR_SIZE) | 337 | if (error || attr->ia_valid == ATTR_SIZE) |
331 | goto out; | 338 | goto out; |
332 | attr->ia_valid &= ~ATTR_SIZE; | 339 | attr->ia_valid &= ~ATTR_SIZE; |
333 | } | 340 | } |
334 | } | 341 | } |
335 | 342 | ||
336 | /* Check for setting the inode time. */ | 343 | /* Check for setting the inode time. */ |
337 | ia_valid = attr->ia_valid; | 344 | ia_valid = attr->ia_valid; |
338 | if (ia_valid & TIMES_SET_FLAGS) { | 345 | if (ia_valid & TIMES_SET_FLAGS) { |
339 | if (fat_allow_set_time(sbi, inode)) | 346 | if (fat_allow_set_time(sbi, inode)) |
340 | attr->ia_valid &= ~TIMES_SET_FLAGS; | 347 | attr->ia_valid &= ~TIMES_SET_FLAGS; |
341 | } | 348 | } |
342 | 349 | ||
343 | error = inode_change_ok(inode, attr); | 350 | error = inode_change_ok(inode, attr); |
344 | attr->ia_valid = ia_valid; | 351 | attr->ia_valid = ia_valid; |
345 | if (error) { | 352 | if (error) { |
346 | if (sbi->options.quiet) | 353 | if (sbi->options.quiet) |
347 | error = 0; | 354 | error = 0; |
348 | goto out; | 355 | goto out; |
349 | } | 356 | } |
350 | 357 | ||
351 | if (((attr->ia_valid & ATTR_UID) && | 358 | if (((attr->ia_valid & ATTR_UID) && |
352 | (attr->ia_uid != sbi->options.fs_uid)) || | 359 | (attr->ia_uid != sbi->options.fs_uid)) || |
353 | ((attr->ia_valid & ATTR_GID) && | 360 | ((attr->ia_valid & ATTR_GID) && |
354 | (attr->ia_gid != sbi->options.fs_gid)) || | 361 | (attr->ia_gid != sbi->options.fs_gid)) || |
355 | ((attr->ia_valid & ATTR_MODE) && | 362 | ((attr->ia_valid & ATTR_MODE) && |
356 | (attr->ia_mode & ~FAT_VALID_MODE))) | 363 | (attr->ia_mode & ~FAT_VALID_MODE))) |
357 | error = -EPERM; | 364 | error = -EPERM; |
358 | 365 | ||
359 | if (error) { | 366 | if (error) { |
360 | if (sbi->options.quiet) | 367 | if (sbi->options.quiet) |
361 | error = 0; | 368 | error = 0; |
362 | goto out; | 369 | goto out; |
363 | } | 370 | } |
364 | 371 | ||
365 | /* | 372 | /* |
366 | * We don't return -EPERM here. Yes, strange, but this is too | 373 | * We don't return -EPERM here. Yes, strange, but this is too |
367 | * old behavior. | 374 | * old behavior. |
368 | */ | 375 | */ |
369 | if (attr->ia_valid & ATTR_MODE) { | 376 | if (attr->ia_valid & ATTR_MODE) { |
370 | if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0) | 377 | if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0) |
371 | attr->ia_valid &= ~ATTR_MODE; | 378 | attr->ia_valid &= ~ATTR_MODE; |
372 | } | 379 | } |
373 | 380 | ||
374 | error = inode_setattr(inode, attr); | 381 | if (attr->ia_valid) |
382 | error = inode_setattr(inode, attr); | ||
375 | out: | 383 | out: |
376 | return error; | 384 | return error; |
377 | } | 385 | } |
378 | EXPORT_SYMBOL_GPL(fat_setattr); | 386 | EXPORT_SYMBOL_GPL(fat_setattr); |
379 | 387 | ||
380 | const struct inode_operations fat_file_inode_operations = { | 388 | const struct inode_operations fat_file_inode_operations = { |
381 | .truncate = fat_truncate, | 389 | .truncate = fat_truncate, |
382 | .setattr = fat_setattr, | 390 | .setattr = fat_setattr, |
383 | .getattr = fat_getattr, | 391 | .getattr = fat_getattr, |
384 | }; | 392 | }; |
fs/fat/inode.c
1 | /* | 1 | /* |
2 | * linux/fs/fat/inode.c | 2 | * linux/fs/fat/inode.c |
3 | * | 3 | * |
4 | * Written 1992,1993 by Werner Almesberger | 4 | * Written 1992,1993 by Werner Almesberger |
5 | * VFAT extensions by Gordon Chaffee, merged with msdos fs by Henrik Storner | 5 | * VFAT extensions by Gordon Chaffee, merged with msdos fs by Henrik Storner |
6 | * Rewritten for the constant inumbers support by Al Viro | 6 | * Rewritten for the constant inumbers support by Al Viro |
7 | * | 7 | * |
8 | * Fixes: | 8 | * Fixes: |
9 | * | 9 | * |
10 | * Max Cohan: Fixed invalid FSINFO offset when info_sector is 0 | 10 | * Max Cohan: Fixed invalid FSINFO offset when info_sector is 0 |
11 | */ | 11 | */ |
12 | 12 | ||
13 | #include <linux/module.h> | 13 | #include <linux/module.h> |
14 | #include <linux/init.h> | 14 | #include <linux/init.h> |
15 | #include <linux/time.h> | 15 | #include <linux/time.h> |
16 | #include <linux/slab.h> | 16 | #include <linux/slab.h> |
17 | #include <linux/smp_lock.h> | 17 | #include <linux/smp_lock.h> |
18 | #include <linux/seq_file.h> | 18 | #include <linux/seq_file.h> |
19 | #include <linux/pagemap.h> | 19 | #include <linux/pagemap.h> |
20 | #include <linux/mpage.h> | 20 | #include <linux/mpage.h> |
21 | #include <linux/buffer_head.h> | 21 | #include <linux/buffer_head.h> |
22 | #include <linux/exportfs.h> | 22 | #include <linux/exportfs.h> |
23 | #include <linux/mount.h> | 23 | #include <linux/mount.h> |
24 | #include <linux/vfs.h> | 24 | #include <linux/vfs.h> |
25 | #include <linux/parser.h> | 25 | #include <linux/parser.h> |
26 | #include <linux/uio.h> | 26 | #include <linux/uio.h> |
27 | #include <linux/writeback.h> | 27 | #include <linux/writeback.h> |
28 | #include <linux/log2.h> | 28 | #include <linux/log2.h> |
29 | #include <linux/hash.h> | 29 | #include <linux/hash.h> |
30 | #include <asm/unaligned.h> | 30 | #include <asm/unaligned.h> |
31 | #include "fat.h" | 31 | #include "fat.h" |
32 | 32 | ||
33 | #ifndef CONFIG_FAT_DEFAULT_IOCHARSET | 33 | #ifndef CONFIG_FAT_DEFAULT_IOCHARSET |
34 | /* if user don't select VFAT, this is undefined. */ | 34 | /* if user don't select VFAT, this is undefined. */ |
35 | #define CONFIG_FAT_DEFAULT_IOCHARSET "" | 35 | #define CONFIG_FAT_DEFAULT_IOCHARSET "" |
36 | #endif | 36 | #endif |
37 | 37 | ||
38 | static int fat_default_codepage = CONFIG_FAT_DEFAULT_CODEPAGE; | 38 | static int fat_default_codepage = CONFIG_FAT_DEFAULT_CODEPAGE; |
39 | static char fat_default_iocharset[] = CONFIG_FAT_DEFAULT_IOCHARSET; | 39 | static char fat_default_iocharset[] = CONFIG_FAT_DEFAULT_IOCHARSET; |
40 | 40 | ||
41 | 41 | ||
42 | static int fat_add_cluster(struct inode *inode) | 42 | static int fat_add_cluster(struct inode *inode) |
43 | { | 43 | { |
44 | int err, cluster; | 44 | int err, cluster; |
45 | 45 | ||
46 | err = fat_alloc_clusters(inode, &cluster, 1); | 46 | err = fat_alloc_clusters(inode, &cluster, 1); |
47 | if (err) | 47 | if (err) |
48 | return err; | 48 | return err; |
49 | /* FIXME: this cluster should be added after data of this | 49 | /* FIXME: this cluster should be added after data of this |
50 | * cluster is writed */ | 50 | * cluster is writed */ |
51 | err = fat_chain_add(inode, cluster, 1); | 51 | err = fat_chain_add(inode, cluster, 1); |
52 | if (err) | 52 | if (err) |
53 | fat_free_clusters(inode, cluster); | 53 | fat_free_clusters(inode, cluster); |
54 | return err; | 54 | return err; |
55 | } | 55 | } |
56 | 56 | ||
57 | static inline int __fat_get_block(struct inode *inode, sector_t iblock, | 57 | static inline int __fat_get_block(struct inode *inode, sector_t iblock, |
58 | unsigned long *max_blocks, | 58 | unsigned long *max_blocks, |
59 | struct buffer_head *bh_result, int create) | 59 | struct buffer_head *bh_result, int create) |
60 | { | 60 | { |
61 | struct super_block *sb = inode->i_sb; | 61 | struct super_block *sb = inode->i_sb; |
62 | struct msdos_sb_info *sbi = MSDOS_SB(sb); | 62 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
63 | unsigned long mapped_blocks; | 63 | unsigned long mapped_blocks; |
64 | sector_t phys; | 64 | sector_t phys; |
65 | int err, offset; | 65 | int err, offset; |
66 | 66 | ||
67 | err = fat_bmap(inode, iblock, &phys, &mapped_blocks); | 67 | err = fat_bmap(inode, iblock, &phys, &mapped_blocks); |
68 | if (err) | 68 | if (err) |
69 | return err; | 69 | return err; |
70 | if (phys) { | 70 | if (phys) { |
71 | map_bh(bh_result, sb, phys); | 71 | map_bh(bh_result, sb, phys); |
72 | *max_blocks = min(mapped_blocks, *max_blocks); | 72 | *max_blocks = min(mapped_blocks, *max_blocks); |
73 | return 0; | 73 | return 0; |
74 | } | 74 | } |
75 | if (!create) | 75 | if (!create) |
76 | return 0; | 76 | return 0; |
77 | 77 | ||
78 | if (iblock != MSDOS_I(inode)->mmu_private >> sb->s_blocksize_bits) { | 78 | if (iblock != MSDOS_I(inode)->mmu_private >> sb->s_blocksize_bits) { |
79 | fat_fs_panic(sb, "corrupted file size (i_pos %lld, %lld)", | 79 | fat_fs_panic(sb, "corrupted file size (i_pos %lld, %lld)", |
80 | MSDOS_I(inode)->i_pos, MSDOS_I(inode)->mmu_private); | 80 | MSDOS_I(inode)->i_pos, MSDOS_I(inode)->mmu_private); |
81 | return -EIO; | 81 | return -EIO; |
82 | } | 82 | } |
83 | 83 | ||
84 | offset = (unsigned long)iblock & (sbi->sec_per_clus - 1); | 84 | offset = (unsigned long)iblock & (sbi->sec_per_clus - 1); |
85 | if (!offset) { | 85 | if (!offset) { |
86 | /* TODO: multiple cluster allocation would be desirable. */ | 86 | /* TODO: multiple cluster allocation would be desirable. */ |
87 | err = fat_add_cluster(inode); | 87 | err = fat_add_cluster(inode); |
88 | if (err) | 88 | if (err) |
89 | return err; | 89 | return err; |
90 | } | 90 | } |
91 | /* available blocks on this cluster */ | 91 | /* available blocks on this cluster */ |
92 | mapped_blocks = sbi->sec_per_clus - offset; | 92 | mapped_blocks = sbi->sec_per_clus - offset; |
93 | 93 | ||
94 | *max_blocks = min(mapped_blocks, *max_blocks); | 94 | *max_blocks = min(mapped_blocks, *max_blocks); |
95 | MSDOS_I(inode)->mmu_private += *max_blocks << sb->s_blocksize_bits; | 95 | MSDOS_I(inode)->mmu_private += *max_blocks << sb->s_blocksize_bits; |
96 | 96 | ||
97 | err = fat_bmap(inode, iblock, &phys, &mapped_blocks); | 97 | err = fat_bmap(inode, iblock, &phys, &mapped_blocks); |
98 | if (err) | 98 | if (err) |
99 | return err; | 99 | return err; |
100 | 100 | ||
101 | BUG_ON(!phys); | 101 | BUG_ON(!phys); |
102 | BUG_ON(*max_blocks != mapped_blocks); | 102 | BUG_ON(*max_blocks != mapped_blocks); |
103 | set_buffer_new(bh_result); | 103 | set_buffer_new(bh_result); |
104 | map_bh(bh_result, sb, phys); | 104 | map_bh(bh_result, sb, phys); |
105 | 105 | ||
106 | return 0; | 106 | return 0; |
107 | } | 107 | } |
108 | 108 | ||
109 | static int fat_get_block(struct inode *inode, sector_t iblock, | 109 | static int fat_get_block(struct inode *inode, sector_t iblock, |
110 | struct buffer_head *bh_result, int create) | 110 | struct buffer_head *bh_result, int create) |
111 | { | 111 | { |
112 | struct super_block *sb = inode->i_sb; | 112 | struct super_block *sb = inode->i_sb; |
113 | unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits; | 113 | unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits; |
114 | int err; | 114 | int err; |
115 | 115 | ||
116 | err = __fat_get_block(inode, iblock, &max_blocks, bh_result, create); | 116 | err = __fat_get_block(inode, iblock, &max_blocks, bh_result, create); |
117 | if (err) | 117 | if (err) |
118 | return err; | 118 | return err; |
119 | bh_result->b_size = max_blocks << sb->s_blocksize_bits; | 119 | bh_result->b_size = max_blocks << sb->s_blocksize_bits; |
120 | return 0; | 120 | return 0; |
121 | } | 121 | } |
122 | 122 | ||
123 | static int fat_writepage(struct page *page, struct writeback_control *wbc) | 123 | static int fat_writepage(struct page *page, struct writeback_control *wbc) |
124 | { | 124 | { |
125 | return block_write_full_page(page, fat_get_block, wbc); | 125 | return block_write_full_page(page, fat_get_block, wbc); |
126 | } | 126 | } |
127 | 127 | ||
128 | static int fat_writepages(struct address_space *mapping, | 128 | static int fat_writepages(struct address_space *mapping, |
129 | struct writeback_control *wbc) | 129 | struct writeback_control *wbc) |
130 | { | 130 | { |
131 | return mpage_writepages(mapping, wbc, fat_get_block); | 131 | return mpage_writepages(mapping, wbc, fat_get_block); |
132 | } | 132 | } |
133 | 133 | ||
134 | static int fat_readpage(struct file *file, struct page *page) | 134 | static int fat_readpage(struct file *file, struct page *page) |
135 | { | 135 | { |
136 | return mpage_readpage(page, fat_get_block); | 136 | return mpage_readpage(page, fat_get_block); |
137 | } | 137 | } |
138 | 138 | ||
139 | static int fat_readpages(struct file *file, struct address_space *mapping, | 139 | static int fat_readpages(struct file *file, struct address_space *mapping, |
140 | struct list_head *pages, unsigned nr_pages) | 140 | struct list_head *pages, unsigned nr_pages) |
141 | { | 141 | { |
142 | return mpage_readpages(mapping, pages, nr_pages, fat_get_block); | 142 | return mpage_readpages(mapping, pages, nr_pages, fat_get_block); |
143 | } | 143 | } |
144 | 144 | ||
145 | static int fat_write_begin(struct file *file, struct address_space *mapping, | 145 | static int fat_write_begin(struct file *file, struct address_space *mapping, |
146 | loff_t pos, unsigned len, unsigned flags, | 146 | loff_t pos, unsigned len, unsigned flags, |
147 | struct page **pagep, void **fsdata) | 147 | struct page **pagep, void **fsdata) |
148 | { | 148 | { |
149 | *pagep = NULL; | 149 | *pagep = NULL; |
150 | return cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata, | 150 | return cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata, |
151 | fat_get_block, | 151 | fat_get_block, |
152 | &MSDOS_I(mapping->host)->mmu_private); | 152 | &MSDOS_I(mapping->host)->mmu_private); |
153 | } | 153 | } |
154 | 154 | ||
155 | static int fat_write_end(struct file *file, struct address_space *mapping, | 155 | static int fat_write_end(struct file *file, struct address_space *mapping, |
156 | loff_t pos, unsigned len, unsigned copied, | 156 | loff_t pos, unsigned len, unsigned copied, |
157 | struct page *pagep, void *fsdata) | 157 | struct page *pagep, void *fsdata) |
158 | { | 158 | { |
159 | struct inode *inode = mapping->host; | 159 | struct inode *inode = mapping->host; |
160 | int err; | 160 | int err; |
161 | err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata); | 161 | err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata); |
162 | if (!(err < 0) && !(MSDOS_I(inode)->i_attrs & ATTR_ARCH)) { | 162 | if (!(err < 0) && !(MSDOS_I(inode)->i_attrs & ATTR_ARCH)) { |
163 | inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC; | 163 | inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC; |
164 | MSDOS_I(inode)->i_attrs |= ATTR_ARCH; | 164 | MSDOS_I(inode)->i_attrs |= ATTR_ARCH; |
165 | mark_inode_dirty(inode); | 165 | mark_inode_dirty(inode); |
166 | } | 166 | } |
167 | return err; | 167 | return err; |
168 | } | 168 | } |
169 | 169 | ||
170 | static ssize_t fat_direct_IO(int rw, struct kiocb *iocb, | 170 | static ssize_t fat_direct_IO(int rw, struct kiocb *iocb, |
171 | const struct iovec *iov, | 171 | const struct iovec *iov, |
172 | loff_t offset, unsigned long nr_segs) | 172 | loff_t offset, unsigned long nr_segs) |
173 | { | 173 | { |
174 | struct file *file = iocb->ki_filp; | 174 | struct file *file = iocb->ki_filp; |
175 | struct inode *inode = file->f_mapping->host; | 175 | struct inode *inode = file->f_mapping->host; |
176 | 176 | ||
177 | if (rw == WRITE) { | 177 | if (rw == WRITE) { |
178 | /* | 178 | /* |
179 | * FIXME: blockdev_direct_IO() doesn't use ->write_begin(), | 179 | * FIXME: blockdev_direct_IO() doesn't use ->write_begin(), |
180 | * so we need to update the ->mmu_private to block boundary. | 180 | * so we need to update the ->mmu_private to block boundary. |
181 | * | 181 | * |
182 | * But we must fill the remaining area or hole by nul for | 182 | * But we must fill the remaining area or hole by nul for |
183 | * updating ->mmu_private. | 183 | * updating ->mmu_private. |
184 | * | 184 | * |
185 | * Return 0, and fallback to normal buffered write. | 185 | * Return 0, and fallback to normal buffered write. |
186 | */ | 186 | */ |
187 | loff_t size = offset + iov_length(iov, nr_segs); | 187 | loff_t size = offset + iov_length(iov, nr_segs); |
188 | if (MSDOS_I(inode)->mmu_private < size) | 188 | if (MSDOS_I(inode)->mmu_private < size) |
189 | return 0; | 189 | return 0; |
190 | } | 190 | } |
191 | 191 | ||
192 | /* | 192 | /* |
193 | * FAT need to use the DIO_LOCKING for avoiding the race | 193 | * FAT need to use the DIO_LOCKING for avoiding the race |
194 | * condition of fat_get_block() and ->truncate(). | 194 | * condition of fat_get_block() and ->truncate(). |
195 | */ | 195 | */ |
196 | return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov, | 196 | return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov, |
197 | offset, nr_segs, fat_get_block, NULL); | 197 | offset, nr_segs, fat_get_block, NULL); |
198 | } | 198 | } |
199 | 199 | ||
200 | static sector_t _fat_bmap(struct address_space *mapping, sector_t block) | 200 | static sector_t _fat_bmap(struct address_space *mapping, sector_t block) |
201 | { | 201 | { |
202 | return generic_block_bmap(mapping, block, fat_get_block); | 202 | return generic_block_bmap(mapping, block, fat_get_block); |
203 | } | 203 | } |
204 | 204 | ||
205 | static const struct address_space_operations fat_aops = { | 205 | static const struct address_space_operations fat_aops = { |
206 | .readpage = fat_readpage, | 206 | .readpage = fat_readpage, |
207 | .readpages = fat_readpages, | 207 | .readpages = fat_readpages, |
208 | .writepage = fat_writepage, | 208 | .writepage = fat_writepage, |
209 | .writepages = fat_writepages, | 209 | .writepages = fat_writepages, |
210 | .sync_page = block_sync_page, | 210 | .sync_page = block_sync_page, |
211 | .write_begin = fat_write_begin, | 211 | .write_begin = fat_write_begin, |
212 | .write_end = fat_write_end, | 212 | .write_end = fat_write_end, |
213 | .direct_IO = fat_direct_IO, | 213 | .direct_IO = fat_direct_IO, |
214 | .bmap = _fat_bmap | 214 | .bmap = _fat_bmap |
215 | }; | 215 | }; |
216 | 216 | ||
217 | /* | 217 | /* |
218 | * New FAT inode stuff. We do the following: | 218 | * New FAT inode stuff. We do the following: |
219 | * a) i_ino is constant and has nothing with on-disk location. | 219 | * a) i_ino is constant and has nothing with on-disk location. |
220 | * b) FAT manages its own cache of directory entries. | 220 | * b) FAT manages its own cache of directory entries. |
221 | * c) *This* cache is indexed by on-disk location. | 221 | * c) *This* cache is indexed by on-disk location. |
222 | * d) inode has an associated directory entry, all right, but | 222 | * d) inode has an associated directory entry, all right, but |
223 | * it may be unhashed. | 223 | * it may be unhashed. |
224 | * e) currently entries are stored within struct inode. That should | 224 | * e) currently entries are stored within struct inode. That should |
225 | * change. | 225 | * change. |
226 | * f) we deal with races in the following way: | 226 | * f) we deal with races in the following way: |
227 | * 1. readdir() and lookup() do FAT-dir-cache lookup. | 227 | * 1. readdir() and lookup() do FAT-dir-cache lookup. |
228 | * 2. rename() unhashes the F-d-c entry and rehashes it in | 228 | * 2. rename() unhashes the F-d-c entry and rehashes it in |
229 | * a new place. | 229 | * a new place. |
230 | * 3. unlink() and rmdir() unhash F-d-c entry. | 230 | * 3. unlink() and rmdir() unhash F-d-c entry. |
231 | * 4. fat_write_inode() checks whether the thing is unhashed. | 231 | * 4. fat_write_inode() checks whether the thing is unhashed. |
232 | * If it is we silently return. If it isn't we do bread(), | 232 | * If it is we silently return. If it isn't we do bread(), |
233 | * check if the location is still valid and retry if it | 233 | * check if the location is still valid and retry if it |
234 | * isn't. Otherwise we do changes. | 234 | * isn't. Otherwise we do changes. |
235 | * 5. Spinlock is used to protect hash/unhash/location check/lookup | 235 | * 5. Spinlock is used to protect hash/unhash/location check/lookup |
236 | * 6. fat_clear_inode() unhashes the F-d-c entry. | 236 | * 6. fat_clear_inode() unhashes the F-d-c entry. |
237 | * 7. lookup() and readdir() do igrab() if they find a F-d-c entry | 237 | * 7. lookup() and readdir() do igrab() if they find a F-d-c entry |
238 | * and consider negative result as cache miss. | 238 | * and consider negative result as cache miss. |
239 | */ | 239 | */ |
240 | 240 | ||
241 | static void fat_hash_init(struct super_block *sb) | 241 | static void fat_hash_init(struct super_block *sb) |
242 | { | 242 | { |
243 | struct msdos_sb_info *sbi = MSDOS_SB(sb); | 243 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
244 | int i; | 244 | int i; |
245 | 245 | ||
246 | spin_lock_init(&sbi->inode_hash_lock); | 246 | spin_lock_init(&sbi->inode_hash_lock); |
247 | for (i = 0; i < FAT_HASH_SIZE; i++) | 247 | for (i = 0; i < FAT_HASH_SIZE; i++) |
248 | INIT_HLIST_HEAD(&sbi->inode_hashtable[i]); | 248 | INIT_HLIST_HEAD(&sbi->inode_hashtable[i]); |
249 | } | 249 | } |
250 | 250 | ||
251 | static inline unsigned long fat_hash(loff_t i_pos) | 251 | static inline unsigned long fat_hash(loff_t i_pos) |
252 | { | 252 | { |
253 | return hash_32(i_pos, FAT_HASH_BITS); | 253 | return hash_32(i_pos, FAT_HASH_BITS); |
254 | } | 254 | } |
255 | 255 | ||
256 | void fat_attach(struct inode *inode, loff_t i_pos) | 256 | void fat_attach(struct inode *inode, loff_t i_pos) |
257 | { | 257 | { |
258 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); | 258 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); |
259 | struct hlist_head *head = sbi->inode_hashtable + fat_hash(i_pos); | 259 | struct hlist_head *head = sbi->inode_hashtable + fat_hash(i_pos); |
260 | 260 | ||
261 | spin_lock(&sbi->inode_hash_lock); | 261 | spin_lock(&sbi->inode_hash_lock); |
262 | MSDOS_I(inode)->i_pos = i_pos; | 262 | MSDOS_I(inode)->i_pos = i_pos; |
263 | hlist_add_head(&MSDOS_I(inode)->i_fat_hash, head); | 263 | hlist_add_head(&MSDOS_I(inode)->i_fat_hash, head); |
264 | spin_unlock(&sbi->inode_hash_lock); | 264 | spin_unlock(&sbi->inode_hash_lock); |
265 | } | 265 | } |
266 | EXPORT_SYMBOL_GPL(fat_attach); | 266 | EXPORT_SYMBOL_GPL(fat_attach); |
267 | 267 | ||
268 | void fat_detach(struct inode *inode) | 268 | void fat_detach(struct inode *inode) |
269 | { | 269 | { |
270 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); | 270 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); |
271 | spin_lock(&sbi->inode_hash_lock); | 271 | spin_lock(&sbi->inode_hash_lock); |
272 | MSDOS_I(inode)->i_pos = 0; | 272 | MSDOS_I(inode)->i_pos = 0; |
273 | hlist_del_init(&MSDOS_I(inode)->i_fat_hash); | 273 | hlist_del_init(&MSDOS_I(inode)->i_fat_hash); |
274 | spin_unlock(&sbi->inode_hash_lock); | 274 | spin_unlock(&sbi->inode_hash_lock); |
275 | } | 275 | } |
276 | EXPORT_SYMBOL_GPL(fat_detach); | 276 | EXPORT_SYMBOL_GPL(fat_detach); |
277 | 277 | ||
278 | struct inode *fat_iget(struct super_block *sb, loff_t i_pos) | 278 | struct inode *fat_iget(struct super_block *sb, loff_t i_pos) |
279 | { | 279 | { |
280 | struct msdos_sb_info *sbi = MSDOS_SB(sb); | 280 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
281 | struct hlist_head *head = sbi->inode_hashtable + fat_hash(i_pos); | 281 | struct hlist_head *head = sbi->inode_hashtable + fat_hash(i_pos); |
282 | struct hlist_node *_p; | 282 | struct hlist_node *_p; |
283 | struct msdos_inode_info *i; | 283 | struct msdos_inode_info *i; |
284 | struct inode *inode = NULL; | 284 | struct inode *inode = NULL; |
285 | 285 | ||
286 | spin_lock(&sbi->inode_hash_lock); | 286 | spin_lock(&sbi->inode_hash_lock); |
287 | hlist_for_each_entry(i, _p, head, i_fat_hash) { | 287 | hlist_for_each_entry(i, _p, head, i_fat_hash) { |
288 | BUG_ON(i->vfs_inode.i_sb != sb); | 288 | BUG_ON(i->vfs_inode.i_sb != sb); |
289 | if (i->i_pos != i_pos) | 289 | if (i->i_pos != i_pos) |
290 | continue; | 290 | continue; |
291 | inode = igrab(&i->vfs_inode); | 291 | inode = igrab(&i->vfs_inode); |
292 | if (inode) | 292 | if (inode) |
293 | break; | 293 | break; |
294 | } | 294 | } |
295 | spin_unlock(&sbi->inode_hash_lock); | 295 | spin_unlock(&sbi->inode_hash_lock); |
296 | return inode; | 296 | return inode; |
297 | } | 297 | } |
298 | 298 | ||
299 | static int is_exec(unsigned char *extension) | 299 | static int is_exec(unsigned char *extension) |
300 | { | 300 | { |
301 | unsigned char *exe_extensions = "EXECOMBAT", *walk; | 301 | unsigned char *exe_extensions = "EXECOMBAT", *walk; |
302 | 302 | ||
303 | for (walk = exe_extensions; *walk; walk += 3) | 303 | for (walk = exe_extensions; *walk; walk += 3) |
304 | if (!strncmp(extension, walk, 3)) | 304 | if (!strncmp(extension, walk, 3)) |
305 | return 1; | 305 | return 1; |
306 | return 0; | 306 | return 0; |
307 | } | 307 | } |
308 | 308 | ||
309 | static int fat_calc_dir_size(struct inode *inode) | 309 | static int fat_calc_dir_size(struct inode *inode) |
310 | { | 310 | { |
311 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); | 311 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); |
312 | int ret, fclus, dclus; | 312 | int ret, fclus, dclus; |
313 | 313 | ||
314 | inode->i_size = 0; | 314 | inode->i_size = 0; |
315 | if (MSDOS_I(inode)->i_start == 0) | 315 | if (MSDOS_I(inode)->i_start == 0) |
316 | return 0; | 316 | return 0; |
317 | 317 | ||
318 | ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus); | 318 | ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus); |
319 | if (ret < 0) | 319 | if (ret < 0) |
320 | return ret; | 320 | return ret; |
321 | inode->i_size = (fclus + 1) << sbi->cluster_bits; | 321 | inode->i_size = (fclus + 1) << sbi->cluster_bits; |
322 | 322 | ||
323 | return 0; | 323 | return 0; |
324 | } | 324 | } |
325 | 325 | ||
326 | /* doesn't deal with root inode */ | 326 | /* doesn't deal with root inode */ |
327 | static int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de) | 327 | static int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de) |
328 | { | 328 | { |
329 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); | 329 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); |
330 | int error; | 330 | int error; |
331 | 331 | ||
332 | MSDOS_I(inode)->i_pos = 0; | 332 | MSDOS_I(inode)->i_pos = 0; |
333 | inode->i_uid = sbi->options.fs_uid; | 333 | inode->i_uid = sbi->options.fs_uid; |
334 | inode->i_gid = sbi->options.fs_gid; | 334 | inode->i_gid = sbi->options.fs_gid; |
335 | inode->i_version++; | 335 | inode->i_version++; |
336 | inode->i_generation = get_seconds(); | 336 | inode->i_generation = get_seconds(); |
337 | 337 | ||
338 | if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) { | 338 | if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) { |
339 | inode->i_generation &= ~1; | 339 | inode->i_generation &= ~1; |
340 | inode->i_mode = fat_make_mode(sbi, de->attr, S_IRWXUGO); | 340 | inode->i_mode = fat_make_mode(sbi, de->attr, S_IRWXUGO); |
341 | inode->i_op = sbi->dir_ops; | 341 | inode->i_op = sbi->dir_ops; |
342 | inode->i_fop = &fat_dir_operations; | 342 | inode->i_fop = &fat_dir_operations; |
343 | 343 | ||
344 | MSDOS_I(inode)->i_start = le16_to_cpu(de->start); | 344 | MSDOS_I(inode)->i_start = le16_to_cpu(de->start); |
345 | if (sbi->fat_bits == 32) | 345 | if (sbi->fat_bits == 32) |
346 | MSDOS_I(inode)->i_start |= (le16_to_cpu(de->starthi) << 16); | 346 | MSDOS_I(inode)->i_start |= (le16_to_cpu(de->starthi) << 16); |
347 | 347 | ||
348 | MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start; | 348 | MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start; |
349 | error = fat_calc_dir_size(inode); | 349 | error = fat_calc_dir_size(inode); |
350 | if (error < 0) | 350 | if (error < 0) |
351 | return error; | 351 | return error; |
352 | MSDOS_I(inode)->mmu_private = inode->i_size; | 352 | MSDOS_I(inode)->mmu_private = inode->i_size; |
353 | 353 | ||
354 | inode->i_nlink = fat_subdirs(inode); | 354 | inode->i_nlink = fat_subdirs(inode); |
355 | } else { /* not a directory */ | 355 | } else { /* not a directory */ |
356 | inode->i_generation |= 1; | 356 | inode->i_generation |= 1; |
357 | inode->i_mode = fat_make_mode(sbi, de->attr, | 357 | inode->i_mode = fat_make_mode(sbi, de->attr, |
358 | ((sbi->options.showexec && !is_exec(de->name + 8)) | 358 | ((sbi->options.showexec && !is_exec(de->name + 8)) |
359 | ? S_IRUGO|S_IWUGO : S_IRWXUGO)); | 359 | ? S_IRUGO|S_IWUGO : S_IRWXUGO)); |
360 | MSDOS_I(inode)->i_start = le16_to_cpu(de->start); | 360 | MSDOS_I(inode)->i_start = le16_to_cpu(de->start); |
361 | if (sbi->fat_bits == 32) | 361 | if (sbi->fat_bits == 32) |
362 | MSDOS_I(inode)->i_start |= (le16_to_cpu(de->starthi) << 16); | 362 | MSDOS_I(inode)->i_start |= (le16_to_cpu(de->starthi) << 16); |
363 | 363 | ||
364 | MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start; | 364 | MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start; |
365 | inode->i_size = le32_to_cpu(de->size); | 365 | inode->i_size = le32_to_cpu(de->size); |
366 | inode->i_op = &fat_file_inode_operations; | 366 | inode->i_op = &fat_file_inode_operations; |
367 | inode->i_fop = &fat_file_operations; | 367 | inode->i_fop = &fat_file_operations; |
368 | inode->i_mapping->a_ops = &fat_aops; | 368 | inode->i_mapping->a_ops = &fat_aops; |
369 | MSDOS_I(inode)->mmu_private = inode->i_size; | 369 | MSDOS_I(inode)->mmu_private = inode->i_size; |
370 | } | 370 | } |
371 | if (de->attr & ATTR_SYS) { | 371 | if (de->attr & ATTR_SYS) { |
372 | if (sbi->options.sys_immutable) | 372 | if (sbi->options.sys_immutable) |
373 | inode->i_flags |= S_IMMUTABLE; | 373 | inode->i_flags |= S_IMMUTABLE; |
374 | } | 374 | } |
375 | fat_save_attrs(inode, de->attr); | 375 | fat_save_attrs(inode, de->attr); |
376 | 376 | ||
377 | inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1)) | 377 | inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1)) |
378 | & ~((loff_t)sbi->cluster_size - 1)) >> 9; | 378 | & ~((loff_t)sbi->cluster_size - 1)) >> 9; |
379 | 379 | ||
380 | fat_time_fat2unix(sbi, &inode->i_mtime, de->time, de->date, 0); | 380 | fat_time_fat2unix(sbi, &inode->i_mtime, de->time, de->date, 0); |
381 | if (sbi->options.isvfat) { | 381 | if (sbi->options.isvfat) { |
382 | fat_time_fat2unix(sbi, &inode->i_ctime, de->ctime, | 382 | fat_time_fat2unix(sbi, &inode->i_ctime, de->ctime, |
383 | de->cdate, de->ctime_cs); | 383 | de->cdate, de->ctime_cs); |
384 | fat_time_fat2unix(sbi, &inode->i_atime, 0, de->adate, 0); | 384 | fat_time_fat2unix(sbi, &inode->i_atime, 0, de->adate, 0); |
385 | } else | 385 | } else |
386 | inode->i_ctime = inode->i_atime = inode->i_mtime; | 386 | inode->i_ctime = inode->i_atime = inode->i_mtime; |
387 | 387 | ||
388 | return 0; | 388 | return 0; |
389 | } | 389 | } |
390 | 390 | ||
391 | struct inode *fat_build_inode(struct super_block *sb, | 391 | struct inode *fat_build_inode(struct super_block *sb, |
392 | struct msdos_dir_entry *de, loff_t i_pos) | 392 | struct msdos_dir_entry *de, loff_t i_pos) |
393 | { | 393 | { |
394 | struct inode *inode; | 394 | struct inode *inode; |
395 | int err; | 395 | int err; |
396 | 396 | ||
397 | inode = fat_iget(sb, i_pos); | 397 | inode = fat_iget(sb, i_pos); |
398 | if (inode) | 398 | if (inode) |
399 | goto out; | 399 | goto out; |
400 | inode = new_inode(sb); | 400 | inode = new_inode(sb); |
401 | if (!inode) { | 401 | if (!inode) { |
402 | inode = ERR_PTR(-ENOMEM); | 402 | inode = ERR_PTR(-ENOMEM); |
403 | goto out; | 403 | goto out; |
404 | } | 404 | } |
405 | inode->i_ino = iunique(sb, MSDOS_ROOT_INO); | 405 | inode->i_ino = iunique(sb, MSDOS_ROOT_INO); |
406 | inode->i_version = 1; | 406 | inode->i_version = 1; |
407 | err = fat_fill_inode(inode, de); | 407 | err = fat_fill_inode(inode, de); |
408 | if (err) { | 408 | if (err) { |
409 | iput(inode); | 409 | iput(inode); |
410 | inode = ERR_PTR(err); | 410 | inode = ERR_PTR(err); |
411 | goto out; | 411 | goto out; |
412 | } | 412 | } |
413 | fat_attach(inode, i_pos); | 413 | fat_attach(inode, i_pos); |
414 | insert_inode_hash(inode); | 414 | insert_inode_hash(inode); |
415 | out: | 415 | out: |
416 | return inode; | 416 | return inode; |
417 | } | 417 | } |
418 | 418 | ||
419 | EXPORT_SYMBOL_GPL(fat_build_inode); | 419 | EXPORT_SYMBOL_GPL(fat_build_inode); |
420 | 420 | ||
421 | static void fat_delete_inode(struct inode *inode) | 421 | static void fat_delete_inode(struct inode *inode) |
422 | { | 422 | { |
423 | truncate_inode_pages(&inode->i_data, 0); | 423 | truncate_inode_pages(&inode->i_data, 0); |
424 | inode->i_size = 0; | 424 | inode->i_size = 0; |
425 | fat_truncate(inode); | 425 | fat_truncate(inode); |
426 | clear_inode(inode); | 426 | clear_inode(inode); |
427 | } | 427 | } |
428 | 428 | ||
429 | static void fat_clear_inode(struct inode *inode) | 429 | static void fat_clear_inode(struct inode *inode) |
430 | { | 430 | { |
431 | fat_cache_inval_inode(inode); | 431 | fat_cache_inval_inode(inode); |
432 | fat_detach(inode); | 432 | fat_detach(inode); |
433 | } | 433 | } |
434 | 434 | ||
435 | static void fat_write_super(struct super_block *sb) | 435 | static void fat_write_super(struct super_block *sb) |
436 | { | 436 | { |
437 | sb->s_dirt = 0; | 437 | sb->s_dirt = 0; |
438 | 438 | ||
439 | if (!(sb->s_flags & MS_RDONLY)) | 439 | if (!(sb->s_flags & MS_RDONLY)) |
440 | fat_clusters_flush(sb); | 440 | fat_clusters_flush(sb); |
441 | } | 441 | } |
442 | 442 | ||
443 | static void fat_put_super(struct super_block *sb) | 443 | static void fat_put_super(struct super_block *sb) |
444 | { | 444 | { |
445 | struct msdos_sb_info *sbi = MSDOS_SB(sb); | 445 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
446 | 446 | ||
447 | if (sbi->nls_disk) { | 447 | if (sbi->nls_disk) { |
448 | unload_nls(sbi->nls_disk); | 448 | unload_nls(sbi->nls_disk); |
449 | sbi->nls_disk = NULL; | 449 | sbi->nls_disk = NULL; |
450 | sbi->options.codepage = fat_default_codepage; | 450 | sbi->options.codepage = fat_default_codepage; |
451 | } | 451 | } |
452 | if (sbi->nls_io) { | 452 | if (sbi->nls_io) { |
453 | unload_nls(sbi->nls_io); | 453 | unload_nls(sbi->nls_io); |
454 | sbi->nls_io = NULL; | 454 | sbi->nls_io = NULL; |
455 | } | 455 | } |
456 | if (sbi->options.iocharset != fat_default_iocharset) { | 456 | if (sbi->options.iocharset != fat_default_iocharset) { |
457 | kfree(sbi->options.iocharset); | 457 | kfree(sbi->options.iocharset); |
458 | sbi->options.iocharset = fat_default_iocharset; | 458 | sbi->options.iocharset = fat_default_iocharset; |
459 | } | 459 | } |
460 | 460 | ||
461 | sb->s_fs_info = NULL; | 461 | sb->s_fs_info = NULL; |
462 | kfree(sbi); | 462 | kfree(sbi); |
463 | } | 463 | } |
464 | 464 | ||
465 | static struct kmem_cache *fat_inode_cachep; | 465 | static struct kmem_cache *fat_inode_cachep; |
466 | 466 | ||
467 | static struct inode *fat_alloc_inode(struct super_block *sb) | 467 | static struct inode *fat_alloc_inode(struct super_block *sb) |
468 | { | 468 | { |
469 | struct msdos_inode_info *ei; | 469 | struct msdos_inode_info *ei; |
470 | ei = kmem_cache_alloc(fat_inode_cachep, GFP_NOFS); | 470 | ei = kmem_cache_alloc(fat_inode_cachep, GFP_NOFS); |
471 | if (!ei) | 471 | if (!ei) |
472 | return NULL; | 472 | return NULL; |
473 | return &ei->vfs_inode; | 473 | return &ei->vfs_inode; |
474 | } | 474 | } |
475 | 475 | ||
476 | static void fat_destroy_inode(struct inode *inode) | 476 | static void fat_destroy_inode(struct inode *inode) |
477 | { | 477 | { |
478 | kmem_cache_free(fat_inode_cachep, MSDOS_I(inode)); | 478 | kmem_cache_free(fat_inode_cachep, MSDOS_I(inode)); |
479 | } | 479 | } |
480 | 480 | ||
481 | static void init_once(void *foo) | 481 | static void init_once(void *foo) |
482 | { | 482 | { |
483 | struct msdos_inode_info *ei = (struct msdos_inode_info *)foo; | 483 | struct msdos_inode_info *ei = (struct msdos_inode_info *)foo; |
484 | 484 | ||
485 | spin_lock_init(&ei->cache_lru_lock); | 485 | spin_lock_init(&ei->cache_lru_lock); |
486 | ei->nr_caches = 0; | 486 | ei->nr_caches = 0; |
487 | ei->cache_valid_id = FAT_CACHE_VALID + 1; | 487 | ei->cache_valid_id = FAT_CACHE_VALID + 1; |
488 | INIT_LIST_HEAD(&ei->cache_lru); | 488 | INIT_LIST_HEAD(&ei->cache_lru); |
489 | INIT_HLIST_NODE(&ei->i_fat_hash); | 489 | INIT_HLIST_NODE(&ei->i_fat_hash); |
490 | inode_init_once(&ei->vfs_inode); | 490 | inode_init_once(&ei->vfs_inode); |
491 | } | 491 | } |
492 | 492 | ||
493 | static int __init fat_init_inodecache(void) | 493 | static int __init fat_init_inodecache(void) |
494 | { | 494 | { |
495 | fat_inode_cachep = kmem_cache_create("fat_inode_cache", | 495 | fat_inode_cachep = kmem_cache_create("fat_inode_cache", |
496 | sizeof(struct msdos_inode_info), | 496 | sizeof(struct msdos_inode_info), |
497 | 0, (SLAB_RECLAIM_ACCOUNT| | 497 | 0, (SLAB_RECLAIM_ACCOUNT| |
498 | SLAB_MEM_SPREAD), | 498 | SLAB_MEM_SPREAD), |
499 | init_once); | 499 | init_once); |
500 | if (fat_inode_cachep == NULL) | 500 | if (fat_inode_cachep == NULL) |
501 | return -ENOMEM; | 501 | return -ENOMEM; |
502 | return 0; | 502 | return 0; |
503 | } | 503 | } |
504 | 504 | ||
505 | static void __exit fat_destroy_inodecache(void) | 505 | static void __exit fat_destroy_inodecache(void) |
506 | { | 506 | { |
507 | kmem_cache_destroy(fat_inode_cachep); | 507 | kmem_cache_destroy(fat_inode_cachep); |
508 | } | 508 | } |
509 | 509 | ||
510 | static int fat_remount(struct super_block *sb, int *flags, char *data) | 510 | static int fat_remount(struct super_block *sb, int *flags, char *data) |
511 | { | 511 | { |
512 | struct msdos_sb_info *sbi = MSDOS_SB(sb); | 512 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
513 | *flags |= MS_NODIRATIME | (sbi->options.isvfat ? 0 : MS_NOATIME); | 513 | *flags |= MS_NODIRATIME | (sbi->options.isvfat ? 0 : MS_NOATIME); |
514 | return 0; | 514 | return 0; |
515 | } | 515 | } |
516 | 516 | ||
517 | static int fat_statfs(struct dentry *dentry, struct kstatfs *buf) | 517 | static int fat_statfs(struct dentry *dentry, struct kstatfs *buf) |
518 | { | 518 | { |
519 | struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb); | 519 | struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb); |
520 | 520 | ||
521 | /* If the count of free cluster is still unknown, counts it here. */ | 521 | /* If the count of free cluster is still unknown, counts it here. */ |
522 | if (sbi->free_clusters == -1 || !sbi->free_clus_valid) { | 522 | if (sbi->free_clusters == -1 || !sbi->free_clus_valid) { |
523 | int err = fat_count_free_clusters(dentry->d_sb); | 523 | int err = fat_count_free_clusters(dentry->d_sb); |
524 | if (err) | 524 | if (err) |
525 | return err; | 525 | return err; |
526 | } | 526 | } |
527 | 527 | ||
528 | buf->f_type = dentry->d_sb->s_magic; | 528 | buf->f_type = dentry->d_sb->s_magic; |
529 | buf->f_bsize = sbi->cluster_size; | 529 | buf->f_bsize = sbi->cluster_size; |
530 | buf->f_blocks = sbi->max_cluster - FAT_START_ENT; | 530 | buf->f_blocks = sbi->max_cluster - FAT_START_ENT; |
531 | buf->f_bfree = sbi->free_clusters; | 531 | buf->f_bfree = sbi->free_clusters; |
532 | buf->f_bavail = sbi->free_clusters; | 532 | buf->f_bavail = sbi->free_clusters; |
533 | buf->f_namelen = sbi->options.isvfat ? 260 : 12; | 533 | buf->f_namelen = sbi->options.isvfat ? 260 : 12; |
534 | 534 | ||
535 | return 0; | 535 | return 0; |
536 | } | 536 | } |
537 | 537 | ||
538 | static int fat_write_inode(struct inode *inode, int wait) | 538 | static int fat_write_inode(struct inode *inode, int wait) |
539 | { | 539 | { |
540 | struct super_block *sb = inode->i_sb; | 540 | struct super_block *sb = inode->i_sb; |
541 | struct msdos_sb_info *sbi = MSDOS_SB(sb); | 541 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
542 | struct buffer_head *bh; | 542 | struct buffer_head *bh; |
543 | struct msdos_dir_entry *raw_entry; | 543 | struct msdos_dir_entry *raw_entry; |
544 | loff_t i_pos; | 544 | loff_t i_pos; |
545 | int err; | 545 | int err; |
546 | 546 | ||
547 | retry: | 547 | retry: |
548 | i_pos = MSDOS_I(inode)->i_pos; | 548 | i_pos = MSDOS_I(inode)->i_pos; |
549 | if (inode->i_ino == MSDOS_ROOT_INO || !i_pos) | 549 | if (inode->i_ino == MSDOS_ROOT_INO || !i_pos) |
550 | return 0; | 550 | return 0; |
551 | 551 | ||
552 | bh = sb_bread(sb, i_pos >> sbi->dir_per_block_bits); | 552 | bh = sb_bread(sb, i_pos >> sbi->dir_per_block_bits); |
553 | if (!bh) { | 553 | if (!bh) { |
554 | printk(KERN_ERR "FAT: unable to read inode block " | 554 | printk(KERN_ERR "FAT: unable to read inode block " |
555 | "for updating (i_pos %lld)\n", i_pos); | 555 | "for updating (i_pos %lld)\n", i_pos); |
556 | return -EIO; | 556 | return -EIO; |
557 | } | 557 | } |
558 | spin_lock(&sbi->inode_hash_lock); | 558 | spin_lock(&sbi->inode_hash_lock); |
559 | if (i_pos != MSDOS_I(inode)->i_pos) { | 559 | if (i_pos != MSDOS_I(inode)->i_pos) { |
560 | spin_unlock(&sbi->inode_hash_lock); | 560 | spin_unlock(&sbi->inode_hash_lock); |
561 | brelse(bh); | 561 | brelse(bh); |
562 | goto retry; | 562 | goto retry; |
563 | } | 563 | } |
564 | 564 | ||
565 | raw_entry = &((struct msdos_dir_entry *) (bh->b_data)) | 565 | raw_entry = &((struct msdos_dir_entry *) (bh->b_data)) |
566 | [i_pos & (sbi->dir_per_block - 1)]; | 566 | [i_pos & (sbi->dir_per_block - 1)]; |
567 | if (S_ISDIR(inode->i_mode)) | 567 | if (S_ISDIR(inode->i_mode)) |
568 | raw_entry->size = 0; | 568 | raw_entry->size = 0; |
569 | else | 569 | else |
570 | raw_entry->size = cpu_to_le32(inode->i_size); | 570 | raw_entry->size = cpu_to_le32(inode->i_size); |
571 | raw_entry->attr = fat_make_attrs(inode); | 571 | raw_entry->attr = fat_make_attrs(inode); |
572 | raw_entry->start = cpu_to_le16(MSDOS_I(inode)->i_logstart); | 572 | raw_entry->start = cpu_to_le16(MSDOS_I(inode)->i_logstart); |
573 | raw_entry->starthi = cpu_to_le16(MSDOS_I(inode)->i_logstart >> 16); | 573 | raw_entry->starthi = cpu_to_le16(MSDOS_I(inode)->i_logstart >> 16); |
574 | fat_time_unix2fat(sbi, &inode->i_mtime, &raw_entry->time, | 574 | fat_time_unix2fat(sbi, &inode->i_mtime, &raw_entry->time, |
575 | &raw_entry->date, NULL); | 575 | &raw_entry->date, NULL); |
576 | if (sbi->options.isvfat) { | 576 | if (sbi->options.isvfat) { |
577 | __le16 atime; | 577 | __le16 atime; |
578 | fat_time_unix2fat(sbi, &inode->i_ctime, &raw_entry->ctime, | 578 | fat_time_unix2fat(sbi, &inode->i_ctime, &raw_entry->ctime, |
579 | &raw_entry->cdate, &raw_entry->ctime_cs); | 579 | &raw_entry->cdate, &raw_entry->ctime_cs); |
580 | fat_time_unix2fat(sbi, &inode->i_atime, &atime, | 580 | fat_time_unix2fat(sbi, &inode->i_atime, &atime, |
581 | &raw_entry->adate, NULL); | 581 | &raw_entry->adate, NULL); |
582 | } | 582 | } |
583 | spin_unlock(&sbi->inode_hash_lock); | 583 | spin_unlock(&sbi->inode_hash_lock); |
584 | mark_buffer_dirty(bh); | 584 | mark_buffer_dirty(bh); |
585 | err = 0; | 585 | err = 0; |
586 | if (wait) | 586 | if (wait) |
587 | err = sync_dirty_buffer(bh); | 587 | err = sync_dirty_buffer(bh); |
588 | brelse(bh); | 588 | brelse(bh); |
589 | return err; | 589 | return err; |
590 | } | 590 | } |
591 | 591 | ||
592 | int fat_sync_inode(struct inode *inode) | 592 | int fat_sync_inode(struct inode *inode) |
593 | { | 593 | { |
594 | return fat_write_inode(inode, 1); | 594 | return fat_write_inode(inode, 1); |
595 | } | 595 | } |
596 | 596 | ||
597 | EXPORT_SYMBOL_GPL(fat_sync_inode); | 597 | EXPORT_SYMBOL_GPL(fat_sync_inode); |
598 | 598 | ||
599 | static int fat_show_options(struct seq_file *m, struct vfsmount *mnt); | 599 | static int fat_show_options(struct seq_file *m, struct vfsmount *mnt); |
600 | static const struct super_operations fat_sops = { | 600 | static const struct super_operations fat_sops = { |
601 | .alloc_inode = fat_alloc_inode, | 601 | .alloc_inode = fat_alloc_inode, |
602 | .destroy_inode = fat_destroy_inode, | 602 | .destroy_inode = fat_destroy_inode, |
603 | .write_inode = fat_write_inode, | 603 | .write_inode = fat_write_inode, |
604 | .delete_inode = fat_delete_inode, | 604 | .delete_inode = fat_delete_inode, |
605 | .put_super = fat_put_super, | 605 | .put_super = fat_put_super, |
606 | .write_super = fat_write_super, | 606 | .write_super = fat_write_super, |
607 | .statfs = fat_statfs, | 607 | .statfs = fat_statfs, |
608 | .clear_inode = fat_clear_inode, | 608 | .clear_inode = fat_clear_inode, |
609 | .remount_fs = fat_remount, | 609 | .remount_fs = fat_remount, |
610 | 610 | ||
611 | .show_options = fat_show_options, | 611 | .show_options = fat_show_options, |
612 | }; | 612 | }; |
613 | 613 | ||
614 | /* | 614 | /* |
615 | * a FAT file handle with fhtype 3 is | 615 | * a FAT file handle with fhtype 3 is |
616 | * 0/ i_ino - for fast, reliable lookup if still in the cache | 616 | * 0/ i_ino - for fast, reliable lookup if still in the cache |
617 | * 1/ i_generation - to see if i_ino is still valid | 617 | * 1/ i_generation - to see if i_ino is still valid |
618 | * bit 0 == 0 iff directory | 618 | * bit 0 == 0 iff directory |
619 | * 2/ i_pos(8-39) - if ino has changed, but still in cache | 619 | * 2/ i_pos(8-39) - if ino has changed, but still in cache |
620 | * 3/ i_pos(4-7)|i_logstart - to semi-verify inode found at i_pos | 620 | * 3/ i_pos(4-7)|i_logstart - to semi-verify inode found at i_pos |
621 | * 4/ i_pos(0-3)|parent->i_logstart - maybe used to hunt for the file on disc | 621 | * 4/ i_pos(0-3)|parent->i_logstart - maybe used to hunt for the file on disc |
622 | * | 622 | * |
623 | * Hack for NFSv2: Maximum FAT entry number is 28bits and maximum | 623 | * Hack for NFSv2: Maximum FAT entry number is 28bits and maximum |
624 | * i_pos is 40bits (blocknr(32) + dir offset(8)), so two 4bits | 624 | * i_pos is 40bits (blocknr(32) + dir offset(8)), so two 4bits |
625 | * of i_logstart is used to store the directory entry offset. | 625 | * of i_logstart is used to store the directory entry offset. |
626 | */ | 626 | */ |
627 | 627 | ||
628 | static struct dentry *fat_fh_to_dentry(struct super_block *sb, | 628 | static struct dentry *fat_fh_to_dentry(struct super_block *sb, |
629 | struct fid *fid, int fh_len, int fh_type) | 629 | struct fid *fid, int fh_len, int fh_type) |
630 | { | 630 | { |
631 | struct inode *inode = NULL; | 631 | struct inode *inode = NULL; |
632 | struct dentry *result; | 632 | struct dentry *result; |
633 | u32 *fh = fid->raw; | 633 | u32 *fh = fid->raw; |
634 | 634 | ||
635 | if (fh_len < 5 || fh_type != 3) | 635 | if (fh_len < 5 || fh_type != 3) |
636 | return NULL; | 636 | return NULL; |
637 | 637 | ||
638 | inode = ilookup(sb, fh[0]); | 638 | inode = ilookup(sb, fh[0]); |
639 | if (!inode || inode->i_generation != fh[1]) { | 639 | if (!inode || inode->i_generation != fh[1]) { |
640 | if (inode) | 640 | if (inode) |
641 | iput(inode); | 641 | iput(inode); |
642 | inode = NULL; | 642 | inode = NULL; |
643 | } | 643 | } |
644 | if (!inode) { | 644 | if (!inode) { |
645 | loff_t i_pos; | 645 | loff_t i_pos; |
646 | int i_logstart = fh[3] & 0x0fffffff; | 646 | int i_logstart = fh[3] & 0x0fffffff; |
647 | 647 | ||
648 | i_pos = (loff_t)fh[2] << 8; | 648 | i_pos = (loff_t)fh[2] << 8; |
649 | i_pos |= ((fh[3] >> 24) & 0xf0) | (fh[4] >> 28); | 649 | i_pos |= ((fh[3] >> 24) & 0xf0) | (fh[4] >> 28); |
650 | 650 | ||
651 | /* try 2 - see if i_pos is in F-d-c | 651 | /* try 2 - see if i_pos is in F-d-c |
652 | * require i_logstart to be the same | 652 | * require i_logstart to be the same |
653 | * Will fail if you truncate and then re-write | 653 | * Will fail if you truncate and then re-write |
654 | */ | 654 | */ |
655 | 655 | ||
656 | inode = fat_iget(sb, i_pos); | 656 | inode = fat_iget(sb, i_pos); |
657 | if (inode && MSDOS_I(inode)->i_logstart != i_logstart) { | 657 | if (inode && MSDOS_I(inode)->i_logstart != i_logstart) { |
658 | iput(inode); | 658 | iput(inode); |
659 | inode = NULL; | 659 | inode = NULL; |
660 | } | 660 | } |
661 | } | 661 | } |
662 | 662 | ||
663 | /* | 663 | /* |
664 | * For now, do nothing if the inode is not found. | 664 | * For now, do nothing if the inode is not found. |
665 | * | 665 | * |
666 | * What we could do is: | 666 | * What we could do is: |
667 | * | 667 | * |
668 | * - follow the file starting at fh[4], and record the ".." entry, | 668 | * - follow the file starting at fh[4], and record the ".." entry, |
669 | * and the name of the fh[2] entry. | 669 | * and the name of the fh[2] entry. |
670 | * - then follow the ".." file finding the next step up. | 670 | * - then follow the ".." file finding the next step up. |
671 | * | 671 | * |
672 | * This way we build a path to the root of the tree. If this works, we | 672 | * This way we build a path to the root of the tree. If this works, we |
673 | * lookup the path and so get this inode into the cache. Finally try | 673 | * lookup the path and so get this inode into the cache. Finally try |
674 | * the fat_iget lookup again. If that fails, then we are totally out | 674 | * the fat_iget lookup again. If that fails, then we are totally out |
675 | * of luck. But all that is for another day | 675 | * of luck. But all that is for another day |
676 | */ | 676 | */ |
677 | result = d_obtain_alias(inode); | 677 | result = d_obtain_alias(inode); |
678 | if (!IS_ERR(result)) | 678 | if (!IS_ERR(result)) |
679 | result->d_op = sb->s_root->d_op; | 679 | result->d_op = sb->s_root->d_op; |
680 | return result; | 680 | return result; |
681 | } | 681 | } |
682 | 682 | ||
683 | static int | 683 | static int |
684 | fat_encode_fh(struct dentry *de, __u32 *fh, int *lenp, int connectable) | 684 | fat_encode_fh(struct dentry *de, __u32 *fh, int *lenp, int connectable) |
685 | { | 685 | { |
686 | int len = *lenp; | 686 | int len = *lenp; |
687 | struct inode *inode = de->d_inode; | 687 | struct inode *inode = de->d_inode; |
688 | u32 ipos_h, ipos_m, ipos_l; | 688 | u32 ipos_h, ipos_m, ipos_l; |
689 | 689 | ||
690 | if (len < 5) | 690 | if (len < 5) |
691 | return 255; /* no room */ | 691 | return 255; /* no room */ |
692 | 692 | ||
693 | ipos_h = MSDOS_I(inode)->i_pos >> 8; | 693 | ipos_h = MSDOS_I(inode)->i_pos >> 8; |
694 | ipos_m = (MSDOS_I(inode)->i_pos & 0xf0) << 24; | 694 | ipos_m = (MSDOS_I(inode)->i_pos & 0xf0) << 24; |
695 | ipos_l = (MSDOS_I(inode)->i_pos & 0x0f) << 28; | 695 | ipos_l = (MSDOS_I(inode)->i_pos & 0x0f) << 28; |
696 | *lenp = 5; | 696 | *lenp = 5; |
697 | fh[0] = inode->i_ino; | 697 | fh[0] = inode->i_ino; |
698 | fh[1] = inode->i_generation; | 698 | fh[1] = inode->i_generation; |
699 | fh[2] = ipos_h; | 699 | fh[2] = ipos_h; |
700 | fh[3] = ipos_m | MSDOS_I(inode)->i_logstart; | 700 | fh[3] = ipos_m | MSDOS_I(inode)->i_logstart; |
701 | spin_lock(&de->d_lock); | 701 | spin_lock(&de->d_lock); |
702 | fh[4] = ipos_l | MSDOS_I(de->d_parent->d_inode)->i_logstart; | 702 | fh[4] = ipos_l | MSDOS_I(de->d_parent->d_inode)->i_logstart; |
703 | spin_unlock(&de->d_lock); | 703 | spin_unlock(&de->d_lock); |
704 | return 3; | 704 | return 3; |
705 | } | 705 | } |
706 | 706 | ||
707 | static struct dentry *fat_get_parent(struct dentry *child) | 707 | static struct dentry *fat_get_parent(struct dentry *child) |
708 | { | 708 | { |
709 | struct super_block *sb = child->d_sb; | 709 | struct super_block *sb = child->d_sb; |
710 | struct buffer_head *bh; | 710 | struct buffer_head *bh; |
711 | struct msdos_dir_entry *de; | 711 | struct msdos_dir_entry *de; |
712 | loff_t i_pos; | 712 | loff_t i_pos; |
713 | struct dentry *parent; | 713 | struct dentry *parent; |
714 | struct inode *inode; | 714 | struct inode *inode; |
715 | int err; | 715 | int err; |
716 | 716 | ||
717 | lock_super(sb); | 717 | lock_super(sb); |
718 | 718 | ||
719 | err = fat_get_dotdot_entry(child->d_inode, &bh, &de, &i_pos); | 719 | err = fat_get_dotdot_entry(child->d_inode, &bh, &de, &i_pos); |
720 | if (err) { | 720 | if (err) { |
721 | parent = ERR_PTR(err); | 721 | parent = ERR_PTR(err); |
722 | goto out; | 722 | goto out; |
723 | } | 723 | } |
724 | inode = fat_build_inode(sb, de, i_pos); | 724 | inode = fat_build_inode(sb, de, i_pos); |
725 | brelse(bh); | 725 | brelse(bh); |
726 | 726 | ||
727 | parent = d_obtain_alias(inode); | 727 | parent = d_obtain_alias(inode); |
728 | out: | 728 | out: |
729 | unlock_super(sb); | 729 | unlock_super(sb); |
730 | 730 | ||
731 | return parent; | 731 | return parent; |
732 | } | 732 | } |
733 | 733 | ||
734 | static const struct export_operations fat_export_ops = { | 734 | static const struct export_operations fat_export_ops = { |
735 | .encode_fh = fat_encode_fh, | 735 | .encode_fh = fat_encode_fh, |
736 | .fh_to_dentry = fat_fh_to_dentry, | 736 | .fh_to_dentry = fat_fh_to_dentry, |
737 | .get_parent = fat_get_parent, | 737 | .get_parent = fat_get_parent, |
738 | }; | 738 | }; |
739 | 739 | ||
740 | static int fat_show_options(struct seq_file *m, struct vfsmount *mnt) | 740 | static int fat_show_options(struct seq_file *m, struct vfsmount *mnt) |
741 | { | 741 | { |
742 | struct msdos_sb_info *sbi = MSDOS_SB(mnt->mnt_sb); | 742 | struct msdos_sb_info *sbi = MSDOS_SB(mnt->mnt_sb); |
743 | struct fat_mount_options *opts = &sbi->options; | 743 | struct fat_mount_options *opts = &sbi->options; |
744 | int isvfat = opts->isvfat; | 744 | int isvfat = opts->isvfat; |
745 | 745 | ||
746 | if (opts->fs_uid != 0) | 746 | if (opts->fs_uid != 0) |
747 | seq_printf(m, ",uid=%u", opts->fs_uid); | 747 | seq_printf(m, ",uid=%u", opts->fs_uid); |
748 | if (opts->fs_gid != 0) | 748 | if (opts->fs_gid != 0) |
749 | seq_printf(m, ",gid=%u", opts->fs_gid); | 749 | seq_printf(m, ",gid=%u", opts->fs_gid); |
750 | seq_printf(m, ",fmask=%04o", opts->fs_fmask); | 750 | seq_printf(m, ",fmask=%04o", opts->fs_fmask); |
751 | seq_printf(m, ",dmask=%04o", opts->fs_dmask); | 751 | seq_printf(m, ",dmask=%04o", opts->fs_dmask); |
752 | if (opts->allow_utime) | 752 | if (opts->allow_utime) |
753 | seq_printf(m, ",allow_utime=%04o", opts->allow_utime); | 753 | seq_printf(m, ",allow_utime=%04o", opts->allow_utime); |
754 | if (sbi->nls_disk) | 754 | if (sbi->nls_disk) |
755 | seq_printf(m, ",codepage=%s", sbi->nls_disk->charset); | 755 | seq_printf(m, ",codepage=%s", sbi->nls_disk->charset); |
756 | if (isvfat) { | 756 | if (isvfat) { |
757 | if (sbi->nls_io) | 757 | if (sbi->nls_io) |
758 | seq_printf(m, ",iocharset=%s", sbi->nls_io->charset); | 758 | seq_printf(m, ",iocharset=%s", sbi->nls_io->charset); |
759 | 759 | ||
760 | switch (opts->shortname) { | 760 | switch (opts->shortname) { |
761 | case VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95: | 761 | case VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95: |
762 | seq_puts(m, ",shortname=win95"); | 762 | seq_puts(m, ",shortname=win95"); |
763 | break; | 763 | break; |
764 | case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT: | 764 | case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT: |
765 | seq_puts(m, ",shortname=winnt"); | 765 | seq_puts(m, ",shortname=winnt"); |
766 | break; | 766 | break; |
767 | case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95: | 767 | case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95: |
768 | seq_puts(m, ",shortname=mixed"); | 768 | seq_puts(m, ",shortname=mixed"); |
769 | break; | 769 | break; |
770 | case VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95: | 770 | case VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95: |
771 | /* seq_puts(m, ",shortname=lower"); */ | 771 | /* seq_puts(m, ",shortname=lower"); */ |
772 | break; | 772 | break; |
773 | default: | 773 | default: |
774 | seq_puts(m, ",shortname=unknown"); | 774 | seq_puts(m, ",shortname=unknown"); |
775 | break; | 775 | break; |
776 | } | 776 | } |
777 | } | 777 | } |
778 | if (opts->name_check != 'n') | 778 | if (opts->name_check != 'n') |
779 | seq_printf(m, ",check=%c", opts->name_check); | 779 | seq_printf(m, ",check=%c", opts->name_check); |
780 | if (opts->usefree) | 780 | if (opts->usefree) |
781 | seq_puts(m, ",usefree"); | 781 | seq_puts(m, ",usefree"); |
782 | if (opts->quiet) | 782 | if (opts->quiet) |
783 | seq_puts(m, ",quiet"); | 783 | seq_puts(m, ",quiet"); |
784 | if (opts->showexec) | 784 | if (opts->showexec) |
785 | seq_puts(m, ",showexec"); | 785 | seq_puts(m, ",showexec"); |
786 | if (opts->sys_immutable) | 786 | if (opts->sys_immutable) |
787 | seq_puts(m, ",sys_immutable"); | 787 | seq_puts(m, ",sys_immutable"); |
788 | if (!isvfat) { | 788 | if (!isvfat) { |
789 | if (opts->dotsOK) | 789 | if (opts->dotsOK) |
790 | seq_puts(m, ",dotsOK=yes"); | 790 | seq_puts(m, ",dotsOK=yes"); |
791 | if (opts->nocase) | 791 | if (opts->nocase) |
792 | seq_puts(m, ",nocase"); | 792 | seq_puts(m, ",nocase"); |
793 | } else { | 793 | } else { |
794 | if (opts->utf8) | 794 | if (opts->utf8) |
795 | seq_puts(m, ",utf8"); | 795 | seq_puts(m, ",utf8"); |
796 | if (opts->unicode_xlate) | 796 | if (opts->unicode_xlate) |
797 | seq_puts(m, ",uni_xlate"); | 797 | seq_puts(m, ",uni_xlate"); |
798 | if (!opts->numtail) | 798 | if (!opts->numtail) |
799 | seq_puts(m, ",nonumtail"); | 799 | seq_puts(m, ",nonumtail"); |
800 | if (opts->rodir) | ||
801 | seq_puts(m, ",rodir"); | ||
800 | } | 802 | } |
801 | if (sbi->options.flush) | 803 | if (opts->flush) |
802 | seq_puts(m, ",flush"); | 804 | seq_puts(m, ",flush"); |
803 | if (opts->tz_utc) | 805 | if (opts->tz_utc) |
804 | seq_puts(m, ",tz=UTC"); | 806 | seq_puts(m, ",tz=UTC"); |
805 | 807 | ||
806 | return 0; | 808 | return 0; |
807 | } | 809 | } |
808 | 810 | ||
809 | enum { | 811 | enum { |
810 | Opt_check_n, Opt_check_r, Opt_check_s, Opt_uid, Opt_gid, | 812 | Opt_check_n, Opt_check_r, Opt_check_s, Opt_uid, Opt_gid, |
811 | Opt_umask, Opt_dmask, Opt_fmask, Opt_allow_utime, Opt_codepage, | 813 | Opt_umask, Opt_dmask, Opt_fmask, Opt_allow_utime, Opt_codepage, |
812 | Opt_usefree, Opt_nocase, Opt_quiet, Opt_showexec, Opt_debug, | 814 | Opt_usefree, Opt_nocase, Opt_quiet, Opt_showexec, Opt_debug, |
813 | Opt_immutable, Opt_dots, Opt_nodots, | 815 | Opt_immutable, Opt_dots, Opt_nodots, |
814 | Opt_charset, Opt_shortname_lower, Opt_shortname_win95, | 816 | Opt_charset, Opt_shortname_lower, Opt_shortname_win95, |
815 | Opt_shortname_winnt, Opt_shortname_mixed, Opt_utf8_no, Opt_utf8_yes, | 817 | Opt_shortname_winnt, Opt_shortname_mixed, Opt_utf8_no, Opt_utf8_yes, |
816 | Opt_uni_xl_no, Opt_uni_xl_yes, Opt_nonumtail_no, Opt_nonumtail_yes, | 818 | Opt_uni_xl_no, Opt_uni_xl_yes, Opt_nonumtail_no, Opt_nonumtail_yes, |
817 | Opt_obsolate, Opt_flush, Opt_tz_utc, Opt_err, | 819 | Opt_obsolate, Opt_flush, Opt_tz_utc, Opt_rodir, Opt_err, |
818 | }; | 820 | }; |
819 | 821 | ||
820 | static const match_table_t fat_tokens = { | 822 | static const match_table_t fat_tokens = { |
821 | {Opt_check_r, "check=relaxed"}, | 823 | {Opt_check_r, "check=relaxed"}, |
822 | {Opt_check_s, "check=strict"}, | 824 | {Opt_check_s, "check=strict"}, |
823 | {Opt_check_n, "check=normal"}, | 825 | {Opt_check_n, "check=normal"}, |
824 | {Opt_check_r, "check=r"}, | 826 | {Opt_check_r, "check=r"}, |
825 | {Opt_check_s, "check=s"}, | 827 | {Opt_check_s, "check=s"}, |
826 | {Opt_check_n, "check=n"}, | 828 | {Opt_check_n, "check=n"}, |
827 | {Opt_uid, "uid=%u"}, | 829 | {Opt_uid, "uid=%u"}, |
828 | {Opt_gid, "gid=%u"}, | 830 | {Opt_gid, "gid=%u"}, |
829 | {Opt_umask, "umask=%o"}, | 831 | {Opt_umask, "umask=%o"}, |
830 | {Opt_dmask, "dmask=%o"}, | 832 | {Opt_dmask, "dmask=%o"}, |
831 | {Opt_fmask, "fmask=%o"}, | 833 | {Opt_fmask, "fmask=%o"}, |
832 | {Opt_allow_utime, "allow_utime=%o"}, | 834 | {Opt_allow_utime, "allow_utime=%o"}, |
833 | {Opt_codepage, "codepage=%u"}, | 835 | {Opt_codepage, "codepage=%u"}, |
834 | {Opt_usefree, "usefree"}, | 836 | {Opt_usefree, "usefree"}, |
835 | {Opt_nocase, "nocase"}, | 837 | {Opt_nocase, "nocase"}, |
836 | {Opt_quiet, "quiet"}, | 838 | {Opt_quiet, "quiet"}, |
837 | {Opt_showexec, "showexec"}, | 839 | {Opt_showexec, "showexec"}, |
838 | {Opt_debug, "debug"}, | 840 | {Opt_debug, "debug"}, |
839 | {Opt_immutable, "sys_immutable"}, | 841 | {Opt_immutable, "sys_immutable"}, |
840 | {Opt_obsolate, "conv=binary"}, | 842 | {Opt_obsolate, "conv=binary"}, |
841 | {Opt_obsolate, "conv=text"}, | 843 | {Opt_obsolate, "conv=text"}, |
842 | {Opt_obsolate, "conv=auto"}, | 844 | {Opt_obsolate, "conv=auto"}, |
843 | {Opt_obsolate, "conv=b"}, | 845 | {Opt_obsolate, "conv=b"}, |
844 | {Opt_obsolate, "conv=t"}, | 846 | {Opt_obsolate, "conv=t"}, |
845 | {Opt_obsolate, "conv=a"}, | 847 | {Opt_obsolate, "conv=a"}, |
846 | {Opt_obsolate, "fat=%u"}, | 848 | {Opt_obsolate, "fat=%u"}, |
847 | {Opt_obsolate, "blocksize=%u"}, | 849 | {Opt_obsolate, "blocksize=%u"}, |
848 | {Opt_obsolate, "cvf_format=%20s"}, | 850 | {Opt_obsolate, "cvf_format=%20s"}, |
849 | {Opt_obsolate, "cvf_options=%100s"}, | 851 | {Opt_obsolate, "cvf_options=%100s"}, |
850 | {Opt_obsolate, "posix"}, | 852 | {Opt_obsolate, "posix"}, |
851 | {Opt_flush, "flush"}, | 853 | {Opt_flush, "flush"}, |
852 | {Opt_tz_utc, "tz=UTC"}, | 854 | {Opt_tz_utc, "tz=UTC"}, |
853 | {Opt_err, NULL}, | 855 | {Opt_err, NULL}, |
854 | }; | 856 | }; |
855 | static const match_table_t msdos_tokens = { | 857 | static const match_table_t msdos_tokens = { |
856 | {Opt_nodots, "nodots"}, | 858 | {Opt_nodots, "nodots"}, |
857 | {Opt_nodots, "dotsOK=no"}, | 859 | {Opt_nodots, "dotsOK=no"}, |
858 | {Opt_dots, "dots"}, | 860 | {Opt_dots, "dots"}, |
859 | {Opt_dots, "dotsOK=yes"}, | 861 | {Opt_dots, "dotsOK=yes"}, |
860 | {Opt_err, NULL} | 862 | {Opt_err, NULL} |
861 | }; | 863 | }; |
862 | static const match_table_t vfat_tokens = { | 864 | static const match_table_t vfat_tokens = { |
863 | {Opt_charset, "iocharset=%s"}, | 865 | {Opt_charset, "iocharset=%s"}, |
864 | {Opt_shortname_lower, "shortname=lower"}, | 866 | {Opt_shortname_lower, "shortname=lower"}, |
865 | {Opt_shortname_win95, "shortname=win95"}, | 867 | {Opt_shortname_win95, "shortname=win95"}, |
866 | {Opt_shortname_winnt, "shortname=winnt"}, | 868 | {Opt_shortname_winnt, "shortname=winnt"}, |
867 | {Opt_shortname_mixed, "shortname=mixed"}, | 869 | {Opt_shortname_mixed, "shortname=mixed"}, |
868 | {Opt_utf8_no, "utf8=0"}, /* 0 or no or false */ | 870 | {Opt_utf8_no, "utf8=0"}, /* 0 or no or false */ |
869 | {Opt_utf8_no, "utf8=no"}, | 871 | {Opt_utf8_no, "utf8=no"}, |
870 | {Opt_utf8_no, "utf8=false"}, | 872 | {Opt_utf8_no, "utf8=false"}, |
871 | {Opt_utf8_yes, "utf8=1"}, /* empty or 1 or yes or true */ | 873 | {Opt_utf8_yes, "utf8=1"}, /* empty or 1 or yes or true */ |
872 | {Opt_utf8_yes, "utf8=yes"}, | 874 | {Opt_utf8_yes, "utf8=yes"}, |
873 | {Opt_utf8_yes, "utf8=true"}, | 875 | {Opt_utf8_yes, "utf8=true"}, |
874 | {Opt_utf8_yes, "utf8"}, | 876 | {Opt_utf8_yes, "utf8"}, |
875 | {Opt_uni_xl_no, "uni_xlate=0"}, /* 0 or no or false */ | 877 | {Opt_uni_xl_no, "uni_xlate=0"}, /* 0 or no or false */ |
876 | {Opt_uni_xl_no, "uni_xlate=no"}, | 878 | {Opt_uni_xl_no, "uni_xlate=no"}, |
877 | {Opt_uni_xl_no, "uni_xlate=false"}, | 879 | {Opt_uni_xl_no, "uni_xlate=false"}, |
878 | {Opt_uni_xl_yes, "uni_xlate=1"}, /* empty or 1 or yes or true */ | 880 | {Opt_uni_xl_yes, "uni_xlate=1"}, /* empty or 1 or yes or true */ |
879 | {Opt_uni_xl_yes, "uni_xlate=yes"}, | 881 | {Opt_uni_xl_yes, "uni_xlate=yes"}, |
880 | {Opt_uni_xl_yes, "uni_xlate=true"}, | 882 | {Opt_uni_xl_yes, "uni_xlate=true"}, |
881 | {Opt_uni_xl_yes, "uni_xlate"}, | 883 | {Opt_uni_xl_yes, "uni_xlate"}, |
882 | {Opt_nonumtail_no, "nonumtail=0"}, /* 0 or no or false */ | 884 | {Opt_nonumtail_no, "nonumtail=0"}, /* 0 or no or false */ |
883 | {Opt_nonumtail_no, "nonumtail=no"}, | 885 | {Opt_nonumtail_no, "nonumtail=no"}, |
884 | {Opt_nonumtail_no, "nonumtail=false"}, | 886 | {Opt_nonumtail_no, "nonumtail=false"}, |
885 | {Opt_nonumtail_yes, "nonumtail=1"}, /* empty or 1 or yes or true */ | 887 | {Opt_nonumtail_yes, "nonumtail=1"}, /* empty or 1 or yes or true */ |
886 | {Opt_nonumtail_yes, "nonumtail=yes"}, | 888 | {Opt_nonumtail_yes, "nonumtail=yes"}, |
887 | {Opt_nonumtail_yes, "nonumtail=true"}, | 889 | {Opt_nonumtail_yes, "nonumtail=true"}, |
888 | {Opt_nonumtail_yes, "nonumtail"}, | 890 | {Opt_nonumtail_yes, "nonumtail"}, |
891 | {Opt_rodir, "rodir"}, | ||
889 | {Opt_err, NULL} | 892 | {Opt_err, NULL} |
890 | }; | 893 | }; |
891 | 894 | ||
892 | static int parse_options(char *options, int is_vfat, int silent, int *debug, | 895 | static int parse_options(char *options, int is_vfat, int silent, int *debug, |
893 | struct fat_mount_options *opts) | 896 | struct fat_mount_options *opts) |
894 | { | 897 | { |
895 | char *p; | 898 | char *p; |
896 | substring_t args[MAX_OPT_ARGS]; | 899 | substring_t args[MAX_OPT_ARGS]; |
897 | int option; | 900 | int option; |
898 | char *iocharset; | 901 | char *iocharset; |
899 | 902 | ||
900 | opts->isvfat = is_vfat; | 903 | opts->isvfat = is_vfat; |
901 | 904 | ||
902 | opts->fs_uid = current->uid; | 905 | opts->fs_uid = current->uid; |
903 | opts->fs_gid = current->gid; | 906 | opts->fs_gid = current->gid; |
904 | opts->fs_fmask = opts->fs_dmask = current->fs->umask; | 907 | opts->fs_fmask = opts->fs_dmask = current->fs->umask; |
905 | opts->allow_utime = -1; | 908 | opts->allow_utime = -1; |
906 | opts->codepage = fat_default_codepage; | 909 | opts->codepage = fat_default_codepage; |
907 | opts->iocharset = fat_default_iocharset; | 910 | opts->iocharset = fat_default_iocharset; |
908 | if (is_vfat) | 911 | if (is_vfat) { |
909 | opts->shortname = VFAT_SFN_DISPLAY_LOWER|VFAT_SFN_CREATE_WIN95; | 912 | opts->shortname = VFAT_SFN_DISPLAY_LOWER|VFAT_SFN_CREATE_WIN95; |
910 | else | 913 | opts->rodir = 0; |
914 | } else { | ||
911 | opts->shortname = 0; | 915 | opts->shortname = 0; |
916 | opts->rodir = 1; | ||
917 | } | ||
912 | opts->name_check = 'n'; | 918 | opts->name_check = 'n'; |
913 | opts->quiet = opts->showexec = opts->sys_immutable = opts->dotsOK = 0; | 919 | opts->quiet = opts->showexec = opts->sys_immutable = opts->dotsOK = 0; |
914 | opts->utf8 = opts->unicode_xlate = 0; | 920 | opts->utf8 = opts->unicode_xlate = 0; |
915 | opts->numtail = 1; | 921 | opts->numtail = 1; |
916 | opts->usefree = opts->nocase = 0; | 922 | opts->usefree = opts->nocase = 0; |
917 | opts->tz_utc = 0; | 923 | opts->tz_utc = 0; |
918 | *debug = 0; | 924 | *debug = 0; |
919 | 925 | ||
920 | if (!options) | 926 | if (!options) |
921 | goto out; | 927 | goto out; |
922 | 928 | ||
923 | while ((p = strsep(&options, ",")) != NULL) { | 929 | while ((p = strsep(&options, ",")) != NULL) { |
924 | int token; | 930 | int token; |
925 | if (!*p) | 931 | if (!*p) |
926 | continue; | 932 | continue; |
927 | 933 | ||
928 | token = match_token(p, fat_tokens, args); | 934 | token = match_token(p, fat_tokens, args); |
929 | if (token == Opt_err) { | 935 | if (token == Opt_err) { |
930 | if (is_vfat) | 936 | if (is_vfat) |
931 | token = match_token(p, vfat_tokens, args); | 937 | token = match_token(p, vfat_tokens, args); |
932 | else | 938 | else |
933 | token = match_token(p, msdos_tokens, args); | 939 | token = match_token(p, msdos_tokens, args); |
934 | } | 940 | } |
935 | switch (token) { | 941 | switch (token) { |
936 | case Opt_check_s: | 942 | case Opt_check_s: |
937 | opts->name_check = 's'; | 943 | opts->name_check = 's'; |
938 | break; | 944 | break; |
939 | case Opt_check_r: | 945 | case Opt_check_r: |
940 | opts->name_check = 'r'; | 946 | opts->name_check = 'r'; |
941 | break; | 947 | break; |
942 | case Opt_check_n: | 948 | case Opt_check_n: |
943 | opts->name_check = 'n'; | 949 | opts->name_check = 'n'; |
944 | break; | 950 | break; |
945 | case Opt_usefree: | 951 | case Opt_usefree: |
946 | opts->usefree = 1; | 952 | opts->usefree = 1; |
947 | break; | 953 | break; |
948 | case Opt_nocase: | 954 | case Opt_nocase: |
949 | if (!is_vfat) | 955 | if (!is_vfat) |
950 | opts->nocase = 1; | 956 | opts->nocase = 1; |
951 | else { | 957 | else { |
952 | /* for backward compatibility */ | 958 | /* for backward compatibility */ |
953 | opts->shortname = VFAT_SFN_DISPLAY_WIN95 | 959 | opts->shortname = VFAT_SFN_DISPLAY_WIN95 |
954 | | VFAT_SFN_CREATE_WIN95; | 960 | | VFAT_SFN_CREATE_WIN95; |
955 | } | 961 | } |
956 | break; | 962 | break; |
957 | case Opt_quiet: | 963 | case Opt_quiet: |
958 | opts->quiet = 1; | 964 | opts->quiet = 1; |
959 | break; | 965 | break; |
960 | case Opt_showexec: | 966 | case Opt_showexec: |
961 | opts->showexec = 1; | 967 | opts->showexec = 1; |
962 | break; | 968 | break; |
963 | case Opt_debug: | 969 | case Opt_debug: |
964 | *debug = 1; | 970 | *debug = 1; |
965 | break; | 971 | break; |
966 | case Opt_immutable: | 972 | case Opt_immutable: |
967 | opts->sys_immutable = 1; | 973 | opts->sys_immutable = 1; |
968 | break; | 974 | break; |
969 | case Opt_uid: | 975 | case Opt_uid: |
970 | if (match_int(&args[0], &option)) | 976 | if (match_int(&args[0], &option)) |
971 | return 0; | 977 | return 0; |
972 | opts->fs_uid = option; | 978 | opts->fs_uid = option; |
973 | break; | 979 | break; |
974 | case Opt_gid: | 980 | case Opt_gid: |
975 | if (match_int(&args[0], &option)) | 981 | if (match_int(&args[0], &option)) |
976 | return 0; | 982 | return 0; |
977 | opts->fs_gid = option; | 983 | opts->fs_gid = option; |
978 | break; | 984 | break; |
979 | case Opt_umask: | 985 | case Opt_umask: |
980 | if (match_octal(&args[0], &option)) | 986 | if (match_octal(&args[0], &option)) |
981 | return 0; | 987 | return 0; |
982 | opts->fs_fmask = opts->fs_dmask = option; | 988 | opts->fs_fmask = opts->fs_dmask = option; |
983 | break; | 989 | break; |
984 | case Opt_dmask: | 990 | case Opt_dmask: |
985 | if (match_octal(&args[0], &option)) | 991 | if (match_octal(&args[0], &option)) |
986 | return 0; | 992 | return 0; |
987 | opts->fs_dmask = option; | 993 | opts->fs_dmask = option; |
988 | break; | 994 | break; |
989 | case Opt_fmask: | 995 | case Opt_fmask: |
990 | if (match_octal(&args[0], &option)) | 996 | if (match_octal(&args[0], &option)) |
991 | return 0; | 997 | return 0; |
992 | opts->fs_fmask = option; | 998 | opts->fs_fmask = option; |
993 | break; | 999 | break; |
994 | case Opt_allow_utime: | 1000 | case Opt_allow_utime: |
995 | if (match_octal(&args[0], &option)) | 1001 | if (match_octal(&args[0], &option)) |
996 | return 0; | 1002 | return 0; |
997 | opts->allow_utime = option & (S_IWGRP | S_IWOTH); | 1003 | opts->allow_utime = option & (S_IWGRP | S_IWOTH); |
998 | break; | 1004 | break; |
999 | case Opt_codepage: | 1005 | case Opt_codepage: |
1000 | if (match_int(&args[0], &option)) | 1006 | if (match_int(&args[0], &option)) |
1001 | return 0; | 1007 | return 0; |
1002 | opts->codepage = option; | 1008 | opts->codepage = option; |
1003 | break; | 1009 | break; |
1004 | case Opt_flush: | 1010 | case Opt_flush: |
1005 | opts->flush = 1; | 1011 | opts->flush = 1; |
1006 | break; | 1012 | break; |
1007 | case Opt_tz_utc: | 1013 | case Opt_tz_utc: |
1008 | opts->tz_utc = 1; | 1014 | opts->tz_utc = 1; |
1009 | break; | 1015 | break; |
1010 | 1016 | ||
1011 | /* msdos specific */ | 1017 | /* msdos specific */ |
1012 | case Opt_dots: | 1018 | case Opt_dots: |
1013 | opts->dotsOK = 1; | 1019 | opts->dotsOK = 1; |
1014 | break; | 1020 | break; |
1015 | case Opt_nodots: | 1021 | case Opt_nodots: |
1016 | opts->dotsOK = 0; | 1022 | opts->dotsOK = 0; |
1017 | break; | 1023 | break; |
1018 | 1024 | ||
1019 | /* vfat specific */ | 1025 | /* vfat specific */ |
1020 | case Opt_charset: | 1026 | case Opt_charset: |
1021 | if (opts->iocharset != fat_default_iocharset) | 1027 | if (opts->iocharset != fat_default_iocharset) |
1022 | kfree(opts->iocharset); | 1028 | kfree(opts->iocharset); |
1023 | iocharset = match_strdup(&args[0]); | 1029 | iocharset = match_strdup(&args[0]); |
1024 | if (!iocharset) | 1030 | if (!iocharset) |
1025 | return -ENOMEM; | 1031 | return -ENOMEM; |
1026 | opts->iocharset = iocharset; | 1032 | opts->iocharset = iocharset; |
1027 | break; | 1033 | break; |
1028 | case Opt_shortname_lower: | 1034 | case Opt_shortname_lower: |
1029 | opts->shortname = VFAT_SFN_DISPLAY_LOWER | 1035 | opts->shortname = VFAT_SFN_DISPLAY_LOWER |
1030 | | VFAT_SFN_CREATE_WIN95; | 1036 | | VFAT_SFN_CREATE_WIN95; |
1031 | break; | 1037 | break; |
1032 | case Opt_shortname_win95: | 1038 | case Opt_shortname_win95: |
1033 | opts->shortname = VFAT_SFN_DISPLAY_WIN95 | 1039 | opts->shortname = VFAT_SFN_DISPLAY_WIN95 |
1034 | | VFAT_SFN_CREATE_WIN95; | 1040 | | VFAT_SFN_CREATE_WIN95; |
1035 | break; | 1041 | break; |
1036 | case Opt_shortname_winnt: | 1042 | case Opt_shortname_winnt: |
1037 | opts->shortname = VFAT_SFN_DISPLAY_WINNT | 1043 | opts->shortname = VFAT_SFN_DISPLAY_WINNT |
1038 | | VFAT_SFN_CREATE_WINNT; | 1044 | | VFAT_SFN_CREATE_WINNT; |
1039 | break; | 1045 | break; |
1040 | case Opt_shortname_mixed: | 1046 | case Opt_shortname_mixed: |
1041 | opts->shortname = VFAT_SFN_DISPLAY_WINNT | 1047 | opts->shortname = VFAT_SFN_DISPLAY_WINNT |
1042 | | VFAT_SFN_CREATE_WIN95; | 1048 | | VFAT_SFN_CREATE_WIN95; |
1043 | break; | 1049 | break; |
1044 | case Opt_utf8_no: /* 0 or no or false */ | 1050 | case Opt_utf8_no: /* 0 or no or false */ |
1045 | opts->utf8 = 0; | 1051 | opts->utf8 = 0; |
1046 | break; | 1052 | break; |
1047 | case Opt_utf8_yes: /* empty or 1 or yes or true */ | 1053 | case Opt_utf8_yes: /* empty or 1 or yes or true */ |
1048 | opts->utf8 = 1; | 1054 | opts->utf8 = 1; |
1049 | break; | 1055 | break; |
1050 | case Opt_uni_xl_no: /* 0 or no or false */ | 1056 | case Opt_uni_xl_no: /* 0 or no or false */ |
1051 | opts->unicode_xlate = 0; | 1057 | opts->unicode_xlate = 0; |
1052 | break; | 1058 | break; |
1053 | case Opt_uni_xl_yes: /* empty or 1 or yes or true */ | 1059 | case Opt_uni_xl_yes: /* empty or 1 or yes or true */ |
1054 | opts->unicode_xlate = 1; | 1060 | opts->unicode_xlate = 1; |
1055 | break; | 1061 | break; |
1056 | case Opt_nonumtail_no: /* 0 or no or false */ | 1062 | case Opt_nonumtail_no: /* 0 or no or false */ |
1057 | opts->numtail = 1; /* negated option */ | 1063 | opts->numtail = 1; /* negated option */ |
1058 | break; | 1064 | break; |
1059 | case Opt_nonumtail_yes: /* empty or 1 or yes or true */ | 1065 | case Opt_nonumtail_yes: /* empty or 1 or yes or true */ |
1060 | opts->numtail = 0; /* negated option */ | 1066 | opts->numtail = 0; /* negated option */ |
1067 | break; | ||
1068 | case Opt_rodir: | ||
1069 | opts->rodir = 1; | ||
1061 | break; | 1070 | break; |
1062 | 1071 | ||
1063 | /* obsolete mount options */ | 1072 | /* obsolete mount options */ |
1064 | case Opt_obsolate: | 1073 | case Opt_obsolate: |
1065 | printk(KERN_INFO "FAT: \"%s\" option is obsolete, " | 1074 | printk(KERN_INFO "FAT: \"%s\" option is obsolete, " |
1066 | "not supported now\n", p); | 1075 | "not supported now\n", p); |
1067 | break; | 1076 | break; |
1068 | /* unknown option */ | 1077 | /* unknown option */ |
1069 | default: | 1078 | default: |
1070 | if (!silent) { | 1079 | if (!silent) { |
1071 | printk(KERN_ERR | 1080 | printk(KERN_ERR |
1072 | "FAT: Unrecognized mount option \"%s\" " | 1081 | "FAT: Unrecognized mount option \"%s\" " |
1073 | "or missing value\n", p); | 1082 | "or missing value\n", p); |
1074 | } | 1083 | } |
1075 | return -EINVAL; | 1084 | return -EINVAL; |
1076 | } | 1085 | } |
1077 | } | 1086 | } |
1078 | 1087 | ||
1079 | out: | 1088 | out: |
1080 | /* UTF-8 doesn't provide FAT semantics */ | 1089 | /* UTF-8 doesn't provide FAT semantics */ |
1081 | if (!strcmp(opts->iocharset, "utf8")) { | 1090 | if (!strcmp(opts->iocharset, "utf8")) { |
1082 | printk(KERN_ERR "FAT: utf8 is not a recommended IO charset" | 1091 | printk(KERN_ERR "FAT: utf8 is not a recommended IO charset" |
1083 | " for FAT filesystems, filesystem will be " | 1092 | " for FAT filesystems, filesystem will be " |
1084 | "case sensitive!\n"); | 1093 | "case sensitive!\n"); |
1085 | } | 1094 | } |
1086 | 1095 | ||
1087 | /* If user doesn't specify allow_utime, it's initialized from dmask. */ | 1096 | /* If user doesn't specify allow_utime, it's initialized from dmask. */ |
1088 | if (opts->allow_utime == (unsigned short)-1) | 1097 | if (opts->allow_utime == (unsigned short)-1) |
1089 | opts->allow_utime = ~opts->fs_dmask & (S_IWGRP | S_IWOTH); | 1098 | opts->allow_utime = ~opts->fs_dmask & (S_IWGRP | S_IWOTH); |
1090 | if (opts->unicode_xlate) | 1099 | if (opts->unicode_xlate) |
1091 | opts->utf8 = 0; | 1100 | opts->utf8 = 0; |
1092 | 1101 | ||
1093 | return 0; | 1102 | return 0; |
1094 | } | 1103 | } |
1095 | 1104 | ||
1096 | static int fat_read_root(struct inode *inode) | 1105 | static int fat_read_root(struct inode *inode) |
1097 | { | 1106 | { |
1098 | struct super_block *sb = inode->i_sb; | 1107 | struct super_block *sb = inode->i_sb; |
1099 | struct msdos_sb_info *sbi = MSDOS_SB(sb); | 1108 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
1100 | int error; | 1109 | int error; |
1101 | 1110 | ||
1102 | MSDOS_I(inode)->i_pos = 0; | 1111 | MSDOS_I(inode)->i_pos = 0; |
1103 | inode->i_uid = sbi->options.fs_uid; | 1112 | inode->i_uid = sbi->options.fs_uid; |
1104 | inode->i_gid = sbi->options.fs_gid; | 1113 | inode->i_gid = sbi->options.fs_gid; |
1105 | inode->i_version++; | 1114 | inode->i_version++; |
1106 | inode->i_generation = 0; | 1115 | inode->i_generation = 0; |
1107 | inode->i_mode = fat_make_mode(sbi, ATTR_DIR, S_IRWXUGO); | 1116 | inode->i_mode = fat_make_mode(sbi, ATTR_DIR, S_IRWXUGO); |
1108 | inode->i_op = sbi->dir_ops; | 1117 | inode->i_op = sbi->dir_ops; |
1109 | inode->i_fop = &fat_dir_operations; | 1118 | inode->i_fop = &fat_dir_operations; |
1110 | if (sbi->fat_bits == 32) { | 1119 | if (sbi->fat_bits == 32) { |
1111 | MSDOS_I(inode)->i_start = sbi->root_cluster; | 1120 | MSDOS_I(inode)->i_start = sbi->root_cluster; |
1112 | error = fat_calc_dir_size(inode); | 1121 | error = fat_calc_dir_size(inode); |
1113 | if (error < 0) | 1122 | if (error < 0) |
1114 | return error; | 1123 | return error; |
1115 | } else { | 1124 | } else { |
1116 | MSDOS_I(inode)->i_start = 0; | 1125 | MSDOS_I(inode)->i_start = 0; |
1117 | inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry); | 1126 | inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry); |
1118 | } | 1127 | } |
1119 | inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1)) | 1128 | inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1)) |
1120 | & ~((loff_t)sbi->cluster_size - 1)) >> 9; | 1129 | & ~((loff_t)sbi->cluster_size - 1)) >> 9; |
1121 | MSDOS_I(inode)->i_logstart = 0; | 1130 | MSDOS_I(inode)->i_logstart = 0; |
1122 | MSDOS_I(inode)->mmu_private = inode->i_size; | 1131 | MSDOS_I(inode)->mmu_private = inode->i_size; |
1123 | 1132 | ||
1124 | fat_save_attrs(inode, ATTR_DIR); | 1133 | fat_save_attrs(inode, ATTR_DIR); |
1125 | inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec = 0; | 1134 | inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec = 0; |
1126 | inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = 0; | 1135 | inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = 0; |
1127 | inode->i_nlink = fat_subdirs(inode)+2; | 1136 | inode->i_nlink = fat_subdirs(inode)+2; |
1128 | 1137 | ||
1129 | return 0; | 1138 | return 0; |
1130 | } | 1139 | } |
1131 | 1140 | ||
1132 | /* | 1141 | /* |
1133 | * Read the super block of an MS-DOS FS. | 1142 | * Read the super block of an MS-DOS FS. |
1134 | */ | 1143 | */ |
1135 | int fat_fill_super(struct super_block *sb, void *data, int silent, | 1144 | int fat_fill_super(struct super_block *sb, void *data, int silent, |
1136 | const struct inode_operations *fs_dir_inode_ops, int isvfat) | 1145 | const struct inode_operations *fs_dir_inode_ops, int isvfat) |
1137 | { | 1146 | { |
1138 | struct inode *root_inode = NULL; | 1147 | struct inode *root_inode = NULL; |
1139 | struct buffer_head *bh; | 1148 | struct buffer_head *bh; |
1140 | struct fat_boot_sector *b; | 1149 | struct fat_boot_sector *b; |
1141 | struct msdos_sb_info *sbi; | 1150 | struct msdos_sb_info *sbi; |
1142 | u16 logical_sector_size; | 1151 | u16 logical_sector_size; |
1143 | u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors; | 1152 | u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors; |
1144 | int debug; | 1153 | int debug; |
1145 | unsigned int media; | 1154 | unsigned int media; |
1146 | long error; | 1155 | long error; |
1147 | char buf[50]; | 1156 | char buf[50]; |
1148 | 1157 | ||
1149 | /* | 1158 | /* |
1150 | * GFP_KERNEL is ok here, because while we do hold the | 1159 | * GFP_KERNEL is ok here, because while we do hold the |
1151 | * supeblock lock, memory pressure can't call back into | 1160 | * supeblock lock, memory pressure can't call back into |
1152 | * the filesystem, since we're only just about to mount | 1161 | * the filesystem, since we're only just about to mount |
1153 | * it and have no inodes etc active! | 1162 | * it and have no inodes etc active! |
1154 | */ | 1163 | */ |
1155 | sbi = kzalloc(sizeof(struct msdos_sb_info), GFP_KERNEL); | 1164 | sbi = kzalloc(sizeof(struct msdos_sb_info), GFP_KERNEL); |
1156 | if (!sbi) | 1165 | if (!sbi) |
1157 | return -ENOMEM; | 1166 | return -ENOMEM; |
1158 | sb->s_fs_info = sbi; | 1167 | sb->s_fs_info = sbi; |
1159 | 1168 | ||
1160 | sb->s_flags |= MS_NODIRATIME; | 1169 | sb->s_flags |= MS_NODIRATIME; |
1161 | sb->s_magic = MSDOS_SUPER_MAGIC; | 1170 | sb->s_magic = MSDOS_SUPER_MAGIC; |
1162 | sb->s_op = &fat_sops; | 1171 | sb->s_op = &fat_sops; |
1163 | sb->s_export_op = &fat_export_ops; | 1172 | sb->s_export_op = &fat_export_ops; |
1164 | sbi->dir_ops = fs_dir_inode_ops; | 1173 | sbi->dir_ops = fs_dir_inode_ops; |
1165 | 1174 | ||
1166 | error = parse_options(data, isvfat, silent, &debug, &sbi->options); | 1175 | error = parse_options(data, isvfat, silent, &debug, &sbi->options); |
1167 | if (error) | 1176 | if (error) |
1168 | goto out_fail; | 1177 | goto out_fail; |
1169 | 1178 | ||
1170 | error = -EIO; | 1179 | error = -EIO; |
1171 | sb_min_blocksize(sb, 512); | 1180 | sb_min_blocksize(sb, 512); |
1172 | bh = sb_bread(sb, 0); | 1181 | bh = sb_bread(sb, 0); |
1173 | if (bh == NULL) { | 1182 | if (bh == NULL) { |
1174 | printk(KERN_ERR "FAT: unable to read boot sector\n"); | 1183 | printk(KERN_ERR "FAT: unable to read boot sector\n"); |
1175 | goto out_fail; | 1184 | goto out_fail; |
1176 | } | 1185 | } |
1177 | 1186 | ||
1178 | b = (struct fat_boot_sector *) bh->b_data; | 1187 | b = (struct fat_boot_sector *) bh->b_data; |
1179 | if (!b->reserved) { | 1188 | if (!b->reserved) { |
1180 | if (!silent) | 1189 | if (!silent) |
1181 | printk(KERN_ERR "FAT: bogus number of reserved sectors\n"); | 1190 | printk(KERN_ERR "FAT: bogus number of reserved sectors\n"); |
1182 | brelse(bh); | 1191 | brelse(bh); |
1183 | goto out_invalid; | 1192 | goto out_invalid; |
1184 | } | 1193 | } |
1185 | if (!b->fats) { | 1194 | if (!b->fats) { |
1186 | if (!silent) | 1195 | if (!silent) |
1187 | printk(KERN_ERR "FAT: bogus number of FAT structure\n"); | 1196 | printk(KERN_ERR "FAT: bogus number of FAT structure\n"); |
1188 | brelse(bh); | 1197 | brelse(bh); |
1189 | goto out_invalid; | 1198 | goto out_invalid; |
1190 | } | 1199 | } |
1191 | 1200 | ||
1192 | /* | 1201 | /* |
1193 | * Earlier we checked here that b->secs_track and b->head are nonzero, | 1202 | * Earlier we checked here that b->secs_track and b->head are nonzero, |
1194 | * but it turns out valid FAT filesystems can have zero there. | 1203 | * but it turns out valid FAT filesystems can have zero there. |
1195 | */ | 1204 | */ |
1196 | 1205 | ||
1197 | media = b->media; | 1206 | media = b->media; |
1198 | if (!fat_valid_media(media)) { | 1207 | if (!fat_valid_media(media)) { |
1199 | if (!silent) | 1208 | if (!silent) |
1200 | printk(KERN_ERR "FAT: invalid media value (0x%02x)\n", | 1209 | printk(KERN_ERR "FAT: invalid media value (0x%02x)\n", |
1201 | media); | 1210 | media); |
1202 | brelse(bh); | 1211 | brelse(bh); |
1203 | goto out_invalid; | 1212 | goto out_invalid; |
1204 | } | 1213 | } |
1205 | logical_sector_size = get_unaligned_le16(&b->sector_size); | 1214 | logical_sector_size = get_unaligned_le16(&b->sector_size); |
1206 | if (!is_power_of_2(logical_sector_size) | 1215 | if (!is_power_of_2(logical_sector_size) |
1207 | || (logical_sector_size < 512) | 1216 | || (logical_sector_size < 512) |
1208 | || (logical_sector_size > 4096)) { | 1217 | || (logical_sector_size > 4096)) { |
1209 | if (!silent) | 1218 | if (!silent) |
1210 | printk(KERN_ERR "FAT: bogus logical sector size %u\n", | 1219 | printk(KERN_ERR "FAT: bogus logical sector size %u\n", |
1211 | logical_sector_size); | 1220 | logical_sector_size); |
1212 | brelse(bh); | 1221 | brelse(bh); |
1213 | goto out_invalid; | 1222 | goto out_invalid; |
1214 | } | 1223 | } |
1215 | sbi->sec_per_clus = b->sec_per_clus; | 1224 | sbi->sec_per_clus = b->sec_per_clus; |
1216 | if (!is_power_of_2(sbi->sec_per_clus)) { | 1225 | if (!is_power_of_2(sbi->sec_per_clus)) { |
1217 | if (!silent) | 1226 | if (!silent) |
1218 | printk(KERN_ERR "FAT: bogus sectors per cluster %u\n", | 1227 | printk(KERN_ERR "FAT: bogus sectors per cluster %u\n", |
1219 | sbi->sec_per_clus); | 1228 | sbi->sec_per_clus); |
1220 | brelse(bh); | 1229 | brelse(bh); |
1221 | goto out_invalid; | 1230 | goto out_invalid; |
1222 | } | 1231 | } |
1223 | 1232 | ||
1224 | if (logical_sector_size < sb->s_blocksize) { | 1233 | if (logical_sector_size < sb->s_blocksize) { |
1225 | printk(KERN_ERR "FAT: logical sector size too small for device" | 1234 | printk(KERN_ERR "FAT: logical sector size too small for device" |
1226 | " (logical sector size = %u)\n", logical_sector_size); | 1235 | " (logical sector size = %u)\n", logical_sector_size); |
1227 | brelse(bh); | 1236 | brelse(bh); |
1228 | goto out_fail; | 1237 | goto out_fail; |
1229 | } | 1238 | } |
1230 | if (logical_sector_size > sb->s_blocksize) { | 1239 | if (logical_sector_size > sb->s_blocksize) { |
1231 | brelse(bh); | 1240 | brelse(bh); |
1232 | 1241 | ||
1233 | if (!sb_set_blocksize(sb, logical_sector_size)) { | 1242 | if (!sb_set_blocksize(sb, logical_sector_size)) { |
1234 | printk(KERN_ERR "FAT: unable to set blocksize %u\n", | 1243 | printk(KERN_ERR "FAT: unable to set blocksize %u\n", |
1235 | logical_sector_size); | 1244 | logical_sector_size); |
1236 | goto out_fail; | 1245 | goto out_fail; |
1237 | } | 1246 | } |
1238 | bh = sb_bread(sb, 0); | 1247 | bh = sb_bread(sb, 0); |
1239 | if (bh == NULL) { | 1248 | if (bh == NULL) { |
1240 | printk(KERN_ERR "FAT: unable to read boot sector" | 1249 | printk(KERN_ERR "FAT: unable to read boot sector" |
1241 | " (logical sector size = %lu)\n", | 1250 | " (logical sector size = %lu)\n", |
1242 | sb->s_blocksize); | 1251 | sb->s_blocksize); |
1243 | goto out_fail; | 1252 | goto out_fail; |
1244 | } | 1253 | } |
1245 | b = (struct fat_boot_sector *) bh->b_data; | 1254 | b = (struct fat_boot_sector *) bh->b_data; |
1246 | } | 1255 | } |
1247 | 1256 | ||
1248 | sbi->cluster_size = sb->s_blocksize * sbi->sec_per_clus; | 1257 | sbi->cluster_size = sb->s_blocksize * sbi->sec_per_clus; |
1249 | sbi->cluster_bits = ffs(sbi->cluster_size) - 1; | 1258 | sbi->cluster_bits = ffs(sbi->cluster_size) - 1; |
1250 | sbi->fats = b->fats; | 1259 | sbi->fats = b->fats; |
1251 | sbi->fat_bits = 0; /* Don't know yet */ | 1260 | sbi->fat_bits = 0; /* Don't know yet */ |
1252 | sbi->fat_start = le16_to_cpu(b->reserved); | 1261 | sbi->fat_start = le16_to_cpu(b->reserved); |
1253 | sbi->fat_length = le16_to_cpu(b->fat_length); | 1262 | sbi->fat_length = le16_to_cpu(b->fat_length); |
1254 | sbi->root_cluster = 0; | 1263 | sbi->root_cluster = 0; |
1255 | sbi->free_clusters = -1; /* Don't know yet */ | 1264 | sbi->free_clusters = -1; /* Don't know yet */ |
1256 | sbi->free_clus_valid = 0; | 1265 | sbi->free_clus_valid = 0; |
1257 | sbi->prev_free = FAT_START_ENT; | 1266 | sbi->prev_free = FAT_START_ENT; |
1258 | 1267 | ||
1259 | if (!sbi->fat_length && b->fat32_length) { | 1268 | if (!sbi->fat_length && b->fat32_length) { |
1260 | struct fat_boot_fsinfo *fsinfo; | 1269 | struct fat_boot_fsinfo *fsinfo; |
1261 | struct buffer_head *fsinfo_bh; | 1270 | struct buffer_head *fsinfo_bh; |
1262 | 1271 | ||
1263 | /* Must be FAT32 */ | 1272 | /* Must be FAT32 */ |
1264 | sbi->fat_bits = 32; | 1273 | sbi->fat_bits = 32; |
1265 | sbi->fat_length = le32_to_cpu(b->fat32_length); | 1274 | sbi->fat_length = le32_to_cpu(b->fat32_length); |
1266 | sbi->root_cluster = le32_to_cpu(b->root_cluster); | 1275 | sbi->root_cluster = le32_to_cpu(b->root_cluster); |
1267 | 1276 | ||
1268 | sb->s_maxbytes = 0xffffffff; | 1277 | sb->s_maxbytes = 0xffffffff; |
1269 | 1278 | ||
1270 | /* MC - if info_sector is 0, don't multiply by 0 */ | 1279 | /* MC - if info_sector is 0, don't multiply by 0 */ |
1271 | sbi->fsinfo_sector = le16_to_cpu(b->info_sector); | 1280 | sbi->fsinfo_sector = le16_to_cpu(b->info_sector); |
1272 | if (sbi->fsinfo_sector == 0) | 1281 | if (sbi->fsinfo_sector == 0) |
1273 | sbi->fsinfo_sector = 1; | 1282 | sbi->fsinfo_sector = 1; |
1274 | 1283 | ||
1275 | fsinfo_bh = sb_bread(sb, sbi->fsinfo_sector); | 1284 | fsinfo_bh = sb_bread(sb, sbi->fsinfo_sector); |
1276 | if (fsinfo_bh == NULL) { | 1285 | if (fsinfo_bh == NULL) { |
1277 | printk(KERN_ERR "FAT: bread failed, FSINFO block" | 1286 | printk(KERN_ERR "FAT: bread failed, FSINFO block" |
1278 | " (sector = %lu)\n", sbi->fsinfo_sector); | 1287 | " (sector = %lu)\n", sbi->fsinfo_sector); |
1279 | brelse(bh); | 1288 | brelse(bh); |
1280 | goto out_fail; | 1289 | goto out_fail; |
1281 | } | 1290 | } |
1282 | 1291 | ||
1283 | fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data; | 1292 | fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data; |
1284 | if (!IS_FSINFO(fsinfo)) { | 1293 | if (!IS_FSINFO(fsinfo)) { |
1285 | printk(KERN_WARNING "FAT: Invalid FSINFO signature: " | 1294 | printk(KERN_WARNING "FAT: Invalid FSINFO signature: " |
1286 | "0x%08x, 0x%08x (sector = %lu)\n", | 1295 | "0x%08x, 0x%08x (sector = %lu)\n", |
1287 | le32_to_cpu(fsinfo->signature1), | 1296 | le32_to_cpu(fsinfo->signature1), |
1288 | le32_to_cpu(fsinfo->signature2), | 1297 | le32_to_cpu(fsinfo->signature2), |
1289 | sbi->fsinfo_sector); | 1298 | sbi->fsinfo_sector); |
1290 | } else { | 1299 | } else { |
1291 | if (sbi->options.usefree) | 1300 | if (sbi->options.usefree) |
1292 | sbi->free_clus_valid = 1; | 1301 | sbi->free_clus_valid = 1; |
1293 | sbi->free_clusters = le32_to_cpu(fsinfo->free_clusters); | 1302 | sbi->free_clusters = le32_to_cpu(fsinfo->free_clusters); |
1294 | sbi->prev_free = le32_to_cpu(fsinfo->next_cluster); | 1303 | sbi->prev_free = le32_to_cpu(fsinfo->next_cluster); |
1295 | } | 1304 | } |
1296 | 1305 | ||
1297 | brelse(fsinfo_bh); | 1306 | brelse(fsinfo_bh); |
1298 | } | 1307 | } |
1299 | 1308 | ||
1300 | sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry); | 1309 | sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry); |
1301 | sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1; | 1310 | sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1; |
1302 | 1311 | ||
1303 | sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length; | 1312 | sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length; |
1304 | sbi->dir_entries = get_unaligned_le16(&b->dir_entries); | 1313 | sbi->dir_entries = get_unaligned_le16(&b->dir_entries); |
1305 | if (sbi->dir_entries & (sbi->dir_per_block - 1)) { | 1314 | if (sbi->dir_entries & (sbi->dir_per_block - 1)) { |
1306 | if (!silent) | 1315 | if (!silent) |
1307 | printk(KERN_ERR "FAT: bogus directroy-entries per block" | 1316 | printk(KERN_ERR "FAT: bogus directroy-entries per block" |
1308 | " (%u)\n", sbi->dir_entries); | 1317 | " (%u)\n", sbi->dir_entries); |
1309 | brelse(bh); | 1318 | brelse(bh); |
1310 | goto out_invalid; | 1319 | goto out_invalid; |
1311 | } | 1320 | } |
1312 | 1321 | ||
1313 | rootdir_sectors = sbi->dir_entries | 1322 | rootdir_sectors = sbi->dir_entries |
1314 | * sizeof(struct msdos_dir_entry) / sb->s_blocksize; | 1323 | * sizeof(struct msdos_dir_entry) / sb->s_blocksize; |
1315 | sbi->data_start = sbi->dir_start + rootdir_sectors; | 1324 | sbi->data_start = sbi->dir_start + rootdir_sectors; |
1316 | total_sectors = get_unaligned_le16(&b->sectors); | 1325 | total_sectors = get_unaligned_le16(&b->sectors); |
1317 | if (total_sectors == 0) | 1326 | if (total_sectors == 0) |
1318 | total_sectors = le32_to_cpu(b->total_sect); | 1327 | total_sectors = le32_to_cpu(b->total_sect); |
1319 | 1328 | ||
1320 | total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus; | 1329 | total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus; |
1321 | 1330 | ||
1322 | if (sbi->fat_bits != 32) | 1331 | if (sbi->fat_bits != 32) |
1323 | sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12; | 1332 | sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12; |
1324 | 1333 | ||
1325 | /* check that FAT table does not overflow */ | 1334 | /* check that FAT table does not overflow */ |
1326 | fat_clusters = sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits; | 1335 | fat_clusters = sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits; |
1327 | total_clusters = min(total_clusters, fat_clusters - FAT_START_ENT); | 1336 | total_clusters = min(total_clusters, fat_clusters - FAT_START_ENT); |
1328 | if (total_clusters > MAX_FAT(sb)) { | 1337 | if (total_clusters > MAX_FAT(sb)) { |
1329 | if (!silent) | 1338 | if (!silent) |
1330 | printk(KERN_ERR "FAT: count of clusters too big (%u)\n", | 1339 | printk(KERN_ERR "FAT: count of clusters too big (%u)\n", |
1331 | total_clusters); | 1340 | total_clusters); |
1332 | brelse(bh); | 1341 | brelse(bh); |
1333 | goto out_invalid; | 1342 | goto out_invalid; |
1334 | } | 1343 | } |
1335 | 1344 | ||
1336 | sbi->max_cluster = total_clusters + FAT_START_ENT; | 1345 | sbi->max_cluster = total_clusters + FAT_START_ENT; |
1337 | /* check the free_clusters, it's not necessarily correct */ | 1346 | /* check the free_clusters, it's not necessarily correct */ |
1338 | if (sbi->free_clusters != -1 && sbi->free_clusters > total_clusters) | 1347 | if (sbi->free_clusters != -1 && sbi->free_clusters > total_clusters) |
1339 | sbi->free_clusters = -1; | 1348 | sbi->free_clusters = -1; |
1340 | /* check the prev_free, it's not necessarily correct */ | 1349 | /* check the prev_free, it's not necessarily correct */ |
1341 | sbi->prev_free %= sbi->max_cluster; | 1350 | sbi->prev_free %= sbi->max_cluster; |
1342 | if (sbi->prev_free < FAT_START_ENT) | 1351 | if (sbi->prev_free < FAT_START_ENT) |
1343 | sbi->prev_free = FAT_START_ENT; | 1352 | sbi->prev_free = FAT_START_ENT; |
1344 | 1353 | ||
1345 | brelse(bh); | 1354 | brelse(bh); |
1346 | 1355 | ||
1347 | /* set up enough so that it can read an inode */ | 1356 | /* set up enough so that it can read an inode */ |
1348 | fat_hash_init(sb); | 1357 | fat_hash_init(sb); |
1349 | fat_ent_access_init(sb); | 1358 | fat_ent_access_init(sb); |
1350 | 1359 | ||
1351 | /* | 1360 | /* |
1352 | * The low byte of FAT's first entry must have same value with | 1361 | * The low byte of FAT's first entry must have same value with |
1353 | * media-field. But in real world, too many devices is | 1362 | * media-field. But in real world, too many devices is |
1354 | * writing wrong value. So, removed that validity check. | 1363 | * writing wrong value. So, removed that validity check. |
1355 | * | 1364 | * |
1356 | * if (FAT_FIRST_ENT(sb, media) != first) | 1365 | * if (FAT_FIRST_ENT(sb, media) != first) |
1357 | */ | 1366 | */ |
1358 | 1367 | ||
1359 | error = -EINVAL; | 1368 | error = -EINVAL; |
1360 | sprintf(buf, "cp%d", sbi->options.codepage); | 1369 | sprintf(buf, "cp%d", sbi->options.codepage); |
1361 | sbi->nls_disk = load_nls(buf); | 1370 | sbi->nls_disk = load_nls(buf); |
1362 | if (!sbi->nls_disk) { | 1371 | if (!sbi->nls_disk) { |
1363 | printk(KERN_ERR "FAT: codepage %s not found\n", buf); | 1372 | printk(KERN_ERR "FAT: codepage %s not found\n", buf); |
1364 | goto out_fail; | 1373 | goto out_fail; |
1365 | } | 1374 | } |
1366 | 1375 | ||
1367 | /* FIXME: utf8 is using iocharset for upper/lower conversion */ | 1376 | /* FIXME: utf8 is using iocharset for upper/lower conversion */ |
1368 | if (sbi->options.isvfat) { | 1377 | if (sbi->options.isvfat) { |
1369 | sbi->nls_io = load_nls(sbi->options.iocharset); | 1378 | sbi->nls_io = load_nls(sbi->options.iocharset); |
1370 | if (!sbi->nls_io) { | 1379 | if (!sbi->nls_io) { |
1371 | printk(KERN_ERR "FAT: IO charset %s not found\n", | 1380 | printk(KERN_ERR "FAT: IO charset %s not found\n", |
1372 | sbi->options.iocharset); | 1381 | sbi->options.iocharset); |
1373 | goto out_fail; | 1382 | goto out_fail; |
1374 | } | 1383 | } |
1375 | } | 1384 | } |
1376 | 1385 | ||
1377 | error = -ENOMEM; | 1386 | error = -ENOMEM; |
1378 | root_inode = new_inode(sb); | 1387 | root_inode = new_inode(sb); |
1379 | if (!root_inode) | 1388 | if (!root_inode) |
1380 | goto out_fail; | 1389 | goto out_fail; |
1381 | root_inode->i_ino = MSDOS_ROOT_INO; | 1390 | root_inode->i_ino = MSDOS_ROOT_INO; |
1382 | root_inode->i_version = 1; | 1391 | root_inode->i_version = 1; |
1383 | error = fat_read_root(root_inode); | 1392 | error = fat_read_root(root_inode); |
1384 | if (error < 0) | 1393 | if (error < 0) |
1385 | goto out_fail; | 1394 | goto out_fail; |
1386 | error = -ENOMEM; | 1395 | error = -ENOMEM; |
1387 | insert_inode_hash(root_inode); | 1396 | insert_inode_hash(root_inode); |
1388 | sb->s_root = d_alloc_root(root_inode); | 1397 | sb->s_root = d_alloc_root(root_inode); |
1389 | if (!sb->s_root) { | 1398 | if (!sb->s_root) { |
1390 | printk(KERN_ERR "FAT: get root inode failed\n"); | 1399 | printk(KERN_ERR "FAT: get root inode failed\n"); |
1391 | goto out_fail; | 1400 | goto out_fail; |
1392 | } | 1401 | } |
1393 | 1402 | ||
1394 | return 0; | 1403 | return 0; |
1395 | 1404 | ||
1396 | out_invalid: | 1405 | out_invalid: |
1397 | error = -EINVAL; | 1406 | error = -EINVAL; |
1398 | if (!silent) | 1407 | if (!silent) |
1399 | printk(KERN_INFO "VFS: Can't find a valid FAT filesystem" | 1408 | printk(KERN_INFO "VFS: Can't find a valid FAT filesystem" |
1400 | " on dev %s.\n", sb->s_id); | 1409 | " on dev %s.\n", sb->s_id); |
1401 | 1410 | ||
1402 | out_fail: | 1411 | out_fail: |
1403 | if (root_inode) | 1412 | if (root_inode) |
1404 | iput(root_inode); | 1413 | iput(root_inode); |
1405 | if (sbi->nls_io) | 1414 | if (sbi->nls_io) |
1406 | unload_nls(sbi->nls_io); | 1415 | unload_nls(sbi->nls_io); |
1407 | if (sbi->nls_disk) | 1416 | if (sbi->nls_disk) |
1408 | unload_nls(sbi->nls_disk); | 1417 | unload_nls(sbi->nls_disk); |
1409 | if (sbi->options.iocharset != fat_default_iocharset) | 1418 | if (sbi->options.iocharset != fat_default_iocharset) |
1410 | kfree(sbi->options.iocharset); | 1419 | kfree(sbi->options.iocharset); |
1411 | sb->s_fs_info = NULL; | 1420 | sb->s_fs_info = NULL; |
1412 | kfree(sbi); | 1421 | kfree(sbi); |
1413 | return error; | 1422 | return error; |
1414 | } | 1423 | } |
1415 | 1424 | ||
1416 | EXPORT_SYMBOL_GPL(fat_fill_super); | 1425 | EXPORT_SYMBOL_GPL(fat_fill_super); |
1417 | 1426 | ||
1418 | /* | 1427 | /* |
1419 | * helper function for fat_flush_inodes. This writes both the inode | 1428 | * helper function for fat_flush_inodes. This writes both the inode |
1420 | * and the file data blocks, waiting for in flight data blocks before | 1429 | * and the file data blocks, waiting for in flight data blocks before |
1421 | * the start of the call. It does not wait for any io started | 1430 | * the start of the call. It does not wait for any io started |
1422 | * during the call | 1431 | * during the call |
1423 | */ | 1432 | */ |
1424 | static int writeback_inode(struct inode *inode) | 1433 | static int writeback_inode(struct inode *inode) |
1425 | { | 1434 | { |
1426 | 1435 | ||
1427 | int ret; | 1436 | int ret; |
1428 | struct address_space *mapping = inode->i_mapping; | 1437 | struct address_space *mapping = inode->i_mapping; |
1429 | struct writeback_control wbc = { | 1438 | struct writeback_control wbc = { |
1430 | .sync_mode = WB_SYNC_NONE, | 1439 | .sync_mode = WB_SYNC_NONE, |
1431 | .nr_to_write = 0, | 1440 | .nr_to_write = 0, |
1432 | }; | 1441 | }; |
1433 | /* if we used WB_SYNC_ALL, sync_inode waits for the io for the | 1442 | /* if we used WB_SYNC_ALL, sync_inode waits for the io for the |
1434 | * inode to finish. So WB_SYNC_NONE is sent down to sync_inode | 1443 | * inode to finish. So WB_SYNC_NONE is sent down to sync_inode |
1435 | * and filemap_fdatawrite is used for the data blocks | 1444 | * and filemap_fdatawrite is used for the data blocks |
1436 | */ | 1445 | */ |
1437 | ret = sync_inode(inode, &wbc); | 1446 | ret = sync_inode(inode, &wbc); |
1438 | if (!ret) | 1447 | if (!ret) |
1439 | ret = filemap_fdatawrite(mapping); | 1448 | ret = filemap_fdatawrite(mapping); |
1440 | return ret; | 1449 | return ret; |
1441 | } | 1450 | } |
1442 | 1451 | ||
1443 | /* | 1452 | /* |
1444 | * write data and metadata corresponding to i1 and i2. The io is | 1453 | * write data and metadata corresponding to i1 and i2. The io is |
1445 | * started but we do not wait for any of it to finish. | 1454 | * started but we do not wait for any of it to finish. |
1446 | * | 1455 | * |
1447 | * filemap_flush is used for the block device, so if there is a dirty | 1456 | * filemap_flush is used for the block device, so if there is a dirty |
1448 | * page for a block already in flight, we will not wait and start the | 1457 | * page for a block already in flight, we will not wait and start the |
1449 | * io over again | 1458 | * io over again |
1450 | */ | 1459 | */ |
1451 | int fat_flush_inodes(struct super_block *sb, struct inode *i1, struct inode *i2) | 1460 | int fat_flush_inodes(struct super_block *sb, struct inode *i1, struct inode *i2) |
1452 | { | 1461 | { |
1453 | int ret = 0; | 1462 | int ret = 0; |
1454 | if (!MSDOS_SB(sb)->options.flush) | 1463 | if (!MSDOS_SB(sb)->options.flush) |
1455 | return 0; | 1464 | return 0; |
1456 | if (i1) | 1465 | if (i1) |
1457 | ret = writeback_inode(i1); | 1466 | ret = writeback_inode(i1); |
1458 | if (!ret && i2) | 1467 | if (!ret && i2) |
1459 | ret = writeback_inode(i2); | 1468 | ret = writeback_inode(i2); |
1460 | if (!ret) { | 1469 | if (!ret) { |
1461 | struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping; | 1470 | struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping; |
1462 | ret = filemap_flush(mapping); | 1471 | ret = filemap_flush(mapping); |
1463 | } | 1472 | } |
1464 | return ret; | 1473 | return ret; |
1465 | } | 1474 | } |
1466 | EXPORT_SYMBOL_GPL(fat_flush_inodes); | 1475 | EXPORT_SYMBOL_GPL(fat_flush_inodes); |
1467 | 1476 | ||
1468 | static int __init init_fat_fs(void) | 1477 | static int __init init_fat_fs(void) |
1469 | { | 1478 | { |
1470 | int err; | 1479 | int err; |
1471 | 1480 | ||
1472 | err = fat_cache_init(); | 1481 | err = fat_cache_init(); |
1473 | if (err) | 1482 | if (err) |
1474 | return err; | 1483 | return err; |
1475 | 1484 | ||
1476 | err = fat_init_inodecache(); | 1485 | err = fat_init_inodecache(); |
1477 | if (err) | 1486 | if (err) |
1478 | goto failed; | 1487 | goto failed; |
1479 | 1488 | ||
1480 | return 0; | 1489 | return 0; |
1481 | 1490 | ||
1482 | failed: | 1491 | failed: |
1483 | fat_cache_destroy(); | 1492 | fat_cache_destroy(); |
1484 | return err; | 1493 | return err; |
1485 | } | 1494 | } |
1486 | 1495 | ||
1487 | static void __exit exit_fat_fs(void) | 1496 | static void __exit exit_fat_fs(void) |
1488 | { | 1497 | { |
1489 | fat_cache_destroy(); | 1498 | fat_cache_destroy(); |
1490 | fat_destroy_inodecache(); | 1499 | fat_destroy_inodecache(); |
1491 | } | 1500 | } |
1492 | 1501 | ||
1493 | module_init(init_fat_fs) | 1502 | module_init(init_fat_fs) |
1494 | module_exit(exit_fat_fs) | 1503 | module_exit(exit_fat_fs) |
1495 | 1504 | ||
1496 | MODULE_LICENSE("GPL"); | 1505 | MODULE_LICENSE("GPL"); |
1497 | 1506 |