file.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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/errno.h>
  16. #include <linux/smp_lock.h>
  17. #include <linux/string.h>
  18. #include <asm/uaccess.h>
  19. #include <linux/coda.h>
  20. #include <linux/coda_linux.h>
  21. #include <linux/coda_fs_i.h>
  22. #include <linux/coda_psdev.h>
  23. #include "coda_int.h"
  24. static ssize_t
  25. coda_file_read(struct file *coda_file, char __user *buf, size_t count, loff_t *ppos)
  26. {
  27. struct coda_file_info *cfi;
  28. struct file *host_file;
  29. cfi = CODA_FTOC(coda_file);
  30. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  31. host_file = cfi->cfi_container;
  32. if (!host_file->f_op || !host_file->f_op->read)
  33. return -EINVAL;
  34. return host_file->f_op->read(host_file, buf, count, ppos);
  35. }
  36. static ssize_t
  37. coda_file_splice_read(struct file *coda_file, loff_t *ppos,
  38. struct pipe_inode_info *pipe, size_t count,
  39. unsigned int flags)
  40. {
  41. struct coda_file_info *cfi;
  42. struct file *host_file;
  43. cfi = CODA_FTOC(coda_file);
  44. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  45. host_file = cfi->cfi_container;
  46. if (!host_file->f_op || !host_file->f_op->splice_read)
  47. return -EINVAL;
  48. return host_file->f_op->splice_read(host_file, ppos, pipe, count,flags);
  49. }
  50. static ssize_t
  51. coda_file_write(struct file *coda_file, const char __user *buf, size_t count, loff_t *ppos)
  52. {
  53. struct inode *host_inode, *coda_inode = coda_file->f_path.dentry->d_inode;
  54. struct coda_file_info *cfi;
  55. struct file *host_file;
  56. ssize_t ret;
  57. cfi = CODA_FTOC(coda_file);
  58. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  59. host_file = cfi->cfi_container;
  60. if (!host_file->f_op || !host_file->f_op->write)
  61. return -EINVAL;
  62. host_inode = host_file->f_path.dentry->d_inode;
  63. mutex_lock(&coda_inode->i_mutex);
  64. ret = host_file->f_op->write(host_file, buf, count, ppos);
  65. coda_inode->i_size = host_inode->i_size;
  66. coda_inode->i_blocks = (coda_inode->i_size + 511) >> 9;
  67. coda_inode->i_mtime = coda_inode->i_ctime = CURRENT_TIME_SEC;
  68. mutex_unlock(&coda_inode->i_mutex);
  69. return ret;
  70. }
  71. static int
  72. coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
  73. {
  74. struct coda_file_info *cfi;
  75. struct coda_inode_info *cii;
  76. struct file *host_file;
  77. struct inode *coda_inode, *host_inode;
  78. cfi = CODA_FTOC(coda_file);
  79. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  80. host_file = cfi->cfi_container;
  81. if (!host_file->f_op || !host_file->f_op->mmap)
  82. return -ENODEV;
  83. coda_inode = coda_file->f_path.dentry->d_inode;
  84. host_inode = host_file->f_path.dentry->d_inode;
  85. coda_file->f_mapping = host_file->f_mapping;
  86. if (coda_inode->i_mapping == &coda_inode->i_data)
  87. coda_inode->i_mapping = host_inode->i_mapping;
  88. /* only allow additional mmaps as long as userspace isn't changing
  89. * the container file on us! */
  90. else if (coda_inode->i_mapping != host_inode->i_mapping)
  91. return -EBUSY;
  92. /* keep track of how often the coda_inode/host_file has been mmapped */
  93. cii = ITOC(coda_inode);
  94. cii->c_mapcount++;
  95. cfi->cfi_mapcount++;
  96. return host_file->f_op->mmap(host_file, vma);
  97. }
  98. int coda_open(struct inode *coda_inode, struct file *coda_file)
  99. {
  100. struct file *host_file = NULL;
  101. int error;
  102. unsigned short flags = coda_file->f_flags & (~O_EXCL);
  103. unsigned short coda_flags = coda_flags_to_cflags(flags);
  104. struct coda_file_info *cfi;
  105. cfi = kmalloc(sizeof(struct coda_file_info), GFP_KERNEL);
  106. if (!cfi)
  107. return -ENOMEM;
  108. lock_kernel();
  109. error = venus_open(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags,
  110. &host_file);
  111. if (!host_file)
  112. error = -EIO;
  113. if (error) {
  114. kfree(cfi);
  115. unlock_kernel();
  116. return error;
  117. }
  118. host_file->f_flags |= coda_file->f_flags & (O_APPEND | O_SYNC);
  119. cfi->cfi_magic = CODA_MAGIC;
  120. cfi->cfi_mapcount = 0;
  121. cfi->cfi_container = host_file;
  122. BUG_ON(coda_file->private_data != NULL);
  123. coda_file->private_data = cfi;
  124. unlock_kernel();
  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 = 0;
  135. lock_kernel();
  136. cfi = CODA_FTOC(coda_file);
  137. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  138. err = venus_close(coda_inode->i_sb, coda_i2f(coda_inode),
  139. coda_flags, coda_file->f_uid);
  140. host_inode = cfi->cfi_container->f_path.dentry->d_inode;
  141. cii = ITOC(coda_inode);
  142. /* did we mmap this file? */
  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. fput(cfi->cfi_container);
  149. kfree(coda_file->private_data);
  150. coda_file->private_data = NULL;
  151. unlock_kernel();
  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, struct dentry *coda_dentry, int datasync)
  157. {
  158. struct file *host_file;
  159. struct dentry *host_dentry;
  160. struct inode *host_inode, *coda_inode = coda_dentry->d_inode;
  161. struct coda_file_info *cfi;
  162. int err = 0;
  163. if (!(S_ISREG(coda_inode->i_mode) || S_ISDIR(coda_inode->i_mode) ||
  164. S_ISLNK(coda_inode->i_mode)))
  165. return -EINVAL;
  166. cfi = CODA_FTOC(coda_file);
  167. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  168. host_file = cfi->cfi_container;
  169. if (host_file->f_op && host_file->f_op->fsync) {
  170. host_dentry = host_file->f_path.dentry;
  171. host_inode = host_dentry->d_inode;
  172. mutex_lock(&host_inode->i_mutex);
  173. err = host_file->f_op->fsync(host_file, host_dentry, datasync);
  174. mutex_unlock(&host_inode->i_mutex);
  175. }
  176. if ( !err && !datasync ) {
  177. lock_kernel();
  178. err = venus_fsync(coda_inode->i_sb, coda_i2f(coda_inode));
  179. unlock_kernel();
  180. }
  181. return err;
  182. }
  183. const struct file_operations coda_file_operations = {
  184. .llseek = generic_file_llseek,
  185. .read = coda_file_read,
  186. .write = coda_file_write,
  187. .mmap = coda_file_mmap,
  188. .open = coda_open,
  189. .release = coda_release,
  190. .fsync = coda_fsync,
  191. .splice_read = coda_file_splice_read,
  192. };