1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "ext2.h"
25#include <linux/buffer_head.h>
26#include <linux/pagemap.h>
27#include <linux/swap.h>
28
29typedef struct ext2_dir_entry_2 ext2_dirent;
30
31static inline unsigned ext2_rec_len_from_disk(__le16 dlen)
32{
33 unsigned len = le16_to_cpu(dlen);
34
35 if (len == EXT2_MAX_REC_LEN)
36 return 1 << 16;
37 return len;
38}
39
40static inline __le16 ext2_rec_len_to_disk(unsigned len)
41{
42 if (len == (1 << 16))
43 return cpu_to_le16(EXT2_MAX_REC_LEN);
44 else if (len > (1 << 16))
45 BUG();
46 return cpu_to_le16(len);
47}
48
49
50
51
52
53static inline unsigned ext2_chunk_size(struct inode *inode)
54{
55 return inode->i_sb->s_blocksize;
56}
57
58static inline void ext2_put_page(struct page *page)
59{
60 kunmap(page);
61 page_cache_release(page);
62}
63
64static inline unsigned long dir_pages(struct inode *inode)
65{
66 return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
67}
68
69
70
71
72
73static unsigned
74ext2_last_byte(struct inode *inode, unsigned long page_nr)
75{
76 unsigned last_byte = inode->i_size;
77
78 last_byte -= page_nr << PAGE_CACHE_SHIFT;
79 if (last_byte > PAGE_CACHE_SIZE)
80 last_byte = PAGE_CACHE_SIZE;
81 return last_byte;
82}
83
84static int ext2_commit_chunk(struct page *page, loff_t pos, unsigned len)
85{
86 struct address_space *mapping = page->mapping;
87 struct inode *dir = mapping->host;
88 int err = 0;
89
90 dir->i_version++;
91 block_write_end(NULL, mapping, pos, len, len, page, NULL);
92
93 if (pos+len > dir->i_size) {
94 i_size_write(dir, pos+len);
95 mark_inode_dirty(dir);
96 }
97
98 if (IS_DIRSYNC(dir))
99 err = write_one_page(page, 1);
100 else
101 unlock_page(page);
102
103 return err;
104}
105
106static void ext2_check_page(struct page *page)
107{
108 struct inode *dir = page->mapping->host;
109 struct super_block *sb = dir->i_sb;
110 unsigned chunk_size = ext2_chunk_size(dir);
111 char *kaddr = page_address(page);
112 u32 max_inumber = le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count);
113 unsigned offs, rec_len;
114 unsigned limit = PAGE_CACHE_SIZE;
115 ext2_dirent *p;
116 char *error;
117
118 if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
119 limit = dir->i_size & ~PAGE_CACHE_MASK;
120 if (limit & (chunk_size - 1))
121 goto Ebadsize;
122 if (!limit)
123 goto out;
124 }
125 for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
126 p = (ext2_dirent *)(kaddr + offs);
127 rec_len = ext2_rec_len_from_disk(p->rec_len);
128
129 if (rec_len < EXT2_DIR_REC_LEN(1))
130 goto Eshort;
131 if (rec_len & 3)
132 goto Ealign;
133 if (rec_len < EXT2_DIR_REC_LEN(p->name_len))
134 goto Enamelen;
135 if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
136 goto Espan;
137 if (le32_to_cpu(p->inode) > max_inumber)
138 goto Einumber;
139 }
140 if (offs != limit)
141 goto Eend;
142out:
143 SetPageChecked(page);
144 return;
145
146
147
148Ebadsize:
149 ext2_error(sb, "ext2_check_page",
150 "size of directory #%lu is not a multiple of chunk size",
151 dir->i_ino
152 );
153 goto fail;
154Eshort:
155 error = "rec_len is smaller than minimal";
156 goto bad_entry;
157Ealign:
158 error = "unaligned directory entry";
159 goto bad_entry;
160Enamelen:
161 error = "rec_len is too small for name_len";
162 goto bad_entry;
163Espan:
164 error = "directory entry across blocks";
165 goto bad_entry;
166Einumber:
167 error = "inode out of bounds";
168bad_entry:
169 ext2_error (sb, "ext2_check_page", "bad entry in directory #%lu: %s - "
170 "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
171 dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT)+offs,
172 (unsigned long) le32_to_cpu(p->inode),
173 rec_len, p->name_len);
174 goto fail;
175Eend:
176 p = (ext2_dirent *)(kaddr + offs);
177 ext2_error (sb, "ext2_check_page",
178 "entry in directory #%lu spans the page boundary"
179 "offset=%lu, inode=%lu",
180 dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs,
181 (unsigned long) le32_to_cpu(p->inode));
182fail:
183 SetPageChecked(page);
184 SetPageError(page);
185}
186
187static struct page * ext2_get_page(struct inode *dir, unsigned long n)
188{
189 struct address_space *mapping = dir->i_mapping;
190 struct page *page = read_mapping_page(mapping, n, NULL);
191 if (!IS_ERR(page)) {
192 kmap(page);
193 if (!PageChecked(page))
194 ext2_check_page(page);
195 if (PageError(page))
196 goto fail;
197 }
198 return page;
199
200fail:
201 ext2_put_page(page);
202 return ERR_PTR(-EIO);
203}
204
205
206
207
208
209
210static inline int ext2_match (int len, const char * const name,
211 struct ext2_dir_entry_2 * de)
212{
213 if (len != de->name_len)
214 return 0;
215 if (!de->inode)
216 return 0;
217 return !memcmp(name, de->name, len);
218}
219
220
221
222
223static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
224{
225 return (ext2_dirent *)((char *)p +
226 ext2_rec_len_from_disk(p->rec_len));
227}
228
229static inline unsigned
230ext2_validate_entry(char *base, unsigned offset, unsigned mask)
231{
232 ext2_dirent *de = (ext2_dirent*)(base + offset);
233 ext2_dirent *p = (ext2_dirent*)(base + (offset&mask));
234 while ((char*)p < (char*)de) {
235 if (p->rec_len == 0)
236 break;
237 p = ext2_next_entry(p);
238 }
239 return (char *)p - base;
240}
241
242static unsigned char ext2_filetype_table[EXT2_FT_MAX] = {
243 [EXT2_FT_UNKNOWN] = DT_UNKNOWN,
244 [EXT2_FT_REG_FILE] = DT_REG,
245 [EXT2_FT_DIR] = DT_DIR,
246 [EXT2_FT_CHRDEV] = DT_CHR,
247 [EXT2_FT_BLKDEV] = DT_BLK,
248 [EXT2_FT_FIFO] = DT_FIFO,
249 [EXT2_FT_SOCK] = DT_SOCK,
250 [EXT2_FT_SYMLINK] = DT_LNK,
251};
252
253#define S_SHIFT 12
254static unsigned char ext2_type_by_mode[S_IFMT >> S_SHIFT] = {
255 [S_IFREG >> S_SHIFT] = EXT2_FT_REG_FILE,
256 [S_IFDIR >> S_SHIFT] = EXT2_FT_DIR,
257 [S_IFCHR >> S_SHIFT] = EXT2_FT_CHRDEV,
258 [S_IFBLK >> S_SHIFT] = EXT2_FT_BLKDEV,
259 [S_IFIFO >> S_SHIFT] = EXT2_FT_FIFO,
260 [S_IFSOCK >> S_SHIFT] = EXT2_FT_SOCK,
261 [S_IFLNK >> S_SHIFT] = EXT2_FT_SYMLINK,
262};
263
264static inline void ext2_set_de_type(ext2_dirent *de, struct inode *inode)
265{
266 mode_t mode = inode->i_mode;
267 if (EXT2_HAS_INCOMPAT_FEATURE(inode->i_sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
268 de->file_type = ext2_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
269 else
270 de->file_type = 0;
271}
272
273static int
274ext2_readdir (struct file * filp, void * dirent, filldir_t filldir)
275{
276 loff_t pos = filp->f_pos;
277 struct inode *inode = filp->f_path.dentry->d_inode;
278 struct super_block *sb = inode->i_sb;
279 unsigned int offset = pos & ~PAGE_CACHE_MASK;
280 unsigned long n = pos >> PAGE_CACHE_SHIFT;
281 unsigned long npages = dir_pages(inode);
282 unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
283 unsigned char *types = NULL;
284 int need_revalidate = filp->f_version != inode->i_version;
285
286 if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
287 return 0;
288
289 if (EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
290 types = ext2_filetype_table;
291
292 for ( ; n < npages; n++, offset = 0) {
293 char *kaddr, *limit;
294 ext2_dirent *de;
295 struct page *page = ext2_get_page(inode, n);
296
297 if (IS_ERR(page)) {
298 ext2_error(sb, __FUNCTION__,
299 "bad page in #%lu",
300 inode->i_ino);
301 filp->f_pos += PAGE_CACHE_SIZE - offset;
302 return -EIO;
303 }
304 kaddr = page_address(page);
305 if (unlikely(need_revalidate)) {
306 if (offset) {
307 offset = ext2_validate_entry(kaddr, offset, chunk_mask);
308 filp->f_pos = (n<<PAGE_CACHE_SHIFT) + offset;
309 }
310 filp->f_version = inode->i_version;
311 need_revalidate = 0;
312 }
313 de = (ext2_dirent *)(kaddr+offset);
314 limit = kaddr + ext2_last_byte(inode, n) - EXT2_DIR_REC_LEN(1);
315 for ( ;(char*)de <= limit; de = ext2_next_entry(de)) {
316 if (de->rec_len == 0) {
317 ext2_error(sb, __FUNCTION__,
318 "zero-length directory entry");
319 ext2_put_page(page);
320 return -EIO;
321 }
322 if (de->inode) {
323 int over;
324 unsigned char d_type = DT_UNKNOWN;
325
326 if (types && de->file_type < EXT2_FT_MAX)
327 d_type = types[de->file_type];
328
329 offset = (char *)de - kaddr;
330 over = filldir(dirent, de->name, de->name_len,
331 (n<<PAGE_CACHE_SHIFT) | offset,
332 le32_to_cpu(de->inode), d_type);
333 if (over) {
334 ext2_put_page(page);
335 return 0;
336 }
337 }
338 filp->f_pos += ext2_rec_len_from_disk(de->rec_len);
339 }
340 ext2_put_page(page);
341 }
342 return 0;
343}
344
345
346
347
348
349
350
351
352
353struct ext2_dir_entry_2 * ext2_find_entry (struct inode * dir,
354 struct dentry *dentry, struct page ** res_page)
355{
356 const char *name = dentry->d_name.name;
357 int namelen = dentry->d_name.len;
358 unsigned reclen = EXT2_DIR_REC_LEN(namelen);
359 unsigned long start, n;
360 unsigned long npages = dir_pages(dir);
361 struct page *page = NULL;
362 struct ext2_inode_info *ei = EXT2_I(dir);
363 ext2_dirent * de;
364
365 if (npages == 0)
366 goto out;
367
368
369 *res_page = NULL;
370
371 start = ei->i_dir_start_lookup;
372 if (start >= npages)
373 start = 0;
374 n = start;
375 do {
376 char *kaddr;
377 page = ext2_get_page(dir, n);
378 if (!IS_ERR(page)) {
379 kaddr = page_address(page);
380 de = (ext2_dirent *) kaddr;
381 kaddr += ext2_last_byte(dir, n) - reclen;
382 while ((char *) de <= kaddr) {
383 if (de->rec_len == 0) {
384 ext2_error(dir->i_sb, __FUNCTION__,
385 "zero-length directory entry");
386 ext2_put_page(page);
387 goto out;
388 }
389 if (ext2_match (namelen, name, de))
390 goto found;
391 de = ext2_next_entry(de);
392 }
393 ext2_put_page(page);
394 }
395 if (++n >= npages)
396 n = 0;
397
398 if (unlikely(n > (dir->i_blocks >> (PAGE_CACHE_SHIFT - 9)))) {
399 ext2_error(dir->i_sb, __FUNCTION__,
400 "dir %lu size %lld exceeds block count %llu",
401 dir->i_ino, dir->i_size,
402 (unsigned long long)dir->i_blocks);
403 goto out;
404 }
405 } while (n != start);
406out:
407 return NULL;
408
409found:
410 *res_page = page;
411 ei->i_dir_start_lookup = n;
412 return de;
413}
414
415struct ext2_dir_entry_2 * ext2_dotdot (struct inode *dir, struct page **p)
416{
417 struct page *page = ext2_get_page(dir, 0);
418 ext2_dirent *de = NULL;
419
420 if (!IS_ERR(page)) {
421 de = ext2_next_entry((ext2_dirent *) page_address(page));
422 *p = page;
423 }
424 return de;
425}
426
427ino_t ext2_inode_by_name(struct inode * dir, struct dentry *dentry)
428{
429 ino_t res = 0;
430 struct ext2_dir_entry_2 * de;
431 struct page *page;
432
433 de = ext2_find_entry (dir, dentry, &page);
434 if (de) {
435 res = le32_to_cpu(de->inode);
436 ext2_put_page(page);
437 }
438 return res;
439}
440
441
442void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
443 struct page *page, struct inode *inode)
444{
445 loff_t pos = page_offset(page) +
446 (char *) de - (char *) page_address(page);
447 unsigned len = ext2_rec_len_from_disk(de->rec_len);
448 int err;
449
450 lock_page(page);
451 err = __ext2_write_begin(NULL, page->mapping, pos, len,
452 AOP_FLAG_UNINTERRUPTIBLE, &page, NULL);
453 BUG_ON(err);
454 de->inode = cpu_to_le32(inode->i_ino);
455 ext2_set_de_type(de, inode);
456 err = ext2_commit_chunk(page, pos, len);
457 ext2_put_page(page);
458 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
459 EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
460 mark_inode_dirty(dir);
461}
462
463
464
465
466int ext2_add_link (struct dentry *dentry, struct inode *inode)
467{
468 struct inode *dir = dentry->d_parent->d_inode;
469 const char *name = dentry->d_name.name;
470 int namelen = dentry->d_name.len;
471 unsigned chunk_size = ext2_chunk_size(dir);
472 unsigned reclen = EXT2_DIR_REC_LEN(namelen);
473 unsigned short rec_len, name_len;
474 struct page *page = NULL;
475 ext2_dirent * de;
476 unsigned long npages = dir_pages(dir);
477 unsigned long n;
478 char *kaddr;
479 loff_t pos;
480 int err;
481
482
483
484
485
486
487 for (n = 0; n <= npages; n++) {
488 char *dir_end;
489
490 page = ext2_get_page(dir, n);
491 err = PTR_ERR(page);
492 if (IS_ERR(page))
493 goto out;
494 lock_page(page);
495 kaddr = page_address(page);
496 dir_end = kaddr + ext2_last_byte(dir, n);
497 de = (ext2_dirent *)kaddr;
498 kaddr += PAGE_CACHE_SIZE - reclen;
499 while ((char *)de <= kaddr) {
500 if ((char *)de == dir_end) {
501
502 name_len = 0;
503 rec_len = chunk_size;
504 de->rec_len = ext2_rec_len_to_disk(chunk_size);
505 de->inode = 0;
506 goto got_it;
507 }
508 if (de->rec_len == 0) {
509 ext2_error(dir->i_sb, __FUNCTION__,
510 "zero-length directory entry");
511 err = -EIO;
512 goto out_unlock;
513 }
514 err = -EEXIST;
515 if (ext2_match (namelen, name, de))
516 goto out_unlock;
517 name_len = EXT2_DIR_REC_LEN(de->name_len);
518 rec_len = ext2_rec_len_from_disk(de->rec_len);
519 if (!de->inode && rec_len >= reclen)
520 goto got_it;
521 if (rec_len >= name_len + reclen)
522 goto got_it;
523 de = (ext2_dirent *) ((char *) de + rec_len);
524 }
525 unlock_page(page);
526 ext2_put_page(page);
527 }
528 BUG();
529 return -EINVAL;
530
531got_it:
532 pos = page_offset(page) +
533 (char*)de - (char*)page_address(page);
534 err = __ext2_write_begin(NULL, page->mapping, pos, rec_len, 0,
535 &page, NULL);
536 if (err)
537 goto out_unlock;
538 if (de->inode) {
539 ext2_dirent *de1 = (ext2_dirent *) ((char *) de + name_len);
540 de1->rec_len = ext2_rec_len_to_disk(rec_len - name_len);
541 de->rec_len = ext2_rec_len_to_disk(name_len);
542 de = de1;
543 }
544 de->name_len = namelen;
545 memcpy(de->name, name, namelen);
546 de->inode = cpu_to_le32(inode->i_ino);
547 ext2_set_de_type (de, inode);
548 err = ext2_commit_chunk(page, pos, rec_len);
549 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
550 EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
551 mark_inode_dirty(dir);
552
553out_put:
554 ext2_put_page(page);
555out:
556 return err;
557out_unlock:
558 unlock_page(page);
559 goto out_put;
560}
561
562
563
564
565
566int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page )
567{
568 struct address_space *mapping = page->mapping;
569 struct inode *inode = mapping->host;
570 char *kaddr = page_address(page);
571 unsigned from = ((char*)dir - kaddr) & ~(ext2_chunk_size(inode)-1);
572 unsigned to = ((char *)dir - kaddr) +
573 ext2_rec_len_from_disk(dir->rec_len);
574 loff_t pos;
575 ext2_dirent * pde = NULL;
576 ext2_dirent * de = (ext2_dirent *) (kaddr + from);
577 int err;
578
579 while ((char*)de < (char*)dir) {
580 if (de->rec_len == 0) {
581 ext2_error(inode->i_sb, __FUNCTION__,
582 "zero-length directory entry");
583 err = -EIO;
584 goto out;
585 }
586 pde = de;
587 de = ext2_next_entry(de);
588 }
589 if (pde)
590 from = (char*)pde - (char*)page_address(page);
591 pos = page_offset(page) + from;
592 lock_page(page);
593 err = __ext2_write_begin(NULL, page->mapping, pos, to - from, 0,
594 &page, NULL);
595 BUG_ON(err);
596 if (pde)
597 pde->rec_len = ext2_rec_len_to_disk(to - from);
598 dir->inode = 0;
599 err = ext2_commit_chunk(page, pos, to - from);
600 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
601 EXT2_I(inode)->i_flags &= ~EXT2_BTREE_FL;
602 mark_inode_dirty(inode);
603out:
604 ext2_put_page(page);
605 return err;
606}
607
608
609
610
611int ext2_make_empty(struct inode *inode, struct inode *parent)
612{
613 struct address_space *mapping = inode->i_mapping;
614 struct page *page = grab_cache_page(mapping, 0);
615 unsigned chunk_size = ext2_chunk_size(inode);
616 struct ext2_dir_entry_2 * de;
617 int err;
618 void *kaddr;
619
620 if (!page)
621 return -ENOMEM;
622
623 err = __ext2_write_begin(NULL, page->mapping, 0, chunk_size, 0,
624 &page, NULL);
625 if (err) {
626 unlock_page(page);
627 goto fail;
628 }
629 kaddr = kmap_atomic(page, KM_USER0);
630 memset(kaddr, 0, chunk_size);
631 de = (struct ext2_dir_entry_2 *)kaddr;
632 de->name_len = 1;
633 de->rec_len = ext2_rec_len_to_disk(EXT2_DIR_REC_LEN(1));
634 memcpy (de->name, ".\0\0", 4);
635 de->inode = cpu_to_le32(inode->i_ino);
636 ext2_set_de_type (de, inode);
637
638 de = (struct ext2_dir_entry_2 *)(kaddr + EXT2_DIR_REC_LEN(1));
639 de->name_len = 2;
640 de->rec_len = ext2_rec_len_to_disk(chunk_size - EXT2_DIR_REC_LEN(1));
641 de->inode = cpu_to_le32(parent->i_ino);
642 memcpy (de->name, "..\0", 4);
643 ext2_set_de_type (de, inode);
644 kunmap_atomic(kaddr, KM_USER0);
645 err = ext2_commit_chunk(page, 0, chunk_size);
646fail:
647 page_cache_release(page);
648 return err;
649}
650
651
652
653
654int ext2_empty_dir (struct inode * inode)
655{
656 struct page *page = NULL;
657 unsigned long i, npages = dir_pages(inode);
658
659 for (i = 0; i < npages; i++) {
660 char *kaddr;
661 ext2_dirent * de;
662 page = ext2_get_page(inode, i);
663
664 if (IS_ERR(page))
665 continue;
666
667 kaddr = page_address(page);
668 de = (ext2_dirent *)kaddr;
669 kaddr += ext2_last_byte(inode, i) - EXT2_DIR_REC_LEN(1);
670
671 while ((char *)de <= kaddr) {
672 if (de->rec_len == 0) {
673 ext2_error(inode->i_sb, __FUNCTION__,
674 "zero-length directory entry");
675 printk("kaddr=%p, de=%p\n", kaddr, de);
676 goto not_empty;
677 }
678 if (de->inode != 0) {
679
680 if (de->name[0] != '.')
681 goto not_empty;
682 if (de->name_len > 2)
683 goto not_empty;
684 if (de->name_len < 2) {
685 if (de->inode !=
686 cpu_to_le32(inode->i_ino))
687 goto not_empty;
688 } else if (de->name[1] != '.')
689 goto not_empty;
690 }
691 de = ext2_next_entry(de);
692 }
693 ext2_put_page(page);
694 }
695 return 1;
696
697not_empty:
698 ext2_put_page(page);
699 return 0;
700}
701
702const struct file_operations ext2_dir_operations = {
703 .llseek = generic_file_llseek,
704 .read = generic_read_dir,
705 .readdir = ext2_readdir,
706 .ioctl = ext2_ioctl,
707#ifdef CONFIG_COMPAT
708 .compat_ioctl = ext2_compat_ioctl,
709#endif
710 .fsync = ext2_sync_file,
711};
712