file.c 6.9 KB

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