MAINTAINERS: Add Reinette Chatre to iwlwifi & ipw2100/ipw2200
[linux-2.6/sactl.git] / fs / ext2 / dir.c
blobd868e26c15eba90e48c5c1324c9f207ef109d1e6
1 /*
2 * linux/fs/ext2/dir.c
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)
9 * from
11 * linux/fs/minix/dir.c
13 * Copyright (C) 1991, 1992 Linus Torvalds
15 * ext2 directory handling functions
17 * Big-endian to little-endian byte-swapping/bitmaps by
18 * David S. Miller (davem@caip.rutgers.edu), 1995
20 * All code that works with directory layout had been switched to pagecache
21 * and moved here. AV
24 #include "ext2.h"
25 #include <linux/buffer_head.h>
26 #include <linux/pagemap.h>
27 #include <linux/swap.h>
29 typedef struct ext2_dir_entry_2 ext2_dirent;
31 static inline unsigned ext2_rec_len_from_disk(__le16 dlen)
33 unsigned len = le16_to_cpu(dlen);
35 if (len == EXT2_MAX_REC_LEN)
36 return 1 << 16;
37 return len;
40 static inline __le16 ext2_rec_len_to_disk(unsigned len)
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);
50 * ext2 uses block-sized chunks. Arguably, sector-sized ones would be
51 * more robust, but we have what we have
53 static inline unsigned ext2_chunk_size(struct inode *inode)
55 return inode->i_sb->s_blocksize;
58 static inline void ext2_put_page(struct page *page)
60 kunmap(page);
61 page_cache_release(page);
64 static inline unsigned long dir_pages(struct inode *inode)
66 return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
70 * Return the offset into page `page_nr' of the last valid
71 * byte in that page, plus one.
73 static unsigned
74 ext2_last_byte(struct inode *inode, unsigned long page_nr)
76 unsigned last_byte = inode->i_size;
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;
84 static int ext2_commit_chunk(struct page *page, loff_t pos, unsigned len)
86 struct address_space *mapping = page->mapping;
87 struct inode *dir = mapping->host;
88 int err = 0;
90 dir->i_version++;
91 block_write_end(NULL, mapping, pos, len, len, page, NULL);
93 if (pos+len > dir->i_size) {
94 i_size_write(dir, pos+len);
95 mark_inode_dirty(dir);
98 if (IS_DIRSYNC(dir))
99 err = write_one_page(page, 1);
100 else
101 unlock_page(page);
103 return err;
106 static void ext2_check_page(struct page *page)
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;
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;
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);
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;
140 if (offs != limit)
141 goto Eend;
142 out:
143 SetPageChecked(page);
144 return;
146 /* Too bad, we had an error */
148 Ebadsize:
149 ext2_error(sb, "ext2_check_page",
150 "size of directory #%lu is not a multiple of chunk size",
151 dir->i_ino
153 goto fail;
154 Eshort:
155 error = "rec_len is smaller than minimal";
156 goto bad_entry;
157 Ealign:
158 error = "unaligned directory entry";
159 goto bad_entry;
160 Enamelen:
161 error = "rec_len is too small for name_len";
162 goto bad_entry;
163 Espan:
164 error = "directory entry across blocks";
165 goto bad_entry;
166 Einumber:
167 error = "inode out of bounds";
168 bad_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;
175 Eend:
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));
182 fail:
183 SetPageChecked(page);
184 SetPageError(page);
187 static struct page * ext2_get_page(struct inode *dir, unsigned long n)
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;
198 return page;
200 fail:
201 ext2_put_page(page);
202 return ERR_PTR(-EIO);
206 * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure.
208 * len <= EXT2_NAME_LEN and de != NULL are guaranteed by caller.
210 static inline int ext2_match (int len, const char * const name,
211 struct ext2_dir_entry_2 * de)
213 if (len != de->name_len)
214 return 0;
215 if (!de->inode)
216 return 0;
217 return !memcmp(name, de->name, len);
221 * p is at least 6 bytes before the end of page
223 static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
225 return (ext2_dirent *)((char *)p +
226 ext2_rec_len_from_disk(p->rec_len));
229 static inline unsigned
230 ext2_validate_entry(char *base, unsigned offset, unsigned mask)
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);
239 return (char *)p - base;
242 static 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,
253 #define S_SHIFT 12
254 static 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,
264 static inline void ext2_set_de_type(ext2_dirent *de, struct inode *inode)
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;
273 static int
274 ext2_readdir (struct file * filp, void * dirent, filldir_t filldir)
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;
286 if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
287 return 0;
289 if (EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
290 types = ext2_filetype_table;
292 for ( ; n < npages; n++, offset = 0) {
293 char *kaddr, *limit;
294 ext2_dirent *de;
295 struct page *page = ext2_get_page(inode, n);
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;
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;
310 filp->f_version = inode->i_version;
311 need_revalidate = 0;
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;
322 if (de->inode) {
323 int over;
324 unsigned char d_type = DT_UNKNOWN;
326 if (types && de->file_type < EXT2_FT_MAX)
327 d_type = types[de->file_type];
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;
338 filp->f_pos += ext2_rec_len_from_disk(de->rec_len);
340 ext2_put_page(page);
342 return 0;
346 * ext2_find_entry()
348 * finds an entry in the specified directory with the wanted name. It
349 * returns the page in which the entry was found, and the entry itself
350 * (as a parameter - res_dir). Page is returned mapped and unlocked.
351 * Entry is guaranteed to be valid.
353 struct ext2_dir_entry_2 * ext2_find_entry (struct inode * dir,
354 struct dentry *dentry, struct page ** res_page)
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;
365 if (npages == 0)
366 goto out;
368 /* OFFSET_CACHE */
369 *res_page = NULL;
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;
389 if (ext2_match (namelen, name, de))
390 goto found;
391 de = ext2_next_entry(de);
393 ext2_put_page(page);
395 if (++n >= npages)
396 n = 0;
397 /* next page is past the blocks we've got */
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;
405 } while (n != start);
406 out:
407 return NULL;
409 found:
410 *res_page = page;
411 ei->i_dir_start_lookup = n;
412 return de;
415 struct ext2_dir_entry_2 * ext2_dotdot (struct inode *dir, struct page **p)
417 struct page *page = ext2_get_page(dir, 0);
418 ext2_dirent *de = NULL;
420 if (!IS_ERR(page)) {
421 de = ext2_next_entry((ext2_dirent *) page_address(page));
422 *p = page;
424 return de;
427 ino_t ext2_inode_by_name(struct inode * dir, struct dentry *dentry)
429 ino_t res = 0;
430 struct ext2_dir_entry_2 * de;
431 struct page *page;
433 de = ext2_find_entry (dir, dentry, &page);
434 if (de) {
435 res = le32_to_cpu(de->inode);
436 ext2_put_page(page);
438 return res;
441 /* Releases the page */
442 void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
443 struct page *page, struct inode *inode)
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;
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);
464 * Parent is locked.
466 int ext2_add_link (struct dentry *dentry, struct inode *inode)
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;
483 * We take care of directory expansion in the same loop.
484 * This code plays outside i_size, so it locks the page
485 * to protect that region.
487 for (n = 0; n <= npages; n++) {
488 char *dir_end;
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 /* We hit i_size */
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;
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;
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);
525 unlock_page(page);
526 ext2_put_page(page);
528 BUG();
529 return -EINVAL;
531 got_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;
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 /* OFFSET_CACHE */
553 out_put:
554 ext2_put_page(page);
555 out:
556 return err;
557 out_unlock:
558 unlock_page(page);
559 goto out_put;
563 * ext2_delete_entry deletes a directory entry by merging it with the
564 * previous entry. Page is up-to-date. Releases the page.
566 int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page )
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;
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;
586 pde = de;
587 de = ext2_next_entry(de);
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);
603 out:
604 ext2_put_page(page);
605 return err;
609 * Set the first fragment of directory.
611 int ext2_make_empty(struct inode *inode, struct inode *parent)
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;
620 if (!page)
621 return -ENOMEM;
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;
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);
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);
646 fail:
647 page_cache_release(page);
648 return err;
652 * routine to check that the specified directory is empty (for rmdir)
654 int ext2_empty_dir (struct inode * inode)
656 struct page *page = NULL;
657 unsigned long i, npages = dir_pages(inode);
659 for (i = 0; i < npages; i++) {
660 char *kaddr;
661 ext2_dirent * de;
662 page = ext2_get_page(inode, i);
664 if (IS_ERR(page))
665 continue;
667 kaddr = page_address(page);
668 de = (ext2_dirent *)kaddr;
669 kaddr += ext2_last_byte(inode, i) - EXT2_DIR_REC_LEN(1);
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;
678 if (de->inode != 0) {
679 /* check for . and .. */
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;
691 de = ext2_next_entry(de);
693 ext2_put_page(page);
695 return 1;
697 not_empty:
698 ext2_put_page(page);
699 return 0;
702 const 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,