svcshare.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/lockd/svcshare.c
  4. *
  5. * Management of DOS shares.
  6. *
  7. * Copyright (C) 1996 Olaf Kirch <okir@monad.swb.de>
  8. */
  9. #include <linux/time.h>
  10. #include <linux/unistd.h>
  11. #include <linux/string.h>
  12. #include <linux/slab.h>
  13. #include <linux/sunrpc/clnt.h>
  14. #include <linux/sunrpc/svc.h>
  15. #include <linux/lockd/lockd.h>
  16. #include <linux/lockd/share.h>
  17. static inline int
  18. nlm_cmp_owner(struct nlm_share *share, struct xdr_netobj *oh)
  19. {
  20. return share->s_owner.len == oh->len
  21. && !memcmp(share->s_owner.data, oh->data, oh->len);
  22. }
  23. __be32
  24. nlmsvc_share_file(struct nlm_host *host, struct nlm_file *file,
  25. struct nlm_args *argp)
  26. {
  27. struct nlm_share *share;
  28. struct xdr_netobj *oh = &argp->lock.oh;
  29. u8 *ohdata;
  30. if (nlmsvc_file_cannot_lock(file))
  31. return nlm_lck_denied_nolocks;
  32. for (share = file->f_shares; share; share = share->s_next) {
  33. if (share->s_host == host && nlm_cmp_owner(share, oh))
  34. goto update;
  35. if ((argp->fsm_access & share->s_mode)
  36. || (argp->fsm_mode & share->s_access ))
  37. return nlm_lck_denied;
  38. }
  39. share = kmalloc(sizeof(*share) + oh->len,
  40. GFP_KERNEL);
  41. if (share == NULL)
  42. return nlm_lck_denied_nolocks;
  43. /* Copy owner handle */
  44. ohdata = (u8 *) (share + 1);
  45. memcpy(ohdata, oh->data, oh->len);
  46. share->s_file = file;
  47. share->s_host = host;
  48. share->s_owner.data = ohdata;
  49. share->s_owner.len = oh->len;
  50. share->s_next = file->f_shares;
  51. file->f_shares = share;
  52. update:
  53. share->s_access = argp->fsm_access;
  54. share->s_mode = argp->fsm_mode;
  55. return nlm_granted;
  56. }
  57. /*
  58. * Delete a share.
  59. */
  60. __be32
  61. nlmsvc_unshare_file(struct nlm_host *host, struct nlm_file *file,
  62. struct nlm_args *argp)
  63. {
  64. struct nlm_share *share, **shpp;
  65. struct xdr_netobj *oh = &argp->lock.oh;
  66. if (nlmsvc_file_cannot_lock(file))
  67. return nlm_lck_denied_nolocks;
  68. for (shpp = &file->f_shares; (share = *shpp) != NULL;
  69. shpp = &share->s_next) {
  70. if (share->s_host == host && nlm_cmp_owner(share, oh)) {
  71. *shpp = share->s_next;
  72. kfree(share);
  73. return nlm_granted;
  74. }
  75. }
  76. /* X/Open spec says return success even if there was no
  77. * corresponding share. */
  78. return nlm_granted;
  79. }
  80. /*
  81. * Traverse all shares for a given file, and delete
  82. * those owned by the given (type of) host
  83. */
  84. void nlmsvc_traverse_shares(struct nlm_host *host, struct nlm_file *file,
  85. nlm_host_match_fn_t match)
  86. {
  87. struct nlm_share *share, **shpp;
  88. shpp = &file->f_shares;
  89. while ((share = *shpp) != NULL) {
  90. if (match(share->s_host, host)) {
  91. *shpp = share->s_next;
  92. kfree(share);
  93. continue;
  94. }
  95. shpp = &share->s_next;
  96. }
  97. }