1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2005-2019 Junjiro R. Okajima
5 * This program, aufs is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 * the pseudo-link maintenance mode.
27 * during a user process maintains the pseudo-links,
28 * prohibit adding a new plink and branch manipulation.
32 * For entry functions which will handle plink, and i_mutex is already held
34 * They cannot wait and should return an error at once.
35 * Callers has to check the error.
37 * For entry functions which will handle plink, but i_mutex is not held
39 * They can wait the plink maintenance mode to finish.
41 * They behave like F_SETLK and F_SETLKW.
42 * If the caller never handle plink, then both flags are unnecessary.
45 int au_plink_maint(struct super_block
*sb
, int flags
)
49 struct task_struct
*parent
, *prev
;
50 struct au_sbinfo
*sbi
;
55 if (!au_opt_test(au_mntflags(sb
), PLINK
))
59 pid
= sbi
->si_plink_maint_pid
;
60 if (!pid
|| pid
== current
->pid
)
63 /* todo: it highly depends upon /sbin/mount.aufs */
69 parent
= rcu_dereference(parent
->real_parent
);
72 ppid
= task_pid_vnr(parent
);
81 if (au_ftest_lock(flags
, NOPLMW
)) {
82 /* if there is no i_mutex lock in VFS, we don't need to wait */
83 /* AuDebugOn(!lockdep_depth(current)); */
84 while (sbi
->si_plink_maint_pid
) {
86 /* gave up wake_up_bit() */
87 wait_event(sbi
->si_plink_wq
, !sbi
->si_plink_maint_pid
);
89 if (au_ftest_lock(flags
, FLUSH
))
90 au_nwt_flush(&sbi
->si_nowait
);
91 si_noflush_read_lock(sb
);
93 } else if (au_ftest_lock(flags
, NOPLM
)) {
94 AuDbg("ppid %d, pid %d\n", ppid
, pid
);
102 void au_plink_maint_leave(struct au_sbinfo
*sbinfo
)
104 spin_lock(&sbinfo
->si_plink_maint_lock
);
105 sbinfo
->si_plink_maint_pid
= 0;
106 spin_unlock(&sbinfo
->si_plink_maint_lock
);
107 wake_up_all(&sbinfo
->si_plink_wq
);
110 int au_plink_maint_enter(struct super_block
*sb
)
113 struct au_sbinfo
*sbinfo
;
117 /* make sure i am the only one in this fs */
118 si_write_lock(sb
, AuLock_FLUSH
);
119 if (au_opt_test(au_mntflags(sb
), PLINK
)) {
120 spin_lock(&sbinfo
->si_plink_maint_lock
);
121 if (!sbinfo
->si_plink_maint_pid
)
122 sbinfo
->si_plink_maint_pid
= current
->pid
;
125 spin_unlock(&sbinfo
->si_plink_maint_lock
);
132 /* ---------------------------------------------------------------------- */
134 #ifdef CONFIG_AUFS_DEBUG
135 void au_plink_list(struct super_block
*sb
)
138 struct au_sbinfo
*sbinfo
;
139 struct hlist_bl_head
*hbl
;
140 struct hlist_bl_node
*pos
;
141 struct au_icntnr
*icntnr
;
146 AuDebugOn(!au_opt_test(au_mntflags(sb
), PLINK
));
147 AuDebugOn(au_plink_maint(sb
, AuLock_NOPLM
));
149 for (i
= 0; i
< AuPlink_NHASH
; i
++) {
150 hbl
= sbinfo
->si_plink
+ i
;
152 hlist_bl_for_each_entry(icntnr
, pos
, hbl
, plink
)
153 AuDbg("%lu\n", icntnr
->vfs_inode
.i_ino
);
154 hlist_bl_unlock(hbl
);
159 /* is the inode pseudo-linked? */
160 int au_plink_test(struct inode
*inode
)
163 struct au_sbinfo
*sbinfo
;
164 struct hlist_bl_head
*hbl
;
165 struct hlist_bl_node
*pos
;
166 struct au_icntnr
*icntnr
;
168 sbinfo
= au_sbi(inode
->i_sb
);
169 AuRwMustAnyLock(&sbinfo
->si_rwsem
);
170 AuDebugOn(!au_opt_test(au_mntflags(inode
->i_sb
), PLINK
));
171 AuDebugOn(au_plink_maint(inode
->i_sb
, AuLock_NOPLM
));
174 i
= au_plink_hash(inode
->i_ino
);
175 hbl
= sbinfo
->si_plink
+ i
;
177 hlist_bl_for_each_entry(icntnr
, pos
, hbl
, plink
)
178 if (&icntnr
->vfs_inode
== inode
) {
182 hlist_bl_unlock(hbl
);
186 /* ---------------------------------------------------------------------- */
189 * generate a name for plink.
190 * the file will be stored under AUFS_WH_PLINKDIR.
192 /* 20 is max digits length of ulong 64 */
193 #define PLINK_NAME_LEN ((20 + 1) * 2)
195 static int plink_name(char *name
, int len
, struct inode
*inode
,
196 aufs_bindex_t bindex
)
199 struct inode
*h_inode
;
201 h_inode
= au_h_iptr(inode
, bindex
);
202 rlen
= snprintf(name
, len
, "%lu.%lu", inode
->i_ino
, h_inode
->i_ino
);
206 struct au_do_plink_lkup_args
{
207 struct dentry
**errp
;
208 struct qstr
*tgtname
;
209 struct dentry
*h_parent
;
210 struct au_branch
*br
;
213 static struct dentry
*au_do_plink_lkup(struct qstr
*tgtname
,
214 struct dentry
*h_parent
,
215 struct au_branch
*br
)
217 struct dentry
*h_dentry
;
218 struct inode
*h_inode
;
220 h_inode
= d_inode(h_parent
);
221 inode_lock_shared_nested(h_inode
, AuLsc_I_CHILD2
);
222 h_dentry
= vfsub_lkup_one(tgtname
, h_parent
);
223 inode_unlock_shared(h_inode
);
227 static void au_call_do_plink_lkup(void *args
)
229 struct au_do_plink_lkup_args
*a
= args
;
230 *a
->errp
= au_do_plink_lkup(a
->tgtname
, a
->h_parent
, a
->br
);
233 /* lookup the plink-ed @inode under the branch at @bindex */
234 struct dentry
*au_plink_lkup(struct inode
*inode
, aufs_bindex_t bindex
)
236 struct dentry
*h_dentry
, *h_parent
;
237 struct au_branch
*br
;
239 char a
[PLINK_NAME_LEN
];
240 struct qstr tgtname
= QSTR_INIT(a
, 0);
242 AuDebugOn(au_plink_maint(inode
->i_sb
, AuLock_NOPLM
));
244 br
= au_sbr(inode
->i_sb
, bindex
);
245 h_parent
= br
->br_wbr
->wbr_plink
;
246 tgtname
.len
= plink_name(a
, sizeof(a
), inode
, bindex
);
248 if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID
)) {
249 struct au_do_plink_lkup_args args
= {
252 .h_parent
= h_parent
,
256 wkq_err
= au_wkq_wait(au_call_do_plink_lkup
, &args
);
257 if (unlikely(wkq_err
))
258 h_dentry
= ERR_PTR(wkq_err
);
260 h_dentry
= au_do_plink_lkup(&tgtname
, h_parent
, br
);
265 /* create a pseudo-link */
266 static int do_whplink(struct qstr
*tgt
, struct dentry
*h_parent
,
267 struct dentry
*h_dentry
, struct au_branch
*br
)
270 struct path h_path
= {
273 struct inode
*h_dir
, *delegated
;
275 h_dir
= d_inode(h_parent
);
276 inode_lock_nested(h_dir
, AuLsc_I_CHILD2
);
278 h_path
.dentry
= vfsub_lkup_one(tgt
, h_parent
);
279 err
= PTR_ERR(h_path
.dentry
);
280 if (IS_ERR(h_path
.dentry
))
284 /* wh.plink dir is not monitored */
285 /* todo: is it really safe? */
286 if (d_is_positive(h_path
.dentry
)
287 && d_inode(h_path
.dentry
) != d_inode(h_dentry
)) {
289 err
= vfsub_unlink(h_dir
, &h_path
, &delegated
, /*force*/0);
290 if (unlikely(err
== -EWOULDBLOCK
)) {
291 pr_warn("cannot retry for NFSv4 delegation"
292 " for an internal unlink\n");
296 h_path
.dentry
= NULL
;
300 if (!err
&& d_is_negative(h_path
.dentry
)) {
302 err
= vfsub_link(h_dentry
, h_dir
, &h_path
, &delegated
);
303 if (unlikely(err
== -EWOULDBLOCK
)) {
304 pr_warn("cannot retry for NFSv4 delegation"
305 " for an internal link\n");
316 struct do_whplink_args
{
319 struct dentry
*h_parent
;
320 struct dentry
*h_dentry
;
321 struct au_branch
*br
;
324 static void call_do_whplink(void *args
)
326 struct do_whplink_args
*a
= args
;
327 *a
->errp
= do_whplink(a
->tgt
, a
->h_parent
, a
->h_dentry
, a
->br
);
330 static int whplink(struct dentry
*h_dentry
, struct inode
*inode
,
331 aufs_bindex_t bindex
, struct au_branch
*br
)
335 struct dentry
*h_parent
;
336 char a
[PLINK_NAME_LEN
];
337 struct qstr tgtname
= QSTR_INIT(a
, 0);
339 wbr
= au_sbr(inode
->i_sb
, bindex
)->br_wbr
;
340 h_parent
= wbr
->wbr_plink
;
341 tgtname
.len
= plink_name(a
, sizeof(a
), inode
, bindex
);
343 /* always superio. */
344 if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID
)) {
345 struct do_whplink_args args
= {
348 .h_parent
= h_parent
,
349 .h_dentry
= h_dentry
,
352 wkq_err
= au_wkq_wait(call_do_whplink
, &args
);
353 if (unlikely(wkq_err
))
356 err
= do_whplink(&tgtname
, h_parent
, h_dentry
, br
);
362 * create a new pseudo-link for @h_dentry on @bindex.
363 * the linked inode is held in aufs @inode.
365 void au_plink_append(struct inode
*inode
, aufs_bindex_t bindex
,
366 struct dentry
*h_dentry
)
368 struct super_block
*sb
;
369 struct au_sbinfo
*sbinfo
;
370 struct hlist_bl_head
*hbl
;
371 struct hlist_bl_node
*pos
;
372 struct au_icntnr
*icntnr
;
373 int found
, err
, cnt
, i
;
377 AuDebugOn(!au_opt_test(au_mntflags(sb
), PLINK
));
378 AuDebugOn(au_plink_maint(sb
, AuLock_NOPLM
));
380 found
= au_plink_test(inode
);
384 i
= au_plink_hash(inode
->i_ino
);
385 hbl
= sbinfo
->si_plink
+ i
;
389 hlist_bl_for_each_entry(icntnr
, pos
, hbl
, plink
) {
390 if (&icntnr
->vfs_inode
== inode
) {
396 icntnr
= container_of(inode
, struct au_icntnr
, vfs_inode
);
397 hlist_bl_add_head(&icntnr
->plink
, hbl
);
399 hlist_bl_unlock(hbl
);
401 cnt
= au_hbl_count(hbl
);
402 #define msg "unexpectedly unbalanced or too many pseudo-links"
403 if (cnt
> AUFS_PLINK_WARN
)
404 AuWarn1(msg
", %d\n", cnt
);
406 err
= whplink(h_dentry
, inode
, bindex
, au_sbr(sb
, bindex
));
408 pr_warn("err %d, damaged pseudo link.\n", err
);
409 au_hbl_del(&icntnr
->plink
, hbl
);
410 iput(&icntnr
->vfs_inode
);
413 iput(&icntnr
->vfs_inode
);
416 /* free all plinks */
417 void au_plink_put(struct super_block
*sb
, int verbose
)
420 struct au_sbinfo
*sbinfo
;
421 struct hlist_bl_head
*hbl
;
422 struct hlist_bl_node
*pos
, *tmp
;
423 struct au_icntnr
*icntnr
;
428 AuDebugOn(!au_opt_test(au_mntflags(sb
), PLINK
));
429 AuDebugOn(au_plink_maint(sb
, AuLock_NOPLM
));
431 /* no spin_lock since sbinfo is write-locked */
433 for (i
= 0; i
< AuPlink_NHASH
; i
++) {
434 hbl
= sbinfo
->si_plink
+ i
;
435 if (!warned
&& verbose
&& !hlist_bl_empty(hbl
)) {
436 pr_warn("pseudo-link is not flushed");
439 hlist_bl_for_each_entry_safe(icntnr
, pos
, tmp
, hbl
, plink
)
440 iput(&icntnr
->vfs_inode
);
441 INIT_HLIST_BL_HEAD(hbl
);
445 void au_plink_clean(struct super_block
*sb
, int verbose
)
450 aufs_write_lock(root
);
451 if (au_opt_test(au_mntflags(sb
), PLINK
))
452 au_plink_put(sb
, verbose
);
453 aufs_write_unlock(root
);
456 static int au_plink_do_half_refresh(struct inode
*inode
, aufs_bindex_t br_id
)
459 aufs_bindex_t btop
, bbot
, bindex
;
462 btop
= au_ibtop(inode
);
463 bbot
= au_ibbot(inode
);
465 for (bindex
= btop
; bindex
<= bbot
; bindex
++) {
466 if (!au_h_iptr(inode
, bindex
)
467 || au_ii_br_id(inode
, bindex
) != br_id
)
469 au_set_h_iptr(inode
, bindex
, NULL
, 0);
474 for (bindex
= btop
; bindex
<= bbot
; bindex
++)
475 if (au_h_iptr(inode
, bindex
)) {
485 /* free the plinks on a branch specified by @br_id */
486 void au_plink_half_refresh(struct super_block
*sb
, aufs_bindex_t br_id
)
488 struct au_sbinfo
*sbinfo
;
489 struct hlist_bl_head
*hbl
;
490 struct hlist_bl_node
*pos
, *tmp
;
491 struct au_icntnr
*icntnr
;
498 AuDebugOn(!au_opt_test(au_mntflags(sb
), PLINK
));
499 AuDebugOn(au_plink_maint(sb
, AuLock_NOPLM
));
501 /* no bit_lock since sbinfo is write-locked */
502 for (i
= 0; i
< AuPlink_NHASH
; i
++) {
503 hbl
= sbinfo
->si_plink
+ i
;
504 hlist_bl_for_each_entry_safe(icntnr
, pos
, tmp
, hbl
, plink
) {
505 inode
= au_igrab(&icntnr
->vfs_inode
);
506 ii_write_lock_child(inode
);
507 do_put
= au_plink_do_half_refresh(inode
, br_id
);
509 hlist_bl_del(&icntnr
->plink
);
512 ii_write_unlock(inode
);