unlink.c 4.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/*
 *  linux/fs/nfs/unlink.c
 *
 * nfs sillydelete handling
 *
 * NOTE: we rely on holding the BKL for list manipulation protection.
 */

#include <linux/slab.h>
#include <linux/string.h>
#include <linux/dcache.h>
#include <linux/sunrpc/sched.h>
#include <linux/sunrpc/clnt.h>
#include <linux/nfs_fs.h>


struct nfs_unlinkdata {
	struct nfs_unlinkdata	*next;
	struct dentry	*dir, *dentry;
	struct qstr	name;
	struct rpc_task	task;
	struct rpc_cred	*cred;
	unsigned int	count;
};

static struct nfs_unlinkdata	*nfs_deletes;
static RPC_WAITQ(nfs_delete_queue, "nfs_delete_queue");

/**
 * nfs_detach_unlinkdata - Remove asynchronous unlink from global list
 * @data: pointer to descriptor
 */
static inline void
nfs_detach_unlinkdata(struct nfs_unlinkdata *data)
{
	struct nfs_unlinkdata	**q;

	for (q = &nfs_deletes; *q != NULL; q = &((*q)->next)) {
		if (*q == data) {
			*q = data->next;
			break;
		}
	}
}

/**
 * nfs_put_unlinkdata - release data from a sillydelete operation.
 * @data: pointer to unlink structure.
 */
static void
nfs_put_unlinkdata(struct nfs_unlinkdata *data)
{
	if (--data->count == 0) {
		nfs_detach_unlinkdata(data);
J
Jesper Juhl 已提交
55
		kfree(data->name.name);
L
Linus Torvalds 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
		kfree(data);
	}
}

#define NAME_ALLOC_LEN(len)	((len+16) & ~15)
/**
 * nfs_copy_dname - copy dentry name to data structure
 * @dentry: pointer to dentry
 * @data: nfs_unlinkdata
 */
static inline void
nfs_copy_dname(struct dentry *dentry, struct nfs_unlinkdata *data)
{
	char		*str;
	int		len = dentry->d_name.len;

	str = kmalloc(NAME_ALLOC_LEN(len), GFP_KERNEL);
	if (!str)
		return;
	memcpy(str, dentry->d_name.name, len);
	if (!data->name.len) {
		data->name.len = len;
		data->name.name = str;
	} else
		kfree(str);
}

/**
 * nfs_async_unlink_init - Initialize the RPC info
 * @task: rpc_task of the sillydelete
 *
 * We delay initializing RPC info until after the call to dentry_iput()
 * in order to minimize races against rename().
 */
T
Trond Myklebust 已提交
90
static void nfs_async_unlink_init(struct rpc_task *task, void *calldata)
L
Linus Torvalds 已提交
91
{
T
Trond Myklebust 已提交
92
	struct nfs_unlinkdata	*data = calldata;
L
Linus Torvalds 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
	struct dentry		*dir = data->dir;
	struct rpc_message	msg = {
		.rpc_cred	= data->cred,
	};
	int			status = -ENOENT;

	if (!data->name.len)
		goto out_err;

	status = NFS_PROTO(dir->d_inode)->unlink_setup(&msg, dir, &data->name);
	if (status < 0)
		goto out_err;
	nfs_begin_data_update(dir->d_inode);
	rpc_call_setup(task, &msg, 0);
	return;
 out_err:
	rpc_exit(task, status);
}

/**
 * nfs_async_unlink_done - Sillydelete post-processing
 * @task: rpc_task of the sillydelete
 *
 * Do the directory attribute update.
 */
118
static void nfs_async_unlink_done(struct rpc_task *task, void *calldata)
L
Linus Torvalds 已提交
119
{
120
	struct nfs_unlinkdata	*data = calldata;
L
Linus Torvalds 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
	struct dentry		*dir = data->dir;
	struct inode		*dir_i;

	if (!dir)
		return;
	dir_i = dir->d_inode;
	nfs_end_data_update(dir_i);
	if (NFS_PROTO(dir_i)->unlink_done(dir, task))
		return;
	put_rpccred(data->cred);
	data->cred = NULL;
	dput(dir);
}

/**
 * nfs_async_unlink_release - Release the sillydelete data.
 * @task: rpc_task of the sillydelete
 *
 * We need to call nfs_put_unlinkdata as a 'tk_release' task since the
 * rpc_task would be freed too.
 */
142
static void nfs_async_unlink_release(void *calldata)
L
Linus Torvalds 已提交
143
{
144
	struct nfs_unlinkdata	*data = calldata;
L
Linus Torvalds 已提交
145 146 147
	nfs_put_unlinkdata(data);
}

148
static const struct rpc_call_ops nfs_unlink_ops = {
T
Trond Myklebust 已提交
149
	.rpc_call_prepare = nfs_async_unlink_init,
150 151 152 153
	.rpc_call_done = nfs_async_unlink_done,
	.rpc_release = nfs_async_unlink_release,
};

L
Linus Torvalds 已提交
154 155 156 157 158 159 160 161 162 163 164 165
/**
 * nfs_async_unlink - asynchronous unlinking of a file
 * @dentry: dentry to unlink
 */
int
nfs_async_unlink(struct dentry *dentry)
{
	struct dentry	*dir = dentry->d_parent;
	struct nfs_unlinkdata	*data;
	struct rpc_clnt	*clnt = NFS_CLIENT(dir->d_inode);
	int		status = -ENOMEM;

166
	data = kzalloc(sizeof(*data), GFP_KERNEL);
L
Linus Torvalds 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
	if (!data)
		goto out;

	data->cred = rpcauth_lookupcred(clnt->cl_auth, 0);
	if (IS_ERR(data->cred)) {
		status = PTR_ERR(data->cred);
		goto out_free;
	}
	data->dir = dget(dir);
	data->dentry = dentry;

	data->next = nfs_deletes;
	nfs_deletes = data;
	data->count = 1;

T
Trond Myklebust 已提交
182
	rpc_init_task(&data->task, clnt, RPC_TASK_ASYNC, &nfs_unlink_ops, data);
L
Linus Torvalds 已提交
183 184 185 186 187

	spin_lock(&dentry->d_lock);
	dentry->d_flags |= DCACHE_NFSFS_RENAMED;
	spin_unlock(&dentry->d_lock);

T
Trond Myklebust 已提交
188
	rpc_sleep_on(&nfs_delete_queue, &data->task, NULL, NULL);
L
Linus Torvalds 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	status = 0;
 out:
	return status;
out_free:
	kfree(data);
	return status;
}

/**
 * nfs_complete_unlink - Initialize completion of the sillydelete
 * @dentry: dentry to delete
 *
 * Since we're most likely to be called by dentry_iput(), we
 * only use the dentry to find the sillydelete. We then copy the name
 * into the qstr.
 */
void
nfs_complete_unlink(struct dentry *dentry)
{
	struct nfs_unlinkdata	*data;

	for(data = nfs_deletes; data != NULL; data = data->next) {
		if (dentry == data->dentry)
			break;
	}
	if (!data)
		return;
	data->count++;
	nfs_copy_dname(dentry, data);
	spin_lock(&dentry->d_lock);
	dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
	spin_unlock(&dentry->d_lock);
	rpc_wake_up_task(&data->task);
	nfs_put_unlinkdata(data);
}