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