dir.c 23 KB

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