Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/nfs/unlink.c |
| 3 | * |
| 4 | * nfs sillydelete handling |
| 5 | * |
| 6 | * NOTE: we rely on holding the BKL for list manipulation protection. |
| 7 | */ |
| 8 | |
| 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> |
| 15 | |
| 16 | |
| 17 | struct nfs_unlinkdata { |
| 18 | struct nfs_unlinkdata *next; |
| 19 | struct dentry *dir, *dentry; |
| 20 | struct qstr name; |
| 21 | struct rpc_task task; |
| 22 | struct rpc_cred *cred; |
| 23 | unsigned int count; |
| 24 | }; |
| 25 | |
| 26 | static struct nfs_unlinkdata *nfs_deletes; |
| 27 | static RPC_WAITQ(nfs_delete_queue, "nfs_delete_queue"); |
| 28 | |
| 29 | /** |
| 30 | * nfs_detach_unlinkdata - Remove asynchronous unlink from global list |
| 31 | * @data: pointer to descriptor |
| 32 | */ |
| 33 | static inline void |
| 34 | nfs_detach_unlinkdata(struct nfs_unlinkdata *data) |
| 35 | { |
| 36 | struct nfs_unlinkdata **q; |
| 37 | |
| 38 | for (q = &nfs_deletes; *q != NULL; q = &((*q)->next)) { |
| 39 | if (*q == data) { |
| 40 | *q = data->next; |
| 41 | break; |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * nfs_put_unlinkdata - release data from a sillydelete operation. |
| 48 | * @data: pointer to unlink structure. |
| 49 | */ |
| 50 | static void |
| 51 | nfs_put_unlinkdata(struct nfs_unlinkdata *data) |
| 52 | { |
| 53 | if (--data->count == 0) { |
| 54 | nfs_detach_unlinkdata(data); |
Jesper Juhl | f99d49a | 2005-11-07 01:01:34 -0800 | [diff] [blame] | 55 | kfree(data->name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | kfree(data); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | #define NAME_ALLOC_LEN(len) ((len+16) & ~15) |
| 61 | /** |
| 62 | * nfs_copy_dname - copy dentry name to data structure |
| 63 | * @dentry: pointer to dentry |
| 64 | * @data: nfs_unlinkdata |
| 65 | */ |
| 66 | static inline void |
| 67 | nfs_copy_dname(struct dentry *dentry, struct nfs_unlinkdata *data) |
| 68 | { |
| 69 | char *str; |
| 70 | int len = dentry->d_name.len; |
| 71 | |
| 72 | str = kmalloc(NAME_ALLOC_LEN(len), GFP_KERNEL); |
| 73 | if (!str) |
| 74 | return; |
| 75 | memcpy(str, dentry->d_name.name, len); |
| 76 | if (!data->name.len) { |
| 77 | data->name.len = len; |
| 78 | data->name.name = str; |
| 79 | } else |
| 80 | kfree(str); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * nfs_async_unlink_init - Initialize the RPC info |
| 85 | * @task: rpc_task of the sillydelete |
| 86 | * |
| 87 | * We delay initializing RPC info until after the call to dentry_iput() |
| 88 | * in order to minimize races against rename(). |
| 89 | */ |
| 90 | static void |
| 91 | nfs_async_unlink_init(struct rpc_task *task) |
| 92 | { |
| 93 | struct nfs_unlinkdata *data = (struct nfs_unlinkdata *)task->tk_calldata; |
| 94 | struct dentry *dir = data->dir; |
| 95 | struct rpc_message msg = { |
| 96 | .rpc_cred = data->cred, |
| 97 | }; |
| 98 | int status = -ENOENT; |
| 99 | |
| 100 | if (!data->name.len) |
| 101 | goto out_err; |
| 102 | |
| 103 | status = NFS_PROTO(dir->d_inode)->unlink_setup(&msg, dir, &data->name); |
| 104 | if (status < 0) |
| 105 | goto out_err; |
| 106 | nfs_begin_data_update(dir->d_inode); |
| 107 | rpc_call_setup(task, &msg, 0); |
| 108 | return; |
| 109 | out_err: |
| 110 | rpc_exit(task, status); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * nfs_async_unlink_done - Sillydelete post-processing |
| 115 | * @task: rpc_task of the sillydelete |
| 116 | * |
| 117 | * Do the directory attribute update. |
| 118 | */ |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame^] | 119 | static void nfs_async_unlink_done(struct rpc_task *task, void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | { |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame^] | 121 | struct nfs_unlinkdata *data = calldata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | struct dentry *dir = data->dir; |
| 123 | struct inode *dir_i; |
| 124 | |
| 125 | if (!dir) |
| 126 | return; |
| 127 | dir_i = dir->d_inode; |
| 128 | nfs_end_data_update(dir_i); |
| 129 | if (NFS_PROTO(dir_i)->unlink_done(dir, task)) |
| 130 | return; |
| 131 | put_rpccred(data->cred); |
| 132 | data->cred = NULL; |
| 133 | dput(dir); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * nfs_async_unlink_release - Release the sillydelete data. |
| 138 | * @task: rpc_task of the sillydelete |
| 139 | * |
| 140 | * We need to call nfs_put_unlinkdata as a 'tk_release' task since the |
| 141 | * rpc_task would be freed too. |
| 142 | */ |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame^] | 143 | static void nfs_async_unlink_release(void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame^] | 145 | struct nfs_unlinkdata *data = calldata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | nfs_put_unlinkdata(data); |
| 147 | } |
| 148 | |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame^] | 149 | static const struct rpc_call_ops nfs_unlink_ops = { |
| 150 | .rpc_call_done = nfs_async_unlink_done, |
| 151 | .rpc_release = nfs_async_unlink_release, |
| 152 | }; |
| 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | /** |
| 155 | * nfs_async_unlink - asynchronous unlinking of a file |
| 156 | * @dentry: dentry to unlink |
| 157 | */ |
| 158 | int |
| 159 | nfs_async_unlink(struct dentry *dentry) |
| 160 | { |
| 161 | struct dentry *dir = dentry->d_parent; |
| 162 | struct nfs_unlinkdata *data; |
| 163 | struct rpc_task *task; |
| 164 | struct rpc_clnt *clnt = NFS_CLIENT(dir->d_inode); |
| 165 | int status = -ENOMEM; |
| 166 | |
| 167 | data = kmalloc(sizeof(*data), GFP_KERNEL); |
| 168 | if (!data) |
| 169 | goto out; |
| 170 | memset(data, 0, sizeof(*data)); |
| 171 | |
| 172 | data->cred = rpcauth_lookupcred(clnt->cl_auth, 0); |
| 173 | if (IS_ERR(data->cred)) { |
| 174 | status = PTR_ERR(data->cred); |
| 175 | goto out_free; |
| 176 | } |
| 177 | data->dir = dget(dir); |
| 178 | data->dentry = dentry; |
| 179 | |
| 180 | data->next = nfs_deletes; |
| 181 | nfs_deletes = data; |
| 182 | data->count = 1; |
| 183 | |
| 184 | task = &data->task; |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame^] | 185 | rpc_init_task(task, clnt, RPC_TASK_ASYNC, &nfs_unlink_ops, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | task->tk_action = nfs_async_unlink_init; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
| 188 | spin_lock(&dentry->d_lock); |
| 189 | dentry->d_flags |= DCACHE_NFSFS_RENAMED; |
| 190 | spin_unlock(&dentry->d_lock); |
| 191 | |
| 192 | rpc_sleep_on(&nfs_delete_queue, task, NULL, NULL); |
| 193 | status = 0; |
| 194 | out: |
| 195 | return status; |
| 196 | out_free: |
| 197 | kfree(data); |
| 198 | return status; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * nfs_complete_unlink - Initialize completion of the sillydelete |
| 203 | * @dentry: dentry to delete |
| 204 | * |
| 205 | * Since we're most likely to be called by dentry_iput(), we |
| 206 | * only use the dentry to find the sillydelete. We then copy the name |
| 207 | * into the qstr. |
| 208 | */ |
| 209 | void |
| 210 | nfs_complete_unlink(struct dentry *dentry) |
| 211 | { |
| 212 | struct nfs_unlinkdata *data; |
| 213 | |
| 214 | for(data = nfs_deletes; data != NULL; data = data->next) { |
| 215 | if (dentry == data->dentry) |
| 216 | break; |
| 217 | } |
| 218 | if (!data) |
| 219 | return; |
| 220 | data->count++; |
| 221 | nfs_copy_dname(dentry, data); |
| 222 | spin_lock(&dentry->d_lock); |
| 223 | dentry->d_flags &= ~DCACHE_NFSFS_RENAMED; |
| 224 | spin_unlock(&dentry->d_lock); |
| 225 | rpc_wake_up_task(&data->task); |
| 226 | nfs_put_unlinkdata(data); |
| 227 | } |