blob: 19c1bcf70e3e879707e8194aa51caa3bfa6e637b [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.
Tom Haynes085d1e32014-12-11 13:04:55 -050064 * Note this must be called holding the inode (/cinfo) 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);
101 if (cond_resched_lock(cinfo->lock))
102 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
Tom Haynes085d1e32014-12-11 13:04:55 -0500122 lockdep_assert_held(cinfo->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 Myklebust94d06a42015-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 Myklebust94d06a42015-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
Tom Haynes085d1e32014-12-11 13:04:55 -0500145 lockdep_assert_held(cinfo->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
Tom Haynes085d1e32014-12-11 13:04:55 -0500164 lockdep_assert_held(cinfo->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;
171 spin_unlock(cinfo->lock);
172 pnfs_put_lseg(freeme);
Tom Haynes085d1e32014-12-11 13:04:55 -0500173 spin_lock(cinfo->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 Myklebust94d06a42015-08-03 17:38:33 -0400186 LIST_HEAD(pages);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500187 int i;
188
Trond Myklebust94d06a42015-08-03 17:38:33 -0400189 spin_lock(cinfo->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 Myklebust94d06a42015-08-03 17:38:33 -0400196 list_splice_init(&bucket->committing, &pages);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500197 spin_unlock(cinfo->lock);
Trond Myklebust94d06a42015-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);
Trond Myklebust94d06a42015-08-03 17:38:33 -0400200 spin_lock(cinfo->lock);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500201 }
Trond Myklebust94d06a42015-08-03 17:38:33 -0400202 spin_unlock(cinfo->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;
220 data = nfs_commitdata_alloc();
221 if (!data)
222 break;
223 data->ds_commit_index = i;
Tom Haynesf54bcf22014-12-11 15:34:59 -0500224 list_add(&data->pages, list);
225 nreq++;
226 }
227
228 /* Clean up on error */
229 pnfs_generic_retry_commit(cinfo, i);
230 return nreq;
231}
232
Trond Myklebust94d06a42015-08-03 17:38:33 -0400233static inline
234void pnfs_fetch_commit_bucket_list(struct list_head *pages,
235 struct nfs_commit_data *data,
236 struct nfs_commit_info *cinfo)
237{
238 struct pnfs_commit_bucket *bucket;
239
240 bucket = &cinfo->ds->buckets[data->ds_commit_index];
241 spin_lock(cinfo->lock);
242 list_splice_init(pages, &bucket->committing);
243 data->lseg = bucket->clseg;
244 bucket->clseg = NULL;
245 spin_unlock(cinfo->lock);
246
247}
248
Weston Andros Adamson691c5072016-05-25 10:07:23 -0400249/* Helper function for pnfs_generic_commit_pagelist to catch an empty
Weston Andros Adamsoneba391c2016-06-17 16:48:24 -0400250 * page list. This can happen when two commits race.
251 *
252 * This must be called instead of nfs_init_commit - call one or the other, but
253 * not both!
254 */
Weston Andros Adamson691c5072016-05-25 10:07:23 -0400255static bool
256pnfs_generic_commit_cancel_empty_pagelist(struct list_head *pages,
257 struct nfs_commit_data *data,
258 struct nfs_commit_info *cinfo)
259{
260 if (list_empty(pages)) {
261 if (atomic_dec_and_test(&cinfo->mds->rpcs_out))
262 wake_up_atomic_t(&cinfo->mds->rpcs_out);
Weston Andros Adamsoneba391c2016-06-17 16:48:24 -0400263 /* don't call nfs_commitdata_release - it tries to put
264 * the open_context which is not acquired until nfs_init_commit
265 * which has not been called on @data */
266 WARN_ON_ONCE(data->context);
267 nfs_commit_free(data);
Weston Andros Adamson691c5072016-05-25 10:07:23 -0400268 return true;
269 }
270
271 return false;
272}
273
Tom Haynesf54bcf22014-12-11 15:34:59 -0500274/* This follows nfs_commit_list pretty closely */
275int
276pnfs_generic_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
277 int how, struct nfs_commit_info *cinfo,
278 int (*initiate_commit)(struct nfs_commit_data *data,
279 int how))
280{
281 struct nfs_commit_data *data, *tmp;
282 LIST_HEAD(list);
283 unsigned int nreq = 0;
284
285 if (!list_empty(mds_pages)) {
286 data = nfs_commitdata_alloc();
287 if (data != NULL) {
Trond Myklebust94d06a42015-08-03 17:38:33 -0400288 data->ds_commit_index = -1;
Tom Haynesf54bcf22014-12-11 15:34:59 -0500289 list_add(&data->pages, &list);
290 nreq++;
291 } else {
Weston Andros Adamsonb57ff132014-09-05 18:20:21 -0400292 nfs_retry_commit(mds_pages, NULL, cinfo, 0);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500293 pnfs_generic_retry_commit(cinfo, 0);
294 cinfo->completion_ops->error_cleanup(NFS_I(inode));
295 return -ENOMEM;
296 }
297 }
298
299 nreq += pnfs_generic_alloc_ds_commits(cinfo, &list);
300
301 if (nreq == 0) {
302 cinfo->completion_ops->error_cleanup(NFS_I(inode));
303 goto out;
304 }
305
306 atomic_add(nreq, &cinfo->mds->rpcs_out);
307
308 list_for_each_entry_safe(data, tmp, &list, pages) {
309 list_del_init(&data->pages);
Trond Myklebust94d06a42015-08-03 17:38:33 -0400310 if (data->ds_commit_index < 0) {
Weston Andros Adamson691c5072016-05-25 10:07:23 -0400311 /* another commit raced with us */
312 if (pnfs_generic_commit_cancel_empty_pagelist(mds_pages,
313 data, cinfo))
314 continue;
315
Tom Haynesf54bcf22014-12-11 15:34:59 -0500316 nfs_init_commit(data, mds_pages, NULL, cinfo);
317 nfs_initiate_commit(NFS_CLIENT(inode), data,
Peng Taoc36aae92014-06-09 07:10:14 +0800318 NFS_PROTO(data->inode),
Tom Haynesf54bcf22014-12-11 15:34:59 -0500319 data->mds_ops, how, 0);
320 } else {
Trond Myklebust94d06a42015-08-03 17:38:33 -0400321 LIST_HEAD(pages);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500322
Trond Myklebust94d06a42015-08-03 17:38:33 -0400323 pnfs_fetch_commit_bucket_list(&pages, data, cinfo);
Weston Andros Adamson691c5072016-05-25 10:07:23 -0400324
325 /* another commit raced with us */
326 if (pnfs_generic_commit_cancel_empty_pagelist(&pages,
327 data, cinfo))
328 continue;
329
Trond Myklebust94d06a42015-08-03 17:38:33 -0400330 nfs_init_commit(data, &pages, data->lseg, cinfo);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500331 initiate_commit(data, how);
332 }
333 }
334out:
335 cinfo->ds->ncommitting = 0;
336 return PNFS_ATTEMPTED;
337}
338EXPORT_SYMBOL_GPL(pnfs_generic_commit_pagelist);
Peng Tao875ae062014-05-29 21:06:57 +0800339
340/*
341 * Data server cache
342 *
343 * Data servers can be mapped to different device ids.
344 * nfs4_pnfs_ds reference counting
345 * - set to 1 on allocation
346 * - incremented when a device id maps a data server already in the cache.
347 * - decremented when deviceid is removed from the cache.
348 */
349static DEFINE_SPINLOCK(nfs4_ds_cache_lock);
350static LIST_HEAD(nfs4_data_server_cache);
351
352/* Debug routines */
353static void
354print_ds(struct nfs4_pnfs_ds *ds)
355{
356 if (ds == NULL) {
357 printk(KERN_WARNING "%s NULL device\n", __func__);
358 return;
359 }
360 printk(KERN_WARNING " ds %s\n"
361 " ref count %d\n"
362 " client %p\n"
363 " cl_exchange_flags %x\n",
364 ds->ds_remotestr,
365 atomic_read(&ds->ds_count), ds->ds_clp,
366 ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0);
367}
368
369static bool
370same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2)
371{
372 struct sockaddr_in *a, *b;
373 struct sockaddr_in6 *a6, *b6;
374
375 if (addr1->sa_family != addr2->sa_family)
376 return false;
377
378 switch (addr1->sa_family) {
379 case AF_INET:
380 a = (struct sockaddr_in *)addr1;
381 b = (struct sockaddr_in *)addr2;
382
383 if (a->sin_addr.s_addr == b->sin_addr.s_addr &&
384 a->sin_port == b->sin_port)
385 return true;
386 break;
387
388 case AF_INET6:
389 a6 = (struct sockaddr_in6 *)addr1;
390 b6 = (struct sockaddr_in6 *)addr2;
391
392 /* LINKLOCAL addresses must have matching scope_id */
393 if (ipv6_addr_src_scope(&a6->sin6_addr) ==
394 IPV6_ADDR_SCOPE_LINKLOCAL &&
395 a6->sin6_scope_id != b6->sin6_scope_id)
396 return false;
397
398 if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) &&
399 a6->sin6_port == b6->sin6_port)
400 return true;
401 break;
402
403 default:
404 dprintk("%s: unhandled address family: %u\n",
405 __func__, addr1->sa_family);
406 return false;
407 }
408
409 return false;
410}
411
Trond Myklebust0bdce6a82015-08-13 10:59:07 -0400412/*
413 * Checks if 'dsaddrs1' contains a subset of 'dsaddrs2'. If it does,
414 * declare a match.
415 */
Peng Tao875ae062014-05-29 21:06:57 +0800416static bool
417_same_data_server_addrs_locked(const struct list_head *dsaddrs1,
418 const struct list_head *dsaddrs2)
419{
420 struct nfs4_pnfs_ds_addr *da1, *da2;
Trond Myklebust0bdce6a82015-08-13 10:59:07 -0400421 struct sockaddr *sa1, *sa2;
422 bool match = false;
Peng Tao875ae062014-05-29 21:06:57 +0800423
Trond Myklebust0bdce6a82015-08-13 10:59:07 -0400424 list_for_each_entry(da1, dsaddrs1, da_node) {
425 sa1 = (struct sockaddr *)&da1->da_addr;
426 match = false;
427 list_for_each_entry(da2, dsaddrs2, da_node) {
428 sa2 = (struct sockaddr *)&da2->da_addr;
429 match = same_sockaddr(sa1, sa2);
430 if (match)
431 break;
432 }
433 if (!match)
434 break;
Peng Tao875ae062014-05-29 21:06:57 +0800435 }
Trond Myklebust0bdce6a82015-08-13 10:59:07 -0400436 return match;
Peng Tao875ae062014-05-29 21:06:57 +0800437}
438
439/*
440 * Lookup DS by addresses. nfs4_ds_cache_lock is held
441 */
442static struct nfs4_pnfs_ds *
443_data_server_lookup_locked(const struct list_head *dsaddrs)
444{
445 struct nfs4_pnfs_ds *ds;
446
447 list_for_each_entry(ds, &nfs4_data_server_cache, ds_node)
448 if (_same_data_server_addrs_locked(&ds->ds_addrs, dsaddrs))
449 return ds;
450 return NULL;
451}
452
453static void destroy_ds(struct nfs4_pnfs_ds *ds)
454{
455 struct nfs4_pnfs_ds_addr *da;
456
457 dprintk("--> %s\n", __func__);
458 ifdebug(FACILITY)
459 print_ds(ds);
460
461 nfs_put_client(ds->ds_clp);
462
463 while (!list_empty(&ds->ds_addrs)) {
464 da = list_first_entry(&ds->ds_addrs,
465 struct nfs4_pnfs_ds_addr,
466 da_node);
467 list_del_init(&da->da_node);
468 kfree(da->da_remotestr);
469 kfree(da);
470 }
471
472 kfree(ds->ds_remotestr);
473 kfree(ds);
474}
475
476void nfs4_pnfs_ds_put(struct nfs4_pnfs_ds *ds)
477{
478 if (atomic_dec_and_lock(&ds->ds_count,
479 &nfs4_ds_cache_lock)) {
480 list_del_init(&ds->ds_node);
481 spin_unlock(&nfs4_ds_cache_lock);
482 destroy_ds(ds);
483 }
484}
485EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_put);
486
487/*
488 * Create a string with a human readable address and port to avoid
489 * complicated setup around many dprinks.
490 */
491static char *
492nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags)
493{
494 struct nfs4_pnfs_ds_addr *da;
495 char *remotestr;
496 size_t len;
497 char *p;
498
499 len = 3; /* '{', '}' and eol */
500 list_for_each_entry(da, dsaddrs, da_node) {
501 len += strlen(da->da_remotestr) + 1; /* string plus comma */
502 }
503
504 remotestr = kzalloc(len, gfp_flags);
505 if (!remotestr)
506 return NULL;
507
508 p = remotestr;
509 *(p++) = '{';
510 len--;
511 list_for_each_entry(da, dsaddrs, da_node) {
512 size_t ll = strlen(da->da_remotestr);
513
514 if (ll > len)
515 goto out_err;
516
517 memcpy(p, da->da_remotestr, ll);
518 p += ll;
519 len -= ll;
520
521 if (len < 1)
522 goto out_err;
523 (*p++) = ',';
524 len--;
525 }
526 if (len < 2)
527 goto out_err;
528 *(p++) = '}';
529 *p = '\0';
530 return remotestr;
531out_err:
532 kfree(remotestr);
533 return NULL;
534}
535
536/*
537 * Given a list of multipath struct nfs4_pnfs_ds_addr, add it to ds cache if
538 * uncached and return cached struct nfs4_pnfs_ds.
539 */
540struct nfs4_pnfs_ds *
541nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags)
542{
543 struct nfs4_pnfs_ds *tmp_ds, *ds = NULL;
544 char *remotestr;
545
546 if (list_empty(dsaddrs)) {
547 dprintk("%s: no addresses defined\n", __func__);
548 goto out;
549 }
550
551 ds = kzalloc(sizeof(*ds), gfp_flags);
552 if (!ds)
553 goto out;
554
555 /* this is only used for debugging, so it's ok if its NULL */
556 remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags);
557
558 spin_lock(&nfs4_ds_cache_lock);
559 tmp_ds = _data_server_lookup_locked(dsaddrs);
560 if (tmp_ds == NULL) {
561 INIT_LIST_HEAD(&ds->ds_addrs);
562 list_splice_init(dsaddrs, &ds->ds_addrs);
563 ds->ds_remotestr = remotestr;
564 atomic_set(&ds->ds_count, 1);
565 INIT_LIST_HEAD(&ds->ds_node);
566 ds->ds_clp = NULL;
567 list_add(&ds->ds_node, &nfs4_data_server_cache);
568 dprintk("%s add new data server %s\n", __func__,
569 ds->ds_remotestr);
570 } else {
571 kfree(remotestr);
572 kfree(ds);
573 atomic_inc(&tmp_ds->ds_count);
574 dprintk("%s data server %s found, inc'ed ds_count to %d\n",
575 __func__, tmp_ds->ds_remotestr,
576 atomic_read(&tmp_ds->ds_count));
577 ds = tmp_ds;
578 }
579 spin_unlock(&nfs4_ds_cache_lock);
580out:
581 return ds;
582}
583EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_add);
Peng Tao6b7f3cf2014-05-29 21:06:59 +0800584
Peng Tao7405f9e2014-05-29 21:06:58 +0800585static void nfs4_wait_ds_connect(struct nfs4_pnfs_ds *ds)
586{
587 might_sleep();
588 wait_on_bit(&ds->ds_state, NFS4DS_CONNECTING,
589 TASK_KILLABLE);
590}
591
592static void nfs4_clear_ds_conn_bit(struct nfs4_pnfs_ds *ds)
593{
594 smp_mb__before_atomic();
595 clear_bit(NFS4DS_CONNECTING, &ds->ds_state);
596 smp_mb__after_atomic();
597 wake_up_bit(&ds->ds_state, NFS4DS_CONNECTING);
598}
599
Peng Tao5f01d952014-05-30 18:15:59 +0800600static struct nfs_client *(*get_v3_ds_connect)(
601 struct nfs_client *mds_clp,
602 const struct sockaddr *ds_addr,
603 int ds_addrlen,
604 int ds_proto,
605 unsigned int ds_timeo,
606 unsigned int ds_retrans,
607 rpc_authflavor_t au_flavor);
608
609static bool load_v3_ds_connect(void)
610{
611 if (!get_v3_ds_connect) {
612 get_v3_ds_connect = symbol_request(nfs3_set_ds_client);
613 WARN_ON_ONCE(!get_v3_ds_connect);
614 }
615
616 return(get_v3_ds_connect != NULL);
617}
618
Arnd Bergmanndf137bc2015-03-11 14:37:25 +0100619void nfs4_pnfs_v3_ds_connect_unload(void)
Peng Tao5f01d952014-05-30 18:15:59 +0800620{
621 if (get_v3_ds_connect) {
622 symbol_put(nfs3_set_ds_client);
623 get_v3_ds_connect = NULL;
624 }
625}
626EXPORT_SYMBOL_GPL(nfs4_pnfs_v3_ds_connect_unload);
627
628static int _nfs4_pnfs_v3_ds_connect(struct nfs_server *mds_srv,
629 struct nfs4_pnfs_ds *ds,
630 unsigned int timeo,
631 unsigned int retrans,
632 rpc_authflavor_t au_flavor)
633{
634 struct nfs_client *clp = ERR_PTR(-EIO);
635 struct nfs4_pnfs_ds_addr *da;
636 int status = 0;
637
638 dprintk("--> %s DS %s au_flavor %d\n", __func__,
639 ds->ds_remotestr, au_flavor);
640
641 if (!load_v3_ds_connect())
642 goto out;
643
644 list_for_each_entry(da, &ds->ds_addrs, da_node) {
645 dprintk("%s: DS %s: trying address %s\n",
646 __func__, ds->ds_remotestr, da->da_remotestr);
647
648 clp = get_v3_ds_connect(mds_srv->nfs_client,
649 (struct sockaddr *)&da->da_addr,
650 da->da_addrlen, IPPROTO_TCP,
651 timeo, retrans, au_flavor);
652 if (!IS_ERR(clp))
653 break;
654 }
655
656 if (IS_ERR(clp)) {
657 status = PTR_ERR(clp);
658 goto out;
659 }
660
661 smp_wmb();
662 ds->ds_clp = clp;
663 dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
664out:
665 return status;
666}
667
668static int _nfs4_pnfs_v4_ds_connect(struct nfs_server *mds_srv,
Peng Tao7405f9e2014-05-29 21:06:58 +0800669 struct nfs4_pnfs_ds *ds,
670 unsigned int timeo,
Peng Tao064172f2014-05-29 21:07:00 +0800671 unsigned int retrans,
Peng Tao30626f92014-05-30 18:15:58 +0800672 u32 minor_version,
Peng Tao064172f2014-05-29 21:07:00 +0800673 rpc_authflavor_t au_flavor)
Peng Tao7405f9e2014-05-29 21:06:58 +0800674{
675 struct nfs_client *clp = ERR_PTR(-EIO);
676 struct nfs4_pnfs_ds_addr *da;
677 int status = 0;
678
679 dprintk("--> %s DS %s au_flavor %d\n", __func__, ds->ds_remotestr,
Peng Tao5f01d952014-05-30 18:15:59 +0800680 au_flavor);
Peng Tao7405f9e2014-05-29 21:06:58 +0800681
682 list_for_each_entry(da, &ds->ds_addrs, da_node) {
683 dprintk("%s: DS %s: trying address %s\n",
684 __func__, ds->ds_remotestr, da->da_remotestr);
685
686 clp = nfs4_set_ds_client(mds_srv->nfs_client,
687 (struct sockaddr *)&da->da_addr,
688 da->da_addrlen, IPPROTO_TCP,
Peng Tao30626f92014-05-30 18:15:58 +0800689 timeo, retrans, minor_version,
690 au_flavor);
Peng Tao7405f9e2014-05-29 21:06:58 +0800691 if (!IS_ERR(clp))
692 break;
693 }
694
695 if (IS_ERR(clp)) {
696 status = PTR_ERR(clp);
697 goto out;
698 }
699
700 status = nfs4_init_ds_session(clp, mds_srv->nfs_client->cl_lease_time);
701 if (status)
702 goto out_put;
703
704 smp_wmb();
705 ds->ds_clp = clp;
706 dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
707out:
708 return status;
709out_put:
710 nfs_put_client(clp);
711 goto out;
712}
713
714/*
715 * Create an rpc connection to the nfs4_pnfs_ds data server.
716 * Currently only supports IPv4 and IPv6 addresses.
717 * If connection fails, make devid unavailable.
718 */
719void nfs4_pnfs_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds,
720 struct nfs4_deviceid_node *devid, unsigned int timeo,
Peng Tao30626f92014-05-30 18:15:58 +0800721 unsigned int retrans, u32 version,
722 u32 minor_version, rpc_authflavor_t au_flavor)
Peng Tao7405f9e2014-05-29 21:06:58 +0800723{
724 if (test_and_set_bit(NFS4DS_CONNECTING, &ds->ds_state) == 0) {
725 int err = 0;
726
Peng Tao5f01d952014-05-30 18:15:59 +0800727 if (version == 3) {
728 err = _nfs4_pnfs_v3_ds_connect(mds_srv, ds, timeo,
729 retrans, au_flavor);
730 } else if (version == 4) {
731 err = _nfs4_pnfs_v4_ds_connect(mds_srv, ds, timeo,
732 retrans, minor_version,
733 au_flavor);
734 } else {
735 dprintk("%s: unsupported DS version %d\n", __func__,
736 version);
737 err = -EPROTONOSUPPORT;
738 }
739
Peng Tao7405f9e2014-05-29 21:06:58 +0800740 if (err)
741 nfs4_mark_deviceid_unavailable(devid);
742 nfs4_clear_ds_conn_bit(ds);
743 } else {
744 nfs4_wait_ds_connect(ds);
745 }
746}
747EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_connect);
748
Peng Tao6b7f3cf2014-05-29 21:06:59 +0800749/*
750 * Currently only supports ipv4, ipv6 and one multi-path address.
751 */
752struct nfs4_pnfs_ds_addr *
753nfs4_decode_mp_ds_addr(struct net *net, struct xdr_stream *xdr, gfp_t gfp_flags)
754{
755 struct nfs4_pnfs_ds_addr *da = NULL;
756 char *buf, *portstr;
757 __be16 port;
758 int nlen, rlen;
759 int tmp[2];
760 __be32 *p;
761 char *netid, *match_netid;
762 size_t len, match_netid_len;
763 char *startsep = "";
764 char *endsep = "";
765
766
767 /* r_netid */
768 p = xdr_inline_decode(xdr, 4);
769 if (unlikely(!p))
770 goto out_err;
771 nlen = be32_to_cpup(p++);
772
773 p = xdr_inline_decode(xdr, nlen);
774 if (unlikely(!p))
775 goto out_err;
776
777 netid = kmalloc(nlen+1, gfp_flags);
778 if (unlikely(!netid))
779 goto out_err;
780
781 netid[nlen] = '\0';
782 memcpy(netid, p, nlen);
783
784 /* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */
785 p = xdr_inline_decode(xdr, 4);
786 if (unlikely(!p))
787 goto out_free_netid;
788 rlen = be32_to_cpup(p);
789
790 p = xdr_inline_decode(xdr, rlen);
791 if (unlikely(!p))
792 goto out_free_netid;
793
794 /* port is ".ABC.DEF", 8 chars max */
795 if (rlen > INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 8) {
796 dprintk("%s: Invalid address, length %d\n", __func__,
797 rlen);
798 goto out_free_netid;
799 }
800 buf = kmalloc(rlen + 1, gfp_flags);
801 if (!buf) {
802 dprintk("%s: Not enough memory\n", __func__);
803 goto out_free_netid;
804 }
805 buf[rlen] = '\0';
806 memcpy(buf, p, rlen);
807
808 /* replace port '.' with '-' */
809 portstr = strrchr(buf, '.');
810 if (!portstr) {
811 dprintk("%s: Failed finding expected dot in port\n",
812 __func__);
813 goto out_free_buf;
814 }
815 *portstr = '-';
816
817 /* find '.' between address and port */
818 portstr = strrchr(buf, '.');
819 if (!portstr) {
820 dprintk("%s: Failed finding expected dot between address and "
821 "port\n", __func__);
822 goto out_free_buf;
823 }
824 *portstr = '\0';
825
826 da = kzalloc(sizeof(*da), gfp_flags);
827 if (unlikely(!da))
828 goto out_free_buf;
829
830 INIT_LIST_HEAD(&da->da_node);
831
832 if (!rpc_pton(net, buf, portstr-buf, (struct sockaddr *)&da->da_addr,
833 sizeof(da->da_addr))) {
834 dprintk("%s: error parsing address %s\n", __func__, buf);
835 goto out_free_da;
836 }
837
838 portstr++;
839 sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]);
840 port = htons((tmp[0] << 8) | (tmp[1]));
841
842 switch (da->da_addr.ss_family) {
843 case AF_INET:
844 ((struct sockaddr_in *)&da->da_addr)->sin_port = port;
845 da->da_addrlen = sizeof(struct sockaddr_in);
846 match_netid = "tcp";
847 match_netid_len = 3;
848 break;
849
850 case AF_INET6:
851 ((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port;
852 da->da_addrlen = sizeof(struct sockaddr_in6);
853 match_netid = "tcp6";
854 match_netid_len = 4;
855 startsep = "[";
856 endsep = "]";
857 break;
858
859 default:
860 dprintk("%s: unsupported address family: %u\n",
861 __func__, da->da_addr.ss_family);
862 goto out_free_da;
863 }
864
865 if (nlen != match_netid_len || strncmp(netid, match_netid, nlen)) {
866 dprintk("%s: ERROR: r_netid \"%s\" != \"%s\"\n",
867 __func__, netid, match_netid);
868 goto out_free_da;
869 }
870
871 /* save human readable address */
872 len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7;
873 da->da_remotestr = kzalloc(len, gfp_flags);
874
875 /* NULL is ok, only used for dprintk */
876 if (da->da_remotestr)
877 snprintf(da->da_remotestr, len, "%s%s%s:%u", startsep,
878 buf, endsep, ntohs(port));
879
880 dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr);
881 kfree(buf);
882 kfree(netid);
883 return da;
884
885out_free_da:
886 kfree(da);
887out_free_buf:
888 dprintk("%s: Error parsing DS addr: %s\n", __func__, buf);
889 kfree(buf);
890out_free_netid:
891 kfree(netid);
892out_err:
893 return NULL;
894}
895EXPORT_SYMBOL_GPL(nfs4_decode_mp_ds_addr);
Tom Haynes338d00c2015-02-17 14:58:15 -0800896
897void
898pnfs_layout_mark_request_commit(struct nfs_page *req,
899 struct pnfs_layout_segment *lseg,
900 struct nfs_commit_info *cinfo,
901 u32 ds_commit_idx)
902{
903 struct list_head *list;
904 struct pnfs_commit_bucket *buckets;
905
906 spin_lock(cinfo->lock);
907 buckets = cinfo->ds->buckets;
908 list = &buckets[ds_commit_idx].written;
909 if (list_empty(list)) {
910 /* Non-empty buckets hold a reference on the lseg. That ref
911 * is normally transferred to the COMMIT call and released
912 * there. It could also be released if the last req is pulled
913 * off due to a rewrite, in which case it will be done in
914 * pnfs_common_clear_request_commit
915 */
916 WARN_ON_ONCE(buckets[ds_commit_idx].wlseg != NULL);
917 buckets[ds_commit_idx].wlseg = pnfs_get_lseg(lseg);
918 }
919 set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
920 cinfo->ds->nwritten++;
921 spin_unlock(cinfo->lock);
922
923 nfs_request_add_commit_list(req, list, cinfo);
924}
925EXPORT_SYMBOL_GPL(pnfs_layout_mark_request_commit);
Trond Myklebust5bb89b42015-03-25 14:14:42 -0400926
927int
928pnfs_nfs_generic_sync(struct inode *inode, bool datasync)
929{
930 if (datasync)
931 return 0;
932 return pnfs_layoutcommit_inode(inode, true);
933}
934EXPORT_SYMBOL_GPL(pnfs_nfs_generic_sync);
935