aufs: fix up for the new stable kernel
[pandora-kernel.git] / fs / aufs / plink.c
1 /*
2  * Copyright (C) 2005-2013 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, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 /*
20  * pseudo-link
21  */
22
23 #include "aufs.h"
24
25 /*
26  * the pseudo-link maintenance mode.
27  * during a user process maintains the pseudo-links,
28  * prohibit adding a new plink and branch manipulation.
29  *
30  * Flags
31  * NOPLM:
32  *      For entry functions which will handle plink, and i_mutex is already held
33  *      in VFS.
34  *      They cannot wait and should return an error at once.
35  *      Callers has to check the error.
36  * NOPLMW:
37  *      For entry functions which will handle plink, but i_mutex is not held
38  *      in VFS.
39  *      They can wait the plink maintenance mode to finish.
40  *
41  * They behave like F_SETLK and F_SETLKW.
42  * If the caller never handle plink, then both flags are unnecessary.
43  */
44
45 int au_plink_maint(struct super_block *sb, int flags)
46 {
47         int err;
48         pid_t pid, ppid;
49         struct au_sbinfo *sbi;
50
51         SiMustAnyLock(sb);
52
53         err = 0;
54         if (!au_opt_test(au_mntflags(sb), PLINK))
55                 goto out;
56
57         sbi = au_sbi(sb);
58         pid = sbi->si_plink_maint_pid;
59         if (!pid || pid == current->pid)
60                 goto out;
61
62         /* todo: it highly depends upon /sbin/mount.aufs */
63         rcu_read_lock();
64         ppid = task_pid_vnr(rcu_dereference(current->real_parent));
65         rcu_read_unlock();
66         if (pid == ppid)
67                 goto out;
68
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) {
73                         si_read_unlock(sb);
74                         /* gave up wake_up_bit() */
75                         wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
76
77                         if (au_ftest_lock(flags, FLUSH))
78                                 au_nwt_flush(&sbi->si_nowait);
79                         si_noflush_read_lock(sb);
80                 }
81         } else if (au_ftest_lock(flags, NOPLM)) {
82                 AuDbg("ppid %d, pid %d\n", ppid, pid);
83                 err = -EAGAIN;
84         }
85
86 out:
87         return err;
88 }
89
90 void au_plink_maint_leave(struct au_sbinfo *sbinfo)
91 {
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);
96 }
97
98 int au_plink_maint_enter(struct super_block *sb)
99 {
100         int err;
101         struct au_sbinfo *sbinfo;
102
103         err = 0;
104         sbinfo = au_sbi(sb);
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;
111                 else
112                         err = -EBUSY;
113                 spin_unlock(&sbinfo->si_plink_maint_lock);
114         }
115         si_write_unlock(sb);
116
117         return err;
118 }
119
120 /* ---------------------------------------------------------------------- */
121
122 #ifdef CONFIG_AUFS_DEBUG
123 void au_plink_list(struct super_block *sb)
124 {
125         int i;
126         struct au_sbinfo *sbinfo;
127         struct hlist_head *plink_hlist;
128         struct hlist_node *pos;
129         struct pseudo_link *plink;
130
131         SiMustAnyLock(sb);
132
133         sbinfo = au_sbi(sb);
134         AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
135         AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
136
137         for (i = 0; i < AuPlink_NHASH; i++) {
138                 plink_hlist = &sbinfo->si_plink[i].head;
139                 rcu_read_lock();
140                 hlist_for_each_entry_rcu(plink, pos, plink_hlist, hlist)
141                         AuDbg("%lu\n", plink->inode->i_ino);
142                 rcu_read_unlock();
143         }
144 }
145 #endif
146
147 /* is the inode pseudo-linked? */
148 int au_plink_test(struct inode *inode)
149 {
150         int found, i;
151         struct au_sbinfo *sbinfo;
152         struct hlist_head *plink_hlist;
153         struct hlist_node *pos;
154         struct pseudo_link *plink;
155
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));
160
161         found = 0;
162         i = au_plink_hash(inode->i_ino);
163         plink_hlist = &sbinfo->si_plink[i].head;
164         rcu_read_lock();
165         hlist_for_each_entry_rcu(plink, pos, plink_hlist, hlist)
166                 if (plink->inode == inode) {
167                         found = 1;
168                         break;
169                 }
170         rcu_read_unlock();
171         return found;
172 }
173
174 /* ---------------------------------------------------------------------- */
175
176 /*
177  * generate a name for plink.
178  * the file will be stored under AUFS_WH_PLINKDIR.
179  */
180 /* 20 is max digits length of ulong 64 */
181 #define PLINK_NAME_LEN  ((20 + 1) * 2)
182
183 static int plink_name(char *name, int len, struct inode *inode,
184                       aufs_bindex_t bindex)
185 {
186         int rlen;
187         struct inode *h_inode;
188
189         h_inode = au_h_iptr(inode, bindex);
190         rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
191         return rlen;
192 }
193
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;
199 };
200
201 static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
202                                        struct dentry *h_parent,
203                                        struct au_branch *br)
204 {
205         struct dentry *h_dentry;
206         struct mutex *h_mtx;
207
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);
211         mutex_unlock(h_mtx);
212         return h_dentry;
213 }
214
215 static void au_call_do_plink_lkup(void *args)
216 {
217         struct au_do_plink_lkup_args *a = args;
218         *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
219 }
220
221 /* lookup the plink-ed @inode under the branch at @bindex */
222 struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
223 {
224         struct dentry *h_dentry, *h_parent;
225         struct au_branch *br;
226         struct inode *h_dir;
227         int wkq_err;
228         char a[PLINK_NAME_LEN];
229         struct qstr tgtname = {
230                 .name   = a
231         };
232
233         AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
234
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);
239
240         if (current_fsuid()) {
241                 struct au_do_plink_lkup_args args = {
242                         .errp           = &h_dentry,
243                         .tgtname        = &tgtname,
244                         .h_parent       = h_parent,
245                         .br             = br
246                 };
247
248                 wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
249                 if (unlikely(wkq_err))
250                         h_dentry = ERR_PTR(wkq_err);
251         } else
252                 h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
253
254         return h_dentry;
255 }
256
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)
260 {
261         int err;
262         struct path h_path = {
263                 .mnt = au_br_mnt(br)
264         };
265         struct inode *h_dir;
266
267         h_dir = h_parent->d_inode;
268         mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_CHILD2);
269 again:
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))
273                 goto out;
274
275         err = 0;
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);
281                 dput(h_path.dentry);
282                 h_path.dentry = NULL;
283                 if (!err)
284                         goto again;
285         }
286         if (!err && !h_path.dentry->d_inode)
287                 err = vfsub_link(h_dentry, h_dir, &h_path);
288         dput(h_path.dentry);
289
290 out:
291         mutex_unlock(&h_dir->i_mutex);
292         return err;
293 }
294
295 struct do_whplink_args {
296         int *errp;
297         struct qstr *tgt;
298         struct dentry *h_parent;
299         struct dentry *h_dentry;
300         struct au_branch *br;
301 };
302
303 static void call_do_whplink(void *args)
304 {
305         struct do_whplink_args *a = args;
306         *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
307 }
308
309 static int whplink(struct dentry *h_dentry, struct inode *inode,
310                    aufs_bindex_t bindex, struct au_branch *br)
311 {
312         int err, wkq_err;
313         struct au_wbr *wbr;
314         struct dentry *h_parent;
315         struct inode *h_dir;
316         char a[PLINK_NAME_LEN];
317         struct qstr tgtname = {
318                 .name = a
319         };
320
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);
325
326         /* always superio. */
327         if (current_fsuid()) {
328                 struct do_whplink_args args = {
329                         .errp           = &err,
330                         .tgt            = &tgtname,
331                         .h_parent       = h_parent,
332                         .h_dentry       = h_dentry,
333                         .br             = br
334                 };
335                 wkq_err = au_wkq_wait(call_do_whplink, &args);
336                 if (unlikely(wkq_err))
337                         err = wkq_err;
338         } else
339                 err = do_whplink(&tgtname, h_parent, h_dentry, br);
340
341         return err;
342 }
343
344 /* free a single plink */
345 static void do_put_plink(struct pseudo_link *plink, int do_del)
346 {
347         if (do_del)
348                 hlist_del(&plink->hlist);
349         iput(plink->inode);
350         kfree(plink);
351 }
352
353 static void do_put_plink_rcu(struct rcu_head *rcu)
354 {
355         struct pseudo_link *plink;
356
357         plink = container_of(rcu, struct pseudo_link, rcu);
358         iput(plink->inode);
359         kfree(plink);
360 }
361
362 /*
363  * create a new pseudo-link for @h_dentry on @bindex.
364  * the linked inode is held in aufs @inode.
365  */
366 void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
367                      struct dentry *h_dentry)
368 {
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;
376
377         sb = inode->i_sb;
378         sbinfo = au_sbi(sb);
379         AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
380         AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
381
382         found = au_plink_test(inode);
383         if (found)
384                 return;
385
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);
390         if (tmp)
391                 tmp->inode = au_igrab(inode);
392         else {
393                 err = -ENOMEM;
394                 goto out;
395         }
396
397         spin_lock(&sphl->spin);
398         hlist_for_each_entry(plink, pos, plink_hlist, hlist) {
399                 if (plink->inode == inode) {
400                         found = 1;
401                         break;
402                 }
403         }
404         if (!found)
405                 hlist_add_head_rcu(&tmp->hlist, plink_hlist);
406         spin_unlock(&sphl->spin);
407         if (!found) {
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);
412 #undef msg
413                 err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
414         } else {
415                 do_put_plink(tmp, 0);
416                 return;
417         }
418
419 out:
420         if (unlikely(err)) {
421                 pr_warn("err %d, damaged pseudo link.\n", err);
422                 if (tmp) {
423                         au_sphl_del_rcu(&tmp->hlist, sphl);
424                         call_rcu(&tmp->rcu, do_put_plink_rcu);
425                 }
426         }
427 }
428
429 /* free all plinks */
430 void au_plink_put(struct super_block *sb, int verbose)
431 {
432         int i, warned;
433         struct au_sbinfo *sbinfo;
434         struct hlist_head *plink_hlist;
435         struct hlist_node *pos, *tmp;
436         struct pseudo_link *plink;
437
438         SiMustWriteLock(sb);
439
440         sbinfo = au_sbi(sb);
441         AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
442         AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
443
444         /* no spin_lock since sbinfo is write-locked */
445         warned = 0;
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");
450                         warned = 1;
451                 }
452                 hlist_for_each_entry_safe(plink, pos, tmp, plink_hlist, hlist)
453                         do_put_plink(plink, 0);
454                 INIT_HLIST_HEAD(plink_hlist);
455         }
456 }
457
458 void au_plink_clean(struct super_block *sb, int verbose)
459 {
460         struct dentry *root;
461
462         root = sb->s_root;
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);
467 }
468
469 static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
470 {
471         int do_put;
472         aufs_bindex_t bstart, bend, bindex;
473
474         do_put = 0;
475         bstart = au_ibstart(inode);
476         bend = au_ibend(inode);
477         if (bstart >= 0) {
478                 for (bindex = bstart; bindex <= bend; bindex++) {
479                         if (!au_h_iptr(inode, bindex)
480                             || au_ii_br_id(inode, bindex) != br_id)
481                                 continue;
482                         au_set_h_iptr(inode, bindex, NULL, 0);
483                         do_put = 1;
484                         break;
485                 }
486                 if (do_put)
487                         for (bindex = bstart; bindex <= bend; bindex++)
488                                 if (au_h_iptr(inode, bindex)) {
489                                         do_put = 0;
490                                         break;
491                                 }
492         } else
493                 do_put = 1;
494
495         return do_put;
496 }
497
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)
500 {
501         struct au_sbinfo *sbinfo;
502         struct hlist_head *plink_hlist;
503         struct hlist_node *pos, *tmp;
504         struct pseudo_link *plink;
505         struct inode *inode;
506         int i, do_put;
507
508         SiMustWriteLock(sb);
509
510         sbinfo = au_sbi(sb);
511         AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
512         AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
513
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);
521                         if (do_put)
522                                 do_put_plink(plink, 1);
523                         ii_write_unlock(inode);
524                         iput(inode);
525                 }
526         }
527 }