shim_handle.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_handle.c
  17. *
  18. * This file contains codes to maintain bookkeeping for handles in library OS.
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_thread.h>
  22. #include <shim_handle.h>
  23. #include <shim_checkpoint.h>
  24. #include <shim_fs.h>
  25. #include <pal.h>
  26. #include <pal_error.h>
  27. static LOCKTYPE handle_mgr_lock;
  28. #define HANDLE_MGR_ALLOC 32
  29. #define system_lock() lock(handle_mgr_lock)
  30. #define system_unlock() unlock(handle_mgr_lock)
  31. #define PAGE_SIZE allocsize
  32. #define OBJ_TYPE struct shim_handle
  33. #include <memmgr.h>
  34. static MEM_MGR handle_mgr = NULL;
  35. #define INIT_HANDLE_MAP_SIZE 32
  36. //#define DEBUG_REF
  37. static inline int init_tty_handle (struct shim_handle * hdl, bool write)
  38. {
  39. struct shim_dentry * dent = NULL;
  40. int ret;
  41. if ((ret = path_lookupat(NULL, "/dev/tty", LOOKUP_OPEN, &dent)) < 0)
  42. return ret;
  43. int flags = (write ? O_WRONLY : O_RDONLY)|O_APPEND;
  44. struct shim_mount * fs = dent->fs;
  45. ret = fs->d_ops->open(hdl, dent, flags);
  46. if (ret < 0)
  47. return ret;
  48. set_handle_fs(hdl, fs);
  49. hdl->dentry = dent;
  50. hdl->flags = O_RDWR|O_APPEND|0100000;
  51. int size;
  52. char * path = dentry_get_path(dent, true, &size);
  53. if (path)
  54. qstrsetstr(&hdl->path, path, size);
  55. else
  56. qstrsetstr(&hdl->path, "/dev/tty", 8);
  57. return 0;
  58. }
  59. static inline int init_exec_handle (struct shim_thread * thread)
  60. {
  61. if (!PAL_CB(executable))
  62. return 0;
  63. struct shim_handle * exec = get_new_handle();
  64. if (!exec)
  65. return -ENOMEM;
  66. qstrsetstr(&exec->uri, PAL_CB(executable), strlen(PAL_CB(executable)));
  67. exec->type = TYPE_FILE;
  68. exec->flags = O_RDONLY;
  69. exec->acc_mode = MAY_READ;
  70. struct shim_mount * fs = find_mount_from_uri(PAL_CB(executable));
  71. if (fs) {
  72. path_lookupat(fs->root, PAL_CB(executable) + fs->uri.len, 0,
  73. &exec->dentry);
  74. set_handle_fs(exec, fs);
  75. if (exec->dentry) {
  76. int len;
  77. const char * path = dentry_get_path(exec->dentry, true, &len);
  78. qstrsetstr(&exec->path, path, len);
  79. }
  80. put_mount(fs);
  81. } else {
  82. set_handle_fs(exec, &chroot_builtin_fs);
  83. }
  84. lock(thread->lock);
  85. thread->exec = exec;
  86. unlock(thread->lock);
  87. return 0;
  88. }
  89. static struct shim_handle_map * get_new_handle_map (FDTYPE size);
  90. PAL_HANDLE shim_stdio = NULL;
  91. static int __set_new_fd_handle(struct shim_fd_handle ** fdhdl, FDTYPE fd,
  92. struct shim_handle * hdl, int flags);
  93. static struct shim_handle_map * __enlarge_handle_map
  94. (struct shim_handle_map * map, FDTYPE size);
  95. int init_handle (void)
  96. {
  97. create_lock(handle_mgr_lock);
  98. handle_mgr = create_mem_mgr(init_align_up(HANDLE_MGR_ALLOC));
  99. if (!handle_mgr)
  100. return -ENOMEM;
  101. return 0;
  102. }
  103. int init_important_handles (void)
  104. {
  105. struct shim_thread * thread = get_cur_thread();
  106. if (thread->handle_map)
  107. goto done;
  108. struct shim_handle_map * handle_map = get_cur_handle_map(thread);
  109. if (!handle_map) {
  110. handle_map = get_new_handle_map(INIT_HANDLE_MAP_SIZE);
  111. if (!handle_map)
  112. return -ENOMEM;
  113. set_handle_map(thread, handle_map);
  114. }
  115. lock(handle_map->lock);
  116. if (handle_map->fd_size < 3) {
  117. if (!__enlarge_handle_map(handle_map, INIT_HANDLE_MAP_SIZE)) {
  118. unlock(handle_map->lock);
  119. return -ENOMEM;
  120. }
  121. }
  122. struct shim_handle * hdl = NULL;
  123. int ret;
  124. for (int fd = 0 ; fd < 3 ; fd++)
  125. if (!HANDLE_ALLOCATED(handle_map->map[fd])) {
  126. if (!hdl) {
  127. hdl = get_new_handle();
  128. if (!hdl)
  129. return -ENOMEM;
  130. if ((ret = init_tty_handle(hdl, fd)) < 0) {
  131. put_handle(hdl);
  132. return ret;
  133. }
  134. } else {
  135. get_handle(hdl);
  136. }
  137. __set_new_fd_handle(&handle_map->map[fd], fd, hdl, 0);
  138. put_handle(hdl);
  139. if (fd != 1)
  140. hdl = NULL;
  141. } else {
  142. if (fd == 1)
  143. hdl = handle_map->map[fd]->handle;
  144. }
  145. if (handle_map->fd_top == FD_NULL || handle_map->fd_top < 2)
  146. handle_map->fd_top = 2;
  147. unlock(handle_map->lock);
  148. done:
  149. init_exec_handle(thread);
  150. return 0;
  151. }
  152. struct shim_handle * __get_fd_handle (FDTYPE fd, int * flags,
  153. struct shim_handle_map * map)
  154. {
  155. struct shim_fd_handle * fd_handle = NULL;
  156. if (map->fd_top != FD_NULL &&
  157. fd <= map->fd_top) {
  158. fd_handle = map->map[fd];
  159. if (!HANDLE_ALLOCATED(fd_handle))
  160. return NULL;
  161. if (flags)
  162. *flags = fd_handle->flags;
  163. return fd_handle->handle;
  164. }
  165. return NULL;
  166. }
  167. struct shim_handle * get_fd_handle (FDTYPE fd, int * flags,
  168. struct shim_handle_map * map)
  169. {
  170. if (!map)
  171. map = get_cur_handle_map(NULL);
  172. struct shim_handle * hdl = NULL;
  173. lock(map->lock);
  174. if ((hdl = __get_fd_handle(fd, flags, map)))
  175. get_handle(hdl);
  176. unlock(map->lock);
  177. return hdl;
  178. }
  179. struct shim_handle *
  180. __detach_fd_handle (struct shim_fd_handle * fd, int * flags,
  181. struct shim_handle_map * map)
  182. {
  183. struct shim_handle * handle = NULL;
  184. if (HANDLE_ALLOCATED(fd)) {
  185. int vfd = fd->vfd;
  186. handle = fd->handle;
  187. if (flags)
  188. *flags = fd->flags;
  189. fd->vfd = FD_NULL;
  190. fd->handle = NULL;
  191. fd->flags = 0;
  192. if (vfd == map->fd_top)
  193. do {
  194. map->fd_top = vfd ? vfd - 1 : FD_NULL;
  195. vfd--;
  196. } while (vfd >= 0 &&
  197. !HANDLE_ALLOCATED(map->map[vfd]));
  198. }
  199. return handle;
  200. }
  201. struct shim_handle * detach_fd_handle (FDTYPE fd, int * flags,
  202. struct shim_handle_map * handle_map)
  203. {
  204. struct shim_handle * handle = NULL;
  205. if (!handle_map && !(handle_map = get_cur_handle_map(NULL)))
  206. return NULL;
  207. lock(handle_map->lock);
  208. if (fd < handle_map->fd_size)
  209. handle = __detach_fd_handle(handle_map->map[fd], flags,
  210. handle_map);
  211. unlock(handle_map->lock);
  212. return handle;
  213. }
  214. struct shim_handle * get_new_handle (void)
  215. {
  216. struct shim_handle * new_handle =
  217. get_mem_obj_from_mgr_enlarge(handle_mgr,
  218. size_align_up(HANDLE_MGR_ALLOC));
  219. if (!new_handle)
  220. return NULL;
  221. memset(new_handle, 0, sizeof(struct shim_handle));
  222. REF_SET(new_handle->ref_count, 1);
  223. create_lock(new_handle->lock);
  224. new_handle->owner = cur_process.vmid;
  225. return new_handle;
  226. }
  227. static int __set_new_fd_handle(struct shim_fd_handle ** fdhdl, FDTYPE fd,
  228. struct shim_handle * hdl, int flags)
  229. {
  230. struct shim_fd_handle * new_handle = *fdhdl;
  231. if (!new_handle) {
  232. new_handle = malloc(sizeof(struct shim_fd_handle));
  233. if (!new_handle)
  234. return -ENOMEM;
  235. *fdhdl = new_handle;
  236. }
  237. new_handle->vfd = fd;
  238. new_handle->flags = flags;
  239. open_handle(hdl);
  240. new_handle->handle = hdl;
  241. return 0;
  242. }
  243. int set_new_fd_handle (struct shim_handle * hdl, int flags,
  244. struct shim_handle_map * handle_map)
  245. {
  246. FDTYPE fd = 0;
  247. int new_size = 0;
  248. int ret = 0;
  249. if (!handle_map && !(handle_map = get_cur_handle_map(NULL)))
  250. return -EBADF;
  251. lock(handle_map->lock);
  252. if (!handle_map->map ||
  253. handle_map->fd_size < INIT_HANDLE_MAP_SIZE)
  254. new_size = INIT_HANDLE_MAP_SIZE;
  255. if (!handle_map->map)
  256. goto extend;
  257. if (handle_map->fd_top != FD_NULL)
  258. do {
  259. ++fd;
  260. if (fd == handle_map->fd_size) {
  261. new_size = handle_map->fd_size < new_size ? new_size :
  262. handle_map->fd_size * 2;
  263. extend:
  264. if (!__enlarge_handle_map(handle_map, new_size)) {
  265. ret = -ENOMEM;
  266. goto out;
  267. }
  268. }
  269. } while (handle_map->fd_top != FD_NULL &&
  270. fd <= handle_map->fd_top &&
  271. HANDLE_ALLOCATED(handle_map->map[fd]));
  272. if (handle_map->fd_top == FD_NULL ||
  273. fd > handle_map->fd_top)
  274. handle_map->fd_top = fd;
  275. ret = __set_new_fd_handle(&handle_map->map[fd], fd, hdl, flags);
  276. if (ret < 0) {
  277. if (fd == handle_map->fd_top)
  278. handle_map->fd_top = fd ? fd - 1 : FD_NULL;
  279. } else
  280. ret = fd;
  281. out:
  282. unlock(handle_map->lock);
  283. return ret;
  284. }
  285. int set_new_fd_handle_by_fd (FDTYPE fd, struct shim_handle * hdl, int flags,
  286. struct shim_handle_map * handle_map)
  287. {
  288. int new_size = 0;
  289. int ret = 0;
  290. if (!handle_map && !(handle_map = get_cur_handle_map(NULL)))
  291. return -EBADF;
  292. lock(handle_map->lock);
  293. if (!handle_map->map ||
  294. handle_map->fd_size < INIT_HANDLE_MAP_SIZE)
  295. new_size = INIT_HANDLE_MAP_SIZE;
  296. if (!handle_map->map)
  297. goto extend;
  298. if (fd >= handle_map->fd_size) {
  299. new_size = handle_map->fd_size < new_size ? new_size :
  300. handle_map->fd_size;
  301. extend:
  302. while (new_size <= fd)
  303. new_size *= 2;
  304. if (!__enlarge_handle_map(handle_map, new_size)) {
  305. ret = -ENOMEM;
  306. goto out;
  307. }
  308. }
  309. if (handle_map->fd_top != FD_NULL &&
  310. fd <= handle_map->fd_top &&
  311. HANDLE_ALLOCATED(handle_map->map[fd])) {
  312. ret = -EBADF;
  313. goto out;
  314. }
  315. if (handle_map->fd_top == FD_NULL ||
  316. fd > handle_map->fd_top)
  317. handle_map->fd_top = fd;
  318. struct shim_fd_handle * new_handle = handle_map->map[fd];
  319. if (!new_handle) {
  320. new_handle = malloc(sizeof(struct shim_fd_handle));
  321. if (!new_handle) {
  322. ret = -ENOMEM;
  323. goto out;
  324. }
  325. handle_map->map[fd] = new_handle;
  326. }
  327. ret = __set_new_fd_handle(&handle_map->map[fd], fd, hdl, flags);
  328. if (ret < 0) {
  329. if (fd == handle_map->fd_top)
  330. handle_map->fd_top = fd ? fd - 1 : FD_NULL;
  331. } else
  332. ret = fd;
  333. out:
  334. unlock(handle_map->lock);
  335. return fd;
  336. }
  337. void flush_handle (struct shim_handle * hdl)
  338. {
  339. if (hdl->fs && hdl->fs->fs_ops &&
  340. hdl->fs->fs_ops->flush)
  341. hdl->fs->fs_ops->flush(hdl);
  342. }
  343. static inline __attribute__((unused))
  344. const char * __handle_name (struct shim_handle * hdl)
  345. {
  346. if (!qstrempty(&hdl->path))
  347. return qstrgetstr(&hdl->path);
  348. if (!qstrempty(&hdl->uri))
  349. return qstrgetstr(&hdl->uri);
  350. if (hdl->fs_type[0])
  351. return hdl->fs_type;
  352. return "(unknown)";
  353. }
  354. void open_handle (struct shim_handle * hdl)
  355. {
  356. get_handle(hdl);
  357. #ifdef DEBUG_REF
  358. int opened = REF_INC(hdl->opened);
  359. debug("open handle %p(%s) (opened = %d)\n", hdl, __handle_name(hdl),
  360. opened);
  361. #else
  362. REF_INC(hdl->opened);
  363. #endif
  364. }
  365. void close_handle (struct shim_handle * hdl)
  366. {
  367. int opened = REF_DEC(hdl->opened);
  368. #ifdef DEBUG_REF
  369. debug("close handle %p(%s) (opened = %d)\n", hdl, __handle_name(hdl),
  370. opened);
  371. #endif
  372. if (!opened) {
  373. if (hdl->type == TYPE_DIR) {
  374. struct shim_dir_handle * dir = &hdl->info.dir;
  375. if (dir->dot) {
  376. put_dentry(dir->dot);
  377. dir->dot = NULL;
  378. }
  379. if (dir->dotdot) {
  380. put_dentry(dir->dotdot);
  381. dir->dotdot = NULL;
  382. }
  383. while (dir->ptr && *dir->ptr) {
  384. struct shim_dentry * dent = *dir->ptr;
  385. put_dentry(dent);
  386. *(dir->ptr++) = NULL;
  387. }
  388. } else {
  389. if (hdl->fs && hdl->fs->fs_ops &&
  390. hdl->fs->fs_ops->close)
  391. hdl->fs->fs_ops->close(hdl);
  392. }
  393. }
  394. put_handle(hdl);
  395. }
  396. void get_handle (struct shim_handle * hdl)
  397. {
  398. #ifdef DEBUG_REF
  399. int ref_count = REF_INC(hdl->ref_count);
  400. debug("get handle %p(%s) (ref_count = %d)\n", hdl, __handle_name(hdl),
  401. ref_count);
  402. #else
  403. REF_INC(hdl->ref_count);
  404. #endif
  405. }
  406. static void destroy_handle (struct shim_handle * hdl)
  407. {
  408. destroy_lock(hdl->lock);
  409. if (MEMORY_MIGRATED(hdl))
  410. memset(hdl, 0, sizeof(struct shim_handle));
  411. else
  412. free_mem_obj_to_mgr(handle_mgr, hdl);
  413. }
  414. void put_handle (struct shim_handle * hdl)
  415. {
  416. int ref_count = REF_DEC(hdl->ref_count);
  417. #ifdef DEBUG_REF
  418. debug("put handle %p(%s) (ref_count = %d)\n", hdl, __handle_name(hdl),
  419. ref_count);
  420. #endif
  421. if (!ref_count) {
  422. if (hdl->fs && hdl->fs->fs_ops &&
  423. hdl->fs->fs_ops->hput)
  424. hdl->fs->fs_ops->hput(hdl);
  425. qstrfree(&hdl->path);
  426. qstrfree(&hdl->uri);
  427. if (hdl->pal_handle)
  428. DkObjectClose(hdl->pal_handle);
  429. if (hdl->dentry)
  430. put_dentry(hdl->dentry);
  431. if (hdl->fs)
  432. put_mount(hdl->fs);
  433. destroy_handle(hdl);
  434. }
  435. }
  436. size_t get_file_size (struct shim_handle * hdl)
  437. {
  438. if (!hdl->fs || !hdl->fs->fs_ops)
  439. return -EINVAL;
  440. if (hdl->fs->fs_ops->poll)
  441. return hdl->fs->fs_ops->poll(hdl, FS_POLL_SZ);
  442. if (hdl->fs->fs_ops->hstat) {
  443. struct stat stat;
  444. int ret = hdl->fs->fs_ops->hstat(hdl, &stat);
  445. if (ret < 0)
  446. return ret;
  447. return stat.st_size;
  448. }
  449. return 0;
  450. }
  451. void dup_fd_handle (struct shim_handle_map * map,
  452. const struct shim_fd_handle * old,
  453. struct shim_fd_handle * new)
  454. {
  455. struct shim_handle * replaced = NULL;
  456. lock(map->lock);
  457. if (old->vfd != FD_NULL) {
  458. open_handle(old->handle);
  459. replaced = new->handle;
  460. new->handle = old->handle;
  461. }
  462. unlock(map->lock);
  463. if (replaced)
  464. close_handle(replaced);
  465. }
  466. static struct shim_handle_map * get_new_handle_map (FDTYPE size)
  467. {
  468. struct shim_handle_map * handle_map =
  469. malloc(sizeof(struct shim_handle_map));
  470. if (handle_map == NULL)
  471. return NULL;
  472. memset(handle_map, 0, sizeof(struct shim_handle_map));
  473. handle_map->map = malloc(sizeof(struct shim_fd_handle) * size);
  474. if (handle_map->map == NULL) {
  475. free(handle_map);
  476. return NULL;
  477. }
  478. memset(handle_map->map, 0,
  479. sizeof(struct shim_fd_handle) * size);
  480. handle_map->fd_top = FD_NULL;
  481. handle_map->fd_size = size;
  482. create_lock(handle_map->lock);
  483. return handle_map;
  484. }
  485. static struct shim_handle_map * __enlarge_handle_map
  486. (struct shim_handle_map * map, FDTYPE size)
  487. {
  488. if (size <= map->fd_size)
  489. return NULL;
  490. struct shim_fd_handle ** old_map = map->map;
  491. map->map = malloc(sizeof(struct shim_fd_handle *) * size);
  492. if (map->map == NULL) {
  493. map->map = old_map;
  494. return NULL;
  495. }
  496. size_t copy_size = sizeof(struct shim_fd_handle *) * map->fd_size;
  497. map->fd_size = size;
  498. if (old_map && copy_size)
  499. memcpy(map->map, old_map, copy_size);
  500. memset(&map->map[map->fd_size], 0,
  501. (sizeof(struct shim_fd_handle *) * size) - copy_size);
  502. if (old_map)
  503. free(old_map);
  504. return map;
  505. }
  506. int dup_handle_map (struct shim_handle_map ** new,
  507. struct shim_handle_map * old_map)
  508. {
  509. lock(old_map->lock);
  510. /* allocate a new handle mapping with the same size as
  511. the old one */
  512. struct shim_handle_map * new_map =
  513. get_new_handle_map(old_map->fd_size);
  514. new_map->fd_top = old_map->fd_top;
  515. if (old_map->fd_top == FD_NULL)
  516. goto done;
  517. for (int i = 0 ; i <= old_map->fd_top ; i++) {
  518. struct shim_fd_handle * fd_old = old_map->map[i];
  519. struct shim_fd_handle * fd_new;
  520. /* now we go through the handle map and reassign each
  521. of them being allocated */
  522. if (HANDLE_ALLOCATED(fd_old)) {
  523. /* first, get the handle to prevent it from being deleted */
  524. struct shim_handle * hdl = fd_old->handle;
  525. open_handle(hdl);
  526. /* DP: I assume we really need a deep copy of the handle map? */
  527. fd_new = malloc(sizeof(struct shim_fd_handle));
  528. new_map->map[i] = fd_new;
  529. fd_new->vfd = fd_old->vfd;
  530. fd_new->handle = hdl;
  531. fd_new->flags = fd_old->flags;
  532. }
  533. }
  534. done:
  535. unlock(old_map->lock);
  536. *new = new_map;
  537. return 0;
  538. }
  539. void get_handle_map (struct shim_handle_map * map)
  540. {
  541. REF_INC(map->ref_count);
  542. }
  543. void put_handle_map (struct shim_handle_map * map)
  544. {
  545. int ref_count = REF_DEC(map->ref_count);
  546. if (!ref_count) {
  547. if (map->fd_top == FD_NULL)
  548. goto done;
  549. for (int i = 0 ; i <= map->fd_top ; i++) {
  550. if (!map->map[i])
  551. continue;
  552. if (map->map[i]->vfd != FD_NULL) {
  553. struct shim_handle * handle = map->map[i]->handle;
  554. if (handle)
  555. close_handle(handle);
  556. }
  557. free(map->map[i]);
  558. }
  559. done:
  560. destroy_lock(map->lock);
  561. free(map->map);
  562. free(map);
  563. }
  564. }
  565. int flush_handle_map (struct shim_handle_map * map)
  566. {
  567. get_handle_map(map);
  568. lock(map->lock);
  569. if (map->fd_top == FD_NULL)
  570. goto done;
  571. /* now we go through the handle map and flush each handle */
  572. for (int i = 0 ; i <= map->fd_top ; i++) {
  573. if (!HANDLE_ALLOCATED(map->map[i]))
  574. continue;
  575. struct shim_handle * handle = map->map[i]->handle;
  576. if (handle)
  577. flush_handle(handle);
  578. }
  579. done:
  580. unlock(map->lock);
  581. put_handle_map(map);
  582. return 0;
  583. }
  584. int walk_handle_map (int (*callback) (struct shim_fd_handle *,
  585. struct shim_handle_map *, void *),
  586. struct shim_handle_map * map, void * arg)
  587. {
  588. int ret = 0;
  589. lock(map->lock);
  590. if (map->fd_top == FD_NULL)
  591. goto done;
  592. for (int i = 0 ; i <= map->fd_top ; i++) {
  593. if (!HANDLE_ALLOCATED(map->map[i]))
  594. continue;
  595. if ((ret = (*callback) (map->map[i], map, arg)) < 0)
  596. break;
  597. }
  598. done:
  599. unlock(map->lock);
  600. return ret;
  601. }
  602. BEGIN_CP_FUNC(handle)
  603. {
  604. assert(size == sizeof(struct shim_handle));
  605. struct shim_handle * hdl = (struct shim_handle *) obj;
  606. struct shim_handle * new_hdl = NULL;
  607. ptr_t off = GET_FROM_CP_MAP(obj);
  608. if (!off) {
  609. off = ADD_CP_OFFSET(sizeof(struct shim_handle));
  610. ADD_TO_CP_MAP(obj, off);
  611. new_hdl = (struct shim_handle *) (base + off);
  612. lock(hdl->lock);
  613. struct shim_mount * fs = hdl->fs;
  614. *new_hdl = *hdl;
  615. if (fs && fs->fs_ops && fs->fs_ops->checkout)
  616. fs->fs_ops->checkout(new_hdl);
  617. new_hdl->dentry = NULL;
  618. REF_SET(new_hdl->opened, 0);
  619. REF_SET(new_hdl->ref_count, 0);
  620. clear_lock(new_hdl->lock);
  621. DO_CP_IN_MEMBER(qstr, new_hdl, path);
  622. DO_CP_IN_MEMBER(qstr, new_hdl, uri);
  623. if (fs && hdl->dentry) {
  624. DO_CP_MEMBER(mount, hdl, new_hdl, fs);
  625. } else {
  626. new_hdl->fs = NULL;
  627. }
  628. if (hdl->dentry)
  629. DO_CP_MEMBER(dentry, hdl, new_hdl, dentry);
  630. if (new_hdl->pal_handle) {
  631. struct shim_palhdl_entry * entry;
  632. DO_CP(palhdl, hdl->pal_handle, &entry);
  633. entry->uri = &new_hdl->uri;
  634. entry->phandle = &new_hdl->pal_handle;
  635. }
  636. unlock(hdl->lock);
  637. ADD_CP_FUNC_ENTRY(off);
  638. } else {
  639. new_hdl = (struct shim_handle *) (base + off);
  640. }
  641. if (objp)
  642. *objp = (void *) new_hdl;
  643. }
  644. END_CP_FUNC(handle)
  645. BEGIN_RS_FUNC(handle)
  646. {
  647. struct shim_handle * hdl = (void *) (base + GET_CP_FUNC_ENTRY());
  648. CP_REBASE(hdl->fs);
  649. CP_REBASE(hdl->dentry);
  650. create_lock(hdl->lock);
  651. if (!hdl->fs) {
  652. assert(hdl->fs_type);
  653. search_builtin_fs(hdl->fs_type, &hdl->fs);
  654. if (!hdl->fs)
  655. return -EINVAL;
  656. }
  657. if (hdl->fs && hdl->fs->fs_ops &&
  658. hdl->fs->fs_ops->checkin)
  659. hdl->fs->fs_ops->checkin(hdl);
  660. DEBUG_RS("path=%s,type=%s,uri=%s,flags=%03o",
  661. qstrgetstr(&hdl->path), hdl->fs_type, qstrgetstr(&hdl->uri),
  662. hdl->flags);
  663. }
  664. END_RS_FUNC(handle)
  665. BEGIN_CP_FUNC(fd_handle)
  666. {
  667. assert(size == sizeof(struct shim_fd_handle));
  668. struct shim_fd_handle * fdhdl = (struct shim_fd_handle *) obj;
  669. struct shim_fd_handle * new_fdhdl = NULL;
  670. ptr_t off = ADD_CP_OFFSET(sizeof(struct shim_fd_handle));
  671. new_fdhdl = (struct shim_fd_handle *) (base + off);
  672. memcpy(new_fdhdl, fdhdl, sizeof(struct shim_fd_handle));
  673. DO_CP(handle, fdhdl->handle, &new_fdhdl->handle);
  674. ADD_CP_FUNC_ENTRY(off);
  675. if (objp)
  676. *objp = (void *) new_fdhdl;
  677. }
  678. END_CP_FUNC_NO_RS(fd_handle)
  679. BEGIN_CP_FUNC(handle_map)
  680. {
  681. assert(size >= sizeof(struct shim_handle_map));
  682. struct shim_handle_map * handle_map = (struct shim_handle_map *) obj;
  683. struct shim_handle_map * new_handle_map = NULL;
  684. struct shim_fd_handle ** ptr_array;
  685. lock(handle_map->lock);
  686. int fd_size = handle_map->fd_top != FD_NULL ?
  687. handle_map->fd_top + 1 : 0;
  688. size = sizeof(struct shim_handle_map) +
  689. (sizeof(struct shim_fd_handle *) * fd_size);
  690. ptr_t off = GET_FROM_CP_MAP(obj);
  691. if (!off) {
  692. off = ADD_CP_OFFSET(size);
  693. new_handle_map = (struct shim_handle_map *) (base + off);
  694. memcpy(new_handle_map, handle_map,
  695. sizeof(struct shim_handle_map));
  696. ptr_array = (void *) new_handle_map + sizeof(struct shim_handle_map);
  697. new_handle_map->fd_size = fd_size;
  698. new_handle_map->map = fd_size ? ptr_array : NULL;
  699. REF_SET(new_handle_map->ref_count, 0);
  700. clear_lock(new_handle_map->lock);
  701. for (int i = 0 ; i < fd_size ; i++) {
  702. if (HANDLE_ALLOCATED(handle_map->map[i]))
  703. DO_CP(fd_handle, handle_map->map[i], &ptr_array[i]);
  704. else
  705. ptr_array[i] = NULL;
  706. }
  707. ADD_CP_FUNC_ENTRY(off);
  708. } else {
  709. new_handle_map = (struct shim_handle_map *) (base + off);
  710. }
  711. unlock(handle_map->lock);
  712. if (objp)
  713. *objp = (void *) new_handle_map;
  714. }
  715. END_CP_FUNC(handle_map)
  716. BEGIN_RS_FUNC(handle_map)
  717. {
  718. struct shim_handle_map * handle_map = (void *) (base + GET_CP_FUNC_ENTRY());
  719. CP_REBASE(handle_map->map);
  720. assert(handle_map->map);
  721. DEBUG_RS("size=%d,top=%d", handle_map->fd_size, handle_map->fd_top);
  722. create_lock(handle_map->lock);
  723. lock(handle_map->lock);
  724. if (handle_map->fd_top != FD_NULL)
  725. for (int i = 0 ; i <= handle_map->fd_top ; i++) {
  726. CP_REBASE(handle_map->map[i]);
  727. if (HANDLE_ALLOCATED(handle_map->map[i])) {
  728. CP_REBASE(handle_map->map[i]->handle);
  729. struct shim_handle * hdl = handle_map->map[i]->handle;
  730. assert(hdl);
  731. open_handle(hdl);
  732. DEBUG_RS("[%d]%s", i, qstrempty(&hdl->uri) ? hdl->fs_type :
  733. qstrgetstr(&hdl->uri));
  734. }
  735. }
  736. unlock(handle_map->lock);
  737. }
  738. END_RS_FUNC(handle_map)