OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [fs/] [fat/] [inode.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 *  linux/fs/fat/inode.c
3
 *
4
 *  Written 1992,1993 by Werner Almesberger
5
 *  VFAT extensions by Gordon Chaffee, merged with msdos fs by Henrik Storner
6
 *  Rewritten for the constant inumbers support by Al Viro
7
 *
8
 *  Fixes:
9
 *
10
 *      Max Cohan: Fixed invalid FSINFO offset when info_sector is 0
11
 */
12
 
13
#include <linux/module.h>
14
#include <linux/msdos_fs.h>
15
#include <linux/nls.h>
16
#include <linux/kernel.h>
17
#include <linux/sched.h>
18
#include <linux/errno.h>
19
#include <linux/string.h>
20
#include <linux/bitops.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
#include <linux/fat_cvf.h>
27
#include <linux/slab.h>
28
#include <linux/smp_lock.h>
29
 
30
#include <asm/uaccess.h>
31
#include <asm/unaligned.h>
32
 
33
extern struct cvf_format default_cvf;
34
 
35
/* #define FAT_PARANOIA 1 */
36
#define DEBUG_LEVEL 0
37
#ifdef FAT_DEBUG
38
#  define PRINTK(x) printk x
39
#else
40
#  define PRINTK(x)
41
#endif
42
#if (DEBUG_LEVEL >= 1)
43
#  define PRINTK1(x) printk x
44
#else
45
#  define PRINTK1(x)
46
#endif
47
 
48
/*
49
 * New FAT inode stuff. We do the following:
50
 *      a) i_ino is constant and has nothing with on-disk location.
51
 *      b) FAT manages its own cache of directory entries.
52
 *      c) *This* cache is indexed by on-disk location.
53
 *      d) inode has an associated directory entry, all right, but
54
 *              it may be unhashed.
55
 *      e) currently entries are stored within struct inode. That should
56
 *              change.
57
 *      f) we deal with races in the following way:
58
 *              1. readdir() and lookup() do FAT-dir-cache lookup.
59
 *              2. rename() unhashes the F-d-c entry and rehashes it in
60
 *                      a new place.
61
 *              3. unlink() and rmdir() unhash F-d-c entry.
62
 *              4. fat_write_inode() checks whether the thing is unhashed.
63
 *                      If it is we silently return. If it isn't we do bread(),
64
 *                      check if the location is still valid and retry if it
65
 *                      isn't. Otherwise we do changes.
66
 *              5. Spinlock is used to protect hash/unhash/location check/lookup
67
 *              6. fat_clear_inode() unhashes the F-d-c entry.
68
 *              7. lookup() and readdir() do igrab() if they find a F-d-c entry
69
 *                      and consider negative result as cache miss.
70
 */
71
 
72
#define FAT_HASH_BITS   8
73
#define FAT_HASH_SIZE   (1UL << FAT_HASH_BITS)
74
#define FAT_HASH_MASK   (FAT_HASH_SIZE-1)
75
static struct list_head fat_inode_hashtable[FAT_HASH_SIZE];
76
spinlock_t fat_inode_lock = SPIN_LOCK_UNLOCKED;
77
 
78
void fat_hash_init(void)
79
{
80
        int i;
81
        for(i = 0; i < FAT_HASH_SIZE; i++) {
82
                INIT_LIST_HEAD(&fat_inode_hashtable[i]);
83
        }
84
}
85
 
86
static inline unsigned long fat_hash(struct super_block *sb, loff_t i_pos)
87
{
88
        unsigned long tmp = (unsigned long)i_pos | (unsigned long) sb;
89
        tmp = tmp + (tmp >> FAT_HASH_BITS) + (tmp >> FAT_HASH_BITS * 2);
90
        return tmp & FAT_HASH_MASK;
91
}
92
 
93
void fat_attach(struct inode *inode, loff_t i_pos)
94
{
95
        spin_lock(&fat_inode_lock);
96
        MSDOS_I(inode)->i_pos = i_pos;
97
        list_add(&MSDOS_I(inode)->i_fat_hash,
98
                fat_inode_hashtable + fat_hash(inode->i_sb, i_pos));
99
        spin_unlock(&fat_inode_lock);
100
}
101
 
102
void fat_detach(struct inode *inode)
103
{
104
        spin_lock(&fat_inode_lock);
105
        MSDOS_I(inode)->i_pos = 0;
106
        list_del(&MSDOS_I(inode)->i_fat_hash);
107
        INIT_LIST_HEAD(&MSDOS_I(inode)->i_fat_hash);
108
        spin_unlock(&fat_inode_lock);
109
}
110
 
111
struct inode *fat_iget(struct super_block *sb, loff_t i_pos)
112
{
113
        struct list_head *p = fat_inode_hashtable + fat_hash(sb, i_pos);
114
        struct list_head *walk;
115
        struct msdos_inode_info *i;
116
        struct inode *inode = NULL;
117
 
118
        spin_lock(&fat_inode_lock);
119
        list_for_each(walk, p) {
120
                i = list_entry(walk, struct msdos_inode_info, i_fat_hash);
121
                if (i->i_fat_inode->i_sb != sb)
122
                        continue;
123
                if (i->i_pos != i_pos)
124
                        continue;
125
                inode = igrab(i->i_fat_inode);
126
                if (inode)
127
                        break;
128
        }
129
        spin_unlock(&fat_inode_lock);
130
        return inode;
131
}
132
 
133
static void fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de);
134
 
135
struct inode *fat_build_inode(struct super_block *sb,
136
                        struct msdos_dir_entry *de, loff_t i_pos, int *res)
