dir.c 28 KB

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