svcshare.c 2.4 KB

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