nfs4filelayoutdev.c 21 KB

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