nfs4filelayoutdev.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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. * Create an rpc connection to the nfs4_pnfs_ds data server
  132. * Currently only supports IPv4 and IPv6 addresses
  133. */
  134. static int
  135. nfs4_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds)
  136. {
  137. struct nfs_client *clp = ERR_PTR(-EIO);
  138. struct nfs4_pnfs_ds_addr *da;
  139. int status = 0;
  140. dprintk("--> %s DS %s au_flavor %d\n", __func__, ds->ds_remotestr,
  141. mds_srv->nfs_client->cl_rpcclient->cl_auth->au_flavor);
  142. BUG_ON(list_empty(&ds->ds_addrs));
  143. list_for_each_entry(da, &ds->ds_addrs, da_node) {
  144. dprintk("%s: DS %s: trying address %s\n",
  145. __func__, ds->ds_remotestr, da->da_remotestr);
  146. clp = nfs4_set_ds_client(mds_srv->nfs_client,
  147. (struct sockaddr *)&da->da_addr,
  148. da->da_addrlen, IPPROTO_TCP,
  149. dataserver_timeo, dataserver_retrans);
  150. if (!IS_ERR(clp))
  151. break;
  152. }
  153. if (IS_ERR(clp)) {
  154. status = PTR_ERR(clp);
  155. goto out;
  156. }
  157. if ((clp->cl_exchange_flags & EXCHGID4_FLAG_MASK_PNFS) != 0) {
  158. if (!is_ds_client(clp)) {
  159. status = -ENODEV;
  160. goto out_put;
  161. }
  162. ds->ds_clp = clp;
  163. dprintk("%s [existing] server=%s\n", __func__,
  164. ds->ds_remotestr);
  165. goto out;
  166. }
  167. /*
  168. * Do not set NFS_CS_CHECK_LEASE_TIME instead set the DS lease to
  169. * be equal to the MDS lease. Renewal is scheduled in create_session.
  170. */
  171. spin_lock(&mds_srv->nfs_client->cl_lock);
  172. clp->cl_lease_time = mds_srv->nfs_client->cl_lease_time;
  173. spin_unlock(&mds_srv->nfs_client->cl_lock);
  174. clp->cl_last_renewal = jiffies;
  175. /* New nfs_client */
  176. status = nfs4_init_ds_session(clp);
  177. if (status)
  178. goto out_put;
  179. ds->ds_clp = clp;
  180. dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
  181. out:
  182. return status;
  183. out_put:
  184. nfs_put_client(clp);
  185. goto out;
  186. }
  187. static void
  188. destroy_ds(struct nfs4_pnfs_ds *ds)
  189. {
  190. struct nfs4_pnfs_ds_addr *da;
  191. dprintk("--> %s\n", __func__);
  192. ifdebug(FACILITY)
  193. print_ds(ds);
  194. if (ds->ds_clp)
  195. nfs_put_client(ds->ds_clp);
  196. while (!list_empty(&ds->ds_addrs)) {
  197. da = list_first_entry(&ds->ds_addrs,
  198. struct nfs4_pnfs_ds_addr,
  199. da_node);
  200. list_del_init(&da->da_node);
  201. kfree(da->da_remotestr);
  202. kfree(da);
  203. }
  204. kfree(ds->ds_remotestr);
  205. kfree(ds);
  206. }
  207. void
  208. nfs4_fl_free_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
  209. {
  210. struct nfs4_pnfs_ds *ds;
  211. int i;
  212. nfs4_print_deviceid(&dsaddr->id_node.deviceid);
  213. for (i = 0; i < dsaddr->ds_num; i++) {
  214. ds = dsaddr->ds_list[i];
  215. if (ds != NULL) {
  216. if (atomic_dec_and_lock(&ds->ds_count,
  217. &nfs4_ds_cache_lock)) {
  218. list_del_init(&ds->ds_node);
  219. spin_unlock(&nfs4_ds_cache_lock);
  220. destroy_ds(ds);
  221. }
  222. }
  223. }
  224. kfree(dsaddr->stripe_indices);
  225. kfree(dsaddr);
  226. }
  227. /*
  228. * Create a string with a human readable address and port to avoid
  229. * complicated setup around many dprinks.
  230. */
  231. static char *
  232. nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags)
  233. {
  234. struct nfs4_pnfs_ds_addr *da;
  235. char *remotestr;
  236. size_t len;
  237. char *p;
  238. len = 3; /* '{', '}' and eol */
  239. list_for_each_entry(da, dsaddrs, da_node) {
  240. len += strlen(da->da_remotestr) + 1; /* string plus comma */
  241. }
  242. remotestr = kzalloc(len, gfp_flags);
  243. if (!remotestr)
  244. return NULL;
  245. p = remotestr;
  246. *(p++) = '{';
  247. len--;
  248. list_for_each_entry(da, dsaddrs, da_node) {
  249. size_t ll = strlen(da->da_remotestr);
  250. if (ll > len)
  251. goto out_err;
  252. memcpy(p, da->da_remotestr, ll);
  253. p += ll;
  254. len -= ll;
  255. if (len < 1)
  256. goto out_err;
  257. (*p++) = ',';
  258. len--;
  259. }
  260. if (len < 2)
  261. goto out_err;
  262. *(p++) = '}';
  263. *p = '\0';
  264. return remotestr;
  265. out_err:
  266. kfree(remotestr);
  267. return NULL;
  268. }
  269. static struct nfs4_pnfs_ds *
  270. nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags)
  271. {
  272. struct nfs4_pnfs_ds *tmp_ds, *ds = NULL;
  273. char *remotestr;
  274. if (list_empty(dsaddrs)) {
  275. dprintk("%s: no addresses defined\n", __func__);
  276. goto out;
  277. }
  278. ds = kzalloc(sizeof(*ds), gfp_flags);
  279. if (!ds)
  280. goto out;
  281. /* this is only used for debugging, so it's ok if its NULL */
  282. remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags);
  283. spin_lock(&nfs4_ds_cache_lock);
  284. tmp_ds = _data_server_lookup_locked(dsaddrs);
  285. if (tmp_ds == NULL) {
  286. INIT_LIST_HEAD(&ds->ds_addrs);
  287. list_splice_init(dsaddrs, &ds->ds_addrs);
  288. ds->ds_remotestr = remotestr;
  289. atomic_set(&ds->ds_count, 1);
  290. INIT_LIST_HEAD(&ds->ds_node);
  291. ds->ds_clp = NULL;
  292. list_add(&ds->ds_node, &nfs4_data_server_cache);
  293. dprintk("%s add new data server %s\n", __func__,
  294. ds->ds_remotestr);
  295. } else {
  296. kfree(remotestr);
  297. kfree(ds);
  298. atomic_inc(&tmp_ds->ds_count);
  299. dprintk("%s data server %s found, inc'ed ds_count to %d\n",
  300. __func__, tmp_ds->ds_remotestr,
  301. atomic_read(&tmp_ds->ds_count));
  302. ds = tmp_ds;
  303. }
  304. spin_unlock(&nfs4_ds_cache_lock);
  305. out:
  306. return ds;
  307. }
  308. /*
  309. * Currently only supports ipv4, ipv6 and one multi-path address.
  310. */
  311. static struct nfs4_pnfs_ds_addr *
  312. decode_ds_addr(struct net *net, struct xdr_stream *streamp, gfp_t gfp_flags)
  313. {
  314. struct nfs4_pnfs_ds_addr *da = NULL;
  315. char *buf, *portstr;
  316. __be16 port;
  317. int nlen, rlen;
  318. int tmp[2];
  319. __be32 *p;
  320. char *netid, *match_netid;
  321. size_t len, match_netid_len;
  322. char *startsep = "";
  323. char *endsep = "";
  324. /* r_netid */
  325. p = xdr_inline_decode(streamp, 4);
  326. if (unlikely(!p))
  327. goto out_err;
  328. nlen = be32_to_cpup(p++);
  329. p = xdr_inline_decode(streamp, nlen);
  330. if (unlikely(!p))
  331. goto out_err;
  332. netid = kmalloc(nlen+1, gfp_flags);
  333. if (unlikely(!netid))
  334. goto out_err;
  335. netid[nlen] = '\0';
  336. memcpy(netid, p, nlen);
  337. /* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */
  338. p = xdr_inline_decode(streamp, 4);
  339. if (unlikely(!p))
  340. goto out_free_netid;
  341. rlen = be32_to_cpup(p);
  342. p = xdr_inline_decode(streamp, rlen);
  343. if (unlikely(!p))
  344. goto out_free_netid;
  345. /* port is ".ABC.DEF", 8 chars max */
  346. if (rlen > INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 8) {
  347. dprintk("%s: Invalid address, length %d\n", __func__,
  348. rlen);
  349. goto out_free_netid;
  350. }
  351. buf = kmalloc(rlen + 1, gfp_flags);
  352. if (!buf) {
  353. dprintk("%s: Not enough memory\n", __func__);
  354. goto out_free_netid;
  355. }
  356. buf[rlen] = '\0';
  357. memcpy(buf, p, rlen);
  358. /* replace port '.' with '-' */
  359. portstr = strrchr(buf, '.');
  360. if (!portstr) {
  361. dprintk("%s: Failed finding expected dot in port\n",
  362. __func__);
  363. goto out_free_buf;
  364. }
  365. *portstr = '-';
  366. /* find '.' between address and port */
  367. portstr = strrchr(buf, '.');
  368. if (!portstr) {
  369. dprintk("%s: Failed finding expected dot between address and "
  370. "port\n", __func__);
  371. goto out_free_buf;
  372. }
  373. *portstr = '\0';
  374. da = kzalloc(sizeof(*da), gfp_flags);
  375. if (unlikely(!da))
  376. goto out_free_buf;
  377. INIT_LIST_HEAD(&da->da_node);
  378. if (!rpc_pton(net, buf, portstr-buf, (struct sockaddr *)&da->da_addr,
  379. sizeof(da->da_addr))) {
  380. dprintk("%s: error parsing address %s\n", __func__, buf);
  381. goto out_free_da;
  382. }
  383. portstr++;
  384. sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]);
  385. port = htons((tmp[0] << 8) | (tmp[1]));
  386. switch (da->da_addr.ss_family) {
  387. case AF_INET:
  388. ((struct sockaddr_in *)&da->da_addr)->sin_port = port;
  389. da->da_addrlen = sizeof(struct sockaddr_in);
  390. match_netid = "tcp";
  391. match_netid_len = 3;
  392. break;
  393. case AF_INET6:
  394. ((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port;
  395. da->da_addrlen = sizeof(struct sockaddr_in6);
  396. match_netid = "tcp6";
  397. match_netid_len = 4;
  398. startsep = "[";
  399. endsep = "]";
  400. break;
  401. default:
  402. dprintk("%s: unsupported address family: %u\n",
  403. __func__, da->da_addr.ss_family);
  404. goto out_free_da;
  405. }
  406. if (nlen != match_netid_len || strncmp(netid, match_netid, nlen)) {
  407. dprintk("%s: ERROR: r_netid \"%s\" != \"%s\"\n",
  408. __func__, netid, match_netid);
  409. goto out_free_da;
  410. }
  411. /* save human readable address */
  412. len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7;
  413. da->da_remotestr = kzalloc(len, gfp_flags);
  414. /* NULL is ok, only used for dprintk */
  415. if (da->da_remotestr)
  416. snprintf(da->da_remotestr, len, "%s%s%s:%u", startsep,
  417. buf, endsep, ntohs(port));
  418. dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr);
  419. kfree(buf);
  420. kfree(netid);
  421. return da;
  422. out_free_da:
  423. kfree(da);
  424. out_free_buf:
  425. dprintk("%s: Error parsing DS addr: %s\n", __func__, buf);
  426. kfree(buf);
  427. out_free_netid:
  428. kfree(netid);
  429. out_err:
  430. return NULL;
  431. }
  432. /* Decode opaque device data and return the result */
  433. static struct nfs4_file_layout_dsaddr*
  434. decode_device(struct inode *ino, struct pnfs_device *pdev, gfp_t gfp_flags)
  435. {
  436. int i;
  437. u32 cnt, num;
  438. u8 *indexp;
  439. __be32 *p;
  440. u8 *stripe_indices;
  441. u8 max_stripe_index;
  442. struct nfs4_file_layout_dsaddr *dsaddr = NULL;
  443. struct xdr_stream stream;
  444. struct xdr_buf buf;
  445. struct page *scratch;
  446. struct list_head dsaddrs;
  447. struct nfs4_pnfs_ds_addr *da;
  448. /* set up xdr stream */
  449. scratch = alloc_page(gfp_flags);
  450. if (!scratch)
  451. goto out_err;
  452. xdr_init_decode_pages(&stream, &buf, pdev->pages, pdev->pglen);
  453. xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
  454. /* Get the stripe count (number of stripe index) */
  455. p = xdr_inline_decode(&stream, 4);
  456. if (unlikely(!p))
  457. goto out_err_free_scratch;
  458. cnt = be32_to_cpup(p);
  459. dprintk("%s stripe count %d\n", __func__, cnt);
  460. if (cnt > NFS4_PNFS_MAX_STRIPE_CNT) {
  461. printk(KERN_WARNING "NFS: %s: stripe count %d greater than "
  462. "supported maximum %d\n", __func__,
  463. cnt, NFS4_PNFS_MAX_STRIPE_CNT);
  464. goto out_err_free_scratch;
  465. }
  466. /* read stripe indices */
  467. stripe_indices = kcalloc(cnt, sizeof(u8), gfp_flags);
  468. if (!stripe_indices)
  469. goto out_err_free_scratch;
  470. p = xdr_inline_decode(&stream, cnt << 2);
  471. if (unlikely(!p))
  472. goto out_err_free_stripe_indices;
  473. indexp = &stripe_indices[0];
  474. max_stripe_index = 0;
  475. for (i = 0; i < cnt; i++) {
  476. *indexp = be32_to_cpup(p++);
  477. max_stripe_index = max(max_stripe_index, *indexp);
  478. indexp++;
  479. }
  480. /* Check the multipath list count */
  481. p = xdr_inline_decode(&stream, 4);
  482. if (unlikely(!p))
  483. goto out_err_free_stripe_indices;
  484. num = be32_to_cpup(p);
  485. dprintk("%s ds_num %u\n", __func__, num);
  486. if (num > NFS4_PNFS_MAX_MULTI_CNT) {
  487. printk(KERN_WARNING "NFS: %s: multipath count %d greater than "
  488. "supported maximum %d\n", __func__,
  489. num, NFS4_PNFS_MAX_MULTI_CNT);
  490. goto out_err_free_stripe_indices;
  491. }
  492. /* validate stripe indices are all < num */
  493. if (max_stripe_index >= num) {
  494. printk(KERN_WARNING "NFS: %s: stripe index %u >= num ds %u\n",
  495. __func__, max_stripe_index, num);
  496. goto out_err_free_stripe_indices;
  497. }
  498. dsaddr = kzalloc(sizeof(*dsaddr) +
  499. (sizeof(struct nfs4_pnfs_ds *) * (num - 1)),
  500. gfp_flags);
  501. if (!dsaddr)
  502. goto out_err_free_stripe_indices;
  503. dsaddr->stripe_count = cnt;
  504. dsaddr->stripe_indices = stripe_indices;
  505. stripe_indices = NULL;
  506. dsaddr->ds_num = num;
  507. nfs4_init_deviceid_node(&dsaddr->id_node,
  508. NFS_SERVER(ino)->pnfs_curr_ld,
  509. NFS_SERVER(ino)->nfs_client,
  510. &pdev->dev_id);
  511. INIT_LIST_HEAD(&dsaddrs);
  512. for (i = 0; i < dsaddr->ds_num; i++) {
  513. int j;
  514. u32 mp_count;
  515. p = xdr_inline_decode(&stream, 4);
  516. if (unlikely(!p))
  517. goto out_err_free_deviceid;
  518. mp_count = be32_to_cpup(p); /* multipath count */
  519. for (j = 0; j < mp_count; j++) {
  520. da = decode_ds_addr(NFS_SERVER(ino)->nfs_client->net,
  521. &stream, gfp_flags);
  522. if (da)
  523. list_add_tail(&da->da_node, &dsaddrs);
  524. }
  525. if (list_empty(&dsaddrs)) {
  526. dprintk("%s: no suitable DS addresses found\n",
  527. __func__);
  528. goto out_err_free_deviceid;
  529. }
  530. dsaddr->ds_list[i] = nfs4_pnfs_ds_add(&dsaddrs, gfp_flags);
  531. if (!dsaddr->ds_list[i])
  532. goto out_err_drain_dsaddrs;
  533. /* If DS was already in cache, free ds addrs */
  534. while (!list_empty(&dsaddrs)) {
  535. da = list_first_entry(&dsaddrs,
  536. struct nfs4_pnfs_ds_addr,
  537. da_node);
  538. list_del_init(&da->da_node);
  539. kfree(da->da_remotestr);
  540. kfree(da);
  541. }
  542. }
  543. __free_page(scratch);
  544. return dsaddr;
  545. out_err_drain_dsaddrs:
  546. while (!list_empty(&dsaddrs)) {
  547. da = list_first_entry(&dsaddrs, struct nfs4_pnfs_ds_addr,
  548. da_node);
  549. list_del_init(&da->da_node);
  550. kfree(da->da_remotestr);
  551. kfree(da);
  552. }
  553. out_err_free_deviceid:
  554. nfs4_fl_free_deviceid(dsaddr);
  555. /* stripe_indicies was part of dsaddr */
  556. goto out_err_free_scratch;
  557. out_err_free_stripe_indices:
  558. kfree(stripe_indices);
  559. out_err_free_scratch:
  560. __free_page(scratch);
  561. out_err:
  562. dprintk("%s ERROR: returning NULL\n", __func__);
  563. return NULL;
  564. }
  565. /*
  566. * Decode the opaque device specified in 'dev' and add it to the cache of
  567. * available devices.
  568. */
  569. static struct nfs4_file_layout_dsaddr *
  570. decode_and_add_device(struct inode *inode, struct pnfs_device *dev, gfp_t gfp_flags)
  571. {
  572. struct nfs4_deviceid_node *d;
  573. struct nfs4_file_layout_dsaddr *n, *new;
  574. new = decode_device(inode, dev, gfp_flags);
  575. if (!new) {
  576. printk(KERN_WARNING "NFS: %s: Could not decode or add device\n",
  577. __func__);
  578. return NULL;
  579. }
  580. d = nfs4_insert_deviceid_node(&new->id_node);
  581. n = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
  582. if (n != new) {
  583. nfs4_fl_free_deviceid(new);
  584. return n;
  585. }
  586. return new;
  587. }
  588. /*
  589. * Retrieve the information for dev_id, add it to the list
  590. * of available devices, and return it.
  591. */
  592. struct nfs4_file_layout_dsaddr *
  593. get_device_info(struct inode *inode, struct nfs4_deviceid *dev_id, gfp_t gfp_flags)
  594. {
  595. struct pnfs_device *pdev = NULL;
  596. u32 max_resp_sz;
  597. int max_pages;
  598. struct page **pages = NULL;
  599. struct nfs4_file_layout_dsaddr *dsaddr = NULL;
  600. int rc, i;
  601. struct nfs_server *server = NFS_SERVER(inode);
  602. /*
  603. * Use the session max response size as the basis for setting
  604. * GETDEVICEINFO's maxcount
  605. */
  606. max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
  607. max_pages = nfs_page_array_len(0, max_resp_sz);
  608. dprintk("%s inode %p max_resp_sz %u max_pages %d\n",
  609. __func__, inode, max_resp_sz, max_pages);
  610. pdev = kzalloc(sizeof(struct pnfs_device), gfp_flags);
  611. if (pdev == NULL)
  612. return NULL;
  613. pages = kzalloc(max_pages * sizeof(struct page *), gfp_flags);
  614. if (pages == NULL) {
  615. kfree(pdev);
  616. return NULL;
  617. }
  618. for (i = 0; i < max_pages; i++) {
  619. pages[i] = alloc_page(gfp_flags);
  620. if (!pages[i])
  621. goto out_free;
  622. }
  623. memcpy(&pdev->dev_id, dev_id, sizeof(*dev_id));
  624. pdev->layout_type = LAYOUT_NFSV4_1_FILES;
  625. pdev->pages = pages;
  626. pdev->pgbase = 0;
  627. pdev->pglen = PAGE_SIZE * max_pages;
  628. pdev->mincount = 0;
  629. rc = nfs4_proc_getdeviceinfo(server, pdev);
  630. dprintk("%s getdevice info returns %d\n", __func__, rc);
  631. if (rc)
  632. goto out_free;
  633. /*
  634. * Found new device, need to decode it and then add it to the
  635. * list of known devices for this mountpoint.
  636. */
  637. dsaddr = decode_and_add_device(inode, pdev, gfp_flags);
  638. out_free:
  639. for (i = 0; i < max_pages; i++)
  640. __free_page(pages[i]);
  641. kfree(pages);
  642. kfree(pdev);
  643. dprintk("<-- %s dsaddr %p\n", __func__, dsaddr);
  644. return dsaddr;
  645. }
  646. void
  647. nfs4_fl_put_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
  648. {
  649. nfs4_put_deviceid_node(&dsaddr->id_node);
  650. }
  651. /*
  652. * Want res = (offset - layout->pattern_offset)/ layout->stripe_unit
  653. * Then: ((res + fsi) % dsaddr->stripe_count)
  654. */
  655. u32
  656. nfs4_fl_calc_j_index(struct pnfs_layout_segment *lseg, loff_t offset)
  657. {
  658. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  659. u64 tmp;
  660. tmp = offset - flseg->pattern_offset;
  661. do_div(tmp, flseg->stripe_unit);
  662. tmp += flseg->first_stripe_index;
  663. return do_div(tmp, flseg->dsaddr->stripe_count);
  664. }
  665. u32
  666. nfs4_fl_calc_ds_index(struct pnfs_layout_segment *lseg, u32 j)
  667. {
  668. return FILELAYOUT_LSEG(lseg)->dsaddr->stripe_indices[j];
  669. }
  670. struct nfs_fh *
  671. nfs4_fl_select_ds_fh(struct pnfs_layout_segment *lseg, u32 j)
  672. {
  673. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  674. u32 i;
  675. if (flseg->stripe_type == STRIPE_SPARSE) {
  676. if (flseg->num_fh == 1)
  677. i = 0;
  678. else if (flseg->num_fh == 0)
  679. /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
  680. return NULL;
  681. else
  682. i = nfs4_fl_calc_ds_index(lseg, j);
  683. } else
  684. i = j;
  685. return flseg->fh_array[i];
  686. }
  687. struct nfs4_pnfs_ds *
  688. nfs4_fl_prepare_ds(struct pnfs_layout_segment *lseg, u32 ds_idx)
  689. {
  690. struct nfs4_file_layout_dsaddr *dsaddr = FILELAYOUT_LSEG(lseg)->dsaddr;
  691. struct nfs4_pnfs_ds *ds = dsaddr->ds_list[ds_idx];
  692. struct nfs4_deviceid_node *devid = FILELAYOUT_DEVID_NODE(lseg);
  693. if (filelayout_test_devid_invalid(devid))
  694. return NULL;
  695. if (ds == NULL) {
  696. printk(KERN_ERR "NFS: %s: No data server for offset index %d\n",
  697. __func__, ds_idx);
  698. goto mark_dev_invalid;
  699. }
  700. if (!ds->ds_clp) {
  701. struct nfs_server *s = NFS_SERVER(lseg->pls_layout->plh_inode);
  702. int err;
  703. err = nfs4_ds_connect(s, ds);
  704. if (err)
  705. goto mark_dev_invalid;
  706. }
  707. return ds;
  708. mark_dev_invalid:
  709. filelayout_mark_devid_invalid(devid);
  710. return NULL;
  711. }
  712. module_param(dataserver_retrans, uint, 0644);
  713. MODULE_PARM_DESC(dataserver_retrans, "The number of times the NFSv4.1 client "
  714. "retries a request before it attempts further "
  715. " recovery action.");
  716. module_param(dataserver_timeo, uint, 0644);
  717. MODULE_PARM_DESC(dataserver_timeo, "The time (in tenths of a second) the "
  718. "NFSv4.1 client waits for a response from a "
  719. " data server before it retries an NFS request.");