blob: 06df2e6663dc2cc10e0c0e45bf9d9e5655b400d9 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Tom Haynesf54bcf22014-12-11 15:34:59 -05002/*
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>
Peng Tao6b7f3cf2014-05-29 21:06:59 +080013#include <linux/sunrpc/addr.h>
Peng Tao5f01d952014-05-30 18:15:59 +080014#include <linux/module.h>
Tom Haynesf54bcf22014-12-11 15:34:59 -050015
Peng Tao7405f9e2014-05-29 21:06:58 +080016#include "nfs4session.h"
Tom Haynesf54bcf22014-12-11 15:34:59 -050017#include "internal.h"
18#include "pnfs.h"
19
Peng Tao875ae062014-05-29 21:06:57 +080020#define NFSDBG_FACILITY NFSDBG_PNFS
21
Tom Haynesf54bcf22014-12-11 15:34:59 -050022void pnfs_generic_rw_release(void *data)
23{
24 struct nfs_pgio_header *hdr = data;
Tom Haynesf54bcf22014-12-11 15:34:59 -050025
Tom Haynesf54bcf22014-12-11 15:34:59 -050026 nfs_put_client(hdr->ds_clp);
27 hdr->mds_ops->rpc_release(data);
28}
29EXPORT_SYMBOL_GPL(pnfs_generic_rw_release);
30
31/* Fake up some data that will cause nfs_commit_release to retry the writes. */
32void pnfs_generic_prepare_to_resend_writes(struct nfs_commit_data *data)
33{
Trond Myklebust221203c2020-01-06 15:25:04 -050034 struct nfs_writeverf *verf = data->res.verf;
Tom Haynesf54bcf22014-12-11 15:34:59 -050035
36 data->task.tk_status = 0;
Trond Myklebust221203c2020-01-06 15:25:04 -050037 memset(&verf->verifier, 0, sizeof(verf->verifier));
38 verf->committed = NFS_UNSTABLE;
Tom Haynesf54bcf22014-12-11 15:34:59 -050039}
40EXPORT_SYMBOL_GPL(pnfs_generic_prepare_to_resend_writes);
41
42void 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}
49EXPORT_SYMBOL_GPL(pnfs_generic_write_commit_done);
50
51void 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}
60EXPORT_SYMBOL_GPL(pnfs_generic_commit_release);
61
62/* The generic layer is about to remove the req from the commit list.
63 * If this will make the bucket empty, it will need to put the lseg reference.
Trond Myklebustd0fbb1d2018-08-14 17:25:37 -040064 * Note this must be called holding nfsi->commit_mutex
Tom Haynesf54bcf22014-12-11 15:34:59 -050065 */
66void
67pnfs_generic_clear_request_commit(struct nfs_page *req,
68 struct nfs_commit_info *cinfo)
69{
70 struct pnfs_layout_segment *freeme = NULL;
71
72 if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags))
73 goto out;
74 cinfo->ds->nwritten--;
75 if (list_is_singular(&req->wb_list)) {
76 struct pnfs_commit_bucket *bucket;
77
78 bucket = list_first_entry(&req->wb_list,
79 struct pnfs_commit_bucket,
80 written);
81 freeme = bucket->wlseg;
82 bucket->wlseg = NULL;
83 }
84out:
85 nfs_request_remove_commit_list(req, cinfo);
Trond Myklebust8205b9c2017-08-01 17:07:02 -040086 pnfs_put_lseg(freeme);
Tom Haynesf54bcf22014-12-11 15:34:59 -050087}
88EXPORT_SYMBOL_GPL(pnfs_generic_clear_request_commit);
89
Trond Myklebustd7242c42020-03-18 17:22:47 -040090struct pnfs_commit_array *
91pnfs_alloc_commit_array(size_t n, gfp_t gfp_flags)
92{
93 struct pnfs_commit_array *p;
94 struct pnfs_commit_bucket *b;
95
96 p = kmalloc(struct_size(p, buckets, n), gfp_flags);
97 if (!p)
98 return NULL;
99 p->nbuckets = n;
100 INIT_LIST_HEAD(&p->cinfo_list);
101 INIT_LIST_HEAD(&p->lseg_list);
102 p->lseg = NULL;
103 for (b = &p->buckets[0]; n != 0; b++, n--) {
104 INIT_LIST_HEAD(&b->written);
105 INIT_LIST_HEAD(&b->committing);
106 b->wlseg = NULL;
107 b->clseg = NULL;
108 b->direct_verf.committed = NFS_INVALID_STABLE_HOW;
109 }
110 return p;
111}
112EXPORT_SYMBOL_GPL(pnfs_alloc_commit_array);
113
114void
115pnfs_free_commit_array(struct pnfs_commit_array *p)
116{
117 kfree_rcu(p, rcu);
118}
119EXPORT_SYMBOL_GPL(pnfs_free_commit_array);
120
Trond Myklebustba827c92020-03-20 18:34:33 -0400121static struct pnfs_commit_array *
122pnfs_find_commit_array_by_lseg(struct pnfs_ds_commit_info *fl_cinfo,
123 struct pnfs_layout_segment *lseg)
124{
125 struct pnfs_commit_array *array;
126
127 list_for_each_entry_rcu(array, &fl_cinfo->commits, cinfo_list) {
128 if (array->lseg == lseg)
129 return array;
130 }
131 return NULL;
132}
133
134struct pnfs_commit_array *
135pnfs_add_commit_array(struct pnfs_ds_commit_info *fl_cinfo,
136 struct pnfs_commit_array *new,
137 struct pnfs_layout_segment *lseg)
138{
139 struct pnfs_commit_array *array;
140
141 array = pnfs_find_commit_array_by_lseg(fl_cinfo, lseg);
142 if (array)
143 return array;
144 new->lseg = lseg;
145 refcount_set(&new->refcount, 1);
146 list_add_rcu(&new->cinfo_list, &fl_cinfo->commits);
147 list_add(&new->lseg_list, &lseg->pls_commits);
148 return new;
149}
150EXPORT_SYMBOL_GPL(pnfs_add_commit_array);
151
Trond Myklebustba827c92020-03-20 18:34:33 -0400152static struct pnfs_commit_array *
153pnfs_lookup_commit_array(struct pnfs_ds_commit_info *fl_cinfo,
154 struct pnfs_layout_segment *lseg)
155{
156 struct pnfs_commit_array *array;
157
158 rcu_read_lock();
159 array = pnfs_find_commit_array_by_lseg(fl_cinfo, lseg);
160 if (!array) {
161 rcu_read_unlock();
Trond Myklebust9c455a82020-03-21 11:13:05 -0400162 fl_cinfo->ops->setup_ds_info(fl_cinfo, lseg);
Trond Myklebustba827c92020-03-20 18:34:33 -0400163 rcu_read_lock();
164 array = pnfs_find_commit_array_by_lseg(fl_cinfo, lseg);
165 }
166 rcu_read_unlock();
167 return array;
168}
169
Trond Myklebusta9901892020-03-20 16:04:06 -0400170static void
171pnfs_release_commit_array_locked(struct pnfs_commit_array *array)
172{
173 list_del_rcu(&array->cinfo_list);
174 list_del(&array->lseg_list);
175 pnfs_free_commit_array(array);
176}
177
178static void
179pnfs_put_commit_array_locked(struct pnfs_commit_array *array)
180{
181 if (refcount_dec_and_test(&array->refcount))
182 pnfs_release_commit_array_locked(array);
183}
184
185static void
186pnfs_put_commit_array(struct pnfs_commit_array *array, struct inode *inode)
187{
188 if (refcount_dec_and_lock(&array->refcount, &inode->i_lock)) {
189 pnfs_release_commit_array_locked(array);
190 spin_unlock(&inode->i_lock);
191 }
192}
193
194static struct pnfs_commit_array *
195pnfs_get_commit_array(struct pnfs_commit_array *array)
196{
197 if (refcount_inc_not_zero(&array->refcount))
198 return array;
199 return NULL;
200}
201
202static void
203pnfs_remove_and_free_commit_array(struct pnfs_commit_array *array)
204{
205 array->lseg = NULL;
206 list_del_init(&array->lseg_list);
207 pnfs_put_commit_array_locked(array);
208}
209
210void
211pnfs_generic_ds_cinfo_release_lseg(struct pnfs_ds_commit_info *fl_cinfo,
212 struct pnfs_layout_segment *lseg)
213{
214 struct pnfs_commit_array *array, *tmp;
215
216 list_for_each_entry_safe(array, tmp, &lseg->pls_commits, lseg_list)
217 pnfs_remove_and_free_commit_array(array);
218}
219EXPORT_SYMBOL_GPL(pnfs_generic_ds_cinfo_release_lseg);
220
221void
222pnfs_generic_ds_cinfo_destroy(struct pnfs_ds_commit_info *fl_cinfo)
223{
224 struct pnfs_commit_array *array, *tmp;
225
226 list_for_each_entry_safe(array, tmp, &fl_cinfo->commits, cinfo_list)
227 pnfs_remove_and_free_commit_array(array);
228}
229EXPORT_SYMBOL_GPL(pnfs_generic_ds_cinfo_destroy);
230
Trond Myklebusta8e37652020-03-19 13:41:08 -0400231/*
232 * Locks the nfs_page requests for commit and moves them to
233 * @bucket->committing.
234 */
Tom Haynesf54bcf22014-12-11 15:34:59 -0500235static int
Trond Myklebusta8e37652020-03-19 13:41:08 -0400236pnfs_bucket_scan_ds_commit_list(struct pnfs_commit_bucket *bucket,
237 struct nfs_commit_info *cinfo,
238 int max)
Tom Haynesf54bcf22014-12-11 15:34:59 -0500239{
240 struct list_head *src = &bucket->written;
241 struct list_head *dst = &bucket->committing;
242 int ret;
243
Trond Myklebuste824f992017-08-01 11:53:49 -0400244 lockdep_assert_held(&NFS_I(cinfo->inode)->commit_mutex);
Trond Myklebust5d2a9d92017-09-09 12:40:38 -0400245 ret = nfs_scan_commit_list(src, dst, cinfo, max);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500246 if (ret) {
247 cinfo->ds->nwritten -= ret;
248 cinfo->ds->ncommitting += ret;
Trond Myklebust27571292015-08-03 17:38:33 -0400249 if (bucket->clseg == NULL)
250 bucket->clseg = pnfs_get_lseg(bucket->wlseg);
251 if (list_empty(src)) {
Trond Myklebust8205b9c2017-08-01 17:07:02 -0400252 pnfs_put_lseg(bucket->wlseg);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500253 bucket->wlseg = NULL;
Trond Myklebust27571292015-08-03 17:38:33 -0400254 }
Tom Haynesf54bcf22014-12-11 15:34:59 -0500255 }
256 return ret;
257}
258
Trond Myklebusta8e37652020-03-19 13:41:08 -0400259static int pnfs_bucket_scan_array(struct nfs_commit_info *cinfo,
260 struct pnfs_commit_bucket *buckets,
261 unsigned int nbuckets,
262 int max)
263{
264 unsigned int i;
265 int rv = 0, cnt;
266
267 for (i = 0; i < nbuckets && max != 0; i++) {
268 cnt = pnfs_bucket_scan_ds_commit_list(&buckets[i], cinfo, max);
269 rv += cnt;
270 max -= cnt;
271 }
272 return rv;
273}
274
Tom Haynes085d1e32014-12-11 13:04:55 -0500275/* Move reqs from written to committing lists, returning count
276 * of number moved.
Tom Haynesf54bcf22014-12-11 15:34:59 -0500277 */
Trond Myklebusta8e37652020-03-19 13:41:08 -0400278int pnfs_generic_scan_commit_lists(struct nfs_commit_info *cinfo, int max)
Tom Haynesf54bcf22014-12-11 15:34:59 -0500279{
Trond Myklebusta8e37652020-03-19 13:41:08 -0400280 struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds;
281 struct pnfs_commit_array *array;
282 int rv = 0, cnt;
Tom Haynesf54bcf22014-12-11 15:34:59 -0500283
Trond Myklebusta9901892020-03-20 16:04:06 -0400284 rcu_read_lock();
285 list_for_each_entry_rcu(array, &fl_cinfo->commits, cinfo_list) {
286 if (!array->lseg || !pnfs_get_commit_array(array))
287 continue;
288 rcu_read_unlock();
Trond Myklebusta8e37652020-03-19 13:41:08 -0400289 cnt = pnfs_bucket_scan_array(cinfo, array->buckets,
290 array->nbuckets, max);
Trond Myklebusta9901892020-03-20 16:04:06 -0400291 rcu_read_lock();
292 pnfs_put_commit_array(array, cinfo->inode);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500293 rv += cnt;
Trond Myklebusta8e37652020-03-19 13:41:08 -0400294 max -= cnt;
295 if (!max)
296 break;
Tom Haynesf54bcf22014-12-11 15:34:59 -0500297 }
Trond Myklebusta9901892020-03-20 16:04:06 -0400298 rcu_read_unlock();
Tom Haynesf54bcf22014-12-11 15:34:59 -0500299 return rv;
300}
301EXPORT_SYMBOL_GPL(pnfs_generic_scan_commit_lists);
302
Trond Myklebustfce9ed02020-03-19 15:48:42 -0400303static unsigned int
304pnfs_bucket_recover_commit_reqs(struct list_head *dst,
305 struct pnfs_commit_bucket *buckets,
306 unsigned int nbuckets,
307 struct nfs_commit_info *cinfo)
Tom Haynesf54bcf22014-12-11 15:34:59 -0500308{
309 struct pnfs_commit_bucket *b;
310 struct pnfs_layout_segment *freeme;
Trond Myklebustfce9ed02020-03-19 15:48:42 -0400311 unsigned int nwritten, ret = 0;
312 unsigned int i;
Tom Haynesf54bcf22014-12-11 15:34:59 -0500313
314restart:
Trond Myklebustfce9ed02020-03-19 15:48:42 -0400315 for (i = 0, b = buckets; i < nbuckets; i++, b++) {
Trond Myklebust5d2a9d92017-09-09 12:40:38 -0400316 nwritten = nfs_scan_commit_list(&b->written, dst, cinfo, 0);
Trond Myklebuste39928f2017-07-12 19:10:56 -0400317 if (!nwritten)
318 continue;
Trond Myklebustfce9ed02020-03-19 15:48:42 -0400319 ret += nwritten;
Trond Myklebuste39928f2017-07-12 19:10:56 -0400320 if (list_empty(&b->written)) {
Tom Haynesf54bcf22014-12-11 15:34:59 -0500321 freeme = b->wlseg;
322 b->wlseg = NULL;
Tom Haynesf54bcf22014-12-11 15:34:59 -0500323 pnfs_put_lseg(freeme);
324 goto restart;
325 }
326 }
Trond Myklebustfce9ed02020-03-19 15:48:42 -0400327 return ret;
328}
329
330/* Pull everything off the committing lists and dump into @dst. */
331void pnfs_generic_recover_commit_reqs(struct list_head *dst,
332 struct nfs_commit_info *cinfo)
333{
334 struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds;
335 struct pnfs_commit_array *array;
336 unsigned int nwritten;
337
338 lockdep_assert_held(&NFS_I(cinfo->inode)->commit_mutex);
Trond Myklebusta9901892020-03-20 16:04:06 -0400339 rcu_read_lock();
340 list_for_each_entry_rcu(array, &fl_cinfo->commits, cinfo_list) {
341 if (!array->lseg || !pnfs_get_commit_array(array))
342 continue;
343 rcu_read_unlock();
Trond Myklebustfce9ed02020-03-19 15:48:42 -0400344 nwritten = pnfs_bucket_recover_commit_reqs(dst,
345 array->buckets,
346 array->nbuckets,
347 cinfo);
Trond Myklebusta9901892020-03-20 16:04:06 -0400348 rcu_read_lock();
349 pnfs_put_commit_array(array, cinfo->inode);
Trond Myklebustfce9ed02020-03-19 15:48:42 -0400350 fl_cinfo->nwritten -= nwritten;
351 }
Trond Myklebusta9901892020-03-20 16:04:06 -0400352 rcu_read_unlock();
Tom Haynesf54bcf22014-12-11 15:34:59 -0500353}
354EXPORT_SYMBOL_GPL(pnfs_generic_recover_commit_reqs);
355
Trond Myklebustfb6b53b2020-03-20 19:24:19 -0400356static struct nfs_page *
357pnfs_bucket_search_commit_reqs(struct pnfs_commit_bucket *buckets,
358 unsigned int nbuckets, struct page *page)
359{
360 struct nfs_page *req;
361 struct pnfs_commit_bucket *b;
362 unsigned int i;
363
364 /* Linearly search the commit lists for each bucket until a matching
365 * request is found */
366 for (i = 0, b = buckets; i < nbuckets; i++, b++) {
367 list_for_each_entry(req, &b->written, wb_list) {
368 if (req->wb_page == page)
369 return req->wb_head;
370 }
371 list_for_each_entry(req, &b->committing, wb_list) {
372 if (req->wb_page == page)
373 return req->wb_head;
374 }
375 }
376 return NULL;
377}
378
379/* pnfs_generic_search_commit_reqs - Search lists in @cinfo for the head reqest
380 * for @page
381 * @cinfo - commit info for current inode
382 * @page - page to search for matching head request
383 *
384 * Returns a the head request if one is found, otherwise returns NULL.
385 */
386struct nfs_page *
387pnfs_generic_search_commit_reqs(struct nfs_commit_info *cinfo, struct page *page)
388{
389 struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds;
390 struct pnfs_commit_array *array;
391 struct nfs_page *req;
392
Trond Myklebustfb6b53b2020-03-20 19:24:19 -0400393 list_for_each_entry(array, &fl_cinfo->commits, cinfo_list) {
394 req = pnfs_bucket_search_commit_reqs(array->buckets,
395 array->nbuckets, page);
396 if (req)
397 return req;
398 }
399 return NULL;
400}
401EXPORT_SYMBOL_GPL(pnfs_generic_search_commit_reqs);
402
Trond Myklebust19573c92020-03-19 17:29:12 -0400403static struct pnfs_layout_segment *
404pnfs_bucket_get_committing(struct list_head *head,
405 struct pnfs_commit_bucket *bucket,
406 struct nfs_commit_info *cinfo)
Tom Haynesf54bcf22014-12-11 15:34:59 -0500407{
Tom Haynesf54bcf22014-12-11 15:34:59 -0500408 struct pnfs_layout_segment *freeme;
Trond Myklebust41181882017-07-12 19:10:57 -0400409 struct list_head *pos;
Tom Haynesf54bcf22014-12-11 15:34:59 -0500410
Trond Myklebust19573c92020-03-19 17:29:12 -0400411 list_for_each(pos, &bucket->committing)
412 cinfo->ds->ncommitting--;
413 list_splice_init(&bucket->committing, head);
414 freeme = bucket->clseg;
415 bucket->clseg = NULL;
416 return freeme;
417}
418
419static struct nfs_commit_data *
420pnfs_bucket_fetch_commitdata(struct pnfs_commit_bucket *bucket,
421 struct nfs_commit_info *cinfo)
422{
423 struct nfs_commit_data *data = nfs_commitdata_alloc(false);
424
425 if (!data)
426 return NULL;
427 data->lseg = pnfs_bucket_get_committing(&data->pages, bucket, cinfo);
428 return data;
429}
430
431static void pnfs_generic_retry_commit(struct pnfs_commit_bucket *buckets,
432 unsigned int nbuckets,
433 struct nfs_commit_info *cinfo,
434 unsigned int idx)
435{
436 struct pnfs_commit_bucket *bucket;
437 struct pnfs_layout_segment *freeme;
438 LIST_HEAD(pages);
439
440 for (bucket = buckets; idx < nbuckets; bucket++, idx++) {
Tom Haynesf54bcf22014-12-11 15:34:59 -0500441 if (list_empty(&bucket->committing))
442 continue;
Trond Myklebustd0fbb1d2018-08-14 17:25:37 -0400443 mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
Trond Myklebust19573c92020-03-19 17:29:12 -0400444 freeme = pnfs_bucket_get_committing(&pages, bucket, cinfo);
445 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
446 nfs_retry_commit(&pages, freeme, cinfo, idx);
447 pnfs_put_lseg(freeme);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500448 }
449}
450
451static unsigned int
Trond Myklebust19573c92020-03-19 17:29:12 -0400452pnfs_bucket_alloc_ds_commits(struct list_head *list,
453 struct pnfs_commit_bucket *buckets,
454 unsigned int nbuckets,
455 struct nfs_commit_info *cinfo)
Tom Haynesf54bcf22014-12-11 15:34:59 -0500456{
Tom Haynesf54bcf22014-12-11 15:34:59 -0500457 struct pnfs_commit_bucket *bucket;
458 struct nfs_commit_data *data;
Trond Myklebust19573c92020-03-19 17:29:12 -0400459 unsigned int i;
Tom Haynesf54bcf22014-12-11 15:34:59 -0500460 unsigned int nreq = 0;
461
Trond Myklebust19573c92020-03-19 17:29:12 -0400462 for (i = 0, bucket = buckets; i < nbuckets; i++, bucket++) {
Tom Haynesf54bcf22014-12-11 15:34:59 -0500463 if (list_empty(&bucket->committing))
464 continue;
Trond Myklebust19573c92020-03-19 17:29:12 -0400465 mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
466 if (!list_empty(&bucket->committing)) {
467 data = pnfs_bucket_fetch_commitdata(bucket, cinfo);
468 if (!data)
469 goto out_error;
470 data->ds_commit_index = i;
471 list_add_tail(&data->list, list);
472 atomic_inc(&cinfo->mds->rpcs_out);
473 nreq++;
474 }
475 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500476 }
Tom Haynesf54bcf22014-12-11 15:34:59 -0500477 return nreq;
Trond Myklebust19573c92020-03-19 17:29:12 -0400478out_error:
Trond Myklebustd0fbb1d2018-08-14 17:25:37 -0400479 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
Trond Myklebust19573c92020-03-19 17:29:12 -0400480 /* Clean up on error */
481 pnfs_generic_retry_commit(buckets, nbuckets, cinfo, i);
482 return nreq;
Weston Andros Adamsonade8feb2016-05-25 10:07:23 -0400483}
484
Trond Myklebust0cb1f6d2020-03-19 19:31:00 -0400485static unsigned int
486pnfs_alloc_ds_commits_list(struct list_head *list,
487 struct pnfs_ds_commit_info *fl_cinfo,
488 struct nfs_commit_info *cinfo)
489{
490 struct pnfs_commit_array *array;
491 unsigned int ret = 0;
492
Trond Myklebusta9901892020-03-20 16:04:06 -0400493 rcu_read_lock();
494 list_for_each_entry_rcu(array, &fl_cinfo->commits, cinfo_list) {
495 if (!array->lseg || !pnfs_get_commit_array(array))
496 continue;
497 rcu_read_unlock();
Trond Myklebust0cb1f6d2020-03-19 19:31:00 -0400498 ret += pnfs_bucket_alloc_ds_commits(list, array->buckets,
499 array->nbuckets, cinfo);
Trond Myklebusta9901892020-03-20 16:04:06 -0400500 rcu_read_lock();
501 pnfs_put_commit_array(array, cinfo->inode);
502 }
Trond Myklebust0cb1f6d2020-03-19 19:31:00 -0400503 return ret;
504}
505
Tom Haynesf54bcf22014-12-11 15:34:59 -0500506/* This follows nfs_commit_list pretty closely */
507int
508pnfs_generic_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
509 int how, struct nfs_commit_info *cinfo,
510 int (*initiate_commit)(struct nfs_commit_data *data,
511 int how))
512{
Trond Myklebust19573c92020-03-19 17:29:12 -0400513 struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds;
Tom Haynesf54bcf22014-12-11 15:34:59 -0500514 struct nfs_commit_data *data, *tmp;
515 LIST_HEAD(list);
516 unsigned int nreq = 0;
517
518 if (!list_empty(mds_pages)) {
NeilBrown518662e2017-04-10 12:22:09 +1000519 data = nfs_commitdata_alloc(true);
520 data->ds_commit_index = -1;
Trond Myklebust19573c92020-03-19 17:29:12 -0400521 list_splice_init(mds_pages, &data->pages);
522 list_add_tail(&data->list, &list);
523 atomic_inc(&cinfo->mds->rpcs_out);
NeilBrown518662e2017-04-10 12:22:09 +1000524 nreq++;
Tom Haynesf54bcf22014-12-11 15:34:59 -0500525 }
526
Trond Myklebust0cb1f6d2020-03-19 19:31:00 -0400527 nreq += pnfs_alloc_ds_commits_list(&list, fl_cinfo, cinfo);
Trond Myklebustaf7cf052015-09-29 20:34:05 -0400528 if (nreq == 0)
Tom Haynesf54bcf22014-12-11 15:34:59 -0500529 goto out;
Tom Haynesf54bcf22014-12-11 15:34:59 -0500530
Trond Myklebust19573c92020-03-19 17:29:12 -0400531 list_for_each_entry_safe(data, tmp, &list, list) {
532 list_del(&data->list);
Trond Myklebust27571292015-08-03 17:38:33 -0400533 if (data->ds_commit_index < 0) {
Trond Myklebust19573c92020-03-19 17:29:12 -0400534 nfs_init_commit(data, NULL, NULL, cinfo);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500535 nfs_initiate_commit(NFS_CLIENT(inode), data,
Peng Taoc36aae92014-06-09 07:10:14 +0800536 NFS_PROTO(data->inode),
Tom Haynesf54bcf22014-12-11 15:34:59 -0500537 data->mds_ops, how, 0);
538 } else {
Trond Myklebust19573c92020-03-19 17:29:12 -0400539 nfs_init_commit(data, NULL, data->lseg, cinfo);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500540 initiate_commit(data, how);
541 }
542 }
543out:
Tom Haynesf54bcf22014-12-11 15:34:59 -0500544 return PNFS_ATTEMPTED;
545}
546EXPORT_SYMBOL_GPL(pnfs_generic_commit_pagelist);
Peng Tao875ae062014-05-29 21:06:57 +0800547
548/*
549 * Data server cache
550 *
551 * Data servers can be mapped to different device ids.
552 * nfs4_pnfs_ds reference counting
553 * - set to 1 on allocation
554 * - incremented when a device id maps a data server already in the cache.
555 * - decremented when deviceid is removed from the cache.
556 */
557static DEFINE_SPINLOCK(nfs4_ds_cache_lock);
558static LIST_HEAD(nfs4_data_server_cache);
559
560/* Debug routines */
561static void
562print_ds(struct nfs4_pnfs_ds *ds)
563{
564 if (ds == NULL) {
565 printk(KERN_WARNING "%s NULL device\n", __func__);
566 return;
567 }
568 printk(KERN_WARNING " ds %s\n"
569 " ref count %d\n"
570 " client %p\n"
571 " cl_exchange_flags %x\n",
572 ds->ds_remotestr,
Elena Reshetovaa2a5dea2017-10-20 12:53:31 +0300573 refcount_read(&ds->ds_count), ds->ds_clp,
Peng Tao875ae062014-05-29 21:06:57 +0800574 ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0);
575}
576
577static bool
578same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2)
579{
580 struct sockaddr_in *a, *b;
581 struct sockaddr_in6 *a6, *b6;
582
583 if (addr1->sa_family != addr2->sa_family)
584 return false;
585
586 switch (addr1->sa_family) {
587 case AF_INET:
588 a = (struct sockaddr_in *)addr1;
589 b = (struct sockaddr_in *)addr2;
590
591 if (a->sin_addr.s_addr == b->sin_addr.s_addr &&
592 a->sin_port == b->sin_port)
593 return true;
594 break;
595
596 case AF_INET6:
597 a6 = (struct sockaddr_in6 *)addr1;
598 b6 = (struct sockaddr_in6 *)addr2;
599
600 /* LINKLOCAL addresses must have matching scope_id */
601 if (ipv6_addr_src_scope(&a6->sin6_addr) ==
602 IPV6_ADDR_SCOPE_LINKLOCAL &&
603 a6->sin6_scope_id != b6->sin6_scope_id)
604 return false;
605
606 if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) &&
607 a6->sin6_port == b6->sin6_port)
608 return true;
609 break;
610
611 default:
612 dprintk("%s: unhandled address family: %u\n",
613 __func__, addr1->sa_family);
614 return false;
615 }
616
617 return false;
618}
619
Trond Myklebust6f536932015-08-13 10:59:07 -0400620/*
621 * Checks if 'dsaddrs1' contains a subset of 'dsaddrs2'. If it does,
622 * declare a match.
623 */
Peng Tao875ae062014-05-29 21:06:57 +0800624static bool
625_same_data_server_addrs_locked(const struct list_head *dsaddrs1,
626 const struct list_head *dsaddrs2)
627{
628 struct nfs4_pnfs_ds_addr *da1, *da2;
Trond Myklebust6f536932015-08-13 10:59:07 -0400629 struct sockaddr *sa1, *sa2;
630 bool match = false;
Peng Tao875ae062014-05-29 21:06:57 +0800631
Trond Myklebust6f536932015-08-13 10:59:07 -0400632 list_for_each_entry(da1, dsaddrs1, da_node) {
633 sa1 = (struct sockaddr *)&da1->da_addr;
634 match = false;
635 list_for_each_entry(da2, dsaddrs2, da_node) {
636 sa2 = (struct sockaddr *)&da2->da_addr;
637 match = same_sockaddr(sa1, sa2);
638 if (match)
639 break;
640 }
641 if (!match)
642 break;
Peng Tao875ae062014-05-29 21:06:57 +0800643 }
Trond Myklebust6f536932015-08-13 10:59:07 -0400644 return match;
Peng Tao875ae062014-05-29 21:06:57 +0800645}
646
647/*
648 * Lookup DS by addresses. nfs4_ds_cache_lock is held
649 */
650static struct nfs4_pnfs_ds *
651_data_server_lookup_locked(const struct list_head *dsaddrs)
652{
653 struct nfs4_pnfs_ds *ds;
654
655 list_for_each_entry(ds, &nfs4_data_server_cache, ds_node)
656 if (_same_data_server_addrs_locked(&ds->ds_addrs, dsaddrs))
657 return ds;
658 return NULL;
659}
660
661static void destroy_ds(struct nfs4_pnfs_ds *ds)
662{
663 struct nfs4_pnfs_ds_addr *da;
664
665 dprintk("--> %s\n", __func__);
666 ifdebug(FACILITY)
667 print_ds(ds);
668
669 nfs_put_client(ds->ds_clp);
670
671 while (!list_empty(&ds->ds_addrs)) {
672 da = list_first_entry(&ds->ds_addrs,
673 struct nfs4_pnfs_ds_addr,
674 da_node);
675 list_del_init(&da->da_node);
676 kfree(da->da_remotestr);
677 kfree(da);
678 }
679
680 kfree(ds->ds_remotestr);
681 kfree(ds);
682}
683
684void nfs4_pnfs_ds_put(struct nfs4_pnfs_ds *ds)
685{
Elena Reshetovaa2a5dea2017-10-20 12:53:31 +0300686 if (refcount_dec_and_lock(&ds->ds_count,
Peng Tao875ae062014-05-29 21:06:57 +0800687 &nfs4_ds_cache_lock)) {
688 list_del_init(&ds->ds_node);
689 spin_unlock(&nfs4_ds_cache_lock);
690 destroy_ds(ds);
691 }
692}
693EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_put);
694
695/*
696 * Create a string with a human readable address and port to avoid
697 * complicated setup around many dprinks.
698 */
699static char *
700nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags)
701{
702 struct nfs4_pnfs_ds_addr *da;
703 char *remotestr;
704 size_t len;
705 char *p;
706
707 len = 3; /* '{', '}' and eol */
708 list_for_each_entry(da, dsaddrs, da_node) {
709 len += strlen(da->da_remotestr) + 1; /* string plus comma */
710 }
711
712 remotestr = kzalloc(len, gfp_flags);
713 if (!remotestr)
714 return NULL;
715
716 p = remotestr;
717 *(p++) = '{';
718 len--;
719 list_for_each_entry(da, dsaddrs, da_node) {
720 size_t ll = strlen(da->da_remotestr);
721
722 if (ll > len)
723 goto out_err;
724
725 memcpy(p, da->da_remotestr, ll);
726 p += ll;
727 len -= ll;
728
729 if (len < 1)
730 goto out_err;
731 (*p++) = ',';
732 len--;
733 }
734 if (len < 2)
735 goto out_err;
736 *(p++) = '}';
737 *p = '\0';
738 return remotestr;
739out_err:
740 kfree(remotestr);
741 return NULL;
742}
743
744/*
745 * Given a list of multipath struct nfs4_pnfs_ds_addr, add it to ds cache if
746 * uncached and return cached struct nfs4_pnfs_ds.
747 */
748struct nfs4_pnfs_ds *
749nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags)
750{
751 struct nfs4_pnfs_ds *tmp_ds, *ds = NULL;
752 char *remotestr;
753
754 if (list_empty(dsaddrs)) {
755 dprintk("%s: no addresses defined\n", __func__);
756 goto out;
757 }
758
759 ds = kzalloc(sizeof(*ds), gfp_flags);
760 if (!ds)
761 goto out;
762
763 /* this is only used for debugging, so it's ok if its NULL */
764 remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags);
765
766 spin_lock(&nfs4_ds_cache_lock);
767 tmp_ds = _data_server_lookup_locked(dsaddrs);
768 if (tmp_ds == NULL) {
769 INIT_LIST_HEAD(&ds->ds_addrs);
770 list_splice_init(dsaddrs, &ds->ds_addrs);
771 ds->ds_remotestr = remotestr;
Elena Reshetovaa2a5dea2017-10-20 12:53:31 +0300772 refcount_set(&ds->ds_count, 1);
Peng Tao875ae062014-05-29 21:06:57 +0800773 INIT_LIST_HEAD(&ds->ds_node);
774 ds->ds_clp = NULL;
775 list_add(&ds->ds_node, &nfs4_data_server_cache);
776 dprintk("%s add new data server %s\n", __func__,
777 ds->ds_remotestr);
778 } else {
779 kfree(remotestr);
780 kfree(ds);
Elena Reshetovaa2a5dea2017-10-20 12:53:31 +0300781 refcount_inc(&tmp_ds->ds_count);
Peng Tao875ae062014-05-29 21:06:57 +0800782 dprintk("%s data server %s found, inc'ed ds_count to %d\n",
783 __func__, tmp_ds->ds_remotestr,
Elena Reshetovaa2a5dea2017-10-20 12:53:31 +0300784 refcount_read(&tmp_ds->ds_count));
Peng Tao875ae062014-05-29 21:06:57 +0800785 ds = tmp_ds;
786 }
787 spin_unlock(&nfs4_ds_cache_lock);
788out:
789 return ds;
790}
791EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_add);
Peng Tao6b7f3cf2014-05-29 21:06:59 +0800792
Peng Tao7405f9e2014-05-29 21:06:58 +0800793static void nfs4_wait_ds_connect(struct nfs4_pnfs_ds *ds)
794{
795 might_sleep();
796 wait_on_bit(&ds->ds_state, NFS4DS_CONNECTING,
797 TASK_KILLABLE);
798}
799
800static void nfs4_clear_ds_conn_bit(struct nfs4_pnfs_ds *ds)
801{
802 smp_mb__before_atomic();
803 clear_bit(NFS4DS_CONNECTING, &ds->ds_state);
804 smp_mb__after_atomic();
805 wake_up_bit(&ds->ds_state, NFS4DS_CONNECTING);
806}
807
Peng Tao5f01d952014-05-30 18:15:59 +0800808static struct nfs_client *(*get_v3_ds_connect)(
Tigran Mkrtchyanb224f7c2016-06-13 20:52:00 +0200809 struct nfs_server *mds_srv,
Peng Tao5f01d952014-05-30 18:15:59 +0800810 const struct sockaddr *ds_addr,
811 int ds_addrlen,
812 int ds_proto,
813 unsigned int ds_timeo,
Anna Schumaker7d38de32016-11-17 15:15:55 -0500814 unsigned int ds_retrans);
Peng Tao5f01d952014-05-30 18:15:59 +0800815
816static bool load_v3_ds_connect(void)
817{
818 if (!get_v3_ds_connect) {
819 get_v3_ds_connect = symbol_request(nfs3_set_ds_client);
820 WARN_ON_ONCE(!get_v3_ds_connect);
821 }
822
823 return(get_v3_ds_connect != NULL);
824}
825
Arnd Bergmanndf137bc2015-03-11 14:37:25 +0100826void nfs4_pnfs_v3_ds_connect_unload(void)
Peng Tao5f01d952014-05-30 18:15:59 +0800827{
828 if (get_v3_ds_connect) {
829 symbol_put(nfs3_set_ds_client);
830 get_v3_ds_connect = NULL;
831 }
832}
Peng Tao5f01d952014-05-30 18:15:59 +0800833
834static int _nfs4_pnfs_v3_ds_connect(struct nfs_server *mds_srv,
835 struct nfs4_pnfs_ds *ds,
836 unsigned int timeo,
Anna Schumaker7d38de32016-11-17 15:15:55 -0500837 unsigned int retrans)
Peng Tao5f01d952014-05-30 18:15:59 +0800838{
839 struct nfs_client *clp = ERR_PTR(-EIO);
840 struct nfs4_pnfs_ds_addr *da;
841 int status = 0;
842
Anna Schumaker7d38de32016-11-17 15:15:55 -0500843 dprintk("--> %s DS %s\n", __func__, ds->ds_remotestr);
Peng Tao5f01d952014-05-30 18:15:59 +0800844
845 if (!load_v3_ds_connect())
846 goto out;
847
848 list_for_each_entry(da, &ds->ds_addrs, da_node) {
849 dprintk("%s: DS %s: trying address %s\n",
850 __func__, ds->ds_remotestr, da->da_remotestr);
851
Trond Myklebustfc821d52016-01-31 14:53:08 -0500852 if (!IS_ERR(clp)) {
853 struct xprt_create xprt_args = {
854 .ident = XPRT_TRANSPORT_TCP,
855 .net = clp->cl_net,
856 .dstaddr = (struct sockaddr *)&da->da_addr,
857 .addrlen = da->da_addrlen,
858 .servername = clp->cl_hostname,
859 };
860 /* Add this address as an alias */
861 rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
862 rpc_clnt_test_and_add_xprt, NULL);
Trond Myklebustbf2bf9b2019-08-15 20:18:48 -0400863 continue;
864 }
865 clp = get_v3_ds_connect(mds_srv,
866 (struct sockaddr *)&da->da_addr,
867 da->da_addrlen, IPPROTO_TCP,
868 timeo, retrans);
869 if (IS_ERR(clp))
870 continue;
871 clp->cl_rpcclient->cl_softerr = 0;
872 clp->cl_rpcclient->cl_softrtry = 0;
Peng Tao5f01d952014-05-30 18:15:59 +0800873 }
874
875 if (IS_ERR(clp)) {
876 status = PTR_ERR(clp);
877 goto out;
878 }
879
880 smp_wmb();
881 ds->ds_clp = clp;
882 dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
883out:
884 return status;
885}
886
887static int _nfs4_pnfs_v4_ds_connect(struct nfs_server *mds_srv,
Peng Tao7405f9e2014-05-29 21:06:58 +0800888 struct nfs4_pnfs_ds *ds,
889 unsigned int timeo,
Peng Tao064172f2014-05-29 21:07:00 +0800890 unsigned int retrans,
Anna Schumaker7d38de32016-11-17 15:15:55 -0500891 u32 minor_version)
Peng Tao7405f9e2014-05-29 21:06:58 +0800892{
893 struct nfs_client *clp = ERR_PTR(-EIO);
894 struct nfs4_pnfs_ds_addr *da;
895 int status = 0;
896
Anna Schumaker7d38de32016-11-17 15:15:55 -0500897 dprintk("--> %s DS %s\n", __func__, ds->ds_remotestr);
Peng Tao7405f9e2014-05-29 21:06:58 +0800898
899 list_for_each_entry(da, &ds->ds_addrs, da_node) {
900 dprintk("%s: DS %s: trying address %s\n",
901 __func__, ds->ds_remotestr, da->da_remotestr);
902
Andy Adamson04fa2c62016-09-09 09:22:29 -0400903 if (!IS_ERR(clp) && clp->cl_mvops->session_trunk) {
904 struct xprt_create xprt_args = {
905 .ident = XPRT_TRANSPORT_TCP,
906 .net = clp->cl_net,
907 .dstaddr = (struct sockaddr *)&da->da_addr,
908 .addrlen = da->da_addrlen,
909 .servername = clp->cl_hostname,
910 };
911 struct nfs4_add_xprt_data xprtdata = {
912 .clp = clp,
913 .cred = nfs4_get_clid_cred(clp),
914 };
915 struct rpc_add_xprt_test rpcdata = {
916 .add_xprt_test = clp->cl_mvops->session_trunk,
917 .data = &xprtdata,
918 };
919
920 /**
921 * Test this address for session trunking and
922 * add as an alias
923 */
924 rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
925 rpc_clnt_setup_test_and_add_xprt,
926 &rpcdata);
927 if (xprtdata.cred)
NeilBrowna52458b2018-12-03 11:30:31 +1100928 put_cred(xprtdata.cred);
Andy Adamson04fa2c62016-09-09 09:22:29 -0400929 } else {
930 clp = nfs4_set_ds_client(mds_srv,
931 (struct sockaddr *)&da->da_addr,
932 da->da_addrlen, IPPROTO_TCP,
Anna Schumaker7d38de32016-11-17 15:15:55 -0500933 timeo, retrans, minor_version);
Andy Adamson04fa2c62016-09-09 09:22:29 -0400934 if (IS_ERR(clp))
935 continue;
936
937 status = nfs4_init_ds_session(clp,
938 mds_srv->nfs_client->cl_lease_time);
939 if (status) {
940 nfs_put_client(clp);
941 clp = ERR_PTR(-EIO);
942 continue;
943 }
944
945 }
Peng Tao7405f9e2014-05-29 21:06:58 +0800946 }
947
948 if (IS_ERR(clp)) {
949 status = PTR_ERR(clp);
950 goto out;
951 }
952
Peng Tao7405f9e2014-05-29 21:06:58 +0800953 smp_wmb();
954 ds->ds_clp = clp;
955 dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
956out:
957 return status;
Peng Tao7405f9e2014-05-29 21:06:58 +0800958}
959
960/*
961 * Create an rpc connection to the nfs4_pnfs_ds data server.
962 * Currently only supports IPv4 and IPv6 addresses.
Weston Andros Adamsona33e4b02017-03-09 12:56:48 -0500963 * If connection fails, make devid unavailable and return a -errno.
Peng Tao7405f9e2014-05-29 21:06:58 +0800964 */
Weston Andros Adamsona33e4b02017-03-09 12:56:48 -0500965int nfs4_pnfs_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds,
Peng Tao7405f9e2014-05-29 21:06:58 +0800966 struct nfs4_deviceid_node *devid, unsigned int timeo,
Anna Schumaker7d38de32016-11-17 15:15:55 -0500967 unsigned int retrans, u32 version, u32 minor_version)
Peng Tao7405f9e2014-05-29 21:06:58 +0800968{
Weston Andros Adamsonda066f32017-03-09 12:56:49 -0500969 int err;
Peng Tao7405f9e2014-05-29 21:06:58 +0800970
Weston Andros Adamsonda066f32017-03-09 12:56:49 -0500971again:
972 err = 0;
973 if (test_and_set_bit(NFS4DS_CONNECTING, &ds->ds_state) == 0) {
Peng Tao5f01d952014-05-30 18:15:59 +0800974 if (version == 3) {
975 err = _nfs4_pnfs_v3_ds_connect(mds_srv, ds, timeo,
Anna Schumaker7d38de32016-11-17 15:15:55 -0500976 retrans);
Peng Tao5f01d952014-05-30 18:15:59 +0800977 } else if (version == 4) {
978 err = _nfs4_pnfs_v4_ds_connect(mds_srv, ds, timeo,
Anna Schumaker7d38de32016-11-17 15:15:55 -0500979 retrans, minor_version);
Peng Tao5f01d952014-05-30 18:15:59 +0800980 } else {
981 dprintk("%s: unsupported DS version %d\n", __func__,
982 version);
983 err = -EPROTONOSUPPORT;
984 }
985
Peng Tao7405f9e2014-05-29 21:06:58 +0800986 nfs4_clear_ds_conn_bit(ds);
987 } else {
988 nfs4_wait_ds_connect(ds);
Weston Andros Adamsonda066f32017-03-09 12:56:49 -0500989
990 /* what was waited on didn't connect AND didn't mark unavail */
991 if (!ds->ds_clp && !nfs4_test_deviceid_unavailable(devid))
992 goto again;
Peng Tao7405f9e2014-05-29 21:06:58 +0800993 }
Weston Andros Adamsona33e4b02017-03-09 12:56:48 -0500994
995 /*
996 * At this point the ds->ds_clp should be ready, but it might have
997 * hit an error.
998 */
Weston Andros Adamsonda066f32017-03-09 12:56:49 -0500999 if (!err) {
1000 if (!ds->ds_clp || !nfs_client_init_is_complete(ds->ds_clp)) {
1001 WARN_ON_ONCE(ds->ds_clp ||
1002 !nfs4_test_deviceid_unavailable(devid));
1003 return -EINVAL;
1004 }
1005 err = nfs_client_init_status(ds->ds_clp);
Weston Andros Adamsona33e4b02017-03-09 12:56:48 -05001006 }
1007
Weston Andros Adamsonda066f32017-03-09 12:56:49 -05001008 return err;
Peng Tao7405f9e2014-05-29 21:06:58 +08001009}
1010EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_connect);
1011
Peng Tao6b7f3cf2014-05-29 21:06:59 +08001012/*
1013 * Currently only supports ipv4, ipv6 and one multi-path address.
1014 */
1015struct nfs4_pnfs_ds_addr *
1016nfs4_decode_mp_ds_addr(struct net *net, struct xdr_stream *xdr, gfp_t gfp_flags)
1017{
1018 struct nfs4_pnfs_ds_addr *da = NULL;
1019 char *buf, *portstr;
1020 __be16 port;
1021 int nlen, rlen;
1022 int tmp[2];
1023 __be32 *p;
1024 char *netid, *match_netid;
1025 size_t len, match_netid_len;
1026 char *startsep = "";
1027 char *endsep = "";
1028
1029
1030 /* r_netid */
1031 p = xdr_inline_decode(xdr, 4);
1032 if (unlikely(!p))
1033 goto out_err;
1034 nlen = be32_to_cpup(p++);
1035
1036 p = xdr_inline_decode(xdr, nlen);
1037 if (unlikely(!p))
1038 goto out_err;
1039
1040 netid = kmalloc(nlen+1, gfp_flags);
1041 if (unlikely(!netid))
1042 goto out_err;
1043
1044 netid[nlen] = '\0';
1045 memcpy(netid, p, nlen);
1046
1047 /* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */
1048 p = xdr_inline_decode(xdr, 4);
1049 if (unlikely(!p))
1050 goto out_free_netid;
1051 rlen = be32_to_cpup(p);
1052
1053 p = xdr_inline_decode(xdr, rlen);
1054 if (unlikely(!p))
1055 goto out_free_netid;
1056
1057 /* port is ".ABC.DEF", 8 chars max */
1058 if (rlen > INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 8) {
1059 dprintk("%s: Invalid address, length %d\n", __func__,
1060 rlen);
1061 goto out_free_netid;
1062 }
1063 buf = kmalloc(rlen + 1, gfp_flags);
1064 if (!buf) {
1065 dprintk("%s: Not enough memory\n", __func__);
1066 goto out_free_netid;
1067 }
1068 buf[rlen] = '\0';
1069 memcpy(buf, p, rlen);
1070
1071 /* replace port '.' with '-' */
1072 portstr = strrchr(buf, '.');
1073 if (!portstr) {
1074 dprintk("%s: Failed finding expected dot in port\n",
1075 __func__);
1076 goto out_free_buf;
1077 }
1078 *portstr = '-';
1079
1080 /* find '.' between address and port */
1081 portstr = strrchr(buf, '.');
1082 if (!portstr) {
1083 dprintk("%s: Failed finding expected dot between address and "
1084 "port\n", __func__);
1085 goto out_free_buf;
1086 }
1087 *portstr = '\0';
1088
1089 da = kzalloc(sizeof(*da), gfp_flags);
1090 if (unlikely(!da))
1091 goto out_free_buf;
1092
1093 INIT_LIST_HEAD(&da->da_node);
1094
1095 if (!rpc_pton(net, buf, portstr-buf, (struct sockaddr *)&da->da_addr,
1096 sizeof(da->da_addr))) {
1097 dprintk("%s: error parsing address %s\n", __func__, buf);
1098 goto out_free_da;
1099 }
1100
1101 portstr++;
1102 sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]);
1103 port = htons((tmp[0] << 8) | (tmp[1]));
1104
1105 switch (da->da_addr.ss_family) {
1106 case AF_INET:
1107 ((struct sockaddr_in *)&da->da_addr)->sin_port = port;
1108 da->da_addrlen = sizeof(struct sockaddr_in);
1109 match_netid = "tcp";
1110 match_netid_len = 3;
1111 break;
1112
1113 case AF_INET6:
1114 ((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port;
1115 da->da_addrlen = sizeof(struct sockaddr_in6);
1116 match_netid = "tcp6";
1117 match_netid_len = 4;
1118 startsep = "[";
1119 endsep = "]";
1120 break;
1121
1122 default:
1123 dprintk("%s: unsupported address family: %u\n",
1124 __func__, da->da_addr.ss_family);
1125 goto out_free_da;
1126 }
1127
1128 if (nlen != match_netid_len || strncmp(netid, match_netid, nlen)) {
1129 dprintk("%s: ERROR: r_netid \"%s\" != \"%s\"\n",
1130 __func__, netid, match_netid);
1131 goto out_free_da;
1132 }
1133
1134 /* save human readable address */
1135 len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7;
1136 da->da_remotestr = kzalloc(len, gfp_flags);
1137
1138 /* NULL is ok, only used for dprintk */
1139 if (da->da_remotestr)
1140 snprintf(da->da_remotestr, len, "%s%s%s:%u", startsep,
1141 buf, endsep, ntohs(port));
1142
1143 dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr);
1144 kfree(buf);
1145 kfree(netid);
1146 return da;
1147
1148out_free_da:
1149 kfree(da);
1150out_free_buf:
1151 dprintk("%s: Error parsing DS addr: %s\n", __func__, buf);
1152 kfree(buf);
1153out_free_netid:
1154 kfree(netid);
1155out_err:
1156 return NULL;
1157}
1158EXPORT_SYMBOL_GPL(nfs4_decode_mp_ds_addr);
Tom Haynes338d00c2015-02-17 14:58:15 -08001159
1160void
1161pnfs_layout_mark_request_commit(struct nfs_page *req,
1162 struct pnfs_layout_segment *lseg,
1163 struct nfs_commit_info *cinfo,
1164 u32 ds_commit_idx)
1165{
1166 struct list_head *list;
Trond Myklebustba827c92020-03-20 18:34:33 -04001167 struct pnfs_commit_array *array;
Tom Haynes338d00c2015-02-17 14:58:15 -08001168 struct pnfs_commit_bucket *buckets;
1169
Trond Myklebuste824f992017-08-01 11:53:49 -04001170 mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
Trond Myklebustba827c92020-03-20 18:34:33 -04001171 array = pnfs_lookup_commit_array(cinfo->ds, lseg);
1172 if (!array)
1173 goto out_resched;
1174 buckets = array->buckets;
Tom Haynes338d00c2015-02-17 14:58:15 -08001175 list = &buckets[ds_commit_idx].written;
1176 if (list_empty(list)) {
Trond Myklebustba827c92020-03-20 18:34:33 -04001177 if (!pnfs_is_valid_lseg(lseg))
1178 goto out_resched;
Tom Haynes338d00c2015-02-17 14:58:15 -08001179 /* Non-empty buckets hold a reference on the lseg. That ref
1180 * is normally transferred to the COMMIT call and released
1181 * there. It could also be released if the last req is pulled
1182 * off due to a rewrite, in which case it will be done in
1183 * pnfs_common_clear_request_commit
1184 */
1185 WARN_ON_ONCE(buckets[ds_commit_idx].wlseg != NULL);
1186 buckets[ds_commit_idx].wlseg = pnfs_get_lseg(lseg);
1187 }
1188 set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
1189 cinfo->ds->nwritten++;
Tom Haynes338d00c2015-02-17 14:58:15 -08001190
Trond Myklebust86d80f92015-07-31 16:24:30 -04001191 nfs_request_add_commit_list_locked(req, list, cinfo);
Trond Myklebuste824f992017-08-01 11:53:49 -04001192 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
Trond Myklebust86d80f92015-07-31 16:24:30 -04001193 nfs_mark_page_unstable(req->wb_page, cinfo);
Trond Myklebustba827c92020-03-20 18:34:33 -04001194 return;
1195out_resched:
1196 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
1197 cinfo->completion_ops->resched_write(cinfo, req);
Tom Haynes338d00c2015-02-17 14:58:15 -08001198}
1199EXPORT_SYMBOL_GPL(pnfs_layout_mark_request_commit);
Trond Myklebust5bb89b42015-03-25 14:14:42 -04001200
1201int
1202pnfs_nfs_generic_sync(struct inode *inode, bool datasync)
1203{
Trond Myklebust2e18d4d2016-06-26 18:54:58 -04001204 int ret;
1205
1206 if (!pnfs_layoutcommit_outstanding(inode))
1207 return 0;
1208 ret = nfs_commit_inode(inode, FLUSH_SYNC);
1209 if (ret < 0)
1210 return ret;
Trond Myklebust5bb89b42015-03-25 14:14:42 -04001211 if (datasync)
1212 return 0;
1213 return pnfs_layoutcommit_inode(inode, true);
1214}
1215EXPORT_SYMBOL_GPL(pnfs_nfs_generic_sync);
1216