137
{
138
        struct inode *inode;
139
        *res = 0;
140
        inode = fat_iget(sb, i_pos);
141
        if (inode)
142
                goto out;
143
        inode = new_inode(sb);
144
        *res = -ENOMEM;
145
        if (!inode)
146
                goto out;
147
        *res = 0;
148
        inode->i_ino = iunique(sb, MSDOS_ROOT_INO);
149
        fat_fill_inode(inode, de);
150
        fat_attach(inode, i_pos);
151
        insert_inode_hash(inode);
152
out:
153
        return inode;
154
}
155
 
156
void fat_delete_inode(struct inode *inode)
157
{
158
        if (!is_bad_inode(inode)) {
159
                lock_kernel();
160
                inode->i_size = 0;
161
                fat_truncate(inode);
162
                unlock_kernel();
163
        }
164
        clear_inode(inode);
165
}
166
 
167
void fat_clear_inode(struct inode *inode)
168
{
169
        if (is_bad_inode(inode))
170
                return;
171
        lock_kernel();
172
        spin_lock(&fat_inode_lock);
173
        fat_cache_inval_inode(inode);
174
        list_del(&MSDOS_I(inode)->i_fat_hash);
175
        spin_unlock(&fat_inode_lock);
176
        unlock_kernel();
177
}
178
 
179
void fat_put_super(struct super_block *sb)
180
{
181
        if (MSDOS_SB(sb)->cvf_format->cvf_version) {
182
                dec_cvf_format_use_count_by_version(MSDOS_SB(sb)->cvf_format->cvf_version);
183
                MSDOS_SB(sb)->cvf_format->unmount_cvf(sb);
184
        }
185
        if (MSDOS_SB(sb)->fat_bits == 32) {
186
                fat_clusters_flush(sb);
187
        }
188
        fat_cache_inval_dev(sb->s_dev);
189
        set_blocksize (sb->s_dev,BLOCK_SIZE);
190
        if (MSDOS_SB(sb)->nls_disk) {
191
                unload_nls(MSDOS_SB(sb)->nls_disk);
192
                MSDOS_SB(sb)->nls_disk = NULL;
193
                MSDOS_SB(sb)->options.codepage = 0;
194
        }
195
        if (MSDOS_SB(sb)->nls_io) {
196
                unload_nls(MSDOS_SB(sb)->nls_io);
197
                MSDOS_SB(sb)->nls_io = NULL;
198
        }
199
        /*
200
         * Note: the iocharset option might have been specified
201
         * without enabling nls_io, so check for it here.
202
         */
203
        if (MSDOS_SB(sb)->options.iocharset) {
204
                kfree(MSDOS_SB(sb)->options.iocharset);
205
                MSDOS_SB(sb)->options.iocharset = NULL;
206
        }
207
}
208
 
209
 
210
static int parse_options(char *options,int *fat, int *debug,
211
                         struct fat_mount_options *opts,
212
                         char *cvf_format, char *cvf_options)
213
{
214
        char *this_char,*value,save,*savep;
215
        char *p;
216
        int ret = 1, len;
217
 
218
        opts->name_check = 'n';
219
        opts->conversion = 'b';
220
        opts->fs_uid = current->uid;
221
        opts->fs_gid = current->gid;
222
        opts->fs_umask = current->fs->umask;
223
        opts->quiet = opts->sys_immutable = opts->dotsOK = opts->showexec = 0;
224
        opts->codepage = 0;
225
        opts->nocase = 0;
226
        opts->shortname = 0;
227
        opts->utf8 = 0;
228
        opts->iocharset = NULL;
229
        *debug = *fat = 0;
230
 
231
        if (!options)
232
                goto out;
233
        save = 0;
234
        savep = NULL;
235
        for (this_char = strtok(options,","); this_char;
236
             this_char = strtok(NULL,",")) {
237
                if ((value = strchr(this_char,'=')) != NULL) {
238
                        save = *value;
239
                        savep = value;
240
                        *value++ = 0;
241
                }
242
                if (!strcmp(this_char,"check") && value) {
243
                        if (value[0] && !value[1] && strchr("rns",*value))
244
                                opts->name_check = *value;
245
                        else if (!strcmp(value,"relaxed"))
246
                                opts->name_check = 'r';
247
                        else if (!strcmp(value,"normal"))
248
                                opts->name_check = 'n';
249
                        else if (!strcmp(value,"strict"))
250
                                opts->name_check = 's';
251
                        else ret = 0;
252
                }
253
                else if (!strcmp(this_char,"conv") && value) {
254
                        if (value[0] && !value[1] && strchr("bta",*value))
255
                                opts->conversion = *value;
256
                        else if (!strcmp(value,"binary"))
257
                                opts->conversion = 'b';
258
                        else if (!strcmp(value,"text"))
259
                                opts->conversion = 't';
260
                        else if (!strcmp(value,"auto"))
261
                                opts->conversion = 'a';
262
                        else ret = 0;
263
                }
264
                else if (!strcmp(this_char,"dots")) {
265
                        opts->dotsOK = 1;
266
                }
267
                else if (!strcmp(this_char,"nocase")) {
268
                        opts->nocase = 1;
269
                }
270
                else if (!strcmp(this_char,"nodots")) {
271
                        opts->dotsOK = 0;
272
                }
273
                else if (!strcmp(this_char,"showexec")) {
274
                        opts->showexec = 1;
275
                }
276
                else if (!strcmp(this_char,"dotsOK") && value) {
277
                        if (!strcmp(value,"yes")) opts->dotsOK = 1;
278
                        else if (!strcmp(value,"no")) opts->dotsOK = 0;
279
                        else ret = 0;
280
                }
281
                else if (!strcmp(this_char,"uid")) {
282
                        if (!value || !*value) ret = 0;
283
                        else {
284
                                opts->fs_uid = simple_strtoul(value,&value,0);
285
                                if (*value) ret = 0;
286
                        }
287
                }
288
                else if (!strcmp(this_char,"gid")) {
289
                        if (!value || !*value) ret= 0;
290
                        else {
291
                                opts->fs_gid = simple_strtoul(value,&value,0);
292
                                if (*value) ret = 0;
293
                        }
294
                }
295
                else if (!strcmp(this_char,"umask")) {
296
                        if (!value || !*value) ret = 0;
297
                        else {
298
                                opts->fs_umask = simple_strtoul(value,&value,8);
299
                                if (*value) ret = 0;
300
                        }
301
                }
302
                else if (!strcmp(this_char,"debug")) {
303
                        if (value) ret = 0;
304
                        else *debug = 1;
305
                }
306
                else if (!strcmp(this_char,"fat")) {
307
                        if (!value || !*value) ret = 0;
308
                        else {
309
                                *fat = simple_strtoul(value,&value,0);
310
                                if (*value || (*fat != 12 && *fat != 16 &&
311
                                               *fat != 32))
312
                                        ret = 0;
313
                        }
314
                }
315
                else if (!strcmp(this_char,"quiet")) {
316
                        if (value) ret = 0;
317
                        else opts->quiet = 1;
318
                }
319
                else if (!strcmp(this_char,"blocksize")) {
320
                        printk("FAT: blocksize option is obsolete, "
321
                               "not supported now\n");
322
                }
323
                else if (!strcmp(this_char,"sys_immutable")) {
324
                        if (value) ret = 0;
325
                        else opts->sys_immutable = 1;
326
                }
327
                else if (!strcmp(this_char,"codepage") && value) {
328
                        opts->codepage = simple_strtoul(value,&value,0);
329
                        if (*value) ret = 0;
330
                        else printk ("MSDOS FS: Using codepage %d\n",
331
                                        opts->codepage);
332
                }
333
                else if (!strcmp(this_char,"iocharset") && value) {
334
                        p = value;
335
                        while (*value && *value != ',')
336
                                value++;
337
                        len = value - p;
338
                        if (len) {
339
                                char *buffer;
340
 
341
                                if (opts->iocharset != NULL) {
342
                                        kfree(opts->iocharset);
343
                                        opts->iocharset = NULL;
344
                                }
345
                                buffer = kmalloc(len + 1, GFP_KERNEL);
346
                                if (buffer != NULL) {
347
                                        opts->iocharset = buffer;
348
                                        memcpy(buffer, p, len);
349
                                        buffer[len] = 0;
350
                                        printk("MSDOS FS: IO charset %s\n", buffer);
351
                                } else
352
                                        ret = 0;
353
                        }
354
                }
355
                else if (!strcmp(this_char,"cvf_format")) {
356
                        if (!value)
357
                                return 0;
358
                        strncpy(cvf_format,value,20);
359
                }
360
                else if (!strcmp(this_char,"cvf_options")) {
361
                        if (!value)
362
                                return 0;
363
                        strncpy(cvf_options,value,100);
364
                }
365
 
366
                if (this_char != options) *(this_char-1) = ',';
367
                if (value) *savep = save;
368
                if (ret == 0)
369
                        break;
370
        }
371
out:
372
        return ret;
373
}
374
 
