d9571c47609f0ab531d1d92f82790a73cf973415
[pandora-kernel.git] / fs / aufs / iinfo.c
1 /*
2  * Copyright (C) 2005-2012 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)
114 {
115         atomic_set(&au_ii(inode)->ii_generation, au_sigen(inode->i_sb));
116         /* smp_mb(); */ /* atomic_set */
117 }
118
119 /* it may be called at remount time, too */
120 void au_update_ibrange(struct inode *inode, int do_put_zero)
121 {
122         struct au_iinfo *iinfo;
123         aufs_bindex_t bindex, bend;
124
125         iinfo = au_ii(inode);
126         if (!iinfo)
127                 return;
128
129         IiMustWriteLock(inode);
130
131         if (do_put_zero && iinfo->ii_bstart >= 0) {
132                 for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
133                      bindex++) {
134                         struct inode *h_i;
135
136                         h_i = iinfo->ii_hinode[0 + bindex].hi_inode;
137                         if (h_i && !h_i->i_nlink)
138                                 au_set_h_iptr(inode, bindex, NULL, 0);
139                 }
140         }
141
142         iinfo->ii_bstart = -1;
143         iinfo->ii_bend = -1;
144         bend = au_sbend(inode->i_sb);
145         for (bindex = 0; bindex <= bend; bindex++)
146                 if (iinfo->ii_hinode[0 + bindex].hi_inode) {
147                         iinfo->ii_bstart = bindex;
148                         break;
149                 }
150         if (iinfo->ii_bstart >= 0)
151                 for (bindex = bend; bindex >= iinfo->ii_bstart; bindex--)
152                         if (iinfo->ii_hinode[0 + bindex].hi_inode) {
153                                 iinfo->ii_bend = bindex;
154                                 break;
155                         }
156         AuDebugOn(iinfo->ii_bstart > iinfo->ii_bend);
157 }
158
159 /* ---------------------------------------------------------------------- */
160
161 void au_icntnr_init_once(void *_c)
162 {
163         struct au_icntnr *c = _c;
164         struct au_iinfo *iinfo = &c->iinfo;
165         static struct lock_class_key aufs_ii;
166
167         au_rw_init(&iinfo->ii_rwsem);
168         au_rw_class(&iinfo->ii_rwsem, &aufs_ii);
169         inode_init_once(&c->vfs_inode);
170 }
171
172 int au_iinfo_init(struct inode *inode)
173 {
174         struct au_iinfo *iinfo;
175         struct super_block *sb;
176         int nbr, i;
177
178         sb = inode->i_sb;
179         iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
180         nbr = au_sbend(sb) + 1;
181         if (unlikely(nbr <= 0))
182                 nbr = 1;
183         iinfo->ii_hinode = kcalloc(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
184         if (iinfo->ii_hinode) {
185                 au_ninodes_inc(sb);
186                 for (i = 0; i < nbr; i++)
187                         iinfo->ii_hinode[i].hi_id = -1;
188
189                 atomic_set(&iinfo->ii_generation, au_sigen(sb));
190                 /* smp_mb(); */ /* atomic_set */
191                 iinfo->ii_bstart = -1;
192                 iinfo->ii_bend = -1;
193                 iinfo->ii_vdir = NULL;
194                 return 0;
195         }
196         return -ENOMEM;
197 }
198
199 int au_ii_realloc(struct au_iinfo *iinfo, int nbr)
200 {
201         int err, sz;
202         struct au_hinode *hip;
203
204         AuRwMustWriteLock(&iinfo->ii_rwsem);
205
206         err = -ENOMEM;
207         sz = sizeof(*hip) * (iinfo->ii_bend + 1);
208         if (!sz)
209                 sz = sizeof(*hip);
210         hip = au_kzrealloc(iinfo->ii_hinode, sz, sizeof(*hip) * nbr, GFP_NOFS);
211         if (hip) {
212                 iinfo->ii_hinode = hip;
213                 err = 0;
214         }
215
216         return err;
217 }
218
219 void au_iinfo_fin(struct inode *inode)
220 {
221         struct au_iinfo *iinfo;
222         struct au_hinode *hi;
223         struct super_block *sb;
224         aufs_bindex_t bindex, bend;
225         const unsigned char unlinked = !inode->i_nlink;
226
227         iinfo = au_ii(inode);
228         /* bad_inode case */
229         if (!iinfo)
230                 return;
231
232         sb = inode->i_sb;
233         au_ninodes_dec(sb);
234         if (si_pid_test(sb))
235                 au_xino_delete_inode(inode, unlinked);
236         else {
237                 /*
238                  * it is safe to hide the dependency between sbinfo and
239                  * sb->s_umount.
240                  */
241                 lockdep_off();
242                 si_noflush_read_lock(sb);
243                 au_xino_delete_inode(inode, unlinked);
244                 si_read_unlock(sb);
245                 lockdep_on();
246         }
247
248         if (iinfo->ii_vdir)
249                 au_vdir_free(iinfo->ii_vdir);
250
251         bindex = iinfo->ii_bstart;
252         if (bindex >= 0) {
253                 hi = iinfo->ii_hinode + bindex;
254                 bend = iinfo->ii_bend;
255                 while (bindex++ <= bend) {
256                         if (hi->hi_inode)
257                                 au_hiput(hi);
258                         hi++;
259                 }
260         }
261         kfree(iinfo->ii_hinode);
262         iinfo->ii_hinode = NULL;
263         AuRwDestroy(&iinfo->ii_rwsem);
264 }