9p.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. // -------------------------------------------------
  2. // --------------------- 9P ------------------------
  3. // -------------------------------------------------
  4. // Implementation of the 9p filesystem device following the
  5. // 9P2000.L protocol ( https://code.google.com/p/diod/wiki/protocol )
  6. "use strict";
  7. // Feature bit (bit position) for mount tag.
  8. const VIRTIO_9P_F_MOUNT_TAG = 0;
  9. // Assumed max tag length in bytes.
  10. const VIRTIO_9P_MAX_TAGLEN = 254;
  11. // TODO
  12. // flush
  13. var EPERM = 1; /* Operation not permitted */
  14. var ENOENT = 2; /* No such file or directory */
  15. var EEXIST = 17; /* File exists */
  16. var EINVAL = 22; /* Invalid argument */
  17. var EOPNOTSUPP = 95; /* Operation is not supported */
  18. var ENOTEMPTY = 39; /* Directory not empty */
  19. var EPROTO = 71; /* Protocol error */
  20. var P9_SETATTR_MODE = 0x00000001;
  21. var P9_SETATTR_UID = 0x00000002;
  22. var P9_SETATTR_GID = 0x00000004;
  23. var P9_SETATTR_SIZE = 0x00000008;
  24. var P9_SETATTR_ATIME = 0x00000010;
  25. var P9_SETATTR_MTIME = 0x00000020;
  26. var P9_SETATTR_CTIME = 0x00000040;
  27. var P9_SETATTR_ATIME_SET = 0x00000080;
  28. var P9_SETATTR_MTIME_SET = 0x00000100;
  29. var P9_STAT_MODE_DIR = 0x80000000;
  30. var P9_STAT_MODE_APPEND = 0x40000000;
  31. var P9_STAT_MODE_EXCL = 0x20000000;
  32. var P9_STAT_MODE_MOUNT = 0x10000000;
  33. var P9_STAT_MODE_AUTH = 0x08000000;
  34. var P9_STAT_MODE_TMP = 0x04000000;
  35. var P9_STAT_MODE_SYMLINK = 0x02000000;
  36. var P9_STAT_MODE_LINK = 0x01000000;
  37. var P9_STAT_MODE_DEVICE = 0x00800000;
  38. var P9_STAT_MODE_NAMED_PIPE = 0x00200000;
  39. var P9_STAT_MODE_SOCKET = 0x00100000;
  40. var P9_STAT_MODE_SETUID = 0x00080000;
  41. var P9_STAT_MODE_SETGID = 0x00040000;
  42. var P9_STAT_MODE_SETVTX = 0x00010000;
  43. const P9_LOCK_TYPE_RDLCK = 0;
  44. const P9_LOCK_TYPE_WRLCK = 1;
  45. const P9_LOCK_TYPE_UNLCK = 2;
  46. const P9_LOCK_TYPES = Object.freeze(["shared", "exclusive", "unlock"]);
  47. const P9_LOCK_FLAGS_BLOCK = 1;
  48. const P9_LOCK_FLAGS_RECLAIM = 2;
  49. const P9_LOCK_SUCCESS = 0;
  50. const P9_LOCK_BLOCKED = 1;
  51. const P9_LOCK_ERROR = 2;
  52. const P9_LOCK_GRACE = 3;
  53. var FID_NONE = -1;
  54. var FID_INODE = 1;
  55. var FID_XATTR = 2;
  56. /**
  57. * @constructor
  58. *
  59. * @param {FS} filesystem
  60. * @param {CPU} cpu
  61. */
  62. function Virtio9p(filesystem, cpu, bus) {
  63. /** @type {FS} */
  64. this.fs = filesystem;
  65. /** @const @type {BusConnector} */
  66. this.bus = bus;
  67. //this.configspace = [0x0, 0x4, 0x68, 0x6F, 0x73, 0x74]; // length of string and "host" string
  68. //this.configspace = [0x0, 0x9, 0x2F, 0x64, 0x65, 0x76, 0x2F, 0x72, 0x6F, 0x6F, 0x74 ]; // length of string and "/dev/root" string
  69. this.configspace_tagname = [0x68, 0x6F, 0x73, 0x74, 0x39, 0x70]; // "host9p" string
  70. this.configspace_taglen = this.configspace_tagname.length; // num bytes
  71. this.VERSION = "9P2000.L";
  72. this.BLOCKSIZE = 8192; // Let's define one page.
  73. this.msize = 8192; // maximum message size
  74. this.replybuffer = new Uint8Array(this.msize*2); // Twice the msize to stay on the safe site
  75. this.replybuffersize = 0;
  76. this.fids = [];
  77. /** @type {VirtIO} */
  78. this.virtio = new VirtIO(cpu,
  79. {
  80. name: "virtio-9p",
  81. pci_id: 0x06 << 3,
  82. device_id: 0x1049,
  83. subsystem_device_id: 9,
  84. common:
  85. {
  86. initial_port: 0xA800,
  87. queues:
  88. [
  89. {
  90. size_supported: 32,
  91. notify_offset: 0,
  92. },
  93. ],
  94. features:
  95. [
  96. VIRTIO_9P_F_MOUNT_TAG,
  97. VIRTIO_F_VERSION_1,
  98. VIRTIO_F_RING_EVENT_IDX,
  99. VIRTIO_F_RING_INDIRECT_DESC,
  100. ],
  101. on_driver_ok: () => {},
  102. },
  103. notification:
  104. {
  105. initial_port: 0xA900,
  106. single_handler: false,
  107. handlers:
  108. [
  109. (queue_id) =>
  110. {
  111. if(queue_id !== 0)
  112. {
  113. dbg_assert(false, "Virtio9P Notified for non-existent queue: " + queue_id +
  114. " (expected queue_id of 0)");
  115. return;
  116. }
  117. while(this.virtqueue.has_request())
  118. {
  119. const bufchain = this.virtqueue.pop_request();
  120. this.ReceiveRequest(bufchain);
  121. }
  122. this.virtqueue.notify_me_after(0);
  123. // Don't flush replies here: async replies are not completed yet.
  124. },
  125. ],
  126. },
  127. isr_status:
  128. {
  129. initial_port: 0xA700,
  130. },
  131. device_specific:
  132. {
  133. initial_port: 0xA600,
  134. struct:
  135. [
  136. {
  137. bytes: 2,
  138. name: "mount tag length",
  139. read: () => this.configspace_taglen,
  140. write: data => { /* read only */ },
  141. },
  142. ].concat(v86util.range(VIRTIO_9P_MAX_TAGLEN).map(index =>
  143. ({
  144. bytes: 1,
  145. name: "mount tag name " + index,
  146. // Note: configspace_tagname may have changed after set_state
  147. read: () => this.configspace_tagname[index] || 0,
  148. write: data => { /* read only */ },
  149. })
  150. )),
  151. },
  152. });
  153. this.virtqueue = this.virtio.queues[0];
  154. }
  155. Virtio9p.prototype.get_state = function()
  156. {
  157. var state = [];
  158. state[0] = this.configspace_tagname;
  159. state[1] = this.configspace_taglen;
  160. state[2] = this.virtio;
  161. state[3] = this.VERSION;
  162. state[4] = this.BLOCKSIZE;
  163. state[5] = this.msize;
  164. state[6] = this.replybuffer;
  165. state[7] = this.replybuffersize;
  166. state[8] = this.fids.map(function(f) { return [f.inodeid, f.type, f.uid, f.dbg_name]; });
  167. state[9] = this.fs;
  168. return state;
  169. };
  170. Virtio9p.prototype.set_state = function(state)
  171. {
  172. this.configspace_tagname = state[0];
  173. this.configspace_taglen = state[1];
  174. this.virtio.set_state(state[2]);
  175. this.virtqueue = this.virtio.queues[0];
  176. this.VERSION = state[3];
  177. this.BLOCKSIZE = state[4];
  178. this.msize = state[5];
  179. this.replybuffer = state[6];
  180. this.replybuffersize = state[7];
  181. this.fids = state[8].map(function(f)
  182. {
  183. return { inodeid: f[0], type: f[1], uid: f[2], dbg_name: f[3] };
  184. });
  185. this.fs.set_state(state[9]);
  186. };
  187. // Note: dbg_name is only used for debugging messages and may not be the same as the filename,
  188. // since it is not synchronised with renames done outside of 9p. Hard-links, linking and unlinking
  189. // operations also mean that having a single filename no longer makes sense.
  190. // Set TRACK_FILENAMES = true (in config.js) to sync dbg_name during 9p renames.
  191. Virtio9p.prototype.Createfid = function(inodeid, type, uid, dbg_name) {
  192. return {inodeid, type, uid, dbg_name};
  193. };
  194. Virtio9p.prototype.update_dbg_name = function(idx, newname)
  195. {
  196. for(const fid of this.fids)
  197. {
  198. if(fid.inodeid === idx) fid.dbg_name = newname;
  199. }
  200. };
  201. Virtio9p.prototype.Reset = function() {
  202. this.fids = [];
  203. };
  204. Virtio9p.prototype.BuildReply = function(id, tag, payloadsize) {
  205. dbg_assert(payloadsize >= 0, "9P: Negative payload size");
  206. marshall.Marshall(["w", "b", "h"], [payloadsize+7, id+1, tag], this.replybuffer, 0);
  207. if ((payloadsize+7) >= this.replybuffer.length) {
  208. message.Debug("Error in 9p: payloadsize exceeds maximum length");
  209. }
  210. //for(var i=0; i<payload.length; i++)
  211. // this.replybuffer[7+i] = payload[i];
  212. this.replybuffersize = payloadsize+7;
  213. return;
  214. };
  215. Virtio9p.prototype.SendError = function (tag, errormsg, errorcode) {
  216. //var size = marshall.Marshall(["s", "w"], [errormsg, errorcode], this.replybuffer, 7);
  217. var size = marshall.Marshall(["w"], [errorcode], this.replybuffer, 7);
  218. this.BuildReply(6, tag, size);
  219. };
  220. Virtio9p.prototype.SendReply = function (bufchain) {
  221. dbg_assert(this.replybuffersize >= 0, "9P: Negative replybuffersize");
  222. bufchain.set_next_blob(this.replybuffer.subarray(0, this.replybuffersize));
  223. this.virtqueue.push_reply(bufchain);
  224. this.virtqueue.flush_replies();
  225. };
  226. Virtio9p.prototype.ReceiveRequest = async function (bufchain) {
  227. // TODO: split into header + data blobs to avoid unnecessary copying.
  228. const buffer = new Uint8Array(bufchain.length_readable);
  229. bufchain.get_next_blob(buffer);
  230. const state = { offset : 0 };
  231. var header = marshall.Unmarshall(["w", "b", "h"], buffer, state);
  232. var size = header[0];
  233. var id = header[1];
  234. var tag = header[2];
  235. //message.Debug("size:" + size + " id:" + id + " tag:" + tag);
  236. switch(id)
  237. {
  238. case 8: // statfs
  239. size = this.fs.GetTotalSize(); // size used by all files
  240. var space = this.fs.GetSpace();
  241. var req = [];
  242. req[0] = 0x01021997;
  243. req[1] = this.BLOCKSIZE; // optimal transfer block size
  244. req[2] = Math.floor(space/req[1]); // free blocks
  245. req[3] = req[2] - Math.floor(size/req[1]); // free blocks in fs
  246. req[4] = req[2] - Math.floor(size/req[1]); // free blocks avail to non-superuser
  247. req[5] = this.fs.CountUsedInodes(); // total number of inodes
  248. req[6] = this.fs.CountFreeInodes();
  249. req[7] = 0; // file system id?
  250. req[8] = 256; // maximum length of filenames
  251. size = marshall.Marshall(["w", "w", "d", "d", "d", "d", "d", "d", "w"], req, this.replybuffer, 7);
  252. this.BuildReply(id, tag, size);
  253. this.SendReply(bufchain);
  254. break;
  255. case 112: // topen
  256. case 12: // tlopen
  257. var req = marshall.Unmarshall(["w", "w"], buffer, state);
  258. var fid = req[0];
  259. var mode = req[1];
  260. message.Debug("[open] fid=" + fid + ", mode=" + mode);
  261. var idx = this.fids[fid].inodeid;
  262. var inode = this.fs.GetInode(idx);
  263. message.Debug("file open " + this.fids[fid].dbg_name);
  264. //if (inode.status == STATUS_LOADING) return;
  265. var ret = this.fs.OpenInode(idx, mode);
  266. this.fs.AddEvent(this.fids[fid].inodeid,
  267. function() {
  268. message.Debug("file opened " + this.fids[fid].dbg_name + " tag:"+tag);
  269. var req = [];
  270. req[0] = inode.qid;
  271. req[1] = this.msize - 24;
  272. marshall.Marshall(["Q", "w"], req, this.replybuffer, 7);
  273. this.BuildReply(id, tag, 13+4);
  274. this.SendReply(bufchain);
  275. }.bind(this)
  276. );
  277. break;
  278. case 70: // link
  279. var req = marshall.Unmarshall(["w", "w", "s"], buffer, state);
  280. var dfid = req[0];
  281. var fid = req[1];
  282. var name = req[2];
  283. message.Debug("[link] dfid=" + dfid + ", name=" + name);
  284. var ret = this.fs.Link(this.fids[dfid].inodeid, this.fids[fid].inodeid, name);
  285. if(ret < 0)
  286. {
  287. let error_message = "";
  288. if(ret === -EPERM) error_message = "Operation not permitted";
  289. else
  290. {
  291. error_message = "Unknown error: " + (-ret);
  292. dbg_assert(false, "[link]: Unexpected error code: " + (-ret));
  293. }
  294. this.SendError(tag, error_message, -ret);
  295. this.SendReply(bufchain);
  296. break;
  297. }
  298. this.BuildReply(id, tag, 0);
  299. this.SendReply(bufchain);
  300. break;
  301. case 16: // symlink
  302. var req = marshall.Unmarshall(["w", "s", "s", "w"], buffer, state);
  303. var fid = req[0];
  304. var name = req[1];
  305. var symgt = req[2];
  306. var gid = req[3];
  307. message.Debug("[symlink] fid=" + fid + ", name=" + name + ", symgt=" + symgt + ", gid=" + gid);
  308. var idx = this.fs.CreateSymlink(name, this.fids[fid].inodeid, symgt);
  309. var inode = this.fs.GetInode(idx);
  310. inode.uid = this.fids[fid].uid;
  311. inode.gid = gid;
  312. marshall.Marshall(["Q"], [inode.qid], this.replybuffer, 7);
  313. this.BuildReply(id, tag, 13);
  314. this.SendReply(bufchain);
  315. break;
  316. case 18: // mknod
  317. var req = marshall.Unmarshall(["w", "s", "w", "w", "w", "w"], buffer, state);
  318. var fid = req[0];
  319. var name = req[1];
  320. var mode = req[2];
  321. var major = req[3];
  322. var minor = req[4];
  323. var gid = req[5];
  324. message.Debug("[mknod] fid=" + fid + ", name=" + name + ", major=" + major + ", minor=" + minor+ "");
  325. var idx = this.fs.CreateNode(name, this.fids[fid].inodeid, major, minor);
  326. var inode = this.fs.GetInode(idx);
  327. inode.mode = mode;
  328. inode.uid = this.fids[fid].uid;
  329. inode.gid = gid;
  330. marshall.Marshall(["Q"], [inode.qid], this.replybuffer, 7);
  331. this.BuildReply(id, tag, 13);
  332. this.SendReply(bufchain);
  333. break;
  334. case 22: // TREADLINK
  335. var req = marshall.Unmarshall(["w"], buffer, state);
  336. var fid = req[0];
  337. var inode = this.fs.GetInode(this.fids[fid].inodeid);
  338. message.Debug("[readlink] fid=" + fid + " name=" + this.fids[fid].dbg_name + " target=" + inode.symlink);
  339. size = marshall.Marshall(["s"], [inode.symlink], this.replybuffer, 7);
  340. this.BuildReply(id, tag, size);
  341. this.SendReply(bufchain);
  342. break;
  343. case 72: // tmkdir
  344. var req = marshall.Unmarshall(["w", "s", "w", "w"], buffer, state);
  345. var fid = req[0];
  346. var name = req[1];
  347. var mode = req[2];
  348. var gid = req[3];
  349. message.Debug("[mkdir] fid=" + fid + ", name=" + name + ", mode=" + mode + ", gid=" + gid);
  350. var idx = this.fs.CreateDirectory(name, this.fids[fid].inodeid);
  351. var inode = this.fs.GetInode(idx);
  352. inode.mode = mode | S_IFDIR;
  353. inode.uid = this.fids[fid].uid;
  354. inode.gid = gid;
  355. marshall.Marshall(["Q"], [inode.qid], this.replybuffer, 7);
  356. this.BuildReply(id, tag, 13);
  357. this.SendReply(bufchain);
  358. break;
  359. case 14: // tlcreate
  360. var req = marshall.Unmarshall(["w", "s", "w", "w", "w"], buffer, state);
  361. var fid = req[0];
  362. var name = req[1];
  363. var flags = req[2];
  364. var mode = req[3];
  365. var gid = req[4];
  366. this.bus.send("9p-create", [name, this.fids[fid].inodeid]);
  367. message.Debug("[create] fid=" + fid + ", name=" + name + ", flags=" + flags + ", mode=" + mode + ", gid=" + gid);
  368. var idx = this.fs.CreateFile(name, this.fids[fid].inodeid);
  369. this.fids[fid].inodeid = idx;
  370. this.fids[fid].type = FID_INODE;
  371. this.fids[fid].dbg_name = name;
  372. var inode = this.fs.GetInode(idx);
  373. inode.uid = this.fids[fid].uid;
  374. inode.gid = gid;
  375. inode.mode = mode;
  376. marshall.Marshall(["Q", "w"], [inode.qid, this.msize - 24], this.replybuffer, 7);
  377. this.BuildReply(id, tag, 13+4);
  378. this.SendReply(bufchain);
  379. break;
  380. case 52: // lock
  381. var req = marshall.Unmarshall(["w", "b", "w", "d", "d", "w", "s"], buffer, state);
  382. var fid = req[0];
  383. var flags = req[2];
  384. var lock_length = req[4] === 0 ? Infinity : req[4];
  385. var lock_request = this.fs.DescribeLock(req[1], req[3], lock_length, req[5], req[6]);
  386. message.Debug("[lock] fid=" + fid +
  387. ", type=" + P9_LOCK_TYPES[lock_request.type] + ", start=" + lock_request.start +
  388. ", length=" + lock_request.length + ", proc_id=" + lock_request.proc_id);
  389. var ret = this.fs.Lock(this.fids[fid].inodeid, lock_request, flags);
  390. marshall.Marshall(["b"], [ret], this.replybuffer, 7);
  391. this.BuildReply(id, tag, 1);
  392. this.SendReply(bufchain);
  393. break;
  394. case 54: // getlock
  395. var req = marshall.Unmarshall(["w", "b", "d", "d", "w", "s"], buffer, state);
  396. var fid = req[0];
  397. var lock_length = req[3] === 0 ? Infinity : req[3];
  398. var lock_request = this.fs.DescribeLock(req[1], req[2], lock_length, req[4], req[5]);
  399. message.Debug("[getlock] fid=" + fid +
  400. ", type=" + P9_LOCK_TYPES[lock_request.type] + ", start=" + lock_request.start +
  401. ", length=" + lock_request.length + ", proc_id=" + lock_request.proc_id);
  402. var ret = this.fs.GetLock(this.fids[fid].inodeid, lock_request);
  403. if(!ret)
  404. {
  405. ret = lock_request;
  406. ret.type = P9_LOCK_TYPE_UNLCK;
  407. }
  408. var ret_length = ret.length === Infinity ? 0 : ret.length;
  409. size = marshall.Marshall(["b", "d", "d", "w", "s"],
  410. [ret.type, ret.start, ret_length, ret.proc_id, ret.client_id],
  411. this.replybuffer, 7);
  412. this.BuildReply(id, tag, size);
  413. this.SendReply(bufchain);
  414. break;
  415. case 24: // getattr
  416. var req = marshall.Unmarshall(["w", "d"], buffer, state);
  417. var fid = req[0];
  418. var inode = this.fs.GetInode(this.fids[fid].inodeid);
  419. message.Debug("[getattr]: fid=" + fid + " name=" + this.fids[fid].dbg_name + " request mask=" + req[1]);
  420. if(!inode || inode.status === STATUS_UNLINKED)
  421. {
  422. message.Debug("getattr: unlinked");
  423. this.SendError(tag, "No such file or directory", ENOENT);
  424. this.SendReply(bufchain);
  425. break;
  426. }
  427. req[0] |= 0x1000; // P9_STATS_GEN
  428. req[0] = req[1]; // request mask
  429. req[1] = inode.qid;
  430. req[2] = inode.mode;
  431. req[3] = inode.uid; // user id
  432. req[4] = inode.gid; // group id
  433. req[5] = inode.nlinks; // number of hard links
  434. req[6] = (inode.major<<8) | (inode.minor); // device id low
  435. req[7] = inode.size; // size low
  436. req[8] = this.BLOCKSIZE;
  437. req[9] = Math.floor(inode.size/512+1); // blk size low
  438. req[10] = inode.atime; // atime
  439. req[11] = 0x0;
  440. req[12] = inode.mtime; // mtime
  441. req[13] = 0x0;
  442. req[14] = inode.ctime; // ctime
  443. req[15] = 0x0;
  444. req[16] = 0x0; // btime
  445. req[17] = 0x0;
  446. req[18] = 0x0; // st_gen
  447. req[19] = 0x0; // data_version
  448. marshall.Marshall([
  449. "d", "Q",
  450. "w",
  451. "w", "w",
  452. "d", "d",
  453. "d", "d", "d",
  454. "d", "d", // atime
  455. "d", "d", // mtime
  456. "d", "d", // ctime
  457. "d", "d", // btime
  458. "d", "d",
  459. ], req, this.replybuffer, 7);
  460. this.BuildReply(id, tag, 8 + 13 + 4 + 4+ 4 + 8*15);
  461. this.SendReply(bufchain);
  462. break;
  463. case 26: // setattr
  464. var req = marshall.Unmarshall(["w", "w",
  465. "w", // mode
  466. "w", "w", // uid, gid
  467. "d", // size
  468. "d", "d", // atime
  469. "d", "d", // mtime
  470. ], buffer, state);
  471. var fid = req[0];
  472. var inode = this.fs.GetInode(this.fids[fid].inodeid);
  473. message.Debug("[setattr]: fid=" + fid + " request mask=" + req[1] + " name=" + this.fids[fid].dbg_name);
  474. if (req[1] & P9_SETATTR_MODE) {
  475. inode.mode = req[2];
  476. }
  477. if (req[1] & P9_SETATTR_UID) {
  478. inode.uid = req[3];
  479. }
  480. if (req[1] & P9_SETATTR_GID) {
  481. inode.gid = req[4];
  482. }
  483. if (req[1] & P9_SETATTR_ATIME) {
  484. inode.atime = Math.floor((new Date()).getTime()/1000);
  485. }
  486. if (req[1] & P9_SETATTR_MTIME) {
  487. inode.mtime = Math.floor((new Date()).getTime()/1000);
  488. }
  489. if (req[1] & P9_SETATTR_CTIME) {
  490. inode.ctime = Math.floor((new Date()).getTime()/1000);
  491. }
  492. if (req[1] & P9_SETATTR_ATIME_SET) {
  493. inode.atime = req[6];
  494. }
  495. if (req[1] & P9_SETATTR_MTIME_SET) {
  496. inode.mtime = req[8];
  497. }
  498. if (req[1] & P9_SETATTR_SIZE) {
  499. await this.fs.ChangeSize(this.fids[fid].inodeid, req[5]);
  500. }
  501. this.BuildReply(id, tag, 0);
  502. this.SendReply(bufchain);
  503. break;
  504. case 50: // fsync
  505. var req = marshall.Unmarshall(["w", "d"], buffer, state);
  506. var fid = req[0];
  507. this.BuildReply(id, tag, 0);
  508. this.SendReply(bufchain);
  509. break;
  510. case 40: // TREADDIR
  511. case 116: // read
  512. var req = marshall.Unmarshall(["w", "d", "w"], buffer, state);
  513. var fid = req[0];
  514. var offset = req[1];
  515. var count = req[2];
  516. var inode = this.fs.GetInode(this.fids[fid].inodeid);
  517. if (id == 40) message.Debug("[treaddir]: fid=" + fid + " offset=" + offset + " count=" + count);
  518. if (id == 116) message.Debug("[read]: fid=" + fid + " (" + this.fids[fid].dbg_name + ") offset=" + offset + " count=" + count + " fidtype=" + this.fids[fid].type);
  519. if(!inode || inode.status === STATUS_UNLINKED)
  520. {
  521. message.Debug("read/treaddir: unlinked");
  522. this.SendError(tag, "No such file or directory", ENOENT);
  523. this.SendReply(bufchain);
  524. break;
  525. }
  526. if (this.fids[fid].type == FID_XATTR) {
  527. if (inode.caps.length < offset+count) count = inode.caps.length - offset;
  528. for(var i=0; i<count; i++)
  529. this.replybuffer[7+4+i] = inode.caps[offset+i];
  530. marshall.Marshall(["w"], [count], this.replybuffer, 7);
  531. this.BuildReply(id, tag, 4 + count);
  532. this.SendReply(bufchain);
  533. } else {
  534. this.fs.OpenInode(this.fids[fid].inodeid, undefined);
  535. const inodeid = this.fids[fid].inodeid;
  536. count = Math.min(count, this.replybuffer.length - (7 + 4));
  537. if (inode.size < offset+count) count = inode.size - offset;
  538. else if(id == 40)
  539. {
  540. // for directories, return whole number of dir-entries.
  541. count = this.fs.RoundToDirentry(inodeid, offset + count) - offset;
  542. }
  543. if(offset > inode.size)
  544. {
  545. // offset can be greater than available - should return count of zero.
  546. // See http://ericvh.github.io/9p-rfc/rfc9p2000.html#anchor30
  547. count = 0;
  548. }
  549. this.bus.send("9p-read-start", [this.fids[fid].dbg_name]);
  550. const data = await this.fs.Read(inodeid, offset, count);
  551. this.bus.send("9p-read-end", [this.fids[fid].dbg_name, count]);
  552. if(data) {
  553. this.replybuffer.set(data, 7 + 4);
  554. }
  555. marshall.Marshall(["w"], [count], this.replybuffer, 7);
  556. this.BuildReply(id, tag, 4 + count);
  557. this.SendReply(bufchain);
  558. }
  559. break;
  560. case 118: // write
  561. var req = marshall.Unmarshall(["w", "d", "w"], buffer, state);
  562. var fid = req[0];
  563. var offset = req[1];
  564. var count = req[2];
  565. const filename = this.fids[fid].dbg_name;
  566. message.Debug("[write]: fid=" + fid + " (" + filename + ") offset=" + offset + " count=" + count + " fidtype=" + this.fids[fid].type);
  567. if(this.fids[fid].type === FID_XATTR)
  568. {
  569. // XXX: xattr not supported yet. Ignore write.
  570. this.SendError(tag, "Setxattr not supported", EOPNOTSUPP);
  571. this.SendReply(bufchain);
  572. break;
  573. }
  574. else
  575. {
  576. // XXX: Size of the subarray is unchecked
  577. await this.fs.Write(this.fids[fid].inodeid, offset, count, buffer.subarray(state.offset));
  578. }
  579. this.bus.send("9p-write-end", [filename, count]);
  580. marshall.Marshall(["w"], [count], this.replybuffer, 7);
  581. this.BuildReply(id, tag, 4);
  582. this.SendReply(bufchain);
  583. break;
  584. case 74: // RENAMEAT
  585. var req = marshall.Unmarshall(["w", "s", "w", "s"], buffer, state);
  586. var olddirfid = req[0];
  587. var oldname = req[1];
  588. var newdirfid = req[2];
  589. var newname = req[3];
  590. message.Debug("[renameat]: oldname=" + oldname + " newname=" + newname);
  591. var ret = await this.fs.Rename(this.fids[olddirfid].inodeid, oldname, this.fids[newdirfid].inodeid, newname);
  592. if (ret < 0) {
  593. let error_message = "";
  594. if(ret === -ENOENT) error_message = "No such file or directory";
  595. else if(ret === -EPERM) error_message = "Operation not permitted";
  596. else if(ret === -ENOTEMPTY) error_message = "Directory not empty";
  597. else
  598. {
  599. error_message = "Unknown error: " + (-ret);
  600. dbg_assert(false, "[renameat]: Unexpected error code: " + (-ret));
  601. }
  602. this.SendError(tag, error_message, -ret);
  603. this.SendReply(bufchain);
  604. break;
  605. }
  606. if(TRACK_FILENAMES)
  607. {
  608. const newidx = this.fs.Search(this.fids[newdirfid].inodeid, newname);
  609. this.update_dbg_name(newidx, newname);
  610. }
  611. this.BuildReply(id, tag, 0);
  612. this.SendReply(bufchain);
  613. break;
  614. case 76: // TUNLINKAT
  615. var req = marshall.Unmarshall(["w", "s", "w"], buffer, state);
  616. var dirfd = req[0];
  617. var name = req[1];
  618. var flags = req[2];
  619. message.Debug("[unlink]: dirfd=" + dirfd + " name=" + name + " flags=" + flags);
  620. var fid = this.fs.Search(this.fids[dirfd].inodeid, name);
  621. if (fid == -1) {
  622. this.SendError(tag, "No such file or directory", ENOENT);
  623. this.SendReply(bufchain);
  624. break;
  625. }
  626. var ret = this.fs.Unlink(this.fids[dirfd].inodeid, name);
  627. if (ret < 0) {
  628. let error_message = "";
  629. if(ret === -ENOTEMPTY) error_message = "Directory not empty";
  630. else if(ret === -EPERM) error_message = "Operation not permitted";
  631. else
  632. {
  633. error_message = "Unknown error: " + (-ret);
  634. dbg_assert(false, "[unlink]: Unexpected error code: " + (-ret));
  635. }
  636. this.SendError(tag, error_message, -ret);
  637. this.SendReply(bufchain);
  638. break;
  639. }
  640. this.BuildReply(id, tag, 0);
  641. this.SendReply(bufchain);
  642. break;
  643. case 100: // version
  644. var version = marshall.Unmarshall(["w", "s"], buffer, state);
  645. message.Debug("[version]: msize=" + version[0] + " version=" + version[1]);
  646. this.msize = version[0];
  647. size = marshall.Marshall(["w", "s"], [this.msize, this.VERSION], this.replybuffer, 7);
  648. this.BuildReply(id, tag, size);
  649. this.SendReply(bufchain);
  650. break;
  651. case 104: // attach
  652. // return root directorie's QID
  653. var req = marshall.Unmarshall(["w", "w", "s", "s", "w"], buffer, state);
  654. var fid = req[0];
  655. var uid = req[4];
  656. message.Debug("[attach]: fid=" + fid + " afid=" + hex8(req[1]) + " uname=" + req[2] + " aname=" + req[3]);
  657. this.fids[fid] = this.Createfid(0, FID_INODE, uid, "");
  658. var inode = this.fs.GetInode(this.fids[fid].inodeid);
  659. marshall.Marshall(["Q"], [inode.qid], this.replybuffer, 7);
  660. this.BuildReply(id, tag, 13);
  661. this.SendReply(bufchain);
  662. this.bus.send("9p-attach");
  663. break;
  664. case 108: // tflush
  665. var req = marshall.Unmarshall(["h"], buffer, state);
  666. var oldtag = req[0];
  667. message.Debug("[flush] " + tag);
  668. //marshall.Marshall(["Q"], [inode.qid], this.replybuffer, 7);
  669. this.BuildReply(id, tag, 0);
  670. this.SendReply(bufchain);
  671. break;
  672. case 110: // walk
  673. var req = marshall.Unmarshall(["w", "w", "h"], buffer, state);
  674. var fid = req[0];
  675. var nwfid = req[1];
  676. var nwname = req[2];
  677. message.Debug("[walk]: fid=" + req[0] + " nwfid=" + req[1] + " nwname=" + nwname);
  678. if (nwname == 0) {
  679. this.fids[nwfid] = this.Createfid(this.fids[fid].inodeid, FID_INODE, this.fids[fid].uid, this.fids[fid].dbg_name);
  680. //this.fids[nwfid].inodeid = this.fids[fid].inodeid;
  681. marshall.Marshall(["h"], [0], this.replybuffer, 7);
  682. this.BuildReply(id, tag, 2);
  683. this.SendReply(bufchain);
  684. break;
  685. }
  686. var wnames = [];
  687. for(var i=0; i<nwname; i++) {
  688. wnames.push("s");
  689. }
  690. var walk = marshall.Unmarshall(wnames, buffer, state);
  691. var idx = this.fids[fid].inodeid;
  692. var offset = 7+2;
  693. var nwidx = 0;
  694. //console.log(idx, this.fs.GetInode(idx));
  695. message.Debug("walk in dir " + this.fids[fid].dbg_name + " to: " + walk.toString());
  696. for(var i=0; i<nwname; i++) {
  697. idx = this.fs.Search(idx, walk[i]);
  698. if (idx == -1) {
  699. message.Debug("Could not find: " + walk[i]);
  700. break;
  701. }
  702. offset += marshall.Marshall(["Q"], [this.fs.GetInode(idx).qid], this.replybuffer, offset);
  703. nwidx++;
  704. //message.Debug(this.fids[nwfid].inodeid);
  705. //this.fids[nwfid].inodeid = idx;
  706. //this.fids[nwfid].type = FID_INODE;
  707. this.fids[nwfid] = this.Createfid(idx, FID_INODE, this.fids[fid].uid, walk[i]);
  708. }
  709. marshall.Marshall(["h"], [nwidx], this.replybuffer, 7);
  710. this.BuildReply(id, tag, offset-7);
  711. this.SendReply(bufchain);
  712. break;
  713. case 120: // clunk
  714. var req = marshall.Unmarshall(["w"], buffer, state);
  715. message.Debug("[clunk]: fid=" + req[0]);
  716. if (this.fids[req[0]] && this.fids[req[0]].inodeid >= 0) {
  717. await this.fs.CloseInode(this.fids[req[0]].inodeid);
  718. this.fids[req[0]].inodeid = -1;
  719. this.fids[req[0]].type = FID_NONE;
  720. }
  721. this.BuildReply(id, tag, 0);
  722. this.SendReply(bufchain);
  723. break;
  724. case 32: // txattrcreate
  725. var req = marshall.Unmarshall(["w", "s", "d", "w"], buffer, state);
  726. var fid = req[0];
  727. var name = req[1];
  728. var attr_size = req[2];
  729. var flags = req[3];
  730. message.Debug("[txattrcreate]: fid=" + fid + " name=" + name + " attr_size=" + attr_size + " flags=" + flags);
  731. // XXX: xattr not supported yet. E.g. checks corresponding to the flags needed.
  732. this.fids[fid].type = FID_XATTR;
  733. this.BuildReply(id, tag, 0);
  734. this.SendReply(bufchain);
  735. //this.SendError(tag, "Operation i not supported", EINVAL);
  736. //this.SendReply(bufchain);
  737. break;
  738. case 30: // xattrwalk
  739. var req = marshall.Unmarshall(["w", "w", "s"], buffer, state);
  740. var fid = req[0];
  741. var newfid = req[1];
  742. var name = req[2];
  743. message.Debug("[xattrwalk]: fid=" + req[0] + " newfid=" + req[1] + " name=" + req[2]);
  744. // Workaround for Linux restarts writes until full blocksize
  745. this.SendError(tag, "Setxattr not supported", EOPNOTSUPP);
  746. this.SendReply(bufchain);
  747. /*
  748. this.fids[newfid] = this.Createfid(this.fids[fid].inodeid, FID_NONE, this.fids[fid].uid, this.fids[fid].dbg_name);
  749. //this.fids[newfid].inodeid = this.fids[fid].inodeid;
  750. //this.fids[newfid].type = FID_NONE;
  751. var length = 0;
  752. if (name == "security.capability") {
  753. length = this.fs.PrepareCAPs(this.fids[fid].inodeid);
  754. this.fids[newfid].type = FID_XATTR;
  755. }
  756. marshall.Marshall(["d"], [length], this.replybuffer, 7);
  757. this.BuildReply(id, tag, 8);
  758. this.SendReply(bufchain);
  759. */
  760. break;
  761. default:
  762. message.Debug("Error in Virtio9p: Unknown id " + id + " received");
  763. message.Abort();
  764. //this.SendError(tag, "Operation i not supported", EOPNOTSUPP);
  765. //this.SendReply(bufchain);
  766. break;
  767. }
  768. //consistency checks if there are problems with the filesystem
  769. //this.fs.Check();
  770. };