1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1992, 1993, 1994, 1995
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
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;
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_SHIFT;
79 if (last_byte > PAGE_SIZE)
80 last_byte = PAGE_SIZE;
84 static void ext2_commit_chunk(struct folio *folio, loff_t pos, unsigned len)
86 struct address_space *mapping = folio->mapping;
87 struct inode *dir = mapping->host;
89 inode_inc_iversion(dir);
90 block_write_end(NULL, mapping, pos, len, len, &folio->page, NULL);
92 if (pos+len > dir->i_size) {
93 i_size_write(dir, pos+len);
94 mark_inode_dirty(dir);
99 static bool ext2_check_folio(struct folio *folio, int quiet, char *kaddr)
101 struct inode *dir = folio->mapping->host;
102 struct super_block *sb = dir->i_sb;
103 unsigned chunk_size = ext2_chunk_size(dir);
104 u32 max_inumber = le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count);
105 unsigned offs, rec_len;
106 unsigned limit = folio_size(folio);
110 if (dir->i_size < folio_pos(folio) + limit) {
111 limit = offset_in_folio(folio, dir->i_size);
112 if (limit & (chunk_size - 1))
117 for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
118 p = (ext2_dirent *)(kaddr + offs);
119 rec_len = ext2_rec_len_from_disk(p->rec_len);
121 if (unlikely(rec_len < EXT2_DIR_REC_LEN(1)))
123 if (unlikely(rec_len & 3))
125 if (unlikely(rec_len < EXT2_DIR_REC_LEN(p->name_len)))
127 if (unlikely(((offs + rec_len - 1) ^ offs) & ~(chunk_size-1)))
129 if (unlikely(le32_to_cpu(p->inode) > max_inumber))
135 folio_set_checked(folio);
138 /* Too bad, we had an error */
142 ext2_error(sb, __func__,
143 "size of directory #%lu is not a multiple "
144 "of chunk size", dir->i_ino);
147 error = "rec_len is smaller than minimal";
150 error = "unaligned directory entry";
153 error = "rec_len is too small for name_len";
156 error = "directory entry across blocks";
159 error = "inode out of bounds";
162 ext2_error(sb, __func__, "bad entry in directory #%lu: : %s - "
163 "offset=%llu, inode=%lu, rec_len=%d, name_len=%d",
164 dir->i_ino, error, folio_pos(folio) + offs,
165 (unsigned long) le32_to_cpu(p->inode),
166 rec_len, p->name_len);
170 p = (ext2_dirent *)(kaddr + offs);
171 ext2_error(sb, "ext2_check_folio",
172 "entry in directory #%lu spans the page boundary"
173 "offset=%llu, inode=%lu",
174 dir->i_ino, folio_pos(folio) + offs,
175 (unsigned long) le32_to_cpu(p->inode));
182 * Calls to ext2_get_folio()/folio_release_kmap() must be nested according
183 * to the rules documented in kmap_local_folio()/kunmap_local().
185 * NOTE: ext2_find_entry() and ext2_dotdot() act as a call
186 * to folio_release_kmap() and should be treated as a call to
187 * folio_release_kmap() for nesting purposes.
189 static void *ext2_get_folio(struct inode *dir, unsigned long n,
190 int quiet, struct folio **foliop)
192 struct address_space *mapping = dir->i_mapping;
193 struct folio *folio = read_mapping_folio(mapping, n, NULL);
197 return ERR_CAST(folio);
198 kaddr = kmap_local_folio(folio, 0);
199 if (unlikely(!folio_test_checked(folio))) {
200 if (!ext2_check_folio(folio, quiet, kaddr))
207 folio_release_kmap(folio, kaddr);
208 return ERR_PTR(-EIO);
212 * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure.
214 * len <= EXT2_NAME_LEN and de != NULL are guaranteed by caller.
216 static inline int ext2_match (int len, const char * const name,
217 struct ext2_dir_entry_2 * de)
219 if (len != de->name_len)
223 return !memcmp(name, de->name, len);
227 * p is at least 6 bytes before the end of page
229 static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
231 return (ext2_dirent *)((char *)p +
232 ext2_rec_len_from_disk(p->rec_len));
235 static inline unsigned
236 ext2_validate_entry(char *base, unsigned offset, unsigned mask)
238 ext2_dirent *de = (ext2_dirent*)(base + offset);
239 ext2_dirent *p = (ext2_dirent*)(base + (offset&mask));
240 while ((char*)p < (char*)de) {
243 p = ext2_next_entry(p);
245 return offset_in_page(p);
248 static inline void ext2_set_de_type(ext2_dirent *de, struct inode *inode)
250 if (EXT2_HAS_INCOMPAT_FEATURE(inode->i_sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
251 de->file_type = fs_umode_to_ftype(inode->i_mode);
257 ext2_readdir(struct file *file, struct dir_context *ctx)
259 loff_t pos = ctx->pos;
260 struct inode *inode = file_inode(file);
261 struct super_block *sb = inode->i_sb;
262 unsigned int offset = pos & ~PAGE_MASK;
263 unsigned long n = pos >> PAGE_SHIFT;
264 unsigned long npages = dir_pages(inode);
265 unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
266 bool need_revalidate = !inode_eq_iversion(inode, file->f_version);
269 if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
273 EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE);
275 for ( ; n < npages; n++, offset = 0) {
278 char *kaddr = ext2_get_folio(inode, n, 0, &folio);
282 ext2_error(sb, __func__,
285 ctx->pos += PAGE_SIZE - offset;
286 return PTR_ERR(kaddr);
288 if (unlikely(need_revalidate)) {
290 offset = ext2_validate_entry(kaddr, offset, chunk_mask);
291 ctx->pos = (n<<PAGE_SHIFT) + offset;
293 file->f_version = inode_query_iversion(inode);
294 need_revalidate = false;
296 de = (ext2_dirent *)(kaddr+offset);
297 limit = kaddr + ext2_last_byte(inode, n) - EXT2_DIR_REC_LEN(1);
298 for ( ;(char*)de <= limit; de = ext2_next_entry(de)) {
299 if (de->rec_len == 0) {
300 ext2_error(sb, __func__,
301 "zero-length directory entry");
302 folio_release_kmap(folio, de);
306 unsigned char d_type = DT_UNKNOWN;
309 d_type = fs_ftype_to_dtype(de->file_type);
311 if (!dir_emit(ctx, de->name, de->name_len,
312 le32_to_cpu(de->inode),
314 folio_release_kmap(folio, de);
318 ctx->pos += ext2_rec_len_from_disk(de->rec_len);
320 folio_release_kmap(folio, kaddr);
328 * finds an entry in the specified directory with the wanted name. It
329 * returns the page in which the entry was found (as a parameter - res_page),
330 * and the entry itself. Page is returned mapped and unlocked.
331 * Entry is guaranteed to be valid.
333 * On Success folio_release_kmap() should be called on *foliop.
335 * NOTE: Calls to ext2_get_folio()/folio_release_kmap() must be nested
336 * according to the rules documented in kmap_local_folio()/kunmap_local().
338 * ext2_find_entry() and ext2_dotdot() act as a call to ext2_get_folio()
339 * and should be treated as a call to ext2_get_folio() for nesting
342 struct ext2_dir_entry_2 *ext2_find_entry (struct inode *dir,
343 const struct qstr *child, struct folio **foliop)
345 const char *name = child->name;
346 int namelen = child->len;
347 unsigned reclen = EXT2_DIR_REC_LEN(namelen);
348 unsigned long start, n;
349 unsigned long npages = dir_pages(dir);
350 struct ext2_inode_info *ei = EXT2_I(dir);
356 start = ei->i_dir_start_lookup;
361 char *kaddr = ext2_get_folio(dir, n, 0, foliop);
363 return ERR_CAST(kaddr);
365 de = (ext2_dirent *) kaddr;
366 kaddr += ext2_last_byte(dir, n) - reclen;
367 while ((char *) de <= kaddr) {
368 if (de->rec_len == 0) {
369 ext2_error(dir->i_sb, __func__,
370 "zero-length directory entry");
371 folio_release_kmap(*foliop, de);
374 if (ext2_match(namelen, name, de))
376 de = ext2_next_entry(de);
378 folio_release_kmap(*foliop, kaddr);
382 /* next folio is past the blocks we've got */
383 if (unlikely(n > (dir->i_blocks >> (PAGE_SHIFT - 9)))) {
384 ext2_error(dir->i_sb, __func__,
385 "dir %lu size %lld exceeds block count %llu",
386 dir->i_ino, dir->i_size,
387 (unsigned long long)dir->i_blocks);
390 } while (n != start);
392 return ERR_PTR(-ENOENT);
395 ei->i_dir_start_lookup = n;
400 * Return the '..' directory entry and the page in which the entry was found
401 * (as a parameter - p).
403 * On Success folio_release_kmap() should be called on *foliop.
405 * NOTE: Calls to ext2_get_folio()/folio_release_kmap() must be nested
406 * according to the rules documented in kmap_local_folio()/kunmap_local().
408 * ext2_find_entry() and ext2_dotdot() act as a call to ext2_get_folio()
409 * and should be treated as a call to ext2_get_folio() for nesting
412 struct ext2_dir_entry_2 *ext2_dotdot(struct inode *dir, struct folio **foliop)
414 ext2_dirent *de = ext2_get_folio(dir, 0, 0, foliop);
417 return ext2_next_entry(de);
421 int ext2_inode_by_name(struct inode *dir, const struct qstr *child, ino_t *ino)
423 struct ext2_dir_entry_2 *de;
426 de = ext2_find_entry(dir, child, &folio);
430 *ino = le32_to_cpu(de->inode);
431 folio_release_kmap(folio, de);
435 static int ext2_prepare_chunk(struct folio *folio, loff_t pos, unsigned len)
437 return __block_write_begin(&folio->page, pos, len, ext2_get_block);
440 static int ext2_handle_dirsync(struct inode *dir)
444 err = filemap_write_and_wait(dir->i_mapping);
446 err = sync_inode_metadata(dir, 1);
450 int ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
451 struct folio *folio, struct inode *inode, bool update_times)
453 loff_t pos = folio_pos(folio) + offset_in_folio(folio, de);
454 unsigned len = ext2_rec_len_from_disk(de->rec_len);
458 err = ext2_prepare_chunk(folio, pos, len);
463 de->inode = cpu_to_le32(inode->i_ino);
464 ext2_set_de_type(de, inode);
465 ext2_commit_chunk(folio, pos, len);
467 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
468 EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
469 mark_inode_dirty(dir);
470 return ext2_handle_dirsync(dir);
476 int ext2_add_link (struct dentry *dentry, struct inode *inode)
478 struct inode *dir = d_inode(dentry->d_parent);
479 const char *name = dentry->d_name.name;
480 int namelen = dentry->d_name.len;
481 unsigned chunk_size = ext2_chunk_size(dir);
482 unsigned reclen = EXT2_DIR_REC_LEN(namelen);
483 unsigned short rec_len, name_len;
484 struct folio *folio = NULL;
486 unsigned long npages = dir_pages(dir);
492 * We take care of directory expansion in the same loop.
493 * This code plays outside i_size, so it locks the folio
494 * to protect that region.
496 for (n = 0; n <= npages; n++) {
497 char *kaddr = ext2_get_folio(dir, n, 0, &folio);
501 return PTR_ERR(kaddr);
503 dir_end = kaddr + ext2_last_byte(dir, n);
504 de = (ext2_dirent *)kaddr;
505 kaddr += folio_size(folio) - reclen;
506 while ((char *)de <= kaddr) {
507 if ((char *)de == dir_end) {
510 rec_len = chunk_size;
511 de->rec_len = ext2_rec_len_to_disk(chunk_size);
515 if (de->rec_len == 0) {
516 ext2_error(dir->i_sb, __func__,
517 "zero-length directory entry");
522 if (ext2_match (namelen, name, de))
524 name_len = EXT2_DIR_REC_LEN(de->name_len);
525 rec_len = ext2_rec_len_from_disk(de->rec_len);
526 if (!de->inode && rec_len >= reclen)
528 if (rec_len >= name_len + reclen)
530 de = (ext2_dirent *) ((char *) de + rec_len);
533 folio_release_kmap(folio, kaddr);
539 pos = folio_pos(folio) + offset_in_folio(folio, de);
540 err = ext2_prepare_chunk(folio, pos, rec_len);
544 ext2_dirent *de1 = (ext2_dirent *) ((char *) de + name_len);
545 de1->rec_len = ext2_rec_len_to_disk(rec_len - name_len);
546 de->rec_len = ext2_rec_len_to_disk(name_len);
549 de->name_len = namelen;
550 memcpy(de->name, name, namelen);
551 de->inode = cpu_to_le32(inode->i_ino);
552 ext2_set_de_type (de, inode);
553 ext2_commit_chunk(folio, pos, rec_len);
554 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
555 EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
556 mark_inode_dirty(dir);
557 err = ext2_handle_dirsync(dir);
560 folio_release_kmap(folio, de);
568 * ext2_delete_entry deletes a directory entry by merging it with the
569 * previous entry. Page is up-to-date.
571 int ext2_delete_entry(struct ext2_dir_entry_2 *dir, struct folio *folio)
573 struct inode *inode = folio->mapping->host;
577 ext2_dirent *de, *pde = NULL;
580 from = offset_in_folio(folio, dir);
581 to = from + ext2_rec_len_from_disk(dir->rec_len);
582 kaddr = (char *)dir - from;
583 from &= ~(ext2_chunk_size(inode)-1);
584 de = (ext2_dirent *)(kaddr + from);
586 while ((char*)de < (char*)dir) {
587 if (de->rec_len == 0) {
588 ext2_error(inode->i_sb, __func__,
589 "zero-length directory entry");
593 de = ext2_next_entry(de);
596 from = offset_in_folio(folio, pde);
597 pos = folio_pos(folio) + from;
599 err = ext2_prepare_chunk(folio, pos, to - from);
605 pde->rec_len = ext2_rec_len_to_disk(to - from);
607 ext2_commit_chunk(folio, pos, to - from);
608 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
609 EXT2_I(inode)->i_flags &= ~EXT2_BTREE_FL;
610 mark_inode_dirty(inode);
611 return ext2_handle_dirsync(inode);
615 * Set the first fragment of directory.
617 int ext2_make_empty(struct inode *inode, struct inode *parent)
619 struct folio *folio = filemap_grab_folio(inode->i_mapping, 0);
620 unsigned chunk_size = ext2_chunk_size(inode);
621 struct ext2_dir_entry_2 * de;
626 return PTR_ERR(folio);
628 err = ext2_prepare_chunk(folio, 0, chunk_size);
633 kaddr = kmap_local_folio(folio, 0);
634 memset(kaddr, 0, chunk_size);
635 de = (struct ext2_dir_entry_2 *)kaddr;
637 de->rec_len = ext2_rec_len_to_disk(EXT2_DIR_REC_LEN(1));
638 memcpy (de->name, ".\0\0", 4);
639 de->inode = cpu_to_le32(inode->i_ino);
640 ext2_set_de_type (de, inode);
642 de = (struct ext2_dir_entry_2 *)(kaddr + EXT2_DIR_REC_LEN(1));
644 de->rec_len = ext2_rec_len_to_disk(chunk_size - EXT2_DIR_REC_LEN(1));
645 de->inode = cpu_to_le32(parent->i_ino);
646 memcpy (de->name, "..\0", 4);
647 ext2_set_de_type (de, inode);
649 ext2_commit_chunk(folio, 0, chunk_size);
650 err = ext2_handle_dirsync(inode);
657 * routine to check that the specified directory is empty (for rmdir)
659 int ext2_empty_dir(struct inode *inode)
663 unsigned long i, npages = dir_pages(inode);
665 for (i = 0; i < npages; i++) {
668 kaddr = ext2_get_folio(inode, i, 0, &folio);
672 de = (ext2_dirent *)kaddr;
673 kaddr += ext2_last_byte(inode, i) - EXT2_DIR_REC_LEN(1);
675 while ((char *)de <= kaddr) {
676 if (de->rec_len == 0) {
677 ext2_error(inode->i_sb, __func__,
678 "zero-length directory entry");
679 printk("kaddr=%p, de=%p\n", kaddr, de);
682 if (de->inode != 0) {
683 /* check for . and .. */
684 if (de->name[0] != '.')
686 if (de->name_len > 2)
688 if (de->name_len < 2) {
690 cpu_to_le32(inode->i_ino))
692 } else if (de->name[1] != '.')
695 de = ext2_next_entry(de);
697 folio_release_kmap(folio, kaddr);
702 folio_release_kmap(folio, kaddr);
706 const struct file_operations ext2_dir_operations = {
707 .llseek = generic_file_llseek,
708 .read = generic_read_dir,
709 .iterate_shared = ext2_readdir,
710 .unlocked_ioctl = ext2_ioctl,
712 .compat_ioctl = ext2_compat_ioctl,