2 * linux/fs/nfs/unlink.c
4 * nfs sillydelete handling
6 * NOTE: we rely on holding the BKL for list manipulation protection.
9 #include <linux/slab.h>
10 #include <linux/string.h>
11 #include <linux/dcache.h>
12 #include <linux/sunrpc/sched.h>
13 #include <linux/sunrpc/clnt.h>
14 #include <linux/nfs_fs.h>
17 struct nfs_unlinkdata
{
18 struct nfs_unlinkdata
*next
;
19 struct dentry
*dir
, *dentry
;
22 struct rpc_cred
*cred
;
26 static struct nfs_unlinkdata
*nfs_deletes
;
27 static RPC_WAITQ(nfs_delete_queue
, "nfs_delete_queue");
30 * nfs_detach_unlinkdata - Remove asynchronous unlink from global list
31 * @data: pointer to descriptor
34 nfs_detach_unlinkdata(struct nfs_unlinkdata
*data
)
36 struct nfs_unlinkdata
**q
;
38 for (q
= &nfs_deletes
; *q
!= NULL
; q
= &((*q
)->next
)) {
47 * nfs_put_unlinkdata - release data from a sillydelete operation.
48 * @data: pointer to unlink structure.
51 nfs_put_unlinkdata(struct nfs_unlinkdata
*data
)
53 if (--data
->count
== 0) {
54 nfs_detach_unlinkdata(data
);
55 kfree(data
->name
.name
);
60 #define NAME_ALLOC_LEN(len) ((len+16) & ~15)
62 * nfs_copy_dname - copy dentry name to data structure
63 * @dentry: pointer to dentry
64 * @data: nfs_unlinkdata
67 nfs_copy_dname(struct dentry
*dentry
, struct nfs_unlinkdata
*data
)
70 int len
= dentry
->d_name
.len
;
72 str
= kmalloc(NAME_ALLOC_LEN(len
), GFP_KERNEL
);
75 memcpy(str
, dentry
->d_name
.name
, len
);
76 if (!data
->name
.len
) {
78 data
->name
.name
= str
;
84 * nfs_async_unlink_init - Initialize the RPC info
85 * @task: rpc_task of the sillydelete
87 * We delay initializing RPC info until after the call to dentry_iput()
88 * in order to minimize races against rename().
90 static void nfs_async_unlink_init(struct rpc_task
*task
, void *calldata
)
92 struct nfs_unlinkdata
*data
= calldata
;
93 struct dentry
*dir
= data
->dir
;
94 struct rpc_message msg
= {
95 .rpc_cred
= data
->cred
,
102 status
= NFS_PROTO(dir
->d_inode
)->unlink_setup(&msg
, dir
, &data
->name
);
105 nfs_begin_data_update(dir
->d_inode
);
106 rpc_call_setup(task
, &msg
, 0);
109 rpc_exit(task
, status
);
113 * nfs_async_unlink_done - Sillydelete post-processing
114 * @task: rpc_task of the sillydelete
116 * Do the directory attribute update.
118 static void nfs_async_unlink_done(struct rpc_task
*task
, void *calldata
)
120 struct nfs_unlinkdata
*data
= calldata
;
121 struct dentry
*dir
= data
->dir
;
126 dir_i
= dir
->d_inode
;
127 nfs_end_data_update(dir_i
);
128 if (NFS_PROTO(dir_i
)->unlink_done(dir
, task
))
130 put_rpccred(data
->cred
);
136 * nfs_async_unlink_release - Release the sillydelete data.
137 * @task: rpc_task of the sillydelete
139 * We need to call nfs_put_unlinkdata as a 'tk_release' task since the
140 * rpc_task would be freed too.
142 static void nfs_async_unlink_release(void *calldata
)
144 struct nfs_unlinkdata
*data
= calldata
;
145 nfs_put_unlinkdata(data
);
148 static const struct rpc_call_ops nfs_unlink_ops
= {
149 .rpc_call_prepare
= nfs_async_unlink_init
,
150 .rpc_call_done
= nfs_async_unlink_done
,
151 .rpc_release
= nfs_async_unlink_release
,
155 * nfs_async_unlink - asynchronous unlinking of a file
156 * @dentry: dentry to unlink
159 nfs_async_unlink(struct dentry
*dentry
)
161 struct dentry
*dir
= dentry
->d_parent
;
162 struct nfs_unlinkdata
*data
;
163 struct rpc_clnt
*clnt
= NFS_CLIENT(dir
->d_inode
);
164 int status
= -ENOMEM
;
166 data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
169 memset(data
, 0, sizeof(*data
));
171 data
->cred
= rpcauth_lookupcred(clnt
->cl_auth
, 0);
172 if (IS_ERR(data
->cred
)) {
173 status
= PTR_ERR(data
->cred
);
176 data
->dir
= dget(dir
);
177 data
->dentry
= dentry
;
179 data
->next
= nfs_deletes
;
183 rpc_init_task(&data
->task
, clnt
, RPC_TASK_ASYNC
, &nfs_unlink_ops
, data
);
185 spin_lock(&dentry
->d_lock
);
186 dentry
->d_flags
|= DCACHE_NFSFS_RENAMED
;
187 spin_unlock(&dentry
->d_lock
);
189 rpc_sleep_on(&nfs_delete_queue
, &data
->task
, NULL
, NULL
);
199 * nfs_complete_unlink - Initialize completion of the sillydelete
200 * @dentry: dentry to delete
202 * Since we're most likely to be called by dentry_iput(), we
203 * only use the dentry to find the sillydelete. We then copy the name
207 nfs_complete_unlink(struct dentry
*dentry
)
209 struct nfs_unlinkdata
*data
;
211 for(data
= nfs_deletes
; data
!= NULL
; data
= data
->next
) {
212 if (dentry
== data
->dentry
)
218 nfs_copy_dname(dentry
, data
);
219 spin_lock(&dentry
->d_lock
);
220 dentry
->d_flags
&= ~DCACHE_NFSFS_RENAMED
;
221 spin_unlock(&dentry
->d_lock
);
222 rpc_wake_up_task(&data
->task
);
223 nfs_put_unlinkdata(data
);