Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/nfs/delegation.c |
| 3 | * |
| 4 | * Copyright (C) 2004 Trond Myklebust |
| 5 | * |
| 6 | * NFS file delegation management |
| 7 | * |
| 8 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/completion.h> |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 10 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/spinlock.h> |
| 15 | |
| 16 | #include <linux/nfs4.h> |
| 17 | #include <linux/nfs_fs.h> |
| 18 | #include <linux/nfs_xdr.h> |
| 19 | |
Trond Myklebust | 4ce7971 | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 20 | #include "nfs4_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "delegation.h" |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 22 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Trond Myklebust | 905f8d1 | 2007-08-06 12:18:34 -0400 | [diff] [blame] | 24 | static void nfs_do_free_delegation(struct nfs_delegation *delegation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | { |
David Howells | 17d2c0a | 2010-05-01 12:37:18 -0400 | [diff] [blame] | 26 | if (delegation->cred) |
| 27 | put_rpccred(delegation->cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | kfree(delegation); |
| 29 | } |
| 30 | |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 31 | static void nfs_free_delegation_callback(struct rcu_head *head) |
| 32 | { |
| 33 | struct nfs_delegation *delegation = container_of(head, struct nfs_delegation, rcu); |
| 34 | |
Trond Myklebust | 905f8d1 | 2007-08-06 12:18:34 -0400 | [diff] [blame] | 35 | nfs_do_free_delegation(delegation); |
| 36 | } |
| 37 | |
| 38 | static void nfs_free_delegation(struct nfs_delegation *delegation) |
| 39 | { |
Trond Myklebust | 905f8d1 | 2007-08-06 12:18:34 -0400 | [diff] [blame] | 40 | call_rcu(&delegation->rcu, nfs_free_delegation_callback); |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 41 | } |
| 42 | |
Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 43 | void nfs_mark_delegation_referenced(struct nfs_delegation *delegation) |
| 44 | { |
| 45 | set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags); |
| 46 | } |
| 47 | |
Trond Myklebust | bd7bf9d | 2008-12-23 15:21:53 -0500 | [diff] [blame] | 48 | int nfs_have_delegation(struct inode *inode, fmode_t flags) |
Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 49 | { |
| 50 | struct nfs_delegation *delegation; |
| 51 | int ret = 0; |
| 52 | |
| 53 | flags &= FMODE_READ|FMODE_WRITE; |
| 54 | rcu_read_lock(); |
| 55 | delegation = rcu_dereference(NFS_I(inode)->delegation); |
| 56 | if (delegation != NULL && (delegation->type & flags) == flags) { |
| 57 | nfs_mark_delegation_referenced(delegation); |
| 58 | ret = 1; |
| 59 | } |
| 60 | rcu_read_unlock(); |
| 61 | return ret; |
| 62 | } |
| 63 | |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 64 | static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state) |
| 65 | { |
| 66 | struct inode *inode = state->inode; |
| 67 | struct file_lock *fl; |
Trond Myklebust | d512220 | 2009-06-17 13:22:58 -0700 | [diff] [blame] | 68 | int status = 0; |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 69 | |
Trond Myklebust | 3f09df7 | 2009-06-17 13:23:00 -0700 | [diff] [blame] | 70 | if (inode->i_flock == NULL) |
| 71 | goto out; |
| 72 | |
Arnd Bergmann | b89f432 | 2010-09-18 15:09:31 +0200 | [diff] [blame] | 73 | /* Protect inode->i_flock using the file locks lock */ |
| 74 | lock_flocks(); |
Harvey Harrison | 90dc7d2 | 2008-02-20 13:03:05 -0800 | [diff] [blame] | 75 | for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 76 | if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK))) |
| 77 | continue; |
Trond Myklebust | cd3758e | 2007-08-10 17:44:32 -0400 | [diff] [blame] | 78 | if (nfs_file_open_context(fl->fl_file) != ctx) |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 79 | continue; |
Arnd Bergmann | b89f432 | 2010-09-18 15:09:31 +0200 | [diff] [blame] | 80 | unlock_flocks(); |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 81 | status = nfs4_lock_delegation_recall(state, fl); |
Trond Myklebust | d512220 | 2009-06-17 13:22:58 -0700 | [diff] [blame] | 82 | if (status < 0) |
Trond Myklebust | 3f09df7 | 2009-06-17 13:23:00 -0700 | [diff] [blame] | 83 | goto out; |
Arnd Bergmann | b89f432 | 2010-09-18 15:09:31 +0200 | [diff] [blame] | 84 | lock_flocks(); |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 85 | } |
Arnd Bergmann | b89f432 | 2010-09-18 15:09:31 +0200 | [diff] [blame] | 86 | unlock_flocks(); |
Trond Myklebust | 3f09df7 | 2009-06-17 13:23:00 -0700 | [diff] [blame] | 87 | out: |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 88 | return status; |
| 89 | } |
| 90 | |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 91 | static int nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | { |
| 93 | struct nfs_inode *nfsi = NFS_I(inode); |
| 94 | struct nfs_open_context *ctx; |
| 95 | struct nfs4_state *state; |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 96 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | again: |
| 99 | spin_lock(&inode->i_lock); |
| 100 | list_for_each_entry(ctx, &nfsi->open_files, list) { |
| 101 | state = ctx->state; |
| 102 | if (state == NULL) |
| 103 | continue; |
| 104 | if (!test_bit(NFS_DELEGATED_STATE, &state->flags)) |
| 105 | continue; |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 106 | if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0) |
| 107 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | get_nfs_open_context(ctx); |
| 109 | spin_unlock(&inode->i_lock); |
Trond Myklebust | 13437e1 | 2007-07-06 15:10:43 -0400 | [diff] [blame] | 110 | err = nfs4_open_delegation_recall(ctx, state, stateid); |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 111 | if (err >= 0) |
| 112 | err = nfs_delegation_claim_locks(ctx, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | put_nfs_open_context(ctx); |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 114 | if (err != 0) |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 115 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | goto again; |
| 117 | } |
| 118 | spin_unlock(&inode->i_lock); |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 119 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Set up a delegation on an inode |
| 124 | */ |
| 125 | void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res) |
| 126 | { |
Trond Myklebust | 8f649c3 | 2010-05-01 12:36:18 -0400 | [diff] [blame] | 127 | struct nfs_delegation *delegation; |
| 128 | struct rpc_cred *oldcred = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Trond Myklebust | 8f649c3 | 2010-05-01 12:36:18 -0400 | [diff] [blame] | 130 | rcu_read_lock(); |
| 131 | delegation = rcu_dereference(NFS_I(inode)->delegation); |
| 132 | if (delegation != NULL) { |
| 133 | spin_lock(&delegation->lock); |
| 134 | if (delegation->inode != NULL) { |
| 135 | memcpy(delegation->stateid.data, res->delegation.data, |
| 136 | sizeof(delegation->stateid.data)); |
| 137 | delegation->type = res->delegation_type; |
| 138 | delegation->maxsize = res->maxsize; |
| 139 | oldcred = delegation->cred; |
| 140 | delegation->cred = get_rpccred(cred); |
| 141 | clear_bit(NFS_DELEGATION_NEED_RECLAIM, |
| 142 | &delegation->flags); |
| 143 | NFS_I(inode)->delegation_state = delegation->type; |
| 144 | spin_unlock(&delegation->lock); |
| 145 | put_rpccred(oldcred); |
| 146 | rcu_read_unlock(); |
| 147 | } else { |
| 148 | /* We appear to have raced with a delegation return. */ |
| 149 | spin_unlock(&delegation->lock); |
| 150 | rcu_read_unlock(); |
| 151 | nfs_inode_set_delegation(inode, cred, res); |
| 152 | } |
| 153 | } else { |
| 154 | rcu_read_unlock(); |
| 155 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 158 | static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync) |
| 159 | { |
| 160 | int res = 0; |
| 161 | |
| 162 | res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync); |
| 163 | nfs_free_delegation(delegation); |
| 164 | return res; |
| 165 | } |
| 166 | |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 167 | static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation) |
| 168 | { |
| 169 | struct inode *inode = NULL; |
| 170 | |
| 171 | spin_lock(&delegation->lock); |
| 172 | if (delegation->inode != NULL) |
| 173 | inode = igrab(delegation->inode); |
| 174 | spin_unlock(&delegation->lock); |
| 175 | return inode; |
| 176 | } |
| 177 | |
Chuck Lever | dda4b22 | 2010-12-24 01:32:54 +0000 | [diff] [blame^] | 178 | static struct nfs_delegation * |
| 179 | nfs_detach_delegation_locked(struct nfs_inode *nfsi, |
| 180 | struct nfs_client *clp) |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 181 | { |
David Howells | 17d2c0a | 2010-05-01 12:37:18 -0400 | [diff] [blame] | 182 | struct nfs_delegation *delegation = |
| 183 | rcu_dereference_protected(nfsi->delegation, |
| 184 | lockdep_is_held(&clp->cl_lock)); |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 185 | |
| 186 | if (delegation == NULL) |
| 187 | goto nomatch; |
Chuck Lever | dda4b22 | 2010-12-24 01:32:54 +0000 | [diff] [blame^] | 188 | |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 189 | spin_lock(&delegation->lock); |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 190 | list_del_rcu(&delegation->super_list); |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 191 | delegation->inode = NULL; |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 192 | nfsi->delegation_state = 0; |
| 193 | rcu_assign_pointer(nfsi->delegation, NULL); |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 194 | spin_unlock(&delegation->lock); |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 195 | return delegation; |
| 196 | nomatch: |
| 197 | return NULL; |
| 198 | } |
| 199 | |
Chuck Lever | dda4b22 | 2010-12-24 01:32:54 +0000 | [diff] [blame^] | 200 | static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi, |
| 201 | struct nfs_client *clp) |
| 202 | { |
| 203 | struct nfs_delegation *delegation; |
| 204 | |
| 205 | spin_lock(&clp->cl_lock); |
| 206 | delegation = nfs_detach_delegation_locked(nfsi, clp); |
| 207 | spin_unlock(&clp->cl_lock); |
| 208 | return delegation; |
| 209 | } |
| 210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | /* |
| 212 | * Set up a delegation on an inode |
| 213 | */ |
| 214 | int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res) |
| 215 | { |
David Howells | 7539bba | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 216 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | struct nfs_inode *nfsi = NFS_I(inode); |
David Howells | 17d2c0a | 2010-05-01 12:37:18 -0400 | [diff] [blame] | 218 | struct nfs_delegation *delegation, *old_delegation; |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 219 | struct nfs_delegation *freeme = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | int status = 0; |
| 221 | |
Trond Myklebust | 8535b2b | 2010-05-13 12:51:01 -0400 | [diff] [blame] | 222 | delegation = kmalloc(sizeof(*delegation), GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | if (delegation == NULL) |
| 224 | return -ENOMEM; |
| 225 | memcpy(delegation->stateid.data, res->delegation.data, |
| 226 | sizeof(delegation->stateid.data)); |
| 227 | delegation->type = res->delegation_type; |
| 228 | delegation->maxsize = res->maxsize; |
Trond Myklebust | beb2a5e | 2006-01-03 09:55:37 +0100 | [diff] [blame] | 229 | delegation->change_attr = nfsi->change_attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | delegation->cred = get_rpccred(cred); |
| 231 | delegation->inode = inode; |
Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 232 | delegation->flags = 1<<NFS_DELEGATION_REFERENCED; |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 233 | spin_lock_init(&delegation->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
| 235 | spin_lock(&clp->cl_lock); |
David Howells | 17d2c0a | 2010-05-01 12:37:18 -0400 | [diff] [blame] | 236 | old_delegation = rcu_dereference_protected(nfsi->delegation, |
| 237 | lockdep_is_held(&clp->cl_lock)); |
| 238 | if (old_delegation != NULL) { |
| 239 | if (memcmp(&delegation->stateid, &old_delegation->stateid, |
| 240 | sizeof(old_delegation->stateid)) == 0 && |
| 241 | delegation->type == old_delegation->type) { |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 242 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 244 | /* |
| 245 | * Deal with broken servers that hand out two |
| 246 | * delegations for the same file. |
| 247 | */ |
| 248 | dfprintk(FILE, "%s: server %s handed out " |
| 249 | "a duplicate delegation!\n", |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 250 | __func__, clp->cl_hostname); |
David Howells | 17d2c0a | 2010-05-01 12:37:18 -0400 | [diff] [blame] | 251 | if (delegation->type <= old_delegation->type) { |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 252 | freeme = delegation; |
| 253 | delegation = NULL; |
| 254 | goto out; |
| 255 | } |
Chuck Lever | dda4b22 | 2010-12-24 01:32:54 +0000 | [diff] [blame^] | 256 | freeme = nfs_detach_delegation_locked(nfsi, clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 258 | list_add_rcu(&delegation->super_list, &clp->cl_delegations); |
| 259 | nfsi->delegation_state = delegation->type; |
| 260 | rcu_assign_pointer(nfsi->delegation, delegation); |
| 261 | delegation = NULL; |
Trond Myklebust | 412c77c | 2007-07-03 16:10:55 -0400 | [diff] [blame] | 262 | |
| 263 | /* Ensure we revalidate the attributes and page cache! */ |
| 264 | spin_lock(&inode->i_lock); |
| 265 | nfsi->cache_validity |= NFS_INO_REVAL_FORCED; |
| 266 | spin_unlock(&inode->i_lock); |
| 267 | |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 268 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 603c83d | 2007-10-18 19:59:20 -0400 | [diff] [blame] | 270 | if (delegation != NULL) |
| 271 | nfs_free_delegation(delegation); |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 272 | if (freeme != NULL) |
| 273 | nfs_do_return_delegation(inode, freeme, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | return status; |
| 275 | } |
| 276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | /* |
| 278 | * Basic procedure for returning a delegation to the server |
| 279 | */ |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 280 | static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | struct nfs_inode *nfsi = NFS_I(inode); |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 283 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
Trond Myklebust | 3f09df7 | 2009-06-17 13:23:00 -0700 | [diff] [blame] | 285 | /* |
| 286 | * Guard against new delegated open/lock/unlock calls and against |
| 287 | * state recovery |
| 288 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | down_write(&nfsi->rwsem); |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 290 | err = nfs_delegation_claim_opens(inode, &delegation->stateid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | up_write(&nfsi->rwsem); |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 292 | if (err) |
| 293 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 295 | err = nfs_do_return_delegation(inode, delegation, issync); |
| 296 | out: |
| 297 | return err; |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 298 | } |
| 299 | |
Trond Myklebust | e6f8107 | 2008-01-24 18:14:34 -0500 | [diff] [blame] | 300 | /* |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 301 | * Return all delegations that have been marked for return |
| 302 | */ |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 303 | int nfs_client_return_marked_delegations(struct nfs_client *clp) |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 304 | { |
| 305 | struct nfs_delegation *delegation; |
| 306 | struct inode *inode; |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 307 | int err = 0; |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 308 | |
| 309 | restart: |
| 310 | rcu_read_lock(); |
| 311 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { |
| 312 | if (!test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags)) |
| 313 | continue; |
| 314 | inode = nfs_delegation_grab_inode(delegation); |
| 315 | if (inode == NULL) |
| 316 | continue; |
Chuck Lever | dda4b22 | 2010-12-24 01:32:54 +0000 | [diff] [blame^] | 317 | delegation = nfs_detach_delegation(NFS_I(inode), clp); |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 318 | rcu_read_unlock(); |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 319 | if (delegation != NULL) { |
| 320 | filemap_flush(inode->i_mapping); |
| 321 | err = __nfs_inode_return_delegation(inode, delegation, 0); |
| 322 | } |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 323 | iput(inode); |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 324 | if (!err) |
| 325 | goto restart; |
| 326 | set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state); |
| 327 | return err; |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 328 | } |
| 329 | rcu_read_unlock(); |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 330 | return 0; |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /* |
Trond Myklebust | e6f8107 | 2008-01-24 18:14:34 -0500 | [diff] [blame] | 334 | * This function returns the delegation without reclaiming opens |
| 335 | * or protecting against delegation reclaims. |
| 336 | * It is therefore really only safe to be called from |
| 337 | * nfs4_clear_inode() |
| 338 | */ |
| 339 | void nfs_inode_return_delegation_noreclaim(struct inode *inode) |
| 340 | { |
| 341 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
| 342 | struct nfs_inode *nfsi = NFS_I(inode); |
| 343 | struct nfs_delegation *delegation; |
| 344 | |
David Howells | 17d2c0a | 2010-05-01 12:37:18 -0400 | [diff] [blame] | 345 | if (rcu_access_pointer(nfsi->delegation) != NULL) { |
Chuck Lever | dda4b22 | 2010-12-24 01:32:54 +0000 | [diff] [blame^] | 346 | delegation = nfs_detach_delegation(nfsi, clp); |
Trond Myklebust | e6f8107 | 2008-01-24 18:14:34 -0500 | [diff] [blame] | 347 | if (delegation != NULL) |
| 348 | nfs_do_return_delegation(inode, delegation, 0); |
| 349 | } |
| 350 | } |
| 351 | |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 352 | int nfs_inode_return_delegation(struct inode *inode) |
| 353 | { |
| 354 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
| 355 | struct nfs_inode *nfsi = NFS_I(inode); |
| 356 | struct nfs_delegation *delegation; |
| 357 | int err = 0; |
| 358 | |
David Howells | 17d2c0a | 2010-05-01 12:37:18 -0400 | [diff] [blame] | 359 | if (rcu_access_pointer(nfsi->delegation) != NULL) { |
Chuck Lever | dda4b22 | 2010-12-24 01:32:54 +0000 | [diff] [blame^] | 360 | delegation = nfs_detach_delegation(nfsi, clp); |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 361 | if (delegation != NULL) { |
Trond Myklebust | 1b924e5 | 2010-07-31 14:29:06 -0400 | [diff] [blame] | 362 | nfs_wb_all(inode); |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 363 | err = __nfs_inode_return_delegation(inode, delegation, 1); |
| 364 | } |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 365 | } |
| 366 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Trond Myklebust | 6411bd4 | 2008-12-23 15:21:51 -0500 | [diff] [blame] | 369 | static void nfs_mark_return_delegation(struct nfs_client *clp, struct nfs_delegation *delegation) |
| 370 | { |
| 371 | set_bit(NFS_DELEGATION_RETURN, &delegation->flags); |
| 372 | set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state); |
| 373 | } |
| 374 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | /* |
| 376 | * Return all delegations associated to a super block |
| 377 | */ |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 378 | void nfs_super_return_all_delegations(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | { |
David Howells | 7539bba | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 380 | struct nfs_client *clp = NFS_SB(sb)->nfs_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | struct nfs_delegation *delegation; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | |
| 383 | if (clp == NULL) |
| 384 | return; |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 385 | rcu_read_lock(); |
| 386 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 387 | spin_lock(&delegation->lock); |
| 388 | if (delegation->inode != NULL && delegation->inode->i_sb == sb) |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 389 | set_bit(NFS_DELEGATION_RETURN, &delegation->flags); |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 390 | spin_unlock(&delegation->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | } |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 392 | rcu_read_unlock(); |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 393 | if (nfs_client_return_marked_delegations(clp) != 0) |
| 394 | nfs4_schedule_state_manager(clp); |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 395 | } |
| 396 | |
Alexandros Batsakis | c79571a | 2009-12-05 13:20:52 -0500 | [diff] [blame] | 397 | static |
| 398 | void nfs_client_mark_return_all_delegation_types(struct nfs_client *clp, fmode_t flags) |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 399 | { |
| 400 | struct nfs_delegation *delegation; |
| 401 | |
| 402 | rcu_read_lock(); |
Trond Myklebust | 707fb4b | 2008-12-23 15:21:47 -0500 | [diff] [blame] | 403 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { |
Alexandros Batsakis | c79571a | 2009-12-05 13:20:52 -0500 | [diff] [blame] | 404 | if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE)) |
| 405 | continue; |
| 406 | if (delegation->type & flags) |
| 407 | nfs_mark_return_delegation(clp, delegation); |
Trond Myklebust | 707fb4b | 2008-12-23 15:21:47 -0500 | [diff] [blame] | 408 | } |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 409 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Alexandros Batsakis | c79571a | 2009-12-05 13:20:52 -0500 | [diff] [blame] | 412 | static void nfs_client_mark_return_all_delegations(struct nfs_client *clp) |
| 413 | { |
| 414 | nfs_client_mark_return_all_delegation_types(clp, FMODE_READ|FMODE_WRITE); |
| 415 | } |
| 416 | |
Trond Myklebust | b0d3ded | 2008-12-23 15:21:50 -0500 | [diff] [blame] | 417 | static void nfs_delegation_run_state_manager(struct nfs_client *clp) |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 418 | { |
Trond Myklebust | b0d3ded | 2008-12-23 15:21:50 -0500 | [diff] [blame] | 419 | if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) |
| 420 | nfs4_schedule_state_manager(clp); |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 421 | } |
| 422 | |
Alexandros Batsakis | 31f0960 | 2009-12-05 13:27:02 -0500 | [diff] [blame] | 423 | void nfs_expire_all_delegation_types(struct nfs_client *clp, fmode_t flags) |
Alexandros Batsakis | c79571a | 2009-12-05 13:20:52 -0500 | [diff] [blame] | 424 | { |
| 425 | nfs_client_mark_return_all_delegation_types(clp, flags); |
| 426 | nfs_delegation_run_state_manager(clp); |
| 427 | } |
| 428 | |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 429 | void nfs_expire_all_delegations(struct nfs_client *clp) |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 430 | { |
Alexandros Batsakis | c79571a | 2009-12-05 13:20:52 -0500 | [diff] [blame] | 431 | nfs_expire_all_delegation_types(clp, FMODE_READ|FMODE_WRITE); |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 432 | } |
| 433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | /* |
| 435 | * Return all delegations following an NFS4ERR_CB_PATH_DOWN error. |
| 436 | */ |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 437 | void nfs_handle_cb_pathdown(struct nfs_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | if (clp == NULL) |
| 440 | return; |
Trond Myklebust | 707fb4b | 2008-12-23 15:21:47 -0500 | [diff] [blame] | 441 | nfs_client_mark_return_all_delegations(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 444 | static void nfs_client_mark_return_unreferenced_delegations(struct nfs_client *clp) |
| 445 | { |
| 446 | struct nfs_delegation *delegation; |
| 447 | |
| 448 | rcu_read_lock(); |
| 449 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { |
| 450 | if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags)) |
| 451 | continue; |
Alexandros Batsakis | b4a6f49 | 2009-12-05 13:19:11 -0500 | [diff] [blame] | 452 | nfs_mark_return_delegation(clp, delegation); |
Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 453 | } |
| 454 | rcu_read_unlock(); |
| 455 | } |
| 456 | |
| 457 | void nfs_expire_unreferenced_delegations(struct nfs_client *clp) |
| 458 | { |
| 459 | nfs_client_mark_return_unreferenced_delegations(clp); |
| 460 | nfs_delegation_run_state_manager(clp); |
| 461 | } |
| 462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | /* |
| 464 | * Asynchronous delegation recall! |
| 465 | */ |
Trond Myklebust | e047a10 | 2010-06-16 09:52:27 -0400 | [diff] [blame] | 466 | int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | { |
Trond Myklebust | 6411bd4 | 2008-12-23 15:21:51 -0500 | [diff] [blame] | 468 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
| 469 | struct nfs_delegation *delegation; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
Trond Myklebust | 6411bd4 | 2008-12-23 15:21:51 -0500 | [diff] [blame] | 471 | rcu_read_lock(); |
| 472 | delegation = rcu_dereference(NFS_I(inode)->delegation); |
Alexandros Batsakis | 2597641 | 2009-12-05 13:48:55 -0500 | [diff] [blame] | 473 | |
Trond Myklebust | e047a10 | 2010-06-16 09:52:27 -0400 | [diff] [blame] | 474 | if (!clp->cl_mvops->validate_stateid(delegation, stateid)) { |
Trond Myklebust | 6411bd4 | 2008-12-23 15:21:51 -0500 | [diff] [blame] | 475 | rcu_read_unlock(); |
| 476 | return -ENOENT; |
| 477 | } |
Alexandros Batsakis | 2597641 | 2009-12-05 13:48:55 -0500 | [diff] [blame] | 478 | |
Trond Myklebust | 6411bd4 | 2008-12-23 15:21:51 -0500 | [diff] [blame] | 479 | nfs_mark_return_delegation(clp, delegation); |
| 480 | rcu_read_unlock(); |
| 481 | nfs_delegation_run_state_manager(clp); |
| 482 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | /* |
| 486 | * Retrieve the inode associated with a delegation |
| 487 | */ |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 488 | struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | { |
| 490 | struct nfs_delegation *delegation; |
| 491 | struct inode *res = NULL; |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 492 | rcu_read_lock(); |
| 493 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 494 | spin_lock(&delegation->lock); |
| 495 | if (delegation->inode != NULL && |
| 496 | nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | res = igrab(delegation->inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 499 | spin_unlock(&delegation->lock); |
| 500 | if (res != NULL) |
| 501 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | } |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 503 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | return res; |
| 505 | } |
| 506 | |
| 507 | /* |
| 508 | * Mark all delegations as needing to be reclaimed |
| 509 | */ |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 510 | void nfs_delegation_mark_reclaim(struct nfs_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | { |
| 512 | struct nfs_delegation *delegation; |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 513 | rcu_read_lock(); |
| 514 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) |
Trond Myklebust | 15c831b | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 515 | set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags); |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 516 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /* |
| 520 | * Reap all unclaimed delegations after reboot recovery is done |
| 521 | */ |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 522 | void nfs_delegation_reap_unclaimed(struct nfs_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | { |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 524 | struct nfs_delegation *delegation; |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 525 | struct inode *inode; |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 526 | restart: |
| 527 | rcu_read_lock(); |
| 528 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { |
Trond Myklebust | 15c831b | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 529 | if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | continue; |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 531 | inode = nfs_delegation_grab_inode(delegation); |
| 532 | if (inode == NULL) |
| 533 | continue; |
Chuck Lever | dda4b22 | 2010-12-24 01:32:54 +0000 | [diff] [blame^] | 534 | delegation = nfs_detach_delegation(NFS_I(inode), clp); |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 535 | rcu_read_unlock(); |
| 536 | if (delegation != NULL) |
Trond Myklebust | 905f8d1 | 2007-08-06 12:18:34 -0400 | [diff] [blame] | 537 | nfs_free_delegation(delegation); |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 538 | iput(inode); |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 539 | goto restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | } |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 541 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | } |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 543 | |
| 544 | int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode) |
| 545 | { |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 546 | struct nfs_inode *nfsi = NFS_I(inode); |
| 547 | struct nfs_delegation *delegation; |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 548 | int ret = 0; |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 549 | |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 550 | rcu_read_lock(); |
| 551 | delegation = rcu_dereference(nfsi->delegation); |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 552 | if (delegation != NULL) { |
| 553 | memcpy(dst->data, delegation->stateid.data, sizeof(dst->data)); |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 554 | ret = 1; |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 555 | } |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 556 | rcu_read_unlock(); |
| 557 | return ret; |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 558 | } |