upcall.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /*
  2. * Mostly platform independent upcall operations to Venus:
  3. * -- upcalls
  4. * -- upcall routines
  5. *
  6. * Linux 2.0 version
  7. * Copyright (C) 1996 Peter J. Braam <braam@maths.ox.ac.uk>,
  8. * Michael Callahan <callahan@maths.ox.ac.uk>
  9. *
  10. * Redone for Linux 2.1
  11. * Copyright (C) 1997 Carnegie Mellon University
  12. *
  13. * Carnegie Mellon University encourages users of this code to contribute
  14. * improvements to the Coda project. Contact Peter Braam <coda@cs.cmu.edu>.
  15. */
  16. #include <asm/system.h>
  17. #include <linux/signal.h>
  18. #include <linux/sched.h>
  19. #include <linux/types.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mm.h>
  22. #include <linux/time.h>
  23. #include <linux/fs.h>
  24. #include <linux/file.h>
  25. #include <linux/stat.h>
  26. #include <linux/errno.h>
  27. #include <linux/string.h>
  28. #include <linux/slab.h>
  29. #include <linux/mutex.h>
  30. #include <asm/uaccess.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/vfs.h>
  33. #include <linux/coda.h>
  34. #include <linux/coda_psdev.h>
  35. #include "coda_linux.h"
  36. #include "coda_cache.h"
  37. #include "coda_int.h"
  38. static int coda_upcall(struct venus_comm *vc, int inSize, int *outSize,
  39. union inputArgs *buffer);
  40. static void *alloc_upcall(int opcode, int size)
  41. {
  42. union inputArgs *inp;
  43. CODA_ALLOC(inp, union inputArgs *, size);
  44. if (!inp)
  45. return ERR_PTR(-ENOMEM);
  46. inp->ih.opcode = opcode;
  47. inp->ih.pid = current->pid;
  48. inp->ih.pgid = task_pgrp_nr(current);
  49. inp->ih.uid = current_fsuid();
  50. return (void*)inp;
  51. }
  52. #define UPARG(op)\
  53. do {\
  54. inp = (union inputArgs *)alloc_upcall(op, insize); \
  55. if (IS_ERR(inp)) { return PTR_ERR(inp); }\
  56. outp = (union outputArgs *)(inp); \
  57. outsize = insize; \
  58. } while (0)
  59. #define INSIZE(tag) sizeof(struct coda_ ## tag ## _in)
  60. #define OUTSIZE(tag) sizeof(struct coda_ ## tag ## _out)
  61. #define SIZE(tag) max_t(unsigned int, INSIZE(tag), OUTSIZE(tag))
  62. /* the upcalls */
  63. int venus_rootfid(struct super_block *sb, struct CodaFid *fidp)
  64. {
  65. union inputArgs *inp;
  66. union outputArgs *outp;
  67. int insize, outsize, error;
  68. insize = SIZE(root);
  69. UPARG(CODA_ROOT);
  70. error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
  71. if (!error)
  72. *fidp = outp->coda_root.VFid;
  73. CODA_FREE(inp, insize);
  74. return error;
  75. }
  76. int venus_getattr(struct super_block *sb, struct CodaFid *fid,
  77. struct coda_vattr *attr)
  78. {
  79. union inputArgs *inp;
  80. union outputArgs *outp;
  81. int insize, outsize, error;
  82. insize = SIZE(getattr);
  83. UPARG(CODA_GETATTR);
  84. inp->coda_getattr.VFid = *fid;
  85. error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
  86. if (!error)
  87. *attr = outp->coda_getattr.attr;
  88. CODA_FREE(inp, insize);
  89. return error;
  90. }
  91. int venus_setattr(struct super_block *sb, struct CodaFid *fid,
  92. struct coda_vattr *vattr)
  93. {
  94. union inputArgs *inp;
  95. union outputArgs *outp;
  96. int insize, outsize, error;
  97. insize = SIZE(setattr);
  98. UPARG(CODA_SETATTR);
  99. inp->coda_setattr.VFid = *fid;
  100. inp->coda_setattr.attr = *vattr;
  101. error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
  102. CODA_FREE(inp, insize);
  103. return error;
  104. }
  105. int venus_lookup(struct super_block *sb, struct CodaFid *fid,
  106. const char *name, int length, int * type,
  107. struct CodaFid *resfid)
  108. {
  109. union inputArgs *inp;
  110. union outputArgs *outp;
  111. int insize, outsize, error;
  112. int offset;
  113. offset = INSIZE(lookup);
  114. insize = max_t(unsigned int, offset + length +1, OUTSIZE(lookup));
  115. UPARG(CODA_LOOKUP);
  116. inp->coda_lookup.VFid = *fid;
  117. inp->coda_lookup.name = offset;
  118. inp->coda_lookup.flags = CLU_CASE_SENSITIVE;
  119. /* send Venus a null terminated string */
  120. memcpy((char *)(inp) + offset, name, length);
  121. *((char *)inp + offset + length) = '\0';
  122. error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
  123. if (!error) {
  124. *resfid = outp->coda_lookup.VFid;
  125. *type = outp->coda_lookup.vtype;
  126. }
  127. CODA_FREE(inp, insize);
  128. return error;
  129. }
  130. int venus_close(struct super_block *sb, struct CodaFid *fid, int flags,
  131. vuid_t uid)
  132. {
  133. union inputArgs *inp;
  134. union outputArgs *outp;
  135. int insize, outsize, error;
  136. insize = SIZE(release);
  137. UPARG(CODA_CLOSE);
  138. inp->ih.uid = uid;
  139. inp->coda_close.VFid = *fid;
  140. inp->coda_close.flags = flags;
  141. error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
  142. CODA_FREE(inp, insize);
  143. return error;
  144. }
  145. int venus_open(struct super_block *sb, struct CodaFid *fid,
  146. int flags, struct file **fh)
  147. {
  148. union inputArgs *inp;
  149. union outputArgs *outp;
  150. int insize, outsize, error;
  151. insize = SIZE(open_by_fd);
  152. UPARG(CODA_OPEN_BY_FD);
  153. inp->coda_open_by_fd.VFid = *fid;
  154. inp->coda_open_by_fd.flags = flags;
  155. error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
  156. if (!error)
  157. *fh = outp->coda_open_by_fd.fh;
  158. CODA_FREE(inp, insize);
  159. return error;
  160. }
  161. int venus_mkdir(struct super_block *sb, struct CodaFid *dirfid,
  162. const char *name, int length,
  163. struct CodaFid *newfid, struct coda_vattr *attrs)
  164. {
  165. union inputArgs *inp;
  166. union outputArgs *outp;
  167. int insize, outsize, error;
  168. int offset;
  169. offset = INSIZE(mkdir);
  170. insize = max_t(unsigned int, offset + length + 1, OUTSIZE(mkdir));
  171. UPARG(CODA_MKDIR);
  172. inp->coda_mkdir.VFid = *dirfid;
  173. inp->coda_mkdir.attr = *attrs;
  174. inp->coda_mkdir.name = offset;
  175. /* Venus must get null terminated string */
  176. memcpy((char *)(inp) + offset, name, length);
  177. *((char *)inp + offset + length) = '\0';
  178. error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
  179. if (!error) {
  180. *attrs = outp->coda_mkdir.attr;
  181. *newfid = outp->coda_mkdir.VFid;
  182. }
  183. CODA_FREE(inp, insize);
  184. return error;
  185. }
  186. int venus_rename(struct super_block *sb, struct CodaFid *old_fid,
  187. struct CodaFid *new_fid, size_t old_length,
  188. size_t new_length, const char *old_name,
  189. const char *new_name)
  190. {
  191. union inputArgs *inp;
  192. union outputArgs *outp;
  193. int insize, outsize, error;
  194. int offset, s;
  195. offset = INSIZE(rename);
  196. insize = max_t(unsigned int, offset + new_length + old_length + 8,
  197. OUTSIZE(rename));
  198. UPARG(CODA_RENAME);
  199. inp->coda_rename.sourceFid = *old_fid;
  200. inp->coda_rename.destFid = *new_fid;
  201. inp->coda_rename.srcname = offset;
  202. /* Venus must receive an null terminated string */
  203. s = ( old_length & ~0x3) +4; /* round up to word boundary */
  204. memcpy((char *)(inp) + offset, old_name, old_length);
  205. *((char *)inp + offset + old_length) = '\0';
  206. /* another null terminated string for Venus */
  207. offset += s;
  208. inp->coda_rename.destname = offset;
  209. s = ( new_length & ~0x3) +4; /* round up to word boundary */
  210. memcpy((char *)(inp) + offset, new_name, new_length);
  211. *((char *)inp + offset + new_length) = '\0';
  212. error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
  213. CODA_FREE(inp, insize);
  214. return error;
  215. }
  216. int venus_create(struct super_block *sb, struct CodaFid *dirfid,
  217. const char *name, int length, int excl, int mode,
  218. struct CodaFid *newfid, struct coda_vattr *attrs)
  219. {
  220. union inputArgs *inp;
  221. union outputArgs *outp;
  222. int insize, outsize, error;
  223. int offset;
  224. offset = INSIZE(create);
  225. insize = max_t(unsigned int, offset + length + 1, OUTSIZE(create));
  226. UPARG(CODA_CREATE);
  227. inp->coda_create.VFid = *dirfid;
  228. inp->coda_create.attr.va_mode = mode;
  229. inp->coda_create.excl = excl;
  230. inp->coda_create.mode = mode;
  231. inp->coda_create.name = offset;
  232. /* Venus must get null terminated string */
  233. memcpy((char *)(inp) + offset, name, length);
  234. *((char *)inp + offset + length) = '\0';
  235. error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
  236. if (!error) {
  237. *attrs = outp->coda_create.attr;
  238. *newfid = outp->coda_create.VFid;
  239. }
  240. CODA_FREE(inp, insize);
  241. return error;
  242. }
  243. int venus_rmdir(struct super_block *sb, struct CodaFid *dirfid,
  244. const char *name, int length)
  245. {
  246. union inputArgs *inp;
  247. union outputArgs *outp;
  248. int insize, outsize, error;
  249. int offset;
  250. offset = INSIZE(rmdir);
  251. insize = max_t(unsigned int, offset + length + 1, OUTSIZE(rmdir));
  252. UPARG(CODA_RMDIR);
  253. inp->coda_rmdir.VFid = *dirfid;
  254. inp->coda_rmdir.name = offset;
  255. memcpy((char *)(inp) + offset, name, length);
  256. *((char *)inp + offset + length) = '\0';
  257. error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
  258. CODA_FREE(inp, insize);
  259. return error;
  260. }
  261. int venus_remove(struct super_block *sb, struct CodaFid *dirfid,
  262. const char *name, int length)
  263. {
  264. union inputArgs *inp;
  265. union outputArgs *outp;
  266. int error=0, insize, outsize, offset;
  267. offset = INSIZE(remove);
  268. insize = max_t(unsigned int, offset + length + 1, OUTSIZE(remove));
  269. UPARG(CODA_REMOVE);
  270. inp->coda_remove.VFid = *dirfid;
  271. inp->coda_remove.name = offset;
  272. memcpy((char *)(inp) + offset, name, length);
  273. *((char *)inp + offset + length) = '\0';
  274. error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
  275. CODA_FREE(inp, insize);
  276. return error;
  277. }
  278. int venus_readlink(struct super_block *sb, struct CodaFid *fid,
  279. char *buffer, int *length)
  280. {
  281. union inputArgs *inp;
  282. union outputArgs *outp;
  283. int insize, outsize, error;
  284. int retlen;
  285. char *result;
  286. insize = max_t(unsigned int,
  287. INSIZE(readlink), OUTSIZE(readlink)+ *length + 1);
  288. UPARG(CODA_READLINK);
  289. inp->coda_readlink.VFid = *fid;
  290. error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
  291. if (!error) {
  292. retlen = outp->coda_readlink.count;
  293. if ( retlen > *length )
  294. retlen = *length;
  295. *length = retlen;
  296. result = (char *)outp + (long)outp->coda_readlink.data;
  297. memcpy(buffer, result, retlen);
  298. *(buffer + retlen) = '\0';
  299. }
  300. CODA_FREE(inp, insize);
  301. return error;
  302. }
  303. int venus_link(struct super_block *sb, struct CodaFid *fid,
  304. struct CodaFid *dirfid, const char *name, int len )
  305. {
  306. union inputArgs *inp;
  307. union outputArgs *outp;
  308. int insize, outsize, error;
  309. int offset;
  310. offset = INSIZE(link);
  311. insize = max_t(unsigned int, offset + len + 1, OUTSIZE(link));
  312. UPARG(CODA_LINK);
  313. inp->coda_link.sourceFid = *fid;
  314. inp->coda_link.destFid = *dirfid;
  315. inp->coda_link.tname = offset;
  316. /* make sure strings are null terminated */
  317. memcpy((char *)(inp) + offset, name, len);
  318. *((char *)inp + offset + len) = '\0';
  319. error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
  320. CODA_FREE(inp, insize);
  321. return error;
  322. }
  323. int venus_symlink(struct super_block *sb, struct CodaFid *fid,
  324. const char *name, int len,
  325. const char *symname, int symlen)
  326. {
  327. union inputArgs *inp;
  328. union outputArgs *outp;
  329. int insize, outsize, error;
  330. int offset, s;
  331. offset = INSIZE(symlink);
  332. insize = max_t(unsigned int, offset + len + symlen + 8, OUTSIZE(symlink));
  333. UPARG(CODA_SYMLINK);
  334. /* inp->coda_symlink.attr = *tva; XXXXXX */
  335. inp->coda_symlink.VFid = *fid;
  336. /* Round up to word boundary and null terminate */
  337. inp->coda_symlink.srcname = offset;
  338. s = ( symlen & ~0x3 ) + 4;
  339. memcpy((char *)(inp) + offset, symname, symlen);
  340. *((char *)inp + offset + symlen) = '\0';
  341. /* Round up to word boundary and null terminate */
  342. offset += s;
  343. inp->coda_symlink.tname = offset;
  344. s = (len & ~0x3) + 4;
  345. memcpy((char *)(inp) + offset, name, len);
  346. *((char *)inp + offset + len) = '\0';
  347. error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
  348. CODA_FREE(inp, insize);
  349. return error;
  350. }
  351. int venus_fsync(struct super_block *sb, struct CodaFid *fid)
  352. {
  353. union inputArgs *inp;
  354. union outputArgs *outp;
  355. int insize, outsize, error;
  356. insize=SIZE(fsync);
  357. UPARG(CODA_FSYNC);
  358. inp->coda_fsync.VFid = *fid;
  359. error = coda_upcall(coda_vcp(sb), sizeof(union inputArgs),
  360. &outsize, inp);
  361. CODA_FREE(inp, insize);
  362. return error;
  363. }
  364. int venus_access(struct super_block *sb, struct CodaFid *fid, int mask)
  365. {
  366. union inputArgs *inp;
  367. union outputArgs *outp;
  368. int insize, outsize, error;
  369. insize = SIZE(access);
  370. UPARG(CODA_ACCESS);
  371. inp->coda_access.VFid = *fid;
  372. inp->coda_access.flags = mask;
  373. error = coda_upcall(coda_vcp(sb), insize, &outsize, inp);
  374. CODA_FREE(inp, insize);
  375. return error;
  376. }
  377. int venus_pioctl(struct super_block *sb, struct CodaFid *fid,
  378. unsigned int cmd, struct PioctlData *data)
  379. {
  380. union inputArgs *inp;
  381. union outputArgs *outp;
  382. int insize, outsize, error;
  383. int iocsize;
  384. insize = VC_MAXMSGSIZE;
  385. UPARG(CODA_IOCTL);
  386. /* build packet for Venus */
  387. if (data->vi.in_size > VC_MAXDATASIZE) {
  388. error = -EINVAL;
  389. goto exit;
  390. }
  391. if (data->vi.out_size > VC_MAXDATASIZE) {
  392. error = -EINVAL;
  393. goto exit;
  394. }
  395. inp->coda_ioctl.VFid = *fid;
  396. /* the cmd field was mutated by increasing its size field to
  397. * reflect the path and follow args. We need to subtract that
  398. * out before sending the command to Venus. */
  399. inp->coda_ioctl.cmd = (cmd & ~(PIOCPARM_MASK << 16));
  400. iocsize = ((cmd >> 16) & PIOCPARM_MASK) - sizeof(char *) - sizeof(int);
  401. inp->coda_ioctl.cmd |= (iocsize & PIOCPARM_MASK) << 16;
  402. /* in->coda_ioctl.rwflag = flag; */
  403. inp->coda_ioctl.len = data->vi.in_size;
  404. inp->coda_ioctl.data = (char *)(INSIZE(ioctl));
  405. /* get the data out of user space */
  406. if ( copy_from_user((char*)inp + (long)inp->coda_ioctl.data,
  407. data->vi.in, data->vi.in_size) ) {
  408. error = -EINVAL;
  409. goto exit;
  410. }
  411. error = coda_upcall(coda_vcp(sb), SIZE(ioctl) + data->vi.in_size,
  412. &outsize, inp);
  413. if (error) {
  414. printk("coda_pioctl: Venus returns: %d for %s\n",
  415. error, coda_f2s(fid));
  416. goto exit;
  417. }
  418. if (outsize < (long)outp->coda_ioctl.data + outp->coda_ioctl.len) {
  419. error = -EINVAL;
  420. goto exit;
  421. }
  422. /* Copy out the OUT buffer. */
  423. if (outp->coda_ioctl.len > data->vi.out_size) {
  424. error = -EINVAL;
  425. goto exit;
  426. }
  427. /* Copy out the OUT buffer. */
  428. if (copy_to_user(data->vi.out,
  429. (char *)outp + (long)outp->coda_ioctl.data,
  430. outp->coda_ioctl.len)) {
  431. error = -EFAULT;
  432. goto exit;
  433. }
  434. exit:
  435. CODA_FREE(inp, insize);
  436. return error;
  437. }
  438. int venus_statfs(struct dentry *dentry, struct kstatfs *sfs)
  439. {
  440. union inputArgs *inp;
  441. union outputArgs *outp;
  442. int insize, outsize, error;
  443. insize = max_t(unsigned int, INSIZE(statfs), OUTSIZE(statfs));
  444. UPARG(CODA_STATFS);
  445. error = coda_upcall(coda_vcp(dentry->d_sb), insize, &outsize, inp);
  446. if (!error) {
  447. sfs->f_blocks = outp->coda_statfs.stat.f_blocks;
  448. sfs->f_bfree = outp->coda_statfs.stat.f_bfree;
  449. sfs->f_bavail = outp->coda_statfs.stat.f_bavail;
  450. sfs->f_files = outp->coda_statfs.stat.f_files;
  451. sfs->f_ffree = outp->coda_statfs.stat.f_ffree;
  452. }
  453. CODA_FREE(inp, insize);
  454. return error;
  455. }
  456. /*
  457. * coda_upcall and coda_downcall routines.
  458. */
  459. static void coda_block_signals(sigset_t *old)
  460. {
  461. spin_lock_irq(&current->sighand->siglock);
  462. *old = current->blocked;
  463. sigfillset(&current->blocked);
  464. sigdelset(&current->blocked, SIGKILL);
  465. sigdelset(&current->blocked, SIGSTOP);
  466. sigdelset(&current->blocked, SIGINT);
  467. recalc_sigpending();
  468. spin_unlock_irq(&current->sighand->siglock);
  469. }
  470. static void coda_unblock_signals(sigset_t *old)
  471. {
  472. spin_lock_irq(&current->sighand->siglock);
  473. current->blocked = *old;
  474. recalc_sigpending();
  475. spin_unlock_irq(&current->sighand->siglock);
  476. }
  477. /* Don't allow signals to interrupt the following upcalls before venus
  478. * has seen them,
  479. * - CODA_CLOSE or CODA_RELEASE upcall (to avoid reference count problems)
  480. * - CODA_STORE (to avoid data loss)
  481. */
  482. #define CODA_INTERRUPTIBLE(r) (!coda_hard && \
  483. (((r)->uc_opcode != CODA_CLOSE && \
  484. (r)->uc_opcode != CODA_STORE && \
  485. (r)->uc_opcode != CODA_RELEASE) || \
  486. (r)->uc_flags & CODA_REQ_READ))
  487. static inline void coda_waitfor_upcall(struct venus_comm *vcp,
  488. struct upc_req *req)
  489. {
  490. DECLARE_WAITQUEUE(wait, current);
  491. unsigned long timeout = jiffies + coda_timeout * HZ;
  492. sigset_t old;
  493. int blocked;
  494. coda_block_signals(&old);
  495. blocked = 1;
  496. add_wait_queue(&req->uc_sleep, &wait);
  497. for (;;) {
  498. if (CODA_INTERRUPTIBLE(req))
  499. set_current_state(TASK_INTERRUPTIBLE);
  500. else
  501. set_current_state(TASK_UNINTERRUPTIBLE);
  502. /* got a reply */
  503. if (req->uc_flags & (CODA_REQ_WRITE | CODA_REQ_ABORT))
  504. break;
  505. if (blocked && time_after(jiffies, timeout) &&
  506. CODA_INTERRUPTIBLE(req))
  507. {
  508. coda_unblock_signals(&old);
  509. blocked = 0;
  510. }
  511. if (signal_pending(current)) {
  512. list_del(&req->uc_chain);
  513. break;
  514. }
  515. mutex_unlock(&vcp->vc_mutex);
  516. if (blocked)
  517. schedule_timeout(HZ);
  518. else
  519. schedule();
  520. mutex_lock(&vcp->vc_mutex);
  521. }
  522. if (blocked)
  523. coda_unblock_signals(&old);
  524. remove_wait_queue(&req->uc_sleep, &wait);
  525. set_current_state(TASK_RUNNING);
  526. }
  527. /*
  528. * coda_upcall will return an error in the case of
  529. * failed communication with Venus _or_ will peek at Venus
  530. * reply and return Venus' error.
  531. *
  532. * As venus has 2 types of errors, normal errors (positive) and internal
  533. * errors (negative), normal errors are negated, while internal errors
  534. * are all mapped to -EINTR, while showing a nice warning message. (jh)
  535. */
  536. static int coda_upcall(struct venus_comm *vcp,
  537. int inSize, int *outSize,
  538. union inputArgs *buffer)
  539. {
  540. union outputArgs *out;
  541. union inputArgs *sig_inputArgs;
  542. struct upc_req *req = NULL, *sig_req;
  543. int error;
  544. mutex_lock(&vcp->vc_mutex);
  545. if (!vcp->vc_inuse) {
  546. printk(KERN_NOTICE "coda: Venus dead, not sending upcall\n");
  547. error = -ENXIO;
  548. goto exit;
  549. }
  550. /* Format the request message. */
  551. req = kmalloc(sizeof(struct upc_req), GFP_KERNEL);
  552. if (!req) {
  553. error = -ENOMEM;
  554. goto exit;
  555. }
  556. req->uc_data = (void *)buffer;
  557. req->uc_flags = 0;
  558. req->uc_inSize = inSize;
  559. req->uc_outSize = *outSize ? *outSize : inSize;
  560. req->uc_opcode = ((union inputArgs *)buffer)->ih.opcode;
  561. req->uc_unique = ++vcp->vc_seq;
  562. init_waitqueue_head(&req->uc_sleep);
  563. /* Fill in the common input args. */
  564. ((union inputArgs *)buffer)->ih.unique = req->uc_unique;
  565. /* Append msg to pending queue and poke Venus. */
  566. list_add_tail(&req->uc_chain, &vcp->vc_pending);
  567. wake_up_interruptible(&vcp->vc_waitq);
  568. /* We can be interrupted while we wait for Venus to process
  569. * our request. If the interrupt occurs before Venus has read
  570. * the request, we dequeue and return. If it occurs after the
  571. * read but before the reply, we dequeue, send a signal
  572. * message, and return. If it occurs after the reply we ignore
  573. * it. In no case do we want to restart the syscall. If it
  574. * was interrupted by a venus shutdown (psdev_close), return
  575. * ENODEV. */
  576. /* Go to sleep. Wake up on signals only after the timeout. */
  577. coda_waitfor_upcall(vcp, req);
  578. /* Op went through, interrupt or not... */
  579. if (req->uc_flags & CODA_REQ_WRITE) {
  580. out = (union outputArgs *)req->uc_data;
  581. /* here we map positive Venus errors to kernel errors */
  582. error = -out->oh.result;
  583. *outSize = req->uc_outSize;
  584. goto exit;
  585. }
  586. error = -EINTR;
  587. if ((req->uc_flags & CODA_REQ_ABORT) || !signal_pending(current)) {
  588. printk(KERN_WARNING "coda: Unexpected interruption.\n");
  589. goto exit;
  590. }
  591. /* Interrupted before venus read it. */
  592. if (!(req->uc_flags & CODA_REQ_READ))
  593. goto exit;
  594. /* Venus saw the upcall, make sure we can send interrupt signal */
  595. if (!vcp->vc_inuse) {
  596. printk(KERN_INFO "coda: Venus dead, not sending signal.\n");
  597. goto exit;
  598. }
  599. error = -ENOMEM;
  600. sig_req = kmalloc(sizeof(struct upc_req), GFP_KERNEL);
  601. if (!sig_req) goto exit;
  602. CODA_ALLOC((sig_req->uc_data), char *, sizeof(struct coda_in_hdr));
  603. if (!sig_req->uc_data) {
  604. kfree(sig_req);
  605. goto exit;
  606. }
  607. error = -EINTR;
  608. sig_inputArgs = (union inputArgs *)sig_req->uc_data;
  609. sig_inputArgs->ih.opcode = CODA_SIGNAL;
  610. sig_inputArgs->ih.unique = req->uc_unique;
  611. sig_req->uc_flags = CODA_REQ_ASYNC;
  612. sig_req->uc_opcode = sig_inputArgs->ih.opcode;
  613. sig_req->uc_unique = sig_inputArgs->ih.unique;
  614. sig_req->uc_inSize = sizeof(struct coda_in_hdr);
  615. sig_req->uc_outSize = sizeof(struct coda_in_hdr);
  616. /* insert at head of queue! */
  617. list_add(&(sig_req->uc_chain), &vcp->vc_pending);
  618. wake_up_interruptible(&vcp->vc_waitq);
  619. exit:
  620. kfree(req);
  621. mutex_unlock(&vcp->vc_mutex);
  622. return error;
  623. }
  624. /*
  625. The statements below are part of the Coda opportunistic
  626. programming -- taken from the Mach/BSD kernel code for Coda.
  627. You don't get correct semantics by stating what needs to be
  628. done without guaranteeing the invariants needed for it to happen.
  629. When will be have time to find out what exactly is going on? (pjb)
  630. */
  631. /*
  632. * There are 7 cases where cache invalidations occur. The semantics
  633. * of each is listed here:
  634. *
  635. * CODA_FLUSH -- flush all entries from the name cache and the cnode cache.
  636. * CODA_PURGEUSER -- flush all entries from the name cache for a specific user
  637. * This call is a result of token expiration.
  638. *
  639. * The next arise as the result of callbacks on a file or directory.
  640. * CODA_ZAPFILE -- flush the cached attributes for a file.
  641. * CODA_ZAPDIR -- flush the attributes for the dir and
  642. * force a new lookup for all the children
  643. of this dir.
  644. *
  645. * The next is a result of Venus detecting an inconsistent file.
  646. * CODA_PURGEFID -- flush the attribute for the file
  647. * purge it and its children from the dcache
  648. *
  649. * The last allows Venus to replace local fids with global ones
  650. * during reintegration.
  651. *
  652. * CODA_REPLACE -- replace one CodaFid with another throughout the name cache */
  653. int coda_downcall(struct venus_comm *vcp, int opcode, union outputArgs *out)
  654. {
  655. struct inode *inode = NULL;
  656. struct CodaFid *fid = NULL, *newfid;
  657. struct super_block *sb;
  658. /* Handle invalidation requests. */
  659. mutex_lock(&vcp->vc_mutex);
  660. sb = vcp->vc_sb;
  661. if (!sb || !sb->s_root)
  662. goto unlock_out;
  663. switch (opcode) {
  664. case CODA_FLUSH:
  665. coda_cache_clear_all(sb);
  666. shrink_dcache_sb(sb);
  667. if (sb->s_root->d_inode)
  668. coda_flag_inode(sb->s_root->d_inode, C_FLUSH);
  669. break;
  670. case CODA_PURGEUSER:
  671. coda_cache_clear_all(sb);
  672. break;
  673. case CODA_ZAPDIR:
  674. fid = &out->coda_zapdir.CodaFid;
  675. break;
  676. case CODA_ZAPFILE:
  677. fid = &out->coda_zapfile.CodaFid;
  678. break;
  679. case CODA_PURGEFID:
  680. fid = &out->coda_purgefid.CodaFid;
  681. break;
  682. case CODA_REPLACE:
  683. fid = &out->coda_replace.OldFid;
  684. break;
  685. }
  686. if (fid)
  687. inode = coda_fid_to_inode(fid, sb);
  688. unlock_out:
  689. mutex_unlock(&vcp->vc_mutex);
  690. if (!inode)
  691. return 0;
  692. switch (opcode) {
  693. case CODA_ZAPDIR:
  694. coda_flag_inode_children(inode, C_PURGE);
  695. coda_flag_inode(inode, C_VATTR);
  696. break;
  697. case CODA_ZAPFILE:
  698. coda_flag_inode(inode, C_VATTR);
  699. break;
  700. case CODA_PURGEFID:
  701. coda_flag_inode_children(inode, C_PURGE);
  702. /* catch the dentries later if some are still busy */
  703. coda_flag_inode(inode, C_PURGE);
  704. d_prune_aliases(inode);
  705. break;
  706. case CODA_REPLACE:
  707. newfid = &out->coda_replace.NewFid;
  708. coda_replace_fid(inode, fid, newfid);
  709. break;
  710. }
  711. iput(inode);
  712. return 0;
  713. }