Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / fs / aufs / inode.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 functions
21  */
22
23 #include "aufs.h"
24
25 struct inode *au_igrab(struct inode *inode)
26 {
27         if (inode) {
28                 AuDebugOn(!atomic_read(&inode->i_count));
29                 ihold(inode);
30         }
31         return inode;
32 }
33
34 static void au_refresh_hinode_attr(struct inode *inode, int do_version)
35 {
36         au_cpup_attr_all(inode, /*force*/0);
37         au_update_iigen(inode);
38         if (do_version)
39                 inode->i_version++;
40 }
41
42 static int au_ii_refresh(struct inode *inode, int *update)
43 {
44         int err, e;
45         umode_t type;
46         aufs_bindex_t bindex, new_bindex;
47         struct super_block *sb;
48         struct au_iinfo *iinfo;
49         struct au_hinode *p, *q, tmp;
50
51         IiMustWriteLock(inode);
52
53         *update = 0;
54         sb = inode->i_sb;
55         type = inode->i_mode & S_IFMT;
56         iinfo = au_ii(inode);
57         err = au_ii_realloc(iinfo, au_sbend(sb) + 1);
58         if (unlikely(err))
59                 goto out;
60
61         AuDebugOn(iinfo->ii_bstart < 0);
62         p = iinfo->ii_hinode + iinfo->ii_bstart;
63         for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
64              bindex++, p++) {
65                 if (!p->hi_inode)
66                         continue;
67
68                 AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
69                 new_bindex = au_br_index(sb, p->hi_id);
70                 if (new_bindex == bindex)
71                         continue;
72
73                 if (new_bindex < 0) {
74                         *update = 1;
75                         au_hiput(p);
76                         p->hi_inode = NULL;
77                         continue;
78                 }
79
80                 if (new_bindex < iinfo->ii_bstart)
81                         iinfo->ii_bstart = new_bindex;
82                 if (iinfo->ii_bend < new_bindex)
83                         iinfo->ii_bend = new_bindex;
84                 /* swap two lower inode, and loop again */
85                 q = iinfo->ii_hinode + new_bindex;
86                 tmp = *q;
87                 *q = *p;
88                 *p = tmp;
89                 if (tmp.hi_inode) {
90                         bindex--;
91                         p--;
92                 }
93         }
94         au_update_ibrange(inode, /*do_put_zero*/0);
95         e = au_dy_irefresh(inode);
96         if (unlikely(e && !err))
97                 err = e;
98
99 out:
100         AuTraceErr(err);
101         return err;
102 }
103
104 int au_refresh_hinode_self(struct inode *inode)
105 {
106         int err, update;
107
108         err = au_ii_refresh(inode, &update);
109         if (!err)
110                 au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
111
112         AuTraceErr(err);
113         return err;
114 }
115
116 int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
117 {
118         int err, e, update;
119         unsigned int flags;
120         umode_t mode;
121         aufs_bindex_t bindex, bend;
122         unsigned char isdir;
123         struct au_hinode *p;
124         struct au_iinfo *iinfo;
125
126         err = au_ii_refresh(inode, &update);
127         if (unlikely(err))
128                 goto out;
129
130         update = 0;
131         iinfo = au_ii(inode);
132         p = iinfo->ii_hinode + iinfo->ii_bstart;
133         mode = (inode->i_mode & S_IFMT);
134         isdir = S_ISDIR(mode);
135         flags = au_hi_flags(inode, isdir);
136         bend = au_dbend(dentry);
137         for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
138                 struct inode *h_i;
139                 struct dentry *h_d;
140
141                 h_d = au_h_dptr(dentry, bindex);
142                 if (!h_d || !h_d->d_inode)
143                         continue;
144
145                 AuDebugOn(mode != (h_d->d_inode->i_mode & S_IFMT));
146                 if (iinfo->ii_bstart <= bindex && bindex <= iinfo->ii_bend) {
147                         h_i = au_h_iptr(inode, bindex);
148                         if (h_i) {
149                                 if (h_i == h_d->d_inode)
150                                         continue;
151                                 err = -EIO;
152                                 break;
153                         }
154                 }
155                 if (bindex < iinfo->ii_bstart)
156                         iinfo->ii_bstart = bindex;
157                 if (iinfo->ii_bend < bindex)
158                         iinfo->ii_bend = bindex;
159                 au_set_h_iptr(inode, bindex, au_igrab(h_d->d_inode), flags);
160                 update = 1;
161         }
162         au_update_ibrange(inode, /*do_put_zero*/0);
163         e = au_dy_irefresh(inode);
164         if (unlikely(e && !err))
165                 err = e;
166         if (!err)
167                 au_refresh_hinode_attr(inode, update && isdir);
168
169 out:
170         AuTraceErr(err);
171         return err;
172 }
173
174 static int set_inode(struct inode *inode, struct dentry *dentry)
175 {
176         int err;
177         unsigned int flags;
178         umode_t mode;
179         aufs_bindex_t bindex, bstart, btail;
180         unsigned char isdir;
181         struct dentry *h_dentry;
182         struct inode *h_inode;
183         struct au_iinfo *iinfo;
184
185         IiMustWriteLock(inode);
186
187         err = 0;
188         isdir = 0;
189         bstart = au_dbstart(dentry);
190         h_inode = au_h_dptr(dentry, bstart)->d_inode;
191         mode = h_inode->i_mode;
192         switch (mode & S_IFMT) {
193         case S_IFREG:
194                 btail = au_dbtail(dentry);
195                 inode->i_op = &aufs_iop;
196                 inode->i_fop = &aufs_file_fop;
197                 err = au_dy_iaop(inode, bstart, h_inode);
198                 if (unlikely(err))
199                         goto out;
200                 break;
201         case S_IFDIR:
202                 isdir = 1;
203                 btail = au_dbtaildir(dentry);
204                 inode->i_op = &aufs_dir_iop;
205                 inode->i_fop = &aufs_dir_fop;
206                 break;
207         case S_IFLNK:
208                 btail = au_dbtail(dentry);
209                 inode->i_op = &aufs_symlink_iop;
210                 break;
211         case S_IFBLK:
212         case S_IFCHR:
213         case S_IFIFO:
214         case S_IFSOCK:
215                 btail = au_dbtail(dentry);
216                 inode->i_op = &aufs_iop;
217                 au_init_special_fop(inode, mode, h_inode->i_rdev);
218                 break;
219         default:
220                 AuIOErr("Unknown file type 0%o\n", mode);
221                 err = -EIO;
222                 goto out;
223         }
224
225         /* do not set hnotify for whiteouted dirs (SHWH mode) */
226         flags = au_hi_flags(inode, isdir);
227         if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
228             && au_ftest_hi(flags, HNOTIFY)
229             && dentry->d_name.len > AUFS_WH_PFX_LEN
230             && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
231                 au_fclr_hi(flags, HNOTIFY);
232         iinfo = au_ii(inode);
233         iinfo->ii_bstart = bstart;
234         iinfo->ii_bend = btail;
235         for (bindex = bstart; bindex <= btail; bindex++) {
236                 h_dentry = au_h_dptr(dentry, bindex);
237                 if (h_dentry)
238                         au_set_h_iptr(inode, bindex,
239                                       au_igrab(h_dentry->d_inode), flags);
240         }
241         au_cpup_attr_all(inode, /*force*/1);
242
243 out:
244         return err;
245 }
246
247 /*
248  * successful returns with iinfo write_locked
249  * minus: errno
250  * zero: success, matched
251  * plus: no error, but unmatched
252  */
253 static int reval_inode(struct inode *inode, struct dentry *dentry)
254 {
255         int err;
256         aufs_bindex_t bindex, bend;
257         struct inode *h_inode, *h_dinode;
258
259         /*
260          * before this function, if aufs got any iinfo lock, it must be only
261          * one, the parent dir.
262          * it can happen by UDBA and the obsoleted inode number.
263          */
264         err = -EIO;
265         if (unlikely(inode->i_ino == parent_ino(dentry)))
266                 goto out;
267
268         err = 1;
269         ii_write_lock_new_child(inode);
270         h_dinode = au_h_dptr(dentry, au_dbstart(dentry))->d_inode;
271         bend = au_ibend(inode);
272         for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
273                 h_inode = au_h_iptr(inode, bindex);
274                 if (h_inode && h_inode == h_dinode) {
275                         err = 0;
276                         if (au_iigen_test(inode, au_digen(dentry)))
277                                 err = au_refresh_hinode(inode, dentry);
278                         break;
279                 }
280         }
281
282         if (unlikely(err))
283                 ii_write_unlock(inode);
284 out:
285         return err;
286 }
287
288 int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
289            unsigned int d_type, ino_t *ino)
290 {
291         int err;
292         struct mutex *mtx;
293
294         /* prevent hardlinked inode number from race condition */
295         mtx = NULL;
296         if (d_type != DT_DIR) {
297                 mtx = &au_sbr(sb, bindex)->br_xino.xi_nondir_mtx;
298                 mutex_lock(mtx);
299         }
300         err = au_xino_read(sb, bindex, h_ino, ino);
301         if (unlikely(err))
302                 goto out;
303
304         if (!*ino) {
305                 err = -EIO;
306                 *ino = au_xino_new_ino(sb);
307                 if (unlikely(!*ino))
308                         goto out;
309                 err = au_xino_write(sb, bindex, h_ino, *ino);
310                 if (unlikely(err))
311                         goto out;
312         }
313
314 out:
315         if (mtx)
316                 mutex_unlock(mtx);
317         return err;
318 }
319
320 /* successful returns with iinfo write_locked */
321 /* todo: return with unlocked? */
322 struct inode *au_new_inode(struct dentry *dentry, int must_new)
323 {
324         struct inode *inode, *h_inode;
325         struct dentry *h_dentry;
326         struct super_block *sb;
327         struct mutex *mtx;
328         ino_t h_ino, ino;
329         int err;
330         aufs_bindex_t bstart;
331
332         sb = dentry->d_sb;
333         bstart = au_dbstart(dentry);
334         h_dentry = au_h_dptr(dentry, bstart);
335         h_inode = h_dentry->d_inode;
336         h_ino = h_inode->i_ino;
337
338         /*
339          * stop 'race'-ing between hardlinks under different
340          * parents.
341          */
342         mtx = NULL;
343         if (!S_ISDIR(h_inode->i_mode))
344                 mtx = &au_sbr(sb, bstart)->br_xino.xi_nondir_mtx;
345
346 new_ino:
347         if (mtx)
348                 mutex_lock(mtx);
349         err = au_xino_read(sb, bstart, h_ino, &ino);
350         inode = ERR_PTR(err);
351         if (unlikely(err))
352                 goto out;
353
354         if (!ino) {
355                 ino = au_xino_new_ino(sb);
356                 if (unlikely(!ino)) {
357                         inode = ERR_PTR(-EIO);
358                         goto out;
359                 }
360         }
361
362         AuDbg("i%lu\n", (unsigned long)ino);
363         inode = au_iget_locked(sb, ino);
364         err = PTR_ERR(inode);
365         if (IS_ERR(inode))
366                 goto out;
367
368         AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
369         if (inode->i_state & I_NEW) {
370                 ii_write_lock_new_child(inode);
371                 err = set_inode(inode, dentry);
372                 if (!err) {
373                         unlock_new_inode(inode);
374                         goto out; /* success */
375                 }
376
377                 /*
378                  * iget_failed() calls iput(), but we need to call
379                  * ii_write_unlock() after iget_failed(). so dirty hack for
380                  * i_count.
381                  */
382                 atomic_inc(&inode->i_count);
383                 iget_failed(inode);
384                 ii_write_unlock(inode);
385                 au_xino_write(sb, bstart, h_ino, /*ino*/0);
386                 /* ignore this error */
387                 goto out_iput;
388         } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
389                 /*
390                  * horrible race condition between lookup, readdir and copyup
391                  * (or something).
392                  */
393                 if (mtx)
394                         mutex_unlock(mtx);
395                 err = reval_inode(inode, dentry);
396                 if (unlikely(err < 0)) {
397                         mtx = NULL;
398                         goto out_iput;
399                 }
400
401                 if (!err) {
402                         mtx = NULL;
403                         goto out; /* success */
404                 } else if (mtx)
405                         mutex_lock(mtx);
406         }
407
408         if (unlikely(au_test_fs_unique_ino(h_dentry->d_inode)))
409                 AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
410                         " b%d, %s, %.*s, hi%lu, i%lu.\n",
411                         bstart, au_sbtype(h_dentry->d_sb), AuDLNPair(dentry),
412                         (unsigned long)h_ino, (unsigned long)ino);
413         ino = 0;
414         err = au_xino_write(sb, bstart, h_ino, /*ino*/0);
415         if (!err) {
416                 iput(inode);
417                 if (mtx)
418                         mutex_unlock(mtx);
419                 goto new_ino;
420         }
421
422 out_iput:
423         iput(inode);
424         inode = ERR_PTR(err);
425 out:
426         if (mtx)
427                 mutex_unlock(mtx);
428         return inode;
429 }
430
431 /* ---------------------------------------------------------------------- */
432
433 int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
434                struct inode *inode)
435 {
436         int err;
437
438         err = au_br_rdonly(au_sbr(sb, bindex));
439
440         /* pseudo-link after flushed may happen out of bounds */
441         if (!err
442             && inode
443             && au_ibstart(inode) <= bindex
444             && bindex <= au_ibend(inode)) {
445                 /*
446                  * permission check is unnecessary since vfsub routine
447                  * will be called later
448                  */
449                 struct inode *hi = au_h_iptr(inode, bindex);
450                 if (hi)
451                         err = IS_IMMUTABLE(hi) ? -EROFS : 0;
452         }
453
454         return err;
455 }
456
457 int au_test_h_perm(struct inode *h_inode, int mask)
458 {
459         if (!current_fsuid())
460                 return 0;
461         return inode_permission(h_inode, mask);
462 }
463
464 int au_test_h_perm_sio(struct inode *h_inode, int mask)
465 {
466         if (au_test_nfs(h_inode->i_sb)
467             && (mask & MAY_WRITE)
468             && S_ISDIR(h_inode->i_mode))
469                 mask |= MAY_READ; /* force permission check */
470         return au_test_h_perm(h_inode, mask);
471 }