This source file includes following definitions.
- msdos_put_inode
- msdos_put_super
- parse_options
- msdos_read_super
- msdos_statfs
- msdos_bmap
- msdos_read_inode
- msdos_write_inode
- msdos_notify_change
- init_module
- cleanup_module
1
2
3
4
5
6
7 #ifdef MODULE
8 #include <linux/module.h>
9 #include <linux/version.h>
10 #else
11 #define MOD_INC_USE_COUNT
12 #define MOD_DEC_USE_COUNT
13 #endif
14
15 #include <linux/msdos_fs.h>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/errno.h>
19 #include <linux/string.h>
20 #include <linux/ctype.h>
21 #include <linux/major.h>
22 #include <linux/blkdev.h>
23 #include <linux/fs.h>
24 #include <linux/stat.h>
25 #include <linux/locks.h>
26
27 #include "msbuffer.h"
28
29 #include <asm/segment.h>
30
31 void msdos_put_inode(struct inode *inode)
32 {
33 struct inode *depend;
34 struct super_block *sb;
35
36 if (inode->i_nlink) {
37 if (MSDOS_I(inode)->i_busy) cache_inval_inode(inode);
38 return;
39 }
40 inode->i_size = 0;
41 msdos_truncate(inode);
42 depend = MSDOS_I(inode)->i_depend;
43 sb = inode->i_sb;
44 clear_inode(inode);
45 if (depend) {
46 if (MSDOS_I(depend)->i_old != inode) {
47 printk("Invalid link (0x%p): expected 0x%p, got 0x%p\n",
48 depend, inode, MSDOS_I(depend)->i_old);
49 fs_panic(sb,"...");
50 return;
51 }
52 MSDOS_I(depend)->i_old = NULL;
53 iput(depend);
54 }
55 }
56
57
58 void msdos_put_super(struct super_block *sb)
59 {
60 cache_inval_dev(sb->s_dev);
61 set_blocksize (sb->s_dev,BLOCK_SIZE);
62 lock_super(sb);
63 sb->s_dev = 0;
64 unlock_super(sb);
65 MOD_DEC_USE_COUNT;
66 return;
67 }
68
69
70 static struct super_operations msdos_sops = {
71 msdos_read_inode,
72 msdos_notify_change,
73 msdos_write_inode,
74 msdos_put_inode,
75 msdos_put_super,
76 NULL,
77 msdos_statfs,
78 NULL
79 };
80
81
82 static int parse_options(char *options,char *check,char *conversion,uid_t *uid,
83 gid_t *gid,int *umask,int *debug,int *fat,int *quiet,
84 int *blksize)
85 {
86 char *this_char,*value;
87
88 *check = 'n';
89 *conversion = 'b';
90 *uid = current->uid;
91 *gid = current->gid;
92 *umask = current->fs->umask;
93 *debug = *fat = *quiet = 0;
94 if (!options) return 1;
95 for (this_char = strtok(options,","); this_char; this_char = strtok(NULL,",")) {
96 if ((value = strchr(this_char,'=')) != NULL)
97 *value++ = 0;
98 if (!strcmp(this_char,"check") && value) {
99 if (value[0] && !value[1] && strchr("rns",*value))
100 *check = *value;
101 else if (!strcmp(value,"relaxed")) *check = 'r';
102 else if (!strcmp(value,"normal")) *check = 'n';
103 else if (!strcmp(value,"strict")) *check = 's';
104 else return 0;
105 }
106 else if (!strcmp(this_char,"conv") && value) {
107 if (value[0] && !value[1] && strchr("bta",*value))
108 *conversion = *value;
109 else if (!strcmp(value,"binary")) *conversion = 'b';
110 else if (!strcmp(value,"text")) *conversion = 't';
111 else if (!strcmp(value,"auto")) *conversion = 'a';
112 else return 0;
113 }
114 else if (!strcmp(this_char,"uid")) {
115 if (!value || !*value)
116 return 0;
117 *uid = simple_strtoul(value,&value,0);
118 if (*value)
119 return 0;
120 }
121 else if (!strcmp(this_char,"gid")) {
122 if (!value || !*value)
123 return 0;
124 *gid = simple_strtoul(value,&value,0);
125 if (*value)
126 return 0;
127 }
128 else if (!strcmp(this_char,"umask")) {
129 if (!value || !*value)
130 return 0;
131 *umask = simple_strtoul(value,&value,8);
132 if (*value)
133 return 0;
134 }
135 else if (!strcmp(this_char,"debug")) {
136 if (value) return 0;
137 *debug = 1;
138 }
139 else if (!strcmp(this_char,"fat")) {
140 if (!value || !*value)
141 return 0;
142 *fat = simple_strtoul(value,&value,0);
143 if (*value || (*fat != 12 && *fat != 16))
144 return 0;
145 }
146 else if (!strcmp(this_char,"quiet")) {
147 if (value) return 0;
148 *quiet = 1;
149 }
150 else if (!strcmp(this_char,"blocksize")) {
151 *blksize = simple_strtoul(value,&value,0);
152 if (*value)
153 return 0;
154 if (*blksize != 512 && *blksize != 1024){
155 printk ("MSDOS FS: Invalid blocksize (512 or 1024)\n");
156 }
157 }
158 else return 0;
159 }
160 return 1;
161 }
162
163
164
165
166 struct super_block *msdos_read_super(struct super_block *sb,void *data,
167 int silent)
168 {
169 struct buffer_head *bh;
170 struct msdos_boot_sector *b;
171 int data_sectors,logical_sector_size,sector_mult,fat_clusters=0;
172 int debug,error,fat,quiet;
173 char check,conversion;
174 uid_t uid;
175 gid_t gid;
176 int umask;
177 int blksize = 512;
178
179 MOD_INC_USE_COUNT;
180 if (hardsect_size[MAJOR(sb->s_dev)] != NULL){
181 blksize = hardsect_size[MAJOR(sb->s_dev)][MINOR(sb->s_dev)];
182 if (blksize != 512){
183 printk ("MSDOS: Hardware sector size is %d\n",blksize);
184 }
185 }
186 if (!parse_options((char *) data,&check,&conversion,&uid,&gid,&umask,
187 &debug,&fat,&quiet,&blksize)
188 || (blksize != 512 && blksize != 1024)) {
189 sb->s_dev = 0;
190 MOD_DEC_USE_COUNT;
191 return NULL;
192 }
193 cache_init();
194 lock_super(sb);
195
196 sb->s_blocksize = 1024;
197 set_blocksize(sb->s_dev, 1024);
198 bh = bread(sb->s_dev, 0, 1024);
199 unlock_super(sb);
200 if (bh == NULL || !msdos_is_uptodate(sb,bh)) {
201 brelse (bh);
202 sb->s_dev = 0;
203 printk("MSDOS bread failed\n");
204 MOD_DEC_USE_COUNT;
205 return NULL;
206 }
207 b = (struct msdos_boot_sector *) bh->b_data;
208 set_blocksize(sb->s_dev, blksize);
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224 #define ROUND_TO_MULTIPLE(n,m) ((n) && (m) ? (n)+(m)-1-((n)-1)%(m) : 0)
225
226
227 logical_sector_size = CF_LE_W(*(unsigned short *) &b->sector_size);
228 sector_mult = logical_sector_size >> SECTOR_BITS;
229 MSDOS_SB(sb)->cluster_size = b->cluster_size*sector_mult;
230 MSDOS_SB(sb)->fats = b->fats;
231 MSDOS_SB(sb)->fat_start = CF_LE_W(b->reserved)*sector_mult;
232 MSDOS_SB(sb)->fat_length = CF_LE_W(b->fat_length)*sector_mult;
233 MSDOS_SB(sb)->dir_start = (CF_LE_W(b->reserved)+b->fats*CF_LE_W(
234 b->fat_length))*sector_mult;
235 MSDOS_SB(sb)->dir_entries = CF_LE_W(*((unsigned short *) &b->dir_entries
236 ));
237 MSDOS_SB(sb)->data_start = MSDOS_SB(sb)->dir_start+ROUND_TO_MULTIPLE((
238 MSDOS_SB(sb)->dir_entries << MSDOS_DIR_BITS) >> SECTOR_BITS,
239 sector_mult);
240 data_sectors = (CF_LE_W(*((unsigned short *) &b->sectors)) ?
241 CF_LE_W(*((unsigned short *) &b->sectors)) :
242 CF_LE_L(b->total_sect))*sector_mult-MSDOS_SB(sb)->data_start;
243 error = !b->cluster_size || !sector_mult;
244 if (!error) {
245 MSDOS_SB(sb)->clusters = b->cluster_size ? data_sectors/
246 b->cluster_size/sector_mult : 0;
247 MSDOS_SB(sb)->fat_bits = fat ? fat : MSDOS_SB(sb)->clusters >
248 MSDOS_FAT12 ? 16 : 12;
249 fat_clusters = MSDOS_SB(sb)->fat_length*SECTOR_SIZE*8/
250 MSDOS_SB(sb)->fat_bits;
251 error = !MSDOS_SB(sb)->fats || (MSDOS_SB(sb)->dir_entries &
252 (MSDOS_DPS-1)) || MSDOS_SB(sb)->clusters+2 > fat_clusters+
253 MSDOS_MAX_EXTRA || (logical_sector_size & (SECTOR_SIZE-1))
254 || !b->secs_track || !b->heads;
255 }
256 brelse(bh);
257
258
259
260
261 sb->s_blocksize = blksize;
262
263
264
265 sb->s_blocksize_bits = blksize == 512 ? 9 : 10;
266 if (error || debug) {
267
268 printk("[MS-DOS FS Rel. 12,FAT %d,check=%c,conv=%c,"
269 "uid=%d,gid=%d,umask=%03o%s]\n",MSDOS_SB(sb)->fat_bits,check,
270 conversion,uid,gid,umask,MSDOS_CAN_BMAP(MSDOS_SB(sb)) ?
271 ",bmap" : "");
272 printk("[me=0x%x,cs=%d,#f=%d,fs=%d,fl=%d,ds=%d,de=%d,data=%d,"
273 "se=%d,ts=%ld,ls=%d]\n",b->media,MSDOS_SB(sb)->cluster_size,
274 MSDOS_SB(sb)->fats,MSDOS_SB(sb)->fat_start,MSDOS_SB(sb)->
275 fat_length,MSDOS_SB(sb)->dir_start,MSDOS_SB(sb)->dir_entries,
276 MSDOS_SB(sb)->data_start,CF_LE_W(*(unsigned short *) &b->
277 sectors),(unsigned long)b->total_sect,logical_sector_size);
278 printk ("Transaction block size = %d\n",blksize);
279 }
280 if (MSDOS_SB(sb)->clusters+2 > fat_clusters)
281 MSDOS_SB(sb)->clusters = fat_clusters-2;
282 if (error) {
283 if (!silent)
284 printk("VFS: Can't find a valid MSDOS filesystem on dev "
285 "%s.\n", kdevname(sb->s_dev));
286 sb->s_dev = 0;
287 MOD_DEC_USE_COUNT;
288 return NULL;
289 }
290 sb->s_magic = MSDOS_SUPER_MAGIC;
291 MSDOS_SB(sb)->name_check = check;
292 MSDOS_SB(sb)->conversion = conversion;
293
294 sb->s_op = &msdos_sops;
295 MSDOS_SB(sb)->fs_uid = uid;
296 MSDOS_SB(sb)->fs_gid = gid;
297 MSDOS_SB(sb)->fs_umask = umask;
298 MSDOS_SB(sb)->quiet = quiet;
299 MSDOS_SB(sb)->free_clusters = -1;
300 MSDOS_SB(sb)->fat_wait = NULL;
301 MSDOS_SB(sb)->fat_lock = 0;
302 MSDOS_SB(sb)->prev_free = 0;
303 if (!(sb->s_mounted = iget(sb,MSDOS_ROOT_INO))) {
304 sb->s_dev = 0;
305 printk("get root inode failed\n");
306 MOD_DEC_USE_COUNT;
307 return NULL;
308 }
309 return sb;
310 }
311
312
313 void msdos_statfs(struct super_block *sb,struct statfs *buf, int bufsiz)
314 {
315 int free,nr;
316 struct statfs tmp;
317
318 lock_fat(sb);
319 if (MSDOS_SB(sb)->free_clusters != -1)
320 free = MSDOS_SB(sb)->free_clusters;
321 else {
322 free = 0;
323 for (nr = 2; nr < MSDOS_SB(sb)->clusters+2; nr++)
324 if (!fat_access(sb,nr,-1)) free++;
325 MSDOS_SB(sb)->free_clusters = free;
326 }
327 unlock_fat(sb);
328 tmp.f_type = sb->s_magic;
329 tmp.f_bsize = MSDOS_SB(sb)->cluster_size*SECTOR_SIZE;
330 tmp.f_blocks = MSDOS_SB(sb)->clusters;
331 tmp.f_bfree = free;
332 tmp.f_bavail = free;
333 tmp.f_files = 0;
334 tmp.f_ffree = 0;
335 tmp.f_namelen = 12;
336 memcpy_tofs(buf, &tmp, bufsiz);
337 }
338
339
340 int msdos_bmap(struct inode *inode,int block)
341 {
342 struct msdos_sb_info *sb;
343 int cluster,offset;
344
345 sb = MSDOS_SB(inode->i_sb);
346 if (inode->i_ino == MSDOS_ROOT_INO) {
347 return sb->dir_start + block;
348 }
349 cluster = block/sb->cluster_size;
350 offset = block % sb->cluster_size;
351 if (!(cluster = get_cluster(inode,cluster))) return 0;
352 return (cluster-2)*sb->cluster_size+sb->data_start+offset;
353 }
354
355
356 void msdos_read_inode(struct inode *inode)
357 {
358 struct super_block *sb = inode->i_sb;
359 struct buffer_head *bh;
360 struct msdos_dir_entry *raw_entry;
361 int nr;
362
363
364 MSDOS_I(inode)->i_busy = 0;
365 MSDOS_I(inode)->i_depend = MSDOS_I(inode)->i_old = NULL;
366 MSDOS_I(inode)->i_binary = 1;
367 inode->i_uid = MSDOS_SB(inode->i_sb)->fs_uid;
368 inode->i_gid = MSDOS_SB(inode->i_sb)->fs_gid;
369 if (inode->i_ino == MSDOS_ROOT_INO) {
370 inode->i_mode = (S_IRWXUGO & ~MSDOS_SB(inode->i_sb)->fs_umask) |
371 S_IFDIR;
372 inode->i_op = &msdos_dir_inode_operations;
373 inode->i_nlink = msdos_subdirs(inode)+2;
374
375 inode->i_size = MSDOS_SB(inode->i_sb)->dir_entries*
376 sizeof(struct msdos_dir_entry);
377 inode->i_blksize = MSDOS_SB(inode->i_sb)->cluster_size*
378 SECTOR_SIZE;
379 inode->i_blocks = (inode->i_size+inode->i_blksize-1)/
380 inode->i_blksize*MSDOS_SB(inode->i_sb)->cluster_size;
381 MSDOS_I(inode)->i_start = 0;
382 MSDOS_I(inode)->i_attrs = 0;
383 inode->i_mtime = inode->i_atime = inode->i_ctime = 0;
384 return;
385 }
386 if (!(bh = bread(inode->i_dev,inode->i_ino >> MSDOS_DPB_BITS,
387 SECTOR_SIZE))) {
388 printk("dev = %s, ino = %ld\n",
389 kdevname(inode->i_dev), inode->i_ino);
390 panic("msdos_read_inode: unable to read i-node block");
391 }
392 raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
393 [inode->i_ino & (MSDOS_DPB-1)];
394 if ((raw_entry->attr & ATTR_DIR) && !IS_FREE(raw_entry->name)) {
395 inode->i_mode = MSDOS_MKMODE(raw_entry->attr,S_IRWXUGO &
396 ~MSDOS_SB(inode->i_sb)->fs_umask) | S_IFDIR;
397 inode->i_op = &msdos_dir_inode_operations;
398 MSDOS_I(inode)->i_start = CF_LE_W(raw_entry->start);
399 inode->i_nlink = msdos_subdirs(inode);
400
401 #ifdef DEBUG
402 if (!inode->i_nlink) {
403 printk("directory %d: i_nlink == 0\n",inode->i_ino);
404 inode->i_nlink = 1;
405 }
406 #endif
407 inode->i_size = 0;
408 if ((nr = CF_LE_W(raw_entry->start)) != 0)
409 while (nr != -1) {
410 inode->i_size += SECTOR_SIZE*MSDOS_SB(inode->
411 i_sb)->cluster_size;
412 if (!(nr = fat_access(inode->i_sb,nr,-1))) {
413 printk("Directory %ld: bad FAT\n",
414 inode->i_ino);
415 break;
416 }
417 }
418 }
419 else {
420 inode->i_mode = MSDOS_MKMODE(raw_entry->attr,(IS_NOEXEC(inode)
421 ? S_IRUGO|S_IWUGO : S_IRWXUGO) & ~MSDOS_SB(inode->i_sb)->fs_umask) |
422 S_IFREG;
423 inode->i_op = sb->s_blocksize == 1024
424 ? &msdos_file_inode_operations_1024
425 : &msdos_file_inode_operations;
426 MSDOS_I(inode)->i_start = CF_LE_W(raw_entry->start);
427 inode->i_nlink = 1;
428 inode->i_size = CF_LE_L(raw_entry->size);
429 }
430 MSDOS_I(inode)->i_binary = is_binary(MSDOS_SB(inode->i_sb)->conversion,
431 raw_entry->ext);
432 MSDOS_I(inode)->i_attrs = raw_entry->attr & ATTR_UNUSED;
433
434 inode->i_blksize = MSDOS_SB(inode->i_sb)->cluster_size*SECTOR_SIZE;
435 inode->i_blocks = (inode->i_size+inode->i_blksize-1)/
436 inode->i_blksize*MSDOS_SB(inode->i_sb)->cluster_size;
437 inode->i_mtime = inode->i_atime = inode->i_ctime =
438 date_dos2unix(CF_LE_W(raw_entry->time),CF_LE_W(raw_entry->date));
439 brelse(bh);
440 }
441
442
443 void msdos_write_inode(struct inode *inode)
444 {
445 struct super_block *sb = inode->i_sb;
446 struct buffer_head *bh;
447 struct msdos_dir_entry *raw_entry;
448
449 inode->i_dirt = 0;
450 if (inode->i_ino == MSDOS_ROOT_INO || !inode->i_nlink) return;
451 if (!(bh = bread(inode->i_dev,inode->i_ino >> MSDOS_DPB_BITS,
452 SECTOR_SIZE))) {
453 printk("dev = %s, ino = %ld\n",
454 kdevname(inode->i_dev), inode->i_ino);
455 panic("msdos_write_inode: unable to read i-node block");
456 }
457 raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
458 [inode->i_ino & (MSDOS_DPB-1)];
459 if (S_ISDIR(inode->i_mode)) {
460 raw_entry->attr = ATTR_DIR;
461 raw_entry->size = 0;
462 }
463 else {
464 raw_entry->attr = ATTR_NONE;
465 raw_entry->size = CT_LE_L(inode->i_size);
466 }
467 raw_entry->attr |= MSDOS_MKATTR(inode->i_mode) |
468 MSDOS_I(inode)->i_attrs;
469 raw_entry->start = CT_LE_L(MSDOS_I(inode)->i_start);
470 date_unix2dos(inode->i_mtime,&raw_entry->time,&raw_entry->date);
471 raw_entry->time = CT_LE_W(raw_entry->time);
472 raw_entry->date = CT_LE_W(raw_entry->date);
473 mark_buffer_dirty(bh, 1);
474 brelse(bh);
475 }
476
477
478 int msdos_notify_change(struct inode * inode,struct iattr * attr)
479 {
480 int error;
481
482 error = inode_change_ok(inode, attr);
483 if (error)
484 return error;
485
486 if (((attr->ia_valid & ATTR_UID) &&
487 (attr->ia_uid != MSDOS_SB(inode->i_sb)->fs_uid)) ||
488 ((attr->ia_valid & ATTR_GID) &&
489 (attr->ia_gid != MSDOS_SB(inode->i_sb)->fs_gid)) ||
490 ((attr->ia_valid & ATTR_MODE) &&
491 (attr->ia_mode & ~MSDOS_VALID_MODE)))
492 error = -EPERM;
493
494 if (error)
495 return MSDOS_SB(inode->i_sb)->quiet ? 0 : error;
496
497 inode_setattr(inode, attr);
498
499 if (IS_NOEXEC(inode) && !S_ISDIR(inode->i_mode))
500 inode->i_mode &= S_IFMT | S_IRUGO | S_IWUGO;
501 else
502 inode->i_mode |= S_IXUGO;
503
504 inode->i_mode = ((inode->i_mode & S_IFMT) | ((((inode->i_mode & S_IRWXU
505 & ~MSDOS_SB(inode->i_sb)->fs_umask) | S_IRUSR) >> 6)*S_IXUGO)) &
506 ~MSDOS_SB(inode->i_sb)->fs_umask;
507 return 0;
508 }
509 #ifdef MODULE
510
511 char kernel_version[] = UTS_RELEASE;
512
513 static struct file_system_type msdos_fs_type = {
514 msdos_read_super, "msdos", 1, NULL
515 };
516
517 int init_module(void)
518 {
519 register_filesystem(&msdos_fs_type);
520 return 0;
521 }
522
523 void cleanup_module(void)
524 {
525 unregister_filesystem(&msdos_fs_type);
526 }
527
528 #endif
529