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