db_files.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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 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 Lesser 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 Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * db_files.c
  17. *
  18. * This file contains operands to handle streams with URIs that start with
  19. * "file:" or "dir:".
  20. */
  21. #include "pal_defs.h"
  22. #include "pal_linux_defs.h"
  23. #include "pal.h"
  24. #include "pal_internal.h"
  25. #include "pal_linux.h"
  26. #include "pal_debug.h"
  27. #include "pal_error.h"
  28. #include "api.h"
  29. #include <linux/types.h>
  30. typedef __kernel_pid_t pid_t;
  31. #undef __GLIBC__
  32. #include <linux/stat.h>
  33. #include <asm/stat.h>
  34. #include <asm/fcntl.h>
  35. #include <asm/errno.h>
  36. /* 'open' operation for file streams */
  37. static int file_open (PAL_HANDLE * handle, const char * type, const char * uri,
  38. int access, int share, int create, int options)
  39. {
  40. /* try to do the real open */
  41. int ret = INLINE_SYSCALL(open, 3, uri,
  42. HOST_ACCESS(access)|create|options|O_CLOEXEC,
  43. share);
  44. if (IS_ERR(ret))
  45. return unix_to_pal_error(ERRNO(ret));
  46. /* if try_create_path succeeded, prepare for the file handle */
  47. int len = strlen(uri);
  48. PAL_HANDLE hdl = malloc(HANDLE_SIZE(file) + len + 1);
  49. SET_HANDLE_TYPE(hdl, file);
  50. HANDLE_HDR(hdl)->flags |= RFD(0)|WFD(0)|WRITEABLE(0);
  51. hdl->file.fd = ret;
  52. hdl->file.offset = 0;
  53. hdl->file.append = 0;
  54. hdl->file.pass = 0;
  55. char * path = (void *) hdl + HANDLE_SIZE(file);
  56. memcpy(path, uri, len + 1);
  57. hdl->file.realpath = (PAL_STR) path;
  58. *handle = hdl;
  59. return 0;
  60. }
  61. #ifndef SEEK_SET
  62. # define SEEK_SET 0
  63. #endif
  64. /* 'read' operation for file streams. */
  65. static int file_read (PAL_HANDLE handle, int offset, int count,
  66. void * buffer)
  67. {
  68. int fd = handle->file.fd;
  69. int ret;
  70. if (handle->file.offset != offset) {
  71. ret = INLINE_SYSCALL(lseek, 3, fd, offset, SEEK_SET);
  72. if (IS_ERR(ret))
  73. return -PAL_ERROR_DENIED;
  74. handle->file.offset = offset;
  75. }
  76. ret = INLINE_SYSCALL(read, 3, fd, buffer, count);
  77. if (IS_ERR(ret))
  78. return unix_to_pal_error(ERRNO(ret));
  79. handle->file.offset = offset + ret;
  80. return ret;
  81. }
  82. /* 'write' operation for file streams. */
  83. static int file_write (PAL_HANDLE handle, int offset, int count,
  84. const void * buffer)
  85. {
  86. int fd = handle->file.fd;
  87. int ret;
  88. if (handle->file.offset != offset) {
  89. ret = INLINE_SYSCALL(lseek, 3, fd, offset, SEEK_SET);
  90. if (IS_ERR(ret))
  91. return -PAL_ERROR_DENIED;
  92. handle->file.offset = offset;
  93. }
  94. ret = INLINE_SYSCALL(write, 3, fd, buffer, count);
  95. if (IS_ERR(ret))
  96. return unix_to_pal_error(ERRNO(ret));
  97. handle->file.offset = offset + ret;
  98. return ret;
  99. }
  100. /* 'close' operation for file streams. In this case, it will only
  101. close the file withou deleting it. */
  102. static int file_close (PAL_HANDLE handle)
  103. {
  104. int fd = handle->file.fd;
  105. int ret = INLINE_SYSCALL(close, 1, fd);
  106. if (handle->file.realpath &&
  107. handle->file.realpath != (void *) handle + HANDLE_SIZE(file))
  108. free((void *) handle->file.realpath);
  109. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : 0;
  110. }
  111. /* 'delete' operation for file streams. It will actually delete
  112. the file if we can successfully close it. */
  113. static int file_delete (PAL_HANDLE handle, int access)
  114. {
  115. if (access)
  116. return -PAL_ERROR_INVAL;
  117. INLINE_SYSCALL(unlink, 1, handle->file.realpath);
  118. return 0;
  119. }
  120. /* 'map' operation for file stream. */
  121. static int file_map (PAL_HANDLE handle, void ** addr, int prot,
  122. uint64_t offset, uint64_t size)
  123. {
  124. int fd = handle->file.fd;
  125. void * mem = *addr;
  126. int flags = MAP_FILE|HOST_FLAGS(0, prot)|(mem ? MAP_FIXED : 0);
  127. prot = HOST_PROT(prot);
  128. /* The memory will always allocated with flag MAP_PRIVATE
  129. and MAP_FILE */
  130. mem = (void *) ARCH_MMAP(mem, size, prot, flags, fd, offset);
  131. if (IS_ERR_P(mem))
  132. return -PAL_ERROR_DENIED;
  133. *addr = mem;
  134. return 0;
  135. }
  136. /* 'setlength' operation for file stream. */
  137. static int64_t file_setlength (PAL_HANDLE handle, uint64_t length)
  138. {
  139. int ret = INLINE_SYSCALL(ftruncate, 2, handle->file.fd, length);
  140. if (IS_ERR(ret))
  141. return (ERRNO(ret) == EINVAL || ERRNO(ret) == EBADF) ?
  142. -PAL_ERROR_BADHANDLE : -PAL_ERROR_DENIED;
  143. return (int64_t) length;
  144. }
  145. /* 'flush' operation for file stream. */
  146. static int file_flush (PAL_HANDLE handle)
  147. {
  148. int ret = INLINE_SYSCALL(fsync, 1, handle->file.fd);
  149. if (IS_ERR(ret))
  150. return (ERRNO(ret) == EINVAL || ERRNO(ret) == EBADF) ?
  151. -PAL_ERROR_BADHANDLE : -PAL_ERROR_DENIED;
  152. return 0;
  153. }
  154. static inline int file_stat_type (struct stat * stat)
  155. {
  156. if (S_ISREG(stat->st_mode))
  157. return pal_type_file;
  158. if (S_ISDIR(stat->st_mode))
  159. return pal_type_dir;
  160. if (S_ISCHR(stat->st_mode))
  161. return pal_type_dev;
  162. if (S_ISFIFO(stat->st_mode))
  163. return pal_type_pipe;
  164. if (S_ISSOCK(stat->st_mode))
  165. return pal_type_dev;
  166. return 0;
  167. }
  168. /* copy attr content from POSIX stat struct to PAL_STREAM_ATTR */
  169. static inline void
  170. file_attrcopy (PAL_STREAM_ATTR * attr, struct stat * stat)
  171. {
  172. attr->handle_type = file_stat_type(stat);
  173. attr->disconnected = PAL_FALSE;
  174. attr->nonblocking = PAL_FALSE;
  175. attr->readable = stataccess(stat, ACCESS_R);
  176. attr->writeable = stataccess(stat, ACCESS_W);
  177. attr->runnable = stataccess(stat, ACCESS_X);
  178. attr->share_flags = stat->st_mode;
  179. attr->pending_size = stat->st_size;
  180. }
  181. /* 'attrquery' operation for file streams */
  182. static int file_attrquery (const char * type, const char * uri,
  183. PAL_STREAM_ATTR * attr)
  184. {
  185. struct stat stat_buf;
  186. /* try to do the real open */
  187. int ret = INLINE_SYSCALL(stat, 2, uri, &stat_buf);
  188. /* if it failed, return the right error code */
  189. if (IS_ERR(ret))
  190. return unix_to_pal_error(ERRNO(ret));
  191. file_attrcopy(attr, &stat_buf);
  192. return 0;
  193. }
  194. /* 'attrquerybyhdl' operation for file streams */
  195. static int file_attrquerybyhdl (PAL_HANDLE handle,
  196. PAL_STREAM_ATTR * attr)
  197. {
  198. int fd = handle->generic.fds[0];
  199. struct stat stat_buf;
  200. int ret = INLINE_SYSCALL(fstat, 2, fd, &stat_buf);
  201. if (IS_ERR(ret))
  202. return unix_to_pal_error(ERRNO(ret));
  203. file_attrcopy(attr, &stat_buf);
  204. return 0;
  205. }
  206. static int file_attrsetbyhdl (PAL_HANDLE handle,
  207. PAL_STREAM_ATTR * attr)
  208. {
  209. int fd = handle->generic.fds[0], ret;
  210. ret = INLINE_SYSCALL(fchmod, 2, fd, attr->share_flags | 0600);
  211. if (IS_ERR(ret))
  212. return unix_to_pal_error(ERRNO(ret));
  213. return 0;
  214. }
  215. static int file_rename (PAL_HANDLE handle, const char * type,
  216. const char * uri)
  217. {
  218. int ret = INLINE_SYSCALL(rename, 2, handle->file.realpath, uri);
  219. if (IS_ERR(ret))
  220. return unix_to_pal_error(ERRNO(ret));
  221. handle->file.realpath = remalloc(uri, strlen(uri));
  222. return 0;
  223. }
  224. static int file_getname (PAL_HANDLE handle, char * buffer, int count)
  225. {
  226. if (!handle->file.realpath)
  227. return 0;
  228. int len = strlen(handle->file.realpath);
  229. char * tmp = strcpy_static(buffer, "file:", count);
  230. if (!tmp || buffer + count < tmp + len + 1)
  231. return -PAL_ERROR_TOOLONG;
  232. memcpy(tmp, handle->file.realpath, len + 1);
  233. return tmp + len - buffer;
  234. }
  235. const char * file_getrealpath (PAL_HANDLE handle)
  236. {
  237. return handle->file.realpath;
  238. }
  239. struct handle_ops file_ops = {
  240. .getname = &file_getname,
  241. .getrealpath = &file_getrealpath,
  242. .open = &file_open,
  243. .read = &file_read,
  244. .write = &file_write,
  245. .close = &file_close,
  246. .delete = &file_delete,
  247. .map = &file_map,
  248. .setlength = &file_setlength,
  249. .flush = &file_flush,
  250. .attrquery = &file_attrquery,
  251. .attrquerybyhdl = &file_attrquerybyhdl,
  252. .attrsetbyhdl = &file_attrsetbyhdl,
  253. .rename = &file_rename,
  254. };
  255. /* 'open' operation for directory stream. Directory stream does not have a
  256. specific type prefix, its URI looks the same file streams, plus it
  257. ended with slashes. dir_open will be called by file_open. */
  258. static int dir_open (PAL_HANDLE * handle, const char * type, const char * uri,
  259. int access, int share, int create, int options)
  260. {
  261. int ret;
  262. if (create & PAL_CREAT_TRY) {
  263. ret = INLINE_SYSCALL(mkdir, 2, uri, share);
  264. if (IS_ERR(ret) && ERRNO(ret) == EEXIST &&
  265. create & PAL_CREAT_ALWAYS)
  266. return -PAL_ERROR_STREAMEXIST;
  267. }
  268. ret = INLINE_SYSCALL(open, 3, uri, O_DIRECTORY|options|O_CLOEXEC, 0);
  269. if (IS_ERR(ret))
  270. return unix_to_pal_error(ERRNO(ret));
  271. int len = strlen(uri);
  272. PAL_HANDLE hdl = malloc(HANDLE_SIZE(dir) + len + 1);
  273. SET_HANDLE_TYPE(hdl, dir);
  274. HANDLE_HDR(hdl)->flags |= RFD(0);
  275. hdl->dir.fd = ret;
  276. char * path = (void *) hdl + HANDLE_SIZE(dir);
  277. memcpy(path, uri, len + 1);
  278. hdl->dir.realpath = (PAL_STR) path;
  279. hdl->dir.buf = (PAL_PTR) NULL;
  280. hdl->dir.ptr = (PAL_PTR) NULL;
  281. hdl->dir.end = (PAL_PTR) NULL;
  282. hdl->dir.endofstream = PAL_FALSE;
  283. *handle = hdl;
  284. return 0;
  285. }
  286. struct linux_dirent64 {
  287. unsigned long d_ino;
  288. unsigned long d_off;
  289. unsigned short d_reclen;
  290. unsigned char d_type;
  291. char d_name[];
  292. };
  293. #define DT_UNKNOWN 0
  294. #define DT_FIFO 1
  295. #define DT_CHR 2
  296. #define DT_DIR 4
  297. #define DT_BLK 6
  298. #define DT_REG 8
  299. #define DT_LNK 10
  300. #define DT_SOCK 12
  301. #define DT_WHT 14
  302. #define DIRBUF_SIZE 1024
  303. /* 'read' operation for directory stream. Directory stream will not
  304. need a 'write' operat4on. */
  305. int dir_read (PAL_HANDLE handle, int offset, int count, void * buf)
  306. {
  307. void * dent_buf = (void *) handle->dir.buf ? : __alloca(DIRBUF_SIZE);
  308. void * ptr = (void *) handle->dir.ptr;
  309. void * end = (void *) handle->dir.end;
  310. int bytes = 0;
  311. if (ptr && ptr < end)
  312. goto output;
  313. do {
  314. if (handle->dir.endofstream)
  315. break;
  316. int size = INLINE_SYSCALL(getdents64, 3, handle->dir.fd, dent_buf,
  317. DIRBUF_SIZE);
  318. if (IS_ERR(size))
  319. return -PAL_ERROR_DENIED;
  320. if (size == 0) {
  321. handle->dir.endofstream = PAL_TRUE;
  322. break;
  323. }
  324. ptr = dent_buf;
  325. end = dent_buf + size;
  326. output:
  327. while (ptr < end) {
  328. struct linux_dirent64 * d = (struct linux_dirent64 *) ptr;
  329. if (d->d_name[0] == '.' &&
  330. (!d->d_name[1] || d->d_name[1] == '.'))
  331. goto next;
  332. bool isdir = (d->d_type == DT_DIR);
  333. int len = strlen(d->d_name);
  334. if (len + (isdir ? 2 : 1) > count)
  335. break;
  336. memcpy(buf, d->d_name, len);
  337. if (isdir)
  338. ((char *) buf)[len++] = '/';
  339. ((char *) buf)[len++] = '\0';
  340. bytes += len;
  341. buf += len;
  342. count -= len;
  343. next:
  344. ptr += d->d_reclen;
  345. }
  346. } while (ptr == end);
  347. if (ptr < end) {
  348. if (!handle->dir.buf)
  349. handle->dir.buf = (PAL_PTR) malloc(DIRBUF_SIZE);
  350. if ((void *) handle->dir.buf != ptr) {
  351. memmove((void *) handle->dir.buf, ptr, end - ptr);
  352. end = (void *) handle->dir.buf + (end - ptr);
  353. ptr = (void *) handle->dir.buf;
  354. }
  355. if (!bytes)
  356. return -PAL_ERROR_OVERFLOW;
  357. }
  358. return bytes ? : -PAL_ERROR_ENDOFSTREAM;
  359. }
  360. /* 'close' operation of directory streams */
  361. static int dir_close (PAL_HANDLE handle)
  362. {
  363. int fd = handle->dir.fd;
  364. int ret = INLINE_SYSCALL(close, 1, fd);
  365. if (handle->dir.buf) {
  366. free((void *) handle->dir.buf);
  367. handle->dir.buf = handle->dir.ptr = handle->dir.end = (PAL_PTR) NULL;
  368. }
  369. if (handle->dir.realpath &&
  370. handle->dir.realpath != (void *) handle + HANDLE_SIZE(dir))
  371. free((void *) handle->dir.realpath);
  372. if (IS_ERR(ret))
  373. return -PAL_ERROR_BADHANDLE;
  374. return 0;
  375. }
  376. /* 'delete' operation of directoy streams */
  377. static int dir_delete (PAL_HANDLE handle, int access)
  378. {
  379. if (access)
  380. return -PAL_ERROR_INVAL;
  381. int ret = dir_close(handle);
  382. if (ret < 0)
  383. return ret;
  384. ret = INLINE_SYSCALL(rmdir, 1, handle->dir.realpath);
  385. return (IS_ERR(ret) && ERRNO(ret) != ENOENT) ?
  386. -PAL_ERROR_DENIED : 0;
  387. }
  388. static int dir_rename (PAL_HANDLE handle, const char * type,
  389. const char * uri)
  390. {
  391. int ret = INLINE_SYSCALL(rename, 2, handle->dir.realpath, uri);
  392. if (IS_ERR(ret))
  393. return unix_to_pal_error(ERRNO(ret));
  394. handle->dir.realpath = remalloc(uri, strlen(uri));
  395. return 0;
  396. }
  397. static int dir_getname (PAL_HANDLE handle, char * buffer, int count)
  398. {
  399. if (!handle->dir.realpath)
  400. return 0;
  401. int len = strlen(handle->dir.realpath);
  402. char * tmp = strcpy_static(buffer, "dir:", count);
  403. if (!tmp || buffer + count < tmp + len + 1)
  404. return -PAL_ERROR_TOOLONG;
  405. memcpy(tmp, handle->dir.realpath, len + 1);
  406. return tmp + len - buffer;
  407. }
  408. static const char * dir_getrealpath (PAL_HANDLE handle)
  409. {
  410. return handle->dir.realpath;
  411. }
  412. struct handle_ops dir_ops = {
  413. .getname = &dir_getname,
  414. .getrealpath = &dir_getrealpath,
  415. .open = &dir_open,
  416. .read = &dir_read,
  417. .close = &dir_close,
  418. .delete = &dir_delete,
  419. .attrquery = &file_attrquery,
  420. .attrquerybyhdl = &file_attrquerybyhdl,
  421. .attrsetbyhdl = &file_attrsetbyhdl,
  422. .rename = &dir_rename,
  423. };