1 |
1628 |
jcastillo |
/*
|
2 |
|
|
* linux/fs/fat/inode.c
|
3 |
|
|
*
|
4 |
|
|
* Written 1992,1993 by Werner Almesberger
|
5 |
|
|
* VFAT extensions by Gordon Chaffee, merged with msdos fs by Henrik Storner
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
#define ASC_LINUX_VERSION(V, P, S) (((V) * 65536) + ((P) * 256) + (S))
|
9 |
|
|
#include <linux/version.h>
|
10 |
|
|
#define __NO_VERSION__
|
11 |
|
|
#include <linux/module.h>
|
12 |
|
|
|
13 |
|
|
#include <linux/msdos_fs.h>
|
14 |
|
|
#include <linux/nls.h>
|
15 |
|
|
#include <linux/kernel.h>
|
16 |
|
|
#include <linux/sched.h>
|
17 |
|
|
#include <linux/errno.h>
|
18 |
|
|
#include <linux/string.h>
|
19 |
|
|
#include <linux/major.h>
|
20 |
|
|
#include <linux/blkdev.h>
|
21 |
|
|
#include <linux/fs.h>
|
22 |
|
|
#include <linux/stat.h>
|
23 |
|
|
#include <linux/locks.h>
|
24 |
|
|
#include <linux/malloc.h>
|
25 |
|
|
|
26 |
|
|
#include "msbuffer.h"
|
27 |
|
|
|
28 |
|
|
#if LINUX_VERSION_CODE >= ASC_LINUX_VERSION(2,1,0)
|
29 |
|
|
#include <asm/uaccess.h>
|
30 |
|
|
#define FAT_COPY_TO_USER(uaddr, kaddr, len) copy_to_user(uaddr, kaddr, len)
|
31 |
|
|
#else
|
32 |
|
|
#include <asm/segment.h>
|
33 |
|
|
#define FAT_COPY_TO_USER(uaddr, kaddr, len) memcpy_tofs(uaddr, kaddr, len)
|
34 |
|
|
#endif
|
35 |
|
|
#include <asm/unaligned.h>
|
36 |
|
|
|
37 |
|
|
#if 0
|
38 |
|
|
# define PRINTK(x) printk x
|
39 |
|
|
#else
|
40 |
|
|
# define PRINTK(x)
|
41 |
|
|
#endif
|
42 |
|
|
|
43 |
|
|
void fat_put_inode(struct inode *inode)
|
44 |
|
|
{
|
45 |
|
|
struct inode *depend, *linked;
|
46 |
|
|
struct super_block *sb;
|
47 |
|
|
|
48 |
|
|
depend = MSDOS_I(inode)->i_depend;
|
49 |
|
|
linked = MSDOS_I(inode)->i_linked;
|
50 |
|
|
sb = inode->i_sb;
|
51 |
|
|
if (inode->i_nlink) {
|
52 |
|
|
if (depend) {
|
53 |
|
|
iput(depend);
|
54 |
|
|
}
|
55 |
|
|
if (linked) {
|
56 |
|
|
iput(linked);
|
57 |
|
|
MSDOS_I(inode)->i_linked = NULL;
|
58 |
|
|
}
|
59 |
|
|
if (MSDOS_I(inode)->i_busy) fat_cache_inval_inode(inode);
|
60 |
|
|
return;
|
61 |
|
|
}
|
62 |
|
|
inode->i_size = 0;
|
63 |
|
|
fat_truncate(inode);
|
64 |
|
|
if (depend) {
|
65 |
|
|
if (MSDOS_I(depend)->i_old != inode) {
|
66 |
|
|
printk("Invalid link (0x%p): expected 0x%p, got 0x%p\n",
|
67 |
|
|
depend, inode, MSDOS_I(depend)->i_old);
|
68 |
|
|
fat_fs_panic(sb,"...");
|
69 |
|
|
goto done;
|
70 |
|
|
}
|
71 |
|
|
MSDOS_I(depend)->i_old = NULL;
|
72 |
|
|
iput(depend);
|
73 |
|
|
}
|
74 |
|
|
if (linked) {
|
75 |
|
|
if (MSDOS_I(linked)->i_oldlink != inode) {
|
76 |
|
|
printk("Invalid link (0x%p): expected 0x%p, got 0x%p\n",
|
77 |
|
|
linked, inode, MSDOS_I(linked)->i_oldlink);
|
78 |
|
|
fat_fs_panic(sb,"...");
|
79 |
|
|
goto done;
|
80 |
|
|
}
|
81 |
|
|
MSDOS_I(linked)->i_oldlink = NULL;
|
82 |
|
|
iput(linked);
|
83 |
|
|
}
|
84 |
|
|
done:
|
85 |
|
|
clear_inode(inode);
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
|
89 |
|
|
void fat_put_super(struct super_block *sb)
|
90 |
|
|
{
|
91 |
|
|
if (MSDOS_SB(sb)->fat_bits == 32) {
|
92 |
|
|
fat_clusters_flush(sb);
|
93 |
|
|
}
|
94 |
|
|
fat_cache_inval_dev(sb->s_dev);
|
95 |
|
|
set_blocksize (sb->s_dev,BLOCK_SIZE);
|
96 |
|
|
if (MSDOS_SB(sb)->nls_disk) {
|
97 |
|
|
unload_nls(MSDOS_SB(sb)->nls_disk);
|
98 |
|
|
MSDOS_SB(sb)->nls_disk = NULL;
|
99 |
|
|
MSDOS_SB(sb)->options.codepage = 0;
|
100 |
|
|
}
|
101 |
|
|
if (MSDOS_SB(sb)->nls_io) {
|
102 |
|
|
unload_nls(MSDOS_SB(sb)->nls_io);
|
103 |
|
|
MSDOS_SB(sb)->nls_io = NULL;
|
104 |
|
|
kfree(MSDOS_SB(sb)->options.iocharset);
|
105 |
|
|
MSDOS_SB(sb)->options.iocharset = NULL;
|
106 |
|
|
}
|
107 |
|
|
lock_super(sb);
|
108 |
|
|
sb->s_dev = 0;
|
109 |
|
|
unlock_super(sb);
|
110 |
|
|
MOD_DEC_USE_COUNT;
|
111 |
|
|
return;
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
|
115 |
|
|
static int parse_options(char *options,int *fat, int *blksize, int *debug,
|
116 |
|
|
struct fat_mount_options *opts)
|
117 |
|
|
{
|
118 |
|
|
char *this_char,*value,save,*savep;
|
119 |
|
|
char *p;
|
120 |
|
|
int ret, len;
|
121 |
|
|
|
122 |
|
|
opts->name_check = 'n';
|
123 |
|
|
opts->conversion = 'b';
|
124 |
|
|
opts->fs_uid = current->uid;
|
125 |
|
|
opts->fs_gid = current->gid;
|
126 |
|
|
opts->fs_umask = current->fs->umask;
|
127 |
|
|
opts->quiet = opts->sys_immutable = opts->dotsOK = opts->showexec = 0;
|
128 |
|
|
opts->codepage = 0;
|
129 |
|
|
opts->utf8 = 0;
|
130 |
|
|
opts->iocharset = NULL;
|
131 |
|
|
*debug = *fat = 0;
|
132 |
|
|
|
133 |
|
|
if (!options) return 1;
|
134 |
|
|
save = 0;
|
135 |
|
|
savep = NULL;
|
136 |
|
|
ret = 1;
|
137 |
|
|
for (this_char = strtok(options,","); this_char; this_char = strtok(NULL,",")) {
|
138 |
|
|
if ((value = strchr(this_char,'=')) != NULL) {
|
139 |
|
|
save = *value;
|
140 |
|
|
savep = value;
|
141 |
|
|
*value++ = 0;
|
142 |
|
|
}
|
143 |
|
|
if (!strcmp(this_char,"check") && value) {
|
144 |
|
|
if (value[0] && !value[1] && strchr("rns",*value))
|
145 |
|
|
opts->name_check = *value;
|
146 |
|
|
else if (!strcmp(value,"relaxed")) opts->name_check = 'r';
|
147 |
|
|
else if (!strcmp(value,"normal")) opts->name_check = 'n';
|
148 |
|
|
else if (!strcmp(value,"strict")) opts->name_check = 's';
|
149 |
|
|
else ret = 0;
|
150 |
|
|
}
|
151 |
|
|
else if (!strcmp(this_char,"conv") && value) {
|
152 |
|
|
if (value[0] && !value[1] && strchr("bta",*value))
|
153 |
|
|
opts->conversion = *value;
|
154 |
|
|
else if (!strcmp(value,"binary")) opts->conversion = 'b';
|
155 |
|
|
else if (!strcmp(value,"text")) opts->conversion = 't';
|
156 |
|
|
else if (!strcmp(value,"auto")) opts->conversion = 'a';
|
157 |
|
|
else ret = 0;
|
158 |
|
|
}
|
159 |
|
|
else if (!strcmp(this_char,"dots")) {
|
160 |
|
|
opts->dotsOK = 1;
|
161 |
|
|
}
|
162 |
|
|
else if (!strcmp(this_char,"nodots")) {
|
163 |
|
|
opts->dotsOK = 0;
|
164 |
|
|
}
|
165 |
|
|
else if (!strcmp(this_char,"showexec")) {
|
166 |
|
|
opts->showexec = 1;
|
167 |
|
|
}
|
168 |
|
|
else if (!strcmp(this_char,"dotsOK") && value) {
|
169 |
|
|
if (!strcmp(value,"yes")) opts->dotsOK = 1;
|
170 |
|
|
else if (!strcmp(value,"no")) opts->dotsOK = 0;
|
171 |
|
|
else ret = 0;
|
172 |
|
|
}
|
173 |
|
|
else if (!strcmp(this_char,"uid")) {
|
174 |
|
|
if (!value || !*value) ret = 0;
|
175 |
|
|
else {
|
176 |
|
|
opts->fs_uid = simple_strtoul(value,&value,0);
|
177 |
|
|
if (*value) ret = 0;
|
178 |
|
|
}
|
179 |
|
|
}
|
180 |
|
|
else if (!strcmp(this_char,"gid")) {
|
181 |
|
|
if (!value || !*value) ret= 0;
|
182 |
|
|
else {
|
183 |
|
|
opts->fs_gid = simple_strtoul(value,&value,0);
|
184 |
|
|
if (*value) ret = 0;
|
185 |
|
|
}
|
186 |
|
|
}
|
187 |
|
|
else if (!strcmp(this_char,"umask")) {
|
188 |
|
|
if (!value || !*value) ret = 0;
|
189 |
|
|
else {
|
190 |
|
|
opts->fs_umask = simple_strtoul(value,&value,8);
|
191 |
|
|
if (*value) ret = 0;
|
192 |
|
|
}
|
193 |
|
|
}
|
194 |
|
|
else if (!strcmp(this_char,"debug")) {
|
195 |
|
|
if (value) ret = 0;
|
196 |
|
|
else *debug = 1;
|
197 |
|
|
}
|
198 |
|
|
else if (!strcmp(this_char,"fat")) {
|
199 |
|
|
if (!value || !*value) ret = 0;
|
200 |
|
|
else {
|
201 |
|
|
*fat = simple_strtoul(value,&value,0);
|
202 |
|
|
if (*value || (*fat != 12 && *fat != 16 &&
|
203 |
|
|
*fat != 32))
|
204 |
|
|
ret = 0;
|
205 |
|
|
}
|
206 |
|
|
}
|
207 |
|
|
else if (!strcmp(this_char,"quiet")) {
|
208 |
|
|
if (value) ret = 0;
|
209 |
|
|
else opts->quiet = 1;
|
210 |
|
|
}
|
211 |
|
|
else if (!strcmp(this_char,"blocksize")) {
|
212 |
|
|
*blksize = simple_strtoul(value,&value,0);
|
213 |
|
|
if (*value) ret = 0;
|
214 |
|
|
else if (*blksize != 512 && *blksize != 1024){
|
215 |
|
|
printk ("MSDOS FS: Invalid blocksize (512 or 1024)\n");
|
216 |
|
|
}
|
217 |
|
|
}
|
218 |
|
|
else if (!strcmp(this_char,"sys_immutable")) {
|
219 |
|
|
if (value) ret = 0;
|
220 |
|
|
else opts->sys_immutable = 1;
|
221 |
|
|
}
|
222 |
|
|
else if (!strcmp(this_char,"codepage")) {
|
223 |
|
|
opts->codepage = simple_strtoul(value,&value,0);
|
224 |
|
|
if (*value) ret = 0;
|
225 |
|
|
else printk ("MSDOS FS: Using codepage %d\n", opts->codepage);
|
226 |
|
|
}
|
227 |
|
|
else if (!strcmp(this_char,"iocharset")) {
|
228 |
|
|
p = value;
|
229 |
|
|
while (*value && *value != ',') value++;
|
230 |
|
|
len = value - p;
|
231 |
|
|
if (len) {
|
232 |
|
|
opts->iocharset = kmalloc(len+1, GFP_KERNEL);
|
233 |
|
|
memcpy(opts->iocharset, p, len);
|
234 |
|
|
opts->iocharset[len] = 0;
|
235 |
|
|
printk ("MSDOS FS: Using IO charset %s\n",
|
236 |
|
|
opts->iocharset);
|
237 |
|
|
} else {
|
238 |
|
|
opts->iocharset = NULL;
|
239 |
|
|
ret = 0;
|
240 |
|
|
}
|
241 |
|
|
}
|
242 |
|
|
|
243 |
|
|
if (this_char != options) *(this_char-1) = ',';
|
244 |
|
|
if (value) *savep = save;
|
245 |
|
|
if (ret == 0) return 0;
|
246 |
|
|
}
|
247 |
|
|
return 1;
|
248 |
|
|
}
|
249 |
|
|
|
250 |
|
|
|
251 |
|
|
/* Read the super block of an MS-DOS FS. */
|
252 |
|
|
/* XXX: Make sure we handle the case of no codepage (unable to load) */
|
253 |
|
|
struct super_block *fat_read_super(struct super_block *sb,void *data, int silent)
|
254 |
|
|
{
|
255 |
|
|
struct buffer_head *bh;
|
256 |
|
|
struct fat_boot_sector *b;
|
257 |
|
|
int data_sectors,logical_sector_size,sector_mult,fat_clusters=0;
|
258 |
|
|
int debug,error,fat,cp;
|
259 |
|
|
int blksize = 512;
|
260 |
|
|
int fat32;
|
261 |
|
|
struct fat_mount_options opts;
|
262 |
|
|
char buf[50];
|
263 |
|
|
char *p;
|
264 |
|
|
|
265 |
|
|
MOD_INC_USE_COUNT;
|
266 |
|
|
if (hardsect_size[MAJOR(sb->s_dev)] != NULL){
|
267 |
|
|
blksize = hardsect_size[MAJOR(sb->s_dev)][MINOR(sb->s_dev)];
|
268 |
|
|
if (blksize != 512){
|
269 |
|
|
printk ("MSDOS: Hardware sector size is %d\n",blksize);
|
270 |
|
|
}
|
271 |
|
|
}
|
272 |
|
|
opts.isvfat = MSDOS_SB(sb)->options.isvfat;
|
273 |
|
|
if (!parse_options((char *) data, &fat, &blksize, &debug, &opts)
|
274 |
|
|
|| (blksize != 512 && blksize != 1024)) {
|
275 |
|
|
sb->s_dev = 0;
|
276 |
|
|
MOD_DEC_USE_COUNT;
|
277 |
|
|
return NULL;
|
278 |
|
|
}
|
279 |
|
|
cache_init();
|
280 |
|
|
lock_super(sb);
|
281 |
|
|
/* The first read is always 1024 bytes */
|
282 |
|
|
sb->s_blocksize = 1024;
|
283 |
|
|
set_blocksize(sb->s_dev, 1024);
|
284 |
|
|
bh = fat_bread(sb, 0);
|
285 |
|
|
unlock_super(sb);
|
286 |
|
|
if (bh == NULL || !fat_is_uptodate(sb,bh)) {
|
287 |
|
|
fat_brelse (sb, bh);
|
288 |
|
|
sb->s_dev = 0;
|
289 |
|
|
printk("FAT bread failed\n");
|
290 |
|
|
MOD_DEC_USE_COUNT;
|
291 |
|
|
return NULL;
|
292 |
|
|
}
|
293 |
|
|
set_blocksize(sb->s_dev, blksize);
|
294 |
|
|
|
295 |
|
|
/*
|
296 |
|
|
* The DOS3 partition size limit is *not* 32M as many people think.
|
297 |
|
|
* Instead, it is 64K sectors (with the usual sector size being
|
298 |
|
|
* 512 bytes, leading to a 32M limit).
|
299 |
|
|
*
|
300 |
|
|
* DOS 3 partition managers got around this problem by faking a
|
301 |
|
|
* larger sector size, ie treating multiple physical sectors as
|
302 |
|
|
* a single logical sector.
|
303 |
|
|
*
|
304 |
|
|
* We can accommodate this scheme by adjusting our cluster size,
|
305 |
|
|
* fat_start, and data_start by an appropriate value.
|
306 |
|
|
*
|
307 |
|
|
* (by Drew Eckhardt)
|
308 |
|
|
*/
|
309 |
|
|
|
310 |
|
|
#define ROUND_TO_MULTIPLE(n,m) ((n) && (m) ? (n)+(m)-1-((n)-1)%(m) : 0)
|
311 |
|
|
/* don't divide by zero */
|
312 |
|
|
|
313 |
|
|
b = (struct fat_boot_sector *) bh->b_data;
|
314 |
|
|
logical_sector_size =
|
315 |
|
|
CF_LE_W(get_unaligned((unsigned short *) &b->sector_size));
|
316 |
|
|
sector_mult = logical_sector_size >> SECTOR_BITS;
|
317 |
|
|
MSDOS_SB(sb)->cluster_size = b->cluster_size*sector_mult;
|
318 |
|
|
MSDOS_SB(sb)->fats = b->fats;
|
319 |
|
|
MSDOS_SB(sb)->fat_start = CF_LE_W(b->reserved)*sector_mult;
|
320 |
|
|
if (!b->fat_length && b->fat32_length) {
|
321 |
|
|
struct fat_boot_fsinfo *fsinfo;
|
322 |
|
|
|
323 |
|
|
/* Must be FAT32 */
|
324 |
|
|
fat32 = 1;
|
325 |
|
|
MSDOS_SB(sb)->fat_length= CF_LE_W(b->fat32_length)*sector_mult;
|
326 |
|
|
MSDOS_SB(sb)->root_cluster = CF_LE_L(b->root_cluster);
|
327 |
|
|
MSDOS_SB(sb)->fsinfo_offset =
|
328 |
|
|
CF_LE_W(b->info_sector) * logical_sector_size + 0x1e0;
|
329 |
|
|
fsinfo = (struct fat_boot_fsinfo *)
|
330 |
|
|
&bh->b_data[MSDOS_SB(sb)->fsinfo_offset];
|
331 |
|
|
if (CF_LE_L(fsinfo->signature) != 0x61417272) {
|
332 |
|
|
printk("fat_read_super: Did not find valid FSINFO signature. Found 0x%x\n", CF_LE_L(fsinfo->signature));
|
333 |
|
|
} else {
|
334 |
|
|
MSDOS_SB(sb)->free_clusters = CF_LE_L(fsinfo->free_clusters);
|
335 |
|
|
}
|
336 |
|
|
} else {
|
337 |
|
|
fat32 = 0;
|
338 |
|
|
MSDOS_SB(sb)->fat_length = CF_LE_W(b->fat_length)*sector_mult;
|
339 |
|
|
MSDOS_SB(sb)->root_cluster = 0;
|
340 |
|
|
MSDOS_SB(sb)->free_clusters = -1; /* Don't know yet */
|
341 |
|
|
}
|
342 |
|
|
MSDOS_SB(sb)->dir_start= CF_LE_W(b->reserved)*sector_mult+
|
343 |
|
|
b->fats*MSDOS_SB(sb)->fat_length;
|
344 |
|
|
MSDOS_SB(sb)->dir_entries =
|
345 |
|
|
CF_LE_W(get_unaligned((unsigned short *) &b->dir_entries));
|
346 |
|
|
MSDOS_SB(sb)->data_start = MSDOS_SB(sb)->dir_start+ROUND_TO_MULTIPLE((
|
347 |
|
|
MSDOS_SB(sb)->dir_entries << MSDOS_DIR_BITS) >> SECTOR_BITS,
|
348 |
|
|
sector_mult);
|
349 |
|
|
data_sectors = CF_LE_W(get_unaligned((unsigned short *) &b->sectors));
|
350 |
|
|
if (!data_sectors) {
|
351 |
|
|
data_sectors = CF_LE_L(b->total_sect);
|
352 |
|
|
}
|
353 |
|
|
data_sectors = data_sectors * sector_mult - MSDOS_SB(sb)->data_start;
|
354 |
|
|
error = !b->cluster_size || !sector_mult;
|
355 |
|
|
if (!error) {
|
356 |
|
|
MSDOS_SB(sb)->clusters = b->cluster_size ? data_sectors/
|
357 |
|
|
b->cluster_size/sector_mult : 0;
|
358 |
|
|
MSDOS_SB(sb)->fat_bits = fat32 ? 32 :
|
359 |
|
|
(fat ? fat :
|
360 |
|
|
(MSDOS_SB(sb)->clusters > MSDOS_FAT12 ? 16 : 12));
|
361 |
|
|
fat_clusters = MSDOS_SB(sb)->fat_length*SECTOR_SIZE*8/
|
362 |
|
|
MSDOS_SB(sb)->fat_bits;
|
363 |
|
|
error = !MSDOS_SB(sb)->fats || (MSDOS_SB(sb)->dir_entries &
|
364 |
|
|
(MSDOS_DPS-1)) || MSDOS_SB(sb)->clusters+2 > fat_clusters+
|
365 |
|
|
MSDOS_MAX_EXTRA || (logical_sector_size & (SECTOR_SIZE-1))
|
366 |
|
|
|| !b->secs_track || !b->heads;
|
367 |
|
|
}
|
368 |
|
|
fat_brelse(sb, bh);
|
369 |
|
|
/*
|
370 |
|
|
This must be done after the brelse because the bh is a dummy
|
371 |
|
|
allocated by fat_bread (see buffer.c)
|
372 |
|
|
*/
|
373 |
|
|
sb->s_blocksize = blksize; /* Using this small block size solves */
|
374 |
|
|
/* the misfit with buffer cache and cluster */
|
375 |
|
|
/* because clusters (DOS) are often aligned */
|
376 |
|
|
/* on odd sectors. */
|
377 |
|
|
sb->s_blocksize_bits = blksize == 512 ? 9 : 10;
|
378 |
|
|
if (error || debug) {
|
379 |
|
|
/* The MSDOS_CAN_BMAP is obsolete, but left just to remember */
|
380 |
|
|
printk("[MS-DOS FS Rel. 12,FAT %d,check=%c,conv=%c,"
|
381 |
|
|
"uid=%d,gid=%d,umask=%03o%s]\n",
|
382 |
|
|
MSDOS_SB(sb)->fat_bits,opts.name_check,
|
383 |
|
|
opts.conversion,opts.fs_uid,opts.fs_gid,opts.fs_umask,
|
384 |
|
|
MSDOS_CAN_BMAP(MSDOS_SB(sb)) ? ",bmap" : "");
|
385 |
|
|
printk("[me=0x%x,cs=%d,#f=%d,fs=%d,fl=%d,ds=%d,de=%d,data=%d,"
|
386 |
|
|
"se=%d,ts=%ld,ls=%d,rc=%ld,fc=%u]\n",b->media,MSDOS_SB(sb)->cluster_size,
|
387 |
|
|
MSDOS_SB(sb)->fats,MSDOS_SB(sb)->fat_start,MSDOS_SB(sb)->fat_length,
|
388 |
|
|
MSDOS_SB(sb)->dir_start,MSDOS_SB(sb)->dir_entries,
|
389 |
|
|
MSDOS_SB(sb)->data_start,
|
390 |
|
|
CF_LE_W(*(unsigned short *) &b->sectors),
|
391 |
|
|
(unsigned long)b->total_sect,logical_sector_size,
|
392 |
|
|
MSDOS_SB(sb)->root_cluster,MSDOS_SB(sb)->free_clusters);
|
393 |
|
|
printk ("Transaction block size = %d\n",blksize);
|
394 |
|
|
}
|
395 |
|
|
if (MSDOS_SB(sb)->clusters+2 > fat_clusters)
|
396 |
|
|
MSDOS_SB(sb)->clusters = fat_clusters-2;
|
397 |
|
|
if (error) {
|
398 |
|
|
if (!silent)
|
399 |
|
|
printk("VFS: Can't find a valid MSDOS filesystem on dev "
|
400 |
|
|
"%s.\n", kdevname(sb->s_dev));
|
401 |
|
|
sb->s_dev = 0;
|
402 |
|
|
MOD_DEC_USE_COUNT;
|
403 |
|
|
return NULL;
|
404 |
|
|
}
|
405 |
|
|
sb->s_magic = MSDOS_SUPER_MAGIC;
|
406 |
|
|
/* set up enough so that it can read an inode */
|
407 |
|
|
MSDOS_SB(sb)->fat_wait = NULL;
|
408 |
|
|
MSDOS_SB(sb)->fat_lock = 0;
|
409 |
|
|
MSDOS_SB(sb)->prev_free = 0;
|
410 |
|
|
memcpy(&(MSDOS_SB(sb)->options), &opts, sizeof(struct fat_mount_options));
|
411 |
|
|
|
412 |
|
|
cp = opts.codepage ? opts.codepage : 437;
|
413 |
|
|
sprintf(buf, "cp%d", cp);
|
414 |
|
|
MSDOS_SB(sb)->nls_disk = load_nls(buf);
|
415 |
|
|
if (! MSDOS_SB(sb)->nls_disk) {
|
416 |
|
|
/* Fail only if explicit charset specified */
|
417 |
|
|
if (opts.codepage == 0) {
|
418 |
|
|
MSDOS_SB(sb)->options.codepage = 0;
|
419 |
|
|
MSDOS_SB(sb)->nls_disk = load_nls_default();
|
420 |
|
|
} else {
|
421 |
|
|
sb->s_dev = 0;
|
422 |
|
|
MOD_DEC_USE_COUNT;
|
423 |
|
|
return NULL;
|
424 |
|
|
}
|
425 |
|
|
}
|
426 |
|
|
|
427 |
|
|
p = opts.iocharset ? opts.iocharset : "iso8859-1";
|
428 |
|
|
MSDOS_SB(sb)->nls_io = NULL;
|
429 |
|
|
if (MSDOS_SB(sb)->options.isvfat && !opts.utf8) {
|
430 |
|
|
MSDOS_SB(sb)->nls_io = load_nls(p);
|
431 |
|
|
if (! MSDOS_SB(sb)->nls_io) {
|
432 |
|
|
/* Fail only if explicit charset specified */
|
433 |
|
|
if (opts.iocharset) {
|
434 |
|
|
kfree(opts.iocharset);
|
435 |
|
|
unload_nls(MSDOS_SB(sb)->nls_disk);
|
436 |
|
|
sb->s_dev = 0;
|
437 |
|
|
MOD_DEC_USE_COUNT;
|
438 |
|
|
return NULL;
|
439 |
|
|
} else {
|
440 |
|
|
MSDOS_SB(sb)->nls_io = load_nls_default();
|
441 |
|
|
}
|
442 |
|
|
}
|
443 |
|
|
}
|
444 |
|
|
|
445 |
|
|
if (!(sb->s_mounted = iget(sb,MSDOS_ROOT_INO))) {
|
446 |
|
|
sb->s_dev = 0;
|
447 |
|
|
printk("get root inode failed\n");
|
448 |
|
|
unload_nls(MSDOS_SB(sb)->nls_disk);
|
449 |
|
|
if (MSDOS_SB(sb)->nls_io) unload_nls(MSDOS_SB(sb)->nls_io);
|
450 |
|
|
if (opts.iocharset) kfree(opts.iocharset);
|
451 |
|
|
MOD_DEC_USE_COUNT;
|
452 |
|
|
return NULL;
|
453 |
|
|
}
|
454 |
|
|
|
455 |
|
|
return sb;
|
456 |
|
|
}
|
457 |
|
|
|
458 |
|
|
|
459 |
|
|
void fat_statfs(struct super_block *sb,struct statfs *buf, int bufsiz)
|
460 |
|
|
{
|
461 |
|
|
int free,nr;
|
462 |
|
|
struct statfs tmp;
|
463 |
|
|
|
464 |
|
|
lock_fat(sb);
|
465 |
|
|
if (MSDOS_SB(sb)->free_clusters != -1)
|
466 |
|
|
free = MSDOS_SB(sb)->free_clusters;
|
467 |
|
|
else {
|
468 |
|
|
free = 0;
|
469 |
|
|
for (nr = 2; nr < MSDOS_SB(sb)->clusters+2; nr++)
|
470 |
|
|
if (!fat_access(sb,nr,-1)) free++;
|
471 |
|
|
MSDOS_SB(sb)->free_clusters = free;
|
472 |
|
|
}
|
473 |
|
|
unlock_fat(sb);
|
474 |
|
|
tmp.f_type = sb->s_magic;
|
475 |
|
|
tmp.f_bsize = MSDOS_SB(sb)->cluster_size*SECTOR_SIZE;
|
476 |
|
|
tmp.f_blocks = MSDOS_SB(sb)->clusters;
|
477 |
|
|
tmp.f_bfree = free;
|
478 |
|
|
tmp.f_bavail = free;
|
479 |
|
|
tmp.f_files = 0;
|
480 |
|
|
tmp.f_ffree = 0;
|
481 |
|
|
tmp.f_namelen = 12;
|
482 |
|
|
FAT_COPY_TO_USER(buf, &tmp, bufsiz);
|
483 |
|
|
}
|
484 |
|
|
|
485 |
|
|
|
486 |
|
|
int fat_bmap(struct inode *inode,int block)
|
487 |
|
|
{
|
488 |
|
|
struct msdos_sb_info *sb;
|
489 |
|
|
int cluster,offset;
|
490 |
|
|
|
491 |
|
|
sb = MSDOS_SB(inode->i_sb);
|
492 |
|
|
if ((inode->i_ino == MSDOS_ROOT_INO) && (sb->fat_bits != 32)) {
|
493 |
|
|
return sb->dir_start + block;
|
494 |
|
|
}
|
495 |
|
|
cluster = block/sb->cluster_size;
|
496 |
|
|
offset = block % sb->cluster_size;
|
497 |
|
|
if (!(cluster = get_cluster(inode,cluster))) return 0;
|
498 |
|
|
return (cluster-2)*sb->cluster_size+sb->data_start+offset;
|
499 |
|
|
}
|
500 |
|
|
|
501 |
|
|
static int is_exec(char *extension)
|
502 |
|
|
{
|
503 |
|
|
char *exe_extensions = "EXECOMBAT", *walk;
|
504 |
|
|
|
505 |
|
|
for (walk = exe_extensions; *walk; walk += 3)
|
506 |
|
|
if (!strncmp(extension, walk, 3))
|
507 |
|
|
return 1;
|
508 |
|
|
return 0;
|
509 |
|
|
}
|
510 |
|
|
|
511 |
|
|
void fat_read_inode(struct inode *inode, struct inode_operations *fs_dir_inode_ops)
|
512 |
|
|
{
|
513 |
|
|
struct super_block *sb = inode->i_sb;
|
514 |
|
|
struct buffer_head *bh;
|
515 |
|
|
struct msdos_dir_entry *raw_entry;
|
516 |
|
|
int nr;
|
517 |
|
|
|
518 |
|
|
PRINTK(("fat_read_inode: inode=%p, sb->dir_start=0x%x\n",
|
519 |
|
|
inode, MSDOS_SB(sb)->dir_start));
|
520 |
|
|
MSDOS_I(inode)->i_busy = 0;
|
521 |
|
|
MSDOS_I(inode)->i_depend = MSDOS_I(inode)->i_old = NULL;
|
522 |
|
|
MSDOS_I(inode)->i_linked = MSDOS_I(inode)->i_oldlink = NULL;
|
523 |
|
|
MSDOS_I(inode)->i_binary = 1;
|
524 |
|
|
inode->i_uid = MSDOS_SB(sb)->options.fs_uid;
|
525 |
|
|
inode->i_gid = MSDOS_SB(sb)->options.fs_gid;
|
526 |
|
|
inode->i_version = ++event;
|
527 |
|
|
if (inode->i_ino == MSDOS_ROOT_INO) {
|
528 |
|
|
inode->i_mode = (S_IRWXUGO & ~MSDOS_SB(sb)->options.fs_umask) |
|
529 |
|
|
S_IFDIR;
|
530 |
|
|
inode->i_op = fs_dir_inode_ops;
|
531 |
|
|
if (MSDOS_SB(sb)->fat_bits == 32) {
|
532 |
|
|
MSDOS_I(inode)->i_start = MSDOS_SB(sb)->root_cluster;
|
533 |
|
|
if ((nr = MSDOS_I(inode)->i_start) != 0) {
|
534 |
|
|
while (nr != -1) {
|
535 |
|
|
inode->i_size += SECTOR_SIZE*MSDOS_SB(sb)->cluster_size;
|
536 |
|
|
if (!(nr = fat_access(sb,nr,-1))) {
|
537 |
|
|
printk("Directory %ld: bad FAT\n",
|
538 |
|
|
inode->i_ino);
|
539 |
|
|
break;
|
540 |
|
|
}
|
541 |
|
|
}
|
542 |
|
|
}
|
543 |
|
|
} else {
|
544 |
|
|
MSDOS_I(inode)->i_start = 0;
|
545 |
|
|
inode->i_size = MSDOS_SB(sb)->dir_entries*
|
546 |
|
|
sizeof(struct msdos_dir_entry);
|
547 |
|
|
}
|
548 |
|
|
inode->i_blksize = MSDOS_SB(sb)->cluster_size*
|
549 |
|
|
SECTOR_SIZE;
|
550 |
|
|
inode->i_blocks = (inode->i_size+inode->i_blksize-1)/
|
551 |
|
|
inode->i_blksize*MSDOS_SB(sb)->cluster_size;
|
552 |
|
|
MSDOS_I(inode)->i_logstart = 0;
|
553 |
|
|
|
554 |
|
|
MSDOS_I(inode)->i_attrs = 0;
|
555 |
|
|
inode->i_mtime = inode->i_atime = inode->i_ctime = 0;
|
556 |
|
|
inode->i_nlink = fat_subdirs(inode)+2;
|
557 |
|
|
/* subdirs (neither . nor ..) plus . and "self" */
|
558 |
|
|
return;
|
559 |
|
|
}
|
560 |
|
|
if (!(bh = fat_bread(sb, inode->i_ino >> MSDOS_DPB_BITS))) {
|
561 |
|
|
printk("dev = %s, ino = %ld\n",
|
562 |
|
|
kdevname(inode->i_dev), inode->i_ino);
|
563 |
|
|
panic("fat_read_inode: unable to read i-node block");
|
564 |
|
|
}
|
565 |
|
|
raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
|
566 |
|
|
[inode->i_ino & (MSDOS_DPB-1)];
|
567 |
|
|
if ((raw_entry->attr & ATTR_DIR) && !IS_FREE(raw_entry->name)) {
|
568 |
|
|
inode->i_mode = MSDOS_MKMODE(raw_entry->attr,S_IRWXUGO &
|
569 |
|
|
~MSDOS_SB(sb)->options.fs_umask) | S_IFDIR;
|
570 |
|
|
inode->i_op = fs_dir_inode_ops;
|
571 |
|
|
|
572 |
|
|
MSDOS_I(inode)->i_start = CF_LE_W(raw_entry->start);
|
573 |
|
|
if (MSDOS_SB(sb)->fat_bits == 32) {
|
574 |
|
|
MSDOS_I(inode)->i_start |=
|
575 |
|
|
(CF_LE_W(raw_entry->starthi) << 16);
|
576 |
|
|
}
|
577 |
|
|
MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
|
578 |
|
|
inode->i_nlink = fat_subdirs(inode);
|
579 |
|
|
/* includes .., compensating for "self" */
|
580 |
|
|
#ifdef DEBUG
|
581 |
|
|
if (!inode->i_nlink) {
|
582 |
|
|
printk("directory %d: i_nlink == 0\n",inode->i_ino);
|
583 |
|
|
inode->i_nlink = 1;
|
584 |
|
|
}
|
585 |
|
|
#endif
|
586 |
|
|
inode->i_size = 0;
|
587 |
|
|
if ((nr = MSDOS_I(inode)->i_start) != 0)
|
588 |
|
|
while (nr != -1) {
|
589 |
|
|
inode->i_size += SECTOR_SIZE*MSDOS_SB(inode->
|
590 |
|
|
i_sb)->cluster_size;
|
591 |
|
|
if (!(nr = fat_access(sb,nr,-1))) {
|
592 |
|
|
printk("Directory %ld: bad FAT\n",
|
593 |
|
|
inode->i_ino);
|
594 |
|
|
break;
|
595 |
|
|
}
|
596 |
|
|
}
|
597 |
|
|
} else { /* not a directory */
|
598 |
|
|
inode->i_mode = MSDOS_MKMODE(raw_entry->attr,
|
599 |
|
|
((IS_NOEXEC(inode) ||
|
600 |
|
|
(MSDOS_SB(sb)->options.showexec &&
|
601 |
|
|
!is_exec(raw_entry->ext)))
|
602 |
|
|
? S_IRUGO|S_IWUGO : S_IRWXUGO)
|
603 |
|
|
& ~MSDOS_SB(sb)->options.fs_umask) | S_IFREG;
|
604 |
|
|
inode->i_op = (sb->s_blocksize == 1024)
|
605 |
|
|
? &fat_file_inode_operations_1024
|
606 |
|
|
: &fat_file_inode_operations;
|
607 |
|
|
MSDOS_I(inode)->i_start = CF_LE_W(raw_entry->start);
|
608 |
|
|
if (MSDOS_SB(sb)->fat_bits == 32) {
|
609 |
|
|
MSDOS_I(inode)->i_start |=
|
610 |
|
|
(CF_LE_W(raw_entry->starthi) << 16);
|
611 |
|
|
}
|
612 |
|
|
MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
|
613 |
|
|
inode->i_nlink = 1;
|
614 |
|
|
inode->i_size = CF_LE_L(raw_entry->size);
|
615 |
|
|
}
|
616 |
|
|
if(raw_entry->attr & ATTR_SYS)
|
617 |
|
|
if (MSDOS_SB(sb)->options.sys_immutable)
|
618 |
|
|
inode->i_flags |= S_IMMUTABLE;
|
619 |
|
|
MSDOS_I(inode)->i_binary = is_binary(MSDOS_SB(sb)->options.conversion,
|
620 |
|
|
raw_entry->ext);
|
621 |
|
|
MSDOS_I(inode)->i_attrs = raw_entry->attr & ATTR_UNUSED;
|
622 |
|
|
/* this is as close to the truth as we can get ... */
|
623 |
|
|
inode->i_blksize = MSDOS_SB(sb)->cluster_size*SECTOR_SIZE;
|
624 |
|
|
inode->i_blocks = (inode->i_size+inode->i_blksize-1)/
|
625 |
|
|
inode->i_blksize*MSDOS_SB(sb)->cluster_size;
|
626 |
|
|
inode->i_mtime = inode->i_atime =
|
627 |
|
|
date_dos2unix(CF_LE_W(raw_entry->time),CF_LE_W(raw_entry->date));
|
628 |
|
|
inode->i_ctime =
|
629 |
|
|
MSDOS_SB(sb)->options.isvfat
|
630 |
|
|
? date_dos2unix(CF_LE_W(raw_entry->ctime),CF_LE_W(raw_entry->cdate))
|
631 |
|
|
: inode->i_mtime;
|
632 |
|
|
fat_brelse(sb, bh);
|
633 |
|
|
}
|
634 |
|
|
|
635 |
|
|
|
636 |
|
|
void fat_write_inode(struct inode *inode)
|
637 |
|
|
{
|
638 |
|
|
struct super_block *sb = inode->i_sb;
|
639 |
|
|
struct buffer_head *bh;
|
640 |
|
|
struct msdos_dir_entry *raw_entry;
|
641 |
|
|
struct inode *linked;
|
642 |
|
|
|
643 |
|
|
linked = MSDOS_I(inode)->i_linked;
|
644 |
|
|
if (linked) {
|
645 |
|
|
if (MSDOS_I(linked)->i_oldlink != inode) {
|
646 |
|
|
printk("Invalid link (0x%p): expected 0x%p, got 0x%p\n",
|
647 |
|
|
linked, inode, MSDOS_I(linked)->i_oldlink);
|
648 |
|
|
fat_fs_panic(sb,"...");
|
649 |
|
|
return;
|
650 |
|
|
}
|
651 |
|
|
linked->i_version = ++event;
|
652 |
|
|
linked->i_mode = inode->i_mode;
|
653 |
|
|
linked->i_uid = inode->i_uid;
|
654 |
|
|
linked->i_gid = inode->i_gid;
|
655 |
|
|
linked->i_size = inode->i_size;
|
656 |
|
|
linked->i_atime = inode->i_atime;
|
657 |
|
|
linked->i_mtime = inode->i_mtime;
|
658 |
|
|
linked->i_ctime = inode->i_ctime;
|
659 |
|
|
linked->i_blocks = inode->i_blocks;
|
660 |
|
|
linked->i_atime = inode->i_atime;
|
661 |
|
|
MSDOS_I(linked)->i_attrs = MSDOS_I(inode)->i_attrs;
|
662 |
|
|
linked->i_dirt = 1;
|
663 |
|
|
}
|
664 |
|
|
|
665 |
|
|
inode->i_dirt = 0;
|
666 |
|
|
if (inode->i_ino == MSDOS_ROOT_INO || !inode->i_nlink) return;
|
667 |
|
|
if (!(bh = fat_bread(sb, inode->i_ino >> MSDOS_DPB_BITS))) {
|
668 |
|
|
printk("dev = %s, ino = %ld\n",
|
669 |
|
|
kdevname(inode->i_dev), inode->i_ino);
|
670 |
|
|
panic("msdos_write_inode: unable to read i-node block");
|
671 |
|
|
}
|
672 |
|
|
raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
|
673 |
|
|
[inode->i_ino & (MSDOS_DPB-1)];
|
674 |
|
|
if (S_ISDIR(inode->i_mode)) {
|
675 |
|
|
raw_entry->attr = ATTR_DIR;
|
676 |
|
|
raw_entry->size = 0;
|
677 |
|
|
}
|
678 |
|
|
else {
|
679 |
|
|
raw_entry->attr = ATTR_NONE;
|
680 |
|
|
raw_entry->size = CT_LE_L(inode->i_size);
|
681 |
|
|
}
|
682 |
|
|
raw_entry->attr |= MSDOS_MKATTR(inode->i_mode) |
|
683 |
|
|
MSDOS_I(inode)->i_attrs;
|
684 |
|
|
raw_entry->start = CT_LE_W(MSDOS_I(inode)->i_logstart);
|
685 |
|
|
raw_entry->starthi = CT_LE_W(MSDOS_I(inode)->i_logstart >> 16);
|
686 |
|
|
fat_date_unix2dos(inode->i_mtime,&raw_entry->time,&raw_entry->date);
|
687 |
|
|
raw_entry->time = CT_LE_W(raw_entry->time);
|
688 |
|
|
raw_entry->date = CT_LE_W(raw_entry->date);
|
689 |
|
|
if (MSDOS_SB(sb)->options.isvfat) {
|
690 |
|
|
fat_date_unix2dos(inode->i_ctime,&raw_entry->ctime,&raw_entry->cdate);
|
691 |
|
|
raw_entry->ctime = CT_LE_W(raw_entry->ctime);
|
692 |
|
|
raw_entry->cdate = CT_LE_W(raw_entry->cdate);
|
693 |
|
|
}
|
694 |
|
|
fat_mark_buffer_dirty(sb, bh, 1);
|
695 |
|
|
fat_brelse(sb, bh);
|
696 |
|
|
}
|
697 |
|
|
|
698 |
|
|
|
699 |
|
|
int fat_notify_change(struct inode * inode,struct iattr * attr)
|
700 |
|
|
{
|
701 |
|
|
struct super_block *sb = inode->i_sb;
|
702 |
|
|
int error;
|
703 |
|
|
|
704 |
|
|
error = inode_change_ok(inode, attr);
|
705 |
|
|
if (error)
|
706 |
|
|
return MSDOS_SB(sb)->options.quiet ? 0 : error;
|
707 |
|
|
|
708 |
|
|
if (((attr->ia_valid & ATTR_UID) &&
|
709 |
|
|
(attr->ia_uid != MSDOS_SB(sb)->options.fs_uid)) ||
|
710 |
|
|
((attr->ia_valid & ATTR_GID) &&
|
711 |
|
|
(attr->ia_gid != MSDOS_SB(sb)->options.fs_gid)) ||
|
712 |
|
|
((attr->ia_valid & ATTR_MODE) &&
|
713 |
|
|
(attr->ia_mode & ~MSDOS_VALID_MODE)))
|
714 |
|
|
error = -EPERM;
|
715 |
|
|
|
716 |
|
|
if (error)
|
717 |
|
|
return MSDOS_SB(sb)->options.quiet ? 0 : error;
|
718 |
|
|
|
719 |
|
|
inode_setattr(inode, attr);
|
720 |
|
|
|
721 |
|
|
if (IS_NOEXEC(inode) && !S_ISDIR(inode->i_mode))
|
722 |
|
|
inode->i_mode &= S_IFMT | S_IRUGO | S_IWUGO;
|
723 |
|
|
else
|
724 |
|
|
inode->i_mode |= S_IXUGO;
|
725 |
|
|
|
726 |
|
|
inode->i_mode = ((inode->i_mode & S_IFMT) | ((((inode->i_mode & S_IRWXU
|
727 |
|
|
& ~MSDOS_SB(sb)->options.fs_umask) | S_IRUSR) >> 6)*S_IXUGO)) &
|
728 |
|
|
~MSDOS_SB(sb)->options.fs_umask;
|
729 |
|
|
return 0;
|
730 |
|
|
}
|
731 |
|
|
|
732 |
|
|
|
733 |
|
|
#ifdef MODULE
|
734 |
|
|
int init_module(void)
|
735 |
|
|
{
|
736 |
|
|
return init_fat_fs();
|
737 |
|
|
}
|
738 |
|
|
|
739 |
|
|
|
740 |
|
|
void cleanup_module(void)
|
741 |
|
|
{
|
742 |
|
|
/* Nothing to be done, really! */
|
743 |
|
|
return;
|
744 |
|
|
}
|
745 |
|
|
#endif
|
746 |
|
|
|