update aufs to it's latest standalone 3.2 branch
[pandora-kernel.git] / fs / aufs / iinfo.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  * inode private data
21  */
22
23 #include "aufs.h"
24
25 struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
26 {
27         struct inode *h_inode;
28
29         IiMustAnyLock(inode);
30
31         h_inode = au_ii(inode)->ii_hinode[0 + bindex].hi_inode;
32         AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
33         return h_inode;
34 }
35
36 /* todo: hard/soft set? */
37 void au_hiput(struct au_hinode *hinode)
38 {
39         au_hn_free(hinode);
40         dput(hinode->hi_whdentry);
41         iput(hinode->hi_inode);
42 }
43
44 unsigned int au_hi_flags(struct inode *inode, int isdir)
45 {
46         unsigned int flags;
47         const unsigned int mnt_flags = au_mntflags(inode->i_sb);
48
49         flags = 0;
50         if (au_opt_test(mnt_flags, XINO))
51                 au_fset_hi(flags, XINO);
52         if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
53                 au_fset_hi(flags, HNOTIFY);
54         return flags;
55 }
56
57 void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
58                    struct inode *h_inode, unsigned int flags)
59 {
60         struct au_hinode *hinode;
61         struct inode *hi;
62         struct au_iinfo *iinfo = au_ii(inode);
63
64         IiMustWriteLock(inode);
65
66         hinode = iinfo->ii_hinode + bindex;
67         hi = hinode->hi_inode;
68         AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
69
70         if (hi)
71                 au_hiput(hinode);
72         hinode->hi_inode = h_inode;
73         if (h_inode) {
74                 int err;
75                 struct super_block *sb = inode->i_sb;
76                 struct au_branch *br;
77
78                 AuDebugOn(inode->i_mode
79                           && (h_inode->i_mode & S_IFMT)
80                           != (inode->i_mode & S_IFMT));
81                 if (bindex == iinfo->ii_bstart)
82                         au_cpup_igen(inode, h_inode);
83                 br = au_sbr(sb, bindex);
84                 hinode->hi_id = br->br_id;
85                 if (au_ftest_hi(flags, XINO)) {
86                         err = au_xino_write(sb, bindex, h_inode->i_ino,
87                                             inode->i_ino);
88                         if (unlikely(err))
89                                 AuIOErr1("failed au_xino_write() %d\n", err);
90                 }
91
92                 if (au_ftest_hi(flags, HNOTIFY)
93                     && au_br_hnotifyable(br->br_perm)) {
94                         err = au_hn_alloc(hinode, inode);
95                         if (unlikely(err))
96                                 AuIOErr1("au_hn_alloc() %d\n", err);
97                 }
98         }
99 }
100
101 void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
102                   struct dentry *h_wh)
103 {
104         struct au_hinode *hinode;
105
106         IiMustWriteLock(inode);
107
108         hinode = au_ii(inode)->ii_hinode + bindex;
109         AuDebugOn(hinode->hi_whdentry);
110         hinode->hi_whdentry = h_wh;
111 }
112
113 void au_update_iigen(struct inode *inode, int half)
114 {
115         struct au_iinfo *iinfo;
116         struct au_iigen *iigen;
117         unsigned int sigen;
118
119         sigen = au_sigen(inode->i_sb);
120         iinfo = au_ii(inode);
121         iigen = &iinfo->ii_generation;
122         spin_lock(&iinfo->ii_genspin);
123         iigen->ig_generation = sigen;
124         if (half)
125                 au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
126         else
127                 au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
128         spin_unlock(&iinfo->ii_genspin);
129 }
130
131 /* it may be called at remount time, too */
132 void au_update_ibrange(struct inode *inode, int do_put_zero)
133 {
134         struct au_iinfo *iinfo;
135         aufs_bindex_t bindex, bend;
136
137         iinfo = au_ii(inode);
138         if (!iinfo)
139                 return;
140
141         IiMustWriteLock(inode);
142
143         if (do_put_zero && iinfo->ii_bstart >= 0) {
144                 for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
145                      bindex++) {
146                         struct inode *h_i;
147
148                         h_i = iinfo->ii_hinode[0 + bindex].hi_inode;
149                         if (h_i && !h_i->i_nlink)
150                                 au_set_h_iptr(inode, bindex, NULL, 0);
151                 }
152         }
153
154         iinfo->ii_bstart = -1;
155         iinfo->ii_bend = -1;
156         bend = au_sbend(inode->i_sb);
157         for (bindex = 0; bindex <= bend; bindex++)
158                 if (iinfo->ii_hinode[0 + bindex].hi_inode) {
159                         iinfo->ii_bstart = bindex;
160                         break;
161                 }
162         if (iinfo->ii_bstart >= 0)
163                 for (bindex = bend; bindex >= iinfo->ii_bstart; bindex--)
164                         if (iinfo->ii_hinode[0 + bindex].hi_inode) {
165                                 iinfo->ii_bend = bindex;
166                                 break;
167                         }
168         AuDebugOn(iinfo->ii_bstart > iinfo->ii_bend);
169 }
170
171 /* ---------------------------------------------------------------------- */
172
173 void au_icntnr_init_once(void *_c)
174 {
175         struct au_icntnr *c = _c;
176         struct au_iinfo *iinfo = &c->iinfo;
177         static struct lock_class_key aufs_ii;
178
179         spin_lock_init(&iinfo->ii_genspin);
180         au_rw_init(&iinfo->ii_rwsem);
181         au_rw_class(&iinfo->ii_rwsem, &aufs_ii);
182         inode_init_once(&c->vfs_inode);
183 }
184
185 int au_iinfo_init(struct inode *inode)
186 {
187         struct au_iinfo *iinfo;
188         struct super_block *sb;
189         int nbr, i;
190
191         sb = inode->i_sb;
192         iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
193         nbr = au_sbend(sb) + 1;
194         if (unlikely(nbr <= 0))
195                 nbr = 1;
196         iinfo->ii_hinode = kcalloc(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
197         if (iinfo->ii_hinode) {
198                 au_ninodes_inc(sb);
199                 for (i = 0; i < nbr; i++)
200                         iinfo->ii_hinode[i].hi_id = -1;
201
202                 iinfo->ii_generation.ig_generation = au_sigen(sb);
203                 iinfo->ii_bstart = -1;
204                 iinfo->ii_bend = -1;
205                 iinfo->ii_vdir = NULL;
206                 return 0;
207         }
208         return -ENOMEM;
209 }
210
211 int au_ii_realloc(struct au_iinfo *iinfo, int nbr)
212 {
213         int err, sz;
214         struct au_hinode *hip;
215
216         AuRwMustWriteLock(&iinfo->ii_rwsem);
217
218         err = -ENOMEM;
219         sz = sizeof(*hip) * (iinfo->ii_bend + 1);
220         if (!sz)
221                 sz = sizeof(*hip);
222         hip = au_kzrealloc(iinfo->ii_hinode, sz, sizeof(*hip) * nbr, GFP_NOFS);
223         if (hip) {
224                 iinfo->ii_hinode = hip;
225                 err = 0;
226         }
227
228         return err;
229 }
230
231 void au_iinfo_fin(struct inode *inode)
232 {
233         struct au_iinfo *iinfo;
234         struct au_hinode *hi;
235         struct super_block *sb;
236         aufs_bindex_t bindex, bend;
237         const unsigned char unlinked = !inode->i_nlink;
238
239         iinfo = au_ii(inode);
240         /* bad_inode case */
241         if (!iinfo)
242                 return;
243
244         sb = inode->i_sb;
245         au_ninodes_dec(sb);
246         if (si_pid_test(sb))
247                 au_xino_delete_inode(inode, unlinked);
248         else {
249                 /*
250                  * it is safe to hide the dependency between sbinfo and
251                  * sb->s_umount.
252                  */
253                 lockdep_off();
254                 si_noflush_read_lock(sb);
255                 au_xino_delete_inode(inode, unlinked);
256                 si_read_unlock(sb);
257                 lockdep_on();
258         }
259
260         if (iinfo->ii_vdir)
261                 au_vdir_free(iinfo->ii_vdir);
262
263         bindex = iinfo->ii_bstart;
264         if (bindex >= 0) {
265                 hi = iinfo->ii_hinode + bindex;
266                 bend = iinfo->ii_bend;
267                 while (bindex++ <= bend) {
268                         if (hi->hi_inode)
269                                 au_hiput(hi);
270                         hi++;
271                 }
272         }
273         kfree(iinfo->ii_hinode);
274         iinfo->ii_hinode = NULL;
275         AuRwDestroy(&iinfo->ii_rwsem);
276 }