ext2fs.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <fcall.h>
  4. #include <thread.h>
  5. #include <9p.h>
  6. #include "dat.h"
  7. #include "fns.h"
  8. #define thdr r->ifcall
  9. #define rhdr r->ofcall
  10. extern int errno;
  11. static void
  12. response(Req *r)
  13. {
  14. char *err;
  15. if (errno) {
  16. err = xerrstr(errno);
  17. chat("%s\n", err);
  18. respond(r, err);
  19. } else {
  20. chat("OK\n");
  21. respond(r, nil);
  22. }
  23. }
  24. static void
  25. rattach(Req *r)
  26. {
  27. Xfs *xf;
  28. Xfile *root;
  29. chat("attach(fid=%d,uname=\"%s\",aname=\"%s\",afid=\"%d\")...",
  30. thdr.fid, thdr.uname, thdr.aname, thdr.afid);
  31. errno = 0;
  32. root = xfile(r->fid, Clean);
  33. if(!root){
  34. errno = Enomem;
  35. goto error;
  36. }
  37. root->xf = xf = getxfs(thdr.aname);
  38. if(!xf)
  39. goto error;
  40. /* now attach root inode */
  41. if( get_inode(root, EXT2_ROOT_INODE) < 0 )
  42. goto error;
  43. r->fid->qid.type = QTDIR;
  44. r->fid->qid.vers = 0;
  45. root->xf->rootqid = r->fid->qid;
  46. root->pinbr = EXT2_ROOT_INODE;
  47. root->root = 1;
  48. rhdr.qid = r->fid->qid;
  49. error:
  50. response(r);
  51. }
  52. static char *
  53. rclone(Fid *fid, Fid *newfid)
  54. {
  55. Xfile *of = xfile(fid, Asis);
  56. Xfile *nf = xfile(newfid, Clean);
  57. chat("clone(fid=%d,newfid=%d)...", fid->fid, newfid->fid);
  58. errno = 0;
  59. if(!of)
  60. errno = Eio;
  61. else if(!nf)
  62. errno = Enomem;
  63. else{
  64. Xfile *next = nf->next;
  65. *nf = *of;
  66. nf->next = next;
  67. nf->fid = newfid->fid;
  68. nf->root = 0;
  69. }
  70. chat("%s\n", errno? xerrstr(errno) : "OK");
  71. return errno ? xerrstr(errno) : 0;
  72. }
  73. static char *
  74. rwalk1(Fid *fid, char *name, Qid *qid)
  75. {
  76. Xfile *f=xfile(fid, Asis);
  77. int nr, sinbr = 0;
  78. chat("walk1(fid=%d,name=\"%s\")...", fid->fid, name);
  79. errno = 0;
  80. if( !f ){
  81. chat("no xfile...");
  82. goto error;
  83. }
  84. if( !(fid->qid.type & QTDIR) ){
  85. chat("qid.type=0x%x...", fid->qid.type);
  86. goto error;
  87. }
  88. sinbr = f->pinbr;
  89. if( name == 0 || name[0] == 0 || !strcmp(name, ".") ){
  90. *qid = fid->qid;
  91. goto ok;
  92. }else if( !strcmp(name, "..") ){
  93. if( fid->qid.path == f->xf->rootqid.path ){
  94. chat("walkup from root...");
  95. *qid = fid->qid;
  96. goto ok;
  97. }
  98. if( get_inode(f, f->pinbr) < 0 )
  99. goto error;
  100. if( f->pinbr == EXT2_ROOT_INODE ){
  101. *qid = f->xf->rootqid;
  102. f->pinbr = EXT2_ROOT_INODE;
  103. } else {
  104. *qid = (Qid){f->pinbr,0,QTDIR};
  105. f->inbr = f->pinbr;
  106. if( (nr = get_file(f, "..")) < 0 )
  107. goto error;
  108. f->pinbr = nr;
  109. }
  110. }else{
  111. f->pinbr = f->inbr;
  112. if( (nr = get_file(f, name)) < 0 )
  113. goto error;
  114. if( get_inode(f, nr) < 0 )
  115. goto error;
  116. *qid = (Qid){nr,0,0};
  117. if( nr == EXT2_ROOT_INODE )
  118. *qid = f->xf->rootqid;
  119. else if( S_ISDIR(getmode(f)) )
  120. qid->type = QTDIR;
  121. /*strcpy(f->name, thdr.name);*/
  122. }
  123. ok:
  124. chat("OK\n");
  125. return 0;
  126. error:
  127. f->pinbr = sinbr;
  128. chat("%s\n", xerrstr(Enonexist));
  129. return xerrstr(Enonexist);
  130. }
  131. static void
  132. rstat(Req *r)
  133. {
  134. Xfile *f=xfile(r->fid, Asis);
  135. chat("stat(fid=%d)...", thdr.fid);
  136. errno = 0;
  137. if( !f )
  138. errno = Eio;
  139. else{
  140. dostat(r->fid->qid, f, &r->d);
  141. }
  142. response(r);
  143. }
  144. static void
  145. rwstat(Req *r)
  146. {
  147. Xfile *f=xfile(r->fid, Asis);
  148. chat("wstat(fid=%d)...", thdr.fid);
  149. errno = 0;
  150. if( !f )
  151. errno = Eio;
  152. else
  153. dowstat(f, &r->d);
  154. response(r);
  155. }
  156. static void
  157. rread(Req *r)
  158. {
  159. Xfile *f;
  160. int nr;
  161. chat("read(fid=%d,offset=%lld,count=%d)...",
  162. thdr.fid, thdr.offset, thdr.count);
  163. errno = 0;
  164. if ( !(f=xfile(r->fid, Asis)) )
  165. goto error;
  166. if( r->fid->qid.type & QTDIR ){
  167. nr = readdir(f, r->rbuf, thdr.offset, thdr.count);
  168. }else
  169. nr = readfile(f, r->rbuf, thdr.offset, thdr.count);
  170. if(nr >= 0){
  171. rhdr.count = nr;
  172. chat("rcnt=%d...OK\n", nr);
  173. respond(r, nil);
  174. return;
  175. }
  176. error:
  177. errno = Eio;
  178. response(r);
  179. }
  180. static void
  181. rwrite(Req *r)
  182. {
  183. Xfile *f; int nr;
  184. chat("write(fid=%d,offset=%lld,count=%d)...",
  185. thdr.fid, thdr.offset, thdr.count);
  186. errno = 0;
  187. if (!(f=xfile(r->fid, Asis)) ){
  188. errno = Eio;
  189. goto error;
  190. }
  191. if( !S_ISREG(getmode(f)) ){
  192. errno = Elink;
  193. goto error;
  194. }
  195. nr = writefile(f, thdr.data, thdr.offset, thdr.count);
  196. if(nr >= 0){
  197. rhdr.count = nr;
  198. chat("rcnt=%d...OK\n", nr);
  199. respond(r, nil);
  200. return;
  201. }
  202. errno = Eio;
  203. error:
  204. response(r);
  205. }
  206. static void
  207. destroyfid(Fid *fid)
  208. {
  209. chat("destroy(fid=%d)\n", fid->fid);
  210. xfile(fid, Clunk);
  211. /*syncbuf(xf);*/
  212. }
  213. static void
  214. ropen(Req *r)
  215. {
  216. Xfile *f;
  217. chat("open(fid=%d,mode=%d)...", thdr.fid, thdr.mode);
  218. errno = 0;
  219. f = xfile(r->fid, Asis);
  220. if( !f ){
  221. errno = Eio;
  222. goto error;
  223. }
  224. if(thdr.mode & OTRUNC){
  225. if( !S_ISREG(getmode(f)) ){
  226. errno = Eperm;
  227. goto error;
  228. }
  229. if(truncfile(f) < 0){
  230. goto error;
  231. }
  232. }
  233. chat("f->qid=0x%8.8lux...", r->fid->qid.path);
  234. rhdr.qid = r->fid->qid;
  235. error:
  236. response(r);
  237. }
  238. static void
  239. rcreate(Req *r)
  240. {
  241. Xfile *f;
  242. int inr, perm;
  243. chat("create(fid=%d,name=\"%s\",perm=%uo,mode=%d)...",
  244. thdr.fid, thdr.name, thdr.perm, thdr.mode);
  245. errno = 0;
  246. if(strcmp(thdr.name, ".") == 0 || strcmp(thdr.name, "..") == 0){
  247. errno = Eperm;
  248. goto error;
  249. }
  250. f = xfile(r->fid, Asis);
  251. if( !f ){
  252. errno = Eio;
  253. goto error;
  254. }
  255. if( strlen(thdr.name) > EXT2_NAME_LEN ){
  256. chat("name too long ...");
  257. errno = Elongname;
  258. goto error;
  259. }
  260. /* create */
  261. errno = 0;
  262. if( thdr.perm & DMDIR ){
  263. perm = (thdr.perm & ~0777) |
  264. (getmode(f) & thdr.perm & 0777);
  265. perm |= S_IFDIR;
  266. inr = create_dir(f, thdr.name, perm);
  267. }else{
  268. perm = (thdr.perm & (~0777|0111)) |
  269. (getmode(f) & thdr.perm & 0666);
  270. perm |= S_IFREG;
  271. inr = create_file(f, thdr.name, perm);
  272. }
  273. if( inr < 0 )
  274. goto error;
  275. /* fill with new inode */
  276. f->pinbr = f->inbr;
  277. if( get_inode(f, inr) < 0 ){
  278. errno = Eio;
  279. goto error;
  280. }
  281. r->fid->qid = (Qid){inr, 0, 0};
  282. if( S_ISDIR(getmode(f)) )
  283. r->fid->qid.type |= QTDIR;
  284. chat("f->qid=0x%8.8lux...", r->fid->qid.path);
  285. rhdr.qid = r->fid->qid;
  286. error:
  287. response(r);
  288. }
  289. static void
  290. rremove(Req *r)
  291. {
  292. Xfile *f=xfile(r->fid, Asis);
  293. chat("remove(fid=%d) ...", thdr.fid);
  294. errno = 0;
  295. if(!f){
  296. errno = Eio;
  297. goto error;
  298. }
  299. /* check permission here !!!!*/
  300. unlink(f);
  301. error:
  302. response(r);
  303. }
  304. Srv ext2srv = {
  305. .destroyfid = destroyfid,
  306. .attach = rattach,
  307. .stat = rstat,
  308. .wstat = rwstat,
  309. .clone = rclone,
  310. .walk1 = rwalk1,
  311. .open = ropen,
  312. .read = rread,
  313. .write = rwrite,
  314. .create = rcreate,
  315. .remove = rremove,
  316. };