2 * Copyright (C) 2005-2012 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, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 struct inode *au_igrab(struct inode *inode)
28 AuDebugOn(!atomic_read(&inode->i_count));
34 static void au_refresh_hinode_attr(struct inode *inode, int do_version)
36 au_cpup_attr_all(inode, /*force*/0);
37 au_update_iigen(inode);
42 static int au_ii_refresh(struct inode *inode, int *update)
46 aufs_bindex_t bindex, new_bindex;
47 struct super_block *sb;
48 struct au_iinfo *iinfo;
49 struct au_hinode *p, *q, tmp;
51 IiMustWriteLock(inode);
55 type = inode->i_mode & S_IFMT;
57 err = au_ii_realloc(iinfo, au_sbend(sb) + 1);
61 AuDebugOn(iinfo->ii_bstart < 0);
62 p = iinfo->ii_hinode + iinfo->ii_bstart;
63 for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
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)
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;
94 au_update_ibrange(inode, /*do_put_zero*/0);
95 e = au_dy_irefresh(inode);
96 if (unlikely(e && !err))
104 int au_refresh_hinode_self(struct inode *inode)
108 err = au_ii_refresh(inode, &update);
110 au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
116 int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
121 aufs_bindex_t bindex, bend;
124 struct au_iinfo *iinfo;
126 err = au_ii_refresh(inode, &update);
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++) {
141 h_d = au_h_dptr(dentry, bindex);
142 if (!h_d || !h_d->d_inode)
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);
149 if (h_i == h_d->d_inode)
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);
162 au_update_ibrange(inode, /*do_put_zero*/0);
163 e = au_dy_irefresh(inode);
164 if (unlikely(e && !err))
167 au_refresh_hinode_attr(inode, update && isdir);
174 static int set_inode(struct inode *inode, struct dentry *dentry)
179 aufs_bindex_t bindex, bstart, btail;
181 struct dentry *h_dentry;
182 struct inode *h_inode;
183 struct au_iinfo *iinfo;
185 IiMustWriteLock(inode);
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) {
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);
203 btail = au_dbtaildir(dentry);
204 inode->i_op = &aufs_dir_iop;
205 inode->i_fop = &aufs_dir_fop;
208 btail = au_dbtail(dentry);
209 inode->i_op = &aufs_symlink_iop;
215 btail = au_dbtail(dentry);
216 inode->i_op = &aufs_iop;
217 au_init_special_fop(inode, mode, h_inode->i_rdev);
220 AuIOErr("Unknown file type 0%o\n", mode);
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);
238 au_set_h_iptr(inode, bindex,
239 au_igrab(h_dentry->d_inode), flags);
241 au_cpup_attr_all(inode, /*force*/1);
248 * successful returns with iinfo write_locked
250 * zero: success, matched
251 * plus: no error, but unmatched
253 static int reval_inode(struct inode *inode, struct dentry *dentry)
256 aufs_bindex_t bindex, bend;
257 struct inode *h_inode, *h_dinode;
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.
265 if (unlikely(inode->i_ino == parent_ino(dentry)))
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) {
276 if (au_iigen_test(inode, au_digen(dentry)))
277 err = au_refresh_hinode(inode, dentry);
283 ii_write_unlock(inode);
288 int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
289 unsigned int d_type, ino_t *ino)
294 /* prevent hardlinked inode number from race condition */
296 if (d_type != DT_DIR) {
297 mtx = &au_sbr(sb, bindex)->br_xino.xi_nondir_mtx;
300 err = au_xino_read(sb, bindex, h_ino, ino);
306 *ino = au_xino_new_ino(sb);
309 err = au_xino_write(sb, bindex, h_ino, *ino);
320 /* successful returns with iinfo write_locked */
321 /* todo: return with unlocked? */
322 struct inode *au_new_inode(struct dentry *dentry, int must_new)
324 struct inode *inode, *h_inode;
325 struct dentry *h_dentry;
326 struct super_block *sb;
330 aufs_bindex_t bstart;
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;
339 * stop 'race'-ing between hardlinks under different
343 if (!S_ISDIR(h_inode->i_mode))
344 mtx = &au_sbr(sb, bstart)->br_xino.xi_nondir_mtx;
349 err = au_xino_read(sb, bstart, h_ino, &ino);
350 inode = ERR_PTR(err);
355 ino = au_xino_new_ino(sb);
356 if (unlikely(!ino)) {
357 inode = ERR_PTR(-EIO);
362 AuDbg("i%lu\n", (unsigned long)ino);
363 inode = au_iget_locked(sb, ino);
364 err = PTR_ERR(inode);
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);
373 unlock_new_inode(inode);
374 goto out; /* success */
378 * iget_failed() calls iput(), but we need to call
379 * ii_write_unlock() after iget_failed(). so dirty hack for
382 atomic_inc(&inode->i_count);
384 ii_write_unlock(inode);
385 au_xino_write(sb, bstart, h_ino, /*ino*/0);
386 /* ignore this error */
388 } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
390 * horrible race condition between lookup, readdir and copyup
395 err = reval_inode(inode, dentry);
396 if (unlikely(err < 0)) {
403 goto out; /* success */
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);
414 err = au_xino_write(sb, bstart, h_ino, /*ino*/0);
424 inode = ERR_PTR(err);
431 /* ---------------------------------------------------------------------- */
433 int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
438 err = au_br_rdonly(au_sbr(sb, bindex));
440 /* pseudo-link after flushed may happen out of bounds */
443 && au_ibstart(inode) <= bindex
444 && bindex <= au_ibend(inode)) {
446 * permission check is unnecessary since vfsub routine
447 * will be called later
449 struct inode *hi = au_h_iptr(inode, bindex);
451 err = IS_IMMUTABLE(hi) ? -EROFS : 0;
457 int au_test_h_perm(struct inode *h_inode, int mask)
459 if (!current_fsuid())
461 return inode_permission(h_inode, mask);
464 int au_test_h_perm_sio(struct inode *h_inode, int mask)
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);