1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1992, 1993, 1994, 1995
6 * Remy Card (card@masi.ibp.fr)
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)
12 * linux/fs/minix/dir.c
14 * Copyright (C) 1991, 1992 Linus Torvalds
16 * ext2 directory handling functions
18 * Big-endian to little-endian byte-swapping/bitmaps by
19 * David S. Miller (davem@caip.rutgers.edu), 1995
21 * All code that works with directory layout had been switched to pagecache
26 #include <linux/buffer_head.h>
27 #include <linux/pagemap.h>
28 #include <linux/swap.h>
29 #include <linux/iversion.h>
31 typedef struct ext2_dir_entry_2 ext2_dirent
;
34 * Tests against MAX_REC_LEN etc were put in place for 64k block
35 * sizes; if that is not possible on this arch, we can skip
36 * those tests and speed things up.
38 static inline unsigned ext2_rec_len_from_disk(__le16 dlen
)
40 unsigned len
= le16_to_cpu(dlen
);
42 #if (PAGE_SIZE >= 65536)
43 if (len
== EXT2_MAX_REC_LEN
)
49 static inline __le16
ext2_rec_len_to_disk(unsigned len
)
51 #if (PAGE_SIZE >= 65536)
53 return cpu_to_le16(EXT2_MAX_REC_LEN
);
55 BUG_ON(len
> (1 << 16));
57 return cpu_to_le16(len
);
61 * ext2 uses block-sized chunks. Arguably, sector-sized ones would be
62 * more robust, but we have what we have
64 static inline unsigned ext2_chunk_size(struct inode
*inode
)
66 return inode
->i_sb
->s_blocksize
;
69 static inline void ext2_put_page(struct page
*page
)
76 * Return the offset into page `page_nr' of the last valid
77 * byte in that page, plus one.
80 ext2_last_byte(struct inode
*inode
, unsigned long page_nr
)
82 unsigned last_byte
= inode
->i_size
;
84 last_byte
-= page_nr
<< PAGE_SHIFT
;
85 if (last_byte
> PAGE_SIZE
)
86 last_byte
= PAGE_SIZE
;
90 static int ext2_commit_chunk(struct page
*page
, loff_t pos
, unsigned len
)
92 struct address_space
*mapping
= page
->mapping
;
93 struct inode
*dir
= mapping
->host
;
96 inode_inc_iversion(dir
);
97 block_write_end(NULL
, mapping
, pos
, len
, len
, page
, NULL
);
99 if (pos
+len
> dir
->i_size
) {
100 i_size_write(dir
, pos
+len
);
101 mark_inode_dirty(dir
);
104 if (IS_DIRSYNC(dir
)) {
105 err
= write_one_page(page
);
107 err
= sync_inode_metadata(dir
, 1);
115 static bool ext2_check_page(struct page
*page
, int quiet
)
117 struct inode
*dir
= page
->mapping
->host
;
118 struct super_block
*sb
= dir
->i_sb
;
119 unsigned chunk_size
= ext2_chunk_size(dir
);
120 char *kaddr
= page_address(page
);
121 u32 max_inumber
= le32_to_cpu(EXT2_SB(sb
)->s_es
->s_inodes_count
);
122 unsigned offs
, rec_len
;
123 unsigned limit
= PAGE_SIZE
;
127 if ((dir
->i_size
>> PAGE_SHIFT
) == page
->index
) {
128 limit
= dir
->i_size
& ~PAGE_MASK
;
129 if (limit
& (chunk_size
- 1))
134 for (offs
= 0; offs
<= limit
- EXT2_DIR_REC_LEN(1); offs
+= rec_len
) {
135 p
= (ext2_dirent
*)(kaddr
+ offs
);
136 rec_len
= ext2_rec_len_from_disk(p
->rec_len
);
138 if (unlikely(rec_len
< EXT2_DIR_REC_LEN(1)))
140 if (unlikely(rec_len
& 3))
142 if (unlikely(rec_len
< EXT2_DIR_REC_LEN(p
->name_len
)))
144 if (unlikely(((offs
+ rec_len
- 1) ^ offs
) & ~(chunk_size
-1)))
146 if (unlikely(le32_to_cpu(p
->inode
) > max_inumber
))
152 SetPageChecked(page
);
155 /* Too bad, we had an error */
159 ext2_error(sb
, __func__
,
160 "size of directory #%lu is not a multiple "
161 "of chunk size", dir
->i_ino
);
164 error
= "rec_len is smaller than minimal";
167 error
= "unaligned directory entry";
170 error
= "rec_len is too small for name_len";
173 error
= "directory entry across blocks";
176 error
= "inode out of bounds";
179 ext2_error(sb
, __func__
, "bad entry in directory #%lu: : %s - "
180 "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
181 dir
->i_ino
, error
, (page
->index
<<PAGE_SHIFT
)+offs
,
182 (unsigned long) le32_to_cpu(p
->inode
),
183 rec_len
, p
->name_len
);
187 p
= (ext2_dirent
*)(kaddr
+ offs
);
188 ext2_error(sb
, "ext2_check_page",
189 "entry in directory #%lu spans the page boundary"
190 "offset=%lu, inode=%lu",
191 dir
->i_ino
, (page
->index
<<PAGE_SHIFT
)+offs
,
192 (unsigned long) le32_to_cpu(p
->inode
));
199 static struct page
* ext2_get_page(struct inode
*dir
, unsigned long n
,
202 struct address_space
*mapping
= dir
->i_mapping
;
203 struct page
*page
= read_mapping_page(mapping
, n
, NULL
);
206 if (unlikely(!PageChecked(page
))) {
207 if (PageError(page
) || !ext2_check_page(page
, quiet
))
215 return ERR_PTR(-EIO
);
219 * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure.
221 * len <= EXT2_NAME_LEN and de != NULL are guaranteed by caller.
223 static inline int ext2_match (int len
, const char * const name
,
224 struct ext2_dir_entry_2
* de
)
226 if (len
!= de
->name_len
)
230 return !memcmp(name
, de
->name
, len
);
234 * p is at least 6 bytes before the end of page
236 static inline ext2_dirent
*ext2_next_entry(ext2_dirent
*p
)
238 return (ext2_dirent
*)((char *)p
+
239 ext2_rec_len_from_disk(p
->rec_len
));
242 static inline unsigned
243 ext2_validate_entry(char *base
, unsigned offset
, unsigned mask
)
245 ext2_dirent
*de
= (ext2_dirent
*)(base
+ offset
);
246 ext2_dirent
*p
= (ext2_dirent
*)(base
+ (offset
&mask
));
247 while ((char*)p
< (char*)de
) {
250 p
= ext2_next_entry(p
);
252 return (char *)p
- base
;
255 static unsigned char ext2_filetype_table
[EXT2_FT_MAX
] = {
256 [EXT2_FT_UNKNOWN
] = DT_UNKNOWN
,
257 [EXT2_FT_REG_FILE
] = DT_REG
,
258 [EXT2_FT_DIR
] = DT_DIR
,
259 [EXT2_FT_CHRDEV
] = DT_CHR
,
260 [EXT2_FT_BLKDEV
] = DT_BLK
,
261 [EXT2_FT_FIFO
] = DT_FIFO
,
262 [EXT2_FT_SOCK
] = DT_SOCK
,
263 [EXT2_FT_SYMLINK
] = DT_LNK
,
267 static unsigned char ext2_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
268 [S_IFREG
>> S_SHIFT
] = EXT2_FT_REG_FILE
,
269 [S_IFDIR
>> S_SHIFT
] = EXT2_FT_DIR
,
270 [S_IFCHR
>> S_SHIFT
] = EXT2_FT_CHRDEV
,
271 [S_IFBLK
>> S_SHIFT
] = EXT2_FT_BLKDEV
,
272 [S_IFIFO
>> S_SHIFT
] = EXT2_FT_FIFO
,
273 [S_IFSOCK
>> S_SHIFT
] = EXT2_FT_SOCK
,
274 [S_IFLNK
>> S_SHIFT
] = EXT2_FT_SYMLINK
,
277 static inline void ext2_set_de_type(ext2_dirent
*de
, struct inode
*inode
)
279 umode_t mode
= inode
->i_mode
;
280 if (EXT2_HAS_INCOMPAT_FEATURE(inode
->i_sb
, EXT2_FEATURE_INCOMPAT_FILETYPE
))
281 de
->file_type
= ext2_type_by_mode
[(mode
& S_IFMT
)>>S_SHIFT
];
287 ext2_readdir(struct file
*file
, struct dir_context
*ctx
)
289 loff_t pos
= ctx
->pos
;
290 struct inode
*inode
= file_inode(file
);
291 struct super_block
*sb
= inode
->i_sb
;
292 unsigned int offset
= pos
& ~PAGE_MASK
;
293 unsigned long n
= pos
>> PAGE_SHIFT
;
294 unsigned long npages
= dir_pages(inode
);
295 unsigned chunk_mask
= ~(ext2_chunk_size(inode
)-1);
296 unsigned char *types
= NULL
;
297 bool need_revalidate
= !inode_eq_iversion(inode
, file
->f_version
);
299 if (pos
> inode
->i_size
- EXT2_DIR_REC_LEN(1))
302 if (EXT2_HAS_INCOMPAT_FEATURE(sb
, EXT2_FEATURE_INCOMPAT_FILETYPE
))
303 types
= ext2_filetype_table
;
305 for ( ; n
< npages
; n
++, offset
= 0) {
308 struct page
*page
= ext2_get_page(inode
, n
, 0);
311 ext2_error(sb
, __func__
,
314 ctx
->pos
+= PAGE_SIZE
- offset
;
315 return PTR_ERR(page
);
317 kaddr
= page_address(page
);
318 if (unlikely(need_revalidate
)) {
320 offset
= ext2_validate_entry(kaddr
, offset
, chunk_mask
);
321 ctx
->pos
= (n
<<PAGE_SHIFT
) + offset
;
323 file
->f_version
= inode_query_iversion(inode
);
324 need_revalidate
= false;
326 de
= (ext2_dirent
*)(kaddr
+offset
);
327 limit
= kaddr
+ ext2_last_byte(inode
, n
) - EXT2_DIR_REC_LEN(1);
328 for ( ;(char*)de
<= limit
; de
= ext2_next_entry(de
)) {
329 if (de
->rec_len
== 0) {
330 ext2_error(sb
, __func__
,
331 "zero-length directory entry");
336 unsigned char d_type
= DT_UNKNOWN
;
338 if (types
&& de
->file_type
< EXT2_FT_MAX
)
339 d_type
= types
[de
->file_type
];
341 if (!dir_emit(ctx
, de
->name
, de
->name_len
,
342 le32_to_cpu(de
->inode
),
348 ctx
->pos
+= ext2_rec_len_from_disk(de
->rec_len
);
358 * finds an entry in the specified directory with the wanted name. It
359 * returns the page in which the entry was found (as a parameter - res_page),
360 * and the entry itself. Page is returned mapped and unlocked.
361 * Entry is guaranteed to be valid.
363 struct ext2_dir_entry_2
*ext2_find_entry (struct inode
*dir
,
364 const struct qstr
*child
, struct page
**res_page
)
366 const char *name
= child
->name
;
367 int namelen
= child
->len
;
368 unsigned reclen
= EXT2_DIR_REC_LEN(namelen
);
369 unsigned long start
, n
;
370 unsigned long npages
= dir_pages(dir
);
371 struct page
*page
= NULL
;
372 struct ext2_inode_info
*ei
= EXT2_I(dir
);
374 int dir_has_error
= 0;
382 start
= ei
->i_dir_start_lookup
;
388 page
= ext2_get_page(dir
, n
, dir_has_error
);
390 kaddr
= page_address(page
);
391 de
= (ext2_dirent
*) kaddr
;
392 kaddr
+= ext2_last_byte(dir
, n
) - reclen
;
393 while ((char *) de
<= kaddr
) {
394 if (de
->rec_len
== 0) {
395 ext2_error(dir
->i_sb
, __func__
,
396 "zero-length directory entry");
400 if (ext2_match (namelen
, name
, de
))
402 de
= ext2_next_entry(de
);
410 /* next page is past the blocks we've got */
411 if (unlikely(n
> (dir
->i_blocks
>> (PAGE_SHIFT
- 9)))) {
412 ext2_error(dir
->i_sb
, __func__
,
413 "dir %lu size %lld exceeds block count %llu",
414 dir
->i_ino
, dir
->i_size
,
415 (unsigned long long)dir
->i_blocks
);
418 } while (n
!= start
);
424 ei
->i_dir_start_lookup
= n
;
428 struct ext2_dir_entry_2
* ext2_dotdot (struct inode
*dir
, struct page
**p
)
430 struct page
*page
= ext2_get_page(dir
, 0, 0);
431 ext2_dirent
*de
= NULL
;
434 de
= ext2_next_entry((ext2_dirent
*) page_address(page
));
440 ino_t
ext2_inode_by_name(struct inode
*dir
, const struct qstr
*child
)
443 struct ext2_dir_entry_2
*de
;
446 de
= ext2_find_entry (dir
, child
, &page
);
448 res
= le32_to_cpu(de
->inode
);
454 static int ext2_prepare_chunk(struct page
*page
, loff_t pos
, unsigned len
)
456 return __block_write_begin(page
, pos
, len
, ext2_get_block
);
459 /* Releases the page */
460 void ext2_set_link(struct inode
*dir
, struct ext2_dir_entry_2
*de
,
461 struct page
*page
, struct inode
*inode
, int update_times
)
463 loff_t pos
= page_offset(page
) +
464 (char *) de
- (char *) page_address(page
);
465 unsigned len
= ext2_rec_len_from_disk(de
->rec_len
);
469 err
= ext2_prepare_chunk(page
, pos
, len
);
471 de
->inode
= cpu_to_le32(inode
->i_ino
);
472 ext2_set_de_type(de
, inode
);
473 err
= ext2_commit_chunk(page
, pos
, len
);
476 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
477 EXT2_I(dir
)->i_flags
&= ~EXT2_BTREE_FL
;
478 mark_inode_dirty(dir
);
484 int ext2_add_link (struct dentry
*dentry
, struct inode
*inode
)
486 struct inode
*dir
= d_inode(dentry
->d_parent
);
487 const char *name
= dentry
->d_name
.name
;
488 int namelen
= dentry
->d_name
.len
;
489 unsigned chunk_size
= ext2_chunk_size(dir
);
490 unsigned reclen
= EXT2_DIR_REC_LEN(namelen
);
491 unsigned short rec_len
, name_len
;
492 struct page
*page
= NULL
;
494 unsigned long npages
= dir_pages(dir
);
501 * We take care of directory expansion in the same loop.
502 * This code plays outside i_size, so it locks the page
503 * to protect that region.
505 for (n
= 0; n
<= npages
; n
++) {
508 page
= ext2_get_page(dir
, n
, 0);
513 kaddr
= page_address(page
);
514 dir_end
= kaddr
+ ext2_last_byte(dir
, n
);
515 de
= (ext2_dirent
*)kaddr
;
516 kaddr
+= PAGE_SIZE
- reclen
;
517 while ((char *)de
<= kaddr
) {
518 if ((char *)de
== dir_end
) {
521 rec_len
= chunk_size
;
522 de
->rec_len
= ext2_rec_len_to_disk(chunk_size
);
526 if (de
->rec_len
== 0) {
527 ext2_error(dir
->i_sb
, __func__
,
528 "zero-length directory entry");
533 if (ext2_match (namelen
, name
, de
))
535 name_len
= EXT2_DIR_REC_LEN(de
->name_len
);
536 rec_len
= ext2_rec_len_from_disk(de
->rec_len
);
537 if (!de
->inode
&& rec_len
>= reclen
)
539 if (rec_len
>= name_len
+ reclen
)
541 de
= (ext2_dirent
*) ((char *) de
+ rec_len
);
550 pos
= page_offset(page
) +
551 (char*)de
- (char*)page_address(page
);
552 err
= ext2_prepare_chunk(page
, pos
, rec_len
);
556 ext2_dirent
*de1
= (ext2_dirent
*) ((char *) de
+ name_len
);
557 de1
->rec_len
= ext2_rec_len_to_disk(rec_len
- name_len
);
558 de
->rec_len
= ext2_rec_len_to_disk(name_len
);
561 de
->name_len
= namelen
;
562 memcpy(de
->name
, name
, namelen
);
563 de
->inode
= cpu_to_le32(inode
->i_ino
);
564 ext2_set_de_type (de
, inode
);
565 err
= ext2_commit_chunk(page
, pos
, rec_len
);
566 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
567 EXT2_I(dir
)->i_flags
&= ~EXT2_BTREE_FL
;
568 mark_inode_dirty(dir
);
580 * ext2_delete_entry deletes a directory entry by merging it with the
581 * previous entry. Page is up-to-date. Releases the page.
583 int ext2_delete_entry (struct ext2_dir_entry_2
* dir
, struct page
* page
)
585 struct inode
*inode
= page
->mapping
->host
;
586 char *kaddr
= page_address(page
);
587 unsigned from
= ((char*)dir
- kaddr
) & ~(ext2_chunk_size(inode
)-1);
588 unsigned to
= ((char *)dir
- kaddr
) +
589 ext2_rec_len_from_disk(dir
->rec_len
);
591 ext2_dirent
* pde
= NULL
;
592 ext2_dirent
* de
= (ext2_dirent
*) (kaddr
+ from
);
595 while ((char*)de
< (char*)dir
) {
596 if (de
->rec_len
== 0) {
597 ext2_error(inode
->i_sb
, __func__
,
598 "zero-length directory entry");
603 de
= ext2_next_entry(de
);
606 from
= (char*)pde
- (char*)page_address(page
);
607 pos
= page_offset(page
) + from
;
609 err
= ext2_prepare_chunk(page
, pos
, to
- from
);
612 pde
->rec_len
= ext2_rec_len_to_disk(to
- from
);
614 err
= ext2_commit_chunk(page
, pos
, to
- from
);
615 inode
->i_ctime
= inode
->i_mtime
= current_time(inode
);
616 EXT2_I(inode
)->i_flags
&= ~EXT2_BTREE_FL
;
617 mark_inode_dirty(inode
);
624 * Set the first fragment of directory.
626 int ext2_make_empty(struct inode
*inode
, struct inode
*parent
)
628 struct page
*page
= grab_cache_page(inode
->i_mapping
, 0);
629 unsigned chunk_size
= ext2_chunk_size(inode
);
630 struct ext2_dir_entry_2
* de
;
637 err
= ext2_prepare_chunk(page
, 0, chunk_size
);
642 kaddr
= kmap_atomic(page
);
643 memset(kaddr
, 0, chunk_size
);
644 de
= (struct ext2_dir_entry_2
*)kaddr
;
646 de
->rec_len
= ext2_rec_len_to_disk(EXT2_DIR_REC_LEN(1));
647 memcpy (de
->name
, ".\0\0", 4);
648 de
->inode
= cpu_to_le32(inode
->i_ino
);
649 ext2_set_de_type (de
, inode
);
651 de
= (struct ext2_dir_entry_2
*)(kaddr
+ EXT2_DIR_REC_LEN(1));
653 de
->rec_len
= ext2_rec_len_to_disk(chunk_size
- EXT2_DIR_REC_LEN(1));
654 de
->inode
= cpu_to_le32(parent
->i_ino
);
655 memcpy (de
->name
, "..\0", 4);
656 ext2_set_de_type (de
, inode
);
657 kunmap_atomic(kaddr
);
658 err
= ext2_commit_chunk(page
, 0, chunk_size
);
665 * routine to check that the specified directory is empty (for rmdir)
667 int ext2_empty_dir (struct inode
* inode
)
669 struct page
*page
= NULL
;
670 unsigned long i
, npages
= dir_pages(inode
);
671 int dir_has_error
= 0;
673 for (i
= 0; i
< npages
; i
++) {
676 page
= ext2_get_page(inode
, i
, dir_has_error
);
683 kaddr
= page_address(page
);
684 de
= (ext2_dirent
*)kaddr
;
685 kaddr
+= ext2_last_byte(inode
, i
) - EXT2_DIR_REC_LEN(1);
687 while ((char *)de
<= kaddr
) {
688 if (de
->rec_len
== 0) {
689 ext2_error(inode
->i_sb
, __func__
,
690 "zero-length directory entry");
691 printk("kaddr=%p, de=%p\n", kaddr
, de
);
694 if (de
->inode
!= 0) {
695 /* check for . and .. */
696 if (de
->name
[0] != '.')
698 if (de
->name_len
> 2)
700 if (de
->name_len
< 2) {
702 cpu_to_le32(inode
->i_ino
))
704 } else if (de
->name
[1] != '.')
707 de
= ext2_next_entry(de
);
718 const struct file_operations ext2_dir_operations
= {
719 .llseek
= generic_file_llseek
,
720 .read
= generic_read_dir
,
721 .iterate_shared
= ext2_readdir
,
722 .unlocked_ioctl
= ext2_ioctl
,
724 .compat_ioctl
= ext2_compat_ioctl
,