375
static void fat_read_root(struct inode *inode)
376
{
377
        struct super_block *sb = inode->i_sb;
378
        struct msdos_sb_info *sbi = MSDOS_SB(sb);
379
        int nr;
380
 
381
        INIT_LIST_HEAD(&MSDOS_I(inode)->i_fat_hash);
382
        MSDOS_I(inode)->i_pos = 0;
383
        MSDOS_I(inode)->i_fat_inode = inode;
384
        inode->i_uid = sbi->options.fs_uid;
385
        inode->i_gid = sbi->options.fs_gid;
386
        inode->i_version = ++event;
387
        inode->i_generation = 0;
388
        inode->i_mode = (S_IRWXUGO & ~sbi->options.fs_umask) | S_IFDIR;
389
        inode->i_op = sbi->dir_ops;
390
        inode->i_fop = &fat_dir_operations;
391
        if (sbi->fat_bits == 32) {
392
                MSDOS_I(inode)->i_start = sbi->root_cluster;
393
                if ((nr = MSDOS_I(inode)->i_start) != 0) {
394
                        while (nr != -1) {
395
                                inode->i_size += 1 << sbi->cluster_bits;
396
                                if (!(nr = fat_access(sb, nr, -1))) {
397
                                        printk("Directory %ld: bad FAT\n",
398
                                               inode->i_ino);
399
                                        break;
400
                                }
401
                        }
402
                }
403
        } else {
404
                MSDOS_I(inode)->i_start = 0;
405
                inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry);
406
        }
407
        inode->i_blksize = 1 << sbi->cluster_bits;
408
        inode->i_blocks = ((inode->i_size + inode->i_blksize - 1)
409
                           & ~(inode->i_blksize - 1)) >> 9;
410
        MSDOS_I(inode)->i_logstart = 0;
411
        MSDOS_I(inode)->mmu_private = inode->i_size;
412
 
413
        MSDOS_I(inode)->i_attrs = 0;
414
        inode->i_mtime = inode->i_atime = inode->i_ctime = 0;
415
        MSDOS_I(inode)->i_ctime_ms = 0;
416
        inode->i_nlink = fat_subdirs(inode)+2;
417
}
418
 
419
/*
420
 * a FAT file handle with fhtype 3 is
421
 *  0/  i_ino - for fast, reliable lookup if still in the cache
422
 *  1/  i_generation - to see if i_ino is still valid
423
 *          bit 0 == 0 iff directory
424
 *  2/  i_pos(8-39) - if ino has changed, but still in cache
425
 *  3/  i_pos(4-7)|i_logstart - to semi-verify inode found at i_pos
426
 *  4/  i_pos(0-3)|parent->i_logstart - maybe used to hunt for the file on disc
427
 *
428
 * Hack for NFSv2: Maximum FAT entry number is 28bits and maximum
429
 * i_pos is 40bits (blocknr(32) + dir offset(8)), so two 4bits
430
 * of i_logstart is used to store the directory entry offset.
431
 */
