file.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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/errno.h>
  16. #include <linux/smp_lock.h>
  17. #include <linux/string.h>
  18. #include <asm/uaccess.h>
  19. #include <linux/coda.h>
  20. #include <linux/coda_linux.h>
  21. #include <linux/coda_fs_i.h>
  22. #include <linux/coda_psdev.h>
  23. #include <linux/coda_proc.h>
  24. #include "coda_int.h"
  25. /* if CODA_STORE fails with EOPNOTSUPP, venus clearly doesn't support
  26. * CODA_STORE/CODA_RELEASE and we fall back on using the CODA_CLOSE upcall */
  27. static int use_coda_close;
  28. static ssize_t
  29. coda_file_read(struct file *coda_file, char __user *buf, size_t count, loff_t *ppos)
  30. {
  31. struct coda_file_info *cfi;
  32. struct file *host_file;
  33. cfi = CODA_FTOC(coda_file);
  34. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  35. host_file = cfi->cfi_container;
  36. if (!host_file->f_op || !host_file->f_op->read)
  37. return -EINVAL;
  38. return host_file->f_op->read(host_file, buf, count, ppos);
  39. }
  40. static ssize_t
  41. coda_file_splice_read(struct file *coda_file, loff_t *ppos,
  42. struct pipe_inode_info *pipe, size_t count,
  43. unsigned int flags)
  44. {
  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. if (!host_file->f_op || !host_file->f_op->splice_read)
  51. return -EINVAL;
  52. return host_file->f_op->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. coda_file->f_mapping = host_file->f_mapping;
  90. if (coda_inode->i_mapping == &coda_inode->i_data)
  91. coda_inode->i_mapping = host_inode->i_mapping;
  92. /* only allow additional mmaps as long as userspace isn't changing
  93. * the container file on us! */
  94. else if (coda_inode->i_mapping != host_inode->i_mapping)
  95. return -EBUSY;
  96. /* keep track of how often the coda_inode/host_file has been mmapped */
  97. cii = ITOC(coda_inode);
  98. cii->c_mapcount++;
  99. cfi->cfi_mapcount++;
  100. return host_file->f_op->mmap(host_file, vma);
  101. }
  102. int coda_open(struct inode *coda_inode, struct file *coda_file)
  103. {
  104. struct file *host_file = NULL;
  105. int error;
  106. unsigned short flags = coda_file->f_flags & (~O_EXCL);
  107. unsigned short coda_flags = coda_flags_to_cflags(flags);
  108. struct coda_file_info *cfi;
  109. coda_vfs_stat.open++;
  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 (error || !host_file) {
  117. kfree(cfi);
  118. unlock_kernel();
  119. return error;
  120. }
  121. host_file->f_flags |= coda_file->f_flags & (O_APPEND | O_SYNC);
  122. cfi->cfi_magic = CODA_MAGIC;
  123. cfi->cfi_mapcount = 0;
  124. cfi->cfi_container = host_file;
  125. BUG_ON(coda_file->private_data != NULL);
  126. coda_file->private_data = cfi;
  127. unlock_kernel();
  128. return 0;
  129. }
  130. int coda_flush(struct file *coda_file, fl_owner_t id)
  131. {
  132. unsigned short flags = coda_file->f_flags & ~O_EXCL;
  133. unsigned short coda_flags = coda_flags_to_cflags(flags);
  134. struct coda_file_info *cfi;
  135. struct inode *coda_inode;
  136. int err = 0, fcnt;
  137. lock_kernel();
  138. coda_vfs_stat.flush++;
  139. /* last close semantics */
  140. fcnt = file_count(coda_file);
  141. if (fcnt > 1)
  142. goto out;
  143. /* No need to make an upcall when we have not made any modifications
  144. * to the file */
  145. if ((coda_file->f_flags & O_ACCMODE) == O_RDONLY)
  146. goto out;
  147. if (use_coda_close)
  148. goto out;
  149. cfi = CODA_FTOC(coda_file);
  150. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  151. coda_inode = coda_file->f_path.dentry->d_inode;
  152. err = venus_store(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags,
  153. coda_file->f_uid);
  154. if (err == -EOPNOTSUPP) {
  155. use_coda_close = 1;
  156. err = 0;
  157. }
  158. out:
  159. unlock_kernel();
  160. return err;
  161. }
  162. int coda_release(struct inode *coda_inode, struct file *coda_file)
  163. {
  164. unsigned short flags = (coda_file->f_flags) & (~O_EXCL);
  165. unsigned short coda_flags = coda_flags_to_cflags(flags);
  166. struct coda_file_info *cfi;
  167. struct coda_inode_info *cii;
  168. struct inode *host_inode;
  169. int err = 0;
  170. lock_kernel();
  171. coda_vfs_stat.release++;
  172. if (!use_coda_close) {
  173. err = venus_release(coda_inode->i_sb, coda_i2f(coda_inode),
  174. coda_flags);
  175. if (err == -EOPNOTSUPP) {
  176. use_coda_close = 1;
  177. err = 0;
  178. }
  179. }
  180. cfi = CODA_FTOC(coda_file);
  181. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  182. if (use_coda_close)
  183. err = venus_close(coda_inode->i_sb, coda_i2f(coda_inode),
  184. coda_flags, coda_file->f_uid);
  185. host_inode = cfi->cfi_container->f_path.dentry->d_inode;
  186. cii = ITOC(coda_inode);
  187. /* did we mmap this file? */
  188. if (coda_inode->i_mapping == &host_inode->i_data) {
  189. cii->c_mapcount -= cfi->cfi_mapcount;
  190. if (!cii->c_mapcount)
  191. coda_inode->i_mapping = &coda_inode->i_data;
  192. }
  193. fput(cfi->cfi_container);
  194. kfree(coda_file->private_data);
  195. coda_file->private_data = NULL;
  196. unlock_kernel();
  197. return err;
  198. }
  199. int coda_fsync(struct file *coda_file, struct dentry *coda_dentry, int datasync)
  200. {
  201. struct file *host_file;
  202. struct dentry *host_dentry;
  203. struct inode *host_inode, *coda_inode = coda_dentry->d_inode;
  204. struct coda_file_info *cfi;
  205. int err = 0;
  206. if (!(S_ISREG(coda_inode->i_mode) || S_ISDIR(coda_inode->i_mode) ||
  207. S_ISLNK(coda_inode->i_mode)))
  208. return -EINVAL;
  209. cfi = CODA_FTOC(coda_file);
  210. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  211. host_file = cfi->cfi_container;
  212. coda_vfs_stat.fsync++;
  213. if (host_file->f_op && host_file->f_op->fsync) {
  214. host_dentry = host_file->f_path.dentry;
  215. host_inode = host_dentry->d_inode;
  216. mutex_lock(&host_inode->i_mutex);
  217. err = host_file->f_op->fsync(host_file, host_dentry, datasync);
  218. mutex_unlock(&host_inode->i_mutex);
  219. }
  220. if ( !err && !datasync ) {
  221. lock_kernel();
  222. err = venus_fsync(coda_inode->i_sb, coda_i2f(coda_inode));
  223. unlock_kernel();
  224. }
  225. return err;
  226. }
  227. const struct file_operations coda_file_operations = {
  228. .llseek = generic_file_llseek,
  229. .read = coda_file_read,
  230. .write = coda_file_write,
  231. .mmap = coda_file_mmap,
  232. .open = coda_open,
  233. .flush = coda_flush,
  234. .release = coda_release,
  235. .fsync = coda_fsync,
  236. .splice_read = coda_file_splice_read,
  237. };