2 * linux/fs/nfs/unlink.c
4 * nfs sillydelete handling
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>
14 #include <linux/sched.h>
15 #include <linux/wait.h>
20 struct nfs_unlinkdata
{
21 struct hlist_node list
;
22 struct nfs_removeargs args
;
23 struct nfs_removeres res
;
25 struct rpc_cred
*cred
;
29 * nfs_free_unlinkdata - release data from a sillydelete operation.
30 * @data: pointer to unlink structure.
33 nfs_free_unlinkdata(struct nfs_unlinkdata
*data
)
36 put_rpccred(data
->cred
);
37 kfree(data
->args
.name
.name
);
41 #define NAME_ALLOC_LEN(len) ((len+16) & ~15)
43 * nfs_copy_dname - copy dentry name to data structure
44 * @dentry: pointer to dentry
45 * @data: nfs_unlinkdata
47 static int nfs_copy_dname(struct dentry
*dentry
, struct nfs_unlinkdata
*data
)
50 int len
= dentry
->d_name
.len
;
52 str
= kmemdup(dentry
->d_name
.name
, NAME_ALLOC_LEN(len
), GFP_KERNEL
);
55 data
->args
.name
.len
= len
;
56 data
->args
.name
.name
= str
;
60 static void nfs_free_dname(struct nfs_unlinkdata
*data
)
62 kfree(data
->args
.name
.name
);
63 data
->args
.name
.name
= NULL
;
64 data
->args
.name
.len
= 0;
67 static void nfs_dec_sillycount(struct inode
*dir
)
69 struct nfs_inode
*nfsi
= NFS_I(dir
);
70 if (atomic_dec_return(&nfsi
->silly_count
) == 1)
71 wake_up(&nfsi
->waitqueue
);
75 * nfs_async_unlink_done - Sillydelete post-processing
76 * @task: rpc_task of the sillydelete
78 * Do the directory attribute update.
80 static void nfs_async_unlink_done(struct rpc_task
*task
, void *calldata
)
82 struct nfs_unlinkdata
*data
= calldata
;
83 struct inode
*dir
= data
->dir
;
85 if (!NFS_PROTO(dir
)->unlink_done(task
, dir
))
86 nfs4_restart_rpc(task
, NFS_SERVER(dir
)->nfs_client
);
90 * nfs_async_unlink_release - Release the sillydelete data.
91 * @task: rpc_task of the sillydelete
93 * We need to call nfs_put_unlinkdata as a 'tk_release' task since the
94 * rpc_task would be freed too.
96 static void nfs_async_unlink_release(void *calldata
)
98 struct nfs_unlinkdata
*data
= calldata
;
99 struct super_block
*sb
= data
->dir
->i_sb
;
101 nfs_dec_sillycount(data
->dir
);
102 nfs_free_unlinkdata(data
);
106 #if defined(CONFIG_NFS_V4_1)
107 void nfs_unlink_prepare(struct rpc_task
*task
, void *calldata
)
109 struct nfs_unlinkdata
*data
= calldata
;
110 struct nfs_server
*server
= NFS_SERVER(data
->dir
);
112 if (nfs4_setup_sequence(server
->nfs_client
, &data
->args
.seq_args
,
113 &data
->res
.seq_res
, 1, task
))
115 rpc_call_start(task
);
117 #endif /* CONFIG_NFS_V4_1 */
119 static const struct rpc_call_ops nfs_unlink_ops
= {
120 .rpc_call_done
= nfs_async_unlink_done
,
121 .rpc_release
= nfs_async_unlink_release
,
122 #if defined(CONFIG_NFS_V4_1)
123 .rpc_call_prepare
= nfs_unlink_prepare
,
124 #endif /* CONFIG_NFS_V4_1 */
127 static int nfs_do_call_unlink(struct dentry
*parent
, struct inode
*dir
, struct nfs_unlinkdata
*data
)
129 struct rpc_message msg
= {
130 .rpc_argp
= &data
->args
,
131 .rpc_resp
= &data
->res
,
132 .rpc_cred
= data
->cred
,
134 struct rpc_task_setup task_setup_data
= {
136 .callback_ops
= &nfs_unlink_ops
,
137 .callback_data
= data
,
138 .workqueue
= nfsiod_workqueue
,
139 .flags
= RPC_TASK_ASYNC
,
141 struct rpc_task
*task
;
142 struct dentry
*alias
;
144 alias
= d_lookup(parent
, &data
->args
.name
);
149 * Hey, we raced with lookup... See if we need to transfer
150 * the sillyrename information to the aliased dentry.
152 nfs_free_dname(data
);
153 spin_lock(&alias
->d_lock
);
154 if (alias
->d_inode
!= NULL
&&
155 !(alias
->d_flags
& DCACHE_NFSFS_RENAMED
)) {
156 alias
->d_fsdata
= data
;
157 alias
->d_flags
|= DCACHE_NFSFS_RENAMED
;
160 spin_unlock(&alias
->d_lock
);
161 nfs_dec_sillycount(dir
);
165 data
->dir
= igrab(dir
);
167 nfs_dec_sillycount(dir
);
170 nfs_sb_active(dir
->i_sb
);
171 data
->args
.fh
= NFS_FH(dir
);
172 nfs_fattr_init(&data
->res
.dir_attr
);
174 NFS_PROTO(dir
)->unlink_setup(&msg
, dir
);
176 task_setup_data
.rpc_client
= NFS_CLIENT(dir
);
177 task
= rpc_run_task(&task_setup_data
);
183 static int nfs_call_unlink(struct dentry
*dentry
, struct nfs_unlinkdata
*data
)
185 struct dentry
*parent
;
190 parent
= dget_parent(dentry
);
193 dir
= parent
->d_inode
;
194 if (nfs_copy_dname(dentry
, data
) != 0)
196 /* Non-exclusive lock protects against concurrent lookup() calls */
197 spin_lock(&dir
->i_lock
);
198 if (atomic_inc_not_zero(&NFS_I(dir
)->silly_count
) == 0) {
199 /* Deferred delete */
200 hlist_add_head(&data
->list
, &NFS_I(dir
)->silly_list
);
201 spin_unlock(&dir
->i_lock
);
205 spin_unlock(&dir
->i_lock
);
206 ret
= nfs_do_call_unlink(parent
, dir
, data
);
213 void nfs_block_sillyrename(struct dentry
*dentry
)
215 struct nfs_inode
*nfsi
= NFS_I(dentry
->d_inode
);
217 wait_event(nfsi
->waitqueue
, atomic_cmpxchg(&nfsi
->silly_count
, 1, 0) == 1);
220 void nfs_unblock_sillyrename(struct dentry
*dentry
)
222 struct inode
*dir
= dentry
->d_inode
;
223 struct nfs_inode
*nfsi
= NFS_I(dir
);
224 struct nfs_unlinkdata
*data
;
226 atomic_inc(&nfsi
->silly_count
);
227 spin_lock(&dir
->i_lock
);
228 while (!hlist_empty(&nfsi
->silly_list
)) {
229 if (!atomic_inc_not_zero(&nfsi
->silly_count
))
231 data
= hlist_entry(nfsi
->silly_list
.first
, struct nfs_unlinkdata
, list
);
232 hlist_del(&data
->list
);
233 spin_unlock(&dir
->i_lock
);
234 if (nfs_do_call_unlink(dentry
, dir
, data
) == 0)
235 nfs_free_unlinkdata(data
);
236 spin_lock(&dir
->i_lock
);
238 spin_unlock(&dir
->i_lock
);
242 * nfs_async_unlink - asynchronous unlinking of a file
243 * @dir: parent directory of dentry
244 * @dentry: dentry to unlink
247 nfs_async_unlink(struct inode
*dir
, struct dentry
*dentry
)
249 struct nfs_unlinkdata
*data
;
250 int status
= -ENOMEM
;
252 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
256 data
->cred
= rpc_lookup_cred();
257 if (IS_ERR(data
->cred
)) {
258 status
= PTR_ERR(data
->cred
);
261 data
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
264 spin_lock(&dentry
->d_lock
);
265 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)
267 dentry
->d_flags
|= DCACHE_NFSFS_RENAMED
;
268 dentry
->d_fsdata
= data
;
269 spin_unlock(&dentry
->d_lock
);
272 spin_unlock(&dentry
->d_lock
);
273 put_rpccred(data
->cred
);
281 * nfs_complete_unlink - Initialize completion of the sillydelete
282 * @dentry: dentry to delete
285 * Since we're most likely to be called by dentry_iput(), we
286 * only use the dentry to find the sillydelete. We then copy the name
290 nfs_complete_unlink(struct dentry
*dentry
, struct inode
*inode
)
292 struct nfs_unlinkdata
*data
= NULL
;
294 spin_lock(&dentry
->d_lock
);
295 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
) {
296 dentry
->d_flags
&= ~DCACHE_NFSFS_RENAMED
;
297 data
= dentry
->d_fsdata
;
299 spin_unlock(&dentry
->d_lock
);
301 if (data
!= NULL
&& (NFS_STALE(inode
) || !nfs_call_unlink(dentry
, data
)))
302 nfs_free_unlinkdata(data
);