pnfs_nfs.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. /*
  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. #include <linux/nfs_fs.h>
  10. #include <linux/nfs_page.h>
  11. #include <linux/sunrpc/addr.h>
  12. #include <linux/module.h>
  13. #include "nfs4session.h"
  14. #include "internal.h"
  15. #include "pnfs.h"
  16. #define NFSDBG_FACILITY NFSDBG_PNFS
  17. void pnfs_generic_rw_release(void *data)
  18. {
  19. struct nfs_pgio_header *hdr = data;
  20. nfs_put_client(hdr->ds_clp);
  21. hdr->mds_ops->rpc_release(data);
  22. }
  23. EXPORT_SYMBOL_GPL(pnfs_generic_rw_release);
  24. /* Fake up some data that will cause nfs_commit_release to retry the writes. */
  25. void pnfs_generic_prepare_to_resend_writes(struct nfs_commit_data *data)
  26. {
  27. struct nfs_page *first = nfs_list_entry(data->pages.next);
  28. data->task.tk_status = 0;
  29. memcpy(&data->verf.verifier, &first->wb_verf,
  30. sizeof(data->verf.verifier));
  31. data->verf.verifier.data[0]++; /* ensure verifier mismatch */
  32. }
  33. EXPORT_SYMBOL_GPL(pnfs_generic_prepare_to_resend_writes);
  34. void pnfs_generic_write_commit_done(struct rpc_task *task, void *data)
  35. {
  36. struct nfs_commit_data *wdata = data;
  37. /* Note this may cause RPC to be resent */
  38. wdata->mds_ops->rpc_call_done(task, data);
  39. }
  40. EXPORT_SYMBOL_GPL(pnfs_generic_write_commit_done);
  41. void pnfs_generic_commit_release(void *calldata)
  42. {
  43. struct nfs_commit_data *data = calldata;
  44. data->completion_ops->completion(data);
  45. pnfs_put_lseg(data->lseg);
  46. nfs_put_client(data->ds_clp);
  47. nfs_commitdata_release(data);
  48. }
  49. EXPORT_SYMBOL_GPL(pnfs_generic_commit_release);
  50. /* The generic layer is about to remove the req from the commit list.
  51. * If this will make the bucket empty, it will need to put the lseg reference.
  52. * Note this must be called holding nfsi->commit_mutex
  53. */
  54. void
  55. pnfs_generic_clear_request_commit(struct nfs_page *req,
  56. struct nfs_commit_info *cinfo)
  57. {
  58. struct pnfs_layout_segment *freeme = NULL;
  59. if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags))
  60. goto out;
  61. cinfo->ds->nwritten--;
  62. if (list_is_singular(&req->wb_list)) {
  63. struct pnfs_commit_bucket *bucket;
  64. bucket = list_first_entry(&req->wb_list,
  65. struct pnfs_commit_bucket,
  66. written);
  67. freeme = bucket->wlseg;
  68. bucket->wlseg = NULL;
  69. }
  70. out:
  71. nfs_request_remove_commit_list(req, cinfo);
  72. pnfs_put_lseg(freeme);
  73. }
  74. EXPORT_SYMBOL_GPL(pnfs_generic_clear_request_commit);
  75. static int
  76. pnfs_generic_scan_ds_commit_list(struct pnfs_commit_bucket *bucket,
  77. struct nfs_commit_info *cinfo,
  78. int max)
  79. {
  80. struct list_head *src = &bucket->written;
  81. struct list_head *dst = &bucket->committing;
  82. int ret;
  83. lockdep_assert_held(&NFS_I(cinfo->inode)->commit_mutex);
  84. ret = nfs_scan_commit_list(src, dst, cinfo, max);
  85. if (ret) {
  86. cinfo->ds->nwritten -= ret;
  87. cinfo->ds->ncommitting += ret;
  88. if (bucket->clseg == NULL)
  89. bucket->clseg = pnfs_get_lseg(bucket->wlseg);
  90. if (list_empty(src)) {
  91. pnfs_put_lseg(bucket->wlseg);
  92. bucket->wlseg = NULL;
  93. }
  94. }
  95. return ret;
  96. }
  97. /* Move reqs from written to committing lists, returning count
  98. * of number moved.
  99. */
  100. int pnfs_generic_scan_commit_lists(struct nfs_commit_info *cinfo,
  101. int max)
  102. {
  103. int i, rv = 0, cnt;
  104. lockdep_assert_held(&NFS_I(cinfo->inode)->commit_mutex);
  105. for (i = 0; i < cinfo->ds->nbuckets && max != 0; i++) {
  106. cnt = pnfs_generic_scan_ds_commit_list(&cinfo->ds->buckets[i],
  107. cinfo, max);
  108. max -= cnt;
  109. rv += cnt;
  110. }
  111. return rv;
  112. }
  113. EXPORT_SYMBOL_GPL(pnfs_generic_scan_commit_lists);
  114. /* Pull everything off the committing lists and dump into @dst. */
  115. void pnfs_generic_recover_commit_reqs(struct list_head *dst,
  116. struct nfs_commit_info *cinfo)
  117. {
  118. struct pnfs_commit_bucket *b;
  119. struct pnfs_layout_segment *freeme;
  120. int nwritten;
  121. int i;
  122. lockdep_assert_held(&NFS_I(cinfo->inode)->commit_mutex);
  123. restart:
  124. for (i = 0, b = cinfo->ds->buckets; i < cinfo->ds->nbuckets; i++, b++) {
  125. nwritten = nfs_scan_commit_list(&b->written, dst, cinfo, 0);
  126. if (!nwritten)
  127. continue;
  128. cinfo->ds->nwritten -= nwritten;
  129. if (list_empty(&b->written)) {
  130. freeme = b->wlseg;
  131. b->wlseg = NULL;
  132. pnfs_put_lseg(freeme);
  133. goto restart;
  134. }
  135. }
  136. }
  137. EXPORT_SYMBOL_GPL(pnfs_generic_recover_commit_reqs);
  138. static void pnfs_generic_retry_commit(struct nfs_commit_info *cinfo, int idx)
  139. {
  140. struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds;
  141. struct pnfs_commit_bucket *bucket;
  142. struct pnfs_layout_segment *freeme;
  143. struct list_head *pos;
  144. LIST_HEAD(pages);
  145. int i;
  146. mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
  147. for (i = idx; i < fl_cinfo->nbuckets; i++) {
  148. bucket = &fl_cinfo->buckets[i];
  149. if (list_empty(&bucket->committing))
  150. continue;
  151. freeme = bucket->clseg;
  152. bucket->clseg = NULL;
  153. list_for_each(pos, &bucket->committing)
  154. cinfo->ds->ncommitting--;
  155. list_splice_init(&bucket->committing, &pages);
  156. mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
  157. nfs_retry_commit(&pages, freeme, cinfo, i);
  158. pnfs_put_lseg(freeme);
  159. mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
  160. }
  161. mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
  162. }
  163. static unsigned int
  164. pnfs_generic_alloc_ds_commits(struct nfs_commit_info *cinfo,
  165. struct list_head *list)
  166. {
  167. struct pnfs_ds_commit_info *fl_cinfo;
  168. struct pnfs_commit_bucket *bucket;
  169. struct nfs_commit_data *data;
  170. int i;
  171. unsigned int nreq = 0;
  172. fl_cinfo = cinfo->ds;
  173. bucket = fl_cinfo->buckets;
  174. for (i = 0; i < fl_cinfo->nbuckets; i++, bucket++) {
  175. if (list_empty(&bucket->committing))
  176. continue;
  177. data = nfs_commitdata_alloc(false);
  178. if (!data)
  179. break;
  180. data->ds_commit_index = i;
  181. list_add(&data->pages, list);
  182. nreq++;
  183. }
  184. /* Clean up on error */
  185. pnfs_generic_retry_commit(cinfo, i);
  186. return nreq;
  187. }
  188. static inline
  189. void pnfs_fetch_commit_bucket_list(struct list_head *pages,
  190. struct nfs_commit_data *data,
  191. struct nfs_commit_info *cinfo)
  192. {
  193. struct pnfs_commit_bucket *bucket;
  194. struct list_head *pos;
  195. bucket = &cinfo->ds->buckets[data->ds_commit_index];
  196. mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
  197. list_for_each(pos, &bucket->committing)
  198. cinfo->ds->ncommitting--;
  199. list_splice_init(&bucket->committing, pages);
  200. data->lseg = bucket->clseg;
  201. bucket->clseg = NULL;
  202. mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
  203. }
  204. /* Helper function for pnfs_generic_commit_pagelist to catch an empty
  205. * page list. This can happen when two commits race.
  206. *
  207. * This must be called instead of nfs_init_commit - call one or the other, but
  208. * not both!
  209. */
  210. static bool
  211. pnfs_generic_commit_cancel_empty_pagelist(struct list_head *pages,
  212. struct nfs_commit_data *data,
  213. struct nfs_commit_info *cinfo)
  214. {
  215. if (list_empty(pages)) {
  216. if (atomic_dec_and_test(&cinfo->mds->rpcs_out))
  217. wake_up_var(&cinfo->mds->rpcs_out);
  218. /* don't call nfs_commitdata_release - it tries to put
  219. * the open_context which is not acquired until nfs_init_commit
  220. * which has not been called on @data */
  221. WARN_ON_ONCE(data->context);
  222. nfs_commit_free(data);
  223. return true;
  224. }
  225. return false;
  226. }
  227. /* This follows nfs_commit_list pretty closely */
  228. int
  229. pnfs_generic_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
  230. int how, struct nfs_commit_info *cinfo,
  231. int (*initiate_commit)(struct nfs_commit_data *data,
  232. int how))
  233. {
  234. struct nfs_commit_data *data, *tmp;
  235. LIST_HEAD(list);
  236. unsigned int nreq = 0;
  237. if (!list_empty(mds_pages)) {
  238. data = nfs_commitdata_alloc(true);
  239. data->ds_commit_index = -1;
  240. list_add(&data->pages, &list);
  241. nreq++;
  242. }
  243. nreq += pnfs_generic_alloc_ds_commits(cinfo, &list);
  244. if (nreq == 0)
  245. goto out;
  246. atomic_add(nreq, &cinfo->mds->rpcs_out);
  247. list_for_each_entry_safe(data, tmp, &list, pages) {
  248. list_del_init(&data->pages);
  249. if (data->ds_commit_index < 0) {
  250. /* another commit raced with us */
  251. if (pnfs_generic_commit_cancel_empty_pagelist(mds_pages,
  252. data, cinfo))
  253. continue;
  254. nfs_init_commit(data, mds_pages, NULL, cinfo);
  255. nfs_initiate_commit(NFS_CLIENT(inode), data,
  256. NFS_PROTO(data->inode),
  257. data->mds_ops, how, 0);
  258. } else {
  259. LIST_HEAD(pages);
  260. pnfs_fetch_commit_bucket_list(&pages, data, cinfo);
  261. /* another commit raced with us */
  262. if (pnfs_generic_commit_cancel_empty_pagelist(&pages,
  263. data, cinfo))
  264. continue;
  265. nfs_init_commit(data, &pages, data->lseg, cinfo);
  266. initiate_commit(data, how);
  267. }
  268. }
  269. out:
  270. return PNFS_ATTEMPTED;
  271. }
  272. EXPORT_SYMBOL_GPL(pnfs_generic_commit_pagelist);
  273. /*
  274. * Data server cache
  275. *
  276. * Data servers can be mapped to different device ids.
  277. * nfs4_pnfs_ds reference counting
  278. * - set to 1 on allocation
  279. * - incremented when a device id maps a data server already in the cache.
  280. * - decremented when deviceid is removed from the cache.
  281. */
  282. static DEFINE_SPINLOCK(nfs4_ds_cache_lock);
  283. static LIST_HEAD(nfs4_data_server_cache);
  284. /* Debug routines */
  285. static void
  286. print_ds(struct nfs4_pnfs_ds *ds)
  287. {
  288. if (ds == NULL) {
  289. printk(KERN_WARNING "%s NULL device\n", __func__);
  290. return;
  291. }
  292. printk(KERN_WARNING " ds %s\n"
  293. " ref count %d\n"
  294. " client %p\n"
  295. " cl_exchange_flags %x\n",
  296. ds->ds_remotestr,
  297. refcount_read(&ds->ds_count), ds->ds_clp,
  298. ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0);
  299. }
  300. static bool
  301. same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2)
  302. {
  303. struct sockaddr_in *a, *b;
  304. struct sockaddr_in6 *a6, *b6;
  305. if (addr1->sa_family != addr2->sa_family)
  306. return false;
  307. switch (addr1->sa_family) {
  308. case AF_INET:
  309. a = (struct sockaddr_in *)addr1;
  310. b = (struct sockaddr_in *)addr2;
  311. if (a->sin_addr.s_addr == b->sin_addr.s_addr &&
  312. a->sin_port == b->sin_port)
  313. return true;
  314. break;
  315. case AF_INET6:
  316. a6 = (struct sockaddr_in6 *)addr1;
  317. b6 = (struct sockaddr_in6 *)addr2;
  318. /* LINKLOCAL addresses must have matching scope_id */
  319. if (ipv6_addr_src_scope(&a6->sin6_addr) ==
  320. IPV6_ADDR_SCOPE_LINKLOCAL &&
  321. a6->sin6_scope_id != b6->sin6_scope_id)
  322. return false;
  323. if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) &&
  324. a6->sin6_port == b6->sin6_port)
  325. return true;
  326. break;
  327. default:
  328. dprintk("%s: unhandled address family: %u\n",
  329. __func__, addr1->sa_family);
  330. return false;
  331. }
  332. return false;
  333. }
  334. /*
  335. * Checks if 'dsaddrs1' contains a subset of 'dsaddrs2'. If it does,
  336. * declare a match.
  337. */
  338. static bool
  339. _same_data_server_addrs_locked(const struct list_head *dsaddrs1,
  340. const struct list_head *dsaddrs2)
  341. {
  342. struct nfs4_pnfs_ds_addr *da1, *da2;
  343. struct sockaddr *sa1, *sa2;
  344. bool match = false;
  345. list_for_each_entry(da1, dsaddrs1, da_node) {
  346. sa1 = (struct sockaddr *)&da1->da_addr;
  347. match = false;
  348. list_for_each_entry(da2, dsaddrs2, da_node) {
  349. sa2 = (struct sockaddr *)&da2->da_addr;
  350. match = same_sockaddr(sa1, sa2);
  351. if (match)
  352. break;
  353. }
  354. if (!match)
  355. break;
  356. }
  357. return match;
  358. }
  359. /*
  360. * Lookup DS by addresses. nfs4_ds_cache_lock is held
  361. */
  362. static struct nfs4_pnfs_ds *
  363. _data_server_lookup_locked(const struct list_head *dsaddrs)
  364. {
  365. struct nfs4_pnfs_ds *ds;
  366. list_for_each_entry(ds, &nfs4_data_server_cache, ds_node)
  367. if (_same_data_server_addrs_locked(&ds->ds_addrs, dsaddrs))
  368. return ds;
  369. return NULL;
  370. }
  371. static void destroy_ds(struct nfs4_pnfs_ds *ds)
  372. {
  373. struct nfs4_pnfs_ds_addr *da;
  374. dprintk("--> %s\n", __func__);
  375. ifdebug(FACILITY)
  376. print_ds(ds);
  377. nfs_put_client(ds->ds_clp);
  378. while (!list_empty(&ds->ds_addrs)) {
  379. da = list_first_entry(&ds->ds_addrs,
  380. struct nfs4_pnfs_ds_addr,
  381. da_node);
  382. list_del_init(&da->da_node);
  383. kfree(da->da_remotestr);
  384. kfree(da);
  385. }
  386. kfree(ds->ds_remotestr);
  387. kfree(ds);
  388. }
  389. void nfs4_pnfs_ds_put(struct nfs4_pnfs_ds *ds)
  390. {
  391. if (refcount_dec_and_lock(&ds->ds_count,
  392. &nfs4_ds_cache_lock)) {
  393. list_del_init(&ds->ds_node);
  394. spin_unlock(&nfs4_ds_cache_lock);
  395. destroy_ds(ds);
  396. }
  397. }
  398. EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_put);
  399. /*
  400. * Create a string with a human readable address and port to avoid
  401. * complicated setup around many dprinks.
  402. */
  403. static char *
  404. nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags)
  405. {
  406. struct nfs4_pnfs_ds_addr *da;
  407. char *remotestr;
  408. size_t len;
  409. char *p;
  410. len = 3; /* '{', '}' and eol */
  411. list_for_each_entry(da, dsaddrs, da_node) {
  412. len += strlen(da->da_remotestr) + 1; /* string plus comma */
  413. }
  414. remotestr = kzalloc(len, gfp_flags);
  415. if (!remotestr)
  416. return NULL;
  417. p = remotestr;
  418. *(p++) = '{';
  419. len--;
  420. list_for_each_entry(da, dsaddrs, da_node) {
  421. size_t ll = strlen(da->da_remotestr);
  422. if (ll > len)
  423. goto out_err;
  424. memcpy(p, da->da_remotestr, ll);
  425. p += ll;
  426. len -= ll;
  427. if (len < 1)
  428. goto out_err;
  429. (*p++) = ',';
  430. len--;
  431. }
  432. if (len < 2)
  433. goto out_err;
  434. *(p++) = '}';
  435. *p = '\0';
  436. return remotestr;
  437. out_err:
  438. kfree(remotestr);
  439. return NULL;
  440. }
  441. /*
  442. * Given a list of multipath struct nfs4_pnfs_ds_addr, add it to ds cache if
  443. * uncached and return cached struct nfs4_pnfs_ds.
  444. */
  445. struct nfs4_pnfs_ds *
  446. nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags)
  447. {
  448. struct nfs4_pnfs_ds *tmp_ds, *ds = NULL;
  449. char *remotestr;
  450. if (list_empty(dsaddrs)) {
  451. dprintk("%s: no addresses defined\n", __func__);
  452. goto out;
  453. }
  454. ds = kzalloc(sizeof(*ds), gfp_flags);
  455. if (!ds)
  456. goto out;
  457. /* this is only used for debugging, so it's ok if its NULL */
  458. remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags);
  459. spin_lock(&nfs4_ds_cache_lock);
  460. tmp_ds = _data_server_lookup_locked(dsaddrs);
  461. if (tmp_ds == NULL) {
  462. INIT_LIST_HEAD(&ds->ds_addrs);
  463. list_splice_init(dsaddrs, &ds->ds_addrs);
  464. ds->ds_remotestr = remotestr;
  465. refcount_set(&ds->ds_count, 1);
  466. INIT_LIST_HEAD(&ds->ds_node);
  467. ds->ds_clp = NULL;
  468. list_add(&ds->ds_node, &nfs4_data_server_cache);
  469. dprintk("%s add new data server %s\n", __func__,
  470. ds->ds_remotestr);
  471. } else {
  472. kfree(remotestr);
  473. kfree(ds);
  474. refcount_inc(&tmp_ds->ds_count);
  475. dprintk("%s data server %s found, inc'ed ds_count to %d\n",
  476. __func__, tmp_ds->ds_remotestr,
  477. refcount_read(&tmp_ds->ds_count));
  478. ds = tmp_ds;
  479. }
  480. spin_unlock(&nfs4_ds_cache_lock);
  481. out:
  482. return ds;
  483. }
  484. EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_add);
  485. static void nfs4_wait_ds_connect(struct nfs4_pnfs_ds *ds)
  486. {
  487. might_sleep();
  488. wait_on_bit(&ds->ds_state, NFS4DS_CONNECTING,
  489. TASK_KILLABLE);
  490. }
  491. static void nfs4_clear_ds_conn_bit(struct nfs4_pnfs_ds *ds)
  492. {
  493. smp_mb__before_atomic();
  494. clear_bit(NFS4DS_CONNECTING, &ds->ds_state);
  495. smp_mb__after_atomic();
  496. wake_up_bit(&ds->ds_state, NFS4DS_CONNECTING);
  497. }
  498. static struct nfs_client *(*get_v3_ds_connect)(
  499. struct nfs_server *mds_srv,
  500. const struct sockaddr *ds_addr,
  501. int ds_addrlen,
  502. int ds_proto,
  503. unsigned int ds_timeo,
  504. unsigned int ds_retrans);
  505. static bool load_v3_ds_connect(void)
  506. {
  507. if (!get_v3_ds_connect) {
  508. get_v3_ds_connect = symbol_request(nfs3_set_ds_client);
  509. WARN_ON_ONCE(!get_v3_ds_connect);
  510. }
  511. return(get_v3_ds_connect != NULL);
  512. }
  513. void nfs4_pnfs_v3_ds_connect_unload(void)
  514. {
  515. if (get_v3_ds_connect) {
  516. symbol_put(nfs3_set_ds_client);
  517. get_v3_ds_connect = NULL;
  518. }
  519. }
  520. static int _nfs4_pnfs_v3_ds_connect(struct nfs_server *mds_srv,
  521. struct nfs4_pnfs_ds *ds,
  522. unsigned int timeo,
  523. unsigned int retrans)
  524. {
  525. struct nfs_client *clp = ERR_PTR(-EIO);
  526. struct nfs4_pnfs_ds_addr *da;
  527. int status = 0;
  528. dprintk("--> %s DS %s\n", __func__, ds->ds_remotestr);
  529. if (!load_v3_ds_connect())
  530. goto out;
  531. list_for_each_entry(da, &ds->ds_addrs, da_node) {
  532. dprintk("%s: DS %s: trying address %s\n",
  533. __func__, ds->ds_remotestr, da->da_remotestr);
  534. if (!IS_ERR(clp)) {
  535. struct xprt_create xprt_args = {
  536. .ident = XPRT_TRANSPORT_TCP,
  537. .net = clp->cl_net,
  538. .dstaddr = (struct sockaddr *)&da->da_addr,
  539. .addrlen = da->da_addrlen,
  540. .servername = clp->cl_hostname,
  541. };
  542. /* Add this address as an alias */
  543. rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
  544. rpc_clnt_test_and_add_xprt, NULL);
  545. } else
  546. clp = get_v3_ds_connect(mds_srv,
  547. (struct sockaddr *)&da->da_addr,
  548. da->da_addrlen, IPPROTO_TCP,
  549. timeo, retrans);
  550. }
  551. if (IS_ERR(clp)) {
  552. status = PTR_ERR(clp);
  553. goto out;
  554. }
  555. smp_wmb();
  556. ds->ds_clp = clp;
  557. dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
  558. out:
  559. return status;
  560. }
  561. static int _nfs4_pnfs_v4_ds_connect(struct nfs_server *mds_srv,
  562. struct nfs4_pnfs_ds *ds,
  563. unsigned int timeo,
  564. unsigned int retrans,
  565. u32 minor_version)
  566. {
  567. struct nfs_client *clp = ERR_PTR(-EIO);
  568. struct nfs4_pnfs_ds_addr *da;
  569. int status = 0;
  570. dprintk("--> %s DS %s\n", __func__, ds->ds_remotestr);
  571. list_for_each_entry(da, &ds->ds_addrs, da_node) {
  572. dprintk("%s: DS %s: trying address %s\n",
  573. __func__, ds->ds_remotestr, da->da_remotestr);
  574. if (!IS_ERR(clp) && clp->cl_mvops->session_trunk) {
  575. struct xprt_create xprt_args = {
  576. .ident = XPRT_TRANSPORT_TCP,
  577. .net = clp->cl_net,
  578. .dstaddr = (struct sockaddr *)&da->da_addr,
  579. .addrlen = da->da_addrlen,
  580. .servername = clp->cl_hostname,
  581. };
  582. struct nfs4_add_xprt_data xprtdata = {
  583. .clp = clp,
  584. .cred = nfs4_get_clid_cred(clp),
  585. };
  586. struct rpc_add_xprt_test rpcdata = {
  587. .add_xprt_test = clp->cl_mvops->session_trunk,
  588. .data = &xprtdata,
  589. };
  590. /**
  591. * Test this address for session trunking and
  592. * add as an alias
  593. */
  594. rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
  595. rpc_clnt_setup_test_and_add_xprt,
  596. &rpcdata);
  597. if (xprtdata.cred)
  598. put_rpccred(xprtdata.cred);
  599. } else {
  600. clp = nfs4_set_ds_client(mds_srv,
  601. (struct sockaddr *)&da->da_addr,
  602. da->da_addrlen, IPPROTO_TCP,
  603. timeo, retrans, minor_version);
  604. if (IS_ERR(clp))
  605. continue;
  606. status = nfs4_init_ds_session(clp,
  607. mds_srv->nfs_client->cl_lease_time);
  608. if (status) {
  609. nfs_put_client(clp);
  610. clp = ERR_PTR(-EIO);
  611. continue;
  612. }
  613. }
  614. }
  615. if (IS_ERR(clp)) {
  616. status = PTR_ERR(clp);
  617. goto out;
  618. }
  619. smp_wmb();
  620. ds->ds_clp = clp;
  621. dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
  622. out:
  623. return status;
  624. }
  625. /*
  626. * Create an rpc connection to the nfs4_pnfs_ds data server.
  627. * Currently only supports IPv4 and IPv6 addresses.
  628. * If connection fails, make devid unavailable and return a -errno.
  629. */
  630. int nfs4_pnfs_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds,
  631. struct nfs4_deviceid_node *devid, unsigned int timeo,
  632. unsigned int retrans, u32 version, u32 minor_version)
  633. {
  634. int err;
  635. again:
  636. err = 0;
  637. if (test_and_set_bit(NFS4DS_CONNECTING, &ds->ds_state) == 0) {
  638. if (version == 3) {
  639. err = _nfs4_pnfs_v3_ds_connect(mds_srv, ds, timeo,
  640. retrans);
  641. } else if (version == 4) {
  642. err = _nfs4_pnfs_v4_ds_connect(mds_srv, ds, timeo,
  643. retrans, minor_version);
  644. } else {
  645. dprintk("%s: unsupported DS version %d\n", __func__,
  646. version);
  647. err = -EPROTONOSUPPORT;
  648. }
  649. nfs4_clear_ds_conn_bit(ds);
  650. } else {
  651. nfs4_wait_ds_connect(ds);
  652. /* what was waited on didn't connect AND didn't mark unavail */
  653. if (!ds->ds_clp && !nfs4_test_deviceid_unavailable(devid))
  654. goto again;
  655. }
  656. /*
  657. * At this point the ds->ds_clp should be ready, but it might have
  658. * hit an error.
  659. */
  660. if (!err) {
  661. if (!ds->ds_clp || !nfs_client_init_is_complete(ds->ds_clp)) {
  662. WARN_ON_ONCE(ds->ds_clp ||
  663. !nfs4_test_deviceid_unavailable(devid));
  664. return -EINVAL;
  665. }
  666. err = nfs_client_init_status(ds->ds_clp);
  667. }
  668. return err;
  669. }
  670. EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_connect);
  671. /*
  672. * Currently only supports ipv4, ipv6 and one multi-path address.
  673. */
  674. struct nfs4_pnfs_ds_addr *
  675. nfs4_decode_mp_ds_addr(struct net *net, struct xdr_stream *xdr, gfp_t gfp_flags)
  676. {
  677. struct nfs4_pnfs_ds_addr *da = NULL;
  678. char *buf, *portstr;
  679. __be16 port;
  680. int nlen, rlen;
  681. int tmp[2];
  682. __be32 *p;
  683. char *netid, *match_netid;
  684. size_t len, match_netid_len;
  685. char *startsep = "";
  686. char *endsep = "";
  687. /* r_netid */
  688. p = xdr_inline_decode(xdr, 4);
  689. if (unlikely(!p))
  690. goto out_err;
  691. nlen = be32_to_cpup(p++);
  692. p = xdr_inline_decode(xdr, nlen);
  693. if (unlikely(!p))
  694. goto out_err;
  695. netid = kmalloc(nlen+1, gfp_flags);
  696. if (unlikely(!netid))
  697. goto out_err;
  698. netid[nlen] = '\0';
  699. memcpy(netid, p, nlen);
  700. /* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */
  701. p = xdr_inline_decode(xdr, 4);
  702. if (unlikely(!p))
  703. goto out_free_netid;
  704. rlen = be32_to_cpup(p);
  705. p = xdr_inline_decode(xdr, rlen);
  706. if (unlikely(!p))
  707. goto out_free_netid;
  708. /* port is ".ABC.DEF", 8 chars max */
  709. if (rlen > INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 8) {
  710. dprintk("%s: Invalid address, length %d\n", __func__,
  711. rlen);
  712. goto out_free_netid;
  713. }
  714. buf = kmalloc(rlen + 1, gfp_flags);
  715. if (!buf) {
  716. dprintk("%s: Not enough memory\n", __func__);
  717. goto out_free_netid;
  718. }
  719. buf[rlen] = '\0';
  720. memcpy(buf, p, rlen);
  721. /* replace port '.' with '-' */
  722. portstr = strrchr(buf, '.');
  723. if (!portstr) {
  724. dprintk("%s: Failed finding expected dot in port\n",
  725. __func__);
  726. goto out_free_buf;
  727. }
  728. *portstr = '-';
  729. /* find '.' between address and port */
  730. portstr = strrchr(buf, '.');
  731. if (!portstr) {
  732. dprintk("%s: Failed finding expected dot between address and "
  733. "port\n", __func__);
  734. goto out_free_buf;
  735. }
  736. *portstr = '\0';
  737. da = kzalloc(sizeof(*da), gfp_flags);
  738. if (unlikely(!da))
  739. goto out_free_buf;
  740. INIT_LIST_HEAD(&da->da_node);
  741. if (!rpc_pton(net, buf, portstr-buf, (struct sockaddr *)&da->da_addr,
  742. sizeof(da->da_addr))) {
  743. dprintk("%s: error parsing address %s\n", __func__, buf);
  744. goto out_free_da;
  745. }
  746. portstr++;
  747. sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]);
  748. port = htons((tmp[0] << 8) | (tmp[1]));
  749. switch (da->da_addr.ss_family) {
  750. case AF_INET:
  751. ((struct sockaddr_in *)&da->da_addr)->sin_port = port;
  752. da->da_addrlen = sizeof(struct sockaddr_in);
  753. match_netid = "tcp";
  754. match_netid_len = 3;
  755. break;
  756. case AF_INET6:
  757. ((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port;
  758. da->da_addrlen = sizeof(struct sockaddr_in6);
  759. match_netid = "tcp6";
  760. match_netid_len = 4;
  761. startsep = "[";
  762. endsep = "]";
  763. break;
  764. default:
  765. dprintk("%s: unsupported address family: %u\n",
  766. __func__, da->da_addr.ss_family);
  767. goto out_free_da;
  768. }
  769. if (nlen != match_netid_len || strncmp(netid, match_netid, nlen)) {
  770. dprintk("%s: ERROR: r_netid \"%s\" != \"%s\"\n",
  771. __func__, netid, match_netid);
  772. goto out_free_da;
  773. }
  774. /* save human readable address */
  775. len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7;
  776. da->da_remotestr = kzalloc(len, gfp_flags);
  777. /* NULL is ok, only used for dprintk */
  778. if (da->da_remotestr)
  779. snprintf(da->da_remotestr, len, "%s%s%s:%u", startsep,
  780. buf, endsep, ntohs(port));
  781. dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr);
  782. kfree(buf);
  783. kfree(netid);
  784. return da;
  785. out_free_da:
  786. kfree(da);
  787. out_free_buf:
  788. dprintk("%s: Error parsing DS addr: %s\n", __func__, buf);
  789. kfree(buf);
  790. out_free_netid:
  791. kfree(netid);
  792. out_err:
  793. return NULL;
  794. }
  795. EXPORT_SYMBOL_GPL(nfs4_decode_mp_ds_addr);
  796. void
  797. pnfs_layout_mark_request_commit(struct nfs_page *req,
  798. struct pnfs_layout_segment *lseg,
  799. struct nfs_commit_info *cinfo,
  800. u32 ds_commit_idx)
  801. {
  802. struct list_head *list;
  803. struct pnfs_commit_bucket *buckets;
  804. mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
  805. buckets = cinfo->ds->buckets;
  806. list = &buckets[ds_commit_idx].written;
  807. if (list_empty(list)) {
  808. if (!pnfs_is_valid_lseg(lseg)) {
  809. mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
  810. cinfo->completion_ops->resched_write(cinfo, req);
  811. return;
  812. }
  813. /* Non-empty buckets hold a reference on the lseg. That ref
  814. * is normally transferred to the COMMIT call and released
  815. * there. It could also be released if the last req is pulled
  816. * off due to a rewrite, in which case it will be done in
  817. * pnfs_common_clear_request_commit
  818. */
  819. WARN_ON_ONCE(buckets[ds_commit_idx].wlseg != NULL);
  820. buckets[ds_commit_idx].wlseg = pnfs_get_lseg(lseg);
  821. }
  822. set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
  823. cinfo->ds->nwritten++;
  824. nfs_request_add_commit_list_locked(req, list, cinfo);
  825. mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
  826. nfs_mark_page_unstable(req->wb_page, cinfo);
  827. }
  828. EXPORT_SYMBOL_GPL(pnfs_layout_mark_request_commit);
  829. int
  830. pnfs_nfs_generic_sync(struct inode *inode, bool datasync)
  831. {
  832. int ret;
  833. if (!pnfs_layoutcommit_outstanding(inode))
  834. return 0;
  835. ret = nfs_commit_inode(inode, FLUSH_SYNC);
  836. if (ret < 0)
  837. return ret;
  838. if (datasync)
  839. return 0;
  840. return pnfs_layoutcommit_inode(inode, true);
  841. }
  842. EXPORT_SYMBOL_GPL(pnfs_nfs_generic_sync);