432
struct dentry *fat_fh_to_dentry(struct super_block *sb, __u32 *fh,
433
                                int len, int fhtype, int parent)
434
{
435
        struct inode *inode = NULL;
436
        struct list_head *lp;
437
        struct dentry *result;
438
 
439
        if (fhtype != 3)
440
                return ERR_PTR(-ESTALE);
441
        if (len < 5)
442
                return ERR_PTR(-ESTALE);
443
        /* We cannot find the parent,
444
           It better just *be* there */
445
        if (parent)
446
                return ERR_PTR(-ESTALE);
447
 
448
        inode = iget(sb, fh[0]);
449
        if (!inode || is_bad_inode(inode) || inode->i_generation != fh[1]) {
450
                if (inode)
451
                        iput(inode);
452
                inode = NULL;
453
        }
454
        if (!inode) {
455
                loff_t i_pos;
456
                int i_logstart = fh[3] & 0x0fffffff;
457
 
458
                i_pos = (loff_t)fh[2] << 8;
459
                i_pos |= ((fh[3] >> 24) & 0xf0) | (fh[4] >> 28);
460
 
461
                /* try 2 - see if i_pos is in F-d-c
462
                 * require i_logstart to be the same
463
                 * Will fail if you truncate and then re-write
464
                 */
465
 
466
                inode = fat_iget(sb, i_pos);
467
                if (inode && MSDOS_I(inode)->i_logstart != i_logstart) {
468
                        iput(inode);
469
                        inode = NULL;
470
                }
471
        }
472
        if (!inode) {
473
                /* For now, do nothing
474
                 * What we could do is:
475
                 * follow the file starting at fh[4], and record
476
                 * the ".." entry, and the name of the fh[2] entry.
477
                 * The follow the ".." file finding the next step up.
478
                 * This way we build a path to the root of
479
                 * the tree. If this works, we lookup the path and so
480
                 * get this inode into the cache.
481
                 * Finally try the fat_iget lookup again
482
                 * If that fails, then weare totally out of luck
483
                 * But all that is for another day
484
                 */
485
        }
486
        if (!inode)
487
                return ERR_PTR(-ESTALE);
488
 
489
 
490
        /* now to find a dentry.
491
         * If possible, get a well-connected one
492
         *
493
         * Given the way that we found the inode, it *MUST* be
494
         * well-connected, but it is easiest to just copy the
495
         * code.
496
         */
497
        spin_lock(&dcache_lock);
498
        for (lp = inode->i_dentry.next; lp != &inode->i_dentry ; lp=lp->next) {
499
                result = list_entry(lp,struct dentry, d_alias);
500
                if (! (result->d_flags & DCACHE_NFSD_DISCONNECTED)) {
501
                        dget_locked(result);
502
                        result->d_vfs_flags |= DCACHE_REFERENCED;
503
                        spin_unlock(&dcache_lock);
504
                        iput(inode);
505
                        return result;
506
                }
507
        }
508
        spin_unlock(&dcache_lock);
509
        result = d_alloc_root(inode);
510
        if (result == NULL) {
511
                iput(inode);
512
                return ERR_PTR(-ENOMEM);
513
        }
514
        result->d_op = sb->s_root->d_op;
515
        result->d_flags |= DCACHE_NFSD_DISCONNECTED;
516
        return result;
517
}
518
 
519
int fat_dentry_to_fh(struct dentry *de, __u32 *fh, int *lenp, int needparent)
520
{
521
        int len = *lenp;
522
        struct inode *inode =  de->d_inode;
523
        u32 ipos_h, ipos_m, ipos_l;
524
 
525
        if (len < 5)
526
                return 255; /* no room */
527
 
528
        ipos_h = MSDOS_I(inode)->i_pos >> 8;
529
        ipos_m = (MSDOS_I(inode)->i_pos & 0xf0) << 24;
530
        ipos_l = (MSDOS_I(inode)->i_pos & 0x0f) << 28;
531
        *lenp = 5;
532
        fh[0] = inode->i_ino;
533
        fh[1] = inode->i_generation;
534
        fh[2] = ipos_h;
535
        fh[3] = ipos_m | MSDOS_I(inode)->i_logstart;
536
        fh[4] = ipos_l | MSDOS_I(de->d_parent->d_inode)->i_logstart;
537
        return 3;
538
}
539
 
540
static struct super_operations fat_sops = {
541
        write_inode:    fat_write_inode,
542
        delete_inode:   fat_delete_inode,
543
        put_super:      fat_put_super,
544
        statfs:         fat_statfs,
545
        clear_inode:    fat_clear_inode,
546
 
547
        read_inode:     make_bad_inode,
548
        fh_to_dentry:   fat_fh_to_dentry,
549
        dentry_to_fh:   fat_dentry_to_fh,
550
};
551
 
552
/*
553
 * Read the super block of an MS-DOS FS.
554
 *
555
 * Note that this may be called from vfat_read_super
556
 * with some fields already initialized.
557
 */
558
struct super_block *
559
fat_read_super(struct super_block *sb, void *data, int silent,
560
                struct inode_operations *fs_dir_inode_ops)
