namei.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /*
  2. * fs/f2fs/namei.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 <linux/pagemap.h>
  14. #include <linux/sched.h>
  15. #include <linux/ctype.h>
  16. #include <linux/dcache.h>
  17. #include <linux/namei.h>
  18. #include "f2fs.h"
  19. #include "node.h"
  20. #include "xattr.h"
  21. #include "acl.h"
  22. #include <trace/events/f2fs.h>
  23. static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
  24. {
  25. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  26. nid_t ino;
  27. struct inode *inode;
  28. bool nid_free = false;
  29. int err;
  30. inode = new_inode(dir->i_sb);
  31. if (!inode)
  32. return ERR_PTR(-ENOMEM);
  33. f2fs_lock_op(sbi);
  34. if (!alloc_nid(sbi, &ino)) {
  35. f2fs_unlock_op(sbi);
  36. err = -ENOSPC;
  37. goto fail;
  38. }
  39. f2fs_unlock_op(sbi);
  40. inode_init_owner(inode, dir, mode);
  41. inode->i_ino = ino;
  42. inode->i_blocks = 0;
  43. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  44. inode->i_generation = sbi->s_next_generation++;
  45. err = insert_inode_locked(inode);
  46. if (err) {
  47. err = -EINVAL;
  48. nid_free = true;
  49. goto fail;
  50. }
  51. /* If the directory encrypted, then we should encrypt the inode. */
  52. if (f2fs_encrypted_inode(dir) && f2fs_may_encrypt(inode))
  53. f2fs_set_encrypted_inode(inode);
  54. set_inode_flag(inode, FI_NEW_INODE);
  55. if (test_opt(sbi, INLINE_XATTR))
  56. set_inode_flag(inode, FI_INLINE_XATTR);
  57. if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
  58. set_inode_flag(inode, FI_INLINE_DATA);
  59. if (f2fs_may_inline_dentry(inode))
  60. set_inode_flag(inode, FI_INLINE_DENTRY);
  61. f2fs_init_extent_tree(inode, NULL);
  62. stat_inc_inline_xattr(inode);
  63. stat_inc_inline_inode(inode);
  64. stat_inc_inline_dir(inode);
  65. trace_f2fs_new_inode(inode, 0);
  66. return inode;
  67. fail:
  68. trace_f2fs_new_inode(inode, err);
  69. make_bad_inode(inode);
  70. if (nid_free)
  71. set_inode_flag(inode, FI_FREE_NID);
  72. iput(inode);
  73. return ERR_PTR(err);
  74. }
  75. static int is_multimedia_file(const unsigned char *s, const char *sub)
  76. {
  77. size_t slen = strlen(s);
  78. size_t sublen = strlen(sub);
  79. int i;
  80. /*
  81. * filename format of multimedia file should be defined as:
  82. * "filename + '.' + extension + (optional: '.' + temp extension)".
  83. */
  84. if (slen < sublen + 2)
  85. return 0;
  86. for (i = 1; i < slen - sublen; i++) {
  87. if (s[i] != '.')
  88. continue;
  89. if (!strncasecmp(s + i + 1, sub, sublen))
  90. return 1;
  91. }
  92. return 0;
  93. }
  94. /*
  95. * Set multimedia files as cold files for hot/cold data separation
  96. */
  97. static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
  98. const unsigned char *name)
  99. {
  100. int i;
  101. __u8 (*extlist)[8] = sbi->raw_super->extension_list;
  102. int count = le32_to_cpu(sbi->raw_super->extension_count);
  103. for (i = 0; i < count; i++) {
  104. if (is_multimedia_file(name, extlist[i])) {
  105. file_set_cold(inode);
  106. break;
  107. }
  108. }
  109. }
  110. static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  111. struct nameidata *nd)
  112. {
  113. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  114. struct inode *inode;
  115. nid_t ino = 0;
  116. int err;
  117. inode = f2fs_new_inode(dir, mode);
  118. if (IS_ERR(inode))
  119. return PTR_ERR(inode);
  120. if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
  121. set_cold_files(sbi, inode, dentry->d_name.name);
  122. inode->i_op = &f2fs_file_inode_operations;
  123. inode->i_fop = &f2fs_file_operations;
  124. inode->i_mapping->a_ops = &f2fs_dblock_aops;
  125. ino = inode->i_ino;
  126. f2fs_balance_fs(sbi, true);
  127. f2fs_lock_op(sbi);
  128. err = f2fs_add_link(dentry, inode);
  129. if (err)
  130. goto out;
  131. f2fs_unlock_op(sbi);
  132. alloc_nid_done(sbi, ino);
  133. d_instantiate(dentry, inode);
  134. unlock_new_inode(inode);
  135. if (IS_DIRSYNC(dir))
  136. f2fs_sync_fs(sbi->sb, 1);
  137. return 0;
  138. out:
  139. handle_failed_inode(inode);
  140. return err;
  141. }
  142. static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
  143. struct dentry *dentry)
  144. {
  145. struct inode *inode = d_inode(old_dentry);
  146. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  147. int err;
  148. if (f2fs_encrypted_inode(dir) &&
  149. !fscrypt_has_permitted_context(dir, inode))
  150. return -EPERM;
  151. f2fs_balance_fs(sbi, true);
  152. inode->i_ctime = current_time(inode);
  153. ihold(inode);
  154. set_inode_flag(inode, FI_INC_LINK);
  155. f2fs_lock_op(sbi);
  156. err = f2fs_add_link(dentry, inode);
  157. if (err)
  158. goto out;
  159. f2fs_unlock_op(sbi);
  160. d_instantiate(dentry, inode);
  161. if (IS_DIRSYNC(dir))
  162. f2fs_sync_fs(sbi->sb, 1);
  163. return 0;
  164. out:
  165. clear_inode_flag(inode, FI_INC_LINK);
  166. iput(inode);
  167. f2fs_unlock_op(sbi);
  168. return err;
  169. }
  170. struct dentry *f2fs_get_parent(struct dentry *child)
  171. {
  172. struct qstr dotdot = {.len = 2, .name = ".."};
  173. struct page *page;
  174. unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot, &page);
  175. if (!ino) {
  176. if (IS_ERR(page))
  177. return ERR_CAST(page);
  178. return ERR_PTR(-ENOENT);
  179. }
  180. return d_obtain_alias(f2fs_iget(child->d_sb, ino));
  181. }
  182. static int __recover_dot_dentries(struct inode *dir, nid_t pino)
  183. {
  184. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  185. struct qstr dot = QSTR_INIT(".", 1);
  186. struct qstr dotdot = QSTR_INIT("..", 2);
  187. struct f2fs_dir_entry *de;
  188. struct page *page;
  189. int err = 0;
  190. if (f2fs_readonly(sbi->sb)) {
  191. f2fs_msg(sbi->sb, KERN_INFO,
  192. "skip recovering inline_dots inode (ino:%lu, pino:%u) "
  193. "in readonly mountpoint", dir->i_ino, pino);
  194. return 0;
  195. }
  196. f2fs_balance_fs(sbi, true);
  197. f2fs_lock_op(sbi);
  198. de = f2fs_find_entry(dir, &dot, &page);
  199. if (de) {
  200. f2fs_dentry_kunmap(dir, page);
  201. f2fs_put_page(page, 0);
  202. } else if (IS_ERR(page)) {
  203. err = PTR_ERR(page);
  204. goto out;
  205. } else {
  206. err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
  207. if (err)
  208. goto out;
  209. }
  210. de = f2fs_find_entry(dir, &dotdot, &page);
  211. if (de) {
  212. f2fs_dentry_kunmap(dir, page);
  213. f2fs_put_page(page, 0);
  214. } else if (IS_ERR(page)) {
  215. err = PTR_ERR(page);
  216. } else {
  217. err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
  218. }
  219. out:
  220. if (!err)
  221. clear_inode_flag(dir, FI_INLINE_DOTS);
  222. f2fs_unlock_op(sbi);
  223. return err;
  224. }
  225. static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
  226. struct nameidata *nd)
  227. {
  228. struct inode *inode = NULL;
  229. struct f2fs_dir_entry *de;
  230. struct page *page;
  231. nid_t ino;
  232. int err = 0;
  233. unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
  234. if (f2fs_encrypted_inode(dir)) {
  235. int res = fscrypt_get_encryption_info(dir);
  236. /*
  237. * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is
  238. * created while the directory was encrypted and we
  239. * don't have access to the key.
  240. */
  241. if (fscrypt_has_encryption_key(dir))
  242. fscrypt_set_encrypted_dentry(dentry);
  243. fscrypt_set_d_op(dentry);
  244. if (res && res != -ENOKEY)
  245. return ERR_PTR(res);
  246. }
  247. if (dentry->d_name.len > F2FS_NAME_LEN)
  248. return ERR_PTR(-ENAMETOOLONG);
  249. de = f2fs_find_entry(dir, &dentry->d_name, &page);
  250. if (!de) {
  251. if (IS_ERR(page))
  252. return (struct dentry *)page;
  253. return d_splice_alias(inode, dentry);
  254. }
  255. ino = le32_to_cpu(de->ino);
  256. f2fs_dentry_kunmap(dir, page);
  257. f2fs_put_page(page, 0);
  258. inode = f2fs_iget(dir->i_sb, ino);
  259. if (IS_ERR(inode))
  260. return ERR_CAST(inode);
  261. if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
  262. err = __recover_dot_dentries(dir, root_ino);
  263. if (err)
  264. goto err_out;
  265. }
  266. if (f2fs_has_inline_dots(inode)) {
  267. err = __recover_dot_dentries(inode, dir->i_ino);
  268. if (err)
  269. goto err_out;
  270. }
  271. if (!IS_ERR(inode) && f2fs_encrypted_inode(dir) &&
  272. (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
  273. !fscrypt_has_permitted_context(dir, inode)) {
  274. bool nokey = f2fs_encrypted_inode(inode) &&
  275. !fscrypt_has_encryption_key(inode);
  276. err = nokey ? -ENOKEY : -EPERM;
  277. goto err_out;
  278. }
  279. return d_splice_alias(inode, dentry);
  280. err_out:
  281. iput(inode);
  282. return ERR_PTR(err);
  283. }
  284. static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
  285. {
  286. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  287. struct inode *inode = d_inode(dentry);
  288. struct f2fs_dir_entry *de;
  289. struct page *page;
  290. int err = -ENOENT;
  291. trace_f2fs_unlink_enter(dir, dentry);
  292. de = f2fs_find_entry(dir, &dentry->d_name, &page);
  293. if (!de) {
  294. if (IS_ERR(page))
  295. err = PTR_ERR(page);
  296. goto fail;
  297. }
  298. f2fs_balance_fs(sbi, true);
  299. f2fs_lock_op(sbi);
  300. err = acquire_orphan_inode(sbi);
  301. if (err) {
  302. f2fs_unlock_op(sbi);
  303. f2fs_dentry_kunmap(dir, page);
  304. f2fs_put_page(page, 0);
  305. goto fail;
  306. }
  307. f2fs_delete_entry(de, page, dir, inode);
  308. f2fs_unlock_op(sbi);
  309. if (IS_DIRSYNC(dir))
  310. f2fs_sync_fs(sbi->sb, 1);
  311. fail:
  312. trace_f2fs_unlink_exit(inode, err);
  313. return err;
  314. }
  315. static void *f2fs_follow_link(struct dentry *dentry, struct nameidata *nd)
  316. {
  317. struct page *page;
  318. char *link;
  319. page = page_follow_link_light(dentry, nd);
  320. if (IS_ERR(page))
  321. return page;
  322. link = nd_get_link(nd);
  323. if (IS_ERR(link))
  324. return link;
  325. /* this is broken symlink case */
  326. if (*link == 0) {
  327. kunmap(page);
  328. page_cache_release(page);
  329. return ERR_PTR(-ENOENT);
  330. }
  331. return page;
  332. }
  333. static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
  334. const char *symname)
  335. {
  336. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  337. struct inode *inode;
  338. size_t len = strlen(symname);
  339. struct fscrypt_str disk_link = FSTR_INIT((char *)symname, len + 1);
  340. struct fscrypt_symlink_data *sd = NULL;
  341. int err;
  342. if (f2fs_encrypted_inode(dir)) {
  343. err = fscrypt_get_encryption_info(dir);
  344. if (err)
  345. return err;
  346. if (!fscrypt_has_encryption_key(dir))
  347. return -EPERM;
  348. disk_link.len = (fscrypt_fname_encrypted_size(dir, len) +
  349. sizeof(struct fscrypt_symlink_data));
  350. }
  351. if (disk_link.len > dir->i_sb->s_blocksize)
  352. return -ENAMETOOLONG;
  353. inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
  354. if (IS_ERR(inode))
  355. return PTR_ERR(inode);
  356. if (f2fs_encrypted_inode(inode))
  357. inode->i_op = &f2fs_encrypted_symlink_inode_operations;
  358. else
  359. inode->i_op = &f2fs_symlink_inode_operations;
  360. inode_nohighmem(inode);
  361. inode->i_mapping->a_ops = &f2fs_dblock_aops;
  362. f2fs_balance_fs(sbi, true);
  363. f2fs_lock_op(sbi);
  364. err = f2fs_add_link(dentry, inode);
  365. if (err)
  366. goto out;
  367. f2fs_unlock_op(sbi);
  368. alloc_nid_done(sbi, inode->i_ino);
  369. if (f2fs_encrypted_inode(inode)) {
  370. struct qstr istr = QSTR_INIT(symname, len);
  371. struct fscrypt_str ostr;
  372. sd = kzalloc(disk_link.len, GFP_NOFS);
  373. if (!sd) {
  374. err = -ENOMEM;
  375. goto err_out;
  376. }
  377. err = fscrypt_get_encryption_info(inode);
  378. if (err)
  379. goto err_out;
  380. if (!fscrypt_has_encryption_key(inode)) {
  381. err = -EPERM;
  382. goto err_out;
  383. }
  384. ostr.name = sd->encrypted_path;
  385. ostr.len = disk_link.len;
  386. err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
  387. if (err < 0)
  388. goto err_out;
  389. sd->len = cpu_to_le16(ostr.len);
  390. disk_link.name = (char *)sd;
  391. }
  392. err = page_symlink(inode, disk_link.name, disk_link.len);
  393. err_out:
  394. d_instantiate(dentry, inode);
  395. unlock_new_inode(inode);
  396. /*
  397. * Let's flush symlink data in order to avoid broken symlink as much as
  398. * possible. Nevertheless, fsyncing is the best way, but there is no
  399. * way to get a file descriptor in order to flush that.
  400. *
  401. * Note that, it needs to do dir->fsync to make this recoverable.
  402. * If the symlink path is stored into inline_data, there is no
  403. * performance regression.
  404. */
  405. if (!err) {
  406. filemap_write_and_wait_range(inode->i_mapping, 0,
  407. disk_link.len - 1);
  408. if (IS_DIRSYNC(dir))
  409. f2fs_sync_fs(sbi->sb, 1);
  410. } else {
  411. f2fs_unlink(dir, dentry);
  412. }
  413. kfree(sd);
  414. return err;
  415. out:
  416. handle_failed_inode(inode);
  417. return err;
  418. }
  419. static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  420. {
  421. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  422. struct inode *inode;
  423. int err;
  424. inode = f2fs_new_inode(dir, S_IFDIR | mode);
  425. if (IS_ERR(inode))
  426. return PTR_ERR(inode);
  427. inode->i_op = &f2fs_dir_inode_operations;
  428. inode->i_fop = &f2fs_dir_operations;
  429. inode->i_mapping->a_ops = &f2fs_dblock_aops;
  430. mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
  431. f2fs_balance_fs(sbi, true);
  432. set_inode_flag(inode, FI_INC_LINK);
  433. f2fs_lock_op(sbi);
  434. err = f2fs_add_link(dentry, inode);
  435. if (err)
  436. goto out_fail;
  437. f2fs_unlock_op(sbi);
  438. alloc_nid_done(sbi, inode->i_ino);
  439. d_instantiate(dentry, inode);
  440. unlock_new_inode(inode);
  441. if (IS_DIRSYNC(dir))
  442. f2fs_sync_fs(sbi->sb, 1);
  443. return 0;
  444. out_fail:
  445. clear_inode_flag(inode, FI_INC_LINK);
  446. handle_failed_inode(inode);
  447. return err;
  448. }
  449. static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
  450. {
  451. struct inode *inode = d_inode(dentry);
  452. if (f2fs_empty_dir(inode))
  453. return f2fs_unlink(dir, dentry);
  454. return -ENOTEMPTY;
  455. }
  456. static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
  457. umode_t mode, dev_t rdev)
  458. {
  459. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  460. struct inode *inode;
  461. int err = 0;
  462. if (!new_valid_dev(rdev))
  463. return -EINVAL;
  464. inode = f2fs_new_inode(dir, mode);
  465. if (IS_ERR(inode))
  466. return PTR_ERR(inode);
  467. init_special_inode(inode, inode->i_mode, rdev);
  468. inode->i_op = &f2fs_special_inode_operations;
  469. f2fs_balance_fs(sbi, true);
  470. f2fs_lock_op(sbi);
  471. err = f2fs_add_link(dentry, inode);
  472. if (err)
  473. goto out;
  474. f2fs_unlock_op(sbi);
  475. alloc_nid_done(sbi, inode->i_ino);
  476. d_instantiate(dentry, inode);
  477. unlock_new_inode(inode);
  478. if (IS_DIRSYNC(dir))
  479. f2fs_sync_fs(sbi->sb, 1);
  480. return 0;
  481. out:
  482. handle_failed_inode(inode);
  483. return err;
  484. }
  485. static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
  486. struct inode *new_dir, struct dentry *new_dentry)
  487. {
  488. struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
  489. struct inode *old_inode = d_inode(old_dentry);
  490. struct inode *new_inode = d_inode(new_dentry);
  491. struct page *old_dir_page;
  492. struct page *old_page, *new_page;
  493. struct f2fs_dir_entry *old_dir_entry = NULL;
  494. struct f2fs_dir_entry *old_entry;
  495. struct f2fs_dir_entry *new_entry;
  496. bool is_old_inline = f2fs_has_inline_dentry(old_dir);
  497. int err = -ENOENT;
  498. if ((old_dir != new_dir) && f2fs_encrypted_inode(new_dir) &&
  499. !fscrypt_has_permitted_context(new_dir, old_inode)) {
  500. err = -EPERM;
  501. goto out;
  502. }
  503. old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
  504. if (!old_entry) {
  505. if (IS_ERR(old_page))
  506. err = PTR_ERR(old_page);
  507. goto out;
  508. }
  509. if (S_ISDIR(old_inode->i_mode)) {
  510. old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
  511. if (!old_dir_entry) {
  512. if (IS_ERR(old_dir_page))
  513. err = PTR_ERR(old_dir_page);
  514. goto out_old;
  515. }
  516. }
  517. if (new_inode) {
  518. err = -ENOTEMPTY;
  519. if (old_dir_entry && !f2fs_empty_dir(new_inode))
  520. goto out_dir;
  521. err = -ENOENT;
  522. new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
  523. &new_page);
  524. if (!new_entry) {
  525. if (IS_ERR(new_page))
  526. err = PTR_ERR(new_page);
  527. goto out_dir;
  528. }
  529. f2fs_balance_fs(sbi, true);
  530. f2fs_lock_op(sbi);
  531. err = acquire_orphan_inode(sbi);
  532. if (err)
  533. goto put_out_dir;
  534. err = update_dent_inode(old_inode, new_inode,
  535. &new_dentry->d_name);
  536. if (err) {
  537. release_orphan_inode(sbi);
  538. goto put_out_dir;
  539. }
  540. f2fs_set_link(new_dir, new_entry, new_page, old_inode);
  541. new_inode->i_ctime = current_time(new_inode);
  542. down_write(&F2FS_I(new_inode)->i_sem);
  543. if (old_dir_entry)
  544. f2fs_i_links_write(new_inode, false);
  545. f2fs_i_links_write(new_inode, false);
  546. up_write(&F2FS_I(new_inode)->i_sem);
  547. if (!new_inode->i_nlink)
  548. add_orphan_inode(new_inode);
  549. else
  550. release_orphan_inode(sbi);
  551. } else {
  552. f2fs_balance_fs(sbi, true);
  553. f2fs_lock_op(sbi);
  554. err = f2fs_add_link(new_dentry, old_inode);
  555. if (err) {
  556. f2fs_unlock_op(sbi);
  557. goto out_dir;
  558. }
  559. if (old_dir_entry)
  560. f2fs_i_links_write(new_dir, true);
  561. /*
  562. * old entry and new entry can locate in the same inline
  563. * dentry in inode, when attaching new entry in inline dentry,
  564. * it could force inline dentry conversion, after that,
  565. * old_entry and old_page will point to wrong address, in
  566. * order to avoid this, let's do the check and update here.
  567. */
  568. if (is_old_inline && !f2fs_has_inline_dentry(old_dir)) {
  569. f2fs_put_page(old_page, 0);
  570. old_page = NULL;
  571. old_entry = f2fs_find_entry(old_dir,
  572. &old_dentry->d_name, &old_page);
  573. if (!old_entry) {
  574. err = -ENOENT;
  575. if (IS_ERR(old_page))
  576. err = PTR_ERR(old_page);
  577. f2fs_unlock_op(sbi);
  578. goto out_dir;
  579. }
  580. }
  581. }
  582. down_write(&F2FS_I(old_inode)->i_sem);
  583. file_lost_pino(old_inode);
  584. if (new_inode && file_enc_name(new_inode))
  585. file_set_enc_name(old_inode);
  586. up_write(&F2FS_I(old_inode)->i_sem);
  587. old_inode->i_ctime = current_time(old_inode);
  588. f2fs_mark_inode_dirty_sync(old_inode, false);
  589. f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
  590. if (old_dir_entry) {
  591. if (old_dir != new_dir) {
  592. f2fs_set_link(old_inode, old_dir_entry,
  593. old_dir_page, new_dir);
  594. } else {
  595. f2fs_dentry_kunmap(old_inode, old_dir_page);
  596. f2fs_put_page(old_dir_page, 0);
  597. }
  598. f2fs_i_links_write(old_dir, false);
  599. }
  600. f2fs_unlock_op(sbi);
  601. if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
  602. f2fs_sync_fs(sbi->sb, 1);
  603. return 0;
  604. put_out_dir:
  605. f2fs_unlock_op(sbi);
  606. f2fs_dentry_kunmap(new_dir, new_page);
  607. f2fs_put_page(new_page, 0);
  608. out_dir:
  609. if (old_dir_entry) {
  610. f2fs_dentry_kunmap(old_inode, old_dir_page);
  611. f2fs_put_page(old_dir_page, 0);
  612. }
  613. out_old:
  614. f2fs_dentry_kunmap(old_dir, old_page);
  615. f2fs_put_page(old_page, 0);
  616. out:
  617. return err;
  618. }
  619. static void *f2fs_encrypted_follow_link(struct dentry *dentry,
  620. struct nameidata *nd)
  621. {
  622. struct page *cpage = NULL;
  623. char *caddr, *paddr = NULL;
  624. struct fscrypt_str cstr = FSTR_INIT(NULL, 0);
  625. struct fscrypt_str pstr = FSTR_INIT(NULL, 0);
  626. struct fscrypt_symlink_data *sd;
  627. struct inode *inode = d_inode(dentry);
  628. u32 max_size = inode->i_sb->s_blocksize;
  629. int res;
  630. res = fscrypt_get_encryption_info(inode);
  631. if (res)
  632. return ERR_PTR(res);
  633. cpage = read_mapping_page(inode->i_mapping, 0, NULL);
  634. if (IS_ERR(cpage))
  635. return ERR_CAST(cpage);
  636. caddr = kmap(cpage);
  637. /* Symlink is encrypted */
  638. sd = (struct fscrypt_symlink_data *)caddr;
  639. cstr.name = sd->encrypted_path;
  640. cstr.len = le16_to_cpu(sd->len);
  641. /* this is broken symlink case */
  642. if (unlikely(cstr.len == 0)) {
  643. res = -ENOENT;
  644. goto errout;
  645. }
  646. if ((cstr.len + sizeof(struct fscrypt_symlink_data) - 1) > max_size) {
  647. /* Symlink data on the disk is corrupted */
  648. res = -EIO;
  649. goto errout;
  650. }
  651. res = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
  652. if (res)
  653. goto errout;
  654. res = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
  655. if (res < 0)
  656. goto errout;
  657. /* this is broken symlink case */
  658. if (unlikely(pstr.name[0] == 0)) {
  659. res = -ENOENT;
  660. goto errout;
  661. }
  662. paddr = pstr.name;
  663. /* Null-terminate the name */
  664. paddr[res] = '\0';
  665. nd_set_link(nd, paddr);
  666. kunmap(cpage);
  667. page_cache_release(cpage);
  668. return NULL;
  669. errout:
  670. fscrypt_fname_free_buffer(&pstr);
  671. kunmap(cpage);
  672. page_cache_release(cpage);
  673. return ERR_PTR(res);
  674. }
  675. void kfree_put_link(struct dentry *dentry, struct nameidata *nd,
  676. void *cookie)
  677. {
  678. char *s = nd_get_link(nd);
  679. if (!IS_ERR(s))
  680. kfree(s);
  681. }
  682. const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
  683. .readlink = generic_readlink,
  684. .follow_link = f2fs_encrypted_follow_link,
  685. .put_link = kfree_put_link,
  686. .getattr = f2fs_getattr,
  687. .setattr = f2fs_setattr,
  688. #ifdef CONFIG_F2FS_FS_XATTR
  689. .setxattr = generic_setxattr,
  690. .getxattr = generic_getxattr,
  691. .listxattr = f2fs_listxattr,
  692. .removexattr = generic_removexattr,
  693. #endif
  694. };
  695. const struct inode_operations f2fs_dir_inode_operations = {
  696. .create = f2fs_create,
  697. .lookup = f2fs_lookup,
  698. .link = f2fs_link,
  699. .unlink = f2fs_unlink,
  700. .symlink = f2fs_symlink,
  701. .mkdir = f2fs_mkdir,
  702. .rmdir = f2fs_rmdir,
  703. .mknod = f2fs_mknod,
  704. .rename = f2fs_rename,
  705. .getattr = f2fs_getattr,
  706. .setattr = f2fs_setattr,
  707. .get_acl = f2fs_get_acl,
  708. #ifdef CONFIG_F2FS_FS_XATTR
  709. .setxattr = generic_setxattr,
  710. .getxattr = generic_getxattr,
  711. .listxattr = f2fs_listxattr,
  712. .removexattr = generic_removexattr,
  713. #endif
  714. };
  715. const struct inode_operations f2fs_symlink_inode_operations = {
  716. .readlink = generic_readlink,
  717. .follow_link = f2fs_follow_link,
  718. .put_link = page_put_link,
  719. .getattr = f2fs_getattr,
  720. .setattr = f2fs_setattr,
  721. #ifdef CONFIG_F2FS_FS_XATTR
  722. .setxattr = generic_setxattr,
  723. .getxattr = generic_getxattr,
  724. .listxattr = f2fs_listxattr,
  725. .removexattr = generic_removexattr,
  726. #endif
  727. };
  728. const struct inode_operations f2fs_special_inode_operations = {
  729. .getattr = f2fs_getattr,
  730. .setattr = f2fs_setattr,
  731. .get_acl = f2fs_get_acl,
  732. #ifdef CONFIG_F2FS_FS_XATTR
  733. .setxattr = generic_setxattr,
  734. .getxattr = generic_getxattr,
  735. .listxattr = f2fs_listxattr,
  736. .removexattr = generic_removexattr,
  737. #endif
  738. };