svcshare.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. for (share = file->f_shares; share; share = share->s_next) {
  31. if (share->s_host == host && nlm_cmp_owner(share, oh))
  32. goto update;
  33. if ((argp->fsm_access & share->s_mode)
  34. || (argp->fsm_mode & share->s_access ))
  35. return nlm_lck_denied;
  36. }
  37. share = kmalloc(sizeof(*share) + oh->len,
  38. GFP_KERNEL);
  39. if (share == NULL)
  40. return nlm_lck_denied_nolocks;
  41. /* Copy owner handle */
  42. ohdata = (u8 *) (share + 1);
  43. memcpy(ohdata, oh->data, oh->len);
  44. share->s_file = file;
  45. share->s_host = host;
  46. share->s_owner.data = ohdata;
  47. share->s_owner.len = oh->len;
  48. share->s_next = file->f_shares;
  49. file->f_shares = share;
  50. update:
  51. share->s_access = argp->fsm_access;
  52. share->s_mode = argp->fsm_mode;
  53. return nlm_granted;
  54. }
  55. /*
  56. * Delete a share.
  57. */
  58. __be32
  59. nlmsvc_unshare_file(struct nlm_host *host, struct nlm_file *file,
  60. struct nlm_args *argp)
  61. {
  62. struct nlm_share *share, **shpp;
  63. struct xdr_netobj *oh = &argp->lock.oh;
  64. for (shpp = &file->f_shares; (share = *shpp) != NULL;
  65. shpp = &share->s_next) {
  66. if (share->s_host == host && nlm_cmp_owner(share, oh)) {
  67. *shpp = share->s_next;
  68. kfree(share);
  69. return nlm_granted;
  70. }
  71. }
  72. /* X/Open spec says return success even if there was no
  73. * corresponding share. */
  74. return nlm_granted;
  75. }
  76. /*
  77. * Traverse all shares for a given file, and delete
  78. * those owned by the given (type of) host
  79. */
  80. void nlmsvc_traverse_shares(struct nlm_host *host, struct nlm_file *file,
  81. nlm_host_match_fn_t match)
  82. {
  83. struct nlm_share *share, **shpp;
  84. shpp = &file->f_shares;
  85. while ((share = *shpp) != NULL) {
  86. if (match(share->s_host, host)) {
  87. *shpp = share->s_next;
  88. kfree(share);
  89. continue;
  90. }
  91. shpp = &share->s_next;
  92. }
  93. }