mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jan Kara <jack@suse.cz>
To: Miklos Szeredi <miklos@szeredi.hu>
Cc: viro@ZenIV.linux.org.uk, torvalds@linux-foundation.org,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	bfields@fieldses.org, hch@infradead.org,
	akpm@linux-foundation.org, dhowells@redhat.com, zab@redhat.com,
	jack@suse.cz, luto@amacapital.net, mszeredi@suse.cz
Subject: Re: [PATCH 12/13] ext4: add cross rename support
Date: Tue, 11 Feb 2014 22:23:46 +0100	[thread overview]
Message-ID: <20140211212346.GF22835@quack.suse.cz> (raw)
In-Reply-To: <1391791751-2533-13-git-send-email-miklos@szeredi.hu>

On Fri 07-02-14 17:49:10, Miklos Szeredi wrote:
> From: Miklos Szeredi <mszeredi@suse.cz>
> 
> Implement RENAME_EXCHANGE flag in renameat2 syscall.
  Hum, I guess the nice symmetry of ext4_cross_rename() outweights the code
duplication. So you can add:

Reviewed-by: Jan Kara <jack@suse.cz>

								Honza

> 
> Signed-off-by: Miklos Szeredi <mszeredi@suse.cz>
> ---
>  fs/ext4/namei.c | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 138 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
> index 75f1bde43dcc..1cb84f78909e 100644
> --- a/fs/ext4/namei.c
> +++ b/fs/ext4/namei.c
> @@ -3004,6 +3004,8 @@ struct ext4_renament {
>  	struct inode *dir;
>  	struct dentry *dentry;
>  	struct inode *inode;
> +	bool is_dir;
> +	int dir_nlink_delta;
>  
>  	/* entry for "dentry" */
>  	struct buffer_head *bh;
> @@ -3135,6 +3137,17 @@ static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent)
>  	}
>  }
>  
> +static void ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent)
> +{
> +	if (ent->dir_nlink_delta) {
> +		if (ent->dir_nlink_delta == -1)
> +			ext4_dec_count(handle, ent->dir);
> +		else
> +			ext4_inc_count(handle, ent->dir);
> +		ext4_mark_inode_dirty(handle, ent->dir);
> +	}
> +}
> +
>  /*
>   * Anybody can rename anything with this: the permission checks are left to the
>   * higher-level routines.
> @@ -3274,13 +3287,137 @@ end_rename:
>  	return retval;
>  }
>  
> +static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
> +			     struct inode *new_dir, struct dentry *new_dentry)
> +{
> +	handle_t *handle = NULL;
> +	struct ext4_renament old = {
> +		.dir = old_dir,
> +		.dentry = old_dentry,
> +		.inode = old_dentry->d_inode,
> +	};
> +	struct ext4_renament new = {
> +		.dir = new_dir,
> +		.dentry = new_dentry,
> +		.inode = new_dentry->d_inode,
> +	};
> +	u8 new_file_type;
> +	int retval;
> +
> +	dquot_initialize(old.dir);
> +	dquot_initialize(new.dir);
> +
> +	old.bh = ext4_find_entry(old.dir, &old.dentry->d_name,
> +				 &old.de, &old.inlined);
> +	/*
> +	 *  Check for inode number is _not_ due to possible IO errors.
> +	 *  We might rmdir the source, keep it as pwd of some process
> +	 *  and merrily kill the link to whatever was created under the
> +	 *  same name. Goodbye sticky bit ;-<
> +	 */
> +	retval = -ENOENT;
> +	if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
> +		goto end_rename;
> +
> +	new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
> +				 &new.de, &new.inlined);
> +
> +	/* RENAME_EXCHANGE case: old *and* new must both exist */
> +	if (!new.bh || le32_to_cpu(new.de->inode) != new.inode->i_ino)
> +		goto end_rename;
> +
> +	handle = ext4_journal_start(old.dir, EXT4_HT_DIR,
> +		(2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
> +		 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
> +	if (IS_ERR(handle))
> +		return PTR_ERR(handle);
> +
> +	if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
> +		ext4_handle_sync(handle);
> +
> +	if (S_ISDIR(old.inode->i_mode)) {
> +		old.is_dir = true;
> +		retval = ext4_rename_dir_prepare(handle, &old);
> +		if (retval)
> +			goto end_rename;
> +	}
> +	if (S_ISDIR(new.inode->i_mode)) {
> +		new.is_dir = true;
> +		retval = ext4_rename_dir_prepare(handle, &new);
> +		if (retval)
> +			goto end_rename;
> +	}
> +
> +	/*
> +	 * Other than the special case of overwriting a directory, parents'
> +	 * nlink only needs to be modified if this is a cross directory rename.
> +	 */
> +	if (old.dir != new.dir && old.is_dir != new.is_dir) {
> +		old.dir_nlink_delta = old.is_dir ? -1 : 1;
> +		new.dir_nlink_delta = -old.dir_nlink_delta;
> +		retval = -EMLINK;
> +		if ((old.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(old.dir)) ||
> +		    (new.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(new.dir)))
> +			goto end_rename;
> +	}
> +
> +	new_file_type = new.de->file_type;
> +	retval = ext4_setent(handle, &new, old.inode->i_ino, old.de->file_type);
> +	if (retval)
> +		goto end_rename;
> +
> +	retval = ext4_setent(handle, &old, new.inode->i_ino, new_file_type);
> +	if (retval)
> +		goto end_rename;
> +
> +	/*
> +	 * Like most other Unix systems, set the ctime for inodes on a
> +	 * rename.
> +	 */
> +	old.inode->i_ctime = ext4_current_time(old.inode);
> +	new.inode->i_ctime = ext4_current_time(new.inode);
> +	ext4_mark_inode_dirty(handle, old.inode);
> +	ext4_mark_inode_dirty(handle, new.inode);
> +
> +	if (old.dir_bh) {
> +		retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
> +		if (retval)
> +			goto end_rename;
> +	}
> +	if (new.dir_bh) {
> +		retval = ext4_rename_dir_finish(handle, &new, old.dir->i_ino);
> +		if (retval)
> +			goto end_rename;
> +	}
> +	ext4_update_dir_count(handle, &old);
> +	ext4_update_dir_count(handle, &new);
> +	retval = 0;
> +
> +end_rename:
> +	brelse(old.dir_bh);
> +	brelse(new.dir_bh);
> +	brelse(old.bh);
> +	brelse(new.bh);
> +	if (handle)
> +		ext4_journal_stop(handle);
> +	return retval;
> +}
> +
>  static int ext4_rename2(struct inode *old_dir, struct dentry *old_dentry,
>  			struct inode *new_dir, struct dentry *new_dentry,
>  			unsigned int flags)
>  {
> -	if (flags & ~RENAME_NOREPLACE)
> +	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
>  		return -EINVAL;
>  
> +	if (flags & RENAME_EXCHANGE) {
> +		return ext4_cross_rename(old_dir, old_dentry,
> +					 new_dir, new_dentry);
> +	}
> +	/*
> +	 * Existence checking was done by the VFS, otherwise "RENAME_NOREPLACE"
> +	 * is equivalent to regular rename.
> +	 */
>  	return ext4_rename(old_dir, old_dentry, new_dir, new_dentry);
>  }
>  
> -- 
> 1.8.1.4
> 
-- 
Jan Kara <jack@suse.cz>
SUSE Labs, CR

  reply	other threads:[~2014-02-11 21:23 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-07 16:48 [PATCH 00/13] cross rename v4 Miklos Szeredi
2014-02-07 16:48 ` [PATCH 01/13] vfs: add d_is_dir() Miklos Szeredi
2014-02-07 17:36   ` J. Bruce Fields
2014-02-07 19:30   ` David Howells
2014-02-07 16:49 ` [PATCH 02/13] vfs: rename: move d_move() up Miklos Szeredi
2014-02-07 16:49 ` [PATCH 03/13] vfs: rename: use common code for dir and non-dir Miklos Szeredi
2014-02-07 16:49 ` [PATCH 04/13] vfs: add renameat2 syscall Miklos Szeredi
2014-02-07 16:49 ` [PATCH 05/13] vfs: add RENAME_NOREPLACE flag Miklos Szeredi
2014-02-07 16:49 ` [PATCH 06/13] security: add flags to rename hooks Miklos Szeredi
2014-02-07 16:49 ` [PATCH 07/13] vfs: lock_two_nondirectories: allow directory args Miklos Szeredi
2014-02-07 21:16   ` J. Bruce Fields
2014-02-11 15:32     ` Miklos Szeredi
2014-02-07 16:49 ` [PATCH 08/13] vfs: add cross-rename Miklos Szeredi
2014-02-07 22:40   ` J. Bruce Fields
2014-02-11 15:55     ` Miklos Szeredi
2014-02-07 16:49 ` [PATCH 09/13] ext4: rename: create ext4_renament structure for local vars Miklos Szeredi
2014-02-07 16:49 ` [PATCH 10/13] ext4: rename: move EMLINK check up Miklos Szeredi
2014-02-07 16:49 ` [PATCH 11/13] ext4: rename: split out helper functions Miklos Szeredi
2014-02-07 16:49 ` [PATCH 12/13] ext4: add cross rename support Miklos Szeredi
2014-02-11 21:23   ` Jan Kara [this message]
2014-02-07 16:49 ` [PATCH 13/13] vfs: merge rename2 into rename Miklos Szeredi
2014-02-07 22:46 ` [PATCH 00/13] cross rename v4 J. Bruce Fields
2014-02-11 15:57   ` Miklos Szeredi
2014-02-13 19:32     ` J. Bruce Fields
2014-02-10 10:51 ` Dave Chinner
2014-02-11 16:01   ` Miklos Szeredi
2014-02-12 17:18     ` Miklos Szeredi
2014-02-17  8:19       ` Dave Chinner
2014-02-17 18:04         ` Theodore Ts'o
2014-03-19 13:57         ` xfstest for renameat2 system call (was: [PATCH 00/13] cross rename v4) Miklos Szeredi
2014-04-08  1:23           ` Dave Chinner
2014-02-13 15:54 ` [PATCH 00/13] cross rename v4 David Howells
2014-02-13 16:25   ` Miklos Szeredi
2014-02-13 16:42   ` David Howells
2014-02-13 17:28     ` Miklos Szeredi
2014-02-13 18:21       ` Andy Lutomirski
2014-02-13 18:29       ` Linus Torvalds
2014-02-13 18:56         ` Miklos Szeredi
2014-02-13 19:20           ` Linus Torvalds
2014-02-13 19:02       ` David Howells
2014-02-13 19:32         ` Linus Torvalds
2014-02-13 20:17           ` Eric W. Biederman
2014-02-13 20:28           ` Miklos Szeredi
2014-02-24 17:12             ` Miklos Szeredi
2014-02-24 17:49               ` Linus Torvalds
2014-02-25  4:07               ` J. R. Okajima
2014-02-26 15:15               ` Jan Kara

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140211212346.GF22835@quack.suse.cz \
    --to=jack@suse.cz \
    --cc=akpm@linux-foundation.org \
    --cc=bfields@fieldses.org \
    --cc=dhowells@redhat.com \
    --cc=hch@infradead.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=miklos@szeredi.hu \
    --cc=mszeredi@suse.cz \
    --cc=torvalds@linux-foundation.org \
    --cc=viro@ZenIV.linux.org.uk \
    --cc=zab@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®