xfile.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include "iotrack.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #define FIDMOD 127 /* prime */
  15. static Xfs *xhead;
  16. static Xfile *xfiles[FIDMOD], *freelist;
  17. static MLock xlock, xlocks[FIDMOD], freelock;
  18. static int
  19. okmode(int omode, int fmode)
  20. {
  21. if(omode == OREAD)
  22. return fmode & 4;
  23. /* else ORDWR */
  24. return (fmode & 6) == 6;
  25. }
  26. Xfs *
  27. getxfs(char *user, char *name)
  28. {
  29. Xfs *xf, *fxf;
  30. Dir *dir;
  31. Qid dqid;
  32. char *p, *q;
  33. int32_t offset;
  34. int fd, omode;
  35. USED(user);
  36. if(name==nil || name[0]==0)
  37. name = deffile;
  38. if(name == nil){
  39. errno = Enofilsys;
  40. return 0;
  41. }
  42. /*
  43. * If the name passed is of the form 'name:offset' then
  44. * offset is used to prime xf->offset. This allows accessing
  45. * a FAT-based filesystem anywhere within a partition.
  46. * Typical use would be to mount a filesystem in the presence
  47. * of a boot manager programme at the beginning of the disc.
  48. */
  49. offset = 0;
  50. if(p = strrchr(name, ':')){
  51. *p++ = 0;
  52. offset = strtol(p, &q, 0);
  53. chat("name %s, offset %ld\n", p, offset);
  54. if(offset < 0 || p == q){
  55. errno = Enofilsys;
  56. return 0;
  57. }
  58. offset *= Sectorsize;
  59. }
  60. if(readonly)
  61. omode = OREAD;
  62. else
  63. omode = ORDWR;
  64. fd = open(name, omode);
  65. if(fd < 0 && omode==ORDWR){
  66. omode = OREAD;
  67. fd = open(name, omode);
  68. }
  69. if(fd < 0){
  70. chat("can't open %s: %r\n", name);
  71. errno = Eerrstr;
  72. return 0;
  73. }
  74. dir = dirfstat(fd);
  75. if(dir == nil){
  76. errno = Eio;
  77. close(fd);
  78. return 0;
  79. }
  80. dqid = dir->qid;
  81. free(dir);
  82. mlock(&xlock);
  83. for(fxf=0,xf=xhead; xf; xf=xf->next){
  84. if(xf->ref == 0){
  85. if(fxf == 0)
  86. fxf = xf;
  87. continue;
  88. }
  89. if(!eqqid(xf->qid, dqid))
  90. continue;
  91. if(strcmp(xf->name, name) != 0 || xf->dev < 0)
  92. continue;
  93. if(devcheck(xf) < 0) /* look for media change */
  94. continue;
  95. if(offset && xf->offset != offset)
  96. continue;
  97. chat("incref \"%s\", dev=%d...", xf->name, xf->dev);
  98. ++xf->ref;
  99. unmlock(&xlock);
  100. close(fd);
  101. return xf;
  102. }
  103. if(fxf == nil){
  104. fxf = malloc(sizeof(Xfs));
  105. if(fxf == nil){
  106. unmlock(&xlock);
  107. close(fd);
  108. errno = Enomem;
  109. return nil;
  110. }
  111. fxf->next = xhead;
  112. xhead = fxf;
  113. }
  114. chat("alloc \"%s\", dev=%d...", name, fd);
  115. fxf->name = strdup(name);
  116. fxf->ref = 1;
  117. fxf->qid = dqid;
  118. fxf->dev = fd;
  119. fxf->fmt = 0;
  120. fxf->offset = offset;
  121. fxf->ptr = nil;
  122. fxf->isfat32 = 0;
  123. fxf->omode = omode;
  124. unmlock(&xlock);
  125. return fxf;
  126. }
  127. void
  128. refxfs(Xfs *xf, int delta)
  129. {
  130. mlock(&xlock);
  131. xf->ref += delta;
  132. if(xf->ref == 0){
  133. chat("free \"%s\", dev=%d...", xf->name, xf->dev);
  134. free(xf->name);
  135. free(xf->ptr);
  136. purgebuf(xf);
  137. if(xf->dev >= 0){
  138. close(xf->dev);
  139. xf->dev = -1;
  140. }
  141. }
  142. unmlock(&xlock);
  143. }
  144. Xfile *
  145. xfile(int fid, int flag)
  146. {
  147. Xfile **hp, *f, *pf;
  148. int k;
  149. k = ((uint32_t)fid) % FIDMOD;
  150. hp = &xfiles[k];
  151. mlock(&xlocks[k]);
  152. pf = nil;
  153. for(f=*hp; f; f=f->next){
  154. if(f->fid == fid)
  155. break;
  156. pf = f;
  157. }
  158. if(f && pf){
  159. pf->next = f->next;
  160. f->next = *hp;
  161. *hp = f;
  162. }
  163. switch(flag){
  164. default:
  165. panic("xfile");
  166. case Asis:
  167. unmlock(&xlocks[k]);
  168. return (f && f->xf && f->xf->dev < 0) ? nil : f;
  169. case Clean:
  170. break;
  171. case Clunk:
  172. if(f){
  173. *hp = f->next;
  174. unmlock(&xlocks[k]);
  175. clean(f);
  176. mlock(&freelock);
  177. f->next = freelist;
  178. freelist = f;
  179. unmlock(&freelock);
  180. } else
  181. unmlock(&xlocks[k]);
  182. return nil;
  183. }
  184. unmlock(&xlocks[k]);
  185. if(f)
  186. return clean(f);
  187. mlock(&freelock);
  188. if(f = freelist){ /* assign = */
  189. freelist = f->next;
  190. unmlock(&freelock);
  191. } else {
  192. unmlock(&freelock);
  193. f = malloc(sizeof(Xfile));
  194. if(f == nil){
  195. errno = Enomem;
  196. return nil;
  197. }
  198. }
  199. mlock(&xlocks[k]);
  200. f->next = *hp;
  201. *hp = f;
  202. unmlock(&xlocks[k]);
  203. f->fid = fid;
  204. f->flags = 0;
  205. f->qid = (Qid){0,0,0};
  206. f->xf = nil;
  207. f->ptr = nil;
  208. return f;
  209. }
  210. Xfile *
  211. clean(Xfile *f)
  212. {
  213. if(f->ptr){
  214. free(f->ptr);
  215. f->ptr = nil;
  216. }
  217. if(f->xf){
  218. refxfs(f->xf, -1);
  219. f->xf = nil;
  220. }
  221. f->flags = 0;
  222. f->qid = (Qid){0,0,0};
  223. return f;
  224. }
  225. /*
  226. * the file at <addr, offset> has moved
  227. * relocate the dos entries of all fids in the same file
  228. */
  229. void
  230. dosptrreloc(Xfile *f, Dosptr *dp, uint32_t addr, uint32_t offset)
  231. {
  232. int i;
  233. Xfile *p;
  234. Dosptr *xdp;
  235. for(i=0; i < FIDMOD; i++){
  236. for(p = xfiles[i]; p != nil; p = p->next){
  237. xdp = p->ptr;
  238. if(p != f && p->xf == f->xf
  239. && xdp != nil && xdp->addr == addr && xdp->offset == offset){
  240. memmove(xdp, dp, sizeof(Dosptr));
  241. xdp->p = nil;
  242. xdp->d = nil;
  243. p->qid.path = QIDPATH(xdp);
  244. }
  245. }
  246. }
  247. }