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 page *page, loff_t pos, unsigned len)
86 struct address_space *mapping = page->mapping;
87 struct inode *dir = mapping->host;
89 inode_inc_iversion(dir);
90 block_write_end(NULL, mapping, pos, len, len, 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_page(struct page *page, int quiet, char *kaddr)
101 struct inode *dir = page->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 = PAGE_SIZE;
110 if ((dir->i_size >> PAGE_SHIFT) == page->index) {
111 limit = dir->i_size & ~PAGE_MASK;
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 SetPageChecked(page);
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=%lu, inode=%lu, rec_len=%d, name_len=%d",
164 dir->i_ino, error, (page->index<<PAGE_SHIFT)+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_page",
172 "entry in directory #%lu spans the page boundary"
173 "offset=%lu, inode=%lu",
174 dir->i_ino, (page->index<<PAGE_SHIFT)+offs,
175 (unsigned long) le32_to_cpu(p->inode));
183 * Calls to ext2_get_page()/ext2_put_page() must be nested according to the
184 * rules documented in kmap_local_page()/kunmap_local().
186 * NOTE: ext2_find_entry() and ext2_dotdot() act as a call to ext2_get_page()
187 * and should be treated as a call to ext2_get_page() for nesting purposes.
189 static struct page * ext2_get_page(struct inode *dir, unsigned long n,
190 int quiet, void **page_addr)
192 struct address_space *mapping = dir->i_mapping;
193 struct folio *folio = read_mapping_folio(mapping, n, NULL);
197 *page_addr = kmap_local_folio(folio, n & (folio_nr_pages(folio) - 1));
198 if (unlikely(!folio_test_checked(folio))) {
199 if (!ext2_check_page(&folio->page, quiet, *page_addr))
205 ext2_put_page(&folio->page, *page_addr);
206 return ERR_PTR(-EIO);
210 * NOTE! unlike strncmp, ext2_match returns 1 for success, 0 for failure.
212 * len <= EXT2_NAME_LEN and de != NULL are guaranteed by caller.
214 static inline int ext2_match (int len, const char * const name,
215 struct ext2_dir_entry_2 * de)
217 if (len != de->name_len)
221 return !memcmp(name, de->name, len);
225 * p is at least 6 bytes before the end of page
227 static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
229 return (ext2_dirent *)((char *)p +
230 ext2_rec_len_from_disk(p->rec_len));
233 static inline unsigned
234 ext2_validate_entry(char *base, unsigned offset, unsigned mask)
236 ext2_dirent *de = (ext2_dirent*)(base + offset);
237 ext2_dirent *p = (ext2_dirent*)(base + (offset&mask));
238 while ((char*)p < (char*)de) {
241 p = ext2_next_entry(p);
243 return (char *)p - base;
246 static inline void ext2_set_de_type(ext2_dirent *de, struct inode *inode)
248 if (EXT2_HAS_INCOMPAT_FEATURE(inode->i_sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
249 de->file_type = fs_umode_to_ftype(inode->i_mode);
255 ext2_readdir(struct file *file, struct dir_context *ctx)
257 loff_t pos = ctx->pos;
258 struct inode *inode = file_inode(file);
259 struct super_block *sb = inode->i_sb;
260 unsigned int offset = pos & ~PAGE_MASK;
261 unsigned long n = pos >> PAGE_SHIFT;
262 unsigned long npages = dir_pages(inode);
263 unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
264 bool need_revalidate = !inode_eq_iversion(inode, file->f_version);
267 if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
271 EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE);
273 for ( ; n < npages; n++, offset = 0) {
276 struct page *page = ext2_get_page(inode, n, 0, (void **)&kaddr);
279 ext2_error(sb, __func__,
282 ctx->pos += PAGE_SIZE - offset;
283 return PTR_ERR(page);
285 if (unlikely(need_revalidate)) {
287 offset = ext2_validate_entry(kaddr, offset, chunk_mask);
288 ctx->pos = (n<<PAGE_SHIFT) + offset;
290 file->f_version = inode_query_iversion(inode);
291 need_revalidate = false;
293 de = (ext2_dirent *)(kaddr+offset);
294 limit = kaddr + ext2_last_byte(inode, n) - EXT2_DIR_REC_LEN(1);
295 for ( ;(char*)de <= limit; de = ext2_next_entry(de)) {
296 if (de->rec_len == 0) {
297 ext2_error(sb, __func__,
298 "zero-length directory entry");
299 ext2_put_page(page, kaddr);
303 unsigned char d_type = DT_UNKNOWN;
306 d_type = fs_ftype_to_dtype(de->file_type);
308 if (!dir_emit(ctx, de->name, de->name_len,
309 le32_to_cpu(de->inode),
311 ext2_put_page(page, kaddr);
315 ctx->pos += ext2_rec_len_from_disk(de->rec_len);
317 ext2_put_page(page, kaddr);
325 * finds an entry in the specified directory with the wanted name. It
326 * returns the page in which the entry was found (as a parameter - res_page),
327 * and the entry itself. Page is returned mapped and unlocked.
328 * Entry is guaranteed to be valid.
330 * On Success ext2_put_page() should be called on *res_page.
332 * NOTE: Calls to ext2_get_page()/ext2_put_page() must be nested according to
333 * the rules documented in kmap_local_page()/kunmap_local().
335 * ext2_find_entry() and ext2_dotdot() act as a call to ext2_get_page() and
336 * should be treated as a call to ext2_get_page() for nesting purposes.
338 struct ext2_dir_entry_2 *ext2_find_entry (struct inode *dir,
339 const struct qstr *child, struct page **res_page,
340 void **res_page_addr)
342 const char *name = child->name;
343 int namelen = child->len;
344 unsigned reclen = EXT2_DIR_REC_LEN(namelen);
345 unsigned long start, n;
346 unsigned long npages = dir_pages(dir);
347 struct page *page = NULL;
348 struct ext2_inode_info *ei = EXT2_I(dir);
357 *res_page_addr = NULL;
359 start = ei->i_dir_start_lookup;
365 page = ext2_get_page(dir, n, 0, &page_addr);
367 return ERR_CAST(page);
370 de = (ext2_dirent *) kaddr;
371 kaddr += ext2_last_byte(dir, n) - reclen;
372 while ((char *) de <= kaddr) {
373 if (de->rec_len == 0) {
374 ext2_error(dir->i_sb, __func__,
375 "zero-length directory entry");
376 ext2_put_page(page, page_addr);
379 if (ext2_match(namelen, name, de))
381 de = ext2_next_entry(de);
383 ext2_put_page(page, page_addr);
387 /* next page is past the blocks we've got */
388 if (unlikely(n > (dir->i_blocks >> (PAGE_SHIFT - 9)))) {
389 ext2_error(dir->i_sb, __func__,
390 "dir %lu size %lld exceeds block count %llu",
391 dir->i_ino, dir->i_size,
392 (unsigned long long)dir->i_blocks);
395 } while (n != start);
397 return ERR_PTR(-ENOENT);
401 *res_page_addr = page_addr;
402 ei->i_dir_start_lookup = n;
407 * Return the '..' directory entry and the page in which the entry was found
408 * (as a parameter - p).
410 * On Success ext2_put_page() should be called on *p.
412 * NOTE: Calls to ext2_get_page()/ext2_put_page() must be nested according to
413 * the rules documented in kmap_local_page()/kunmap_local().
415 * ext2_find_entry() and ext2_dotdot() act as a call to ext2_get_page() and
416 * should be treated as a call to ext2_get_page() for nesting purposes.
418 struct ext2_dir_entry_2 *ext2_dotdot(struct inode *dir, struct page **p,
422 struct page *page = ext2_get_page(dir, 0, 0, &page_addr);
423 ext2_dirent *de = NULL;
426 de = ext2_next_entry((ext2_dirent *) page_addr);
433 int ext2_inode_by_name(struct inode *dir, const struct qstr *child, ino_t *ino)
435 struct ext2_dir_entry_2 *de;
439 de = ext2_find_entry(dir, child, &page, &page_addr);
443 *ino = le32_to_cpu(de->inode);
444 ext2_put_page(page, page_addr);
448 static int ext2_prepare_chunk(struct page *page, loff_t pos, unsigned len)
450 return __block_write_begin(page, pos, len, ext2_get_block);
454 static int ext2_handle_dirsync(struct inode *dir)
458 err = filemap_write_and_wait(dir->i_mapping);
460 err = sync_inode_metadata(dir, 1);
464 void ext2_set_link(struct inode *dir, struct ext2_dir_entry_2 *de,
465 struct page *page, void *page_addr, struct inode *inode,
468 loff_t pos = page_offset(page) +
469 (char *) de - (char *) page_addr;
470 unsigned len = ext2_rec_len_from_disk(de->rec_len);
474 err = ext2_prepare_chunk(page, pos, len);
476 de->inode = cpu_to_le32(inode->i_ino);
477 ext2_set_de_type(de, inode);
478 ext2_commit_chunk(page, pos, len);
480 dir->i_mtime = dir->i_ctime = current_time(dir);
481 EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
482 mark_inode_dirty(dir);
483 ext2_handle_dirsync(dir);
489 int ext2_add_link (struct dentry *dentry, struct inode *inode)
491 struct inode *dir = d_inode(dentry->d_parent);
492 const char *name = dentry->d_name.name;
493 int namelen = dentry->d_name.len;
494 unsigned chunk_size = ext2_chunk_size(dir);
495 unsigned reclen = EXT2_DIR_REC_LEN(namelen);
496 unsigned short rec_len, name_len;
497 struct page *page = NULL;
498 void *page_addr = NULL;
500 unsigned long npages = dir_pages(dir);
506 * We take care of directory expansion in the same loop.
507 * This code plays outside i_size, so it locks the page
508 * to protect that region.
510 for (n = 0; n <= npages; n++) {
514 page = ext2_get_page(dir, n, 0, &page_addr);
520 dir_end = kaddr + ext2_last_byte(dir, n);
521 de = (ext2_dirent *)kaddr;
522 kaddr += PAGE_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);
550 ext2_put_page(page, page_addr);
556 pos = page_offset(page) +
557 (char *)de - (char *)page_addr;
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 ext2_commit_chunk(page, pos, rec_len);
572 dir->i_mtime = dir->i_ctime = current_time(dir);
573 EXT2_I(dir)->i_flags &= ~EXT2_BTREE_FL;
574 mark_inode_dirty(dir);
575 err = ext2_handle_dirsync(dir);
578 ext2_put_page(page, page_addr);
587 * ext2_delete_entry deletes a directory entry by merging it with the
588 * previous entry. Page is up-to-date.
590 int ext2_delete_entry (struct ext2_dir_entry_2 *dir, struct page *page,
593 struct inode *inode = page->mapping->host;
594 unsigned from = ((char*)dir - kaddr) & ~(ext2_chunk_size(inode)-1);
595 unsigned to = ((char *)dir - kaddr) +
596 ext2_rec_len_from_disk(dir->rec_len);
598 ext2_dirent * pde = NULL;
599 ext2_dirent * de = (ext2_dirent *) (kaddr + from);
602 while ((char*)de < (char*)dir) {
603 if (de->rec_len == 0) {
604 ext2_error(inode->i_sb, __func__,
605 "zero-length directory entry");
610 de = ext2_next_entry(de);
613 from = (char *)pde - kaddr;
614 pos = page_offset(page) + from;
616 err = ext2_prepare_chunk(page, pos, to - from);
619 pde->rec_len = ext2_rec_len_to_disk(to - from);
621 ext2_commit_chunk(page, pos, to - from);
622 inode->i_ctime = inode->i_mtime = current_time(inode);
623 EXT2_I(inode)->i_flags &= ~EXT2_BTREE_FL;
624 mark_inode_dirty(inode);
625 err = ext2_handle_dirsync(inode);
631 * Set the first fragment of directory.
633 int ext2_make_empty(struct inode *inode, struct inode *parent)
635 struct page *page = grab_cache_page(inode->i_mapping, 0);
636 unsigned chunk_size = ext2_chunk_size(inode);
637 struct ext2_dir_entry_2 * de;
644 err = ext2_prepare_chunk(page, 0, chunk_size);
649 kaddr = kmap_atomic(page);
650 memset(kaddr, 0, chunk_size);
651 de = (struct ext2_dir_entry_2 *)kaddr;
653 de->rec_len = ext2_rec_len_to_disk(EXT2_DIR_REC_LEN(1));
654 memcpy (de->name, ".\0\0", 4);
655 de->inode = cpu_to_le32(inode->i_ino);
656 ext2_set_de_type (de, inode);
658 de = (struct ext2_dir_entry_2 *)(kaddr + EXT2_DIR_REC_LEN(1));
660 de->rec_len = ext2_rec_len_to_disk(chunk_size - EXT2_DIR_REC_LEN(1));
661 de->inode = cpu_to_le32(parent->i_ino);
662 memcpy (de->name, "..\0", 4);
663 ext2_set_de_type (de, inode);
664 kunmap_atomic(kaddr);
665 ext2_commit_chunk(page, 0, chunk_size);
666 err = ext2_handle_dirsync(inode);
673 * routine to check that the specified directory is empty (for rmdir)
675 int ext2_empty_dir (struct inode * inode)
677 void *page_addr = NULL;
678 struct page *page = NULL;
679 unsigned long i, npages = dir_pages(inode);
681 for (i = 0; i < npages; i++) {
684 page = ext2_get_page(inode, i, 0, &page_addr);
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);
715 ext2_put_page(page, page_addr);
720 ext2_put_page(page, page_addr);
724 const struct file_operations ext2_dir_operations = {
725 .llseek = generic_file_llseek,
726 .read = generic_read_dir,
727 .iterate_shared = ext2_readdir,
728 .unlocked_ioctl = ext2_ioctl,
730 .compat_ioctl = ext2_compat_ioctl,