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

Subversion Repositories or1k

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 *  linux/fs/ext2/dir.c
3
 *
4
 * Copyright (C) 1992, 1993, 1994, 1995
5
 * Remy Card (card@masi.ibp.fr)
6
 * Laboratoire MASI - Institut Blaise Pascal
7
 * Universite Pierre et Marie Curie (Paris VI)
8
 *
9
 *  from
10
 *
11
 *  linux/fs/minix/dir.c
12
 *
13
 *  Copyright (C) 1991, 1992  Linus Torvalds
14
 *
15
 *  ext2 directory handling functions
16
 *
17
 *  Big-endian to little-endian byte-swapping/bitmaps by
18
 *        David S. Miller (davem@caip.rutgers.edu), 1995
19
 *
20
 * All code that works with directory layout had been switched to pagecache
21
 * and moved here. AV
22
 */
23
 
24
#include <linux/fs.h>
25
#include <linux/ext2_fs.h>
26
#include <linux/pagemap.h>
27
 
28
typedef struct ext2_dir_entry_2 ext2_dirent;
29
 
30
/*
31
 * ext2 uses block-sized chunks. Arguably, sector-sized ones would be
32
 * more robust, but we have what we have
33
 */
34
static inline unsigned ext2_chunk_size(struct inode *inode)
35
{
36
        return inode->i_sb->s_blocksize;
37
}
38
 
39
static inline void ext2_put_page(struct page *page)
40
{
41
        kunmap(page);
42
        page_cache_release(page);
43
}
44
 
45
static inline unsigned long dir_pages(struct inode *inode)
46
{
47
        return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
48
}
49
 
50
static int ext2_commit_chunk(struct page *page, unsigned from, unsigned to)
51
{
52
        struct inode *dir = page->mapping->host;
53
        int err = 0;
54
        dir->i_version = ++event;
55
        page->mapping->a_ops->commit_write(NULL, page, from, to);
56
        if (IS_SYNC(dir)) {
57
                int err2;
58
                err = writeout_one_page(page);
59
                err2 = waitfor_one_page(page);
60
                if (err == 0)
61
                        err = err2;
62
        }
63
        return err;
64
}
65
 
66
static void ext2_check_page(struct page *page)
67
{
68
        struct inode *dir = page->mapping->host;
69
        struct super_block *sb = dir->i_sb;
70
        unsigned chunk_size = ext2_chunk_size(dir);
71
        char *kaddr = page_address(page);
72
        u32 max_inumber = le32_to_cpu(sb->u.ext2_sb.s_es->s_inodes_count);
73
        unsigned offs, rec_len;
74
        unsigned limit = PAGE_CACHE_SIZE;
75
        ext2_dirent *p;
76
        char *error;
77
 
78
        if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
79
                limit = dir->i_size & ~PAGE_CACHE_MASK;
80
                if (limit & (chunk_size - 1))
81
                        goto Ebadsize;
82
                for (offs = limit; offs<PAGE_CACHE_SIZE; offs += chunk_size) {
83
                        ext2_dirent *p = (ext2_dirent*)(kaddr + offs);
84
                        p->rec_len = cpu_to_le16(chunk_size);
85
                }
86
                if (!limit)
87
                        goto out;
88
        }
89
        for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
90
                p = (ext2_dirent *)(kaddr + offs);
91
                rec_len = le16_to_cpu(p->rec_len);
92
 
93
                if (rec_len < EXT2_DIR_REC_LEN(1))
94
                        goto Eshort;
95
                if (rec_len & 3)
96
                        goto Ealign;
97
                if (rec_len < EXT2_DIR_REC_LEN(p->name_len))
98
                        goto Enamelen;
99
                if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
100
                        goto Espan;
101
                if (le32_to_cpu(p->inode) > max_inumber)
102
                        goto Einumber;
103
        }
104
        if (offs != limit)
105
                goto Eend;
106
out:
107
        SetPageChecked(page);
108
        return;
109
 
110
        /* Too bad, we had an error */
111
 
112
Ebadsize:
113
        ext2_error(sb, "ext2_check_page",
114
                "size of directory #%lu is not a multiple of chunk size",
115
                dir->i_ino
116
        );
117
        goto fail;
118
Eshort:
119
        error = "rec_len is smaller than minimal";
120
        goto bad_entry;
121
Ealign:
122
        error = "unaligned directory entry";
123
        goto bad_entry;
124
Enamelen:
125
        error = "rec_len is too small for name_len";
126
        goto bad_entry;
127
Espan:
128
        error = "directory entry across blocks";