561
{
562
        struct inode *root_inode;
563
        struct buffer_head *bh;
564
        struct fat_boot_sector *b;
565
        struct msdos_sb_info *sbi = MSDOS_SB(sb);
566
        char *p;
567
        int logical_sector_size, hard_blksize, fat_clusters = 0;
568
        unsigned int total_sectors, rootdir_sectors;
569
        int fat32, debug, error, fat, cp;
570
        struct fat_mount_options opts;
571
        char buf[50];
572
        int i;
573
        char cvf_format[21];
574
        char cvf_options[101];
575
 
576
        cvf_format[0] = '\0';
577
        cvf_options[0] = '\0';
578
        sbi->cvf_format = NULL;
579
        sbi->private_data = NULL;
580
 
581
        sbi->dir_ops = fs_dir_inode_ops;
582
 
583
        sb->s_maxbytes = MAX_NON_LFS;
584
        sb->s_op = &fat_sops;
585
 
586
        hard_blksize = get_hardsect_size(sb->s_dev);
587
        if (!hard_blksize)
588
                hard_blksize = 512;
589
 
590
        opts.isvfat = sbi->options.isvfat;
591
        if (!parse_options((char *) data, &fat, &debug, &opts,
592
                           cvf_format, cvf_options))
593
                goto out_fail;
594
        /* N.B. we should parse directly into the sb structure */
595
        memcpy(&(sbi->options), &opts, sizeof(struct fat_mount_options));
596
 
597
        fat_cache_init();
598
 
599
        sb->s_blocksize = hard_blksize;
600
        set_blocksize(sb->s_dev, hard_blksize);
601
        bh = sb_bread(sb, 0);
602
        if (bh == NULL) {
603
                printk("FAT: unable to read boot sector\n");
604
                goto out_fail;
605
        }
606
 
607
/*
608
 * The DOS3 partition size limit is *not* 32M as many people think.
609
 * Instead, it is 64K sectors (with the usual sector size being
610
 * 512 bytes, leading to a 32M limit).
611
 *
612
 * DOS 3 partition managers got around this problem by faking a
613
 * larger sector size, ie treating multiple physical sectors as
614
 * a single logical sector.
615
 *
616
 * We can accommodate this scheme by adjusting our cluster size,
617
 * fat_start, and data_start by an appropriate value.
618
 *
619
 * (by Drew Eckhardt)
620
 */
621
 
622
 
623
        b = (struct fat_boot_sector *) bh->b_data;
624
        logical_sector_size =
625
                CF_LE_W(get_unaligned((unsigned short *) &b->sector_size));
626
        if (!logical_sector_size
627
            || (logical_sector_size & (logical_sector_size - 1))) {
628
                printk("FAT: bogus logical sector size %d\n",
629
                       logical_sector_size);
630
                brelse(bh);
631
                goto out_invalid;
632
        }
633
 
634
        sbi->cluster_size = b->cluster_size;
635
        if (!sbi->cluster_size
636
            || (sbi->cluster_size & (sbi->cluster_size - 1))) {
637
                printk("FAT: bogus cluster size %d\n", sbi->cluster_size);
638
                brelse(bh);
639
                goto out_invalid;
640
        }
641
 
642
        if (logical_sector_size < hard_blksize) {
643
                printk("FAT: logical sector size too small for device"
644
                       " (logical sector size = %d)\n", logical_sector_size);
645
                brelse(bh);
646
                goto out_invalid;
647
        }
648
 
649
        sbi->cluster_bits = ffs(logical_sector_size * sbi->cluster_size) - 1;
650
        sbi->fats = b->fats;
651
        sbi->fat_start = CF_LE_W(b->reserved);
652
        sbi->prev_free = 0;
653
        if (!b->fat_length && b->fat32_length) {
654
                struct fat_boot_fsinfo *fsinfo;
655
                struct buffer_head *fsinfo_bh;
656
                int fsinfo_block, fsinfo_offset;
657
 
658
                /* Must be FAT32 */
659
                fat32 = 1;
660
                sbi->fat_length = CF_LE_L(b->fat32_length);
661
                sbi->root_cluster = CF_LE_L(b->root_cluster);
662
 
663
                sbi->fsinfo_sector = CF_LE_W(b->info_sector);
664
                /* MC - if info_sector is 0, don't multiply by 0 */
665
                if (sbi->fsinfo_sector == 0)
666
                        sbi->fsinfo_sector = 1;
667
 
668
                fsinfo_block =
669
                        (sbi->fsinfo_sector * logical_sector_size) / hard_blksize;
670
                fsinfo_offset =
671
                        (sbi->fsinfo_sector * logical_sector_size) % hard_blksize;
672
                fsinfo_bh = bh;
673
                if (fsinfo_block != 0) {
674
                        fsinfo_bh = sb_bread(sb, fsinfo_block);
675
                        if (fsinfo_bh == NULL) {
676
                                printk("FAT: bread failed, FSINFO block"
677
                                       " (blocknr = %d)\n", fsinfo_block);
678
                                brelse(bh);
679
                                goto out_invalid;
680
                        }
681
                }
682
                fsinfo = (struct fat_boot_fsinfo *)&fsinfo_bh->b_data[fsinfo_offset];
683
                if (!IS_FSINFO(fsinfo)) {
684
                        printk("FAT: Did not find valid FSINFO signature.\n"
685
                               "Found signature1 0x%x signature2 0x%x sector=%ld.\n",
686
                               CF_LE_L(fsinfo->signature1),
687
                               CF_LE_L(fsinfo->signature2),
688
                               sbi->fsinfo_sector);
689
                } else {
690
                        sbi->free_clusters = CF_LE_L(fsinfo->free_clusters);
691
                        sbi->prev_free = CF_LE_L(fsinfo->next_cluster);
692
                }
693
 
694
                if (fsinfo_block != 0)
695
                        brelse(fsinfo_bh);
696
        } else {
697
                fat32 = 0;
698
                sbi->fat_length = CF_LE_W(b->fat_length);
699
                sbi->root_cluster = 0;
700
                sbi->free_clusters = -1; /* Don't know yet */
701
        }
702
 
703
        sbi->dir_per_block = logical_sector_size / sizeof(struct msdos_dir_entry);
704
        sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1;
705
 
706
        sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length;
707
        sbi->dir_entries =
708
                CF_LE_W(get_unaligned((unsigned short *)&b->dir_entries));
709
        rootdir_sectors = sbi->dir_entries
710
                * sizeof(struct msdos_dir_entry) / logical_sector_size;
711
        sbi->data_start = sbi->dir_start + rootdir_sectors;
712
        total_sectors = CF_LE_W(get_unaligned((unsigned short *)&b->sectors));
713
        if (total_sectors == 0)
714
                total_sectors = CF_LE_L(b->total_sect);
715
        sbi->clusters = (total_sectors - sbi->data_start) / sbi->cluster_size;
716
 
717
        error = 0;
718
        if (!error) {
719
                sbi->fat_bits = fat32 ? 32 :
720
                        (fat ? fat :
721
                         (sbi->clusters > MSDOS_FAT12 ? 16 : 12));
722
                fat_clusters =
723
                        sbi->fat_length * logical_sector_size * 8 / sbi->fat_bits;
724
                error = !sbi->fats || (sbi->dir_entries & (sbi->dir_per_block - 1))
725
                        || sbi->clusters + 2 > fat_clusters + MSDOS_MAX_EXTRA
726
                        || logical_sector_size < 512
727
                        || PAGE_CACHE_SIZE < logical_sector_size
728
                        || !b->secs_track || !b->heads;
729
        }
730
        brelse(bh);
731
 
732
        if (error)
733
                goto out_invalid;
734
 
735
        sb->s_blocksize = logical_sector_size;
736
        sb->s_blocksize_bits = ffs(logical_sector_size) - 1;
737
        set_blocksize(sb->s_dev, sb->s_blocksize);
738
        sbi->cvf_format = &default_cvf;
739
        if (!strcmp(cvf_format, "none"))
740
                i = -1;
741
        else
742
                i = detect_cvf(sb,cvf_format);
743
        if (i >= 0)
744
                error = cvf_formats[i]->mount_cvf(sb, cvf_options);
745
        if (error || debug) {
746
                /* The MSDOS_CAN_BMAP is obsolete, but left just to remember */
747
                printk("[MS-DOS FS Rel. 12,FAT %d,check=%c,conv=%c,"
748
                       "uid=%d,gid=%d,umask=%03o%s]\n",
749
                       sbi->fat_bits,opts.name_check,
750
                       opts.conversion,opts.fs_uid,opts.fs_gid,opts.fs_umask,
751
                       MSDOS_CAN_BMAP(sbi) ? ",bmap" : "");
752
                printk("[me=0x%x,cs=%d,#f=%d,fs=%d,fl=%ld,ds=%ld,de=%d,data=%ld,"
753
                       "se=%u,ts=%u,ls=%d,rc=%ld,fc=%u]\n",
754
                       b->media, sbi->cluster_size, sbi->fats,
755
                       sbi->fat_start, sbi->fat_length, sbi->dir_start,
756
                       sbi->dir_entries, sbi->data_start,
757
                       CF_LE_W(get_unaligned((unsigned short *)&b->sectors)),
758
                       CF_LE_L(b->total_sect), logical_sector_size,
759
                       sbi->root_cluster, sbi->free_clusters);
760
                printk ("hard sector size = %d\n", hard_blksize);
761
        }
762
        if (i < 0)
763
                if (sbi->clusters + 2 > fat_clusters)
764
                        sbi->clusters = fat_clusters - 2;
765
        if (error)
766
                goto out_invalid;
767
 
768
        sb->s_magic = MSDOS_SUPER_MAGIC;
769
        /* set up enough so that it can read an inode */
770
        init_MUTEX(&sbi->fat_lock);
771
 
772
        cp = opts.codepage ? opts.codepage : 437;
773
        sprintf(buf, "cp%d", cp);
774
        sbi->nls_disk = load_nls(buf);
775
        if (! sbi->nls_disk) {
776
                /* Fail only if explicit charset specified */
777
                if (opts.codepage != 0)
778
                        goto out_fail;
779
                sbi->options.codepage = 0; /* already 0?? */
780
                sbi->nls_disk = load_nls_default();
781
        }
782
 
783
        sbi->nls_io = NULL;
784
        if (sbi->options.isvfat && !opts.utf8) {
785
                p = opts.iocharset ? opts.iocharset : CONFIG_NLS_DEFAULT;
786
                sbi->nls_io = load_nls(p);
787
                if (! sbi->nls_io)
788
                        /* Fail only if explicit charset specified */
789
                        if (opts.iocharset)
790
                                goto out_unload_nls;
791
        }
792
        if (! sbi->nls_io)
793
                sbi->nls_io = load_nls_default();
794
 
795
        root_inode = new_inode(sb);
796
        if (!root_inode)
797
                goto out_unload_nls;
798
        root_inode->i_ino = MSDOS_ROOT_INO;
799
        fat_read_root(root_inode);
800
        insert_inode_hash(root_inode);
801
        sb->s_root = d_alloc_root(root_inode);
802
        if (!sb->s_root)
803
                goto out_no_root;
804
        if(i >= 0) {
805
                sbi->cvf_format = cvf_formats[i];
806
                ++cvf_format_use_count[i];
807
        }
808
        return sb;
809
 
810
out_no_root:
811
        printk("FAT: get root inode failed\n");
812
        iput(root_inode);
813
        unload_nls(sbi->nls_io);
814
out_unload_nls:
815
        unload_nls(sbi->nls_disk);
816
        goto out_fail;
817
out_invalid:
818
        if (!silent) {
819
                printk("VFS: Can't find a valid FAT filesystem on dev %s.\n",
820
                        kdevname(sb->s_dev));
821
        }
822
out_fail:
823
        if (opts.iocharset) {
824
                printk("FAT: freeing iocharset=%s\n", opts.iocharset);
825
                kfree(opts.iocharset);
826
        }
827
        if(sbi->private_data)
828
                kfree(sbi->private_data);
829
        sbi->private_data = NULL;
830
 
831
        return NULL;
832
}
833
 
