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