ARM: 8255/1: perf: Prevent wraparound during overflow
[pandora-kernel.git] / fs / aufs / inode.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 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, /*half*/1);
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         unsigned int gen;
257         struct au_iigen iigen;
258         aufs_bindex_t bindex, bend;
259         struct inode *h_inode, *h_dinode;
260
261         /*
262          * before this function, if aufs got any iinfo lock, it must be only
263          * one, the parent dir.
264          * it can happen by UDBA and the obsoleted inode number.
265          */
266         err = -EIO;
267         if (unlikely(inode->i_ino == parent_ino(dentry)))
268                 goto out;
269
270         err = 1;
271         ii_write_lock_new_child(inode);
272         h_dinode = au_h_dptr(dentry, au_dbstart(dentry))->d_inode;
273         bend = au_ibend(inode);
274         for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
275                 h_inode = au_h_iptr(inode, bindex);
276                 if (!h_inode || h_inode != h_dinode)
277                         continue;
278
279                 err = 0;
280                 gen = au_iigen(inode, &iigen);
281                 if (gen == au_digen(dentry)
282                     && !au_ig_ftest(iigen.ig_flags, HALF_REFRESHED))
283                         break;
284
285                 /* fully refresh inode using dentry */
286                 err = au_refresh_hinode(inode, dentry);
287                 if (!err)
288                         au_update_iigen(inode, /*half*/0);
289                 break;
290         }
291
292         if (unlikely(err))
293                 ii_write_unlock(inode);
294 out:
295         return err;
296 }
297
298 int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
299            unsigned int d_type, ino_t *ino)
300 {
301         int err;
302         struct mutex *mtx;
303
304         /* prevent hardlinked inode number from race condition */
305         mtx = NULL;
306         if (d_type != DT_DIR) {
307                 mtx = &au_sbr(sb, bindex)->br_xino.xi_nondir_mtx;
308                 mutex_lock(mtx);
309         }
310         err = au_xino_read(sb, bindex, h_ino, ino);
311         if (unlikely(err))
312                 goto out;
313
314         if (!*ino) {
315                 err = -EIO;
316                 *ino = au_xino_new_ino(sb);
317                 if (unlikely(!*ino))
318                         goto out;
319                 err = au_xino_write(sb, bindex, h_ino, *ino);
320                 if (unlikely(err))
321                         goto out;
322         }
323
324 out:
325         if (mtx)
326                 mutex_unlock(mtx);
327         return err;
328 }
329
330 /* successful returns with iinfo write_locked */
331 /* todo: return with unlocked? */
332 struct inode *au_new_inode(struct dentry *dentry, int must_new)
333 {
334         struct inode *inode, *h_inode;
335         struct dentry *h_dentry;
336         struct super_block *sb;
337         struct mutex *mtx;
338         ino_t h_ino, ino;
339         int err;
340         aufs_bindex_t bstart;
341
342         sb = dentry->d_sb;
343         bstart = au_dbstart(dentry);
344         h_dentry = au_h_dptr(dentry, bstart);
345         h_inode = h_dentry->d_inode;
346         h_ino = h_inode->i_ino;
347
348         /*
349          * stop 'race'-ing between hardlinks under different
350          * parents.
351          */
352         mtx = NULL;
353         if (!S_ISDIR(h_inode->i_mode))
354                 mtx = &au_sbr(sb, bstart)->br_xino.xi_nondir_mtx;
355
356 new_ino:
357         if (mtx)
358                 mutex_lock(mtx);
359         err = au_xino_read(sb, bstart, h_ino, &ino);
360         inode = ERR_PTR(err);
361         if (unlikely(err))
362                 goto out;
363
364         if (!ino) {
365                 ino = au_xino_new_ino(sb);
366                 if (unlikely(!ino)) {
367                         inode = ERR_PTR(-EIO);
368                         goto out;
369                 }
370         }
371
372         AuDbg("i%lu\n", (unsigned long)ino);
373         inode = au_iget_locked(sb, ino);
374         err = PTR_ERR(inode);
375         if (IS_ERR(inode))
376                 goto out;
377
378         AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
379         if (inode->i_state & I_NEW) {
380                 /* verbose coding for lock class name */
381                 if (unlikely(S_ISLNK(h_inode->i_mode)))
382                         au_rw_class(&au_ii(inode)->ii_rwsem,
383                                     au_lc_key + AuLcSymlink_IIINFO);
384                 else if (unlikely(S_ISDIR(h_inode->i_mode)))
385                         au_rw_class(&au_ii(inode)->ii_rwsem,
386                                     au_lc_key + AuLcDir_IIINFO);
387                 else /* likely */
388                         au_rw_class(&au_ii(inode)->ii_rwsem,
389                                     au_lc_key + AuLcNonDir_IIINFO);
390
391                 ii_write_lock_new_child(inode);
392                 err = set_inode(inode, dentry);
393                 if (!err) {
394                         unlock_new_inode(inode);
395                         goto out; /* success */
396                 }
397
398                 /*
399                  * iget_failed() calls iput(), but we need to call
400                  * ii_write_unlock() after iget_failed(). so dirty hack for
401                  * i_count.
402                  */
403                 atomic_inc(&inode->i_count);
404                 iget_failed(inode);
405                 ii_write_unlock(inode);
406                 au_xino_write(sb, bstart, h_ino, /*ino*/0);
407                 /* ignore this error */
408                 goto out_iput;
409         } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
410                 /*
411                  * horrible race condition between lookup, readdir and copyup
412                  * (or something).
413                  */
414                 if (mtx)
415                         mutex_unlock(mtx);
416                 err = reval_inode(inode, dentry);
417                 if (unlikely(err < 0)) {
418                         mtx = NULL;
419                         goto out_iput;
420                 }
421
422                 if (!err) {
423                         mtx = NULL;
424                         goto out; /* success */
425                 } else if (mtx)
426                         mutex_lock(mtx);
427         }
428
429         if (unlikely(au_test_fs_unique_ino(h_dentry->d_inode)))
430                 AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
431                         " b%d, %s, %.*s, hi%lu, i%lu.\n",
432                         bstart, au_sbtype(h_dentry->d_sb), AuDLNPair(dentry),
433                         (unsigned long)h_ino, (unsigned long)ino);
434         ino = 0;
435         err = au_xino_write(sb, bstart, h_ino, /*ino*/0);
436         if (!err) {
437                 iput(inode);
438                 if (mtx)
439                         mutex_unlock(mtx);
440                 goto new_ino;
441         }
442
443 out_iput:
444         iput(inode);
445         inode = ERR_PTR(err);
446 out:
447         if (mtx)
448                 mutex_unlock(mtx);
449         return inode;
450 }
451
452 /* ---------------------------------------------------------------------- */
453
454 int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
455                struct inode *inode)
456 {
457         int err;
458
459         err = au_br_rdonly(au_sbr(sb, bindex));
460
461         /* pseudo-link after flushed may happen out of bounds */
462         if (!err
463             && inode
464             && au_ibstart(inode) <= bindex
465             && bindex <= au_ibend(inode)) {
466                 /*
467                  * permission check is unnecessary since vfsub routine
468                  * will be called later
469                  */
470                 struct inode *hi = au_h_iptr(inode, bindex);
471                 if (hi)
472                         err = IS_IMMUTABLE(hi) ? -EROFS : 0;
473         }
474
475         return err;
476 }
477
478 int au_test_h_perm(struct inode *h_inode, int mask)
479 {
480         if (!current_fsuid())
481                 return 0;
482         return inode_permission(h_inode, mask);
483 }
484
485 int au_test_h_perm_sio(struct inode *h_inode, int mask)
486 {
487         if (au_test_nfs(h_inode->i_sb)
488             && (mask & MAY_WRITE)
489             && S_ISDIR(h_inode->i_mode))
490                 mask |= MAY_READ; /* force permission check */
491         return au_test_h_perm(h_inode, mask);
492 }