834
int fat_statfs(struct super_block *sb,struct statfs *buf)
835
{
836
        int free,nr;
837
 
838
        if (MSDOS_SB(sb)->cvf_format &&
839
            MSDOS_SB(sb)->cvf_format->cvf_statfs)
840
                return MSDOS_SB(sb)->cvf_format->cvf_statfs(sb,buf,
841
                                                sizeof(struct statfs));
842
 
843
        lock_fat(sb);
844
        if (MSDOS_SB(sb)->free_clusters != -1)
845
                free = MSDOS_SB(sb)->free_clusters;
846
        else {
847
                free = 0;
848
                for (nr = 2; nr < MSDOS_SB(sb)->clusters+2; nr++)
849
                        if (!fat_access(sb,nr,-1)) free++;
850
                MSDOS_SB(sb)->free_clusters = free;
851
        }
852
        unlock_fat(sb);
853
        buf->f_type = sb->s_magic;
854
        buf->f_bsize = 1 << MSDOS_SB(sb)->cluster_bits;
855
        buf->f_blocks = MSDOS_SB(sb)->clusters;
856
        buf->f_bfree = free;
857
        buf->f_bavail = free;
858
        buf->f_namelen = MSDOS_SB(sb)->options.isvfat ? 260 : 12;
859
        return 0;
860
}
861
 
