141-jffs2-add-RENAME_EXCHANGE-support.patch 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. From: Felix Fietkau <nbd@nbd.name>
  2. Subject: jffs2: add RENAME_EXCHANGE support
  3. Signed-off-by: Felix Fietkau <nbd@nbd.name>
  4. ---
  5. --- a/fs/jffs2/dir.c
  6. +++ b/fs/jffs2/dir.c
  7. @@ -777,18 +777,31 @@ static int jffs2_rename (struct inode *o
  8. int ret;
  9. struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb);
  10. struct jffs2_inode_info *victim_f = NULL;
  11. + struct inode *fst_inode = d_inode(old_dentry);
  12. + struct inode *snd_inode = d_inode(new_dentry);
  13. uint8_t type;
  14. uint32_t now;
  15. - if (flags & ~(RENAME_NOREPLACE|RENAME_WHITEOUT))
  16. + if (flags & ~(RENAME_NOREPLACE|RENAME_WHITEOUT|RENAME_EXCHANGE))
  17. return -EINVAL;
  18. + if ((flags & RENAME_EXCHANGE) && (old_dir_i != new_dir_i)) {
  19. + if (S_ISDIR(fst_inode->i_mode) && !S_ISDIR(snd_inode->i_mode)) {
  20. + inc_nlink(new_dir_i);
  21. + drop_nlink(old_dir_i);
  22. + }
  23. + else if (!S_ISDIR(fst_inode->i_mode) && S_ISDIR(snd_inode->i_mode)) {
  24. + drop_nlink(new_dir_i);
  25. + inc_nlink(old_dir_i);
  26. + }
  27. + }
  28. +
  29. /* The VFS will check for us and prevent trying to rename a
  30. * file over a directory and vice versa, but if it's a directory,
  31. * the VFS can't check whether the victim is empty. The filesystem
  32. * needs to do that for itself.
  33. */
  34. - if (d_really_is_positive(new_dentry)) {
  35. + if (d_really_is_positive(new_dentry) && !(flags & RENAME_EXCHANGE)) {
  36. victim_f = JFFS2_INODE_INFO(d_inode(new_dentry));
  37. if (d_is_dir(new_dentry)) {
  38. struct jffs2_full_dirent *fd;
  39. @@ -823,7 +836,7 @@ static int jffs2_rename (struct inode *o
  40. if (ret)
  41. return ret;
  42. - if (victim_f) {
  43. + if (victim_f && !(flags & RENAME_EXCHANGE)) {
  44. /* There was a victim. Kill it off nicely */
  45. if (d_is_dir(new_dentry))
  46. clear_nlink(d_inode(new_dentry));
  47. @@ -849,6 +862,12 @@ static int jffs2_rename (struct inode *o
  48. if (flags & RENAME_WHITEOUT)
  49. /* Replace with whiteout */
  50. ret = jffs2_whiteout(old_dir_i, old_dentry);
  51. + else if (flags & RENAME_EXCHANGE)
  52. + /* Replace the original */
  53. + ret = jffs2_do_link(c, JFFS2_INODE_INFO(old_dir_i),
  54. + d_inode(new_dentry)->i_ino, type,
  55. + old_dentry->d_name.name, old_dentry->d_name.len,
  56. + now);
  57. else
  58. /* Unlink the original */
  59. ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
  60. @@ -880,7 +899,7 @@ static int jffs2_rename (struct inode *o
  61. return ret;
  62. }
  63. - if (d_is_dir(old_dentry))
  64. + if (d_is_dir(old_dentry) && !(flags & RENAME_EXCHANGE))
  65. drop_nlink(old_dir_i);
  66. new_dir_i->i_mtime = new_dir_i->i_ctime = old_dir_i->i_mtime = old_dir_i->i_ctime = ITIME(now);