static inline int __realloc_dentry_private_data(struct dentry *dentry) { struct unionfs_dentry_info *info = UNIONFS_D(dentry); void *p; int size; BUG_ON(!info); size = sizeof(struct path) * sbmax(dentry->d_sb); p = krealloc(info->lower_paths, size, GFP_ATOMIC); if (unlikely(!p)) return -ENOMEM; info->lower_paths = p; info->bstart = -1; info->bend = -1; info->bopaque = -1; info->bcount = sbmax(dentry->d_sb); atomic_set(&info->generation, atomic_read(&UNIONFS_SB(dentry->d_sb)->generation)); memset(info->lower_paths, 0, size); return 0; }
static void unionfs_read_inode(struct inode *inode) { static struct address_space_operations unionfs_empty_aops; print_entry_location(); if (!itopd(inode)) { FISTBUG ("No kernel memory when allocating inode private data!\n"); } PASSERT(inode->i_sb); memset(itopd(inode), 0, sizeof(struct unionfs_inode_info)); itopd(inode)->b_start = -1; itopd(inode)->b_end = -1; atomic_set(&itopd(inode)->uii_generation, atomic_read(&stopd(inode->i_sb)->usi_generation)); itopd(inode)->uii_rdlock = SPIN_LOCK_UNLOCKED; itopd(inode)->uii_rdcount = 1; itopd(inode)->uii_hashsize = -1; INIT_LIST_HEAD(&itopd(inode)->uii_readdircache); if (sbmax(inode->i_sb) > UNIONFS_INLINE_OBJECTS) { int size = (sbmax(inode->i_sb) - UNIONFS_INLINE_OBJECTS) * sizeof(struct inode *); itohi_ptr(inode) = KMALLOC(size, GFP_UNIONFS); if (!itohi_ptr(inode)) { FISTBUG ("No kernel memory when allocating lower-pointer array!\n"); } memset(itohi_ptr(inode), 0, size); } memset(itohi_inline(inode), 0, UNIONFS_INLINE_OBJECTS * sizeof(struct inode *)); inode->i_version++; inode->i_op = &unionfs_main_iops; inode->i_fop = &unionfs_main_fops; /* I don't think ->a_ops is ever allowed to be NULL */ inode->i_mapping->a_ops = &unionfs_empty_aops; fist_dprint(7, "setting inode 0x%p a_ops to empty (0x%p)\n", inode, inode->i_mapping->a_ops); print_exit_location(); }
/* useful to track vfsmount leaks that could cause EBUSY on unmount */ void __show_branch_counts(const struct super_block *sb, const char *file, const char *fxn, int line) { int i; struct vfsmount *mnt; pr_debug("BC:"); for (i = 0; i < sbmax(sb); i++) { if (likely(sb->s_root)) mnt = UNIONFS_D(sb->s_root)->lower_paths[i].mnt; else mnt = NULL; printk(KERN_CONT "%d:", (mnt ? atomic_read(&mnt->mnt_count) : -99)); } printk(KERN_CONT "%s:%s:%d\n", file, fxn, line); }
/* * __unionfs_check_{inode,dentry,file} perform exhaustive sanity checking on * the fan-out of various Unionfs objects. We check that no lower objects * exist outside the start/end branch range; that all objects within are * non-NULL (with some allowed exceptions); that for every lower file * there's a lower dentry+inode; that the start/end ranges match for all * corresponding lower objects; that open files/symlinks have only one lower * objects, but directories can have several; and more. */ void __unionfs_check_inode(const struct inode *inode, const char *fname, const char *fxn, int line) { int bindex; int istart, iend; struct inode *lower_inode; struct super_block *sb; int printed_caller = 0; void *poison_ptr; /* for inodes now */ BUG_ON(!inode); sb = inode->i_sb; istart = ibstart(inode); iend = ibend(inode); /* don't check inode if no lower branches */ if (istart < 0 && iend < 0) return; if (unlikely(istart > iend)) { PRINT_CALLER(fname, fxn, line); pr_debug(" Ci0: inode=%p istart/end=%d:%d\n", inode, istart, iend); } if (unlikely((istart == -1 && iend != -1) || (istart != -1 && iend == -1))) { PRINT_CALLER(fname, fxn, line); pr_debug(" Ci1: inode=%p istart/end=%d:%d\n", inode, istart, iend); } if (!S_ISDIR(inode->i_mode)) { if (unlikely(iend != istart)) { PRINT_CALLER(fname, fxn, line); pr_debug(" Ci2: inode=%p istart=%d iend=%d\n", inode, istart, iend); } } for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) { if (unlikely(!UNIONFS_I(inode))) { PRINT_CALLER(fname, fxn, line); pr_debug(" Ci3: no inode_info %p\n", inode); return; } if (unlikely(!UNIONFS_I(inode)->lower_inodes)) { PRINT_CALLER(fname, fxn, line); pr_debug(" Ci4: no lower_inodes %p\n", inode); return; } lower_inode = unionfs_lower_inode_idx(inode, bindex); if (lower_inode) { memset(&poison_ptr, POISON_INUSE, sizeof(void *)); if (unlikely(bindex < istart || bindex > iend)) { PRINT_CALLER(fname, fxn, line); pr_debug(" Ci5: inode/linode=%p:%p bindex=%d " "istart/end=%d:%d\n", inode, lower_inode, bindex, istart, iend); } else if (unlikely(lower_inode == poison_ptr)) { /* freed inode! */ PRINT_CALLER(fname, fxn, line); pr_debug(" Ci6: inode/linode=%p:%p bindex=%d " "istart/end=%d:%d\n", inode, lower_inode, bindex, istart, iend); } continue; } /* if we get here, then lower_inode == NULL */ if (bindex < istart || bindex > iend) continue; /* * directories can have NULL lower inodes in b/t start/end, * but NOT if at the start/end range. */ if (unlikely(S_ISDIR(inode->i_mode) && bindex > istart && bindex < iend)) continue; PRINT_CALLER(fname, fxn, line); pr_debug(" Ci7: inode/linode=%p:%p " "bindex=%d istart/end=%d:%d\n", inode, lower_inode, bindex, istart, iend); } }
void __unionfs_check_file(const struct file *file, const char *fname, const char *fxn, int line) { int bindex; int dstart, dend, fstart, fend; struct dentry *dentry; struct file *lower_file; struct inode *inode; struct super_block *sb; int printed_caller = 0; BUG_ON(!file); dentry = file->f_path.dentry; sb = dentry->d_sb; dstart = dbstart(dentry); dend = dbend(dentry); BUG_ON(dstart > dend); fstart = fbstart(file); fend = fbend(file); BUG_ON(fstart > fend); if (unlikely((fstart == -1 && fend != -1) || (fstart != -1 && fend == -1))) { PRINT_CALLER(fname, fxn, line); pr_debug(" CF0: file/dentry=%p:%p fstart/end=%d:%d\n", file, dentry, fstart, fend); } if (unlikely(fstart != dstart)) { PRINT_CALLER(fname, fxn, line); pr_debug(" CF1: file/dentry=%p:%p fstart=%d dstart=%d\n", file, dentry, fstart, dstart); } if (unlikely(fend != dend)) { PRINT_CALLER(fname, fxn, line); pr_debug(" CF2: file/dentry=%p:%p fend=%d dend=%d\n", file, dentry, fend, dend); } inode = dentry->d_inode; if (!S_ISDIR(inode->i_mode)) { if (unlikely(fend != fstart)) { PRINT_CALLER(fname, fxn, line); pr_debug(" CF3: file/inode=%p:%p fstart=%d fend=%d\n", file, inode, fstart, fend); } if (unlikely(dend != dstart)) { PRINT_CALLER(fname, fxn, line); pr_debug(" CF4: file/dentry=%p:%p dstart=%d dend=%d\n", file, dentry, dstart, dend); } } /* * check for NULL dentries inside the start/end range, or * non-NULL dentries outside the start/end range. */ for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) { lower_file = unionfs_lower_file_idx(file, bindex); if (lower_file) { if (unlikely(bindex < fstart || bindex > fend)) { PRINT_CALLER(fname, fxn, line); pr_debug(" CF5: file/lower=%p:%p bindex=%d " "fstart/end=%d:%d\n", file, lower_file, bindex, fstart, fend); } } else { /* lower_file == NULL */ if (bindex >= fstart && bindex <= fend) { /* * directories can have NULL lower inodes in * b/t start/end, but NOT if at the * start/end range. */ if (unlikely(!(S_ISDIR(inode->i_mode) && bindex > fstart && bindex < fend))) { PRINT_CALLER(fname, fxn, line); pr_debug(" CF6: file/lower=%p:%p " "bindex=%d fstart/end=%d:%d\n", file, lower_file, bindex, fstart, fend); } } } } __unionfs_check_dentry(dentry, fname, fxn, line); }
void __unionfs_check_dentry(const struct dentry *dentry, const char *fname, const char *fxn, int line) { int bindex; int dstart, dend, istart, iend; struct dentry *lower_dentry; struct inode *inode, *lower_inode; struct super_block *sb; struct vfsmount *lower_mnt; int printed_caller = 0; void *poison_ptr; BUG_ON(!dentry); sb = dentry->d_sb; inode = dentry->d_inode; dstart = dbstart(dentry); dend = dbend(dentry); /* don't check dentry/mnt if no lower branches */ if (dstart < 0 && dend < 0) goto check_inode; BUG_ON(dstart > dend); if (unlikely((dstart == -1 && dend != -1) || (dstart != -1 && dend == -1))) { PRINT_CALLER(fname, fxn, line); pr_debug(" CD0: dentry=%p dstart/end=%d:%d\n", dentry, dstart, dend); } /* * check for NULL dentries inside the start/end range, or * non-NULL dentries outside the start/end range. */ for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) { lower_dentry = unionfs_lower_dentry_idx(dentry, bindex); if (lower_dentry) { if (unlikely(bindex < dstart || bindex > dend)) { PRINT_CALLER(fname, fxn, line); pr_debug(" CD1: dentry/lower=%p:%p(%p) " "bindex=%d dstart/end=%d:%d\n", dentry, lower_dentry, (lower_dentry ? lower_dentry->d_inode : (void *) -1L), bindex, dstart, dend); } } else { /* lower_dentry == NULL */ if (bindex < dstart || bindex > dend) continue; /* * Directories can have NULL lower inodes in b/t * start/end, but NOT if at the start/end range. * Ignore this rule, however, if this is a NULL * dentry or a deleted dentry. */ if (unlikely(!d_deleted((struct dentry *) dentry) && inode && !(inode && S_ISDIR(inode->i_mode) && bindex > dstart && bindex < dend))) { PRINT_CALLER(fname, fxn, line); pr_debug(" CD2: dentry/lower=%p:%p(%p) " "bindex=%d dstart/end=%d:%d\n", dentry, lower_dentry, (lower_dentry ? lower_dentry->d_inode : (void *) -1L), bindex, dstart, dend); } } } /* check for vfsmounts same as for dentries */ for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) { lower_mnt = unionfs_lower_mnt_idx(dentry, bindex); if (lower_mnt) { if (unlikely(bindex < dstart || bindex > dend)) { PRINT_CALLER(fname, fxn, line); pr_debug(" CM0: dentry/lmnt=%p:%p bindex=%d " "dstart/end=%d:%d\n", dentry, lower_mnt, bindex, dstart, dend); } } else { /* lower_mnt == NULL */ if (bindex < dstart || bindex > dend) continue; /* * Directories can have NULL lower inodes in b/t * start/end, but NOT if at the start/end range. * Ignore this rule, however, if this is a NULL * dentry. */ if (unlikely(inode && !(inode && S_ISDIR(inode->i_mode) && bindex > dstart && bindex < dend))) { PRINT_CALLER(fname, fxn, line); pr_debug(" CM1: dentry/lmnt=%p:%p " "bindex=%d dstart/end=%d:%d\n", dentry, lower_mnt, bindex, dstart, dend); } } } check_inode: /* for inodes now */ if (!inode) return; istart = ibstart(inode); iend = ibend(inode); /* don't check inode if no lower branches */ if (istart < 0 && iend < 0) return; BUG_ON(istart > iend); if (unlikely((istart == -1 && iend != -1) || (istart != -1 && iend == -1))) { PRINT_CALLER(fname, fxn, line); pr_debug(" CI0: dentry/inode=%p:%p istart/end=%d:%d\n", dentry, inode, istart, iend); } if (unlikely(istart != dstart)) { PRINT_CALLER(fname, fxn, line); pr_debug(" CI1: dentry/inode=%p:%p istart=%d dstart=%d\n", dentry, inode, istart, dstart); } if (unlikely(iend != dend)) { PRINT_CALLER(fname, fxn, line); pr_debug(" CI2: dentry/inode=%p:%p iend=%d dend=%d\n", dentry, inode, iend, dend); } if (!S_ISDIR(inode->i_mode)) { if (unlikely(dend != dstart)) { PRINT_CALLER(fname, fxn, line); pr_debug(" CI3: dentry/inode=%p:%p dstart=%d dend=%d\n", dentry, inode, dstart, dend); } if (unlikely(iend != istart)) { PRINT_CALLER(fname, fxn, line); pr_debug(" CI4: dentry/inode=%p:%p istart=%d iend=%d\n", dentry, inode, istart, iend); } } for (bindex = sbstart(sb); bindex < sbmax(sb); bindex++) { lower_inode = unionfs_lower_inode_idx(inode, bindex); if (lower_inode) { memset(&poison_ptr, POISON_INUSE, sizeof(void *)); if (unlikely(bindex < istart || bindex > iend)) { PRINT_CALLER(fname, fxn, line); pr_debug(" CI5: dentry/linode=%p:%p bindex=%d " "istart/end=%d:%d\n", dentry, lower_inode, bindex, istart, iend); } else if (unlikely(lower_inode == poison_ptr)) { /* freed inode! */ PRINT_CALLER(fname, fxn, line); pr_debug(" CI6: dentry/linode=%p:%p bindex=%d " "istart/end=%d:%d\n", dentry, lower_inode, bindex, istart, iend); } continue; } /* if we get here, then lower_inode == NULL */ if (bindex < istart || bindex > iend) continue; /* * directories can have NULL lower inodes in b/t start/end, * but NOT if at the start/end range. */ if (unlikely(S_ISDIR(inode->i_mode) && bindex > istart && bindex < iend)) continue; PRINT_CALLER(fname, fxn, line); pr_debug(" CI7: dentry/linode=%p:%p " "bindex=%d istart/end=%d:%d\n", dentry, lower_inode, bindex, istart, iend); } /* * If it's a directory, then intermediate objects b/t start/end can * be NULL. But, check that all three are NULL: lower dentry, mnt, * and inode. */ if (dstart >= 0 && dend >= 0 && S_ISDIR(inode->i_mode)) for (bindex = dstart+1; bindex < dend; bindex++) { lower_inode = unionfs_lower_inode_idx(inode, bindex); lower_dentry = unionfs_lower_dentry_idx(dentry, bindex); lower_mnt = unionfs_lower_mnt_idx(dentry, bindex); if (unlikely(!((lower_inode && lower_dentry && lower_mnt) || (!lower_inode && !lower_dentry && !lower_mnt)))) { PRINT_CALLER(fname, fxn, line); pr_debug(" Cx: lmnt/ldentry/linode=%p:%p:%p " "bindex=%d dstart/end=%d:%d\n", lower_mnt, lower_dentry, lower_inode, bindex, dstart, dend); } } /* check if lower inode is newer than upper one (it shouldn't) */ if (unlikely(is_newer_lower(dentry) && !is_negative_lower(dentry))) { PRINT_CALLER(fname, fxn, line); for (bindex = ibstart(inode); bindex <= ibend(inode); bindex++) { lower_inode = unionfs_lower_inode_idx(inode, bindex); if (unlikely(!lower_inode)) continue; pr_debug(" CI8: bindex=%d mtime/lmtime=%lu.%lu/%lu.%lu " "ctime/lctime=%lu.%lu/%lu.%lu\n", bindex, inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec, lower_inode->i_mtime.tv_sec, lower_inode->i_mtime.tv_nsec, inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec, lower_inode->i_ctime.tv_sec, lower_inode->i_ctime.tv_nsec); } } }
struct dentry *unionfs_lookup_backend(struct dentry *dentry, struct nameidata *nd, int lookupmode) { int err = 0; struct dentry *hidden_dentry = NULL; struct dentry *wh_hidden_dentry = NULL; struct dentry *hidden_dir_dentry = NULL; struct dentry *parent_dentry = NULL; int bindex, bstart, bend, bopaque; int dentry_count = 0; /* Number of positive dentries. */ int first_dentry_offset = -1; struct dentry *first_hidden_dentry = NULL; struct vfsmount *first_hidden_mnt = NULL; int locked_parent = 0; int locked_child = 0; int opaque; char *whname = NULL; const char *name; int namelen; /* We should already have a lock on this dentry in the case of a * partial lookup, or a revalidation. Otherwise it is returned from * new_dentry_private_data already locked. */ if (lookupmode == INTERPOSE_PARTIAL || lookupmode == INTERPOSE_REVAL || lookupmode == INTERPOSE_REVAL_NEG) verify_locked(dentry); else { BUG_ON(UNIONFS_D(dentry) != NULL); locked_child = 1; } if (lookupmode != INTERPOSE_PARTIAL) if ((err = new_dentry_private_data(dentry))) goto out; /* must initialize dentry operations */ dentry->d_op = &unionfs_dops; parent_dentry = dget_parent(dentry); /* We never partial lookup the root directory. */ if (parent_dentry != dentry) { unionfs_lock_dentry(parent_dentry); locked_parent = 1; } else { dput(parent_dentry); parent_dentry = NULL; goto out; } name = dentry->d_name.name; namelen = dentry->d_name.len; /* No dentries should get created for possible whiteout names. */ if (!is_validname(name)) { err = -EPERM; goto out_free; } /* Now start the actual lookup procedure. */ bstart = dbstart(parent_dentry); bend = dbend(parent_dentry); bopaque = dbopaque(parent_dentry); BUG_ON(bstart < 0); /* It would be ideal if we could convert partial lookups to only have * to do this work when they really need to. It could probably improve * performance quite a bit, and maybe simplify the rest of the code. */ if (lookupmode == INTERPOSE_PARTIAL) { bstart++; if ((bopaque != -1) && (bopaque < bend)) bend = bopaque; } for (bindex = bstart; bindex <= bend; bindex++) { hidden_dentry = unionfs_lower_dentry_idx(dentry, bindex); if (lookupmode == INTERPOSE_PARTIAL && hidden_dentry) continue; BUG_ON(hidden_dentry != NULL); hidden_dir_dentry = unionfs_lower_dentry_idx(parent_dentry, bindex); /* if the parent hidden dentry does not exist skip this */ if (!(hidden_dir_dentry && hidden_dir_dentry->d_inode)) continue; /* also skip it if the parent isn't a directory. */ if (!S_ISDIR(hidden_dir_dentry->d_inode->i_mode)) continue; /* Reuse the whiteout name because its value doesn't change. */ if (!whname) { whname = alloc_whname(name, namelen); if (IS_ERR(whname)) { err = PTR_ERR(whname); goto out_free; } } /* check if whiteout exists in this branch: lookup .wh.foo */ wh_hidden_dentry = lookup_one_len(whname, hidden_dir_dentry, namelen + UNIONFS_WHLEN); if (IS_ERR(wh_hidden_dentry)) { dput(first_hidden_dentry); mntput(first_hidden_mnt); err = PTR_ERR(wh_hidden_dentry); goto out_free; } if (wh_hidden_dentry->d_inode) { /* We found a whiteout so lets give up. */ if (S_ISREG(wh_hidden_dentry->d_inode->i_mode)) { set_dbend(dentry, bindex); set_dbopaque(dentry, bindex); dput(wh_hidden_dentry); break; } err = -EIO; printk(KERN_NOTICE "EIO: Invalid whiteout entry type" " %d.\n", wh_hidden_dentry->d_inode->i_mode); dput(wh_hidden_dentry); dput(first_hidden_dentry); mntput(first_hidden_mnt); goto out_free; } dput(wh_hidden_dentry); wh_hidden_dentry = NULL; /* Now do regular lookup; lookup foo */ nd->dentry = unionfs_lower_dentry_idx(dentry, bindex); /* FIXME: fix following line for mount point crossing */ nd->mnt = unionfs_lower_mnt_idx(parent_dentry, bindex); hidden_dentry = lookup_one_len_nd(name, hidden_dir_dentry, namelen, nd); if (IS_ERR(hidden_dentry)) { dput(first_hidden_dentry); mntput(first_hidden_mnt); err = PTR_ERR(hidden_dentry); goto out_free; } /* Store the first negative dentry specially, because if they * are all negative we need this for future creates. */ if (!hidden_dentry->d_inode) { if (!first_hidden_dentry && (dbstart(dentry) == -1)) { first_hidden_dentry = hidden_dentry; /* FIXME: following line needs to be changed * to allow mountpoint crossing */ first_hidden_mnt = mntget( unionfs_lower_mnt_idx(parent_dentry, bindex)); first_dentry_offset = bindex; } else dput(hidden_dentry); continue; } /* number of positive dentries */ dentry_count++; /* store underlying dentry */ if (dbstart(dentry) == -1) set_dbstart(dentry, bindex); unionfs_set_lower_dentry_idx(dentry, bindex, hidden_dentry); /* FIXME: the following line needs to get fixed to allow * mountpoint crossing */ unionfs_set_lower_mnt_idx(dentry, bindex, mntget(unionfs_lower_mnt_idx(parent_dentry, bindex))); set_dbend(dentry, bindex); /* update parent directory's atime with the bindex */ fsstack_copy_attr_atime(parent_dentry->d_inode, hidden_dir_dentry->d_inode); /* We terminate file lookups here. */ if (!S_ISDIR(hidden_dentry->d_inode->i_mode)) { if (lookupmode == INTERPOSE_PARTIAL) continue; if (dentry_count == 1) goto out_positive; /* This can only happen with mixed D-*-F-* */ BUG_ON(!S_ISDIR(unionfs_lower_dentry(dentry)->d_inode->i_mode)); continue; } opaque = is_opaque_dir(dentry, bindex); if (opaque < 0) { dput(first_hidden_dentry); mntput(first_hidden_mnt); err = opaque; goto out_free; } else if (opaque) { set_dbend(dentry, bindex); set_dbopaque(dentry, bindex); break; } } if (dentry_count) goto out_positive; else goto out_negative; out_negative: if (lookupmode == INTERPOSE_PARTIAL) goto out; /* If we've only got negative dentries, then use the leftmost one. */ if (lookupmode == INTERPOSE_REVAL) { if (dentry->d_inode) UNIONFS_I(dentry->d_inode)->stale = 1; goto out; } /* This should only happen if we found a whiteout. */ if (first_dentry_offset == -1) { nd->dentry = dentry; /* FIXME: fix following line for mount point crossing */ nd->mnt = unionfs_lower_mnt_idx(parent_dentry, bindex); first_hidden_dentry = lookup_one_len_nd(name, hidden_dir_dentry, namelen, nd); first_dentry_offset = bindex; if (IS_ERR(first_hidden_dentry)) { err = PTR_ERR(first_hidden_dentry); goto out; } /* FIXME: the following line needs to be changed to allow * mountpoint crossing */ first_hidden_mnt = mntget(unionfs_lower_mnt_idx(dentry, bindex)); } unionfs_set_lower_dentry_idx(dentry, first_dentry_offset, first_hidden_dentry); unionfs_set_lower_mnt_idx(dentry, first_dentry_offset, first_hidden_mnt); set_dbstart(dentry, first_dentry_offset); set_dbend(dentry, first_dentry_offset); if (lookupmode == INTERPOSE_REVAL_NEG) BUG_ON(dentry->d_inode != NULL); else d_add(dentry, NULL); goto out; /* This part of the code is for positive dentries. */ out_positive: BUG_ON(dentry_count <= 0); /* If we're holding onto the first negative dentry & corresponding * vfsmount - throw it out. */ dput(first_hidden_dentry); mntput(first_hidden_mnt); /* Partial lookups need to reinterpose, or throw away older negs. */ if (lookupmode == INTERPOSE_PARTIAL) { if (dentry->d_inode) { unionfs_reinterpose(dentry); goto out; } /* This somehow turned positive, so it is as if we had a * negative revalidation. */ lookupmode = INTERPOSE_REVAL_NEG; update_bstart(dentry); bstart = dbstart(dentry); bend = dbend(dentry); } err = unionfs_interpose(dentry, dentry->d_sb, lookupmode); if (err) goto out_drop; goto out; out_drop: d_drop(dentry); out_free: /* should dput all the underlying dentries on error condition */ bstart = dbstart(dentry); if (bstart >= 0) { bend = dbend(dentry); for (bindex = bstart; bindex <= bend; bindex++) { dput(unionfs_lower_dentry_idx(dentry, bindex)); mntput(unionfs_lower_mnt_idx(dentry, bindex)); } } kfree(UNIONFS_D(dentry)->lower_paths); UNIONFS_D(dentry)->lower_paths = NULL; set_dbstart(dentry, -1); set_dbend(dentry, -1); out: if (!err && UNIONFS_D(dentry)) { BUG_ON(dbend(dentry) > UNIONFS_D(dentry)->bcount); BUG_ON(dbend(dentry) > sbmax(dentry->d_sb)); BUG_ON(dbstart(dentry) < 0); } kfree(whname); if (locked_parent) unionfs_unlock_dentry(parent_dentry); dput(parent_dentry); if (locked_child) unionfs_unlock_dentry(dentry); return ERR_PTR(err); }
/* This must be called with the super block already locked. */ int unionfs_ioctl_delbranch(struct super_block *sb, unsigned long arg) { struct dentry *hidden_dentry; struct inode *hidden_inode; struct super_block *hidden_sb; struct vfsmount *hidden_mnt; struct dentry *root_dentry; struct inode *root_inode; int err = 0; int pmindex, i, gen; print_entry("branch = %lu ", arg); lock_dentry(sb->s_root); err = -EBUSY; if (sbmax(sb) == 1) goto out; err = -EINVAL; if (arg < 0 || arg > stopd(sb)->b_end) goto out; err = -EBUSY; if (branch_count(sb, arg)) goto out; if ((err = newputmap(sb))) goto out; pmindex = stopd(sb)->usi_lastputmap; pmindex -= stopd(sb)->usi_firstputmap; atomic_inc(&stopd(sb)->usi_generation); gen = atomic_read(&stopd(sb)->usi_generation); root_dentry = sb->s_root; root_inode = sb->s_root->d_inode; hidden_dentry = dtohd_index(root_dentry, arg); hidden_mnt = stohiddenmnt_index(sb, arg); hidden_inode = itohi_index(root_inode, arg); hidden_sb = stohs_index(sb, arg); DPUT(hidden_dentry); iput(hidden_inode); mntput(hidden_mnt); for (i = arg; i <= (sbend(sb) - 1); i++) { set_branch_count(sb, i, branch_count(sb, i + 1)); set_stohiddenmnt_index(sb, i, stohiddenmnt_index(sb, i + 1)); set_stohs_index(sb, i, stohs_index(sb, i + 1)); set_branchperms(sb, i, branchperms(sb, i + 1)); set_dtohd_index(root_dentry, i, dtohd_index(root_dentry, i + 1)); set_itohi_index(root_inode, i, itohi_index(root_inode, i + 1)); stopd(sb)->usi_putmaps[pmindex]->map[i + 1] = i; } set_dtohd_index(root_dentry, sbend(sb), NULL); set_itohi_index(root_inode, sbend(sb), NULL); set_stohiddenmnt_index(sb, sbend(sb), NULL); set_stohs_index(sb, sbend(sb), NULL); stopd(sb)->b_end--; set_dbend(root_dentry, dbend(root_dentry) - 1); dtopd(root_dentry)->udi_bcount--; itopd(root_inode)->b_end--; atomic_set(&dtopd(root_dentry)->udi_generation, gen); atomic_set(&itopd(root_inode)->uii_generation, gen); fixputmaps(sb); /* This doesn't open a file, so we might have to free the map here. */ if (atomic_read(&stopd(sb)->usi_putmaps[pmindex]->count) == 0) { KFREE(stopd(sb)->usi_putmaps[pmindex]); stopd(sb)->usi_putmaps[pmindex] = NULL; } out: unlock_dentry(sb->s_root); print_exit_status(err); return err; }
/* * Connect a unionfs inode dentry/inode with several lower ones. This is * the classic stackable file system "vnode interposition" action. * * @sb: unionfs's super_block */ struct dentry *unionfs_interpose(struct dentry *dentry, struct super_block *sb, int flag) { int err = 0; struct inode *inode; int need_fill_inode = 1; struct dentry *spliced = NULL; verify_locked(dentry); /* * We allocate our new inode below, by calling iget. * iget will call our read_inode which will initialize some * of the new inode's fields */ /* * On revalidate we've already got our own inode and just need * to fix it up. */ if (flag == INTERPOSE_REVAL) { inode = dentry->d_inode; UNIONFS_I(inode)->bstart = -1; UNIONFS_I(inode)->bend = -1; atomic_set(&UNIONFS_I(inode)->generation, atomic_read(&UNIONFS_SB(sb)->generation)); UNIONFS_I(inode)->lower_inodes = kcalloc(sbmax(sb), sizeof(struct inode *), GFP_KERNEL); if (unlikely(!UNIONFS_I(inode)->lower_inodes)) { err = -ENOMEM; goto out; } } else { /* get unique inode number for unionfs */ inode = iget(sb, iunique(sb, UNIONFS_ROOT_INO)); if (!inode) { err = -EACCES; goto out; } if (atomic_read(&inode->i_count) > 1) goto skip; } need_fill_inode = 0; unionfs_fill_inode(dentry, inode); skip: /* only (our) lookup wants to do a d_add */ switch (flag) { case INTERPOSE_DEFAULT: /* for operations which create new inodes */ d_add(dentry, inode); break; case INTERPOSE_REVAL_NEG: d_instantiate(dentry, inode); break; case INTERPOSE_LOOKUP: spliced = d_splice_alias(inode, dentry); if (spliced && spliced != dentry) { /* * d_splice can return a dentry if it was * disconnected and had to be moved. We must ensure * that the private data of the new dentry is * correct and that the inode info was filled * properly. Finally we must return this new * dentry. */ spliced->d_op = &unionfs_dops; spliced->d_fsdata = dentry->d_fsdata; dentry->d_fsdata = NULL; dentry = spliced; if (need_fill_inode) { need_fill_inode = 0; unionfs_fill_inode(dentry, inode); } goto out_spliced; } else if (!spliced) { if (need_fill_inode) { need_fill_inode = 0; unionfs_fill_inode(dentry, inode); goto out_spliced; } } break; case INTERPOSE_REVAL: /* Do nothing. */ break; default: printk(KERN_CRIT "unionfs: invalid interpose flag passed!\n"); BUG(); } goto out; out_spliced: if (!err) return spliced; out: return ERR_PTR(err); }
/* allocate new dentry private data, free old one if necessary */ int new_dentry_private_data(struct dentry *dentry) { int newsize; int oldsize = 0; struct unionfs_dentry_info *info = UNIONFS_D(dentry); spin_lock(&dentry->d_lock); if (!info) { dentry->d_fsdata = kmem_cache_alloc(unionfs_dentry_cachep, GFP_ATOMIC); info = UNIONFS_D(dentry); if (!info) goto out; mutex_init(&info->lock); mutex_lock(&info->lock); info->lower_paths = NULL; } else oldsize = sizeof(struct path) * info->bcount; info->bstart = -1; info->bend = -1; info->bopaque = -1; info->bcount = sbmax(dentry->d_sb); atomic_set(&info->generation, atomic_read(&UNIONFS_SB(dentry->d_sb)->generation)); newsize = sizeof(struct path) * sbmax(dentry->d_sb); /* Don't reallocate when we already have enough space. */ /* It would be ideal if we could actually use the slab macros to * determine what our object sizes is, but those are not exported. */ if (oldsize) { int minsize = malloc_sizes[0].cs_size; if (!newsize || ((oldsize < newsize) && (newsize > minsize))) { kfree(info->lower_paths); info->lower_paths = NULL; } } if (!info->lower_paths && newsize) { info->lower_paths = kmalloc(newsize, GFP_ATOMIC); if (!info->lower_paths) goto out_free; } memset(info->lower_paths, 0, (oldsize > newsize ? oldsize : newsize)); spin_unlock(&dentry->d_lock); return 0; out_free: kfree(info->lower_paths); out: free_dentry_private_data(info); dentry->d_fsdata = NULL; spin_unlock(&dentry->d_lock); return -ENOMEM; }
int unionfs_open(struct inode *inode, struct file *file) { int err = 0; struct file *lower_file = NULL; struct dentry *dentry = file->f_path.dentry; struct dentry *parent; int bindex = 0, bstart = 0, bend = 0; int size; int valid = 0; unionfs_read_lock(inode->i_sb, UNIONFS_SMUTEX_PARENT); parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT); unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD); /* don't open unhashed/deleted files */ if (d_deleted(dentry)) { err = -ENOENT; goto out_nofree; } /* XXX: should I change 'false' below to the 'willwrite' flag? */ valid = __unionfs_d_revalidate(dentry, parent, false); if (unlikely(!valid)) { err = -ESTALE; goto out_nofree; } file->private_data = kzalloc(sizeof(struct unionfs_file_info), GFP_KERNEL); if (unlikely(!UNIONFS_F(file))) { err = -ENOMEM; goto out_nofree; } fbstart(file) = -1; fbend(file) = -1; atomic_set(&UNIONFS_F(file)->generation, atomic_read(&UNIONFS_I(inode)->generation)); size = sizeof(struct file *) * sbmax(inode->i_sb); UNIONFS_F(file)->lower_files = kzalloc(size, GFP_KERNEL); if (unlikely(!UNIONFS_F(file)->lower_files)) { err = -ENOMEM; goto out; } size = sizeof(int) * sbmax(inode->i_sb); UNIONFS_F(file)->saved_branch_ids = kzalloc(size, GFP_KERNEL); if (unlikely(!UNIONFS_F(file)->saved_branch_ids)) { err = -ENOMEM; goto out; } bstart = fbstart(file) = dbstart(dentry); bend = fbend(file) = dbend(dentry); /* * open all directories and make the unionfs file struct point to * these lower file structs */ if (S_ISDIR(inode->i_mode)) err = __open_dir(inode, file); /* open a dir */ else err = __open_file(inode, file, parent); /* open a file */ /* freeing the allocated resources, and fput the opened files */ if (err) { for (bindex = bstart; bindex <= bend; bindex++) { lower_file = unionfs_lower_file_idx(file, bindex); if (!lower_file) continue; branchput(dentry->d_sb, bindex); /* fput calls dput for lower_dentry */ fput(lower_file); } } out: if (err) { kfree(UNIONFS_F(file)->lower_files); kfree(UNIONFS_F(file)->saved_branch_ids); kfree(UNIONFS_F(file)); } out_nofree: if (!err) { unionfs_postcopyup_setmnt(dentry); unionfs_copy_attr_times(inode); unionfs_check_file(file); unionfs_check_inode(inode); } unionfs_unlock_dentry(dentry); unionfs_unlock_parent(dentry, parent); unionfs_read_unlock(inode->i_sb); return err; }
/* * Helper function for unionfs_file_revalidate/locked. * Expects dentry/parent to be locked already, and revalidated. */ static int __unionfs_file_revalidate(struct file *file, struct dentry *dentry, struct dentry *parent, struct super_block *sb, int sbgen, int dgen, bool willwrite) { int fgen; int bstart, bend, orig_brid; int size; int err = 0; fgen = atomic_read(&UNIONFS_F(file)->generation); /* * There are two cases we are interested in. The first is if the * generation is lower than the super-block. The second is if * someone has copied up this file from underneath us, we also need * to refresh things. */ if (d_deleted(dentry) || (sbgen <= fgen && dbstart(dentry) == fbstart(file) && unionfs_lower_file(file))) goto out_may_copyup; /* save orig branch ID */ orig_brid = UNIONFS_F(file)->saved_branch_ids[fbstart(file)]; /* First we throw out the existing files. */ cleanup_file(file); /* Now we reopen the file(s) as in unionfs_open. */ bstart = fbstart(file) = dbstart(dentry); bend = fbend(file) = dbend(dentry); size = sizeof(struct file *) * sbmax(sb); UNIONFS_F(file)->lower_files = kzalloc(size, GFP_KERNEL); if (unlikely(!UNIONFS_F(file)->lower_files)) { err = -ENOMEM; goto out; } size = sizeof(int) * sbmax(sb); UNIONFS_F(file)->saved_branch_ids = kzalloc(size, GFP_KERNEL); if (unlikely(!UNIONFS_F(file)->saved_branch_ids)) { err = -ENOMEM; goto out; } if (S_ISDIR(dentry->d_inode->i_mode)) { /* We need to open all the files. */ err = open_all_files(file); if (err) goto out; } else { int new_brid; /* We only open the highest priority branch. */ err = open_highest_file(file, willwrite); if (err) goto out; new_brid = UNIONFS_F(file)->saved_branch_ids[fbstart(file)]; if (unlikely(new_brid != orig_brid && sbgen > fgen)) { /* * If we re-opened the file on a different branch * than the original one, and this was due to a new * branch inserted, then update the mnt counts of * the old and new branches accordingly. */ unionfs_mntget(dentry, bstart); unionfs_mntput(sb->s_root, branch_id_to_idx(sb, orig_brid)); } /* regular files have only one open lower file */ fbend(file) = fbstart(file); } atomic_set(&UNIONFS_F(file)->generation, atomic_read(&UNIONFS_I(dentry->d_inode)->generation)); out_may_copyup: /* Copyup on the first write to a file on a readonly branch. */ if (willwrite && IS_WRITE_FLAG(file->f_flags) && !IS_WRITE_FLAG(unionfs_lower_file(file)->f_flags) && is_robranch(dentry)) { pr_debug("unionfs: do delay copyup of \"%s\"\n", dentry->d_name.name); err = do_delayed_copyup(file, parent); /* regular files have only one open lower file */ if (!err && !S_ISDIR(dentry->d_inode->i_mode)) fbend(file) = fbstart(file); } out: if (err) { kfree(UNIONFS_F(file)->lower_files); kfree(UNIONFS_F(file)->saved_branch_ids); } return err; }
int new_dentry_private_data(struct dentry *dentry) { int newsize; int oldsize = 0; spin_lock(&dentry->d_lock); if (!dtopd_nocheck(dentry)) { dtopd_lhs(dentry) = (struct unionfs_dentry_info *) kmem_cache_alloc(unionfs_dentry_cachep, SLAB_ATOMIC); if (!dtopd_nocheck(dentry)) goto out; init_MUTEX_LOCKED(&dtopd_nocheck(dentry)->udi_sem); #ifdef TRACKLOCK printk("INITLOCK:%p\n", dentry); #endif dtohd_ptr(dentry) = NULL; } else { oldsize = sizeof(struct dentry *) * dtopd(dentry)->udi_bcount; } dtopd_nocheck(dentry)->udi_bstart = -1; dtopd_nocheck(dentry)->udi_bend = -1; dtopd_nocheck(dentry)->udi_bopaque = -1; dtopd_nocheck(dentry)->udi_bcount = sbmax(dentry->d_sb); atomic_set(&dtopd_nocheck(dentry)->udi_generation, atomic_read(&stopd(dentry->d_sb)->usi_generation)); newsize = sizeof(struct dentry *) * sbmax(dentry->d_sb); /* Don't reallocate when we already have enough space. */ /* It would be ideal if we could actually use the slab macros to * determine what our object sizes is, but those are not exported. */ if (oldsize) { int minsize = malloc_sizes[0].cs_size; if (!newsize || ((oldsize < newsize) && (newsize > minsize))) { KFREE(dtohd_ptr(dentry)); dtohd_ptr(dentry) = NULL; } } if (!dtohd_ptr(dentry) && newsize) { dtohd_ptr(dentry) = KMALLOC(newsize, GFP_ATOMIC); if (!dtohd_ptr(dentry)) goto out; } if (oldsize > newsize) memset(dtohd_ptr(dentry), 0, oldsize); else memset(dtohd_ptr(dentry), 0, newsize); spin_unlock(&dentry->d_lock); return 0; out: free_dentry_private_data(dtopd_nocheck(dentry)); dtopd_lhs(dentry) = NULL; spin_unlock(&dentry->d_lock); return -ENOMEM; }
struct dentry *unionfs_lookup_backend(struct dentry *dentry, int lookupmode) { int err = 0; struct dentry *hidden_dentry = NULL; struct dentry *wh_hidden_dentry = NULL; struct dentry *hidden_dir_dentry = NULL; struct dentry *parent_dentry = NULL; int bindex, bstart, bend, bopaque; int dentry_count = 0; /* Number of positive dentries. */ int first_dentry_offset = -1; struct dentry *first_hidden_dentry = NULL; int locked_parent = 0; int locked_child = 0; int opaque; char *whname = NULL; const char *name; int namelen; print_entry("mode = %d", lookupmode); /* We should already have a lock on this dentry in the case of a * partial lookup, or a revalidation. Otherwise it is returned from * new_dentry_private_data already locked. */ if (lookupmode == INTERPOSE_PARTIAL || lookupmode == INTERPOSE_REVAL || lookupmode == INTERPOSE_REVAL_NEG) { verify_locked(dentry); } else { BUG_ON(dtopd_nocheck(dentry) != NULL); locked_child = 1; } if (lookupmode != INTERPOSE_PARTIAL) if ((err = new_dentry_private_data(dentry))) goto out; /* must initialize dentry operations */ dentry->d_op = &unionfs_dops; parent_dentry = GET_PARENT(dentry); /* We never partial lookup the root directory. */ if (parent_dentry != dentry) { lock_dentry(parent_dentry); locked_parent = 1; } else { DPUT(parent_dentry); parent_dentry = NULL; goto out; } fist_print_dentry("IN unionfs_lookup (parent)", parent_dentry); fist_print_dentry("IN unionfs_lookup (child)", dentry); name = dentry->d_name.name; namelen = dentry->d_name.len; /* No dentries should get created for possible whiteout names. */ if (!is_validname(name)) { err = -EPERM; goto out_free; } /* Now start the actual lookup procedure. */ bstart = dbstart(parent_dentry); bend = dbend(parent_dentry); bopaque = dbopaque(parent_dentry); BUG_ON(bstart < 0); /* It would be ideal if we could convert partial lookups to only have * to do this work when they really need to. It could probably improve * performance quite a bit, and maybe simplify the rest of the code. */ if (lookupmode == INTERPOSE_PARTIAL) { bstart++; if ((bopaque != -1) && (bopaque < bend)) bend = bopaque; } fist_dprint(8, "bstart = %d, bend = %d\n", bstart, bend); for (bindex = bstart; bindex <= bend; bindex++) { hidden_dentry = dtohd_index(dentry, bindex); if (lookupmode == INTERPOSE_PARTIAL && hidden_dentry) continue; BUG_ON(hidden_dentry != NULL); hidden_dir_dentry = dtohd_index(parent_dentry, bindex); /* if the parent hidden dentry does not exist skip this */ if (!(hidden_dir_dentry && hidden_dir_dentry->d_inode)) continue; /* also skip it if the parent isn't a directory. */ if (!S_ISDIR(hidden_dir_dentry->d_inode->i_mode)) continue; /* Reuse the whiteout name because its value doesn't change. */ if (!whname) { whname = alloc_whname(name, namelen); if (IS_ERR(whname)) { err = PTR_ERR(whname); goto out_free; } } /* check if whiteout exists in this branch: lookup .wh.foo */ wh_hidden_dentry = LOOKUP_ONE_LEN(whname, hidden_dir_dentry, namelen + WHLEN); if (IS_ERR(wh_hidden_dentry)) { DPUT(first_hidden_dentry); err = PTR_ERR(wh_hidden_dentry); goto out_free; } if (wh_hidden_dentry->d_inode) { /* We found a whiteout so lets give up. */ fist_dprint(8, "whiteout found in %d\n", bindex); if (S_ISREG(wh_hidden_dentry->d_inode->i_mode)) { set_dbend(dentry, bindex); set_dbopaque(dentry, bindex); DPUT(wh_hidden_dentry); break; } err = -EIO; printk(KERN_NOTICE "EIO: Invalid whiteout entry type" " %d.\n", wh_hidden_dentry->d_inode->i_mode); DPUT(wh_hidden_dentry); DPUT(first_hidden_dentry); goto out_free; } DPUT(wh_hidden_dentry); wh_hidden_dentry = NULL; /* Now do regular lookup; lookup foo */ hidden_dentry = LOOKUP_ONE_LEN(name, hidden_dir_dentry, namelen); fist_print_generic_dentry("hidden result", hidden_dentry); if (IS_ERR(hidden_dentry)) { DPUT(first_hidden_dentry); err = PTR_ERR(hidden_dentry); goto out_free; } /* Store the first negative dentry specially, because if they * are all negative we need this for future creates. */ if (!hidden_dentry->d_inode) { if (!first_hidden_dentry && (dbstart(dentry) == -1)) { first_hidden_dentry = hidden_dentry; first_dentry_offset = bindex; } else { DPUT(hidden_dentry); } continue; } /* number of positive dentries */ dentry_count++; /* store underlying dentry */ if (dbstart(dentry) == -1) set_dbstart(dentry, bindex); set_dtohd_index(dentry, bindex, hidden_dentry); set_dbend(dentry, bindex); /* update parent directory's atime with the bindex */ fist_copy_attr_atime(parent_dentry->d_inode, hidden_dir_dentry->d_inode); /* We terminate file lookups here. */ if (!S_ISDIR(hidden_dentry->d_inode->i_mode)) { if (lookupmode == INTERPOSE_PARTIAL) continue; if (dentry_count == 1) goto out_positive; /* This can only happen with mixed D-*-F-* */ BUG_ON(!S_ISDIR(dtohd(dentry)->d_inode->i_mode)); continue; } opaque = is_opaque_dir(dentry, bindex); if (opaque < 0) { DPUT(first_hidden_dentry); err = opaque; goto out_free; } if (opaque) { set_dbend(dentry, bindex); set_dbopaque(dentry, bindex); break; } } if (dentry_count) goto out_positive; else goto out_negative; out_negative: if (lookupmode == INTERPOSE_PARTIAL) goto out; /* If we've only got negative dentries, then use the leftmost one. */ if (lookupmode == INTERPOSE_REVAL) { if (dentry->d_inode) { itopd(dentry->d_inode)->uii_stale = 1; } goto out; } /* This should only happen if we found a whiteout. */ if (first_dentry_offset == -1) { first_hidden_dentry = LOOKUP_ONE_LEN(name, hidden_dir_dentry, namelen); first_dentry_offset = bindex; if (IS_ERR(first_hidden_dentry)) { err = PTR_ERR(first_hidden_dentry); goto out; } } set_dtohd_index(dentry, first_dentry_offset, first_hidden_dentry); set_dbstart(dentry, first_dentry_offset); set_dbend(dentry, first_dentry_offset); if (lookupmode == INTERPOSE_REVAL_NEG) BUG_ON(dentry->d_inode != NULL); else d_add(dentry, NULL); goto out; /* This part of the code is for positive dentries. */ out_positive: BUG_ON(dentry_count <= 0); /* If we're holding onto the first negative dentry throw it out. */ DPUT(first_hidden_dentry); /* Partial lookups need to reinterpose, or throw away older negs. */ if (lookupmode == INTERPOSE_PARTIAL) { if (dentry->d_inode) { unionfs_reinterpose(dentry); goto out; } /* This somehow turned positive, so it is as if we had a * negative revalidation. */ lookupmode = INTERPOSE_REVAL_NEG; update_bstart(dentry); bstart = dbstart(dentry); bend = dbend(dentry); } err = unionfs_interpose(dentry, dentry->d_sb, lookupmode); if (err) goto out_drop; fist_checkinode(dentry->d_inode, "unionfs_lookup OUT: child"); fist_checkinode(parent_dentry->d_inode, "unionfs_lookup OUT: dir"); goto out; out_drop: d_drop(dentry); out_free: /* should dput all the underlying dentries on error condition */ bstart = dbstart(dentry); if (bstart >= 0) { bend = dbend(dentry); for (bindex = bstart; bindex <= bend; bindex++) DPUT(dtohd_index(dentry, bindex)); } KFREE(dtohd_ptr(dentry)); dtohd_ptr(dentry) = NULL; set_dbstart(dentry, -1); set_dbend(dentry, -1); out: if (!err && dtopd(dentry)) { BUG_ON(dbend(dentry) > dtopd(dentry)->udi_bcount); BUG_ON(dbend(dentry) > sbmax(dentry->d_sb)); BUG_ON(dbstart(dentry) < 0); } KFREE(whname); fist_print_dentry("OUT unionfs_lookup (parent)", parent_dentry); fist_print_dentry("OUT unionfs_lookup (child)", dentry); if (locked_parent) unlock_dentry(parent_dentry); DPUT(parent_dentry); if (locked_child) unlock_dentry(dentry); print_exit_status(err); return ERR_PTR(err); }
/* sb we pass is unionfs's super_block */ int unionfs_interpose(struct dentry *dentry, struct super_block *sb, int flag) { struct inode *hidden_inode; struct dentry *hidden_dentry; int err = 0; struct inode *inode; int is_negative_dentry = 1; int bindex, bstart, bend; print_entry("flag = %d", flag); verify_locked(dentry); fist_print_dentry("In unionfs_interpose", dentry); bstart = dbstart(dentry); bend = dbend(dentry); /* Make sure that we didn't get a negative dentry. */ for (bindex = bstart; bindex <= bend; bindex++) { if (dtohd_index(dentry, bindex) && dtohd_index(dentry, bindex)->d_inode) { is_negative_dentry = 0; break; } } BUG_ON(is_negative_dentry); /* We allocate our new inode below, by calling iget. * iget will call our read_inode which will initialize some * of the new inode's fields */ /* On revalidate we've already got our own inode and just need * to fix it up. */ if (flag == INTERPOSE_REVAL) { inode = dentry->d_inode; itopd(inode)->b_start = -1; itopd(inode)->b_end = -1; atomic_set(&itopd(inode)->uii_generation, atomic_read(&stopd(sb)->usi_generation)); itohi_ptr(inode) = KZALLOC(sbmax(sb) * sizeof(struct inode *), GFP_KERNEL); if (!itohi_ptr(inode)) { err = -ENOMEM; goto out; } } else { ino_t ino; /* get unique inode number for unionfs */ #ifdef UNIONFS_IMAP if (stopd(sb)->usi_persistent) { err = read_uin(sb, bindex, dtohd_index(dentry, bindex)->d_inode->i_ino, O_CREAT, &ino); if (err) goto out; } else #endif ino = iunique(sb, UNIONFS_ROOT_INO); inode = IGET(sb, ino); if (!inode) { err = -EACCES; /* should be impossible??? */ goto out; } } down(&inode->i_sem); if (atomic_read(&inode->i_count) > 1) goto skip; for (bindex = bstart; bindex <= bend; bindex++) { hidden_dentry = dtohd_index(dentry, bindex); if (!hidden_dentry) { set_itohi_index(inode, bindex, NULL); continue; } /* Initialize the hidden inode to the new hidden inode. */ if (!hidden_dentry->d_inode) continue; set_itohi_index(inode, bindex, IGRAB(hidden_dentry->d_inode)); } ibstart(inode) = dbstart(dentry); ibend(inode) = dbend(dentry); /* Use attributes from the first branch. */ hidden_inode = itohi(inode); /* Use different set of inode ops for symlinks & directories */ if (S_ISLNK(hidden_inode->i_mode)) inode->i_op = &unionfs_symlink_iops; else if (S_ISDIR(hidden_inode->i_mode)) inode->i_op = &unionfs_dir_iops; /* Use different set of file ops for directories */ if (S_ISDIR(hidden_inode->i_mode)) inode->i_fop = &unionfs_dir_fops; /* properly initialize special inodes */ if (S_ISBLK(hidden_inode->i_mode) || S_ISCHR(hidden_inode->i_mode) || S_ISFIFO(hidden_inode->i_mode) || S_ISSOCK(hidden_inode->i_mode)) init_special_inode(inode, hidden_inode->i_mode, hidden_inode->i_rdev); /* Fix our inode's address operations to that of the lower inode (Unionfs is FiST-Lite) */ if (inode->i_mapping->a_ops != hidden_inode->i_mapping->a_ops) { fist_dprint(7, "fixing inode 0x%p a_ops (0x%p -> 0x%p)\n", inode, inode->i_mapping->a_ops, hidden_inode->i_mapping->a_ops); inode->i_mapping->a_ops = hidden_inode->i_mapping->a_ops; } /* all well, copy inode attributes */ fist_copy_attr_all(inode, hidden_inode); skip: /* only (our) lookup wants to do a d_add */ switch (flag) { case INTERPOSE_DEFAULT: case INTERPOSE_REVAL_NEG: d_instantiate(dentry, inode); break; case INTERPOSE_LOOKUP: err = PTR_ERR(d_splice_alias(inode, dentry)); break; case INTERPOSE_REVAL: /* Do nothing. */ break; default: printk(KERN_ERR "Invalid interpose flag passed!"); BUG(); } fist_print_dentry("Leaving unionfs_interpose", dentry); fist_print_inode("Leaving unionfs_interpose", inode); up(&inode->i_sem); out: print_exit_status(err); return err; }