2 * Copyright (C) 2005-2016 Junjiro R. Okajima
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.
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.
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/>.
25 * the pseudo-link maintenance mode.
26 * during a user process maintains the pseudo-links,
27 * prohibit adding a new plink and branch manipulation.
31 * For entry functions which will handle plink, and i_mutex is already held
33 * They cannot wait and should return an error at once.
34 * Callers has to check the error.
36 * For entry functions which will handle plink, but i_mutex is not held
38 * They can wait the plink maintenance mode to finish.
40 * They behave like F_SETLK and F_SETLKW.
41 * If the caller never handle plink, then both flags are unnecessary.
44 int au_plink_maint(struct super_block
*sb
, int flags
)
48 struct task_struct
*parent
, *prev
;
49 struct au_sbinfo
*sbi
;
54 if (!au_opt_test(au_mntflags(sb
), PLINK
))
58 pid
= sbi
->si_plink_maint_pid
;
59 if (!pid
|| pid
== current
->pid
)
62 /* todo: it highly depends upon /sbin/mount.aufs */
68 parent
= rcu_dereference(parent
->real_parent
);
71 ppid
= task_pid_vnr(parent
);
80 if (au_ftest_lock(flags
, NOPLMW
)) {
81 /* if there is no i_mutex lock in VFS, we don't need to wait */
82 /* AuDebugOn(!lockdep_depth(current)); */
83 while (sbi
->si_plink_maint_pid
) {
85 /* gave up wake_up_bit() */
86 wait_event(sbi
->si_plink_wq
, !sbi
->si_plink_maint_pid
);
88 if (au_ftest_lock(flags
, FLUSH
))
89 au_nwt_flush(&sbi
->si_nowait
);
90 si_noflush_read_lock(sb
);
92 } else if (au_ftest_lock(flags
, NOPLM
)) {
93 AuDbg("ppid %d, pid %d\n", ppid
, pid
);
101 void au_plink_maint_leave(struct au_sbinfo
*sbinfo
)
103 spin_lock(&sbinfo
->si_plink_maint_lock
);
104 sbinfo
->si_plink_maint_pid
= 0;
105 spin_unlock(&sbinfo
->si_plink_maint_lock
);
106 wake_up_all(&sbinfo
->si_plink_wq
);
109 int au_plink_maint_enter(struct super_block
*sb
)
112 struct au_sbinfo
*sbinfo
;
116 /* make sure i am the only one in this fs */
117 si_write_lock(sb
, AuLock_FLUSH
);
118 if (au_opt_test(au_mntflags(sb
), PLINK
)) {
119 spin_lock(&sbinfo
->si_plink_maint_lock
);
120 if (!sbinfo
->si_plink_maint_pid
)
121 sbinfo
->si_plink_maint_pid
= current
->pid
;
124 spin_unlock(&sbinfo
->si_plink_maint_lock
);
131 /* ---------------------------------------------------------------------- */
133 #ifdef CONFIG_AUFS_DEBUG
134 void au_plink_list(struct super_block
*sb
)
137 struct au_sbinfo
*sbinfo
;
138 struct hlist_head
*plink_hlist
;
139 struct au_icntnr
*icntnr
;
144 AuDebugOn(!au_opt_test(au_mntflags(sb
), PLINK
));
145 AuDebugOn(au_plink_maint(sb
, AuLock_NOPLM
));
147 for (i
= 0; i
< AuPlink_NHASH
; i
++) {
148 plink_hlist
= &sbinfo
->si_plink
[i
].head
;
150 hlist_for_each_entry_rcu(icntnr
, plink_hlist
, plink
)
151 AuDbg("%lu\n", icntnr
->vfs_inode
.i_ino
);
157 /* is the inode pseudo-linked? */
158 int au_plink_test(struct inode
*inode
)
161 struct au_sbinfo
*sbinfo
;
162 struct hlist_head
*plink_hlist
;
163 struct au_icntnr
*icntnr
;
165 sbinfo
= au_sbi(inode
->i_sb
);
166 AuRwMustAnyLock(&sbinfo
->si_rwsem
);
167 AuDebugOn(!au_opt_test(au_mntflags(inode
->i_sb
), PLINK
));
168 AuDebugOn(au_plink_maint(inode
->i_sb
, AuLock_NOPLM
));
171 i
= au_plink_hash(inode
->i_ino
);
172 plink_hlist
= &sbinfo
->si_plink
[i
].head
;
174 hlist_for_each_entry_rcu(icntnr
, plink_hlist
, plink
)
175 if (&icntnr
->vfs_inode
== inode
) {
183 /* ---------------------------------------------------------------------- */
186 * generate a name for plink.
187 * the file will be stored under AUFS_WH_PLINKDIR.
189 /* 20 is max digits length of ulong 64 */
190 #define PLINK_NAME_LEN ((20 + 1) * 2)
192 static int plink_name(char *name
, int len
, struct inode
*inode
,
193 aufs_bindex_t bindex
)
196 struct inode
*h_inode
;
198 h_inode
= au_h_iptr(inode
, bindex
);
199 rlen
= snprintf(name
, len
, "%lu.%lu", inode
->i_ino
, h_inode
->i_ino
);
203 struct au_do_plink_lkup_args
{
204 struct dentry
**errp
;
205 struct qstr
*tgtname
;
206 struct dentry
*h_parent
;
207 struct au_branch
*br
;
210 static struct dentry
*au_do_plink_lkup(struct qstr
*tgtname
,
211 struct dentry
*h_parent
,
212 struct au_branch
*br
)
214 struct dentry
*h_dentry
;
215 struct inode
*h_inode
;
217 h_inode
= d_inode(h_parent
);
218 inode_lock_nested(h_inode
, AuLsc_I_CHILD2
);
219 h_dentry
= vfsub_lkup_one(tgtname
, h_parent
);
220 inode_unlock(h_inode
);
224 static void au_call_do_plink_lkup(void *args
)
226 struct au_do_plink_lkup_args
*a
= args
;
227 *a
->errp
= au_do_plink_lkup(a
->tgtname
, a
->h_parent
, a
->br
);
230 /* lookup the plink-ed @inode under the branch at @bindex */
231 struct dentry
*au_plink_lkup(struct inode
*inode
, aufs_bindex_t bindex
)
233 struct dentry
*h_dentry
, *h_parent
;
234 struct au_branch
*br
;
236 char a
[PLINK_NAME_LEN
];
237 struct qstr tgtname
= QSTR_INIT(a
, 0);
239 AuDebugOn(au_plink_maint(inode
->i_sb
, AuLock_NOPLM
));
241 br
= au_sbr(inode
->i_sb
, bindex
);
242 h_parent
= br
->br_wbr
->wbr_plink
;
243 tgtname
.len
= plink_name(a
, sizeof(a
), inode
, bindex
);
245 if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID
)) {
246 struct au_do_plink_lkup_args args
= {
249 .h_parent
= h_parent
,
253 wkq_err
= au_wkq_wait(au_call_do_plink_lkup
, &args
);
254 if (unlikely(wkq_err
))
255 h_dentry
= ERR_PTR(wkq_err
);
257 h_dentry
= au_do_plink_lkup(&tgtname
, h_parent
, br
);
262 /* create a pseudo-link */
263 static int do_whplink(struct qstr
*tgt
, struct dentry
*h_parent
,
264 struct dentry
*h_dentry
, struct au_branch
*br
)
267 struct path h_path
= {
270 struct inode
*h_dir
, *delegated
;
272 h_dir
= d_inode(h_parent
);
273 inode_lock_nested(h_dir
, AuLsc_I_CHILD2
);
275 h_path
.dentry
= vfsub_lkup_one(tgt
, h_parent
);
276 err
= PTR_ERR(h_path
.dentry
);
277 if (IS_ERR(h_path
.dentry
))
281 /* wh.plink dir is not monitored */
282 /* todo: is it really safe? */
283 if (d_is_positive(h_path
.dentry
)
284 && d_inode(h_path
.dentry
) != d_inode(h_dentry
)) {
286 err
= vfsub_unlink(h_dir
, &h_path
, &delegated
, /*force*/0);
287 if (unlikely(err
== -EWOULDBLOCK
)) {
288 pr_warn("cannot retry for NFSv4 delegation"
289 " for an internal unlink\n");
293 h_path
.dentry
= NULL
;
297 if (!err
&& d_is_negative(h_path
.dentry
)) {
299 err
= vfsub_link(h_dentry
, h_dir
, &h_path
, &delegated
);
300 if (unlikely(err
== -EWOULDBLOCK
)) {
301 pr_warn("cannot retry for NFSv4 delegation"
302 " for an internal link\n");
313 struct do_whplink_args
{
316 struct dentry
*h_parent
;
317 struct dentry
*h_dentry
;
318 struct au_branch
*br
;
321 static void call_do_whplink(void *args
)
323 struct do_whplink_args
*a
= args
;
324 *a
->errp
= do_whplink(a
->tgt
, a
->h_parent
, a
->h_dentry
, a
->br
);
327 static int whplink(struct dentry
*h_dentry
, struct inode
*inode
,
328 aufs_bindex_t bindex
, struct au_branch
*br
)
332 struct dentry
*h_parent
;
333 char a
[PLINK_NAME_LEN
];
334 struct qstr tgtname
= QSTR_INIT(a
, 0);
336 wbr
= au_sbr(inode
->i_sb
, bindex
)->br_wbr
;
337 h_parent
= wbr
->wbr_plink
;
338 tgtname
.len
= plink_name(a
, sizeof(a
), inode
, bindex
);
340 /* always superio. */
341 if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID
)) {
342 struct do_whplink_args args
= {
345 .h_parent
= h_parent
,
346 .h_dentry
= h_dentry
,
349 wkq_err
= au_wkq_wait(call_do_whplink
, &args
);
350 if (unlikely(wkq_err
))
353 err
= do_whplink(&tgtname
, h_parent
, h_dentry
, br
);
359 * create a new pseudo-link for @h_dentry on @bindex.
360 * the linked inode is held in aufs @inode.
362 void au_plink_append(struct inode
*inode
, aufs_bindex_t bindex
,
363 struct dentry
*h_dentry
)
365 struct super_block
*sb
;
366 struct au_sbinfo
*sbinfo
;
367 struct hlist_head
*plink_hlist
;
368 struct au_icntnr
*icntnr
;
369 struct au_sphlhead
*sphl
;
370 int found
, err
, cnt
, i
;
374 AuDebugOn(!au_opt_test(au_mntflags(sb
), PLINK
));
375 AuDebugOn(au_plink_maint(sb
, AuLock_NOPLM
));
377 found
= au_plink_test(inode
);
381 i
= au_plink_hash(inode
->i_ino
);
382 sphl
= sbinfo
->si_plink
+ i
;
383 plink_hlist
= &sphl
->head
;
386 spin_lock(&sphl
->spin
);
387 hlist_for_each_entry(icntnr
, plink_hlist
, plink
) {
388 if (&icntnr
->vfs_inode
== inode
) {
394 icntnr
= container_of(inode
, struct au_icntnr
, vfs_inode
);
395 hlist_add_head_rcu(&icntnr
->plink
, plink_hlist
);
397 spin_unlock(&sphl
->spin
);
399 cnt
= au_sphl_count(sphl
);
400 #define msg "unexpectedly unblanced or too many pseudo-links"
401 if (cnt
> AUFS_PLINK_WARN
)
402 AuWarn1(msg
", %d\n", cnt
);
404 err
= whplink(h_dentry
, inode
, bindex
, au_sbr(sb
, bindex
));
406 pr_warn("err %d, damaged pseudo link.\n", err
);
407 au_sphl_del_rcu(&icntnr
->plink
, sphl
);
408 iput(&icntnr
->vfs_inode
);
411 iput(&icntnr
->vfs_inode
);
414 /* free all plinks */
415 void au_plink_put(struct super_block
*sb
, int verbose
)
418 struct au_sbinfo
*sbinfo
;
419 struct hlist_head
*plink_hlist
;
420 struct hlist_node
*tmp
;
421 struct au_icntnr
*icntnr
;
426 AuDebugOn(!au_opt_test(au_mntflags(sb
), PLINK
));
427 AuDebugOn(au_plink_maint(sb
, AuLock_NOPLM
));
429 /* no spin_lock since sbinfo is write-locked */
431 for (i
= 0; i
< AuPlink_NHASH
; i
++) {
432 plink_hlist
= &sbinfo
->si_plink
[i
].head
;
433 if (!warned
&& verbose
&& !hlist_empty(plink_hlist
)) {
434 pr_warn("pseudo-link is not flushed");
437 hlist_for_each_entry_safe(icntnr
, tmp
, plink_hlist
, plink
)
438 iput(&icntnr
->vfs_inode
);
439 INIT_HLIST_HEAD(plink_hlist
);
443 void au_plink_clean(struct super_block
*sb
, int verbose
)
448 aufs_write_lock(root
);
449 if (au_opt_test(au_mntflags(sb
), PLINK
))
450 au_plink_put(sb
, verbose
);
451 aufs_write_unlock(root
);
454 static int au_plink_do_half_refresh(struct inode
*inode
, aufs_bindex_t br_id
)
457 aufs_bindex_t btop
, bbot
, bindex
;
460 btop
= au_ibtop(inode
);
461 bbot
= au_ibbot(inode
);
463 for (bindex
= btop
; bindex
<= bbot
; bindex
++) {
464 if (!au_h_iptr(inode
, bindex
)
465 || au_ii_br_id(inode
, bindex
) != br_id
)
467 au_set_h_iptr(inode
, bindex
, NULL
, 0);
472 for (bindex
= btop
; bindex
<= bbot
; bindex
++)
473 if (au_h_iptr(inode
, bindex
)) {
483 /* free the plinks on a branch specified by @br_id */
484 void au_plink_half_refresh(struct super_block
*sb
, aufs_bindex_t br_id
)
486 struct au_sbinfo
*sbinfo
;
487 struct hlist_head
*plink_hlist
;
488 struct hlist_node
*tmp
;
489 struct au_icntnr
*icntnr
;
496 AuDebugOn(!au_opt_test(au_mntflags(sb
), PLINK
));
497 AuDebugOn(au_plink_maint(sb
, AuLock_NOPLM
));
499 /* no spin_lock since sbinfo is write-locked */
500 for (i
= 0; i
< AuPlink_NHASH
; i
++) {
501 plink_hlist
= &sbinfo
->si_plink
[i
].head
;
502 hlist_for_each_entry_safe(icntnr
, tmp
, plink_hlist
, plink
) {
503 inode
= au_igrab(&icntnr
->vfs_inode
);
504 ii_write_lock_child(inode
);
505 do_put
= au_plink_do_half_refresh(inode
, br_id
);
507 hlist_del(&icntnr
->plink
);
510 ii_write_unlock(inode
);