]>
Commit | Line | Data |
---|---|---|
c088e31d SF |
1 | /* |
2 | * Copyright (C) 2005-2017 Junjiro R. Okajima | |
3 | * | |
4 | * This program, aufs is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
18 | /* | |
19 | * inode operation (rename entry) | |
20 | * todo: this is crazy monster | |
21 | */ | |
22 | ||
23 | #include "aufs.h" | |
24 | ||
25 | enum { AuSRC, AuDST, AuSrcDst }; | |
26 | enum { AuPARENT, AuCHILD, AuParentChild }; | |
27 | ||
28 | #define AuRen_ISDIR_SRC 1 | |
29 | #define AuRen_ISDIR_DST (1 << 1) | |
30 | #define AuRen_ISSAMEDIR (1 << 2) | |
31 | #define AuRen_WHSRC (1 << 3) | |
32 | #define AuRen_WHDST (1 << 4) | |
33 | #define AuRen_MNT_WRITE (1 << 5) | |
34 | #define AuRen_DT_DSTDIR (1 << 6) | |
35 | #define AuRen_DIROPQ_SRC (1 << 7) | |
36 | #define AuRen_DIROPQ_DST (1 << 8) | |
37 | #define AuRen_DIRREN (1 << 9) | |
38 | #define AuRen_DROPPED_SRC (1 << 10) | |
39 | #define AuRen_DROPPED_DST (1 << 11) | |
40 | #define au_ftest_ren(flags, name) ((flags) & AuRen_##name) | |
41 | #define au_fset_ren(flags, name) \ | |
42 | do { (flags) |= AuRen_##name; } while (0) | |
43 | #define au_fclr_ren(flags, name) \ | |
44 | do { (flags) &= ~AuRen_##name; } while (0) | |
45 | ||
46 | #ifndef CONFIG_AUFS_DIRREN | |
47 | #undef AuRen_DIRREN | |
48 | #define AuRen_DIRREN 0 | |
49 | #endif | |
50 | ||
51 | struct au_ren_args { | |
52 | struct { | |
53 | struct dentry *dentry, *h_dentry, *parent, *h_parent, | |
54 | *wh_dentry; | |
55 | struct inode *dir, *inode; | |
56 | struct au_hinode *hdir, *hinode; | |
57 | struct au_dtime dt[AuParentChild]; | |
58 | aufs_bindex_t btop, bdiropq; | |
59 | } sd[AuSrcDst]; | |
60 | ||
61 | #define src_dentry sd[AuSRC].dentry | |
62 | #define src_dir sd[AuSRC].dir | |
63 | #define src_inode sd[AuSRC].inode | |
64 | #define src_h_dentry sd[AuSRC].h_dentry | |
65 | #define src_parent sd[AuSRC].parent | |
66 | #define src_h_parent sd[AuSRC].h_parent | |
67 | #define src_wh_dentry sd[AuSRC].wh_dentry | |
68 | #define src_hdir sd[AuSRC].hdir | |
69 | #define src_hinode sd[AuSRC].hinode | |
70 | #define src_h_dir sd[AuSRC].hdir->hi_inode | |
71 | #define src_dt sd[AuSRC].dt | |
72 | #define src_btop sd[AuSRC].btop | |
73 | #define src_bdiropq sd[AuSRC].bdiropq | |
74 | ||
75 | #define dst_dentry sd[AuDST].dentry | |
76 | #define dst_dir sd[AuDST].dir | |
77 | #define dst_inode sd[AuDST].inode | |
78 | #define dst_h_dentry sd[AuDST].h_dentry | |
79 | #define dst_parent sd[AuDST].parent | |
80 | #define dst_h_parent sd[AuDST].h_parent | |
81 | #define dst_wh_dentry sd[AuDST].wh_dentry | |
82 | #define dst_hdir sd[AuDST].hdir | |
83 | #define dst_hinode sd[AuDST].hinode | |
84 | #define dst_h_dir sd[AuDST].hdir->hi_inode | |
85 | #define dst_dt sd[AuDST].dt | |
86 | #define dst_btop sd[AuDST].btop | |
87 | #define dst_bdiropq sd[AuDST].bdiropq | |
88 | ||
89 | struct dentry *h_trap; | |
90 | struct au_branch *br; | |
91 | struct path h_path; | |
92 | struct au_nhash whlist; | |
93 | aufs_bindex_t btgt, src_bwh; | |
94 | ||
95 | struct { | |
96 | unsigned short auren_flags; | |
97 | unsigned char flags; /* syscall parameter */ | |
98 | unsigned char exchange; | |
99 | } __packed; | |
100 | ||
101 | struct au_whtmp_rmdir *thargs; | |
102 | struct dentry *h_dst; | |
103 | struct au_hinode *h_root; | |
104 | }; | |
105 | ||
106 | /* ---------------------------------------------------------------------- */ | |
107 | ||
108 | /* | |
109 | * functions for reverting. | |
110 | * when an error happened in a single rename systemcall, we should revert | |
111 | * everything as if nothing happened. | |
112 | * we don't need to revert the copied-up/down the parent dir since they are | |
113 | * harmless. | |
114 | */ | |
115 | ||
116 | #define RevertFailure(fmt, ...) do { \ | |
117 | AuIOErr("revert failure: " fmt " (%d, %d)\n", \ | |
118 | ##__VA_ARGS__, err, rerr); \ | |
119 | err = -EIO; \ | |
120 | } while (0) | |
121 | ||
122 | static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx) | |
123 | { | |
124 | int rerr; | |
125 | struct dentry *d; | |
126 | #define src_or_dst(member) a->sd[idx].member | |
127 | ||
128 | d = src_or_dst(dentry); /* {src,dst}_dentry */ | |
129 | au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD); | |
130 | rerr = au_diropq_remove(d, a->btgt); | |
131 | au_hn_inode_unlock(src_or_dst(hinode)); | |
132 | au_set_dbdiropq(d, src_or_dst(bdiropq)); | |
133 | if (rerr) | |
134 | RevertFailure("remove diropq %pd", d); | |
135 | ||
136 | #undef src_or_dst_ | |
137 | } | |
138 | ||
139 | static void au_ren_rev_diropq(int err, struct au_ren_args *a) | |
140 | { | |
141 | if (au_ftest_ren(a->auren_flags, DIROPQ_SRC)) | |
142 | au_ren_do_rev_diropq(err, a, AuSRC); | |
143 | if (au_ftest_ren(a->auren_flags, DIROPQ_DST)) | |
144 | au_ren_do_rev_diropq(err, a, AuDST); | |
145 | } | |
146 | ||
147 | static void au_ren_rev_rename(int err, struct au_ren_args *a) | |
148 | { | |
149 | int rerr; | |
150 | struct inode *delegated; | |
151 | ||
152 | a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name, | |
153 | a->src_h_parent); | |
154 | rerr = PTR_ERR(a->h_path.dentry); | |
155 | if (IS_ERR(a->h_path.dentry)) { | |
156 | RevertFailure("lkup one %pd", a->src_dentry); | |
157 | return; | |
158 | } | |
159 | ||
160 | delegated = NULL; | |
161 | rerr = vfsub_rename(a->dst_h_dir, | |
162 | au_h_dptr(a->src_dentry, a->btgt), | |
163 | a->src_h_dir, &a->h_path, &delegated, a->flags); | |
164 | if (unlikely(rerr == -EWOULDBLOCK)) { | |
165 | pr_warn("cannot retry for NFSv4 delegation" | |
166 | " for an internal rename\n"); | |
167 | iput(delegated); | |
168 | } | |
169 | d_drop(a->h_path.dentry); | |
170 | dput(a->h_path.dentry); | |
171 | /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */ | |
172 | if (rerr) | |
173 | RevertFailure("rename %pd", a->src_dentry); | |
174 | } | |
175 | ||
176 | static void au_ren_rev_whtmp(int err, struct au_ren_args *a) | |
177 | { | |
178 | int rerr; | |
179 | struct inode *delegated; | |
180 | ||
181 | a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name, | |
182 | a->dst_h_parent); | |
183 | rerr = PTR_ERR(a->h_path.dentry); | |
184 | if (IS_ERR(a->h_path.dentry)) { | |
185 | RevertFailure("lkup one %pd", a->dst_dentry); | |
186 | return; | |
187 | } | |
188 | if (d_is_positive(a->h_path.dentry)) { | |
189 | d_drop(a->h_path.dentry); | |
190 | dput(a->h_path.dentry); | |
191 | return; | |
192 | } | |
193 | ||
194 | delegated = NULL; | |
195 | rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path, | |
196 | &delegated, a->flags); | |
197 | if (unlikely(rerr == -EWOULDBLOCK)) { | |
198 | pr_warn("cannot retry for NFSv4 delegation" | |
199 | " for an internal rename\n"); | |
200 | iput(delegated); | |
201 | } | |
202 | d_drop(a->h_path.dentry); | |
203 | dput(a->h_path.dentry); | |
204 | if (!rerr) | |
205 | au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst)); | |
206 | else | |
207 | RevertFailure("rename %pd", a->h_dst); | |
208 | } | |
209 | ||
210 | static void au_ren_rev_whsrc(int err, struct au_ren_args *a) | |
211 | { | |
212 | int rerr; | |
213 | ||
214 | a->h_path.dentry = a->src_wh_dentry; | |
215 | rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry); | |
216 | au_set_dbwh(a->src_dentry, a->src_bwh); | |
217 | if (rerr) | |
218 | RevertFailure("unlink %pd", a->src_wh_dentry); | |
219 | } | |
220 | #undef RevertFailure | |
221 | ||
222 | /* ---------------------------------------------------------------------- */ | |
223 | ||
224 | /* | |
225 | * when we have to copyup the renaming entry, do it with the rename-target name | |
226 | * in order to minimize the cost (the later actual rename is unnecessary). | |
227 | * otherwise rename it on the target branch. | |
228 | */ | |
229 | static int au_ren_or_cpup(struct au_ren_args *a) | |
230 | { | |
231 | int err; | |
232 | struct dentry *d; | |
233 | struct inode *delegated; | |
234 | ||
235 | d = a->src_dentry; | |
236 | if (au_dbtop(d) == a->btgt) { | |
237 | a->h_path.dentry = a->dst_h_dentry; | |
238 | AuDebugOn(au_dbtop(d) != a->btgt); | |
239 | delegated = NULL; | |
240 | err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt), | |
241 | a->dst_h_dir, &a->h_path, &delegated, | |
242 | a->flags); | |
243 | if (unlikely(err == -EWOULDBLOCK)) { | |
244 | pr_warn("cannot retry for NFSv4 delegation" | |
245 | " for an internal rename\n"); | |
246 | iput(delegated); | |
247 | } | |
248 | } else | |
249 | BUG(); | |
250 | ||
251 | if (!err && a->h_dst) | |
252 | /* it will be set to dinfo later */ | |
253 | dget(a->h_dst); | |
254 | ||
255 | return err; | |
256 | } | |
257 | ||
258 | /* cf. aufs_rmdir() */ | |
259 | static int au_ren_del_whtmp(struct au_ren_args *a) | |
260 | { | |
261 | int err; | |
262 | struct inode *dir; | |
263 | ||
264 | dir = a->dst_dir; | |
265 | SiMustAnyLock(dir->i_sb); | |
266 | if (!au_nhash_test_longer_wh(&a->whlist, a->btgt, | |
267 | au_sbi(dir->i_sb)->si_dirwh) | |
268 | || au_test_fs_remote(a->h_dst->d_sb)) { | |
269 | err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist); | |
270 | if (unlikely(err)) | |
271 | pr_warn("failed removing whtmp dir %pd (%d), " | |
272 | "ignored.\n", a->h_dst, err); | |
273 | } else { | |
274 | au_nhash_wh_free(&a->thargs->whlist); | |
275 | a->thargs->whlist = a->whlist; | |
276 | a->whlist.nh_num = 0; | |
277 | au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs); | |
278 | dput(a->h_dst); | |
279 | a->thargs = NULL; | |
280 | } | |
281 | ||
282 | return 0; | |
283 | } | |
284 | ||
285 | /* make it 'opaque' dir. */ | |
286 | static int au_ren_do_diropq(struct au_ren_args *a, int idx) | |
287 | { | |
288 | int err; | |
289 | struct dentry *d, *diropq; | |
290 | #define src_or_dst(member) a->sd[idx].member | |
291 | ||
292 | err = 0; | |
293 | d = src_or_dst(dentry); /* {src,dst}_dentry */ | |
294 | src_or_dst(bdiropq) = au_dbdiropq(d); | |
295 | src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt); | |
296 | au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD); | |
297 | diropq = au_diropq_create(d, a->btgt); | |
298 | au_hn_inode_unlock(src_or_dst(hinode)); | |
299 | if (IS_ERR(diropq)) | |
300 | err = PTR_ERR(diropq); | |
301 | else | |
302 | dput(diropq); | |
303 | ||
304 | #undef src_or_dst_ | |
305 | return err; | |
306 | } | |
307 | ||
308 | static int au_ren_diropq(struct au_ren_args *a) | |
309 | { | |
310 | int err; | |
311 | unsigned char always; | |
312 | struct dentry *d; | |
313 | ||
314 | err = 0; | |
315 | d = a->dst_dentry; /* already renamed on the branch */ | |
316 | always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ); | |
317 | if (au_ftest_ren(a->auren_flags, ISDIR_SRC) | |
318 | && !au_ftest_ren(a->auren_flags, DIRREN) | |
319 | && a->btgt != au_dbdiropq(a->src_dentry) | |
320 | && (a->dst_wh_dentry | |
321 | || a->btgt <= au_dbdiropq(d) | |
322 | /* hide the lower to keep xino */ | |
323 | /* the lowers may not be a dir, but we hide them anyway */ | |
324 | || a->btgt < au_dbbot(d) | |
325 | || always)) { | |
326 | AuDbg("here\n"); | |
327 | err = au_ren_do_diropq(a, AuSRC); | |
328 | if (unlikely(err)) | |
329 | goto out; | |
330 | au_fset_ren(a->auren_flags, DIROPQ_SRC); | |
331 | } | |
332 | if (!a->exchange) | |
333 | goto out; /* success */ | |
334 | ||
335 | d = a->src_dentry; /* already renamed on the branch */ | |
336 | if (au_ftest_ren(a->auren_flags, ISDIR_DST) | |
337 | && a->btgt != au_dbdiropq(a->dst_dentry) | |
338 | && (a->btgt < au_dbdiropq(d) | |
339 | || a->btgt < au_dbbot(d) | |
340 | || always)) { | |
341 | AuDbgDentry(a->src_dentry); | |
342 | AuDbgDentry(a->dst_dentry); | |
343 | err = au_ren_do_diropq(a, AuDST); | |
344 | if (unlikely(err)) | |
345 | goto out_rev_src; | |
346 | au_fset_ren(a->auren_flags, DIROPQ_DST); | |
347 | } | |
348 | goto out; /* success */ | |
349 | ||
350 | out_rev_src: | |
351 | AuDbg("err %d, reverting src\n", err); | |
352 | au_ren_rev_diropq(err, a); | |
353 | out: | |
354 | return err; | |
355 | } | |
356 | ||
357 | static int do_rename(struct au_ren_args *a) | |
358 | { | |
359 | int err; | |
360 | struct dentry *d, *h_d; | |
361 | ||
362 | if (!a->exchange) { | |
363 | /* prepare workqueue args for asynchronous rmdir */ | |
364 | h_d = a->dst_h_dentry; | |
365 | if (au_ftest_ren(a->auren_flags, ISDIR_DST) | |
366 | /* && !au_ftest_ren(a->auren_flags, DIRREN) */ | |
367 | && d_is_positive(h_d)) { | |
368 | err = -ENOMEM; | |
369 | a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb, | |
370 | GFP_NOFS); | |
371 | if (unlikely(!a->thargs)) | |
372 | goto out; | |
373 | a->h_dst = dget(h_d); | |
374 | } | |
375 | ||
376 | /* create whiteout for src_dentry */ | |
377 | if (au_ftest_ren(a->auren_flags, WHSRC)) { | |
378 | a->src_bwh = au_dbwh(a->src_dentry); | |
379 | AuDebugOn(a->src_bwh >= 0); | |
380 | a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt, | |
381 | a->src_h_parent); | |
382 | err = PTR_ERR(a->src_wh_dentry); | |
383 | if (IS_ERR(a->src_wh_dentry)) | |
384 | goto out_thargs; | |
385 | } | |
386 | ||
387 | /* lookup whiteout for dentry */ | |
388 | if (au_ftest_ren(a->auren_flags, WHDST)) { | |
389 | h_d = au_wh_lkup(a->dst_h_parent, | |
390 | &a->dst_dentry->d_name, a->br); | |
391 | err = PTR_ERR(h_d); | |
392 | if (IS_ERR(h_d)) | |
393 | goto out_whsrc; | |
394 | if (d_is_negative(h_d)) | |
395 | dput(h_d); | |
396 | else | |
397 | a->dst_wh_dentry = h_d; | |
398 | } | |
399 | ||
400 | /* rename dentry to tmpwh */ | |
401 | if (a->thargs) { | |
402 | err = au_whtmp_ren(a->dst_h_dentry, a->br); | |
403 | if (unlikely(err)) | |
404 | goto out_whdst; | |
405 | ||
406 | d = a->dst_dentry; | |
407 | au_set_h_dptr(d, a->btgt, NULL); | |
408 | err = au_lkup_neg(d, a->btgt, /*wh*/0); | |
409 | if (unlikely(err)) | |
410 | goto out_whtmp; | |
411 | a->dst_h_dentry = au_h_dptr(d, a->btgt); | |
412 | } | |
413 | } | |
414 | ||
415 | BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt); | |
416 | #if 0 | |
417 | BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN) | |
418 | && d_is_positive(a->dst_h_dentry) | |
419 | && a->src_btop != a->btgt); | |
420 | #endif | |
421 | ||
422 | /* rename by vfs_rename or cpup */ | |
423 | err = au_ren_or_cpup(a); | |
424 | if (unlikely(err)) | |
425 | /* leave the copied-up one */ | |
426 | goto out_whtmp; | |
427 | ||
428 | /* make dir opaque */ | |
429 | err = au_ren_diropq(a); | |
430 | if (unlikely(err)) | |
431 | goto out_rename; | |
432 | ||
433 | /* update target timestamps */ | |
434 | if (a->exchange) { | |
435 | AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt); | |
436 | a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt); | |
437 | vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/ | |
438 | a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime; | |
439 | } | |
440 | AuDebugOn(au_dbtop(a->src_dentry) != a->btgt); | |
441 | a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt); | |
442 | vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/ | |
443 | a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime; | |
444 | ||
445 | if (!a->exchange) { | |
446 | /* remove whiteout for dentry */ | |
447 | if (a->dst_wh_dentry) { | |
448 | a->h_path.dentry = a->dst_wh_dentry; | |
449 | err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path, | |
450 | a->dst_dentry); | |
451 | if (unlikely(err)) | |
452 | goto out_diropq; | |
453 | } | |
454 | ||
455 | /* remove whtmp */ | |
456 | if (a->thargs) | |
457 | au_ren_del_whtmp(a); /* ignore this error */ | |
458 | ||
459 | au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0); | |
460 | } | |
461 | err = 0; | |
462 | goto out_success; | |
463 | ||
464 | out_diropq: | |
465 | au_ren_rev_diropq(err, a); | |
466 | out_rename: | |
467 | au_ren_rev_rename(err, a); | |
468 | dput(a->h_dst); | |
469 | out_whtmp: | |
470 | if (a->thargs) | |
471 | au_ren_rev_whtmp(err, a); | |
472 | out_whdst: | |
473 | dput(a->dst_wh_dentry); | |
474 | a->dst_wh_dentry = NULL; | |
475 | out_whsrc: | |
476 | if (a->src_wh_dentry) | |
477 | au_ren_rev_whsrc(err, a); | |
478 | out_success: | |
479 | dput(a->src_wh_dentry); | |
480 | dput(a->dst_wh_dentry); | |
481 | out_thargs: | |
482 | if (a->thargs) { | |
483 | dput(a->h_dst); | |
484 | au_whtmp_rmdir_free(a->thargs); | |
485 | a->thargs = NULL; | |
486 | } | |
487 | out: | |
488 | return err; | |
489 | } | |
490 | ||
491 | /* ---------------------------------------------------------------------- */ | |
492 | ||
493 | /* | |
494 | * test if @dentry dir can be rename destination or not. | |
495 | * success means, it is a logically empty dir. | |
496 | */ | |
497 | static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist) | |
498 | { | |
499 | return au_test_empty(dentry, whlist); | |
500 | } | |
501 | ||
502 | /* | |
503 | * test if @a->src_dentry dir can be rename source or not. | |
504 | * if it can, return 0. | |
505 | * success means, | |
506 | * - it is a logically empty dir. | |
507 | * - or, it exists on writable branch and has no children including whiteouts | |
508 | * on the lower branch unless DIRREN is on. | |
509 | */ | |
510 | static int may_rename_srcdir(struct au_ren_args *a) | |
511 | { | |
512 | int err; | |
513 | unsigned int rdhash; | |
514 | aufs_bindex_t btop, btgt; | |
515 | struct dentry *dentry; | |
516 | struct super_block *sb; | |
517 | struct au_sbinfo *sbinfo; | |
518 | ||
519 | dentry = a->src_dentry; | |
520 | sb = dentry->d_sb; | |
521 | sbinfo = au_sbi(sb); | |
522 | if (au_opt_test(sbinfo->si_mntflags, DIRREN)) | |
523 | au_fset_ren(a->auren_flags, DIRREN); | |
524 | ||
525 | btgt = a->btgt; | |
526 | btop = au_dbtop(dentry); | |
527 | if (btop != btgt) { | |
528 | struct au_nhash whlist; | |
529 | ||
530 | SiMustAnyLock(sb); | |
531 | rdhash = sbinfo->si_rdhash; | |
532 | if (!rdhash) | |
533 | rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, | |
534 | dentry)); | |
535 | err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS); | |
536 | if (unlikely(err)) | |
537 | goto out; | |
538 | err = au_test_empty(dentry, &whlist); | |
539 | au_nhash_wh_free(&whlist); | |
540 | goto out; | |
541 | } | |
542 | ||
543 | if (btop == au_dbtaildir(dentry)) | |
544 | return 0; /* success */ | |
545 | ||
546 | err = au_test_empty_lower(dentry); | |
547 | ||
548 | out: | |
549 | if (err == -ENOTEMPTY) { | |
550 | if (au_ftest_ren(a->auren_flags, DIRREN)) { | |
551 | err = 0; | |
552 | } else { | |
553 | AuWarn1("renaming dir who has child(ren) on multiple " | |
554 | "branches, is not supported\n"); | |
555 | err = -EXDEV; | |
556 | } | |
557 | } | |
558 | return err; | |
559 | } | |
560 | ||
561 | /* side effect: sets whlist and h_dentry */ | |
562 | static int au_ren_may_dir(struct au_ren_args *a) | |
563 | { | |
564 | int err; | |
565 | unsigned int rdhash; | |
566 | struct dentry *d; | |
567 | ||
568 | d = a->dst_dentry; | |
569 | SiMustAnyLock(d->d_sb); | |
570 | ||
571 | err = 0; | |
572 | if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) { | |
573 | rdhash = au_sbi(d->d_sb)->si_rdhash; | |
574 | if (!rdhash) | |
575 | rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d)); | |
576 | err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS); | |
577 | if (unlikely(err)) | |
578 | goto out; | |
579 | ||
580 | if (!a->exchange) { | |
581 | au_set_dbtop(d, a->dst_btop); | |
582 | err = may_rename_dstdir(d, &a->whlist); | |
583 | au_set_dbtop(d, a->btgt); | |
584 | } else | |
585 | err = may_rename_srcdir(a); | |
586 | } | |
587 | a->dst_h_dentry = au_h_dptr(d, au_dbtop(d)); | |
588 | if (unlikely(err)) | |
589 | goto out; | |
590 | ||
591 | d = a->src_dentry; | |
592 | a->src_h_dentry = au_h_dptr(d, au_dbtop(d)); | |
593 | if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) { | |
594 | err = may_rename_srcdir(a); | |
595 | if (unlikely(err)) { | |
596 | au_nhash_wh_free(&a->whlist); | |
597 | a->whlist.nh_num = 0; | |
598 | } | |
599 | } | |
600 | out: | |
601 | return err; | |
602 | } | |
603 | ||
604 | /* ---------------------------------------------------------------------- */ | |
605 | ||
606 | /* | |
607 | * simple tests for rename. | |
608 | * following the checks in vfs, plus the parent-child relationship. | |
609 | */ | |
610 | static int au_may_ren(struct au_ren_args *a) | |
611 | { | |
612 | int err, isdir; | |
613 | struct inode *h_inode; | |
614 | ||
615 | if (a->src_btop == a->btgt) { | |
616 | err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent, | |
617 | au_ftest_ren(a->auren_flags, ISDIR_SRC)); | |
618 | if (unlikely(err)) | |
619 | goto out; | |
620 | err = -EINVAL; | |
621 | if (unlikely(a->src_h_dentry == a->h_trap)) | |
622 | goto out; | |
623 | } | |
624 | ||
625 | err = 0; | |
626 | if (a->dst_btop != a->btgt) | |
627 | goto out; | |
628 | ||
629 | err = -ENOTEMPTY; | |
630 | if (unlikely(a->dst_h_dentry == a->h_trap)) | |
631 | goto out; | |
632 | ||
633 | err = -EIO; | |
634 | isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST); | |
635 | if (d_really_is_negative(a->dst_dentry)) { | |
636 | if (d_is_negative(a->dst_h_dentry)) | |
637 | err = au_may_add(a->dst_dentry, a->btgt, | |
638 | a->dst_h_parent, isdir); | |
639 | } else { | |
640 | if (unlikely(d_is_negative(a->dst_h_dentry))) | |
641 | goto out; | |
642 | h_inode = d_inode(a->dst_h_dentry); | |
643 | if (h_inode->i_nlink) | |
644 | err = au_may_del(a->dst_dentry, a->btgt, | |
645 | a->dst_h_parent, isdir); | |
646 | } | |
647 | ||
648 | out: | |
649 | if (unlikely(err == -ENOENT || err == -EEXIST)) | |
650 | err = -EIO; | |
651 | AuTraceErr(err); | |
652 | return err; | |
653 | } | |
654 | ||
655 | /* ---------------------------------------------------------------------- */ | |
656 | ||
657 | /* | |
658 | * locking order | |
659 | * (VFS) | |
660 | * - src_dir and dir by lock_rename() | |
661 | * - inode if exitsts | |
662 | * (aufs) | |
663 | * - lock all | |
664 | * + src_dentry and dentry by aufs_read_and_write_lock2() which calls, | |
665 | * + si_read_lock | |
666 | * + di_write_lock2_child() | |
667 | * + di_write_lock_child() | |
668 | * + ii_write_lock_child() | |
669 | * + di_write_lock_child2() | |
670 | * + ii_write_lock_child2() | |
671 | * + src_parent and parent | |
672 | * + di_write_lock_parent() | |
673 | * + ii_write_lock_parent() | |
674 | * + di_write_lock_parent2() | |
675 | * + ii_write_lock_parent2() | |
676 | * + lower src_dir and dir by vfsub_lock_rename() | |
677 | * + verify the every relationships between child and parent. if any | |
678 | * of them failed, unlock all and return -EBUSY. | |
679 | */ | |
680 | static void au_ren_unlock(struct au_ren_args *a) | |
681 | { | |
682 | vfsub_unlock_rename(a->src_h_parent, a->src_hdir, | |
683 | a->dst_h_parent, a->dst_hdir); | |
684 | if (au_ftest_ren(a->auren_flags, DIRREN) | |
685 | && a->h_root) | |
686 | au_hn_inode_unlock(a->h_root); | |
687 | if (au_ftest_ren(a->auren_flags, MNT_WRITE)) | |
688 | vfsub_mnt_drop_write(au_br_mnt(a->br)); | |
689 | } | |
690 | ||
691 | static int au_ren_lock(struct au_ren_args *a) | |
692 | { | |
693 | int err; | |
694 | unsigned int udba; | |
695 | ||
696 | err = 0; | |
697 | a->src_h_parent = au_h_dptr(a->src_parent, a->btgt); | |
698 | a->src_hdir = au_hi(a->src_dir, a->btgt); | |
699 | a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt); | |
700 | a->dst_hdir = au_hi(a->dst_dir, a->btgt); | |
701 | ||
702 | err = vfsub_mnt_want_write(au_br_mnt(a->br)); | |
703 | if (unlikely(err)) | |
704 | goto out; | |
705 | au_fset_ren(a->auren_flags, MNT_WRITE); | |
706 | if (au_ftest_ren(a->auren_flags, DIRREN)) { | |
707 | struct dentry *root; | |
708 | struct inode *dir; | |
709 | ||
710 | /* | |
711 | * sbinfo is already locked, so this ii_read_lock is | |
712 | * unnecessary. but our debugging feature checks it. | |
713 | */ | |
714 | root = a->src_inode->i_sb->s_root; | |
715 | if (root != a->src_parent && root != a->dst_parent) { | |
716 | dir = d_inode(root); | |
717 | ii_read_lock_parent3(dir); | |
718 | a->h_root = au_hi(dir, a->btgt); | |
719 | ii_read_unlock(dir); | |
720 | au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3); | |
721 | } | |
722 | } | |
723 | a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir, | |
724 | a->dst_h_parent, a->dst_hdir); | |
725 | udba = au_opt_udba(a->src_dentry->d_sb); | |
726 | if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent) | |
727 | || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent))) | |
728 | err = au_busy_or_stale(); | |
729 | if (!err && au_dbtop(a->src_dentry) == a->btgt) | |
730 | err = au_h_verify(a->src_h_dentry, udba, | |
731 | d_inode(a->src_h_parent), a->src_h_parent, | |
732 | a->br); | |
733 | if (!err && au_dbtop(a->dst_dentry) == a->btgt) | |
734 | err = au_h_verify(a->dst_h_dentry, udba, | |
735 | d_inode(a->dst_h_parent), a->dst_h_parent, | |
736 | a->br); | |
737 | if (!err) | |
738 | goto out; /* success */ | |
739 | ||
740 | err = au_busy_or_stale(); | |
741 | au_ren_unlock(a); | |
742 | ||
743 | out: | |
744 | return err; | |
745 | } | |
746 | ||
747 | /* ---------------------------------------------------------------------- */ | |
748 | ||
749 | static void au_ren_refresh_dir(struct au_ren_args *a) | |
750 | { | |
751 | struct inode *dir; | |
752 | ||
753 | dir = a->dst_dir; | |
754 | dir->i_version++; | |
755 | if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) { | |
756 | /* is this updating defined in POSIX? */ | |
757 | au_cpup_attr_timesizes(a->src_inode); | |
758 | au_cpup_attr_nlink(dir, /*force*/1); | |
759 | } | |
760 | au_dir_ts(dir, a->btgt); | |
761 | ||
762 | if (a->exchange) { | |
763 | dir = a->src_dir; | |
764 | dir->i_version++; | |
765 | if (au_ftest_ren(a->auren_flags, ISDIR_DST)) { | |
766 | /* is this updating defined in POSIX? */ | |
767 | au_cpup_attr_timesizes(a->dst_inode); | |
768 | au_cpup_attr_nlink(dir, /*force*/1); | |
769 | } | |
770 | au_dir_ts(dir, a->btgt); | |
771 | } | |
772 | ||
773 | if (au_ftest_ren(a->auren_flags, ISSAMEDIR)) | |
774 | return; | |
775 | ||
776 | dir = a->src_dir; | |
777 | dir->i_version++; | |
778 | if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) | |
779 | au_cpup_attr_nlink(dir, /*force*/1); | |
780 | au_dir_ts(dir, a->btgt); | |
781 | } | |
782 | ||
783 | static void au_ren_refresh(struct au_ren_args *a) | |
784 | { | |
785 | aufs_bindex_t bbot, bindex; | |
786 | struct dentry *d, *h_d; | |
787 | struct inode *i, *h_i; | |
788 | struct super_block *sb; | |
789 | ||
790 | d = a->dst_dentry; | |
791 | d_drop(d); | |
792 | if (a->h_dst) | |
793 | /* already dget-ed by au_ren_or_cpup() */ | |
794 | au_set_h_dptr(d, a->btgt, a->h_dst); | |
795 | ||
796 | i = a->dst_inode; | |
797 | if (i) { | |
798 | if (!a->exchange) { | |
799 | if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) | |
800 | vfsub_drop_nlink(i); | |
801 | else { | |
802 | vfsub_dead_dir(i); | |
803 | au_cpup_attr_timesizes(i); | |
804 | } | |
805 | au_update_dbrange(d, /*do_put_zero*/1); | |
806 | } else | |
807 | au_cpup_attr_nlink(i, /*force*/1); | |
808 | } else { | |
809 | bbot = a->btgt; | |
810 | for (bindex = au_dbtop(d); bindex < bbot; bindex++) | |
811 | au_set_h_dptr(d, bindex, NULL); | |
812 | bbot = au_dbbot(d); | |
813 | for (bindex = a->btgt + 1; bindex <= bbot; bindex++) | |
814 | au_set_h_dptr(d, bindex, NULL); | |
815 | au_update_dbrange(d, /*do_put_zero*/0); | |
816 | } | |
817 | ||
818 | if (a->exchange | |
819 | || au_ftest_ren(a->auren_flags, DIRREN)) { | |
820 | d_drop(a->src_dentry); | |
821 | if (au_ftest_ren(a->auren_flags, DIRREN)) | |
822 | au_set_dbwh(a->src_dentry, -1); | |
823 | return; | |
824 | } | |
825 | ||
826 | d = a->src_dentry; | |
827 | au_set_dbwh(d, -1); | |
828 | bbot = au_dbbot(d); | |
829 | for (bindex = a->btgt + 1; bindex <= bbot; bindex++) { | |
830 | h_d = au_h_dptr(d, bindex); | |
831 | if (h_d) | |
832 | au_set_h_dptr(d, bindex, NULL); | |
833 | } | |
834 | au_set_dbbot(d, a->btgt); | |
835 | ||
836 | sb = d->d_sb; | |
837 | i = a->src_inode; | |
838 | if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i)) | |
839 | return; /* success */ | |
840 | ||
841 | bbot = au_ibbot(i); | |
842 | for (bindex = a->btgt + 1; bindex <= bbot; bindex++) { | |
843 | h_i = au_h_iptr(i, bindex); | |
844 | if (h_i) { | |
845 | au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0); | |
846 | /* ignore this error */ | |
847 | au_set_h_iptr(i, bindex, NULL, 0); | |
848 | } | |
849 | } | |
850 | au_set_ibbot(i, a->btgt); | |
851 | } | |
852 | ||
853 | /* ---------------------------------------------------------------------- */ | |
854 | ||
855 | /* mainly for link(2) and rename(2) */ | |
856 | int au_wbr(struct dentry *dentry, aufs_bindex_t btgt) | |
857 | { | |
858 | aufs_bindex_t bdiropq, bwh; | |
859 | struct dentry *parent; | |
860 | struct au_branch *br; | |
861 | ||
862 | parent = dentry->d_parent; | |
863 | IMustLock(d_inode(parent)); /* dir is locked */ | |
864 | ||
865 | bdiropq = au_dbdiropq(parent); | |
866 | bwh = au_dbwh(dentry); | |
867 | br = au_sbr(dentry->d_sb, btgt); | |
868 | if (au_br_rdonly(br) | |
869 | || (0 <= bdiropq && bdiropq < btgt) | |
870 | || (0 <= bwh && bwh < btgt)) | |
871 | btgt = -1; | |
872 | ||
873 | AuDbg("btgt %d\n", btgt); | |
874 | return btgt; | |
875 | } | |
876 | ||
877 | /* sets src_btop, dst_btop and btgt */ | |
878 | static int au_ren_wbr(struct au_ren_args *a) | |
879 | { | |
880 | int err; | |
881 | struct au_wr_dir_args wr_dir_args = { | |
882 | /* .force_btgt = -1, */ | |
883 | .flags = AuWrDir_ADD_ENTRY | |
884 | }; | |
885 | ||
886 | a->src_btop = au_dbtop(a->src_dentry); | |
887 | a->dst_btop = au_dbtop(a->dst_dentry); | |
888 | if (au_ftest_ren(a->auren_flags, ISDIR_SRC) | |
889 | || au_ftest_ren(a->auren_flags, ISDIR_DST)) | |
890 | au_fset_wrdir(wr_dir_args.flags, ISDIR); | |
891 | wr_dir_args.force_btgt = a->src_btop; | |
892 | if (a->dst_inode && a->dst_btop < a->src_btop) | |
893 | wr_dir_args.force_btgt = a->dst_btop; | |
894 | wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt); | |
895 | err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args); | |
896 | a->btgt = err; | |
897 | if (a->exchange) | |
898 | au_update_dbtop(a->dst_dentry); | |
899 | ||
900 | return err; | |
901 | } | |
902 | ||
903 | static void au_ren_dt(struct au_ren_args *a) | |
904 | { | |
905 | a->h_path.dentry = a->src_h_parent; | |
906 | au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path); | |
907 | if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) { | |
908 | a->h_path.dentry = a->dst_h_parent; | |
909 | au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path); | |
910 | } | |
911 | ||
912 | au_fclr_ren(a->auren_flags, DT_DSTDIR); | |
913 | if (!au_ftest_ren(a->auren_flags, ISDIR_SRC) | |
914 | && !a->exchange) | |
915 | return; | |
916 | ||
917 | a->h_path.dentry = a->src_h_dentry; | |
918 | au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path); | |
919 | if (d_is_positive(a->dst_h_dentry)) { | |
920 | au_fset_ren(a->auren_flags, DT_DSTDIR); | |
921 | a->h_path.dentry = a->dst_h_dentry; | |
922 | au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path); | |
923 | } | |
924 | } | |
925 | ||
926 | static void au_ren_rev_dt(int err, struct au_ren_args *a) | |
927 | { | |
928 | struct dentry *h_d; | |
929 | struct inode *h_inode; | |
930 | ||
931 | au_dtime_revert(a->src_dt + AuPARENT); | |
932 | if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) | |
933 | au_dtime_revert(a->dst_dt + AuPARENT); | |
934 | ||
935 | if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) { | |
936 | h_d = a->src_dt[AuCHILD].dt_h_path.dentry; | |
937 | h_inode = d_inode(h_d); | |
938 | inode_lock_nested(h_inode, AuLsc_I_CHILD); | |
939 | au_dtime_revert(a->src_dt + AuCHILD); | |
940 | inode_unlock(h_inode); | |
941 | ||
942 | if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) { | |
943 | h_d = a->dst_dt[AuCHILD].dt_h_path.dentry; | |
944 | h_inode = d_inode(h_d); | |
945 | inode_lock_nested(h_inode, AuLsc_I_CHILD); | |
946 | au_dtime_revert(a->dst_dt + AuCHILD); | |
947 | inode_unlock(h_inode); | |
948 | } | |
949 | } | |
950 | } | |
951 | ||
952 | /* ---------------------------------------------------------------------- */ | |
953 | ||
954 | int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry, | |
955 | struct inode *_dst_dir, struct dentry *_dst_dentry, | |
956 | unsigned int _flags) | |
957 | { | |
958 | int err, lock_flags; | |
959 | void *rev; | |
960 | /* reduce stack space */ | |
961 | struct au_ren_args *a; | |
962 | struct au_pin pin; | |
963 | ||
964 | AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags); | |
965 | IMustLock(_src_dir); | |
966 | IMustLock(_dst_dir); | |
967 | ||
968 | err = -EINVAL; | |
969 | if (unlikely(_flags & RENAME_WHITEOUT)) | |
970 | goto out; | |
971 | ||
972 | err = -ENOMEM; | |
973 | BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE); | |
974 | a = kzalloc(sizeof(*a), GFP_NOFS); | |
975 | if (unlikely(!a)) | |
976 | goto out; | |
977 | ||
978 | a->flags = _flags; | |
979 | a->exchange = _flags & RENAME_EXCHANGE; | |
980 | a->src_dir = _src_dir; | |
981 | a->src_dentry = _src_dentry; | |
982 | a->src_inode = NULL; | |
983 | if (d_really_is_positive(a->src_dentry)) | |
984 | a->src_inode = d_inode(a->src_dentry); | |
985 | a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */ | |
986 | a->dst_dir = _dst_dir; | |
987 | a->dst_dentry = _dst_dentry; | |
988 | a->dst_inode = NULL; | |
989 | if (d_really_is_positive(a->dst_dentry)) | |
990 | a->dst_inode = d_inode(a->dst_dentry); | |
991 | a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */ | |
992 | if (a->dst_inode) { | |
993 | /* | |
994 | * if EXCHANGE && src is non-dir && dst is dir, | |
995 | * dst is not locked. | |
996 | */ | |
997 | /* IMustLock(a->dst_inode); */ | |
998 | au_igrab(a->dst_inode); | |
999 | } | |
1000 | ||
1001 | err = -ENOTDIR; | |
1002 | lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN; | |
1003 | if (d_is_dir(a->src_dentry)) { | |
1004 | au_fset_ren(a->auren_flags, ISDIR_SRC); | |
1005 | if (unlikely(!a->exchange | |
1006 | && d_really_is_positive(a->dst_dentry) | |
1007 | && !d_is_dir(a->dst_dentry))) | |
1008 | goto out_free; | |
1009 | lock_flags |= AuLock_DIRS; | |
1010 | } | |
1011 | if (a->dst_inode && d_is_dir(a->dst_dentry)) { | |
1012 | au_fset_ren(a->auren_flags, ISDIR_DST); | |
1013 | if (unlikely(!a->exchange | |
1014 | && d_really_is_positive(a->src_dentry) | |
1015 | && !d_is_dir(a->src_dentry))) | |
1016 | goto out_free; | |
1017 | lock_flags |= AuLock_DIRS; | |
1018 | } | |
1019 | err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry, | |
1020 | lock_flags); | |
1021 | if (unlikely(err)) | |
1022 | goto out_free; | |
1023 | ||
1024 | err = au_d_hashed_positive(a->src_dentry); | |
1025 | if (unlikely(err)) | |
1026 | goto out_unlock; | |
1027 | err = -ENOENT; | |
1028 | if (a->dst_inode) { | |
1029 | /* | |
1030 | * If it is a dir, VFS unhash it before this | |
1031 | * function. It means we cannot rely upon d_unhashed(). | |
1032 | */ | |
1033 | if (unlikely(!a->dst_inode->i_nlink)) | |
1034 | goto out_unlock; | |
1035 | if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) { | |
1036 | err = au_d_hashed_positive(a->dst_dentry); | |
1037 | if (unlikely(err && !a->exchange)) | |
1038 | goto out_unlock; | |
1039 | } else if (unlikely(IS_DEADDIR(a->dst_inode))) | |
1040 | goto out_unlock; | |
1041 | } else if (unlikely(d_unhashed(a->dst_dentry))) | |
1042 | goto out_unlock; | |
1043 | ||
1044 | /* | |
1045 | * is it possible? | |
1046 | * yes, it happened (in linux-3.3-rcN) but I don't know why. | |
1047 | * there may exist a problem somewhere else. | |
1048 | */ | |
1049 | err = -EINVAL; | |
1050 | if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry))) | |
1051 | goto out_unlock; | |
1052 | ||
1053 | au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */ | |
1054 | di_write_lock_parent(a->dst_parent); | |
1055 | ||
1056 | /* which branch we process */ | |
1057 | err = au_ren_wbr(a); | |
1058 | if (unlikely(err < 0)) | |
1059 | goto out_parent; | |
1060 | a->br = au_sbr(a->dst_dentry->d_sb, a->btgt); | |
1061 | a->h_path.mnt = au_br_mnt(a->br); | |
1062 | ||
1063 | /* are they available to be renamed */ | |
1064 | err = au_ren_may_dir(a); | |
1065 | if (unlikely(err)) | |
1066 | goto out_children; | |
1067 | ||
1068 | /* prepare the writable parent dir on the same branch */ | |
1069 | if (a->dst_btop == a->btgt) { | |
1070 | au_fset_ren(a->auren_flags, WHDST); | |
1071 | } else { | |
1072 | err = au_cpup_dirs(a->dst_dentry, a->btgt); | |
1073 | if (unlikely(err)) | |
1074 | goto out_children; | |
1075 | } | |
1076 | ||
1077 | err = 0; | |
1078 | if (!a->exchange) { | |
1079 | if (a->src_dir != a->dst_dir) { | |
1080 | /* | |
1081 | * this temporary unlock is safe, | |
1082 | * because both dir->i_mutex are locked. | |
1083 | */ | |
1084 | di_write_unlock(a->dst_parent); | |
1085 | di_write_lock_parent(a->src_parent); | |
1086 | err = au_wr_dir_need_wh(a->src_dentry, | |
1087 | au_ftest_ren(a->auren_flags, | |
1088 | ISDIR_SRC), | |
1089 | &a->btgt); | |
1090 | di_write_unlock(a->src_parent); | |
1091 | di_write_lock2_parent(a->src_parent, a->dst_parent, | |
1092 | /*isdir*/1); | |
1093 | au_fclr_ren(a->auren_flags, ISSAMEDIR); | |
1094 | } else | |
1095 | err = au_wr_dir_need_wh(a->src_dentry, | |
1096 | au_ftest_ren(a->auren_flags, | |
1097 | ISDIR_SRC), | |
1098 | &a->btgt); | |
1099 | } | |
1100 | if (unlikely(err < 0)) | |
1101 | goto out_children; | |
1102 | if (err) | |
1103 | au_fset_ren(a->auren_flags, WHSRC); | |
1104 | ||
1105 | /* cpup src */ | |
1106 | if (a->src_btop != a->btgt) { | |
1107 | err = au_pin(&pin, a->src_dentry, a->btgt, | |
1108 | au_opt_udba(a->src_dentry->d_sb), | |
1109 | AuPin_DI_LOCKED | AuPin_MNT_WRITE); | |
1110 | if (!err) { | |
1111 | struct au_cp_generic cpg = { | |
1112 | .dentry = a->src_dentry, | |
1113 | .bdst = a->btgt, | |
1114 | .bsrc = a->src_btop, | |
1115 | .len = -1, | |
1116 | .pin = &pin, | |
1117 | .flags = AuCpup_DTIME | AuCpup_HOPEN | |
1118 | }; | |
1119 | AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop); | |
1120 | err = au_sio_cpup_simple(&cpg); | |
1121 | au_unpin(&pin); | |
1122 | } | |
1123 | if (unlikely(err)) | |
1124 | goto out_children; | |
1125 | a->src_btop = a->btgt; | |
1126 | a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt); | |
1127 | if (!a->exchange) | |
1128 | au_fset_ren(a->auren_flags, WHSRC); | |
1129 | } | |
1130 | ||
1131 | /* cpup dst */ | |
1132 | if (a->exchange && a->dst_inode | |
1133 | && a->dst_btop != a->btgt) { | |
1134 | err = au_pin(&pin, a->dst_dentry, a->btgt, | |
1135 | au_opt_udba(a->dst_dentry->d_sb), | |
1136 | AuPin_DI_LOCKED | AuPin_MNT_WRITE); | |
1137 | if (!err) { | |
1138 | struct au_cp_generic cpg = { | |
1139 | .dentry = a->dst_dentry, | |
1140 | .bdst = a->btgt, | |
1141 | .bsrc = a->dst_btop, | |
1142 | .len = -1, | |
1143 | .pin = &pin, | |
1144 | .flags = AuCpup_DTIME | AuCpup_HOPEN | |
1145 | }; | |
1146 | err = au_sio_cpup_simple(&cpg); | |
1147 | au_unpin(&pin); | |
1148 | } | |
1149 | if (unlikely(err)) | |
1150 | goto out_children; | |
1151 | a->dst_btop = a->btgt; | |
1152 | a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt); | |
1153 | } | |
1154 | ||
1155 | /* lock them all */ | |
1156 | err = au_ren_lock(a); | |
1157 | if (unlikely(err)) | |
1158 | /* leave the copied-up one */ | |
1159 | goto out_children; | |
1160 | ||
1161 | if (!a->exchange) { | |
1162 | if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE)) | |
1163 | err = au_may_ren(a); | |
1164 | else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN)) | |
1165 | err = -ENAMETOOLONG; | |
1166 | if (unlikely(err)) | |
1167 | goto out_hdir; | |
1168 | } | |
1169 | ||
1170 | /* store timestamps to be revertible */ | |
1171 | au_ren_dt(a); | |
1172 | ||
1173 | /* store dirren info */ | |
1174 | if (au_ftest_ren(a->auren_flags, DIRREN)) { | |
1175 | err = au_dr_rename(a->src_dentry, a->btgt, | |
1176 | &a->dst_dentry->d_name, &rev); | |
1177 | AuTraceErr(err); | |
1178 | if (unlikely(err)) | |
1179 | goto out_dt; | |
1180 | } | |
1181 | ||
1182 | /* here we go */ | |
1183 | err = do_rename(a); | |
1184 | if (unlikely(err)) | |
1185 | goto out_dirren; | |
1186 | ||
1187 | if (au_ftest_ren(a->auren_flags, DIRREN)) | |
1188 | au_dr_rename_fin(a->src_dentry, a->btgt, rev); | |
1189 | ||
1190 | /* update dir attributes */ | |
1191 | au_ren_refresh_dir(a); | |
1192 | ||
1193 | /* dput/iput all lower dentries */ | |
1194 | au_ren_refresh(a); | |
1195 | ||
1196 | goto out_hdir; /* success */ | |
1197 | ||
1198 | out_dirren: | |
1199 | if (au_ftest_ren(a->auren_flags, DIRREN)) | |
1200 | au_dr_rename_rev(a->src_dentry, a->btgt, rev); | |
1201 | out_dt: | |
1202 | au_ren_rev_dt(err, a); | |
1203 | out_hdir: | |
1204 | au_ren_unlock(a); | |
1205 | out_children: | |
1206 | au_nhash_wh_free(&a->whlist); | |
1207 | if (err && a->dst_inode && a->dst_btop != a->btgt) { | |
1208 | AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt); | |
1209 | au_set_h_dptr(a->dst_dentry, a->btgt, NULL); | |
1210 | au_set_dbtop(a->dst_dentry, a->dst_btop); | |
1211 | } | |
1212 | out_parent: | |
1213 | if (!err) { | |
1214 | if (d_unhashed(a->src_dentry)) | |
1215 | au_fset_ren(a->auren_flags, DROPPED_SRC); | |
1216 | if (d_unhashed(a->dst_dentry)) | |
1217 | au_fset_ren(a->auren_flags, DROPPED_DST); | |
1218 | if (!a->exchange) | |
1219 | d_move(a->src_dentry, a->dst_dentry); | |
1220 | else { | |
1221 | d_exchange(a->src_dentry, a->dst_dentry); | |
1222 | if (au_ftest_ren(a->auren_flags, DROPPED_DST)) | |
1223 | d_drop(a->dst_dentry); | |
1224 | } | |
1225 | if (au_ftest_ren(a->auren_flags, DROPPED_SRC)) | |
1226 | d_drop(a->src_dentry); | |
1227 | } else { | |
1228 | au_update_dbtop(a->dst_dentry); | |
1229 | if (!a->dst_inode) | |
1230 | d_drop(a->dst_dentry); | |
1231 | } | |
1232 | if (au_ftest_ren(a->auren_flags, ISSAMEDIR)) | |
1233 | di_write_unlock(a->dst_parent); | |
1234 | else | |
1235 | di_write_unlock2(a->src_parent, a->dst_parent); | |
1236 | out_unlock: | |
1237 | aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry); | |
1238 | out_free: | |
1239 | iput(a->dst_inode); | |
1240 | if (a->thargs) | |
1241 | au_whtmp_rmdir_free(a->thargs); | |
1242 | kfree(a); | |
1243 | out: | |
1244 | AuTraceErr(err); | |
1245 | return err; | |
1246 | } |