862
static int is_exec(char *extension)
863
{
864
        char *exe_extensions = "EXECOMBAT", *walk;
865
 
866
        for (walk = exe_extensions; *walk; walk += 3)
867
                if (!strncmp(extension, walk, 3))
868
                        return 1;
869
        return 0;
870
}
871
 
872
static int fat_writepage(struct page *page)
873
{
874
        return block_write_full_page(page,fat_get_block);
875
}
876
static int fat_readpage(struct file *file, struct page *page)
877
{
878
        return block_read_full_page(page,fat_get_block);
879
}
880
static int fat_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
881
{
882
        return cont_prepare_write(page,from,to,fat_get_block,
883
                &MSDOS_I(page->mapping->host)->mmu_private);
884
}
885
static int _fat_bmap(struct address_space *mapping, long block)
886
{
887
        return generic_block_bmap(mapping,block,fat_get_block);
888
}
889
static struct address_space_operations fat_aops = {
890
        readpage: fat_readpage,
891
        writepage: fat_writepage,
892
        sync_page: block_sync_page,
893
        prepare_write: fat_prepare_write,
894
        commit_write: generic_commit_write,
895
        bmap: _fat_bmap
896
};
897
 
898
/* doesn't deal with root inode */
899
static void fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
900
{
901
        struct super_block *sb = inode->i_sb;
902
        struct msdos_sb_info *sbi = MSDOS_SB(sb);
903
        int nr;
904
 
905
        INIT_LIST_HEAD(&MSDOS_I(inode)->i_fat_hash);
906
        MSDOS_I(inode)->i_pos = 0;
907
        MSDOS_I(inode)->i_fat_inode = inode;
908
        inode->i_uid = sbi->options.fs_uid;
909
        inode->i_gid = sbi->options.fs_gid;
910
        inode->i_version = ++event;
911
        inode->i_generation = CURRENT_TIME;
912
 
913
        if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
914
                inode->i_generation &= ~1;
915
                inode->i_mode = MSDOS_MKMODE(de->attr,S_IRWXUGO &
916
                    ~sbi->options.fs_umask) | S_IFDIR;
917
                inode->i_op = sbi->dir_ops;
918
                inode->i_fop = &fat_dir_operations;
919
 
920
                MSDOS_I(inode)->i_start = CF_LE_W(de->start);
921
                if (sbi->fat_bits == 32)
922
                        MSDOS_I(inode)->i_start |= (CF_LE_W(de->starthi) << 16);
923
 
924
                MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
925
                inode->i_nlink = fat_subdirs(inode);
926
                    /* includes .., compensating for "self" */
927
#ifdef DEBUG
928
                if (!inode->i_nlink) {
929
                        printk("directory %d: i_nlink == 0\n",inode->i_ino);
930
                        inode->i_nlink = 1;
931
                }
932
#endif
933
                if ((nr = MSDOS_I(inode)->i_start) != 0)
934
                        while (nr != -1) {
935
                                inode->i_size += 1 << sbi->cluster_bits;
936
                                if (!(nr = fat_access(sb, nr, -1))) {
937
                                        printk("Directory %ld: bad FAT\n",
938
                                            inode->i_ino);
939
                                        break;
940
                                }
941
                        }
942
                MSDOS_I(inode)->mmu_private = inode->i_size;
943
        } else { /* not a directory */
944
                inode->i_generation |= 1;
945
                inode->i_mode = MSDOS_MKMODE(de->attr,
946
                    ((sbi->options.showexec &&
947
                       !is_exec(de->ext))
948
                        ? S_IRUGO|S_IWUGO : S_IRWXUGO)
949
                    & ~sbi->options.fs_umask) | S_IFREG;
950
                MSDOS_I(inode)->i_start = CF_LE_W(de->start);
951
                if (sbi->fat_bits == 32)
952
                        MSDOS_I(inode)->i_start |= (CF_LE_W(de->starthi) << 16);
953
 
954
                MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
955
                inode->i_size = CF_LE_L(de->size);
956
                inode->i_op = &fat_file_inode_operations;
957
                inode->i_fop = &fat_file_operations;
958
                inode->i_mapping->a_ops = &fat_aops;
959
                MSDOS_I(inode)->mmu_private = inode->i_size;
960
        }
961
        if(de->attr & ATTR_SYS)
