]>
git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - fs/aufs/inode.c
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2005-2020 Junjiro R. Okajima
5 * This program, aufs is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <linux/iversion.h>
26 struct inode
*au_igrab(struct inode
*inode
)
29 AuDebugOn(!atomic_read(&inode
->i_count
));
35 static void au_refresh_hinode_attr(struct inode
*inode
, int do_version
)
37 au_cpup_attr_all(inode
, /*force*/0);
38 au_update_iigen(inode
, /*half*/1);
40 inode_inc_iversion(inode
);
43 static int au_ii_refresh(struct inode
*inode
, int *update
)
47 aufs_bindex_t bindex
, new_bindex
;
48 struct super_block
*sb
;
49 struct au_iinfo
*iinfo
;
50 struct au_hinode
*p
, *q
, tmp
;
52 AuDebugOn(au_is_bad_inode(inode
));
53 IiMustWriteLock(inode
);
57 nbr
= au_sbbot(sb
) + 1;
58 type
= inode
->i_mode
& S_IFMT
;
60 err
= au_hinode_realloc(iinfo
, nbr
, /*may_shrink*/0);
64 AuDebugOn(iinfo
->ii_btop
< 0);
65 p
= au_hinode(iinfo
, iinfo
->ii_btop
);
66 for (bindex
= iinfo
->ii_btop
; bindex
<= iinfo
->ii_bbot
;
71 AuDebugOn(type
!= (p
->hi_inode
->i_mode
& S_IFMT
));
72 new_bindex
= au_br_index(sb
, p
->hi_id
);
73 if (new_bindex
== bindex
)
83 if (new_bindex
< iinfo
->ii_btop
)
84 iinfo
->ii_btop
= new_bindex
;
85 if (iinfo
->ii_bbot
< new_bindex
)
86 iinfo
->ii_bbot
= new_bindex
;
87 /* swap two lower inode, and loop again */
88 q
= au_hinode(iinfo
, new_bindex
);
97 au_update_ibrange(inode
, /*do_put_zero*/0);
98 au_hinode_realloc(iinfo
, nbr
, /*may_shrink*/1); /* harmless if err */
99 e
= au_dy_irefresh(inode
);
100 if (unlikely(e
&& !err
))
108 void au_refresh_iop(struct inode
*inode
, int force_getattr
)
111 struct au_sbinfo
*sbi
= au_sbi(inode
->i_sb
);
112 const struct inode_operations
*iop
113 = force_getattr
? aufs_iop
: sbi
->si_iop_array
;
115 if (inode
->i_op
== iop
)
118 switch (inode
->i_mode
& S_IFMT
) {
123 type
= AuIop_SYMLINK
;
130 inode
->i_op
= iop
+ type
;
131 /* unnecessary smp_wmb() */
134 int au_refresh_hinode_self(struct inode
*inode
)
138 err
= au_ii_refresh(inode
, &update
);
140 au_refresh_hinode_attr(inode
, update
&& S_ISDIR(inode
->i_mode
));
146 int au_refresh_hinode(struct inode
*inode
, struct dentry
*dentry
)
151 aufs_bindex_t bindex
, bbot
;
154 struct au_iinfo
*iinfo
;
156 err
= au_ii_refresh(inode
, &update
);
161 iinfo
= au_ii(inode
);
162 p
= au_hinode(iinfo
, iinfo
->ii_btop
);
163 mode
= (inode
->i_mode
& S_IFMT
);
164 isdir
= S_ISDIR(mode
);
165 flags
= au_hi_flags(inode
, isdir
);
166 bbot
= au_dbbot(dentry
);
167 for (bindex
= au_dbtop(dentry
); bindex
<= bbot
; bindex
++) {
168 struct inode
*h_i
, *h_inode
;
171 h_d
= au_h_dptr(dentry
, bindex
);
172 if (!h_d
|| d_is_negative(h_d
))
175 h_inode
= d_inode(h_d
);
176 AuDebugOn(mode
!= (h_inode
->i_mode
& S_IFMT
));
177 if (iinfo
->ii_btop
<= bindex
&& bindex
<= iinfo
->ii_bbot
) {
178 h_i
= au_h_iptr(inode
, bindex
);
186 if (bindex
< iinfo
->ii_btop
)
187 iinfo
->ii_btop
= bindex
;
188 if (iinfo
->ii_bbot
< bindex
)
189 iinfo
->ii_bbot
= bindex
;
190 au_set_h_iptr(inode
, bindex
, au_igrab(h_inode
), flags
);
193 au_update_ibrange(inode
, /*do_put_zero*/0);
194 e
= au_dy_irefresh(inode
);
195 if (unlikely(e
&& !err
))
198 au_refresh_hinode_attr(inode
, update
&& isdir
);
205 static int set_inode(struct inode
*inode
, struct dentry
*dentry
)
210 aufs_bindex_t bindex
, btop
, btail
;
212 struct dentry
*h_dentry
;
213 struct inode
*h_inode
;
214 struct au_iinfo
*iinfo
;
215 const struct inode_operations
*iop
;
217 IiMustWriteLock(inode
);
221 iop
= au_sbi(inode
->i_sb
)->si_iop_array
;
222 btop
= au_dbtop(dentry
);
223 h_dentry
= au_h_dptr(dentry
, btop
);
224 h_inode
= d_inode(h_dentry
);
225 mode
= h_inode
->i_mode
;
226 switch (mode
& S_IFMT
) {
228 btail
= au_dbtail(dentry
);
229 inode
->i_op
= iop
+ AuIop_OTHER
;
230 inode
->i_fop
= &aufs_file_fop
;
231 err
= au_dy_iaop(inode
, btop
, h_inode
);
237 btail
= au_dbtaildir(dentry
);
238 inode
->i_op
= iop
+ AuIop_DIR
;
239 inode
->i_fop
= &aufs_dir_fop
;
242 btail
= au_dbtail(dentry
);
243 inode
->i_op
= iop
+ AuIop_SYMLINK
;
249 btail
= au_dbtail(dentry
);
250 inode
->i_op
= iop
+ AuIop_OTHER
;
251 init_special_inode(inode
, mode
, h_inode
->i_rdev
);
254 AuIOErr("Unknown file type 0%o\n", mode
);
259 /* do not set hnotify for whiteouted dirs (SHWH mode) */
260 flags
= au_hi_flags(inode
, isdir
);
261 if (au_opt_test(au_mntflags(dentry
->d_sb
), SHWH
)
262 && au_ftest_hi(flags
, HNOTIFY
)
263 && dentry
->d_name
.len
> AUFS_WH_PFX_LEN
264 && !memcmp(dentry
->d_name
.name
, AUFS_WH_PFX
, AUFS_WH_PFX_LEN
))
265 au_fclr_hi(flags
, HNOTIFY
);
266 iinfo
= au_ii(inode
);
267 iinfo
->ii_btop
= btop
;
268 iinfo
->ii_bbot
= btail
;
269 for (bindex
= btop
; bindex
<= btail
; bindex
++) {
270 h_dentry
= au_h_dptr(dentry
, bindex
);
272 au_set_h_iptr(inode
, bindex
,
273 au_igrab(d_inode(h_dentry
)), flags
);
275 au_cpup_attr_all(inode
, /*force*/1);
277 * to force calling aufs_get_acl() every time,
278 * do not call cache_no_acl() for aufs inode.
286 * successful returns with iinfo write_locked
288 * zero: success, matched
289 * plus: no error, but unmatched
291 static int reval_inode(struct inode
*inode
, struct dentry
*dentry
)
294 unsigned int gen
, igflags
;
295 aufs_bindex_t bindex
, bbot
;
296 struct inode
*h_inode
, *h_dinode
;
297 struct dentry
*h_dentry
;
300 * before this function, if aufs got any iinfo lock, it must be only
301 * one, the parent dir.
302 * it can happen by UDBA and the obsoleted inode number.
305 if (unlikely(inode
->i_ino
== parent_ino(dentry
)))
309 ii_write_lock_new_child(inode
);
310 h_dentry
= au_h_dptr(dentry
, au_dbtop(dentry
));
311 h_dinode
= d_inode(h_dentry
);
312 bbot
= au_ibbot(inode
);
313 for (bindex
= au_ibtop(inode
); bindex
<= bbot
; bindex
++) {
314 h_inode
= au_h_iptr(inode
, bindex
);
315 if (!h_inode
|| h_inode
!= h_dinode
)
319 gen
= au_iigen(inode
, &igflags
);
320 if (gen
== au_digen(dentry
)
321 && !au_ig_ftest(igflags
, HALF_REFRESHED
))
324 /* fully refresh inode using dentry */
325 err
= au_refresh_hinode(inode
, dentry
);
327 au_update_iigen(inode
, /*half*/0);
332 ii_write_unlock(inode
);
337 int au_ino(struct super_block
*sb
, aufs_bindex_t bindex
, ino_t h_ino
,
338 unsigned int d_type
, ino_t
*ino
)
341 const int isnondir
= d_type
!= DT_DIR
;
343 /* prevent hardlinked inode number from race condition */
345 err
= au_xinondir_enter(sb
, bindex
, h_ino
, &idx
);
350 err
= au_xino_read(sb
, bindex
, h_ino
, ino
);
356 *ino
= au_xino_new_ino(sb
);
359 err
= au_xino_write(sb
, bindex
, h_ino
, *ino
);
365 if (isnondir
&& idx
>= 0)
366 au_xinondir_leave(sb
, bindex
, h_ino
, idx
);
371 /* successful returns with iinfo write_locked */
372 /* todo: return with unlocked? */
373 struct inode
*au_new_inode(struct dentry
*dentry
, int must_new
)
375 struct inode
*inode
, *h_inode
;
376 struct dentry
*h_dentry
;
377 struct super_block
*sb
;
379 int err
, idx
, hlinked
;
383 btop
= au_dbtop(dentry
);
384 h_dentry
= au_h_dptr(dentry
, btop
);
385 h_inode
= d_inode(h_dentry
);
386 h_ino
= h_inode
->i_ino
;
387 hlinked
= !d_is_dir(h_dentry
) && h_inode
->i_nlink
> 1;
391 * stop 'race'-ing between hardlinks under different
395 err
= au_xinondir_enter(sb
, btop
, h_ino
, &idx
);
396 inode
= ERR_PTR(err
);
401 err
= au_xino_read(sb
, btop
, h_ino
, &ino
);
402 inode
= ERR_PTR(err
);
407 ino
= au_xino_new_ino(sb
);
408 if (unlikely(!ino
)) {
409 inode
= ERR_PTR(-EIO
);
414 AuDbg("i%lu\n", (unsigned long)ino
);
415 inode
= au_iget_locked(sb
, ino
);
416 err
= PTR_ERR(inode
);
420 AuDbg("%lx, new %d\n", inode
->i_state
, !!(inode
->i_state
& I_NEW
));
421 if (inode
->i_state
& I_NEW
) {
422 ii_write_lock_new_child(inode
);
423 err
= set_inode(inode
, dentry
);
425 unlock_new_inode(inode
);
426 goto out_xinondir
; /* success */
430 * iget_failed() calls iput(), but we need to call
431 * ii_write_unlock() after iget_failed(). so dirty hack for
434 atomic_inc(&inode
->i_count
);
436 ii_write_unlock(inode
);
437 au_xino_write(sb
, btop
, h_ino
, /*ino*/0);
438 /* ignore this error */
440 } else if (!must_new
&& !IS_DEADDIR(inode
) && inode
->i_nlink
) {
442 * horrible race condition between lookup, readdir and copyup
445 if (hlinked
&& idx
>= 0)
446 au_xinondir_leave(sb
, btop
, h_ino
, idx
);
447 err
= reval_inode(inode
, dentry
);
448 if (unlikely(err
< 0)) {
453 goto out
; /* success */
454 else if (hlinked
&& idx
>= 0) {
455 err
= au_xinondir_enter(sb
, btop
, h_ino
, &idx
);
458 inode
= ERR_PTR(err
);
464 if (unlikely(au_test_fs_unique_ino(h_inode
)))
465 AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
466 " b%d, %s, %pd, hi%lu, i%lu.\n",
467 btop
, au_sbtype(h_dentry
->d_sb
), dentry
,
468 (unsigned long)h_ino
, (unsigned long)ino
);
470 err
= au_xino_write(sb
, btop
, h_ino
, /*ino*/0);
473 if (hlinked
&& idx
>= 0)
474 au_xinondir_leave(sb
, btop
, h_ino
, idx
);
480 inode
= ERR_PTR(err
);
482 if (hlinked
&& idx
>= 0)
483 au_xinondir_leave(sb
, btop
, h_ino
, idx
);
488 /* ---------------------------------------------------------------------- */
490 int au_test_ro(struct super_block
*sb
, aufs_bindex_t bindex
,
496 err
= au_br_rdonly(au_sbr(sb
, bindex
));
498 /* pseudo-link after flushed may happen out of bounds */
501 && au_ibtop(inode
) <= bindex
502 && bindex
<= au_ibbot(inode
)) {
504 * permission check is unnecessary since vfsub routine
505 * will be called later
507 hi
= au_h_iptr(inode
, bindex
);
509 err
= IS_IMMUTABLE(hi
) ? -EROFS
: 0;
515 int au_test_h_perm(struct inode
*h_inode
, int mask
)
517 if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID
))
519 return inode_permission(h_inode
, mask
);
522 int au_test_h_perm_sio(struct inode
*h_inode
, int mask
)
524 if (au_test_nfs(h_inode
->i_sb
)
525 && (mask
& MAY_WRITE
)
526 && S_ISDIR(h_inode
->i_mode
))
527 mask
|= MAY_READ
; /* force permission check */
528 return au_test_h_perm(h_inode
, mask
);