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