dir.c 21 KB

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