]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/nfs/unlink.c | |
3 | * | |
4 | * nfs sillydelete handling | |
5 | * | |
1da177e4 LT |
6 | */ |
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> | |
b35e7041 LT |
14 | #include <linux/sched.h> |
15 | #include <linux/wait.h> | |
1da177e4 | 16 | |
ef818a28 SD |
17 | #include "internal.h" |
18 | ||
1da177e4 | 19 | struct nfs_unlinkdata { |
565277f6 | 20 | struct hlist_node list; |
e4eff1a6 TM |
21 | struct nfs_removeargs args; |
22 | struct nfs_removeres res; | |
23 | struct inode *dir; | |
1da177e4 | 24 | struct rpc_cred *cred; |
1da177e4 LT |
25 | }; |
26 | ||
1da177e4 | 27 | /** |
e4eff1a6 | 28 | * nfs_free_unlinkdata - release data from a sillydelete operation. |
1da177e4 LT |
29 | * @data: pointer to unlink structure. |
30 | */ | |
31 | static void | |
e4eff1a6 | 32 | nfs_free_unlinkdata(struct nfs_unlinkdata *data) |
1da177e4 | 33 | { |
e4eff1a6 TM |
34 | iput(data->dir); |
35 | put_rpccred(data->cred); | |
36 | kfree(data->args.name.name); | |
37 | kfree(data); | |
1da177e4 LT |
38 | } |
39 | ||
40 | #define NAME_ALLOC_LEN(len) ((len+16) & ~15) | |
41 | /** | |
42 | * nfs_copy_dname - copy dentry name to data structure | |
43 | * @dentry: pointer to dentry | |
44 | * @data: nfs_unlinkdata | |
45 | */ | |
e4eff1a6 | 46 | static int nfs_copy_dname(struct dentry *dentry, struct nfs_unlinkdata *data) |
1da177e4 LT |
47 | { |
48 | char *str; | |
49 | int len = dentry->d_name.len; | |
50 | ||
e4eff1a6 | 51 | str = kmemdup(dentry->d_name.name, NAME_ALLOC_LEN(len), GFP_KERNEL); |
1da177e4 | 52 | if (!str) |
e4eff1a6 TM |
53 | return -ENOMEM; |
54 | data->args.name.len = len; | |
55 | data->args.name.name = str; | |
56 | return 0; | |
1da177e4 LT |
57 | } |
58 | ||
565277f6 TM |
59 | static void nfs_free_dname(struct nfs_unlinkdata *data) |
60 | { | |
61 | kfree(data->args.name.name); | |
62 | data->args.name.name = NULL; | |
63 | data->args.name.len = 0; | |
64 | } | |
65 | ||
66 | static void nfs_dec_sillycount(struct inode *dir) | |
67 | { | |
68 | struct nfs_inode *nfsi = NFS_I(dir); | |
69 | if (atomic_dec_return(&nfsi->silly_count) == 1) | |
70 | wake_up(&nfsi->waitqueue); | |
71 | } | |
72 | ||
1da177e4 LT |
73 | /** |
74 | * nfs_async_unlink_done - Sillydelete post-processing | |
75 | * @task: rpc_task of the sillydelete | |
76 | * | |
77 | * Do the directory attribute update. | |
78 | */ | |
963d8fe5 | 79 | static void nfs_async_unlink_done(struct rpc_task *task, void *calldata) |
1da177e4 | 80 | { |
e4eff1a6 TM |
81 | struct nfs_unlinkdata *data = calldata; |
82 | struct inode *dir = data->dir; | |
83 | ||
84 | if (!NFS_PROTO(dir)->unlink_done(task, dir)) | |
85 | rpc_restart_call(task); | |
1da177e4 LT |
86 | } |
87 | ||
88 | /** | |
89 | * nfs_async_unlink_release - Release the sillydelete data. | |
90 | * @task: rpc_task of the sillydelete | |
91 | * | |
92 | * We need to call nfs_put_unlinkdata as a 'tk_release' task since the | |
93 | * rpc_task would be freed too. | |
94 | */ | |
963d8fe5 | 95 | static void nfs_async_unlink_release(void *calldata) |
1da177e4 | 96 | { |
963d8fe5 | 97 | struct nfs_unlinkdata *data = calldata; |
565277f6 TM |
98 | |
99 | nfs_dec_sillycount(data->dir); | |
ef818a28 | 100 | nfs_sb_deactive(NFS_SERVER(data->dir)); |
e4eff1a6 | 101 | nfs_free_unlinkdata(data); |
1da177e4 LT |
102 | } |
103 | ||
963d8fe5 TM |
104 | static const struct rpc_call_ops nfs_unlink_ops = { |
105 | .rpc_call_done = nfs_async_unlink_done, | |
106 | .rpc_release = nfs_async_unlink_release, | |
107 | }; | |
108 | ||
565277f6 | 109 | static int nfs_do_call_unlink(struct dentry *parent, struct inode *dir, struct nfs_unlinkdata *data) |
e4eff1a6 | 110 | { |
5138fde0 TM |
111 | struct rpc_message msg = { |
112 | .rpc_argp = &data->args, | |
113 | .rpc_resp = &data->res, | |
114 | .rpc_cred = data->cred, | |
115 | }; | |
c970aa85 | 116 | struct rpc_task_setup task_setup_data = { |
5138fde0 | 117 | .rpc_message = &msg, |
c970aa85 TM |
118 | .callback_ops = &nfs_unlink_ops, |
119 | .callback_data = data, | |
120 | .flags = RPC_TASK_ASYNC, | |
121 | }; | |
e4eff1a6 | 122 | struct rpc_task *task; |
565277f6 TM |
123 | struct dentry *alias; |
124 | ||
125 | alias = d_lookup(parent, &data->args.name); | |
126 | if (alias != NULL) { | |
127 | int ret = 0; | |
609005c3 | 128 | |
565277f6 TM |
129 | /* |
130 | * Hey, we raced with lookup... See if we need to transfer | |
131 | * the sillyrename information to the aliased dentry. | |
132 | */ | |
133 | nfs_free_dname(data); | |
134 | spin_lock(&alias->d_lock); | |
609005c3 TM |
135 | if (alias->d_inode != NULL && |
136 | !(alias->d_flags & DCACHE_NFSFS_RENAMED)) { | |
565277f6 | 137 | alias->d_fsdata = data; |
fccca7fc | 138 | alias->d_flags |= DCACHE_NFSFS_RENAMED; |
565277f6 TM |
139 | ret = 1; |
140 | } | |
141 | spin_unlock(&alias->d_lock); | |
142 | nfs_dec_sillycount(dir); | |
143 | dput(alias); | |
144 | return ret; | |
145 | } | |
146 | data->dir = igrab(dir); | |
147 | if (!data->dir) { | |
148 | nfs_dec_sillycount(dir); | |
149 | return 0; | |
150 | } | |
ef818a28 | 151 | nfs_sb_active(NFS_SERVER(dir)); |
565277f6 TM |
152 | data->args.fh = NFS_FH(dir); |
153 | nfs_fattr_init(&data->res.dir_attr); | |
154 | ||
5138fde0 | 155 | NFS_PROTO(dir)->unlink_setup(&msg, dir); |
c970aa85 | 156 | |
5138fde0 | 157 | task_setup_data.rpc_client = NFS_CLIENT(dir); |
c970aa85 | 158 | task = rpc_run_task(&task_setup_data); |
565277f6 TM |
159 | if (!IS_ERR(task)) |
160 | rpc_put_task(task); | |
161 | return 1; | |
162 | } | |
163 | ||
164 | static int nfs_call_unlink(struct dentry *dentry, struct nfs_unlinkdata *data) | |
165 | { | |
e4eff1a6 TM |
166 | struct dentry *parent; |
167 | struct inode *dir; | |
565277f6 | 168 | int ret = 0; |
e4eff1a6 | 169 | |
e4eff1a6 TM |
170 | |
171 | parent = dget_parent(dentry); | |
172 | if (parent == NULL) | |
173 | goto out_free; | |
565277f6 | 174 | dir = parent->d_inode; |
55b70a03 | 175 | if (nfs_copy_dname(dentry, data) != 0) |
565277f6 TM |
176 | goto out_dput; |
177 | /* Non-exclusive lock protects against concurrent lookup() calls */ | |
178 | spin_lock(&dir->i_lock); | |
179 | if (atomic_inc_not_zero(&NFS_I(dir)->silly_count) == 0) { | |
180 | /* Deferred delete */ | |
181 | hlist_add_head(&data->list, &NFS_I(dir)->silly_list); | |
182 | spin_unlock(&dir->i_lock); | |
183 | ret = 1; | |
184 | goto out_dput; | |
185 | } | |
186 | spin_unlock(&dir->i_lock); | |
187 | ret = nfs_do_call_unlink(parent, dir, data); | |
188 | out_dput: | |
e4eff1a6 | 189 | dput(parent); |
565277f6 TM |
190 | out_free: |
191 | return ret; | |
192 | } | |
e4eff1a6 | 193 | |
565277f6 TM |
194 | void nfs_block_sillyrename(struct dentry *dentry) |
195 | { | |
196 | struct nfs_inode *nfsi = NFS_I(dentry->d_inode); | |
e4eff1a6 | 197 | |
565277f6 TM |
198 | wait_event(nfsi->waitqueue, atomic_cmpxchg(&nfsi->silly_count, 1, 0) == 1); |
199 | } | |
200 | ||
201 | void nfs_unblock_sillyrename(struct dentry *dentry) | |
202 | { | |
203 | struct inode *dir = dentry->d_inode; | |
204 | struct nfs_inode *nfsi = NFS_I(dir); | |
205 | struct nfs_unlinkdata *data; | |
206 | ||
207 | atomic_inc(&nfsi->silly_count); | |
208 | spin_lock(&dir->i_lock); | |
209 | while (!hlist_empty(&nfsi->silly_list)) { | |
210 | if (!atomic_inc_not_zero(&nfsi->silly_count)) | |
211 | break; | |
212 | data = hlist_entry(nfsi->silly_list.first, struct nfs_unlinkdata, list); | |
213 | hlist_del(&data->list); | |
214 | spin_unlock(&dir->i_lock); | |
215 | if (nfs_do_call_unlink(dentry, dir, data) == 0) | |
216 | nfs_free_unlinkdata(data); | |
217 | spin_lock(&dir->i_lock); | |
218 | } | |
219 | spin_unlock(&dir->i_lock); | |
e4eff1a6 TM |
220 | } |
221 | ||
1da177e4 LT |
222 | /** |
223 | * nfs_async_unlink - asynchronous unlinking of a file | |
e4eff1a6 | 224 | * @dir: parent directory of dentry |
1da177e4 LT |
225 | * @dentry: dentry to unlink |
226 | */ | |
227 | int | |
e4eff1a6 | 228 | nfs_async_unlink(struct inode *dir, struct dentry *dentry) |
1da177e4 | 229 | { |
e4eff1a6 TM |
230 | struct nfs_unlinkdata *data; |
231 | int status = -ENOMEM; | |
1da177e4 | 232 | |
bd647545 | 233 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
e4eff1a6 | 234 | if (data == NULL) |
1da177e4 | 235 | goto out; |
1da177e4 | 236 | |
e4eff1a6 | 237 | data->cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0); |
1da177e4 LT |
238 | if (IS_ERR(data->cred)) { |
239 | status = PTR_ERR(data->cred); | |
240 | goto out_free; | |
241 | } | |
1da177e4 | 242 | |
e4eff1a6 | 243 | status = -EBUSY; |
1da177e4 | 244 | spin_lock(&dentry->d_lock); |
e4eff1a6 TM |
245 | if (dentry->d_flags & DCACHE_NFSFS_RENAMED) |
246 | goto out_unlock; | |
1da177e4 | 247 | dentry->d_flags |= DCACHE_NFSFS_RENAMED; |
e4eff1a6 | 248 | dentry->d_fsdata = data; |
1da177e4 | 249 | spin_unlock(&dentry->d_lock); |
e4eff1a6 TM |
250 | return 0; |
251 | out_unlock: | |
252 | spin_unlock(&dentry->d_lock); | |
253 | put_rpccred(data->cred); | |
1da177e4 LT |
254 | out_free: |
255 | kfree(data); | |
e4eff1a6 | 256 | out: |
1da177e4 LT |
257 | return status; |
258 | } | |
259 | ||
260 | /** | |
261 | * nfs_complete_unlink - Initialize completion of the sillydelete | |
262 | * @dentry: dentry to delete | |
e4eff1a6 | 263 | * @inode: inode |
1da177e4 LT |
264 | * |
265 | * Since we're most likely to be called by dentry_iput(), we | |
266 | * only use the dentry to find the sillydelete. We then copy the name | |
267 | * into the qstr. | |
268 | */ | |
269 | void | |
e4eff1a6 | 270 | nfs_complete_unlink(struct dentry *dentry, struct inode *inode) |
1da177e4 | 271 | { |
e4eff1a6 | 272 | struct nfs_unlinkdata *data = NULL; |
1da177e4 | 273 | |
1da177e4 | 274 | spin_lock(&dentry->d_lock); |
e4eff1a6 TM |
275 | if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { |
276 | dentry->d_flags &= ~DCACHE_NFSFS_RENAMED; | |
277 | data = dentry->d_fsdata; | |
278 | } | |
1da177e4 | 279 | spin_unlock(&dentry->d_lock); |
e4eff1a6 TM |
280 | |
281 | if (data != NULL && (NFS_STALE(inode) || !nfs_call_unlink(dentry, data))) | |
282 | nfs_free_unlinkdata(data); | |
1da177e4 | 283 | } |