hostfs_user.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <stddef.h>
  7. #include <unistd.h>
  8. #include <dirent.h>
  9. #include <errno.h>
  10. #include <fcntl.h>
  11. #include <string.h>
  12. #include <sys/stat.h>
  13. #include <sys/time.h>
  14. #include <sys/types.h>
  15. #include <sys/vfs.h>
  16. #include <sys/syscall.h>
  17. #include "hostfs.h"
  18. #include <utime.h>
  19. static void stat64_to_hostfs(const struct stat64 *buf, struct hostfs_stat *p)
  20. {
  21. p->ino = buf->st_ino;
  22. p->mode = buf->st_mode;
  23. p->nlink = buf->st_nlink;
  24. p->uid = buf->st_uid;
  25. p->gid = buf->st_gid;
  26. p->size = buf->st_size;
  27. p->atime.tv_sec = buf->st_atime;
  28. p->atime.tv_nsec = 0;
  29. p->ctime.tv_sec = buf->st_ctime;
  30. p->ctime.tv_nsec = 0;
  31. p->mtime.tv_sec = buf->st_mtime;
  32. p->mtime.tv_nsec = 0;
  33. p->blksize = buf->st_blksize;
  34. p->blocks = buf->st_blocks;
  35. p->maj = os_major(buf->st_rdev);
  36. p->min = os_minor(buf->st_rdev);
  37. }
  38. int stat_file(const char *path, struct hostfs_stat *p, int fd)
  39. {
  40. struct stat64 buf;
  41. if (fd >= 0) {
  42. if (fstat64(fd, &buf) < 0)
  43. return -errno;
  44. } else if (lstat64(path, &buf) < 0) {
  45. return -errno;
  46. }
  47. stat64_to_hostfs(&buf, p);
  48. return 0;
  49. }
  50. int access_file(char *path, int r, int w, int x)
  51. {
  52. int mode = 0;
  53. if (r)
  54. mode = R_OK;
  55. if (w)
  56. mode |= W_OK;
  57. if (x)
  58. mode |= X_OK;
  59. if (access(path, mode) != 0)
  60. return -errno;
  61. else return 0;
  62. }
  63. int open_file(char *path, int r, int w, int append)
  64. {
  65. int mode = 0, fd;
  66. if (r && !w)
  67. mode = O_RDONLY;
  68. else if (!r && w)
  69. mode = O_WRONLY;
  70. else if (r && w)
  71. mode = O_RDWR;
  72. else panic("Impossible mode in open_file");
  73. if (append)
  74. mode |= O_APPEND;
  75. fd = open64(path, mode);
  76. if (fd < 0)
  77. return -errno;
  78. else return fd;
  79. }
  80. void *open_dir(char *path, int *err_out)
  81. {
  82. DIR *dir;
  83. dir = opendir(path);
  84. *err_out = errno;
  85. return dir;
  86. }
  87. void seek_dir(void *stream, unsigned long long pos)
  88. {
  89. DIR *dir = stream;
  90. seekdir(dir, pos);
  91. }
  92. char *read_dir(void *stream, unsigned long long *pos_out,
  93. unsigned long long *ino_out, int *len_out,
  94. unsigned int *type_out)
  95. {
  96. DIR *dir = stream;
  97. struct dirent *ent;
  98. ent = readdir(dir);
  99. if (ent == NULL)
  100. return NULL;
  101. *len_out = strlen(ent->d_name);
  102. *ino_out = ent->d_ino;
  103. *type_out = ent->d_type;
  104. *pos_out = ent->d_off;
  105. return ent->d_name;
  106. }
  107. int read_file(int fd, unsigned long long *offset, char *buf, int len)
  108. {
  109. int n;
  110. n = pread64(fd, buf, len, *offset);
  111. if (n < 0)
  112. return -errno;
  113. *offset += n;
  114. return n;
  115. }
  116. int write_file(int fd, unsigned long long *offset, const char *buf, int len)
  117. {
  118. int n;
  119. n = pwrite64(fd, buf, len, *offset);
  120. if (n < 0)
  121. return -errno;
  122. *offset += n;
  123. return n;
  124. }
  125. int lseek_file(int fd, long long offset, int whence)
  126. {
  127. int ret;
  128. ret = lseek64(fd, offset, whence);
  129. if (ret < 0)
  130. return -errno;
  131. return 0;
  132. }
  133. int fsync_file(int fd, int datasync)
  134. {
  135. int ret;
  136. if (datasync)
  137. ret = fdatasync(fd);
  138. else
  139. ret = fsync(fd);
  140. if (ret < 0)
  141. return -errno;
  142. return 0;
  143. }
  144. int replace_file(int oldfd, int fd)
  145. {
  146. return dup2(oldfd, fd);
  147. }
  148. void close_file(void *stream)
  149. {
  150. close(*((int *) stream));
  151. }
  152. void close_dir(void *stream)
  153. {
  154. closedir(stream);
  155. }
  156. int file_create(char *name, int mode)
  157. {
  158. int fd;
  159. fd = open64(name, O_CREAT | O_RDWR, mode);
  160. if (fd < 0)
  161. return -errno;
  162. return fd;
  163. }
  164. int set_attr(const char *file, struct hostfs_iattr *attrs, int fd)
  165. {
  166. struct hostfs_stat st;
  167. struct timeval times[2];
  168. int err, ma;
  169. if (attrs->ia_valid & HOSTFS_ATTR_MODE) {
  170. if (fd >= 0) {
  171. if (fchmod(fd, attrs->ia_mode) != 0)
  172. return -errno;
  173. } else if (chmod(file, attrs->ia_mode) != 0) {
  174. return -errno;
  175. }
  176. }
  177. if (attrs->ia_valid & HOSTFS_ATTR_UID) {
  178. if (fd >= 0) {
  179. if (fchown(fd, attrs->ia_uid, -1))
  180. return -errno;
  181. } else if (chown(file, attrs->ia_uid, -1)) {
  182. return -errno;
  183. }
  184. }
  185. if (attrs->ia_valid & HOSTFS_ATTR_GID) {
  186. if (fd >= 0) {
  187. if (fchown(fd, -1, attrs->ia_gid))
  188. return -errno;
  189. } else if (chown(file, -1, attrs->ia_gid)) {
  190. return -errno;
  191. }
  192. }
  193. if (attrs->ia_valid & HOSTFS_ATTR_SIZE) {
  194. if (fd >= 0) {
  195. if (ftruncate(fd, attrs->ia_size))
  196. return -errno;
  197. } else if (truncate(file, attrs->ia_size)) {
  198. return -errno;
  199. }
  200. }
  201. /*
  202. * Update accessed and/or modified time, in two parts: first set
  203. * times according to the changes to perform, and then call futimes()
  204. * or utimes() to apply them.
  205. */
  206. ma = (HOSTFS_ATTR_ATIME_SET | HOSTFS_ATTR_MTIME_SET);
  207. if (attrs->ia_valid & ma) {
  208. err = stat_file(file, &st, fd);
  209. if (err != 0)
  210. return err;
  211. times[0].tv_sec = st.atime.tv_sec;
  212. times[0].tv_usec = st.atime.tv_nsec / 1000;
  213. times[1].tv_sec = st.mtime.tv_sec;
  214. times[1].tv_usec = st.mtime.tv_nsec / 1000;
  215. if (attrs->ia_valid & HOSTFS_ATTR_ATIME_SET) {
  216. times[0].tv_sec = attrs->ia_atime.tv_sec;
  217. times[0].tv_usec = attrs->ia_atime.tv_nsec / 1000;
  218. }
  219. if (attrs->ia_valid & HOSTFS_ATTR_MTIME_SET) {
  220. times[1].tv_sec = attrs->ia_mtime.tv_sec;
  221. times[1].tv_usec = attrs->ia_mtime.tv_nsec / 1000;
  222. }
  223. if (fd >= 0) {
  224. if (futimes(fd, times) != 0)
  225. return -errno;
  226. } else if (utimes(file, times) != 0) {
  227. return -errno;
  228. }
  229. }
  230. /* Note: ctime is not handled */
  231. if (attrs->ia_valid & (HOSTFS_ATTR_ATIME | HOSTFS_ATTR_MTIME)) {
  232. err = stat_file(file, &st, fd);
  233. attrs->ia_atime = st.atime;
  234. attrs->ia_mtime = st.mtime;
  235. if (err != 0)
  236. return err;
  237. }
  238. return 0;
  239. }
  240. int make_symlink(const char *from, const char *to)
  241. {
  242. int err;
  243. err = symlink(to, from);
  244. if (err)
  245. return -errno;
  246. return 0;
  247. }
  248. int unlink_file(const char *file)
  249. {
  250. int err;
  251. err = unlink(file);
  252. if (err)
  253. return -errno;
  254. return 0;
  255. }
  256. int do_mkdir(const char *file, int mode)
  257. {
  258. int err;
  259. err = mkdir(file, mode);
  260. if (err)
  261. return -errno;
  262. return 0;
  263. }
  264. int do_rmdir(const char *file)
  265. {
  266. int err;
  267. err = rmdir(file);
  268. if (err)
  269. return -errno;
  270. return 0;
  271. }
  272. int do_mknod(const char *file, int mode, unsigned int major, unsigned int minor)
  273. {
  274. int err;
  275. err = mknod(file, mode, os_makedev(major, minor));
  276. if (err)
  277. return -errno;
  278. return 0;
  279. }
  280. int link_file(const char *to, const char *from)
  281. {
  282. int err;
  283. err = link(to, from);
  284. if (err)
  285. return -errno;
  286. return 0;
  287. }
  288. int hostfs_do_readlink(char *file, char *buf, int size)
  289. {
  290. int n;
  291. n = readlink(file, buf, size);
  292. if (n < 0)
  293. return -errno;
  294. if (n < size)
  295. buf[n] = '\0';
  296. return n;
  297. }
  298. int rename_file(char *from, char *to)
  299. {
  300. int err;
  301. err = rename(from, to);
  302. if (err < 0)
  303. return -errno;
  304. return 0;
  305. }
  306. int rename2_file(char *from, char *to, unsigned int flags)
  307. {
  308. int err;
  309. #ifndef SYS_renameat2
  310. # ifdef __x86_64__
  311. # define SYS_renameat2 316
  312. # endif
  313. # ifdef __i386__
  314. # define SYS_renameat2 353
  315. # endif
  316. #endif
  317. #ifdef SYS_renameat2
  318. err = syscall(SYS_renameat2, AT_FDCWD, from, AT_FDCWD, to, flags);
  319. if (err < 0) {
  320. if (errno != ENOSYS)
  321. return -errno;
  322. else
  323. return -EINVAL;
  324. }
  325. return 0;
  326. #else
  327. return -EINVAL;
  328. #endif
  329. }
  330. int do_statfs(char *root, long *bsize_out, long long *blocks_out,
  331. long long *bfree_out, long long *bavail_out,
  332. long long *files_out, long long *ffree_out,
  333. void *fsid_out, int fsid_size, long *namelen_out)
  334. {
  335. struct statfs64 buf;
  336. int err;
  337. err = statfs64(root, &buf);
  338. if (err < 0)
  339. return -errno;
  340. *bsize_out = buf.f_bsize;
  341. *blocks_out = buf.f_blocks;
  342. *bfree_out = buf.f_bfree;
  343. *bavail_out = buf.f_bavail;
  344. *files_out = buf.f_files;
  345. *ffree_out = buf.f_ffree;
  346. memcpy(fsid_out, &buf.f_fsid,
  347. sizeof(buf.f_fsid) > fsid_size ? fsid_size :
  348. sizeof(buf.f_fsid));
  349. *namelen_out = buf.f_namelen;
  350. return 0;
  351. }