flexfilelayout.c 3.4 KB

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