xref: /linux/fs/nfs/pnfs_nfs.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Common NFS I/O  operations for the pnfs file based
4  * layout drivers.
5  *
6  * Copyright (c) 2014, Primary Data, Inc. All rights reserved.
7  *
8  * Tom Haynes <loghyr@primarydata.com>
9  */
10 
11 #include <linux/nfs_fs.h>
12 #include <linux/nfs_page.h>
13 #include <linux/sunrpc/addr.h>
14 #include <linux/module.h>
15 
16 #include "nfs4session.h"
17 #include "internal.h"
18 #include "pnfs.h"
19 
20 #define NFSDBG_FACILITY		NFSDBG_PNFS
21 
pnfs_generic_rw_release(void * data)22 void pnfs_generic_rw_release(void *data)
23 {
24 	struct nfs_pgio_header *hdr = data;
25 
26 	nfs_put_client(hdr->ds_clp);
27 	hdr->mds_ops->rpc_release(data);
28 }
29 EXPORT_SYMBOL_GPL(pnfs_generic_rw_release);
30 
31 /* Fake up some data that will cause nfs_commit_release to retry the writes. */
pnfs_generic_prepare_to_resend_writes(struct nfs_commit_data * data)32 void pnfs_generic_prepare_to_resend_writes(struct nfs_commit_data *data)
33 {
34 	struct nfs_writeverf *verf = data->res.verf;
35 
36 	data->task.tk_status = 0;
37 	memset(&verf->verifier, 0, sizeof(verf->verifier));
38 	verf->committed = NFS_UNSTABLE;
39 }
40 EXPORT_SYMBOL_GPL(pnfs_generic_prepare_to_resend_writes);
41 
pnfs_generic_write_commit_done(struct rpc_task * task,void * data)42 void pnfs_generic_write_commit_done(struct rpc_task *task, void *data)
43 {
44 	struct nfs_commit_data *wdata = data;
45 
46 	/* Note this may cause RPC to be resent */
47 	wdata->mds_ops->rpc_call_done(task, data);
48 }
49 EXPORT_SYMBOL_GPL(pnfs_generic_write_commit_done);
50 
pnfs_generic_commit_release(void * calldata)51 void pnfs_generic_commit_release(void *calldata)
52 {
53 	struct nfs_commit_data *data = calldata;
54 
55 	data->completion_ops->completion(data);
56 	pnfs_put_lseg(data->lseg);
57 	nfs_put_client(data->ds_clp);
58 	nfs_commitdata_release(data);
59 }
60 EXPORT_SYMBOL_GPL(pnfs_generic_commit_release);
61 
62 static struct pnfs_layout_segment *
pnfs_free_bucket_lseg(struct pnfs_commit_bucket * bucket)63 pnfs_free_bucket_lseg(struct pnfs_commit_bucket *bucket)
64 {
65 	if (list_empty(&bucket->committing) && list_empty(&bucket->written)) {
66 		struct pnfs_layout_segment *freeme = bucket->lseg;
67 		bucket->lseg = NULL;
68 		return freeme;
69 	}
70 	return NULL;
71 }
72 
73 /* The generic layer is about to remove the req from the commit list.
74  * If this will make the bucket empty, it will need to put the lseg reference.
75  * Note this must be called holding nfsi->commit_mutex
76  */
77 void
pnfs_generic_clear_request_commit(struct nfs_page * req,struct nfs_commit_info * cinfo)78 pnfs_generic_clear_request_commit(struct nfs_page *req,
79 				  struct nfs_commit_info *cinfo)
80 {
81 	struct pnfs_commit_bucket *bucket = NULL;
82 
83 	if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags))
84 		goto out;
85 	cinfo->ds->nwritten--;
86 	if (list_is_singular(&req->wb_list))
87 		bucket = list_first_entry(&req->wb_list,
88 					  struct pnfs_commit_bucket, written);
89 out:
90 	nfs_request_remove_commit_list(req, cinfo);
91 	if (bucket)
92 		pnfs_put_lseg(pnfs_free_bucket_lseg(bucket));
93 }
94 EXPORT_SYMBOL_GPL(pnfs_generic_clear_request_commit);
95 
96 struct pnfs_commit_array *
pnfs_alloc_commit_array(size_t n,gfp_t gfp_flags)97 pnfs_alloc_commit_array(size_t n, gfp_t gfp_flags)
98 {
99 	struct pnfs_commit_array *p;
100 	struct pnfs_commit_bucket *b;
101 
102 	p = kmalloc(struct_size(p, buckets, n), gfp_flags);
103 	if (!p)
104 		return NULL;
105 	p->nbuckets = n;
106 	INIT_LIST_HEAD(&p->cinfo_list);
107 	INIT_LIST_HEAD(&p->lseg_list);
108 	p->lseg = NULL;
109 	for (b = &p->buckets[0]; n != 0; b++, n--) {
110 		INIT_LIST_HEAD(&b->written);
111 		INIT_LIST_HEAD(&b->committing);
112 		b->lseg = NULL;
113 		b->direct_verf.committed = NFS_INVALID_STABLE_HOW;
114 	}
115 	return p;
116 }
117 EXPORT_SYMBOL_GPL(pnfs_alloc_commit_array);
118 
119 void
pnfs_free_commit_array(struct pnfs_commit_array * p)120 pnfs_free_commit_array(struct pnfs_commit_array *p)
121 {
122 	kfree_rcu(p, rcu);
123 }
124 EXPORT_SYMBOL_GPL(pnfs_free_commit_array);
125 
126 static struct pnfs_commit_array *
pnfs_find_commit_array_by_lseg(struct pnfs_ds_commit_info * fl_cinfo,struct pnfs_layout_segment * lseg)127 pnfs_find_commit_array_by_lseg(struct pnfs_ds_commit_info *fl_cinfo,
128 		struct pnfs_layout_segment *lseg)
129 {
130 	struct pnfs_commit_array *array;
131 
132 	list_for_each_entry_rcu(array, &fl_cinfo->commits, cinfo_list) {
133 		if (array->lseg == lseg)
134 			return array;
135 	}
136 	return NULL;
137 }
138 
139 struct pnfs_commit_array *
pnfs_add_commit_array(struct pnfs_ds_commit_info * fl_cinfo,struct pnfs_commit_array * new,struct pnfs_layout_segment * lseg)140 pnfs_add_commit_array(struct pnfs_ds_commit_info *fl_cinfo,
141 		struct pnfs_commit_array *new,
142 		struct pnfs_layout_segment *lseg)
143 {
144 	struct pnfs_commit_array *array;
145 
146 	array = pnfs_find_commit_array_by_lseg(fl_cinfo, lseg);
147 	if (array)
148 		return array;
149 	new->lseg = lseg;
150 	refcount_set(&new->refcount, 1);
151 	list_add_rcu(&new->cinfo_list, &fl_cinfo->commits);
152 	list_add(&new->lseg_list, &lseg->pls_commits);
153 	return new;
154 }
155 EXPORT_SYMBOL_GPL(pnfs_add_commit_array);
156 
157 static struct pnfs_commit_array *
pnfs_lookup_commit_array(struct pnfs_ds_commit_info * fl_cinfo,struct pnfs_layout_segment * lseg)158 pnfs_lookup_commit_array(struct pnfs_ds_commit_info *fl_cinfo,
159 		struct pnfs_layout_segment *lseg)
160 {
161 	struct pnfs_commit_array *array;
162 
163 	rcu_read_lock();
164 	array = pnfs_find_commit_array_by_lseg(fl_cinfo, lseg);
165 	if (!array) {
166 		rcu_read_unlock();
167 		fl_cinfo->ops->setup_ds_info(fl_cinfo, lseg);
168 		rcu_read_lock();
169 		array = pnfs_find_commit_array_by_lseg(fl_cinfo, lseg);
170 	}
171 	rcu_read_unlock();
172 	return array;
173 }
174 
175 static void
pnfs_release_commit_array_locked(struct pnfs_commit_array * array)176 pnfs_release_commit_array_locked(struct pnfs_commit_array *array)
177 {
178 	list_del_rcu(&array->cinfo_list);
179 	list_del(&array->lseg_list);
180 	pnfs_free_commit_array(array);
181 }
182 
183 static void
pnfs_put_commit_array_locked(struct pnfs_commit_array * array)184 pnfs_put_commit_array_locked(struct pnfs_commit_array *array)
185 {
186 	if (refcount_dec_and_test(&array->refcount))
187 		pnfs_release_commit_array_locked(array);
188 }
189 
190 static void
pnfs_put_commit_array(struct pnfs_commit_array * array,struct inode * inode)191 pnfs_put_commit_array(struct pnfs_commit_array *array, struct inode *inode)
192 {
193 	if (refcount_dec_and_lock(&array->refcount, &inode->i_lock)) {
194 		pnfs_release_commit_array_locked(array);
195 		spin_unlock(&inode->i_lock);
196 	}
197 }
198 
199 static struct pnfs_commit_array *
pnfs_get_commit_array(struct pnfs_commit_array * array)200 pnfs_get_commit_array(struct pnfs_commit_array *array)
201 {
202 	if (refcount_inc_not_zero(&array->refcount))
203 		return array;
204 	return NULL;
205 }
206 
207 static void
pnfs_remove_and_free_commit_array(struct pnfs_commit_array * array)208 pnfs_remove_and_free_commit_array(struct pnfs_commit_array *array)
209 {
210 	array->lseg = NULL;
211 	list_del_init(&array->lseg_list);
212 	pnfs_put_commit_array_locked(array);
213 }
214 
215 void
pnfs_generic_ds_cinfo_release_lseg(struct pnfs_ds_commit_info * fl_cinfo,struct pnfs_layout_segment * lseg)216 pnfs_generic_ds_cinfo_release_lseg(struct pnfs_ds_commit_info *fl_cinfo,
217 		struct pnfs_layout_segment *lseg)
218 {
219 	struct pnfs_commit_array *array, *tmp;
220 
221 	list_for_each_entry_safe(array, tmp, &lseg->pls_commits, lseg_list)
222 		pnfs_remove_and_free_commit_array(array);
223 }
224 EXPORT_SYMBOL_GPL(pnfs_generic_ds_cinfo_release_lseg);
225 
226 void
pnfs_generic_ds_cinfo_destroy(struct pnfs_ds_commit_info * fl_cinfo)227 pnfs_generic_ds_cinfo_destroy(struct pnfs_ds_commit_info *fl_cinfo)
228 {
229 	struct pnfs_commit_array *array, *tmp;
230 
231 	list_for_each_entry_safe(array, tmp, &fl_cinfo->commits, cinfo_list)
232 		pnfs_remove_and_free_commit_array(array);
233 }
234 EXPORT_SYMBOL_GPL(pnfs_generic_ds_cinfo_destroy);
235 
236 /*
237  * Locks the nfs_page requests for commit and moves them to
238  * @bucket->committing.
239  */
240 static int
pnfs_bucket_scan_ds_commit_list(struct pnfs_commit_bucket * bucket,struct nfs_commit_info * cinfo,int max)241 pnfs_bucket_scan_ds_commit_list(struct pnfs_commit_bucket *bucket,
242 				struct nfs_commit_info *cinfo,
243 				int max)
244 {
245 	struct list_head *src = &bucket->written;
246 	struct list_head *dst = &bucket->committing;
247 	int ret;
248 
249 	lockdep_assert_held(&NFS_I(cinfo->inode)->commit_mutex);
250 	ret = nfs_scan_commit_list(src, dst, cinfo, max);
251 	if (ret) {
252 		cinfo->ds->nwritten -= ret;
253 		cinfo->ds->ncommitting += ret;
254 	}
255 	return ret;
256 }
257 
pnfs_bucket_scan_array(struct nfs_commit_info * cinfo,struct pnfs_commit_bucket * buckets,unsigned int nbuckets,int max)258 static int pnfs_bucket_scan_array(struct nfs_commit_info *cinfo,
259 				  struct pnfs_commit_bucket *buckets,
260 				  unsigned int nbuckets,
261 				  int max)
262 {
263 	unsigned int i;
264 	int rv = 0, cnt;
265 
266 	for (i = 0; i < nbuckets && max != 0; i++) {
267 		cnt = pnfs_bucket_scan_ds_commit_list(&buckets[i], cinfo, max);
268 		rv += cnt;
269 		max -= cnt;
270 	}
271 	return rv;
272 }
273 
274 /* Move reqs from written to committing lists, returning count
275  * of number moved.
276  */
pnfs_generic_scan_commit_lists(struct nfs_commit_info * cinfo,int max)277 int pnfs_generic_scan_commit_lists(struct nfs_commit_info *cinfo, int max)
278 {
279 	struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds;
280 	struct pnfs_commit_array *array;
281 	int rv = 0, cnt;
282 
283 	rcu_read_lock();
284 	list_for_each_entry_rcu(array, &fl_cinfo->commits, cinfo_list) {
285 		if (!array->lseg || !pnfs_get_commit_array(array))
286 			continue;
287 		rcu_read_unlock();
288 		cnt = pnfs_bucket_scan_array(cinfo, array->buckets,
289 				array->nbuckets, max);
290 		rcu_read_lock();
291 		pnfs_put_commit_array(array, cinfo->inode);
292 		rv += cnt;
293 		max -= cnt;
294 		if (!max)
295 			break;
296 	}
297 	rcu_read_unlock();
298 	return rv;
299 }
300 EXPORT_SYMBOL_GPL(pnfs_generic_scan_commit_lists);
301 
302 static unsigned int
pnfs_bucket_recover_commit_reqs(struct list_head * dst,struct pnfs_commit_bucket * buckets,unsigned int nbuckets,struct nfs_commit_info * cinfo)303 pnfs_bucket_recover_commit_reqs(struct list_head *dst,
304 			        struct pnfs_commit_bucket *buckets,
305 				unsigned int nbuckets,
306 				struct nfs_commit_info *cinfo)
307 {
308 	struct pnfs_commit_bucket *b;
309 	struct pnfs_layout_segment *freeme;
310 	unsigned int nwritten, ret = 0;
311 	unsigned int i;
312 
313 restart:
314 	for (i = 0, b = buckets; i < nbuckets; i++, b++) {
315 		nwritten = nfs_scan_commit_list(&b->written, dst, cinfo, 0);
316 		if (!nwritten)
317 			continue;
318 		ret += nwritten;
319 		freeme = pnfs_free_bucket_lseg(b);
320 		if (freeme) {
321 			pnfs_put_lseg(freeme);
322 			goto restart;
323 		}
324 	}
325 	return ret;
326 }
327 
328 /* Pull everything off the committing lists and dump into @dst.  */
pnfs_generic_recover_commit_reqs(struct list_head * dst,struct nfs_commit_info * cinfo)329 void pnfs_generic_recover_commit_reqs(struct list_head *dst,
330 				      struct nfs_commit_info *cinfo)
331 {
332 	struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds;
333 	struct pnfs_commit_array *array;
334 	unsigned int nwritten;
335 
336 	lockdep_assert_held(&NFS_I(cinfo->inode)->commit_mutex);
337 	rcu_read_lock();
338 	list_for_each_entry_rcu(array, &fl_cinfo->commits, cinfo_list) {
339 		if (!array->lseg || !pnfs_get_commit_array(array))
340 			continue;
341 		rcu_read_unlock();
342 		nwritten = pnfs_bucket_recover_commit_reqs(dst,
343 							   array->buckets,
344 							   array->nbuckets,
345 							   cinfo);
346 		rcu_read_lock();
347 		pnfs_put_commit_array(array, cinfo->inode);
348 		fl_cinfo->nwritten -= nwritten;
349 	}
350 	rcu_read_unlock();
351 }
352 EXPORT_SYMBOL_GPL(pnfs_generic_recover_commit_reqs);
353 
354 static struct pnfs_layout_segment *
pnfs_bucket_get_committing(struct list_head * head,struct pnfs_commit_bucket * bucket,struct nfs_commit_info * cinfo)355 pnfs_bucket_get_committing(struct list_head *head,
356 			   struct pnfs_commit_bucket *bucket,
357 			   struct nfs_commit_info *cinfo)
358 {
359 	struct pnfs_layout_segment *lseg;
360 	struct list_head *pos;
361 
362 	list_for_each(pos, &bucket->committing)
363 		cinfo->ds->ncommitting--;
364 	list_splice_init(&bucket->committing, head);
365 	lseg = pnfs_free_bucket_lseg(bucket);
366 	if (!lseg)
367 		lseg = pnfs_get_lseg(bucket->lseg);
368 	return lseg;
369 }
370 
371 static struct nfs_commit_data *
pnfs_bucket_fetch_commitdata(struct pnfs_commit_bucket * bucket,struct nfs_commit_info * cinfo)372 pnfs_bucket_fetch_commitdata(struct pnfs_commit_bucket *bucket,
373 			     struct nfs_commit_info *cinfo)
374 {
375 	struct nfs_commit_data *data = nfs_commitdata_alloc();
376 
377 	if (!data)
378 		return NULL;
379 	data->lseg = pnfs_bucket_get_committing(&data->pages, bucket, cinfo);
380 	return data;
381 }
382 
pnfs_generic_retry_commit(struct pnfs_commit_bucket * buckets,unsigned int nbuckets,struct nfs_commit_info * cinfo,unsigned int idx)383 static void pnfs_generic_retry_commit(struct pnfs_commit_bucket *buckets,
384 				      unsigned int nbuckets,
385 				      struct nfs_commit_info *cinfo,
386 				      unsigned int idx)
387 {
388 	struct pnfs_commit_bucket *bucket;
389 	struct pnfs_layout_segment *freeme;
390 	LIST_HEAD(pages);
391 
392 	for (bucket = buckets; idx < nbuckets; bucket++, idx++) {
393 		if (list_empty(&bucket->committing))
394 			continue;
395 		mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
396 		freeme = pnfs_bucket_get_committing(&pages, bucket, cinfo);
397 		mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
398 		nfs_retry_commit(&pages, freeme, cinfo, idx);
399 		pnfs_put_lseg(freeme);
400 	}
401 }
402 
403 static unsigned int
pnfs_bucket_alloc_ds_commits(struct list_head * list,struct pnfs_commit_bucket * buckets,unsigned int nbuckets,struct nfs_commit_info * cinfo)404 pnfs_bucket_alloc_ds_commits(struct list_head *list,
405 			     struct pnfs_commit_bucket *buckets,
406 			     unsigned int nbuckets,
407 			     struct nfs_commit_info *cinfo)
408 {
409 	struct pnfs_commit_bucket *bucket;
410 	struct nfs_commit_data *data;
411 	unsigned int i;
412 	unsigned int nreq = 0;
413 
414 	for (i = 0, bucket = buckets; i < nbuckets; i++, bucket++) {
415 		if (list_empty(&bucket->committing))
416 			continue;
417 		mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
418 		if (!list_empty(&bucket->committing)) {
419 			data = pnfs_bucket_fetch_commitdata(bucket, cinfo);
420 			if (!data)
421 				goto out_error;
422 			data->ds_commit_index = i;
423 			list_add_tail(&data->list, list);
424 			nreq++;
425 		}
426 		mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
427 	}
428 	return nreq;
429 out_error:
430 	mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
431 	/* Clean up on error */
432 	pnfs_generic_retry_commit(buckets, nbuckets, cinfo, i);
433 	return nreq;
434 }
435 
436 static unsigned int
pnfs_alloc_ds_commits_list(struct list_head * list,struct pnfs_ds_commit_info * fl_cinfo,struct nfs_commit_info * cinfo)437 pnfs_alloc_ds_commits_list(struct list_head *list,
438 			   struct pnfs_ds_commit_info *fl_cinfo,
439 			   struct nfs_commit_info *cinfo)
440 {
441 	struct pnfs_commit_array *array;
442 	unsigned int ret = 0;
443 
444 	rcu_read_lock();
445 	list_for_each_entry_rcu(array, &fl_cinfo->commits, cinfo_list) {
446 		if (!array->lseg || !pnfs_get_commit_array(array))
447 			continue;
448 		rcu_read_unlock();
449 		ret += pnfs_bucket_alloc_ds_commits(list, array->buckets,
450 				array->nbuckets, cinfo);
451 		rcu_read_lock();
452 		pnfs_put_commit_array(array, cinfo->inode);
453 	}
454 	rcu_read_unlock();
455 	return ret;
456 }
457 
458 /* This follows nfs_commit_list pretty closely */
459 int
pnfs_generic_commit_pagelist(struct inode * inode,struct list_head * mds_pages,int how,struct nfs_commit_info * cinfo,int (* initiate_commit)(struct nfs_commit_data * data,int how))460 pnfs_generic_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
461 			     int how, struct nfs_commit_info *cinfo,
462 			     int (*initiate_commit)(struct nfs_commit_data *data,
463 						    int how))
464 {
465 	struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds;
466 	struct nfs_commit_data *data, *tmp;
467 	LIST_HEAD(list);
468 	unsigned int nreq = 0;
469 
470 	if (!list_empty(mds_pages)) {
471 		data = nfs_commitdata_alloc();
472 		if (!data) {
473 			nfs_retry_commit(mds_pages, NULL, cinfo, -1);
474 			return -ENOMEM;
475 		}
476 		data->ds_commit_index = -1;
477 		list_splice_init(mds_pages, &data->pages);
478 		list_add_tail(&data->list, &list);
479 		nreq++;
480 	}
481 
482 	nreq += pnfs_alloc_ds_commits_list(&list, fl_cinfo, cinfo);
483 	if (nreq == 0)
484 		goto out;
485 
486 	list_for_each_entry_safe(data, tmp, &list, list) {
487 		list_del(&data->list);
488 		if (data->ds_commit_index < 0) {
489 			nfs_init_commit(data, NULL, NULL, cinfo);
490 			nfs_initiate_commit(NFS_CLIENT(inode), data,
491 					    NFS_PROTO(data->inode),
492 					    data->mds_ops, how,
493 					    RPC_TASK_CRED_NOREF, NULL);
494 		} else {
495 			nfs_init_commit(data, NULL, data->lseg, cinfo);
496 			initiate_commit(data, how);
497 		}
498 	}
499 out:
500 	return PNFS_ATTEMPTED;
501 }
502 EXPORT_SYMBOL_GPL(pnfs_generic_commit_pagelist);
503 
504 /*
505  * Data server cache
506  *
507  * Data servers can be mapped to different device ids.
508  * nfs4_pnfs_ds reference counting
509  *   - set to 1 on allocation
510  *   - incremented when a device id maps a data server already in the cache.
511  *   - decremented when deviceid is removed from the cache.
512  */
513 static DEFINE_SPINLOCK(nfs4_ds_cache_lock);
514 static LIST_HEAD(nfs4_data_server_cache);
515 
516 /* Debug routines */
517 static void
print_ds(struct nfs4_pnfs_ds * ds)518 print_ds(struct nfs4_pnfs_ds *ds)
519 {
520 	if (ds == NULL) {
521 		printk(KERN_WARNING "%s NULL device\n", __func__);
522 		return;
523 	}
524 	printk(KERN_WARNING "        ds %s\n"
525 		"        ref count %d\n"
526 		"        client %p\n"
527 		"        cl_exchange_flags %x\n",
528 		ds->ds_remotestr,
529 		refcount_read(&ds->ds_count), ds->ds_clp,
530 		ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0);
531 }
532 
533 static bool
same_sockaddr(struct sockaddr * addr1,struct sockaddr * addr2)534 same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2)
535 {
536 	struct sockaddr_in *a, *b;
537 	struct sockaddr_in6 *a6, *b6;
538 
539 	if (addr1->sa_family != addr2->sa_family)
540 		return false;
541 
542 	switch (addr1->sa_family) {
543 	case AF_INET:
544 		a = (struct sockaddr_in *)addr1;
545 		b = (struct sockaddr_in *)addr2;
546 
547 		if (a->sin_addr.s_addr == b->sin_addr.s_addr &&
548 		    a->sin_port == b->sin_port)
549 			return true;
550 		break;
551 
552 	case AF_INET6:
553 		a6 = (struct sockaddr_in6 *)addr1;
554 		b6 = (struct sockaddr_in6 *)addr2;
555 
556 		/* LINKLOCAL addresses must have matching scope_id */
557 		if (ipv6_addr_src_scope(&a6->sin6_addr) ==
558 		    IPV6_ADDR_SCOPE_LINKLOCAL &&
559 		    a6->sin6_scope_id != b6->sin6_scope_id)
560 			return false;
561 
562 		if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) &&
563 		    a6->sin6_port == b6->sin6_port)
564 			return true;
565 		break;
566 
567 	default:
568 		dprintk("%s: unhandled address family: %u\n",
569 			__func__, addr1->sa_family);
570 		return false;
571 	}
572 
573 	return false;
574 }
575 
576 /*
577  * Checks if 'dsaddrs1' contains a subset of 'dsaddrs2'. If it does,
578  * declare a match.
579  */
580 static bool
_same_data_server_addrs_locked(const struct list_head * dsaddrs1,const struct list_head * dsaddrs2)581 _same_data_server_addrs_locked(const struct list_head *dsaddrs1,
582 			       const struct list_head *dsaddrs2)
583 {
584 	struct nfs4_pnfs_ds_addr *da1, *da2;
585 	struct sockaddr *sa1, *sa2;
586 	bool match = false;
587 
588 	list_for_each_entry(da1, dsaddrs1, da_node) {
589 		sa1 = (struct sockaddr *)&da1->da_addr;
590 		match = false;
591 		list_for_each_entry(da2, dsaddrs2, da_node) {
592 			sa2 = (struct sockaddr *)&da2->da_addr;
593 			match = same_sockaddr(sa1, sa2);
594 			if (match)
595 				break;
596 		}
597 		if (!match)
598 			break;
599 	}
600 	return match;
601 }
602 
603 /*
604  * Lookup DS by addresses.  nfs4_ds_cache_lock is held
605  */
606 static struct nfs4_pnfs_ds *
_data_server_lookup_locked(const struct list_head * dsaddrs)607 _data_server_lookup_locked(const struct list_head *dsaddrs)
608 {
609 	struct nfs4_pnfs_ds *ds;
610 
611 	list_for_each_entry(ds, &nfs4_data_server_cache, ds_node)
612 		if (_same_data_server_addrs_locked(&ds->ds_addrs, dsaddrs))
613 			return ds;
614 	return NULL;
615 }
616 
nfs4_pnfs_ds_addr_alloc(gfp_t gfp_flags)617 static struct nfs4_pnfs_ds_addr *nfs4_pnfs_ds_addr_alloc(gfp_t gfp_flags)
618 {
619 	struct nfs4_pnfs_ds_addr *da = kzalloc(sizeof(*da), gfp_flags);
620 	if (da)
621 		INIT_LIST_HEAD(&da->da_node);
622 	return da;
623 }
624 
nfs4_pnfs_ds_addr_free(struct nfs4_pnfs_ds_addr * da)625 static void nfs4_pnfs_ds_addr_free(struct nfs4_pnfs_ds_addr *da)
626 {
627 	kfree(da->da_remotestr);
628 	kfree(da->da_netid);
629 	kfree(da);
630 }
631 
destroy_ds(struct nfs4_pnfs_ds * ds)632 static void destroy_ds(struct nfs4_pnfs_ds *ds)
633 {
634 	struct nfs4_pnfs_ds_addr *da;
635 
636 	dprintk("--> %s\n", __func__);
637 	ifdebug(FACILITY)
638 		print_ds(ds);
639 
640 	nfs_put_client(ds->ds_clp);
641 
642 	while (!list_empty(&ds->ds_addrs)) {
643 		da = list_first_entry(&ds->ds_addrs,
644 				      struct nfs4_pnfs_ds_addr,
645 				      da_node);
646 		list_del_init(&da->da_node);
647 		nfs4_pnfs_ds_addr_free(da);
648 	}
649 
650 	kfree(ds->ds_remotestr);
651 	kfree(ds);
652 }
653 
nfs4_pnfs_ds_put(struct nfs4_pnfs_ds * ds)654 void nfs4_pnfs_ds_put(struct nfs4_pnfs_ds *ds)
655 {
656 	if (refcount_dec_and_lock(&ds->ds_count,
657 				&nfs4_ds_cache_lock)) {
658 		list_del_init(&ds->ds_node);
659 		spin_unlock(&nfs4_ds_cache_lock);
660 		destroy_ds(ds);
661 	}
662 }
663 EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_put);
664 
665 /*
666  * Create a string with a human readable address and port to avoid
667  * complicated setup around many dprinks.
668  */
669 static char *
nfs4_pnfs_remotestr(struct list_head * dsaddrs,gfp_t gfp_flags)670 nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags)
671 {
672 	struct nfs4_pnfs_ds_addr *da;
673 	char *remotestr;
674 	size_t len;
675 	char *p;
676 
677 	len = 3;        /* '{', '}' and eol */
678 	list_for_each_entry(da, dsaddrs, da_node) {
679 		len += strlen(da->da_remotestr) + 1;    /* string plus comma */
680 	}
681 
682 	remotestr = kzalloc(len, gfp_flags);
683 	if (!remotestr)
684 		return NULL;
685 
686 	p = remotestr;
687 	*(p++) = '{';
688 	len--;
689 	list_for_each_entry(da, dsaddrs, da_node) {
690 		size_t ll = strlen(da->da_remotestr);
691 
692 		if (ll > len)
693 			goto out_err;
694 
695 		memcpy(p, da->da_remotestr, ll);
696 		p += ll;
697 		len -= ll;
698 
699 		if (len < 1)
700 			goto out_err;
701 		(*p++) = ',';
702 		len--;
703 	}
704 	if (len < 2)
705 		goto out_err;
706 	*(p++) = '}';
707 	*p = '\0';
708 	return remotestr;
709 out_err:
710 	kfree(remotestr);
711 	return NULL;
712 }
713 
714 /*
715  * Given a list of multipath struct nfs4_pnfs_ds_addr, add it to ds cache if
716  * uncached and return cached struct nfs4_pnfs_ds.
717  */
718 struct nfs4_pnfs_ds *
nfs4_pnfs_ds_add(struct list_head * dsaddrs,gfp_t gfp_flags)719 nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags)
720 {
721 	struct nfs4_pnfs_ds *tmp_ds, *ds = NULL;
722 	char *remotestr;
723 
724 	if (list_empty(dsaddrs)) {
725 		dprintk("%s: no addresses defined\n", __func__);
726 		goto out;
727 	}
728 
729 	ds = kzalloc(sizeof(*ds), gfp_flags);
730 	if (!ds)
731 		goto out;
732 
733 	/* this is only used for debugging, so it's ok if its NULL */
734 	remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags);
735 
736 	spin_lock(&nfs4_ds_cache_lock);
737 	tmp_ds = _data_server_lookup_locked(dsaddrs);
738 	if (tmp_ds == NULL) {
739 		INIT_LIST_HEAD(&ds->ds_addrs);
740 		list_splice_init(dsaddrs, &ds->ds_addrs);
741 		ds->ds_remotestr = remotestr;
742 		refcount_set(&ds->ds_count, 1);
743 		INIT_LIST_HEAD(&ds->ds_node);
744 		ds->ds_clp = NULL;
745 		list_add(&ds->ds_node, &nfs4_data_server_cache);
746 		dprintk("%s add new data server %s\n", __func__,
747 			ds->ds_remotestr);
748 	} else {
749 		kfree(remotestr);
750 		kfree(ds);
751 		refcount_inc(&tmp_ds->ds_count);
752 		dprintk("%s data server %s found, inc'ed ds_count to %d\n",
753 			__func__, tmp_ds->ds_remotestr,
754 			refcount_read(&tmp_ds->ds_count));
755 		ds = tmp_ds;
756 	}
757 	spin_unlock(&nfs4_ds_cache_lock);
758 out:
759 	return ds;
760 }
761 EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_add);
762 
nfs4_wait_ds_connect(struct nfs4_pnfs_ds * ds)763 static int nfs4_wait_ds_connect(struct nfs4_pnfs_ds *ds)
764 {
765 	might_sleep();
766 	return wait_on_bit(&ds->ds_state, NFS4DS_CONNECTING, TASK_KILLABLE);
767 }
768 
nfs4_clear_ds_conn_bit(struct nfs4_pnfs_ds * ds)769 static void nfs4_clear_ds_conn_bit(struct nfs4_pnfs_ds *ds)
770 {
771 	smp_mb__before_atomic();
772 	clear_and_wake_up_bit(NFS4DS_CONNECTING, &ds->ds_state);
773 }
774 
775 static struct nfs_client *(*get_v3_ds_connect)(
776 			struct nfs_server *mds_srv,
777 			const struct sockaddr_storage *ds_addr,
778 			int ds_addrlen,
779 			int ds_proto,
780 			unsigned int ds_timeo,
781 			unsigned int ds_retrans);
782 
load_v3_ds_connect(void)783 static bool load_v3_ds_connect(void)
784 {
785 	if (!get_v3_ds_connect) {
786 		get_v3_ds_connect = symbol_request(nfs3_set_ds_client);
787 		WARN_ON_ONCE(!get_v3_ds_connect);
788 	}
789 
790 	return(get_v3_ds_connect != NULL);
791 }
792 
nfs4_pnfs_v3_ds_connect_unload(void)793 void nfs4_pnfs_v3_ds_connect_unload(void)
794 {
795 	if (get_v3_ds_connect) {
796 		symbol_put(nfs3_set_ds_client);
797 		get_v3_ds_connect = NULL;
798 	}
799 }
800 
_nfs4_pnfs_v3_ds_connect(struct nfs_server * mds_srv,struct nfs4_pnfs_ds * ds,unsigned int timeo,unsigned int retrans)801 static int _nfs4_pnfs_v3_ds_connect(struct nfs_server *mds_srv,
802 				 struct nfs4_pnfs_ds *ds,
803 				 unsigned int timeo,
804 				 unsigned int retrans)
805 {
806 	struct nfs_client *clp = ERR_PTR(-EIO);
807 	struct nfs4_pnfs_ds_addr *da;
808 	unsigned long connect_timeout = timeo * (retrans + 1) * HZ / 10;
809 	int status = 0;
810 
811 	dprintk("--> %s DS %s\n", __func__, ds->ds_remotestr);
812 
813 	if (!load_v3_ds_connect())
814 		return -EPROTONOSUPPORT;
815 
816 	list_for_each_entry(da, &ds->ds_addrs, da_node) {
817 		dprintk("%s: DS %s: trying address %s\n",
818 			__func__, ds->ds_remotestr, da->da_remotestr);
819 
820 		if (!IS_ERR(clp)) {
821 			struct xprt_create xprt_args = {
822 				.ident = da->da_transport,
823 				.net = clp->cl_net,
824 				.dstaddr = (struct sockaddr *)&da->da_addr,
825 				.addrlen = da->da_addrlen,
826 				.servername = clp->cl_hostname,
827 				.connect_timeout = connect_timeout,
828 				.reconnect_timeout = connect_timeout,
829 			};
830 
831 			if (da->da_transport != clp->cl_proto)
832 				continue;
833 			if (da->da_addr.ss_family != clp->cl_addr.ss_family)
834 				continue;
835 			/* Add this address as an alias */
836 			rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
837 					rpc_clnt_test_and_add_xprt, NULL);
838 			continue;
839 		}
840 		clp = get_v3_ds_connect(mds_srv,
841 				&da->da_addr,
842 				da->da_addrlen, da->da_transport,
843 				timeo, retrans);
844 		if (IS_ERR(clp))
845 			continue;
846 		clp->cl_rpcclient->cl_softerr = 0;
847 		clp->cl_rpcclient->cl_softrtry = 0;
848 	}
849 
850 	if (IS_ERR(clp)) {
851 		status = PTR_ERR(clp);
852 		goto out;
853 	}
854 
855 	smp_wmb();
856 	WRITE_ONCE(ds->ds_clp, clp);
857 	dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
858 out:
859 	return status;
860 }
861 
_nfs4_pnfs_v4_ds_connect(struct nfs_server * mds_srv,struct nfs4_pnfs_ds * ds,unsigned int timeo,unsigned int retrans,u32 minor_version)862 static int _nfs4_pnfs_v4_ds_connect(struct nfs_server *mds_srv,
863 				 struct nfs4_pnfs_ds *ds,
864 				 unsigned int timeo,
865 				 unsigned int retrans,
866 				 u32 minor_version)
867 {
868 	struct nfs_client *clp = ERR_PTR(-EIO);
869 	struct nfs4_pnfs_ds_addr *da;
870 	int status = 0;
871 
872 	dprintk("--> %s DS %s\n", __func__, ds->ds_remotestr);
873 
874 	list_for_each_entry(da, &ds->ds_addrs, da_node) {
875 		char servername[48];
876 
877 		dprintk("%s: DS %s: trying address %s\n",
878 			__func__, ds->ds_remotestr, da->da_remotestr);
879 
880 		if (!IS_ERR(clp) && clp->cl_mvops->session_trunk) {
881 			struct xprt_create xprt_args = {
882 				.ident = da->da_transport,
883 				.net = clp->cl_net,
884 				.dstaddr = (struct sockaddr *)&da->da_addr,
885 				.addrlen = da->da_addrlen,
886 				.servername = clp->cl_hostname,
887 				.xprtsec = clp->cl_xprtsec,
888 			};
889 			struct nfs4_add_xprt_data xprtdata = {
890 				.clp = clp,
891 			};
892 			struct rpc_add_xprt_test rpcdata = {
893 				.add_xprt_test = clp->cl_mvops->session_trunk,
894 				.data = &xprtdata,
895 			};
896 
897 			if (da->da_transport != clp->cl_proto &&
898 					clp->cl_proto != XPRT_TRANSPORT_TCP_TLS)
899 				continue;
900 			if (da->da_transport == XPRT_TRANSPORT_TCP &&
901 				mds_srv->nfs_client->cl_proto ==
902 					XPRT_TRANSPORT_TCP_TLS) {
903 				struct sockaddr *addr =
904 					(struct sockaddr *)&da->da_addr;
905 				struct sockaddr_in *sin =
906 					(struct sockaddr_in *)&da->da_addr;
907 				struct sockaddr_in6 *sin6 =
908 					(struct sockaddr_in6 *)&da->da_addr;
909 
910 				/* for NFS with TLS we need to supply a correct
911 				 * servername of the trunked transport, not the
912 				 * servername of the main transport stored in
913 				 * clp->cl_hostname. And set the protocol to
914 				 * indicate to use TLS
915 				 */
916 				servername[0] = '\0';
917 				switch(addr->sa_family) {
918 				case AF_INET:
919 					snprintf(servername, sizeof(servername),
920 						"%pI4", &sin->sin_addr.s_addr);
921 					break;
922 				case AF_INET6:
923 					snprintf(servername, sizeof(servername),
924 						"%pI6", &sin6->sin6_addr);
925 					break;
926 				default:
927 					/* do not consider this address */
928 					continue;
929 				}
930 				xprt_args.ident = XPRT_TRANSPORT_TCP_TLS;
931 				xprt_args.servername = servername;
932 			}
933 			if (da->da_addr.ss_family != clp->cl_addr.ss_family)
934 				continue;
935 
936 			/**
937 			* Test this address for session trunking and
938 			* add as an alias
939 			*/
940 			xprtdata.cred = nfs4_get_clid_cred(clp);
941 			rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
942 					  rpc_clnt_setup_test_and_add_xprt,
943 					  &rpcdata);
944 			if (xprtdata.cred)
945 				put_cred(xprtdata.cred);
946 		} else {
947 			if (da->da_transport == XPRT_TRANSPORT_TCP &&
948 				mds_srv->nfs_client->cl_proto ==
949 					XPRT_TRANSPORT_TCP_TLS)
950 				da->da_transport = XPRT_TRANSPORT_TCP_TLS;
951 			clp = nfs4_set_ds_client(mds_srv,
952 						&da->da_addr,
953 						da->da_addrlen,
954 						da->da_transport, timeo,
955 						retrans, minor_version);
956 			if (IS_ERR(clp))
957 				continue;
958 
959 			status = nfs4_init_ds_session(clp,
960 					mds_srv->nfs_client->cl_lease_time);
961 			if (status) {
962 				nfs_put_client(clp);
963 				clp = ERR_PTR(-EIO);
964 				continue;
965 			}
966 
967 		}
968 	}
969 
970 	if (IS_ERR(clp)) {
971 		status = PTR_ERR(clp);
972 		goto out;
973 	}
974 
975 	smp_wmb();
976 	WRITE_ONCE(ds->ds_clp, clp);
977 	dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
978 out:
979 	return status;
980 }
981 
982 /*
983  * Create an rpc connection to the nfs4_pnfs_ds data server.
984  * Currently only supports IPv4 and IPv6 addresses.
985  * If connection fails, make devid unavailable and return a -errno.
986  */
nfs4_pnfs_ds_connect(struct nfs_server * mds_srv,struct nfs4_pnfs_ds * ds,struct nfs4_deviceid_node * devid,unsigned int timeo,unsigned int retrans,u32 version,u32 minor_version)987 int nfs4_pnfs_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds,
988 			  struct nfs4_deviceid_node *devid, unsigned int timeo,
989 			  unsigned int retrans, u32 version, u32 minor_version)
990 {
991 	int err;
992 
993 	do {
994 		err = nfs4_wait_ds_connect(ds);
995 		if (err || ds->ds_clp)
996 			goto out;
997 		if (nfs4_test_deviceid_unavailable(devid))
998 			return -ENODEV;
999 	} while (test_and_set_bit(NFS4DS_CONNECTING, &ds->ds_state) != 0);
1000 
1001 	if (ds->ds_clp)
1002 		goto connect_done;
1003 
1004 	switch (version) {
1005 	case 3:
1006 		err = _nfs4_pnfs_v3_ds_connect(mds_srv, ds, timeo, retrans);
1007 		break;
1008 	case 4:
1009 		err = _nfs4_pnfs_v4_ds_connect(mds_srv, ds, timeo, retrans,
1010 					       minor_version);
1011 		break;
1012 	default:
1013 		dprintk("%s: unsupported DS version %d\n", __func__, version);
1014 		err = -EPROTONOSUPPORT;
1015 	}
1016 
1017 connect_done:
1018 	nfs4_clear_ds_conn_bit(ds);
1019 out:
1020 	/*
1021 	 * At this point the ds->ds_clp should be ready, but it might have
1022 	 * hit an error.
1023 	 */
1024 	if (!err) {
1025 		if (!ds->ds_clp || !nfs_client_init_is_complete(ds->ds_clp)) {
1026 			WARN_ON_ONCE(ds->ds_clp ||
1027 				!nfs4_test_deviceid_unavailable(devid));
1028 			return -EINVAL;
1029 		}
1030 		err = nfs_client_init_status(ds->ds_clp);
1031 	}
1032 
1033 	return err;
1034 }
1035 EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_connect);
1036 
1037 /*
1038  * Currently only supports ipv4, ipv6 and one multi-path address.
1039  */
1040 struct nfs4_pnfs_ds_addr *
nfs4_decode_mp_ds_addr(struct net * net,struct xdr_stream * xdr,gfp_t gfp_flags)1041 nfs4_decode_mp_ds_addr(struct net *net, struct xdr_stream *xdr, gfp_t gfp_flags)
1042 {
1043 	struct nfs4_pnfs_ds_addr *da = NULL;
1044 	char *buf, *portstr;
1045 	__be16 port;
1046 	ssize_t nlen, rlen;
1047 	int tmp[2];
1048 	char *netid;
1049 	size_t len;
1050 	char *startsep = "";
1051 	char *endsep = "";
1052 
1053 
1054 	/* r_netid */
1055 	nlen = xdr_stream_decode_string_dup(xdr, &netid, XDR_MAX_NETOBJ,
1056 					    gfp_flags);
1057 	if (unlikely(nlen < 0))
1058 		goto out_err;
1059 
1060 	/* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */
1061 	/* port is ".ABC.DEF", 8 chars max */
1062 	rlen = xdr_stream_decode_string_dup(xdr, &buf, INET6_ADDRSTRLEN +
1063 					    IPV6_SCOPE_ID_LEN + 8, gfp_flags);
1064 	if (unlikely(rlen < 0))
1065 		goto out_free_netid;
1066 
1067 	/* replace port '.' with '-' */
1068 	portstr = strrchr(buf, '.');
1069 	if (!portstr) {
1070 		dprintk("%s: Failed finding expected dot in port\n",
1071 			__func__);
1072 		goto out_free_buf;
1073 	}
1074 	*portstr = '-';
1075 
1076 	/* find '.' between address and port */
1077 	portstr = strrchr(buf, '.');
1078 	if (!portstr) {
1079 		dprintk("%s: Failed finding expected dot between address and "
1080 			"port\n", __func__);
1081 		goto out_free_buf;
1082 	}
1083 	*portstr = '\0';
1084 
1085 	da = nfs4_pnfs_ds_addr_alloc(gfp_flags);
1086 	if (unlikely(!da))
1087 		goto out_free_buf;
1088 
1089 	if (!rpc_pton(net, buf, portstr-buf, (struct sockaddr *)&da->da_addr,
1090 		      sizeof(da->da_addr))) {
1091 		dprintk("%s: error parsing address %s\n", __func__, buf);
1092 		goto out_free_da;
1093 	}
1094 
1095 	portstr++;
1096 	sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]);
1097 	port = htons((tmp[0] << 8) | (tmp[1]));
1098 
1099 	switch (da->da_addr.ss_family) {
1100 	case AF_INET:
1101 		((struct sockaddr_in *)&da->da_addr)->sin_port = port;
1102 		da->da_addrlen = sizeof(struct sockaddr_in);
1103 		break;
1104 
1105 	case AF_INET6:
1106 		((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port;
1107 		da->da_addrlen = sizeof(struct sockaddr_in6);
1108 		startsep = "[";
1109 		endsep = "]";
1110 		break;
1111 
1112 	default:
1113 		dprintk("%s: unsupported address family: %u\n",
1114 			__func__, da->da_addr.ss_family);
1115 		goto out_free_da;
1116 	}
1117 
1118 	da->da_transport = xprt_find_transport_ident(netid);
1119 	if (da->da_transport < 0) {
1120 		dprintk("%s: ERROR: unknown r_netid \"%s\"\n",
1121 			__func__, netid);
1122 		goto out_free_da;
1123 	}
1124 
1125 	da->da_netid = netid;
1126 
1127 	/* save human readable address */
1128 	len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7;
1129 	da->da_remotestr = kzalloc(len, gfp_flags);
1130 
1131 	/* NULL is ok, only used for dprintk */
1132 	if (da->da_remotestr)
1133 		snprintf(da->da_remotestr, len, "%s%s%s:%u", startsep,
1134 			 buf, endsep, ntohs(port));
1135 
1136 	dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr);
1137 	kfree(buf);
1138 	return da;
1139 
1140 out_free_da:
1141 	kfree(da);
1142 out_free_buf:
1143 	dprintk("%s: Error parsing DS addr: %s\n", __func__, buf);
1144 	kfree(buf);
1145 out_free_netid:
1146 	kfree(netid);
1147 out_err:
1148 	return NULL;
1149 }
1150 EXPORT_SYMBOL_GPL(nfs4_decode_mp_ds_addr);
1151 
1152 void
pnfs_layout_mark_request_commit(struct nfs_page * req,struct pnfs_layout_segment * lseg,struct nfs_commit_info * cinfo,u32 ds_commit_idx)1153 pnfs_layout_mark_request_commit(struct nfs_page *req,
1154 				struct pnfs_layout_segment *lseg,
1155 				struct nfs_commit_info *cinfo,
1156 				u32 ds_commit_idx)
1157 {
1158 	struct list_head *list;
1159 	struct pnfs_commit_array *array;
1160 	struct pnfs_commit_bucket *bucket;
1161 
1162 	mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
1163 	array = pnfs_lookup_commit_array(cinfo->ds, lseg);
1164 	if (!array || !pnfs_is_valid_lseg(lseg))
1165 		goto out_resched;
1166 	bucket = &array->buckets[ds_commit_idx];
1167 	list = &bucket->written;
1168 	/* Non-empty buckets hold a reference on the lseg.  That ref
1169 	 * is normally transferred to the COMMIT call and released
1170 	 * there.  It could also be released if the last req is pulled
1171 	 * off due to a rewrite, in which case it will be done in
1172 	 * pnfs_common_clear_request_commit
1173 	 */
1174 	if (!bucket->lseg)
1175 		bucket->lseg = pnfs_get_lseg(lseg);
1176 	set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
1177 	cinfo->ds->nwritten++;
1178 
1179 	nfs_request_add_commit_list_locked(req, list, cinfo);
1180 	mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
1181 	nfs_folio_mark_unstable(nfs_page_to_folio(req), cinfo);
1182 	return;
1183 out_resched:
1184 	mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
1185 	cinfo->completion_ops->resched_write(cinfo, req);
1186 }
1187 EXPORT_SYMBOL_GPL(pnfs_layout_mark_request_commit);
1188 
1189 int
pnfs_nfs_generic_sync(struct inode * inode,bool datasync)1190 pnfs_nfs_generic_sync(struct inode *inode, bool datasync)
1191 {
1192 	int ret;
1193 
1194 	if (!pnfs_layoutcommit_outstanding(inode))
1195 		return 0;
1196 	ret = nfs_commit_inode(inode, FLUSH_SYNC);
1197 	if (ret < 0)
1198 		return ret;
1199 	if (datasync)
1200 		return 0;
1201 	return pnfs_layoutcommit_inode(inode, true);
1202 }
1203 EXPORT_SYMBOL_GPL(pnfs_nfs_generic_sync);
1204 
1205