1 // SPDX-License-Identifier: GPL-2.0+
3 * NILFS directory entry operations
5 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
7 * Modified for NILFS by Amagai Yoshiji.
12 * Copyright (C) 1992, 1993, 1994, 1995
14 * Laboratoire MASI - Institut Blaise Pascal
15 * Universite Pierre et Marie Curie (Paris VI)
19 * linux/fs/minix/dir.c
21 * Copyright (C) 1991, 1992 Linus Torvalds
23 * ext2 directory handling functions
25 * Big-endian to little-endian byte-swapping/bitmaps by
28 * All code that works with directory layout had been switched to pagecache
32 #include <linux/pagemap.h>
36 static inline unsigned int nilfs_rec_len_from_disk(__le16 dlen)
38 unsigned int len = le16_to_cpu(dlen);
40 #if (PAGE_SIZE >= 65536)
41 if (len == NILFS_MAX_REC_LEN)
47 static inline __le16 nilfs_rec_len_to_disk(unsigned int len)
49 #if (PAGE_SIZE >= 65536)
51 return cpu_to_le16(NILFS_MAX_REC_LEN);
53 BUG_ON(len > (1 << 16));
55 return cpu_to_le16(len);
59 * nilfs uses block-sized chunks. Arguably, sector-sized ones would be
60 * more robust, but we have what we have
62 static inline unsigned int nilfs_chunk_size(struct inode *inode)
64 return inode->i_sb->s_blocksize;
68 * Return the offset into page `page_nr' of the last valid
69 * byte in that page, plus one.
71 static unsigned int nilfs_last_byte(struct inode *inode, unsigned long page_nr)
73 unsigned int last_byte = inode->i_size;
75 last_byte -= page_nr << PAGE_SHIFT;
76 if (last_byte > PAGE_SIZE)
77 last_byte = PAGE_SIZE;
81 static int nilfs_prepare_chunk(struct folio *folio, unsigned int from,
84 loff_t pos = folio_pos(folio) + from;
86 return __block_write_begin(&folio->page, pos, to - from, nilfs_get_block);
89 static void nilfs_commit_chunk(struct folio *folio,
90 struct address_space *mapping, size_t from, size_t to)
92 struct inode *dir = mapping->host;
93 loff_t pos = folio_pos(folio) + from;
94 size_t copied, len = to - from;
95 unsigned int nr_dirty;
98 nr_dirty = nilfs_page_count_clean_buffers(&folio->page, from, to);
99 copied = block_write_end(NULL, mapping, pos, len, len, &folio->page, NULL);
100 if (pos + copied > dir->i_size)
101 i_size_write(dir, pos + copied);
103 nilfs_set_transaction_flag(NILFS_TI_SYNC);
104 err = nilfs_set_file_dirty(dir, nr_dirty);
105 WARN_ON(err); /* do not happen */
109 static bool nilfs_check_folio(struct folio *folio, char *kaddr)
111 struct inode *dir = folio->mapping->host;
112 struct super_block *sb = dir->i_sb;
113 unsigned int chunk_size = nilfs_chunk_size(dir);
114 size_t offs, rec_len;
115 size_t limit = folio_size(folio);
116 struct nilfs_dir_entry *p;
119 if (dir->i_size < folio_pos(folio) + limit) {
120 limit = dir->i_size - folio_pos(folio);
121 if (limit & (chunk_size - 1))
126 for (offs = 0; offs <= limit - NILFS_DIR_REC_LEN(1); offs += rec_len) {
127 p = (struct nilfs_dir_entry *)(kaddr + offs);
128 rec_len = nilfs_rec_len_from_disk(p->rec_len);
130 if (rec_len < NILFS_DIR_REC_LEN(1))
134 if (rec_len < NILFS_DIR_REC_LEN(p->name_len))
136 if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
142 folio_set_checked(folio);
145 /* Too bad, we had an error */
149 "size of directory #%lu is not a multiple of chunk size",
153 error = "rec_len is smaller than minimal";
156 error = "unaligned directory entry";
159 error = "rec_len is too small for name_len";
162 error = "directory entry across blocks";
165 "bad entry in directory #%lu: %s - offset=%lu, inode=%lu, rec_len=%zd, name_len=%d",
166 dir->i_ino, error, (folio->index << PAGE_SHIFT) + offs,
167 (unsigned long)le64_to_cpu(p->inode),
168 rec_len, p->name_len);
171 p = (struct nilfs_dir_entry *)(kaddr + offs);
173 "entry in directory #%lu spans the page boundary offset=%lu, inode=%lu",
174 dir->i_ino, (folio->index << PAGE_SHIFT) + offs,
175 (unsigned long)le64_to_cpu(p->inode));
180 static void *nilfs_get_folio(struct inode *dir, unsigned long n,
181 struct folio **foliop)
183 struct address_space *mapping = dir->i_mapping;
184 struct folio *folio = read_mapping_folio(mapping, n, NULL);
190 kaddr = kmap_local_folio(folio, 0);
191 if (unlikely(!folio_test_checked(folio))) {
192 if (!nilfs_check_folio(folio, kaddr))
200 folio_release_kmap(folio, kaddr);
201 return ERR_PTR(-EIO);
205 * NOTE! unlike strncmp, nilfs_match returns 1 for success, 0 for failure.
207 * len <= NILFS_NAME_LEN and de != NULL are guaranteed by caller.
210 nilfs_match(int len, const unsigned char *name, struct nilfs_dir_entry *de)
212 if (len != de->name_len)
216 return !memcmp(name, de->name, len);
220 * p is at least 6 bytes before the end of page
222 static struct nilfs_dir_entry *nilfs_next_entry(struct nilfs_dir_entry *p)
224 return (struct nilfs_dir_entry *)((char *)p +
225 nilfs_rec_len_from_disk(p->rec_len));
229 nilfs_filetype_table[NILFS_FT_MAX] = {
230 [NILFS_FT_UNKNOWN] = DT_UNKNOWN,
231 [NILFS_FT_REG_FILE] = DT_REG,
232 [NILFS_FT_DIR] = DT_DIR,
233 [NILFS_FT_CHRDEV] = DT_CHR,
234 [NILFS_FT_BLKDEV] = DT_BLK,
235 [NILFS_FT_FIFO] = DT_FIFO,
236 [NILFS_FT_SOCK] = DT_SOCK,
237 [NILFS_FT_SYMLINK] = DT_LNK,
242 nilfs_type_by_mode[(S_IFMT >> S_SHIFT) + 1] = {
243 [S_IFREG >> S_SHIFT] = NILFS_FT_REG_FILE,
244 [S_IFDIR >> S_SHIFT] = NILFS_FT_DIR,
245 [S_IFCHR >> S_SHIFT] = NILFS_FT_CHRDEV,
246 [S_IFBLK >> S_SHIFT] = NILFS_FT_BLKDEV,
247 [S_IFIFO >> S_SHIFT] = NILFS_FT_FIFO,
248 [S_IFSOCK >> S_SHIFT] = NILFS_FT_SOCK,
249 [S_IFLNK >> S_SHIFT] = NILFS_FT_SYMLINK,
252 static void nilfs_set_de_type(struct nilfs_dir_entry *de, struct inode *inode)
254 umode_t mode = inode->i_mode;
256 de->file_type = nilfs_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
259 static int nilfs_readdir(struct file *file, struct dir_context *ctx)
261 loff_t pos = ctx->pos;
262 struct inode *inode = file_inode(file);
263 struct super_block *sb = inode->i_sb;
264 unsigned int offset = pos & ~PAGE_MASK;
265 unsigned long n = pos >> PAGE_SHIFT;
266 unsigned long npages = dir_pages(inode);
268 if (pos > inode->i_size - NILFS_DIR_REC_LEN(1))
271 for ( ; n < npages; n++, offset = 0) {
273 struct nilfs_dir_entry *de;
276 kaddr = nilfs_get_folio(inode, n, &folio);
278 nilfs_error(sb, "bad page in #%lu", inode->i_ino);
279 ctx->pos += PAGE_SIZE - offset;
282 de = (struct nilfs_dir_entry *)(kaddr + offset);
283 limit = kaddr + nilfs_last_byte(inode, n) -
284 NILFS_DIR_REC_LEN(1);
285 for ( ; (char *)de <= limit; de = nilfs_next_entry(de)) {
286 if (de->rec_len == 0) {
287 nilfs_error(sb, "zero-length directory entry");
288 folio_release_kmap(folio, kaddr);
294 if (de->file_type < NILFS_FT_MAX)
295 t = nilfs_filetype_table[de->file_type];
299 if (!dir_emit(ctx, de->name, de->name_len,
300 le64_to_cpu(de->inode), t)) {
301 folio_release_kmap(folio, kaddr);
305 ctx->pos += nilfs_rec_len_from_disk(de->rec_len);
307 folio_release_kmap(folio, kaddr);
315 * Finds an entry in the specified directory with the wanted name. It
316 * returns the folio in which the entry was found, and the entry itself.
317 * The folio is mapped and unlocked. When the caller is finished with
318 * the entry, it should call folio_release_kmap().
320 * On failure, returns NULL and the caller should ignore foliop.
322 struct nilfs_dir_entry *nilfs_find_entry(struct inode *dir,
323 const struct qstr *qstr, struct folio **foliop)
325 const unsigned char *name = qstr->name;
326 int namelen = qstr->len;
327 unsigned int reclen = NILFS_DIR_REC_LEN(namelen);
328 unsigned long start, n;
329 unsigned long npages = dir_pages(dir);
330 struct nilfs_inode_info *ei = NILFS_I(dir);
331 struct nilfs_dir_entry *de;
336 start = ei->i_dir_start_lookup;
341 char *kaddr = nilfs_get_folio(dir, n, foliop);
343 if (!IS_ERR(kaddr)) {
344 de = (struct nilfs_dir_entry *)kaddr;
345 kaddr += nilfs_last_byte(dir, n) - reclen;
346 while ((char *) de <= kaddr) {
347 if (de->rec_len == 0) {
348 nilfs_error(dir->i_sb,
349 "zero-length directory entry");
350 folio_release_kmap(*foliop, kaddr);
353 if (nilfs_match(namelen, name, de))
355 de = nilfs_next_entry(de);
357 folio_release_kmap(*foliop, kaddr);
361 /* next folio is past the blocks we've got */
362 if (unlikely(n > (dir->i_blocks >> (PAGE_SHIFT - 9)))) {
363 nilfs_error(dir->i_sb,
364 "dir %lu size %lld exceeds block count %llu",
365 dir->i_ino, dir->i_size,
366 (unsigned long long)dir->i_blocks);
369 } while (n != start);
374 ei->i_dir_start_lookup = n;
378 struct nilfs_dir_entry *nilfs_dotdot(struct inode *dir, struct folio **foliop)
380 struct nilfs_dir_entry *de = nilfs_get_folio(dir, 0, foliop);
384 return nilfs_next_entry(de);
387 ino_t nilfs_inode_by_name(struct inode *dir, const struct qstr *qstr)
390 struct nilfs_dir_entry *de;
393 de = nilfs_find_entry(dir, qstr, &folio);
395 res = le64_to_cpu(de->inode);
396 folio_release_kmap(folio, de);
401 void nilfs_set_link(struct inode *dir, struct nilfs_dir_entry *de,
402 struct folio *folio, struct inode *inode)
404 size_t from = offset_in_folio(folio, de);
405 size_t to = from + nilfs_rec_len_from_disk(de->rec_len);
406 struct address_space *mapping = folio->mapping;
410 err = nilfs_prepare_chunk(folio, from, to);
412 de->inode = cpu_to_le64(inode->i_ino);
413 nilfs_set_de_type(de, inode);
414 nilfs_commit_chunk(folio, mapping, from, to);
415 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
421 int nilfs_add_link(struct dentry *dentry, struct inode *inode)
423 struct inode *dir = d_inode(dentry->d_parent);
424 const unsigned char *name = dentry->d_name.name;
425 int namelen = dentry->d_name.len;
426 unsigned int chunk_size = nilfs_chunk_size(dir);
427 unsigned int reclen = NILFS_DIR_REC_LEN(namelen);
428 unsigned short rec_len, name_len;
429 struct folio *folio = NULL;
430 struct nilfs_dir_entry *de;
431 unsigned long npages = dir_pages(dir);
437 * We take care of directory expansion in the same loop.
438 * This code plays outside i_size, so it locks the folio
439 * to protect that region.
441 for (n = 0; n <= npages; n++) {
442 char *kaddr = nilfs_get_folio(dir, n, &folio);
446 return PTR_ERR(kaddr);
448 dir_end = kaddr + nilfs_last_byte(dir, n);
449 de = (struct nilfs_dir_entry *)kaddr;
450 kaddr += folio_size(folio) - reclen;
451 while ((char *)de <= kaddr) {
452 if ((char *)de == dir_end) {
455 rec_len = chunk_size;
456 de->rec_len = nilfs_rec_len_to_disk(chunk_size);
460 if (de->rec_len == 0) {
461 nilfs_error(dir->i_sb,
462 "zero-length directory entry");
467 if (nilfs_match(namelen, name, de))
469 name_len = NILFS_DIR_REC_LEN(de->name_len);
470 rec_len = nilfs_rec_len_from_disk(de->rec_len);
471 if (!de->inode && rec_len >= reclen)
473 if (rec_len >= name_len + reclen)
475 de = (struct nilfs_dir_entry *)((char *)de + rec_len);
478 folio_release_kmap(folio, kaddr);
484 from = offset_in_folio(folio, de);
486 err = nilfs_prepare_chunk(folio, from, to);
490 struct nilfs_dir_entry *de1;
492 de1 = (struct nilfs_dir_entry *)((char *)de + name_len);
493 de1->rec_len = nilfs_rec_len_to_disk(rec_len - name_len);
494 de->rec_len = nilfs_rec_len_to_disk(name_len);
497 de->name_len = namelen;
498 memcpy(de->name, name, namelen);
499 de->inode = cpu_to_le64(inode->i_ino);
500 nilfs_set_de_type(de, inode);
501 nilfs_commit_chunk(folio, folio->mapping, from, to);
502 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
503 nilfs_mark_inode_dirty(dir);
506 folio_release_kmap(folio, de);
514 * nilfs_delete_entry deletes a directory entry by merging it with the
515 * previous entry. Folio is up-to-date.
517 int nilfs_delete_entry(struct nilfs_dir_entry *dir, struct folio *folio)
519 struct address_space *mapping = folio->mapping;
520 struct inode *inode = mapping->host;
521 char *kaddr = (char *)((unsigned long)dir & ~(folio_size(folio) - 1));
523 struct nilfs_dir_entry *de, *pde = NULL;
526 from = ((char *)dir - kaddr) & ~(nilfs_chunk_size(inode) - 1);
527 to = ((char *)dir - kaddr) + nilfs_rec_len_from_disk(dir->rec_len);
528 de = (struct nilfs_dir_entry *)(kaddr + from);
530 while ((char *)de < (char *)dir) {
531 if (de->rec_len == 0) {
532 nilfs_error(inode->i_sb,
533 "zero-length directory entry");
538 de = nilfs_next_entry(de);
541 from = (char *)pde - kaddr;
543 err = nilfs_prepare_chunk(folio, from, to);
546 pde->rec_len = nilfs_rec_len_to_disk(to - from);
548 nilfs_commit_chunk(folio, mapping, from, to);
549 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
555 * Set the first fragment of directory.
557 int nilfs_make_empty(struct inode *inode, struct inode *parent)
559 struct address_space *mapping = inode->i_mapping;
560 struct folio *folio = filemap_grab_folio(mapping, 0);
561 unsigned int chunk_size = nilfs_chunk_size(inode);
562 struct nilfs_dir_entry *de;
567 return PTR_ERR(folio);
569 err = nilfs_prepare_chunk(folio, 0, chunk_size);
574 kaddr = kmap_local_folio(folio, 0);
575 memset(kaddr, 0, chunk_size);
576 de = (struct nilfs_dir_entry *)kaddr;
578 de->rec_len = nilfs_rec_len_to_disk(NILFS_DIR_REC_LEN(1));
579 memcpy(de->name, ".\0\0", 4);
580 de->inode = cpu_to_le64(inode->i_ino);
581 nilfs_set_de_type(de, inode);
583 de = (struct nilfs_dir_entry *)(kaddr + NILFS_DIR_REC_LEN(1));
585 de->rec_len = nilfs_rec_len_to_disk(chunk_size - NILFS_DIR_REC_LEN(1));
586 de->inode = cpu_to_le64(parent->i_ino);
587 memcpy(de->name, "..\0", 4);
588 nilfs_set_de_type(de, inode);
590 nilfs_commit_chunk(folio, mapping, 0, chunk_size);
597 * routine to check that the specified directory is empty (for rmdir)
599 int nilfs_empty_dir(struct inode *inode)
601 struct folio *folio = NULL;
603 unsigned long i, npages = dir_pages(inode);
605 for (i = 0; i < npages; i++) {
606 struct nilfs_dir_entry *de;
608 kaddr = nilfs_get_folio(inode, i, &folio);
612 de = (struct nilfs_dir_entry *)kaddr;
613 kaddr += nilfs_last_byte(inode, i) - NILFS_DIR_REC_LEN(1);
615 while ((char *)de <= kaddr) {
616 if (de->rec_len == 0) {
617 nilfs_error(inode->i_sb,
618 "zero-length directory entry (kaddr=%p, de=%p)",
622 if (de->inode != 0) {
623 /* check for . and .. */
624 if (de->name[0] != '.')
626 if (de->name_len > 2)
628 if (de->name_len < 2) {
630 cpu_to_le64(inode->i_ino))
632 } else if (de->name[1] != '.')
635 de = nilfs_next_entry(de);
637 folio_release_kmap(folio, kaddr);
642 folio_release_kmap(folio, kaddr);
646 const struct file_operations nilfs_dir_operations = {
647 .llseek = generic_file_llseek,
648 .read = generic_read_dir,
649 .iterate_shared = nilfs_readdir,
650 .unlocked_ioctl = nilfs_ioctl,
652 .compat_ioctl = nilfs_compat_ioctl,
653 #endif /* CONFIG_COMPAT */
654 .fsync = nilfs_sync_file,