file.c 7.9 KB

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