xref: /linux/fs/f2fs/dir.c (revision 86d563ac5fb0c6f404e82692581bb67a6f35e5de)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * fs/f2fs/dir.c
4  *
5  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6  *             http://www.samsung.com/
7  */
8 #include <linux/unaligned.h>
9 #include <linux/fs.h>
10 #include <linux/f2fs_fs.h>
11 #include <linux/sched/signal.h>
12 #include <linux/unicode.h>
13 #include "f2fs.h"
14 #include "node.h"
15 #include "acl.h"
16 #include "xattr.h"
17 #include <trace/events/f2fs.h>
18 
f2fs_should_fallback_to_linear(struct inode * dir)19 static inline bool f2fs_should_fallback_to_linear(struct inode *dir)
20 {
21 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
22 
23 	switch (F2FS_OPTION(sbi).lookup_mode) {
24 	case LOOKUP_PERF:
25 		return false;
26 	case LOOKUP_COMPAT:
27 		return true;
28 	case LOOKUP_AUTO:
29 		return !sb_no_casefold_compat_fallback(sbi->sb);
30 	}
31 	return false;
32 }
33 
34 #if IS_ENABLED(CONFIG_UNICODE)
35 extern struct kmem_cache *f2fs_cf_name_slab;
36 #endif
37 
dir_blocks(struct inode * inode)38 static unsigned long dir_blocks(struct inode *inode)
39 {
40 	return ((unsigned long long) (i_size_read(inode) + PAGE_SIZE - 1))
41 							>> PAGE_SHIFT;
42 }
43 
dir_buckets(unsigned int level,int dir_level)44 static unsigned int dir_buckets(unsigned int level, int dir_level)
45 {
46 	if (level + dir_level < MAX_DIR_HASH_DEPTH / 2)
47 		return BIT(level + dir_level);
48 	else
49 		return MAX_DIR_BUCKETS;
50 }
51 
bucket_blocks(unsigned int level)52 static unsigned int bucket_blocks(unsigned int level)
53 {
54 	if (level < MAX_DIR_HASH_DEPTH / 2)
55 		return 2;
56 	else
57 		return 4;
58 }
59 
60 #if IS_ENABLED(CONFIG_UNICODE)
61 /* If @dir is casefolded, initialize @fname->cf_name from @fname->usr_fname. */
f2fs_init_casefolded_name(const struct inode * dir,struct f2fs_filename * fname)62 int f2fs_init_casefolded_name(const struct inode *dir,
63 			      struct f2fs_filename *fname)
64 {
65 	struct super_block *sb = dir->i_sb;
66 	unsigned char *buf;
67 	int len;
68 
69 	if (IS_CASEFOLDED(dir) &&
70 	    !is_dot_dotdot(fname->usr_fname->name, fname->usr_fname->len)) {
71 		buf = f2fs_kmem_cache_alloc(f2fs_cf_name_slab,
72 					    GFP_NOFS, false, F2FS_SB(sb));
73 		if (!buf)
74 			return -ENOMEM;
75 
76 		len = utf8_casefold(sb->s_encoding, fname->usr_fname,
77 				    buf, F2FS_NAME_LEN);
78 		if (len <= 0) {
79 			kmem_cache_free(f2fs_cf_name_slab, buf);
80 			if (sb_has_strict_encoding(sb))
81 				return -EINVAL;
82 			/* fall back to treating name as opaque byte sequence */
83 			return 0;
84 		}
85 		fname->cf_name.name = buf;
86 		fname->cf_name.len = len;
87 	}
88 
89 	return 0;
90 }
91 
f2fs_free_casefolded_name(struct f2fs_filename * fname)92 void f2fs_free_casefolded_name(struct f2fs_filename *fname)
93 {
94 	unsigned char *buf = (unsigned char *)fname->cf_name.name;
95 
96 	if (buf) {
97 		kmem_cache_free(f2fs_cf_name_slab, buf);
98 		fname->cf_name.name = NULL;
99 	}
100 }
101 #endif /* CONFIG_UNICODE */
102 
__f2fs_setup_filename(const struct inode * dir,const struct fscrypt_name * crypt_name,struct f2fs_filename * fname)103 static int __f2fs_setup_filename(const struct inode *dir,
104 				 const struct fscrypt_name *crypt_name,
105 				 struct f2fs_filename *fname)
106 {
107 	int err;
108 
109 	memset(fname, 0, sizeof(*fname));
110 
111 	fname->usr_fname = crypt_name->usr_fname;
112 	fname->disk_name = crypt_name->disk_name;
113 #ifdef CONFIG_FS_ENCRYPTION
114 	fname->crypto_buf = crypt_name->crypto_buf;
115 #endif
116 	if (crypt_name->is_nokey_name) {
117 		/* hash was decoded from the no-key name */
118 		fname->hash = cpu_to_le32(crypt_name->hash);
119 	} else {
120 		err = f2fs_init_casefolded_name(dir, fname);
121 		if (err) {
122 			f2fs_free_filename(fname);
123 			return err;
124 		}
125 		f2fs_hash_filename(dir, fname);
126 	}
127 	return 0;
128 }
129 
130 /*
131  * Prepare to search for @iname in @dir.  This is similar to
132  * fscrypt_setup_filename(), but this also handles computing the casefolded name
133  * and the f2fs dirhash if needed, then packing all the information about this
134  * filename up into a 'struct f2fs_filename'.
135  */
f2fs_setup_filename(struct inode * dir,const struct qstr * iname,int lookup,struct f2fs_filename * fname)136 int f2fs_setup_filename(struct inode *dir, const struct qstr *iname,
137 			int lookup, struct f2fs_filename *fname)
138 {
139 	struct fscrypt_name crypt_name;
140 	int err;
141 
142 	err = fscrypt_setup_filename(dir, iname, lookup, &crypt_name);
143 	if (err)
144 		return err;
145 
146 	return __f2fs_setup_filename(dir, &crypt_name, fname);
147 }
148 
149 /*
150  * Prepare to look up @dentry in @dir.  This is similar to
151  * fscrypt_prepare_lookup(), but this also handles computing the casefolded name
152  * and the f2fs dirhash if needed, then packing all the information about this
153  * filename up into a 'struct f2fs_filename'.
154  */
f2fs_prepare_lookup(struct inode * dir,struct dentry * dentry,struct f2fs_filename * fname)155 int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry,
156 			struct f2fs_filename *fname)
157 {
158 	struct fscrypt_name crypt_name;
159 	int err;
160 
161 	err = fscrypt_prepare_lookup(dir, dentry, &crypt_name);
162 	if (err)
163 		return err;
164 
165 	return __f2fs_setup_filename(dir, &crypt_name, fname);
166 }
167 
f2fs_free_filename(struct f2fs_filename * fname)168 void f2fs_free_filename(struct f2fs_filename *fname)
169 {
170 #ifdef CONFIG_FS_ENCRYPTION
171 	kfree(fname->crypto_buf.name);
172 	fname->crypto_buf.name = NULL;
173 #endif
174 	f2fs_free_casefolded_name(fname);
175 }
176 
dir_block_index(unsigned int level,int dir_level,unsigned int idx)177 static unsigned long dir_block_index(unsigned int level,
178 				int dir_level, unsigned int idx)
179 {
180 	unsigned long i;
181 	unsigned long bidx = 0;
182 
183 	for (i = 0; i < level; i++)
184 		bidx += mul_u32_u32(dir_buckets(i, dir_level),
185 				    bucket_blocks(i));
186 	bidx += idx * bucket_blocks(level);
187 	return bidx;
188 }
189 
find_in_block(struct inode * dir,struct folio * dentry_folio,const struct f2fs_filename * fname,int * max_slots,bool use_hash)190 static struct f2fs_dir_entry *find_in_block(struct inode *dir,
191 				struct folio *dentry_folio,
192 				const struct f2fs_filename *fname,
193 				int *max_slots,
194 				bool use_hash)
195 {
196 	struct f2fs_dentry_block *dentry_blk;
197 	struct f2fs_dentry_ptr d;
198 
199 	dentry_blk = folio_address(dentry_folio);
200 
201 	make_dentry_ptr_block(dir, &d, dentry_blk);
202 	return f2fs_find_target_dentry(&d, fname, max_slots, use_hash);
203 }
204 
f2fs_match_name(const struct inode * dir,const struct f2fs_filename * fname,const u8 * de_name,u32 de_name_len)205 static inline int f2fs_match_name(const struct inode *dir,
206 				   const struct f2fs_filename *fname,
207 				   const u8 *de_name, u32 de_name_len)
208 {
209 	struct fscrypt_name f;
210 
211 #if IS_ENABLED(CONFIG_UNICODE)
212 	if (fname->cf_name.name)
213 		return generic_ci_match(dir, fname->usr_fname,
214 					&fname->cf_name,
215 					de_name, de_name_len);
216 
217 #endif
218 	f.usr_fname = fname->usr_fname;
219 	f.disk_name = fname->disk_name;
220 #ifdef CONFIG_FS_ENCRYPTION
221 	f.crypto_buf = fname->crypto_buf;
222 #endif
223 	return fscrypt_match_name(&f, de_name, de_name_len);
224 }
225 
f2fs_find_target_dentry(const struct f2fs_dentry_ptr * d,const struct f2fs_filename * fname,int * max_slots,bool use_hash)226 struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d,
227 			const struct f2fs_filename *fname, int *max_slots,
228 			bool use_hash)
229 {
230 	struct f2fs_dir_entry *de;
231 	unsigned long bit_pos = 0;
232 	int max_len = 0;
233 	int res = 0;
234 
235 	if (max_slots)
236 		*max_slots = 0;
237 	while (bit_pos < d->max) {
238 		if (!test_bit_le(bit_pos, d->bitmap)) {
239 			bit_pos++;
240 			max_len++;
241 			continue;
242 		}
243 
244 		de = &d->dentry[bit_pos];
245 
246 		if (unlikely(!de->name_len)) {
247 			bit_pos++;
248 			continue;
249 		}
250 
251 		if (!use_hash || de->hash_code == fname->hash) {
252 			res = f2fs_match_name(d->inode, fname,
253 					      d->filename[bit_pos],
254 					      le16_to_cpu(de->name_len));
255 			if (res < 0)
256 				return ERR_PTR(res);
257 			if (res)
258 				goto found;
259 		}
260 
261 		if (max_slots && max_len > *max_slots)
262 			*max_slots = max_len;
263 		max_len = 0;
264 
265 		bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
266 	}
267 
268 	de = NULL;
269 found:
270 	if (max_slots && max_len > *max_slots)
271 		*max_slots = max_len;
272 	return de;
273 }
274 
find_in_level(struct inode * dir,unsigned int level,const struct f2fs_filename * fname,struct folio ** res_folio,bool use_hash)275 static struct f2fs_dir_entry *find_in_level(struct inode *dir,
276 					unsigned int level,
277 					const struct f2fs_filename *fname,
278 					struct folio **res_folio,
279 					bool use_hash)
280 {
281 	int s = GET_DENTRY_SLOTS(fname->disk_name.len);
282 	unsigned int nbucket, nblock;
283 	unsigned int bidx, end_block, bucket_no;
284 	struct f2fs_dir_entry *de = NULL;
285 	pgoff_t next_pgofs;
286 	bool room = false;
287 	int max_slots;
288 
289 	nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
290 	nblock = bucket_blocks(level);
291 
292 	bucket_no = use_hash ? le32_to_cpu(fname->hash) % nbucket : 0;
293 
294 start_find_bucket:
295 	bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
296 			       bucket_no);
297 	end_block = bidx + nblock;
298 
299 	while (bidx < end_block) {
300 		/* no need to allocate new dentry pages to all the indices */
301 		struct folio *dentry_folio;
302 		dentry_folio = f2fs_find_data_folio(dir, bidx, &next_pgofs);
303 		if (IS_ERR(dentry_folio)) {
304 			if (PTR_ERR(dentry_folio) == -ENOENT) {
305 				room = true;
306 				bidx = next_pgofs;
307 				continue;
308 			} else {
309 				*res_folio = dentry_folio;
310 				break;
311 			}
312 		}
313 
314 		de = find_in_block(dir, dentry_folio, fname, &max_slots, use_hash);
315 		if (IS_ERR(de)) {
316 			*res_folio = ERR_CAST(de);
317 			de = NULL;
318 			break;
319 		} else if (de) {
320 			*res_folio = dentry_folio;
321 			break;
322 		}
323 
324 		if (max_slots >= s)
325 			room = true;
326 		f2fs_folio_put(dentry_folio, false);
327 
328 		bidx++;
329 	}
330 
331 	if (de)
332 		return de;
333 
334 	if (likely(use_hash)) {
335 		if (room && F2FS_I(dir)->chash != fname->hash) {
336 			F2FS_I(dir)->chash = fname->hash;
337 			F2FS_I(dir)->clevel = level;
338 		}
339 	} else if (++bucket_no < nbucket) {
340 		goto start_find_bucket;
341 	}
342 	return NULL;
343 }
344 
__f2fs_find_entry(struct inode * dir,const struct f2fs_filename * fname,struct folio ** res_folio)345 struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
346 					 const struct f2fs_filename *fname,
347 					 struct folio **res_folio)
348 {
349 	unsigned long npages = dir_blocks(dir);
350 	struct f2fs_dir_entry *de = NULL;
351 	unsigned int max_depth;
352 	unsigned int level;
353 	bool use_hash = true;
354 
355 	*res_folio = NULL;
356 
357 #if IS_ENABLED(CONFIG_UNICODE)
358 start_find_entry:
359 #endif
360 	if (f2fs_has_inline_dentry(dir)) {
361 		de = f2fs_find_in_inline_dir(dir, fname, res_folio, use_hash);
362 		goto out;
363 	}
364 
365 	if (npages == 0)
366 		goto out;
367 
368 	max_depth = F2FS_I(dir)->i_current_depth;
369 	if (unlikely(max_depth > MAX_DIR_HASH_DEPTH)) {
370 		f2fs_warn(F2FS_I_SB(dir), "Corrupted max_depth of %lu: %u",
371 			  dir->i_ino, max_depth);
372 		max_depth = MAX_DIR_HASH_DEPTH;
373 		f2fs_i_depth_write(dir, max_depth);
374 	}
375 
376 	for (level = 0; level < max_depth; level++) {
377 		de = find_in_level(dir, level, fname, res_folio, use_hash);
378 		if (de || IS_ERR(*res_folio))
379 			break;
380 	}
381 
382 out:
383 #if IS_ENABLED(CONFIG_UNICODE)
384 	if (f2fs_should_fallback_to_linear(dir) &&
385 		IS_CASEFOLDED(dir) && !de && use_hash) {
386 		use_hash = false;
387 		goto start_find_entry;
388 	}
389 #endif
390 	/* This is to increase the speed of f2fs_create */
391 	if (!de)
392 		F2FS_I(dir)->task = current;
393 	return de;
394 }
395 
396 /*
397  * Find an entry in the specified directory with the wanted name.
398  * It returns the page where the entry was found (as a parameter - res_page),
399  * and the entry itself. Page is returned mapped and unlocked.
400  * Entry is guaranteed to be valid.
401  */
f2fs_find_entry(struct inode * dir,const struct qstr * child,struct folio ** res_folio)402 struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
403 			const struct qstr *child, struct folio **res_folio)
404 {
405 	struct f2fs_dir_entry *de = NULL;
406 	struct f2fs_filename fname;
407 	int err;
408 
409 	err = f2fs_setup_filename(dir, child, 1, &fname);
410 	if (err) {
411 		if (err == -ENOENT)
412 			*res_folio = NULL;
413 		else
414 			*res_folio = ERR_PTR(err);
415 		return NULL;
416 	}
417 
418 	de = __f2fs_find_entry(dir, &fname, res_folio);
419 
420 	f2fs_free_filename(&fname);
421 	return de;
422 }
423 
f2fs_parent_dir(struct inode * dir,struct folio ** f)424 struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct folio **f)
425 {
426 	return f2fs_find_entry(dir, &dotdot_name, f);
427 }
428 
f2fs_inode_by_name(struct inode * dir,const struct qstr * qstr,struct folio ** folio)429 ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
430 							struct folio **folio)
431 {
432 	ino_t res = 0;
433 	struct f2fs_dir_entry *de;
434 
435 	de = f2fs_find_entry(dir, qstr, folio);
436 	if (de) {
437 		res = le32_to_cpu(de->ino);
438 		f2fs_folio_put(*folio, false);
439 	}
440 
441 	return res;
442 }
443 
f2fs_set_link(struct inode * dir,struct f2fs_dir_entry * de,struct folio * folio,struct inode * inode)444 void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
445 		struct folio *folio, struct inode *inode)
446 {
447 	enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA;
448 
449 	folio_lock(folio);
450 	f2fs_folio_wait_writeback(folio, type, true, true);
451 	de->ino = cpu_to_le32(inode->i_ino);
452 	de->file_type = fs_umode_to_ftype(inode->i_mode);
453 	folio_mark_dirty(folio);
454 
455 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
456 	f2fs_mark_inode_dirty_sync(dir, false);
457 	f2fs_folio_put(folio, true);
458 }
459 
init_dent_inode(struct inode * dir,struct inode * inode,const struct f2fs_filename * fname,struct folio * ifolio)460 static void init_dent_inode(struct inode *dir, struct inode *inode,
461 			    const struct f2fs_filename *fname,
462 			    struct folio *ifolio)
463 {
464 	struct f2fs_inode *ri;
465 
466 	if (!fname) /* tmpfile case? */
467 		return;
468 
469 	f2fs_folio_wait_writeback(ifolio, NODE, true, true);
470 
471 	/* copy name info. to this inode folio */
472 	ri = F2FS_INODE(ifolio);
473 	ri->i_namelen = cpu_to_le32(fname->disk_name.len);
474 	memcpy(ri->i_name, fname->disk_name.name, fname->disk_name.len);
475 	if (IS_ENCRYPTED(dir)) {
476 		file_set_enc_name(inode);
477 		/*
478 		 * Roll-forward recovery doesn't have encryption keys available,
479 		 * so it can't compute the dirhash for encrypted+casefolded
480 		 * filenames.  Append it to i_name if possible.  Else, disable
481 		 * roll-forward recovery of the dentry (i.e., make fsync'ing the
482 		 * file force a checkpoint) by setting LOST_PINO.
483 		 */
484 		if (IS_CASEFOLDED(dir)) {
485 			if (fname->disk_name.len + sizeof(f2fs_hash_t) <=
486 			    F2FS_NAME_LEN)
487 				put_unaligned(fname->hash, (f2fs_hash_t *)
488 					&ri->i_name[fname->disk_name.len]);
489 			else
490 				file_lost_pino(inode);
491 		}
492 	}
493 	folio_mark_dirty(ifolio);
494 }
495 
f2fs_do_make_empty_dir(struct inode * inode,struct inode * parent,struct f2fs_dentry_ptr * d)496 void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
497 					struct f2fs_dentry_ptr *d)
498 {
499 	struct fscrypt_str dot = FSTR_INIT(".", 1);
500 	struct fscrypt_str dotdot = FSTR_INIT("..", 2);
501 
502 	/* update dirent of "." */
503 	f2fs_update_dentry(inode->i_ino, inode->i_mode, d, &dot, 0, 0);
504 
505 	/* update dirent of ".." */
506 	f2fs_update_dentry(parent->i_ino, parent->i_mode, d, &dotdot, 0, 1);
507 }
508 
make_empty_dir(struct inode * inode,struct inode * parent,struct folio * folio)509 static int make_empty_dir(struct inode *inode,
510 		struct inode *parent, struct folio *folio)
511 {
512 	struct folio *dentry_folio;
513 	struct f2fs_dentry_block *dentry_blk;
514 	struct f2fs_dentry_ptr d;
515 
516 	if (f2fs_has_inline_dentry(inode))
517 		return f2fs_make_empty_inline_dir(inode, parent, folio);
518 
519 	dentry_folio = f2fs_get_new_data_folio(inode, folio, 0, true);
520 	if (IS_ERR(dentry_folio))
521 		return PTR_ERR(dentry_folio);
522 
523 	dentry_blk = folio_address(dentry_folio);
524 
525 	make_dentry_ptr_block(NULL, &d, dentry_blk);
526 	f2fs_do_make_empty_dir(inode, parent, &d);
527 
528 	folio_mark_dirty(dentry_folio);
529 	f2fs_folio_put(dentry_folio, true);
530 	return 0;
531 }
532 
f2fs_init_inode_metadata(struct inode * inode,struct inode * dir,const struct f2fs_filename * fname,struct folio * dfolio)533 struct folio *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
534 		const struct f2fs_filename *fname, struct folio *dfolio)
535 {
536 	struct folio *folio;
537 	int err;
538 
539 	if (is_inode_flag_set(inode, FI_NEW_INODE)) {
540 		folio = f2fs_new_inode_folio(inode);
541 		if (IS_ERR(folio))
542 			return folio;
543 
544 		if (S_ISDIR(inode->i_mode)) {
545 			/* in order to handle error case */
546 			folio_get(folio);
547 			err = make_empty_dir(inode, dir, folio);
548 			if (err) {
549 				folio_lock(folio);
550 				goto put_error;
551 			}
552 			folio_put(folio);
553 		}
554 
555 		err = f2fs_init_acl(inode, dir, folio, dfolio);
556 		if (err)
557 			goto put_error;
558 
559 		err = f2fs_init_security(inode, dir,
560 					 fname ? fname->usr_fname : NULL,
561 					 folio);
562 		if (err)
563 			goto put_error;
564 
565 		if (IS_ENCRYPTED(inode)) {
566 			err = fscrypt_set_context(inode, folio);
567 			if (err)
568 				goto put_error;
569 		}
570 	} else {
571 		folio = f2fs_get_inode_folio(F2FS_I_SB(dir), inode->i_ino);
572 		if (IS_ERR(folio))
573 			return folio;
574 	}
575 
576 	init_dent_inode(dir, inode, fname, folio);
577 
578 	/*
579 	 * This file should be checkpointed during fsync.
580 	 * We lost i_pino from now on.
581 	 */
582 	if (is_inode_flag_set(inode, FI_INC_LINK)) {
583 		if (!S_ISDIR(inode->i_mode))
584 			file_lost_pino(inode);
585 		/*
586 		 * If link the tmpfile to alias through linkat path,
587 		 * we should remove this inode from orphan list.
588 		 */
589 		if (inode->i_nlink == 0)
590 			f2fs_remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
591 		f2fs_i_links_write(inode, true);
592 	}
593 	return folio;
594 
595 put_error:
596 	clear_nlink(inode);
597 	f2fs_update_inode(inode, folio);
598 	f2fs_folio_put(folio, true);
599 	return ERR_PTR(err);
600 }
601 
f2fs_update_parent_metadata(struct inode * dir,struct inode * inode,unsigned int current_depth)602 void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
603 						unsigned int current_depth)
604 {
605 	if (inode && is_inode_flag_set(inode, FI_NEW_INODE)) {
606 		if (S_ISDIR(inode->i_mode))
607 			f2fs_i_links_write(dir, true);
608 		clear_inode_flag(inode, FI_NEW_INODE);
609 	}
610 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
611 	f2fs_mark_inode_dirty_sync(dir, false);
612 
613 	if (F2FS_I(dir)->i_current_depth != current_depth)
614 		f2fs_i_depth_write(dir, current_depth);
615 
616 	if (inode && is_inode_flag_set(inode, FI_INC_LINK))
617 		clear_inode_flag(inode, FI_INC_LINK);
618 }
619 
f2fs_room_for_filename(const void * bitmap,int slots,int max_slots)620 int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots)
621 {
622 	int bit_start = 0;
623 	int zero_start, zero_end;
624 next:
625 	zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
626 	if (zero_start >= max_slots)
627 		return max_slots;
628 
629 	zero_end = find_next_bit_le(bitmap, max_slots, zero_start);
630 	if (zero_end - zero_start >= slots)
631 		return zero_start;
632 
633 	bit_start = zero_end + 1;
634 
635 	if (zero_end + 1 >= max_slots)
636 		return max_slots;
637 	goto next;
638 }
639 
f2fs_has_enough_room(struct inode * dir,struct folio * ifolio,const struct f2fs_filename * fname)640 bool f2fs_has_enough_room(struct inode *dir, struct folio *ifolio,
641 			  const struct f2fs_filename *fname)
642 {
643 	struct f2fs_dentry_ptr d;
644 	unsigned int bit_pos;
645 	int slots = GET_DENTRY_SLOTS(fname->disk_name.len);
646 
647 	make_dentry_ptr_inline(dir, &d, inline_data_addr(dir, ifolio));
648 
649 	bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max);
650 
651 	return bit_pos < d.max;
652 }
653 
f2fs_update_dentry(nid_t ino,umode_t mode,struct f2fs_dentry_ptr * d,const struct fscrypt_str * name,f2fs_hash_t name_hash,unsigned int bit_pos)654 void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
655 			const struct fscrypt_str *name, f2fs_hash_t name_hash,
656 			unsigned int bit_pos)
657 {
658 	struct f2fs_dir_entry *de;
659 	int slots = GET_DENTRY_SLOTS(name->len);
660 	int i;
661 
662 	de = &d->dentry[bit_pos];
663 	de->hash_code = name_hash;
664 	de->name_len = cpu_to_le16(name->len);
665 	memcpy(d->filename[bit_pos], name->name, name->len);
666 	de->ino = cpu_to_le32(ino);
667 	de->file_type = fs_umode_to_ftype(mode);
668 	for (i = 0; i < slots; i++) {
669 		__set_bit_le(bit_pos + i, (void *)d->bitmap);
670 		/* avoid wrong garbage data for readdir */
671 		if (i)
672 			(de + i)->name_len = 0;
673 	}
674 }
675 
f2fs_add_regular_entry(struct inode * dir,const struct f2fs_filename * fname,struct inode * inode,nid_t ino,umode_t mode)676 int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
677 			   struct inode *inode, nid_t ino, umode_t mode)
678 {
679 	unsigned int bit_pos;
680 	unsigned int level;
681 	unsigned int current_depth;
682 	unsigned long bidx, block;
683 	unsigned int nbucket, nblock;
684 	struct folio *dentry_folio = NULL;
685 	struct f2fs_dentry_block *dentry_blk = NULL;
686 	struct f2fs_dentry_ptr d;
687 	struct folio *folio = NULL;
688 	int slots, err = 0;
689 
690 	level = 0;
691 	slots = GET_DENTRY_SLOTS(fname->disk_name.len);
692 
693 	current_depth = F2FS_I(dir)->i_current_depth;
694 	if (F2FS_I(dir)->chash == fname->hash) {
695 		level = F2FS_I(dir)->clevel;
696 		F2FS_I(dir)->chash = 0;
697 	}
698 
699 start:
700 	if (time_to_inject(F2FS_I_SB(dir), FAULT_DIR_DEPTH))
701 		return -ENOSPC;
702 
703 	if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
704 		return -ENOSPC;
705 
706 	/* Increase the depth, if required */
707 	if (level == current_depth)
708 		++current_depth;
709 
710 	nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
711 	nblock = bucket_blocks(level);
712 
713 	bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
714 				(le32_to_cpu(fname->hash) % nbucket));
715 
716 	for (block = bidx; block <= (bidx + nblock - 1); block++) {
717 		dentry_folio = f2fs_get_new_data_folio(dir, NULL, block, true);
718 		if (IS_ERR(dentry_folio))
719 			return PTR_ERR(dentry_folio);
720 
721 		dentry_blk = folio_address(dentry_folio);
722 		bit_pos = f2fs_room_for_filename(&dentry_blk->dentry_bitmap,
723 						slots, NR_DENTRY_IN_BLOCK);
724 		if (bit_pos < NR_DENTRY_IN_BLOCK)
725 			goto add_dentry;
726 
727 		f2fs_folio_put(dentry_folio, true);
728 	}
729 
730 	/* Move to next level to find the empty slot for new dentry */
731 	++level;
732 	goto start;
733 add_dentry:
734 	f2fs_folio_wait_writeback(dentry_folio, DATA, true, true);
735 
736 	if (inode) {
737 		f2fs_down_write(&F2FS_I(inode)->i_sem);
738 		folio = f2fs_init_inode_metadata(inode, dir, fname, NULL);
739 		if (IS_ERR(folio)) {
740 			err = PTR_ERR(folio);
741 			goto fail;
742 		}
743 	}
744 
745 	make_dentry_ptr_block(NULL, &d, dentry_blk);
746 	f2fs_update_dentry(ino, mode, &d, &fname->disk_name, fname->hash,
747 			   bit_pos);
748 
749 	folio_mark_dirty(dentry_folio);
750 
751 	if (inode) {
752 		f2fs_i_pino_write(inode, dir->i_ino);
753 
754 		/* synchronize inode page's data from inode cache */
755 		if (is_inode_flag_set(inode, FI_NEW_INODE))
756 			f2fs_update_inode(inode, folio);
757 
758 		f2fs_folio_put(folio, true);
759 	}
760 
761 	f2fs_update_parent_metadata(dir, inode, current_depth);
762 fail:
763 	if (inode)
764 		f2fs_up_write(&F2FS_I(inode)->i_sem);
765 
766 	f2fs_folio_put(dentry_folio, true);
767 
768 	return err;
769 }
770 
f2fs_add_dentry(struct inode * dir,const struct f2fs_filename * fname,struct inode * inode,nid_t ino,umode_t mode)771 int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
772 		    struct inode *inode, nid_t ino, umode_t mode)
773 {
774 	int err = -EAGAIN;
775 
776 	if (f2fs_has_inline_dentry(dir)) {
777 		/*
778 		 * Should get i_xattr_sem to keep the lock order:
779 		 * i_xattr_sem -> inode_page lock used by f2fs_setxattr.
780 		 */
781 		f2fs_down_read(&F2FS_I(dir)->i_xattr_sem);
782 		err = f2fs_add_inline_entry(dir, fname, inode, ino, mode);
783 		f2fs_up_read(&F2FS_I(dir)->i_xattr_sem);
784 	}
785 	if (err == -EAGAIN)
786 		err = f2fs_add_regular_entry(dir, fname, inode, ino, mode);
787 
788 	f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
789 	return err;
790 }
791 
792 /*
793  * Caller should grab and release a rwsem by calling f2fs_lock_op() and
794  * f2fs_unlock_op().
795  */
f2fs_do_add_link(struct inode * dir,const struct qstr * name,struct inode * inode,nid_t ino,umode_t mode)796 int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
797 				struct inode *inode, nid_t ino, umode_t mode)
798 {
799 	struct f2fs_filename fname;
800 	struct folio *folio = NULL;
801 	struct f2fs_dir_entry *de = NULL;
802 	int err;
803 
804 	err = f2fs_setup_filename(dir, name, 0, &fname);
805 	if (err)
806 		return err;
807 
808 	/*
809 	 * An immature stackable filesystem shows a race condition between lookup
810 	 * and create. If we have same task when doing lookup and create, it's
811 	 * definitely fine as expected by VFS normally. Otherwise, let's just
812 	 * verify on-disk dentry one more time, which guarantees filesystem
813 	 * consistency more.
814 	 */
815 	if (current != F2FS_I(dir)->task) {
816 		de = __f2fs_find_entry(dir, &fname, &folio);
817 		F2FS_I(dir)->task = NULL;
818 	}
819 	if (de) {
820 		f2fs_folio_put(folio, false);
821 		err = -EEXIST;
822 	} else if (IS_ERR(folio)) {
823 		err = PTR_ERR(folio);
824 	} else {
825 		err = f2fs_add_dentry(dir, &fname, inode, ino, mode);
826 	}
827 	f2fs_free_filename(&fname);
828 	return err;
829 }
830 
f2fs_do_tmpfile(struct inode * inode,struct inode * dir,struct f2fs_filename * fname)831 int f2fs_do_tmpfile(struct inode *inode, struct inode *dir,
832 					struct f2fs_filename *fname)
833 {
834 	struct folio *folio;
835 	int err = 0;
836 
837 	f2fs_down_write(&F2FS_I(inode)->i_sem);
838 	folio = f2fs_init_inode_metadata(inode, dir, fname, NULL);
839 	if (IS_ERR(folio)) {
840 		err = PTR_ERR(folio);
841 		goto fail;
842 	}
843 	f2fs_folio_put(folio, true);
844 
845 	clear_inode_flag(inode, FI_NEW_INODE);
846 	f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
847 fail:
848 	f2fs_up_write(&F2FS_I(inode)->i_sem);
849 	return err;
850 }
851 
f2fs_drop_nlink(struct inode * dir,struct inode * inode)852 void f2fs_drop_nlink(struct inode *dir, struct inode *inode)
853 {
854 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
855 
856 	f2fs_down_write(&F2FS_I(inode)->i_sem);
857 
858 	if (S_ISDIR(inode->i_mode))
859 		f2fs_i_links_write(dir, false);
860 	inode_set_ctime_current(inode);
861 
862 	f2fs_i_links_write(inode, false);
863 	if (S_ISDIR(inode->i_mode)) {
864 		f2fs_i_links_write(inode, false);
865 		f2fs_i_size_write(inode, 0);
866 	}
867 	f2fs_up_write(&F2FS_I(inode)->i_sem);
868 
869 	if (inode->i_nlink == 0)
870 		f2fs_add_orphan_inode(inode);
871 	else
872 		f2fs_release_orphan_inode(sbi);
873 }
874 
875 /*
876  * It only removes the dentry from the dentry page, corresponding name
877  * entry in name page does not need to be touched during deletion.
878  */
f2fs_delete_entry(struct f2fs_dir_entry * dentry,struct folio * folio,struct inode * dir,struct inode * inode)879 void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct folio *folio,
880 					struct inode *dir, struct inode *inode)
881 {
882 	struct f2fs_dentry_block *dentry_blk;
883 	unsigned int bit_pos;
884 	int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
885 	pgoff_t index = folio->index;
886 	int i;
887 
888 	f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
889 
890 	if (F2FS_OPTION(F2FS_I_SB(dir)).fsync_mode == FSYNC_MODE_STRICT)
891 		f2fs_add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
892 
893 	if (f2fs_has_inline_dentry(dir))
894 		return f2fs_delete_inline_entry(dentry, folio, dir, inode);
895 
896 	folio_lock(folio);
897 	f2fs_folio_wait_writeback(folio, DATA, true, true);
898 
899 	dentry_blk = folio_address(folio);
900 	bit_pos = dentry - dentry_blk->dentry;
901 	for (i = 0; i < slots; i++)
902 		__clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
903 
904 	/* Let's check and deallocate this dentry page */
905 	bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
906 			NR_DENTRY_IN_BLOCK,
907 			0);
908 	folio_mark_dirty(folio);
909 
910 	if (bit_pos == NR_DENTRY_IN_BLOCK &&
911 		!f2fs_truncate_hole(dir, index, index + 1)) {
912 		f2fs_clear_page_cache_dirty_tag(folio);
913 		folio_clear_dirty_for_io(folio);
914 		folio_clear_uptodate(folio);
915 		folio_detach_private(folio);
916 
917 		inode_dec_dirty_pages(dir);
918 		f2fs_remove_dirty_inode(dir);
919 	}
920 	f2fs_folio_put(folio, true);
921 
922 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
923 	f2fs_mark_inode_dirty_sync(dir, false);
924 
925 	if (inode)
926 		f2fs_drop_nlink(dir, inode);
927 }
928 
f2fs_empty_dir(struct inode * dir)929 bool f2fs_empty_dir(struct inode *dir)
930 {
931 	unsigned long bidx = 0;
932 	unsigned int bit_pos;
933 	struct f2fs_dentry_block *dentry_blk;
934 	unsigned long nblock = dir_blocks(dir);
935 
936 	if (f2fs_has_inline_dentry(dir))
937 		return f2fs_empty_inline_dir(dir);
938 
939 	while (bidx < nblock) {
940 		pgoff_t next_pgofs;
941 		struct folio *dentry_folio;
942 
943 		dentry_folio = f2fs_find_data_folio(dir, bidx, &next_pgofs);
944 		if (IS_ERR(dentry_folio)) {
945 			if (PTR_ERR(dentry_folio) == -ENOENT) {
946 				bidx = next_pgofs;
947 				continue;
948 			} else {
949 				return false;
950 			}
951 		}
952 
953 		dentry_blk = folio_address(dentry_folio);
954 		if (bidx == 0)
955 			bit_pos = 2;
956 		else
957 			bit_pos = 0;
958 		bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
959 						NR_DENTRY_IN_BLOCK,
960 						bit_pos);
961 
962 		f2fs_folio_put(dentry_folio, false);
963 
964 		if (bit_pos < NR_DENTRY_IN_BLOCK)
965 			return false;
966 
967 		bidx++;
968 	}
969 	return true;
970 }
971 
f2fs_fill_dentries(struct dir_context * ctx,struct f2fs_dentry_ptr * d,unsigned int start_pos,struct fscrypt_str * fstr)972 int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
973 			unsigned int start_pos, struct fscrypt_str *fstr)
974 {
975 	unsigned char d_type = DT_UNKNOWN;
976 	unsigned int bit_pos;
977 	struct f2fs_dir_entry *de = NULL;
978 	struct fscrypt_str de_name = FSTR_INIT(NULL, 0);
979 	struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode);
980 	struct blk_plug plug;
981 	bool readdir_ra = sbi->readdir_ra;
982 	bool found_valid_dirent = false;
983 	int err = 0;
984 
985 	bit_pos = ((unsigned long)ctx->pos % d->max);
986 
987 	if (readdir_ra)
988 		blk_start_plug(&plug);
989 
990 	while (bit_pos < d->max) {
991 		bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
992 		if (bit_pos >= d->max)
993 			break;
994 
995 		de = &d->dentry[bit_pos];
996 		if (de->name_len == 0) {
997 			if (found_valid_dirent || !bit_pos) {
998 				f2fs_warn_ratelimited(sbi,
999 					"invalid namelen(0), ino:%u, run fsck to fix.",
1000 					le32_to_cpu(de->ino));
1001 				set_sbi_flag(sbi, SBI_NEED_FSCK);
1002 			}
1003 			bit_pos++;
1004 			ctx->pos = start_pos + bit_pos;
1005 			continue;
1006 		}
1007 
1008 		d_type = fs_ftype_to_dtype(de->file_type);
1009 
1010 		de_name.name = d->filename[bit_pos];
1011 		de_name.len = le16_to_cpu(de->name_len);
1012 
1013 		/* check memory boundary before moving forward */
1014 		bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
1015 		if (unlikely(bit_pos > d->max ||
1016 				le16_to_cpu(de->name_len) > F2FS_NAME_LEN)) {
1017 			f2fs_warn(sbi, "%s: corrupted namelen=%d, run fsck to fix.",
1018 				  __func__, le16_to_cpu(de->name_len));
1019 			set_sbi_flag(sbi, SBI_NEED_FSCK);
1020 			err = -EFSCORRUPTED;
1021 			f2fs_handle_error(sbi, ERROR_CORRUPTED_DIRENT);
1022 			goto out;
1023 		}
1024 
1025 		if (IS_ENCRYPTED(d->inode)) {
1026 			int save_len = fstr->len;
1027 
1028 			err = fscrypt_fname_disk_to_usr(d->inode,
1029 						(u32)le32_to_cpu(de->hash_code),
1030 						0, &de_name, fstr);
1031 			if (err)
1032 				goto out;
1033 
1034 			de_name = *fstr;
1035 			fstr->len = save_len;
1036 		}
1037 
1038 		if (!dir_emit(ctx, de_name.name, de_name.len,
1039 					le32_to_cpu(de->ino), d_type)) {
1040 			err = 1;
1041 			goto out;
1042 		}
1043 
1044 		if (readdir_ra)
1045 			f2fs_ra_node_page(sbi, le32_to_cpu(de->ino));
1046 
1047 		ctx->pos = start_pos + bit_pos;
1048 		found_valid_dirent = true;
1049 	}
1050 out:
1051 	if (readdir_ra)
1052 		blk_finish_plug(&plug);
1053 	return err;
1054 }
1055 
f2fs_readdir(struct file * file,struct dir_context * ctx)1056 static int f2fs_readdir(struct file *file, struct dir_context *ctx)
1057 {
1058 	struct inode *inode = file_inode(file);
1059 	unsigned long npages = dir_blocks(inode);
1060 	struct f2fs_dentry_block *dentry_blk = NULL;
1061 	struct file_ra_state *ra = &file->f_ra;
1062 	loff_t start_pos = ctx->pos;
1063 	unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
1064 	struct f2fs_dentry_ptr d;
1065 	struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
1066 	int err = 0;
1067 
1068 	if (IS_ENCRYPTED(inode)) {
1069 		err = fscrypt_prepare_readdir(inode);
1070 		if (err)
1071 			goto out;
1072 
1073 		err = fscrypt_fname_alloc_buffer(F2FS_NAME_LEN, &fstr);
1074 		if (err < 0)
1075 			goto out;
1076 	}
1077 
1078 	if (f2fs_has_inline_dentry(inode)) {
1079 		err = f2fs_read_inline_dir(file, ctx, &fstr);
1080 		goto out_free;
1081 	}
1082 
1083 	for (; n < npages; ctx->pos = n * NR_DENTRY_IN_BLOCK) {
1084 		struct folio *dentry_folio;
1085 		pgoff_t next_pgofs;
1086 
1087 		/* allow readdir() to be interrupted */
1088 		if (fatal_signal_pending(current)) {
1089 			err = -ERESTARTSYS;
1090 			goto out_free;
1091 		}
1092 		cond_resched();
1093 
1094 		/* readahead for multi pages of dir */
1095 		if (npages - n > 1 && !ra_has_index(ra, n))
1096 			page_cache_sync_readahead(inode->i_mapping, ra, file, n,
1097 				min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
1098 
1099 		dentry_folio = f2fs_find_data_folio(inode, n, &next_pgofs);
1100 		if (IS_ERR(dentry_folio)) {
1101 			err = PTR_ERR(dentry_folio);
1102 			if (err == -ENOENT) {
1103 				err = 0;
1104 				n = next_pgofs;
1105 				continue;
1106 			} else {
1107 				goto out_free;
1108 			}
1109 		}
1110 
1111 		dentry_blk = folio_address(dentry_folio);
1112 
1113 		make_dentry_ptr_block(inode, &d, dentry_blk);
1114 
1115 		err = f2fs_fill_dentries(ctx, &d,
1116 				n * NR_DENTRY_IN_BLOCK, &fstr);
1117 		f2fs_folio_put(dentry_folio, false);
1118 		if (err)
1119 			break;
1120 
1121 		n++;
1122 	}
1123 out_free:
1124 	fscrypt_fname_free_buffer(&fstr);
1125 out:
1126 	trace_f2fs_readdir(inode, start_pos, ctx->pos, err);
1127 	return err < 0 ? err : 0;
1128 }
1129 
1130 const struct file_operations f2fs_dir_operations = {
1131 	.llseek		= generic_file_llseek,
1132 	.read		= generic_read_dir,
1133 	.iterate_shared	= f2fs_readdir,
1134 	.fsync		= f2fs_sync_file,
1135 	.unlocked_ioctl	= f2fs_ioctl,
1136 #ifdef CONFIG_COMPAT
1137 	.compat_ioctl   = f2fs_compat_ioctl,
1138 #endif
1139 };
1140