file.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 <asm/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(struct file *coda_file, char __user *buf, size_t count, loff_t *ppos)
  27. {
  28. struct coda_file_info *cfi;
  29. struct file *host_file;
  30. cfi = CODA_FTOC(coda_file);
  31. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  32. host_file = cfi->cfi_container;
  33. if (!host_file->f_op || !host_file->f_op->read)
  34. return -EINVAL;
  35. return host_file->f_op->read(host_file, buf, count, ppos);
  36. }
  37. static ssize_t
  38. coda_file_splice_read(struct file *coda_file, loff_t *ppos,
  39. struct pipe_inode_info *pipe, size_t count,
  40. unsigned int flags)
  41. {
  42. ssize_t (*splice_read)(struct file *, loff_t *,
  43. struct pipe_inode_info *, size_t, unsigned int);
  44. struct coda_file_info *cfi;
  45. struct file *host_file;
  46. cfi = CODA_FTOC(coda_file);
  47. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  48. host_file = cfi->cfi_container;
  49. splice_read = host_file->f_op->splice_read;
  50. if (!splice_read)
  51. splice_read = default_file_splice_read;
  52. return splice_read(host_file, ppos, pipe, count, flags);
  53. }
  54. static ssize_t
  55. coda_file_write(struct file *coda_file, const char __user *buf, size_t count, loff_t *ppos)
  56. {
  57. struct inode *host_inode, *coda_inode = coda_file->f_path.dentry->d_inode;
  58. struct coda_file_info *cfi;
  59. struct file *host_file;
  60. ssize_t ret;
  61. cfi = CODA_FTOC(coda_file);
  62. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  63. host_file = cfi->cfi_container;
  64. if (!host_file->f_op || !host_file->f_op->write)
  65. return -EINVAL;
  66. host_inode = host_file->f_path.dentry->d_inode;
  67. mutex_lock(&coda_inode->i_mutex);
  68. ret = host_file->f_op->write(host_file, buf, count, ppos);
  69. coda_inode->i_size = host_inode->i_size;
  70. coda_inode->i_blocks = (coda_inode->i_size + 511) >> 9;
  71. coda_inode->i_mtime = coda_inode->i_ctime = CURRENT_TIME_SEC;
  72. mutex_unlock(&coda_inode->i_mutex);
  73. return ret;
  74. }
  75. static int
  76. coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
  77. {
  78. struct coda_file_info *cfi;
  79. struct coda_inode_info *cii;
  80. struct file *host_file;
  81. struct inode *coda_inode, *host_inode;
  82. cfi = CODA_FTOC(coda_file);
  83. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  84. host_file = cfi->cfi_container;
  85. if (!host_file->f_op || !host_file->f_op->mmap)
  86. return -ENODEV;
  87. coda_inode = coda_file->f_path.dentry->d_inode;
  88. host_inode = host_file->f_path.dentry->d_inode;
  89. cii = ITOC(coda_inode);
  90. spin_lock(&cii->c_lock);
  91. coda_file->f_mapping = host_file->f_mapping;
  92. if (coda_inode->i_mapping == &coda_inode->i_data)
  93. coda_inode->i_mapping = host_inode->i_mapping;
  94. /* only allow additional mmaps as long as userspace isn't changing
  95. * the container file on us! */
  96. else if (coda_inode->i_mapping != host_inode->i_mapping) {
  97. spin_unlock(&cii->c_lock);
  98. return -EBUSY;
  99. }
  100. /* keep track of how often the coda_inode/host_file has been mmapped */
  101. cii->c_mapcount++;
  102. cfi->cfi_mapcount++;
  103. spin_unlock(&cii->c_lock);
  104. return host_file->f_op->mmap(host_file, vma);
  105. }
  106. int coda_open(struct inode *coda_inode, struct file *coda_file)
  107. {
  108. struct file *host_file = NULL;
  109. int error;
  110. unsigned short flags = coda_file->f_flags & (~O_EXCL);
  111. unsigned short coda_flags = coda_flags_to_cflags(flags);
  112. struct coda_file_info *cfi;
  113. cfi = kmalloc(sizeof(struct coda_file_info), GFP_KERNEL);
  114. if (!cfi)
  115. return -ENOMEM;
  116. error = venus_open(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags,
  117. &host_file);
  118. if (!host_file)
  119. error = -EIO;
  120. if (error) {
  121. kfree(cfi);
  122. return error;
  123. }
  124. host_file->f_flags |= coda_file->f_flags & (O_APPEND | O_SYNC);
  125. cfi->cfi_magic = CODA_MAGIC;
  126. cfi->cfi_mapcount = 0;
  127. cfi->cfi_container = host_file;
  128. BUG_ON(coda_file->private_data != NULL);
  129. coda_file->private_data = cfi;
  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;
  140. cfi = CODA_FTOC(coda_file);
  141. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  142. err = venus_close(coda_inode->i_sb, coda_i2f(coda_inode),
  143. coda_flags, coda_file->f_cred->fsuid);
  144. host_inode = cfi->cfi_container->f_path.dentry->d_inode;
  145. cii = ITOC(coda_inode);
  146. /* did we mmap this file? */
  147. spin_lock(&cii->c_lock);
  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. spin_unlock(&cii->c_lock);
  154. fput(cfi->cfi_container);
  155. kfree(coda_file->private_data);
  156. coda_file->private_data = NULL;
  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, loff_t start, loff_t end, 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;
  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. err = filemap_write_and_wait_range(coda_inode->i_mapping, start, end);
  171. if (err)
  172. return err;
  173. mutex_lock(&coda_inode->i_mutex);
  174. cfi = CODA_FTOC(coda_file);
  175. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  176. host_file = cfi->cfi_container;
  177. err = vfs_fsync(host_file, datasync);
  178. if (!err && !datasync)
  179. err = venus_fsync(coda_inode->i_sb, coda_i2f(coda_inode));
  180. mutex_unlock(&coda_inode->i_mutex);
  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. };