xref: /linux/fs/nilfs2/dir.c (revision 43102a2012c2e2f8424d7eef52aede8e73cf2fed)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * NILFS directory entry operations
4  *
5  * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6  *
7  * Modified for NILFS by Amagai Yoshiji.
8  */
9 /*
10  *  linux/fs/ext2/dir.c
11  *
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)
16  *
17  *  from
18  *
19  *  linux/fs/minix/dir.c
20  *
21  *  Copyright (C) 1991, 1992  Linus Torvalds
22  *
23  *  ext2 directory handling functions
24  *
25  *  Big-endian to little-endian byte-swapping/bitmaps by
26  *        David S. Miller (davem@caip.rutgers.edu), 1995
27  *
28  * All code that works with directory layout had been switched to pagecache
29  * and moved here. AV
30  */
31 
32 #include <linux/pagemap.h>
33 #include "nilfs.h"
34 #include "page.h"
35 
36 static inline unsigned int nilfs_rec_len_from_disk(__le16 dlen)
37 {
38 	unsigned int len = le16_to_cpu(dlen);
39 
40 #if (PAGE_SIZE >= 65536)
41 	if (len == NILFS_MAX_REC_LEN)
42 		return 1 << 16;
43 #endif
44 	return len;
45 }
46 
47 static inline __le16 nilfs_rec_len_to_disk(unsigned int len)
48 {
49 #if (PAGE_SIZE >= 65536)
50 	if (len == (1 << 16))
51 		return cpu_to_le16(NILFS_MAX_REC_LEN);
52 
53 	BUG_ON(len > (1 << 16));
54 #endif
55 	return cpu_to_le16(len);
56 }
57 
58 /*
59  * nilfs uses block-sized chunks. Arguably, sector-sized ones would be
60  * more robust, but we have what we have
61  */
62 static inline unsigned int nilfs_chunk_size(struct inode *inode)
63 {
64 	return inode->i_sb->s_blocksize;
65 }
66 
67 /*
68  * Return the offset into page `page_nr' of the last valid
69  * byte in that page, plus one.
70  */
71 static unsigned int nilfs_last_byte(struct inode *inode, unsigned long page_nr)
72 {
73 	unsigned int last_byte = inode->i_size;
74 
75 	last_byte -= page_nr << PAGE_SHIFT;
76 	if (last_byte > PAGE_SIZE)
77 		last_byte = PAGE_SIZE;
78 	return last_byte;
79 }
80 
81 static int nilfs_prepare_chunk(struct folio *folio, unsigned int from,
82 			       unsigned int to)
83 {
84 	loff_t pos = folio_pos(folio) + from;
85 
86 	return __block_write_begin(folio, pos, to - from, nilfs_get_block);
87 }
88 
89 static void nilfs_commit_chunk(struct folio *folio,
90 		struct address_space *mapping, size_t from, size_t to)
91 {
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;
97 
98 	nr_dirty = nilfs_page_count_clean_buffers(&folio->page, 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);
107 }
108 
109 static bool nilfs_check_folio(struct folio *folio, char *kaddr)
110 {
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;
118 
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;
125 	}
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);
129 
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;
141 	}
142 	if (offs != limit)
143 		goto Eend;
144 out:
145 	folio_set_checked(folio);
146 	return true;
147 
148 	/* Too bad, we had an error */
149 
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;
184 }
185 
186 static void *nilfs_get_folio(struct inode *dir, unsigned long n,
187 		struct folio **foliop)
188 {
189 	struct address_space *mapping = dir->i_mapping;
190 	struct folio *folio = read_mapping_folio(mapping, n, NULL);
191 	void *kaddr;
192 
193 	if (IS_ERR(folio))
194 		return folio;
195 
196 	kaddr = kmap_local_folio(folio, 0);
197 	if (unlikely(!folio_test_checked(folio))) {
198 		if (!nilfs_check_folio(folio, kaddr))
199 			goto fail;
200 	}
201 
202 	*foliop = folio;
203 	return kaddr;
204 
205 fail:
206 	folio_release_kmap(folio, kaddr);
207 	return ERR_PTR(-EIO);
208 }
209 
210 /*
211  * NOTE! unlike strncmp, nilfs_match returns 1 for success, 0 for failure.
212  *
213  * len <= NILFS_NAME_LEN and de != NULL are guaranteed by caller.
214  */
215 static int
216 nilfs_match(int len, const unsigned char *name, struct nilfs_dir_entry *de)
217 {
218 	if (len != de->name_len)
219 		return 0;
220 	if (!de->inode)
221 		return 0;
222 	return !memcmp(name, de->name, len);
223 }
224 
225 /*
226  * p is at least 6 bytes before the end of page
227  */
228 static struct nilfs_dir_entry *nilfs_next_entry(struct nilfs_dir_entry *p)
229 {
230 	return (struct nilfs_dir_entry *)((char *)p +
231 					  nilfs_rec_len_from_disk(p->rec_len));
232 }
233 
234 static int nilfs_readdir(struct file *file, struct dir_context *ctx)
235 {
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);
242 
243 	if (pos > inode->i_size - NILFS_DIR_REC_LEN(1))
244 		return 0;
245 
246 	for ( ; n < npages; n++, offset = 0) {
247 		char *kaddr, *limit;
248 		struct nilfs_dir_entry *de;
249 		struct folio *folio;
250 
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;
256 		}
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;
265 			}
266 			if (de->inode) {
267 				unsigned char t;
268 
269 				t = fs_ftype_to_dtype(de->file_type);
270 
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;
275 				}
276 			}
277 			ctx->pos += nilfs_rec_len_from_disk(de->rec_len);
278 		}
279 		folio_release_kmap(folio, kaddr);
280 	}
281 	return 0;
282 }
283 
284 /*
285  * nilfs_find_entry()
286  *
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().
291  *
292  * On failure, returns NULL and the caller should ignore foliop.
293  */
294 struct nilfs_dir_entry *nilfs_find_entry(struct inode *dir,
295 		const struct qstr *qstr, struct folio **foliop)
296 {
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;
304 
305 	if (npages == 0)
306 		goto out;
307 
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);
314 
315 		if (!IS_ERR(kaddr)) {
316 			de = (struct nilfs_dir_entry *)kaddr;
317 			kaddr += nilfs_last_byte(dir, n) - reclen;
318 			while ((char *) de <= kaddr) {
319 				if (de->rec_len == 0) {
320 					nilfs_error(dir->i_sb,
321 						"zero-length directory entry");
322 					folio_release_kmap(*foliop, kaddr);
323 					goto out;
324 				}
325 				if (nilfs_match(namelen, name, de))
326 					goto found;
327 				de = nilfs_next_entry(de);
328 			}
329 			folio_release_kmap(*foliop, kaddr);
330 		}
331 		if (++n >= npages)
332 			n = 0;
333 		/* next folio is past the blocks we've got */
334 		if (unlikely(n > (dir->i_blocks >> (PAGE_SHIFT - 9)))) {
335 			nilfs_error(dir->i_sb,
336 			       "dir %lu size %lld exceeds block count %llu",
337 			       dir->i_ino, dir->i_size,
338 			       (unsigned long long)dir->i_blocks);
339 			goto out;
340 		}
341 	} while (n != start);
342 out:
343 	return NULL;
344 
345 found:
346 	ei->i_dir_start_lookup = n;
347 	return de;
348 }
349 
350 struct nilfs_dir_entry *nilfs_dotdot(struct inode *dir, struct folio **foliop)
351 {
352 	struct folio *folio;
353 	struct nilfs_dir_entry *de, *next_de;
354 	size_t limit;
355 	char *msg;
356 
357 	de = nilfs_get_folio(dir, 0, &folio);
358 	if (IS_ERR(de))
359 		return NULL;
360 
361 	limit = nilfs_last_byte(dir, 0);  /* is a multiple of chunk size */
362 	if (unlikely(!limit || le64_to_cpu(de->inode) != dir->i_ino ||
363 		     !nilfs_match(1, ".", de))) {
364 		msg = "missing '.'";
365 		goto fail;
366 	}
367 
368 	next_de = nilfs_next_entry(de);
369 	/*
370 	 * If "next_de" has not reached the end of the chunk, there is
371 	 * at least one more record.  Check whether it matches "..".
372 	 */
373 	if (unlikely((char *)next_de == (char *)de + nilfs_chunk_size(dir) ||
374 		     !nilfs_match(2, "..", next_de))) {
375 		msg = "missing '..'";
376 		goto fail;
377 	}
378 	*foliop = folio;
379 	return next_de;
380 
381 fail:
382 	nilfs_error(dir->i_sb, "directory #%lu %s", dir->i_ino, msg);
383 	folio_release_kmap(folio, de);
384 	return NULL;
385 }
386 
387 ino_t nilfs_inode_by_name(struct inode *dir, const struct qstr *qstr)
388 {
389 	ino_t res = 0;
390 	struct nilfs_dir_entry *de;
391 	struct folio *folio;
392 
393 	de = nilfs_find_entry(dir, qstr, &folio);
394 	if (de) {
395 		res = le64_to_cpu(de->inode);
396 		folio_release_kmap(folio, de);
397 	}
398 	return res;
399 }
400 
401 void nilfs_set_link(struct inode *dir, struct nilfs_dir_entry *de,
402 		    struct folio *folio, struct inode *inode)
403 {
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;
407 	int err;
408 
409 	folio_lock(folio);
410 	err = nilfs_prepare_chunk(folio, from, to);
411 	BUG_ON(err);
412 	de->inode = cpu_to_le64(inode->i_ino);
413 	de->file_type = fs_umode_to_ftype(inode->i_mode);
414 	nilfs_commit_chunk(folio, mapping, from, to);
415 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
416 }
417 
418 /*
419  *	Parent is locked.
420  */
421 int nilfs_add_link(struct dentry *dentry, struct inode *inode)
422 {
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);
432 	unsigned long n;
433 	size_t from, to;
434 	int err;
435 
436 	/*
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.
440 	 */
441 	for (n = 0; n <= npages; n++) {
442 		char *kaddr = nilfs_get_folio(dir, n, &folio);
443 		char *dir_end;
444 
445 		if (IS_ERR(kaddr))
446 			return PTR_ERR(kaddr);
447 		folio_lock(folio);
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) {
453 				/* We hit i_size */
454 				name_len = 0;
455 				rec_len = chunk_size;
456 				de->rec_len = nilfs_rec_len_to_disk(chunk_size);
457 				de->inode = 0;
458 				goto got_it;
459 			}
460 			if (de->rec_len == 0) {
461 				nilfs_error(dir->i_sb,
462 					    "zero-length directory entry");
463 				err = -EIO;
464 				goto out_unlock;
465 			}
466 			err = -EEXIST;
467 			if (nilfs_match(namelen, name, de))
468 				goto out_unlock;
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)
472 				goto got_it;
473 			if (rec_len >= name_len + reclen)
474 				goto got_it;
475 			de = (struct nilfs_dir_entry *)((char *)de + rec_len);
476 		}
477 		folio_unlock(folio);
478 		folio_release_kmap(folio, kaddr);
479 	}
480 	BUG();
481 	return -EINVAL;
482 
483 got_it:
484 	from = offset_in_folio(folio, de);
485 	to = from + rec_len;
486 	err = nilfs_prepare_chunk(folio, from, to);
487 	if (err)
488 		goto out_unlock;
489 	if (de->inode) {
490 		struct nilfs_dir_entry *de1;
491 
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);
495 		de = de1;
496 	}
497 	de->name_len = namelen;
498 	memcpy(de->name, name, namelen);
499 	de->inode = cpu_to_le64(inode->i_ino);
500 	de->file_type = fs_umode_to_ftype(inode->i_mode);
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);
504 	/* OFFSET_CACHE */
505 out_put:
506 	folio_release_kmap(folio, de);
507 	return err;
508 out_unlock:
509 	folio_unlock(folio);
510 	goto out_put;
511 }
512 
513 /*
514  * nilfs_delete_entry deletes a directory entry by merging it with the
515  * previous entry. Folio is up-to-date.
516  */
517 int nilfs_delete_entry(struct nilfs_dir_entry *dir, struct folio *folio)
518 {
519 	struct address_space *mapping = folio->mapping;
520 	struct inode *inode = mapping->host;
521 	char *kaddr = (char *)((unsigned long)dir & ~(folio_size(folio) - 1));
522 	size_t from, to;
523 	struct nilfs_dir_entry *de, *pde = NULL;
524 	int err;
525 
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);
529 
530 	while ((char *)de < (char *)dir) {
531 		if (de->rec_len == 0) {
532 			nilfs_error(inode->i_sb,
533 				    "zero-length directory entry");
534 			err = -EIO;
535 			goto out;
536 		}
537 		pde = de;
538 		de = nilfs_next_entry(de);
539 	}
540 	if (pde)
541 		from = (char *)pde - kaddr;
542 	folio_lock(folio);
543 	err = nilfs_prepare_chunk(folio, from, to);
544 	BUG_ON(err);
545 	if (pde)
546 		pde->rec_len = nilfs_rec_len_to_disk(to - from);
547 	dir->inode = 0;
548 	nilfs_commit_chunk(folio, mapping, from, to);
549 	inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
550 out:
551 	return err;
552 }
553 
554 /*
555  * Set the first fragment of directory.
556  */
557 int nilfs_make_empty(struct inode *inode, struct inode *parent)
558 {
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;
563 	int err;
564 	void *kaddr;
565 
566 	if (IS_ERR(folio))
567 		return PTR_ERR(folio);
568 
569 	err = nilfs_prepare_chunk(folio, 0, chunk_size);
570 	if (unlikely(err)) {
571 		folio_unlock(folio);
572 		goto fail;
573 	}
574 	kaddr = kmap_local_folio(folio, 0);
575 	memset(kaddr, 0, chunk_size);
576 	de = (struct nilfs_dir_entry *)kaddr;
577 	de->name_len = 1;
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 	de->file_type = fs_umode_to_ftype(inode->i_mode);
582 
583 	de = (struct nilfs_dir_entry *)(kaddr + NILFS_DIR_REC_LEN(1));
584 	de->name_len = 2;
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 	de->file_type = fs_umode_to_ftype(inode->i_mode);
589 	kunmap_local(kaddr);
590 	nilfs_commit_chunk(folio, mapping, 0, chunk_size);
591 fail:
592 	folio_put(folio);
593 	return err;
594 }
595 
596 /*
597  * routine to check that the specified directory is empty (for rmdir)
598  */
599 int nilfs_empty_dir(struct inode *inode)
600 {
601 	struct folio *folio = NULL;
602 	char *kaddr;
603 	unsigned long i, npages = dir_pages(inode);
604 
605 	for (i = 0; i < npages; i++) {
606 		struct nilfs_dir_entry *de;
607 
608 		kaddr = nilfs_get_folio(inode, i, &folio);
609 		if (IS_ERR(kaddr))
610 			return 0;
611 
612 		de = (struct nilfs_dir_entry *)kaddr;
613 		kaddr += nilfs_last_byte(inode, i) - NILFS_DIR_REC_LEN(1);
614 
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)",
619 					    kaddr, de);
620 				goto not_empty;
621 			}
622 			if (de->inode != 0) {
623 				/* check for . and .. */
624 				if (de->name[0] != '.')
625 					goto not_empty;
626 				if (de->name_len > 2)
627 					goto not_empty;
628 				if (de->name_len < 2) {
629 					if (de->inode !=
630 					    cpu_to_le64(inode->i_ino))
631 						goto not_empty;
632 				} else if (de->name[1] != '.')
633 					goto not_empty;
634 			}
635 			de = nilfs_next_entry(de);
636 		}
637 		folio_release_kmap(folio, kaddr);
638 	}
639 	return 1;
640 
641 not_empty:
642 	folio_release_kmap(folio, kaddr);
643 	return 0;
644 }
645 
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,
651 #ifdef CONFIG_COMPAT
652 	.compat_ioctl	= nilfs_compat_ioctl,
653 #endif	/* CONFIG_COMPAT */
654 	.fsync		= nilfs_sync_file,
655 
656 };
657