9fid.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #include "stdinc.h"
  2. #include "9.h"
  3. static struct {
  4. VtLock* lock;
  5. Fid* free;
  6. int nfree;
  7. int inuse;
  8. } fbox;
  9. static void
  10. fidLock(Fid* fid, int flags)
  11. {
  12. if(flags & FidFWlock){
  13. vtLock(fid->lock);
  14. fid->flags = flags;
  15. }
  16. else
  17. vtRLock(fid->lock);
  18. /*
  19. * Callers of file* routines are expected to lock fsys->fs->elk
  20. * before making any calls in order to make sure the epoch doesn't
  21. * change underfoot. With the exception of Tversion and Tattach,
  22. * that implies all 9P functions need to lock on entry and unlock
  23. * on exit. Fortunately, the general case is the 9P functions do
  24. * fidGet on entry and fidPut on exit, so this is a convenient place
  25. * to do the locking.
  26. * No fsys->fs->elk lock is required if the fid is being created
  27. * (Tauth, Tattach and Twalk). FidFCreate is always accompanied by
  28. * FidFWlock so the setting and testing of FidFCreate here and in
  29. * fidUnlock below is always done under fid->lock.
  30. * A side effect is that fidFree is called with the fid locked, and
  31. * must call fidUnlock only after it has disposed of any File
  32. * resources still held.
  33. */
  34. if(!(flags & FidFCreate))
  35. fsysFsRlock(fid->fsys);
  36. }
  37. static void
  38. fidUnlock(Fid* fid)
  39. {
  40. if(!(fid->flags & FidFCreate))
  41. fsysFsRUnlock(fid->fsys);
  42. if(fid->flags & FidFWlock){
  43. fid->flags = 0;
  44. vtUnlock(fid->lock);
  45. return;
  46. }
  47. vtRUnlock(fid->lock);
  48. }
  49. static Fid*
  50. fidAlloc(void)
  51. {
  52. Fid *fid;
  53. vtLock(fbox.lock);
  54. if(fbox.nfree > 0){
  55. fid = fbox.free;
  56. fbox.free = fid->hash;
  57. fbox.nfree--;
  58. }
  59. else{
  60. fid = vtMemAllocZ(sizeof(Fid));
  61. fid->lock = vtLockAlloc();
  62. fid->alock = vtLockAlloc();
  63. }
  64. fbox.inuse++;
  65. vtUnlock(fbox.lock);
  66. fid->con = nil;
  67. fid->fidno = NOFID;
  68. fid->ref = 0;
  69. fid->flags = 0;
  70. fid->open = FidOCreate;
  71. assert(fid->fsys == nil);
  72. assert(fid->file == nil);
  73. fid->qid = (Qid){0, 0, 0};
  74. assert(fid->uid == nil);
  75. assert(fid->uname == nil);
  76. assert(fid->db == nil);
  77. assert(fid->excl == nil);
  78. assert(fid->rpc == nil);
  79. assert(fid->cuname == nil);
  80. fid->hash = fid->next = fid->prev = nil;
  81. return fid;
  82. }
  83. static void
  84. fidFree(Fid* fid)
  85. {
  86. if(fid->file != nil){
  87. fileDecRef(fid->file);
  88. fid->file = nil;
  89. }
  90. if(fid->db != nil){
  91. dirBufFree(fid->db);
  92. fid->db = nil;
  93. }
  94. fidUnlock(fid);
  95. if(fid->uid != nil){
  96. vtMemFree(fid->uid);
  97. fid->uid = nil;
  98. }
  99. if(fid->uname != nil){
  100. vtMemFree(fid->uname);
  101. fid->uname = nil;
  102. }
  103. if(fid->excl != nil)
  104. exclFree(fid);
  105. if(fid->rpc != nil){
  106. close(fid->rpc->afd);
  107. auth_freerpc(fid->rpc);
  108. fid->rpc = nil;
  109. }
  110. if(fid->fsys != nil){
  111. fsysPut(fid->fsys);
  112. fid->fsys = nil;
  113. }
  114. if(fid->cuname != nil){
  115. vtMemFree(fid->cuname);
  116. fid->cuname = nil;
  117. }
  118. vtLock(fbox.lock);
  119. fbox.inuse--;
  120. if(fbox.nfree < 10){
  121. fid->hash = fbox.free;
  122. fbox.free = fid;
  123. fbox.nfree++;
  124. }
  125. else{
  126. vtLockFree(fid->alock);
  127. vtLockFree(fid->lock);
  128. vtMemFree(fid);
  129. }
  130. vtUnlock(fbox.lock);
  131. }
  132. static void
  133. fidUnHash(Fid* fid)
  134. {
  135. Fid *fp, **hash;
  136. assert(fid->ref == 0);
  137. hash = &fid->con->fidhash[fid->fidno % NFidHash];
  138. for(fp = *hash; fp != nil; fp = fp->hash){
  139. if(fp == fid){
  140. *hash = fp->hash;
  141. break;
  142. }
  143. hash = &fp->hash;
  144. }
  145. assert(fp == fid);
  146. if(fid->prev != nil)
  147. fid->prev->next = fid->next;
  148. else
  149. fid->con->fhead = fid->next;
  150. if(fid->next != nil)
  151. fid->next->prev = fid->prev;
  152. else
  153. fid->con->ftail = fid->prev;
  154. fid->prev = fid->next = nil;
  155. fid->con->nfid--;
  156. }
  157. Fid*
  158. fidGet(Con* con, u32int fidno, int flags)
  159. {
  160. Fid *fid, **hash;
  161. if(fidno == NOFID)
  162. return nil;
  163. hash = &con->fidhash[fidno % NFidHash];
  164. vtLock(con->fidlock);
  165. for(fid = *hash; fid != nil; fid = fid->hash){
  166. if(fid->fidno != fidno)
  167. continue;
  168. /*
  169. * Already in use is an error
  170. * when called from attach, clone or walk.
  171. */
  172. if(flags & FidFCreate){
  173. vtUnlock(con->fidlock);
  174. vtSetError("fid in use");
  175. return nil;
  176. }
  177. fid->ref++;
  178. vtUnlock(con->fidlock);
  179. fidLock(fid, flags);
  180. if((fid->open & FidOCreate) || fid->fidno == NOFID){
  181. fidPut(fid);
  182. vtSetError("fid invalid");
  183. return nil;
  184. }
  185. return fid;
  186. }
  187. if((flags & FidFCreate) && (fid = fidAlloc()) != nil){
  188. assert(flags & FidFWlock);
  189. fid->con = con;
  190. fid->fidno = fidno;
  191. fid->ref = 1;
  192. fid->hash = *hash;
  193. *hash = fid;
  194. if(con->ftail != nil){
  195. fid->prev = con->ftail;
  196. con->ftail->next = fid;
  197. }
  198. else{
  199. con->fhead = fid;
  200. fid->prev = nil;
  201. }
  202. con->ftail = fid;
  203. fid->next = nil;
  204. con->nfid++;
  205. vtUnlock(con->fidlock);
  206. /*
  207. * The FidOCreate flag is used to prevent any
  208. * accidental access to the Fid between unlocking the
  209. * hash and acquiring the Fid lock for return.
  210. */
  211. fidLock(fid, flags);
  212. fid->open &= ~FidOCreate;
  213. return fid;
  214. }
  215. vtUnlock(con->fidlock);
  216. vtSetError("fid not found");
  217. return nil;
  218. }
  219. void
  220. fidPut(Fid* fid)
  221. {
  222. vtLock(fid->con->fidlock);
  223. assert(fid->ref > 0);
  224. fid->ref--;
  225. vtUnlock(fid->con->fidlock);
  226. if(fid->ref == 0 && fid->fidno == NOFID){
  227. fidFree(fid);
  228. return;
  229. }
  230. fidUnlock(fid);
  231. }
  232. void
  233. fidClunk(Fid* fid)
  234. {
  235. assert(fid->flags & FidFWlock);
  236. vtLock(fid->con->fidlock);
  237. assert(fid->ref > 0);
  238. fid->ref--;
  239. fidUnHash(fid);
  240. fid->fidno = NOFID;
  241. vtUnlock(fid->con->fidlock);
  242. if(fid->ref > 0){
  243. fidUnlock(fid);
  244. return;
  245. }
  246. fidFree(fid);
  247. }
  248. void
  249. fidClunkAll(Con* con)
  250. {
  251. Fid *fid;
  252. u32int fidno;
  253. vtLock(con->fidlock);
  254. while(con->fhead != nil){
  255. fidno = con->fhead->fidno;
  256. vtUnlock(con->fidlock);
  257. if((fid = fidGet(con, fidno, FidFWlock)) != nil)
  258. fidClunk(fid);
  259. vtLock(con->fidlock);
  260. }
  261. vtUnlock(con->fidlock);
  262. }
  263. void
  264. fidInit(void)
  265. {
  266. fbox.lock = vtLockAlloc();
  267. }