Linux 6.13-rc4
[linux.git] / fs / nilfs2 / dir.c
blob0a3aea6c416bc63562490fe0c5685febb5e4c28d
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * NILFS directory entry operations
5 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
7 * Modified for NILFS by Amagai Yoshiji.
8 */
9 /*
10 * linux/fs/ext2/dir.c
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)
17 * from
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
29 * and moved here. AV
32 #include <linux/pagemap.h>
33 #include "nilfs.h"
34 #include "page.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)
42 return 1 << 16;
43 #endif
44 return len;
47 static inline __le16 nilfs_rec_len_to_disk(unsigned int len)
49 #if (PAGE_SIZE >= 65536)
50 if (len == (1 << 16))
51 return cpu_to_le16(NILFS_MAX_REC_LEN);
53 BUG_ON(len > (1 << 16));
54 #endif
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;
78 return last_byte;
81 static int nilfs_prepare_chunk(struct folio *folio, unsigned int from,
82 unsigned int to)
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;
96 int err;
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);
102 if (IS_DIRSYNC(dir))
103 nilfs_set_transaction_flag(NILFS_TI_SYNC);
104 err = nilfs_set_file_dirty(dir, nr_dirty);
105 WARN_ON(err); /* do not happen */
106 folio_unlock(folio);
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;
117 char *error;
119 if (dir->i_size < folio_pos(folio) + limit) {
120 limit = dir->i_size - folio_pos(folio);
121 if (limit & (chunk_size - 1))
122 goto Ebadsize;
123 if (!limit)
124 goto out;
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))
131 goto Eshort;
132 if (rec_len & 3)
133 goto Ealign;
134 if (rec_len < NILFS_DIR_REC_LEN(p->name_len))
135 goto Enamelen;
136 if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
137 goto Espan;
138 if (unlikely(p->inode &&
139 NILFS_PRIVATE_INODE(le64_to_cpu(p->inode))))
140 goto Einumber;
142 if (offs != limit)
143 goto Eend;
144 out:
145 folio_set_checked(folio);
146 return true;
148 /* Too bad, we had an error */
150 Ebadsize:
151 nilfs_error(sb,
152 "size of directory #%lu is not a multiple of chunk size",
153 dir->i_ino);
154 goto fail;
155 Eshort:
156 error = "rec_len is smaller than minimal";
157 goto bad_entry;
158 Ealign:
159 error = "unaligned directory entry";
160 goto bad_entry;
161 Enamelen:
162 error = "rec_len is too small for name_len";
163 goto bad_entry;
164 Espan:
165 error = "directory entry across blocks";
166 goto bad_entry;
167 Einumber:
168 error = "disallowed inode number";
169 bad_entry:
170 nilfs_error(sb,
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);
175 goto fail;
176 Eend:
177 p = (struct nilfs_dir_entry *)(kaddr + offs);
178 nilfs_error(sb,
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));
182 fail:
183 return false;
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);
191 void *kaddr;
193 if (IS_ERR(folio))
194 return folio;
196 kaddr = kmap_local_folio(folio, 0);
197 if (unlikely(!folio_test_checked(folio))) {
198 if (!nilfs_check_folio(folio, kaddr))
199 goto fail;
202 *foliop = folio;
203 return kaddr;
205 fail:
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.
215 static int
216 nilfs_match(int len, const unsigned char *name, struct nilfs_dir_entry *de)
218 if (len != de->name_len)
219 return 0;
220 if (!de->inode)
221 return 0;
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))
244 return 0;
246 for ( ; n < npages; n++, offset = 0) {
247 char *kaddr, *limit;
248 struct nilfs_dir_entry *de;
249 struct folio *folio;
251 kaddr = nilfs_get_folio(inode, n, &folio);
252 if (IS_ERR(kaddr)) {
253 nilfs_error(sb, "bad page in #%lu", inode->i_ino);
254 ctx->pos += PAGE_SIZE - offset;
255 return -EIO;
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);
264 return -EIO;
266 if (de->inode) {
267 unsigned char t;
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);
274 return 0;
277 ctx->pos += nilfs_rec_len_from_disk(de->rec_len);
279 folio_release_kmap(folio, kaddr);
281 return 0;
285 * nilfs_find_entry()
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;
305 if (npages == 0)
306 goto out;
308 start = ei->i_dir_start_lookup;
309 if (start >= npages)
310 start = 0;
311 n = start;
312 do {
313 char *kaddr = nilfs_get_folio(dir, n, foliop);
315 if (IS_ERR(kaddr))
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);
325 goto out;
327 if (nilfs_match(namelen, name, de))
328 goto found;
329 de = nilfs_next_entry(de);
331 folio_release_kmap(*foliop, kaddr);
333 if (++n >= npages)
334 n = 0;
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);
341 goto out;
343 } while (n != start);
344 out:
345 return ERR_PTR(-ENOENT);
347 found:
348 ei->i_dir_start_lookup = n;
349 return de;
352 struct nilfs_dir_entry *nilfs_dotdot(struct inode *dir, struct folio **foliop)
354 struct folio *folio;
355 struct nilfs_dir_entry *de, *next_de;
356 size_t limit;
357 char *msg;
359 de = nilfs_get_folio(dir, 0, &folio);
360 if (IS_ERR(de))
361 return NULL;
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))) {
366 msg = "missing '.'";
367 goto fail;
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 '..'";
378 goto fail;
380 *foliop = folio;
381 return next_de;
383 fail:
384 nilfs_error(dir->i_sb, "directory #%lu %s", dir->i_ino, msg);
385 folio_release_kmap(folio, de);
386 return NULL;
389 int nilfs_inode_by_name(struct inode *dir, const struct qstr *qstr, ino_t *ino)
391 struct nilfs_dir_entry *de;
392 struct folio *folio;
394 de = nilfs_find_entry(dir, qstr, &folio);
395 if (IS_ERR(de))
396 return PTR_ERR(de);
398 *ino = le64_to_cpu(de->inode);
399 folio_release_kmap(folio, de);
400 return 0;
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;
409 int err;
411 folio_lock(folio);
412 err = nilfs_prepare_chunk(folio, from, to);
413 BUG_ON(err);
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));
421 * Parent is locked.
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);
434 unsigned long n;
435 size_t from, to;
436 int err;
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);
445 char *dir_end;
447 if (IS_ERR(kaddr))
448 return PTR_ERR(kaddr);
449 folio_lock(folio);
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) {
455 /* We hit i_size */
456 name_len = 0;
457 rec_len = chunk_size;
458 de->rec_len = nilfs_rec_len_to_disk(chunk_size);
459 de->inode = 0;
460 goto got_it;
462 if (de->rec_len == 0) {
463 nilfs_error(dir->i_sb,
464 "zero-length directory entry");
465 err = -EIO;
466 goto out_unlock;
468 err = -EEXIST;
469 if (nilfs_match(namelen, name, de))
470 goto out_unlock;
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)
474 goto got_it;
475 if (rec_len >= name_len + reclen)
476 goto got_it;
477 de = (struct nilfs_dir_entry *)((char *)de + rec_len);
479 folio_unlock(folio);
480 folio_release_kmap(folio, kaddr);
482 BUG();
483 return -EINVAL;
485 got_it:
486 from = offset_in_folio(folio, de);
487 to = from + rec_len;
488 err = nilfs_prepare_chunk(folio, from, to);
489 if (err)
490 goto out_unlock;
491 if (de->inode) {
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);
497 de = de1;
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);
506 /* OFFSET_CACHE */
507 out_put:
508 folio_release_kmap(folio, de);
509 return err;
510 out_unlock:
511 folio_unlock(folio);
512 goto out_put;
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));
524 size_t from, to;
525 struct nilfs_dir_entry *de, *pde = NULL;
526 int err;
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");
536 err = -EIO;
537 goto out;
539 pde = de;
540 de = nilfs_next_entry(de);
542 if (pde)
543 from = (char *)pde - kaddr;
544 folio_lock(folio);
545 err = nilfs_prepare_chunk(folio, from, to);
546 BUG_ON(err);
547 if (pde)
548 pde->rec_len = nilfs_rec_len_to_disk(to - from);
549 dir->inode = 0;
550 nilfs_commit_chunk(folio, mapping, from, to);
551 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
552 out:
553 return err;
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;
565 int err;
566 void *kaddr;
568 if (IS_ERR(folio))
569 return PTR_ERR(folio);
571 err = nilfs_prepare_chunk(folio, 0, chunk_size);
572 if (unlikely(err)) {
573 folio_unlock(folio);
574 goto fail;
576 kaddr = kmap_local_folio(folio, 0);
577 memset(kaddr, 0, chunk_size);
578 de = (struct nilfs_dir_entry *)kaddr;
579 de->name_len = 1;
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));
586 de->name_len = 2;
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);
591 kunmap_local(kaddr);
592 nilfs_commit_chunk(folio, mapping, 0, chunk_size);
593 fail:
594 folio_put(folio);
595 return err;
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;
604 char *kaddr;
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);
611 if (IS_ERR(kaddr))
612 return 0;
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)",
621 kaddr, de);
622 goto not_empty;
624 if (de->inode != 0) {
625 /* check for . and .. */
626 if (de->name[0] != '.')
627 goto not_empty;
628 if (de->name_len > 2)
629 goto not_empty;
630 if (de->name_len < 2) {
631 if (de->inode !=
632 cpu_to_le64(inode->i_ino))
633 goto not_empty;
634 } else if (de->name[1] != '.')
635 goto not_empty;
637 de = nilfs_next_entry(de);
639 folio_release_kmap(folio, kaddr);
641 return 1;
643 not_empty:
644 folio_release_kmap(folio, kaddr);
645 return 0;
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,
653 #ifdef CONFIG_COMPAT
654 .compat_ioctl = nilfs_compat_ioctl,
655 #endif /* CONFIG_COMPAT */
656 .fsync = nilfs_sync_file,