/* * This function creates a copy of a file represented by 'file' which * currently resides in branch 'bstart' to branch 'new_bindex.' The copy * will be named "name". */ int copyup_named_file(struct inode *dir, struct file *file, char *name, int bstart, int new_bindex, loff_t len) { int err = 0; struct file *output_file = NULL; err = copyup_dentry(dir, file->f_path.dentry, bstart, new_bindex, name, strlen(name), &output_file, len); if (!err) { fbstart(file) = new_bindex; unionfs_set_lower_file_idx(file, new_bindex, output_file); } return err; }
/* * This function creates a copy of a file represented by 'file' which * currently resides in branch 'bstart' to branch 'new_bindex'. */ int copyup_file(struct inode *dir, struct file *file, int bstart, int new_bindex, loff_t len) { int err = 0; struct file *output_file = NULL; struct dentry *dentry = file->f_path.dentry; err = copyup_dentry(dir, dentry, bstart, new_bindex, dentry->d_name.name, dentry->d_name.len, &output_file, len); if (!err) { fbstart(file) = new_bindex; unionfs_set_lower_file_idx(file, new_bindex, output_file); } return err; }
/* This function creates a copy of a file represented by 'file' which currently * resides in branch 'bstart' to branch 'new_bindex. */ int copyup_file(struct inode *dir, struct file *file, int bstart, int new_bindex, int len) { int err = 0; struct file *output_file = NULL; print_entry_location(); err = copyup_dentry(dir, file->f_dentry, bstart, new_bindex, &output_file, len); if (!err) { fbstart(file) = new_bindex; set_ftohf_index(file, new_bindex, output_file); } print_exit_status(err); return err; }
static int unionfs_setattr(struct dentry *dentry, struct iattr *ia) { int err = 0; struct dentry *lower_dentry; struct dentry *parent; struct inode *inode; struct inode *lower_inode; int bstart, bend, bindex; loff_t size; struct iattr lower_ia; /* check if user has permission to change inode */ err = inode_change_ok(dentry->d_inode, ia); if (err) goto out_err; unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD); parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT); unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD); if (unlikely(!__unionfs_d_revalidate(dentry, parent, false, 0))) { err = -ESTALE; goto out; } bstart = dbstart(dentry); bend = dbend(dentry); inode = dentry->d_inode; /* * mode change is for clearing setuid/setgid. Allow lower filesystem * to reinterpret it in its own way. */ if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) ia->ia_valid &= ~ATTR_MODE; lower_dentry = unionfs_lower_dentry(dentry); if (!lower_dentry) { /* should never happen after above revalidate */ err = -EINVAL; goto out; } /* * Get the lower inode directly from lower dentry, in case ibstart * is -1 (which happens when the file is open but unlinked. */ lower_inode = lower_dentry->d_inode; /* check if user has permission to change lower inode */ err = inode_change_ok(lower_inode, ia); if (err) goto out; /* copyup if the file is on a read only branch */ if (is_robranch_super(dentry->d_sb, bstart) || __is_rdonly(lower_inode)) { /* check if we have a branch to copy up to */ if (bstart <= 0) { err = -EACCES; goto out; } if (ia->ia_valid & ATTR_SIZE) size = ia->ia_size; else size = i_size_read(inode); /* copyup to next available branch */ for (bindex = bstart - 1; bindex >= 0; bindex--) { err = copyup_dentry(parent->d_inode, dentry, bstart, bindex, dentry->d_name.name, dentry->d_name.len, NULL, size); if (!err) break; } if (err) goto out; /* get updated lower_dentry/inode after copyup */ lower_dentry = unionfs_lower_dentry(dentry); lower_inode = unionfs_lower_inode(inode); /* * check for whiteouts in writeable branch, and remove them * if necessary. */ if (lower_dentry) { err = check_unlink_whiteout(dentry, lower_dentry, bindex); if (err > 0) /* ignore if whiteout found and removed */ err = 0; } } /* * If shrinking, first truncate upper level to cancel writing dirty * pages beyond the new eof; and also if its' maxbytes is more * limiting (fail with -EFBIG before making any change to the lower * level). There is no need to vmtruncate the upper level * afterwards in the other cases: we fsstack_copy_inode_size from * the lower level. */ if (ia->ia_valid & ATTR_SIZE) { err = inode_newsize_ok(inode, ia->ia_size); if (err) goto out; truncate_setsize(inode, ia->ia_size); } /* notify the (possibly copied-up) lower inode */ /* * Note: we use lower_dentry->d_inode, because lower_inode may be * unlinked (no inode->i_sb and i_ino==0. This happens if someone * tries to open(), unlink(), then ftruncate() a file. */ /* prepare our own lower struct iattr (with our own lower file) */ memcpy(&lower_ia, ia, sizeof(lower_ia)); if (ia->ia_valid & ATTR_FILE) { lower_ia.ia_file = unionfs_lower_file(ia->ia_file); BUG_ON(!lower_ia.ia_file); // XXX? } mutex_lock(&lower_dentry->d_inode->i_mutex); err = notify_change(lower_dentry, &lower_ia); mutex_unlock(&lower_dentry->d_inode->i_mutex); if (err) goto out; /* get attributes from the first lower inode */ if (ibstart(inode) >= 0) unionfs_copy_attr_all(inode, lower_inode); /* * unionfs_copy_attr_all will copy the lower times to our inode if * the lower ones are newer (useful for cache coherency). However, * ->setattr is the only place in which we may have to copy the * lower inode times absolutely, to support utimes(2). */ if (ia->ia_valid & ATTR_MTIME_SET) inode->i_mtime = lower_inode->i_mtime; if (ia->ia_valid & ATTR_CTIME) inode->i_ctime = lower_inode->i_ctime; if (ia->ia_valid & ATTR_ATIME_SET) inode->i_atime = lower_inode->i_atime; fsstack_copy_inode_size(inode, lower_inode); out: if (!err) unionfs_check_dentry(dentry); unionfs_unlock_dentry(dentry); unionfs_unlock_parent(dentry, parent); unionfs_read_unlock(dentry->d_sb); out_err: return err; }
static int unionfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) { int err = 0; struct dentry *lower_old_dentry = NULL; struct dentry *lower_new_dentry = NULL; struct dentry *lower_dir_dentry = NULL; struct dentry *old_parent, *new_parent; char *name = NULL; bool valid; unionfs_read_lock(old_dentry->d_sb, UNIONFS_SMUTEX_CHILD); old_parent = dget_parent(old_dentry); new_parent = dget_parent(new_dentry); unionfs_double_lock_parents(old_parent, new_parent); unionfs_double_lock_dentry(old_dentry, new_dentry); valid = __unionfs_d_revalidate(old_dentry, old_parent, false, 0); if (unlikely(!valid)) { err = -ESTALE; goto out; } if (new_dentry->d_inode) { valid = __unionfs_d_revalidate(new_dentry, new_parent, false, 0); if (unlikely(!valid)) { err = -ESTALE; goto out; } } lower_new_dentry = unionfs_lower_dentry(new_dentry); /* check for a whiteout in new dentry branch, and delete it */ err = check_unlink_whiteout(new_dentry, lower_new_dentry, dbstart(new_dentry)); if (err > 0) { /* whiteout found and removed successfully */ lower_dir_dentry = dget_parent(lower_new_dentry); fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); dput(lower_dir_dentry); set_nlink(dir, unionfs_get_nlinks(dir)); err = 0; } if (err) goto out; /* check if parent hierachy is needed, then link in same branch */ if (dbstart(old_dentry) != dbstart(new_dentry)) { lower_new_dentry = create_parents(dir, new_dentry, new_dentry->d_name.name, dbstart(old_dentry)); err = PTR_ERR(lower_new_dentry); if (IS_COPYUP_ERR(err)) goto docopyup; if (!lower_new_dentry || IS_ERR(lower_new_dentry)) goto out; } lower_new_dentry = unionfs_lower_dentry(new_dentry); lower_old_dentry = unionfs_lower_dentry(old_dentry); BUG_ON(dbstart(old_dentry) != dbstart(new_dentry)); lower_dir_dentry = lock_parent(lower_new_dentry); err = is_robranch(old_dentry); if (!err) { /* see Documentation/filesystems/unionfs/issues.txt */ lockdep_off(); err = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode, lower_new_dentry); lockdep_on(); } unlock_dir(lower_dir_dentry); docopyup: if (IS_COPYUP_ERR(err)) { int old_bstart = dbstart(old_dentry); int bindex; for (bindex = old_bstart - 1; bindex >= 0; bindex--) { err = copyup_dentry(old_parent->d_inode, old_dentry, old_bstart, bindex, old_dentry->d_name.name, old_dentry->d_name.len, NULL, i_size_read(old_dentry->d_inode)); if (err) continue; lower_new_dentry = create_parents(dir, new_dentry, new_dentry->d_name.name, bindex); lower_old_dentry = unionfs_lower_dentry(old_dentry); lower_dir_dentry = lock_parent(lower_new_dentry); /* see Documentation/filesystems/unionfs/issues.txt */ lockdep_off(); /* do vfs_link */ err = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode, lower_new_dentry); lockdep_on(); unlock_dir(lower_dir_dentry); goto check_link; } goto out; } check_link: if (err || !lower_new_dentry->d_inode) goto out; /* Its a hard link, so use the same inode */ new_dentry->d_inode = igrab(old_dentry->d_inode); d_add(new_dentry, new_dentry->d_inode); unionfs_copy_attr_all(dir, lower_new_dentry->d_parent->d_inode); fsstack_copy_inode_size(dir, lower_new_dentry->d_parent->d_inode); /* propagate number of hard-links */ set_nlink(old_dentry->d_inode, unionfs_get_nlinks(old_dentry->d_inode)); /* new dentry's ctime may have changed due to hard-link counts */ unionfs_copy_attr_times(new_dentry->d_inode); out: if (!new_dentry->d_inode) d_drop(new_dentry); kfree(name); if (!err) unionfs_postcopyup_setmnt(new_dentry); unionfs_check_inode(dir); unionfs_check_dentry(new_dentry); unionfs_check_dentry(old_dentry); unionfs_double_unlock_dentry(old_dentry, new_dentry); unionfs_double_unlock_parents(old_parent, new_parent); dput(new_parent); dput(old_parent); unionfs_read_unlock(old_dentry->d_sb); return err; }
static int unionfs_rename_whiteout(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) { int err = 0; int bindex, bwh_old; int old_bstart, old_bend; int new_bstart, new_bend; int do_copyup = -1; struct dentry *parent_dentry; int local_err = 0; int eio = 0; int revert = 0; struct dentry *wh_old = NULL; print_entry_location(); old_bstart = dbstart(old_dentry); bwh_old = old_bstart; old_bend = dbend(old_dentry); parent_dentry = old_dentry->d_parent; new_bstart = dbstart(new_dentry); new_bend = dbend(new_dentry); /* Rename source to destination. */ err = do_rename(old_dir, old_dentry, new_dir, new_dentry, old_bstart, &wh_old); if (err) { if (!IS_COPYUP_ERR(err)) { goto out; } do_copyup = old_bstart - 1; } else { revert = 1; } /* Unlink all instances of destination that exist to the left of * bstart of source. On error, revert back, goto out. */ for (bindex = old_bstart - 1; bindex >= new_bstart; bindex--) { struct dentry *unlink_dentry; struct dentry *unlink_dir_dentry; unlink_dentry = dtohd_index(new_dentry, bindex); if (!unlink_dentry) { continue; } unlink_dir_dentry = lock_parent(unlink_dentry); if (!(err = is_robranch_super(old_dir->i_sb, bindex))) { err = vfs_unlink(unlink_dir_dentry->d_inode, unlink_dentry); } fist_copy_attr_times(new_dentry->d_parent->d_inode, unlink_dir_dentry->d_inode); /* propagate number of hard-links */ new_dentry->d_parent->d_inode->i_nlink = get_nlinks(new_dentry->d_parent->d_inode); unlock_dir(unlink_dir_dentry); if (!err) { if (bindex != new_bstart) { DPUT(unlink_dentry); set_dtohd_index(new_dentry, bindex, NULL); } } else if (IS_COPYUP_ERR(err)) { do_copyup = bindex - 1; } else if (revert) { DPUT(wh_old); goto revert; } } if (do_copyup != -1) { for (bindex = do_copyup; bindex >= 0; bindex--) { /* copyup the file into some left directory, so that you can rename it */ err = copyup_dentry(old_dentry->d_parent->d_inode, old_dentry, old_bstart, bindex, NULL, old_dentry->d_inode->i_size); if (!err) { DPUT(wh_old); bwh_old = bindex; err = do_rename(old_dir, old_dentry, new_dir, new_dentry, bindex, &wh_old); break; } } } /* Create whiteout for source, only if: * (1) There is more than one underlying instance of source. * (2) We did a copy_up */ if ((old_bstart != old_bend) || (do_copyup != -1)) { struct dentry *hidden_parent; BUG_ON(!wh_old || IS_ERR(wh_old) || wh_old->d_inode || bwh_old < 0); hidden_parent = lock_parent(wh_old); local_err = vfs_create(hidden_parent->d_inode, wh_old, S_IRUGO, NULL); unlock_dir(hidden_parent); if (!local_err) set_dbopaque(old_dentry, bwh_old); else { /* We can't fix anything now, so we cop-out and use -EIO. */ printk ("<0>We can't create a whiteout for the source in rename!\n"); err = -EIO; } } out: DPUT(wh_old); print_exit_status(err); return err; revert: /* Do revert here. */ local_err = unionfs_refresh_hidden_dentry(new_dentry, old_bstart); if (local_err) { printk(KERN_WARNING "Revert failed in rename: the new refresh failed.\n"); eio = -EIO; } local_err = unionfs_refresh_hidden_dentry(old_dentry, old_bstart); if (local_err) { printk(KERN_WARNING "Revert failed in rename: the old refresh failed.\n"); eio = -EIO; goto revert_out; } if (!dtohd_index(new_dentry, bindex) || !dtohd_index(new_dentry, bindex)->d_inode) { printk(KERN_WARNING "Revert failed in rename: the object disappeared from under us!\n"); eio = -EIO; goto revert_out; } if (dtohd_index(old_dentry, bindex) && dtohd_index(old_dentry, bindex)->d_inode) { printk(KERN_WARNING "Revert failed in rename: the object was created underneath us!\n"); eio = -EIO; goto revert_out; } local_err = do_rename(new_dir, new_dentry, old_dir, old_dentry, old_bstart, NULL); /* If we can't fix it, then we cop-out with -EIO. */ if (local_err) { printk(KERN_WARNING "Revert failed in rename!\n"); eio = -EIO; } local_err = unionfs_refresh_hidden_dentry(new_dentry, bindex); if (local_err) eio = -EIO; local_err = unionfs_refresh_hidden_dentry(old_dentry, bindex); if (local_err) eio = -EIO; revert_out: if (eio) err = eio; print_exit_status(err); return err; }
/* * The function is nasty, nasty, nasty, but so is rename. :( */ static int unionfs_rename_all(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) { struct dentry *parent_dentry = NULL; int err = 0; int eio; /* These variables control error handling. */ fd_set success_mask; char *name = NULL; /* unfortunately, we have to resort to this, because dbstart/dbend would return different things in different place of the rename code */ struct rename_info info; info.rename_ok = FD_SETSIZE; /* The last rename that is ok. */ info.do_copyup = -1; /* Where we should start copyup. */ info.do_whiteout = -1; /* Where we should start whiteouts of the source. */ info.wh_old = NULL; info.bwh_old = -1; print_entry_location(); parent_dentry = old_dentry->d_parent; name = KMALLOC(old_dentry->d_name.len + 1, GFP_KERNEL); if (!name) { err = -ENOMEM; goto out; } strncpy(name, old_dentry->d_name.name, old_dentry->d_name.len + 1); info.new_bstart = dbstart(new_dentry); info.new_bend = dbend(new_dentry); info.old_bstart = dbstart(old_dentry); info.old_bend = dbend(old_dentry); BUG_ON(info.new_bstart < 0); BUG_ON(info.old_bstart < 0); /* The failure mask only can deal with FD_SETSIZE entries. */ BUG_ON(info.old_bend > FD_SETSIZE); BUG_ON(info.new_bend > FD_SETSIZE); FD_ZERO(&success_mask); /* Life is simpler if the dentry doesn't exist. */ info.clobber = (dtohd_index(new_dentry, info.new_bstart)->d_inode) ? 1 : 0; info.isdir = S_ISDIR(old_dentry->d_inode->i_mode); /* rename everything we can */ err = __rename_all(old_dir, old_dentry, new_dir, new_dentry, &success_mask, &info); if (err) goto revert; /* unlink destinations even further left */ err = __rename_all_unlink(old_dir, old_dentry, new_dir, new_dentry, &info); if (err) goto revert; if (info.clobber) { /* Now we need to handle the leftmost of the destination. */ err = __rename_all_clobber(old_dir, old_dentry, new_dir, new_dentry, &info); if (err) goto revert; } /* Copy up if necessary */ if (info.do_copyup != -1) { int bindex; for (bindex = info.do_copyup; bindex >= 0; bindex--) { err = copyup_dentry(old_dentry->d_parent->d_inode, old_dentry, info.old_bstart, bindex, NULL, old_dentry->d_inode->i_size); if (!err) { DPUT(info.wh_old); info.bwh_old = bindex; err = do_rename(old_dir, old_dentry, new_dir, new_dentry, bindex, &info.wh_old); break; } } } /* Create a whiteout for the source. */ if (info.do_whiteout != -1) { struct dentry *hidden_parent; BUG_ON(info.do_whiteout < 0 || !info.wh_old || IS_ERR(info.wh_old) || info.wh_old->d_inode || info.bwh_old < 0); hidden_parent = lock_parent(info.wh_old); err = vfs_create(hidden_parent->d_inode, info.wh_old, S_IRUGO, NULL); unlock_dir(hidden_parent); if (!err) set_dbopaque(old_dentry, info.bwh_old); else { /* We can't fix anything now, so we -EIO. */ printk(KERN_WARNING "We can't create a whiteout for the" "source in rename!\n"); err = -EIO; goto out; } } /* We are at the point where reverting doesn't happen. */ goto out; revert: /* something bad happened, try to revert */ eio = __rename_all_revert(old_dir, old_dentry, new_dir, new_dentry, &success_mask, &info); if (eio) err = eio; out: DPUT(info.wh_old); KFREE(name); print_exit_status(err); return err; }
static int u2fs_setattr(struct dentry *dentry, struct iattr *ia) { int err = 0; struct dentry *lower_dentry=NULL; struct inode *inode; struct inode *lower_inode=NULL; struct dentry *parent = NULL; struct path lower_path; struct iattr lower_ia; inode = dentry->d_inode; /* * Check if user has permission to change inode. We don't check if * this user can change the lower inode: that should happen when * calling notify_change on the lower inode. */ err = inode_change_ok(inode, ia); if (err) goto out_err; /* creating parent directories if destination is read-only */ if((U2FS_D(dentry)->lower_path[LEFT].dentry) == NULL && (U2FS_D(dentry)->lower_path[LEFT].mnt) == NULL){ parent = dget_parent(dentry); err = copyup_dentry(parent->d_inode, dentry, dentry->d_name.name, dentry->d_name.len, NULL, i_size_read(dentry->d_inode)); if(err) goto out; } u2fs_get_lower_path(dentry, &lower_path, LEFT); lower_dentry = lower_path.dentry; lower_inode = u2fs_lower_inode(inode, LEFT); /* prepare our own lower struct iattr (with the lower file) */ memcpy(&lower_ia, ia, sizeof(lower_ia)); if (ia->ia_valid & ATTR_FILE) lower_ia.ia_file = u2fs_lower_file(ia->ia_file, LEFT); /* * If shrinking, first truncate upper level to cancel writing dirty * pages beyond the new eof; and also if its' maxbytes is more * limiting (fail with -EFBIG before making any change to the lower * level). There is no need to vmtruncate the upper level * afterwards in the other cases: we fsstack_copy_inode_size from * the lower level. */ if (ia->ia_valid & ATTR_SIZE) { err = inode_newsize_ok(inode, ia->ia_size); if (err) goto out; truncate_setsize(inode, ia->ia_size); } /* * mode change is for clearing setuid/setgid bits. Allow lower fs * to interpret this in its own way. */ if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) lower_ia.ia_valid &= ~ATTR_MODE; /* notify the (possibly copied-up) lower inode */ /* * Note: we use lower_dentry->d_inode, because lower_inode may be * unlinked (no inode->i_sb and i_ino==0. This happens if someone * tries to open(), unlink(), then ftruncate() a file. */ mutex_lock(&lower_dentry->d_inode->i_mutex); err = notify_change(lower_dentry, &lower_ia); /* note: lower_ia */ mutex_unlock(&lower_dentry->d_inode->i_mutex); if (err) goto out; /* get attributes from the lower inode */ fsstack_copy_attr_all(inode, lower_inode); /* * Not running fsstack_copy_inode_size(inode, lower_inode), because * VFS should update our inode size, and notify_change on * lower_inode should update its size. */ out: if(parent != NULL) dput(parent); u2fs_put_lower_path(dentry, &lower_path); out_err: return err; }
/* * The locking rules in u2fs_rename are complex. We could use a simpler * superblock-level name-space lock for renames and copy-ups. */ static int u2fs_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) { int err = 0; struct dentry *lower_old_dentry = NULL; struct dentry *lower_new_dentry = NULL; struct dentry *lower_old_dir_dentry = NULL; struct dentry *lower_new_dir_dentry = NULL; struct dentry *trap = NULL; struct dentry *ret = NULL; struct path lower_old_path, lower_new_path; /* creating parent directories if destination is read-only */ if((U2FS_D(old_dentry)->lower_path[LEFT].dentry) == NULL && (U2FS_D(old_dentry)->lower_path[LEFT].mnt) == NULL){ err = create_whiteout(old_dentry); if(err){ err = -EIO; goto out_copyup; } err = copyup_dentry(old_dir, old_dentry, old_dentry->d_name.name, old_dentry->d_name.len, NULL, i_size_read(old_dentry->d_inode)); if(err) goto out_copyup; } if((U2FS_D(new_dentry)->lower_path[LEFT].dentry) == NULL && (U2FS_D(new_dentry)->lower_path[LEFT].mnt) == NULL){ ret = create_parents(new_dir, new_dentry, new_dentry->d_name.name); if (!ret || IS_ERR(ret)) { err = PTR_ERR(ret); if (!IS_COPYUP_ERR(err)) printk(KERN_ERR "u2fs: create_parents for " "u2fs_rename failed" "err=%d\n", err); goto out_copyup; } u2fs_postcopyup_setmnt(new_dentry); u2fs_put_reset_lower_path(new_dentry, RIGHT); if(err) goto out_copyup; } u2fs_get_lower_path(old_dentry, &lower_old_path, LEFT); u2fs_get_lower_path(new_dentry, &lower_new_path, LEFT); lower_old_dentry = lower_old_path.dentry; lower_new_dentry = lower_new_path.dentry; lower_old_dir_dentry = dget_parent(lower_old_dentry); lower_new_dir_dentry = dget_parent(lower_new_dentry); trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry); /* source should not be ancestor of target */ if (trap == lower_old_dentry) { err = -EINVAL; goto out; } /* target should not be ancestor of source */ if (trap == lower_new_dentry) { err = -ENOTEMPTY; goto out; } err = mnt_want_write(lower_old_path.mnt); if (err) goto out; err = mnt_want_write(lower_new_path.mnt); if (err) goto out_drop_old_write; err = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry, lower_new_dir_dentry->d_inode, lower_new_dentry); if (err) goto out_err; fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode); fsstack_copy_inode_size(new_dir, lower_new_dir_dentry->d_inode); if (new_dir != old_dir) { fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode); fsstack_copy_inode_size(old_dir, lower_old_dir_dentry->d_inode); } out_err: mnt_drop_write(lower_new_path.mnt); out_drop_old_write: mnt_drop_write(lower_old_path.mnt); out: unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry); dput(lower_old_dir_dentry); dput(lower_new_dir_dentry); u2fs_put_lower_path(old_dentry, &lower_old_path); u2fs_put_lower_path(new_dentry, &lower_new_path); out_copyup: return err; }
/* * Main rename code. This is sufficiently complex, that it's documented in * Documentation/filesystems/unionfs/rename.txt. This routine calls * __unionfs_rename() above to perform some of the work. */ static int do_unionfs_rename(struct inode *old_dir, struct dentry *old_dentry, struct dentry *old_parent, struct inode *new_dir, struct dentry *new_dentry, struct dentry *new_parent) { int err = 0; int bindex, bwh_old; int old_bstart, old_bend; int new_bstart, new_bend; int do_copyup = -1; int local_err = 0; int eio = 0; int revert = 0; old_bstart = dbstart(old_dentry); bwh_old = old_bstart; old_bend = dbend(old_dentry); new_bstart = dbstart(new_dentry); new_bend = dbend(new_dentry); /* Rename source to destination. */ err = __unionfs_rename(old_dir, old_dentry, old_parent, new_dir, new_dentry, new_parent, old_bstart); if (err) { if (!IS_COPYUP_ERR(err)) goto out; do_copyup = old_bstart - 1; } else { revert = 1; } /* * Unlink all instances of destination that exist to the left of * bstart of source. On error, revert back, goto out. */ for (bindex = old_bstart - 1; bindex >= new_bstart; bindex--) { struct dentry *unlink_dentry; struct dentry *unlink_dir_dentry; BUG_ON(bindex < 0); unlink_dentry = unionfs_lower_dentry_idx(new_dentry, bindex); if (!unlink_dentry) continue; unlink_dir_dentry = lock_parent(unlink_dentry); err = is_robranch_super(old_dir->i_sb, bindex); if (!err) err = vfs_unlink(unlink_dir_dentry->d_inode, unlink_dentry); fsstack_copy_attr_times(new_parent->d_inode, unlink_dir_dentry->d_inode); /* propagate number of hard-links */ new_parent->d_inode->i_nlink = unionfs_get_nlinks(new_parent->d_inode); unlock_dir(unlink_dir_dentry); if (!err) { if (bindex != new_bstart) { dput(unlink_dentry); unionfs_set_lower_dentry_idx(new_dentry, bindex, NULL); } } else if (IS_COPYUP_ERR(err)) { do_copyup = bindex - 1; } else if (revert) { goto revert; } } if (do_copyup != -1) { for (bindex = do_copyup; bindex >= 0; bindex--) { /* * copyup the file into some left directory, so that * you can rename it */ err = copyup_dentry(old_parent->d_inode, old_dentry, old_bstart, bindex, old_dentry->d_name.name, old_dentry->d_name.len, NULL, i_size_read(old_dentry->d_inode)); /* if copyup failed, try next branch to the left */ if (err) continue; bwh_old = bindex; err = __unionfs_rename(old_dir, old_dentry, old_parent, new_dir, new_dentry, new_parent, bindex); break; } } /* make it opaque */ if (S_ISDIR(old_dentry->d_inode->i_mode)) { err = make_dir_opaque(old_dentry, dbstart(old_dentry)); if (err) goto revert; } /* * Create whiteout for source, only if: * (1) There is more than one underlying instance of source. * (2) We did a copy_up */ if ((old_bstart != old_bend) || (do_copyup != -1)) { if (bwh_old < 0) { printk(KERN_ERR "unionfs: rename error (bwh_old=%d)\n", bwh_old); err = -EIO; goto out; } err = create_whiteout(old_dentry, bwh_old); if (err) { /* can't fix anything now, so we exit with -EIO */ printk(KERN_ERR "unionfs: can't create a whiteout for " "%s in rename!\n", old_dentry->d_name.name); err = -EIO; } } out: return err; revert: /* Do revert here. */ local_err = unionfs_refresh_lower_dentry(new_dentry, new_parent, old_bstart); if (local_err) { printk(KERN_ERR "unionfs: revert failed in rename: " "the new refresh failed\n"); eio = -EIO; } local_err = unionfs_refresh_lower_dentry(old_dentry, old_parent, old_bstart); if (local_err) { printk(KERN_ERR "unionfs: revert failed in rename: " "the old refresh failed\n"); eio = -EIO; goto revert_out; } if (!unionfs_lower_dentry_idx(new_dentry, bindex) || !unionfs_lower_dentry_idx(new_dentry, bindex)->d_inode) { printk(KERN_ERR "unionfs: revert failed in rename: " "the object disappeared from under us!\n"); eio = -EIO; goto revert_out; } if (unionfs_lower_dentry_idx(old_dentry, bindex) && unionfs_lower_dentry_idx(old_dentry, bindex)->d_inode) { printk(KERN_ERR "unionfs: revert failed in rename: " "the object was created underneath us!\n"); eio = -EIO; goto revert_out; } local_err = __unionfs_rename(new_dir, new_dentry, new_parent, old_dir, old_dentry, old_parent, old_bstart); /* If we can't fix it, then we cop-out with -EIO. */ if (local_err) { printk(KERN_ERR "unionfs: revert failed in rename!\n"); eio = -EIO; } local_err = unionfs_refresh_lower_dentry(new_dentry, new_parent, bindex); if (local_err) eio = -EIO; local_err = unionfs_refresh_lower_dentry(old_dentry, old_parent, bindex); if (local_err) eio = -EIO; revert_out: if (eio) err = eio; return err; }
static int unionfs_setattr(struct dentry *dentry, struct iattr *ia) { int err = 0; struct dentry *hidden_dentry; struct inode *inode = NULL; struct inode *hidden_inode = NULL; int bstart, bend, bindex; int i; int copyup = 0; print_entry_location(); lock_dentry(dentry); bstart = dbstart(dentry); bend = dbend(dentry); inode = dentry->d_inode; for (bindex = bstart; (bindex <= bend) || (bindex == bstart); bindex++) { hidden_dentry = dtohd_index(dentry, bindex); if (!hidden_dentry) continue; BUG_ON(hidden_dentry->d_inode == NULL); /* If the file is on a read only branch */ if (is_robranch_super(dentry->d_sb, bindex) || IS_RDONLY(hidden_dentry->d_inode)) { if (copyup || (bindex != bstart)) continue; /* Only if its the leftmost file, copyup the file */ for (i = bstart - 1; i >= 0; i--) { size_t size = dentry->d_inode->i_size; if (ia->ia_valid & ATTR_SIZE) size = ia->ia_size; err = copyup_dentry(dentry->d_parent->d_inode, dentry, bstart, i, NULL, size); if (!err) { copyup = 1; hidden_dentry = dtohd(dentry); break; } /* if error is in the leftmost f/s, pass it up */ if (i == 0) goto out; } } err = notify_change(hidden_dentry, ia); if (err) goto out; break; } /* get the size from the first hidden inode */ hidden_inode = itohi(dentry->d_inode); fist_checkinode(inode, "unionfs_setattr"); fist_copy_attr_all(inode, hidden_inode); out: unlock_dentry(dentry); fist_checkinode(inode, "post unionfs_setattr"); print_exit_status(err); return err; }
static int unionfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) { int err = 0; struct dentry *hidden_old_dentry = NULL; struct dentry *hidden_new_dentry = NULL; struct dentry *hidden_dir_dentry = NULL; struct dentry *whiteout_dentry; char *name = NULL; print_entry_location(); double_lock_dentry(new_dentry, old_dentry); hidden_new_dentry = dtohd(new_dentry); /* check if whiteout exists in the branch of new dentry, i.e. lookup * .wh.foo first. If present, delete it */ name = alloc_whname(new_dentry->d_name.name, new_dentry->d_name.len); if (IS_ERR(name)) { err = PTR_ERR(name); goto out; } whiteout_dentry = LOOKUP_ONE_LEN(name, hidden_new_dentry->d_parent, new_dentry->d_name.len + WHLEN); if (IS_ERR(whiteout_dentry)) { err = PTR_ERR(whiteout_dentry); goto out; } if (!whiteout_dentry->d_inode) { DPUT(whiteout_dentry); whiteout_dentry = NULL; } else { /* found a .wh.foo entry, unlink it and then call vfs_link() */ hidden_dir_dentry = lock_parent(whiteout_dentry); if (! (err = is_robranch_super(new_dentry->d_sb, dbstart(new_dentry)))) { err = vfs_unlink(hidden_dir_dentry->d_inode, whiteout_dentry); } fist_copy_attr_times(dir, hidden_dir_dentry->d_inode); dir->i_nlink = get_nlinks(dir); unlock_dir(hidden_dir_dentry); hidden_dir_dentry = NULL; DPUT(whiteout_dentry); if (err) goto out; } if (dbstart(old_dentry) != dbstart(new_dentry)) { hidden_new_dentry = create_parents(dir, new_dentry, dbstart(old_dentry)); err = PTR_ERR(hidden_new_dentry); if (IS_COPYUP_ERR(err)) goto docopyup; if (!hidden_new_dentry || IS_ERR(hidden_new_dentry)) goto out; } hidden_new_dentry = dtohd(new_dentry); hidden_old_dentry = dtohd(old_dentry); BUG_ON(dbstart(old_dentry) != dbstart(new_dentry)); hidden_dir_dentry = lock_parent(hidden_new_dentry); if (!(err = is_robranch(old_dentry))) err = vfs_link(hidden_old_dentry, hidden_dir_dentry->d_inode, hidden_new_dentry); unlock_dir(hidden_dir_dentry); docopyup: if (IS_COPYUP_ERR(err)) { int old_bstart = dbstart(old_dentry); int bindex; for (bindex = old_bstart - 1; bindex >= 0; bindex--) { err = copyup_dentry(old_dentry->d_parent-> d_inode, old_dentry, old_bstart, bindex, NULL, old_dentry->d_inode->i_size); if (!err) { hidden_new_dentry = create_parents(dir, new_dentry, bindex); hidden_old_dentry = dtohd(old_dentry); hidden_dir_dentry = lock_parent(hidden_new_dentry); /* do vfs_link */ err = vfs_link(hidden_old_dentry, hidden_dir_dentry->d_inode, hidden_new_dentry); unlock_dir(hidden_dir_dentry); goto check_link; } } goto out; } check_link: if (err || !hidden_new_dentry->d_inode) goto out; /* Its a hard link, so use the same inode */ new_dentry->d_inode = IGRAB(old_dentry->d_inode); d_instantiate(new_dentry, new_dentry->d_inode); fist_copy_attr_all(dir, hidden_new_dentry->d_parent->d_inode); /* propagate number of hard-links */ old_dentry->d_inode->i_nlink = get_nlinks(old_dentry->d_inode); out: if (!new_dentry->d_inode) d_drop(new_dentry); KFREE(name); unlock_dentry(new_dentry); unlock_dentry(old_dentry); print_exit_status(err); return err; }