namei.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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;
  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 out;
  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. if (f2fs_may_inline_data(inode))
  55. set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
  56. if (f2fs_may_inline_dentry(inode))
  57. set_inode_flag(F2FS_I(inode), FI_INLINE_DENTRY);
  58. stat_inc_inline_inode(inode);
  59. stat_inc_inline_dir(inode);
  60. trace_f2fs_new_inode(inode, 0);
  61. mark_inode_dirty(inode);
  62. return inode;
  63. out:
  64. clear_nlink(inode);
  65. unlock_new_inode(inode);
  66. fail:
  67. trace_f2fs_new_inode(inode, err);
  68. make_bad_inode(inode);
  69. iput(inode);
  70. if (nid_free)
  71. alloc_nid_failed(sbi, ino);
  72. return ERR_PTR(err);
  73. }
  74. static int is_multimedia_file(const unsigned char *s, const char *sub)
  75. {
  76. size_t slen = strlen(s);
  77. size_t sublen = strlen(sub);
  78. if (sublen > slen)
  79. return 0;
  80. return !strncasecmp(s + slen - sublen, sub, sublen);
  81. }
  82. /*
  83. * Set multimedia files as cold files for hot/cold data separation
  84. */
  85. static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode,
  86. const unsigned char *name)
  87. {
  88. int i;
  89. __u8 (*extlist)[8] = sbi->raw_super->extension_list;
  90. int count = le32_to_cpu(sbi->raw_super->extension_count);
  91. for (i = 0; i < count; i++) {
  92. if (is_multimedia_file(name, extlist[i])) {
  93. file_set_cold(inode);
  94. break;
  95. }
  96. }
  97. }
  98. static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  99. bool excl)
  100. {
  101. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  102. struct inode *inode;
  103. nid_t ino = 0;
  104. int err;
  105. f2fs_balance_fs(sbi);
  106. inode = f2fs_new_inode(dir, mode);
  107. if (IS_ERR(inode))
  108. return PTR_ERR(inode);
  109. if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
  110. set_cold_files(sbi, inode, dentry->d_name.name);
  111. inode->i_op = &f2fs_file_inode_operations;
  112. inode->i_fop = &f2fs_file_operations;
  113. inode->i_mapping->a_ops = &f2fs_dblock_aops;
  114. ino = inode->i_ino;
  115. f2fs_lock_op(sbi);
  116. err = f2fs_add_link(dentry, inode);
  117. if (err)
  118. goto out;
  119. f2fs_unlock_op(sbi);
  120. alloc_nid_done(sbi, ino);
  121. d_instantiate(dentry, inode);
  122. unlock_new_inode(inode);
  123. if (IS_DIRSYNC(dir))
  124. f2fs_sync_fs(sbi->sb, 1);
  125. return 0;
  126. out:
  127. handle_failed_inode(inode);
  128. return err;
  129. }
  130. static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
  131. struct dentry *dentry)
  132. {
  133. struct inode *inode = d_inode(old_dentry);
  134. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  135. int err;
  136. if (f2fs_encrypted_inode(dir) &&
  137. !f2fs_is_child_context_consistent_with_parent(dir, inode))
  138. return -EPERM;
  139. f2fs_balance_fs(sbi);
  140. inode->i_ctime = CURRENT_TIME;
  141. ihold(inode);
  142. set_inode_flag(F2FS_I(inode), FI_INC_LINK);
  143. f2fs_lock_op(sbi);
  144. err = f2fs_add_link(dentry, inode);
  145. if (err)
  146. goto out;
  147. f2fs_unlock_op(sbi);
  148. d_instantiate(dentry, inode);
  149. if (IS_DIRSYNC(dir))
  150. f2fs_sync_fs(sbi->sb, 1);
  151. return 0;
  152. out:
  153. clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
  154. iput(inode);
  155. f2fs_unlock_op(sbi);
  156. return err;
  157. }
  158. struct dentry *f2fs_get_parent(struct dentry *child)
  159. {
  160. struct qstr dotdot = QSTR_INIT("..", 2);
  161. unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot);
  162. if (!ino)
  163. return ERR_PTR(-ENOENT);
  164. return d_obtain_alias(f2fs_iget(d_inode(child)->i_sb, ino));
  165. }
  166. static int __recover_dot_dentries(struct inode *dir, nid_t pino)
  167. {
  168. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  169. struct qstr dot = QSTR_INIT(".", 1);
  170. struct qstr dotdot = QSTR_INIT("..", 2);
  171. struct f2fs_dir_entry *de;
  172. struct page *page;
  173. int err = 0;
  174. f2fs_lock_op(sbi);
  175. de = f2fs_find_entry(dir, &dot, &page);
  176. if (de) {
  177. f2fs_dentry_kunmap(dir, page);
  178. f2fs_put_page(page, 0);
  179. } else {
  180. err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
  181. if (err)
  182. goto out;
  183. }
  184. de = f2fs_find_entry(dir, &dotdot, &page);
  185. if (de) {
  186. f2fs_dentry_kunmap(dir, page);
  187. f2fs_put_page(page, 0);
  188. } else {
  189. err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
  190. }
  191. out:
  192. if (!err) {
  193. clear_inode_flag(F2FS_I(dir), FI_INLINE_DOTS);
  194. mark_inode_dirty(dir);
  195. }
  196. f2fs_unlock_op(sbi);
  197. return err;
  198. }
  199. static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
  200. unsigned int flags)
  201. {
  202. struct inode *inode = NULL;
  203. struct f2fs_dir_entry *de;
  204. struct page *page;
  205. nid_t ino;
  206. int err = 0;
  207. if (dentry->d_name.len > F2FS_NAME_LEN)
  208. return ERR_PTR(-ENAMETOOLONG);
  209. de = f2fs_find_entry(dir, &dentry->d_name, &page);
  210. if (!de)
  211. return d_splice_alias(inode, dentry);
  212. ino = le32_to_cpu(de->ino);
  213. f2fs_dentry_kunmap(dir, page);
  214. f2fs_put_page(page, 0);
  215. inode = f2fs_iget(dir->i_sb, ino);
  216. if (IS_ERR(inode))
  217. return ERR_CAST(inode);
  218. if (f2fs_has_inline_dots(inode)) {
  219. err = __recover_dot_dentries(inode, dir->i_ino);
  220. if (err)
  221. goto err_out;
  222. }
  223. return d_splice_alias(inode, dentry);
  224. err_out:
  225. iget_failed(inode);
  226. return ERR_PTR(err);
  227. }
  228. static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
  229. {
  230. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  231. struct inode *inode = d_inode(dentry);
  232. struct f2fs_dir_entry *de;
  233. struct page *page;
  234. int err = -ENOENT;
  235. trace_f2fs_unlink_enter(dir, dentry);
  236. f2fs_balance_fs(sbi);
  237. de = f2fs_find_entry(dir, &dentry->d_name, &page);
  238. if (!de)
  239. goto fail;
  240. f2fs_lock_op(sbi);
  241. err = acquire_orphan_inode(sbi);
  242. if (err) {
  243. f2fs_unlock_op(sbi);
  244. f2fs_dentry_kunmap(dir, page);
  245. f2fs_put_page(page, 0);
  246. goto fail;
  247. }
  248. f2fs_delete_entry(de, page, dir, inode);
  249. f2fs_unlock_op(sbi);
  250. /* In order to evict this inode, we set it dirty */
  251. mark_inode_dirty(inode);
  252. if (IS_DIRSYNC(dir))
  253. f2fs_sync_fs(sbi->sb, 1);
  254. fail:
  255. trace_f2fs_unlink_exit(inode, err);
  256. return err;
  257. }
  258. static const char *f2fs_follow_link(struct dentry *dentry, void **cookie)
  259. {
  260. const char *link = page_follow_link_light(dentry, cookie);
  261. if (!IS_ERR(link) && !*link) {
  262. /* this is broken symlink case */
  263. page_put_link(NULL, *cookie);
  264. link = ERR_PTR(-ENOENT);
  265. }
  266. return link;
  267. }
  268. static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
  269. const char *symname)
  270. {
  271. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  272. struct inode *inode;
  273. size_t len = strlen(symname);
  274. size_t p_len;
  275. char *p_str;
  276. struct f2fs_str disk_link = FSTR_INIT(NULL, 0);
  277. struct f2fs_encrypted_symlink_data *sd = NULL;
  278. int err;
  279. if (len > dir->i_sb->s_blocksize)
  280. return -ENAMETOOLONG;
  281. f2fs_balance_fs(sbi);
  282. inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
  283. if (IS_ERR(inode))
  284. return PTR_ERR(inode);
  285. if (f2fs_encrypted_inode(inode))
  286. inode->i_op = &f2fs_encrypted_symlink_inode_operations;
  287. else
  288. inode->i_op = &f2fs_symlink_inode_operations;
  289. inode->i_mapping->a_ops = &f2fs_dblock_aops;
  290. f2fs_lock_op(sbi);
  291. err = f2fs_add_link(dentry, inode);
  292. if (err)
  293. goto out;
  294. f2fs_unlock_op(sbi);
  295. alloc_nid_done(sbi, inode->i_ino);
  296. if (f2fs_encrypted_inode(dir)) {
  297. struct qstr istr = QSTR_INIT(symname, len);
  298. err = f2fs_get_encryption_info(inode);
  299. if (err)
  300. goto err_out;
  301. err = f2fs_fname_crypto_alloc_buffer(inode, len, &disk_link);
  302. if (err)
  303. goto err_out;
  304. err = f2fs_fname_usr_to_disk(inode, &istr, &disk_link);
  305. if (err < 0)
  306. goto err_out;
  307. p_len = encrypted_symlink_data_len(disk_link.len) + 1;
  308. if (p_len > dir->i_sb->s_blocksize) {
  309. err = -ENAMETOOLONG;
  310. goto err_out;
  311. }
  312. sd = kzalloc(p_len, GFP_NOFS);
  313. if (!sd) {
  314. err = -ENOMEM;
  315. goto err_out;
  316. }
  317. memcpy(sd->encrypted_path, disk_link.name, disk_link.len);
  318. sd->len = cpu_to_le16(disk_link.len);
  319. p_str = (char *)sd;
  320. } else {
  321. p_len = len + 1;
  322. p_str = (char *)symname;
  323. }
  324. err = page_symlink(inode, p_str, p_len);
  325. err_out:
  326. d_instantiate(dentry, inode);
  327. unlock_new_inode(inode);
  328. /*
  329. * Let's flush symlink data in order to avoid broken symlink as much as
  330. * possible. Nevertheless, fsyncing is the best way, but there is no
  331. * way to get a file descriptor in order to flush that.
  332. *
  333. * Note that, it needs to do dir->fsync to make this recoverable.
  334. * If the symlink path is stored into inline_data, there is no
  335. * performance regression.
  336. */
  337. if (!err)
  338. filemap_write_and_wait_range(inode->i_mapping, 0, p_len - 1);
  339. if (IS_DIRSYNC(dir))
  340. f2fs_sync_fs(sbi->sb, 1);
  341. kfree(sd);
  342. f2fs_fname_crypto_free_buffer(&disk_link);
  343. return err;
  344. out:
  345. handle_failed_inode(inode);
  346. return err;
  347. }
  348. static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  349. {
  350. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  351. struct inode *inode;
  352. int err;
  353. f2fs_balance_fs(sbi);
  354. inode = f2fs_new_inode(dir, S_IFDIR | mode);
  355. if (IS_ERR(inode))
  356. return PTR_ERR(inode);
  357. inode->i_op = &f2fs_dir_inode_operations;
  358. inode->i_fop = &f2fs_dir_operations;
  359. inode->i_mapping->a_ops = &f2fs_dblock_aops;
  360. mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
  361. set_inode_flag(F2FS_I(inode), FI_INC_LINK);
  362. f2fs_lock_op(sbi);
  363. err = f2fs_add_link(dentry, inode);
  364. if (err)
  365. goto out_fail;
  366. f2fs_unlock_op(sbi);
  367. alloc_nid_done(sbi, inode->i_ino);
  368. d_instantiate(dentry, inode);
  369. unlock_new_inode(inode);
  370. if (IS_DIRSYNC(dir))
  371. f2fs_sync_fs(sbi->sb, 1);
  372. return 0;
  373. out_fail:
  374. clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
  375. handle_failed_inode(inode);
  376. return err;
  377. }
  378. static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
  379. {
  380. struct inode *inode = d_inode(dentry);
  381. if (f2fs_empty_dir(inode))
  382. return f2fs_unlink(dir, dentry);
  383. return -ENOTEMPTY;
  384. }
  385. static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
  386. umode_t mode, dev_t rdev)
  387. {
  388. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  389. struct inode *inode;
  390. int err = 0;
  391. if (!new_valid_dev(rdev))
  392. return -EINVAL;
  393. f2fs_balance_fs(sbi);
  394. inode = f2fs_new_inode(dir, mode);
  395. if (IS_ERR(inode))
  396. return PTR_ERR(inode);
  397. init_special_inode(inode, inode->i_mode, rdev);
  398. inode->i_op = &f2fs_special_inode_operations;
  399. f2fs_lock_op(sbi);
  400. err = f2fs_add_link(dentry, inode);
  401. if (err)
  402. goto out;
  403. f2fs_unlock_op(sbi);
  404. alloc_nid_done(sbi, inode->i_ino);
  405. d_instantiate(dentry, inode);
  406. unlock_new_inode(inode);
  407. if (IS_DIRSYNC(dir))
  408. f2fs_sync_fs(sbi->sb, 1);
  409. return 0;
  410. out:
  411. handle_failed_inode(inode);
  412. return err;
  413. }
  414. static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
  415. umode_t mode, struct inode **whiteout)
  416. {
  417. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  418. struct inode *inode;
  419. int err;
  420. if (!whiteout)
  421. f2fs_balance_fs(sbi);
  422. inode = f2fs_new_inode(dir, mode);
  423. if (IS_ERR(inode))
  424. return PTR_ERR(inode);
  425. if (whiteout) {
  426. init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
  427. inode->i_op = &f2fs_special_inode_operations;
  428. } else {
  429. inode->i_op = &f2fs_file_inode_operations;
  430. inode->i_fop = &f2fs_file_operations;
  431. inode->i_mapping->a_ops = &f2fs_dblock_aops;
  432. }
  433. f2fs_lock_op(sbi);
  434. err = acquire_orphan_inode(sbi);
  435. if (err)
  436. goto out;
  437. err = f2fs_do_tmpfile(inode, dir);
  438. if (err)
  439. goto release_out;
  440. /*
  441. * add this non-linked tmpfile to orphan list, in this way we could
  442. * remove all unused data of tmpfile after abnormal power-off.
  443. */
  444. add_orphan_inode(sbi, inode->i_ino);
  445. f2fs_unlock_op(sbi);
  446. alloc_nid_done(sbi, inode->i_ino);
  447. if (whiteout) {
  448. inode_dec_link_count(inode);
  449. *whiteout = inode;
  450. } else {
  451. d_tmpfile(dentry, inode);
  452. }
  453. unlock_new_inode(inode);
  454. return 0;
  455. release_out:
  456. release_orphan_inode(sbi);
  457. out:
  458. handle_failed_inode(inode);
  459. return err;
  460. }
  461. static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  462. {
  463. if (f2fs_encrypted_inode(dir)) {
  464. int err = f2fs_get_encryption_info(dir);
  465. if (err)
  466. return err;
  467. }
  468. return __f2fs_tmpfile(dir, dentry, mode, NULL);
  469. }
  470. static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
  471. {
  472. return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
  473. }
  474. static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
  475. struct inode *new_dir, struct dentry *new_dentry,
  476. unsigned int flags)
  477. {
  478. struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
  479. struct inode *old_inode = d_inode(old_dentry);
  480. struct inode *new_inode = d_inode(new_dentry);
  481. struct inode *whiteout = NULL;
  482. struct page *old_dir_page;
  483. struct page *old_page, *new_page = NULL;
  484. struct f2fs_dir_entry *old_dir_entry = NULL;
  485. struct f2fs_dir_entry *old_entry;
  486. struct f2fs_dir_entry *new_entry;
  487. int err = -ENOENT;
  488. if ((old_dir != new_dir) && f2fs_encrypted_inode(new_dir) &&
  489. !f2fs_is_child_context_consistent_with_parent(new_dir,
  490. old_inode)) {
  491. err = -EPERM;
  492. goto out;
  493. }
  494. f2fs_balance_fs(sbi);
  495. old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
  496. if (!old_entry)
  497. goto out;
  498. if (S_ISDIR(old_inode->i_mode)) {
  499. err = -EIO;
  500. old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
  501. if (!old_dir_entry)
  502. goto out_old;
  503. }
  504. if (flags & RENAME_WHITEOUT) {
  505. err = f2fs_create_whiteout(old_dir, &whiteout);
  506. if (err)
  507. goto out_dir;
  508. }
  509. if (new_inode) {
  510. err = -ENOTEMPTY;
  511. if (old_dir_entry && !f2fs_empty_dir(new_inode))
  512. goto out_whiteout;
  513. err = -ENOENT;
  514. new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
  515. &new_page);
  516. if (!new_entry)
  517. goto out_whiteout;
  518. f2fs_lock_op(sbi);
  519. err = acquire_orphan_inode(sbi);
  520. if (err)
  521. goto put_out_dir;
  522. if (update_dent_inode(old_inode, new_inode,
  523. &new_dentry->d_name)) {
  524. release_orphan_inode(sbi);
  525. goto put_out_dir;
  526. }
  527. f2fs_set_link(new_dir, new_entry, new_page, old_inode);
  528. new_inode->i_ctime = CURRENT_TIME;
  529. down_write(&F2FS_I(new_inode)->i_sem);
  530. if (old_dir_entry)
  531. drop_nlink(new_inode);
  532. drop_nlink(new_inode);
  533. up_write(&F2FS_I(new_inode)->i_sem);
  534. mark_inode_dirty(new_inode);
  535. if (!new_inode->i_nlink)
  536. add_orphan_inode(sbi, new_inode->i_ino);
  537. else
  538. release_orphan_inode(sbi);
  539. update_inode_page(old_inode);
  540. update_inode_page(new_inode);
  541. } else {
  542. f2fs_lock_op(sbi);
  543. err = f2fs_add_link(new_dentry, old_inode);
  544. if (err) {
  545. f2fs_unlock_op(sbi);
  546. goto out_whiteout;
  547. }
  548. if (old_dir_entry) {
  549. inc_nlink(new_dir);
  550. update_inode_page(new_dir);
  551. }
  552. }
  553. down_write(&F2FS_I(old_inode)->i_sem);
  554. file_lost_pino(old_inode);
  555. if (new_inode && file_enc_name(new_inode))
  556. file_set_enc_name(old_inode);
  557. up_write(&F2FS_I(old_inode)->i_sem);
  558. old_inode->i_ctime = CURRENT_TIME;
  559. mark_inode_dirty(old_inode);
  560. f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
  561. if (whiteout) {
  562. whiteout->i_state |= I_LINKABLE;
  563. set_inode_flag(F2FS_I(whiteout), FI_INC_LINK);
  564. err = f2fs_add_link(old_dentry, whiteout);
  565. if (err)
  566. goto put_out_dir;
  567. whiteout->i_state &= ~I_LINKABLE;
  568. iput(whiteout);
  569. }
  570. if (old_dir_entry) {
  571. if (old_dir != new_dir && !whiteout) {
  572. f2fs_set_link(old_inode, old_dir_entry,
  573. old_dir_page, new_dir);
  574. update_inode_page(old_inode);
  575. } else {
  576. f2fs_dentry_kunmap(old_inode, old_dir_page);
  577. f2fs_put_page(old_dir_page, 0);
  578. }
  579. drop_nlink(old_dir);
  580. mark_inode_dirty(old_dir);
  581. update_inode_page(old_dir);
  582. }
  583. f2fs_unlock_op(sbi);
  584. if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
  585. f2fs_sync_fs(sbi->sb, 1);
  586. return 0;
  587. put_out_dir:
  588. f2fs_unlock_op(sbi);
  589. if (new_page) {
  590. f2fs_dentry_kunmap(new_dir, new_page);
  591. f2fs_put_page(new_page, 0);
  592. }
  593. out_whiteout:
  594. if (whiteout)
  595. iput(whiteout);
  596. out_dir:
  597. if (old_dir_entry) {
  598. f2fs_dentry_kunmap(old_inode, old_dir_page);
  599. f2fs_put_page(old_dir_page, 0);
  600. }
  601. out_old:
  602. f2fs_dentry_kunmap(old_dir, old_page);
  603. f2fs_put_page(old_page, 0);
  604. out:
  605. return err;
  606. }
  607. static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
  608. struct inode *new_dir, struct dentry *new_dentry)
  609. {
  610. struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
  611. struct inode *old_inode = d_inode(old_dentry);
  612. struct inode *new_inode = d_inode(new_dentry);
  613. struct page *old_dir_page, *new_dir_page;
  614. struct page *old_page, *new_page;
  615. struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
  616. struct f2fs_dir_entry *old_entry, *new_entry;
  617. int old_nlink = 0, new_nlink = 0;
  618. int err = -ENOENT;
  619. if ((f2fs_encrypted_inode(old_dir) || f2fs_encrypted_inode(new_dir)) &&
  620. (old_dir != new_dir) &&
  621. (!f2fs_is_child_context_consistent_with_parent(new_dir,
  622. old_inode) ||
  623. !f2fs_is_child_context_consistent_with_parent(old_dir,
  624. new_inode)))
  625. return -EPERM;
  626. f2fs_balance_fs(sbi);
  627. old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
  628. if (!old_entry)
  629. goto out;
  630. new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
  631. if (!new_entry)
  632. goto out_old;
  633. /* prepare for updating ".." directory entry info later */
  634. if (old_dir != new_dir) {
  635. if (S_ISDIR(old_inode->i_mode)) {
  636. err = -EIO;
  637. old_dir_entry = f2fs_parent_dir(old_inode,
  638. &old_dir_page);
  639. if (!old_dir_entry)
  640. goto out_new;
  641. }
  642. if (S_ISDIR(new_inode->i_mode)) {
  643. err = -EIO;
  644. new_dir_entry = f2fs_parent_dir(new_inode,
  645. &new_dir_page);
  646. if (!new_dir_entry)
  647. goto out_old_dir;
  648. }
  649. }
  650. /*
  651. * If cross rename between file and directory those are not
  652. * in the same directory, we will inc nlink of file's parent
  653. * later, so we should check upper boundary of its nlink.
  654. */
  655. if ((!old_dir_entry || !new_dir_entry) &&
  656. old_dir_entry != new_dir_entry) {
  657. old_nlink = old_dir_entry ? -1 : 1;
  658. new_nlink = -old_nlink;
  659. err = -EMLINK;
  660. if ((old_nlink > 0 && old_inode->i_nlink >= F2FS_LINK_MAX) ||
  661. (new_nlink > 0 && new_inode->i_nlink >= F2FS_LINK_MAX))
  662. goto out_new_dir;
  663. }
  664. f2fs_lock_op(sbi);
  665. err = update_dent_inode(old_inode, new_inode, &new_dentry->d_name);
  666. if (err)
  667. goto out_unlock;
  668. if (file_enc_name(new_inode))
  669. file_set_enc_name(old_inode);
  670. err = update_dent_inode(new_inode, old_inode, &old_dentry->d_name);
  671. if (err)
  672. goto out_undo;
  673. if (file_enc_name(old_inode))
  674. file_set_enc_name(new_inode);
  675. /* update ".." directory entry info of old dentry */
  676. if (old_dir_entry)
  677. f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
  678. /* update ".." directory entry info of new dentry */
  679. if (new_dir_entry)
  680. f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
  681. /* update directory entry info of old dir inode */
  682. f2fs_set_link(old_dir, old_entry, old_page, new_inode);
  683. down_write(&F2FS_I(old_inode)->i_sem);
  684. file_lost_pino(old_inode);
  685. up_write(&F2FS_I(old_inode)->i_sem);
  686. update_inode_page(old_inode);
  687. old_dir->i_ctime = CURRENT_TIME;
  688. if (old_nlink) {
  689. down_write(&F2FS_I(old_dir)->i_sem);
  690. if (old_nlink < 0)
  691. drop_nlink(old_dir);
  692. else
  693. inc_nlink(old_dir);
  694. up_write(&F2FS_I(old_dir)->i_sem);
  695. }
  696. mark_inode_dirty(old_dir);
  697. update_inode_page(old_dir);
  698. /* update directory entry info of new dir inode */
  699. f2fs_set_link(new_dir, new_entry, new_page, old_inode);
  700. down_write(&F2FS_I(new_inode)->i_sem);
  701. file_lost_pino(new_inode);
  702. up_write(&F2FS_I(new_inode)->i_sem);
  703. update_inode_page(new_inode);
  704. new_dir->i_ctime = CURRENT_TIME;
  705. if (new_nlink) {
  706. down_write(&F2FS_I(new_dir)->i_sem);
  707. if (new_nlink < 0)
  708. drop_nlink(new_dir);
  709. else
  710. inc_nlink(new_dir);
  711. up_write(&F2FS_I(new_dir)->i_sem);
  712. }
  713. mark_inode_dirty(new_dir);
  714. update_inode_page(new_dir);
  715. f2fs_unlock_op(sbi);
  716. if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
  717. f2fs_sync_fs(sbi->sb, 1);
  718. return 0;
  719. out_undo:
  720. /*
  721. * Still we may fail to recover name info of f2fs_inode here
  722. * Drop it, once its name is set as encrypted
  723. */
  724. update_dent_inode(old_inode, old_inode, &old_dentry->d_name);
  725. out_unlock:
  726. f2fs_unlock_op(sbi);
  727. out_new_dir:
  728. if (new_dir_entry) {
  729. f2fs_dentry_kunmap(new_inode, new_dir_page);
  730. f2fs_put_page(new_dir_page, 0);
  731. }
  732. out_old_dir:
  733. if (old_dir_entry) {
  734. f2fs_dentry_kunmap(old_inode, old_dir_page);
  735. f2fs_put_page(old_dir_page, 0);
  736. }
  737. out_new:
  738. f2fs_dentry_kunmap(new_dir, new_page);
  739. f2fs_put_page(new_page, 0);
  740. out_old:
  741. f2fs_dentry_kunmap(old_dir, old_page);
  742. f2fs_put_page(old_page, 0);
  743. out:
  744. return err;
  745. }
  746. static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
  747. struct inode *new_dir, struct dentry *new_dentry,
  748. unsigned int flags)
  749. {
  750. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
  751. return -EINVAL;
  752. if (flags & RENAME_EXCHANGE) {
  753. return f2fs_cross_rename(old_dir, old_dentry,
  754. new_dir, new_dentry);
  755. }
  756. /*
  757. * VFS has already handled the new dentry existence case,
  758. * here, we just deal with "RENAME_NOREPLACE" as regular rename.
  759. */
  760. return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
  761. }
  762. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  763. static const char *f2fs_encrypted_follow_link(struct dentry *dentry, void **cookie)
  764. {
  765. struct page *cpage = NULL;
  766. char *caddr, *paddr = NULL;
  767. struct f2fs_str cstr;
  768. struct f2fs_str pstr = FSTR_INIT(NULL, 0);
  769. struct inode *inode = d_inode(dentry);
  770. struct f2fs_encrypted_symlink_data *sd;
  771. loff_t size = min_t(loff_t, i_size_read(inode), PAGE_SIZE - 1);
  772. u32 max_size = inode->i_sb->s_blocksize;
  773. int res;
  774. res = f2fs_get_encryption_info(inode);
  775. if (res)
  776. return ERR_PTR(res);
  777. cpage = read_mapping_page(inode->i_mapping, 0, NULL);
  778. if (IS_ERR(cpage))
  779. return ERR_CAST(cpage);
  780. caddr = kmap(cpage);
  781. caddr[size] = 0;
  782. /* Symlink is encrypted */
  783. sd = (struct f2fs_encrypted_symlink_data *)caddr;
  784. cstr.name = sd->encrypted_path;
  785. cstr.len = le16_to_cpu(sd->len);
  786. /* this is broken symlink case */
  787. if (cstr.name[0] == 0 && cstr.len == 0) {
  788. res = -ENOENT;
  789. goto errout;
  790. }
  791. if ((cstr.len + sizeof(struct f2fs_encrypted_symlink_data) - 1) >
  792. max_size) {
  793. /* Symlink data on the disk is corrupted */
  794. res = -EIO;
  795. goto errout;
  796. }
  797. res = f2fs_fname_crypto_alloc_buffer(inode, cstr.len, &pstr);
  798. if (res)
  799. goto errout;
  800. res = f2fs_fname_disk_to_usr(inode, NULL, &cstr, &pstr);
  801. if (res < 0)
  802. goto errout;
  803. paddr = pstr.name;
  804. /* Null-terminate the name */
  805. paddr[res] = '\0';
  806. kunmap(cpage);
  807. page_cache_release(cpage);
  808. return *cookie = paddr;
  809. errout:
  810. f2fs_fname_crypto_free_buffer(&pstr);
  811. kunmap(cpage);
  812. page_cache_release(cpage);
  813. return ERR_PTR(res);
  814. }
  815. const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
  816. .readlink = generic_readlink,
  817. .follow_link = f2fs_encrypted_follow_link,
  818. .put_link = kfree_put_link,
  819. .getattr = f2fs_getattr,
  820. .setattr = f2fs_setattr,
  821. .setxattr = generic_setxattr,
  822. .getxattr = generic_getxattr,
  823. .listxattr = f2fs_listxattr,
  824. .removexattr = generic_removexattr,
  825. };
  826. #endif
  827. const struct inode_operations f2fs_dir_inode_operations = {
  828. .create = f2fs_create,
  829. .lookup = f2fs_lookup,
  830. .link = f2fs_link,
  831. .unlink = f2fs_unlink,
  832. .symlink = f2fs_symlink,
  833. .mkdir = f2fs_mkdir,
  834. .rmdir = f2fs_rmdir,
  835. .mknod = f2fs_mknod,
  836. .rename2 = f2fs_rename2,
  837. .tmpfile = f2fs_tmpfile,
  838. .getattr = f2fs_getattr,
  839. .setattr = f2fs_setattr,
  840. .get_acl = f2fs_get_acl,
  841. .set_acl = f2fs_set_acl,
  842. #ifdef CONFIG_F2FS_FS_XATTR
  843. .setxattr = generic_setxattr,
  844. .getxattr = generic_getxattr,
  845. .listxattr = f2fs_listxattr,
  846. .removexattr = generic_removexattr,
  847. #endif
  848. };
  849. const struct inode_operations f2fs_symlink_inode_operations = {
  850. .readlink = generic_readlink,
  851. .follow_link = f2fs_follow_link,
  852. .put_link = page_put_link,
  853. .getattr = f2fs_getattr,
  854. .setattr = f2fs_setattr,
  855. #ifdef CONFIG_F2FS_FS_XATTR
  856. .setxattr = generic_setxattr,
  857. .getxattr = generic_getxattr,
  858. .listxattr = f2fs_listxattr,
  859. .removexattr = generic_removexattr,
  860. #endif
  861. };
  862. const struct inode_operations f2fs_special_inode_operations = {
  863. .getattr = f2fs_getattr,
  864. .setattr = f2fs_setattr,
  865. .get_acl = f2fs_get_acl,
  866. .set_acl = f2fs_set_acl,
  867. #ifdef CONFIG_F2FS_FS_XATTR
  868. .setxattr = generic_setxattr,
  869. .getxattr = generic_getxattr,
  870. .listxattr = f2fs_listxattr,
  871. .removexattr = generic_removexattr,
  872. #endif
  873. };