file.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 <coda@cs.cmu.edu>.
  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_splice_read(struct file *coda_file, loff_t *ppos,
  35. struct pipe_inode_info *pipe, size_t count,
  36. unsigned int flags)
  37. {
  38. ssize_t (*splice_read)(struct file *, loff_t *,
  39. struct pipe_inode_info *, size_t, unsigned int);
  40. struct coda_file_info *cfi;
  41. struct file *host_file;
  42. cfi = CODA_FTOC(coda_file);
  43. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  44. host_file = cfi->cfi_container;
  45. splice_read = host_file->f_op->splice_read;
  46. if (!splice_read)
  47. splice_read = default_file_splice_read;
  48. return splice_read(host_file, ppos, pipe, count, flags);
  49. }
  50. static ssize_t
  51. coda_file_write_iter(struct kiocb *iocb, struct iov_iter *to)
  52. {
  53. struct file *coda_file = iocb->ki_filp;
  54. struct inode *coda_inode = file_inode(coda_file);
  55. struct coda_file_info *cfi = CODA_FTOC(coda_file);
  56. struct file *host_file;
  57. ssize_t ret;
  58. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  59. host_file = cfi->cfi_container;
  60. file_start_write(host_file);
  61. mutex_lock(&coda_inode->i_mutex);
  62. ret = vfs_iter_write(cfi->cfi_container, to, &iocb->ki_pos);
  63. coda_inode->i_size = file_inode(host_file)->i_size;
  64. coda_inode->i_blocks = (coda_inode->i_size + 511) >> 9;
  65. coda_inode->i_mtime = coda_inode->i_ctime = CURRENT_TIME_SEC;
  66. mutex_unlock(&coda_inode->i_mutex);
  67. file_end_write(host_file);
  68. return ret;
  69. }
  70. static int
  71. coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
  72. {
  73. struct coda_file_info *cfi;
  74. struct coda_inode_info *cii;
  75. struct file *host_file;
  76. struct inode *coda_inode, *host_inode;
  77. cfi = CODA_FTOC(coda_file);
  78. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  79. host_file = cfi->cfi_container;
  80. if (!host_file->f_op->mmap)
  81. return -ENODEV;
  82. coda_inode = file_inode(coda_file);
  83. host_inode = file_inode(host_file);
  84. cii = ITOC(coda_inode);
  85. spin_lock(&cii->c_lock);
  86. coda_file->f_mapping = host_file->f_mapping;
  87. if (coda_inode->i_mapping == &coda_inode->i_data)
  88. coda_inode->i_mapping = host_inode->i_mapping;
  89. /* only allow additional mmaps as long as userspace isn't changing
  90. * the container file on us! */
  91. else if (coda_inode->i_mapping != host_inode->i_mapping) {
  92. spin_unlock(&cii->c_lock);
  93. return -EBUSY;
  94. }
  95. /* keep track of how often the coda_inode/host_file has been mmapped */
  96. cii->c_mapcount++;
  97. cfi->cfi_mapcount++;
  98. spin_unlock(&cii->c_lock);
  99. return host_file->f_op->mmap(host_file, vma);
  100. }
  101. int coda_open(struct inode *coda_inode, struct file *coda_file)
  102. {
  103. struct file *host_file = NULL;
  104. int error;
  105. unsigned short flags = coda_file->f_flags & (~O_EXCL);
  106. unsigned short coda_flags = coda_flags_to_cflags(flags);
  107. struct coda_file_info *cfi;
  108. cfi = kmalloc(sizeof(struct coda_file_info), GFP_KERNEL);
  109. if (!cfi)
  110. return -ENOMEM;
  111. error = venus_open(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags,
  112. &host_file);
  113. if (!host_file)
  114. error = -EIO;
  115. if (error) {
  116. kfree(cfi);
  117. return error;
  118. }
  119. host_file->f_flags |= coda_file->f_flags & (O_APPEND | O_SYNC);
  120. cfi->cfi_magic = CODA_MAGIC;
  121. cfi->cfi_mapcount = 0;
  122. cfi->cfi_container = host_file;
  123. BUG_ON(coda_file->private_data != NULL);
  124. coda_file->private_data = cfi;
  125. return 0;
  126. }
  127. int coda_release(struct inode *coda_inode, struct file *coda_file)
  128. {
  129. unsigned short flags = (coda_file->f_flags) & (~O_EXCL);
  130. unsigned short coda_flags = coda_flags_to_cflags(flags);
  131. struct coda_file_info *cfi;
  132. struct coda_inode_info *cii;
  133. struct inode *host_inode;
  134. int err;
  135. cfi = CODA_FTOC(coda_file);
  136. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  137. err = venus_close(coda_inode->i_sb, coda_i2f(coda_inode),
  138. coda_flags, coda_file->f_cred->fsuid);
  139. host_inode = file_inode(cfi->cfi_container);
  140. cii = ITOC(coda_inode);
  141. /* did we mmap this file? */
  142. spin_lock(&cii->c_lock);
  143. if (coda_inode->i_mapping == &host_inode->i_data) {
  144. cii->c_mapcount -= cfi->cfi_mapcount;
  145. if (!cii->c_mapcount)
  146. coda_inode->i_mapping = &coda_inode->i_data;
  147. }
  148. spin_unlock(&cii->c_lock);
  149. fput(cfi->cfi_container);
  150. kfree(coda_file->private_data);
  151. coda_file->private_data = NULL;
  152. /* VFS fput ignores the return value from file_operations->release, so
  153. * there is no use returning an error here */
  154. return 0;
  155. }
  156. int coda_fsync(struct file *coda_file, loff_t start, loff_t end, int datasync)
  157. {
  158. struct file *host_file;
  159. struct inode *coda_inode = file_inode(coda_file);
  160. struct coda_file_info *cfi;
  161. int err;
  162. if (!(S_ISREG(coda_inode->i_mode) || S_ISDIR(coda_inode->i_mode) ||
  163. S_ISLNK(coda_inode->i_mode)))
  164. return -EINVAL;
  165. err = filemap_write_and_wait_range(coda_inode->i_mapping, start, end);
  166. if (err)
  167. return err;
  168. mutex_lock(&coda_inode->i_mutex);
  169. cfi = CODA_FTOC(coda_file);
  170. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  171. host_file = cfi->cfi_container;
  172. err = vfs_fsync(host_file, datasync);
  173. if (!err && !datasync)
  174. err = venus_fsync(coda_inode->i_sb, coda_i2f(coda_inode));
  175. mutex_unlock(&coda_inode->i_mutex);
  176. return err;
  177. }
  178. const struct file_operations coda_file_operations = {
  179. .llseek = generic_file_llseek,
  180. .read_iter = coda_file_read_iter,
  181. .write_iter = coda_file_write_iter,
  182. .mmap = coda_file_mmap,
  183. .open = coda_open,
  184. .release = coda_release,
  185. .fsync = coda_fsync,
  186. .splice_read = coda_file_splice_read,
  187. };