flexfilelayout.c 3.6 KB

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