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 | { |
| 132 | struct nfs_delegation *delegation = NFS_I(inode)->delegation; |
Trond Myklebust | 05c88ba | 2007-10-11 15:11:51 -0400 | [diff] [blame] | 133 | struct rpc_cred *oldcred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
| 135 | if (delegation == NULL) |
| 136 | return; |
| 137 | memcpy(delegation->stateid.data, res->delegation.data, |
| 138 | sizeof(delegation->stateid.data)); |
| 139 | delegation->type = res->delegation_type; |
| 140 | delegation->maxsize = res->maxsize; |
Trond Myklebust | 05c88ba | 2007-10-11 15:11:51 -0400 | [diff] [blame] | 141 | oldcred = delegation->cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | delegation->cred = get_rpccred(cred); |
Trond Myklebust | 15c831b | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 143 | clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | NFS_I(inode)->delegation_state = delegation->type; |
| 145 | smp_wmb(); |
Trond Myklebust | 05c88ba | 2007-10-11 15:11:51 -0400 | [diff] [blame] | 146 | put_rpccred(oldcred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 149 | static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync) |
| 150 | { |
| 151 | int res = 0; |
| 152 | |
| 153 | res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync); |
| 154 | nfs_free_delegation(delegation); |
| 155 | return res; |
| 156 | } |
| 157 | |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 158 | static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation) |
| 159 | { |
| 160 | struct inode *inode = NULL; |
| 161 | |
| 162 | spin_lock(&delegation->lock); |
| 163 | if (delegation->inode != NULL) |
| 164 | inode = igrab(delegation->inode); |
| 165 | spin_unlock(&delegation->lock); |
| 166 | return inode; |
| 167 | } |
| 168 | |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 169 | static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid) |
| 170 | { |
| 171 | struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation); |
| 172 | |
| 173 | if (delegation == NULL) |
| 174 | goto nomatch; |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 175 | spin_lock(&delegation->lock); |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 176 | if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data, |
| 177 | sizeof(delegation->stateid.data)) != 0) |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 178 | goto nomatch_unlock; |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 179 | list_del_rcu(&delegation->super_list); |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 180 | delegation->inode = NULL; |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 181 | nfsi->delegation_state = 0; |
| 182 | rcu_assign_pointer(nfsi->delegation, NULL); |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 183 | spin_unlock(&delegation->lock); |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 184 | return delegation; |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 185 | nomatch_unlock: |
| 186 | spin_unlock(&delegation->lock); |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 187 | nomatch: |
| 188 | return NULL; |
| 189 | } |
| 190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | /* |
| 192 | * Set up a delegation on an inode |
| 193 | */ |
| 194 | int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res) |
| 195 | { |
David Howells | 7539bba | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 196 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | struct nfs_inode *nfsi = NFS_I(inode); |
| 198 | struct nfs_delegation *delegation; |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 199 | struct nfs_delegation *freeme = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | int status = 0; |
| 201 | |
Panagiotis Issaris | f52720c | 2006-09-27 01:49:39 -0700 | [diff] [blame] | 202 | delegation = kmalloc(sizeof(*delegation), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | if (delegation == NULL) |
| 204 | return -ENOMEM; |
| 205 | memcpy(delegation->stateid.data, res->delegation.data, |
| 206 | sizeof(delegation->stateid.data)); |
| 207 | delegation->type = res->delegation_type; |
| 208 | delegation->maxsize = res->maxsize; |
Trond Myklebust | beb2a5e | 2006-01-03 09:55:37 +0100 | [diff] [blame] | 209 | delegation->change_attr = nfsi->change_attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | delegation->cred = get_rpccred(cred); |
| 211 | delegation->inode = inode; |
Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 212 | delegation->flags = 1<<NFS_DELEGATION_REFERENCED; |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 213 | spin_lock_init(&delegation->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
| 215 | spin_lock(&clp->cl_lock); |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 216 | if (rcu_dereference(nfsi->delegation) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | if (memcmp(&delegation->stateid, &nfsi->delegation->stateid, |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 218 | sizeof(delegation->stateid)) == 0 && |
| 219 | delegation->type == nfsi->delegation->type) { |
| 220 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 222 | /* |
| 223 | * Deal with broken servers that hand out two |
| 224 | * delegations for the same file. |
| 225 | */ |
| 226 | dfprintk(FILE, "%s: server %s handed out " |
| 227 | "a duplicate delegation!\n", |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 228 | __func__, clp->cl_hostname); |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 229 | if (delegation->type <= nfsi->delegation->type) { |
| 230 | freeme = delegation; |
| 231 | delegation = NULL; |
| 232 | goto out; |
| 233 | } |
| 234 | freeme = nfs_detach_delegation_locked(nfsi, NULL); |
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 | list_add_rcu(&delegation->super_list, &clp->cl_delegations); |
| 237 | nfsi->delegation_state = delegation->type; |
| 238 | rcu_assign_pointer(nfsi->delegation, delegation); |
| 239 | delegation = NULL; |
Trond Myklebust | 412c77c | 2007-07-03 16:10:55 -0400 | [diff] [blame] | 240 | |
| 241 | /* Ensure we revalidate the attributes and page cache! */ |
| 242 | spin_lock(&inode->i_lock); |
| 243 | nfsi->cache_validity |= NFS_INO_REVAL_FORCED; |
| 244 | spin_unlock(&inode->i_lock); |
| 245 | |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 246 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 603c83d | 2007-10-18 19:59:20 -0400 | [diff] [blame] | 248 | if (delegation != NULL) |
| 249 | nfs_free_delegation(delegation); |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 250 | if (freeme != NULL) |
| 251 | nfs_do_return_delegation(inode, freeme, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | return status; |
| 253 | } |
| 254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | /* Sync all data to disk upon delegation return */ |
| 256 | static void nfs_msync_inode(struct inode *inode) |
| 257 | { |
| 258 | filemap_fdatawrite(inode->i_mapping); |
| 259 | nfs_wb_all(inode); |
| 260 | filemap_fdatawait(inode->i_mapping); |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * Basic procedure for returning a delegation to the server |
| 265 | */ |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 266 | 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] | 267 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | struct nfs_inode *nfsi = NFS_I(inode); |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 269 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Trond Myklebust | 3f09df7 | 2009-06-17 13:23:00 -0700 | [diff] [blame] | 271 | /* |
| 272 | * Guard against new delegated open/lock/unlock calls and against |
| 273 | * state recovery |
| 274 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | down_write(&nfsi->rwsem); |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 276 | err = nfs_delegation_claim_opens(inode, &delegation->stateid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | up_write(&nfsi->rwsem); |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 278 | if (err) |
| 279 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 281 | err = nfs_do_return_delegation(inode, delegation, issync); |
| 282 | out: |
| 283 | return err; |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 284 | } |
| 285 | |
Trond Myklebust | e6f8107 | 2008-01-24 18:14:34 -0500 | [diff] [blame] | 286 | /* |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 287 | * Return all delegations that have been marked for return |
| 288 | */ |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 289 | int nfs_client_return_marked_delegations(struct nfs_client *clp) |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 290 | { |
| 291 | struct nfs_delegation *delegation; |
| 292 | struct inode *inode; |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 293 | int err = 0; |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 294 | |
| 295 | restart: |
| 296 | rcu_read_lock(); |
| 297 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { |
| 298 | if (!test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags)) |
| 299 | continue; |
| 300 | inode = nfs_delegation_grab_inode(delegation); |
| 301 | if (inode == NULL) |
| 302 | continue; |
| 303 | spin_lock(&clp->cl_lock); |
| 304 | delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL); |
| 305 | spin_unlock(&clp->cl_lock); |
| 306 | rcu_read_unlock(); |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 307 | if (delegation != NULL) { |
| 308 | filemap_flush(inode->i_mapping); |
| 309 | err = __nfs_inode_return_delegation(inode, delegation, 0); |
| 310 | } |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 311 | iput(inode); |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 312 | if (!err) |
| 313 | goto restart; |
| 314 | set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state); |
| 315 | return err; |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 316 | } |
| 317 | rcu_read_unlock(); |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 318 | return 0; |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /* |
Trond Myklebust | e6f8107 | 2008-01-24 18:14:34 -0500 | [diff] [blame] | 322 | * This function returns the delegation without reclaiming opens |
| 323 | * or protecting against delegation reclaims. |
| 324 | * It is therefore really only safe to be called from |
| 325 | * nfs4_clear_inode() |
| 326 | */ |
| 327 | void nfs_inode_return_delegation_noreclaim(struct inode *inode) |
| 328 | { |
| 329 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
| 330 | struct nfs_inode *nfsi = NFS_I(inode); |
| 331 | struct nfs_delegation *delegation; |
| 332 | |
| 333 | if (rcu_dereference(nfsi->delegation) != NULL) { |
| 334 | spin_lock(&clp->cl_lock); |
| 335 | delegation = nfs_detach_delegation_locked(nfsi, NULL); |
| 336 | spin_unlock(&clp->cl_lock); |
| 337 | if (delegation != NULL) |
| 338 | nfs_do_return_delegation(inode, delegation, 0); |
| 339 | } |
| 340 | } |
| 341 | |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 342 | int nfs_inode_return_delegation(struct inode *inode) |
| 343 | { |
| 344 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
| 345 | struct nfs_inode *nfsi = NFS_I(inode); |
| 346 | struct nfs_delegation *delegation; |
| 347 | int err = 0; |
| 348 | |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 349 | if (rcu_dereference(nfsi->delegation) != NULL) { |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 350 | spin_lock(&clp->cl_lock); |
| 351 | delegation = nfs_detach_delegation_locked(nfsi, NULL); |
| 352 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 353 | if (delegation != NULL) { |
| 354 | nfs_msync_inode(inode); |
| 355 | err = __nfs_inode_return_delegation(inode, delegation, 1); |
| 356 | } |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 357 | } |
| 358 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Trond Myklebust | 6411bd4 | 2008-12-23 15:21:51 -0500 | [diff] [blame] | 361 | static void nfs_mark_return_delegation(struct nfs_client *clp, struct nfs_delegation *delegation) |
| 362 | { |
| 363 | set_bit(NFS_DELEGATION_RETURN, &delegation->flags); |
| 364 | set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state); |
| 365 | } |
| 366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | /* |
| 368 | * Return all delegations associated to a super block |
| 369 | */ |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 370 | void nfs_super_return_all_delegations(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | { |
David Howells | 7539bba | 2006-08-22 20:06:09 -0400 | [diff] [blame] | 372 | struct nfs_client *clp = NFS_SB(sb)->nfs_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | struct nfs_delegation *delegation; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
| 375 | if (clp == NULL) |
| 376 | return; |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 377 | rcu_read_lock(); |
| 378 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 379 | spin_lock(&delegation->lock); |
| 380 | if (delegation->inode != NULL && delegation->inode->i_sb == sb) |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 381 | set_bit(NFS_DELEGATION_RETURN, &delegation->flags); |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 382 | spin_unlock(&delegation->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | } |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 384 | rcu_read_unlock(); |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 385 | if (nfs_client_return_marked_delegations(clp) != 0) |
| 386 | nfs4_schedule_state_manager(clp); |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 387 | } |
| 388 | |
Alexandros Batsakis | c79571a | 2009-12-05 13:20:52 -0500 | [diff] [blame] | 389 | static |
| 390 | 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] | 391 | { |
| 392 | struct nfs_delegation *delegation; |
| 393 | |
| 394 | rcu_read_lock(); |
Trond Myklebust | 707fb4b | 2008-12-23 15:21:47 -0500 | [diff] [blame] | 395 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { |
Alexandros Batsakis | c79571a | 2009-12-05 13:20:52 -0500 | [diff] [blame] | 396 | if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE)) |
| 397 | continue; |
| 398 | if (delegation->type & flags) |
| 399 | nfs_mark_return_delegation(clp, delegation); |
Trond Myklebust | 707fb4b | 2008-12-23 15:21:47 -0500 | [diff] [blame] | 400 | } |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 401 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Alexandros Batsakis | c79571a | 2009-12-05 13:20:52 -0500 | [diff] [blame] | 404 | static void nfs_client_mark_return_all_delegations(struct nfs_client *clp) |
| 405 | { |
| 406 | nfs_client_mark_return_all_delegation_types(clp, FMODE_READ|FMODE_WRITE); |
| 407 | } |
| 408 | |
Trond Myklebust | b0d3ded | 2008-12-23 15:21:50 -0500 | [diff] [blame] | 409 | static void nfs_delegation_run_state_manager(struct nfs_client *clp) |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 410 | { |
Trond Myklebust | b0d3ded | 2008-12-23 15:21:50 -0500 | [diff] [blame] | 411 | if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) |
| 412 | nfs4_schedule_state_manager(clp); |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 413 | } |
| 414 | |
Alexandros Batsakis | 31f0960 | 2009-12-05 13:27:02 -0500 | [diff] [blame] | 415 | 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] | 416 | { |
| 417 | nfs_client_mark_return_all_delegation_types(clp, flags); |
| 418 | nfs_delegation_run_state_manager(clp); |
| 419 | } |
| 420 | |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 421 | void nfs_expire_all_delegations(struct nfs_client *clp) |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 422 | { |
Alexandros Batsakis | c79571a | 2009-12-05 13:20:52 -0500 | [diff] [blame] | 423 | nfs_expire_all_delegation_types(clp, FMODE_READ|FMODE_WRITE); |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 424 | } |
| 425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | /* |
| 427 | * Return all delegations following an NFS4ERR_CB_PATH_DOWN error. |
| 428 | */ |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 429 | void nfs_handle_cb_pathdown(struct nfs_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | if (clp == NULL) |
| 432 | return; |
Trond Myklebust | 707fb4b | 2008-12-23 15:21:47 -0500 | [diff] [blame] | 433 | nfs_client_mark_return_all_delegations(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 436 | static void nfs_client_mark_return_unreferenced_delegations(struct nfs_client *clp) |
| 437 | { |
| 438 | struct nfs_delegation *delegation; |
| 439 | |
| 440 | rcu_read_lock(); |
| 441 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { |
| 442 | if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags)) |
| 443 | continue; |
Alexandros Batsakis | b4a6f49 | 2009-12-05 13:19:11 -0500 | [diff] [blame] | 444 | nfs_mark_return_delegation(clp, delegation); |
Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 445 | } |
| 446 | rcu_read_unlock(); |
| 447 | } |
| 448 | |
| 449 | void nfs_expire_unreferenced_delegations(struct nfs_client *clp) |
| 450 | { |
| 451 | nfs_client_mark_return_unreferenced_delegations(clp); |
| 452 | nfs_delegation_run_state_manager(clp); |
| 453 | } |
| 454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | /* |
| 456 | * Asynchronous delegation recall! |
| 457 | */ |
Alexandros Batsakis | 2597641 | 2009-12-05 13:48:55 -0500 | [diff] [blame] | 458 | int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid, |
| 459 | int (*validate_stateid)(struct nfs_delegation *delegation, |
| 460 | const nfs4_stateid *stateid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | { |
Trond Myklebust | 6411bd4 | 2008-12-23 15:21:51 -0500 | [diff] [blame] | 462 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
| 463 | struct nfs_delegation *delegation; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
Trond Myklebust | 6411bd4 | 2008-12-23 15:21:51 -0500 | [diff] [blame] | 465 | rcu_read_lock(); |
| 466 | delegation = rcu_dereference(NFS_I(inode)->delegation); |
Alexandros Batsakis | 2597641 | 2009-12-05 13:48:55 -0500 | [diff] [blame] | 467 | |
| 468 | if (!validate_stateid(delegation, stateid)) { |
Trond Myklebust | 6411bd4 | 2008-12-23 15:21:51 -0500 | [diff] [blame] | 469 | rcu_read_unlock(); |
| 470 | return -ENOENT; |
| 471 | } |
Alexandros Batsakis | 2597641 | 2009-12-05 13:48:55 -0500 | [diff] [blame] | 472 | |
Trond Myklebust | 6411bd4 | 2008-12-23 15:21:51 -0500 | [diff] [blame] | 473 | nfs_mark_return_delegation(clp, delegation); |
| 474 | rcu_read_unlock(); |
| 475 | nfs_delegation_run_state_manager(clp); |
| 476 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /* |
| 480 | * Retrieve the inode associated with a delegation |
| 481 | */ |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 482 | 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] | 483 | { |
| 484 | struct nfs_delegation *delegation; |
| 485 | struct inode *res = NULL; |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 486 | rcu_read_lock(); |
| 487 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 488 | spin_lock(&delegation->lock); |
| 489 | if (delegation->inode != NULL && |
| 490 | nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | res = igrab(delegation->inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | } |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 493 | spin_unlock(&delegation->lock); |
| 494 | if (res != NULL) |
| 495 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | } |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 497 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | return res; |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | * Mark all delegations as needing to be reclaimed |
| 503 | */ |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 504 | void nfs_delegation_mark_reclaim(struct nfs_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | { |
| 506 | struct nfs_delegation *delegation; |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 507 | rcu_read_lock(); |
| 508 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) |
Trond Myklebust | 15c831b | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 509 | set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags); |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 510 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /* |
| 514 | * Reap all unclaimed delegations after reboot recovery is done |
| 515 | */ |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 516 | void nfs_delegation_reap_unclaimed(struct nfs_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | { |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 518 | struct nfs_delegation *delegation; |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 519 | struct inode *inode; |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 520 | restart: |
| 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 | if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | continue; |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 525 | inode = nfs_delegation_grab_inode(delegation); |
| 526 | if (inode == NULL) |
| 527 | continue; |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 528 | spin_lock(&clp->cl_lock); |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 529 | delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL); |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 530 | spin_unlock(&clp->cl_lock); |
| 531 | rcu_read_unlock(); |
| 532 | if (delegation != NULL) |
Trond Myklebust | 905f8d1 | 2007-08-06 12:18:34 -0400 | [diff] [blame] | 533 | nfs_free_delegation(delegation); |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 534 | iput(inode); |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 535 | goto restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | } |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 537 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | } |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 539 | |
| 540 | int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode) |
| 541 | { |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 542 | struct nfs_inode *nfsi = NFS_I(inode); |
| 543 | struct nfs_delegation *delegation; |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 544 | int ret = 0; |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 545 | |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 546 | rcu_read_lock(); |
| 547 | delegation = rcu_dereference(nfsi->delegation); |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 548 | if (delegation != NULL) { |
| 549 | memcpy(dst->data, delegation->stateid.data, sizeof(dst->data)); |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 550 | ret = 1; |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 551 | } |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 552 | rcu_read_unlock(); |
| 553 | return ret; |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 554 | } |