dir.c 21 KB

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