file.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * fs/scfs/file.c
  3. *
  4. * Copyright (C) 2014 Samsung Electronics Co., Ltd.
  5. * Authors: Sunghwan Yun <sunghwan.yun@samsung.com>
  6. * Jongmin Kim <jm45.kim@samsung.com>
  7. * Sangwoo Lee <sangwoo2.lee@samsung.com>
  8. * Inbae Lee <inbae.lee@samsung.com>
  9. *
  10. * This program has been developed as a stackable file system based on
  11. * the WrapFS, which was written by:
  12. *
  13. * Copyright (C) 1997-2003 Erez Zadok
  14. * Copyright (C) 2001-2003 Stony Brook University
  15. * Copyright (C) 2004-2006 International Business Machines Corp.
  16. * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
  17. * Michael C. Thompson <mcthomps@us.ibm.com>
  18. *
  19. * This program is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation, either version 2 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful, but
  25. * WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  31. */
  32. #include "scfs.h"
  33. /*
  34. * Check validity of cinfo data(array).
  35. * It is called in scfs_open, failed, the file is treated non-compressed,
  36. * such as the one have no 'footer'.
  37. */
  38. int scfs_check_cinfo(struct scfs_inode_info *sii, void *buf)
  39. {
  40. struct scfs_cinfo *cinfo = buf;
  41. int prev_last_offset = 0;
  42. int cinfo_size = sii->cinfo_array_size;
  43. for (cinfo = buf; (unsigned long)cinfo < (unsigned long)buf + cinfo_size; cinfo++) {
  44. if (cinfo->offset < prev_last_offset || !cinfo->size ||
  45. cinfo->size > sii->cluster_size) {
  46. SCFS_PRINT("invalid cinfo, prev_last_offset : %d, "
  47. "offset : %d, size : %d\n", prev_last_offset,
  48. cinfo->offset, cinfo->size);
  49. return -1;
  50. }
  51. prev_last_offset = cinfo->offset + cinfo->size;
  52. }
  53. return 0;
  54. }
  55. static int scfs_open(struct inode *inode, struct file *file)
  56. {
  57. struct scfs_sb_info *sbi = SCFS_S(inode->i_sb);
  58. struct scfs_inode_info *sii = SCFS_I(inode);
  59. struct scfs_file_info *fi;
  60. int ret = 0;
  61. struct file *lower_file;
  62. if (IS_WROPENED(sii)) {
  63. SCFS_PRINT("This file is already opened with 'WRITE' flag\n");
  64. return -EPERM;
  65. }
  66. fi = kmem_cache_zalloc(scfs_file_info_cache, GFP_KERNEL);
  67. if (!fi)
  68. return -ENOMEM;
  69. profile_add_kmcached(sizeof(struct scfs_file_info), sbi);
  70. file->private_data = fi;
  71. mutex_lock(&sii->cinfo_mutex);
  72. if (IS_INVALID_META(sii)) {
  73. SCFS_PRINT("meta is invalid, so we should re-load it\n");
  74. ret = scfs_reload_meta(file);
  75. if (ret) {
  76. SCFS_PRINT_ERROR("error in re-reading footer, err : %d\n", ret);
  77. goto out;
  78. }
  79. } else if (sii->compressed && !sii->cinfo_array) {
  80. /* 1st lower-open is for getting cinfo */
  81. ret = scfs_initialize_lower_file(file->f_dentry, &lower_file, O_RDONLY);
  82. if (ret) {
  83. SCFS_PRINT_ERROR("err in get_lower_file %s\n",
  84. file->f_dentry->d_name.name);
  85. goto out;
  86. }
  87. scfs_set_lower_file(file, lower_file);
  88. SCFS_PRINT("info size = %d \n", sii->cinfo_array_size);
  89. ret = scfs_load_cinfo(sii, lower_file);
  90. if (ret) {
  91. SCFS_PRINT_ERROR("err in loading cinfo, ret : %d\n",
  92. file->f_dentry->d_name.name);
  93. fput(lower_file);
  94. goto out;
  95. }
  96. fput(lower_file);
  97. }
  98. ret = scfs_initialize_lower_file(file->f_dentry, &lower_file, file->f_flags);
  99. if (ret) {
  100. SCFS_PRINT_ERROR("err in get_lower_file %s\n",
  101. file->f_dentry->d_name.name);
  102. goto out;
  103. }
  104. scfs_set_lower_file(file, lower_file);
  105. out:
  106. if (!ret) {
  107. fsstack_copy_attr_all(inode, scfs_lower_inode(inode));
  108. if (file->f_flags & (O_RDWR | O_WRONLY))
  109. MAKE_WROPENED(sii);
  110. } else {
  111. scfs_set_lower_file(file, NULL);
  112. kmem_cache_free(scfs_file_info_cache, file->private_data);
  113. profile_sub_kmcached(sizeof(struct scfs_file_info), sbi);
  114. sii->cinfo_array = NULL;
  115. }
  116. mutex_unlock(&sii->cinfo_mutex);
  117. SCFS_PRINT("lower, dentry name : %s, count : %d, ret : %d\n",
  118. file->f_dentry->d_name.name, file->f_dentry->d_count, ret);
  119. return ret;
  120. }
  121. /*
  122. * scfs_file_release
  123. */
  124. static int scfs_file_release(struct inode *inode, struct file *file)
  125. {
  126. int ret;
  127. SCFS_PRINT("f:%s calling fput with lower_file\n",
  128. file->f_path.dentry->d_name.name);
  129. if (file->f_flags & (O_RDWR | O_WRONLY)) {
  130. CLEAR_WROPENED(SCFS_I(inode));
  131. ret = scfs_write_meta(file);
  132. if (ret)
  133. return ret;
  134. }
  135. fput(SCFS_F(file)->lower_file);
  136. kmem_cache_free(scfs_file_info_cache, SCFS_F(file));
  137. profile_sub_kmcached(sizeof(struct scfs_file_info), SCFS_S(inode->i_sb));
  138. return 0;
  139. }
  140. /*
  141. * scfs_readdir
  142. */
  143. static int scfs_readdir(struct file *file, void *dirent, filldir_t filldir)
  144. {
  145. struct file *lower_file = NULL;
  146. struct dentry *dentry = file->f_path.dentry;
  147. int ret = 0;
  148. lower_file = scfs_lower_file(file);
  149. lower_file->f_pos = file->f_pos;
  150. ret = vfs_readdir(lower_file, filldir, dirent);
  151. file->f_pos = lower_file->f_pos;
  152. if (ret >= 0)
  153. fsstack_copy_attr_atime(dentry->d_inode, lower_file->f_path.dentry->d_inode);
  154. return ret;
  155. }
  156. /*
  157. * scfs_unlocked_ioctl
  158. */
  159. static long scfs_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  160. {
  161. struct file *lower_file;
  162. long ret = -ENOENT;
  163. lower_file = scfs_lower_file(file);
  164. if (!lower_file || !lower_file->f_op)
  165. goto out;
  166. if (lower_file->f_op->unlocked_ioctl)
  167. ret = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
  168. out:
  169. return ret;
  170. }
  171. #ifdef CONFIG_COMPAT
  172. /*
  173. * scfs_compat_ioctl
  174. */
  175. static long scfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  176. {
  177. struct file *lower_file;
  178. long ret = -ENOIOCTLCMD;
  179. lower_file = scfs_lower_file(file);
  180. if (!lower_file || !lower_file->f_op)
  181. goto out;
  182. if (lower_file->f_op->compat_ioctl)
  183. ret = lower_file->f_op->compat_ioctl(lower_file, cmd, arg);
  184. out:
  185. return ret;
  186. }
  187. #endif
  188. static int scfs_flush(struct file *file, fl_owner_t id)
  189. {
  190. struct file *lower_file = NULL;
  191. int ret = 0;
  192. lower_file = scfs_lower_file(file);
  193. if (lower_file && lower_file->f_op && lower_file->f_op->flush)
  194. ret = lower_file->f_op->flush(lower_file, id);
  195. return ret;
  196. }
  197. static int scfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  198. {
  199. int ret = 0;
  200. ret = scfs_write_meta(file);
  201. if(ret)
  202. return ret;
  203. #ifdef SCFS_MULTI_THREAD_COMPRESSION
  204. // scfs_write_compress_all_cluster(SCFS_I(file->f_path.dentry->d_inode));
  205. #endif
  206. ret = vfs_fsync(scfs_lower_file(file), datasync);
  207. return ret;
  208. }
  209. static int scfs_fasync(int fd, struct file *file, int flag)
  210. {
  211. struct file *lower_file = NULL;
  212. int ret = 0;
  213. lower_file = scfs_lower_file(file);
  214. if (lower_file->f_op && lower_file->f_op->fasync)
  215. ret = lower_file->f_op->fasync(fd, lower_file, flag);
  216. return ret;
  217. }
  218. static const struct vm_operations_struct scfs_file_vm_ops = {
  219. .fault = filemap_fault,
  220. };
  221. /*
  222. * SCFS doesn't have a writepage, so write with mmap has no effect.
  223. * First implementation was returning error when having VM_WRITE,
  224. * but some process in boot sequence uses mmap with VM_WRITE
  225. * - without write, just with flag - so, now using VM_WRITE is
  226. * available.
  227. */
  228. static int scfs_mmap(struct file *file, struct vm_area_struct *vma)
  229. {
  230. struct address_space *mapping = file->f_mapping;
  231. if (!mapping->a_ops->readpage)
  232. return -ENOEXEC;
  233. SCFS_PRINT("file %s\n", file->f_path.dentry->d_name.name);
  234. //if (file->f_mode & FMODE_WRITE) {
  235. /*
  236. if (vma->vm_flags & VM_WRITE) {
  237. SCFS_PRINT_ERROR("f_mode WRITE was set! error. "
  238. "f_mode %x (FMODE_READ: %x FMODE_WRITE %x)\n",
  239. file->f_mode,
  240. file->f_mode & FMODE_READ,
  241. file->f_mode & FMODE_WRITE);
  242. SCFS_PRINT_ERROR("filename : %s\n", file->f_path.dentry->d_name.name);
  243. return -EPERM;
  244. }
  245. */
  246. file_accessed(file);
  247. vma->vm_ops = &scfs_file_vm_ops;
  248. #if LINUX_VERSION_CODE < KERNEL_VERSION(3,10,0)
  249. vma->vm_flags |= VM_CAN_NONLINEAR;
  250. #endif
  251. SCFS_PRINT("VM flags: %lx "
  252. "EXEC %lx IO %lx "
  253. "SEQ %lx RAND %lx "
  254. "READ %lx MAYREAD %lx "
  255. "WRITE %lx MAYWRITE %lx "
  256. "SHARED %lx MAYSHARE %lx\n",
  257. vma->vm_flags,
  258. vma->vm_flags & VM_EXECUTABLE, vma->vm_flags & VM_IO,
  259. vma->vm_flags & VM_SEQ_READ, vma->vm_flags & VM_RAND_READ,
  260. vma->vm_flags & VM_READ, vma->vm_flags & VM_MAYREAD,
  261. vma->vm_flags & VM_WRITE, vma->vm_flags & VM_MAYWRITE,
  262. vma->vm_flags & VM_SHARED, vma->vm_flags & VM_MAYSHARE);
  263. if (vma->vm_flags & VM_WRITE) {
  264. SCFS_PRINT("VM_WRITE: file %s flags %lx VM_MAYWRITE %lx\n",
  265. file->f_path.dentry->d_name.name,
  266. vma->vm_flags,
  267. vma->vm_flags & VM_MAYWRITE);
  268. }
  269. return 0;
  270. }
  271. /*****************************/
  272. /* file_operations structres */
  273. /*****************************/
  274. const struct file_operations scfs_dir_fops = {
  275. .llseek = default_llseek,
  276. .read = generic_read_dir,
  277. .readdir = scfs_readdir,
  278. .unlocked_ioctl = scfs_unlocked_ioctl,
  279. #ifdef CONFIG_COMPAT
  280. .compat_ioctl = scfs_compat_ioctl,
  281. #endif
  282. .open = scfs_open,
  283. .release = scfs_file_release,
  284. .flush = scfs_flush,
  285. .fsync = scfs_fsync,
  286. .fasync = scfs_fasync,
  287. };
  288. const struct file_operations scfs_file_fops = {
  289. .llseek = generic_file_llseek,
  290. .read = do_sync_read,
  291. .aio_read = generic_file_aio_read,
  292. .write = do_sync_write,
  293. .aio_write = generic_file_aio_write,
  294. .unlocked_ioctl = scfs_unlocked_ioctl,
  295. #ifdef CONFIG_COMPAT
  296. .compat_ioctl = scfs_compat_ioctl,
  297. #endif
  298. .mmap = scfs_mmap,
  299. .open = scfs_open,
  300. .release = scfs_file_release,
  301. .flush = scfs_flush,
  302. .fsync = scfs_fsync,
  303. .fasync = scfs_fasync,
  304. };