file.c 5.4 KB

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