xref: /linux/fs/nfs/callback_proc.c (revision a33f32244d8550da8b4a26e277ce07d5c6d158b5)
1 /*
2  * linux/fs/nfs/callback_proc.c
3  *
4  * Copyright (C) 2004 Trond Myklebust
5  *
6  * NFSv4 callback procedures
7  */
8 #include <linux/nfs4.h>
9 #include <linux/nfs_fs.h>
10 #include <linux/slab.h>
11 #include "nfs4_fs.h"
12 #include "callback.h"
13 #include "delegation.h"
14 #include "internal.h"
15 
16 #ifdef NFS_DEBUG
17 #define NFSDBG_FACILITY NFSDBG_CALLBACK
18 #endif
19 
20 __be32 nfs4_callback_getattr(struct cb_getattrargs *args, struct cb_getattrres *res)
21 {
22 	struct nfs_client *clp;
23 	struct nfs_delegation *delegation;
24 	struct nfs_inode *nfsi;
25 	struct inode *inode;
26 
27 	res->bitmap[0] = res->bitmap[1] = 0;
28 	res->status = htonl(NFS4ERR_BADHANDLE);
29 	clp = nfs_find_client(args->addr, 4);
30 	if (clp == NULL)
31 		goto out;
32 
33 	dprintk("NFS: GETATTR callback request from %s\n",
34 		rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
35 
36 	inode = nfs_delegation_find_inode(clp, &args->fh);
37 	if (inode == NULL)
38 		goto out_putclient;
39 	nfsi = NFS_I(inode);
40 	down_read(&nfsi->rwsem);
41 	delegation = nfsi->delegation;
42 	if (delegation == NULL || (delegation->type & FMODE_WRITE) == 0)
43 		goto out_iput;
44 	res->size = i_size_read(inode);
45 	res->change_attr = delegation->change_attr;
46 	if (nfsi->npages != 0)
47 		res->change_attr++;
48 	res->ctime = inode->i_ctime;
49 	res->mtime = inode->i_mtime;
50 	res->bitmap[0] = (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE) &
51 		args->bitmap[0];
52 	res->bitmap[1] = (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY) &
53 		args->bitmap[1];
54 	res->status = 0;
55 out_iput:
56 	up_read(&nfsi->rwsem);
57 	iput(inode);
58 out_putclient:
59 	nfs_put_client(clp);
60 out:
61 	dprintk("%s: exit with status = %d\n", __func__, ntohl(res->status));
62 	return res->status;
63 }
64 
65 static int (*nfs_validate_delegation_stateid(struct nfs_client *clp))(struct nfs_delegation *, const nfs4_stateid *)
66 {
67 #if defined(CONFIG_NFS_V4_1)
68 	if (clp->cl_minorversion > 0)
69 		return nfs41_validate_delegation_stateid;
70 #endif
71 	return nfs4_validate_delegation_stateid;
72 }
73 
74 
75 __be32 nfs4_callback_recall(struct cb_recallargs *args, void *dummy)
76 {
77 	struct nfs_client *clp;
78 	struct inode *inode;
79 	__be32 res;
80 
81 	res = htonl(NFS4ERR_BADHANDLE);
82 	clp = nfs_find_client(args->addr, 4);
83 	if (clp == NULL)
84 		goto out;
85 
86 	dprintk("NFS: RECALL callback request from %s\n",
87 		rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
88 
89 	do {
90 		struct nfs_client *prev = clp;
91 
92 		inode = nfs_delegation_find_inode(clp, &args->fh);
93 		if (inode != NULL) {
94 			/* Set up a helper thread to actually return the delegation */
95 			switch (nfs_async_inode_return_delegation(inode, &args->stateid,
96 								  nfs_validate_delegation_stateid(clp))) {
97 				case 0:
98 					res = 0;
99 					break;
100 				case -ENOENT:
101 					if (res != 0)
102 						res = htonl(NFS4ERR_BAD_STATEID);
103 					break;
104 				default:
105 					res = htonl(NFS4ERR_RESOURCE);
106 			}
107 			iput(inode);
108 		}
109 		clp = nfs_find_client_next(prev);
110 		nfs_put_client(prev);
111 	} while (clp != NULL);
112 out:
113 	dprintk("%s: exit with status = %d\n", __func__, ntohl(res));
114 	return res;
115 }
116 
117 int nfs4_validate_delegation_stateid(struct nfs_delegation *delegation, const nfs4_stateid *stateid)
118 {
119 	if (delegation == NULL || memcmp(delegation->stateid.data, stateid->data,
120 					 sizeof(delegation->stateid.data)) != 0)
121 		return 0;
122 	return 1;
123 }
124 
125 #if defined(CONFIG_NFS_V4_1)
126 
127 int nfs41_validate_delegation_stateid(struct nfs_delegation *delegation, const nfs4_stateid *stateid)
128 {
129 	if (delegation == NULL)
130 		return 0;
131 
132 	/* seqid is 4-bytes long */
133 	if (((u32 *) &stateid->data)[0] != 0)
134 		return 0;
135 	if (memcmp(&delegation->stateid.data[4], &stateid->data[4],
136 		   sizeof(stateid->data)-4))
137 		return 0;
138 
139 	return 1;
140 }
141 
142 /*
143  * Validate the sequenceID sent by the server.
144  * Return success if the sequenceID is one more than what we last saw on
145  * this slot, accounting for wraparound.  Increments the slot's sequence.
146  *
147  * We don't yet implement a duplicate request cache, instead we set the
148  * back channel ca_maxresponsesize_cached to zero. This is OK for now
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 __be32
156 validate_seqid(struct nfs4_slot_table *tbl, struct cb_sequenceargs * args)
157 {
158 	struct nfs4_slot *slot;
159 
160 	dprintk("%s enter. slotid %d seqid %d\n",
161 		__func__, args->csa_slotid, args->csa_sequenceid);
162 
163 	if (args->csa_slotid > NFS41_BC_MAX_CALLBACKS)
164 		return htonl(NFS4ERR_BADSLOT);
165 
166 	slot = tbl->slots + args->csa_slotid;
167 	dprintk("%s slot table seqid: %d\n", __func__, slot->seq_nr);
168 
169 	/* Normal */
170 	if (likely(args->csa_sequenceid == slot->seq_nr + 1)) {
171 		slot->seq_nr++;
172 		return htonl(NFS4_OK);
173 	}
174 
175 	/* Replay */
176 	if (args->csa_sequenceid == slot->seq_nr) {
177 		dprintk("%s seqid %d is a replay\n",
178 			__func__, args->csa_sequenceid);
179 		/* Signal process_op to set this error on next op */
180 		if (args->csa_cachethis == 0)
181 			return htonl(NFS4ERR_RETRY_UNCACHED_REP);
182 
183 		/* The ca_maxresponsesize_cached is 0 with no DRC */
184 		else if (args->csa_cachethis == 1)
185 			return htonl(NFS4ERR_REP_TOO_BIG_TO_CACHE);
186 	}
187 
188 	/* Wraparound */
189 	if (args->csa_sequenceid == 1 && (slot->seq_nr + 1) == 0) {
190 		slot->seq_nr = 1;
191 		return htonl(NFS4_OK);
192 	}
193 
194 	/* Misordered request */
195 	return htonl(NFS4ERR_SEQ_MISORDERED);
196 }
197 
198 /*
199  * Returns a pointer to a held 'struct nfs_client' that matches the server's
200  * address, major version number, and session ID.  It is the caller's
201  * responsibility to release the returned reference.
202  *
203  * Returns NULL if there are no connections with sessions, or if no session
204  * matches the one of interest.
205  */
206  static struct nfs_client *find_client_with_session(
207 	const struct sockaddr *addr, u32 nfsversion,
208 	struct nfs4_sessionid *sessionid)
209 {
210 	struct nfs_client *clp;
211 
212 	clp = nfs_find_client(addr, 4);
213 	if (clp == NULL)
214 		return NULL;
215 
216 	do {
217 		struct nfs_client *prev = clp;
218 
219 		if (clp->cl_session != NULL) {
220 			if (memcmp(clp->cl_session->sess_id.data,
221 					sessionid->data,
222 					NFS4_MAX_SESSIONID_LEN) == 0) {
223 				/* Returns a held reference to clp */
224 				return clp;
225 			}
226 		}
227 		clp = nfs_find_client_next(prev);
228 		nfs_put_client(prev);
229 	} while (clp != NULL);
230 
231 	return NULL;
232 }
233 
234 /*
235  * For each referring call triple, check the session's slot table for
236  * a match.  If the slot is in use and the sequence numbers match, the
237  * client is still waiting for a response to the original request.
238  */
239 static bool referring_call_exists(struct nfs_client *clp,
240 				  uint32_t nrclists,
241 				  struct referring_call_list *rclists)
242 {
243 	bool status = 0;
244 	int i, j;
245 	struct nfs4_session *session;
246 	struct nfs4_slot_table *tbl;
247 	struct referring_call_list *rclist;
248 	struct referring_call *ref;
249 
250 	/*
251 	 * XXX When client trunking is implemented, this becomes
252 	 * a session lookup from within the loop
253 	 */
254 	session = clp->cl_session;
255 	tbl = &session->fc_slot_table;
256 
257 	for (i = 0; i < nrclists; i++) {
258 		rclist = &rclists[i];
259 		if (memcmp(session->sess_id.data,
260 			   rclist->rcl_sessionid.data,
261 			   NFS4_MAX_SESSIONID_LEN) != 0)
262 			continue;
263 
264 		for (j = 0; j < rclist->rcl_nrefcalls; j++) {
265 			ref = &rclist->rcl_refcalls[j];
266 
267 			dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u "
268 				"slotid %u\n", __func__,
269 				((u32 *)&rclist->rcl_sessionid.data)[0],
270 				((u32 *)&rclist->rcl_sessionid.data)[1],
271 				((u32 *)&rclist->rcl_sessionid.data)[2],
272 				((u32 *)&rclist->rcl_sessionid.data)[3],
273 				ref->rc_sequenceid, ref->rc_slotid);
274 
275 			spin_lock(&tbl->slot_tbl_lock);
276 			status = (test_bit(ref->rc_slotid, tbl->used_slots) &&
277 				  tbl->slots[ref->rc_slotid].seq_nr ==
278 					ref->rc_sequenceid);
279 			spin_unlock(&tbl->slot_tbl_lock);
280 			if (status)
281 				goto out;
282 		}
283 	}
284 
285 out:
286 	return status;
287 }
288 
289 __be32 nfs4_callback_sequence(struct cb_sequenceargs *args,
290 				struct cb_sequenceres *res)
291 {
292 	struct nfs_client *clp;
293 	int i;
294 	__be32 status;
295 
296 	status = htonl(NFS4ERR_BADSESSION);
297 	clp = find_client_with_session(args->csa_addr, 4, &args->csa_sessionid);
298 	if (clp == NULL)
299 		goto out;
300 
301 	status = validate_seqid(&clp->cl_session->bc_slot_table, args);
302 	if (status)
303 		goto out_putclient;
304 
305 	/*
306 	 * Check for pending referring calls.  If a match is found, a
307 	 * related callback was received before the response to the original
308 	 * call.
309 	 */
310 	if (referring_call_exists(clp, args->csa_nrclists, args->csa_rclists)) {
311 		status = htonl(NFS4ERR_DELAY);
312 		goto out_putclient;
313 	}
314 
315 	memcpy(&res->csr_sessionid, &args->csa_sessionid,
316 	       sizeof(res->csr_sessionid));
317 	res->csr_sequenceid = args->csa_sequenceid;
318 	res->csr_slotid = args->csa_slotid;
319 	res->csr_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
320 	res->csr_target_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
321 
322 out_putclient:
323 	nfs_put_client(clp);
324 out:
325 	for (i = 0; i < args->csa_nrclists; i++)
326 		kfree(args->csa_rclists[i].rcl_refcalls);
327 	kfree(args->csa_rclists);
328 
329 	if (status == htonl(NFS4ERR_RETRY_UNCACHED_REP))
330 		res->csr_status = 0;
331 	else
332 		res->csr_status = status;
333 	dprintk("%s: exit with status = %d res->csr_status %d\n", __func__,
334 		ntohl(status), ntohl(res->csr_status));
335 	return status;
336 }
337 
338 __be32 nfs4_callback_recallany(struct cb_recallanyargs *args, void *dummy)
339 {
340 	struct nfs_client *clp;
341 	__be32 status;
342 	fmode_t flags = 0;
343 
344 	status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
345 	clp = nfs_find_client(args->craa_addr, 4);
346 	if (clp == NULL)
347 		goto out;
348 
349 	dprintk("NFS: RECALL_ANY callback request from %s\n",
350 		rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
351 
352 	if (test_bit(RCA4_TYPE_MASK_RDATA_DLG, (const unsigned long *)
353 		     &args->craa_type_mask))
354 		flags = FMODE_READ;
355 	if (test_bit(RCA4_TYPE_MASK_WDATA_DLG, (const unsigned long *)
356 		     &args->craa_type_mask))
357 		flags |= FMODE_WRITE;
358 
359 	if (flags)
360 		nfs_expire_all_delegation_types(clp, flags);
361 	status = htonl(NFS4_OK);
362 out:
363 	dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
364 	return status;
365 }
366 
367 /* Reduce the fore channel's max_slots to the target value */
368 __be32 nfs4_callback_recallslot(struct cb_recallslotargs *args, void *dummy)
369 {
370 	struct nfs_client *clp;
371 	struct nfs4_slot_table *fc_tbl;
372 	__be32 status;
373 
374 	status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
375 	clp = nfs_find_client(args->crsa_addr, 4);
376 	if (clp == NULL)
377 		goto out;
378 
379 	dprintk("NFS: CB_RECALL_SLOT request from %s target max slots %d\n",
380 		rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
381 		args->crsa_target_max_slots);
382 
383 	fc_tbl = &clp->cl_session->fc_slot_table;
384 
385 	status = htonl(NFS4ERR_BAD_HIGH_SLOT);
386 	if (args->crsa_target_max_slots > fc_tbl->max_slots ||
387 	    args->crsa_target_max_slots < 1)
388 		goto out_putclient;
389 
390 	status = htonl(NFS4_OK);
391 	if (args->crsa_target_max_slots == fc_tbl->max_slots)
392 		goto out_putclient;
393 
394 	fc_tbl->target_max_slots = args->crsa_target_max_slots;
395 	nfs41_handle_recall_slot(clp);
396 out_putclient:
397 	nfs_put_client(clp);	/* balance nfs_find_client */
398 out:
399 	dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
400 	return status;
401 }
402 #endif /* CONFIG_NFS_V4_1 */
403