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