aufs: fix up for the new stable kernel
[pandora-kernel.git] / fs / aufs / finfo.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  * file private data
21  */
22
23 #include "aufs.h"
24
25 void au_hfput(struct au_hfile *hf, struct file *file)
26 {
27         /* todo: direct access f_flags */
28         if (vfsub_file_flags(file) & __FMODE_EXEC)
29                 allow_write_access(hf->hf_file);
30         fput(hf->hf_file);
31         hf->hf_file = NULL;
32         atomic_dec(&hf->hf_br->br_count);
33         hf->hf_br = NULL;
34 }
35
36 void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
37 {
38         struct au_finfo *finfo = au_fi(file);
39         struct au_hfile *hf;
40         struct au_fidir *fidir;
41
42         fidir = finfo->fi_hdir;
43         if (!fidir) {
44                 AuDebugOn(finfo->fi_btop != bindex);
45                 hf = &finfo->fi_htop;
46         } else
47                 hf = fidir->fd_hfile + bindex;
48
49         if (hf && hf->hf_file)
50                 au_hfput(hf, file);
51         if (val) {
52                 FiMustWriteLock(file);
53                 hf->hf_file = val;
54                 hf->hf_br = au_sbr(file->f_dentry->d_sb, bindex);
55         }
56 }
57
58 void au_update_figen(struct file *file)
59 {
60         atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_dentry));
61         /* smp_mb(); */ /* atomic_set */
62 }
63
64 /* ---------------------------------------------------------------------- */
65
66 struct au_fidir *au_fidir_alloc(struct super_block *sb)
67 {
68         struct au_fidir *fidir;
69         int nbr;
70
71         nbr = au_sbend(sb) + 1;
72         if (nbr < 2)
73                 nbr = 2; /* initial allocate for 2 branches */
74         fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
75         if (fidir) {
76                 fidir->fd_bbot = -1;
77                 fidir->fd_nent = nbr;
78                 fidir->fd_vdir_cache = NULL;
79         }
80
81         return fidir;
82 }
83
84 int au_fidir_realloc(struct au_finfo *finfo, int nbr)
85 {
86         int err;
87         struct au_fidir *fidir, *p;
88
89         AuRwMustWriteLock(&finfo->fi_rwsem);
90         fidir = finfo->fi_hdir;
91         AuDebugOn(!fidir);
92
93         err = -ENOMEM;
94         p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
95                          GFP_NOFS);
96         if (p) {
97                 p->fd_nent = nbr;
98                 finfo->fi_hdir = p;
99                 err = 0;
100         }
101
102         return err;
103 }
104
105 /* ---------------------------------------------------------------------- */
106
107 void au_finfo_fin(struct file *file)
108 {
109         struct au_finfo *finfo;
110
111         au_nfiles_dec(file->f_dentry->d_sb);
112
113         finfo = au_fi(file);
114         AuDebugOn(finfo->fi_hdir);
115         AuRwDestroy(&finfo->fi_rwsem);
116         au_cache_free_finfo(finfo);
117 }
118
119 void au_fi_init_once(void *_finfo)
120 {
121         struct au_finfo *finfo = _finfo;
122         static struct lock_class_key aufs_fi;
123
124         au_rw_init(&finfo->fi_rwsem);
125         au_rw_class(&finfo->fi_rwsem, &aufs_fi);
126 }
127
128 int au_finfo_init(struct file *file, struct au_fidir *fidir)
129 {
130         int err;
131         struct au_finfo *finfo;
132         struct dentry *dentry;
133
134         err = -ENOMEM;
135         dentry = file->f_dentry;
136         finfo = au_cache_alloc_finfo();
137         if (unlikely(!finfo))
138                 goto out;
139
140         err = 0;
141         au_nfiles_inc(dentry->d_sb);
142         /* verbose coding for lock class name */
143         if (!fidir)
144                 au_rw_class(&finfo->fi_rwsem, au_lc_key + AuLcNonDir_FIINFO);
145         else
146                 au_rw_class(&finfo->fi_rwsem, au_lc_key + AuLcDir_FIINFO);
147         au_rw_write_lock(&finfo->fi_rwsem);
148         finfo->fi_btop = -1;
149         finfo->fi_hdir = fidir;
150         atomic_set(&finfo->fi_generation, au_digen(dentry));
151         /* smp_mb(); */ /* atomic_set */
152
153         file->private_data = finfo;
154
155 out:
156         return err;
157 }