file.c 7.0 KB

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