flexfilelayout.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2016 Tom Haynes <loghyr@primarydata.com>
  3. *
  4. * The following implements a super-simple flex-file server
  5. * where the NFSv4.1 mds is also the ds. And the storage is
  6. * the same. I.e., writing to the mds via a NFSv4.1 WRITE
  7. * goes to the same location as the NFSv3 WRITE.
  8. */
  9. #include <linux/slab.h>
  10. #include <linux/nfsd/debug.h>
  11. #include <linux/sunrpc/addr.h>
  12. #include "flexfilelayoutxdr.h"
  13. #include "pnfs.h"
  14. #define NFSDDBG_FACILITY NFSDDBG_PNFS
  15. static __be32
  16. nfsd4_ff_proc_layoutget(struct inode *inode, const struct svc_fh *fhp,
  17. struct nfsd4_layoutget *args)
  18. {
  19. struct nfsd4_layout_seg *seg = &args->lg_seg;
  20. u32 device_generation = 0;
  21. int error;
  22. uid_t u;
  23. struct pnfs_ff_layout *fl;
  24. /*
  25. * The super simple flex file server has 1 mirror, 1 data server,
  26. * and 1 file handle. So instead of 4 allocs, do 1 for now.
  27. * Zero it out for the stateid - don't want junk in there!
  28. */
  29. error = -ENOMEM;
  30. fl = kzalloc(sizeof(*fl), GFP_KERNEL);
  31. if (!fl)
  32. goto out_error;
  33. args->lg_content = fl;
  34. /*
  35. * Avoid layout commit, try to force the I/O to the DS,
  36. * and for fun, cause all IOMODE_RW layout segments to
  37. * effectively be WRITE only.
  38. */
  39. fl->flags = FF_FLAGS_NO_LAYOUTCOMMIT | FF_FLAGS_NO_IO_THRU_MDS |
  40. FF_FLAGS_NO_READ_IO;
  41. /* Do not allow a IOMODE_READ segment to have write pemissions */
  42. if (seg->iomode == IOMODE_READ) {
  43. u = from_kuid(&init_user_ns, inode->i_uid) + 1;
  44. fl->uid = make_kuid(&init_user_ns, u);
  45. } else
  46. fl->uid = inode->i_uid;
  47. fl->gid = inode->i_gid;
  48. error = nfsd4_set_deviceid(&fl->deviceid, fhp, device_generation);
  49. if (error)
  50. goto out_error;
  51. fl->fh.size = fhp->fh_handle.fh_size;
  52. memcpy(fl->fh.data, &fhp->fh_handle.fh_base, fl->fh.size);
  53. /* Give whole file layout segments */
  54. seg->offset = 0;
  55. seg->length = NFS4_MAX_UINT64;
  56. dprintk("GET: 0x%llx:0x%llx %d\n", seg->offset, seg->length,
  57. seg->iomode);
  58. return 0;
  59. out_error:
  60. seg->length = 0;
  61. return nfserrno(error);
  62. }
  63. static __be32
  64. nfsd4_ff_proc_getdeviceinfo(struct super_block *sb, struct svc_rqst *rqstp,
  65. struct nfs4_client *clp, struct nfsd4_getdeviceinfo *gdp)
  66. {
  67. struct pnfs_ff_device_addr *da;
  68. u16 port;
  69. char addr[INET6_ADDRSTRLEN];
  70. da = kzalloc(sizeof(struct pnfs_ff_device_addr), GFP_KERNEL);
  71. if (!da)
  72. return nfserrno(-ENOMEM);
  73. gdp->gd_device = da;
  74. da->version = 3;
  75. da->minor_version = 0;
  76. da->rsize = svc_max_payload(rqstp);
  77. da->wsize = da->rsize;
  78. rpc_ntop((struct sockaddr *)&rqstp->rq_daddr,
  79. addr, INET6_ADDRSTRLEN);
  80. if (rqstp->rq_daddr.ss_family == AF_INET) {
  81. struct sockaddr_in *sin;
  82. sin = (struct sockaddr_in *)&rqstp->rq_daddr;
  83. port = ntohs(sin->sin_port);
  84. snprintf(da->netaddr.netid, FF_NETID_LEN + 1, "tcp");
  85. da->netaddr.netid_len = 3;
  86. } else {
  87. struct sockaddr_in6 *sin6;
  88. sin6 = (struct sockaddr_in6 *)&rqstp->rq_daddr;
  89. port = ntohs(sin6->sin6_port);
  90. snprintf(da->netaddr.netid, FF_NETID_LEN + 1, "tcp6");
  91. da->netaddr.netid_len = 4;
  92. }
  93. da->netaddr.addr_len =
  94. snprintf(da->netaddr.addr, FF_ADDR_LEN + 1,
  95. "%s.%hhu.%hhu", addr, port >> 8, port & 0xff);
  96. da->tightly_coupled = false;
  97. return 0;
  98. }
  99. const struct nfsd4_layout_ops ff_layout_ops = {
  100. .notify_types =
  101. NOTIFY_DEVICEID4_DELETE | NOTIFY_DEVICEID4_CHANGE,
  102. .disable_recalls = true,
  103. .proc_getdeviceinfo = nfsd4_ff_proc_getdeviceinfo,
  104. .encode_getdeviceinfo = nfsd4_ff_encode_getdeviceinfo,
  105. .proc_layoutget = nfsd4_ff_proc_layoutget,
  106. .encode_layoutget = nfsd4_ff_encode_layoutget,
  107. };