file.c 7.3 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. /* if CODA_STORE fails with EOPNOTSUPP, venus clearly doesn't support
  25. * CODA_STORE/CODA_RELEASE and we fall back on using the CODA_CLOSE upcall */
  26. static int use_coda_close;
  27. static ssize_t
  28. coda_file_read(struct file *coda_file, char __user *buf, size_t count, loff_t *ppos)
  29. {
  30. struct coda_file_info *cfi;
  31. struct file *host_file;
  32. cfi = CODA_FTOC(coda_file);
  33. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  34. host_file = cfi->cfi_container;
  35. if (!host_file->f_op || !host_file->f_op->read)
  36. return -EINVAL;
  37. return host_file->f_op->read(host_file, buf, count, ppos);
  38. }
  39. static ssize_t
  40. coda_file_sendfile(struct file *coda_file, loff_t *ppos, size_t count,
  41. read_actor_t actor, void *target)
  42. {
  43. struct coda_file_info *cfi;
  44. struct file *host_file;
  45. cfi = CODA_FTOC(coda_file);
  46. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  47. host_file = cfi->cfi_container;
  48. if (!host_file->f_op || !host_file->f_op->sendfile)
  49. return -EINVAL;
  50. return host_file->f_op->sendfile(host_file, ppos, count, actor, target);
  51. }
  52. static ssize_t
  53. coda_file_write(struct file *coda_file, const char __user *buf, size_t count, loff_t *ppos)
  54. {
  55. struct inode *host_inode, *coda_inode = coda_file->f_dentry->d_inode;
  56. struct coda_file_info *cfi;
  57. struct file *host_file;
  58. ssize_t ret;
  59. cfi = CODA_FTOC(coda_file);
  60. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  61. host_file = cfi->cfi_container;
  62. if (!host_file->f_op || !host_file->f_op->write)
  63. return -EINVAL;
  64. host_inode = host_file->f_dentry->d_inode;
  65. mutex_lock(&coda_inode->i_mutex);
  66. ret = host_file->f_op->write(host_file, buf, count, ppos);
  67. coda_inode->i_size = host_inode->i_size;
  68. coda_inode->i_blocks = (coda_inode->i_size + 511) >> 9;
  69. coda_inode->i_mtime = coda_inode->i_ctime = CURRENT_TIME_SEC;
  70. mutex_unlock(&coda_inode->i_mutex);
  71. return ret;
  72. }
  73. static int
  74. coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
  75. {
  76. struct coda_file_info *cfi;
  77. struct coda_inode_info *cii;
  78. struct file *host_file;
  79. struct inode *coda_inode, *host_inode;
  80. cfi = CODA_FTOC(coda_file);
  81. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  82. host_file = cfi->cfi_container;
  83. if (!host_file->f_op || !host_file->f_op->mmap)
  84. return -ENODEV;
  85. coda_inode = coda_file->f_dentry->d_inode;
  86. host_inode = host_file->f_dentry->d_inode;
  87. coda_file->f_mapping = host_file->f_mapping;
  88. if (coda_inode->i_mapping == &coda_inode->i_data)
  89. coda_inode->i_mapping = host_inode->i_mapping;
  90. /* only allow additional mmaps as long as userspace isn't changing
  91. * the container file on us! */
  92. else if (coda_inode->i_mapping != host_inode->i_mapping)
  93. return -EBUSY;
  94. /* keep track of how often the coda_inode/host_file has been mmapped */
  95. cii = ITOC(coda_inode);
  96. cii->c_mapcount++;
  97. cfi->cfi_mapcount++;
  98. return host_file->f_op->mmap(host_file, vma);
  99. }
  100. int coda_open(struct inode *coda_inode, struct file *coda_file)
  101. {
  102. struct file *host_file = NULL;
  103. int error;
  104. unsigned short flags = coda_file->f_flags & (~O_EXCL);
  105. unsigned short coda_flags = coda_flags_to_cflags(flags);
  106. struct coda_file_info *cfi;
  107. coda_vfs_stat.open++;
  108. cfi = kmalloc(sizeof(struct coda_file_info), GFP_KERNEL);
  109. if (!cfi) {
  110. unlock_kernel();
  111. return -ENOMEM;
  112. }
  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)
  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_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_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_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. 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. .sendfile = coda_file_sendfile,
  237. };