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
13 * Remy Card (card@masi.ibp.fr)
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
26 * David S. Miller (davem@caip.rutgers.edu), 1995
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 u64 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
, 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
, from
, to
);
99 copied
= block_write_end(NULL
, mapping
, pos
, len
, len
, folio
, 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))
138 if (unlikely(p
->inode
&&
139 NILFS_PRIVATE_INODE(le64_to_cpu(p
->inode
))))
145 folio_set_checked(folio
);
148 /* Too bad, we had an error */
152 "size of directory #%lu is not a multiple of chunk size",
156 error
= "rec_len is smaller than minimal";
159 error
= "unaligned directory entry";
162 error
= "rec_len is too small for name_len";
165 error
= "directory entry across blocks";
168 error
= "disallowed inode number";
171 "bad entry in directory #%lu: %s - offset=%lu, inode=%lu, rec_len=%zd, name_len=%d",
172 dir
->i_ino
, error
, (folio
->index
<< PAGE_SHIFT
) + offs
,
173 (unsigned long)le64_to_cpu(p
->inode
),
174 rec_len
, p
->name_len
);
177 p
= (struct nilfs_dir_entry
*)(kaddr
+ offs
);
179 "entry in directory #%lu spans the page boundary offset=%lu, inode=%lu",
180 dir
->i_ino
, (folio
->index
<< PAGE_SHIFT
) + offs
,
181 (unsigned long)le64_to_cpu(p
->inode
));
186 static void *nilfs_get_folio(struct inode
*dir
, unsigned long n
,
187 struct folio
**foliop
)
189 struct address_space
*mapping
= dir
->i_mapping
;
190 struct folio
*folio
= read_mapping_folio(mapping
, n
, NULL
);
196 kaddr
= kmap_local_folio(folio
, 0);
197 if (unlikely(!folio_test_checked(folio
))) {
198 if (!nilfs_check_folio(folio
, kaddr
))
206 folio_release_kmap(folio
, kaddr
);
207 return ERR_PTR(-EIO
);
211 * NOTE! unlike strncmp, nilfs_match returns 1 for success, 0 for failure.
213 * len <= NILFS_NAME_LEN and de != NULL are guaranteed by caller.
216 nilfs_match(int len
, const unsigned char *name
, struct nilfs_dir_entry
*de
)
218 if (len
!= de
->name_len
)
222 return !memcmp(name
, de
->name
, len
);
226 * p is at least 6 bytes before the end of page
228 static struct nilfs_dir_entry
*nilfs_next_entry(struct nilfs_dir_entry
*p
)
230 return (struct nilfs_dir_entry
*)((char *)p
+
231 nilfs_rec_len_from_disk(p
->rec_len
));
234 static int nilfs_readdir(struct file
*file
, struct dir_context
*ctx
)
236 loff_t pos
= ctx
->pos
;
237 struct inode
*inode
= file_inode(file
);
238 struct super_block
*sb
= inode
->i_sb
;
239 unsigned int offset
= pos
& ~PAGE_MASK
;
240 unsigned long n
= pos
>> PAGE_SHIFT
;
241 unsigned long npages
= dir_pages(inode
);
243 if (pos
> inode
->i_size
- NILFS_DIR_REC_LEN(1))
246 for ( ; n
< npages
; n
++, offset
= 0) {
248 struct nilfs_dir_entry
*de
;
251 kaddr
= nilfs_get_folio(inode
, n
, &folio
);
253 nilfs_error(sb
, "bad page in #%lu", inode
->i_ino
);
254 ctx
->pos
+= PAGE_SIZE
- offset
;
257 de
= (struct nilfs_dir_entry
*)(kaddr
+ offset
);
258 limit
= kaddr
+ nilfs_last_byte(inode
, n
) -
259 NILFS_DIR_REC_LEN(1);
260 for ( ; (char *)de
<= limit
; de
= nilfs_next_entry(de
)) {
261 if (de
->rec_len
== 0) {
262 nilfs_error(sb
, "zero-length directory entry");
263 folio_release_kmap(folio
, kaddr
);
269 t
= fs_ftype_to_dtype(de
->file_type
);
271 if (!dir_emit(ctx
, de
->name
, de
->name_len
,
272 le64_to_cpu(de
->inode
), t
)) {
273 folio_release_kmap(folio
, kaddr
);
277 ctx
->pos
+= nilfs_rec_len_from_disk(de
->rec_len
);
279 folio_release_kmap(folio
, kaddr
);
287 * Finds an entry in the specified directory with the wanted name. It
288 * returns the folio in which the entry was found, and the entry itself.
289 * The folio is mapped and unlocked. When the caller is finished with
290 * the entry, it should call folio_release_kmap().
292 * On failure, returns an error pointer and the caller should ignore foliop.
294 struct nilfs_dir_entry
*nilfs_find_entry(struct inode
*dir
,
295 const struct qstr
*qstr
, struct folio
**foliop
)
297 const unsigned char *name
= qstr
->name
;
298 int namelen
= qstr
->len
;
299 unsigned int reclen
= NILFS_DIR_REC_LEN(namelen
);
300 unsigned long start
, n
;
301 unsigned long npages
= dir_pages(dir
);
302 struct nilfs_inode_info
*ei
= NILFS_I(dir
);
303 struct nilfs_dir_entry
*de
;
308 start
= ei
->i_dir_start_lookup
;
313 char *kaddr
= nilfs_get_folio(dir
, n
, foliop
);
316 return ERR_CAST(kaddr
);
318 de
= (struct nilfs_dir_entry
*)kaddr
;
319 kaddr
+= nilfs_last_byte(dir
, n
) - reclen
;
320 while ((char *)de
<= kaddr
) {
321 if (de
->rec_len
== 0) {
322 nilfs_error(dir
->i_sb
,
323 "zero-length directory entry");
324 folio_release_kmap(*foliop
, kaddr
);
327 if (nilfs_match(namelen
, name
, de
))
329 de
= nilfs_next_entry(de
);
331 folio_release_kmap(*foliop
, kaddr
);
335 /* next folio is past the blocks we've got */
336 if (unlikely(n
> (dir
->i_blocks
>> (PAGE_SHIFT
- 9)))) {
337 nilfs_error(dir
->i_sb
,
338 "dir %lu size %lld exceeds block count %llu",
339 dir
->i_ino
, dir
->i_size
,
340 (unsigned long long)dir
->i_blocks
);
343 } while (n
!= start
);
345 return ERR_PTR(-ENOENT
);
348 ei
->i_dir_start_lookup
= n
;
352 struct nilfs_dir_entry
*nilfs_dotdot(struct inode
*dir
, struct folio
**foliop
)
355 struct nilfs_dir_entry
*de
, *next_de
;
359 de
= nilfs_get_folio(dir
, 0, &folio
);
363 limit
= nilfs_last_byte(dir
, 0); /* is a multiple of chunk size */
364 if (unlikely(!limit
|| le64_to_cpu(de
->inode
) != dir
->i_ino
||
365 !nilfs_match(1, ".", de
))) {
370 next_de
= nilfs_next_entry(de
);
372 * If "next_de" has not reached the end of the chunk, there is
373 * at least one more record. Check whether it matches "..".
375 if (unlikely((char *)next_de
== (char *)de
+ nilfs_chunk_size(dir
) ||
376 !nilfs_match(2, "..", next_de
))) {
377 msg
= "missing '..'";
384 nilfs_error(dir
->i_sb
, "directory #%lu %s", dir
->i_ino
, msg
);
385 folio_release_kmap(folio
, de
);
389 int nilfs_inode_by_name(struct inode
*dir
, const struct qstr
*qstr
, ino_t
*ino
)
391 struct nilfs_dir_entry
*de
;
394 de
= nilfs_find_entry(dir
, qstr
, &folio
);
398 *ino
= le64_to_cpu(de
->inode
);
399 folio_release_kmap(folio
, de
);
403 void nilfs_set_link(struct inode
*dir
, struct nilfs_dir_entry
*de
,
404 struct folio
*folio
, struct inode
*inode
)
406 size_t from
= offset_in_folio(folio
, de
);
407 size_t to
= from
+ nilfs_rec_len_from_disk(de
->rec_len
);
408 struct address_space
*mapping
= folio
->mapping
;
412 err
= nilfs_prepare_chunk(folio
, from
, to
);
414 de
->inode
= cpu_to_le64(inode
->i_ino
);
415 de
->file_type
= fs_umode_to_ftype(inode
->i_mode
);
416 nilfs_commit_chunk(folio
, mapping
, from
, to
);
417 inode_set_mtime_to_ts(dir
, inode_set_ctime_current(dir
));
423 int nilfs_add_link(struct dentry
*dentry
, struct inode
*inode
)
425 struct inode
*dir
= d_inode(dentry
->d_parent
);
426 const unsigned char *name
= dentry
->d_name
.name
;
427 int namelen
= dentry
->d_name
.len
;
428 unsigned int chunk_size
= nilfs_chunk_size(dir
);
429 unsigned int reclen
= NILFS_DIR_REC_LEN(namelen
);
430 unsigned short rec_len
, name_len
;
431 struct folio
*folio
= NULL
;
432 struct nilfs_dir_entry
*de
;
433 unsigned long npages
= dir_pages(dir
);
439 * We take care of directory expansion in the same loop.
440 * This code plays outside i_size, so it locks the folio
441 * to protect that region.
443 for (n
= 0; n
<= npages
; n
++) {
444 char *kaddr
= nilfs_get_folio(dir
, n
, &folio
);
448 return PTR_ERR(kaddr
);
450 dir_end
= kaddr
+ nilfs_last_byte(dir
, n
);
451 de
= (struct nilfs_dir_entry
*)kaddr
;
452 kaddr
+= folio_size(folio
) - reclen
;
453 while ((char *)de
<= kaddr
) {
454 if ((char *)de
== dir_end
) {
457 rec_len
= chunk_size
;
458 de
->rec_len
= nilfs_rec_len_to_disk(chunk_size
);
462 if (de
->rec_len
== 0) {
463 nilfs_error(dir
->i_sb
,
464 "zero-length directory entry");
469 if (nilfs_match(namelen
, name
, de
))
471 name_len
= NILFS_DIR_REC_LEN(de
->name_len
);
472 rec_len
= nilfs_rec_len_from_disk(de
->rec_len
);
473 if (!de
->inode
&& rec_len
>= reclen
)
475 if (rec_len
>= name_len
+ reclen
)
477 de
= (struct nilfs_dir_entry
*)((char *)de
+ rec_len
);
480 folio_release_kmap(folio
, kaddr
);
486 from
= offset_in_folio(folio
, de
);
488 err
= nilfs_prepare_chunk(folio
, from
, to
);
492 struct nilfs_dir_entry
*de1
;
494 de1
= (struct nilfs_dir_entry
*)((char *)de
+ name_len
);
495 de1
->rec_len
= nilfs_rec_len_to_disk(rec_len
- name_len
);
496 de
->rec_len
= nilfs_rec_len_to_disk(name_len
);
499 de
->name_len
= namelen
;
500 memcpy(de
->name
, name
, namelen
);
501 de
->inode
= cpu_to_le64(inode
->i_ino
);
502 de
->file_type
= fs_umode_to_ftype(inode
->i_mode
);
503 nilfs_commit_chunk(folio
, folio
->mapping
, from
, to
);
504 inode_set_mtime_to_ts(dir
, inode_set_ctime_current(dir
));
505 nilfs_mark_inode_dirty(dir
);
508 folio_release_kmap(folio
, de
);
516 * nilfs_delete_entry deletes a directory entry by merging it with the
517 * previous entry. Folio is up-to-date.
519 int nilfs_delete_entry(struct nilfs_dir_entry
*dir
, struct folio
*folio
)
521 struct address_space
*mapping
= folio
->mapping
;
522 struct inode
*inode
= mapping
->host
;
523 char *kaddr
= (char *)((unsigned long)dir
& ~(folio_size(folio
) - 1));
525 struct nilfs_dir_entry
*de
, *pde
= NULL
;
528 from
= ((char *)dir
- kaddr
) & ~(nilfs_chunk_size(inode
) - 1);
529 to
= ((char *)dir
- kaddr
) + nilfs_rec_len_from_disk(dir
->rec_len
);
530 de
= (struct nilfs_dir_entry
*)(kaddr
+ from
);
532 while ((char *)de
< (char *)dir
) {
533 if (de
->rec_len
== 0) {
534 nilfs_error(inode
->i_sb
,
535 "zero-length directory entry");
540 de
= nilfs_next_entry(de
);
543 from
= (char *)pde
- kaddr
;
545 err
= nilfs_prepare_chunk(folio
, from
, to
);
548 pde
->rec_len
= nilfs_rec_len_to_disk(to
- from
);
550 nilfs_commit_chunk(folio
, mapping
, from
, to
);
551 inode_set_mtime_to_ts(inode
, inode_set_ctime_current(inode
));
557 * Set the first fragment of directory.
559 int nilfs_make_empty(struct inode
*inode
, struct inode
*parent
)
561 struct address_space
*mapping
= inode
->i_mapping
;
562 struct folio
*folio
= filemap_grab_folio(mapping
, 0);
563 unsigned int chunk_size
= nilfs_chunk_size(inode
);
564 struct nilfs_dir_entry
*de
;
569 return PTR_ERR(folio
);
571 err
= nilfs_prepare_chunk(folio
, 0, chunk_size
);
576 kaddr
= kmap_local_folio(folio
, 0);
577 memset(kaddr
, 0, chunk_size
);
578 de
= (struct nilfs_dir_entry
*)kaddr
;
580 de
->rec_len
= nilfs_rec_len_to_disk(NILFS_DIR_REC_LEN(1));
581 memcpy(de
->name
, ".\0\0", 4);
582 de
->inode
= cpu_to_le64(inode
->i_ino
);
583 de
->file_type
= fs_umode_to_ftype(inode
->i_mode
);
585 de
= (struct nilfs_dir_entry
*)(kaddr
+ NILFS_DIR_REC_LEN(1));
587 de
->rec_len
= nilfs_rec_len_to_disk(chunk_size
- NILFS_DIR_REC_LEN(1));
588 de
->inode
= cpu_to_le64(parent
->i_ino
);
589 memcpy(de
->name
, "..\0", 4);
590 de
->file_type
= fs_umode_to_ftype(inode
->i_mode
);
592 nilfs_commit_chunk(folio
, mapping
, 0, chunk_size
);
599 * routine to check that the specified directory is empty (for rmdir)
601 int nilfs_empty_dir(struct inode
*inode
)
603 struct folio
*folio
= NULL
;
605 unsigned long i
, npages
= dir_pages(inode
);
607 for (i
= 0; i
< npages
; i
++) {
608 struct nilfs_dir_entry
*de
;
610 kaddr
= nilfs_get_folio(inode
, i
, &folio
);
614 de
= (struct nilfs_dir_entry
*)kaddr
;
615 kaddr
+= nilfs_last_byte(inode
, i
) - NILFS_DIR_REC_LEN(1);
617 while ((char *)de
<= kaddr
) {
618 if (de
->rec_len
== 0) {
619 nilfs_error(inode
->i_sb
,
620 "zero-length directory entry (kaddr=%p, de=%p)",
624 if (de
->inode
!= 0) {
625 /* check for . and .. */
626 if (de
->name
[0] != '.')
628 if (de
->name_len
> 2)
630 if (de
->name_len
< 2) {
632 cpu_to_le64(inode
->i_ino
))
634 } else if (de
->name
[1] != '.')
637 de
= nilfs_next_entry(de
);
639 folio_release_kmap(folio
, kaddr
);
644 folio_release_kmap(folio
, kaddr
);
648 const struct file_operations nilfs_dir_operations
= {
649 .llseek
= generic_file_llseek
,
650 .read
= generic_read_dir
,
651 .iterate_shared
= nilfs_readdir
,
652 .unlocked_ioctl
= nilfs_ioctl
,
654 .compat_ioctl
= nilfs_compat_ioctl
,
655 #endif /* CONFIG_COMPAT */
656 .fsync
= nilfs_sync_file
,