namei.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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. bool excl)
  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_new(dentry, inode);
  134. if (IS_DIRSYNC(dir))
  135. f2fs_sync_fs(sbi->sb, 1);
  136. return 0;
  137. out:
  138. handle_failed_inode(inode);
  139. return err;
  140. }
  141. static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
  142. struct dentry *dentry)
  143. {
  144. struct inode *inode = d_inode(old_dentry);
  145. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  146. int err;
  147. if (f2fs_encrypted_inode(dir) &&
  148. !fscrypt_has_permitted_context(dir, inode))
  149. return -EPERM;
  150. f2fs_balance_fs(sbi, true);
  151. inode->i_ctime = current_time(inode);
  152. ihold(inode);
  153. set_inode_flag(inode, FI_INC_LINK);
  154. f2fs_lock_op(sbi);
  155. err = f2fs_add_link(dentry, inode);
  156. if (err)
  157. goto out;
  158. f2fs_unlock_op(sbi);
  159. d_instantiate(dentry, inode);
  160. if (IS_DIRSYNC(dir))
  161. f2fs_sync_fs(sbi->sb, 1);
  162. return 0;
  163. out:
  164. clear_inode_flag(inode, FI_INC_LINK);
  165. iput(inode);
  166. f2fs_unlock_op(sbi);
  167. return err;
  168. }
  169. struct dentry *f2fs_get_parent(struct dentry *child)
  170. {
  171. struct qstr dotdot = QSTR_INIT("..", 2);
  172. struct page *page;
  173. unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot, &page);
  174. if (!ino) {
  175. if (IS_ERR(page))
  176. return ERR_CAST(page);
  177. return ERR_PTR(-ENOENT);
  178. }
  179. return d_obtain_alias(f2fs_iget(child->d_sb, ino));
  180. }
  181. static int __recover_dot_dentries(struct inode *dir, nid_t pino)
  182. {
  183. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  184. struct qstr dot = QSTR_INIT(".", 1);
  185. struct qstr dotdot = QSTR_INIT("..", 2);
  186. struct f2fs_dir_entry *de;
  187. struct page *page;
  188. int err = 0;
  189. if (f2fs_readonly(sbi->sb)) {
  190. f2fs_msg(sbi->sb, KERN_INFO,
  191. "skip recovering inline_dots inode (ino:%lu, pino:%u) "
  192. "in readonly mountpoint", dir->i_ino, pino);
  193. return 0;
  194. }
  195. f2fs_balance_fs(sbi, true);
  196. f2fs_lock_op(sbi);
  197. de = f2fs_find_entry(dir, &dot, &page);
  198. if (de) {
  199. f2fs_dentry_kunmap(dir, page);
  200. f2fs_put_page(page, 0);
  201. } else if (IS_ERR(page)) {
  202. err = PTR_ERR(page);
  203. goto out;
  204. } else {
  205. err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
  206. if (err)
  207. goto out;
  208. }
  209. de = f2fs_find_entry(dir, &dotdot, &page);
  210. if (de) {
  211. f2fs_dentry_kunmap(dir, page);
  212. f2fs_put_page(page, 0);
  213. } else if (IS_ERR(page)) {
  214. err = PTR_ERR(page);
  215. } else {
  216. err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
  217. }
  218. out:
  219. if (!err)
  220. clear_inode_flag(dir, FI_INLINE_DOTS);
  221. f2fs_unlock_op(sbi);
  222. return err;
  223. }
  224. static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
  225. unsigned int flags)
  226. {
  227. struct inode *inode = NULL;
  228. struct f2fs_dir_entry *de;
  229. struct page *page;
  230. nid_t ino;
  231. int err = 0;
  232. unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
  233. if (f2fs_encrypted_inode(dir)) {
  234. int res = fscrypt_get_encryption_info(dir);
  235. /*
  236. * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is
  237. * created while the directory was encrypted and we
  238. * don't have access to the key.
  239. */
  240. if (fscrypt_has_encryption_key(dir))
  241. fscrypt_set_encrypted_dentry(dentry);
  242. fscrypt_set_d_op(dentry);
  243. if (res && res != -ENOKEY)
  244. return ERR_PTR(res);
  245. }
  246. if (dentry->d_name.len > F2FS_NAME_LEN)
  247. return ERR_PTR(-ENAMETOOLONG);
  248. de = f2fs_find_entry(dir, &dentry->d_name, &page);
  249. if (!de) {
  250. if (IS_ERR(page))
  251. return (struct dentry *)page;
  252. return d_splice_alias(inode, dentry);
  253. }
  254. ino = le32_to_cpu(de->ino);
  255. f2fs_dentry_kunmap(dir, page);
  256. f2fs_put_page(page, 0);
  257. inode = f2fs_iget(dir->i_sb, ino);
  258. if (IS_ERR(inode))
  259. return ERR_CAST(inode);
  260. if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
  261. err = __recover_dot_dentries(dir, root_ino);
  262. if (err)
  263. goto err_out;
  264. }
  265. if (f2fs_has_inline_dots(inode)) {
  266. err = __recover_dot_dentries(inode, dir->i_ino);
  267. if (err)
  268. goto err_out;
  269. }
  270. if (!IS_ERR(inode) && f2fs_encrypted_inode(dir) &&
  271. (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
  272. !fscrypt_has_permitted_context(dir, inode)) {
  273. bool nokey = f2fs_encrypted_inode(inode) &&
  274. !fscrypt_has_encryption_key(inode);
  275. err = nokey ? -ENOKEY : -EPERM;
  276. goto err_out;
  277. }
  278. return d_splice_alias(inode, dentry);
  279. err_out:
  280. iput(inode);
  281. return ERR_PTR(err);
  282. }
  283. static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
  284. {
  285. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  286. struct inode *inode = d_inode(dentry);
  287. struct f2fs_dir_entry *de;
  288. struct page *page;
  289. int err = -ENOENT;
  290. trace_f2fs_unlink_enter(dir, dentry);
  291. de = f2fs_find_entry(dir, &dentry->d_name, &page);
  292. if (!de) {
  293. if (IS_ERR(page))
  294. err = PTR_ERR(page);
  295. goto fail;
  296. }
  297. f2fs_balance_fs(sbi, true);
  298. f2fs_lock_op(sbi);
  299. err = acquire_orphan_inode(sbi);
  300. if (err) {
  301. f2fs_unlock_op(sbi);
  302. f2fs_dentry_kunmap(dir, page);
  303. f2fs_put_page(page, 0);
  304. goto fail;
  305. }
  306. f2fs_delete_entry(de, page, dir, inode);
  307. f2fs_unlock_op(sbi);
  308. if (IS_DIRSYNC(dir))
  309. f2fs_sync_fs(sbi->sb, 1);
  310. fail:
  311. trace_f2fs_unlink_exit(inode, err);
  312. return err;
  313. }
  314. static const char *f2fs_get_link(struct dentry *dentry,
  315. struct inode *inode,
  316. struct delayed_call *done)
  317. {
  318. const char *link = page_get_link(dentry, inode, done);
  319. if (!IS_ERR(link) && !*link) {
  320. /* this is broken symlink case */
  321. do_delayed_call(done);
  322. clear_delayed_call(done);
  323. link = ERR_PTR(-ENOENT);
  324. }
  325. return link;
  326. }
  327. static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
  328. const char *symname)
  329. {
  330. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  331. struct inode *inode;
  332. size_t len = strlen(symname);
  333. struct fscrypt_str disk_link = FSTR_INIT((char *)symname, len + 1);
  334. struct fscrypt_symlink_data *sd = NULL;
  335. int err;
  336. if (f2fs_encrypted_inode(dir)) {
  337. err = fscrypt_get_encryption_info(dir);
  338. if (err)
  339. return err;
  340. if (!fscrypt_has_encryption_key(dir))
  341. return -ENOKEY;
  342. disk_link.len = (fscrypt_fname_encrypted_size(dir, len) +
  343. sizeof(struct fscrypt_symlink_data));
  344. }
  345. if (disk_link.len > dir->i_sb->s_blocksize)
  346. return -ENAMETOOLONG;
  347. inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
  348. if (IS_ERR(inode))
  349. return PTR_ERR(inode);
  350. if (f2fs_encrypted_inode(inode))
  351. inode->i_op = &f2fs_encrypted_symlink_inode_operations;
  352. else
  353. inode->i_op = &f2fs_symlink_inode_operations;
  354. inode_nohighmem(inode);
  355. inode->i_mapping->a_ops = &f2fs_dblock_aops;
  356. f2fs_balance_fs(sbi, true);
  357. f2fs_lock_op(sbi);
  358. err = f2fs_add_link(dentry, inode);
  359. if (err)
  360. goto out;
  361. f2fs_unlock_op(sbi);
  362. alloc_nid_done(sbi, inode->i_ino);
  363. if (f2fs_encrypted_inode(inode)) {
  364. struct qstr istr = QSTR_INIT(symname, len);
  365. struct fscrypt_str ostr;
  366. sd = kzalloc(disk_link.len, GFP_NOFS);
  367. if (!sd) {
  368. err = -ENOMEM;
  369. goto err_out;
  370. }
  371. err = fscrypt_get_encryption_info(inode);
  372. if (err)
  373. goto err_out;
  374. if (!fscrypt_has_encryption_key(inode)) {
  375. err = -ENOKEY;
  376. goto err_out;
  377. }
  378. ostr.name = sd->encrypted_path;
  379. ostr.len = disk_link.len;
  380. err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
  381. if (err)
  382. goto err_out;
  383. sd->len = cpu_to_le16(ostr.len);
  384. disk_link.name = (char *)sd;
  385. }
  386. err = page_symlink(inode, disk_link.name, disk_link.len);
  387. err_out:
  388. d_instantiate_new(dentry, inode);
  389. /*
  390. * Let's flush symlink data in order to avoid broken symlink as much as
  391. * possible. Nevertheless, fsyncing is the best way, but there is no
  392. * way to get a file descriptor in order to flush that.
  393. *
  394. * Note that, it needs to do dir->fsync to make this recoverable.
  395. * If the symlink path is stored into inline_data, there is no
  396. * performance regression.
  397. */
  398. if (!err) {
  399. filemap_write_and_wait_range(inode->i_mapping, 0,
  400. disk_link.len - 1);
  401. if (IS_DIRSYNC(dir))
  402. f2fs_sync_fs(sbi->sb, 1);
  403. } else {
  404. f2fs_unlink(dir, dentry);
  405. }
  406. kfree(sd);
  407. return err;
  408. out:
  409. handle_failed_inode(inode);
  410. return err;
  411. }
  412. static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  413. {
  414. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  415. struct inode *inode;
  416. int err;
  417. inode = f2fs_new_inode(dir, S_IFDIR | mode);
  418. if (IS_ERR(inode))
  419. return PTR_ERR(inode);
  420. inode->i_op = &f2fs_dir_inode_operations;
  421. inode->i_fop = &f2fs_dir_operations;
  422. inode->i_mapping->a_ops = &f2fs_dblock_aops;
  423. mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
  424. f2fs_balance_fs(sbi, true);
  425. set_inode_flag(inode, FI_INC_LINK);
  426. f2fs_lock_op(sbi);
  427. err = f2fs_add_link(dentry, inode);
  428. if (err)
  429. goto out_fail;
  430. f2fs_unlock_op(sbi);
  431. alloc_nid_done(sbi, inode->i_ino);
  432. d_instantiate_new(dentry, inode);
  433. if (IS_DIRSYNC(dir))
  434. f2fs_sync_fs(sbi->sb, 1);
  435. return 0;
  436. out_fail:
  437. clear_inode_flag(inode, FI_INC_LINK);
  438. handle_failed_inode(inode);
  439. return err;
  440. }
  441. static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
  442. {
  443. struct inode *inode = d_inode(dentry);
  444. if (f2fs_empty_dir(inode))
  445. return f2fs_unlink(dir, dentry);
  446. return -ENOTEMPTY;
  447. }
  448. static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
  449. umode_t mode, dev_t rdev)
  450. {
  451. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  452. struct inode *inode;
  453. int err = 0;
  454. inode = f2fs_new_inode(dir, mode);
  455. if (IS_ERR(inode))
  456. return PTR_ERR(inode);
  457. init_special_inode(inode, inode->i_mode, rdev);
  458. inode->i_op = &f2fs_special_inode_operations;
  459. f2fs_balance_fs(sbi, true);
  460. f2fs_lock_op(sbi);
  461. err = f2fs_add_link(dentry, inode);
  462. if (err)
  463. goto out;
  464. f2fs_unlock_op(sbi);
  465. alloc_nid_done(sbi, inode->i_ino);
  466. d_instantiate_new(dentry, inode);
  467. if (IS_DIRSYNC(dir))
  468. f2fs_sync_fs(sbi->sb, 1);
  469. return 0;
  470. out:
  471. handle_failed_inode(inode);
  472. return err;
  473. }
  474. static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
  475. umode_t mode, struct inode **whiteout)
  476. {
  477. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  478. struct inode *inode;
  479. int err;
  480. inode = f2fs_new_inode(dir, mode);
  481. if (IS_ERR(inode))
  482. return PTR_ERR(inode);
  483. if (whiteout) {
  484. init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
  485. inode->i_op = &f2fs_special_inode_operations;
  486. } else {
  487. inode->i_op = &f2fs_file_inode_operations;
  488. inode->i_fop = &f2fs_file_operations;
  489. inode->i_mapping->a_ops = &f2fs_dblock_aops;
  490. }
  491. f2fs_balance_fs(sbi, true);
  492. f2fs_lock_op(sbi);
  493. err = acquire_orphan_inode(sbi);
  494. if (err)
  495. goto out;
  496. err = f2fs_do_tmpfile(inode, dir);
  497. if (err)
  498. goto release_out;
  499. /*
  500. * add this non-linked tmpfile to orphan list, in this way we could
  501. * remove all unused data of tmpfile after abnormal power-off.
  502. */
  503. add_orphan_inode(inode);
  504. alloc_nid_done(sbi, inode->i_ino);
  505. if (whiteout) {
  506. f2fs_i_links_write(inode, false);
  507. *whiteout = inode;
  508. } else {
  509. d_tmpfile(dentry, inode);
  510. }
  511. /* link_count was changed by d_tmpfile as well. */
  512. f2fs_unlock_op(sbi);
  513. unlock_new_inode(inode);
  514. return 0;
  515. release_out:
  516. release_orphan_inode(sbi);
  517. out:
  518. handle_failed_inode(inode);
  519. return err;
  520. }
  521. static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  522. {
  523. if (f2fs_encrypted_inode(dir)) {
  524. int err = fscrypt_get_encryption_info(dir);
  525. if (err)
  526. return err;
  527. }
  528. return __f2fs_tmpfile(dir, dentry, mode, NULL);
  529. }
  530. static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
  531. {
  532. return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
  533. }
  534. static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
  535. struct inode *new_dir, struct dentry *new_dentry,
  536. unsigned int flags)
  537. {
  538. struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
  539. struct inode *old_inode = d_inode(old_dentry);
  540. struct inode *new_inode = d_inode(new_dentry);
  541. struct inode *whiteout = NULL;
  542. struct page *old_dir_page;
  543. struct page *old_page, *new_page = NULL;
  544. struct f2fs_dir_entry *old_dir_entry = NULL;
  545. struct f2fs_dir_entry *old_entry;
  546. struct f2fs_dir_entry *new_entry;
  547. bool is_old_inline = f2fs_has_inline_dentry(old_dir);
  548. int err = -ENOENT;
  549. if ((f2fs_encrypted_inode(old_dir) &&
  550. !fscrypt_has_encryption_key(old_dir)) ||
  551. (f2fs_encrypted_inode(new_dir) &&
  552. !fscrypt_has_encryption_key(new_dir)))
  553. return -ENOKEY;
  554. if ((old_dir != new_dir) && f2fs_encrypted_inode(new_dir) &&
  555. !fscrypt_has_permitted_context(new_dir, old_inode)) {
  556. err = -EPERM;
  557. goto out;
  558. }
  559. old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
  560. if (!old_entry) {
  561. if (IS_ERR(old_page))
  562. err = PTR_ERR(old_page);
  563. goto out;
  564. }
  565. if (S_ISDIR(old_inode->i_mode)) {
  566. old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
  567. if (!old_dir_entry) {
  568. if (IS_ERR(old_dir_page))
  569. err = PTR_ERR(old_dir_page);
  570. goto out_old;
  571. }
  572. }
  573. if (flags & RENAME_WHITEOUT) {
  574. err = f2fs_create_whiteout(old_dir, &whiteout);
  575. if (err)
  576. goto out_dir;
  577. }
  578. if (new_inode) {
  579. err = -ENOTEMPTY;
  580. if (old_dir_entry && !f2fs_empty_dir(new_inode))
  581. goto out_whiteout;
  582. err = -ENOENT;
  583. new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
  584. &new_page);
  585. if (!new_entry) {
  586. if (IS_ERR(new_page))
  587. err = PTR_ERR(new_page);
  588. goto out_whiteout;
  589. }
  590. f2fs_balance_fs(sbi, true);
  591. f2fs_lock_op(sbi);
  592. err = acquire_orphan_inode(sbi);
  593. if (err)
  594. goto put_out_dir;
  595. err = update_dent_inode(old_inode, new_inode,
  596. &new_dentry->d_name);
  597. if (err) {
  598. release_orphan_inode(sbi);
  599. goto put_out_dir;
  600. }
  601. f2fs_set_link(new_dir, new_entry, new_page, old_inode);
  602. new_inode->i_ctime = current_time(new_inode);
  603. down_write(&F2FS_I(new_inode)->i_sem);
  604. if (old_dir_entry)
  605. f2fs_i_links_write(new_inode, false);
  606. f2fs_i_links_write(new_inode, false);
  607. up_write(&F2FS_I(new_inode)->i_sem);
  608. if (!new_inode->i_nlink)
  609. add_orphan_inode(new_inode);
  610. else
  611. release_orphan_inode(sbi);
  612. } else {
  613. f2fs_balance_fs(sbi, true);
  614. f2fs_lock_op(sbi);
  615. err = f2fs_add_link(new_dentry, old_inode);
  616. if (err) {
  617. f2fs_unlock_op(sbi);
  618. goto out_whiteout;
  619. }
  620. if (old_dir_entry)
  621. f2fs_i_links_write(new_dir, true);
  622. /*
  623. * old entry and new entry can locate in the same inline
  624. * dentry in inode, when attaching new entry in inline dentry,
  625. * it could force inline dentry conversion, after that,
  626. * old_entry and old_page will point to wrong address, in
  627. * order to avoid this, let's do the check and update here.
  628. */
  629. if (is_old_inline && !f2fs_has_inline_dentry(old_dir)) {
  630. f2fs_put_page(old_page, 0);
  631. old_page = NULL;
  632. old_entry = f2fs_find_entry(old_dir,
  633. &old_dentry->d_name, &old_page);
  634. if (!old_entry) {
  635. err = -ENOENT;
  636. if (IS_ERR(old_page))
  637. err = PTR_ERR(old_page);
  638. f2fs_unlock_op(sbi);
  639. goto out_whiteout;
  640. }
  641. }
  642. }
  643. down_write(&F2FS_I(old_inode)->i_sem);
  644. file_lost_pino(old_inode);
  645. if (new_inode && file_enc_name(new_inode))
  646. file_set_enc_name(old_inode);
  647. up_write(&F2FS_I(old_inode)->i_sem);
  648. old_inode->i_ctime = current_time(old_inode);
  649. f2fs_mark_inode_dirty_sync(old_inode);
  650. f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
  651. if (whiteout) {
  652. whiteout->i_state |= I_LINKABLE;
  653. set_inode_flag(whiteout, FI_INC_LINK);
  654. err = f2fs_add_link(old_dentry, whiteout);
  655. if (err)
  656. goto put_out_dir;
  657. whiteout->i_state &= ~I_LINKABLE;
  658. iput(whiteout);
  659. }
  660. if (old_dir_entry) {
  661. if (old_dir != new_dir && !whiteout) {
  662. f2fs_set_link(old_inode, old_dir_entry,
  663. old_dir_page, new_dir);
  664. } else {
  665. f2fs_dentry_kunmap(old_inode, old_dir_page);
  666. f2fs_put_page(old_dir_page, 0);
  667. }
  668. f2fs_i_links_write(old_dir, false);
  669. }
  670. f2fs_unlock_op(sbi);
  671. if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
  672. f2fs_sync_fs(sbi->sb, 1);
  673. return 0;
  674. put_out_dir:
  675. f2fs_unlock_op(sbi);
  676. if (new_page) {
  677. f2fs_dentry_kunmap(new_dir, new_page);
  678. f2fs_put_page(new_page, 0);
  679. }
  680. out_whiteout:
  681. if (whiteout)
  682. iput(whiteout);
  683. out_dir:
  684. if (old_dir_entry) {
  685. f2fs_dentry_kunmap(old_inode, old_dir_page);
  686. f2fs_put_page(old_dir_page, 0);
  687. }
  688. out_old:
  689. f2fs_dentry_kunmap(old_dir, old_page);
  690. f2fs_put_page(old_page, 0);
  691. out:
  692. return err;
  693. }
  694. static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
  695. struct inode *new_dir, struct dentry *new_dentry)
  696. {
  697. struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
  698. struct inode *old_inode = d_inode(old_dentry);
  699. struct inode *new_inode = d_inode(new_dentry);
  700. struct page *old_dir_page, *new_dir_page;
  701. struct page *old_page, *new_page;
  702. struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
  703. struct f2fs_dir_entry *old_entry, *new_entry;
  704. int old_nlink = 0, new_nlink = 0;
  705. int err = -ENOENT;
  706. if ((f2fs_encrypted_inode(old_dir) &&
  707. !fscrypt_has_encryption_key(old_dir)) ||
  708. (f2fs_encrypted_inode(new_dir) &&
  709. !fscrypt_has_encryption_key(new_dir)))
  710. return -ENOKEY;
  711. if ((f2fs_encrypted_inode(old_dir) || f2fs_encrypted_inode(new_dir)) &&
  712. (old_dir != new_dir) &&
  713. (!fscrypt_has_permitted_context(new_dir, old_inode) ||
  714. !fscrypt_has_permitted_context(old_dir, new_inode)))
  715. return -EPERM;
  716. old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
  717. if (!old_entry) {
  718. if (IS_ERR(old_page))
  719. err = PTR_ERR(old_page);
  720. goto out;
  721. }
  722. new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
  723. if (!new_entry) {
  724. if (IS_ERR(new_page))
  725. err = PTR_ERR(new_page);
  726. goto out_old;
  727. }
  728. /* prepare for updating ".." directory entry info later */
  729. if (old_dir != new_dir) {
  730. if (S_ISDIR(old_inode->i_mode)) {
  731. old_dir_entry = f2fs_parent_dir(old_inode,
  732. &old_dir_page);
  733. if (!old_dir_entry) {
  734. if (IS_ERR(old_dir_page))
  735. err = PTR_ERR(old_dir_page);
  736. goto out_new;
  737. }
  738. }
  739. if (S_ISDIR(new_inode->i_mode)) {
  740. new_dir_entry = f2fs_parent_dir(new_inode,
  741. &new_dir_page);
  742. if (!new_dir_entry) {
  743. if (IS_ERR(new_dir_page))
  744. err = PTR_ERR(new_dir_page);
  745. goto out_old_dir;
  746. }
  747. }
  748. }
  749. /*
  750. * If cross rename between file and directory those are not
  751. * in the same directory, we will inc nlink of file's parent
  752. * later, so we should check upper boundary of its nlink.
  753. */
  754. if ((!old_dir_entry || !new_dir_entry) &&
  755. old_dir_entry != new_dir_entry) {
  756. old_nlink = old_dir_entry ? -1 : 1;
  757. new_nlink = -old_nlink;
  758. err = -EMLINK;
  759. if ((old_nlink > 0 && old_inode->i_nlink >= F2FS_LINK_MAX) ||
  760. (new_nlink > 0 && new_inode->i_nlink >= F2FS_LINK_MAX))
  761. goto out_new_dir;
  762. }
  763. f2fs_balance_fs(sbi, true);
  764. f2fs_lock_op(sbi);
  765. err = update_dent_inode(old_inode, new_inode, &new_dentry->d_name);
  766. if (err)
  767. goto out_unlock;
  768. if (file_enc_name(new_inode))
  769. file_set_enc_name(old_inode);
  770. err = update_dent_inode(new_inode, old_inode, &old_dentry->d_name);
  771. if (err)
  772. goto out_undo;
  773. if (file_enc_name(old_inode))
  774. file_set_enc_name(new_inode);
  775. /* update ".." directory entry info of old dentry */
  776. if (old_dir_entry)
  777. f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
  778. /* update ".." directory entry info of new dentry */
  779. if (new_dir_entry)
  780. f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
  781. /* update directory entry info of old dir inode */
  782. f2fs_set_link(old_dir, old_entry, old_page, new_inode);
  783. down_write(&F2FS_I(old_inode)->i_sem);
  784. file_lost_pino(old_inode);
  785. up_write(&F2FS_I(old_inode)->i_sem);
  786. old_dir->i_ctime = current_time(old_dir);
  787. if (old_nlink) {
  788. down_write(&F2FS_I(old_dir)->i_sem);
  789. f2fs_i_links_write(old_dir, old_nlink > 0);
  790. up_write(&F2FS_I(old_dir)->i_sem);
  791. }
  792. f2fs_mark_inode_dirty_sync(old_dir);
  793. /* update directory entry info of new dir inode */
  794. f2fs_set_link(new_dir, new_entry, new_page, old_inode);
  795. down_write(&F2FS_I(new_inode)->i_sem);
  796. file_lost_pino(new_inode);
  797. up_write(&F2FS_I(new_inode)->i_sem);
  798. new_dir->i_ctime = current_time(new_dir);
  799. if (new_nlink) {
  800. down_write(&F2FS_I(new_dir)->i_sem);
  801. f2fs_i_links_write(new_dir, new_nlink > 0);
  802. up_write(&F2FS_I(new_dir)->i_sem);
  803. }
  804. f2fs_mark_inode_dirty_sync(new_dir);
  805. f2fs_unlock_op(sbi);
  806. if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
  807. f2fs_sync_fs(sbi->sb, 1);
  808. return 0;
  809. out_undo:
  810. /*
  811. * Still we may fail to recover name info of f2fs_inode here
  812. * Drop it, once its name is set as encrypted
  813. */
  814. update_dent_inode(old_inode, old_inode, &old_dentry->d_name);
  815. out_unlock:
  816. f2fs_unlock_op(sbi);
  817. out_new_dir:
  818. if (new_dir_entry) {
  819. f2fs_dentry_kunmap(new_inode, new_dir_page);
  820. f2fs_put_page(new_dir_page, 0);
  821. }
  822. out_old_dir:
  823. if (old_dir_entry) {
  824. f2fs_dentry_kunmap(old_inode, old_dir_page);
  825. f2fs_put_page(old_dir_page, 0);
  826. }
  827. out_new:
  828. f2fs_dentry_kunmap(new_dir, new_page);
  829. f2fs_put_page(new_page, 0);
  830. out_old:
  831. f2fs_dentry_kunmap(old_dir, old_page);
  832. f2fs_put_page(old_page, 0);
  833. out:
  834. return err;
  835. }
  836. static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
  837. struct inode *new_dir, struct dentry *new_dentry,
  838. unsigned int flags)
  839. {
  840. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
  841. return -EINVAL;
  842. if (flags & RENAME_EXCHANGE) {
  843. return f2fs_cross_rename(old_dir, old_dentry,
  844. new_dir, new_dentry);
  845. }
  846. /*
  847. * VFS has already handled the new dentry existence case,
  848. * here, we just deal with "RENAME_NOREPLACE" as regular rename.
  849. */
  850. return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
  851. }
  852. static const char *f2fs_encrypted_get_link(struct dentry *dentry,
  853. struct inode *inode,
  854. struct delayed_call *done)
  855. {
  856. struct page *cpage = NULL;
  857. char *caddr, *paddr = NULL;
  858. struct fscrypt_str cstr = FSTR_INIT(NULL, 0);
  859. struct fscrypt_str pstr = FSTR_INIT(NULL, 0);
  860. struct fscrypt_symlink_data *sd;
  861. u32 max_size = inode->i_sb->s_blocksize;
  862. int res;
  863. if (!dentry)
  864. return ERR_PTR(-ECHILD);
  865. res = fscrypt_get_encryption_info(inode);
  866. if (res)
  867. return ERR_PTR(res);
  868. cpage = read_mapping_page(inode->i_mapping, 0, NULL);
  869. if (IS_ERR(cpage))
  870. return ERR_CAST(cpage);
  871. caddr = page_address(cpage);
  872. /* Symlink is encrypted */
  873. sd = (struct fscrypt_symlink_data *)caddr;
  874. cstr.name = sd->encrypted_path;
  875. cstr.len = le16_to_cpu(sd->len);
  876. /* this is broken symlink case */
  877. if (unlikely(cstr.len == 0)) {
  878. res = -ENOENT;
  879. goto errout;
  880. }
  881. if ((cstr.len + sizeof(struct fscrypt_symlink_data) - 1) > max_size) {
  882. /* Symlink data on the disk is corrupted */
  883. res = -EIO;
  884. goto errout;
  885. }
  886. res = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
  887. if (res)
  888. goto errout;
  889. res = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
  890. if (res)
  891. goto errout;
  892. /* this is broken symlink case */
  893. if (unlikely(pstr.name[0] == 0)) {
  894. res = -ENOENT;
  895. goto errout;
  896. }
  897. paddr = pstr.name;
  898. /* Null-terminate the name */
  899. paddr[pstr.len] = '\0';
  900. put_page(cpage);
  901. set_delayed_call(done, kfree_link, paddr);
  902. return paddr;
  903. errout:
  904. fscrypt_fname_free_buffer(&pstr);
  905. put_page(cpage);
  906. return ERR_PTR(res);
  907. }
  908. const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
  909. .readlink = generic_readlink,
  910. .get_link = f2fs_encrypted_get_link,
  911. .getattr = f2fs_getattr,
  912. .setattr = f2fs_setattr,
  913. #ifdef CONFIG_F2FS_FS_XATTR
  914. .listxattr = f2fs_listxattr,
  915. #endif
  916. };
  917. const struct inode_operations f2fs_dir_inode_operations = {
  918. .create = f2fs_create,
  919. .lookup = f2fs_lookup,
  920. .link = f2fs_link,
  921. .unlink = f2fs_unlink,
  922. .symlink = f2fs_symlink,
  923. .mkdir = f2fs_mkdir,
  924. .rmdir = f2fs_rmdir,
  925. .mknod = f2fs_mknod,
  926. .rename = f2fs_rename2,
  927. .tmpfile = f2fs_tmpfile,
  928. .getattr = f2fs_getattr,
  929. .setattr = f2fs_setattr,
  930. .get_acl = f2fs_get_acl,
  931. .set_acl = f2fs_set_acl,
  932. #ifdef CONFIG_F2FS_FS_XATTR
  933. .listxattr = f2fs_listxattr,
  934. #endif
  935. };
  936. const struct inode_operations f2fs_symlink_inode_operations = {
  937. .readlink = generic_readlink,
  938. .get_link = f2fs_get_link,
  939. .getattr = f2fs_getattr,
  940. .setattr = f2fs_setattr,
  941. #ifdef CONFIG_F2FS_FS_XATTR
  942. .listxattr = f2fs_listxattr,
  943. #endif
  944. };
  945. const struct inode_operations f2fs_special_inode_operations = {
  946. .getattr = f2fs_getattr,
  947. .setattr = f2fs_setattr,
  948. .get_acl = f2fs_get_acl,
  949. .set_acl = f2fs_set_acl,
  950. #ifdef CONFIG_F2FS_FS_XATTR
  951. .listxattr = f2fs_listxattr,
  952. #endif
  953. };