nfs4filelayoutdev.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /*
  2. * Device operations for the pnfs nfs4 file layout driver.
  3. *
  4. * Copyright (c) 2002
  5. * The Regents of the University of Michigan
  6. * All Rights Reserved
  7. *
  8. * Dean Hildebrand <dhildebz@umich.edu>
  9. * Garth Goodson <Garth.Goodson@netapp.com>
  10. *
  11. * Permission is granted to use, copy, create derivative works, and
  12. * redistribute this software and such derivative works for any purpose,
  13. * so long as the name of the University of Michigan is not used in
  14. * any advertising or publicity pertaining to the use or distribution
  15. * of this software without specific, written prior authorization. If
  16. * the above copyright notice or any other identification of the
  17. * University of Michigan is included in any copy of any portion of
  18. * this software, then the disclaimer below must also be included.
  19. *
  20. * This software is provided as is, without representation or warranty
  21. * of any kind either express or implied, including without limitation
  22. * the implied warranties of merchantability, fitness for a particular
  23. * purpose, or noninfringement. The Regents of the University of
  24. * Michigan shall not be liable for any damages, including special,
  25. * indirect, incidental, or consequential damages, with respect to any
  26. * claim arising out of or in connection with the use of the software,
  27. * even if it has been or is hereafter advised of the possibility of
  28. * such damages.
  29. */
  30. #include <linux/nfs_fs.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/module.h>
  33. #include "internal.h"
  34. #include "nfs4filelayout.h"
  35. #define NFSDBG_FACILITY NFSDBG_PNFS_LD
  36. static unsigned int dataserver_timeo = NFS4_DEF_DS_TIMEO;
  37. static unsigned int dataserver_retrans = NFS4_DEF_DS_RETRANS;
  38. /*
  39. * Data server cache
  40. *
  41. * Data servers can be mapped to different device ids.
  42. * nfs4_pnfs_ds reference counting
  43. * - set to 1 on allocation
  44. * - incremented when a device id maps a data server already in the cache.
  45. * - decremented when deviceid is removed from the cache.
  46. */
  47. static DEFINE_SPINLOCK(nfs4_ds_cache_lock);
  48. static LIST_HEAD(nfs4_data_server_cache);
  49. /* Debug routines */
  50. void
  51. print_ds(struct nfs4_pnfs_ds *ds)
  52. {
  53. if (ds == NULL) {
  54. printk("%s NULL device\n", __func__);
  55. return;
  56. }
  57. printk(" ds %s\n"
  58. " ref count %d\n"
  59. " client %p\n"
  60. " cl_exchange_flags %x\n",
  61. ds->ds_remotestr,
  62. atomic_read(&ds->ds_count), ds->ds_clp,
  63. ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0);
  64. }
  65. static bool
  66. same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2)
  67. {
  68. struct sockaddr_in *a, *b;
  69. struct sockaddr_in6 *a6, *b6;
  70. if (addr1->sa_family != addr2->sa_family)
  71. return false;
  72. switch (addr1->sa_family) {
  73. case AF_INET:
  74. a = (struct sockaddr_in *)addr1;
  75. b = (struct sockaddr_in *)addr2;
  76. if (a->sin_addr.s_addr == b->sin_addr.s_addr &&
  77. a->sin_port == b->sin_port)
  78. return true;
  79. break;
  80. case AF_INET6:
  81. a6 = (struct sockaddr_in6 *)addr1;
  82. b6 = (struct sockaddr_in6 *)addr2;
  83. /* LINKLOCAL addresses must have matching scope_id */
  84. if (ipv6_addr_scope(&a6->sin6_addr) ==
  85. IPV6_ADDR_SCOPE_LINKLOCAL &&
  86. a6->sin6_scope_id != b6->sin6_scope_id)
  87. return false;
  88. if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) &&
  89. a6->sin6_port == b6->sin6_port)
  90. return true;
  91. break;
  92. default:
  93. dprintk("%s: unhandled address family: %u\n",
  94. __func__, addr1->sa_family);
  95. return false;
  96. }
  97. return false;
  98. }
  99. static bool
  100. _same_data_server_addrs_locked(const struct list_head *dsaddrs1,
  101. const struct list_head *dsaddrs2)
  102. {
  103. struct nfs4_pnfs_ds_addr *da1, *da2;
  104. /* step through both lists, comparing as we go */
  105. for (da1 = list_first_entry(dsaddrs1, typeof(*da1), da_node),
  106. da2 = list_first_entry(dsaddrs2, typeof(*da2), da_node);
  107. da1 != NULL && da2 != NULL;
  108. da1 = list_entry(da1->da_node.next, typeof(*da1), da_node),
  109. da2 = list_entry(da2->da_node.next, typeof(*da2), da_node)) {
  110. if (!same_sockaddr((struct sockaddr *)&da1->da_addr,
  111. (struct sockaddr *)&da2->da_addr))
  112. return false;
  113. }
  114. if (da1 == NULL && da2 == NULL)
  115. return true;
  116. return false;
  117. }
  118. /*
  119. * Lookup DS by addresses. nfs4_ds_cache_lock is held
  120. */
  121. static struct nfs4_pnfs_ds *
  122. _data_server_lookup_locked(const struct list_head *dsaddrs)
  123. {
  124. struct nfs4_pnfs_ds *ds;
  125. list_for_each_entry(ds, &nfs4_data_server_cache, ds_node)
  126. if (_same_data_server_addrs_locked(&ds->ds_addrs, dsaddrs))
  127. return ds;
  128. return NULL;
  129. }
  130. /*
  131. * Lookup DS by nfs_client pointer. Zero data server client pointer
  132. */
  133. void nfs4_ds_disconnect(struct nfs_client *clp)
  134. {
  135. struct nfs4_pnfs_ds *ds;
  136. struct nfs_client *found = NULL;
  137. dprintk("%s clp %p\n", __func__, clp);
  138. spin_lock(&nfs4_ds_cache_lock);
  139. list_for_each_entry(ds, &nfs4_data_server_cache, ds_node)
  140. if (ds->ds_clp && ds->ds_clp == clp) {
  141. found = ds->ds_clp;
  142. ds->ds_clp = NULL;
  143. }
  144. spin_unlock(&nfs4_ds_cache_lock);
  145. if (found) {
  146. set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
  147. nfs_put_client(clp);
  148. }
  149. }
  150. /*
  151. * Create an rpc connection to the nfs4_pnfs_ds data server
  152. * Currently only supports IPv4 and IPv6 addresses
  153. */
  154. static int
  155. nfs4_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds)
  156. {
  157. struct nfs_client *clp = ERR_PTR(-EIO);
  158. struct nfs4_pnfs_ds_addr *da;
  159. int status = 0;
  160. dprintk("--> %s DS %s au_flavor %d\n", __func__, ds->ds_remotestr,
  161. mds_srv->nfs_client->cl_rpcclient->cl_auth->au_flavor);
  162. BUG_ON(list_empty(&ds->ds_addrs));
  163. list_for_each_entry(da, &ds->ds_addrs, da_node) {
  164. dprintk("%s: DS %s: trying address %s\n",
  165. __func__, ds->ds_remotestr, da->da_remotestr);
  166. clp = nfs4_set_ds_client(mds_srv->nfs_client,
  167. (struct sockaddr *)&da->da_addr,
  168. da->da_addrlen, IPPROTO_TCP,
  169. dataserver_timeo, dataserver_retrans);
  170. if (!IS_ERR(clp))
  171. break;
  172. }
  173. if (IS_ERR(clp)) {
  174. status = PTR_ERR(clp);
  175. goto out;
  176. }
  177. status = nfs4_init_ds_session(clp, mds_srv->nfs_client->cl_lease_time);
  178. if (status)
  179. goto out_put;
  180. ds->ds_clp = clp;
  181. dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
  182. out:
  183. return status;
  184. out_put:
  185. nfs_put_client(clp);
  186. goto out;
  187. }
  188. static void
  189. destroy_ds(struct nfs4_pnfs_ds *ds)
  190. {
  191. struct nfs4_pnfs_ds_addr *da;
  192. dprintk("--> %s\n", __func__);
  193. ifdebug(FACILITY)
  194. print_ds(ds);
  195. if (ds->ds_clp)
  196. nfs_put_client(ds->ds_clp);
  197. while (!list_empty(&ds->ds_addrs)) {
  198. da = list_first_entry(&ds->ds_addrs,
  199. struct nfs4_pnfs_ds_addr,
  200. da_node);
  201. list_del_init(&da->da_node);
  202. kfree(da->da_remotestr);
  203. kfree(da);
  204. }
  205. kfree(ds->ds_remotestr);
  206. kfree(ds);
  207. }
  208. void
  209. nfs4_fl_free_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
  210. {
  211. struct nfs4_pnfs_ds *ds;
  212. int i;
  213. nfs4_print_deviceid(&dsaddr->id_node.deviceid);
  214. for (i = 0; i < dsaddr->ds_num; i++) {
  215. ds = dsaddr->ds_list[i];
  216. if (ds != NULL) {
  217. if (atomic_dec_and_lock(&ds->ds_count,
  218. &nfs4_ds_cache_lock)) {
  219. list_del_init(&ds->ds_node);
  220. spin_unlock(&nfs4_ds_cache_lock);
  221. destroy_ds(ds);
  222. }
  223. }
  224. }
  225. kfree(dsaddr->stripe_indices);
  226. kfree(dsaddr);
  227. }
  228. /*
  229. * Create a string with a human readable address and port to avoid
  230. * complicated setup around many dprinks.
  231. */
  232. static char *
  233. nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags)
  234. {
  235. struct nfs4_pnfs_ds_addr *da;
  236. char *remotestr;
  237. size_t len;
  238. char *p;
  239. len = 3; /* '{', '}' and eol */
  240. list_for_each_entry(da, dsaddrs, da_node) {
  241. len += strlen(da->da_remotestr) + 1; /* string plus comma */
  242. }
  243. remotestr = kzalloc(len, gfp_flags);
  244. if (!remotestr)
  245. return NULL;
  246. p = remotestr;
  247. *(p++) = '{';
  248. len--;
  249. list_for_each_entry(da, dsaddrs, da_node) {
  250. size_t ll = strlen(da->da_remotestr);
  251. if (ll > len)
  252. goto out_err;
  253. memcpy(p, da->da_remotestr, ll);
  254. p += ll;
  255. len -= ll;
  256. if (len < 1)
  257. goto out_err;
  258. (*p++) = ',';
  259. len--;
  260. }
  261. if (len < 2)
  262. goto out_err;
  263. *(p++) = '}';
  264. *p = '\0';
  265. return remotestr;
  266. out_err:
  267. kfree(remotestr);
  268. return NULL;
  269. }
  270. static struct nfs4_pnfs_ds *
  271. nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags)
  272. {
  273. struct nfs4_pnfs_ds *tmp_ds, *ds = NULL;
  274. char *remotestr;
  275. if (list_empty(dsaddrs)) {
  276. dprintk("%s: no addresses defined\n", __func__);
  277. goto out;
  278. }
  279. ds = kzalloc(sizeof(*ds), gfp_flags);
  280. if (!ds)
  281. goto out;
  282. /* this is only used for debugging, so it's ok if its NULL */
  283. remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags);
  284. spin_lock(&nfs4_ds_cache_lock);
  285. tmp_ds = _data_server_lookup_locked(dsaddrs);
  286. if (tmp_ds == NULL) {
  287. INIT_LIST_HEAD(&ds->ds_addrs);
  288. list_splice_init(dsaddrs, &ds->ds_addrs);
  289. ds->ds_remotestr = remotestr;
  290. atomic_set(&ds->ds_count, 1);
  291. INIT_LIST_HEAD(&ds->ds_node);
  292. ds->ds_clp = NULL;
  293. list_add(&ds->ds_node, &nfs4_data_server_cache);
  294. dprintk("%s add new data server %s\n", __func__,
  295. ds->ds_remotestr);
  296. } else {
  297. kfree(remotestr);
  298. kfree(ds);
  299. atomic_inc(&tmp_ds->ds_count);
  300. dprintk("%s data server %s found, inc'ed ds_count to %d\n",
  301. __func__, tmp_ds->ds_remotestr,
  302. atomic_read(&tmp_ds->ds_count));
  303. ds = tmp_ds;
  304. }
  305. spin_unlock(&nfs4_ds_cache_lock);
  306. out:
  307. return ds;
  308. }
  309. /*
  310. * Currently only supports ipv4, ipv6 and one multi-path address.
  311. */
  312. static struct nfs4_pnfs_ds_addr *
  313. decode_ds_addr(struct net *net, struct xdr_stream *streamp, gfp_t gfp_flags)
  314. {
  315. struct nfs4_pnfs_ds_addr *da = NULL;
  316. char *buf, *portstr;
  317. __be16 port;
  318. int nlen, rlen;
  319. int tmp[2];
  320. __be32 *p;
  321. char *netid, *match_netid;
  322. size_t len, match_netid_len;
  323. char *startsep = "";
  324. char *endsep = "";
  325. /* r_netid */
  326. p = xdr_inline_decode(streamp, 4);
  327. if (unlikely(!p))
  328. goto out_err;
  329. nlen = be32_to_cpup(p++);
  330. p = xdr_inline_decode(streamp, nlen);
  331. if (unlikely(!p))
  332. goto out_err;
  333. netid = kmalloc(nlen+1, gfp_flags);
  334. if (unlikely(!netid))
  335. goto out_err;
  336. netid[nlen] = '\0';
  337. memcpy(netid, p, nlen);
  338. /* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */
  339. p = xdr_inline_decode(streamp, 4);
  340. if (unlikely(!p))
  341. goto out_free_netid;
  342. rlen = be32_to_cpup(p);
  343. p = xdr_inline_decode(streamp, rlen);
  344. if (unlikely(!p))
  345. goto out_free_netid;
  346. /* port is ".ABC.DEF", 8 chars max */
  347. if (rlen > INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 8) {
  348. dprintk("%s: Invalid address, length %d\n", __func__,
  349. rlen);
  350. goto out_free_netid;
  351. }
  352. buf = kmalloc(rlen + 1, gfp_flags);
  353. if (!buf) {
  354. dprintk("%s: Not enough memory\n", __func__);
  355. goto out_free_netid;
  356. }
  357. buf[rlen] = '\0';
  358. memcpy(buf, p, rlen);
  359. /* replace port '.' with '-' */
  360. portstr = strrchr(buf, '.');
  361. if (!portstr) {
  362. dprintk("%s: Failed finding expected dot in port\n",
  363. __func__);
  364. goto out_free_buf;
  365. }
  366. *portstr = '-';
  367. /* find '.' between address and port */
  368. portstr = strrchr(buf, '.');
  369. if (!portstr) {
  370. dprintk("%s: Failed finding expected dot between address and "
  371. "port\n", __func__);
  372. goto out_free_buf;
  373. }
  374. *portstr = '\0';
  375. da = kzalloc(sizeof(*da), gfp_flags);
  376. if (unlikely(!da))
  377. goto out_free_buf;
  378. INIT_LIST_HEAD(&da->da_node);
  379. if (!rpc_pton(net, buf, portstr-buf, (struct sockaddr *)&da->da_addr,
  380. sizeof(da->da_addr))) {
  381. dprintk("%s: error parsing address %s\n", __func__, buf);
  382. goto out_free_da;
  383. }
  384. portstr++;
  385. sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]);
  386. port = htons((tmp[0] << 8) | (tmp[1]));
  387. switch (da->da_addr.ss_family) {
  388. case AF_INET:
  389. ((struct sockaddr_in *)&da->da_addr)->sin_port = port;
  390. da->da_addrlen = sizeof(struct sockaddr_in);
  391. match_netid = "tcp";
  392. match_netid_len = 3;
  393. break;
  394. case AF_INET6:
  395. ((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port;
  396. da->da_addrlen = sizeof(struct sockaddr_in6);
  397. match_netid = "tcp6";
  398. match_netid_len = 4;
  399. startsep = "[";
  400. endsep = "]";
  401. break;
  402. default:
  403. dprintk("%s: unsupported address family: %u\n",
  404. __func__, da->da_addr.ss_family);
  405. goto out_free_da;
  406. }
  407. if (nlen != match_netid_len || strncmp(netid, match_netid, nlen)) {
  408. dprintk("%s: ERROR: r_netid \"%s\" != \"%s\"\n",
  409. __func__, netid, match_netid);
  410. goto out_free_da;
  411. }
  412. /* save human readable address */
  413. len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7;
  414. da->da_remotestr = kzalloc(len, gfp_flags);
  415. /* NULL is ok, only used for dprintk */
  416. if (da->da_remotestr)
  417. snprintf(da->da_remotestr, len, "%s%s%s:%u", startsep,
  418. buf, endsep, ntohs(port));
  419. dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr);
  420. kfree(buf);
  421. kfree(netid);
  422. return da;
  423. out_free_da:
  424. kfree(da);
  425. out_free_buf:
  426. dprintk("%s: Error parsing DS addr: %s\n", __func__, buf);
  427. kfree(buf);
  428. out_free_netid:
  429. kfree(netid);
  430. out_err:
  431. return NULL;
  432. }
  433. /* Decode opaque device data and return the result */
  434. static struct nfs4_file_layout_dsaddr*
  435. decode_device(struct inode *ino, struct pnfs_device *pdev, gfp_t gfp_flags)
  436. {
  437. int i;
  438. u32 cnt, num;
  439. u8 *indexp;
  440. __be32 *p;
  441. u8 *stripe_indices;
  442. u8 max_stripe_index;
  443. struct nfs4_file_layout_dsaddr *dsaddr = NULL;
  444. struct xdr_stream stream;
  445. struct xdr_buf buf;
  446. struct page *scratch;
  447. struct list_head dsaddrs;
  448. struct nfs4_pnfs_ds_addr *da;
  449. /* set up xdr stream */
  450. scratch = alloc_page(gfp_flags);
  451. if (!scratch)
  452. goto out_err;
  453. xdr_init_decode_pages(&stream, &buf, pdev->pages, pdev->pglen);
  454. xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
  455. /* Get the stripe count (number of stripe index) */
  456. p = xdr_inline_decode(&stream, 4);
  457. if (unlikely(!p))
  458. goto out_err_free_scratch;
  459. cnt = be32_to_cpup(p);
  460. dprintk("%s stripe count %d\n", __func__, cnt);
  461. if (cnt > NFS4_PNFS_MAX_STRIPE_CNT) {
  462. printk(KERN_WARNING "NFS: %s: stripe count %d greater than "
  463. "supported maximum %d\n", __func__,
  464. cnt, NFS4_PNFS_MAX_STRIPE_CNT);
  465. goto out_err_free_scratch;
  466. }
  467. /* read stripe indices */
  468. stripe_indices = kcalloc(cnt, sizeof(u8), gfp_flags);
  469. if (!stripe_indices)
  470. goto out_err_free_scratch;
  471. p = xdr_inline_decode(&stream, cnt << 2);
  472. if (unlikely(!p))
  473. goto out_err_free_stripe_indices;
  474. indexp = &stripe_indices[0];
  475. max_stripe_index = 0;
  476. for (i = 0; i < cnt; i++) {
  477. *indexp = be32_to_cpup(p++);
  478. max_stripe_index = max(max_stripe_index, *indexp);
  479. indexp++;
  480. }
  481. /* Check the multipath list count */
  482. p = xdr_inline_decode(&stream, 4);
  483. if (unlikely(!p))
  484. goto out_err_free_stripe_indices;
  485. num = be32_to_cpup(p);
  486. dprintk("%s ds_num %u\n", __func__, num);
  487. if (num > NFS4_PNFS_MAX_MULTI_CNT) {
  488. printk(KERN_WARNING "NFS: %s: multipath count %d greater than "
  489. "supported maximum %d\n", __func__,
  490. num, NFS4_PNFS_MAX_MULTI_CNT);
  491. goto out_err_free_stripe_indices;
  492. }
  493. /* validate stripe indices are all < num */
  494. if (max_stripe_index >= num) {
  495. printk(KERN_WARNING "NFS: %s: stripe index %u >= num ds %u\n",
  496. __func__, max_stripe_index, num);
  497. goto out_err_free_stripe_indices;
  498. }
  499. dsaddr = kzalloc(sizeof(*dsaddr) +
  500. (sizeof(struct nfs4_pnfs_ds *) * (num - 1)),
  501. gfp_flags);
  502. if (!dsaddr)
  503. goto out_err_free_stripe_indices;
  504. dsaddr->stripe_count = cnt;
  505. dsaddr->stripe_indices = stripe_indices;
  506. stripe_indices = NULL;
  507. dsaddr->ds_num = num;
  508. nfs4_init_deviceid_node(&dsaddr->id_node,
  509. NFS_SERVER(ino)->pnfs_curr_ld,
  510. NFS_SERVER(ino)->nfs_client,
  511. &pdev->dev_id);
  512. INIT_LIST_HEAD(&dsaddrs);
  513. for (i = 0; i < dsaddr->ds_num; i++) {
  514. int j;
  515. u32 mp_count;
  516. p = xdr_inline_decode(&stream, 4);
  517. if (unlikely(!p))
  518. goto out_err_free_deviceid;
  519. mp_count = be32_to_cpup(p); /* multipath count */
  520. for (j = 0; j < mp_count; j++) {
  521. da = decode_ds_addr(NFS_SERVER(ino)->nfs_client->cl_net,
  522. &stream, gfp_flags);
  523. if (da)
  524. list_add_tail(&da->da_node, &dsaddrs);
  525. }
  526. if (list_empty(&dsaddrs)) {
  527. dprintk("%s: no suitable DS addresses found\n",
  528. __func__);
  529. goto out_err_free_deviceid;
  530. }
  531. dsaddr->ds_list[i] = nfs4_pnfs_ds_add(&dsaddrs, gfp_flags);
  532. if (!dsaddr->ds_list[i])
  533. goto out_err_drain_dsaddrs;
  534. /* If DS was already in cache, free ds addrs */
  535. while (!list_empty(&dsaddrs)) {
  536. da = list_first_entry(&dsaddrs,
  537. struct nfs4_pnfs_ds_addr,
  538. da_node);
  539. list_del_init(&da->da_node);
  540. kfree(da->da_remotestr);
  541. kfree(da);
  542. }
  543. }
  544. __free_page(scratch);
  545. return dsaddr;
  546. out_err_drain_dsaddrs:
  547. while (!list_empty(&dsaddrs)) {
  548. da = list_first_entry(&dsaddrs, struct nfs4_pnfs_ds_addr,
  549. da_node);
  550. list_del_init(&da->da_node);
  551. kfree(da->da_remotestr);
  552. kfree(da);
  553. }
  554. out_err_free_deviceid:
  555. nfs4_fl_free_deviceid(dsaddr);
  556. /* stripe_indicies was part of dsaddr */
  557. goto out_err_free_scratch;
  558. out_err_free_stripe_indices:
  559. kfree(stripe_indices);
  560. out_err_free_scratch:
  561. __free_page(scratch);
  562. out_err:
  563. dprintk("%s ERROR: returning NULL\n", __func__);
  564. return NULL;
  565. }
  566. /*
  567. * Decode the opaque device specified in 'dev' and add it to the cache of
  568. * available devices.
  569. */
  570. static struct nfs4_file_layout_dsaddr *
  571. decode_and_add_device(struct inode *inode, struct pnfs_device *dev, gfp_t gfp_flags)
  572. {
  573. struct nfs4_deviceid_node *d;
  574. struct nfs4_file_layout_dsaddr *n, *new;
  575. new = decode_device(inode, dev, gfp_flags);
  576. if (!new) {
  577. printk(KERN_WARNING "NFS: %s: Could not decode or add device\n",
  578. __func__);
  579. return NULL;
  580. }
  581. d = nfs4_insert_deviceid_node(&new->id_node);
  582. n = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
  583. if (n != new) {
  584. nfs4_fl_free_deviceid(new);
  585. return n;
  586. }
  587. return new;
  588. }
  589. /*
  590. * Retrieve the information for dev_id, add it to the list
  591. * of available devices, and return it.
  592. */
  593. struct nfs4_file_layout_dsaddr *
  594. get_device_info(struct inode *inode, struct nfs4_deviceid *dev_id, gfp_t gfp_flags)
  595. {
  596. struct pnfs_device *pdev = NULL;
  597. u32 max_resp_sz;
  598. int max_pages;
  599. struct page **pages = NULL;
  600. struct nfs4_file_layout_dsaddr *dsaddr = NULL;
  601. int rc, i;
  602. struct nfs_server *server = NFS_SERVER(inode);
  603. /*
  604. * Use the session max response size as the basis for setting
  605. * GETDEVICEINFO's maxcount
  606. */
  607. max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
  608. max_pages = nfs_page_array_len(0, max_resp_sz);
  609. dprintk("%s inode %p max_resp_sz %u max_pages %d\n",
  610. __func__, inode, max_resp_sz, max_pages);
  611. pdev = kzalloc(sizeof(struct pnfs_device), gfp_flags);
  612. if (pdev == NULL)
  613. return NULL;
  614. pages = kzalloc(max_pages * sizeof(struct page *), gfp_flags);
  615. if (pages == NULL) {
  616. kfree(pdev);
  617. return NULL;
  618. }
  619. for (i = 0; i < max_pages; i++) {
  620. pages[i] = alloc_page(gfp_flags);
  621. if (!pages[i])
  622. goto out_free;
  623. }
  624. memcpy(&pdev->dev_id, dev_id, sizeof(*dev_id));
  625. pdev->layout_type = LAYOUT_NFSV4_1_FILES;
  626. pdev->pages = pages;
  627. pdev->pgbase = 0;
  628. pdev->pglen = max_resp_sz;
  629. pdev->mincount = 0;
  630. rc = nfs4_proc_getdeviceinfo(server, pdev);
  631. dprintk("%s getdevice info returns %d\n", __func__, rc);
  632. if (rc)
  633. goto out_free;
  634. /*
  635. * Found new device, need to decode it and then add it to the
  636. * list of known devices for this mountpoint.
  637. */
  638. dsaddr = decode_and_add_device(inode, pdev, gfp_flags);
  639. out_free:
  640. for (i = 0; i < max_pages; i++)
  641. __free_page(pages[i]);
  642. kfree(pages);
  643. kfree(pdev);
  644. dprintk("<-- %s dsaddr %p\n", __func__, dsaddr);
  645. return dsaddr;
  646. }
  647. void
  648. nfs4_fl_put_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
  649. {
  650. nfs4_put_deviceid_node(&dsaddr->id_node);
  651. }
  652. /*
  653. * Want res = (offset - layout->pattern_offset)/ layout->stripe_unit
  654. * Then: ((res + fsi) % dsaddr->stripe_count)
  655. */
  656. u32
  657. nfs4_fl_calc_j_index(struct pnfs_layout_segment *lseg, loff_t offset)
  658. {
  659. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  660. u64 tmp;
  661. tmp = offset - flseg->pattern_offset;
  662. do_div(tmp, flseg->stripe_unit);
  663. tmp += flseg->first_stripe_index;
  664. return do_div(tmp, flseg->dsaddr->stripe_count);
  665. }
  666. u32
  667. nfs4_fl_calc_ds_index(struct pnfs_layout_segment *lseg, u32 j)
  668. {
  669. return FILELAYOUT_LSEG(lseg)->dsaddr->stripe_indices[j];
  670. }
  671. struct nfs_fh *
  672. nfs4_fl_select_ds_fh(struct pnfs_layout_segment *lseg, u32 j)
  673. {
  674. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  675. u32 i;
  676. if (flseg->stripe_type == STRIPE_SPARSE) {
  677. if (flseg->num_fh == 1)
  678. i = 0;
  679. else if (flseg->num_fh == 0)
  680. /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
  681. return NULL;
  682. else
  683. i = nfs4_fl_calc_ds_index(lseg, j);
  684. } else
  685. i = j;
  686. return flseg->fh_array[i];
  687. }
  688. struct nfs4_pnfs_ds *
  689. nfs4_fl_prepare_ds(struct pnfs_layout_segment *lseg, u32 ds_idx)
  690. {
  691. struct nfs4_file_layout_dsaddr *dsaddr = FILELAYOUT_LSEG(lseg)->dsaddr;
  692. struct nfs4_pnfs_ds *ds = dsaddr->ds_list[ds_idx];
  693. struct nfs4_deviceid_node *devid = FILELAYOUT_DEVID_NODE(lseg);
  694. if (filelayout_test_devid_invalid(devid))
  695. return NULL;
  696. if (ds == NULL) {
  697. printk(KERN_ERR "NFS: %s: No data server for offset index %d\n",
  698. __func__, ds_idx);
  699. goto mark_dev_invalid;
  700. }
  701. if (!ds->ds_clp) {
  702. struct nfs_server *s = NFS_SERVER(lseg->pls_layout->plh_inode);
  703. int err;
  704. err = nfs4_ds_connect(s, ds);
  705. if (err)
  706. goto mark_dev_invalid;
  707. }
  708. return ds;
  709. mark_dev_invalid:
  710. filelayout_mark_devid_invalid(devid);
  711. return NULL;
  712. }
  713. module_param(dataserver_retrans, uint, 0644);
  714. MODULE_PARM_DESC(dataserver_retrans, "The number of times the NFSv4.1 client "
  715. "retries a request before it attempts further "
  716. " recovery action.");
  717. module_param(dataserver_timeo, uint, 0644);
  718. MODULE_PARM_DESC(dataserver_timeo, "The time (in tenths of a second) the "
  719. "NFSv4.1 client waits for a response from a "
  720. " data server before it retries an NFS request.");