Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/nfs/callback_proc.c |
| 3 | * |
| 4 | * Copyright (C) 2004 Trond Myklebust |
| 5 | * |
| 6 | * NFSv4 callback procedures |
| 7 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/nfs4.h> |
| 9 | #include <linux/nfs_fs.h> |
Trond Myklebust | 4ce7971 | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 10 | #include "nfs4_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include "callback.h" |
| 12 | #include "delegation.h" |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 13 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
Chuck Lever | 1d98fe6 | 2007-12-10 14:57:23 -0500 | [diff] [blame] | 15 | #ifdef NFS_DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #define NFSDBG_FACILITY NFSDBG_CALLBACK |
Chuck Lever | 1d98fe6 | 2007-12-10 14:57:23 -0500 | [diff] [blame] | 17 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 19 | __be32 nfs4_callback_getattr(struct cb_getattrargs *args, struct cb_getattrres *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | { |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 21 | struct nfs_client *clp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | struct nfs_delegation *delegation; |
| 23 | struct nfs_inode *nfsi; |
| 24 | struct inode *inode; |
Chuck Lever | 1d98fe6 | 2007-12-10 14:57:23 -0500 | [diff] [blame] | 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | res->bitmap[0] = res->bitmap[1] = 0; |
| 27 | res->status = htonl(NFS4ERR_BADHANDLE); |
Chuck Lever | ff05264 | 2007-12-10 14:58:44 -0500 | [diff] [blame] | 28 | clp = nfs_find_client(args->addr, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | if (clp == NULL) |
| 30 | goto out; |
Chuck Lever | 1d98fe6 | 2007-12-10 14:57:23 -0500 | [diff] [blame] | 31 | |
| 32 | dprintk("NFS: GETATTR callback request from %s\n", |
| 33 | rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)); |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | inode = nfs_delegation_find_inode(clp, &args->fh); |
| 36 | if (inode == NULL) |
| 37 | goto out_putclient; |
| 38 | nfsi = NFS_I(inode); |
| 39 | down_read(&nfsi->rwsem); |
| 40 | delegation = nfsi->delegation; |
| 41 | if (delegation == NULL || (delegation->type & FMODE_WRITE) == 0) |
| 42 | goto out_iput; |
| 43 | res->size = i_size_read(inode); |
Trond Myklebust | beb2a5e | 2006-01-03 09:55:37 +0100 | [diff] [blame] | 44 | res->change_attr = delegation->change_attr; |
| 45 | if (nfsi->npages != 0) |
| 46 | res->change_attr++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | res->ctime = inode->i_ctime; |
| 48 | res->mtime = inode->i_mtime; |
| 49 | res->bitmap[0] = (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE) & |
| 50 | args->bitmap[0]; |
| 51 | res->bitmap[1] = (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY) & |
| 52 | args->bitmap[1]; |
| 53 | res->status = 0; |
| 54 | out_iput: |
| 55 | up_read(&nfsi->rwsem); |
| 56 | iput(inode); |
| 57 | out_putclient: |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 58 | nfs_put_client(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | out: |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 60 | dprintk("%s: exit with status = %d\n", __func__, ntohl(res->status)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | return res->status; |
| 62 | } |
| 63 | |
Alexandros Batsakis | 2597641 | 2009-12-05 13:48:55 -0500 | [diff] [blame] | 64 | static int (*nfs_validate_delegation_stateid(struct nfs_client *clp))(struct nfs_delegation *, const nfs4_stateid *) |
| 65 | { |
| 66 | #if defined(CONFIG_NFS_V4_1) |
| 67 | if (clp->cl_minorversion > 0) |
| 68 | return nfs41_validate_delegation_stateid; |
| 69 | #endif |
| 70 | return nfs4_validate_delegation_stateid; |
| 71 | } |
| 72 | |
| 73 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 74 | __be32 nfs4_callback_recall(struct cb_recallargs *args, void *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 76 | struct nfs_client *clp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | struct inode *inode; |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 78 | __be32 res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | res = htonl(NFS4ERR_BADHANDLE); |
Chuck Lever | ff05264 | 2007-12-10 14:58:44 -0500 | [diff] [blame] | 81 | clp = nfs_find_client(args->addr, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | if (clp == NULL) |
| 83 | goto out; |
Chuck Lever | 1d98fe6 | 2007-12-10 14:57:23 -0500 | [diff] [blame] | 84 | |
| 85 | dprintk("NFS: RECALL callback request from %s\n", |
| 86 | rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)); |
| 87 | |
Trond Myklebust | 3fbd67a | 2008-01-26 01:06:40 -0500 | [diff] [blame] | 88 | do { |
| 89 | struct nfs_client *prev = clp; |
| 90 | |
| 91 | inode = nfs_delegation_find_inode(clp, &args->fh); |
| 92 | if (inode != NULL) { |
| 93 | /* Set up a helper thread to actually return the delegation */ |
Alexandros Batsakis | 2597641 | 2009-12-05 13:48:55 -0500 | [diff] [blame] | 94 | switch (nfs_async_inode_return_delegation(inode, &args->stateid, |
| 95 | nfs_validate_delegation_stateid(clp))) { |
Trond Myklebust | 3fbd67a | 2008-01-26 01:06:40 -0500 | [diff] [blame] | 96 | case 0: |
| 97 | res = 0; |
| 98 | break; |
| 99 | case -ENOENT: |
| 100 | if (res != 0) |
| 101 | res = htonl(NFS4ERR_BAD_STATEID); |
| 102 | break; |
| 103 | default: |
| 104 | res = htonl(NFS4ERR_RESOURCE); |
| 105 | } |
| 106 | iput(inode); |
| 107 | } |
| 108 | clp = nfs_find_client_next(prev); |
| 109 | nfs_put_client(prev); |
| 110 | } while (clp != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | out: |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 112 | dprintk("%s: exit with status = %d\n", __func__, ntohl(res)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | return res; |
| 114 | } |
Benny Halevy | d49433e | 2009-04-01 09:23:25 -0400 | [diff] [blame] | 115 | |
Alexandros Batsakis | 2597641 | 2009-12-05 13:48:55 -0500 | [diff] [blame] | 116 | int nfs4_validate_delegation_stateid(struct nfs_delegation *delegation, const nfs4_stateid *stateid) |
| 117 | { |
| 118 | if (delegation == NULL || memcmp(delegation->stateid.data, stateid->data, |
| 119 | sizeof(delegation->stateid.data)) != 0) |
| 120 | return 0; |
| 121 | return 1; |
| 122 | } |
| 123 | |
Benny Halevy | d49433e | 2009-04-01 09:23:25 -0400 | [diff] [blame] | 124 | #if defined(CONFIG_NFS_V4_1) |
| 125 | |
Alexandros Batsakis | 2597641 | 2009-12-05 13:48:55 -0500 | [diff] [blame] | 126 | int nfs41_validate_delegation_stateid(struct nfs_delegation *delegation, const nfs4_stateid *stateid) |
| 127 | { |
| 128 | if (delegation == NULL) |
| 129 | return 0; |
| 130 | |
| 131 | /* seqid is 4-bytes long */ |
| 132 | if (((u32 *) &stateid->data)[0] != 0) |
| 133 | return 0; |
| 134 | if (memcmp(&delegation->stateid.data[4], &stateid->data[4], |
| 135 | sizeof(stateid->data)-4)) |
| 136 | return 0; |
| 137 | |
| 138 | return 1; |
| 139 | } |
| 140 | |
Ricardo Labiaga | 963891a | 2009-04-01 09:23:34 -0400 | [diff] [blame] | 141 | /* |
Ricardo Labiaga | 68f3f90 | 2009-04-01 09:23:35 -0400 | [diff] [blame] | 142 | * Validate the sequenceID sent by the server. |
| 143 | * Return success if the sequenceID is one more than what we last saw on |
| 144 | * this slot, accounting for wraparound. Increments the slot's sequence. |
| 145 | * |
| 146 | * We don't yet implement a duplicate request cache, so at this time |
| 147 | * we will log replays, and process them as if we had not seen them before, |
| 148 | * but we don't bump the sequence in the slot. Not too worried about it, |
| 149 | * since we only currently implement idempotent callbacks anyway. |
| 150 | * |
| 151 | * We have a single slot backchannel at this time, so we don't bother |
| 152 | * checking the used_slots bit array on the table. The lower layer guarantees |
| 153 | * a single outstanding callback request at a time. |
| 154 | */ |
| 155 | static int |
| 156 | validate_seqid(struct nfs4_slot_table *tbl, u32 slotid, u32 seqid) |
| 157 | { |
| 158 | struct nfs4_slot *slot; |
| 159 | |
| 160 | dprintk("%s enter. slotid %d seqid %d\n", |
| 161 | __func__, slotid, seqid); |
| 162 | |
| 163 | if (slotid > NFS41_BC_MAX_CALLBACKS) |
| 164 | return htonl(NFS4ERR_BADSLOT); |
| 165 | |
| 166 | slot = tbl->slots + slotid; |
| 167 | dprintk("%s slot table seqid: %d\n", __func__, slot->seq_nr); |
| 168 | |
| 169 | /* Normal */ |
| 170 | if (likely(seqid == slot->seq_nr + 1)) { |
| 171 | slot->seq_nr++; |
| 172 | return htonl(NFS4_OK); |
| 173 | } |
| 174 | |
| 175 | /* Replay */ |
| 176 | if (seqid == slot->seq_nr) { |
| 177 | dprintk("%s seqid %d is a replay - no DRC available\n", |
| 178 | __func__, seqid); |
| 179 | return htonl(NFS4_OK); |
| 180 | } |
| 181 | |
| 182 | /* Wraparound */ |
| 183 | if (seqid == 1 && (slot->seq_nr + 1) == 0) { |
| 184 | slot->seq_nr = 1; |
| 185 | return htonl(NFS4_OK); |
| 186 | } |
| 187 | |
| 188 | /* Misordered request */ |
| 189 | return htonl(NFS4ERR_SEQ_MISORDERED); |
| 190 | } |
| 191 | |
| 192 | /* |
Ricardo Labiaga | 963891a | 2009-04-01 09:23:34 -0400 | [diff] [blame] | 193 | * Returns a pointer to a held 'struct nfs_client' that matches the server's |
| 194 | * address, major version number, and session ID. It is the caller's |
| 195 | * responsibility to release the returned reference. |
| 196 | * |
| 197 | * Returns NULL if there are no connections with sessions, or if no session |
| 198 | * matches the one of interest. |
| 199 | */ |
| 200 | static struct nfs_client *find_client_with_session( |
| 201 | const struct sockaddr *addr, u32 nfsversion, |
| 202 | struct nfs4_sessionid *sessionid) |
| 203 | { |
| 204 | struct nfs_client *clp; |
| 205 | |
| 206 | clp = nfs_find_client(addr, 4); |
| 207 | if (clp == NULL) |
| 208 | return NULL; |
| 209 | |
| 210 | do { |
| 211 | struct nfs_client *prev = clp; |
| 212 | |
| 213 | if (clp->cl_session != NULL) { |
| 214 | if (memcmp(clp->cl_session->sess_id.data, |
| 215 | sessionid->data, |
| 216 | NFS4_MAX_SESSIONID_LEN) == 0) { |
| 217 | /* Returns a held reference to clp */ |
| 218 | return clp; |
| 219 | } |
| 220 | } |
| 221 | clp = nfs_find_client_next(prev); |
| 222 | nfs_put_client(prev); |
| 223 | } while (clp != NULL); |
| 224 | |
| 225 | return NULL; |
| 226 | } |
| 227 | |
Mike Sager | a7989c3 | 2010-01-19 12:54:40 -0500 | [diff] [blame^] | 228 | /* |
| 229 | * For each referring call triple, check the session's slot table for |
| 230 | * a match. If the slot is in use and the sequence numbers match, the |
| 231 | * client is still waiting for a response to the original request. |
| 232 | */ |
| 233 | static bool referring_call_exists(struct nfs_client *clp, |
| 234 | uint32_t nrclists, |
| 235 | struct referring_call_list *rclists) |
| 236 | { |
| 237 | bool status = 0; |
| 238 | int i, j; |
| 239 | struct nfs4_session *session; |
| 240 | struct nfs4_slot_table *tbl; |
| 241 | struct referring_call_list *rclist; |
| 242 | struct referring_call *ref; |
| 243 | |
| 244 | /* |
| 245 | * XXX When client trunking is implemented, this becomes |
| 246 | * a session lookup from within the loop |
| 247 | */ |
| 248 | session = clp->cl_session; |
| 249 | tbl = &session->fc_slot_table; |
| 250 | |
| 251 | for (i = 0; i < nrclists; i++) { |
| 252 | rclist = &rclists[i]; |
| 253 | if (memcmp(session->sess_id.data, |
| 254 | rclist->rcl_sessionid.data, |
| 255 | NFS4_MAX_SESSIONID_LEN) != 0) |
| 256 | continue; |
| 257 | |
| 258 | for (j = 0; j < rclist->rcl_nrefcalls; j++) { |
| 259 | ref = &rclist->rcl_refcalls[j]; |
| 260 | |
| 261 | dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u " |
| 262 | "slotid %u\n", __func__, |
| 263 | ((u32 *)&rclist->rcl_sessionid.data)[0], |
| 264 | ((u32 *)&rclist->rcl_sessionid.data)[1], |
| 265 | ((u32 *)&rclist->rcl_sessionid.data)[2], |
| 266 | ((u32 *)&rclist->rcl_sessionid.data)[3], |
| 267 | ref->rc_sequenceid, ref->rc_slotid); |
| 268 | |
| 269 | spin_lock(&tbl->slot_tbl_lock); |
| 270 | status = (test_bit(ref->rc_slotid, tbl->used_slots) && |
| 271 | tbl->slots[ref->rc_slotid].seq_nr == |
| 272 | ref->rc_sequenceid); |
| 273 | spin_unlock(&tbl->slot_tbl_lock); |
| 274 | if (status) |
| 275 | goto out; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | out: |
| 280 | return status; |
| 281 | } |
| 282 | |
Benny Halevy | d49433e | 2009-04-01 09:23:25 -0400 | [diff] [blame] | 283 | /* FIXME: referring calls should be processed */ |
| 284 | unsigned nfs4_callback_sequence(struct cb_sequenceargs *args, |
| 285 | struct cb_sequenceres *res) |
| 286 | { |
Ricardo Labiaga | 68f3f90 | 2009-04-01 09:23:35 -0400 | [diff] [blame] | 287 | struct nfs_client *clp; |
| 288 | int i, status; |
Benny Halevy | d49433e | 2009-04-01 09:23:25 -0400 | [diff] [blame] | 289 | |
| 290 | for (i = 0; i < args->csa_nrclists; i++) |
| 291 | kfree(args->csa_rclists[i].rcl_refcalls); |
| 292 | kfree(args->csa_rclists); |
| 293 | |
Ricardo Labiaga | 68f3f90 | 2009-04-01 09:23:35 -0400 | [diff] [blame] | 294 | status = htonl(NFS4ERR_BADSESSION); |
| 295 | clp = find_client_with_session(args->csa_addr, 4, &args->csa_sessionid); |
| 296 | if (clp == NULL) |
| 297 | goto out; |
| 298 | |
| 299 | status = validate_seqid(&clp->cl_session->bc_slot_table, |
| 300 | args->csa_slotid, args->csa_sequenceid); |
| 301 | if (status) |
| 302 | goto out_putclient; |
| 303 | |
Benny Halevy | d49433e | 2009-04-01 09:23:25 -0400 | [diff] [blame] | 304 | memcpy(&res->csr_sessionid, &args->csa_sessionid, |
| 305 | sizeof(res->csr_sessionid)); |
| 306 | res->csr_sequenceid = args->csa_sequenceid; |
| 307 | res->csr_slotid = args->csa_slotid; |
| 308 | res->csr_highestslotid = NFS41_BC_MAX_CALLBACKS - 1; |
| 309 | res->csr_target_highestslotid = NFS41_BC_MAX_CALLBACKS - 1; |
| 310 | |
Ricardo Labiaga | 68f3f90 | 2009-04-01 09:23:35 -0400 | [diff] [blame] | 311 | out_putclient: |
| 312 | nfs_put_client(clp); |
| 313 | out: |
Benny Halevy | d49433e | 2009-04-01 09:23:25 -0400 | [diff] [blame] | 314 | dprintk("%s: exit with status = %d\n", __func__, ntohl(status)); |
| 315 | res->csr_status = status; |
Ricardo Labiaga | 68f3f90 | 2009-04-01 09:23:35 -0400 | [diff] [blame] | 316 | return res->csr_status; |
Benny Halevy | d49433e | 2009-04-01 09:23:25 -0400 | [diff] [blame] | 317 | } |
| 318 | |
Alexandros Batsakis | 31f0960 | 2009-12-05 13:27:02 -0500 | [diff] [blame] | 319 | unsigned nfs4_callback_recallany(struct cb_recallanyargs *args, void *dummy) |
| 320 | { |
| 321 | struct nfs_client *clp; |
| 322 | int status; |
| 323 | fmode_t flags = 0; |
| 324 | |
| 325 | status = htonl(NFS4ERR_OP_NOT_IN_SESSION); |
| 326 | clp = nfs_find_client(args->craa_addr, 4); |
| 327 | if (clp == NULL) |
| 328 | goto out; |
| 329 | |
| 330 | dprintk("NFS: RECALL_ANY callback request from %s\n", |
| 331 | rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)); |
| 332 | |
| 333 | if (test_bit(RCA4_TYPE_MASK_RDATA_DLG, (const unsigned long *) |
| 334 | &args->craa_type_mask)) |
| 335 | flags = FMODE_READ; |
| 336 | if (test_bit(RCA4_TYPE_MASK_WDATA_DLG, (const unsigned long *) |
| 337 | &args->craa_type_mask)) |
| 338 | flags |= FMODE_WRITE; |
| 339 | |
| 340 | if (flags) |
| 341 | nfs_expire_all_delegation_types(clp, flags); |
| 342 | status = htonl(NFS4_OK); |
| 343 | out: |
| 344 | dprintk("%s: exit with status = %d\n", __func__, ntohl(status)); |
| 345 | return status; |
| 346 | } |
Benny Halevy | d49433e | 2009-04-01 09:23:25 -0400 | [diff] [blame] | 347 | #endif /* CONFIG_NFS_V4_1 */ |