pnfs_nfs.c 21 KB

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