nfs4filelayoutdev.c 21 KB

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