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
;
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
)
40 static inline __le16
ext2_rec_len_to_disk(unsigned len
)
43 return cpu_to_le16(EXT2_MAX_REC_LEN
);
44 else if (len
> (1 << 16))
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
)
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.
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
;
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
;
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
);
99 err
= write_one_page(page
, 1);
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
;
118 if ((dir
->i_size
>> PAGE_CACHE_SHIFT
) == page
->index
) {
119 limit
= dir
->i_size
& ~PAGE_CACHE_MASK
;
120 if (limit
& (chunk_size
- 1))
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))
133 if (rec_len
< EXT2_DIR_REC_LEN(p
->name_len
))
135 if (((offs
+ rec_len
- 1) ^ offs
) & ~(chunk_size
-1))
137 if (le32_to_cpu(p
->inode
) > max_inumber
)
143 SetPageChecked(page
);
146 /* Too bad, we had an error */
149 ext2_error(sb
, "ext2_check_page",
150 "size of directory #%lu is not a multiple of chunk size",
155 error
= "rec_len is smaller than minimal";
158 error
= "unaligned directory entry";
161 error
= "rec_len is too small for name_len";
164 error
= "directory entry across blocks";
167 error
= "inode out of bounds";
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
);
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
));
183 SetPageChecked(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
);
193 if (!PageChecked(page
))
194 ext2_check_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
)
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
) {
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
,
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
];
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))
289 if (EXT2_HAS_INCOMPAT_FEATURE(sb
, EXT2_FEATURE_INCOMPAT_FILETYPE
))
290 types
= ext2_filetype_table
;
292 for ( ; n
< npages
; n
++, offset
= 0) {
295 struct page
*page
= ext2_get_page(inode
, n
);
298 ext2_error(sb
, __FUNCTION__
,
301 filp
->f_pos
+= PAGE_CACHE_SIZE
- offset
;
304 kaddr
= page_address(page
);
305 if (unlikely(need_revalidate
)) {
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
;
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");
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
);
338 filp
->f_pos
+= ext2_rec_len_from_disk(de
->rec_len
);
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
);
371 start
= ei
->i_dir_start_lookup
;
377 page
= ext2_get_page(dir
, n
);
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");
389 if (ext2_match (namelen
, name
, de
))
391 de
= ext2_next_entry(de
);
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
);
405 } while (n
!= start
);
411 ei
->i_dir_start_lookup
= n
;
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
;
421 de
= ext2_next_entry((ext2_dirent
*) page_address(page
));
427 ino_t
ext2_inode_by_name(struct inode
* dir
, struct dentry
*dentry
)
430 struct ext2_dir_entry_2
* de
;
433 de
= ext2_find_entry (dir
, dentry
, &page
);
435 res
= le32_to_cpu(de
->inode
);
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
);
451 err
= __ext2_write_begin(NULL
, page
->mapping
, pos
, len
,
452 AOP_FLAG_UNINTERRUPTIBLE
, &page
, NULL
);
454 de
->inode
= cpu_to_le32(inode
->i_ino
);
455 ext2_set_de_type(de
, inode
);
456 err
= ext2_commit_chunk(page
, pos
, len
);
458 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
459 EXT2_I(dir
)->i_flags
&= ~EXT2_BTREE_FL
;
460 mark_inode_dirty(dir
);
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
;
476 unsigned long npages
= dir_pages(dir
);
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
++) {
490 page
= ext2_get_page(dir
, n
);
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
) {
503 rec_len
= chunk_size
;
504 de
->rec_len
= ext2_rec_len_to_disk(chunk_size
);
508 if (de
->rec_len
== 0) {
509 ext2_error(dir
->i_sb
, __FUNCTION__
,
510 "zero-length directory entry");
515 if (ext2_match (namelen
, name
, de
))
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
)
521 if (rec_len
>= name_len
+ reclen
)
523 de
= (ext2_dirent
*) ((char *) de
+ rec_len
);
532 pos
= page_offset(page
) +
533 (char*)de
- (char*)page_address(page
);
534 err
= __ext2_write_begin(NULL
, page
->mapping
, pos
, rec_len
, 0,
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
);
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
);
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
);
575 ext2_dirent
* pde
= NULL
;
576 ext2_dirent
* de
= (ext2_dirent
*) (kaddr
+ from
);
579 while ((char*)de
< (char*)dir
) {
580 if (de
->rec_len
== 0) {
581 ext2_error(inode
->i_sb
, __FUNCTION__
,
582 "zero-length directory entry");
587 de
= ext2_next_entry(de
);
590 from
= (char*)pde
- (char*)page_address(page
);
591 pos
= page_offset(page
) + from
;
593 err
= __ext2_write_begin(NULL
, page
->mapping
, pos
, to
- from
, 0,
597 pde
->rec_len
= ext2_rec_len_to_disk(to
- from
);
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
);
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
;
623 err
= __ext2_write_begin(NULL
, page
->mapping
, 0, chunk_size
, 0,
629 kaddr
= kmap_atomic(page
, KM_USER0
);
630 memset(kaddr
, 0, chunk_size
);
631 de
= (struct ext2_dir_entry_2
*)kaddr
;
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));
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
);
647 page_cache_release(page
);
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
++) {
662 page
= ext2_get_page(inode
, i
);
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
);
678 if (de
->inode
!= 0) {
679 /* check for . and .. */
680 if (de
->name
[0] != '.')
682 if (de
->name_len
> 2)
684 if (de
->name_len
< 2) {
686 cpu_to_le32(inode
->i_ino
))
688 } else if (de
->name
[1] != '.')
691 de
= ext2_next_entry(de
);
702 const struct file_operations ext2_dir_operations
= {
703 .llseek
= generic_file_llseek
,
704 .read
= generic_read_dir
,
705 .readdir
= ext2_readdir
,
706 .unlocked_ioctl
= ext2_ioctl
,
708 .compat_ioctl
= ext2_compat_ioctl
,
710 .fsync
= ext2_sync_file
,