nfs4filelayoutdev.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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. * Device ID RCU cache. A device ID is unique per client ID and layout type.
  37. */
  38. #define NFS4_FL_DEVICE_ID_HASH_BITS 5
  39. #define NFS4_FL_DEVICE_ID_HASH_SIZE (1 << NFS4_FL_DEVICE_ID_HASH_BITS)
  40. #define NFS4_FL_DEVICE_ID_HASH_MASK (NFS4_FL_DEVICE_ID_HASH_SIZE - 1)
  41. static inline u32
  42. nfs4_fl_deviceid_hash(struct nfs4_deviceid *id)
  43. {
  44. unsigned char *cptr = (unsigned char *)id->data;
  45. unsigned int nbytes = NFS4_DEVICEID4_SIZE;
  46. u32 x = 0;
  47. while (nbytes--) {
  48. x *= 37;
  49. x += *cptr++;
  50. }
  51. return x & NFS4_FL_DEVICE_ID_HASH_MASK;
  52. }
  53. static struct hlist_head filelayout_deviceid_cache[NFS4_FL_DEVICE_ID_HASH_SIZE];
  54. static DEFINE_SPINLOCK(filelayout_deviceid_lock);
  55. /*
  56. * Data server cache
  57. *
  58. * Data servers can be mapped to different device ids.
  59. * nfs4_pnfs_ds reference counting
  60. * - set to 1 on allocation
  61. * - incremented when a device id maps a data server already in the cache.
  62. * - decremented when deviceid is removed from the cache.
  63. */
  64. DEFINE_SPINLOCK(nfs4_ds_cache_lock);
  65. static LIST_HEAD(nfs4_data_server_cache);
  66. /* Debug routines */
  67. void
  68. print_ds(struct nfs4_pnfs_ds *ds)
  69. {
  70. if (ds == NULL) {
  71. printk("%s NULL device\n", __func__);
  72. return;
  73. }
  74. printk(" ip_addr %x port %hu\n"
  75. " ref count %d\n"
  76. " client %p\n"
  77. " cl_exchange_flags %x\n",
  78. ntohl(ds->ds_ip_addr), ntohs(ds->ds_port),
  79. atomic_read(&ds->ds_count), ds->ds_clp,
  80. ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0);
  81. }
  82. void
  83. print_ds_list(struct nfs4_file_layout_dsaddr *dsaddr)
  84. {
  85. int i;
  86. ifdebug(FACILITY) {
  87. printk("%s dsaddr->ds_num %d\n", __func__,
  88. dsaddr->ds_num);
  89. for (i = 0; i < dsaddr->ds_num; i++)
  90. print_ds(dsaddr->ds_list[i]);
  91. }
  92. }
  93. void print_deviceid(struct nfs4_deviceid *id)
  94. {
  95. u32 *p = (u32 *)id;
  96. dprintk("%s: device id= [%x%x%x%x]\n", __func__,
  97. p[0], p[1], p[2], p[3]);
  98. }
  99. /* nfs4_ds_cache_lock is held */
  100. static struct nfs4_pnfs_ds *
  101. _data_server_lookup_locked(u32 ip_addr, u32 port)
  102. {
  103. struct nfs4_pnfs_ds *ds;
  104. dprintk("_data_server_lookup: ip_addr=%x port=%hu\n",
  105. ntohl(ip_addr), ntohs(port));
  106. list_for_each_entry(ds, &nfs4_data_server_cache, ds_node) {
  107. if (ds->ds_ip_addr == ip_addr &&
  108. ds->ds_port == port) {
  109. return ds;
  110. }
  111. }
  112. return NULL;
  113. }
  114. /*
  115. * Create an rpc connection to the nfs4_pnfs_ds data server
  116. * Currently only support IPv4
  117. */
  118. static int
  119. nfs4_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds)
  120. {
  121. struct nfs_client *clp;
  122. struct sockaddr_in sin;
  123. int status = 0;
  124. dprintk("--> %s ip:port %x:%hu au_flavor %d\n", __func__,
  125. ntohl(ds->ds_ip_addr), ntohs(ds->ds_port),
  126. mds_srv->nfs_client->cl_rpcclient->cl_auth->au_flavor);
  127. sin.sin_family = AF_INET;
  128. sin.sin_addr.s_addr = ds->ds_ip_addr;
  129. sin.sin_port = ds->ds_port;
  130. clp = nfs4_set_ds_client(mds_srv->nfs_client, (struct sockaddr *)&sin,
  131. sizeof(sin), IPPROTO_TCP);
  132. if (IS_ERR(clp)) {
  133. status = PTR_ERR(clp);
  134. goto out;
  135. }
  136. if ((clp->cl_exchange_flags & EXCHGID4_FLAG_MASK_PNFS) != 0) {
  137. if (!is_ds_client(clp)) {
  138. status = -ENODEV;
  139. goto out_put;
  140. }
  141. ds->ds_clp = clp;
  142. dprintk("%s [existing] ip=%x, port=%hu\n", __func__,
  143. ntohl(ds->ds_ip_addr), ntohs(ds->ds_port));
  144. goto out;
  145. }
  146. /*
  147. * Do not set NFS_CS_CHECK_LEASE_TIME instead set the DS lease to
  148. * be equal to the MDS lease. Renewal is scheduled in create_session.
  149. */
  150. spin_lock(&mds_srv->nfs_client->cl_lock);
  151. clp->cl_lease_time = mds_srv->nfs_client->cl_lease_time;
  152. spin_unlock(&mds_srv->nfs_client->cl_lock);
  153. clp->cl_last_renewal = jiffies;
  154. /* New nfs_client */
  155. status = nfs4_init_ds_session(clp);
  156. if (status)
  157. goto out_put;
  158. ds->ds_clp = clp;
  159. dprintk("%s [new] ip=%x, port=%hu\n", __func__, ntohl(ds->ds_ip_addr),
  160. ntohs(ds->ds_port));
  161. out:
  162. return status;
  163. out_put:
  164. nfs_put_client(clp);
  165. goto out;
  166. }
  167. static void
  168. destroy_ds(struct nfs4_pnfs_ds *ds)
  169. {
  170. dprintk("--> %s\n", __func__);
  171. ifdebug(FACILITY)
  172. print_ds(ds);
  173. if (ds->ds_clp)
  174. nfs_put_client(ds->ds_clp);
  175. kfree(ds);
  176. }
  177. static void
  178. nfs4_fl_free_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
  179. {
  180. struct nfs4_pnfs_ds *ds;
  181. int i;
  182. print_deviceid(&dsaddr->deviceid);
  183. for (i = 0; i < dsaddr->ds_num; i++) {
  184. ds = dsaddr->ds_list[i];
  185. if (ds != NULL) {
  186. if (atomic_dec_and_lock(&ds->ds_count,
  187. &nfs4_ds_cache_lock)) {
  188. list_del_init(&ds->ds_node);
  189. spin_unlock(&nfs4_ds_cache_lock);
  190. destroy_ds(ds);
  191. }
  192. }
  193. }
  194. kfree(dsaddr->stripe_indices);
  195. kfree(dsaddr);
  196. }
  197. static struct nfs4_pnfs_ds *
  198. nfs4_pnfs_ds_add(struct inode *inode, u32 ip_addr, u32 port)
  199. {
  200. struct nfs4_pnfs_ds *tmp_ds, *ds;
  201. ds = kzalloc(sizeof(*tmp_ds), GFP_KERNEL);
  202. if (!ds)
  203. goto out;
  204. spin_lock(&nfs4_ds_cache_lock);
  205. tmp_ds = _data_server_lookup_locked(ip_addr, port);
  206. if (tmp_ds == NULL) {
  207. ds->ds_ip_addr = ip_addr;
  208. ds->ds_port = port;
  209. atomic_set(&ds->ds_count, 1);
  210. INIT_LIST_HEAD(&ds->ds_node);
  211. ds->ds_clp = NULL;
  212. list_add(&ds->ds_node, &nfs4_data_server_cache);
  213. dprintk("%s add new data server ip 0x%x\n", __func__,
  214. ds->ds_ip_addr);
  215. } else {
  216. kfree(ds);
  217. atomic_inc(&tmp_ds->ds_count);
  218. dprintk("%s data server found ip 0x%x, inc'ed ds_count to %d\n",
  219. __func__, tmp_ds->ds_ip_addr,
  220. atomic_read(&tmp_ds->ds_count));
  221. ds = tmp_ds;
  222. }
  223. spin_unlock(&nfs4_ds_cache_lock);
  224. out:
  225. return ds;
  226. }
  227. /*
  228. * Currently only support ipv4, and one multi-path address.
  229. */
  230. static struct nfs4_pnfs_ds *
  231. decode_and_add_ds(struct xdr_stream *streamp, struct inode *inode)
  232. {
  233. struct nfs4_pnfs_ds *ds = NULL;
  234. char *buf;
  235. const char *ipend, *pstr;
  236. u32 ip_addr, port;
  237. int nlen, rlen, i;
  238. int tmp[2];
  239. __be32 *p;
  240. /* r_netid */
  241. p = xdr_inline_decode(streamp, 4);
  242. if (unlikely(!p))
  243. goto out_err;
  244. nlen = be32_to_cpup(p++);
  245. p = xdr_inline_decode(streamp, nlen);
  246. if (unlikely(!p))
  247. goto out_err;
  248. /* Check that netid is "tcp" */
  249. if (nlen != 3 || memcmp((char *)p, "tcp", 3)) {
  250. dprintk("%s: ERROR: non ipv4 TCP r_netid\n", __func__);
  251. goto out_err;
  252. }
  253. /* r_addr */
  254. p = xdr_inline_decode(streamp, 4);
  255. if (unlikely(!p))
  256. goto out_err;
  257. rlen = be32_to_cpup(p);
  258. p = xdr_inline_decode(streamp, rlen);
  259. if (unlikely(!p))
  260. goto out_err;
  261. /* ipv6 length plus port is legal */
  262. if (rlen > INET6_ADDRSTRLEN + 8) {
  263. dprintk("%s: Invalid address, length %d\n", __func__,
  264. rlen);
  265. goto out_err;
  266. }
  267. buf = kmalloc(rlen + 1, GFP_KERNEL);
  268. if (!buf) {
  269. dprintk("%s: Not enough memory\n", __func__);
  270. goto out_err;
  271. }
  272. buf[rlen] = '\0';
  273. memcpy(buf, p, rlen);
  274. /* replace the port dots with dashes for the in4_pton() delimiter*/
  275. for (i = 0; i < 2; i++) {
  276. char *res = strrchr(buf, '.');
  277. if (!res) {
  278. dprintk("%s: Failed finding expected dots in port\n",
  279. __func__);
  280. goto out_free;
  281. }
  282. *res = '-';
  283. }
  284. /* Currently only support ipv4 address */
  285. if (in4_pton(buf, rlen, (u8 *)&ip_addr, '-', &ipend) == 0) {
  286. dprintk("%s: Only ipv4 addresses supported\n", __func__);
  287. goto out_free;
  288. }
  289. /* port */
  290. pstr = ipend;
  291. sscanf(pstr, "-%d-%d", &tmp[0], &tmp[1]);
  292. port = htons((tmp[0] << 8) | (tmp[1]));
  293. ds = nfs4_pnfs_ds_add(inode, ip_addr, port);
  294. dprintk("%s: Decoded address and port %s\n", __func__, buf);
  295. out_free:
  296. kfree(buf);
  297. out_err:
  298. return ds;
  299. }
  300. /* Decode opaque device data and return the result */
  301. static struct nfs4_file_layout_dsaddr*
  302. decode_device(struct inode *ino, struct pnfs_device *pdev)
  303. {
  304. int i;
  305. u32 cnt, num;
  306. u8 *indexp;
  307. __be32 *p;
  308. u8 *stripe_indices;
  309. u8 max_stripe_index;
  310. struct nfs4_file_layout_dsaddr *dsaddr = NULL;
  311. struct xdr_stream stream;
  312. struct xdr_buf buf = {
  313. .pages = pdev->pages,
  314. .page_len = pdev->pglen,
  315. .buflen = pdev->pglen,
  316. .len = pdev->pglen,
  317. };
  318. struct page *scratch;
  319. /* set up xdr stream */
  320. scratch = alloc_page(GFP_KERNEL);
  321. if (!scratch)
  322. goto out_err;
  323. xdr_init_decode(&stream, &buf, NULL);
  324. xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
  325. /* Get the stripe count (number of stripe index) */
  326. p = xdr_inline_decode(&stream, 4);
  327. if (unlikely(!p))
  328. goto out_err_free_scratch;
  329. cnt = be32_to_cpup(p);
  330. dprintk("%s stripe count %d\n", __func__, cnt);
  331. if (cnt > NFS4_PNFS_MAX_STRIPE_CNT) {
  332. printk(KERN_WARNING "%s: stripe count %d greater than "
  333. "supported maximum %d\n", __func__,
  334. cnt, NFS4_PNFS_MAX_STRIPE_CNT);
  335. goto out_err_free_scratch;
  336. }
  337. /* read stripe indices */
  338. stripe_indices = kcalloc(cnt, sizeof(u8), GFP_KERNEL);
  339. if (!stripe_indices)
  340. goto out_err_free_scratch;
  341. p = xdr_inline_decode(&stream, cnt << 2);
  342. if (unlikely(!p))
  343. goto out_err_free_stripe_indices;
  344. indexp = &stripe_indices[0];
  345. max_stripe_index = 0;
  346. for (i = 0; i < cnt; i++) {
  347. *indexp = be32_to_cpup(p++);
  348. max_stripe_index = max(max_stripe_index, *indexp);
  349. indexp++;
  350. }
  351. /* Check the multipath list count */
  352. p = xdr_inline_decode(&stream, 4);
  353. if (unlikely(!p))
  354. goto out_err_free_stripe_indices;
  355. num = be32_to_cpup(p);
  356. dprintk("%s ds_num %u\n", __func__, num);
  357. if (num > NFS4_PNFS_MAX_MULTI_CNT) {
  358. printk(KERN_WARNING "%s: multipath count %d greater than "
  359. "supported maximum %d\n", __func__,
  360. num, NFS4_PNFS_MAX_MULTI_CNT);
  361. goto out_err_free_stripe_indices;
  362. }
  363. /* validate stripe indices are all < num */
  364. if (max_stripe_index >= num) {
  365. printk(KERN_WARNING "%s: stripe index %u >= num ds %u\n",
  366. __func__, max_stripe_index, num);
  367. goto out_err_free_stripe_indices;
  368. }
  369. dsaddr = kzalloc(sizeof(*dsaddr) +
  370. (sizeof(struct nfs4_pnfs_ds *) * (num - 1)),
  371. GFP_KERNEL);
  372. if (!dsaddr)
  373. goto out_err_free_stripe_indices;
  374. dsaddr->stripe_count = cnt;
  375. dsaddr->stripe_indices = stripe_indices;
  376. stripe_indices = NULL;
  377. dsaddr->ds_num = num;
  378. memcpy(&dsaddr->deviceid, &pdev->dev_id, sizeof(pdev->dev_id));
  379. for (i = 0; i < dsaddr->ds_num; i++) {
  380. int j;
  381. u32 mp_count;
  382. p = xdr_inline_decode(&stream, 4);
  383. if (unlikely(!p))
  384. goto out_err_free_deviceid;
  385. mp_count = be32_to_cpup(p); /* multipath count */
  386. if (mp_count > 1) {
  387. printk(KERN_WARNING
  388. "%s: Multipath count %d not supported, "
  389. "skipping all greater than 1\n", __func__,
  390. mp_count);
  391. }
  392. for (j = 0; j < mp_count; j++) {
  393. if (j == 0) {
  394. dsaddr->ds_list[i] = decode_and_add_ds(&stream,
  395. ino);
  396. if (dsaddr->ds_list[i] == NULL)
  397. goto out_err_free_deviceid;
  398. } else {
  399. u32 len;
  400. /* skip extra multipath */
  401. /* read len, skip */
  402. p = xdr_inline_decode(&stream, 4);
  403. if (unlikely(!p))
  404. goto out_err_free_deviceid;
  405. len = be32_to_cpup(p);
  406. p = xdr_inline_decode(&stream, len);
  407. if (unlikely(!p))
  408. goto out_err_free_deviceid;
  409. /* read len, skip */
  410. p = xdr_inline_decode(&stream, 4);
  411. if (unlikely(!p))
  412. goto out_err_free_deviceid;
  413. len = be32_to_cpup(p);
  414. p = xdr_inline_decode(&stream, len);
  415. if (unlikely(!p))
  416. goto out_err_free_deviceid;
  417. }
  418. }
  419. }
  420. __free_page(scratch);
  421. return dsaddr;
  422. out_err_free_deviceid:
  423. nfs4_fl_free_deviceid(dsaddr);
  424. /* stripe_indicies was part of dsaddr */
  425. goto out_err_free_scratch;
  426. out_err_free_stripe_indices:
  427. kfree(stripe_indices);
  428. out_err_free_scratch:
  429. __free_page(scratch);
  430. out_err:
  431. dprintk("%s ERROR: returning NULL\n", __func__);
  432. return NULL;
  433. }
  434. /*
  435. * Decode the opaque device specified in 'dev' and add it to the cache of
  436. * available devices.
  437. */
  438. static struct nfs4_file_layout_dsaddr *
  439. decode_and_add_device(struct inode *inode, struct pnfs_device *dev)
  440. {
  441. struct nfs4_file_layout_dsaddr *d, *new;
  442. long hash;
  443. new = decode_device(inode, dev);
  444. if (!new) {
  445. printk(KERN_WARNING "%s: Could not decode or add device\n",
  446. __func__);
  447. return NULL;
  448. }
  449. spin_lock(&filelayout_deviceid_lock);
  450. d = nfs4_fl_find_get_deviceid(&new->deviceid);
  451. if (d) {
  452. spin_unlock(&filelayout_deviceid_lock);
  453. nfs4_fl_free_deviceid(new);
  454. return d;
  455. }
  456. INIT_HLIST_NODE(&new->node);
  457. atomic_set(&new->ref, 1);
  458. hash = nfs4_fl_deviceid_hash(&new->deviceid);
  459. hlist_add_head_rcu(&new->node, &filelayout_deviceid_cache[hash]);
  460. spin_unlock(&filelayout_deviceid_lock);
  461. return new;
  462. }
  463. /*
  464. * Retrieve the information for dev_id, add it to the list
  465. * of available devices, and return it.
  466. */
  467. struct nfs4_file_layout_dsaddr *
  468. get_device_info(struct inode *inode, struct nfs4_deviceid *dev_id)
  469. {
  470. struct pnfs_device *pdev = NULL;
  471. u32 max_resp_sz;
  472. int max_pages;
  473. struct page **pages = NULL;
  474. struct nfs4_file_layout_dsaddr *dsaddr = NULL;
  475. int rc, i;
  476. struct nfs_server *server = NFS_SERVER(inode);
  477. /*
  478. * Use the session max response size as the basis for setting
  479. * GETDEVICEINFO's maxcount
  480. */
  481. max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
  482. max_pages = max_resp_sz >> PAGE_SHIFT;
  483. dprintk("%s inode %p max_resp_sz %u max_pages %d\n",
  484. __func__, inode, max_resp_sz, max_pages);
  485. pdev = kzalloc(sizeof(struct pnfs_device), GFP_KERNEL);
  486. if (pdev == NULL)
  487. return NULL;
  488. pages = kzalloc(max_pages * sizeof(struct page *), GFP_KERNEL);
  489. if (pages == NULL) {
  490. kfree(pdev);
  491. return NULL;
  492. }
  493. for (i = 0; i < max_pages; i++) {
  494. pages[i] = alloc_page(GFP_KERNEL);
  495. if (!pages[i])
  496. goto out_free;
  497. }
  498. memcpy(&pdev->dev_id, dev_id, sizeof(*dev_id));
  499. pdev->layout_type = LAYOUT_NFSV4_1_FILES;
  500. pdev->pages = pages;
  501. pdev->pgbase = 0;
  502. pdev->pglen = PAGE_SIZE * max_pages;
  503. pdev->mincount = 0;
  504. rc = nfs4_proc_getdeviceinfo(server, pdev);
  505. dprintk("%s getdevice info returns %d\n", __func__, rc);
  506. if (rc)
  507. goto out_free;
  508. /*
  509. * Found new device, need to decode it and then add it to the
  510. * list of known devices for this mountpoint.
  511. */
  512. dsaddr = decode_and_add_device(inode, pdev);
  513. out_free:
  514. for (i = 0; i < max_pages; i++)
  515. __free_page(pages[i]);
  516. kfree(pages);
  517. kfree(pdev);
  518. dprintk("<-- %s dsaddr %p\n", __func__, dsaddr);
  519. return dsaddr;
  520. }
  521. void
  522. nfs4_fl_put_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
  523. {
  524. if (atomic_dec_and_lock(&dsaddr->ref, &filelayout_deviceid_lock)) {
  525. hlist_del_rcu(&dsaddr->node);
  526. spin_unlock(&filelayout_deviceid_lock);
  527. synchronize_rcu();
  528. nfs4_fl_free_deviceid(dsaddr);
  529. }
  530. }
  531. struct nfs4_file_layout_dsaddr *
  532. nfs4_fl_find_get_deviceid(struct nfs4_deviceid *id)
  533. {
  534. struct nfs4_file_layout_dsaddr *d;
  535. struct hlist_node *n;
  536. long hash = nfs4_fl_deviceid_hash(id);
  537. rcu_read_lock();
  538. hlist_for_each_entry_rcu(d, n, &filelayout_deviceid_cache[hash], node) {
  539. if (!memcmp(&d->deviceid, id, sizeof(*id))) {
  540. if (!atomic_inc_not_zero(&d->ref))
  541. goto fail;
  542. rcu_read_unlock();
  543. return d;
  544. }
  545. }
  546. fail:
  547. rcu_read_unlock();
  548. return NULL;
  549. }
  550. /*
  551. * Want res = (offset - layout->pattern_offset)/ layout->stripe_unit
  552. * Then: ((res + fsi) % dsaddr->stripe_count)
  553. */
  554. u32
  555. nfs4_fl_calc_j_index(struct pnfs_layout_segment *lseg, loff_t offset)
  556. {
  557. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  558. u64 tmp;
  559. tmp = offset - flseg->pattern_offset;
  560. do_div(tmp, flseg->stripe_unit);
  561. tmp += flseg->first_stripe_index;
  562. return do_div(tmp, flseg->dsaddr->stripe_count);
  563. }
  564. u32
  565. nfs4_fl_calc_ds_index(struct pnfs_layout_segment *lseg, u32 j)
  566. {
  567. return FILELAYOUT_LSEG(lseg)->dsaddr->stripe_indices[j];
  568. }
  569. struct nfs_fh *
  570. nfs4_fl_select_ds_fh(struct pnfs_layout_segment *lseg, u32 j)
  571. {
  572. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  573. u32 i;
  574. if (flseg->stripe_type == STRIPE_SPARSE) {
  575. if (flseg->num_fh == 1)
  576. i = 0;
  577. else if (flseg->num_fh == 0)
  578. /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
  579. return NULL;
  580. else
  581. i = nfs4_fl_calc_ds_index(lseg, j);
  582. } else
  583. i = j;
  584. return flseg->fh_array[i];
  585. }
  586. static void
  587. filelayout_mark_devid_negative(struct nfs4_file_layout_dsaddr *dsaddr,
  588. int err, u32 ds_addr)
  589. {
  590. u32 *p = (u32 *)&dsaddr->deviceid;
  591. printk(KERN_ERR "NFS: data server %x connection error %d."
  592. " Deviceid [%x%x%x%x] marked out of use.\n",
  593. ds_addr, err, p[0], p[1], p[2], p[3]);
  594. spin_lock(&filelayout_deviceid_lock);
  595. dsaddr->flags |= NFS4_DEVICE_ID_NEG_ENTRY;
  596. spin_unlock(&filelayout_deviceid_lock);
  597. }
  598. struct nfs4_pnfs_ds *
  599. nfs4_fl_prepare_ds(struct pnfs_layout_segment *lseg, u32 ds_idx)
  600. {
  601. struct nfs4_file_layout_dsaddr *dsaddr = FILELAYOUT_LSEG(lseg)->dsaddr;
  602. struct nfs4_pnfs_ds *ds = dsaddr->ds_list[ds_idx];
  603. if (ds == NULL) {
  604. printk(KERN_ERR "%s: No data server for offset index %d\n",
  605. __func__, ds_idx);
  606. return NULL;
  607. }
  608. if (!ds->ds_clp) {
  609. struct nfs_server *s = NFS_SERVER(lseg->pls_layout->plh_inode);
  610. int err;
  611. if (dsaddr->flags & NFS4_DEVICE_ID_NEG_ENTRY) {
  612. /* Already tried to connect, don't try again */
  613. dprintk("%s Deviceid marked out of use\n", __func__);
  614. return NULL;
  615. }
  616. err = nfs4_ds_connect(s, ds);
  617. if (err) {
  618. filelayout_mark_devid_negative(dsaddr, err,
  619. ntohl(ds->ds_ip_addr));
  620. return NULL;
  621. }
  622. }
  623. return ds;
  624. }