file.c 6.2 KB

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