file.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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_sendfile(struct file *coda_file, loff_t *ppos, size_t count,
  42. read_actor_t actor, void *target)
  43. {
  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. if (!host_file->f_op || !host_file->f_op->sendfile)
  50. return -EINVAL;
  51. return host_file->f_op->sendfile(host_file, ppos, count, actor, target);
  52. }
  53. static ssize_t
  54. coda_file_write(struct file *coda_file, const char __user *buf, size_t count, loff_t *ppos)
  55. {
  56. struct inode *host_inode, *coda_inode = coda_file->f_path.dentry->d_inode;
  57. struct coda_file_info *cfi;
  58. struct file *host_file;
  59. ssize_t ret;
  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 || !host_file->f_op->write)
  64. return -EINVAL;
  65. host_inode = host_file->f_path.dentry->d_inode;
  66. mutex_lock(&coda_inode->i_mutex);
  67. ret = host_file->f_op->write(host_file, buf, count, ppos);
  68. coda_inode->i_size = host_inode->i_size;
  69. coda_inode->i_blocks = (coda_inode->i_size + 511) >> 9;
  70. coda_inode->i_mtime = coda_inode->i_ctime = CURRENT_TIME_SEC;
  71. mutex_unlock(&coda_inode->i_mutex);
  72. return ret;
  73. }
  74. static int
  75. coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
  76. {
  77. struct coda_file_info *cfi;
  78. struct coda_inode_info *cii;
  79. struct file *host_file;
  80. struct inode *coda_inode, *host_inode;
  81. cfi = CODA_FTOC(coda_file);
  82. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  83. host_file = cfi->cfi_container;
  84. if (!host_file->f_op || !host_file->f_op->mmap)
  85. return -ENODEV;
  86. coda_inode = coda_file->f_path.dentry->d_inode;
  87. host_inode = host_file->f_path.dentry->d_inode;
  88. coda_file->f_mapping = host_file->f_mapping;
  89. if (coda_inode->i_mapping == &coda_inode->i_data)
  90. coda_inode->i_mapping = host_inode->i_mapping;
  91. /* only allow additional mmaps as long as userspace isn't changing
  92. * the container file on us! */
  93. else if (coda_inode->i_mapping != host_inode->i_mapping)
  94. return -EBUSY;
  95. /* keep track of how often the coda_inode/host_file has been mmapped */
  96. cii = ITOC(coda_inode);
  97. cii->c_mapcount++;
  98. cfi->cfi_mapcount++;
  99. return host_file->f_op->mmap(host_file, vma);
  100. }
  101. int coda_open(struct inode *coda_inode, struct file *coda_file)
  102. {
  103. struct file *host_file = NULL;
  104. int error;
  105. unsigned short flags = coda_file->f_flags & (~O_EXCL);
  106. unsigned short coda_flags = coda_flags_to_cflags(flags);
  107. struct coda_file_info *cfi;
  108. coda_vfs_stat.open++;
  109. cfi = kmalloc(sizeof(struct coda_file_info), GFP_KERNEL);
  110. if (!cfi)
  111. return -ENOMEM;
  112. lock_kernel();
  113. error = venus_open(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags,
  114. &host_file);
  115. if (error || !host_file) {
  116. kfree(cfi);
  117. unlock_kernel();
  118. return error;
  119. }
  120. host_file->f_flags |= coda_file->f_flags & (O_APPEND | O_SYNC);
  121. cfi->cfi_magic = CODA_MAGIC;
  122. cfi->cfi_mapcount = 0;
  123. cfi->cfi_container = host_file;
  124. BUG_ON(coda_file->private_data != NULL);
  125. coda_file->private_data = cfi;
  126. unlock_kernel();
  127. return 0;
  128. }
  129. int coda_flush(struct file *coda_file, fl_owner_t id)
  130. {
  131. unsigned short flags = coda_file->f_flags & ~O_EXCL;
  132. unsigned short coda_flags = coda_flags_to_cflags(flags);
  133. struct coda_file_info *cfi;
  134. struct inode *coda_inode;
  135. int err = 0, fcnt;
  136. lock_kernel();
  137. coda_vfs_stat.flush++;
  138. /* last close semantics */
  139. fcnt = file_count(coda_file);
  140. if (fcnt > 1)
  141. goto out;
  142. /* No need to make an upcall when we have not made any modifications
  143. * to the file */
  144. if ((coda_file->f_flags & O_ACCMODE) == O_RDONLY)
  145. goto out;
  146. if (use_coda_close)
  147. goto out;
  148. cfi = CODA_FTOC(coda_file);
  149. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  150. coda_inode = coda_file->f_path.dentry->d_inode;
  151. err = venus_store(coda_inode->i_sb, coda_i2f(coda_inode), coda_flags,
  152. coda_file->f_uid);
  153. if (err == -EOPNOTSUPP) {
  154. use_coda_close = 1;
  155. err = 0;
  156. }
  157. out:
  158. unlock_kernel();
  159. return err;
  160. }
  161. int coda_release(struct inode *coda_inode, struct file *coda_file)
  162. {
  163. unsigned short flags = (coda_file->f_flags) & (~O_EXCL);
  164. unsigned short coda_flags = coda_flags_to_cflags(flags);
  165. struct coda_file_info *cfi;
  166. struct coda_inode_info *cii;
  167. struct inode *host_inode;
  168. int err = 0;
  169. lock_kernel();
  170. coda_vfs_stat.release++;
  171. if (!use_coda_close) {
  172. err = venus_release(coda_inode->i_sb, coda_i2f(coda_inode),
  173. coda_flags);
  174. if (err == -EOPNOTSUPP) {
  175. use_coda_close = 1;
  176. err = 0;
  177. }
  178. }
  179. cfi = CODA_FTOC(coda_file);
  180. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  181. if (use_coda_close)
  182. err = venus_close(coda_inode->i_sb, coda_i2f(coda_inode),
  183. coda_flags, coda_file->f_uid);
  184. host_inode = cfi->cfi_container->f_path.dentry->d_inode;
  185. cii = ITOC(coda_inode);
  186. /* did we mmap this file? */
  187. if (coda_inode->i_mapping == &host_inode->i_data) {
  188. cii->c_mapcount -= cfi->cfi_mapcount;
  189. if (!cii->c_mapcount)
  190. coda_inode->i_mapping = &coda_inode->i_data;
  191. }
  192. fput(cfi->cfi_container);
  193. kfree(coda_file->private_data);
  194. coda_file->private_data = NULL;
  195. unlock_kernel();
  196. return err;
  197. }
  198. int coda_fsync(struct file *coda_file, struct dentry *coda_dentry, int datasync)
  199. {
  200. struct file *host_file;
  201. struct dentry *host_dentry;
  202. struct inode *host_inode, *coda_inode = coda_dentry->d_inode;
  203. struct coda_file_info *cfi;
  204. int err = 0;
  205. if (!(S_ISREG(coda_inode->i_mode) || S_ISDIR(coda_inode->i_mode) ||
  206. S_ISLNK(coda_inode->i_mode)))
  207. return -EINVAL;
  208. cfi = CODA_FTOC(coda_file);
  209. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  210. host_file = cfi->cfi_container;
  211. coda_vfs_stat.fsync++;
  212. if (host_file->f_op && host_file->f_op->fsync) {
  213. host_dentry = host_file->f_path.dentry;
  214. host_inode = host_dentry->d_inode;
  215. mutex_lock(&host_inode->i_mutex);
  216. err = host_file->f_op->fsync(host_file, host_dentry, datasync);
  217. mutex_unlock(&host_inode->i_mutex);
  218. }
  219. if ( !err && !datasync ) {
  220. lock_kernel();
  221. err = venus_fsync(coda_inode->i_sb, coda_i2f(coda_inode));
  222. unlock_kernel();
  223. }
  224. return err;
  225. }
  226. const struct file_operations coda_file_operations = {
  227. .llseek = generic_file_llseek,
  228. .read = coda_file_read,
  229. .write = coda_file_write,
  230. .mmap = coda_file_mmap,
  231. .open = coda_open,
  232. .flush = coda_flush,
  233. .release = coda_release,
  234. .fsync = coda_fsync,
  235. .sendfile = coda_file_sendfile,
  236. };