blob: 431981d0265f917965abe2e1e589512accdff31a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/unlink.c
3 *
4 * nfs sillydelete handling
5 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
8#include <linux/slab.h>
9#include <linux/string.h>
10#include <linux/dcache.h>
11#include <linux/sunrpc/sched.h>
12#include <linux/sunrpc/clnt.h>
13#include <linux/nfs_fs.h>
Linus Torvaldsb35e7042007-10-19 19:59:18 -070014#include <linux/sched.h>
15#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17struct nfs_unlinkdata {
Trond Myklebust565277f2007-10-15 18:17:53 -040018 struct hlist_node list;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040019 struct nfs_removeargs args;
20 struct nfs_removeres res;
21 struct inode *dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023};
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/**
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040026 * nfs_free_unlinkdata - release data from a sillydelete operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * @data: pointer to unlink structure.
28 */
29static void
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040030nfs_free_unlinkdata(struct nfs_unlinkdata *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040032 iput(data->dir);
33 put_rpccred(data->cred);
34 kfree(data->args.name.name);
35 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
38#define NAME_ALLOC_LEN(len) ((len+16) & ~15)
39/**
40 * nfs_copy_dname - copy dentry name to data structure
41 * @dentry: pointer to dentry
42 * @data: nfs_unlinkdata
43 */
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040044static int nfs_copy_dname(struct dentry *dentry, struct nfs_unlinkdata *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 char *str;
47 int len = dentry->d_name.len;
48
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040049 str = kmemdup(dentry->d_name.name, NAME_ALLOC_LEN(len), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 if (!str)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040051 return -ENOMEM;
52 data->args.name.len = len;
53 data->args.name.name = str;
54 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Trond Myklebust565277f2007-10-15 18:17:53 -040057static void nfs_free_dname(struct nfs_unlinkdata *data)
58{
59 kfree(data->args.name.name);
60 data->args.name.name = NULL;
61 data->args.name.len = 0;
62}
63
64static void nfs_dec_sillycount(struct inode *dir)
65{
66 struct nfs_inode *nfsi = NFS_I(dir);
67 if (atomic_dec_return(&nfsi->silly_count) == 1)
68 wake_up(&nfsi->waitqueue);
69}
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/**
72 * nfs_async_unlink_init - Initialize the RPC info
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040073 * task: rpc_task of the sillydelete
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 */
Trond Myklebust4ce70ad2006-01-03 09:55:05 +010075static void nfs_async_unlink_init(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040077 struct nfs_unlinkdata *data = calldata;
78 struct inode *dir = data->dir;
79 struct rpc_message msg = {
80 .rpc_argp = &data->args,
81 .rpc_resp = &data->res,
82 .rpc_cred = data->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040085 NFS_PROTO(dir)->unlink_setup(&msg, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89/**
90 * nfs_async_unlink_done - Sillydelete post-processing
91 * @task: rpc_task of the sillydelete
92 *
93 * Do the directory attribute update.
94 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +010095static void nfs_async_unlink_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -040097 struct nfs_unlinkdata *data = calldata;
98 struct inode *dir = data->dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400100 if (!NFS_PROTO(dir)->unlink_done(task, dir))
101 rpc_restart_call(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
104/**
105 * nfs_async_unlink_release - Release the sillydelete data.
106 * @task: rpc_task of the sillydelete
107 *
108 * We need to call nfs_put_unlinkdata as a 'tk_release' task since the
109 * rpc_task would be freed too.
110 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100111static void nfs_async_unlink_release(void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100113 struct nfs_unlinkdata *data = calldata;
Trond Myklebust565277f2007-10-15 18:17:53 -0400114
115 nfs_dec_sillycount(data->dir);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400116 nfs_free_unlinkdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100119static const struct rpc_call_ops nfs_unlink_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100120 .rpc_call_prepare = nfs_async_unlink_init,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100121 .rpc_call_done = nfs_async_unlink_done,
122 .rpc_release = nfs_async_unlink_release,
123};
124
Trond Myklebust565277f2007-10-15 18:17:53 -0400125static int nfs_do_call_unlink(struct dentry *parent, struct inode *dir, struct nfs_unlinkdata *data)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400126{
127 struct rpc_task *task;
Trond Myklebust565277f2007-10-15 18:17:53 -0400128 struct dentry *alias;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400129
Trond Myklebust565277f2007-10-15 18:17:53 -0400130 alias = d_lookup(parent, &data->args.name);
131 if (alias != NULL) {
132 int ret = 0;
Trond Myklebust609005c2008-01-28 19:42:59 -0500133
Trond Myklebust565277f2007-10-15 18:17:53 -0400134 /*
135 * Hey, we raced with lookup... See if we need to transfer
136 * the sillyrename information to the aliased dentry.
137 */
138 nfs_free_dname(data);
139 spin_lock(&alias->d_lock);
Trond Myklebust609005c2008-01-28 19:42:59 -0500140 if (alias->d_inode != NULL &&
141 !(alias->d_flags & DCACHE_NFSFS_RENAMED)) {
Trond Myklebust565277f2007-10-15 18:17:53 -0400142 alias->d_fsdata = data;
Trond Myklebustfccca7f2008-01-26 17:37:47 -0500143 alias->d_flags |= DCACHE_NFSFS_RENAMED;
Trond Myklebust565277f2007-10-15 18:17:53 -0400144 ret = 1;
145 }
146 spin_unlock(&alias->d_lock);
147 nfs_dec_sillycount(dir);
148 dput(alias);
149 return ret;
150 }
151 data->dir = igrab(dir);
152 if (!data->dir) {
153 nfs_dec_sillycount(dir);
154 return 0;
155 }
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400156 data->args.fh = NFS_FH(dir);
157 nfs_fattr_init(&data->res.dir_attr);
158
159 task = rpc_run_task(NFS_CLIENT(dir), RPC_TASK_ASYNC, &nfs_unlink_ops, data);
160 if (!IS_ERR(task))
161 rpc_put_task(task);
162 return 1;
Trond Myklebust565277f2007-10-15 18:17:53 -0400163}
164
165static int nfs_call_unlink(struct dentry *dentry, struct nfs_unlinkdata *data)
166{
167 struct dentry *parent;
168 struct inode *dir;
169 int ret = 0;
170
171
172 parent = dget_parent(dentry);
173 if (parent == NULL)
174 goto out_free;
175 dir = parent->d_inode;
Trond Myklebust55b70a02007-10-21 12:02:22 -0400176 if (nfs_copy_dname(dentry, data) != 0)
Trond Myklebust565277f2007-10-15 18:17:53 -0400177 goto out_dput;
178 /* Non-exclusive lock protects against concurrent lookup() calls */
179 spin_lock(&dir->i_lock);
180 if (atomic_inc_not_zero(&NFS_I(dir)->silly_count) == 0) {
181 /* Deferred delete */
182 hlist_add_head(&data->list, &NFS_I(dir)->silly_list);
183 spin_unlock(&dir->i_lock);
184 ret = 1;
185 goto out_dput;
186 }
187 spin_unlock(&dir->i_lock);
188 ret = nfs_do_call_unlink(parent, dir, data);
189out_dput:
190 dput(parent);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400191out_free:
Trond Myklebust565277f2007-10-15 18:17:53 -0400192 return ret;
193}
194
195void nfs_block_sillyrename(struct dentry *dentry)
196{
197 struct nfs_inode *nfsi = NFS_I(dentry->d_inode);
198
199 wait_event(nfsi->waitqueue, atomic_cmpxchg(&nfsi->silly_count, 1, 0) == 1);
200}
201
202void nfs_unblock_sillyrename(struct dentry *dentry)
203{
204 struct inode *dir = dentry->d_inode;
205 struct nfs_inode *nfsi = NFS_I(dir);
206 struct nfs_unlinkdata *data;
207
208 atomic_inc(&nfsi->silly_count);
209 spin_lock(&dir->i_lock);
210 while (!hlist_empty(&nfsi->silly_list)) {
211 if (!atomic_inc_not_zero(&nfsi->silly_count))
212 break;
213 data = hlist_entry(nfsi->silly_list.first, struct nfs_unlinkdata, list);
214 hlist_del(&data->list);
215 spin_unlock(&dir->i_lock);
216 if (nfs_do_call_unlink(dentry, dir, data) == 0)
217 nfs_free_unlinkdata(data);
218 spin_lock(&dir->i_lock);
219 }
220 spin_unlock(&dir->i_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400221}
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223/**
224 * nfs_async_unlink - asynchronous unlinking of a file
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400225 * @dir: parent directory of dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 * @dentry: dentry to unlink
227 */
228int
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400229nfs_async_unlink(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400231 struct nfs_unlinkdata *data;
232 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Eric Sesterhennbd647542006-03-20 13:44:10 -0500234 data = kzalloc(sizeof(*data), GFP_KERNEL);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400235 if (data == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400238 data->cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (IS_ERR(data->cred)) {
240 status = PTR_ERR(data->cred);
241 goto out_free;
242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400244 status = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 spin_lock(&dentry->d_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400246 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
247 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400249 dentry->d_fsdata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 spin_unlock(&dentry->d_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400251 return 0;
252out_unlock:
253 spin_unlock(&dentry->d_lock);
254 put_rpccred(data->cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255out_free:
256 kfree(data);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400257out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return status;
259}
260
261/**
262 * nfs_complete_unlink - Initialize completion of the sillydelete
263 * @dentry: dentry to delete
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400264 * @inode: inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 *
266 * Since we're most likely to be called by dentry_iput(), we
267 * only use the dentry to find the sillydelete. We then copy the name
268 * into the qstr.
269 */
270void
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400271nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400273 struct nfs_unlinkdata *data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 spin_lock(&dentry->d_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400276 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
277 dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
278 data = dentry->d_fsdata;
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 spin_unlock(&dentry->d_lock);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400281
282 if (data != NULL && (NFS_STALE(inode) || !nfs_call_unlink(dentry, data)))
283 nfs_free_unlinkdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}