file.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * File operations for Coda.
  4. * Original version: (C) 1996 Peter Braam
  5. * Rewritten for Linux 2.1: (C) 1997 Carnegie Mellon University
  6. *
  7. * Carnegie Mellon encourages users of this code to contribute improvements
  8. * to the Coda project. Contact Peter Braam <coda@cs.cmu.edu>.
  9. */
  10. #include <linux/types.h>
  11. #include <linux/kernel.h>
  12. #include <linux/time.h>
  13. #include <linux/file.h>
  14. #include <linux/fs.h>
  15. #include <linux/stat.h>
  16. #include <linux/cred.h>
  17. #include <linux/errno.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/string.h>
  20. #include <linux/slab.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/coda.h>
  23. #include <linux/coda_psdev.h>
  24. #include "coda_linux.h"
  25. #include "coda_int.h"
  26. static ssize_t
  27. coda_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
  28. {
  29. struct file *coda_file = iocb->ki_filp;
  30. struct coda_file_info *cfi = CODA_FTOC(coda_file);
  31. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  32. return vfs_iter_read(cfi->cfi_container, to, &iocb->ki_pos, 0);
  33. }
  34. static ssize_t
  35. coda_file_write_iter(struct kiocb *iocb, struct iov_iter *to)
  36. {
  37. struct file *coda_file = iocb->ki_filp;
  38. struct inode *coda_inode = file_inode(coda_file);
  39. struct coda_file_info *cfi = CODA_FTOC(coda_file);
  40. struct file *host_file;
  41. ssize_t ret;
  42. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  43. host_file = cfi->cfi_container;
  44. file_start_write(host_file);
  45. inode_lock(coda_inode);
  46. ret = vfs_iter_write(cfi->cfi_container, to, &iocb->ki_pos, 0);
  47. coda_inode->i_size = file_inode(host_file)->i_size;
  48. coda_inode->i_blocks = (coda_inode->i_size + 511) >> 9;
  49. coda_inode->i_mtime = coda_inode->i_ctime = current_time(coda_inode);
  50. inode_unlock(coda_inode);
  51. file_end_write(host_file);
  52. return ret;
  53. }
  54. static int
  55. coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
  56. {
  57. struct coda_file_info *cfi;
  58. struct coda_inode_info *cii;
  59. struct file *host_file;
  60. struct inode *coda_inode, *host_inode;
  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->mmap)
  65. return -ENODEV;
  66. coda_inode = file_inode(coda_file);
  67. host_inode = file_inode(host_file);
  68. cii = ITOC(coda_inode);
  69. spin_lock(&cii->c_lock);
  70. coda_file->f_mapping = host_file->f_mapping;
  71. if (coda_inode->i_mapping == &coda_inode->i_data)
  72. coda_inode->i_mapping = host_inode->i_mapping;
  73. /* only allow additional mmaps as long as userspace isn't changing
  74. * the container file on us! */
  75. else if (coda_inode->i_mapping != host_inode->i_mapping) {
  76. spin_unlock(&cii->c_lock);
  77. return -EBUSY;
  78. }
  79. /* keep track of how often the coda_inode/host_file has been mmapped */
  80. cii->c_mapcount++;
  81. cfi->cfi_mapcount++;
  82. spin_unlock(&cii->c_lock);
  83. return call_mmap(host_file, vma);
  84. }
  85. int coda_open(struct inode *coda_inode, struct file *coda_file)
  86. {
  87. struct file *host_file = NULL;
  88. int error;
  89. unsigned short flags = coda_file->f_flags & (~O_EXCL);
  90. unsigned short coda_flags = coda_flags_to_cflags(flags);
  91. struct coda_file_info *cfi;
  92. cfi = kmalloc(sizeof(struct coda_file_info), GFP_KERNEL);
  93. if (!cfi)
  94. return -ENOMEM;
  95. error = venus_open(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags,
  96. &host_file);
  97. if (!host_file)
  98. error = -EIO;
  99. if (error) {
  100. kfree(cfi);
  101. return error;
  102. }
  103. host_file->f_flags |= coda_file->f_flags & (O_APPEND | O_SYNC);
  104. cfi->cfi_magic = CODA_MAGIC;
  105. cfi->cfi_mapcount = 0;
  106. cfi->cfi_container = host_file;
  107. BUG_ON(coda_file->private_data != NULL);
  108. coda_file->private_data = cfi;
  109. return 0;
  110. }
  111. int coda_release(struct inode *coda_inode, struct file *coda_file)
  112. {
  113. unsigned short flags = (coda_file->f_flags) & (~O_EXCL);
  114. unsigned short coda_flags = coda_flags_to_cflags(flags);
  115. struct coda_file_info *cfi;
  116. struct coda_inode_info *cii;
  117. struct inode *host_inode;
  118. int err;
  119. cfi = CODA_FTOC(coda_file);
  120. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  121. err = venus_close(coda_inode->i_sb, coda_i2f(coda_inode),
  122. coda_flags, coda_file->f_cred->fsuid);
  123. host_inode = file_inode(cfi->cfi_container);
  124. cii = ITOC(coda_inode);
  125. /* did we mmap this file? */
  126. spin_lock(&cii->c_lock);
  127. if (coda_inode->i_mapping == &host_inode->i_data) {
  128. cii->c_mapcount -= cfi->cfi_mapcount;
  129. if (!cii->c_mapcount)
  130. coda_inode->i_mapping = &coda_inode->i_data;
  131. }
  132. spin_unlock(&cii->c_lock);
  133. fput(cfi->cfi_container);
  134. kfree(coda_file->private_data);
  135. coda_file->private_data = NULL;
  136. /* VFS fput ignores the return value from file_operations->release, so
  137. * there is no use returning an error here */
  138. return 0;
  139. }
  140. int coda_fsync(struct file *coda_file, loff_t start, loff_t end, int datasync)
  141. {
  142. struct file *host_file;
  143. struct inode *coda_inode = file_inode(coda_file);
  144. struct coda_file_info *cfi;
  145. int err;
  146. if (!(S_ISREG(coda_inode->i_mode) || S_ISDIR(coda_inode->i_mode) ||
  147. S_ISLNK(coda_inode->i_mode)))
  148. return -EINVAL;
  149. err = filemap_write_and_wait_range(coda_inode->i_mapping, start, end);
  150. if (err)
  151. return err;
  152. inode_lock(coda_inode);
  153. cfi = CODA_FTOC(coda_file);
  154. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  155. host_file = cfi->cfi_container;
  156. err = vfs_fsync(host_file, datasync);
  157. if (!err && !datasync)
  158. err = venus_fsync(coda_inode->i_sb, coda_i2f(coda_inode));
  159. inode_unlock(coda_inode);
  160. return err;
  161. }
  162. const struct file_operations coda_file_operations = {
  163. .llseek = generic_file_llseek,
  164. .read_iter = coda_file_read_iter,
  165. .write_iter = coda_file_write_iter,
  166. .mmap = coda_file_mmap,
  167. .open = coda_open,
  168. .release = coda_release,
  169. .fsync = coda_fsync,
  170. .splice_read = generic_file_splice_read,
  171. };