ubifs: add full overlayfs support
[openwrt/staging/rmilecki.git] / target / linux / generic / patches-4.4 / 052-03-ubifs-Implement-RENAME_EXCHANGE.patch
1 From: Richard Weinberger <richard@nod.at>
2 Date: Tue, 13 Sep 2016 16:18:57 +0200
3 Subject: [PATCH] ubifs: Implement RENAME_EXCHANGE
4
5 Adds RENAME_EXCHANGE to UBIFS, the operation itself
6 is completely disjunct from a regular rename() that's
7 why we dispatch very early in ubifs_reaname().
8
9 RENAME_EXCHANGE used by the renameat2() system call
10 allows the caller to exchange two paths atomically.
11 Both paths have to exist and have to be on the same
12 filesystem.
13
14 Signed-off-by: Richard Weinberger <richard@nod.at>
15 ---
16
17 --- a/fs/ubifs/dir.c
18 +++ b/fs/ubifs/dir.c
19 @@ -1095,11 +1095,6 @@ static int ubifs_rename(struct inode *ol
20 old_dentry, old_inode->i_ino, old_dir->i_ino,
21 new_dentry, new_dir->i_ino, flags);
22
23 - if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT))
24 - return -EINVAL;
25 -
26 - ubifs_assert(mutex_is_locked(&old_dir->i_mutex));
27 - ubifs_assert(mutex_is_locked(&new_dir->i_mutex));
28 if (unlink)
29 ubifs_assert(mutex_is_locked(&new_inode->i_mutex));
30
31 @@ -1284,6 +1279,64 @@ out_cancel:
32 return err;
33 }
34
35 +static int ubifs_xrename(struct inode *old_dir, struct dentry *old_dentry,
36 + struct inode *new_dir, struct dentry *new_dentry)
37 +{
38 + struct ubifs_info *c = old_dir->i_sb->s_fs_info;
39 + struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
40 + .dirtied_ino = 2 };
41 + int sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
42 + struct inode *fst_inode = d_inode(old_dentry);
43 + struct inode *snd_inode = d_inode(new_dentry);
44 + struct timespec time;
45 + int err;
46 +
47 + ubifs_assert(fst_inode && snd_inode);
48 +
49 + lock_4_inodes(old_dir, new_dir, NULL, NULL);
50 +
51 + time = ubifs_current_time(old_dir);
52 + fst_inode->i_ctime = time;
53 + snd_inode->i_ctime = time;
54 + old_dir->i_mtime = old_dir->i_ctime = time;
55 + new_dir->i_mtime = new_dir->i_ctime = time;
56 +
57 + if (old_dir != new_dir) {
58 + if (S_ISDIR(fst_inode->i_mode) && !S_ISDIR(snd_inode->i_mode)) {
59 + inc_nlink(new_dir);
60 + drop_nlink(old_dir);
61 + }
62 + else if (!S_ISDIR(fst_inode->i_mode) && S_ISDIR(snd_inode->i_mode)) {
63 + drop_nlink(new_dir);
64 + inc_nlink(old_dir);
65 + }
66 + }
67 +
68 + err = ubifs_jnl_xrename(c, old_dir, old_dentry, new_dir, new_dentry,
69 + sync);
70 +
71 + unlock_4_inodes(old_dir, new_dir, NULL, NULL);
72 + ubifs_release_budget(c, &req);
73 +
74 + return err;
75 +}
76 +
77 +static int ubifs_rename2(struct inode *old_dir, struct dentry *old_dentry,
78 + struct inode *new_dir, struct dentry *new_dentry,
79 + unsigned int flags)
80 +{
81 + if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT | RENAME_EXCHANGE))
82 + return -EINVAL;
83 +
84 + ubifs_assert(mutex_is_locked(&old_dir->i_mutex));
85 + ubifs_assert(mutex_is_locked(&new_dir->i_mutex));
86 +
87 + if (flags & RENAME_EXCHANGE)
88 + return ubifs_xrename(old_dir, old_dentry, new_dir, new_dentry);
89 +
90 + return ubifs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
91 +}
92 +
93 int ubifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
94 struct kstat *stat)
95 {
96 @@ -1332,7 +1385,7 @@ const struct inode_operations ubifs_dir_
97 .mkdir = ubifs_mkdir,
98 .rmdir = ubifs_rmdir,
99 .mknod = ubifs_mknod,
100 - .rename2 = ubifs_rename,
101 + .rename2 = ubifs_rename2,
102 .setattr = ubifs_setattr,
103 .getattr = ubifs_getattr,
104 .setxattr = ubifs_setxattr,
105 --- a/fs/ubifs/journal.c
106 +++ b/fs/ubifs/journal.c
107 @@ -908,6 +908,147 @@ int ubifs_jnl_delete_inode(struct ubifs_
108 }
109
110 /**
111 + * ubifs_jnl_xrename - cross rename two directory entries.
112 + * @c: UBIFS file-system description object
113 + * @fst_dir: parent inode of 1st directory entry to exchange
114 + * @fst_dentry: 1st directory entry to exchange
115 + * @snd_dir: parent inode of 2nd directory entry to exchange
116 + * @snd_dentry: 2nd directory entry to exchange
117 + * @sync: non-zero if the write-buffer has to be synchronized
118 + *
119 + * This function implements the cross rename operation which may involve
120 + * writing 2 inodes and 2 directory entries. It marks the written inodes as clean
121 + * and returns zero on success. In case of failure, a negative error code is
122 + * returned.
123 + */
124 +int ubifs_jnl_xrename(struct ubifs_info *c, const struct inode *fst_dir,
125 + const struct dentry *fst_dentry,
126 + const struct inode *snd_dir,
127 + const struct dentry *snd_dentry, int sync)
128 +{
129 + union ubifs_key key;
130 + struct ubifs_dent_node *dent1, *dent2;
131 + int err, dlen1, dlen2, lnum, offs, len, plen = UBIFS_INO_NODE_SZ;
132 + int aligned_dlen1, aligned_dlen2;
133 + int twoparents = (fst_dir != snd_dir);
134 + const struct inode *fst_inode = d_inode(fst_dentry);
135 + const struct inode *snd_inode = d_inode(snd_dentry);
136 + void *p;
137 +
138 + dbg_jnl("dent '%pd' in dir ino %lu between dent '%pd' in dir ino %lu",
139 + fst_dentry, fst_dir->i_ino, snd_dentry, snd_dir->i_ino);
140 +
141 + ubifs_assert(ubifs_inode(fst_dir)->data_len == 0);
142 + ubifs_assert(ubifs_inode(snd_dir)->data_len == 0);
143 + ubifs_assert(mutex_is_locked(&ubifs_inode(fst_dir)->ui_mutex));
144 + ubifs_assert(mutex_is_locked(&ubifs_inode(snd_dir)->ui_mutex));
145 +
146 + dlen1 = UBIFS_DENT_NODE_SZ + snd_dentry->d_name.len + 1;
147 + dlen2 = UBIFS_DENT_NODE_SZ + fst_dentry->d_name.len + 1;
148 + aligned_dlen1 = ALIGN(dlen1, 8);
149 + aligned_dlen2 = ALIGN(dlen2, 8);
150 +
151 + len = aligned_dlen1 + aligned_dlen2 + ALIGN(plen, 8);
152 + if (twoparents)
153 + len += plen;
154 +
155 + dent1 = kmalloc(len, GFP_NOFS);
156 + if (!dent1)
157 + return -ENOMEM;
158 +
159 + /* Make reservation before allocating sequence numbers */
160 + err = make_reservation(c, BASEHD, len);
161 + if (err)
162 + goto out_free;
163 +
164 + /* Make new dent for 1st entry */
165 + dent1->ch.node_type = UBIFS_DENT_NODE;
166 + dent_key_init_flash(c, &dent1->key, snd_dir->i_ino, &snd_dentry->d_name);
167 + dent1->inum = cpu_to_le64(fst_inode->i_ino);
168 + dent1->type = get_dent_type(fst_inode->i_mode);
169 + dent1->nlen = cpu_to_le16(snd_dentry->d_name.len);
170 + memcpy(dent1->name, snd_dentry->d_name.name, snd_dentry->d_name.len);
171 + dent1->name[snd_dentry->d_name.len] = '\0';
172 + zero_dent_node_unused(dent1);
173 + ubifs_prep_grp_node(c, dent1, dlen1, 0);
174 +
175 + /* Make new dent for 2nd entry */
176 + dent2 = (void *)dent1 + aligned_dlen1;
177 + dent2->ch.node_type = UBIFS_DENT_NODE;
178 + dent_key_init_flash(c, &dent2->key, fst_dir->i_ino, &fst_dentry->d_name);
179 + dent2->inum = cpu_to_le64(snd_inode->i_ino);
180 + dent2->type = get_dent_type(snd_inode->i_mode);
181 + dent2->nlen = cpu_to_le16(fst_dentry->d_name.len);
182 + memcpy(dent2->name, fst_dentry->d_name.name, fst_dentry->d_name.len);
183 + dent2->name[fst_dentry->d_name.len] = '\0';
184 + zero_dent_node_unused(dent2);
185 + ubifs_prep_grp_node(c, dent2, dlen2, 0);
186 +
187 + p = (void *)dent2 + aligned_dlen2;
188 + if (!twoparents)
189 + pack_inode(c, p, fst_dir, 1);
190 + else {
191 + pack_inode(c, p, fst_dir, 0);
192 + p += ALIGN(plen, 8);
193 + pack_inode(c, p, snd_dir, 1);
194 + }
195 +
196 + err = write_head(c, BASEHD, dent1, len, &lnum, &offs, sync);
197 + if (err)
198 + goto out_release;
199 + if (!sync) {
200 + struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
201 +
202 + ubifs_wbuf_add_ino_nolock(wbuf, fst_dir->i_ino);
203 + ubifs_wbuf_add_ino_nolock(wbuf, snd_dir->i_ino);
204 + }
205 + release_head(c, BASEHD);
206 +
207 + dent_key_init(c, &key, snd_dir->i_ino, &snd_dentry->d_name);
208 + err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, &snd_dentry->d_name);
209 + if (err)
210 + goto out_ro;
211 +
212 + offs += aligned_dlen1;
213 + dent_key_init(c, &key, fst_dir->i_ino, &fst_dentry->d_name);
214 + err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen2, &fst_dentry->d_name);
215 + if (err)
216 + goto out_ro;
217 +
218 + offs += aligned_dlen2;
219 +
220 + ino_key_init(c, &key, fst_dir->i_ino);
221 + err = ubifs_tnc_add(c, &key, lnum, offs, plen);
222 + if (err)
223 + goto out_ro;
224 +
225 + if (twoparents) {
226 + offs += ALIGN(plen, 8);
227 + ino_key_init(c, &key, snd_dir->i_ino);
228 + err = ubifs_tnc_add(c, &key, lnum, offs, plen);
229 + if (err)
230 + goto out_ro;
231 + }
232 +
233 + finish_reservation(c);
234 +
235 + mark_inode_clean(c, ubifs_inode(fst_dir));
236 + if (twoparents)
237 + mark_inode_clean(c, ubifs_inode(snd_dir));
238 + kfree(dent1);
239 + return 0;
240 +
241 +out_release:
242 + release_head(c, BASEHD);
243 +out_ro:
244 + ubifs_ro_mode(c, err);
245 + finish_reservation(c);
246 +out_free:
247 + kfree(dent1);
248 + return err;
249 +}
250 +
251 +/**
252 * ubifs_jnl_rename - rename a directory entry.
253 * @c: UBIFS file-system description object
254 * @old_dir: parent inode of directory entry to rename
255 --- a/fs/ubifs/ubifs.h
256 +++ b/fs/ubifs/ubifs.h
257 @@ -1544,6 +1544,10 @@ int ubifs_jnl_write_data(struct ubifs_in
258 const union ubifs_key *key, const void *buf, int len);
259 int ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode);
260 int ubifs_jnl_delete_inode(struct ubifs_info *c, const struct inode *inode);
261 +int ubifs_jnl_xrename(struct ubifs_info *c, const struct inode *fst_dir,
262 + const struct dentry *fst_dentry,
263 + const struct inode *snd_dir,
264 + const struct dentry *snd_dentry, int sync);
265 int ubifs_jnl_rename(struct ubifs_info *c, const struct inode *old_dir,
266 const struct dentry *old_dentry,
267 const struct inode *new_dir,