blob: 03aaa60c7768a087ed99bd381fa299161dc9716d [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);
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
90static int
Tom Haynesf54bcf22014-12-11 15:34:59 -050091pnfs_generic_scan_ds_commit_list(struct pnfs_commit_bucket *bucket,
92 struct nfs_commit_info *cinfo,
93 int max)
94{
95 struct list_head *src = &bucket->written;
96 struct list_head *dst = &bucket->committing;
97 int ret;
98
Trond Myklebuste824f992017-08-01 11:53:49 -040099 lockdep_assert_held(&NFS_I(cinfo->inode)->commit_mutex);
Trond Myklebust5d2a9d92017-09-09 12:40:38 -0400100 ret = nfs_scan_commit_list(src, dst, cinfo, max);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500101 if (ret) {
102 cinfo->ds->nwritten -= ret;
103 cinfo->ds->ncommitting += ret;
Trond Myklebust27571292015-08-03 17:38:33 -0400104 if (bucket->clseg == NULL)
105 bucket->clseg = pnfs_get_lseg(bucket->wlseg);
106 if (list_empty(src)) {
Trond Myklebust8205b9c2017-08-01 17:07:02 -0400107 pnfs_put_lseg(bucket->wlseg);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500108 bucket->wlseg = NULL;
Trond Myklebust27571292015-08-03 17:38:33 -0400109 }
Tom Haynesf54bcf22014-12-11 15:34:59 -0500110 }
111 return ret;
112}
113
Tom Haynes085d1e32014-12-11 13:04:55 -0500114/* Move reqs from written to committing lists, returning count
115 * of number moved.
Tom Haynesf54bcf22014-12-11 15:34:59 -0500116 */
117int pnfs_generic_scan_commit_lists(struct nfs_commit_info *cinfo,
118 int max)
119{
120 int i, rv = 0, cnt;
121
Trond Myklebuste824f992017-08-01 11:53:49 -0400122 lockdep_assert_held(&NFS_I(cinfo->inode)->commit_mutex);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500123 for (i = 0; i < cinfo->ds->nbuckets && max != 0; i++) {
124 cnt = pnfs_generic_scan_ds_commit_list(&cinfo->ds->buckets[i],
125 cinfo, max);
126 max -= cnt;
127 rv += cnt;
128 }
129 return rv;
130}
131EXPORT_SYMBOL_GPL(pnfs_generic_scan_commit_lists);
132
Tom Haynes085d1e32014-12-11 13:04:55 -0500133/* Pull everything off the committing lists and dump into @dst. */
Tom Haynesf54bcf22014-12-11 15:34:59 -0500134void pnfs_generic_recover_commit_reqs(struct list_head *dst,
135 struct nfs_commit_info *cinfo)
136{
137 struct pnfs_commit_bucket *b;
138 struct pnfs_layout_segment *freeme;
Trond Myklebuste39928f2017-07-12 19:10:56 -0400139 int nwritten;
Tom Haynesf54bcf22014-12-11 15:34:59 -0500140 int i;
141
Trond Myklebuste824f992017-08-01 11:53:49 -0400142 lockdep_assert_held(&NFS_I(cinfo->inode)->commit_mutex);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500143restart:
Tom Haynesf54bcf22014-12-11 15:34:59 -0500144 for (i = 0, b = cinfo->ds->buckets; i < cinfo->ds->nbuckets; i++, b++) {
Trond Myklebust5d2a9d92017-09-09 12:40:38 -0400145 nwritten = nfs_scan_commit_list(&b->written, dst, cinfo, 0);
Trond Myklebuste39928f2017-07-12 19:10:56 -0400146 if (!nwritten)
147 continue;
148 cinfo->ds->nwritten -= nwritten;
149 if (list_empty(&b->written)) {
Tom Haynesf54bcf22014-12-11 15:34:59 -0500150 freeme = b->wlseg;
151 b->wlseg = NULL;
Dave Wysochanskife238e62016-04-01 13:45:09 -0400152 spin_unlock(&cinfo->inode->i_lock);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500153 pnfs_put_lseg(freeme);
Dave Wysochanskife238e62016-04-01 13:45:09 -0400154 spin_lock(&cinfo->inode->i_lock);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500155 goto restart;
156 }
157 }
Tom Haynesf54bcf22014-12-11 15:34:59 -0500158}
159EXPORT_SYMBOL_GPL(pnfs_generic_recover_commit_reqs);
160
161static void pnfs_generic_retry_commit(struct nfs_commit_info *cinfo, int idx)
162{
163 struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds;
164 struct pnfs_commit_bucket *bucket;
165 struct pnfs_layout_segment *freeme;
Trond Myklebust41181882017-07-12 19:10:57 -0400166 struct list_head *pos;
Trond Myklebust27571292015-08-03 17:38:33 -0400167 LIST_HEAD(pages);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500168 int i;
169
Dave Wysochanskife238e62016-04-01 13:45:09 -0400170 spin_lock(&cinfo->inode->i_lock);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500171 for (i = idx; i < fl_cinfo->nbuckets; i++) {
172 bucket = &fl_cinfo->buckets[i];
173 if (list_empty(&bucket->committing))
174 continue;
Tom Haynesf54bcf22014-12-11 15:34:59 -0500175 freeme = bucket->clseg;
176 bucket->clseg = NULL;
Trond Myklebust41181882017-07-12 19:10:57 -0400177 list_for_each(pos, &bucket->committing)
178 cinfo->ds->ncommitting--;
Trond Myklebust27571292015-08-03 17:38:33 -0400179 list_splice_init(&bucket->committing, &pages);
Dave Wysochanskife238e62016-04-01 13:45:09 -0400180 spin_unlock(&cinfo->inode->i_lock);
Trond Myklebust27571292015-08-03 17:38:33 -0400181 nfs_retry_commit(&pages, freeme, cinfo, i);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500182 pnfs_put_lseg(freeme);
Dave Wysochanskife238e62016-04-01 13:45:09 -0400183 spin_lock(&cinfo->inode->i_lock);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500184 }
Dave Wysochanskife238e62016-04-01 13:45:09 -0400185 spin_unlock(&cinfo->inode->i_lock);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500186}
187
188static unsigned int
189pnfs_generic_alloc_ds_commits(struct nfs_commit_info *cinfo,
190 struct list_head *list)
191{
192 struct pnfs_ds_commit_info *fl_cinfo;
193 struct pnfs_commit_bucket *bucket;
194 struct nfs_commit_data *data;
195 int i;
196 unsigned int nreq = 0;
197
198 fl_cinfo = cinfo->ds;
199 bucket = fl_cinfo->buckets;
200 for (i = 0; i < fl_cinfo->nbuckets; i++, bucket++) {
201 if (list_empty(&bucket->committing))
202 continue;
NeilBrown518662e2017-04-10 12:22:09 +1000203 data = nfs_commitdata_alloc(false);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500204 if (!data)
205 break;
206 data->ds_commit_index = i;
Tom Haynesf54bcf22014-12-11 15:34:59 -0500207 list_add(&data->pages, list);
208 nreq++;
209 }
210
211 /* Clean up on error */
212 pnfs_generic_retry_commit(cinfo, i);
213 return nreq;
214}
215
Trond Myklebust27571292015-08-03 17:38:33 -0400216static inline
217void pnfs_fetch_commit_bucket_list(struct list_head *pages,
218 struct nfs_commit_data *data,
219 struct nfs_commit_info *cinfo)
220{
221 struct pnfs_commit_bucket *bucket;
Trond Myklebust41181882017-07-12 19:10:57 -0400222 struct list_head *pos;
Trond Myklebust27571292015-08-03 17:38:33 -0400223
224 bucket = &cinfo->ds->buckets[data->ds_commit_index];
Dave Wysochanskife238e62016-04-01 13:45:09 -0400225 spin_lock(&cinfo->inode->i_lock);
Trond Myklebust41181882017-07-12 19:10:57 -0400226 list_for_each(pos, &bucket->committing)
227 cinfo->ds->ncommitting--;
Peng Tao046be742015-08-21 10:32:50 +0800228 list_splice_init(&bucket->committing, pages);
Trond Myklebust27571292015-08-03 17:38:33 -0400229 data->lseg = bucket->clseg;
230 bucket->clseg = NULL;
Dave Wysochanskife238e62016-04-01 13:45:09 -0400231 spin_unlock(&cinfo->inode->i_lock);
Trond Myklebust27571292015-08-03 17:38:33 -0400232
233}
234
Weston Andros Adamsonade8feb2016-05-25 10:07:23 -0400235/* Helper function for pnfs_generic_commit_pagelist to catch an empty
Weston Andros Adamson5e3a9882016-06-17 16:48:24 -0400236 * page list. This can happen when two commits race.
237 *
238 * This must be called instead of nfs_init_commit - call one or the other, but
239 * not both!
240 */
Weston Andros Adamsonade8feb2016-05-25 10:07:23 -0400241static bool
242pnfs_generic_commit_cancel_empty_pagelist(struct list_head *pages,
243 struct nfs_commit_data *data,
244 struct nfs_commit_info *cinfo)
245{
246 if (list_empty(pages)) {
247 if (atomic_dec_and_test(&cinfo->mds->rpcs_out))
248 wake_up_atomic_t(&cinfo->mds->rpcs_out);
Weston Andros Adamson5e3a9882016-06-17 16:48:24 -0400249 /* don't call nfs_commitdata_release - it tries to put
250 * the open_context which is not acquired until nfs_init_commit
251 * which has not been called on @data */
252 WARN_ON_ONCE(data->context);
253 nfs_commit_free(data);
Weston Andros Adamsonade8feb2016-05-25 10:07:23 -0400254 return true;
255 }
256
257 return false;
258}
259
Tom Haynesf54bcf22014-12-11 15:34:59 -0500260/* This follows nfs_commit_list pretty closely */
261int
262pnfs_generic_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
263 int how, struct nfs_commit_info *cinfo,
264 int (*initiate_commit)(struct nfs_commit_data *data,
265 int how))
266{
267 struct nfs_commit_data *data, *tmp;
268 LIST_HEAD(list);
269 unsigned int nreq = 0;
270
271 if (!list_empty(mds_pages)) {
NeilBrown518662e2017-04-10 12:22:09 +1000272 data = nfs_commitdata_alloc(true);
273 data->ds_commit_index = -1;
274 list_add(&data->pages, &list);
275 nreq++;
Tom Haynesf54bcf22014-12-11 15:34:59 -0500276 }
277
278 nreq += pnfs_generic_alloc_ds_commits(cinfo, &list);
279
Trond Myklebustaf7cf052015-09-29 20:34:05 -0400280 if (nreq == 0)
Tom Haynesf54bcf22014-12-11 15:34:59 -0500281 goto out;
Tom Haynesf54bcf22014-12-11 15:34:59 -0500282
283 atomic_add(nreq, &cinfo->mds->rpcs_out);
284
285 list_for_each_entry_safe(data, tmp, &list, pages) {
286 list_del_init(&data->pages);
Trond Myklebust27571292015-08-03 17:38:33 -0400287 if (data->ds_commit_index < 0) {
Weston Andros Adamsonade8feb2016-05-25 10:07:23 -0400288 /* another commit raced with us */
289 if (pnfs_generic_commit_cancel_empty_pagelist(mds_pages,
290 data, cinfo))
291 continue;
292
Tom Haynesf54bcf22014-12-11 15:34:59 -0500293 nfs_init_commit(data, mds_pages, NULL, cinfo);
294 nfs_initiate_commit(NFS_CLIENT(inode), data,
Peng Taoc36aae92014-06-09 07:10:14 +0800295 NFS_PROTO(data->inode),
Tom Haynesf54bcf22014-12-11 15:34:59 -0500296 data->mds_ops, how, 0);
297 } else {
Trond Myklebust27571292015-08-03 17:38:33 -0400298 LIST_HEAD(pages);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500299
Trond Myklebust27571292015-08-03 17:38:33 -0400300 pnfs_fetch_commit_bucket_list(&pages, data, cinfo);
Weston Andros Adamsonade8feb2016-05-25 10:07:23 -0400301
302 /* another commit raced with us */
303 if (pnfs_generic_commit_cancel_empty_pagelist(&pages,
304 data, cinfo))
305 continue;
306
Trond Myklebust27571292015-08-03 17:38:33 -0400307 nfs_init_commit(data, &pages, data->lseg, cinfo);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500308 initiate_commit(data, how);
309 }
310 }
311out:
Tom Haynesf54bcf22014-12-11 15:34:59 -0500312 return PNFS_ATTEMPTED;
313}
314EXPORT_SYMBOL_GPL(pnfs_generic_commit_pagelist);
Peng Tao875ae062014-05-29 21:06:57 +0800315
316/*
317 * Data server cache
318 *
319 * Data servers can be mapped to different device ids.
320 * nfs4_pnfs_ds reference counting
321 * - set to 1 on allocation
322 * - incremented when a device id maps a data server already in the cache.
323 * - decremented when deviceid is removed from the cache.
324 */
325static DEFINE_SPINLOCK(nfs4_ds_cache_lock);
326static LIST_HEAD(nfs4_data_server_cache);
327
328/* Debug routines */
329static void
330print_ds(struct nfs4_pnfs_ds *ds)
331{
332 if (ds == NULL) {
333 printk(KERN_WARNING "%s NULL device\n", __func__);
334 return;
335 }
336 printk(KERN_WARNING " ds %s\n"
337 " ref count %d\n"
338 " client %p\n"
339 " cl_exchange_flags %x\n",
340 ds->ds_remotestr,
Elena Reshetovaa2a5dea2017-10-20 12:53:31 +0300341 refcount_read(&ds->ds_count), ds->ds_clp,
Peng Tao875ae062014-05-29 21:06:57 +0800342 ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0);
343}
344
345static bool
346same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2)
347{
348 struct sockaddr_in *a, *b;
349 struct sockaddr_in6 *a6, *b6;
350
351 if (addr1->sa_family != addr2->sa_family)
352 return false;
353
354 switch (addr1->sa_family) {
355 case AF_INET:
356 a = (struct sockaddr_in *)addr1;
357 b = (struct sockaddr_in *)addr2;
358
359 if (a->sin_addr.s_addr == b->sin_addr.s_addr &&
360 a->sin_port == b->sin_port)
361 return true;
362 break;
363
364 case AF_INET6:
365 a6 = (struct sockaddr_in6 *)addr1;
366 b6 = (struct sockaddr_in6 *)addr2;
367
368 /* LINKLOCAL addresses must have matching scope_id */
369 if (ipv6_addr_src_scope(&a6->sin6_addr) ==
370 IPV6_ADDR_SCOPE_LINKLOCAL &&
371 a6->sin6_scope_id != b6->sin6_scope_id)
372 return false;
373
374 if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) &&
375 a6->sin6_port == b6->sin6_port)
376 return true;
377 break;
378
379 default:
380 dprintk("%s: unhandled address family: %u\n",
381 __func__, addr1->sa_family);
382 return false;
383 }
384
385 return false;
386}
387
Trond Myklebust6f536932015-08-13 10:59:07 -0400388/*
389 * Checks if 'dsaddrs1' contains a subset of 'dsaddrs2'. If it does,
390 * declare a match.
391 */
Peng Tao875ae062014-05-29 21:06:57 +0800392static bool
393_same_data_server_addrs_locked(const struct list_head *dsaddrs1,
394 const struct list_head *dsaddrs2)
395{
396 struct nfs4_pnfs_ds_addr *da1, *da2;
Trond Myklebust6f536932015-08-13 10:59:07 -0400397 struct sockaddr *sa1, *sa2;
398 bool match = false;
Peng Tao875ae062014-05-29 21:06:57 +0800399
Trond Myklebust6f536932015-08-13 10:59:07 -0400400 list_for_each_entry(da1, dsaddrs1, da_node) {
401 sa1 = (struct sockaddr *)&da1->da_addr;
402 match = false;
403 list_for_each_entry(da2, dsaddrs2, da_node) {
404 sa2 = (struct sockaddr *)&da2->da_addr;
405 match = same_sockaddr(sa1, sa2);
406 if (match)
407 break;
408 }
409 if (!match)
410 break;
Peng Tao875ae062014-05-29 21:06:57 +0800411 }
Trond Myklebust6f536932015-08-13 10:59:07 -0400412 return match;
Peng Tao875ae062014-05-29 21:06:57 +0800413}
414
415/*
416 * Lookup DS by addresses. nfs4_ds_cache_lock is held
417 */
418static struct nfs4_pnfs_ds *
419_data_server_lookup_locked(const struct list_head *dsaddrs)
420{
421 struct nfs4_pnfs_ds *ds;
422
423 list_for_each_entry(ds, &nfs4_data_server_cache, ds_node)
424 if (_same_data_server_addrs_locked(&ds->ds_addrs, dsaddrs))
425 return ds;
426 return NULL;
427}
428
429static void destroy_ds(struct nfs4_pnfs_ds *ds)
430{
431 struct nfs4_pnfs_ds_addr *da;
432
433 dprintk("--> %s\n", __func__);
434 ifdebug(FACILITY)
435 print_ds(ds);
436
437 nfs_put_client(ds->ds_clp);
438
439 while (!list_empty(&ds->ds_addrs)) {
440 da = list_first_entry(&ds->ds_addrs,
441 struct nfs4_pnfs_ds_addr,
442 da_node);
443 list_del_init(&da->da_node);
444 kfree(da->da_remotestr);
445 kfree(da);
446 }
447
448 kfree(ds->ds_remotestr);
449 kfree(ds);
450}
451
452void nfs4_pnfs_ds_put(struct nfs4_pnfs_ds *ds)
453{
Elena Reshetovaa2a5dea2017-10-20 12:53:31 +0300454 if (refcount_dec_and_lock(&ds->ds_count,
Peng Tao875ae062014-05-29 21:06:57 +0800455 &nfs4_ds_cache_lock)) {
456 list_del_init(&ds->ds_node);
457 spin_unlock(&nfs4_ds_cache_lock);
458 destroy_ds(ds);
459 }
460}
461EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_put);
462
463/*
464 * Create a string with a human readable address and port to avoid
465 * complicated setup around many dprinks.
466 */
467static char *
468nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags)
469{
470 struct nfs4_pnfs_ds_addr *da;
471 char *remotestr;
472 size_t len;
473 char *p;
474
475 len = 3; /* '{', '}' and eol */
476 list_for_each_entry(da, dsaddrs, da_node) {
477 len += strlen(da->da_remotestr) + 1; /* string plus comma */
478 }
479
480 remotestr = kzalloc(len, gfp_flags);
481 if (!remotestr)
482 return NULL;
483
484 p = remotestr;
485 *(p++) = '{';
486 len--;
487 list_for_each_entry(da, dsaddrs, da_node) {
488 size_t ll = strlen(da->da_remotestr);
489
490 if (ll > len)
491 goto out_err;
492
493 memcpy(p, da->da_remotestr, ll);
494 p += ll;
495 len -= ll;
496
497 if (len < 1)
498 goto out_err;
499 (*p++) = ',';
500 len--;
501 }
502 if (len < 2)
503 goto out_err;
504 *(p++) = '}';
505 *p = '\0';
506 return remotestr;
507out_err:
508 kfree(remotestr);
509 return NULL;
510}
511
512/*
513 * Given a list of multipath struct nfs4_pnfs_ds_addr, add it to ds cache if
514 * uncached and return cached struct nfs4_pnfs_ds.
515 */
516struct nfs4_pnfs_ds *
517nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags)
518{
519 struct nfs4_pnfs_ds *tmp_ds, *ds = NULL;
520 char *remotestr;
521
522 if (list_empty(dsaddrs)) {
523 dprintk("%s: no addresses defined\n", __func__);
524 goto out;
525 }
526
527 ds = kzalloc(sizeof(*ds), gfp_flags);
528 if (!ds)
529 goto out;
530
531 /* this is only used for debugging, so it's ok if its NULL */
532 remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags);
533
534 spin_lock(&nfs4_ds_cache_lock);
535 tmp_ds = _data_server_lookup_locked(dsaddrs);
536 if (tmp_ds == NULL) {
537 INIT_LIST_HEAD(&ds->ds_addrs);
538 list_splice_init(dsaddrs, &ds->ds_addrs);
539 ds->ds_remotestr = remotestr;
Elena Reshetovaa2a5dea2017-10-20 12:53:31 +0300540 refcount_set(&ds->ds_count, 1);
Peng Tao875ae062014-05-29 21:06:57 +0800541 INIT_LIST_HEAD(&ds->ds_node);
542 ds->ds_clp = NULL;
543 list_add(&ds->ds_node, &nfs4_data_server_cache);
544 dprintk("%s add new data server %s\n", __func__,
545 ds->ds_remotestr);
546 } else {
547 kfree(remotestr);
548 kfree(ds);
Elena Reshetovaa2a5dea2017-10-20 12:53:31 +0300549 refcount_inc(&tmp_ds->ds_count);
Peng Tao875ae062014-05-29 21:06:57 +0800550 dprintk("%s data server %s found, inc'ed ds_count to %d\n",
551 __func__, tmp_ds->ds_remotestr,
Elena Reshetovaa2a5dea2017-10-20 12:53:31 +0300552 refcount_read(&tmp_ds->ds_count));
Peng Tao875ae062014-05-29 21:06:57 +0800553 ds = tmp_ds;
554 }
555 spin_unlock(&nfs4_ds_cache_lock);
556out:
557 return ds;
558}
559EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_add);
Peng Tao6b7f3cf2014-05-29 21:06:59 +0800560
Peng Tao7405f9e2014-05-29 21:06:58 +0800561static void nfs4_wait_ds_connect(struct nfs4_pnfs_ds *ds)
562{
563 might_sleep();
564 wait_on_bit(&ds->ds_state, NFS4DS_CONNECTING,
565 TASK_KILLABLE);
566}
567
568static void nfs4_clear_ds_conn_bit(struct nfs4_pnfs_ds *ds)
569{
570 smp_mb__before_atomic();
571 clear_bit(NFS4DS_CONNECTING, &ds->ds_state);
572 smp_mb__after_atomic();
573 wake_up_bit(&ds->ds_state, NFS4DS_CONNECTING);
574}
575
Peng Tao5f01d952014-05-30 18:15:59 +0800576static struct nfs_client *(*get_v3_ds_connect)(
Tigran Mkrtchyanb224f7c2016-06-13 20:52:00 +0200577 struct nfs_server *mds_srv,
Peng Tao5f01d952014-05-30 18:15:59 +0800578 const struct sockaddr *ds_addr,
579 int ds_addrlen,
580 int ds_proto,
581 unsigned int ds_timeo,
Anna Schumaker7d38de32016-11-17 15:15:55 -0500582 unsigned int ds_retrans);
Peng Tao5f01d952014-05-30 18:15:59 +0800583
584static bool load_v3_ds_connect(void)
585{
586 if (!get_v3_ds_connect) {
587 get_v3_ds_connect = symbol_request(nfs3_set_ds_client);
588 WARN_ON_ONCE(!get_v3_ds_connect);
589 }
590
591 return(get_v3_ds_connect != NULL);
592}
593
Arnd Bergmanndf137bc2015-03-11 14:37:25 +0100594void nfs4_pnfs_v3_ds_connect_unload(void)
Peng Tao5f01d952014-05-30 18:15:59 +0800595{
596 if (get_v3_ds_connect) {
597 symbol_put(nfs3_set_ds_client);
598 get_v3_ds_connect = NULL;
599 }
600}
Peng Tao5f01d952014-05-30 18:15:59 +0800601
602static int _nfs4_pnfs_v3_ds_connect(struct nfs_server *mds_srv,
603 struct nfs4_pnfs_ds *ds,
604 unsigned int timeo,
Anna Schumaker7d38de32016-11-17 15:15:55 -0500605 unsigned int retrans)
Peng Tao5f01d952014-05-30 18:15:59 +0800606{
607 struct nfs_client *clp = ERR_PTR(-EIO);
608 struct nfs4_pnfs_ds_addr *da;
609 int status = 0;
610
Anna Schumaker7d38de32016-11-17 15:15:55 -0500611 dprintk("--> %s DS %s\n", __func__, ds->ds_remotestr);
Peng Tao5f01d952014-05-30 18:15:59 +0800612
613 if (!load_v3_ds_connect())
614 goto out;
615
616 list_for_each_entry(da, &ds->ds_addrs, da_node) {
617 dprintk("%s: DS %s: trying address %s\n",
618 __func__, ds->ds_remotestr, da->da_remotestr);
619
Trond Myklebustfc821d52016-01-31 14:53:08 -0500620 if (!IS_ERR(clp)) {
621 struct xprt_create xprt_args = {
622 .ident = XPRT_TRANSPORT_TCP,
623 .net = clp->cl_net,
624 .dstaddr = (struct sockaddr *)&da->da_addr,
625 .addrlen = da->da_addrlen,
626 .servername = clp->cl_hostname,
627 };
628 /* Add this address as an alias */
629 rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
630 rpc_clnt_test_and_add_xprt, NULL);
631 } else
Tigran Mkrtchyanb224f7c2016-06-13 20:52:00 +0200632 clp = get_v3_ds_connect(mds_srv,
Peng Tao5f01d952014-05-30 18:15:59 +0800633 (struct sockaddr *)&da->da_addr,
634 da->da_addrlen, IPPROTO_TCP,
Anna Schumaker7d38de32016-11-17 15:15:55 -0500635 timeo, retrans);
Peng Tao5f01d952014-05-30 18:15:59 +0800636 }
637
638 if (IS_ERR(clp)) {
639 status = PTR_ERR(clp);
640 goto out;
641 }
642
643 smp_wmb();
644 ds->ds_clp = clp;
645 dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
646out:
647 return status;
648}
649
650static int _nfs4_pnfs_v4_ds_connect(struct nfs_server *mds_srv,
Peng Tao7405f9e2014-05-29 21:06:58 +0800651 struct nfs4_pnfs_ds *ds,
652 unsigned int timeo,
Peng Tao064172f2014-05-29 21:07:00 +0800653 unsigned int retrans,
Anna Schumaker7d38de32016-11-17 15:15:55 -0500654 u32 minor_version)
Peng Tao7405f9e2014-05-29 21:06:58 +0800655{
656 struct nfs_client *clp = ERR_PTR(-EIO);
657 struct nfs4_pnfs_ds_addr *da;
658 int status = 0;
659
Anna Schumaker7d38de32016-11-17 15:15:55 -0500660 dprintk("--> %s DS %s\n", __func__, ds->ds_remotestr);
Peng Tao7405f9e2014-05-29 21:06:58 +0800661
662 list_for_each_entry(da, &ds->ds_addrs, da_node) {
663 dprintk("%s: DS %s: trying address %s\n",
664 __func__, ds->ds_remotestr, da->da_remotestr);
665
Andy Adamson04fa2c62016-09-09 09:22:29 -0400666 if (!IS_ERR(clp) && clp->cl_mvops->session_trunk) {
667 struct xprt_create xprt_args = {
668 .ident = XPRT_TRANSPORT_TCP,
669 .net = clp->cl_net,
670 .dstaddr = (struct sockaddr *)&da->da_addr,
671 .addrlen = da->da_addrlen,
672 .servername = clp->cl_hostname,
673 };
674 struct nfs4_add_xprt_data xprtdata = {
675 .clp = clp,
676 .cred = nfs4_get_clid_cred(clp),
677 };
678 struct rpc_add_xprt_test rpcdata = {
679 .add_xprt_test = clp->cl_mvops->session_trunk,
680 .data = &xprtdata,
681 };
682
683 /**
684 * Test this address for session trunking and
685 * add as an alias
686 */
687 rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
688 rpc_clnt_setup_test_and_add_xprt,
689 &rpcdata);
690 if (xprtdata.cred)
691 put_rpccred(xprtdata.cred);
692 } else {
693 clp = nfs4_set_ds_client(mds_srv,
694 (struct sockaddr *)&da->da_addr,
695 da->da_addrlen, IPPROTO_TCP,
Anna Schumaker7d38de32016-11-17 15:15:55 -0500696 timeo, retrans, minor_version);
Andy Adamson04fa2c62016-09-09 09:22:29 -0400697 if (IS_ERR(clp))
698 continue;
699
700 status = nfs4_init_ds_session(clp,
701 mds_srv->nfs_client->cl_lease_time);
702 if (status) {
703 nfs_put_client(clp);
704 clp = ERR_PTR(-EIO);
705 continue;
706 }
707
708 }
Peng Tao7405f9e2014-05-29 21:06:58 +0800709 }
710
711 if (IS_ERR(clp)) {
712 status = PTR_ERR(clp);
713 goto out;
714 }
715
Peng Tao7405f9e2014-05-29 21:06:58 +0800716 smp_wmb();
717 ds->ds_clp = clp;
718 dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
719out:
720 return status;
Peng Tao7405f9e2014-05-29 21:06:58 +0800721}
722
723/*
724 * Create an rpc connection to the nfs4_pnfs_ds data server.
725 * Currently only supports IPv4 and IPv6 addresses.
Weston Andros Adamsona33e4b02017-03-09 12:56:48 -0500726 * If connection fails, make devid unavailable and return a -errno.
Peng Tao7405f9e2014-05-29 21:06:58 +0800727 */
Weston Andros Adamsona33e4b02017-03-09 12:56:48 -0500728int nfs4_pnfs_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds,
Peng Tao7405f9e2014-05-29 21:06:58 +0800729 struct nfs4_deviceid_node *devid, unsigned int timeo,
Anna Schumaker7d38de32016-11-17 15:15:55 -0500730 unsigned int retrans, u32 version, u32 minor_version)
Peng Tao7405f9e2014-05-29 21:06:58 +0800731{
Weston Andros Adamsonda066f32017-03-09 12:56:49 -0500732 int err;
Peng Tao7405f9e2014-05-29 21:06:58 +0800733
Weston Andros Adamsonda066f32017-03-09 12:56:49 -0500734again:
735 err = 0;
736 if (test_and_set_bit(NFS4DS_CONNECTING, &ds->ds_state) == 0) {
Peng Tao5f01d952014-05-30 18:15:59 +0800737 if (version == 3) {
738 err = _nfs4_pnfs_v3_ds_connect(mds_srv, ds, timeo,
Anna Schumaker7d38de32016-11-17 15:15:55 -0500739 retrans);
Peng Tao5f01d952014-05-30 18:15:59 +0800740 } else if (version == 4) {
741 err = _nfs4_pnfs_v4_ds_connect(mds_srv, ds, timeo,
Anna Schumaker7d38de32016-11-17 15:15:55 -0500742 retrans, minor_version);
Peng Tao5f01d952014-05-30 18:15:59 +0800743 } else {
744 dprintk("%s: unsupported DS version %d\n", __func__,
745 version);
746 err = -EPROTONOSUPPORT;
747 }
748
Peng Tao7405f9e2014-05-29 21:06:58 +0800749 nfs4_clear_ds_conn_bit(ds);
750 } else {
751 nfs4_wait_ds_connect(ds);
Weston Andros Adamsonda066f32017-03-09 12:56:49 -0500752
753 /* what was waited on didn't connect AND didn't mark unavail */
754 if (!ds->ds_clp && !nfs4_test_deviceid_unavailable(devid))
755 goto again;
Peng Tao7405f9e2014-05-29 21:06:58 +0800756 }
Weston Andros Adamsona33e4b02017-03-09 12:56:48 -0500757
758 /*
759 * At this point the ds->ds_clp should be ready, but it might have
760 * hit an error.
761 */
Weston Andros Adamsonda066f32017-03-09 12:56:49 -0500762 if (!err) {
763 if (!ds->ds_clp || !nfs_client_init_is_complete(ds->ds_clp)) {
764 WARN_ON_ONCE(ds->ds_clp ||
765 !nfs4_test_deviceid_unavailable(devid));
766 return -EINVAL;
767 }
768 err = nfs_client_init_status(ds->ds_clp);
Weston Andros Adamsona33e4b02017-03-09 12:56:48 -0500769 }
770
Weston Andros Adamsonda066f32017-03-09 12:56:49 -0500771 return err;
Peng Tao7405f9e2014-05-29 21:06:58 +0800772}
773EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_connect);
774
Peng Tao6b7f3cf2014-05-29 21:06:59 +0800775/*
776 * Currently only supports ipv4, ipv6 and one multi-path address.
777 */
778struct nfs4_pnfs_ds_addr *
779nfs4_decode_mp_ds_addr(struct net *net, struct xdr_stream *xdr, gfp_t gfp_flags)
780{
781 struct nfs4_pnfs_ds_addr *da = NULL;
782 char *buf, *portstr;
783 __be16 port;
784 int nlen, rlen;
785 int tmp[2];
786 __be32 *p;
787 char *netid, *match_netid;
788 size_t len, match_netid_len;
789 char *startsep = "";
790 char *endsep = "";
791
792
793 /* r_netid */
794 p = xdr_inline_decode(xdr, 4);
795 if (unlikely(!p))
796 goto out_err;
797 nlen = be32_to_cpup(p++);
798
799 p = xdr_inline_decode(xdr, nlen);
800 if (unlikely(!p))
801 goto out_err;
802
803 netid = kmalloc(nlen+1, gfp_flags);
804 if (unlikely(!netid))
805 goto out_err;
806
807 netid[nlen] = '\0';
808 memcpy(netid, p, nlen);
809
810 /* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */
811 p = xdr_inline_decode(xdr, 4);
812 if (unlikely(!p))
813 goto out_free_netid;
814 rlen = be32_to_cpup(p);
815
816 p = xdr_inline_decode(xdr, rlen);
817 if (unlikely(!p))
818 goto out_free_netid;
819
820 /* port is ".ABC.DEF", 8 chars max */
821 if (rlen > INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 8) {
822 dprintk("%s: Invalid address, length %d\n", __func__,
823 rlen);
824 goto out_free_netid;
825 }
826 buf = kmalloc(rlen + 1, gfp_flags);
827 if (!buf) {
828 dprintk("%s: Not enough memory\n", __func__);
829 goto out_free_netid;
830 }
831 buf[rlen] = '\0';
832 memcpy(buf, p, rlen);
833
834 /* replace port '.' with '-' */
835 portstr = strrchr(buf, '.');
836 if (!portstr) {
837 dprintk("%s: Failed finding expected dot in port\n",
838 __func__);
839 goto out_free_buf;
840 }
841 *portstr = '-';
842
843 /* find '.' between address and port */
844 portstr = strrchr(buf, '.');
845 if (!portstr) {
846 dprintk("%s: Failed finding expected dot between address and "
847 "port\n", __func__);
848 goto out_free_buf;
849 }
850 *portstr = '\0';
851
852 da = kzalloc(sizeof(*da), gfp_flags);
853 if (unlikely(!da))
854 goto out_free_buf;
855
856 INIT_LIST_HEAD(&da->da_node);
857
858 if (!rpc_pton(net, buf, portstr-buf, (struct sockaddr *)&da->da_addr,
859 sizeof(da->da_addr))) {
860 dprintk("%s: error parsing address %s\n", __func__, buf);
861 goto out_free_da;
862 }
863
864 portstr++;
865 sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]);
866 port = htons((tmp[0] << 8) | (tmp[1]));
867
868 switch (da->da_addr.ss_family) {
869 case AF_INET:
870 ((struct sockaddr_in *)&da->da_addr)->sin_port = port;
871 da->da_addrlen = sizeof(struct sockaddr_in);
872 match_netid = "tcp";
873 match_netid_len = 3;
874 break;
875
876 case AF_INET6:
877 ((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port;
878 da->da_addrlen = sizeof(struct sockaddr_in6);
879 match_netid = "tcp6";
880 match_netid_len = 4;
881 startsep = "[";
882 endsep = "]";
883 break;
884
885 default:
886 dprintk("%s: unsupported address family: %u\n",
887 __func__, da->da_addr.ss_family);
888 goto out_free_da;
889 }
890
891 if (nlen != match_netid_len || strncmp(netid, match_netid, nlen)) {
892 dprintk("%s: ERROR: r_netid \"%s\" != \"%s\"\n",
893 __func__, netid, match_netid);
894 goto out_free_da;
895 }
896
897 /* save human readable address */
898 len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7;
899 da->da_remotestr = kzalloc(len, gfp_flags);
900
901 /* NULL is ok, only used for dprintk */
902 if (da->da_remotestr)
903 snprintf(da->da_remotestr, len, "%s%s%s:%u", startsep,
904 buf, endsep, ntohs(port));
905
906 dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr);
907 kfree(buf);
908 kfree(netid);
909 return da;
910
911out_free_da:
912 kfree(da);
913out_free_buf:
914 dprintk("%s: Error parsing DS addr: %s\n", __func__, buf);
915 kfree(buf);
916out_free_netid:
917 kfree(netid);
918out_err:
919 return NULL;
920}
921EXPORT_SYMBOL_GPL(nfs4_decode_mp_ds_addr);
Tom Haynes338d00c2015-02-17 14:58:15 -0800922
923void
924pnfs_layout_mark_request_commit(struct nfs_page *req,
925 struct pnfs_layout_segment *lseg,
926 struct nfs_commit_info *cinfo,
927 u32 ds_commit_idx)
928{
929 struct list_head *list;
930 struct pnfs_commit_bucket *buckets;
931
Trond Myklebuste824f992017-08-01 11:53:49 -0400932 mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
Tom Haynes338d00c2015-02-17 14:58:15 -0800933 buckets = cinfo->ds->buckets;
934 list = &buckets[ds_commit_idx].written;
935 if (list_empty(list)) {
Trond Myklebustb20135d2015-12-31 09:28:06 -0500936 if (!pnfs_is_valid_lseg(lseg)) {
Trond Myklebuste824f992017-08-01 11:53:49 -0400937 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
Trond Myklebustb20135d2015-12-31 09:28:06 -0500938 cinfo->completion_ops->resched_write(cinfo, req);
939 return;
940 }
Tom Haynes338d00c2015-02-17 14:58:15 -0800941 /* Non-empty buckets hold a reference on the lseg. That ref
942 * is normally transferred to the COMMIT call and released
943 * there. It could also be released if the last req is pulled
944 * off due to a rewrite, in which case it will be done in
945 * pnfs_common_clear_request_commit
946 */
947 WARN_ON_ONCE(buckets[ds_commit_idx].wlseg != NULL);
948 buckets[ds_commit_idx].wlseg = pnfs_get_lseg(lseg);
949 }
950 set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
951 cinfo->ds->nwritten++;
Tom Haynes338d00c2015-02-17 14:58:15 -0800952
Trond Myklebust86d80f92015-07-31 16:24:30 -0400953 nfs_request_add_commit_list_locked(req, list, cinfo);
Trond Myklebuste824f992017-08-01 11:53:49 -0400954 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
Trond Myklebust86d80f92015-07-31 16:24:30 -0400955 nfs_mark_page_unstable(req->wb_page, cinfo);
Tom Haynes338d00c2015-02-17 14:58:15 -0800956}
957EXPORT_SYMBOL_GPL(pnfs_layout_mark_request_commit);
Trond Myklebust5bb89b42015-03-25 14:14:42 -0400958
959int
960pnfs_nfs_generic_sync(struct inode *inode, bool datasync)
961{
Trond Myklebust2e18d4d2016-06-26 18:54:58 -0400962 int ret;
963
964 if (!pnfs_layoutcommit_outstanding(inode))
965 return 0;
966 ret = nfs_commit_inode(inode, FLUSH_SYNC);
967 if (ret < 0)
968 return ret;
Trond Myklebust5bb89b42015-03-25 14:14:42 -0400969 if (datasync)
970 return 0;
971 return pnfs_layoutcommit_inode(inode, true);
972}
973EXPORT_SYMBOL_GPL(pnfs_nfs_generic_sync);
974