file.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * linux/fs/ext4/file.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/file.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * ext4 fs regular file handling primitives
  16. *
  17. * 64-bit file support on 64-bit platforms by Jakub Jelinek
  18. * (jj@sunsite.ms.mff.cuni.cz)
  19. */
  20. #include <linux/time.h>
  21. #include <linux/fs.h>
  22. #include <linux/jbd2.h>
  23. #include <linux/mount.h>
  24. #include <linux/path.h>
  25. #include <linux/quotaops.h>
  26. #include "ext4.h"
  27. #include "ext4_jbd2.h"
  28. #include "xattr.h"
  29. #include "acl.h"
  30. /*
  31. * Called when an inode is released. Note that this is different
  32. * from ext4_file_open: open gets called at every open, but release
  33. * gets called only when /all/ the files are closed.
  34. */
  35. static int ext4_release_file(struct inode *inode, struct file *filp)
  36. {
  37. if (ext4_test_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE)) {
  38. ext4_alloc_da_blocks(inode);
  39. ext4_clear_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
  40. }
  41. /* if we are the last writer on the inode, drop the block reservation */
  42. if ((filp->f_mode & FMODE_WRITE) &&
  43. (atomic_read(&inode->i_writecount) == 1) &&
  44. !EXT4_I(inode)->i_reserved_data_blocks)
  45. {
  46. down_write(&EXT4_I(inode)->i_data_sem);
  47. ext4_discard_preallocations(inode);
  48. up_write(&EXT4_I(inode)->i_data_sem);
  49. }
  50. if (is_dx(inode) && filp->private_data)
  51. ext4_htree_free_dir_info(filp->private_data);
  52. return 0;
  53. }
  54. static void ext4_aiodio_wait(struct inode *inode)
  55. {
  56. wait_queue_head_t *wq = ext4_ioend_wq(inode);
  57. wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_aiodio_unwritten) == 0));
  58. }
  59. /*
  60. * This tests whether the IO in question is block-aligned or not.
  61. * Ext4 utilizes unwritten extents when hole-filling during direct IO, and they
  62. * are converted to written only after the IO is complete. Until they are
  63. * mapped, these blocks appear as holes, so dio_zero_block() will assume that
  64. * it needs to zero out portions of the start and/or end block. If 2 AIO
  65. * threads are at work on the same unwritten block, they must be synchronized
  66. * or one thread will zero the other's data, causing corruption.
  67. */
  68. static int
  69. ext4_unaligned_aio(struct inode *inode, const struct iovec *iov,
  70. unsigned long nr_segs, loff_t pos)
  71. {
  72. struct super_block *sb = inode->i_sb;
  73. int blockmask = sb->s_blocksize - 1;
  74. size_t count = iov_length(iov, nr_segs);
  75. loff_t final_size = pos + count;
  76. if (pos >= ALIGN(i_size_read(inode), sb->s_blocksize))
  77. return 0;
  78. if ((pos & blockmask) || (final_size & blockmask))
  79. return 1;
  80. return 0;
  81. }
  82. static ssize_t
  83. ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
  84. unsigned long nr_segs, loff_t pos)
  85. {
  86. struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
  87. int unaligned_aio = 0;
  88. int ret;
  89. /*
  90. * If we have encountered a bitmap-format file, the size limit
  91. * is smaller than s_maxbytes, which is for extent-mapped files.
  92. */
  93. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
  94. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  95. size_t length = iov_length(iov, nr_segs);
  96. if ((pos > sbi->s_bitmap_maxbytes ||
  97. (pos == sbi->s_bitmap_maxbytes && length > 0)))
  98. return -EFBIG;
  99. if (pos + length > sbi->s_bitmap_maxbytes) {
  100. nr_segs = iov_shorten((struct iovec *)iov, nr_segs,
  101. sbi->s_bitmap_maxbytes - pos);
  102. }
  103. } else if (unlikely((iocb->ki_filp->f_flags & O_DIRECT) &&
  104. !is_sync_kiocb(iocb))) {
  105. unaligned_aio = ext4_unaligned_aio(inode, iov, nr_segs, pos);
  106. }
  107. /* Unaligned direct AIO must be serialized; see comment above */
  108. if (unaligned_aio) {
  109. static unsigned long unaligned_warn_time;
  110. /* Warn about this once per day */
  111. if (printk_timed_ratelimit(&unaligned_warn_time, 60*60*24*HZ))
  112. ext4_msg(inode->i_sb, KERN_WARNING,
  113. "Unaligned AIO/DIO on inode %ld by %s; "
  114. "performance will be poor.",
  115. inode->i_ino, current->comm);
  116. mutex_lock(ext4_aio_mutex(inode));
  117. ext4_aiodio_wait(inode);
  118. }
  119. ret = generic_file_aio_write(iocb, iov, nr_segs, pos);
  120. if (unaligned_aio)
  121. mutex_unlock(ext4_aio_mutex(inode));
  122. return ret;
  123. }
  124. static const struct vm_operations_struct ext4_file_vm_ops = {
  125. .fault = filemap_fault,
  126. .page_mkwrite = ext4_page_mkwrite,
  127. .remap_pages = generic_file_remap_pages,
  128. };
  129. static int ext4_file_mmap(struct file *file, struct vm_area_struct *vma)
  130. {
  131. struct address_space *mapping = file->f_mapping;
  132. if (!mapping->a_ops->readpage)
  133. return -ENOEXEC;
  134. file_accessed(file);
  135. #ifdef CONFIG_TIMA_RKP
  136. if (vma->vm_end - vma->vm_start) {
  137. /* iommu optimization- needs to be turned ON from
  138. * the tz side.
  139. */
  140. cpu_v7_tima_iommu_opt(vma->vm_start, vma->vm_end, (unsigned long)vma->vm_mm->pgd);
  141. __asm__ __volatile__ (
  142. "mcr p15, 0, r0, c8, c3, 0\n"
  143. "dsb\n"
  144. "isb\n");
  145. }
  146. #endif
  147. vma->vm_ops = &ext4_file_vm_ops;
  148. return 0;
  149. }
  150. static int ext4_file_open(struct inode * inode, struct file * filp)
  151. {
  152. struct super_block *sb = inode->i_sb;
  153. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  154. struct ext4_inode_info *ei = EXT4_I(inode);
  155. struct vfsmount *mnt = filp->f_path.mnt;
  156. struct path path;
  157. char buf[64], *cp;
  158. if (unlikely(!(sbi->s_mount_flags & EXT4_MF_MNTDIR_SAMPLED) &&
  159. !(sb->s_flags & MS_RDONLY))) {
  160. sbi->s_mount_flags |= EXT4_MF_MNTDIR_SAMPLED;
  161. /*
  162. * Sample where the filesystem has been mounted and
  163. * store it in the superblock for sysadmin convenience
  164. * when trying to sort through large numbers of block
  165. * devices or filesystem images.
  166. */
  167. memset(buf, 0, sizeof(buf));
  168. path.mnt = mnt;
  169. path.dentry = mnt->mnt_root;
  170. cp = d_path(&path, buf, sizeof(buf));
  171. if (!IS_ERR(cp)) {
  172. strlcpy(sbi->s_es->s_last_mounted, cp,
  173. sizeof(sbi->s_es->s_last_mounted));
  174. ext4_mark_super_dirty(sb);
  175. }
  176. }
  177. /*
  178. * Set up the jbd2_inode if we are opening the inode for
  179. * writing and the journal is present
  180. */
  181. if (sbi->s_journal && !ei->jinode && (filp->f_mode & FMODE_WRITE)) {
  182. struct jbd2_inode *jinode = jbd2_alloc_inode(GFP_KERNEL);
  183. spin_lock(&inode->i_lock);
  184. if (!ei->jinode) {
  185. if (!jinode) {
  186. spin_unlock(&inode->i_lock);
  187. return -ENOMEM;
  188. }
  189. ei->jinode = jinode;
  190. jbd2_journal_init_jbd_inode(ei->jinode, inode);
  191. jinode = NULL;
  192. }
  193. spin_unlock(&inode->i_lock);
  194. if (unlikely(jinode != NULL))
  195. jbd2_free_inode(jinode);
  196. }
  197. return dquot_file_open(inode, filp);
  198. }
  199. /*
  200. * ext4_llseek() copied from generic_file_llseek() to handle both
  201. * block-mapped and extent-mapped maxbytes values. This should
  202. * otherwise be identical with generic_file_llseek().
  203. */
  204. loff_t ext4_llseek(struct file *file, loff_t offset, int origin)
  205. {
  206. struct inode *inode = file->f_mapping->host;
  207. loff_t maxbytes;
  208. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  209. maxbytes = EXT4_SB(inode->i_sb)->s_bitmap_maxbytes;
  210. else
  211. maxbytes = inode->i_sb->s_maxbytes;
  212. return generic_file_llseek_size(file, offset, origin, maxbytes);
  213. }
  214. const struct file_operations ext4_file_operations = {
  215. .llseek = ext4_llseek,
  216. .read = do_sync_read,
  217. .write = do_sync_write,
  218. .aio_read = generic_file_aio_read,
  219. .aio_write = ext4_file_write,
  220. .unlocked_ioctl = ext4_ioctl,
  221. #ifdef CONFIG_COMPAT
  222. .compat_ioctl = ext4_compat_ioctl,
  223. #endif
  224. .mmap = ext4_file_mmap,
  225. .open = ext4_file_open,
  226. .release = ext4_release_file,
  227. .fsync = ext4_sync_file,
  228. .splice_read = generic_file_splice_read,
  229. .splice_write = generic_file_splice_write,
  230. .fallocate = ext4_fallocate,
  231. };
  232. const struct inode_operations ext4_file_inode_operations = {
  233. .setattr = ext4_setattr,
  234. .getattr = ext4_getattr,
  235. #ifdef CONFIG_EXT4_FS_XATTR
  236. .setxattr = generic_setxattr,
  237. .getxattr = generic_getxattr,
  238. .listxattr = ext4_listxattr,
  239. .removexattr = generic_removexattr,
  240. #endif
  241. .get_acl = ext4_get_acl,
  242. .fiemap = ext4_fiemap,
  243. };