file.c 6.1 KB

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