dir.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. /*
  2. * fs/f2fs/dir.c
  3. *
  4. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com/
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/f2fs_fs.h>
  13. #include "f2fs.h"
  14. #include "node.h"
  15. #include "acl.h"
  16. #include "xattr.h"
  17. static unsigned long dir_blocks(struct inode *inode)
  18. {
  19. return ((unsigned long long) (i_size_read(inode) + PAGE_CACHE_SIZE - 1))
  20. >> PAGE_CACHE_SHIFT;
  21. }
  22. static unsigned int dir_buckets(unsigned int level, int dir_level)
  23. {
  24. if (level + dir_level < MAX_DIR_HASH_DEPTH / 2)
  25. return 1 << (level + dir_level);
  26. else
  27. return MAX_DIR_BUCKETS;
  28. }
  29. static unsigned int bucket_blocks(unsigned int level)
  30. {
  31. if (level < MAX_DIR_HASH_DEPTH / 2)
  32. return 2;
  33. else
  34. return 4;
  35. }
  36. unsigned char f2fs_filetype_table[F2FS_FT_MAX] = {
  37. [F2FS_FT_UNKNOWN] = DT_UNKNOWN,
  38. [F2FS_FT_REG_FILE] = DT_REG,
  39. [F2FS_FT_DIR] = DT_DIR,
  40. [F2FS_FT_CHRDEV] = DT_CHR,
  41. [F2FS_FT_BLKDEV] = DT_BLK,
  42. [F2FS_FT_FIFO] = DT_FIFO,
  43. [F2FS_FT_SOCK] = DT_SOCK,
  44. [F2FS_FT_SYMLINK] = DT_LNK,
  45. };
  46. #define S_SHIFT 12
  47. static unsigned char f2fs_type_by_mode[S_IFMT >> S_SHIFT] = {
  48. [S_IFREG >> S_SHIFT] = F2FS_FT_REG_FILE,
  49. [S_IFDIR >> S_SHIFT] = F2FS_FT_DIR,
  50. [S_IFCHR >> S_SHIFT] = F2FS_FT_CHRDEV,
  51. [S_IFBLK >> S_SHIFT] = F2FS_FT_BLKDEV,
  52. [S_IFIFO >> S_SHIFT] = F2FS_FT_FIFO,
  53. [S_IFSOCK >> S_SHIFT] = F2FS_FT_SOCK,
  54. [S_IFLNK >> S_SHIFT] = F2FS_FT_SYMLINK,
  55. };
  56. void set_de_type(struct f2fs_dir_entry *de, struct inode *inode)
  57. {
  58. umode_t mode = inode->i_mode;
  59. de->file_type = f2fs_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
  60. }
  61. static unsigned long dir_block_index(unsigned int level,
  62. int dir_level, unsigned int idx)
  63. {
  64. unsigned long i;
  65. unsigned long bidx = 0;
  66. for (i = 0; i < level; i++)
  67. bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
  68. bidx += idx * bucket_blocks(level);
  69. return bidx;
  70. }
  71. static bool early_match_name(size_t namelen, f2fs_hash_t namehash,
  72. struct f2fs_dir_entry *de)
  73. {
  74. if (le16_to_cpu(de->name_len) != namelen)
  75. return false;
  76. if (de->hash_code != namehash)
  77. return false;
  78. return true;
  79. }
  80. static struct f2fs_dir_entry *find_in_block(struct page *dentry_page,
  81. struct qstr *name, int *max_slots,
  82. struct page **res_page)
  83. {
  84. struct f2fs_dentry_block *dentry_blk;
  85. struct f2fs_dir_entry *de;
  86. struct f2fs_dentry_ptr d;
  87. dentry_blk = (struct f2fs_dentry_block *)kmap(dentry_page);
  88. make_dentry_ptr(&d, (void *)dentry_blk, 1);
  89. de = find_target_dentry(name, max_slots, &d);
  90. if (de)
  91. *res_page = dentry_page;
  92. else
  93. kunmap(dentry_page);
  94. /*
  95. * For the most part, it should be a bug when name_len is zero.
  96. * We stop here for figuring out where the bugs has occurred.
  97. */
  98. f2fs_bug_on(F2FS_P_SB(dentry_page), d.max < 0);
  99. return de;
  100. }
  101. struct f2fs_dir_entry *find_target_dentry(struct qstr *name, int *max_slots,
  102. struct f2fs_dentry_ptr *d)
  103. {
  104. struct f2fs_dir_entry *de;
  105. unsigned long bit_pos = 0;
  106. f2fs_hash_t namehash = f2fs_dentry_hash(name);
  107. int max_len = 0;
  108. if (max_slots)
  109. *max_slots = 0;
  110. while (bit_pos < d->max) {
  111. if (!test_bit_le(bit_pos, d->bitmap)) {
  112. if (bit_pos == 0)
  113. max_len = 1;
  114. else if (!test_bit_le(bit_pos - 1, d->bitmap))
  115. max_len++;
  116. bit_pos++;
  117. continue;
  118. }
  119. de = &d->dentry[bit_pos];
  120. if (early_match_name(name->len, namehash, de) &&
  121. !memcmp(d->filename[bit_pos], name->name, name->len))
  122. goto found;
  123. if (max_slots && *max_slots >= 0 && max_len > *max_slots) {
  124. *max_slots = max_len;
  125. max_len = 0;
  126. }
  127. /* remain bug on condition */
  128. if (unlikely(!de->name_len))
  129. d->max = -1;
  130. bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
  131. }
  132. de = NULL;
  133. found:
  134. if (max_slots && max_len > *max_slots)
  135. *max_slots = max_len;
  136. return de;
  137. }
  138. static struct f2fs_dir_entry *find_in_level(struct inode *dir,
  139. unsigned int level, struct qstr *name,
  140. f2fs_hash_t namehash, struct page **res_page)
  141. {
  142. int s = GET_DENTRY_SLOTS(name->len);
  143. unsigned int nbucket, nblock;
  144. unsigned int bidx, end_block;
  145. struct page *dentry_page;
  146. struct f2fs_dir_entry *de = NULL;
  147. bool room = false;
  148. int max_slots;
  149. f2fs_bug_on(F2FS_I_SB(dir), level > MAX_DIR_HASH_DEPTH);
  150. nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
  151. nblock = bucket_blocks(level);
  152. bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
  153. le32_to_cpu(namehash) % nbucket);
  154. end_block = bidx + nblock;
  155. for (; bidx < end_block; bidx++) {
  156. /* no need to allocate new dentry pages to all the indices */
  157. dentry_page = find_data_page(dir, bidx, true);
  158. if (IS_ERR(dentry_page)) {
  159. room = true;
  160. continue;
  161. }
  162. de = find_in_block(dentry_page, name, &max_slots, res_page);
  163. if (de)
  164. break;
  165. if (max_slots >= s)
  166. room = true;
  167. f2fs_put_page(dentry_page, 0);
  168. }
  169. if (!de && room && F2FS_I(dir)->chash != namehash) {
  170. F2FS_I(dir)->chash = namehash;
  171. F2FS_I(dir)->clevel = level;
  172. }
  173. return de;
  174. }
  175. /*
  176. * Find an entry in the specified directory with the wanted name.
  177. * It returns the page where the entry was found (as a parameter - res_page),
  178. * and the entry itself. Page is returned mapped and unlocked.
  179. * Entry is guaranteed to be valid.
  180. */
  181. struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
  182. struct qstr *child, struct page **res_page)
  183. {
  184. unsigned long npages = dir_blocks(dir);
  185. struct f2fs_dir_entry *de = NULL;
  186. f2fs_hash_t name_hash;
  187. unsigned int max_depth;
  188. unsigned int level;
  189. if (f2fs_has_inline_dentry(dir))
  190. return find_in_inline_dir(dir, child, res_page);
  191. if (npages == 0)
  192. return NULL;
  193. *res_page = NULL;
  194. name_hash = f2fs_dentry_hash(child);
  195. max_depth = F2FS_I(dir)->i_current_depth;
  196. for (level = 0; level < max_depth; level++) {
  197. de = find_in_level(dir, level, child, name_hash, res_page);
  198. if (de)
  199. break;
  200. }
  201. if (!de && F2FS_I(dir)->chash != name_hash) {
  202. F2FS_I(dir)->chash = name_hash;
  203. F2FS_I(dir)->clevel = level - 1;
  204. }
  205. return de;
  206. }
  207. struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
  208. {
  209. struct page *page;
  210. struct f2fs_dir_entry *de;
  211. struct f2fs_dentry_block *dentry_blk;
  212. if (f2fs_has_inline_dentry(dir))
  213. return f2fs_parent_inline_dir(dir, p);
  214. page = get_lock_data_page(dir, 0);
  215. if (IS_ERR(page))
  216. return NULL;
  217. dentry_blk = kmap(page);
  218. de = &dentry_blk->dentry[1];
  219. *p = page;
  220. unlock_page(page);
  221. return de;
  222. }
  223. ino_t f2fs_inode_by_name(struct inode *dir, struct qstr *qstr)
  224. {
  225. ino_t res = 0;
  226. struct f2fs_dir_entry *de;
  227. struct page *page;
  228. de = f2fs_find_entry(dir, qstr, &page);
  229. if (de) {
  230. res = le32_to_cpu(de->ino);
  231. f2fs_dentry_kunmap(dir, page);
  232. f2fs_put_page(page, 0);
  233. }
  234. return res;
  235. }
  236. void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
  237. struct page *page, struct inode *inode)
  238. {
  239. enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA;
  240. lock_page(page);
  241. f2fs_wait_on_page_writeback(page, type);
  242. de->ino = cpu_to_le32(inode->i_ino);
  243. set_de_type(de, inode);
  244. f2fs_dentry_kunmap(dir, page);
  245. set_page_dirty(page);
  246. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  247. mark_inode_dirty(dir);
  248. f2fs_put_page(page, 1);
  249. }
  250. static void init_dent_inode(const struct qstr *name, struct page *ipage)
  251. {
  252. struct f2fs_inode *ri;
  253. f2fs_wait_on_page_writeback(ipage, NODE);
  254. /* copy name info. to this inode page */
  255. ri = F2FS_INODE(ipage);
  256. ri->i_namelen = cpu_to_le32(name->len);
  257. memcpy(ri->i_name, name->name, name->len);
  258. set_page_dirty(ipage);
  259. }
  260. int update_dent_inode(struct inode *inode, const struct qstr *name)
  261. {
  262. struct page *page;
  263. page = get_node_page(F2FS_I_SB(inode), inode->i_ino);
  264. if (IS_ERR(page))
  265. return PTR_ERR(page);
  266. init_dent_inode(name, page);
  267. f2fs_put_page(page, 1);
  268. return 0;
  269. }
  270. void do_make_empty_dir(struct inode *inode, struct inode *parent,
  271. struct f2fs_dentry_ptr *d)
  272. {
  273. struct f2fs_dir_entry *de;
  274. de = &d->dentry[0];
  275. de->name_len = cpu_to_le16(1);
  276. de->hash_code = 0;
  277. de->ino = cpu_to_le32(inode->i_ino);
  278. memcpy(d->filename[0], ".", 1);
  279. set_de_type(de, inode);
  280. de = &d->dentry[1];
  281. de->hash_code = 0;
  282. de->name_len = cpu_to_le16(2);
  283. de->ino = cpu_to_le32(parent->i_ino);
  284. memcpy(d->filename[1], "..", 2);
  285. set_de_type(de, inode);
  286. test_and_set_bit_le(0, (void *)d->bitmap);
  287. test_and_set_bit_le(1, (void *)d->bitmap);
  288. }
  289. static int make_empty_dir(struct inode *inode,
  290. struct inode *parent, struct page *page)
  291. {
  292. struct page *dentry_page;
  293. struct f2fs_dentry_block *dentry_blk;
  294. struct f2fs_dentry_ptr d;
  295. if (f2fs_has_inline_dentry(inode))
  296. return make_empty_inline_dir(inode, parent, page);
  297. dentry_page = get_new_data_page(inode, page, 0, true);
  298. if (IS_ERR(dentry_page))
  299. return PTR_ERR(dentry_page);
  300. dentry_blk = kmap_atomic(dentry_page);
  301. make_dentry_ptr(&d, (void *)dentry_blk, 1);
  302. do_make_empty_dir(inode, parent, &d);
  303. kunmap_atomic(dentry_blk);
  304. set_page_dirty(dentry_page);
  305. f2fs_put_page(dentry_page, 1);
  306. return 0;
  307. }
  308. struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
  309. const struct qstr *name, struct page *dpage)
  310. {
  311. struct page *page;
  312. int err;
  313. if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
  314. page = new_inode_page(inode);
  315. if (IS_ERR(page))
  316. return page;
  317. if (S_ISDIR(inode->i_mode)) {
  318. err = make_empty_dir(inode, dir, page);
  319. if (err)
  320. goto error;
  321. }
  322. err = f2fs_init_acl(inode, dir, page, dpage);
  323. if (err)
  324. goto put_error;
  325. err = f2fs_init_security(inode, dir, name, page);
  326. if (err)
  327. goto put_error;
  328. } else {
  329. page = get_node_page(F2FS_I_SB(dir), inode->i_ino);
  330. if (IS_ERR(page))
  331. return page;
  332. set_cold_node(inode, page);
  333. }
  334. if (name)
  335. init_dent_inode(name, page);
  336. /*
  337. * This file should be checkpointed during fsync.
  338. * We lost i_pino from now on.
  339. */
  340. if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK)) {
  341. file_lost_pino(inode);
  342. /*
  343. * If link the tmpfile to alias through linkat path,
  344. * we should remove this inode from orphan list.
  345. */
  346. if (inode->i_nlink == 0)
  347. remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
  348. inc_nlink(inode);
  349. }
  350. return page;
  351. put_error:
  352. f2fs_put_page(page, 1);
  353. error:
  354. /* once the failed inode becomes a bad inode, i_mode is S_IFREG */
  355. truncate_inode_pages(&inode->i_data, 0);
  356. truncate_blocks(inode, 0, false);
  357. remove_dirty_dir_inode(inode);
  358. remove_inode_page(inode);
  359. return ERR_PTR(err);
  360. }
  361. void update_parent_metadata(struct inode *dir, struct inode *inode,
  362. unsigned int current_depth)
  363. {
  364. if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
  365. if (S_ISDIR(inode->i_mode)) {
  366. inc_nlink(dir);
  367. set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
  368. }
  369. clear_inode_flag(F2FS_I(inode), FI_NEW_INODE);
  370. }
  371. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  372. mark_inode_dirty(dir);
  373. if (F2FS_I(dir)->i_current_depth != current_depth) {
  374. F2FS_I(dir)->i_current_depth = current_depth;
  375. set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
  376. }
  377. if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK))
  378. clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
  379. }
  380. int room_for_filename(const void *bitmap, int slots, int max_slots)
  381. {
  382. int bit_start = 0;
  383. int zero_start, zero_end;
  384. next:
  385. zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
  386. if (zero_start >= max_slots)
  387. return max_slots;
  388. zero_end = find_next_bit_le(bitmap, max_slots, zero_start);
  389. if (zero_end - zero_start >= slots)
  390. return zero_start;
  391. bit_start = zero_end + 1;
  392. if (zero_end + 1 >= max_slots)
  393. return max_slots;
  394. goto next;
  395. }
  396. /*
  397. * Caller should grab and release a rwsem by calling f2fs_lock_op() and
  398. * f2fs_unlock_op().
  399. */
  400. int __f2fs_add_link(struct inode *dir, const struct qstr *name,
  401. struct inode *inode)
  402. {
  403. unsigned int bit_pos;
  404. unsigned int level;
  405. unsigned int current_depth;
  406. unsigned long bidx, block;
  407. f2fs_hash_t dentry_hash;
  408. struct f2fs_dir_entry *de;
  409. unsigned int nbucket, nblock;
  410. size_t namelen = name->len;
  411. struct page *dentry_page = NULL;
  412. struct f2fs_dentry_block *dentry_blk = NULL;
  413. int slots = GET_DENTRY_SLOTS(namelen);
  414. struct page *page;
  415. int err = 0;
  416. int i;
  417. if (f2fs_has_inline_dentry(dir)) {
  418. err = f2fs_add_inline_entry(dir, name, inode);
  419. if (!err || err != -EAGAIN)
  420. return err;
  421. else
  422. err = 0;
  423. }
  424. dentry_hash = f2fs_dentry_hash(name);
  425. level = 0;
  426. current_depth = F2FS_I(dir)->i_current_depth;
  427. if (F2FS_I(dir)->chash == dentry_hash) {
  428. level = F2FS_I(dir)->clevel;
  429. F2FS_I(dir)->chash = 0;
  430. }
  431. start:
  432. if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
  433. return -ENOSPC;
  434. /* Increase the depth, if required */
  435. if (level == current_depth)
  436. ++current_depth;
  437. nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
  438. nblock = bucket_blocks(level);
  439. bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
  440. (le32_to_cpu(dentry_hash) % nbucket));
  441. for (block = bidx; block <= (bidx + nblock - 1); block++) {
  442. dentry_page = get_new_data_page(dir, NULL, block, true);
  443. if (IS_ERR(dentry_page))
  444. return PTR_ERR(dentry_page);
  445. dentry_blk = kmap(dentry_page);
  446. bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
  447. slots, NR_DENTRY_IN_BLOCK);
  448. if (bit_pos < NR_DENTRY_IN_BLOCK)
  449. goto add_dentry;
  450. kunmap(dentry_page);
  451. f2fs_put_page(dentry_page, 1);
  452. }
  453. /* Move to next level to find the empty slot for new dentry */
  454. ++level;
  455. goto start;
  456. add_dentry:
  457. f2fs_wait_on_page_writeback(dentry_page, DATA);
  458. down_write(&F2FS_I(inode)->i_sem);
  459. page = init_inode_metadata(inode, dir, name, NULL);
  460. if (IS_ERR(page)) {
  461. err = PTR_ERR(page);
  462. goto fail;
  463. }
  464. de = &dentry_blk->dentry[bit_pos];
  465. de->hash_code = dentry_hash;
  466. de->name_len = cpu_to_le16(namelen);
  467. memcpy(dentry_blk->filename[bit_pos], name->name, name->len);
  468. de->ino = cpu_to_le32(inode->i_ino);
  469. set_de_type(de, inode);
  470. for (i = 0; i < slots; i++)
  471. test_and_set_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
  472. set_page_dirty(dentry_page);
  473. /* we don't need to mark_inode_dirty now */
  474. F2FS_I(inode)->i_pino = dir->i_ino;
  475. update_inode(inode, page);
  476. f2fs_put_page(page, 1);
  477. update_parent_metadata(dir, inode, current_depth);
  478. fail:
  479. up_write(&F2FS_I(inode)->i_sem);
  480. if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
  481. update_inode_page(dir);
  482. clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
  483. }
  484. kunmap(dentry_page);
  485. f2fs_put_page(dentry_page, 1);
  486. return err;
  487. }
  488. int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
  489. {
  490. struct page *page;
  491. int err = 0;
  492. down_write(&F2FS_I(inode)->i_sem);
  493. page = init_inode_metadata(inode, dir, NULL, NULL);
  494. if (IS_ERR(page)) {
  495. err = PTR_ERR(page);
  496. goto fail;
  497. }
  498. /* we don't need to mark_inode_dirty now */
  499. update_inode(inode, page);
  500. f2fs_put_page(page, 1);
  501. clear_inode_flag(F2FS_I(inode), FI_NEW_INODE);
  502. fail:
  503. up_write(&F2FS_I(inode)->i_sem);
  504. return err;
  505. }
  506. void f2fs_drop_nlink(struct inode *dir, struct inode *inode, struct page *page)
  507. {
  508. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  509. down_write(&F2FS_I(inode)->i_sem);
  510. if (S_ISDIR(inode->i_mode)) {
  511. drop_nlink(dir);
  512. if (page)
  513. update_inode(dir, page);
  514. else
  515. update_inode_page(dir);
  516. }
  517. inode->i_ctime = CURRENT_TIME;
  518. drop_nlink(inode);
  519. if (S_ISDIR(inode->i_mode)) {
  520. drop_nlink(inode);
  521. i_size_write(inode, 0);
  522. }
  523. up_write(&F2FS_I(inode)->i_sem);
  524. update_inode_page(inode);
  525. if (inode->i_nlink == 0)
  526. add_orphan_inode(sbi, inode->i_ino);
  527. else
  528. release_orphan_inode(sbi);
  529. }
  530. /*
  531. * It only removes the dentry from the dentry page, corresponding name
  532. * entry in name page does not need to be touched during deletion.
  533. */
  534. void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
  535. struct inode *dir, struct inode *inode)
  536. {
  537. struct f2fs_dentry_block *dentry_blk;
  538. unsigned int bit_pos;
  539. int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
  540. int i;
  541. if (f2fs_has_inline_dentry(dir))
  542. return f2fs_delete_inline_entry(dentry, page, dir, inode);
  543. lock_page(page);
  544. f2fs_wait_on_page_writeback(page, DATA);
  545. dentry_blk = page_address(page);
  546. bit_pos = dentry - dentry_blk->dentry;
  547. for (i = 0; i < slots; i++)
  548. clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
  549. /* Let's check and deallocate this dentry page */
  550. bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
  551. NR_DENTRY_IN_BLOCK,
  552. 0);
  553. kunmap(page); /* kunmap - pair of f2fs_find_entry */
  554. set_page_dirty(page);
  555. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  556. if (inode)
  557. f2fs_drop_nlink(dir, inode, NULL);
  558. if (bit_pos == NR_DENTRY_IN_BLOCK) {
  559. truncate_hole(dir, page->index, page->index + 1);
  560. clear_page_dirty_for_io(page);
  561. ClearPageUptodate(page);
  562. inode_dec_dirty_pages(dir);
  563. }
  564. f2fs_put_page(page, 1);
  565. }
  566. bool f2fs_empty_dir(struct inode *dir)
  567. {
  568. unsigned long bidx;
  569. struct page *dentry_page;
  570. unsigned int bit_pos;
  571. struct f2fs_dentry_block *dentry_blk;
  572. unsigned long nblock = dir_blocks(dir);
  573. if (f2fs_has_inline_dentry(dir))
  574. return f2fs_empty_inline_dir(dir);
  575. for (bidx = 0; bidx < nblock; bidx++) {
  576. dentry_page = get_lock_data_page(dir, bidx);
  577. if (IS_ERR(dentry_page)) {
  578. if (PTR_ERR(dentry_page) == -ENOENT)
  579. continue;
  580. else
  581. return false;
  582. }
  583. dentry_blk = kmap_atomic(dentry_page);
  584. if (bidx == 0)
  585. bit_pos = 2;
  586. else
  587. bit_pos = 0;
  588. bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
  589. NR_DENTRY_IN_BLOCK,
  590. bit_pos);
  591. kunmap_atomic(dentry_blk);
  592. f2fs_put_page(dentry_page, 1);
  593. if (bit_pos < NR_DENTRY_IN_BLOCK)
  594. return false;
  595. }
  596. return true;
  597. }
  598. bool f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
  599. unsigned int start_pos)
  600. {
  601. unsigned char d_type = DT_UNKNOWN;
  602. unsigned int bit_pos;
  603. struct f2fs_dir_entry *de = NULL;
  604. bit_pos = ((unsigned long)ctx->pos % d->max);
  605. while (bit_pos < d->max) {
  606. bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
  607. if (bit_pos >= d->max)
  608. break;
  609. de = &d->dentry[bit_pos];
  610. if (de->file_type < F2FS_FT_MAX)
  611. d_type = f2fs_filetype_table[de->file_type];
  612. else
  613. d_type = DT_UNKNOWN;
  614. if (!dir_emit(ctx, d->filename[bit_pos],
  615. le16_to_cpu(de->name_len),
  616. le32_to_cpu(de->ino), d_type))
  617. return true;
  618. bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
  619. ctx->pos = start_pos + bit_pos;
  620. }
  621. return false;
  622. }
  623. static int f2fs_readdir(struct file *file, struct dir_context *ctx)
  624. {
  625. struct inode *inode = file_inode(file);
  626. unsigned long npages = dir_blocks(inode);
  627. struct f2fs_dentry_block *dentry_blk = NULL;
  628. struct page *dentry_page = NULL;
  629. struct file_ra_state *ra = &file->f_ra;
  630. unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
  631. struct f2fs_dentry_ptr d;
  632. if (f2fs_has_inline_dentry(inode))
  633. return f2fs_read_inline_dir(file, ctx);
  634. /* readahead for multi pages of dir */
  635. if (npages - n > 1 && !ra_has_index(ra, n))
  636. page_cache_sync_readahead(inode->i_mapping, ra, file, n,
  637. min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
  638. for (; n < npages; n++) {
  639. dentry_page = get_lock_data_page(inode, n);
  640. if (IS_ERR(dentry_page))
  641. continue;
  642. dentry_blk = kmap(dentry_page);
  643. make_dentry_ptr(&d, (void *)dentry_blk, 1);
  644. if (f2fs_fill_dentries(ctx, &d, n * NR_DENTRY_IN_BLOCK))
  645. goto stop;
  646. ctx->pos = (n + 1) * NR_DENTRY_IN_BLOCK;
  647. kunmap(dentry_page);
  648. f2fs_put_page(dentry_page, 1);
  649. dentry_page = NULL;
  650. }
  651. stop:
  652. if (dentry_page && !IS_ERR(dentry_page)) {
  653. kunmap(dentry_page);
  654. f2fs_put_page(dentry_page, 1);
  655. }
  656. return 0;
  657. }
  658. const struct file_operations f2fs_dir_operations = {
  659. .llseek = generic_file_llseek,
  660. .read = generic_read_dir,
  661. .iterate = f2fs_readdir,
  662. .fsync = f2fs_sync_file,
  663. .unlocked_ioctl = f2fs_ioctl,
  664. };