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)
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
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;
32 * ext2 uses block-sized chunks. Arguably, sector-sized ones would be
33 * more robust, but we have what we have
35 static inline unsigned ext2_chunk_size(struct inode *inode)
37 return inode->i_sb->s_blocksize;
40 static inline void ext2_put_page(struct page *page)
43 page_cache_release(page);
46 static inline unsigned long dir_pages(struct inode *inode)
48 return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
52 * Return the offset into page `page_nr' of the last valid
53 * byte in that page, plus one.
56 ext2_last_byte(struct inode *inode, unsigned long page_nr)
58 unsigned last_byte = inode->i_size;
60 last_byte -= page_nr << PAGE_CACHE_SHIFT;
61 if (last_byte > PAGE_CACHE_SIZE)
62 last_byte = PAGE_CACHE_SIZE;
66 static int ext2_commit_chunk(struct page *page, loff_t pos, unsigned len)
68 struct address_space *mapping = page->mapping;
69 struct inode *dir = mapping->host;
73 block_write_end(NULL, mapping, pos, len, len, page, NULL);
75 if (pos+len > dir->i_size) {
76 i_size_write(dir, pos+len);
77 mark_inode_dirty(dir);
81 err = write_one_page(page, 1);
88 static void ext2_check_page(struct page *page)
90 struct inode *dir = page->mapping->host;
91 struct super_block *sb = dir->i_sb;
92 unsigned chunk_size = ext2_chunk_size(dir);
93 char *kaddr = page_address(page);
94 u32 max_inumber = le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count);
95 unsigned offs, rec_len;
96 unsigned limit = PAGE_CACHE_SIZE;
100 if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
101 limit = dir->i_size & ~PAGE_CACHE_MASK;
102 if (limit & (chunk_size - 1))
107 for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
108 p = (ext2_dirent *)(kaddr + offs);
109 rec_len = le16_to_cpu(p->rec_len);
111 if (rec_len < EXT2_DIR_REC_LEN(1))
115 if (rec_len < EXT2_DIR_REC_LEN(p->name_len))
117 if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
119 if (le32_to_cpu(p->inode) > max_inumber)
125 SetPageChecked(page);
128 /* Too bad, we had an error */
131 ext2_error(sb, "ext2_check_page",
132 "size of directory #%lu is not a multiple of chunk size",
137 error = "rec_len is smaller than minimal";
140 error = "unaligned directory entry";
143 error = "rec_len is too small for name_len";
146 error = "directory entry across blocks";
149 error = "inode out of bounds";
151 ext2_error (sb, "ext2_check_page", "bad entry in directory #%lu: %s - "
152 "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
153 dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT)+offs,
154 (unsigned long) le32_to_cpu(p->inode),
155 rec_len, p->name_len);
158 p = (ext2_dirent *)(kaddr + offs);
159 ext2_error (sb, "ext2_check_page",
160 "entry in directory #%lu spans the page boundary"
161 "offset=%lu, inode=%lu",
162 dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs,
163 (unsigned long) le32_to_cpu(p->inode));
165 SetPageChecked(page);
169 static struct page * ext2_get_page(struct inode *dir, unsigned long n)
171 struct address_space *mapping = dir->i_mapping;
172 struct page *page = read_mapping_page(mapping, n, NULL);
175 if (!PageChecked(page))
176 ext2_check_page(page);
184 return ERR_PTR(-EIO);
188 * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure.
190 * len <= EXT2_NAME_LEN and de != NULL are guaranteed by caller.
192 static inline int ext2_match (int len, const char * const name,
193 struct ext2_dir_entry_2 * de)
195 if (len != de->name_len)
199 return !memcmp(name, de->name, len);
203 * p is at least 6 bytes before the end of page
205 static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
207 return (ext2_dirent *)((char*)p + le16_to_cpu(p->rec_len));
210 static inline unsigned
211 ext2_validate_entry(char *base, unsigned offset, unsigned mask)
213 ext2_dirent *de = (ext2_dirent*)(base + offset);
214 ext2_dirent *p = (ext2_dirent*)(base + (offset&mask));
215 while ((char*)p < (char*)de) {
218 p = ext2_next_entry(p);
220 return (char *)p - base;
223 static unsigned char ext2_filetype_table[EXT2_FT_MAX] = {
224 [EXT2_FT_UNKNOWN] = DT_UNKNOWN,
225 [EXT2_FT_REG_FILE] = DT_REG,
226 [EXT2_FT_DIR] = DT_DIR,
227 [EXT2_FT_CHRDEV] = DT_CHR,
228 [EXT2_FT_BLKDEV] = DT_BLK,
229 [EXT2_FT_FIFO] = DT_FIFO,
230 [EXT2_FT_SOCK] = DT_SOCK,
231 [EXT2_FT_SYMLINK] = DT_LNK,
235 static unsigned char ext2_type_by_mode[S_IFMT >> S_SHIFT] = {
236 [S_IFREG >> S_SHIFT] = EXT2_FT_REG_FILE,
237 [S_IFDIR >> S_SHIFT] = EXT2_FT_DIR,
238 [S_IFCHR >> S_SHIFT] = EXT2_FT_CHRDEV,
239 [S_IFBLK >> S_SHIFT] = EXT2_FT_BLKDEV,
240 [S_IFIFO >> S_SHIFT] = EXT2_FT_FIFO,
241 [S_IFSOCK >> S_SHIFT] = EXT2_FT_SOCK,
242 [S_IFLNK >> S_SHIFT] = EXT2_FT_SYMLINK,
245 static inline void ext2_set_de_type(ext2_dirent *de, struct inode *inode)
247 mode_t mode = inode->i_mode;
248 if (EXT2_HAS_INCOMPAT_FEATURE(inode->i_sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
249 de->file_type = ext2_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
255 ext2_readdir (struct file * filp, void * dirent, filldir_t filldir)
257 loff_t pos = filp->f_pos;
258 struct inode *inode = filp->f_path.dentry->d_inode;
259 struct super_block *sb = inode->i_sb;
260 unsigned int offset = pos & ~PAGE_CACHE_MASK;
261 unsigned long n = pos >> PAGE_CACHE_SHIFT;
262 unsigned long npages = dir_pages(inode);
263 unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
264 unsigned char *types = NULL;
265 int need_revalidate = filp->f_version != inode->i_version;
267 if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
270 if (EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
271 types = ext2_filetype_table;
273 for ( ; n < npages; n++, offset = 0) {
276 struct page *page = ext2_get_page(inode, n);
279 ext2_error(sb, __FUNCTION__,
282 filp->f_pos += PAGE_CACHE_SIZE - offset;
285 kaddr = page_address(page);
286 if (unlikely(need_revalidate)) {
288 offset = ext2_validate_entry(kaddr, offset, chunk_mask);
289 filp->f_pos = (n<<PAGE_CACHE_SHIFT) + offset;
291 filp->f_version = inode->i_version;
294 de = (ext2_dirent *)(kaddr+offset);
295 limit = kaddr + ext2_last_byte(inode, n) - EXT2_DIR_REC_LEN(1);
296 for ( ;(char*)de <= limit; de = ext2_next_entry(de)) {
297 if (de->rec_len == 0) {
298 ext2_error(sb, __FUNCTION__,
299 "zero-length directory entry");
305 unsigned char d_type = DT_UNKNOWN;
307 if (types && de->file_type < EXT2_FT_MAX)
308 d_type = types[de->file_type];
310 offset = (char *)de - kaddr;
311 over = filldir(dirent, de->name, de->name_len,
312 (n<<PAGE_CACHE_SHIFT) | offset,
313 le32_to_cpu(de->inode), d_type);
319 filp->f_pos += le16_to_cpu(de->rec_len);
329 * finds an entry in the specified directory with the wanted name. It
330 * returns the page in which the entry was found, and the entry itself
331 * (as a parameter - res_dir). Page is returned mapped and unlocked.
332 * Entry is guaranteed to be valid.
334 struct ext2_dir_entry_2 * ext2_find_entry (struct inode * dir,
335 struct dentry *dentry, struct page ** res_page)
337 const char *name = dentry->d_name.name;
338 int namelen = dentry->d_name.len;
339 unsigned reclen = EXT2_DIR_REC_LEN(namelen);
340 unsigned long start, n;
341 unsigned long npages = dir_pages(dir);
342 struct page *page = NULL;
343 struct ext2_inode_info *ei = EXT2_I(dir);
352 start = ei->i_dir_start_lookup;
358 page = ext2_get_page(dir, n);
360 kaddr = page_address(page);
361 de = (ext2_dirent *) kaddr;
362 kaddr += ext2_last_byte(dir, n) - reclen;
363 while ((char *) de <= kaddr) {
364 if (de->rec_len == 0) {
365 ext2_error(dir->i_sb, __FUNCTION__,
366 "zero-length directory entry");
370 if (ext2_match (namelen, name, de))
372 de = ext2_next_entry(de);
378 /* next page is past the blocks we've got */
379 if (unlikely(n > (dir->i_blocks >> (PAGE_CACHE_SHIFT - 9)))) {
380 ext2_error(dir->i_sb, __FUNCTION__,
381 "dir %lu size %lld exceeds block count %llu",
382 dir->i_ino, dir->i_size,
383 (unsigned long long)dir->i_blocks);
386 } while (n != start);
392 ei->i_dir_start_lookup = n;
396 struct ext2_dir_entry_2 * ext2_dotdot (struct inode *dir, struct page **p)
398 struct page *page = ext2_get_page(dir, 0);
399 ext2_dirent *de = NULL;
402 de = ext2_next_entry((ext2_dirent *) page_address(page));
408 ino_t ext2_inode_by_name(struct inode * dir, struct dentry *dentry)
411 struct ext2_dir_entry_2 * de;
414 de = ext2_find_entry (dir, dentry, &page);
416 res = le32_to_cpu(de->inode);
422 /* Releases the page */
423 void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
424 struct page *page, struct inode *inode)
426 loff_t pos = page_offset(page) +
427 (char *) de - (char *) page_address(page);
428 unsigned len = le16_to_cpu(de->rec_len);
432 err = __ext2_write_begin(NULL, page->mapping, pos, len,
433 AOP_FLAG_UNINTERRUPTIBLE, &page, NULL);
435 de->inode = cpu_to_le32(inode->i_ino);
436 ext2_set_de_type(de, inode);
437 err = ext2_commit_chunk(page, pos, len);
439 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
440 EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
441 mark_inode_dirty(dir);
447 int ext2_add_link (struct dentry *dentry, struct inode *inode)
449 struct inode *dir = dentry->d_parent->d_inode;
450 const char *name = dentry->d_name.name;
451 int namelen = dentry->d_name.len;
452 unsigned chunk_size = ext2_chunk_size(dir);
453 unsigned reclen = EXT2_DIR_REC_LEN(namelen);
454 unsigned short rec_len, name_len;
455 struct page *page = NULL;
457 unsigned long npages = dir_pages(dir);
464 * We take care of directory expansion in the same loop.
465 * This code plays outside i_size, so it locks the page
466 * to protect that region.
468 for (n = 0; n <= npages; n++) {
471 page = ext2_get_page(dir, n);
476 kaddr = page_address(page);
477 dir_end = kaddr + ext2_last_byte(dir, n);
478 de = (ext2_dirent *)kaddr;
479 kaddr += PAGE_CACHE_SIZE - reclen;
480 while ((char *)de <= kaddr) {
481 if ((char *)de == dir_end) {
484 rec_len = chunk_size;
485 de->rec_len = cpu_to_le16(chunk_size);
489 if (de->rec_len == 0) {
490 ext2_error(dir->i_sb, __FUNCTION__,
491 "zero-length directory entry");
496 if (ext2_match (namelen, name, de))
498 name_len = EXT2_DIR_REC_LEN(de->name_len);
499 rec_len = le16_to_cpu(de->rec_len);
500 if (!de->inode && rec_len >= reclen)
502 if (rec_len >= name_len + reclen)
504 de = (ext2_dirent *) ((char *) de + rec_len);
513 pos = page_offset(page) +
514 (char*)de - (char*)page_address(page);
515 err = __ext2_write_begin(NULL, page->mapping, pos, rec_len, 0,
520 ext2_dirent *de1 = (ext2_dirent *) ((char *) de + name_len);
521 de1->rec_len = cpu_to_le16(rec_len - name_len);
522 de->rec_len = cpu_to_le16(name_len);
525 de->name_len = namelen;
526 memcpy(de->name, name, namelen);
527 de->inode = cpu_to_le32(inode->i_ino);
528 ext2_set_de_type (de, inode);
529 err = ext2_commit_chunk(page, pos, rec_len);
530 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
531 EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
532 mark_inode_dirty(dir);
544 * ext2_delete_entry deletes a directory entry by merging it with the
545 * previous entry. Page is up-to-date. Releases the page.
547 int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page )
549 struct address_space *mapping = page->mapping;
550 struct inode *inode = mapping->host;
551 char *kaddr = page_address(page);
552 unsigned from = ((char*)dir - kaddr) & ~(ext2_chunk_size(inode)-1);
553 unsigned to = ((char*)dir - kaddr) + le16_to_cpu(dir->rec_len);
555 ext2_dirent * pde = NULL;
556 ext2_dirent * de = (ext2_dirent *) (kaddr + from);
559 while ((char*)de < (char*)dir) {
560 if (de->rec_len == 0) {
561 ext2_error(inode->i_sb, __FUNCTION__,
562 "zero-length directory entry");
567 de = ext2_next_entry(de);
570 from = (char*)pde - (char*)page_address(page);
571 pos = page_offset(page) + from;
573 err = __ext2_write_begin(NULL, page->mapping, pos, to - from, 0,
577 pde->rec_len = cpu_to_le16(to - from);
579 err = ext2_commit_chunk(page, pos, to - from);
580 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
581 EXT2_I(inode)->i_flags &= ~EXT2_BTREE_FL;
582 mark_inode_dirty(inode);
589 * Set the first fragment of directory.
591 int ext2_make_empty(struct inode *inode, struct inode *parent)
593 struct address_space *mapping = inode->i_mapping;
594 struct page *page = grab_cache_page(mapping, 0);
595 unsigned chunk_size = ext2_chunk_size(inode);
596 struct ext2_dir_entry_2 * de;
603 err = __ext2_write_begin(NULL, page->mapping, 0, chunk_size, 0,
609 kaddr = kmap_atomic(page, KM_USER0);
610 memset(kaddr, 0, chunk_size);
611 de = (struct ext2_dir_entry_2 *)kaddr;
613 de->rec_len = cpu_to_le16(EXT2_DIR_REC_LEN(1));
614 memcpy (de->name, ".\0\0", 4);
615 de->inode = cpu_to_le32(inode->i_ino);
616 ext2_set_de_type (de, inode);
618 de = (struct ext2_dir_entry_2 *)(kaddr + EXT2_DIR_REC_LEN(1));
620 de->rec_len = cpu_to_le16(chunk_size - EXT2_DIR_REC_LEN(1));
621 de->inode = cpu_to_le32(parent->i_ino);
622 memcpy (de->name, "..\0", 4);
623 ext2_set_de_type (de, inode);
624 kunmap_atomic(kaddr, KM_USER0);
625 err = ext2_commit_chunk(page, 0, chunk_size);
627 page_cache_release(page);
632 * routine to check that the specified directory is empty (for rmdir)
634 int ext2_empty_dir (struct inode * inode)
636 struct page *page = NULL;
637 unsigned long i, npages = dir_pages(inode);
639 for (i = 0; i < npages; i++) {
642 page = ext2_get_page(inode, i);
647 kaddr = page_address(page);
648 de = (ext2_dirent *)kaddr;
649 kaddr += ext2_last_byte(inode, i) - EXT2_DIR_REC_LEN(1);
651 while ((char *)de <= kaddr) {
652 if (de->rec_len == 0) {
653 ext2_error(inode->i_sb, __FUNCTION__,
654 "zero-length directory entry");
655 printk("kaddr=%p, de=%p\n", kaddr, de);
658 if (de->inode != 0) {
659 /* check for . and .. */
660 if (de->name[0] != '.')
662 if (de->name_len > 2)
664 if (de->name_len < 2) {
666 cpu_to_le32(inode->i_ino))
668 } else if (de->name[1] != '.')
671 de = ext2_next_entry(de);
682 const struct file_operations ext2_dir_operations = {
683 .llseek = generic_file_llseek,
684 .read = generic_read_dir,
685 .readdir = ext2_readdir,
688 .compat_ioctl = ext2_compat_ioctl,
690 .fsync = ext2_sync_file,