129
        goto bad_entry;
130
Einumber:
131
        error = "inode out of bounds";
132
bad_entry:
133
        ext2_error (sb, "ext2_check_page", "bad entry in directory #%lu: %s - "
134
                "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
135
                dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT)+offs,
136
                (unsigned long) le32_to_cpu(p->inode),
137
                rec_len, p->name_len);
138
        goto fail;
139
Eend:
140
        p = (ext2_dirent *)(kaddr + offs);
141
        ext2_error (sb, "ext2_check_page",
142
                "entry in directory #%lu spans the page boundary"
143
                "offset=%lu, inode=%lu",
144
                dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs,
145
                (unsigned long) le32_to_cpu(p->inode));
146
fail:
147
        SetPageChecked(page);
148
        SetPageError(page);
149
}
150
 
151
static struct page * ext2_get_page(struct inode *dir, unsigned long n)
152
{
153
        struct address_space *mapping = dir->i_mapping;
154
        struct page *page = read_cache_page(mapping, n,
155
                                (filler_t*)mapping->a_ops->readpage, NULL);
156
        if (!IS_ERR(page)) {
157
                wait_on_page(page);
158
                kmap(page);
159
                if (!Page_Uptodate(page))
160
                        goto fail;
161
                if (!PageChecked(page))
162
                        ext2_check_page(page);
163
                if (PageError(page))
164
                        goto fail;
165
        }
166
        return page;
167
 
168
fail:
169
        ext2_put_page(page);
170
        return ERR_PTR(-EIO);
171
}
172
 
173
/*
174
 * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure.
175
 *
176
 * len <= EXT2_NAME_LEN and de != NULL are guaranteed by caller.
177
 */
178
static inline int ext2_match (int len, const char * const name,
179
                                        struct ext2_dir_entry_2 * de)
180
{
181
        if (len != de->name_len)
182
                return 0;
183
        if (!de->inode)
184
                return 0;
185
        return !memcmp(name, de->name, len);
186
}
187
 
188
/*
189
 * p is at least 6 bytes before the end of page
190
 */
191
static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
192
{
193
        return (ext2_dirent *)((char*)p + le16_to_cpu(p->rec_len));
194
}
195
 
196
static inline unsigned
197
ext2_validate_entry(char *base, unsigned offset, unsigned mask)
198
{
199
        ext2_dirent *de = (ext2_dirent*)(base + offset);
200
        ext2_dirent *p = (ext2_dirent*)(base + (offset&mask));
201
        while ((char*)p < (char*)de)
202
                p = ext2_next_entry(p);
203
        return (char *)p - base;
204
}
205
 
206
static unsigned char ext2_filetype_table[EXT2_FT_MAX] = {
207
        [EXT2_FT_UNKNOWN]       DT_UNKNOWN,
208
        [EXT2_FT_REG_FILE]      DT_REG,
209
        [EXT2_FT_DIR]           DT_DIR,
210
        [EXT2_FT_CHRDEV]        DT_CHR,
211
        [EXT2_FT_BLKDEV]        DT_BLK,
212
        [EXT2_FT_FIFO]          DT_FIFO,
213
        [EXT2_FT_SOCK]          DT_SOCK,
214
        [EXT2_FT_SYMLINK]       DT_LNK,
215
};
216
 
217
#define S_SHIFT 12
218
static unsigned char ext2_type_by_mode[S_IFMT >> S_SHIFT] = {
219
        [S_IFREG >> S_SHIFT]    EXT2_FT_REG_FILE,
220
        [S_IFDIR >> S_SHIFT]    EXT2_FT_DIR,
221
        [S_IFCHR >> S_SHIFT]    EXT2_FT_CHRDEV,
222
        [S_IFBLK >> S_SHIFT]    EXT2_FT_BLKDEV,
223
        [S_IFIFO >> S_SHIFT]    EXT2_FT_FIFO,
224
        [S_IFSOCK >> S_SHIFT]   EXT2_FT_SOCK,
225
        [S_IFLNK >> S_SHIFT]    EXT2_FT_SYMLINK,
226
};
227
 
