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