962
                if (sbi->options.sys_immutable)
963
                        inode->i_flags |= S_IMMUTABLE;
964
        MSDOS_I(inode)->i_attrs = de->attr & ATTR_UNUSED;
965
        /* this is as close to the truth as we can get ... */
966
        inode->i_blksize = 1 << sbi->cluster_bits;
967
        inode->i_blocks = ((inode->i_size + inode->i_blksize - 1)
968
                           & ~(inode->i_blksize - 1)) >> 9;
969
        inode->i_mtime = inode->i_atime =
970
                date_dos2unix(CF_LE_W(de->time),CF_LE_W(de->date));
971
        inode->i_ctime =
972
                MSDOS_SB(sb)->options.isvfat
973
                ? date_dos2unix(CF_LE_W(de->ctime),CF_LE_W(de->cdate))
974
                : inode->i_mtime;
975
        MSDOS_I(inode)->i_ctime_ms = de->ctime_ms;
976
}
977
 
978
void fat_write_inode(struct inode *inode, int wait)
979
{
980
        struct super_block *sb = inode->i_sb;
981
        struct buffer_head *bh;
982
        struct msdos_dir_entry *raw_entry;
983
        loff_t i_pos;
984
 
985
retry:
986
        i_pos = MSDOS_I(inode)->i_pos;
987
        if (inode->i_ino == MSDOS_ROOT_INO || !i_pos) {
988
                return;
989
        }
990
        lock_kernel();
991
        if (!(bh = fat_bread(sb, i_pos >> MSDOS_SB(sb)->dir_per_block_bits))) {
992
                printk("dev = %s, i_pos = %llu\n", kdevname(inode->i_dev), i_pos);
993
                fat_fs_panic(sb, "msdos_write_inode: unable to read i-node block");
994
                unlock_kernel();
995
                return;
996
        }
997
        spin_lock(&fat_inode_lock);
998
        if (i_pos != MSDOS_I(inode)->i_pos) {
999
                spin_unlock(&fat_inode_lock);
1000
                fat_brelse(sb, bh);
1001
                unlock_kernel();
1002
                goto retry;
1003
        }
1004
 
1005
        raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
1006
            [i_pos & (MSDOS_SB(sb)->dir_per_block - 1)];
1007
        if (S_ISDIR(inode->i_mode)) {
1008
                raw_entry->attr = ATTR_DIR;
1009
                raw_entry->size = 0;
1010
        }
1011
        else {
1012
                raw_entry->attr = ATTR_NONE;
1013
                raw_entry->size = CT_LE_L(inode->i_size);
1014
        }
1015
        raw_entry->attr |= MSDOS_MKATTR(inode->i_mode) |
1016
            MSDOS_I(inode)->i_attrs;
1017
        raw_entry->start = CT_LE_W(MSDOS_I(inode)->i_logstart);
1018
        raw_entry->starthi = CT_LE_W(MSDOS_I(inode)->i_logstart >> 16);
1019
        fat_date_unix2dos(inode->i_mtime,&raw_entry->time,&raw_entry->date);
1020
        raw_entry->time = CT_LE_W(raw_entry->time);
1021
        raw_entry->date = CT_LE_W(raw_entry->date);
1022
        if (MSDOS_SB(sb)->options.isvfat) {
1023
                fat_date_unix2dos(inode->i_ctime,&raw_entry->ctime,&raw_entry->cdate);
1024
                raw_entry->ctime_ms = MSDOS_I(inode)->i_ctime_ms;
1025
                raw_entry->ctime = CT_LE_W(raw_entry->ctime);
1026
                raw_entry->cdate = CT_LE_W(raw_entry->cdate);
1027
        }
1028
        spin_unlock(&fat_inode_lock);
1029
        fat_mark_buffer_dirty(sb, bh);
1030
        fat_brelse(sb, bh);
1031
        unlock_kernel();
1032
}
1033
 
1034
 
1035
int fat_notify_change(struct dentry * dentry, struct iattr * attr)
1036
{
1037
        struct super_block *sb = dentry->d_sb;
1038
        struct inode *inode = dentry->d_inode;
1039
        int error;
1040
 
1041
        /* FAT cannot truncate to a longer file */
1042
        if (attr->ia_valid & ATTR_SIZE) {
1043
                if (attr->ia_size > inode->i_size)
1044
                        return -EPERM;
1045
        }
1046
 
1047
        error = inode_change_ok(inode, attr);
1048
        if (error)
1049
                return MSDOS_SB(sb)->options.quiet ? 0 : error;
1050
 
1051
        if (((attr->ia_valid & ATTR_UID) &&
1052
             (attr->ia_uid != MSDOS_SB(sb)->options.fs_uid)) ||
1053
            ((attr->ia_valid & ATTR_GID) &&
1054
             (attr->ia_gid != MSDOS_SB(sb)->options.fs_gid)) ||
1055
            ((attr->ia_valid & ATTR_MODE) &&
1056
             (attr->ia_mode & ~MSDOS_VALID_MODE)))
1057
                error = -EPERM;
1058
 
1059
        if (error)
1060
                return MSDOS_SB(sb)->options.quiet ? 0 : error;
1061
 
1062
        error = inode_setattr(inode, attr);
1063
        if (error)
1064
                return error;
1065
 
1066
        if (S_ISDIR(inode->i_mode))
1067
                inode->i_mode |= S_IXUGO;
1068
 
1069
        inode->i_mode = ((inode->i_mode & S_IFMT) | ((((inode->i_mode & S_IRWXU
1070
            & ~MSDOS_SB(sb)->options.fs_umask) | S_IRUSR) >> 6)*S_IXUGO)) &
1071
            ~MSDOS_SB(sb)->options.fs_umask;
1072
        return 0;
1073
}
1074
MODULE_LICENSE("GPL");

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.