nfs4filelayoutdev.c 21 KB

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