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