2 * Copyright (C) 2005-2013 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, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 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 */
64 ppid = task_pid_vnr(rcu_dereference(current->real_parent));
69 if (au_ftest_lock(flags, NOPLMW)) {
70 /* if there is no i_mutex lock in VFS, we don't need to wait */
71 /* AuDebugOn(!lockdep_depth(current)); */
72 while (sbi->si_plink_maint_pid) {
74 /* gave up wake_up_bit() */
75 wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
77 if (au_ftest_lock(flags, FLUSH))
78 au_nwt_flush(&sbi->si_nowait);
79 si_noflush_read_lock(sb);
81 } else if (au_ftest_lock(flags, NOPLM)) {
82 AuDbg("ppid %d, pid %d\n", ppid, pid);
90 void au_plink_maint_leave(struct au_sbinfo *sbinfo)
92 spin_lock(&sbinfo->si_plink_maint_lock);
93 sbinfo->si_plink_maint_pid = 0;
94 spin_unlock(&sbinfo->si_plink_maint_lock);
95 wake_up_all(&sbinfo->si_plink_wq);
98 int au_plink_maint_enter(struct super_block *sb)
101 struct au_sbinfo *sbinfo;
105 /* make sure i am the only one in this fs */
106 si_write_lock(sb, AuLock_FLUSH);
107 if (au_opt_test(au_mntflags(sb), PLINK)) {
108 spin_lock(&sbinfo->si_plink_maint_lock);
109 if (!sbinfo->si_plink_maint_pid)
110 sbinfo->si_plink_maint_pid = current->pid;
113 spin_unlock(&sbinfo->si_plink_maint_lock);
120 /* ---------------------------------------------------------------------- */
122 #ifdef CONFIG_AUFS_DEBUG
123 void au_plink_list(struct super_block *sb)
126 struct au_sbinfo *sbinfo;
127 struct hlist_head *plink_hlist;
128 struct hlist_node *pos;
129 struct pseudo_link *plink;
134 AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
135 AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
137 for (i = 0; i < AuPlink_NHASH; i++) {
138 plink_hlist = &sbinfo->si_plink[i].head;
140 hlist_for_each_entry_rcu(plink, pos, plink_hlist, hlist)
141 AuDbg("%lu\n", plink->inode->i_ino);
147 /* is the inode pseudo-linked? */
148 int au_plink_test(struct inode *inode)
151 struct au_sbinfo *sbinfo;
152 struct hlist_head *plink_hlist;
153 struct hlist_node *pos;
154 struct pseudo_link *plink;
156 sbinfo = au_sbi(inode->i_sb);
157 AuRwMustAnyLock(&sbinfo->si_rwsem);
158 AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
159 AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
162 i = au_plink_hash(inode->i_ino);
163 plink_hlist = &sbinfo->si_plink[i].head;
165 hlist_for_each_entry_rcu(plink, pos, plink_hlist, hlist)
166 if (plink->inode == inode) {
174 /* ---------------------------------------------------------------------- */
177 * generate a name for plink.
178 * the file will be stored under AUFS_WH_PLINKDIR.
180 /* 20 is max digits length of ulong 64 */
181 #define PLINK_NAME_LEN ((20 + 1) * 2)
183 static int plink_name(char *name, int len, struct inode *inode,
184 aufs_bindex_t bindex)
187 struct inode *h_inode;
189 h_inode = au_h_iptr(inode, bindex);
190 rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
194 struct au_do_plink_lkup_args {
195 struct dentry **errp;
196 struct qstr *tgtname;
197 struct dentry *h_parent;
198 struct au_branch *br;
201 static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
202 struct dentry *h_parent,
203 struct au_branch *br)
205 struct dentry *h_dentry;
208 h_mtx = &h_parent->d_inode->i_mutex;
209 mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
210 h_dentry = au_lkup_one(tgtname, h_parent, br, /*nd*/NULL);
215 static void au_call_do_plink_lkup(void *args)
217 struct au_do_plink_lkup_args *a = args;
218 *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
221 /* lookup the plink-ed @inode under the branch at @bindex */
222 struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
224 struct dentry *h_dentry, *h_parent;
225 struct au_branch *br;
228 char a[PLINK_NAME_LEN];
229 struct qstr tgtname = {
233 AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
235 br = au_sbr(inode->i_sb, bindex);
236 h_parent = br->br_wbr->wbr_plink;
237 h_dir = h_parent->d_inode;
238 tgtname.len = plink_name(a, sizeof(a), inode, bindex);
240 if (current_fsuid()) {
241 struct au_do_plink_lkup_args args = {
244 .h_parent = h_parent,
248 wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
249 if (unlikely(wkq_err))
250 h_dentry = ERR_PTR(wkq_err);
252 h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
257 /* create a pseudo-link */
258 static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
259 struct dentry *h_dentry, struct au_branch *br)
262 struct path h_path = {
267 h_dir = h_parent->d_inode;
268 mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_CHILD2);
270 h_path.dentry = au_lkup_one(tgt, h_parent, br, /*nd*/NULL);
271 err = PTR_ERR(h_path.dentry);
272 if (IS_ERR(h_path.dentry))
276 /* wh.plink dir is not monitored */
277 /* todo: is it really safe? */
278 if (h_path.dentry->d_inode
279 && h_path.dentry->d_inode != h_dentry->d_inode) {
280 err = vfsub_unlink(h_dir, &h_path, /*force*/0);
282 h_path.dentry = NULL;
286 if (!err && !h_path.dentry->d_inode)
287 err = vfsub_link(h_dentry, h_dir, &h_path);
291 mutex_unlock(&h_dir->i_mutex);
295 struct do_whplink_args {
298 struct dentry *h_parent;
299 struct dentry *h_dentry;
300 struct au_branch *br;
303 static void call_do_whplink(void *args)
305 struct do_whplink_args *a = args;
306 *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
309 static int whplink(struct dentry *h_dentry, struct inode *inode,
310 aufs_bindex_t bindex, struct au_branch *br)
314 struct dentry *h_parent;
316 char a[PLINK_NAME_LEN];
317 struct qstr tgtname = {
321 wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
322 h_parent = wbr->wbr_plink;
323 h_dir = h_parent->d_inode;
324 tgtname.len = plink_name(a, sizeof(a), inode, bindex);
326 /* always superio. */
327 if (current_fsuid()) {
328 struct do_whplink_args args = {
331 .h_parent = h_parent,
332 .h_dentry = h_dentry,
335 wkq_err = au_wkq_wait(call_do_whplink, &args);
336 if (unlikely(wkq_err))
339 err = do_whplink(&tgtname, h_parent, h_dentry, br);
344 /* free a single plink */
345 static void do_put_plink(struct pseudo_link *plink, int do_del)
348 hlist_del(&plink->hlist);
353 static void do_put_plink_rcu(struct rcu_head *rcu)
355 struct pseudo_link *plink;
357 plink = container_of(rcu, struct pseudo_link, rcu);
363 * create a new pseudo-link for @h_dentry on @bindex.
364 * the linked inode is held in aufs @inode.
366 void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
367 struct dentry *h_dentry)
369 struct super_block *sb;
370 struct au_sbinfo *sbinfo;
371 struct hlist_head *plink_hlist;
372 struct hlist_node *pos;
373 struct pseudo_link *plink, *tmp;
374 struct au_sphlhead *sphl;
375 int found, err, cnt, i;
379 AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
380 AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
382 found = au_plink_test(inode);
386 i = au_plink_hash(inode->i_ino);
387 sphl = sbinfo->si_plink + i;
388 plink_hlist = &sphl->head;
389 tmp = kmalloc(sizeof(*plink), GFP_NOFS);
391 tmp->inode = au_igrab(inode);
397 spin_lock(&sphl->spin);
398 hlist_for_each_entry(plink, pos, plink_hlist, hlist) {
399 if (plink->inode == inode) {
405 hlist_add_head_rcu(&tmp->hlist, plink_hlist);
406 spin_unlock(&sphl->spin);
408 cnt = au_sphl_count(sphl);
409 #define msg "unexpectedly unblanced or too many pseudo-links"
410 if (cnt > AUFS_PLINK_WARN)
411 AuWarn1(msg ", %d\n", cnt);
413 err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
415 do_put_plink(tmp, 0);
421 pr_warn("err %d, damaged pseudo link.\n", err);
423 au_sphl_del_rcu(&tmp->hlist, sphl);
424 call_rcu(&tmp->rcu, do_put_plink_rcu);
429 /* free all plinks */
430 void au_plink_put(struct super_block *sb, int verbose)
433 struct au_sbinfo *sbinfo;
434 struct hlist_head *plink_hlist;
435 struct hlist_node *pos, *tmp;
436 struct pseudo_link *plink;
441 AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
442 AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
444 /* no spin_lock since sbinfo is write-locked */
446 for (i = 0; i < AuPlink_NHASH; i++) {
447 plink_hlist = &sbinfo->si_plink[i].head;
448 if (!warned && verbose && !hlist_empty(plink_hlist)) {
449 pr_warn("pseudo-link is not flushed");
452 hlist_for_each_entry_safe(plink, pos, tmp, plink_hlist, hlist)
453 do_put_plink(plink, 0);
454 INIT_HLIST_HEAD(plink_hlist);
458 void au_plink_clean(struct super_block *sb, int verbose)
463 aufs_write_lock(root);
464 if (au_opt_test(au_mntflags(sb), PLINK))
465 au_plink_put(sb, verbose);
466 aufs_write_unlock(root);
469 static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
472 aufs_bindex_t bstart, bend, bindex;
475 bstart = au_ibstart(inode);
476 bend = au_ibend(inode);
478 for (bindex = bstart; bindex <= bend; bindex++) {
479 if (!au_h_iptr(inode, bindex)
480 || au_ii_br_id(inode, bindex) != br_id)
482 au_set_h_iptr(inode, bindex, NULL, 0);
487 for (bindex = bstart; bindex <= bend; bindex++)
488 if (au_h_iptr(inode, bindex)) {
498 /* free the plinks on a branch specified by @br_id */
499 void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
501 struct au_sbinfo *sbinfo;
502 struct hlist_head *plink_hlist;
503 struct hlist_node *pos, *tmp;
504 struct pseudo_link *plink;
511 AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
512 AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
514 /* no spin_lock since sbinfo is write-locked */
515 for (i = 0; i < AuPlink_NHASH; i++) {
516 plink_hlist = &sbinfo->si_plink[i].head;
517 hlist_for_each_entry_safe(plink, pos, tmp, plink_hlist, hlist) {
518 inode = au_igrab(plink->inode);
519 ii_write_lock_child(inode);
520 do_put = au_plink_do_half_refresh(inode, br_id);
522 do_put_plink(plink, 1);
523 ii_write_unlock(inode);