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