| 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 |  | 
| Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 64 | __be32 nfs4_callback_recall(struct cb_recallargs *args, void *dummy) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | { | 
| David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 66 | struct nfs_client *clp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | struct inode *inode; | 
| Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 68 | __be32 res; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 |  | 
|  | 70 | res = htonl(NFS4ERR_BADHANDLE); | 
| Chuck Lever | ff05264 | 2007-12-10 14:58:44 -0500 | [diff] [blame] | 71 | clp = nfs_find_client(args->addr, 4); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | if (clp == NULL) | 
|  | 73 | goto out; | 
| Chuck Lever | 1d98fe6 | 2007-12-10 14:57:23 -0500 | [diff] [blame] | 74 |  | 
|  | 75 | dprintk("NFS: RECALL callback request from %s\n", | 
|  | 76 | rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)); | 
|  | 77 |  | 
| Trond Myklebust | 3fbd67a | 2008-01-26 01:06:40 -0500 | [diff] [blame] | 78 | do { | 
|  | 79 | struct nfs_client *prev = clp; | 
|  | 80 |  | 
|  | 81 | inode = nfs_delegation_find_inode(clp, &args->fh); | 
|  | 82 | if (inode != NULL) { | 
|  | 83 | /* Set up a helper thread to actually return the delegation */ | 
|  | 84 | switch(nfs_async_inode_return_delegation(inode, &args->stateid)) { | 
|  | 85 | case 0: | 
|  | 86 | res = 0; | 
|  | 87 | break; | 
|  | 88 | case -ENOENT: | 
|  | 89 | if (res != 0) | 
|  | 90 | res = htonl(NFS4ERR_BAD_STATEID); | 
|  | 91 | break; | 
|  | 92 | default: | 
|  | 93 | res = htonl(NFS4ERR_RESOURCE); | 
|  | 94 | } | 
|  | 95 | iput(inode); | 
|  | 96 | } | 
|  | 97 | clp = nfs_find_client_next(prev); | 
|  | 98 | nfs_put_client(prev); | 
|  | 99 | } while (clp != NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | out: | 
| Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 101 | dprintk("%s: exit with status = %d\n", __func__, ntohl(res)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | return res; | 
|  | 103 | } | 
| Benny Halevy | d49433e | 2009-04-01 09:23:25 -0400 | [diff] [blame] | 104 |  | 
|  | 105 | #if defined(CONFIG_NFS_V4_1) | 
|  | 106 |  | 
| Ricardo Labiaga | 963891a | 2009-04-01 09:23:34 -0400 | [diff] [blame] | 107 | /* | 
| Ricardo Labiaga | 68f3f90 | 2009-04-01 09:23:35 -0400 | [diff] [blame] | 108 | * Validate the sequenceID sent by the server. | 
|  | 109 | * Return success if the sequenceID is one more than what we last saw on | 
|  | 110 | * this slot, accounting for wraparound.  Increments the slot's sequence. | 
|  | 111 | * | 
|  | 112 | * We don't yet implement a duplicate request cache, so at this time | 
|  | 113 | * we will log replays, and process them as if we had not seen them before, | 
|  | 114 | * but we don't bump the sequence in the slot.  Not too worried about it, | 
|  | 115 | * since we only currently implement idempotent callbacks anyway. | 
|  | 116 | * | 
|  | 117 | * We have a single slot backchannel at this time, so we don't bother | 
|  | 118 | * checking the used_slots bit array on the table.  The lower layer guarantees | 
|  | 119 | * a single outstanding callback request at a time. | 
|  | 120 | */ | 
|  | 121 | static int | 
|  | 122 | validate_seqid(struct nfs4_slot_table *tbl, u32 slotid, u32 seqid) | 
|  | 123 | { | 
|  | 124 | struct nfs4_slot *slot; | 
|  | 125 |  | 
|  | 126 | dprintk("%s enter. slotid %d seqid %d\n", | 
|  | 127 | __func__, slotid, seqid); | 
|  | 128 |  | 
|  | 129 | if (slotid > NFS41_BC_MAX_CALLBACKS) | 
|  | 130 | return htonl(NFS4ERR_BADSLOT); | 
|  | 131 |  | 
|  | 132 | slot = tbl->slots + slotid; | 
|  | 133 | dprintk("%s slot table seqid: %d\n", __func__, slot->seq_nr); | 
|  | 134 |  | 
|  | 135 | /* Normal */ | 
|  | 136 | if (likely(seqid == slot->seq_nr + 1)) { | 
|  | 137 | slot->seq_nr++; | 
|  | 138 | return htonl(NFS4_OK); | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | /* Replay */ | 
|  | 142 | if (seqid == slot->seq_nr) { | 
|  | 143 | dprintk("%s seqid %d is a replay - no DRC available\n", | 
|  | 144 | __func__, seqid); | 
|  | 145 | return htonl(NFS4_OK); | 
|  | 146 | } | 
|  | 147 |  | 
|  | 148 | /* Wraparound */ | 
|  | 149 | if (seqid == 1 && (slot->seq_nr + 1) == 0) { | 
|  | 150 | slot->seq_nr = 1; | 
|  | 151 | return htonl(NFS4_OK); | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | /* Misordered request */ | 
|  | 155 | return htonl(NFS4ERR_SEQ_MISORDERED); | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | /* | 
| Ricardo Labiaga | 963891a | 2009-04-01 09:23:34 -0400 | [diff] [blame] | 159 | * Returns a pointer to a held 'struct nfs_client' that matches the server's | 
|  | 160 | * address, major version number, and session ID.  It is the caller's | 
|  | 161 | * responsibility to release the returned reference. | 
|  | 162 | * | 
|  | 163 | * Returns NULL if there are no connections with sessions, or if no session | 
|  | 164 | * matches the one of interest. | 
|  | 165 | */ | 
|  | 166 | static struct nfs_client *find_client_with_session( | 
|  | 167 | const struct sockaddr *addr, u32 nfsversion, | 
|  | 168 | struct nfs4_sessionid *sessionid) | 
|  | 169 | { | 
|  | 170 | struct nfs_client *clp; | 
|  | 171 |  | 
|  | 172 | clp = nfs_find_client(addr, 4); | 
|  | 173 | if (clp == NULL) | 
|  | 174 | return NULL; | 
|  | 175 |  | 
|  | 176 | do { | 
|  | 177 | struct nfs_client *prev = clp; | 
|  | 178 |  | 
|  | 179 | if (clp->cl_session != NULL) { | 
|  | 180 | if (memcmp(clp->cl_session->sess_id.data, | 
|  | 181 | sessionid->data, | 
|  | 182 | NFS4_MAX_SESSIONID_LEN) == 0) { | 
|  | 183 | /* Returns a held reference to clp */ | 
|  | 184 | return clp; | 
|  | 185 | } | 
|  | 186 | } | 
|  | 187 | clp = nfs_find_client_next(prev); | 
|  | 188 | nfs_put_client(prev); | 
|  | 189 | } while (clp != NULL); | 
|  | 190 |  | 
|  | 191 | return NULL; | 
|  | 192 | } | 
|  | 193 |  | 
| Benny Halevy | d49433e | 2009-04-01 09:23:25 -0400 | [diff] [blame] | 194 | /* FIXME: referring calls should be processed */ | 
|  | 195 | unsigned nfs4_callback_sequence(struct cb_sequenceargs *args, | 
|  | 196 | struct cb_sequenceres *res) | 
|  | 197 | { | 
| Ricardo Labiaga | 68f3f90 | 2009-04-01 09:23:35 -0400 | [diff] [blame] | 198 | struct nfs_client *clp; | 
|  | 199 | int i, status; | 
| Benny Halevy | d49433e | 2009-04-01 09:23:25 -0400 | [diff] [blame] | 200 |  | 
|  | 201 | for (i = 0; i < args->csa_nrclists; i++) | 
|  | 202 | kfree(args->csa_rclists[i].rcl_refcalls); | 
|  | 203 | kfree(args->csa_rclists); | 
|  | 204 |  | 
| Ricardo Labiaga | 68f3f90 | 2009-04-01 09:23:35 -0400 | [diff] [blame] | 205 | status = htonl(NFS4ERR_BADSESSION); | 
|  | 206 | clp = find_client_with_session(args->csa_addr, 4, &args->csa_sessionid); | 
|  | 207 | if (clp == NULL) | 
|  | 208 | goto out; | 
|  | 209 |  | 
|  | 210 | status = validate_seqid(&clp->cl_session->bc_slot_table, | 
|  | 211 | args->csa_slotid, args->csa_sequenceid); | 
|  | 212 | if (status) | 
|  | 213 | goto out_putclient; | 
|  | 214 |  | 
| Benny Halevy | d49433e | 2009-04-01 09:23:25 -0400 | [diff] [blame] | 215 | memcpy(&res->csr_sessionid, &args->csa_sessionid, | 
|  | 216 | sizeof(res->csr_sessionid)); | 
|  | 217 | res->csr_sequenceid = args->csa_sequenceid; | 
|  | 218 | res->csr_slotid = args->csa_slotid; | 
|  | 219 | res->csr_highestslotid = NFS41_BC_MAX_CALLBACKS - 1; | 
|  | 220 | res->csr_target_highestslotid = NFS41_BC_MAX_CALLBACKS - 1; | 
|  | 221 |  | 
| Ricardo Labiaga | 68f3f90 | 2009-04-01 09:23:35 -0400 | [diff] [blame] | 222 | out_putclient: | 
|  | 223 | nfs_put_client(clp); | 
|  | 224 | out: | 
| Benny Halevy | d49433e | 2009-04-01 09:23:25 -0400 | [diff] [blame] | 225 | dprintk("%s: exit with status = %d\n", __func__, ntohl(status)); | 
|  | 226 | res->csr_status = status; | 
| Ricardo Labiaga | 68f3f90 | 2009-04-01 09:23:35 -0400 | [diff] [blame] | 227 | return res->csr_status; | 
| Benny Halevy | d49433e | 2009-04-01 09:23:25 -0400 | [diff] [blame] | 228 | } | 
|  | 229 |  | 
|  | 230 | #endif /* CONFIG_NFS_V4_1 */ |