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;
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 error = !MSDOS_SB(sb)->fats || (MSDOS_SB(sb)->dir_entries &
250 (MSDOS_DPS-1)) || MSDOS_SB(sb)->clusters+2 > MSDOS_SB(sb)->
251 fat_length*SECTOR_SIZE*8/MSDOS_SB(sb)->fat_bits ||
252 (logical_sector_size & (SECTOR_SIZE-1)) || !b->secs_track ||
253 !b->heads;
254 }
255 brelse(bh);
256
257
258
259
260 sb->s_blocksize = blksize;
261
262
263
264 sb->s_blocksize_bits = blksize == 512 ? 9 : 10;
265 if (error || debug) {
266
267 printk("[MS-DOS FS Rel. 12,FAT %d,check=%c,conv=%c,"
268 "uid=%d,gid=%d,umask=%03o%s]\n",MSDOS_SB(sb)->fat_bits,check,
269 conversion,uid,gid,umask,MSDOS_CAN_BMAP(MSDOS_SB(sb)) ?
270 ",bmap" : "");
271 printk("[me=0x%x,cs=%d,#f=%d,fs=%d,fl=%d,ds=%d,de=%d,data=%d,"
272 "se=%d,ts=%ld,ls=%d]\n",b->media,MSDOS_SB(sb)->cluster_size,
273 MSDOS_SB(sb)->fats,MSDOS_SB(sb)->fat_start,MSDOS_SB(sb)->
274 fat_length,MSDOS_SB(sb)->dir_start,MSDOS_SB(sb)->dir_entries,
275 MSDOS_SB(sb)->data_start,CF_LE_W(*(unsigned short *) &b->
276 sectors),(unsigned long)b->total_sect,logical_sector_size);
277 printk ("Transaction block size = %d\n",blksize);
278 }
279 if (error) {
280 if (!silent)
281 printk("VFS: Can't find a valid MSDOS filesystem on dev 0x%04x.\n",
282 sb->s_dev);
283 sb->s_dev = 0;
284 MOD_DEC_USE_COUNT;
285 return NULL;
286 }
287 sb->s_magic = MSDOS_SUPER_MAGIC;
288 MSDOS_SB(sb)->name_check = check;
289 MSDOS_SB(sb)->conversion = conversion;
290
291 sb->s_op = &msdos_sops;
292 MSDOS_SB(sb)->fs_uid = uid;
293 MSDOS_SB(sb)->fs_gid = gid;
294 MSDOS_SB(sb)->fs_umask = umask;
295 MSDOS_SB(sb)->quiet = quiet;
296 MSDOS_SB(sb)->free_clusters = -1;
297 MSDOS_SB(sb)->fat_wait = NULL;
298 MSDOS_SB(sb)->fat_lock = 0;
299 MSDOS_SB(sb)->prev_free = 0;
300 if (!(sb->s_mounted = iget(sb,MSDOS_ROOT_INO))) {
301 sb->s_dev = 0;
302 printk("get root inode failed\n");
303 MOD_DEC_USE_COUNT;
304 return NULL;
305 }
306 return sb;
307 }
308
309
310 void msdos_statfs(struct super_block *sb,struct statfs *buf, int bufsiz)
311 {
312 int free,nr;
313 struct statfs tmp;
314
315 lock_fat(sb);
316 if (MSDOS_SB(sb)->free_clusters != -1)
317 free = MSDOS_SB(sb)->free_clusters;
318 else {
319 free = 0;
320 for (nr = 2; nr < MSDOS_SB(sb)->clusters+2; nr++)
321 if (!fat_access(sb,nr,-1)) free++;
322 MSDOS_SB(sb)->free_clusters = free;
323 }
324 unlock_fat(sb);
325 tmp.f_type = sb->s_magic;
326 tmp.f_bsize = MSDOS_SB(sb)->cluster_size*SECTOR_SIZE;
327 tmp.f_blocks = MSDOS_SB(sb)->clusters;
328 tmp.f_bfree = free;
329 tmp.f_bavail = free;
330 tmp.f_files = 0;
331 tmp.f_ffree = 0;
332 tmp.f_namelen = 12;
333 memcpy_tofs(buf, &tmp, bufsiz);
334 }
335
336
337 int msdos_bmap(struct inode *inode,int block)
338 {
339 struct msdos_sb_info *sb;
340 int cluster,offset;
341
342 sb = MSDOS_SB(inode->i_sb);
343 if (inode->i_ino == MSDOS_ROOT_INO) {
344 return sb->dir_start + block;
345 }
346 cluster = block/sb->cluster_size;
347 offset = block % sb->cluster_size;
348 if (!(cluster = get_cluster(inode,cluster))) return 0;
349 return (cluster-2)*sb->cluster_size+sb->data_start+offset;
350 }
351
352
353 void msdos_read_inode(struct inode *inode)
354 {
355 struct super_block *sb = inode->i_sb;
356 struct buffer_head *bh;
357 struct msdos_dir_entry *raw_entry;
358 int nr;
359
360
361 MSDOS_I(inode)->i_busy = 0;
362 MSDOS_I(inode)->i_depend = MSDOS_I(inode)->i_old = NULL;
363 MSDOS_I(inode)->i_binary = 1;
364 inode->i_uid = MSDOS_SB(inode->i_sb)->fs_uid;
365 inode->i_gid = MSDOS_SB(inode->i_sb)->fs_gid;
366 if (inode->i_ino == MSDOS_ROOT_INO) {
367 inode->i_mode = (S_IRWXUGO & ~MSDOS_SB(inode->i_sb)->fs_umask) |
368 S_IFDIR;
369 inode->i_op = &msdos_dir_inode_operations;
370 inode->i_nlink = msdos_subdirs(inode)+2;
371
372 inode->i_size = MSDOS_SB(inode->i_sb)->dir_entries*
373 sizeof(struct msdos_dir_entry);
374 inode->i_blksize = MSDOS_SB(inode->i_sb)->cluster_size*
375 SECTOR_SIZE;
376 inode->i_blocks = (inode->i_size+inode->i_blksize-1)/
377 inode->i_blksize*MSDOS_SB(inode->i_sb)->cluster_size;
378 MSDOS_I(inode)->i_start = 0;
379 MSDOS_I(inode)->i_attrs = 0;
380 inode->i_mtime = inode->i_atime = inode->i_ctime = 0;
381 return;
382 }
383 if (!(bh = bread(inode->i_dev,inode->i_ino >> MSDOS_DPB_BITS,
384 SECTOR_SIZE))) {
385 printk("dev = 0x%04X, ino = %ld\n",inode->i_dev,inode->i_ino);
386 panic("msdos_read_inode: unable to read i-node block");
387 }
388 raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
389 [inode->i_ino & (MSDOS_DPB-1)];
390 if ((raw_entry->attr & ATTR_DIR) && !IS_FREE(raw_entry->name)) {
391 inode->i_mode = MSDOS_MKMODE(raw_entry->attr,S_IRWXUGO &
392 ~MSDOS_SB(inode->i_sb)->fs_umask) | S_IFDIR;
393 inode->i_op = &msdos_dir_inode_operations;
394 MSDOS_I(inode)->i_start = CF_LE_W(raw_entry->start);
395 inode->i_nlink = msdos_subdirs(inode);
396
397 #ifdef DEBUG
398 if (!inode->i_nlink) {
399 printk("directory %d: i_nlink == 0\n",inode->i_ino);
400 inode->i_nlink = 1;
401 }
402 #endif
403 inode->i_size = 0;
404 if ((nr = CF_LE_W(raw_entry->start)) != 0)
405 while (nr != -1) {
406 inode->i_size += SECTOR_SIZE*MSDOS_SB(inode->
407 i_sb)->cluster_size;
408 if (!(nr = fat_access(inode->i_sb,nr,-1))) {
409 printk("Directory %ld: bad FAT\n",
410 inode->i_ino);
411 break;
412 }
413 }
414 }
415 else {
416 inode->i_mode = MSDOS_MKMODE(raw_entry->attr,(IS_NOEXEC(inode)
417 ? S_IRUGO|S_IWUGO : S_IRWXUGO) & ~MSDOS_SB(inode->i_sb)->fs_umask) |
418 S_IFREG;
419 inode->i_op = sb->s_blocksize == 1024
420 ? &msdos_file_inode_operations_1024
421 : &msdos_file_inode_operations;
422 MSDOS_I(inode)->i_start = CF_LE_W(raw_entry->start);
423 inode->i_nlink = 1;
424 inode->i_size = CF_LE_L(raw_entry->size);
425 }
426 MSDOS_I(inode)->i_binary = is_binary(MSDOS_SB(inode->i_sb)->conversion,
427 raw_entry->ext);
428 MSDOS_I(inode)->i_attrs = raw_entry->attr & ATTR_UNUSED;
429
430 inode->i_blksize = MSDOS_SB(inode->i_sb)->cluster_size*SECTOR_SIZE;
431 inode->i_blocks = (inode->i_size+inode->i_blksize-1)/
432 inode->i_blksize*MSDOS_SB(inode->i_sb)->cluster_size;
433 inode->i_mtime = inode->i_atime = inode->i_ctime =
434 date_dos2unix(CF_LE_W(raw_entry->time),CF_LE_W(raw_entry->date));
435 brelse(bh);
436 }
437
438
439 void msdos_write_inode(struct inode *inode)
440 {
441 struct super_block *sb = inode->i_sb;
442 struct buffer_head *bh;
443 struct msdos_dir_entry *raw_entry;
444
445 inode->i_dirt = 0;
446 if (inode->i_ino == MSDOS_ROOT_INO || !inode->i_nlink) return;
447 if (!(bh = bread(inode->i_dev,inode->i_ino >> MSDOS_DPB_BITS,
448 SECTOR_SIZE))) {
449 printk("dev = 0x%04X, ino = %ld\n",inode->i_dev,inode->i_ino);
450 panic("msdos_write_inode: unable to read i-node block");
451 }
452 raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
453 [inode->i_ino & (MSDOS_DPB-1)];
454 if (S_ISDIR(inode->i_mode)) {
455 raw_entry->attr = ATTR_DIR;
456 raw_entry->size = 0;
457 }
458 else {
459 raw_entry->attr = ATTR_NONE;
460 raw_entry->size = CT_LE_L(inode->i_size);
461 }
462 raw_entry->attr |= MSDOS_MKATTR(inode->i_mode) |
463 MSDOS_I(inode)->i_attrs;
464 raw_entry->start = CT_LE_L(MSDOS_I(inode)->i_start);
465 date_unix2dos(inode->i_mtime,&raw_entry->time,&raw_entry->date);
466 raw_entry->time = CT_LE_W(raw_entry->time);
467 raw_entry->date = CT_LE_W(raw_entry->date);
468 mark_buffer_dirty(bh, 1);
469 brelse(bh);
470 }
471
472
473 int msdos_notify_change(struct inode * inode,struct iattr * attr)
474 {
475 int error;
476
477 error = inode_change_ok(inode, attr);
478 if (error)
479 return error;
480
481 if (((attr->ia_valid & ATTR_UID) &&
482 (attr->ia_uid != MSDOS_SB(inode->i_sb)->fs_uid)) ||
483 ((attr->ia_valid & ATTR_GID) &&
484 (attr->ia_gid != MSDOS_SB(inode->i_sb)->fs_gid)) ||
485 ((attr->ia_valid & ATTR_MODE) &&
486 (attr->ia_mode & ~MSDOS_VALID_MODE)))
487 error = -EPERM;
488
489 if (error)
490 return MSDOS_SB(inode->i_sb)->quiet ? 0 : error;
491
492 inode_setattr(inode, attr);
493
494 if (IS_NOEXEC(inode) && !S_ISDIR(inode->i_mode))
495 inode->i_mode &= S_IFMT | S_IRUGO | S_IWUGO;
496 else
497 inode->i_mode |= S_IXUGO;
498
499 inode->i_mode = ((inode->i_mode & S_IFMT) | ((((inode->i_mode & S_IRWXU
500 & ~MSDOS_SB(inode->i_sb)->fs_umask) | S_IRUSR) >> 6)*S_IXUGO)) &
501 ~MSDOS_SB(inode->i_sb)->fs_umask;
502 return 0;
503 }
504 #ifdef MODULE
505
506 char kernel_version[] = UTS_RELEASE;
507
508 static struct file_system_type msdos_fs_type = {
509 msdos_read_super, "msdos", 1, NULL
510 };
511
512 int init_module(void)
513 {
514 register_filesystem(&msdos_fs_type);
515 return 0;
516 }
517
518 void cleanup_module(void)
519 {
520 unregister_filesystem(&msdos_fs_type);
521 }
522
523 #endif
524