fid.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * V9FS FID Management
  3. *
  4. * Copyright (C) 2005, 2006 by Eric Van Hensbergen <ericvh@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to:
  17. * Free Software Foundation
  18. * 51 Franklin Street, Fifth Floor
  19. * Boston, MA 02111-1301 USA
  20. *
  21. */
  22. #include <linux/module.h>
  23. #include <linux/errno.h>
  24. #include <linux/fs.h>
  25. #include <linux/sched.h>
  26. #include <linux/idr.h>
  27. #include <asm/semaphore.h>
  28. #include <net/9p/9p.h>
  29. #include <net/9p/client.h>
  30. #include "v9fs.h"
  31. #include "v9fs_vfs.h"
  32. #include "fid.h"
  33. /**
  34. * v9fs_fid_insert - add a fid to a dentry
  35. * @fid: fid to add
  36. * @dentry: dentry that it is being added to
  37. *
  38. */
  39. int v9fs_fid_add(struct dentry *dentry, struct p9_fid *fid)
  40. {
  41. struct v9fs_dentry *dent;
  42. P9_DPRINTK(P9_DEBUG_VFS, "fid %d dentry %s\n",
  43. fid->fid, dentry->d_iname);
  44. dent = dentry->d_fsdata;
  45. if (!dent) {
  46. dent = kmalloc(sizeof(struct v9fs_dentry), GFP_KERNEL);
  47. if (!dent)
  48. return -ENOMEM;
  49. spin_lock_init(&dent->lock);
  50. INIT_LIST_HEAD(&dent->fidlist);
  51. dentry->d_fsdata = dent;
  52. }
  53. spin_lock(&dent->lock);
  54. list_add(&fid->dlist, &dent->fidlist);
  55. spin_unlock(&dent->lock);
  56. return 0;
  57. }
  58. /**
  59. * v9fs_fid_lookup - return a locked fid from a dentry
  60. * @dentry: dentry to look for fid in
  61. *
  62. * find a fid in the dentry, obtain its semaphore and return a reference to it.
  63. * code calling lookup is responsible for releasing lock
  64. *
  65. * TODO: only match fids that have the same uid as current user
  66. *
  67. */
  68. struct p9_fid *v9fs_fid_lookup(struct dentry *dentry)
  69. {
  70. struct v9fs_dentry *dent;
  71. struct p9_fid *fid;
  72. P9_DPRINTK(P9_DEBUG_VFS, " dentry: %s (%p)\n", dentry->d_iname, dentry);
  73. dent = dentry->d_fsdata;
  74. if (dent)
  75. fid = list_entry(dent->fidlist.next, struct p9_fid, dlist);
  76. else
  77. fid = ERR_PTR(-EBADF);
  78. P9_DPRINTK(P9_DEBUG_VFS, " fid: %p\n", fid);
  79. return fid;
  80. }
  81. /**
  82. * v9fs_fid_clone - lookup the fid for a dentry, clone a private copy and
  83. * release it
  84. * @dentry: dentry to look for fid in
  85. *
  86. * find a fid in the dentry and then clone to a new private fid
  87. *
  88. * TODO: only match fids that have the same uid as current user
  89. *
  90. */
  91. struct p9_fid *v9fs_fid_clone(struct dentry *dentry)
  92. {
  93. struct p9_fid *ofid, *fid;
  94. P9_DPRINTK(P9_DEBUG_VFS, " dentry: %s (%p)\n", dentry->d_iname, dentry);
  95. ofid = v9fs_fid_lookup(dentry);
  96. if (IS_ERR(ofid))
  97. return ofid;
  98. fid = p9_client_walk(ofid, 0, NULL, 1);
  99. return fid;
  100. }