1 // SPDX-License-Identifier: GPL-2.0+
3 * dir.c - 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
;
67 static inline void nilfs_put_page(struct page
*page
)
74 * Return the offset into page `page_nr' of the last valid
75 * byte in that page, plus one.
77 static unsigned int nilfs_last_byte(struct inode
*inode
, unsigned long page_nr
)
79 unsigned int last_byte
= inode
->i_size
;
81 last_byte
-= page_nr
<< PAGE_SHIFT
;
82 if (last_byte
> PAGE_SIZE
)
83 last_byte
= PAGE_SIZE
;
87 static int nilfs_prepare_chunk(struct page
*page
, unsigned int from
,
90 loff_t pos
= page_offset(page
) + from
;
92 return __block_write_begin(page
, pos
, to
- from
, nilfs_get_block
);
95 static void nilfs_commit_chunk(struct page
*page
,
96 struct address_space
*mapping
,
97 unsigned int from
, unsigned int to
)
99 struct inode
*dir
= mapping
->host
;
100 loff_t pos
= page_offset(page
) + from
;
101 unsigned int len
= to
- from
;
102 unsigned int nr_dirty
, copied
;
105 nr_dirty
= nilfs_page_count_clean_buffers(page
, from
, to
);
106 copied
= block_write_end(NULL
, mapping
, pos
, len
, len
, page
, NULL
);
107 if (pos
+ copied
> dir
->i_size
)
108 i_size_write(dir
, pos
+ copied
);
110 nilfs_set_transaction_flag(NILFS_TI_SYNC
);
111 err
= nilfs_set_file_dirty(dir
, nr_dirty
);
112 WARN_ON(err
); /* do not happen */
116 static bool nilfs_check_page(struct page
*page
)
118 struct inode
*dir
= page
->mapping
->host
;
119 struct super_block
*sb
= dir
->i_sb
;
120 unsigned int chunk_size
= nilfs_chunk_size(dir
);
121 char *kaddr
= page_address(page
);
122 unsigned int offs
, rec_len
;
123 unsigned int limit
= PAGE_SIZE
;
124 struct nilfs_dir_entry
*p
;
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
- NILFS_DIR_REC_LEN(1); offs
+= rec_len
) {
135 p
= (struct nilfs_dir_entry
*)(kaddr
+ offs
);
136 rec_len
= nilfs_rec_len_from_disk(p
->rec_len
);
138 if (rec_len
< NILFS_DIR_REC_LEN(1))
142 if (rec_len
< NILFS_DIR_REC_LEN(p
->name_len
))
144 if (((offs
+ rec_len
- 1) ^ offs
) & ~(chunk_size
-1))
150 SetPageChecked(page
);
153 /* Too bad, we had an error */
157 "size of directory #%lu is not a multiple of chunk size",
161 error
= "rec_len is smaller than minimal";
164 error
= "unaligned directory entry";
167 error
= "rec_len is too small for name_len";
170 error
= "directory entry across blocks";
173 "bad entry in directory #%lu: %s - offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
174 dir
->i_ino
, error
, (page
->index
<< PAGE_SHIFT
) + offs
,
175 (unsigned long)le64_to_cpu(p
->inode
),
176 rec_len
, p
->name_len
);
179 p
= (struct nilfs_dir_entry
*)(kaddr
+ offs
);
181 "entry in directory #%lu spans the page boundary offset=%lu, inode=%lu",
182 dir
->i_ino
, (page
->index
<< PAGE_SHIFT
) + offs
,
183 (unsigned long)le64_to_cpu(p
->inode
));
189 static struct page
*nilfs_get_page(struct inode
*dir
, unsigned long n
)
191 struct address_space
*mapping
= dir
->i_mapping
;
192 struct page
*page
= read_mapping_page(mapping
, n
, NULL
);
196 if (unlikely(!PageChecked(page
))) {
197 if (PageError(page
) || !nilfs_check_page(page
))
204 nilfs_put_page(page
);
205 return ERR_PTR(-EIO
);
209 * NOTE! unlike strncmp, nilfs_match returns 1 for success, 0 for failure.
211 * len <= NILFS_NAME_LEN and de != NULL are guaranteed by caller.
214 nilfs_match(int len
, const unsigned char *name
, struct nilfs_dir_entry
*de
)
216 if (len
!= de
->name_len
)
220 return !memcmp(name
, de
->name
, len
);
224 * p is at least 6 bytes before the end of page
226 static struct nilfs_dir_entry
*nilfs_next_entry(struct nilfs_dir_entry
*p
)
228 return (struct nilfs_dir_entry
*)((char *)p
+
229 nilfs_rec_len_from_disk(p
->rec_len
));
233 nilfs_filetype_table
[NILFS_FT_MAX
] = {
234 [NILFS_FT_UNKNOWN
] = DT_UNKNOWN
,
235 [NILFS_FT_REG_FILE
] = DT_REG
,
236 [NILFS_FT_DIR
] = DT_DIR
,
237 [NILFS_FT_CHRDEV
] = DT_CHR
,
238 [NILFS_FT_BLKDEV
] = DT_BLK
,
239 [NILFS_FT_FIFO
] = DT_FIFO
,
240 [NILFS_FT_SOCK
] = DT_SOCK
,
241 [NILFS_FT_SYMLINK
] = DT_LNK
,
246 nilfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
247 [S_IFREG
>> S_SHIFT
] = NILFS_FT_REG_FILE
,
248 [S_IFDIR
>> S_SHIFT
] = NILFS_FT_DIR
,
249 [S_IFCHR
>> S_SHIFT
] = NILFS_FT_CHRDEV
,
250 [S_IFBLK
>> S_SHIFT
] = NILFS_FT_BLKDEV
,
251 [S_IFIFO
>> S_SHIFT
] = NILFS_FT_FIFO
,
252 [S_IFSOCK
>> S_SHIFT
] = NILFS_FT_SOCK
,
253 [S_IFLNK
>> S_SHIFT
] = NILFS_FT_SYMLINK
,
256 static void nilfs_set_de_type(struct nilfs_dir_entry
*de
, struct inode
*inode
)
258 umode_t mode
= inode
->i_mode
;
260 de
->file_type
= nilfs_type_by_mode
[(mode
& S_IFMT
)>>S_SHIFT
];
263 static int nilfs_readdir(struct file
*file
, struct dir_context
*ctx
)
265 loff_t pos
= ctx
->pos
;
266 struct inode
*inode
= file_inode(file
);
267 struct super_block
*sb
= inode
->i_sb
;
268 unsigned int offset
= pos
& ~PAGE_MASK
;
269 unsigned long n
= pos
>> PAGE_SHIFT
;
270 unsigned long npages
= dir_pages(inode
);
272 if (pos
> inode
->i_size
- NILFS_DIR_REC_LEN(1))
275 for ( ; n
< npages
; n
++, offset
= 0) {
277 struct nilfs_dir_entry
*de
;
278 struct page
*page
= nilfs_get_page(inode
, n
);
281 nilfs_error(sb
, "bad page in #%lu", inode
->i_ino
);
282 ctx
->pos
+= PAGE_SIZE
- offset
;
285 kaddr
= page_address(page
);
286 de
= (struct nilfs_dir_entry
*)(kaddr
+ offset
);
287 limit
= kaddr
+ nilfs_last_byte(inode
, n
) -
288 NILFS_DIR_REC_LEN(1);
289 for ( ; (char *)de
<= limit
; de
= nilfs_next_entry(de
)) {
290 if (de
->rec_len
== 0) {
291 nilfs_error(sb
, "zero-length directory entry");
292 nilfs_put_page(page
);
298 if (de
->file_type
< NILFS_FT_MAX
)
299 t
= nilfs_filetype_table
[de
->file_type
];
303 if (!dir_emit(ctx
, de
->name
, de
->name_len
,
304 le64_to_cpu(de
->inode
), t
)) {
305 nilfs_put_page(page
);
309 ctx
->pos
+= nilfs_rec_len_from_disk(de
->rec_len
);
311 nilfs_put_page(page
);
319 * finds an entry in the specified directory with the wanted name. It
320 * returns the page in which the entry was found, and the entry itself
321 * (as a parameter - res_dir). Page is returned mapped and unlocked.
322 * Entry is guaranteed to be valid.
324 struct nilfs_dir_entry
*
325 nilfs_find_entry(struct inode
*dir
, const struct qstr
*qstr
,
326 struct page
**res_page
)
328 const unsigned char *name
= qstr
->name
;
329 int namelen
= qstr
->len
;
330 unsigned int reclen
= NILFS_DIR_REC_LEN(namelen
);
331 unsigned long start
, n
;
332 unsigned long npages
= dir_pages(dir
);
333 struct page
*page
= NULL
;
334 struct nilfs_inode_info
*ei
= NILFS_I(dir
);
335 struct nilfs_dir_entry
*de
;
343 start
= ei
->i_dir_start_lookup
;
350 page
= nilfs_get_page(dir
, n
);
352 kaddr
= page_address(page
);
353 de
= (struct nilfs_dir_entry
*)kaddr
;
354 kaddr
+= nilfs_last_byte(dir
, n
) - reclen
;
355 while ((char *) de
<= kaddr
) {
356 if (de
->rec_len
== 0) {
357 nilfs_error(dir
->i_sb
,
358 "zero-length directory entry");
359 nilfs_put_page(page
);
362 if (nilfs_match(namelen
, name
, de
))
364 de
= nilfs_next_entry(de
);
366 nilfs_put_page(page
);
370 /* next page is past the blocks we've got */
371 if (unlikely(n
> (dir
->i_blocks
>> (PAGE_SHIFT
- 9)))) {
372 nilfs_error(dir
->i_sb
,
373 "dir %lu size %lld exceeds block count %llu",
374 dir
->i_ino
, dir
->i_size
,
375 (unsigned long long)dir
->i_blocks
);
378 } while (n
!= start
);
384 ei
->i_dir_start_lookup
= n
;
388 struct nilfs_dir_entry
*nilfs_dotdot(struct inode
*dir
, struct page
**p
)
390 struct page
*page
= nilfs_get_page(dir
, 0);
391 struct nilfs_dir_entry
*de
= NULL
;
394 de
= nilfs_next_entry(
395 (struct nilfs_dir_entry
*)page_address(page
));
401 ino_t
nilfs_inode_by_name(struct inode
*dir
, const struct qstr
*qstr
)
404 struct nilfs_dir_entry
*de
;
407 de
= nilfs_find_entry(dir
, qstr
, &page
);
409 res
= le64_to_cpu(de
->inode
);
416 /* Releases the page */
417 void nilfs_set_link(struct inode
*dir
, struct nilfs_dir_entry
*de
,
418 struct page
*page
, struct inode
*inode
)
420 unsigned int from
= (char *)de
- (char *)page_address(page
);
421 unsigned int to
= from
+ nilfs_rec_len_from_disk(de
->rec_len
);
422 struct address_space
*mapping
= page
->mapping
;
426 err
= nilfs_prepare_chunk(page
, from
, to
);
428 de
->inode
= cpu_to_le64(inode
->i_ino
);
429 nilfs_set_de_type(de
, inode
);
430 nilfs_commit_chunk(page
, mapping
, from
, to
);
431 nilfs_put_page(page
);
432 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
438 int nilfs_add_link(struct dentry
*dentry
, struct inode
*inode
)
440 struct inode
*dir
= d_inode(dentry
->d_parent
);
441 const unsigned char *name
= dentry
->d_name
.name
;
442 int namelen
= dentry
->d_name
.len
;
443 unsigned int chunk_size
= nilfs_chunk_size(dir
);
444 unsigned int reclen
= NILFS_DIR_REC_LEN(namelen
);
445 unsigned short rec_len
, name_len
;
446 struct page
*page
= NULL
;
447 struct nilfs_dir_entry
*de
;
448 unsigned long npages
= dir_pages(dir
);
451 unsigned int from
, to
;
455 * We take care of directory expansion in the same loop.
456 * This code plays outside i_size, so it locks the page
457 * to protect that region.
459 for (n
= 0; n
<= npages
; n
++) {
462 page
= nilfs_get_page(dir
, n
);
467 kaddr
= page_address(page
);
468 dir_end
= kaddr
+ nilfs_last_byte(dir
, n
);
469 de
= (struct nilfs_dir_entry
*)kaddr
;
470 kaddr
+= PAGE_SIZE
- reclen
;
471 while ((char *)de
<= kaddr
) {
472 if ((char *)de
== dir_end
) {
475 rec_len
= chunk_size
;
476 de
->rec_len
= nilfs_rec_len_to_disk(chunk_size
);
480 if (de
->rec_len
== 0) {
481 nilfs_error(dir
->i_sb
,
482 "zero-length directory entry");
487 if (nilfs_match(namelen
, name
, de
))
489 name_len
= NILFS_DIR_REC_LEN(de
->name_len
);
490 rec_len
= nilfs_rec_len_from_disk(de
->rec_len
);
491 if (!de
->inode
&& rec_len
>= reclen
)
493 if (rec_len
>= name_len
+ reclen
)
495 de
= (struct nilfs_dir_entry
*)((char *)de
+ rec_len
);
498 nilfs_put_page(page
);
504 from
= (char *)de
- (char *)page_address(page
);
506 err
= nilfs_prepare_chunk(page
, from
, to
);
510 struct nilfs_dir_entry
*de1
;
512 de1
= (struct nilfs_dir_entry
*)((char *)de
+ name_len
);
513 de1
->rec_len
= nilfs_rec_len_to_disk(rec_len
- name_len
);
514 de
->rec_len
= nilfs_rec_len_to_disk(name_len
);
517 de
->name_len
= namelen
;
518 memcpy(de
->name
, name
, namelen
);
519 de
->inode
= cpu_to_le64(inode
->i_ino
);
520 nilfs_set_de_type(de
, inode
);
521 nilfs_commit_chunk(page
, page
->mapping
, from
, to
);
522 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
523 nilfs_mark_inode_dirty(dir
);
526 nilfs_put_page(page
);
535 * nilfs_delete_entry deletes a directory entry by merging it with the
536 * previous entry. Page is up-to-date. Releases the page.
538 int nilfs_delete_entry(struct nilfs_dir_entry
*dir
, struct page
*page
)
540 struct address_space
*mapping
= page
->mapping
;
541 struct inode
*inode
= mapping
->host
;
542 char *kaddr
= page_address(page
);
543 unsigned int from
, to
;
544 struct nilfs_dir_entry
*de
, *pde
= NULL
;
547 from
= ((char *)dir
- kaddr
) & ~(nilfs_chunk_size(inode
) - 1);
548 to
= ((char *)dir
- kaddr
) + nilfs_rec_len_from_disk(dir
->rec_len
);
549 de
= (struct nilfs_dir_entry
*)(kaddr
+ from
);
551 while ((char *)de
< (char *)dir
) {
552 if (de
->rec_len
== 0) {
553 nilfs_error(inode
->i_sb
,
554 "zero-length directory entry");
559 de
= nilfs_next_entry(de
);
562 from
= (char *)pde
- (char *)page_address(page
);
564 err
= nilfs_prepare_chunk(page
, from
, to
);
567 pde
->rec_len
= nilfs_rec_len_to_disk(to
- from
);
569 nilfs_commit_chunk(page
, mapping
, from
, to
);
570 inode
->i_ctime
= inode
->i_mtime
= current_time(inode
);
572 nilfs_put_page(page
);
577 * Set the first fragment of directory.
579 int nilfs_make_empty(struct inode
*inode
, struct inode
*parent
)
581 struct address_space
*mapping
= inode
->i_mapping
;
582 struct page
*page
= grab_cache_page(mapping
, 0);
583 unsigned int chunk_size
= nilfs_chunk_size(inode
);
584 struct nilfs_dir_entry
*de
;
591 err
= nilfs_prepare_chunk(page
, 0, chunk_size
);
596 kaddr
= kmap_atomic(page
);
597 memset(kaddr
, 0, chunk_size
);
598 de
= (struct nilfs_dir_entry
*)kaddr
;
600 de
->rec_len
= nilfs_rec_len_to_disk(NILFS_DIR_REC_LEN(1));
601 memcpy(de
->name
, ".\0\0", 4);
602 de
->inode
= cpu_to_le64(inode
->i_ino
);
603 nilfs_set_de_type(de
, inode
);
605 de
= (struct nilfs_dir_entry
*)(kaddr
+ NILFS_DIR_REC_LEN(1));
607 de
->rec_len
= nilfs_rec_len_to_disk(chunk_size
- NILFS_DIR_REC_LEN(1));
608 de
->inode
= cpu_to_le64(parent
->i_ino
);
609 memcpy(de
->name
, "..\0", 4);
610 nilfs_set_de_type(de
, inode
);
611 kunmap_atomic(kaddr
);
612 nilfs_commit_chunk(page
, mapping
, 0, chunk_size
);
619 * routine to check that the specified directory is empty (for rmdir)
621 int nilfs_empty_dir(struct inode
*inode
)
623 struct page
*page
= NULL
;
624 unsigned long i
, npages
= dir_pages(inode
);
626 for (i
= 0; i
< npages
; i
++) {
628 struct nilfs_dir_entry
*de
;
630 page
= nilfs_get_page(inode
, i
);
634 kaddr
= page_address(page
);
635 de
= (struct nilfs_dir_entry
*)kaddr
;
636 kaddr
+= nilfs_last_byte(inode
, i
) - NILFS_DIR_REC_LEN(1);
638 while ((char *)de
<= kaddr
) {
639 if (de
->rec_len
== 0) {
640 nilfs_error(inode
->i_sb
,
641 "zero-length directory entry (kaddr=%p, de=%p)",
645 if (de
->inode
!= 0) {
646 /* check for . and .. */
647 if (de
->name
[0] != '.')
649 if (de
->name_len
> 2)
651 if (de
->name_len
< 2) {
653 cpu_to_le64(inode
->i_ino
))
655 } else if (de
->name
[1] != '.')
658 de
= nilfs_next_entry(de
);
660 nilfs_put_page(page
);
665 nilfs_put_page(page
);
669 const struct file_operations nilfs_dir_operations
= {
670 .llseek
= generic_file_llseek
,
671 .read
= generic_read_dir
,
672 .iterate_shared
= nilfs_readdir
,
673 .unlocked_ioctl
= nilfs_ioctl
,
675 .compat_ioctl
= nilfs_compat_ioctl
,
676 #endif /* CONFIG_COMPAT */
677 .fsync
= nilfs_sync_file
,