flexfilelayoutdev.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /*
  2. * Device operations for the pnfs nfs4 file layout driver.
  3. *
  4. * Copyright (c) 2014, Primary Data, Inc. All rights reserved.
  5. *
  6. * Tao Peng <bergwolf@primarydata.com>
  7. */
  8. #include <linux/nfs_fs.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/module.h>
  11. #include <linux/sunrpc/addr.h>
  12. #include "../internal.h"
  13. #include "../nfs4session.h"
  14. #include "flexfilelayout.h"
  15. #define NFSDBG_FACILITY NFSDBG_PNFS_LD
  16. static unsigned int dataserver_timeo = NFS4_DEF_DS_TIMEO;
  17. static unsigned int dataserver_retrans = NFS4_DEF_DS_RETRANS;
  18. void nfs4_ff_layout_put_deviceid(struct nfs4_ff_layout_ds *mirror_ds)
  19. {
  20. if (mirror_ds)
  21. nfs4_put_deviceid_node(&mirror_ds->id_node);
  22. }
  23. void nfs4_ff_layout_free_deviceid(struct nfs4_ff_layout_ds *mirror_ds)
  24. {
  25. nfs4_print_deviceid(&mirror_ds->id_node.deviceid);
  26. nfs4_pnfs_ds_put(mirror_ds->ds);
  27. kfree_rcu(mirror_ds, id_node.rcu);
  28. }
  29. /* Decode opaque device data and construct new_ds using it */
  30. struct nfs4_ff_layout_ds *
  31. nfs4_ff_alloc_deviceid_node(struct nfs_server *server, struct pnfs_device *pdev,
  32. gfp_t gfp_flags)
  33. {
  34. struct xdr_stream stream;
  35. struct xdr_buf buf;
  36. struct page *scratch;
  37. struct list_head dsaddrs;
  38. struct nfs4_pnfs_ds_addr *da;
  39. struct nfs4_ff_layout_ds *new_ds = NULL;
  40. struct nfs4_ff_ds_version *ds_versions = NULL;
  41. u32 mp_count;
  42. u32 version_count;
  43. __be32 *p;
  44. int i, ret = -ENOMEM;
  45. /* set up xdr stream */
  46. scratch = alloc_page(gfp_flags);
  47. if (!scratch)
  48. goto out_err;
  49. new_ds = kzalloc(sizeof(struct nfs4_ff_layout_ds), gfp_flags);
  50. if (!new_ds)
  51. goto out_scratch;
  52. nfs4_init_deviceid_node(&new_ds->id_node,
  53. server,
  54. &pdev->dev_id);
  55. INIT_LIST_HEAD(&dsaddrs);
  56. xdr_init_decode_pages(&stream, &buf, pdev->pages, pdev->pglen);
  57. xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
  58. /* multipath count */
  59. p = xdr_inline_decode(&stream, 4);
  60. if (unlikely(!p))
  61. goto out_err_drain_dsaddrs;
  62. mp_count = be32_to_cpup(p);
  63. dprintk("%s: multipath ds count %d\n", __func__, mp_count);
  64. for (i = 0; i < mp_count; i++) {
  65. /* multipath ds */
  66. da = nfs4_decode_mp_ds_addr(server->nfs_client->cl_net,
  67. &stream, gfp_flags);
  68. if (da)
  69. list_add_tail(&da->da_node, &dsaddrs);
  70. }
  71. if (list_empty(&dsaddrs)) {
  72. dprintk("%s: no suitable DS addresses found\n",
  73. __func__);
  74. ret = -ENOMEDIUM;
  75. goto out_err_drain_dsaddrs;
  76. }
  77. /* version count */
  78. p = xdr_inline_decode(&stream, 4);
  79. if (unlikely(!p))
  80. goto out_err_drain_dsaddrs;
  81. version_count = be32_to_cpup(p);
  82. dprintk("%s: version count %d\n", __func__, version_count);
  83. ds_versions = kzalloc(version_count * sizeof(struct nfs4_ff_ds_version),
  84. gfp_flags);
  85. if (!ds_versions)
  86. goto out_scratch;
  87. for (i = 0; i < version_count; i++) {
  88. /* 20 = version(4) + minor_version(4) + rsize(4) + wsize(4) +
  89. * tightly_coupled(4) */
  90. p = xdr_inline_decode(&stream, 20);
  91. if (unlikely(!p))
  92. goto out_err_drain_dsaddrs;
  93. ds_versions[i].version = be32_to_cpup(p++);
  94. ds_versions[i].minor_version = be32_to_cpup(p++);
  95. ds_versions[i].rsize = nfs_block_size(be32_to_cpup(p++), NULL);
  96. ds_versions[i].wsize = nfs_block_size(be32_to_cpup(p++), NULL);
  97. ds_versions[i].tightly_coupled = be32_to_cpup(p);
  98. if (ds_versions[i].rsize > NFS_MAX_FILE_IO_SIZE)
  99. ds_versions[i].rsize = NFS_MAX_FILE_IO_SIZE;
  100. if (ds_versions[i].wsize > NFS_MAX_FILE_IO_SIZE)
  101. ds_versions[i].wsize = NFS_MAX_FILE_IO_SIZE;
  102. if (ds_versions[i].version != 3 || ds_versions[i].minor_version != 0) {
  103. dprintk("%s: [%d] unsupported ds version %d-%d\n", __func__,
  104. i, ds_versions[i].version,
  105. ds_versions[i].minor_version);
  106. ret = -EPROTONOSUPPORT;
  107. goto out_err_drain_dsaddrs;
  108. }
  109. dprintk("%s: [%d] vers %u minor_ver %u rsize %u wsize %u coupled %d\n",
  110. __func__, i, ds_versions[i].version,
  111. ds_versions[i].minor_version,
  112. ds_versions[i].rsize,
  113. ds_versions[i].wsize,
  114. ds_versions[i].tightly_coupled);
  115. }
  116. new_ds->ds_versions = ds_versions;
  117. new_ds->ds_versions_cnt = version_count;
  118. new_ds->ds = nfs4_pnfs_ds_add(&dsaddrs, gfp_flags);
  119. if (!new_ds->ds)
  120. goto out_err_drain_dsaddrs;
  121. /* If DS was already in cache, free ds addrs */
  122. while (!list_empty(&dsaddrs)) {
  123. da = list_first_entry(&dsaddrs,
  124. struct nfs4_pnfs_ds_addr,
  125. da_node);
  126. list_del_init(&da->da_node);
  127. kfree(da->da_remotestr);
  128. kfree(da);
  129. }
  130. __free_page(scratch);
  131. return new_ds;
  132. out_err_drain_dsaddrs:
  133. while (!list_empty(&dsaddrs)) {
  134. da = list_first_entry(&dsaddrs, struct nfs4_pnfs_ds_addr,
  135. da_node);
  136. list_del_init(&da->da_node);
  137. kfree(da->da_remotestr);
  138. kfree(da);
  139. }
  140. kfree(ds_versions);
  141. out_scratch:
  142. __free_page(scratch);
  143. out_err:
  144. kfree(new_ds);
  145. dprintk("%s ERROR: returning %d\n", __func__, ret);
  146. return NULL;
  147. }
  148. static void ff_layout_mark_devid_invalid(struct pnfs_layout_segment *lseg,
  149. struct nfs4_deviceid_node *devid)
  150. {
  151. nfs4_mark_deviceid_unavailable(devid);
  152. if (!ff_layout_has_available_ds(lseg))
  153. pnfs_error_mark_layout_for_return(lseg->pls_layout->plh_inode,
  154. lseg);
  155. }
  156. static bool ff_layout_mirror_valid(struct pnfs_layout_segment *lseg,
  157. struct nfs4_ff_layout_mirror *mirror)
  158. {
  159. if (mirror == NULL || mirror->mirror_ds == NULL) {
  160. pnfs_error_mark_layout_for_return(lseg->pls_layout->plh_inode,
  161. lseg);
  162. return false;
  163. }
  164. if (mirror->mirror_ds->ds == NULL) {
  165. struct nfs4_deviceid_node *devid;
  166. devid = &mirror->mirror_ds->id_node;
  167. ff_layout_mark_devid_invalid(lseg, devid);
  168. return false;
  169. }
  170. return true;
  171. }
  172. static u64
  173. end_offset(u64 start, u64 len)
  174. {
  175. u64 end;
  176. end = start + len;
  177. return end >= start ? end : NFS4_MAX_UINT64;
  178. }
  179. static void extend_ds_error(struct nfs4_ff_layout_ds_err *err,
  180. u64 offset, u64 length)
  181. {
  182. u64 end;
  183. end = max_t(u64, end_offset(err->offset, err->length),
  184. end_offset(offset, length));
  185. err->offset = min_t(u64, err->offset, offset);
  186. err->length = end - err->offset;
  187. }
  188. static int
  189. ff_ds_error_match(const struct nfs4_ff_layout_ds_err *e1,
  190. const struct nfs4_ff_layout_ds_err *e2)
  191. {
  192. int ret;
  193. if (e1->opnum != e2->opnum)
  194. return e1->opnum < e2->opnum ? -1 : 1;
  195. if (e1->status != e2->status)
  196. return e1->status < e2->status ? -1 : 1;
  197. ret = memcmp(&e1->stateid, &e2->stateid, sizeof(e1->stateid));
  198. if (ret != 0)
  199. return ret;
  200. ret = memcmp(&e1->deviceid, &e2->deviceid, sizeof(e1->deviceid));
  201. if (ret != 0)
  202. return ret;
  203. if (end_offset(e1->offset, e1->length) < e2->offset)
  204. return -1;
  205. if (e1->offset > end_offset(e2->offset, e2->length))
  206. return 1;
  207. /* If ranges overlap or are contiguous, they are the same */
  208. return 0;
  209. }
  210. static void
  211. ff_layout_add_ds_error_locked(struct nfs4_flexfile_layout *flo,
  212. struct nfs4_ff_layout_ds_err *dserr)
  213. {
  214. struct nfs4_ff_layout_ds_err *err, *tmp;
  215. struct list_head *head = &flo->error_list;
  216. int match;
  217. /* Do insertion sort w/ merges */
  218. list_for_each_entry_safe(err, tmp, &flo->error_list, list) {
  219. match = ff_ds_error_match(err, dserr);
  220. if (match < 0)
  221. continue;
  222. if (match > 0) {
  223. /* Add entry "dserr" _before_ entry "err" */
  224. head = &err->list;
  225. break;
  226. }
  227. /* Entries match, so merge "err" into "dserr" */
  228. extend_ds_error(dserr, err->offset, err->length);
  229. list_del(&err->list);
  230. kfree(err);
  231. }
  232. list_add_tail(&dserr->list, head);
  233. }
  234. int ff_layout_track_ds_error(struct nfs4_flexfile_layout *flo,
  235. struct nfs4_ff_layout_mirror *mirror, u64 offset,
  236. u64 length, int status, enum nfs_opnum4 opnum,
  237. gfp_t gfp_flags)
  238. {
  239. struct nfs4_ff_layout_ds_err *dserr;
  240. if (status == 0)
  241. return 0;
  242. if (mirror->mirror_ds == NULL)
  243. return -EINVAL;
  244. dserr = kmalloc(sizeof(*dserr), gfp_flags);
  245. if (!dserr)
  246. return -ENOMEM;
  247. INIT_LIST_HEAD(&dserr->list);
  248. dserr->offset = offset;
  249. dserr->length = length;
  250. dserr->status = status;
  251. dserr->opnum = opnum;
  252. nfs4_stateid_copy(&dserr->stateid, &mirror->stateid);
  253. memcpy(&dserr->deviceid, &mirror->mirror_ds->id_node.deviceid,
  254. NFS4_DEVICEID4_SIZE);
  255. spin_lock(&flo->generic_hdr.plh_inode->i_lock);
  256. ff_layout_add_ds_error_locked(flo, dserr);
  257. spin_unlock(&flo->generic_hdr.plh_inode->i_lock);
  258. return 0;
  259. }
  260. /* currently we only support AUTH_NONE and AUTH_SYS */
  261. static rpc_authflavor_t
  262. nfs4_ff_layout_choose_authflavor(struct nfs4_ff_layout_mirror *mirror)
  263. {
  264. if (mirror->uid == (u32)-1)
  265. return RPC_AUTH_NULL;
  266. return RPC_AUTH_UNIX;
  267. }
  268. /* fetch cred for NFSv3 DS */
  269. static int ff_layout_update_mirror_cred(struct nfs4_ff_layout_mirror *mirror,
  270. struct nfs4_pnfs_ds *ds)
  271. {
  272. if (ds->ds_clp && !mirror->cred &&
  273. mirror->mirror_ds->ds_versions[0].version == 3) {
  274. struct rpc_auth *auth = ds->ds_clp->cl_rpcclient->cl_auth;
  275. struct rpc_cred *cred;
  276. struct auth_cred acred = {
  277. .uid = make_kuid(&init_user_ns, mirror->uid),
  278. .gid = make_kgid(&init_user_ns, mirror->gid),
  279. };
  280. /* AUTH_NULL ignores acred */
  281. cred = auth->au_ops->lookup_cred(auth, &acred, 0);
  282. if (IS_ERR(cred)) {
  283. dprintk("%s: lookup_cred failed with %ld\n",
  284. __func__, PTR_ERR(cred));
  285. return PTR_ERR(cred);
  286. } else {
  287. if (cmpxchg(&mirror->cred, NULL, cred))
  288. put_rpccred(cred);
  289. }
  290. }
  291. return 0;
  292. }
  293. struct nfs_fh *
  294. nfs4_ff_layout_select_ds_fh(struct pnfs_layout_segment *lseg, u32 mirror_idx)
  295. {
  296. struct nfs4_ff_layout_mirror *mirror = FF_LAYOUT_COMP(lseg, mirror_idx);
  297. struct nfs_fh *fh = NULL;
  298. if (!ff_layout_mirror_valid(lseg, mirror)) {
  299. pr_err_ratelimited("NFS: %s: No data server for mirror offset index %d\n",
  300. __func__, mirror_idx);
  301. goto out;
  302. }
  303. /* FIXME: For now assume there is only 1 version available for the DS */
  304. fh = &mirror->fh_versions[0];
  305. out:
  306. return fh;
  307. }
  308. /* Upon return, either ds is connected, or ds is NULL */
  309. struct nfs4_pnfs_ds *
  310. nfs4_ff_layout_prepare_ds(struct pnfs_layout_segment *lseg, u32 ds_idx,
  311. bool fail_return)
  312. {
  313. struct nfs4_ff_layout_mirror *mirror = FF_LAYOUT_COMP(lseg, ds_idx);
  314. struct nfs4_pnfs_ds *ds = NULL;
  315. struct nfs4_deviceid_node *devid;
  316. struct inode *ino = lseg->pls_layout->plh_inode;
  317. struct nfs_server *s = NFS_SERVER(ino);
  318. unsigned int max_payload;
  319. rpc_authflavor_t flavor;
  320. if (!ff_layout_mirror_valid(lseg, mirror)) {
  321. pr_err_ratelimited("NFS: %s: No data server for offset index %d\n",
  322. __func__, ds_idx);
  323. goto out;
  324. }
  325. devid = &mirror->mirror_ds->id_node;
  326. if (ff_layout_test_devid_unavailable(devid))
  327. goto out;
  328. ds = mirror->mirror_ds->ds;
  329. /* matching smp_wmb() in _nfs4_pnfs_v3/4_ds_connect */
  330. smp_rmb();
  331. if (ds->ds_clp)
  332. goto out_update_creds;
  333. flavor = nfs4_ff_layout_choose_authflavor(mirror);
  334. /* FIXME: For now we assume the server sent only one version of NFS
  335. * to use for the DS.
  336. */
  337. nfs4_pnfs_ds_connect(s, ds, devid, dataserver_timeo,
  338. dataserver_retrans,
  339. mirror->mirror_ds->ds_versions[0].version,
  340. mirror->mirror_ds->ds_versions[0].minor_version,
  341. flavor);
  342. /* connect success, check rsize/wsize limit */
  343. if (ds->ds_clp) {
  344. max_payload =
  345. nfs_block_size(rpc_max_payload(ds->ds_clp->cl_rpcclient),
  346. NULL);
  347. if (mirror->mirror_ds->ds_versions[0].rsize > max_payload)
  348. mirror->mirror_ds->ds_versions[0].rsize = max_payload;
  349. if (mirror->mirror_ds->ds_versions[0].wsize > max_payload)
  350. mirror->mirror_ds->ds_versions[0].wsize = max_payload;
  351. } else {
  352. ff_layout_track_ds_error(FF_LAYOUT_FROM_HDR(lseg->pls_layout),
  353. mirror, lseg->pls_range.offset,
  354. lseg->pls_range.length, NFS4ERR_NXIO,
  355. OP_ILLEGAL, GFP_NOIO);
  356. if (!fail_return) {
  357. if (ff_layout_has_available_ds(lseg))
  358. set_bit(NFS_LAYOUT_RETURN_REQUESTED,
  359. &lseg->pls_layout->plh_flags);
  360. else
  361. pnfs_error_mark_layout_for_return(ino, lseg);
  362. } else
  363. pnfs_error_mark_layout_for_return(ino, lseg);
  364. }
  365. out_update_creds:
  366. if (ff_layout_update_mirror_cred(mirror, ds))
  367. ds = NULL;
  368. out:
  369. return ds;
  370. }
  371. struct rpc_cred *
  372. ff_layout_get_ds_cred(struct pnfs_layout_segment *lseg, u32 ds_idx,
  373. struct rpc_cred *mdscred)
  374. {
  375. struct nfs4_ff_layout_mirror *mirror = FF_LAYOUT_COMP(lseg, ds_idx);
  376. struct rpc_cred *cred = ERR_PTR(-EINVAL);
  377. if (!nfs4_ff_layout_prepare_ds(lseg, ds_idx, true))
  378. goto out;
  379. if (mirror && mirror->cred)
  380. cred = mirror->cred;
  381. else
  382. cred = mdscred;
  383. out:
  384. return cred;
  385. }
  386. /**
  387. * Find or create a DS rpc client with th MDS server rpc client auth flavor
  388. * in the nfs_client cl_ds_clients list.
  389. */
  390. struct rpc_clnt *
  391. nfs4_ff_find_or_create_ds_client(struct pnfs_layout_segment *lseg, u32 ds_idx,
  392. struct nfs_client *ds_clp, struct inode *inode)
  393. {
  394. struct nfs4_ff_layout_mirror *mirror = FF_LAYOUT_COMP(lseg, ds_idx);
  395. switch (mirror->mirror_ds->ds_versions[0].version) {
  396. case 3:
  397. /* For NFSv3 DS, flavor is set when creating DS connections */
  398. return ds_clp->cl_rpcclient;
  399. case 4:
  400. return nfs4_find_or_create_ds_client(ds_clp, inode);
  401. default:
  402. BUG();
  403. }
  404. }
  405. static bool is_range_intersecting(u64 offset1, u64 length1,
  406. u64 offset2, u64 length2)
  407. {
  408. u64 end1 = end_offset(offset1, length1);
  409. u64 end2 = end_offset(offset2, length2);
  410. return (end1 == NFS4_MAX_UINT64 || end1 > offset2) &&
  411. (end2 == NFS4_MAX_UINT64 || end2 > offset1);
  412. }
  413. /* called with inode i_lock held */
  414. int ff_layout_encode_ds_ioerr(struct nfs4_flexfile_layout *flo,
  415. struct xdr_stream *xdr, int *count,
  416. const struct pnfs_layout_range *range)
  417. {
  418. struct nfs4_ff_layout_ds_err *err, *n;
  419. __be32 *p;
  420. list_for_each_entry_safe(err, n, &flo->error_list, list) {
  421. if (!is_range_intersecting(err->offset, err->length,
  422. range->offset, range->length))
  423. continue;
  424. /* offset(8) + length(8) + stateid(NFS4_STATEID_SIZE)
  425. * + array length + deviceid(NFS4_DEVICEID4_SIZE)
  426. * + status(4) + opnum(4)
  427. */
  428. p = xdr_reserve_space(xdr,
  429. 28 + NFS4_STATEID_SIZE + NFS4_DEVICEID4_SIZE);
  430. if (unlikely(!p))
  431. return -ENOBUFS;
  432. p = xdr_encode_hyper(p, err->offset);
  433. p = xdr_encode_hyper(p, err->length);
  434. p = xdr_encode_opaque_fixed(p, &err->stateid,
  435. NFS4_STATEID_SIZE);
  436. /* Encode 1 error */
  437. *p++ = cpu_to_be32(1);
  438. p = xdr_encode_opaque_fixed(p, &err->deviceid,
  439. NFS4_DEVICEID4_SIZE);
  440. *p++ = cpu_to_be32(err->status);
  441. *p++ = cpu_to_be32(err->opnum);
  442. *count += 1;
  443. list_del(&err->list);
  444. dprintk("%s: offset %llu length %llu status %d op %d count %d\n",
  445. __func__, err->offset, err->length, err->status,
  446. err->opnum, *count);
  447. kfree(err);
  448. }
  449. return 0;
  450. }
  451. static bool ff_read_layout_has_available_ds(struct pnfs_layout_segment *lseg)
  452. {
  453. struct nfs4_ff_layout_mirror *mirror;
  454. struct nfs4_deviceid_node *devid;
  455. u32 idx;
  456. for (idx = 0; idx < FF_LAYOUT_MIRROR_COUNT(lseg); idx++) {
  457. mirror = FF_LAYOUT_COMP(lseg, idx);
  458. if (mirror && mirror->mirror_ds) {
  459. devid = &mirror->mirror_ds->id_node;
  460. if (!ff_layout_test_devid_unavailable(devid))
  461. return true;
  462. }
  463. }
  464. return false;
  465. }
  466. static bool ff_rw_layout_has_available_ds(struct pnfs_layout_segment *lseg)
  467. {
  468. struct nfs4_ff_layout_mirror *mirror;
  469. struct nfs4_deviceid_node *devid;
  470. u32 idx;
  471. for (idx = 0; idx < FF_LAYOUT_MIRROR_COUNT(lseg); idx++) {
  472. mirror = FF_LAYOUT_COMP(lseg, idx);
  473. if (!mirror || !mirror->mirror_ds)
  474. return false;
  475. devid = &mirror->mirror_ds->id_node;
  476. if (ff_layout_test_devid_unavailable(devid))
  477. return false;
  478. }
  479. return FF_LAYOUT_MIRROR_COUNT(lseg) != 0;
  480. }
  481. bool ff_layout_has_available_ds(struct pnfs_layout_segment *lseg)
  482. {
  483. if (lseg->pls_range.iomode == IOMODE_READ)
  484. return ff_read_layout_has_available_ds(lseg);
  485. /* Note: RW layout needs all mirrors available */
  486. return ff_rw_layout_has_available_ds(lseg);
  487. }
  488. module_param(dataserver_retrans, uint, 0644);
  489. MODULE_PARM_DESC(dataserver_retrans, "The number of times the NFSv4.1 client "
  490. "retries a request before it attempts further "
  491. " recovery action.");
  492. module_param(dataserver_timeo, uint, 0644);
  493. MODULE_PARM_DESC(dataserver_timeo, "The time (in tenths of a second) the "
  494. "NFSv4.1 client waits for a response from a "
  495. " data server before it retries an NFS request.");