dir.c 22 KB

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