228
static inline void ext2_set_de_type(ext2_dirent *de, struct inode *inode)
229
{
230
        mode_t mode = inode->i_mode;
231
        if (EXT2_HAS_INCOMPAT_FEATURE(inode->i_sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
232
                de->file_type = ext2_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
233
        else
234
                de->file_type = 0;
235
}
236
 
237
static int
238
ext2_readdir (struct file * filp, void * dirent, filldir_t filldir)
239
{
240
        loff_t pos = filp->f_pos;
241
        struct inode *inode = filp->f_dentry->d_inode;
242
        struct super_block *sb = inode->i_sb;
243
        unsigned offset = pos & ~PAGE_CACHE_MASK;
244
        unsigned long n = pos >> PAGE_CACHE_SHIFT;
245
        unsigned long npages = dir_pages(inode);
246
        unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
247
        unsigned char *types = NULL;
248
        int need_revalidate = (filp->f_version != inode->i_version);
249
 
250
        if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
251
                goto done;
252
 
253
        if (EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
254
                types = ext2_filetype_table;
255
 
256
        for ( ; n < npages; n++, offset = 0) {
257
                char *kaddr, *limit;
258
                ext2_dirent *de;
259
                struct page *page = ext2_get_page(inode, n);
260
 
261
                if (IS_ERR(page))
262
                        continue;
263
                kaddr = page_address(page);
264
                if (need_revalidate) {
265
                        offset = ext2_validate_entry(kaddr, offset, chunk_mask);
266
                        need_revalidate = 0;
267
                }
268
                de = (ext2_dirent *)(kaddr+offset);
269
                limit = kaddr + PAGE_CACHE_SIZE - EXT2_DIR_REC_LEN(1);
270
                for ( ;(char*)de <= limit; de = ext2_next_entry(de))
271
                        if (de->inode) {
272
                                int over;
273
                                unsigned char d_type = DT_UNKNOWN;
274
 
275
                                if (types && de->file_type < EXT2_FT_MAX)
276
                                        d_type = types[de->file_type];
277
 
278
                                offset = (char *)de - kaddr;
279
                                over = filldir(dirent, de->name, de->name_len,
280
                                                (n<<PAGE_CACHE_SHIFT) | offset,
281
                                                le32_to_cpu(de->inode), d_type);
282
                                if (over) {
283
                                        ext2_put_page(page);
284
                                        goto done;
285
                                }
286
                        }
287
                ext2_put_page(page);
288
        }
289
 
290
done:
291
        filp->f_pos = (n << PAGE_CACHE_SHIFT) | offset;
292
        filp->f_version = inode->i_version;
293
        UPDATE_ATIME(inode);
294
        return 0;
295
}
296
 
297
/*
298
 *      ext2_find_entry()
299
 *
300
 * finds an entry in the specified directory with the wanted name. It
301
 * returns the page in which the entry was found, and the entry itself
302
 * (as a parameter - res_dir). Page is returned mapped and unlocked.
303
 * Entry is guaranteed to be valid.
304
 */
305
struct ext2_dir_entry_2 * ext2_find_entry (struct inode * dir,
306
                        struct dentry *dentry, struct page ** res_page)
307
{
308
        const char *name = dentry->d_name.name;
309
        int namelen = dentry->d_name.len;
310
        unsigned reclen = EXT2_DIR_REC_LEN(namelen);
311
        unsigned long start, n;
312
        unsigned long npages = dir_pages(dir);
313
        struct page *page = NULL;
314
        ext2_dirent * de;
315
 
316
        /* OFFSET_CACHE */
317
        *res_page = NULL;
318
 
319
        start = dir->u.ext2_i.i_dir_start_lookup;
320
        if (start >= npages)
321
                start = 0;
322
        n = start;
323
        do {
324
                char *kaddr;
325
                page = ext2_get_page(dir, n);
326
                if (!IS_ERR(page)) {
327
                        kaddr = page_address(page);
328
                        de = (ext2_dirent *) kaddr;
329
                        kaddr += PAGE_CACHE_SIZE - reclen;
330
                        while ((char *) de <= kaddr) {
331
                                if (ext2_match (namelen, name, de))
332
                                        goto found;
333
                                de = ext2_next_entry(de);
334
                        }
335
                        ext2_put_page(page);
336
                }
337
                if (++n >= npages)
338
                        n = 0;
339
        } while (n != start);
340
        return NULL;
341
 
342
found:
343
        *res_page = page;
344
        dir->u.ext2_i.i_dir_start_lookup = n;
345
        return de;
346
}
347
 
348
struct ext2_dir_entry_2 * ext2_dotdot (struct inode *dir, struct page **p)
349
{
350
        struct page *page = ext2_get_page(dir, 0);
351
        ext2_dirent *de = NULL;
352
 
353
        if (!IS_ERR(page)) {
354
                de = ext2_next_entry((ext2_dirent *) page_address(page));
355
                *p = page;
356
        }
357
        return de;
358
}
359
 
360
ino_t ext2_inode_by_name(struct inode * dir, struct dentry *dentry)
361
{
362
        ino_t res = 0;
363
        struct ext2_dir_entry_2 * de;
364
        struct page *page;
365
 
366
        de = ext2_find_entry (dir, dentry, &page);
367
        if (de) {
368
                res = le32_to_cpu(de->inode);
369
                kunmap(page);
370
                page_cache_release(page);
371
        }
372
        return res;
373
}
374
 
375
/* Releases the page */
376
void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
377
                        struct page *page, struct inode *inode)
378
{
379
        unsigned from = (char *) de - (char *) page_address(page);
380
        unsigned to = from + le16_to_cpu(de->rec_len);
381
        int err;
382
 
383
        lock_page(page);
384
        err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
385
        if (err)
386
                BUG();
387
        de->inode = cpu_to_le32(inode->i_ino);
388
        ext2_set_de_type (de, inode);
389
        err = ext2_commit_chunk(page, from, to);
390
        UnlockPage(page);
391
        ext2_put_page(page);
392
        dir->i_mtime = dir->i_ctime = CURRENT_TIME;
393
        dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
394
        mark_inode_dirty(dir);
395
}
396
 
397
/*
398
 *      Parent is locked.
399
 */
400
int ext2_add_link (struct dentry *dentry, struct inode *inode)
401
{
402
        struct inode *dir = dentry->d_parent->d_inode;
403
        const char *name = dentry->d_name.name;
404
        int namelen = dentry->d_name.len;
405
        unsigned reclen = EXT2_DIR_REC_LEN(namelen);
406
        unsigned short rec_len, name_len;
407
        struct page *page = NULL;
408
        ext2_dirent * de;
409
        unsigned long npages = dir_pages(dir);
410
        unsigned long n;
411
        char *kaddr;
412
        unsigned from, to;
413
        int err;
414
 
415
        /* We take care of directory expansion in the same loop */
416
        for (n = 0; n <= npages; n++) {
417
                page = ext2_get_page(dir, n);
418
                err = PTR_ERR(page);
419
                if (IS_ERR(page))
420
                        goto out;
421
                kaddr = page_address(page);
422
                de = (ext2_dirent *)kaddr;
423
                kaddr += PAGE_CACHE_SIZE - reclen;
424
                while ((char *)de <= kaddr) {
425
                        err = -EEXIST;
426
                        if (ext2_match (namelen, name, de))
427
                                goto out_page;
428
                        name_len = EXT2_DIR_REC_LEN(de->name_len);
429
                        rec_len = le16_to_cpu(de->rec_len);
430
                        if (!de->inode && rec_len >= reclen)
431
                                goto got_it;
432
                        if (rec_len >= name_len + reclen)
433
                                goto got_it;
434
                        de = (ext2_dirent *) ((char *) de + rec_len);
435
                }
436
                ext2_put_page(page);
437
        }
438
        BUG();
439
        return -EINVAL;
440
 
441
got_it:
442
        from = (char*)de - (char*)page_address(page);
443
        to = from + rec_len;
444
        lock_page(page);
445
        err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
446
        if (err)
447
                goto out_unlock;
448
        if (de->inode) {
449
                ext2_dirent *de1 = (ext2_dirent *) ((char *) de + name_len);
450
                de1->rec_len = cpu_to_le16(rec_len - name_len);
451
                de->rec_len = cpu_to_le16(name_len);
452
                de = de1;
453
        }
454
        de->name_len = namelen;
455
        memcpy (de->name, name, namelen);
456
        de->inode = cpu_to_le32(inode->i_ino);
457
        ext2_set_de_type (de, inode);
458
        err = ext2_commit_chunk(page, from, to);
459
        dir->i_mtime = dir->i_ctime = CURRENT_TIME;
460
        dir->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
461
        mark_inode_dirty(dir);
462
        /* OFFSET_CACHE */
463
out_unlock:
464
        UnlockPage(page);
465
out_page:
466
        ext2_put_page(page);
467
out:
468
        return err;
469
}
470
 
471
/*
472
 * ext2_delete_entry deletes a directory entry by merging it with the
473
 * previous entry. Page is up-to-date. Releases the page.
474
 */
475
int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page )
476
{
477
        struct address_space *mapping = page->mapping;
478
        struct inode *inode = mapping->host;
479
        char *kaddr = page_address(page);
480
        unsigned from = ((char*)dir - kaddr) & ~(ext2_chunk_size(inode)-1);
481
        unsigned to = ((char*)dir - kaddr) + le16_to_cpu(dir->rec_len);
482
        ext2_dirent * pde = NULL;
483
        ext2_dirent * de = (ext2_dirent *) (kaddr + from);
484
        int err;
485
 
486
        while ((char*)de < (char*)dir) {
487
                pde = de;
488
                de = ext2_next_entry(de);
489
        }
490
        if (pde)
491
                from = (char*)pde - (char*)page_address(page);
492
        lock_page(page);
493
        err = mapping->a_ops->prepare_write(NULL, page, from, to);
494
        if (err)
495
                BUG();
496
        if (pde)
497
                pde->rec_len = cpu_to_le16(to-from);
498
        dir->inode = 0;
499
        err = ext2_commit_chunk(page, from, to);
500
        UnlockPage(page);
501
        ext2_put_page(page);
502
        inode->i_ctime = inode->i_mtime = CURRENT_TIME;
503
        inode->u.ext2_i.i_flags &= ~EXT2_BTREE_FL;
504
        mark_inode_dirty(inode);
505
        return err;
506
}
507
 
508
/*
509
 * Set the first fragment of directory.
510
 */
511
int ext2_make_empty(struct inode *inode, struct inode *parent)
512
{
513
        struct address_space *mapping = inode->i_mapping;
514
        struct page *page = grab_cache_page(mapping, 0);
515
        unsigned chunk_size = ext2_chunk_size(inode);
516
        struct ext2_dir_entry_2 * de;
517
        char *base;
518
        int err;
519
 
520
        if (!page)
521
                return -ENOMEM;
522
        err = mapping->a_ops->prepare_write(NULL, page, 0, chunk_size);
523
        if (err)
524
                goto fail;
525
 
526
        base = page_address(page);
527
 
528
        de = (struct ext2_dir_entry_2 *) base;
529
        de->name_len = 1;
530
        de->rec_len = cpu_to_le16(EXT2_DIR_REC_LEN(1));
531
        memcpy (de->name, ".\0\0", 4);
532
        de->inode = cpu_to_le32(inode->i_ino);
533
        ext2_set_de_type (de, inode);
534
 
535
        de = (struct ext2_dir_entry_2 *) (base + EXT2_DIR_REC_LEN(1));
536
        de->name_len = 2;
537
        de->rec_len = cpu_to_le16(chunk_size - EXT2_DIR_REC_LEN(1));
538
        de->inode = cpu_to_le32(parent->i_ino);
539
        memcpy (de->name, "..\0", 4);
540
        ext2_set_de_type (de, inode);
541
 
542
        err = ext2_commit_chunk(page, 0, chunk_size);
543
fail:
544
        UnlockPage(page);
545
        page_cache_release(page);
546
        return err;
547
}
548
 
549
/*
550
 * routine to check that the specified directory is empty (for rmdir)
551
 */
552
int ext2_empty_dir (struct inode * inode)
553
{
554
        struct page *page = NULL;
555
        unsigned long i, npages = dir_pages(inode);
556
 
557
        for (i = 0; i < npages; i++) {
558
                char *kaddr;
559
                ext2_dirent * de;
560
                page = ext2_get_page(inode, i);
561
 
562
                if (IS_ERR(page))
563
                        continue;
564
 
565
                kaddr = page_address(page);
566
                de = (ext2_dirent *)kaddr;
567
                kaddr += PAGE_CACHE_SIZE-EXT2_DIR_REC_LEN(1);
568
 
569
                while ((char *)de <= kaddr) {
570
                        if (de->inode != 0) {
571
                                /* check for . and .. */
572
                                if (de->name[0] != '.')
573
                                        goto not_empty;
574
                                if (de->name_len > 2)
575
                                        goto not_empty;
576
                                if (de->name_len < 2) {
577
                                        if (de->inode !=
578
                                            cpu_to_le32(inode->i_ino))
579
                                                goto not_empty;
580
                                } else if (de->name[1] != '.')
581
                                        goto not_empty;
582
                        }
583
                        de = ext2_next_entry(de);
584
                }
585
                ext2_put_page(page);
586
        }
587
        return 1;
588
 
589
not_empty:
590
        ext2_put_page(page);
591
        return 0;
592
}
593
 
594
struct file_operations ext2_dir_operations = {
595
        read:           generic_read_dir,
596
        readdir:        ext2_readdir,
597
        ioctl:          ext2_ioctl,
598
        fsync:          ext2_sync_file,
599
};

powered by: WebSVN 2.1.0

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