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/pagemap.h>
26 #include <linux/smp_lock.h>
28 typedef struct ext2_dir_entry_2 ext2_dirent;
31 * ext2 uses block-sized chunks. Arguably, sector-sized ones would be
32 * more robust, but we have what we have
34 static inline unsigned ext2_chunk_size(struct inode *inode)
36 return inode->i_sb->s_blocksize;
39 static inline void ext2_put_page(struct page *page)
42 page_cache_release(page);
45 static inline unsigned long dir_pages(struct inode *inode)
47 return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
51 * Return the offset into page `page_nr' of the last valid
52 * byte in that page, plus one.
55 ext2_last_byte(struct inode *inode, unsigned long page_nr)
57 unsigned last_byte = inode->i_size;
59 last_byte -= page_nr << PAGE_CACHE_SHIFT;
60 if (last_byte > PAGE_CACHE_SIZE)
61 last_byte = PAGE_CACHE_SIZE;
65 static int ext2_commit_chunk(struct page *page, unsigned from, unsigned to)
67 struct inode *dir = page->mapping->host;
70 page->mapping->a_ops->commit_write(NULL, page, from, to);
72 err = write_one_page(page, 1);
78 static void ext2_check_page(struct page *page)
80 struct inode *dir = page->mapping->host;
81 struct super_block *sb = dir->i_sb;
82 unsigned chunk_size = ext2_chunk_size(dir);
83 char *kaddr = page_address(page);
84 u32 max_inumber = le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count);
85 unsigned offs, rec_len;
86 unsigned limit = PAGE_CACHE_SIZE;
90 if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
91 limit = dir->i_size & ~PAGE_CACHE_MASK;
92 if (limit & (chunk_size - 1))
97 for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
98 p = (ext2_dirent *)(kaddr + offs);
99 rec_len = le16_to_cpu(p->rec_len);
101 if (rec_len < EXT2_DIR_REC_LEN(1))
105 if (rec_len < EXT2_DIR_REC_LEN(p->name_len))
107 if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
109 if (le32_to_cpu(p->inode) > max_inumber)
115 SetPageChecked(page);
118 /* Too bad, we had an error */
121 ext2_error(sb, "ext2_check_page",
122 "size of directory #%lu is not a multiple of chunk size",
127 error = "rec_len is smaller than minimal";
130 error = "unaligned directory entry";
133 error = "rec_len is too small for name_len";
136 error = "directory entry across blocks";
139 error = "inode out of bounds";
141 ext2_error (sb, "ext2_check_page", "bad entry in directory #%lu: %s - "
142 "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
143 dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT)+offs,
144 (unsigned long) le32_to_cpu(p->inode),
145 rec_len, p->name_len);
148 p = (ext2_dirent *)(kaddr + offs);
149 ext2_error (sb, "ext2_check_page",
150 "entry in directory #%lu spans the page boundary"
151 "offset=%lu, inode=%lu",
152 dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs,
153 (unsigned long) le32_to_cpu(p->inode));
155 SetPageChecked(page);
159 static struct page * ext2_get_page(struct inode *dir, unsigned long n)
161 struct address_space *mapping = dir->i_mapping;
162 struct page *page = read_mapping_page(mapping, n, NULL);
165 if (!PageChecked(page))
166 ext2_check_page(page);
174 return ERR_PTR(-EIO);
178 * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure.
180 * len <= EXT2_NAME_LEN and de != NULL are guaranteed by caller.
182 static inline int ext2_match (int len, const char * const name,
183 struct ext2_dir_entry_2 * de)
185 if (len != de->name_len)
189 return !memcmp(name, de->name, len);
193 * p is at least 6 bytes before the end of page
195 static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
197 return (ext2_dirent *)((char*)p + le16_to_cpu(p->rec_len));
200 static inline unsigned
201 ext2_validate_entry(char *base, unsigned offset, unsigned mask)
203 ext2_dirent *de = (ext2_dirent*)(base + offset);
204 ext2_dirent *p = (ext2_dirent*)(base + (offset&mask));
205 while ((char*)p < (char*)de) {
208 p = ext2_next_entry(p);
210 return (char *)p - base;
213 static unsigned char ext2_filetype_table[EXT2_FT_MAX] = {
214 [EXT2_FT_UNKNOWN] = DT_UNKNOWN,
215 [EXT2_FT_REG_FILE] = DT_REG,
216 [EXT2_FT_DIR] = DT_DIR,
217 [EXT2_FT_CHRDEV] = DT_CHR,
218 [EXT2_FT_BLKDEV] = DT_BLK,
219 [EXT2_FT_FIFO] = DT_FIFO,
220 [EXT2_FT_SOCK] = DT_SOCK,
221 [EXT2_FT_SYMLINK] = DT_LNK,
225 static unsigned char ext2_type_by_mode[S_IFMT >> S_SHIFT] = {
226 [S_IFREG >> S_SHIFT] = EXT2_FT_REG_FILE,
227 [S_IFDIR >> S_SHIFT] = EXT2_FT_DIR,
228 [S_IFCHR >> S_SHIFT] = EXT2_FT_CHRDEV,
229 [S_IFBLK >> S_SHIFT] = EXT2_FT_BLKDEV,
230 [S_IFIFO >> S_SHIFT] = EXT2_FT_FIFO,
231 [S_IFSOCK >> S_SHIFT] = EXT2_FT_SOCK,
232 [S_IFLNK >> S_SHIFT] = EXT2_FT_SYMLINK,
235 static inline void ext2_set_de_type(ext2_dirent *de, struct inode *inode)
237 mode_t mode = inode->i_mode;
238 if (EXT2_HAS_INCOMPAT_FEATURE(inode->i_sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
239 de->file_type = ext2_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
245 ext2_readdir (struct file * filp, void * dirent, filldir_t filldir)
247 loff_t pos = filp->f_pos;
248 struct inode *inode = filp->f_path.dentry->d_inode;
249 struct super_block *sb = inode->i_sb;
250 unsigned int offset = pos & ~PAGE_CACHE_MASK;
251 unsigned long n = pos >> PAGE_CACHE_SHIFT;
252 unsigned long npages = dir_pages(inode);
253 unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
254 unsigned char *types = NULL;
255 int need_revalidate = filp->f_version != inode->i_version;
257 if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
260 if (EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
261 types = ext2_filetype_table;
263 for ( ; n < npages; n++, offset = 0) {
266 struct page *page = ext2_get_page(inode, n);
269 ext2_error(sb, __FUNCTION__,
272 filp->f_pos += PAGE_CACHE_SIZE - offset;
275 kaddr = page_address(page);
276 if (unlikely(need_revalidate)) {
278 offset = ext2_validate_entry(kaddr, offset, chunk_mask);
279 filp->f_pos = (n<<PAGE_CACHE_SHIFT) + offset;
281 filp->f_version = inode->i_version;
284 de = (ext2_dirent *)(kaddr+offset);
285 limit = kaddr + ext2_last_byte(inode, n) - EXT2_DIR_REC_LEN(1);
286 for ( ;(char*)de <= limit; de = ext2_next_entry(de)) {
287 if (de->rec_len == 0) {
288 ext2_error(sb, __FUNCTION__,
289 "zero-length directory entry");
295 unsigned char d_type = DT_UNKNOWN;
297 if (types && de->file_type < EXT2_FT_MAX)
298 d_type = types[de->file_type];
300 offset = (char *)de - kaddr;
301 over = filldir(dirent, de->name, de->name_len,
302 (n<<PAGE_CACHE_SHIFT) | offset,
303 le32_to_cpu(de->inode), d_type);
309 filp->f_pos += le16_to_cpu(de->rec_len);
319 * finds an entry in the specified directory with the wanted name. It
320 * returns the page in which the entry was found, and the entry itself
321 * (as a parameter - res_dir). Page is returned mapped and unlocked.
322 * Entry is guaranteed to be valid.
324 struct ext2_dir_entry_2 * ext2_find_entry (struct inode * dir,
325 struct dentry *dentry, struct page ** res_page)
327 const char *name = dentry->d_name.name;
328 int namelen = dentry->d_name.len;
329 unsigned reclen = EXT2_DIR_REC_LEN(namelen);
330 unsigned long start, n;
331 unsigned long npages = dir_pages(dir);
332 struct page *page = NULL;
333 struct ext2_inode_info *ei = EXT2_I(dir);
342 start = ei->i_dir_start_lookup;
348 page = ext2_get_page(dir, n);
350 kaddr = page_address(page);
351 de = (ext2_dirent *) kaddr;
352 kaddr += ext2_last_byte(dir, n) - reclen;
353 while ((char *) de <= kaddr) {
354 if (de->rec_len == 0) {
355 ext2_error(dir->i_sb, __FUNCTION__,
356 "zero-length directory entry");
360 if (ext2_match (namelen, name, de))
362 de = ext2_next_entry(de);
368 /* next page is past the blocks we've got */
369 if (unlikely(n > (dir->i_blocks >> (PAGE_CACHE_SHIFT - 9)))) {
370 ext2_error(dir->i_sb, __FUNCTION__,
371 "dir %lu size %lld exceeds block count %llu",
372 dir->i_ino, dir->i_size,
373 (unsigned long long)dir->i_blocks);
376 } while (n != start);
382 ei->i_dir_start_lookup = n;
386 struct ext2_dir_entry_2 * ext2_dotdot (struct inode *dir, struct page **p)
388 struct page *page = ext2_get_page(dir, 0);
389 ext2_dirent *de = NULL;
392 de = ext2_next_entry((ext2_dirent *) page_address(page));
398 ino_t ext2_inode_by_name(struct inode * dir, struct dentry *dentry)
401 struct ext2_dir_entry_2 * de;
404 de = ext2_find_entry (dir, dentry, &page);
406 res = le32_to_cpu(de->inode);
412 /* Releases the page */
413 void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
414 struct page *page, struct inode *inode)
416 unsigned from = (char *) de - (char *) page_address(page);
417 unsigned to = from + le16_to_cpu(de->rec_len);
421 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
423 de->inode = cpu_to_le32(inode->i_ino);
424 ext2_set_de_type (de, inode);
425 err = ext2_commit_chunk(page, from, to);
427 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
428 EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
429 mark_inode_dirty(dir);
435 int ext2_add_link (struct dentry *dentry, struct inode *inode)
437 struct inode *dir = dentry->d_parent->d_inode;
438 const char *name = dentry->d_name.name;
439 int namelen = dentry->d_name.len;
440 unsigned chunk_size = ext2_chunk_size(dir);
441 unsigned reclen = EXT2_DIR_REC_LEN(namelen);
442 unsigned short rec_len, name_len;
443 struct page *page = NULL;
445 unsigned long npages = dir_pages(dir);
452 * We take care of directory expansion in the same loop.
453 * This code plays outside i_size, so it locks the page
454 * to protect that region.
456 for (n = 0; n <= npages; n++) {
459 page = ext2_get_page(dir, n);
464 kaddr = page_address(page);
465 dir_end = kaddr + ext2_last_byte(dir, n);
466 de = (ext2_dirent *)kaddr;
467 kaddr += PAGE_CACHE_SIZE - reclen;
468 while ((char *)de <= kaddr) {
469 if ((char *)de == dir_end) {
472 rec_len = chunk_size;
473 de->rec_len = cpu_to_le16(chunk_size);
477 if (de->rec_len == 0) {
478 ext2_error(dir->i_sb, __FUNCTION__,
479 "zero-length directory entry");
484 if (ext2_match (namelen, name, de))
486 name_len = EXT2_DIR_REC_LEN(de->name_len);
487 rec_len = le16_to_cpu(de->rec_len);
488 if (!de->inode && rec_len >= reclen)
490 if (rec_len >= name_len + reclen)
492 de = (ext2_dirent *) ((char *) de + rec_len);
501 from = (char*)de - (char*)page_address(page);
503 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
507 ext2_dirent *de1 = (ext2_dirent *) ((char *) de + name_len);
508 de1->rec_len = cpu_to_le16(rec_len - name_len);
509 de->rec_len = cpu_to_le16(name_len);
512 de->name_len = namelen;
513 memcpy (de->name, name, namelen);
514 de->inode = cpu_to_le32(inode->i_ino);
515 ext2_set_de_type (de, inode);
516 err = ext2_commit_chunk(page, from, to);
517 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
518 EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
519 mark_inode_dirty(dir);
531 * ext2_delete_entry deletes a directory entry by merging it with the
532 * previous entry. Page is up-to-date. Releases the page.
534 int ext2_delete_entry (struct ext2_dir_entry_2 * dir, struct page * page )
536 struct address_space *mapping = page->mapping;
537 struct inode *inode = mapping->host;
538 char *kaddr = page_address(page);
539 unsigned from = ((char*)dir - kaddr) & ~(ext2_chunk_size(inode)-1);
540 unsigned to = ((char*)dir - kaddr) + le16_to_cpu(dir->rec_len);
541 ext2_dirent * pde = NULL;
542 ext2_dirent * de = (ext2_dirent *) (kaddr + from);
545 while ((char*)de < (char*)dir) {
546 if (de->rec_len == 0) {
547 ext2_error(inode->i_sb, __FUNCTION__,
548 "zero-length directory entry");
553 de = ext2_next_entry(de);
556 from = (char*)pde - (char*)page_address(page);
558 err = mapping->a_ops->prepare_write(NULL, page, from, to);
561 pde->rec_len = cpu_to_le16(to-from);
563 err = ext2_commit_chunk(page, from, to);
564 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
565 EXT2_I(inode)->i_flags &= ~EXT2_BTREE_FL;
566 mark_inode_dirty(inode);
573 * Set the first fragment of directory.
575 int ext2_make_empty(struct inode *inode, struct inode *parent)
577 struct address_space *mapping = inode->i_mapping;
578 struct page *page = grab_cache_page(mapping, 0);
579 unsigned chunk_size = ext2_chunk_size(inode);
580 struct ext2_dir_entry_2 * de;
586 err = mapping->a_ops->prepare_write(NULL, page, 0, chunk_size);
591 kaddr = kmap_atomic(page, KM_USER0);
592 memset(kaddr, 0, chunk_size);
593 de = (struct ext2_dir_entry_2 *)kaddr;
595 de->rec_len = cpu_to_le16(EXT2_DIR_REC_LEN(1));
596 memcpy (de->name, ".\0\0", 4);
597 de->inode = cpu_to_le32(inode->i_ino);
598 ext2_set_de_type (de, inode);
600 de = (struct ext2_dir_entry_2 *)(kaddr + EXT2_DIR_REC_LEN(1));
602 de->rec_len = cpu_to_le16(chunk_size - EXT2_DIR_REC_LEN(1));
603 de->inode = cpu_to_le32(parent->i_ino);
604 memcpy (de->name, "..\0", 4);
605 ext2_set_de_type (de, inode);
606 kunmap_atomic(kaddr, KM_USER0);
607 err = ext2_commit_chunk(page, 0, chunk_size);
609 page_cache_release(page);
614 * routine to check that the specified directory is empty (for rmdir)
616 int ext2_empty_dir (struct inode * inode)
618 struct page *page = NULL;
619 unsigned long i, npages = dir_pages(inode);
621 for (i = 0; i < npages; i++) {
624 page = ext2_get_page(inode, i);
629 kaddr = page_address(page);
630 de = (ext2_dirent *)kaddr;
631 kaddr += ext2_last_byte(inode, i) - EXT2_DIR_REC_LEN(1);
633 while ((char *)de <= kaddr) {
634 if (de->rec_len == 0) {
635 ext2_error(inode->i_sb, __FUNCTION__,
636 "zero-length directory entry");
637 printk("kaddr=%p, de=%p\n", kaddr, de);
640 if (de->inode != 0) {
641 /* check for . and .. */
642 if (de->name[0] != '.')
644 if (de->name_len > 2)
646 if (de->name_len < 2) {
648 cpu_to_le32(inode->i_ino))
650 } else if (de->name[1] != '.')
653 de = ext2_next_entry(de);
664 const struct file_operations ext2_dir_operations = {
665 .llseek = generic_file_llseek,
666 .read = generic_read_dir,
667 .readdir = ext2_readdir,
670 .compat_ioctl = ext2_compat_ioctl,
672 .fsync = ext2_sync_file,