pnfs_nfs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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 the inode (/cinfo) lock
  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_locked(freeme);
  73. }
  74. EXPORT_SYMBOL_GPL(pnfs_generic_clear_request_commit);
  75. static int
  76. pnfs_generic_transfer_commit_list(struct list_head *src, struct list_head *dst,
  77. struct nfs_commit_info *cinfo, int max)
  78. {
  79. struct nfs_page *req, *tmp;
  80. int ret = 0;
  81. list_for_each_entry_safe(req, tmp, src, wb_list) {
  82. if (!nfs_lock_request(req))
  83. continue;
  84. kref_get(&req->wb_kref);
  85. if (cond_resched_lock(cinfo->lock))
  86. list_safe_reset_next(req, tmp, wb_list);
  87. nfs_request_remove_commit_list(req, cinfo);
  88. clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
  89. nfs_list_add_request(req, dst);
  90. ret++;
  91. if ((ret == max) && !cinfo->dreq)
  92. break;
  93. }
  94. return ret;
  95. }
  96. static int
  97. pnfs_generic_scan_ds_commit_list(struct pnfs_commit_bucket *bucket,
  98. struct nfs_commit_info *cinfo,
  99. int max)
  100. {
  101. struct list_head *src = &bucket->written;
  102. struct list_head *dst = &bucket->committing;
  103. int ret;
  104. lockdep_assert_held(cinfo->lock);
  105. ret = pnfs_generic_transfer_commit_list(src, dst, cinfo, max);
  106. if (ret) {
  107. cinfo->ds->nwritten -= ret;
  108. cinfo->ds->ncommitting += ret;
  109. if (bucket->clseg == NULL)
  110. bucket->clseg = pnfs_get_lseg(bucket->wlseg);
  111. if (list_empty(src)) {
  112. pnfs_put_lseg_locked(bucket->wlseg);
  113. bucket->wlseg = NULL;
  114. }
  115. }
  116. return ret;
  117. }
  118. /* Move reqs from written to committing lists, returning count
  119. * of number moved.
  120. */
  121. int pnfs_generic_scan_commit_lists(struct nfs_commit_info *cinfo,
  122. int max)
  123. {
  124. int i, rv = 0, cnt;
  125. lockdep_assert_held(cinfo->lock);
  126. for (i = 0; i < cinfo->ds->nbuckets && max != 0; i++) {
  127. cnt = pnfs_generic_scan_ds_commit_list(&cinfo->ds->buckets[i],
  128. cinfo, max);
  129. max -= cnt;
  130. rv += cnt;
  131. }
  132. return rv;
  133. }
  134. EXPORT_SYMBOL_GPL(pnfs_generic_scan_commit_lists);
  135. /* Pull everything off the committing lists and dump into @dst. */
  136. void pnfs_generic_recover_commit_reqs(struct list_head *dst,
  137. struct nfs_commit_info *cinfo)
  138. {
  139. struct pnfs_commit_bucket *b;
  140. struct pnfs_layout_segment *freeme;
  141. int i;
  142. lockdep_assert_held(cinfo->lock);
  143. restart:
  144. for (i = 0, b = cinfo->ds->buckets; i < cinfo->ds->nbuckets; i++, b++) {
  145. if (pnfs_generic_transfer_commit_list(&b->written, dst,
  146. cinfo, 0)) {
  147. freeme = b->wlseg;
  148. b->wlseg = NULL;
  149. spin_unlock(cinfo->lock);
  150. pnfs_put_lseg(freeme);
  151. spin_lock(cinfo->lock);
  152. goto restart;
  153. }
  154. }
  155. cinfo->ds->nwritten = 0;
  156. }
  157. EXPORT_SYMBOL_GPL(pnfs_generic_recover_commit_reqs);
  158. static void pnfs_generic_retry_commit(struct nfs_commit_info *cinfo, int idx)
  159. {
  160. struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds;
  161. struct pnfs_commit_bucket *bucket;
  162. struct pnfs_layout_segment *freeme;
  163. LIST_HEAD(pages);
  164. int i;
  165. spin_lock(cinfo->lock);
  166. for (i = idx; i < fl_cinfo->nbuckets; i++) {
  167. bucket = &fl_cinfo->buckets[i];
  168. if (list_empty(&bucket->committing))
  169. continue;
  170. freeme = bucket->clseg;
  171. bucket->clseg = NULL;
  172. list_splice_init(&bucket->committing, &pages);
  173. spin_unlock(cinfo->lock);
  174. nfs_retry_commit(&pages, freeme, cinfo, i);
  175. pnfs_put_lseg(freeme);
  176. spin_lock(cinfo->lock);
  177. }
  178. spin_unlock(cinfo->lock);
  179. }
  180. static unsigned int
  181. pnfs_generic_alloc_ds_commits(struct nfs_commit_info *cinfo,
  182. struct list_head *list)
  183. {
  184. struct pnfs_ds_commit_info *fl_cinfo;
  185. struct pnfs_commit_bucket *bucket;
  186. struct nfs_commit_data *data;
  187. int i;
  188. unsigned int nreq = 0;
  189. fl_cinfo = cinfo->ds;
  190. bucket = fl_cinfo->buckets;
  191. for (i = 0; i < fl_cinfo->nbuckets; i++, bucket++) {
  192. if (list_empty(&bucket->committing))
  193. continue;
  194. data = nfs_commitdata_alloc();
  195. if (!data)
  196. break;
  197. data->ds_commit_index = i;
  198. list_add(&data->pages, list);
  199. nreq++;
  200. }
  201. /* Clean up on error */
  202. pnfs_generic_retry_commit(cinfo, i);
  203. return nreq;
  204. }
  205. static inline
  206. void pnfs_fetch_commit_bucket_list(struct list_head *pages,
  207. struct nfs_commit_data *data,
  208. struct nfs_commit_info *cinfo)
  209. {
  210. struct pnfs_commit_bucket *bucket;
  211. bucket = &cinfo->ds->buckets[data->ds_commit_index];
  212. spin_lock(cinfo->lock);
  213. list_splice_init(&bucket->committing, pages);
  214. data->lseg = bucket->clseg;
  215. bucket->clseg = NULL;
  216. spin_unlock(cinfo->lock);
  217. }
  218. /* This follows nfs_commit_list pretty closely */
  219. int
  220. pnfs_generic_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
  221. int how, struct nfs_commit_info *cinfo,
  222. int (*initiate_commit)(struct nfs_commit_data *data,
  223. int how))
  224. {
  225. struct nfs_commit_data *data, *tmp;
  226. LIST_HEAD(list);
  227. unsigned int nreq = 0;
  228. if (!list_empty(mds_pages)) {
  229. data = nfs_commitdata_alloc();
  230. if (data != NULL) {
  231. data->ds_commit_index = -1;
  232. list_add(&data->pages, &list);
  233. nreq++;
  234. } else {
  235. nfs_retry_commit(mds_pages, NULL, cinfo, 0);
  236. pnfs_generic_retry_commit(cinfo, 0);
  237. cinfo->completion_ops->error_cleanup(NFS_I(inode));
  238. return -ENOMEM;
  239. }
  240. }
  241. nreq += pnfs_generic_alloc_ds_commits(cinfo, &list);
  242. if (nreq == 0) {
  243. cinfo->completion_ops->error_cleanup(NFS_I(inode));
  244. goto out;
  245. }
  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. nfs_init_commit(data, mds_pages, NULL, cinfo);
  251. nfs_initiate_commit(NFS_CLIENT(inode), data,
  252. NFS_PROTO(data->inode),
  253. data->mds_ops, how, 0);
  254. } else {
  255. LIST_HEAD(pages);
  256. pnfs_fetch_commit_bucket_list(&pages, data, cinfo);
  257. nfs_init_commit(data, &pages, data->lseg, cinfo);
  258. initiate_commit(data, how);
  259. }
  260. }
  261. out:
  262. cinfo->ds->ncommitting = 0;
  263. return PNFS_ATTEMPTED;
  264. }
  265. EXPORT_SYMBOL_GPL(pnfs_generic_commit_pagelist);
  266. /*
  267. * Data server cache
  268. *
  269. * Data servers can be mapped to different device ids.
  270. * nfs4_pnfs_ds reference counting
  271. * - set to 1 on allocation
  272. * - incremented when a device id maps a data server already in the cache.
  273. * - decremented when deviceid is removed from the cache.
  274. */
  275. static DEFINE_SPINLOCK(nfs4_ds_cache_lock);
  276. static LIST_HEAD(nfs4_data_server_cache);
  277. /* Debug routines */
  278. static void
  279. print_ds(struct nfs4_pnfs_ds *ds)
  280. {
  281. if (ds == NULL) {
  282. printk(KERN_WARNING "%s NULL device\n", __func__);
  283. return;
  284. }
  285. printk(KERN_WARNING " ds %s\n"
  286. " ref count %d\n"
  287. " client %p\n"
  288. " cl_exchange_flags %x\n",
  289. ds->ds_remotestr,
  290. atomic_read(&ds->ds_count), ds->ds_clp,
  291. ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0);
  292. }
  293. static bool
  294. same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2)
  295. {
  296. struct sockaddr_in *a, *b;
  297. struct sockaddr_in6 *a6, *b6;
  298. if (addr1->sa_family != addr2->sa_family)
  299. return false;
  300. switch (addr1->sa_family) {
  301. case AF_INET:
  302. a = (struct sockaddr_in *)addr1;
  303. b = (struct sockaddr_in *)addr2;
  304. if (a->sin_addr.s_addr == b->sin_addr.s_addr &&
  305. a->sin_port == b->sin_port)
  306. return true;
  307. break;
  308. case AF_INET6:
  309. a6 = (struct sockaddr_in6 *)addr1;
  310. b6 = (struct sockaddr_in6 *)addr2;
  311. /* LINKLOCAL addresses must have matching scope_id */
  312. if (ipv6_addr_src_scope(&a6->sin6_addr) ==
  313. IPV6_ADDR_SCOPE_LINKLOCAL &&
  314. a6->sin6_scope_id != b6->sin6_scope_id)
  315. return false;
  316. if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) &&
  317. a6->sin6_port == b6->sin6_port)
  318. return true;
  319. break;
  320. default:
  321. dprintk("%s: unhandled address family: %u\n",
  322. __func__, addr1->sa_family);
  323. return false;
  324. }
  325. return false;
  326. }
  327. /*
  328. * Checks if 'dsaddrs1' contains a subset of 'dsaddrs2'. If it does,
  329. * declare a match.
  330. */
  331. static bool
  332. _same_data_server_addrs_locked(const struct list_head *dsaddrs1,
  333. const struct list_head *dsaddrs2)
  334. {
  335. struct nfs4_pnfs_ds_addr *da1, *da2;
  336. struct sockaddr *sa1, *sa2;
  337. bool match = false;
  338. list_for_each_entry(da1, dsaddrs1, da_node) {
  339. sa1 = (struct sockaddr *)&da1->da_addr;
  340. match = false;
  341. list_for_each_entry(da2, dsaddrs2, da_node) {
  342. sa2 = (struct sockaddr *)&da2->da_addr;
  343. match = same_sockaddr(sa1, sa2);
  344. if (match)
  345. break;
  346. }
  347. if (!match)
  348. break;
  349. }
  350. return match;
  351. }
  352. /*
  353. * Lookup DS by addresses. nfs4_ds_cache_lock is held
  354. */
  355. static struct nfs4_pnfs_ds *
  356. _data_server_lookup_locked(const struct list_head *dsaddrs)
  357. {
  358. struct nfs4_pnfs_ds *ds;
  359. list_for_each_entry(ds, &nfs4_data_server_cache, ds_node)
  360. if (_same_data_server_addrs_locked(&ds->ds_addrs, dsaddrs))
  361. return ds;
  362. return NULL;
  363. }
  364. static void destroy_ds(struct nfs4_pnfs_ds *ds)
  365. {
  366. struct nfs4_pnfs_ds_addr *da;
  367. dprintk("--> %s\n", __func__);
  368. ifdebug(FACILITY)
  369. print_ds(ds);
  370. nfs_put_client(ds->ds_clp);
  371. while (!list_empty(&ds->ds_addrs)) {
  372. da = list_first_entry(&ds->ds_addrs,
  373. struct nfs4_pnfs_ds_addr,
  374. da_node);
  375. list_del_init(&da->da_node);
  376. kfree(da->da_remotestr);
  377. kfree(da);
  378. }
  379. kfree(ds->ds_remotestr);
  380. kfree(ds);
  381. }
  382. void nfs4_pnfs_ds_put(struct nfs4_pnfs_ds *ds)
  383. {
  384. if (atomic_dec_and_lock(&ds->ds_count,
  385. &nfs4_ds_cache_lock)) {
  386. list_del_init(&ds->ds_node);
  387. spin_unlock(&nfs4_ds_cache_lock);
  388. destroy_ds(ds);
  389. }
  390. }
  391. EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_put);
  392. /*
  393. * Create a string with a human readable address and port to avoid
  394. * complicated setup around many dprinks.
  395. */
  396. static char *
  397. nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags)
  398. {
  399. struct nfs4_pnfs_ds_addr *da;
  400. char *remotestr;
  401. size_t len;
  402. char *p;
  403. len = 3; /* '{', '}' and eol */
  404. list_for_each_entry(da, dsaddrs, da_node) {
  405. len += strlen(da->da_remotestr) + 1; /* string plus comma */
  406. }
  407. remotestr = kzalloc(len, gfp_flags);
  408. if (!remotestr)
  409. return NULL;
  410. p = remotestr;
  411. *(p++) = '{';
  412. len--;
  413. list_for_each_entry(da, dsaddrs, da_node) {
  414. size_t ll = strlen(da->da_remotestr);
  415. if (ll > len)
  416. goto out_err;
  417. memcpy(p, da->da_remotestr, ll);
  418. p += ll;
  419. len -= ll;
  420. if (len < 1)
  421. goto out_err;
  422. (*p++) = ',';
  423. len--;
  424. }
  425. if (len < 2)
  426. goto out_err;
  427. *(p++) = '}';
  428. *p = '\0';
  429. return remotestr;
  430. out_err:
  431. kfree(remotestr);
  432. return NULL;
  433. }
  434. /*
  435. * Given a list of multipath struct nfs4_pnfs_ds_addr, add it to ds cache if
  436. * uncached and return cached struct nfs4_pnfs_ds.
  437. */
  438. struct nfs4_pnfs_ds *
  439. nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags)
  440. {
  441. struct nfs4_pnfs_ds *tmp_ds, *ds = NULL;
  442. char *remotestr;
  443. if (list_empty(dsaddrs)) {
  444. dprintk("%s: no addresses defined\n", __func__);
  445. goto out;
  446. }
  447. ds = kzalloc(sizeof(*ds), gfp_flags);
  448. if (!ds)
  449. goto out;
  450. /* this is only used for debugging, so it's ok if its NULL */
  451. remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags);
  452. spin_lock(&nfs4_ds_cache_lock);
  453. tmp_ds = _data_server_lookup_locked(dsaddrs);
  454. if (tmp_ds == NULL) {
  455. INIT_LIST_HEAD(&ds->ds_addrs);
  456. list_splice_init(dsaddrs, &ds->ds_addrs);
  457. ds->ds_remotestr = remotestr;
  458. atomic_set(&ds->ds_count, 1);
  459. INIT_LIST_HEAD(&ds->ds_node);
  460. ds->ds_clp = NULL;
  461. list_add(&ds->ds_node, &nfs4_data_server_cache);
  462. dprintk("%s add new data server %s\n", __func__,
  463. ds->ds_remotestr);
  464. } else {
  465. kfree(remotestr);
  466. kfree(ds);
  467. atomic_inc(&tmp_ds->ds_count);
  468. dprintk("%s data server %s found, inc'ed ds_count to %d\n",
  469. __func__, tmp_ds->ds_remotestr,
  470. atomic_read(&tmp_ds->ds_count));
  471. ds = tmp_ds;
  472. }
  473. spin_unlock(&nfs4_ds_cache_lock);
  474. out:
  475. return ds;
  476. }
  477. EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_add);
  478. static void nfs4_wait_ds_connect(struct nfs4_pnfs_ds *ds)
  479. {
  480. might_sleep();
  481. wait_on_bit(&ds->ds_state, NFS4DS_CONNECTING,
  482. TASK_KILLABLE);
  483. }
  484. static void nfs4_clear_ds_conn_bit(struct nfs4_pnfs_ds *ds)
  485. {
  486. smp_mb__before_atomic();
  487. clear_bit(NFS4DS_CONNECTING, &ds->ds_state);
  488. smp_mb__after_atomic();
  489. wake_up_bit(&ds->ds_state, NFS4DS_CONNECTING);
  490. }
  491. static struct nfs_client *(*get_v3_ds_connect)(
  492. struct nfs_client *mds_clp,
  493. const struct sockaddr *ds_addr,
  494. int ds_addrlen,
  495. int ds_proto,
  496. unsigned int ds_timeo,
  497. unsigned int ds_retrans,
  498. rpc_authflavor_t au_flavor);
  499. static bool load_v3_ds_connect(void)
  500. {
  501. if (!get_v3_ds_connect) {
  502. get_v3_ds_connect = symbol_request(nfs3_set_ds_client);
  503. WARN_ON_ONCE(!get_v3_ds_connect);
  504. }
  505. return(get_v3_ds_connect != NULL);
  506. }
  507. void nfs4_pnfs_v3_ds_connect_unload(void)
  508. {
  509. if (get_v3_ds_connect) {
  510. symbol_put(nfs3_set_ds_client);
  511. get_v3_ds_connect = NULL;
  512. }
  513. }
  514. EXPORT_SYMBOL_GPL(nfs4_pnfs_v3_ds_connect_unload);
  515. static int _nfs4_pnfs_v3_ds_connect(struct nfs_server *mds_srv,
  516. struct nfs4_pnfs_ds *ds,
  517. unsigned int timeo,
  518. unsigned int retrans,
  519. rpc_authflavor_t au_flavor)
  520. {
  521. struct nfs_client *clp = ERR_PTR(-EIO);
  522. struct nfs4_pnfs_ds_addr *da;
  523. int status = 0;
  524. dprintk("--> %s DS %s au_flavor %d\n", __func__,
  525. ds->ds_remotestr, au_flavor);
  526. if (!load_v3_ds_connect())
  527. goto out;
  528. list_for_each_entry(da, &ds->ds_addrs, da_node) {
  529. dprintk("%s: DS %s: trying address %s\n",
  530. __func__, ds->ds_remotestr, da->da_remotestr);
  531. clp = get_v3_ds_connect(mds_srv->nfs_client,
  532. (struct sockaddr *)&da->da_addr,
  533. da->da_addrlen, IPPROTO_TCP,
  534. timeo, retrans, au_flavor);
  535. if (!IS_ERR(clp))
  536. break;
  537. }
  538. if (IS_ERR(clp)) {
  539. status = PTR_ERR(clp);
  540. goto out;
  541. }
  542. smp_wmb();
  543. ds->ds_clp = clp;
  544. dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
  545. out:
  546. return status;
  547. }
  548. static int _nfs4_pnfs_v4_ds_connect(struct nfs_server *mds_srv,
  549. struct nfs4_pnfs_ds *ds,
  550. unsigned int timeo,
  551. unsigned int retrans,
  552. u32 minor_version,
  553. rpc_authflavor_t au_flavor)
  554. {
  555. struct nfs_client *clp = ERR_PTR(-EIO);
  556. struct nfs4_pnfs_ds_addr *da;
  557. int status = 0;
  558. dprintk("--> %s DS %s au_flavor %d\n", __func__, ds->ds_remotestr,
  559. au_flavor);
  560. list_for_each_entry(da, &ds->ds_addrs, da_node) {
  561. dprintk("%s: DS %s: trying address %s\n",
  562. __func__, ds->ds_remotestr, da->da_remotestr);
  563. clp = nfs4_set_ds_client(mds_srv->nfs_client,
  564. (struct sockaddr *)&da->da_addr,
  565. da->da_addrlen, IPPROTO_TCP,
  566. timeo, retrans, minor_version,
  567. au_flavor);
  568. if (!IS_ERR(clp))
  569. break;
  570. }
  571. if (IS_ERR(clp)) {
  572. status = PTR_ERR(clp);
  573. goto out;
  574. }
  575. status = nfs4_init_ds_session(clp, mds_srv->nfs_client->cl_lease_time);
  576. if (status)
  577. goto out_put;
  578. smp_wmb();
  579. ds->ds_clp = clp;
  580. dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
  581. out:
  582. return status;
  583. out_put:
  584. nfs_put_client(clp);
  585. goto out;
  586. }
  587. /*
  588. * Create an rpc connection to the nfs4_pnfs_ds data server.
  589. * Currently only supports IPv4 and IPv6 addresses.
  590. * If connection fails, make devid unavailable.
  591. */
  592. void nfs4_pnfs_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds,
  593. struct nfs4_deviceid_node *devid, unsigned int timeo,
  594. unsigned int retrans, u32 version,
  595. u32 minor_version, rpc_authflavor_t au_flavor)
  596. {
  597. if (test_and_set_bit(NFS4DS_CONNECTING, &ds->ds_state) == 0) {
  598. int err = 0;
  599. if (version == 3) {
  600. err = _nfs4_pnfs_v3_ds_connect(mds_srv, ds, timeo,
  601. retrans, au_flavor);
  602. } else if (version == 4) {
  603. err = _nfs4_pnfs_v4_ds_connect(mds_srv, ds, timeo,
  604. retrans, minor_version,
  605. au_flavor);
  606. } else {
  607. dprintk("%s: unsupported DS version %d\n", __func__,
  608. version);
  609. err = -EPROTONOSUPPORT;
  610. }
  611. if (err)
  612. nfs4_mark_deviceid_unavailable(devid);
  613. nfs4_clear_ds_conn_bit(ds);
  614. } else {
  615. nfs4_wait_ds_connect(ds);
  616. }
  617. }
  618. EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_connect);
  619. /*
  620. * Currently only supports ipv4, ipv6 and one multi-path address.
  621. */
  622. struct nfs4_pnfs_ds_addr *
  623. nfs4_decode_mp_ds_addr(struct net *net, struct xdr_stream *xdr, gfp_t gfp_flags)
  624. {
  625. struct nfs4_pnfs_ds_addr *da = NULL;
  626. char *buf, *portstr;
  627. __be16 port;
  628. int nlen, rlen;
  629. int tmp[2];
  630. __be32 *p;
  631. char *netid, *match_netid;
  632. size_t len, match_netid_len;
  633. char *startsep = "";
  634. char *endsep = "";
  635. /* r_netid */
  636. p = xdr_inline_decode(xdr, 4);
  637. if (unlikely(!p))
  638. goto out_err;
  639. nlen = be32_to_cpup(p++);
  640. p = xdr_inline_decode(xdr, nlen);
  641. if (unlikely(!p))
  642. goto out_err;
  643. netid = kmalloc(nlen+1, gfp_flags);
  644. if (unlikely(!netid))
  645. goto out_err;
  646. netid[nlen] = '\0';
  647. memcpy(netid, p, nlen);
  648. /* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */
  649. p = xdr_inline_decode(xdr, 4);
  650. if (unlikely(!p))
  651. goto out_free_netid;
  652. rlen = be32_to_cpup(p);
  653. p = xdr_inline_decode(xdr, rlen);
  654. if (unlikely(!p))
  655. goto out_free_netid;
  656. /* port is ".ABC.DEF", 8 chars max */
  657. if (rlen > INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 8) {
  658. dprintk("%s: Invalid address, length %d\n", __func__,
  659. rlen);
  660. goto out_free_netid;
  661. }
  662. buf = kmalloc(rlen + 1, gfp_flags);
  663. if (!buf) {
  664. dprintk("%s: Not enough memory\n", __func__);
  665. goto out_free_netid;
  666. }
  667. buf[rlen] = '\0';
  668. memcpy(buf, p, rlen);
  669. /* replace port '.' with '-' */
  670. portstr = strrchr(buf, '.');
  671. if (!portstr) {
  672. dprintk("%s: Failed finding expected dot in port\n",
  673. __func__);
  674. goto out_free_buf;
  675. }
  676. *portstr = '-';
  677. /* find '.' between address and port */
  678. portstr = strrchr(buf, '.');
  679. if (!portstr) {
  680. dprintk("%s: Failed finding expected dot between address and "
  681. "port\n", __func__);
  682. goto out_free_buf;
  683. }
  684. *portstr = '\0';
  685. da = kzalloc(sizeof(*da), gfp_flags);
  686. if (unlikely(!da))
  687. goto out_free_buf;
  688. INIT_LIST_HEAD(&da->da_node);
  689. if (!rpc_pton(net, buf, portstr-buf, (struct sockaddr *)&da->da_addr,
  690. sizeof(da->da_addr))) {
  691. dprintk("%s: error parsing address %s\n", __func__, buf);
  692. goto out_free_da;
  693. }
  694. portstr++;
  695. sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]);
  696. port = htons((tmp[0] << 8) | (tmp[1]));
  697. switch (da->da_addr.ss_family) {
  698. case AF_INET:
  699. ((struct sockaddr_in *)&da->da_addr)->sin_port = port;
  700. da->da_addrlen = sizeof(struct sockaddr_in);
  701. match_netid = "tcp";
  702. match_netid_len = 3;
  703. break;
  704. case AF_INET6:
  705. ((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port;
  706. da->da_addrlen = sizeof(struct sockaddr_in6);
  707. match_netid = "tcp6";
  708. match_netid_len = 4;
  709. startsep = "[";
  710. endsep = "]";
  711. break;
  712. default:
  713. dprintk("%s: unsupported address family: %u\n",
  714. __func__, da->da_addr.ss_family);
  715. goto out_free_da;
  716. }
  717. if (nlen != match_netid_len || strncmp(netid, match_netid, nlen)) {
  718. dprintk("%s: ERROR: r_netid \"%s\" != \"%s\"\n",
  719. __func__, netid, match_netid);
  720. goto out_free_da;
  721. }
  722. /* save human readable address */
  723. len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7;
  724. da->da_remotestr = kzalloc(len, gfp_flags);
  725. /* NULL is ok, only used for dprintk */
  726. if (da->da_remotestr)
  727. snprintf(da->da_remotestr, len, "%s%s%s:%u", startsep,
  728. buf, endsep, ntohs(port));
  729. dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr);
  730. kfree(buf);
  731. kfree(netid);
  732. return da;
  733. out_free_da:
  734. kfree(da);
  735. out_free_buf:
  736. dprintk("%s: Error parsing DS addr: %s\n", __func__, buf);
  737. kfree(buf);
  738. out_free_netid:
  739. kfree(netid);
  740. out_err:
  741. return NULL;
  742. }
  743. EXPORT_SYMBOL_GPL(nfs4_decode_mp_ds_addr);
  744. void
  745. pnfs_layout_mark_request_commit(struct nfs_page *req,
  746. struct pnfs_layout_segment *lseg,
  747. struct nfs_commit_info *cinfo,
  748. u32 ds_commit_idx)
  749. {
  750. struct list_head *list;
  751. struct pnfs_commit_bucket *buckets;
  752. spin_lock(cinfo->lock);
  753. buckets = cinfo->ds->buckets;
  754. list = &buckets[ds_commit_idx].written;
  755. if (list_empty(list)) {
  756. /* Non-empty buckets hold a reference on the lseg. That ref
  757. * is normally transferred to the COMMIT call and released
  758. * there. It could also be released if the last req is pulled
  759. * off due to a rewrite, in which case it will be done in
  760. * pnfs_common_clear_request_commit
  761. */
  762. WARN_ON_ONCE(buckets[ds_commit_idx].wlseg != NULL);
  763. buckets[ds_commit_idx].wlseg = pnfs_get_lseg(lseg);
  764. }
  765. set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
  766. cinfo->ds->nwritten++;
  767. nfs_request_add_commit_list_locked(req, list, cinfo);
  768. spin_unlock(cinfo->lock);
  769. nfs_mark_page_unstable(req->wb_page, cinfo);
  770. }
  771. EXPORT_SYMBOL_GPL(pnfs_layout_mark_request_commit);
  772. int
  773. pnfs_nfs_generic_sync(struct inode *inode, bool datasync)
  774. {
  775. if (datasync)
  776. return 0;
  777. return pnfs_layoutcommit_inode(inode, true);
  778. }
  779. EXPORT_SYMBOL_GPL(pnfs_nfs_generic_sync);