nfs4filelayoutdev.c 20 KB

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