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