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 * Tests against MAX_REC_LEN etc were put in place for 64k block
33 * sizes; if that is not possible on this arch, we can skip
34 * those tests and speed things up.
36 static inline unsigned ext2_rec_len_from_disk(__le16 dlen
)
38 unsigned len
= le16_to_cpu(dlen
);
40 #if (PAGE_CACHE_SIZE >= 65536)
41 if (len
== EXT2_MAX_REC_LEN
)
47 static inline __le16
ext2_rec_len_to_disk(unsigned len
)
49 #if (PAGE_CACHE_SIZE >= 65536)
51 return cpu_to_le16(EXT2_MAX_REC_LEN
);
53 BUG_ON(len
> (1 << 16));
55 return cpu_to_le16(len
);
59 * ext2 uses block-sized chunks. Arguably, sector-sized ones would be
60 * more robust, but we have what we have
62 static inline unsigned ext2_chunk_size(struct inode
*inode
)
64 return inode
->i_sb
->s_blocksize
;
67 static inline void ext2_put_page(struct page
*page
)
70 page_cache_release(page
);
73 static inline unsigned long dir_pages(struct inode
*inode
)
75 return (inode
->i_size
+PAGE_CACHE_SIZE
-1)>>PAGE_CACHE_SHIFT
;
79 * Return the offset into page `page_nr' of the last valid
80 * byte in that page, plus one.
83 ext2_last_byte(struct inode
*inode
, unsigned long page_nr
)
85 unsigned last_byte
= inode
->i_size
;
87 last_byte
-= page_nr
<< PAGE_CACHE_SHIFT
;
88 if (last_byte
> PAGE_CACHE_SIZE
)
89 last_byte
= PAGE_CACHE_SIZE
;
93 static int ext2_commit_chunk(struct page
*page
, loff_t pos
, unsigned len
)
95 struct address_space
*mapping
= page
->mapping
;
96 struct inode
*dir
= mapping
->host
;
100 block_write_end(NULL
, mapping
, pos
, len
, len
, page
, NULL
);
102 if (pos
+len
> dir
->i_size
) {
103 i_size_write(dir
, pos
+len
);
104 mark_inode_dirty(dir
);
107 if (IS_DIRSYNC(dir
)) {
108 err
= write_one_page(page
, 1);
110 err
= sync_inode_metadata(dir
, 1);
118 static void ext2_check_page(struct page
*page
, int quiet
)
120 struct inode
*dir
= page
->mapping
->host
;
121 struct super_block
*sb
= dir
->i_sb
;
122 unsigned chunk_size
= ext2_chunk_size(dir
);
123 char *kaddr
= page_address(page
);
124 u32 max_inumber
= le32_to_cpu(EXT2_SB(sb
)->s_es
->s_inodes_count
);
125 unsigned offs
, rec_len
;
126 unsigned limit
= PAGE_CACHE_SIZE
;
130 if ((dir
->i_size
>> PAGE_CACHE_SHIFT
) == page
->index
) {
131 limit
= dir
->i_size
& ~PAGE_CACHE_MASK
;
132 if (limit
& (chunk_size
- 1))
137 for (offs
= 0; offs
<= limit
- EXT2_DIR_REC_LEN(1); offs
+= rec_len
) {
138 p
= (ext2_dirent
*)(kaddr
+ offs
);
139 rec_len
= ext2_rec_len_from_disk(p
->rec_len
);
141 if (unlikely(rec_len
< EXT2_DIR_REC_LEN(1)))
143 if (unlikely(rec_len
& 3))
145 if (unlikely(rec_len
< EXT2_DIR_REC_LEN(p
->name_len
)))
147 if (unlikely(((offs
+ rec_len
- 1) ^ offs
) & ~(chunk_size
-1)))
149 if (unlikely(le32_to_cpu(p
->inode
) > max_inumber
))
155 SetPageChecked(page
);
158 /* Too bad, we had an error */
162 ext2_error(sb
, __func__
,
163 "size of directory #%lu is not a multiple "
164 "of chunk size", dir
->i_ino
);
167 error
= "rec_len is smaller than minimal";
170 error
= "unaligned directory entry";
173 error
= "rec_len is too small for name_len";
176 error
= "directory entry across blocks";
179 error
= "inode out of bounds";
182 ext2_error(sb
, __func__
, "bad entry in directory #%lu: : %s - "
183 "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
184 dir
->i_ino
, error
, (page
->index
<<PAGE_CACHE_SHIFT
)+offs
,
185 (unsigned long) le32_to_cpu(p
->inode
),
186 rec_len
, p
->name_len
);
190 p
= (ext2_dirent
*)(kaddr
+ offs
);
191 ext2_error(sb
, "ext2_check_page",
192 "entry in directory #%lu spans the page boundary"
193 "offset=%lu, inode=%lu",
194 dir
->i_ino
, (page
->index
<<PAGE_CACHE_SHIFT
)+offs
,
195 (unsigned long) le32_to_cpu(p
->inode
));
198 SetPageChecked(page
);
202 static struct page
* ext2_get_page(struct inode
*dir
, unsigned long n
,
205 struct address_space
*mapping
= dir
->i_mapping
;
206 struct page
*page
= read_mapping_page(mapping
, n
, NULL
);
209 if (!PageChecked(page
))
210 ext2_check_page(page
, quiet
);
218 return ERR_PTR(-EIO
);
222 * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure.
224 * len <= EXT2_NAME_LEN and de != NULL are guaranteed by caller.
226 static inline int ext2_match (int len
, const char * const name
,
227 struct ext2_dir_entry_2
* de
)
229 if (len
!= de
->name_len
)
233 return !memcmp(name
, de
->name
, len
);
237 * p is at least 6 bytes before the end of page
239 static inline ext2_dirent
*ext2_next_entry(ext2_dirent
*p
)
241 return (ext2_dirent
*)((char *)p
+
242 ext2_rec_len_from_disk(p
->rec_len
));
245 static inline unsigned
246 ext2_validate_entry(char *base
, unsigned offset
, unsigned mask
)
248 ext2_dirent
*de
= (ext2_dirent
*)(base
+ offset
);
249 ext2_dirent
*p
= (ext2_dirent
*)(base
+ (offset
&mask
));
250 while ((char*)p
< (char*)de
) {
253 p
= ext2_next_entry(p
);
255 return (char *)p
- base
;
258 static unsigned char ext2_filetype_table
[EXT2_FT_MAX
] = {
259 [EXT2_FT_UNKNOWN
] = DT_UNKNOWN
,
260 [EXT2_FT_REG_FILE
] = DT_REG
,
261 [EXT2_FT_DIR
] = DT_DIR
,
262 [EXT2_FT_CHRDEV
] = DT_CHR
,
263 [EXT2_FT_BLKDEV
] = DT_BLK
,
264 [EXT2_FT_FIFO
] = DT_FIFO
,
265 [EXT2_FT_SOCK
] = DT_SOCK
,
266 [EXT2_FT_SYMLINK
] = DT_LNK
,
270 static unsigned char ext2_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
271 [S_IFREG
>> S_SHIFT
] = EXT2_FT_REG_FILE
,
272 [S_IFDIR
>> S_SHIFT
] = EXT2_FT_DIR
,
273 [S_IFCHR
>> S_SHIFT
] = EXT2_FT_CHRDEV
,
274 [S_IFBLK
>> S_SHIFT
] = EXT2_FT_BLKDEV
,
275 [S_IFIFO
>> S_SHIFT
] = EXT2_FT_FIFO
,
276 [S_IFSOCK
>> S_SHIFT
] = EXT2_FT_SOCK
,
277 [S_IFLNK
>> S_SHIFT
] = EXT2_FT_SYMLINK
,
280 static inline void ext2_set_de_type(ext2_dirent
*de
, struct inode
*inode
)
282 mode_t mode
= inode
->i_mode
;
283 if (EXT2_HAS_INCOMPAT_FEATURE(inode
->i_sb
, EXT2_FEATURE_INCOMPAT_FILETYPE
))
284 de
->file_type
= ext2_type_by_mode
[(mode
& S_IFMT
)>>S_SHIFT
];
290 ext2_readdir (struct file
* filp
, void * dirent
, filldir_t filldir
)
292 loff_t pos
= filp
->f_pos
;
293 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
294 struct super_block
*sb
= inode
->i_sb
;
295 unsigned int offset
= pos
& ~PAGE_CACHE_MASK
;
296 unsigned long n
= pos
>> PAGE_CACHE_SHIFT
;
297 unsigned long npages
= dir_pages(inode
);
298 unsigned chunk_mask
= ~(ext2_chunk_size(inode
)-1);
299 unsigned char *types
= NULL
;
300 int need_revalidate
= filp
->f_version
!= inode
->i_version
;
302 if (pos
> inode
->i_size
- EXT2_DIR_REC_LEN(1))
305 if (EXT2_HAS_INCOMPAT_FEATURE(sb
, EXT2_FEATURE_INCOMPAT_FILETYPE
))
306 types
= ext2_filetype_table
;
308 for ( ; n
< npages
; n
++, offset
= 0) {
311 struct page
*page
= ext2_get_page(inode
, n
, 0);
314 ext2_error(sb
, __func__
,
317 filp
->f_pos
+= PAGE_CACHE_SIZE
- offset
;
318 return PTR_ERR(page
);
320 kaddr
= page_address(page
);
321 if (unlikely(need_revalidate
)) {
323 offset
= ext2_validate_entry(kaddr
, offset
, chunk_mask
);
324 filp
->f_pos
= (n
<<PAGE_CACHE_SHIFT
) + offset
;
326 filp
->f_version
= inode
->i_version
;
329 de
= (ext2_dirent
*)(kaddr
+offset
);
330 limit
= kaddr
+ ext2_last_byte(inode
, n
) - EXT2_DIR_REC_LEN(1);
331 for ( ;(char*)de
<= limit
; de
= ext2_next_entry(de
)) {
332 if (de
->rec_len
== 0) {
333 ext2_error(sb
, __func__
,
334 "zero-length directory entry");
340 unsigned char d_type
= DT_UNKNOWN
;
342 if (types
&& de
->file_type
< EXT2_FT_MAX
)
343 d_type
= types
[de
->file_type
];
345 offset
= (char *)de
- kaddr
;
346 over
= filldir(dirent
, de
->name
, de
->name_len
,
347 (n
<<PAGE_CACHE_SHIFT
) | offset
,
348 le32_to_cpu(de
->inode
), d_type
);
354 filp
->f_pos
+= ext2_rec_len_from_disk(de
->rec_len
);
364 * finds an entry in the specified directory with the wanted name. It
365 * returns the page in which the entry was found (as a parameter - res_page),
366 * and the entry itself. Page is returned mapped and unlocked.
367 * Entry is guaranteed to be valid.
369 struct ext2_dir_entry_2
*ext2_find_entry (struct inode
* dir
,
370 struct qstr
*child
, struct page
** res_page
)
372 const char *name
= child
->name
;
373 int namelen
= child
->len
;
374 unsigned reclen
= EXT2_DIR_REC_LEN(namelen
);
375 unsigned long start
, n
;
376 unsigned long npages
= dir_pages(dir
);
377 struct page
*page
= NULL
;
378 struct ext2_inode_info
*ei
= EXT2_I(dir
);
380 int dir_has_error
= 0;
388 start
= ei
->i_dir_start_lookup
;
394 page
= ext2_get_page(dir
, n
, dir_has_error
);
396 kaddr
= page_address(page
);
397 de
= (ext2_dirent
*) kaddr
;
398 kaddr
+= ext2_last_byte(dir
, n
) - reclen
;
399 while ((char *) de
<= kaddr
) {
400 if (de
->rec_len
== 0) {
401 ext2_error(dir
->i_sb
, __func__
,
402 "zero-length directory entry");
406 if (ext2_match (namelen
, name
, de
))
408 de
= ext2_next_entry(de
);
416 /* next page is past the blocks we've got */
417 if (unlikely(n
> (dir
->i_blocks
>> (PAGE_CACHE_SHIFT
- 9)))) {
418 ext2_error(dir
->i_sb
, __func__
,
419 "dir %lu size %lld exceeds block count %llu",
420 dir
->i_ino
, dir
->i_size
,
421 (unsigned long long)dir
->i_blocks
);
424 } while (n
!= start
);
430 ei
->i_dir_start_lookup
= n
;
434 struct ext2_dir_entry_2
* ext2_dotdot (struct inode
*dir
, struct page
**p
)
436 struct page
*page
= ext2_get_page(dir
, 0, 0);
437 ext2_dirent
*de
= NULL
;
440 de
= ext2_next_entry((ext2_dirent
*) page_address(page
));
446 ino_t
ext2_inode_by_name(struct inode
*dir
, struct qstr
*child
)
449 struct ext2_dir_entry_2
*de
;
452 de
= ext2_find_entry (dir
, child
, &page
);
454 res
= le32_to_cpu(de
->inode
);
460 static int ext2_prepare_chunk(struct page
*page
, loff_t pos
, unsigned len
)
462 return __block_write_begin(page
, pos
, len
, ext2_get_block
);
465 /* Releases the page */
466 void ext2_set_link(struct inode
*dir
, struct ext2_dir_entry_2
*de
,
467 struct page
*page
, struct inode
*inode
, int update_times
)
469 loff_t pos
= page_offset(page
) +
470 (char *) de
- (char *) page_address(page
);
471 unsigned len
= ext2_rec_len_from_disk(de
->rec_len
);
475 err
= ext2_prepare_chunk(page
, pos
, len
);
477 de
->inode
= cpu_to_le32(inode
->i_ino
);
478 ext2_set_de_type(de
, inode
);
479 err
= ext2_commit_chunk(page
, pos
, len
);
482 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
483 EXT2_I(dir
)->i_flags
&= ~EXT2_BTREE_FL
;
484 mark_inode_dirty(dir
);
490 int ext2_add_link (struct dentry
*dentry
, struct inode
*inode
)
492 struct inode
*dir
= dentry
->d_parent
->d_inode
;
493 const char *name
= dentry
->d_name
.name
;
494 int namelen
= dentry
->d_name
.len
;
495 unsigned chunk_size
= ext2_chunk_size(dir
);
496 unsigned reclen
= EXT2_DIR_REC_LEN(namelen
);
497 unsigned short rec_len
, name_len
;
498 struct page
*page
= NULL
;
500 unsigned long npages
= dir_pages(dir
);
507 * We take care of directory expansion in the same loop.
508 * This code plays outside i_size, so it locks the page
509 * to protect that region.
511 for (n
= 0; n
<= npages
; n
++) {
514 page
= ext2_get_page(dir
, n
, 0);
519 kaddr
= page_address(page
);
520 dir_end
= kaddr
+ ext2_last_byte(dir
, n
);
521 de
= (ext2_dirent
*)kaddr
;
522 kaddr
+= PAGE_CACHE_SIZE
- reclen
;
523 while ((char *)de
<= kaddr
) {
524 if ((char *)de
== dir_end
) {
527 rec_len
= chunk_size
;
528 de
->rec_len
= ext2_rec_len_to_disk(chunk_size
);
532 if (de
->rec_len
== 0) {
533 ext2_error(dir
->i_sb
, __func__
,
534 "zero-length directory entry");
539 if (ext2_match (namelen
, name
, de
))
541 name_len
= EXT2_DIR_REC_LEN(de
->name_len
);
542 rec_len
= ext2_rec_len_from_disk(de
->rec_len
);
543 if (!de
->inode
&& rec_len
>= reclen
)
545 if (rec_len
>= name_len
+ reclen
)
547 de
= (ext2_dirent
*) ((char *) de
+ rec_len
);
556 pos
= page_offset(page
) +
557 (char*)de
- (char*)page_address(page
);
558 err
= ext2_prepare_chunk(page
, pos
, rec_len
);
562 ext2_dirent
*de1
= (ext2_dirent
*) ((char *) de
+ name_len
);
563 de1
->rec_len
= ext2_rec_len_to_disk(rec_len
- name_len
);
564 de
->rec_len
= ext2_rec_len_to_disk(name_len
);
567 de
->name_len
= namelen
;
568 memcpy(de
->name
, name
, namelen
);
569 de
->inode
= cpu_to_le32(inode
->i_ino
);
570 ext2_set_de_type (de
, inode
);
571 err
= ext2_commit_chunk(page
, pos
, rec_len
);
572 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
573 EXT2_I(dir
)->i_flags
&= ~EXT2_BTREE_FL
;
574 mark_inode_dirty(dir
);
586 * ext2_delete_entry deletes a directory entry by merging it with the
587 * previous entry. Page is up-to-date. Releases the page.
589 int ext2_delete_entry (struct ext2_dir_entry_2
* dir
, struct page
* page
)
591 struct inode
*inode
= page
->mapping
->host
;
592 char *kaddr
= page_address(page
);
593 unsigned from
= ((char*)dir
- kaddr
) & ~(ext2_chunk_size(inode
)-1);
594 unsigned to
= ((char *)dir
- kaddr
) +
595 ext2_rec_len_from_disk(dir
->rec_len
);
597 ext2_dirent
* pde
= NULL
;
598 ext2_dirent
* de
= (ext2_dirent
*) (kaddr
+ from
);
601 while ((char*)de
< (char*)dir
) {
602 if (de
->rec_len
== 0) {
603 ext2_error(inode
->i_sb
, __func__
,
604 "zero-length directory entry");
609 de
= ext2_next_entry(de
);
612 from
= (char*)pde
- (char*)page_address(page
);
613 pos
= page_offset(page
) + from
;
615 err
= ext2_prepare_chunk(page
, pos
, to
- from
);
618 pde
->rec_len
= ext2_rec_len_to_disk(to
- from
);
620 err
= ext2_commit_chunk(page
, pos
, to
- from
);
621 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME_SEC
;
622 EXT2_I(inode
)->i_flags
&= ~EXT2_BTREE_FL
;
623 mark_inode_dirty(inode
);
630 * Set the first fragment of directory.
632 int ext2_make_empty(struct inode
*inode
, struct inode
*parent
)
634 struct page
*page
= grab_cache_page(inode
->i_mapping
, 0);
635 unsigned chunk_size
= ext2_chunk_size(inode
);
636 struct ext2_dir_entry_2
* de
;
643 err
= ext2_prepare_chunk(page
, 0, chunk_size
);
648 kaddr
= kmap_atomic(page
, KM_USER0
);
649 memset(kaddr
, 0, chunk_size
);
650 de
= (struct ext2_dir_entry_2
*)kaddr
;
652 de
->rec_len
= ext2_rec_len_to_disk(EXT2_DIR_REC_LEN(1));
653 memcpy (de
->name
, ".\0\0", 4);
654 de
->inode
= cpu_to_le32(inode
->i_ino
);
655 ext2_set_de_type (de
, inode
);
657 de
= (struct ext2_dir_entry_2
*)(kaddr
+ EXT2_DIR_REC_LEN(1));
659 de
->rec_len
= ext2_rec_len_to_disk(chunk_size
- EXT2_DIR_REC_LEN(1));
660 de
->inode
= cpu_to_le32(parent
->i_ino
);
661 memcpy (de
->name
, "..\0", 4);
662 ext2_set_de_type (de
, inode
);
663 kunmap_atomic(kaddr
, KM_USER0
);
664 err
= ext2_commit_chunk(page
, 0, chunk_size
);
666 page_cache_release(page
);
671 * routine to check that the specified directory is empty (for rmdir)
673 int ext2_empty_dir (struct inode
* inode
)
675 struct page
*page
= NULL
;
676 unsigned long i
, npages
= dir_pages(inode
);
677 int dir_has_error
= 0;
679 for (i
= 0; i
< npages
; i
++) {
682 page
= ext2_get_page(inode
, i
, dir_has_error
);
689 kaddr
= page_address(page
);
690 de
= (ext2_dirent
*)kaddr
;
691 kaddr
+= ext2_last_byte(inode
, i
) - EXT2_DIR_REC_LEN(1);
693 while ((char *)de
<= kaddr
) {
694 if (de
->rec_len
== 0) {
695 ext2_error(inode
->i_sb
, __func__
,
696 "zero-length directory entry");
697 printk("kaddr=%p, de=%p\n", kaddr
, de
);
700 if (de
->inode
!= 0) {
701 /* check for . and .. */
702 if (de
->name
[0] != '.')
704 if (de
->name_len
> 2)
706 if (de
->name_len
< 2) {
708 cpu_to_le32(inode
->i_ino
))
710 } else if (de
->name
[1] != '.')
713 de
= ext2_next_entry(de
);
724 const struct file_operations ext2_dir_operations
= {
725 .llseek
= generic_file_llseek
,
726 .read
= generic_read_dir
,
727 .readdir
= ext2_readdir
,
728 .unlocked_ioctl
= ext2_ioctl
,
730 .compat_ioctl
= ext2_compat_ioctl
,