]>
Commit | Line | Data |
---|---|---|
b6450630 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 operations (add entry) | |
20 | */ | |
21 | ||
22 | #include "aufs.h" | |
23 | ||
24 | /* | |
25 | * final procedure of adding a new entry, except link(2). | |
26 | * remove whiteout, instantiate, copyup the parent dir's times and size | |
27 | * and update version. | |
28 | * if it failed, re-create the removed whiteout. | |
29 | */ | |
30 | static int epilog(struct inode *dir, aufs_bindex_t bindex, | |
31 | struct dentry *wh_dentry, struct dentry *dentry) | |
32 | { | |
33 | int err, rerr; | |
34 | aufs_bindex_t bwh; | |
35 | struct path h_path; | |
36 | struct super_block *sb; | |
37 | struct inode *inode, *h_dir; | |
38 | struct dentry *wh; | |
39 | ||
40 | bwh = -1; | |
41 | sb = dir->i_sb; | |
42 | if (wh_dentry) { | |
43 | h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */ | |
44 | IMustLock(h_dir); | |
45 | AuDebugOn(au_h_iptr(dir, bindex) != h_dir); | |
46 | bwh = au_dbwh(dentry); | |
47 | h_path.dentry = wh_dentry; | |
48 | h_path.mnt = au_sbr_mnt(sb, bindex); | |
49 | err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, | |
50 | dentry); | |
51 | if (unlikely(err)) | |
52 | goto out; | |
53 | } | |
54 | ||
55 | inode = au_new_inode(dentry, /*must_new*/1); | |
56 | if (!IS_ERR(inode)) { | |
57 | d_instantiate(dentry, inode); | |
58 | dir = d_inode(dentry->d_parent); /* dir inode is locked */ | |
59 | IMustLock(dir); | |
60 | au_dir_ts(dir, bindex); | |
61 | dir->i_version++; | |
62 | au_fhsm_wrote(sb, bindex, /*force*/0); | |
63 | return 0; /* success */ | |
64 | } | |
65 | ||
66 | err = PTR_ERR(inode); | |
67 | if (!wh_dentry) | |
68 | goto out; | |
69 | ||
70 | /* revert */ | |
71 | /* dir inode is locked */ | |
72 | wh = au_wh_create(dentry, bwh, wh_dentry->d_parent); | |
73 | rerr = PTR_ERR(wh); | |
74 | if (IS_ERR(wh)) { | |
75 | AuIOErr("%pd reverting whiteout failed(%d, %d)\n", | |
76 | dentry, err, rerr); | |
77 | err = -EIO; | |
78 | } else | |
79 | dput(wh); | |
80 | ||
81 | out: | |
82 | return err; | |
83 | } | |
84 | ||
85 | static int au_d_may_add(struct dentry *dentry) | |
86 | { | |
87 | int err; | |
88 | ||
89 | err = 0; | |
90 | if (unlikely(d_unhashed(dentry))) | |
91 | err = -ENOENT; | |
92 | if (unlikely(d_really_is_positive(dentry))) | |
93 | err = -EEXIST; | |
94 | return err; | |
95 | } | |
96 | ||
97 | /* | |
98 | * simple tests for the adding inode operations. | |
99 | * following the checks in vfs, plus the parent-child relationship. | |
100 | */ | |
101 | int au_may_add(struct dentry *dentry, aufs_bindex_t bindex, | |
102 | struct dentry *h_parent, int isdir) | |
103 | { | |
104 | int err; | |
105 | umode_t h_mode; | |
106 | struct dentry *h_dentry; | |
107 | struct inode *h_inode; | |
108 | ||
109 | err = -ENAMETOOLONG; | |
110 | if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN)) | |
111 | goto out; | |
112 | ||
113 | h_dentry = au_h_dptr(dentry, bindex); | |
114 | if (d_really_is_negative(dentry)) { | |
115 | err = -EEXIST; | |
116 | if (unlikely(d_is_positive(h_dentry))) | |
117 | goto out; | |
118 | } else { | |
119 | /* rename(2) case */ | |
120 | err = -EIO; | |
121 | if (unlikely(d_is_negative(h_dentry))) | |
122 | goto out; | |
123 | h_inode = d_inode(h_dentry); | |
124 | if (unlikely(!h_inode->i_nlink)) | |
125 | goto out; | |
126 | ||
127 | h_mode = h_inode->i_mode; | |
128 | if (!isdir) { | |
129 | err = -EISDIR; | |
130 | if (unlikely(S_ISDIR(h_mode))) | |
131 | goto out; | |
132 | } else if (unlikely(!S_ISDIR(h_mode))) { | |
133 | err = -ENOTDIR; | |
134 | goto out; | |
135 | } | |
136 | } | |
137 | ||
138 | err = 0; | |
139 | /* expected parent dir is locked */ | |
140 | if (unlikely(h_parent != h_dentry->d_parent)) | |
141 | err = -EIO; | |
142 | ||
143 | out: | |
144 | AuTraceErr(err); | |
145 | return err; | |
146 | } | |
147 | ||
148 | /* | |
149 | * initial procedure of adding a new entry. | |
150 | * prepare writable branch and the parent dir, lock it, | |
151 | * and lookup whiteout for the new entry. | |
152 | */ | |
153 | static struct dentry* | |
154 | lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt, | |
155 | struct dentry *src_dentry, struct au_pin *pin, | |
156 | struct au_wr_dir_args *wr_dir_args) | |
157 | { | |
158 | struct dentry *wh_dentry, *h_parent; | |
159 | struct super_block *sb; | |
160 | struct au_branch *br; | |
161 | int err; | |
162 | unsigned int udba; | |
163 | aufs_bindex_t bcpup; | |
164 | ||
165 | AuDbg("%pd\n", dentry); | |
166 | ||
167 | err = au_wr_dir(dentry, src_dentry, wr_dir_args); | |
168 | bcpup = err; | |
169 | wh_dentry = ERR_PTR(err); | |
170 | if (unlikely(err < 0)) | |
171 | goto out; | |
172 | ||
173 | sb = dentry->d_sb; | |
174 | udba = au_opt_udba(sb); | |
175 | err = au_pin(pin, dentry, bcpup, udba, | |
176 | AuPin_DI_LOCKED | AuPin_MNT_WRITE); | |
177 | wh_dentry = ERR_PTR(err); | |
178 | if (unlikely(err)) | |
179 | goto out; | |
180 | ||
181 | h_parent = au_pinned_h_parent(pin); | |
182 | if (udba != AuOpt_UDBA_NONE | |
183 | && au_dbtop(dentry) == bcpup) | |
184 | err = au_may_add(dentry, bcpup, h_parent, | |
185 | au_ftest_wrdir(wr_dir_args->flags, ISDIR)); | |
186 | else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN)) | |
187 | err = -ENAMETOOLONG; | |
188 | wh_dentry = ERR_PTR(err); | |
189 | if (unlikely(err)) | |
190 | goto out_unpin; | |
191 | ||
192 | br = au_sbr(sb, bcpup); | |
193 | if (dt) { | |
194 | struct path tmp = { | |
195 | .dentry = h_parent, | |
196 | .mnt = au_br_mnt(br) | |
197 | }; | |
198 | au_dtime_store(dt, au_pinned_parent(pin), &tmp); | |
199 | } | |
200 | ||
201 | wh_dentry = NULL; | |
202 | if (bcpup != au_dbwh(dentry)) | |
203 | goto out; /* success */ | |
204 | ||
205 | /* | |
206 | * ENAMETOOLONG here means that if we allowed create such name, then it | |
207 | * would not be able to removed in the future. So we don't allow such | |
208 | * name here and we don't handle ENAMETOOLONG differently here. | |
209 | */ | |
210 | wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br); | |
211 | ||
212 | out_unpin: | |
213 | if (IS_ERR(wh_dentry)) | |
214 | au_unpin(pin); | |
215 | out: | |
216 | return wh_dentry; | |
217 | } | |
218 | ||
219 | /* ---------------------------------------------------------------------- */ | |
220 | ||
221 | enum { Mknod, Symlink, Creat }; | |
222 | struct simple_arg { | |
223 | int type; | |
224 | union { | |
225 | struct { | |
226 | umode_t mode; | |
227 | bool want_excl; | |
228 | bool try_aopen; | |
229 | struct vfsub_aopen_args *aopen; | |
230 | } c; | |
231 | struct { | |
232 | const char *symname; | |
233 | } s; | |
234 | struct { | |
235 | umode_t mode; | |
236 | dev_t dev; | |
237 | } m; | |
238 | } u; | |
239 | }; | |
240 | ||
241 | static int add_simple(struct inode *dir, struct dentry *dentry, | |
242 | struct simple_arg *arg) | |
243 | { | |
244 | int err, rerr; | |
245 | aufs_bindex_t btop; | |
246 | unsigned char created; | |
247 | const unsigned char try_aopen | |
248 | = (arg->type == Creat && arg->u.c.try_aopen); | |
249 | struct dentry *wh_dentry, *parent; | |
250 | struct inode *h_dir; | |
251 | struct super_block *sb; | |
252 | struct au_branch *br; | |
253 | /* to reuduce stack size */ | |
254 | struct { | |
255 | struct au_dtime dt; | |
256 | struct au_pin pin; | |
257 | struct path h_path; | |
258 | struct au_wr_dir_args wr_dir_args; | |
259 | } *a; | |
260 | ||
261 | AuDbg("%pd\n", dentry); | |
262 | IMustLock(dir); | |
263 | ||
264 | err = -ENOMEM; | |
265 | a = kmalloc(sizeof(*a), GFP_NOFS); | |
266 | if (unlikely(!a)) | |
267 | goto out; | |
268 | a->wr_dir_args.force_btgt = -1; | |
269 | a->wr_dir_args.flags = AuWrDir_ADD_ENTRY; | |
270 | ||
271 | parent = dentry->d_parent; /* dir inode is locked */ | |
272 | if (!try_aopen) { | |
273 | err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN); | |
274 | if (unlikely(err)) | |
275 | goto out_free; | |
276 | } | |
277 | err = au_d_may_add(dentry); | |
278 | if (unlikely(err)) | |
279 | goto out_unlock; | |
280 | if (!try_aopen) | |
281 | di_write_lock_parent(parent); | |
282 | wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL, | |
283 | &a->pin, &a->wr_dir_args); | |
284 | err = PTR_ERR(wh_dentry); | |
285 | if (IS_ERR(wh_dentry)) | |
286 | goto out_parent; | |
287 | ||
288 | btop = au_dbtop(dentry); | |
289 | sb = dentry->d_sb; | |
290 | br = au_sbr(sb, btop); | |
291 | a->h_path.dentry = au_h_dptr(dentry, btop); | |
292 | a->h_path.mnt = au_br_mnt(br); | |
293 | h_dir = au_pinned_h_dir(&a->pin); | |
294 | switch (arg->type) { | |
295 | case Creat: | |
296 | err = 0; | |
297 | if (!try_aopen || !h_dir->i_op->atomic_open) | |
298 | err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode, | |
299 | arg->u.c.want_excl); | |
300 | else | |
301 | err = vfsub_atomic_open(h_dir, a->h_path.dentry, | |
302 | arg->u.c.aopen, br); | |
303 | break; | |
304 | case Symlink: | |
305 | err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname); | |
306 | break; | |
307 | case Mknod: | |
308 | err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode, | |
309 | arg->u.m.dev); | |
310 | break; | |
311 | default: | |
312 | BUG(); | |
313 | } | |
314 | created = !err; | |
315 | if (!err) | |
316 | err = epilog(dir, btop, wh_dentry, dentry); | |
317 | ||
318 | /* revert */ | |
319 | if (unlikely(created && err && d_is_positive(a->h_path.dentry))) { | |
320 | /* no delegation since it is just created */ | |
321 | rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL, | |
322 | /*force*/0); | |
323 | if (rerr) { | |
324 | AuIOErr("%pd revert failure(%d, %d)\n", | |
325 | dentry, err, rerr); | |
326 | err = -EIO; | |
327 | } | |
328 | au_dtime_revert(&a->dt); | |
329 | } | |
330 | ||
331 | if (!err && try_aopen && !h_dir->i_op->atomic_open) | |
332 | *arg->u.c.aopen->opened |= FILE_CREATED; | |
333 | ||
334 | au_unpin(&a->pin); | |
335 | dput(wh_dentry); | |
336 | ||
337 | out_parent: | |
338 | if (!try_aopen) | |
339 | di_write_unlock(parent); | |
340 | out_unlock: | |
341 | if (unlikely(err)) { | |
342 | au_update_dbtop(dentry); | |
343 | d_drop(dentry); | |
344 | } | |
345 | if (!try_aopen) | |
346 | aufs_read_unlock(dentry, AuLock_DW); | |
347 | out_free: | |
348 | kfree(a); | |
349 | out: | |
350 | return err; | |
351 | } | |
352 | ||
353 | int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, | |
354 | dev_t dev) | |
355 | { | |
356 | struct simple_arg arg = { | |
357 | .type = Mknod, | |
358 | .u.m = { | |
359 | .mode = mode, | |
360 | .dev = dev | |
361 | } | |
362 | }; | |
363 | return add_simple(dir, dentry, &arg); | |
364 | } | |
365 | ||
366 | int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) | |
367 | { | |
368 | struct simple_arg arg = { | |
369 | .type = Symlink, | |
370 | .u.s.symname = symname | |
371 | }; | |
372 | return add_simple(dir, dentry, &arg); | |
373 | } | |
374 | ||
375 | int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode, | |
376 | bool want_excl) | |
377 | { | |
378 | struct simple_arg arg = { | |
379 | .type = Creat, | |
380 | .u.c = { | |
381 | .mode = mode, | |
382 | .want_excl = want_excl | |
383 | } | |
384 | }; | |
385 | return add_simple(dir, dentry, &arg); | |
386 | } | |
387 | ||
388 | int au_aopen_or_create(struct inode *dir, struct dentry *dentry, | |
389 | struct vfsub_aopen_args *aopen_args) | |
390 | { | |
391 | struct simple_arg arg = { | |
392 | .type = Creat, | |
393 | .u.c = { | |
394 | .mode = aopen_args->create_mode, | |
395 | .want_excl = aopen_args->open_flag & O_EXCL, | |
396 | .try_aopen = true, | |
397 | .aopen = aopen_args | |
398 | } | |
399 | }; | |
400 | return add_simple(dir, dentry, &arg); | |
401 | } | |
402 | ||
403 | int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) | |
404 | { | |
405 | int err; | |
406 | aufs_bindex_t bindex; | |
407 | struct super_block *sb; | |
408 | struct dentry *parent, *h_parent, *h_dentry; | |
409 | struct inode *h_dir, *inode; | |
410 | struct vfsmount *h_mnt; | |
411 | struct au_wr_dir_args wr_dir_args = { | |
412 | .force_btgt = -1, | |
413 | .flags = AuWrDir_TMPFILE | |
414 | }; | |
415 | ||
416 | /* copy-up may happen */ | |
417 | inode_lock(dir); | |
418 | ||
419 | sb = dir->i_sb; | |
420 | err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM); | |
421 | if (unlikely(err)) | |
422 | goto out; | |
423 | ||
424 | err = au_di_init(dentry); | |
425 | if (unlikely(err)) | |
426 | goto out_si; | |
427 | ||
428 | err = -EBUSY; | |
429 | parent = d_find_any_alias(dir); | |
430 | AuDebugOn(!parent); | |
431 | di_write_lock_parent(parent); | |
432 | if (unlikely(d_inode(parent) != dir)) | |
433 | goto out_parent; | |
434 | ||
435 | err = au_digen_test(parent, au_sigen(sb)); | |
436 | if (unlikely(err)) | |
437 | goto out_parent; | |
438 | ||
439 | bindex = au_dbtop(parent); | |
440 | au_set_dbtop(dentry, bindex); | |
441 | au_set_dbbot(dentry, bindex); | |
442 | err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args); | |
443 | bindex = err; | |
444 | if (unlikely(err < 0)) | |
445 | goto out_parent; | |
446 | ||
447 | err = -EOPNOTSUPP; | |
448 | h_dir = au_h_iptr(dir, bindex); | |
449 | if (unlikely(!h_dir->i_op->tmpfile)) | |
450 | goto out_parent; | |
451 | ||
452 | h_mnt = au_sbr_mnt(sb, bindex); | |
453 | err = vfsub_mnt_want_write(h_mnt); | |
454 | if (unlikely(err)) | |
455 | goto out_parent; | |
456 | ||
457 | h_parent = au_h_dptr(parent, bindex); | |
458 | h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0); | |
459 | if (IS_ERR(h_dentry)) { | |
460 | err = PTR_ERR(h_dentry); | |
461 | goto out_mnt; | |
462 | } | |
463 | ||
464 | au_set_dbtop(dentry, bindex); | |
465 | au_set_dbbot(dentry, bindex); | |
466 | au_set_h_dptr(dentry, bindex, dget(h_dentry)); | |
467 | inode = au_new_inode(dentry, /*must_new*/1); | |
468 | if (IS_ERR(inode)) { | |
469 | err = PTR_ERR(inode); | |
470 | au_set_h_dptr(dentry, bindex, NULL); | |
471 | au_set_dbtop(dentry, -1); | |
472 | au_set_dbbot(dentry, -1); | |
473 | } else { | |
474 | if (!inode->i_nlink) | |
475 | set_nlink(inode, 1); | |
476 | d_tmpfile(dentry, inode); | |
477 | au_di(dentry)->di_tmpfile = 1; | |
478 | ||
479 | /* update without i_mutex */ | |
480 | if (au_ibtop(dir) == au_dbtop(dentry)) | |
481 | au_cpup_attr_timesizes(dir); | |
482 | } | |
483 | dput(h_dentry); | |
484 | ||
485 | out_mnt: | |
486 | vfsub_mnt_drop_write(h_mnt); | |
487 | out_parent: | |
488 | di_write_unlock(parent); | |
489 | dput(parent); | |
490 | di_write_unlock(dentry); | |
491 | if (unlikely(err)) { | |
492 | au_di_fin(dentry); | |
493 | dentry->d_fsdata = NULL; | |
494 | } | |
495 | out_si: | |
496 | si_read_unlock(sb); | |
497 | out: | |
498 | inode_unlock(dir); | |
499 | return err; | |
500 | } | |
501 | ||
502 | /* ---------------------------------------------------------------------- */ | |
503 | ||
504 | struct au_link_args { | |
505 | aufs_bindex_t bdst, bsrc; | |
506 | struct au_pin pin; | |
507 | struct path h_path; | |
508 | struct dentry *src_parent, *parent; | |
509 | }; | |
510 | ||
511 | static int au_cpup_before_link(struct dentry *src_dentry, | |
512 | struct au_link_args *a) | |
513 | { | |
514 | int err; | |
515 | struct dentry *h_src_dentry; | |
516 | struct au_cp_generic cpg = { | |
517 | .dentry = src_dentry, | |
518 | .bdst = a->bdst, | |
519 | .bsrc = a->bsrc, | |
520 | .len = -1, | |
521 | .pin = &a->pin, | |
522 | .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */ | |
523 | }; | |
524 | ||
525 | di_read_lock_parent(a->src_parent, AuLock_IR); | |
526 | err = au_test_and_cpup_dirs(src_dentry, a->bdst); | |
527 | if (unlikely(err)) | |
528 | goto out; | |
529 | ||
530 | h_src_dentry = au_h_dptr(src_dentry, a->bsrc); | |
531 | err = au_pin(&a->pin, src_dentry, a->bdst, | |
532 | au_opt_udba(src_dentry->d_sb), | |
533 | AuPin_DI_LOCKED | AuPin_MNT_WRITE); | |
534 | if (unlikely(err)) | |
535 | goto out; | |
536 | ||
537 | err = au_sio_cpup_simple(&cpg); | |
538 | au_unpin(&a->pin); | |
539 | ||
540 | out: | |
541 | di_read_unlock(a->src_parent, AuLock_IR); | |
542 | return err; | |
543 | } | |
544 | ||
545 | static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry, | |
546 | struct au_link_args *a) | |
547 | { | |
548 | int err; | |
549 | unsigned char plink; | |
550 | aufs_bindex_t bbot; | |
551 | struct dentry *h_src_dentry; | |
552 | struct inode *h_inode, *inode, *delegated; | |
553 | struct super_block *sb; | |
554 | struct file *h_file; | |
555 | ||
556 | plink = 0; | |
557 | h_inode = NULL; | |
558 | sb = src_dentry->d_sb; | |
559 | inode = d_inode(src_dentry); | |
560 | if (au_ibtop(inode) <= a->bdst) | |
561 | h_inode = au_h_iptr(inode, a->bdst); | |
562 | if (!h_inode || !h_inode->i_nlink) { | |
563 | /* copyup src_dentry as the name of dentry. */ | |
564 | bbot = au_dbbot(dentry); | |
565 | if (bbot < a->bsrc) | |
566 | au_set_dbbot(dentry, a->bsrc); | |
567 | au_set_h_dptr(dentry, a->bsrc, | |
568 | dget(au_h_dptr(src_dentry, a->bsrc))); | |
569 | dget(a->h_path.dentry); | |
570 | au_set_h_dptr(dentry, a->bdst, NULL); | |
571 | AuDbg("temporary d_inode...\n"); | |
572 | spin_lock(&dentry->d_lock); | |
573 | dentry->d_inode = d_inode(src_dentry); /* tmp */ | |
574 | spin_unlock(&dentry->d_lock); | |
575 | h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0); | |
576 | if (IS_ERR(h_file)) | |
577 | err = PTR_ERR(h_file); | |
578 | else { | |
579 | struct au_cp_generic cpg = { | |
580 | .dentry = dentry, | |
581 | .bdst = a->bdst, | |
582 | .bsrc = -1, | |
583 | .len = -1, | |
584 | .pin = &a->pin, | |
585 | .flags = AuCpup_KEEPLINO | |
586 | }; | |
587 | err = au_sio_cpup_simple(&cpg); | |
588 | au_h_open_post(dentry, a->bsrc, h_file); | |
589 | if (!err) { | |
590 | dput(a->h_path.dentry); | |
591 | a->h_path.dentry = au_h_dptr(dentry, a->bdst); | |
592 | } else | |
593 | au_set_h_dptr(dentry, a->bdst, | |
594 | a->h_path.dentry); | |
595 | } | |
596 | spin_lock(&dentry->d_lock); | |
597 | dentry->d_inode = NULL; /* restore */ | |
598 | spin_unlock(&dentry->d_lock); | |
599 | AuDbg("temporary d_inode...done\n"); | |
600 | au_set_h_dptr(dentry, a->bsrc, NULL); | |
601 | au_set_dbbot(dentry, bbot); | |
602 | } else { | |
603 | /* the inode of src_dentry already exists on a.bdst branch */ | |
604 | h_src_dentry = d_find_alias(h_inode); | |
605 | if (!h_src_dentry && au_plink_test(inode)) { | |
606 | plink = 1; | |
607 | h_src_dentry = au_plink_lkup(inode, a->bdst); | |
608 | err = PTR_ERR(h_src_dentry); | |
609 | if (IS_ERR(h_src_dentry)) | |
610 | goto out; | |
611 | ||
612 | if (unlikely(d_is_negative(h_src_dentry))) { | |
613 | dput(h_src_dentry); | |
614 | h_src_dentry = NULL; | |
615 | } | |
616 | ||
617 | } | |
618 | if (h_src_dentry) { | |
619 | delegated = NULL; | |
620 | err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin), | |
621 | &a->h_path, &delegated); | |
622 | if (unlikely(err == -EWOULDBLOCK)) { | |
623 | pr_warn("cannot retry for NFSv4 delegation" | |
624 | " for an internal link\n"); | |
625 | iput(delegated); | |
626 | } | |
627 | dput(h_src_dentry); | |
628 | } else { | |
629 | AuIOErr("no dentry found for hi%lu on b%d\n", | |
630 | h_inode->i_ino, a->bdst); | |
631 | err = -EIO; | |
632 | } | |
633 | } | |
634 | ||
635 | if (!err && !plink) | |
636 | au_plink_append(inode, a->bdst, a->h_path.dentry); | |
637 | ||
638 | out: | |
639 | AuTraceErr(err); | |
640 | return err; | |
641 | } | |
642 | ||
643 | int aufs_link(struct dentry *src_dentry, struct inode *dir, | |
644 | struct dentry *dentry) | |
645 | { | |
646 | int err, rerr; | |
647 | struct au_dtime dt; | |
648 | struct au_link_args *a; | |
649 | struct dentry *wh_dentry, *h_src_dentry; | |
650 | struct inode *inode, *delegated; | |
651 | struct super_block *sb; | |
652 | struct au_wr_dir_args wr_dir_args = { | |
653 | /* .force_btgt = -1, */ | |
654 | .flags = AuWrDir_ADD_ENTRY | |
655 | }; | |
656 | ||
657 | IMustLock(dir); | |
658 | inode = d_inode(src_dentry); | |
659 | IMustLock(inode); | |
660 | ||
661 | err = -ENOMEM; | |
662 | a = kzalloc(sizeof(*a), GFP_NOFS); | |
663 | if (unlikely(!a)) | |
664 | goto out; | |
665 | ||
666 | a->parent = dentry->d_parent; /* dir inode is locked */ | |
667 | err = aufs_read_and_write_lock2(dentry, src_dentry, | |
668 | AuLock_NOPLM | AuLock_GEN); | |
669 | if (unlikely(err)) | |
670 | goto out_kfree; | |
671 | err = au_d_linkable(src_dentry); | |
672 | if (unlikely(err)) | |
673 | goto out_unlock; | |
674 | err = au_d_may_add(dentry); | |
675 | if (unlikely(err)) | |
676 | goto out_unlock; | |
677 | ||
678 | a->src_parent = dget_parent(src_dentry); | |
679 | wr_dir_args.force_btgt = au_ibtop(inode); | |
680 | ||
681 | di_write_lock_parent(a->parent); | |
682 | wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt); | |
683 | wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin, | |
684 | &wr_dir_args); | |
685 | err = PTR_ERR(wh_dentry); | |
686 | if (IS_ERR(wh_dentry)) | |
687 | goto out_parent; | |
688 | ||
689 | err = 0; | |
690 | sb = dentry->d_sb; | |
691 | a->bdst = au_dbtop(dentry); | |
692 | a->h_path.dentry = au_h_dptr(dentry, a->bdst); | |
693 | a->h_path.mnt = au_sbr_mnt(sb, a->bdst); | |
694 | a->bsrc = au_ibtop(inode); | |
695 | h_src_dentry = au_h_d_alias(src_dentry, a->bsrc); | |
696 | if (!h_src_dentry && au_di(src_dentry)->di_tmpfile) | |
697 | h_src_dentry = dget(au_hi_wh(inode, a->bsrc)); | |
698 | if (!h_src_dentry) { | |
699 | a->bsrc = au_dbtop(src_dentry); | |
700 | h_src_dentry = au_h_d_alias(src_dentry, a->bsrc); | |
701 | AuDebugOn(!h_src_dentry); | |
702 | } else if (IS_ERR(h_src_dentry)) { | |
703 | err = PTR_ERR(h_src_dentry); | |
704 | goto out_parent; | |
705 | } | |
706 | ||
707 | /* | |
708 | * aufs doesn't touch the credential so | |
709 | * security_dentry_create_files_as() is unnecrssary. | |
710 | */ | |
711 | if (au_opt_test(au_mntflags(sb), PLINK)) { | |
712 | if (a->bdst < a->bsrc | |
713 | /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) | |
714 | err = au_cpup_or_link(src_dentry, dentry, a); | |
715 | else { | |
716 | delegated = NULL; | |
717 | err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin), | |
718 | &a->h_path, &delegated); | |
719 | if (unlikely(err == -EWOULDBLOCK)) { | |
720 | pr_warn("cannot retry for NFSv4 delegation" | |
721 | " for an internal link\n"); | |
722 | iput(delegated); | |
723 | } | |
724 | } | |
725 | dput(h_src_dentry); | |
726 | } else { | |
727 | /* | |
728 | * copyup src_dentry to the branch we process, | |
729 | * and then link(2) to it. | |
730 | */ | |
731 | dput(h_src_dentry); | |
732 | if (a->bdst < a->bsrc | |
733 | /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) { | |
734 | au_unpin(&a->pin); | |
735 | di_write_unlock(a->parent); | |
736 | err = au_cpup_before_link(src_dentry, a); | |
737 | di_write_lock_parent(a->parent); | |
738 | if (!err) | |
739 | err = au_pin(&a->pin, dentry, a->bdst, | |
740 | au_opt_udba(sb), | |
741 | AuPin_DI_LOCKED | AuPin_MNT_WRITE); | |
742 | if (unlikely(err)) | |
743 | goto out_wh; | |
744 | } | |
745 | if (!err) { | |
746 | h_src_dentry = au_h_dptr(src_dentry, a->bdst); | |
747 | err = -ENOENT; | |
748 | if (h_src_dentry && d_is_positive(h_src_dentry)) { | |
749 | delegated = NULL; | |
750 | err = vfsub_link(h_src_dentry, | |
751 | au_pinned_h_dir(&a->pin), | |
752 | &a->h_path, &delegated); | |
753 | if (unlikely(err == -EWOULDBLOCK)) { | |
754 | pr_warn("cannot retry" | |
755 | " for NFSv4 delegation" | |
756 | " for an internal link\n"); | |
757 | iput(delegated); | |
758 | } | |
759 | } | |
760 | } | |
761 | } | |
762 | if (unlikely(err)) | |
763 | goto out_unpin; | |
764 | ||
765 | if (wh_dentry) { | |
766 | a->h_path.dentry = wh_dentry; | |
767 | err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path, | |
768 | dentry); | |
769 | if (unlikely(err)) | |
770 | goto out_revert; | |
771 | } | |
772 | ||
773 | au_dir_ts(dir, a->bdst); | |
774 | dir->i_version++; | |
775 | inc_nlink(inode); | |
776 | inode->i_ctime = dir->i_ctime; | |
777 | d_instantiate(dentry, au_igrab(inode)); | |
778 | if (d_unhashed(a->h_path.dentry)) | |
779 | /* some filesystem calls d_drop() */ | |
780 | d_drop(dentry); | |
781 | /* some filesystems consume an inode even hardlink */ | |
782 | au_fhsm_wrote(sb, a->bdst, /*force*/0); | |
783 | goto out_unpin; /* success */ | |
784 | ||
785 | out_revert: | |
786 | /* no delegation since it is just created */ | |
787 | rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path, | |
788 | /*delegated*/NULL, /*force*/0); | |
789 | if (unlikely(rerr)) { | |
790 | AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr); | |
791 | err = -EIO; | |
792 | } | |
793 | au_dtime_revert(&dt); | |
794 | out_unpin: | |
795 | au_unpin(&a->pin); | |
796 | out_wh: | |
797 | dput(wh_dentry); | |
798 | out_parent: | |
799 | di_write_unlock(a->parent); | |
800 | dput(a->src_parent); | |
801 | out_unlock: | |
802 | if (unlikely(err)) { | |
803 | au_update_dbtop(dentry); | |
804 | d_drop(dentry); | |
805 | } | |
806 | aufs_read_and_write_unlock2(dentry, src_dentry); | |
807 | out_kfree: | |
808 | kfree(a); | |
809 | out: | |
810 | AuTraceErr(err); | |
811 | return err; | |
812 | } | |
813 | ||
814 | int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) | |
815 | { | |
816 | int err, rerr; | |
817 | aufs_bindex_t bindex; | |
818 | unsigned char diropq; | |
819 | struct path h_path; | |
820 | struct dentry *wh_dentry, *parent, *opq_dentry; | |
821 | struct inode *h_inode; | |
822 | struct super_block *sb; | |
823 | struct { | |
824 | struct au_pin pin; | |
825 | struct au_dtime dt; | |
826 | } *a; /* reduce the stack usage */ | |
827 | struct au_wr_dir_args wr_dir_args = { | |
828 | .force_btgt = -1, | |
829 | .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR | |
830 | }; | |
831 | ||
832 | IMustLock(dir); | |
833 | ||
834 | err = -ENOMEM; | |
835 | a = kmalloc(sizeof(*a), GFP_NOFS); | |
836 | if (unlikely(!a)) | |
837 | goto out; | |
838 | ||
839 | err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN); | |
840 | if (unlikely(err)) | |
841 | goto out_free; | |
842 | err = au_d_may_add(dentry); | |
843 | if (unlikely(err)) | |
844 | goto out_unlock; | |
845 | ||
846 | parent = dentry->d_parent; /* dir inode is locked */ | |
847 | di_write_lock_parent(parent); | |
848 | wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL, | |
849 | &a->pin, &wr_dir_args); | |
850 | err = PTR_ERR(wh_dentry); | |
851 | if (IS_ERR(wh_dentry)) | |
852 | goto out_parent; | |
853 | ||
854 | sb = dentry->d_sb; | |
855 | bindex = au_dbtop(dentry); | |
856 | h_path.dentry = au_h_dptr(dentry, bindex); | |
857 | h_path.mnt = au_sbr_mnt(sb, bindex); | |
858 | err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode); | |
859 | if (unlikely(err)) | |
860 | goto out_unpin; | |
861 | ||
862 | /* make the dir opaque */ | |
863 | diropq = 0; | |
864 | h_inode = d_inode(h_path.dentry); | |
865 | if (wh_dentry | |
866 | || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) { | |
867 | inode_lock_nested(h_inode, AuLsc_I_CHILD); | |
868 | opq_dentry = au_diropq_create(dentry, bindex); | |
869 | inode_unlock(h_inode); | |
870 | err = PTR_ERR(opq_dentry); | |
871 | if (IS_ERR(opq_dentry)) | |
872 | goto out_dir; | |
873 | dput(opq_dentry); | |
874 | diropq = 1; | |
875 | } | |
876 | ||
877 | err = epilog(dir, bindex, wh_dentry, dentry); | |
878 | if (!err) { | |
879 | inc_nlink(dir); | |
880 | goto out_unpin; /* success */ | |
881 | } | |
882 | ||
883 | /* revert */ | |
884 | if (diropq) { | |
885 | AuLabel(revert opq); | |
886 | inode_lock_nested(h_inode, AuLsc_I_CHILD); | |
887 | rerr = au_diropq_remove(dentry, bindex); | |
888 | inode_unlock(h_inode); | |
889 | if (rerr) { | |
890 | AuIOErr("%pd reverting diropq failed(%d, %d)\n", | |
891 | dentry, err, rerr); | |
892 | err = -EIO; | |
893 | } | |
894 | } | |
895 | ||
896 | out_dir: | |
897 | AuLabel(revert dir); | |
898 | rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path); | |
899 | if (rerr) { | |
900 | AuIOErr("%pd reverting dir failed(%d, %d)\n", | |
901 | dentry, err, rerr); | |
902 | err = -EIO; | |
903 | } | |
904 | au_dtime_revert(&a->dt); | |
905 | out_unpin: | |
906 | au_unpin(&a->pin); | |
907 | dput(wh_dentry); | |
908 | out_parent: | |
909 | di_write_unlock(parent); | |
910 | out_unlock: | |
911 | if (unlikely(err)) { | |
912 | au_update_dbtop(dentry); | |
913 | d_drop(dentry); | |
914 | } | |
915 | aufs_read_unlock(dentry, AuLock_DW); | |
916 | out_free: | |
917 | kfree(a); | |
918 | out: | |
919 | return err; | |
920 | } |