nfs4filelayoutdev.c 19 KB

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