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