]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - fs/aufs/inode.c
2 * Copyright (C) 2005-2016 Junjiro R. Okajima
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.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 struct inode
*au_igrab(struct inode
*inode
)
27 AuDebugOn(!atomic_read(&inode
->i_count
));
33 static void au_refresh_hinode_attr(struct inode
*inode
, int do_version
)
35 au_cpup_attr_all(inode
, /*force*/0);
36 au_update_iigen(inode
, /*half*/1);
41 static int au_ii_refresh(struct inode
*inode
, int *update
)
45 aufs_bindex_t bindex
, new_bindex
;
46 struct super_block
*sb
;
47 struct au_iinfo
*iinfo
;
48 struct au_hinode
*p
, *q
, tmp
;
50 AuDebugOn(au_is_bad_inode(inode
));
51 IiMustWriteLock(inode
);
55 nbr
= au_sbbot(sb
) + 1;
56 type
= inode
->i_mode
& S_IFMT
;
58 err
= au_hinode_realloc(iinfo
, nbr
, /*may_shrink*/0);
62 AuDebugOn(iinfo
->ii_btop
< 0);
63 p
= au_hinode(iinfo
, iinfo
->ii_btop
);
64 for (bindex
= iinfo
->ii_btop
; bindex
<= iinfo
->ii_bbot
;
69 AuDebugOn(type
!= (p
->hi_inode
->i_mode
& S_IFMT
));
70 new_bindex
= au_br_index(sb
, p
->hi_id
);
71 if (new_bindex
== bindex
)
81 if (new_bindex
< iinfo
->ii_btop
)
82 iinfo
->ii_btop
= new_bindex
;
83 if (iinfo
->ii_bbot
< new_bindex
)
84 iinfo
->ii_bbot
= new_bindex
;
85 /* swap two lower inode, and loop again */
86 q
= au_hinode(iinfo
, new_bindex
);
95 au_update_ibrange(inode
, /*do_put_zero*/0);
96 au_hinode_realloc(iinfo
, nbr
, /*may_shrink*/1); /* harmless if err */
97 e
= au_dy_irefresh(inode
);
98 if (unlikely(e
&& !err
))
106 void au_refresh_iop(struct inode
*inode
, int force_getattr
)
109 struct au_sbinfo
*sbi
= au_sbi(inode
->i_sb
);
110 const struct inode_operations
*iop
111 = force_getattr
? aufs_iop
: sbi
->si_iop_array
;
113 if (inode
->i_op
== iop
)
116 switch (inode
->i_mode
& S_IFMT
) {
121 type
= AuIop_SYMLINK
;
128 inode
->i_op
= iop
+ type
;
129 /* unnecessary smp_wmb() */
132 int au_refresh_hinode_self(struct inode
*inode
)
136 err
= au_ii_refresh(inode
, &update
);
138 au_refresh_hinode_attr(inode
, update
&& S_ISDIR(inode
->i_mode
));
144 int au_refresh_hinode(struct inode
*inode
, struct dentry
*dentry
)
149 aufs_bindex_t bindex
, bbot
;
152 struct au_iinfo
*iinfo
;
154 err
= au_ii_refresh(inode
, &update
);
159 iinfo
= au_ii(inode
);
160 p
= au_hinode(iinfo
, iinfo
->ii_btop
);
161 mode
= (inode
->i_mode
& S_IFMT
);
162 isdir
= S_ISDIR(mode
);
163 flags
= au_hi_flags(inode
, isdir
);
164 bbot
= au_dbbot(dentry
);
165 for (bindex
= au_dbtop(dentry
); bindex
<= bbot
; bindex
++) {
166 struct inode
*h_i
, *h_inode
;
169 h_d
= au_h_dptr(dentry
, bindex
);
170 if (!h_d
|| d_is_negative(h_d
))
173 h_inode
= d_inode(h_d
);
174 AuDebugOn(mode
!= (h_inode
->i_mode
& S_IFMT
));
175 if (iinfo
->ii_btop
<= bindex
&& bindex
<= iinfo
->ii_bbot
) {
176 h_i
= au_h_iptr(inode
, bindex
);
184 if (bindex
< iinfo
->ii_btop
)
185 iinfo
->ii_btop
= bindex
;
186 if (iinfo
->ii_bbot
< bindex
)
187 iinfo
->ii_bbot
= bindex
;
188 au_set_h_iptr(inode
, bindex
, au_igrab(h_inode
), flags
);
191 au_update_ibrange(inode
, /*do_put_zero*/0);
192 e
= au_dy_irefresh(inode
);
193 if (unlikely(e
&& !err
))
196 au_refresh_hinode_attr(inode
, update
&& isdir
);
203 static int set_inode(struct inode
*inode
, struct dentry
*dentry
)
208 aufs_bindex_t bindex
, btop
, btail
;
210 struct dentry
*h_dentry
;
211 struct inode
*h_inode
;
212 struct au_iinfo
*iinfo
;
213 struct inode_operations
*iop
;
215 IiMustWriteLock(inode
);
219 iop
= au_sbi(inode
->i_sb
)->si_iop_array
;
220 btop
= au_dbtop(dentry
);
221 h_dentry
= au_h_dptr(dentry
, btop
);
222 h_inode
= d_inode(h_dentry
);
223 mode
= h_inode
->i_mode
;
224 switch (mode
& S_IFMT
) {
226 btail
= au_dbtail(dentry
);
227 inode
->i_op
= iop
+ AuIop_OTHER
;
228 inode
->i_fop
= &aufs_file_fop
;
229 err
= au_dy_iaop(inode
, btop
, h_inode
);
235 btail
= au_dbtaildir(dentry
);
236 inode
->i_op
= iop
+ AuIop_DIR
;
237 inode
->i_fop
= &aufs_dir_fop
;
240 btail
= au_dbtail(dentry
);
241 inode
->i_op
= iop
+ AuIop_SYMLINK
;
247 btail
= au_dbtail(dentry
);
248 inode
->i_op
= iop
+ AuIop_OTHER
;
249 init_special_inode(inode
, mode
, h_inode
->i_rdev
);
252 AuIOErr("Unknown file type 0%o\n", mode
);
257 /* do not set hnotify for whiteouted dirs (SHWH mode) */
258 flags
= au_hi_flags(inode
, isdir
);
259 if (au_opt_test(au_mntflags(dentry
->d_sb
), SHWH
)
260 && au_ftest_hi(flags
, HNOTIFY
)
261 && dentry
->d_name
.len
> AUFS_WH_PFX_LEN
262 && !memcmp(dentry
->d_name
.name
, AUFS_WH_PFX
, AUFS_WH_PFX_LEN
))
263 au_fclr_hi(flags
, HNOTIFY
);
264 iinfo
= au_ii(inode
);
265 iinfo
->ii_btop
= btop
;
266 iinfo
->ii_bbot
= btail
;
267 for (bindex
= btop
; bindex
<= btail
; bindex
++) {
268 h_dentry
= au_h_dptr(dentry
, bindex
);
270 au_set_h_iptr(inode
, bindex
,
271 au_igrab(d_inode(h_dentry
)), flags
);
273 au_cpup_attr_all(inode
, /*force*/1);
275 * to force calling aufs_get_acl() every time,
276 * do not call cache_no_acl() for aufs inode.
284 * successful returns with iinfo write_locked
286 * zero: success, matched
287 * plus: no error, but unmatched
289 static int reval_inode(struct inode
*inode
, struct dentry
*dentry
)
292 unsigned int gen
, igflags
;
293 aufs_bindex_t bindex
, bbot
;
294 struct inode
*h_inode
, *h_dinode
;
295 struct dentry
*h_dentry
;
298 * before this function, if aufs got any iinfo lock, it must be only
299 * one, the parent dir.
300 * it can happen by UDBA and the obsoleted inode number.
303 if (unlikely(inode
->i_ino
== parent_ino(dentry
)))
307 ii_write_lock_new_child(inode
);
308 h_dentry
= au_h_dptr(dentry
, au_dbtop(dentry
));
309 h_dinode
= d_inode(h_dentry
);
310 bbot
= au_ibbot(inode
);
311 for (bindex
= au_ibtop(inode
); bindex
<= bbot
; bindex
++) {
312 h_inode
= au_h_iptr(inode
, bindex
);
313 if (!h_inode
|| h_inode
!= h_dinode
)
317 gen
= au_iigen(inode
, &igflags
);
318 if (gen
== au_digen(dentry
)
319 && !au_ig_ftest(igflags
, HALF_REFRESHED
))
322 /* fully refresh inode using dentry */
323 err
= au_refresh_hinode(inode
, dentry
);
325 au_update_iigen(inode
, /*half*/0);
330 ii_write_unlock(inode
);
335 int au_ino(struct super_block
*sb
, aufs_bindex_t bindex
, ino_t h_ino
,
336 unsigned int d_type
, ino_t
*ino
)
341 /* prevent hardlinked inode number from race condition */
343 if (d_type
!= DT_DIR
) {
344 mtx
= &au_sbr(sb
, bindex
)->br_xino
.xi_nondir_mtx
;
347 err
= au_xino_read(sb
, bindex
, h_ino
, ino
);
353 *ino
= au_xino_new_ino(sb
);
356 err
= au_xino_write(sb
, bindex
, h_ino
, *ino
);
367 /* successful returns with iinfo write_locked */
368 /* todo: return with unlocked? */
369 struct inode
*au_new_inode(struct dentry
*dentry
, int must_new
)
371 struct inode
*inode
, *h_inode
;
372 struct dentry
*h_dentry
;
373 struct super_block
*sb
;
380 btop
= au_dbtop(dentry
);
381 h_dentry
= au_h_dptr(dentry
, btop
);
382 h_inode
= d_inode(h_dentry
);
383 h_ino
= h_inode
->i_ino
;
386 * stop 'race'-ing between hardlinks under different
390 if (!d_is_dir(h_dentry
))
391 mtx
= &au_sbr(sb
, btop
)->br_xino
.xi_nondir_mtx
;
396 err
= au_xino_read(sb
, btop
, h_ino
, &ino
);
397 inode
= ERR_PTR(err
);
402 ino
= au_xino_new_ino(sb
);
403 if (unlikely(!ino
)) {
404 inode
= ERR_PTR(-EIO
);
409 AuDbg("i%lu\n", (unsigned long)ino
);
410 inode
= au_iget_locked(sb
, ino
);
411 err
= PTR_ERR(inode
);
415 AuDbg("%lx, new %d\n", inode
->i_state
, !!(inode
->i_state
& I_NEW
));
416 if (inode
->i_state
& I_NEW
) {
417 ii_write_lock_new_child(inode
);
418 err
= set_inode(inode
, dentry
);
420 unlock_new_inode(inode
);
421 goto out
; /* success */
425 * iget_failed() calls iput(), but we need to call
426 * ii_write_unlock() after iget_failed(). so dirty hack for
429 atomic_inc(&inode
->i_count
);
431 ii_write_unlock(inode
);
432 au_xino_write(sb
, btop
, h_ino
, /*ino*/0);
433 /* ignore this error */
435 } else if (!must_new
&& !IS_DEADDIR(inode
) && inode
->i_nlink
) {
437 * horrible race condition between lookup, readdir and copyup
442 err
= reval_inode(inode
, dentry
);
443 if (unlikely(err
< 0)) {
450 goto out
; /* success */
455 if (unlikely(au_test_fs_unique_ino(h_inode
)))
456 AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
457 " b%d, %s, %pd, hi%lu, i%lu.\n",
458 btop
, au_sbtype(h_dentry
->d_sb
), dentry
,
459 (unsigned long)h_ino
, (unsigned long)ino
);
461 err
= au_xino_write(sb
, btop
, h_ino
, /*ino*/0);
471 inode
= ERR_PTR(err
);
478 /* ---------------------------------------------------------------------- */
480 int au_test_ro(struct super_block
*sb
, aufs_bindex_t bindex
,
486 err
= au_br_rdonly(au_sbr(sb
, bindex
));
488 /* pseudo-link after flushed may happen out of bounds */
491 && au_ibtop(inode
) <= bindex
492 && bindex
<= au_ibbot(inode
)) {
494 * permission check is unnecessary since vfsub routine
495 * will be called later
497 hi
= au_h_iptr(inode
, bindex
);
499 err
= IS_IMMUTABLE(hi
) ? -EROFS
: 0;
505 int au_test_h_perm(struct inode
*h_inode
, int mask
)
507 if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID
))
509 return inode_permission(h_inode
, mask
);
512 int au_test_h_perm_sio(struct inode
*h_inode
, int mask
)
514 if (au_test_nfs(h_inode
->i_sb
)
515 && (mask
& MAY_WRITE
)
516 && S_ISDIR(h_inode
->i_mode
))
517 mask
|= MAY_READ
; /* force permission check */
518 return au_test_h_perm(h_inode
, mask
);