file.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * File operations for Coda.
  4. * Original version: (C) 1996 Peter Braam
  5. * Rewritten for Linux 2.1: (C) 1997 Carnegie Mellon University
  6. *
  7. * Carnegie Mellon encourages users of this code to contribute improvements
  8. * to the Coda project. Contact Peter Braam <coda@cs.cmu.edu>.
  9. */
  10. #include <linux/refcount.h>
  11. #include <linux/types.h>
  12. #include <linux/kernel.h>
  13. #include <linux/time.h>
  14. #include <linux/file.h>
  15. #include <linux/fs.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/stat.h>
  18. #include <linux/cred.h>
  19. #include <linux/errno.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/string.h>
  22. #include <linux/slab.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/uio.h>
  25. #include <linux/splice.h>
  26. #include <linux/coda.h>
  27. #include "coda_psdev.h"
  28. #include "coda_linux.h"
  29. #include "coda_int.h"
  30. struct coda_vm_ops {
  31. refcount_t refcnt;
  32. struct file *coda_file;
  33. const struct vm_operations_struct *host_vm_ops;
  34. struct vm_operations_struct vm_ops;
  35. };
  36. static ssize_t
  37. coda_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
  38. {
  39. struct file *coda_file = iocb->ki_filp;
  40. struct inode *coda_inode = file_inode(coda_file);
  41. struct coda_file_info *cfi = coda_ftoc(coda_file);
  42. loff_t ki_pos = iocb->ki_pos;
  43. size_t count = iov_iter_count(to);
  44. ssize_t ret;
  45. ret = venus_access_intent(coda_inode->i_sb, coda_i2f(coda_inode),
  46. &cfi->cfi_access_intent,
  47. count, ki_pos, CODA_ACCESS_TYPE_READ);
  48. if (ret)
  49. goto finish_read;
  50. ret = vfs_iter_read(cfi->cfi_container, to, &iocb->ki_pos, 0);
  51. finish_read:
  52. venus_access_intent(coda_inode->i_sb, coda_i2f(coda_inode),
  53. &cfi->cfi_access_intent,
  54. count, ki_pos, CODA_ACCESS_TYPE_READ_FINISH);
  55. return ret;
  56. }
  57. static ssize_t
  58. coda_file_write_iter(struct kiocb *iocb, struct iov_iter *to)
  59. {
  60. struct file *coda_file = iocb->ki_filp;
  61. struct inode *coda_inode = file_inode(coda_file);
  62. struct coda_file_info *cfi = coda_ftoc(coda_file);
  63. struct file *host_file = cfi->cfi_container;
  64. loff_t ki_pos = iocb->ki_pos;
  65. size_t count = iov_iter_count(to);
  66. ssize_t ret;
  67. ret = venus_access_intent(coda_inode->i_sb, coda_i2f(coda_inode),
  68. &cfi->cfi_access_intent,
  69. count, ki_pos, CODA_ACCESS_TYPE_WRITE);
  70. if (ret)
  71. goto finish_write;
  72. inode_lock(coda_inode);
  73. ret = vfs_iter_write(cfi->cfi_container, to, &iocb->ki_pos, 0);
  74. coda_inode->i_size = file_inode(host_file)->i_size;
  75. coda_inode->i_blocks = (coda_inode->i_size + 511) >> 9;
  76. inode_set_mtime_to_ts(coda_inode, inode_set_ctime_current(coda_inode));
  77. inode_unlock(coda_inode);
  78. finish_write:
  79. venus_access_intent(coda_inode->i_sb, coda_i2f(coda_inode),
  80. &cfi->cfi_access_intent,
  81. count, ki_pos, CODA_ACCESS_TYPE_WRITE_FINISH);
  82. return ret;
  83. }
  84. static ssize_t
  85. coda_file_splice_read(struct file *coda_file, loff_t *ppos,
  86. struct pipe_inode_info *pipe,
  87. size_t len, unsigned int flags)
  88. {
  89. struct inode *coda_inode = file_inode(coda_file);
  90. struct coda_file_info *cfi = coda_ftoc(coda_file);
  91. struct file *in = cfi->cfi_container;
  92. loff_t ki_pos = *ppos;
  93. ssize_t ret;
  94. ret = venus_access_intent(coda_inode->i_sb, coda_i2f(coda_inode),
  95. &cfi->cfi_access_intent,
  96. len, ki_pos, CODA_ACCESS_TYPE_READ);
  97. if (ret)
  98. goto finish_read;
  99. ret = vfs_splice_read(in, ppos, pipe, len, flags);
  100. finish_read:
  101. venus_access_intent(coda_inode->i_sb, coda_i2f(coda_inode),
  102. &cfi->cfi_access_intent,
  103. len, ki_pos, CODA_ACCESS_TYPE_READ_FINISH);
  104. return ret;
  105. }
  106. static void
  107. coda_vm_open(struct vm_area_struct *vma)
  108. {
  109. struct coda_vm_ops *cvm_ops =
  110. container_of(vma->vm_ops, struct coda_vm_ops, vm_ops);
  111. refcount_inc(&cvm_ops->refcnt);
  112. if (cvm_ops->host_vm_ops && cvm_ops->host_vm_ops->open)
  113. cvm_ops->host_vm_ops->open(vma);
  114. }
  115. static void
  116. coda_vm_close(struct vm_area_struct *vma)
  117. {
  118. struct coda_vm_ops *cvm_ops =
  119. container_of(vma->vm_ops, struct coda_vm_ops, vm_ops);
  120. if (cvm_ops->host_vm_ops && cvm_ops->host_vm_ops->close)
  121. cvm_ops->host_vm_ops->close(vma);
  122. if (refcount_dec_and_test(&cvm_ops->refcnt)) {
  123. vma->vm_ops = cvm_ops->host_vm_ops;
  124. fput(cvm_ops->coda_file);
  125. kfree(cvm_ops);
  126. }
  127. }
  128. static int
  129. coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
  130. {
  131. struct inode *coda_inode = file_inode(coda_file);
  132. struct coda_file_info *cfi = coda_ftoc(coda_file);
  133. struct file *host_file = cfi->cfi_container;
  134. struct inode *host_inode = file_inode(host_file);
  135. struct coda_inode_info *cii;
  136. struct coda_vm_ops *cvm_ops;
  137. loff_t ppos;
  138. size_t count;
  139. int ret;
  140. if (!host_file->f_op->mmap)
  141. return -ENODEV;
  142. if (WARN_ON(coda_file != vma->vm_file))
  143. return -EIO;
  144. count = vma->vm_end - vma->vm_start;
  145. ppos = vma->vm_pgoff * PAGE_SIZE;
  146. ret = venus_access_intent(coda_inode->i_sb, coda_i2f(coda_inode),
  147. &cfi->cfi_access_intent,
  148. count, ppos, CODA_ACCESS_TYPE_MMAP);
  149. if (ret)
  150. return ret;
  151. cvm_ops = kmalloc(sizeof(struct coda_vm_ops), GFP_KERNEL);
  152. if (!cvm_ops)
  153. return -ENOMEM;
  154. cii = ITOC(coda_inode);
  155. spin_lock(&cii->c_lock);
  156. coda_file->f_mapping = host_file->f_mapping;
  157. if (coda_inode->i_mapping == &coda_inode->i_data)
  158. coda_inode->i_mapping = host_inode->i_mapping;
  159. /* only allow additional mmaps as long as userspace isn't changing
  160. * the container file on us! */
  161. else if (coda_inode->i_mapping != host_inode->i_mapping) {
  162. spin_unlock(&cii->c_lock);
  163. kfree(cvm_ops);
  164. return -EBUSY;
  165. }
  166. /* keep track of how often the coda_inode/host_file has been mmapped */
  167. cii->c_mapcount++;
  168. cfi->cfi_mapcount++;
  169. spin_unlock(&cii->c_lock);
  170. vma->vm_file = get_file(host_file);
  171. ret = call_mmap(vma->vm_file, vma);
  172. if (ret) {
  173. /* if call_mmap fails, our caller will put host_file so we
  174. * should drop the reference to the coda_file that we got.
  175. */
  176. fput(coda_file);
  177. kfree(cvm_ops);
  178. } else {
  179. /* here we add redirects for the open/close vm_operations */
  180. cvm_ops->host_vm_ops = vma->vm_ops;
  181. if (vma->vm_ops)
  182. cvm_ops->vm_ops = *vma->vm_ops;
  183. cvm_ops->vm_ops.open = coda_vm_open;
  184. cvm_ops->vm_ops.close = coda_vm_close;
  185. cvm_ops->coda_file = coda_file;
  186. refcount_set(&cvm_ops->refcnt, 1);
  187. vma->vm_ops = &cvm_ops->vm_ops;
  188. }
  189. return ret;
  190. }
  191. int coda_open(struct inode *coda_inode, struct file *coda_file)
  192. {
  193. struct file *host_file = NULL;
  194. int error;
  195. unsigned short flags = coda_file->f_flags & (~O_EXCL);
  196. unsigned short coda_flags = coda_flags_to_cflags(flags);
  197. struct coda_file_info *cfi;
  198. cfi = kmalloc(sizeof(struct coda_file_info), GFP_KERNEL);
  199. if (!cfi)
  200. return -ENOMEM;
  201. error = venus_open(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags,
  202. &host_file);
  203. if (!host_file)
  204. error = -EIO;
  205. if (error) {
  206. kfree(cfi);
  207. return error;
  208. }
  209. host_file->f_flags |= coda_file->f_flags & (O_APPEND | O_SYNC);
  210. cfi->cfi_magic = CODA_MAGIC;
  211. cfi->cfi_mapcount = 0;
  212. cfi->cfi_container = host_file;
  213. /* assume access intents are supported unless we hear otherwise */
  214. cfi->cfi_access_intent = true;
  215. BUG_ON(coda_file->private_data != NULL);
  216. coda_file->private_data = cfi;
  217. return 0;
  218. }
  219. int coda_release(struct inode *coda_inode, struct file *coda_file)
  220. {
  221. unsigned short flags = (coda_file->f_flags) & (~O_EXCL);
  222. unsigned short coda_flags = coda_flags_to_cflags(flags);
  223. struct coda_file_info *cfi;
  224. struct coda_inode_info *cii;
  225. struct inode *host_inode;
  226. cfi = coda_ftoc(coda_file);
  227. venus_close(coda_inode->i_sb, coda_i2f(coda_inode),
  228. coda_flags, coda_file->f_cred->fsuid);
  229. host_inode = file_inode(cfi->cfi_container);
  230. cii = ITOC(coda_inode);
  231. /* did we mmap this file? */
  232. spin_lock(&cii->c_lock);
  233. if (coda_inode->i_mapping == &host_inode->i_data) {
  234. cii->c_mapcount -= cfi->cfi_mapcount;
  235. if (!cii->c_mapcount)
  236. coda_inode->i_mapping = &coda_inode->i_data;
  237. }
  238. spin_unlock(&cii->c_lock);
  239. fput(cfi->cfi_container);
  240. kfree(coda_file->private_data);
  241. coda_file->private_data = NULL;
  242. /* VFS fput ignores the return value from file_operations->release, so
  243. * there is no use returning an error here */
  244. return 0;
  245. }
  246. int coda_fsync(struct file *coda_file, loff_t start, loff_t end, int datasync)
  247. {
  248. struct file *host_file;
  249. struct inode *coda_inode = file_inode(coda_file);
  250. struct coda_file_info *cfi;
  251. int err;
  252. if (!(S_ISREG(coda_inode->i_mode) || S_ISDIR(coda_inode->i_mode) ||
  253. S_ISLNK(coda_inode->i_mode)))
  254. return -EINVAL;
  255. err = filemap_write_and_wait_range(coda_inode->i_mapping, start, end);
  256. if (err)
  257. return err;
  258. inode_lock(coda_inode);
  259. cfi = coda_ftoc(coda_file);
  260. host_file = cfi->cfi_container;
  261. err = vfs_fsync(host_file, datasync);
  262. if (!err && !datasync)
  263. err = venus_fsync(coda_inode->i_sb, coda_i2f(coda_inode));
  264. inode_unlock(coda_inode);
  265. return err;
  266. }
  267. const struct file_operations coda_file_operations = {
  268. .llseek = generic_file_llseek,
  269. .read_iter = coda_file_read_iter,
  270. .write_iter = coda_file_write_iter,
  271. .mmap = coda_file_mmap,
  272. .open = coda_open,
  273. .release = coda_release,
  274. .fsync = coda_fsync,
  275. .splice_read = coda_file_splice_read,
  276. };