file.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * VirtualBox Guest Shared Folders support: Regular file inode and file ops.
  4. *
  5. * Copyright (C) 2006-2018 Oracle Corporation
  6. */
  7. #include <linux/mm.h>
  8. #include <linux/page-flags.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/highmem.h>
  11. #include <linux/sizes.h>
  12. #include "vfsmod.h"
  13. struct vboxsf_handle {
  14. u64 handle;
  15. u32 root;
  16. u32 access_flags;
  17. struct kref refcount;
  18. struct list_head head;
  19. };
  20. struct vboxsf_handle *vboxsf_create_sf_handle(struct inode *inode,
  21. u64 handle, u32 access_flags)
  22. {
  23. struct vboxsf_inode *sf_i = VBOXSF_I(inode);
  24. struct vboxsf_handle *sf_handle;
  25. sf_handle = kmalloc(sizeof(*sf_handle), GFP_KERNEL);
  26. if (!sf_handle)
  27. return ERR_PTR(-ENOMEM);
  28. /* the host may have given us different attr then requested */
  29. sf_i->force_restat = 1;
  30. /* init our handle struct and add it to the inode's handles list */
  31. sf_handle->handle = handle;
  32. sf_handle->root = VBOXSF_SBI(inode->i_sb)->root;
  33. sf_handle->access_flags = access_flags;
  34. kref_init(&sf_handle->refcount);
  35. mutex_lock(&sf_i->handle_list_mutex);
  36. list_add(&sf_handle->head, &sf_i->handle_list);
  37. mutex_unlock(&sf_i->handle_list_mutex);
  38. return sf_handle;
  39. }
  40. static int vboxsf_file_open(struct inode *inode, struct file *file)
  41. {
  42. struct vboxsf_sbi *sbi = VBOXSF_SBI(inode->i_sb);
  43. struct shfl_createparms params = {};
  44. struct vboxsf_handle *sf_handle;
  45. u32 access_flags = 0;
  46. int err;
  47. /*
  48. * We check the value of params.handle afterwards to find out if
  49. * the call succeeded or failed, as the API does not seem to cleanly
  50. * distinguish error and informational messages.
  51. *
  52. * Furthermore, we must set params.handle to SHFL_HANDLE_NIL to
  53. * make the shared folders host service use our mode parameter.
  54. */
  55. params.handle = SHFL_HANDLE_NIL;
  56. if (file->f_flags & O_CREAT) {
  57. params.create_flags |= SHFL_CF_ACT_CREATE_IF_NEW;
  58. /*
  59. * We ignore O_EXCL, as the Linux kernel seems to call create
  60. * beforehand itself, so O_EXCL should always fail.
  61. */
  62. if (file->f_flags & O_TRUNC)
  63. params.create_flags |= SHFL_CF_ACT_OVERWRITE_IF_EXISTS;
  64. else
  65. params.create_flags |= SHFL_CF_ACT_OPEN_IF_EXISTS;
  66. } else {
  67. params.create_flags |= SHFL_CF_ACT_FAIL_IF_NEW;
  68. if (file->f_flags & O_TRUNC)
  69. params.create_flags |= SHFL_CF_ACT_OVERWRITE_IF_EXISTS;
  70. }
  71. switch (file->f_flags & O_ACCMODE) {
  72. case O_RDONLY:
  73. access_flags |= SHFL_CF_ACCESS_READ;
  74. break;
  75. case O_WRONLY:
  76. access_flags |= SHFL_CF_ACCESS_WRITE;
  77. break;
  78. case O_RDWR:
  79. access_flags |= SHFL_CF_ACCESS_READWRITE;
  80. break;
  81. default:
  82. WARN_ON(1);
  83. }
  84. if (file->f_flags & O_APPEND)
  85. access_flags |= SHFL_CF_ACCESS_APPEND;
  86. params.create_flags |= access_flags;
  87. params.info.attr.mode = inode->i_mode;
  88. err = vboxsf_create_at_dentry(file_dentry(file), &params);
  89. if (err == 0 && params.handle == SHFL_HANDLE_NIL)
  90. err = (params.result == SHFL_FILE_EXISTS) ? -EEXIST : -ENOENT;
  91. if (err)
  92. return err;
  93. sf_handle = vboxsf_create_sf_handle(inode, params.handle, access_flags);
  94. if (IS_ERR(sf_handle)) {
  95. vboxsf_close(sbi->root, params.handle);
  96. return PTR_ERR(sf_handle);
  97. }
  98. file->private_data = sf_handle;
  99. return 0;
  100. }
  101. static void vboxsf_handle_release(struct kref *refcount)
  102. {
  103. struct vboxsf_handle *sf_handle =
  104. container_of(refcount, struct vboxsf_handle, refcount);
  105. vboxsf_close(sf_handle->root, sf_handle->handle);
  106. kfree(sf_handle);
  107. }
  108. void vboxsf_release_sf_handle(struct inode *inode, struct vboxsf_handle *sf_handle)
  109. {
  110. struct vboxsf_inode *sf_i = VBOXSF_I(inode);
  111. mutex_lock(&sf_i->handle_list_mutex);
  112. list_del(&sf_handle->head);
  113. mutex_unlock(&sf_i->handle_list_mutex);
  114. kref_put(&sf_handle->refcount, vboxsf_handle_release);
  115. }
  116. static int vboxsf_file_release(struct inode *inode, struct file *file)
  117. {
  118. /*
  119. * When a file is closed on our (the guest) side, we want any subsequent
  120. * accesses done on the host side to see all changes done from our side.
  121. */
  122. filemap_write_and_wait(inode->i_mapping);
  123. vboxsf_release_sf_handle(inode, file->private_data);
  124. return 0;
  125. }
  126. /*
  127. * Write back dirty pages now, because there may not be any suitable
  128. * open files later
  129. */
  130. static void vboxsf_vma_close(struct vm_area_struct *vma)
  131. {
  132. filemap_write_and_wait(vma->vm_file->f_mapping);
  133. }
  134. static const struct vm_operations_struct vboxsf_file_vm_ops = {
  135. .close = vboxsf_vma_close,
  136. .fault = filemap_fault,
  137. .map_pages = filemap_map_pages,
  138. };
  139. static int vboxsf_file_mmap(struct file *file, struct vm_area_struct *vma)
  140. {
  141. int err;
  142. err = generic_file_mmap(file, vma);
  143. if (!err)
  144. vma->vm_ops = &vboxsf_file_vm_ops;
  145. return err;
  146. }
  147. /*
  148. * Note that since we are accessing files on the host's filesystem, files
  149. * may always be changed underneath us by the host!
  150. *
  151. * The vboxsf API between the guest and the host does not offer any functions
  152. * to deal with this. There is no inode-generation to check for changes, no
  153. * events / callback on changes and no way to lock files.
  154. *
  155. * To avoid returning stale data when a file gets *opened* on our (the guest)
  156. * side, we do a "stat" on the host side, then compare the mtime with the
  157. * last known mtime and invalidate the page-cache if they differ.
  158. * This is done from vboxsf_inode_revalidate().
  159. *
  160. * When reads are done through the read_iter fop, it is possible to do
  161. * further cache revalidation then, there are 3 options to deal with this:
  162. *
  163. * 1) Rely solely on the revalidation done at open time
  164. * 2) Do another "stat" and compare mtime again. Unfortunately the vboxsf
  165. * host API does not allow stat on handles, so we would need to use
  166. * file->f_path.dentry and the stat will then fail if the file was unlinked
  167. * or renamed (and there is no thing like NFS' silly-rename). So we get:
  168. * 2a) "stat" and compare mtime, on stat failure invalidate the cache
  169. * 2b) "stat" and compare mtime, on stat failure do nothing
  170. * 3) Simply always call invalidate_inode_pages2_range on the range of the read
  171. *
  172. * Currently we are keeping things KISS and using option 1. this allows
  173. * directly using generic_file_read_iter without wrapping it.
  174. *
  175. * This means that only data written on the host side before open() on
  176. * the guest side is guaranteed to be seen by the guest. If necessary
  177. * we may provide other read-cache strategies in the future and make this
  178. * configurable through a mount option.
  179. */
  180. const struct file_operations vboxsf_reg_fops = {
  181. .llseek = generic_file_llseek,
  182. .read_iter = generic_file_read_iter,
  183. .write_iter = generic_file_write_iter,
  184. .mmap = vboxsf_file_mmap,
  185. .open = vboxsf_file_open,
  186. .release = vboxsf_file_release,
  187. .fsync = noop_fsync,
  188. .splice_read = filemap_splice_read,
  189. .setlease = simple_nosetlease,
  190. };
  191. const struct inode_operations vboxsf_reg_iops = {
  192. .getattr = vboxsf_getattr,
  193. .setattr = vboxsf_setattr
  194. };
  195. static int vboxsf_read_folio(struct file *file, struct folio *folio)
  196. {
  197. struct vboxsf_handle *sf_handle = file->private_data;
  198. loff_t off = folio_pos(folio);
  199. u32 nread = PAGE_SIZE;
  200. u8 *buf;
  201. int err;
  202. buf = kmap_local_folio(folio, 0);
  203. err = vboxsf_read(sf_handle->root, sf_handle->handle, off, &nread, buf);
  204. buf = folio_zero_tail(folio, nread, buf + nread);
  205. kunmap_local(buf);
  206. folio_end_read(folio, err == 0);
  207. return err;
  208. }
  209. static struct vboxsf_handle *vboxsf_get_write_handle(struct vboxsf_inode *sf_i)
  210. {
  211. struct vboxsf_handle *h, *sf_handle = NULL;
  212. mutex_lock(&sf_i->handle_list_mutex);
  213. list_for_each_entry(h, &sf_i->handle_list, head) {
  214. if (h->access_flags == SHFL_CF_ACCESS_WRITE ||
  215. h->access_flags == SHFL_CF_ACCESS_READWRITE) {
  216. kref_get(&h->refcount);
  217. sf_handle = h;
  218. break;
  219. }
  220. }
  221. mutex_unlock(&sf_i->handle_list_mutex);
  222. return sf_handle;
  223. }
  224. static int vboxsf_writepage(struct page *page, struct writeback_control *wbc)
  225. {
  226. struct inode *inode = page->mapping->host;
  227. struct vboxsf_inode *sf_i = VBOXSF_I(inode);
  228. struct vboxsf_handle *sf_handle;
  229. loff_t off = page_offset(page);
  230. loff_t size = i_size_read(inode);
  231. u32 nwrite = PAGE_SIZE;
  232. u8 *buf;
  233. int err;
  234. if (off + PAGE_SIZE > size)
  235. nwrite = size & ~PAGE_MASK;
  236. sf_handle = vboxsf_get_write_handle(sf_i);
  237. if (!sf_handle)
  238. return -EBADF;
  239. buf = kmap(page);
  240. err = vboxsf_write(sf_handle->root, sf_handle->handle,
  241. off, &nwrite, buf);
  242. kunmap(page);
  243. kref_put(&sf_handle->refcount, vboxsf_handle_release);
  244. if (err == 0) {
  245. /* mtime changed */
  246. sf_i->force_restat = 1;
  247. } else {
  248. ClearPageUptodate(page);
  249. }
  250. unlock_page(page);
  251. return err;
  252. }
  253. static int vboxsf_write_end(struct file *file, struct address_space *mapping,
  254. loff_t pos, unsigned int len, unsigned int copied,
  255. struct folio *folio, void *fsdata)
  256. {
  257. struct inode *inode = mapping->host;
  258. struct vboxsf_handle *sf_handle = file->private_data;
  259. size_t from = offset_in_folio(folio, pos);
  260. u32 nwritten = len;
  261. u8 *buf;
  262. int err;
  263. /* zero the stale part of the folio if we did a short copy */
  264. if (!folio_test_uptodate(folio) && copied < len)
  265. folio_zero_range(folio, from + copied, len - copied);
  266. buf = kmap(&folio->page);
  267. err = vboxsf_write(sf_handle->root, sf_handle->handle,
  268. pos, &nwritten, buf + from);
  269. kunmap(&folio->page);
  270. if (err) {
  271. nwritten = 0;
  272. goto out;
  273. }
  274. /* mtime changed */
  275. VBOXSF_I(inode)->force_restat = 1;
  276. if (!folio_test_uptodate(folio) && nwritten == folio_size(folio))
  277. folio_mark_uptodate(folio);
  278. pos += nwritten;
  279. if (pos > inode->i_size)
  280. i_size_write(inode, pos);
  281. out:
  282. folio_unlock(folio);
  283. folio_put(folio);
  284. return nwritten;
  285. }
  286. /*
  287. * Note simple_write_begin does not read the page from disk on partial writes
  288. * this is ok since vboxsf_write_end only writes the written parts of the
  289. * page and it does not call folio_mark_uptodate for partial writes.
  290. */
  291. const struct address_space_operations vboxsf_reg_aops = {
  292. .read_folio = vboxsf_read_folio,
  293. .writepage = vboxsf_writepage,
  294. .dirty_folio = filemap_dirty_folio,
  295. .write_begin = simple_write_begin,
  296. .write_end = vboxsf_write_end,
  297. };
  298. static const char *vboxsf_get_link(struct dentry *dentry, struct inode *inode,
  299. struct delayed_call *done)
  300. {
  301. struct vboxsf_sbi *sbi = VBOXSF_SBI(inode->i_sb);
  302. struct shfl_string *path;
  303. char *link;
  304. int err;
  305. if (!dentry)
  306. return ERR_PTR(-ECHILD);
  307. path = vboxsf_path_from_dentry(sbi, dentry);
  308. if (IS_ERR(path))
  309. return ERR_CAST(path);
  310. link = kzalloc(PATH_MAX, GFP_KERNEL);
  311. if (!link) {
  312. __putname(path);
  313. return ERR_PTR(-ENOMEM);
  314. }
  315. err = vboxsf_readlink(sbi->root, path, PATH_MAX, link);
  316. __putname(path);
  317. if (err) {
  318. kfree(link);
  319. return ERR_PTR(err);
  320. }
  321. set_delayed_call(done, kfree_link, link);
  322. return link;
  323. }
  324. const struct inode_operations vboxsf_lnk_iops = {
  325. .get_link = vboxsf_get_link
  326. };