blob: d40755a0984bbb0942e96aee388ed50fe7629a6e [file] [log] [blame]
Tom Haynesf54bcf22014-12-11 15:34:59 -05001/*
2 * Common NFS I/O operations for the pnfs file based
3 * layout drivers.
4 *
5 * Copyright (c) 2014, Primary Data, Inc. All rights reserved.
6 *
7 * Tom Haynes <loghyr@primarydata.com>
8 */
9
10#include <linux/nfs_fs.h>
11#include <linux/nfs_page.h>
Peng Tao6b7f3cf2014-05-29 21:06:59 +080012#include <linux/sunrpc/addr.h>
Peng Tao5f01d952014-05-30 18:15:59 +080013#include <linux/module.h>
Tom Haynesf54bcf22014-12-11 15:34:59 -050014
Peng Tao7405f9e2014-05-29 21:06:58 +080015#include "nfs4session.h"
Tom Haynesf54bcf22014-12-11 15:34:59 -050016#include "internal.h"
17#include "pnfs.h"
18
Peng Tao875ae062014-05-29 21:06:57 +080019#define NFSDBG_FACILITY NFSDBG_PNFS
20
Tom Haynesf54bcf22014-12-11 15:34:59 -050021void pnfs_generic_rw_release(void *data)
22{
23 struct nfs_pgio_header *hdr = data;
Tom Haynesf54bcf22014-12-11 15:34:59 -050024
Tom Haynesf54bcf22014-12-11 15:34:59 -050025 nfs_put_client(hdr->ds_clp);
26 hdr->mds_ops->rpc_release(data);
27}
28EXPORT_SYMBOL_GPL(pnfs_generic_rw_release);
29
30/* Fake up some data that will cause nfs_commit_release to retry the writes. */
31void pnfs_generic_prepare_to_resend_writes(struct nfs_commit_data *data)
32{
33 struct nfs_page *first = nfs_list_entry(data->pages.next);
34
35 data->task.tk_status = 0;
36 memcpy(&data->verf.verifier, &first->wb_verf,
37 sizeof(data->verf.verifier));
38 data->verf.verifier.data[0]++; /* ensure verifier mismatch */
39}
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.
Dave Wysochanskife238e62016-04-01 13:45:09 -040064 * Note this must be called holding i_lock
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);
86 pnfs_put_lseg_locked(freeme);
87}
88EXPORT_SYMBOL_GPL(pnfs_generic_clear_request_commit);
89
90static int
91pnfs_generic_transfer_commit_list(struct list_head *src, struct list_head *dst,
92 struct nfs_commit_info *cinfo, int max)
93{
94 struct nfs_page *req, *tmp;
95 int ret = 0;
96
97 list_for_each_entry_safe(req, tmp, src, wb_list) {
98 if (!nfs_lock_request(req))
99 continue;
100 kref_get(&req->wb_kref);
Dave Wysochanskife238e62016-04-01 13:45:09 -0400101 if (cond_resched_lock(&cinfo->inode->i_lock))
Tom Haynesf54bcf22014-12-11 15:34:59 -0500102 list_safe_reset_next(req, tmp, wb_list);
103 nfs_request_remove_commit_list(req, cinfo);
104 clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
105 nfs_list_add_request(req, dst);
106 ret++;
107 if ((ret == max) && !cinfo->dreq)
108 break;
109 }
110 return ret;
111}
112
Tom Haynesf54bcf22014-12-11 15:34:59 -0500113static int
114pnfs_generic_scan_ds_commit_list(struct pnfs_commit_bucket *bucket,
115 struct nfs_commit_info *cinfo,
116 int max)
117{
118 struct list_head *src = &bucket->written;
119 struct list_head *dst = &bucket->committing;
120 int ret;
121
Dave Wysochanskife238e62016-04-01 13:45:09 -0400122 lockdep_assert_held(&cinfo->inode->i_lock);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500123 ret = pnfs_generic_transfer_commit_list(src, dst, cinfo, max);
124 if (ret) {
125 cinfo->ds->nwritten -= ret;
126 cinfo->ds->ncommitting += ret;
Trond Myklebust27571292015-08-03 17:38:33 -0400127 if (bucket->clseg == NULL)
128 bucket->clseg = pnfs_get_lseg(bucket->wlseg);
129 if (list_empty(src)) {
130 pnfs_put_lseg_locked(bucket->wlseg);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500131 bucket->wlseg = NULL;
Trond Myklebust27571292015-08-03 17:38:33 -0400132 }
Tom Haynesf54bcf22014-12-11 15:34:59 -0500133 }
134 return ret;
135}
136
Tom Haynes085d1e32014-12-11 13:04:55 -0500137/* Move reqs from written to committing lists, returning count
138 * of number moved.
Tom Haynesf54bcf22014-12-11 15:34:59 -0500139 */
140int pnfs_generic_scan_commit_lists(struct nfs_commit_info *cinfo,
141 int max)
142{
143 int i, rv = 0, cnt;
144
Dave Wysochanskife238e62016-04-01 13:45:09 -0400145 lockdep_assert_held(&cinfo->inode->i_lock);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500146 for (i = 0; i < cinfo->ds->nbuckets && max != 0; i++) {
147 cnt = pnfs_generic_scan_ds_commit_list(&cinfo->ds->buckets[i],
148 cinfo, max);
149 max -= cnt;
150 rv += cnt;
151 }
152 return rv;
153}
154EXPORT_SYMBOL_GPL(pnfs_generic_scan_commit_lists);
155
Tom Haynes085d1e32014-12-11 13:04:55 -0500156/* Pull everything off the committing lists and dump into @dst. */
Tom Haynesf54bcf22014-12-11 15:34:59 -0500157void pnfs_generic_recover_commit_reqs(struct list_head *dst,
158 struct nfs_commit_info *cinfo)
159{
160 struct pnfs_commit_bucket *b;
161 struct pnfs_layout_segment *freeme;
162 int i;
163
Dave Wysochanskife238e62016-04-01 13:45:09 -0400164 lockdep_assert_held(&cinfo->inode->i_lock);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500165restart:
Tom Haynesf54bcf22014-12-11 15:34:59 -0500166 for (i = 0, b = cinfo->ds->buckets; i < cinfo->ds->nbuckets; i++, b++) {
167 if (pnfs_generic_transfer_commit_list(&b->written, dst,
168 cinfo, 0)) {
169 freeme = b->wlseg;
170 b->wlseg = NULL;
Dave Wysochanskife238e62016-04-01 13:45:09 -0400171 spin_unlock(&cinfo->inode->i_lock);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500172 pnfs_put_lseg(freeme);
Dave Wysochanskife238e62016-04-01 13:45:09 -0400173 spin_lock(&cinfo->inode->i_lock);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500174 goto restart;
175 }
176 }
177 cinfo->ds->nwritten = 0;
Tom Haynesf54bcf22014-12-11 15:34:59 -0500178}
179EXPORT_SYMBOL_GPL(pnfs_generic_recover_commit_reqs);
180
181static void pnfs_generic_retry_commit(struct nfs_commit_info *cinfo, int idx)
182{
183 struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds;
184 struct pnfs_commit_bucket *bucket;
185 struct pnfs_layout_segment *freeme;
Trond Myklebust27571292015-08-03 17:38:33 -0400186 LIST_HEAD(pages);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500187 int i;
188
Dave Wysochanskife238e62016-04-01 13:45:09 -0400189 spin_lock(&cinfo->inode->i_lock);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500190 for (i = idx; i < fl_cinfo->nbuckets; i++) {
191 bucket = &fl_cinfo->buckets[i];
192 if (list_empty(&bucket->committing))
193 continue;
Tom Haynesf54bcf22014-12-11 15:34:59 -0500194 freeme = bucket->clseg;
195 bucket->clseg = NULL;
Trond Myklebust27571292015-08-03 17:38:33 -0400196 list_splice_init(&bucket->committing, &pages);
Dave Wysochanskife238e62016-04-01 13:45:09 -0400197 spin_unlock(&cinfo->inode->i_lock);
Trond Myklebust27571292015-08-03 17:38:33 -0400198 nfs_retry_commit(&pages, freeme, cinfo, i);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500199 pnfs_put_lseg(freeme);
Dave Wysochanskife238e62016-04-01 13:45:09 -0400200 spin_lock(&cinfo->inode->i_lock);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500201 }
Dave Wysochanskife238e62016-04-01 13:45:09 -0400202 spin_unlock(&cinfo->inode->i_lock);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500203}
204
205static unsigned int
206pnfs_generic_alloc_ds_commits(struct nfs_commit_info *cinfo,
207 struct list_head *list)
208{
209 struct pnfs_ds_commit_info *fl_cinfo;
210 struct pnfs_commit_bucket *bucket;
211 struct nfs_commit_data *data;
212 int i;
213 unsigned int nreq = 0;
214
215 fl_cinfo = cinfo->ds;
216 bucket = fl_cinfo->buckets;
217 for (i = 0; i < fl_cinfo->nbuckets; i++, bucket++) {
218 if (list_empty(&bucket->committing))
219 continue;
Trond Myklebust722f0b82017-04-29 10:27:18 -0400220 /*
221 * If the layout segment is invalid, then let
222 * pnfs_generic_retry_commit() clean up the bucket.
223 */
Fred Isamanc296cfe2017-05-02 16:53:36 -0400224 if (bucket->clseg && !pnfs_is_valid_lseg(bucket->clseg) &&
Trond Myklebust722f0b82017-04-29 10:27:18 -0400225 !test_bit(NFS_LSEG_LAYOUTRETURN, &bucket->clseg->pls_flags))
Trond Myklebust5f011482017-05-01 17:35:58 -0400226 break;
NeilBrown518662e2017-04-10 12:22:09 +1000227 data = nfs_commitdata_alloc(false);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500228 if (!data)
229 break;
230 data->ds_commit_index = i;
Tom Haynesf54bcf22014-12-11 15:34:59 -0500231 list_add(&data->pages, list);
232 nreq++;
233 }
234
235 /* Clean up on error */
236 pnfs_generic_retry_commit(cinfo, i);
237 return nreq;
238}
239
Trond Myklebust27571292015-08-03 17:38:33 -0400240static inline
241void pnfs_fetch_commit_bucket_list(struct list_head *pages,
242 struct nfs_commit_data *data,
243 struct nfs_commit_info *cinfo)
244{
245 struct pnfs_commit_bucket *bucket;
246
247 bucket = &cinfo->ds->buckets[data->ds_commit_index];
Dave Wysochanskife238e62016-04-01 13:45:09 -0400248 spin_lock(&cinfo->inode->i_lock);
Peng Tao046be742015-08-21 10:32:50 +0800249 list_splice_init(&bucket->committing, pages);
Trond Myklebust27571292015-08-03 17:38:33 -0400250 data->lseg = bucket->clseg;
251 bucket->clseg = NULL;
Dave Wysochanskife238e62016-04-01 13:45:09 -0400252 spin_unlock(&cinfo->inode->i_lock);
Trond Myklebust27571292015-08-03 17:38:33 -0400253
254}
255
Weston Andros Adamsonade8feb2016-05-25 10:07:23 -0400256/* Helper function for pnfs_generic_commit_pagelist to catch an empty
Weston Andros Adamson5e3a9882016-06-17 16:48:24 -0400257 * page list. This can happen when two commits race.
258 *
259 * This must be called instead of nfs_init_commit - call one or the other, but
260 * not both!
261 */
Weston Andros Adamsonade8feb2016-05-25 10:07:23 -0400262static bool
263pnfs_generic_commit_cancel_empty_pagelist(struct list_head *pages,
264 struct nfs_commit_data *data,
265 struct nfs_commit_info *cinfo)
266{
267 if (list_empty(pages)) {
268 if (atomic_dec_and_test(&cinfo->mds->rpcs_out))
269 wake_up_atomic_t(&cinfo->mds->rpcs_out);
Weston Andros Adamson5e3a9882016-06-17 16:48:24 -0400270 /* don't call nfs_commitdata_release - it tries to put
271 * the open_context which is not acquired until nfs_init_commit
272 * which has not been called on @data */
273 WARN_ON_ONCE(data->context);
274 nfs_commit_free(data);
Weston Andros Adamsonade8feb2016-05-25 10:07:23 -0400275 return true;
276 }
277
278 return false;
279}
280
Tom Haynesf54bcf22014-12-11 15:34:59 -0500281/* This follows nfs_commit_list pretty closely */
282int
283pnfs_generic_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
284 int how, struct nfs_commit_info *cinfo,
285 int (*initiate_commit)(struct nfs_commit_data *data,
286 int how))
287{
288 struct nfs_commit_data *data, *tmp;
289 LIST_HEAD(list);
290 unsigned int nreq = 0;
291
292 if (!list_empty(mds_pages)) {
NeilBrown518662e2017-04-10 12:22:09 +1000293 data = nfs_commitdata_alloc(true);
294 data->ds_commit_index = -1;
295 list_add(&data->pages, &list);
296 nreq++;
Tom Haynesf54bcf22014-12-11 15:34:59 -0500297 }
298
299 nreq += pnfs_generic_alloc_ds_commits(cinfo, &list);
300
Trond Myklebustaf7cf052015-09-29 20:34:05 -0400301 if (nreq == 0)
Tom Haynesf54bcf22014-12-11 15:34:59 -0500302 goto out;
Tom Haynesf54bcf22014-12-11 15:34:59 -0500303
304 atomic_add(nreq, &cinfo->mds->rpcs_out);
305
306 list_for_each_entry_safe(data, tmp, &list, pages) {
307 list_del_init(&data->pages);
Trond Myklebust27571292015-08-03 17:38:33 -0400308 if (data->ds_commit_index < 0) {
Weston Andros Adamsonade8feb2016-05-25 10:07:23 -0400309 /* another commit raced with us */
310 if (pnfs_generic_commit_cancel_empty_pagelist(mds_pages,
311 data, cinfo))
312 continue;
313
Tom Haynesf54bcf22014-12-11 15:34:59 -0500314 nfs_init_commit(data, mds_pages, NULL, cinfo);
315 nfs_initiate_commit(NFS_CLIENT(inode), data,
Peng Taoc36aae92014-06-09 07:10:14 +0800316 NFS_PROTO(data->inode),
Tom Haynesf54bcf22014-12-11 15:34:59 -0500317 data->mds_ops, how, 0);
318 } else {
Trond Myklebust27571292015-08-03 17:38:33 -0400319 LIST_HEAD(pages);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500320
Trond Myklebust27571292015-08-03 17:38:33 -0400321 pnfs_fetch_commit_bucket_list(&pages, data, cinfo);
Weston Andros Adamsonade8feb2016-05-25 10:07:23 -0400322
323 /* another commit raced with us */
324 if (pnfs_generic_commit_cancel_empty_pagelist(&pages,
325 data, cinfo))
326 continue;
327
Trond Myklebust27571292015-08-03 17:38:33 -0400328 nfs_init_commit(data, &pages, data->lseg, cinfo);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500329 initiate_commit(data, how);
330 }
331 }
332out:
333 cinfo->ds->ncommitting = 0;
334 return PNFS_ATTEMPTED;
335}
336EXPORT_SYMBOL_GPL(pnfs_generic_commit_pagelist);
Peng Tao875ae062014-05-29 21:06:57 +0800337
338/*
339 * Data server cache
340 *
341 * Data servers can be mapped to different device ids.
342 * nfs4_pnfs_ds reference counting
343 * - set to 1 on allocation
344 * - incremented when a device id maps a data server already in the cache.
345 * - decremented when deviceid is removed from the cache.
346 */
347static DEFINE_SPINLOCK(nfs4_ds_cache_lock);
348static LIST_HEAD(nfs4_data_server_cache);
349
350/* Debug routines */
351static void
352print_ds(struct nfs4_pnfs_ds *ds)
353{
354 if (ds == NULL) {
355 printk(KERN_WARNING "%s NULL device\n", __func__);
356 return;
357 }
358 printk(KERN_WARNING " ds %s\n"
359 " ref count %d\n"
360 " client %p\n"
361 " cl_exchange_flags %x\n",
362 ds->ds_remotestr,
363 atomic_read(&ds->ds_count), ds->ds_clp,
364 ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0);
365}
366
367static bool
368same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2)
369{
370 struct sockaddr_in *a, *b;
371 struct sockaddr_in6 *a6, *b6;
372
373 if (addr1->sa_family != addr2->sa_family)
374 return false;
375
376 switch (addr1->sa_family) {
377 case AF_INET:
378 a = (struct sockaddr_in *)addr1;
379 b = (struct sockaddr_in *)addr2;
380
381 if (a->sin_addr.s_addr == b->sin_addr.s_addr &&
382 a->sin_port == b->sin_port)
383 return true;
384 break;
385
386 case AF_INET6:
387 a6 = (struct sockaddr_in6 *)addr1;
388 b6 = (struct sockaddr_in6 *)addr2;
389
390 /* LINKLOCAL addresses must have matching scope_id */
391 if (ipv6_addr_src_scope(&a6->sin6_addr) ==
392 IPV6_ADDR_SCOPE_LINKLOCAL &&
393 a6->sin6_scope_id != b6->sin6_scope_id)
394 return false;
395
396 if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) &&
397 a6->sin6_port == b6->sin6_port)
398 return true;
399 break;
400
401 default:
402 dprintk("%s: unhandled address family: %u\n",
403 __func__, addr1->sa_family);
404 return false;
405 }
406
407 return false;
408}
409
Trond Myklebust6f536932015-08-13 10:59:07 -0400410/*
411 * Checks if 'dsaddrs1' contains a subset of 'dsaddrs2'. If it does,
412 * declare a match.
413 */
Peng Tao875ae062014-05-29 21:06:57 +0800414static bool
415_same_data_server_addrs_locked(const struct list_head *dsaddrs1,
416 const struct list_head *dsaddrs2)
417{
418 struct nfs4_pnfs_ds_addr *da1, *da2;
Trond Myklebust6f536932015-08-13 10:59:07 -0400419 struct sockaddr *sa1, *sa2;
420 bool match = false;
Peng Tao875ae062014-05-29 21:06:57 +0800421
Trond Myklebust6f536932015-08-13 10:59:07 -0400422 list_for_each_entry(da1, dsaddrs1, da_node) {
423 sa1 = (struct sockaddr *)&da1->da_addr;
424 match = false;
425 list_for_each_entry(da2, dsaddrs2, da_node) {
426 sa2 = (struct sockaddr *)&da2->da_addr;
427 match = same_sockaddr(sa1, sa2);
428 if (match)
429 break;
430 }
431 if (!match)
432 break;
Peng Tao875ae062014-05-29 21:06:57 +0800433 }
Trond Myklebust6f536932015-08-13 10:59:07 -0400434 return match;
Peng Tao875ae062014-05-29 21:06:57 +0800435}
436
437/*
438 * Lookup DS by addresses. nfs4_ds_cache_lock is held
439 */
440static struct nfs4_pnfs_ds *
441_data_server_lookup_locked(const struct list_head *dsaddrs)
442{
443 struct nfs4_pnfs_ds *ds;
444
445 list_for_each_entry(ds, &nfs4_data_server_cache, ds_node)
446 if (_same_data_server_addrs_locked(&ds->ds_addrs, dsaddrs))
447 return ds;
448 return NULL;
449}
450
451static void destroy_ds(struct nfs4_pnfs_ds *ds)
452{
453 struct nfs4_pnfs_ds_addr *da;
454
455 dprintk("--> %s\n", __func__);
456 ifdebug(FACILITY)
457 print_ds(ds);
458
459 nfs_put_client(ds->ds_clp);
460
461 while (!list_empty(&ds->ds_addrs)) {
462 da = list_first_entry(&ds->ds_addrs,
463 struct nfs4_pnfs_ds_addr,
464 da_node);
465 list_del_init(&da->da_node);
466 kfree(da->da_remotestr);
467 kfree(da);
468 }
469
470 kfree(ds->ds_remotestr);
471 kfree(ds);
472}
473
474void nfs4_pnfs_ds_put(struct nfs4_pnfs_ds *ds)
475{
476 if (atomic_dec_and_lock(&ds->ds_count,
477 &nfs4_ds_cache_lock)) {
478 list_del_init(&ds->ds_node);
479 spin_unlock(&nfs4_ds_cache_lock);
480 destroy_ds(ds);
481 }
482}
483EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_put);
484
485/*
486 * Create a string with a human readable address and port to avoid
487 * complicated setup around many dprinks.
488 */
489static char *
490nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags)
491{
492 struct nfs4_pnfs_ds_addr *da;
493 char *remotestr;
494 size_t len;
495 char *p;
496
497 len = 3; /* '{', '}' and eol */
498 list_for_each_entry(da, dsaddrs, da_node) {
499 len += strlen(da->da_remotestr) + 1; /* string plus comma */
500 }
501
502 remotestr = kzalloc(len, gfp_flags);
503 if (!remotestr)
504 return NULL;
505
506 p = remotestr;
507 *(p++) = '{';
508 len--;
509 list_for_each_entry(da, dsaddrs, da_node) {
510 size_t ll = strlen(da->da_remotestr);
511
512 if (ll > len)
513 goto out_err;
514
515 memcpy(p, da->da_remotestr, ll);
516 p += ll;
517 len -= ll;
518
519 if (len < 1)
520 goto out_err;
521 (*p++) = ',';
522 len--;
523 }
524 if (len < 2)
525 goto out_err;
526 *(p++) = '}';
527 *p = '\0';
528 return remotestr;
529out_err:
530 kfree(remotestr);
531 return NULL;
532}
533
534/*
535 * Given a list of multipath struct nfs4_pnfs_ds_addr, add it to ds cache if
536 * uncached and return cached struct nfs4_pnfs_ds.
537 */
538struct nfs4_pnfs_ds *
539nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags)
540{
541 struct nfs4_pnfs_ds *tmp_ds, *ds = NULL;
542 char *remotestr;
543
544 if (list_empty(dsaddrs)) {
545 dprintk("%s: no addresses defined\n", __func__);
546 goto out;
547 }
548
549 ds = kzalloc(sizeof(*ds), gfp_flags);
550 if (!ds)
551 goto out;
552
553 /* this is only used for debugging, so it's ok if its NULL */
554 remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags);
555
556 spin_lock(&nfs4_ds_cache_lock);
557 tmp_ds = _data_server_lookup_locked(dsaddrs);
558 if (tmp_ds == NULL) {
559 INIT_LIST_HEAD(&ds->ds_addrs);
560 list_splice_init(dsaddrs, &ds->ds_addrs);
561 ds->ds_remotestr = remotestr;
562 atomic_set(&ds->ds_count, 1);
563 INIT_LIST_HEAD(&ds->ds_node);
564 ds->ds_clp = NULL;
565 list_add(&ds->ds_node, &nfs4_data_server_cache);
566 dprintk("%s add new data server %s\n", __func__,
567 ds->ds_remotestr);
568 } else {
569 kfree(remotestr);
570 kfree(ds);
571 atomic_inc(&tmp_ds->ds_count);
572 dprintk("%s data server %s found, inc'ed ds_count to %d\n",
573 __func__, tmp_ds->ds_remotestr,
574 atomic_read(&tmp_ds->ds_count));
575 ds = tmp_ds;
576 }
577 spin_unlock(&nfs4_ds_cache_lock);
578out:
579 return ds;
580}
581EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_add);
Peng Tao6b7f3cf2014-05-29 21:06:59 +0800582
Peng Tao7405f9e2014-05-29 21:06:58 +0800583static void nfs4_wait_ds_connect(struct nfs4_pnfs_ds *ds)
584{
585 might_sleep();
586 wait_on_bit(&ds->ds_state, NFS4DS_CONNECTING,
587 TASK_KILLABLE);
588}
589
590static void nfs4_clear_ds_conn_bit(struct nfs4_pnfs_ds *ds)
591{
592 smp_mb__before_atomic();
593 clear_bit(NFS4DS_CONNECTING, &ds->ds_state);
594 smp_mb__after_atomic();
595 wake_up_bit(&ds->ds_state, NFS4DS_CONNECTING);
596}
597
Peng Tao5f01d952014-05-30 18:15:59 +0800598static struct nfs_client *(*get_v3_ds_connect)(
Tigran Mkrtchyanb224f7c2016-06-13 20:52:00 +0200599 struct nfs_server *mds_srv,
Peng Tao5f01d952014-05-30 18:15:59 +0800600 const struct sockaddr *ds_addr,
601 int ds_addrlen,
602 int ds_proto,
603 unsigned int ds_timeo,
Anna Schumaker7d38de32016-11-17 15:15:55 -0500604 unsigned int ds_retrans);
Peng Tao5f01d952014-05-30 18:15:59 +0800605
606static bool load_v3_ds_connect(void)
607{
608 if (!get_v3_ds_connect) {
609 get_v3_ds_connect = symbol_request(nfs3_set_ds_client);
610 WARN_ON_ONCE(!get_v3_ds_connect);
611 }
612
613 return(get_v3_ds_connect != NULL);
614}
615
Arnd Bergmanndf137bc2015-03-11 14:37:25 +0100616void nfs4_pnfs_v3_ds_connect_unload(void)
Peng Tao5f01d952014-05-30 18:15:59 +0800617{
618 if (get_v3_ds_connect) {
619 symbol_put(nfs3_set_ds_client);
620 get_v3_ds_connect = NULL;
621 }
622}
Peng Tao5f01d952014-05-30 18:15:59 +0800623
624static int _nfs4_pnfs_v3_ds_connect(struct nfs_server *mds_srv,
625 struct nfs4_pnfs_ds *ds,
626 unsigned int timeo,
Anna Schumaker7d38de32016-11-17 15:15:55 -0500627 unsigned int retrans)
Peng Tao5f01d952014-05-30 18:15:59 +0800628{
629 struct nfs_client *clp = ERR_PTR(-EIO);
630 struct nfs4_pnfs_ds_addr *da;
631 int status = 0;
632
Anna Schumaker7d38de32016-11-17 15:15:55 -0500633 dprintk("--> %s DS %s\n", __func__, ds->ds_remotestr);
Peng Tao5f01d952014-05-30 18:15:59 +0800634
635 if (!load_v3_ds_connect())
636 goto out;
637
638 list_for_each_entry(da, &ds->ds_addrs, da_node) {
639 dprintk("%s: DS %s: trying address %s\n",
640 __func__, ds->ds_remotestr, da->da_remotestr);
641
Trond Myklebustfc821d52016-01-31 14:53:08 -0500642 if (!IS_ERR(clp)) {
643 struct xprt_create xprt_args = {
644 .ident = XPRT_TRANSPORT_TCP,
645 .net = clp->cl_net,
646 .dstaddr = (struct sockaddr *)&da->da_addr,
647 .addrlen = da->da_addrlen,
648 .servername = clp->cl_hostname,
649 };
650 /* Add this address as an alias */
651 rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
652 rpc_clnt_test_and_add_xprt, NULL);
653 } else
Tigran Mkrtchyanb224f7c2016-06-13 20:52:00 +0200654 clp = get_v3_ds_connect(mds_srv,
Peng Tao5f01d952014-05-30 18:15:59 +0800655 (struct sockaddr *)&da->da_addr,
656 da->da_addrlen, IPPROTO_TCP,
Anna Schumaker7d38de32016-11-17 15:15:55 -0500657 timeo, retrans);
Peng Tao5f01d952014-05-30 18:15:59 +0800658 }
659
660 if (IS_ERR(clp)) {
661 status = PTR_ERR(clp);
662 goto out;
663 }
664
665 smp_wmb();
666 ds->ds_clp = clp;
667 dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
668out:
669 return status;
670}
671
672static int _nfs4_pnfs_v4_ds_connect(struct nfs_server *mds_srv,
Peng Tao7405f9e2014-05-29 21:06:58 +0800673 struct nfs4_pnfs_ds *ds,
674 unsigned int timeo,
Peng Tao064172f2014-05-29 21:07:00 +0800675 unsigned int retrans,
Anna Schumaker7d38de32016-11-17 15:15:55 -0500676 u32 minor_version)
Peng Tao7405f9e2014-05-29 21:06:58 +0800677{
678 struct nfs_client *clp = ERR_PTR(-EIO);
679 struct nfs4_pnfs_ds_addr *da;
680 int status = 0;
681
Anna Schumaker7d38de32016-11-17 15:15:55 -0500682 dprintk("--> %s DS %s\n", __func__, ds->ds_remotestr);
Peng Tao7405f9e2014-05-29 21:06:58 +0800683
684 list_for_each_entry(da, &ds->ds_addrs, da_node) {
685 dprintk("%s: DS %s: trying address %s\n",
686 __func__, ds->ds_remotestr, da->da_remotestr);
687
Andy Adamson04fa2c62016-09-09 09:22:29 -0400688 if (!IS_ERR(clp) && clp->cl_mvops->session_trunk) {
689 struct xprt_create xprt_args = {
690 .ident = XPRT_TRANSPORT_TCP,
691 .net = clp->cl_net,
692 .dstaddr = (struct sockaddr *)&da->da_addr,
693 .addrlen = da->da_addrlen,
694 .servername = clp->cl_hostname,
695 };
696 struct nfs4_add_xprt_data xprtdata = {
697 .clp = clp,
698 .cred = nfs4_get_clid_cred(clp),
699 };
700 struct rpc_add_xprt_test rpcdata = {
701 .add_xprt_test = clp->cl_mvops->session_trunk,
702 .data = &xprtdata,
703 };
704
705 /**
706 * Test this address for session trunking and
707 * add as an alias
708 */
709 rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
710 rpc_clnt_setup_test_and_add_xprt,
711 &rpcdata);
712 if (xprtdata.cred)
713 put_rpccred(xprtdata.cred);
714 } else {
715 clp = nfs4_set_ds_client(mds_srv,
716 (struct sockaddr *)&da->da_addr,
717 da->da_addrlen, IPPROTO_TCP,
Anna Schumaker7d38de32016-11-17 15:15:55 -0500718 timeo, retrans, minor_version);
Andy Adamson04fa2c62016-09-09 09:22:29 -0400719 if (IS_ERR(clp))
720 continue;
721
722 status = nfs4_init_ds_session(clp,
723 mds_srv->nfs_client->cl_lease_time);
724 if (status) {
725 nfs_put_client(clp);
726 clp = ERR_PTR(-EIO);
727 continue;
728 }
729
730 }
Peng Tao7405f9e2014-05-29 21:06:58 +0800731 }
732
733 if (IS_ERR(clp)) {
734 status = PTR_ERR(clp);
735 goto out;
736 }
737
Peng Tao7405f9e2014-05-29 21:06:58 +0800738 smp_wmb();
739 ds->ds_clp = clp;
740 dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
741out:
742 return status;
Peng Tao7405f9e2014-05-29 21:06:58 +0800743}
744
745/*
746 * Create an rpc connection to the nfs4_pnfs_ds data server.
747 * Currently only supports IPv4 and IPv6 addresses.
Weston Andros Adamsona33e4b02017-03-09 12:56:48 -0500748 * If connection fails, make devid unavailable and return a -errno.
Peng Tao7405f9e2014-05-29 21:06:58 +0800749 */
Weston Andros Adamsona33e4b02017-03-09 12:56:48 -0500750int nfs4_pnfs_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds,
Peng Tao7405f9e2014-05-29 21:06:58 +0800751 struct nfs4_deviceid_node *devid, unsigned int timeo,
Anna Schumaker7d38de32016-11-17 15:15:55 -0500752 unsigned int retrans, u32 version, u32 minor_version)
Peng Tao7405f9e2014-05-29 21:06:58 +0800753{
Weston Andros Adamsonda066f32017-03-09 12:56:49 -0500754 int err;
Peng Tao7405f9e2014-05-29 21:06:58 +0800755
Weston Andros Adamsonda066f32017-03-09 12:56:49 -0500756again:
757 err = 0;
758 if (test_and_set_bit(NFS4DS_CONNECTING, &ds->ds_state) == 0) {
Peng Tao5f01d952014-05-30 18:15:59 +0800759 if (version == 3) {
760 err = _nfs4_pnfs_v3_ds_connect(mds_srv, ds, timeo,
Anna Schumaker7d38de32016-11-17 15:15:55 -0500761 retrans);
Peng Tao5f01d952014-05-30 18:15:59 +0800762 } else if (version == 4) {
763 err = _nfs4_pnfs_v4_ds_connect(mds_srv, ds, timeo,
Anna Schumaker7d38de32016-11-17 15:15:55 -0500764 retrans, minor_version);
Peng Tao5f01d952014-05-30 18:15:59 +0800765 } else {
766 dprintk("%s: unsupported DS version %d\n", __func__,
767 version);
768 err = -EPROTONOSUPPORT;
769 }
770
Peng Tao7405f9e2014-05-29 21:06:58 +0800771 nfs4_clear_ds_conn_bit(ds);
772 } else {
773 nfs4_wait_ds_connect(ds);
Weston Andros Adamsonda066f32017-03-09 12:56:49 -0500774
775 /* what was waited on didn't connect AND didn't mark unavail */
776 if (!ds->ds_clp && !nfs4_test_deviceid_unavailable(devid))
777 goto again;
Peng Tao7405f9e2014-05-29 21:06:58 +0800778 }
Weston Andros Adamsona33e4b02017-03-09 12:56:48 -0500779
780 /*
781 * At this point the ds->ds_clp should be ready, but it might have
782 * hit an error.
783 */
Weston Andros Adamsonda066f32017-03-09 12:56:49 -0500784 if (!err) {
785 if (!ds->ds_clp || !nfs_client_init_is_complete(ds->ds_clp)) {
786 WARN_ON_ONCE(ds->ds_clp ||
787 !nfs4_test_deviceid_unavailable(devid));
788 return -EINVAL;
789 }
790 err = nfs_client_init_status(ds->ds_clp);
Weston Andros Adamsona33e4b02017-03-09 12:56:48 -0500791 }
792
Weston Andros Adamsonda066f32017-03-09 12:56:49 -0500793 return err;
Peng Tao7405f9e2014-05-29 21:06:58 +0800794}
795EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_connect);
796
Peng Tao6b7f3cf2014-05-29 21:06:59 +0800797/*
798 * Currently only supports ipv4, ipv6 and one multi-path address.
799 */
800struct nfs4_pnfs_ds_addr *
801nfs4_decode_mp_ds_addr(struct net *net, struct xdr_stream *xdr, gfp_t gfp_flags)
802{
803 struct nfs4_pnfs_ds_addr *da = NULL;
804 char *buf, *portstr;
805 __be16 port;
806 int nlen, rlen;
807 int tmp[2];
808 __be32 *p;
809 char *netid, *match_netid;
810 size_t len, match_netid_len;
811 char *startsep = "";
812 char *endsep = "";
813
814
815 /* r_netid */
816 p = xdr_inline_decode(xdr, 4);
817 if (unlikely(!p))
818 goto out_err;
819 nlen = be32_to_cpup(p++);
820
821 p = xdr_inline_decode(xdr, nlen);
822 if (unlikely(!p))
823 goto out_err;
824
825 netid = kmalloc(nlen+1, gfp_flags);
826 if (unlikely(!netid))
827 goto out_err;
828
829 netid[nlen] = '\0';
830 memcpy(netid, p, nlen);
831
832 /* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */
833 p = xdr_inline_decode(xdr, 4);
834 if (unlikely(!p))
835 goto out_free_netid;
836 rlen = be32_to_cpup(p);
837
838 p = xdr_inline_decode(xdr, rlen);
839 if (unlikely(!p))
840 goto out_free_netid;
841
842 /* port is ".ABC.DEF", 8 chars max */
843 if (rlen > INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 8) {
844 dprintk("%s: Invalid address, length %d\n", __func__,
845 rlen);
846 goto out_free_netid;
847 }
848 buf = kmalloc(rlen + 1, gfp_flags);
849 if (!buf) {
850 dprintk("%s: Not enough memory\n", __func__);
851 goto out_free_netid;
852 }
853 buf[rlen] = '\0';
854 memcpy(buf, p, rlen);
855
856 /* replace port '.' with '-' */
857 portstr = strrchr(buf, '.');
858 if (!portstr) {
859 dprintk("%s: Failed finding expected dot in port\n",
860 __func__);
861 goto out_free_buf;
862 }
863 *portstr = '-';
864
865 /* find '.' between address and port */
866 portstr = strrchr(buf, '.');
867 if (!portstr) {
868 dprintk("%s: Failed finding expected dot between address and "
869 "port\n", __func__);
870 goto out_free_buf;
871 }
872 *portstr = '\0';
873
874 da = kzalloc(sizeof(*da), gfp_flags);
875 if (unlikely(!da))
876 goto out_free_buf;
877
878 INIT_LIST_HEAD(&da->da_node);
879
880 if (!rpc_pton(net, buf, portstr-buf, (struct sockaddr *)&da->da_addr,
881 sizeof(da->da_addr))) {
882 dprintk("%s: error parsing address %s\n", __func__, buf);
883 goto out_free_da;
884 }
885
886 portstr++;
887 sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]);
888 port = htons((tmp[0] << 8) | (tmp[1]));
889
890 switch (da->da_addr.ss_family) {
891 case AF_INET:
892 ((struct sockaddr_in *)&da->da_addr)->sin_port = port;
893 da->da_addrlen = sizeof(struct sockaddr_in);
894 match_netid = "tcp";
895 match_netid_len = 3;
896 break;
897
898 case AF_INET6:
899 ((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port;
900 da->da_addrlen = sizeof(struct sockaddr_in6);
901 match_netid = "tcp6";
902 match_netid_len = 4;
903 startsep = "[";
904 endsep = "]";
905 break;
906
907 default:
908 dprintk("%s: unsupported address family: %u\n",
909 __func__, da->da_addr.ss_family);
910 goto out_free_da;
911 }
912
913 if (nlen != match_netid_len || strncmp(netid, match_netid, nlen)) {
914 dprintk("%s: ERROR: r_netid \"%s\" != \"%s\"\n",
915 __func__, netid, match_netid);
916 goto out_free_da;
917 }
918
919 /* save human readable address */
920 len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7;
921 da->da_remotestr = kzalloc(len, gfp_flags);
922
923 /* NULL is ok, only used for dprintk */
924 if (da->da_remotestr)
925 snprintf(da->da_remotestr, len, "%s%s%s:%u", startsep,
926 buf, endsep, ntohs(port));
927
928 dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr);
929 kfree(buf);
930 kfree(netid);
931 return da;
932
933out_free_da:
934 kfree(da);
935out_free_buf:
936 dprintk("%s: Error parsing DS addr: %s\n", __func__, buf);
937 kfree(buf);
938out_free_netid:
939 kfree(netid);
940out_err:
941 return NULL;
942}
943EXPORT_SYMBOL_GPL(nfs4_decode_mp_ds_addr);
Tom Haynes338d00c2015-02-17 14:58:15 -0800944
945void
946pnfs_layout_mark_request_commit(struct nfs_page *req,
947 struct pnfs_layout_segment *lseg,
948 struct nfs_commit_info *cinfo,
949 u32 ds_commit_idx)
950{
951 struct list_head *list;
952 struct pnfs_commit_bucket *buckets;
953
Dave Wysochanskife238e62016-04-01 13:45:09 -0400954 spin_lock(&cinfo->inode->i_lock);
Tom Haynes338d00c2015-02-17 14:58:15 -0800955 buckets = cinfo->ds->buckets;
956 list = &buckets[ds_commit_idx].written;
957 if (list_empty(list)) {
Trond Myklebustb20135d2015-12-31 09:28:06 -0500958 if (!pnfs_is_valid_lseg(lseg)) {
Dave Wysochanskife238e62016-04-01 13:45:09 -0400959 spin_unlock(&cinfo->inode->i_lock);
Trond Myklebustb20135d2015-12-31 09:28:06 -0500960 cinfo->completion_ops->resched_write(cinfo, req);
961 return;
962 }
Tom Haynes338d00c2015-02-17 14:58:15 -0800963 /* Non-empty buckets hold a reference on the lseg. That ref
964 * is normally transferred to the COMMIT call and released
965 * there. It could also be released if the last req is pulled
966 * off due to a rewrite, in which case it will be done in
967 * pnfs_common_clear_request_commit
968 */
969 WARN_ON_ONCE(buckets[ds_commit_idx].wlseg != NULL);
970 buckets[ds_commit_idx].wlseg = pnfs_get_lseg(lseg);
971 }
972 set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
973 cinfo->ds->nwritten++;
Tom Haynes338d00c2015-02-17 14:58:15 -0800974
Trond Myklebust86d80f92015-07-31 16:24:30 -0400975 nfs_request_add_commit_list_locked(req, list, cinfo);
Dave Wysochanskife238e62016-04-01 13:45:09 -0400976 spin_unlock(&cinfo->inode->i_lock);
Trond Myklebust86d80f92015-07-31 16:24:30 -0400977 nfs_mark_page_unstable(req->wb_page, cinfo);
Tom Haynes338d00c2015-02-17 14:58:15 -0800978}
979EXPORT_SYMBOL_GPL(pnfs_layout_mark_request_commit);
Trond Myklebust5bb89b42015-03-25 14:14:42 -0400980
981int
982pnfs_nfs_generic_sync(struct inode *inode, bool datasync)
983{
Trond Myklebust2e18d4d2016-06-26 18:54:58 -0400984 int ret;
985
986 if (!pnfs_layoutcommit_outstanding(inode))
987 return 0;
988 ret = nfs_commit_inode(inode, FLUSH_SYNC);
989 if (ret < 0)
990 return ret;
Trond Myklebust5bb89b42015-03-25 14:14:42 -0400991 if (datasync)
992 return 0;
993 return pnfs_layoutcommit_inode(inode, true);
994}
995EXPORT_SYMBOL_GPL(pnfs_nfs_generic_sync);
996