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