file.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 >= inode->i_size)
  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. };
  128. static int ext4_file_mmap(struct file *file, struct vm_area_struct *vma)
  129. {
  130. struct address_space *mapping = file->f_mapping;
  131. if (!mapping->a_ops->readpage)
  132. return -ENOEXEC;
  133. file_accessed(file);
  134. vma->vm_ops = &ext4_file_vm_ops;
  135. vma->vm_flags |= VM_CAN_NONLINEAR;
  136. return 0;
  137. }
  138. static int ext4_file_open(struct inode * inode, struct file * filp)
  139. {
  140. struct super_block *sb = inode->i_sb;
  141. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  142. struct ext4_inode_info *ei = EXT4_I(inode);
  143. struct vfsmount *mnt = filp->f_path.mnt;
  144. struct path path;
  145. char buf[64], *cp;
  146. if (unlikely(!(sbi->s_mount_flags & EXT4_MF_MNTDIR_SAMPLED) &&
  147. !(sb->s_flags & MS_RDONLY))) {
  148. sbi->s_mount_flags |= EXT4_MF_MNTDIR_SAMPLED;
  149. /*
  150. * Sample where the filesystem has been mounted and
  151. * store it in the superblock for sysadmin convenience
  152. * when trying to sort through large numbers of block
  153. * devices or filesystem images.
  154. */
  155. memset(buf, 0, sizeof(buf));
  156. path.mnt = mnt;
  157. path.dentry = mnt->mnt_root;
  158. cp = d_path(&path, buf, sizeof(buf));
  159. if (!IS_ERR(cp)) {
  160. memcpy(sbi->s_es->s_last_mounted, cp,
  161. sizeof(sbi->s_es->s_last_mounted));
  162. ext4_mark_super_dirty(sb);
  163. }
  164. }
  165. /*
  166. * Set up the jbd2_inode if we are opening the inode for
  167. * writing and the journal is present
  168. */
  169. if (sbi->s_journal && !ei->jinode && (filp->f_mode & FMODE_WRITE)) {
  170. struct jbd2_inode *jinode = jbd2_alloc_inode(GFP_KERNEL);
  171. spin_lock(&inode->i_lock);
  172. if (!ei->jinode) {
  173. if (!jinode) {
  174. spin_unlock(&inode->i_lock);
  175. return -ENOMEM;
  176. }
  177. ei->jinode = jinode;
  178. jbd2_journal_init_jbd_inode(ei->jinode, inode);
  179. jinode = NULL;
  180. }
  181. spin_unlock(&inode->i_lock);
  182. if (unlikely(jinode != NULL))
  183. jbd2_free_inode(jinode);
  184. }
  185. return dquot_file_open(inode, filp);
  186. }
  187. /*
  188. * ext4_llseek() copied from generic_file_llseek() to handle both
  189. * block-mapped and extent-mapped maxbytes values. This should
  190. * otherwise be identical with generic_file_llseek().
  191. */
  192. loff_t ext4_llseek(struct file *file, loff_t offset, int origin)
  193. {
  194. struct inode *inode = file->f_mapping->host;
  195. loff_t maxbytes;
  196. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  197. maxbytes = EXT4_SB(inode->i_sb)->s_bitmap_maxbytes;
  198. else
  199. maxbytes = inode->i_sb->s_maxbytes;
  200. mutex_lock(&inode->i_mutex);
  201. switch (origin) {
  202. case SEEK_END:
  203. offset += inode->i_size;
  204. break;
  205. case SEEK_CUR:
  206. if (offset == 0) {
  207. mutex_unlock(&inode->i_mutex);
  208. return file->f_pos;
  209. }
  210. offset += file->f_pos;
  211. break;
  212. }
  213. if (offset < 0 || offset > maxbytes) {
  214. mutex_unlock(&inode->i_mutex);
  215. return -EINVAL;
  216. }
  217. if (offset != file->f_pos) {
  218. file->f_pos = offset;
  219. file->f_version = 0;
  220. }
  221. mutex_unlock(&inode->i_mutex);
  222. return offset;
  223. }
  224. const struct file_operations ext4_file_operations = {
  225. .llseek = ext4_llseek,
  226. .read = do_sync_read,
  227. .write = do_sync_write,
  228. .aio_read = generic_file_aio_read,
  229. .aio_write = ext4_file_write,
  230. .unlocked_ioctl = ext4_ioctl,
  231. #ifdef CONFIG_COMPAT
  232. .compat_ioctl = ext4_compat_ioctl,
  233. #endif
  234. .mmap = ext4_file_mmap,
  235. .open = ext4_file_open,
  236. .release = ext4_release_file,
  237. .fsync = ext4_sync_file,
  238. .splice_read = generic_file_splice_read,
  239. .splice_write = generic_file_splice_write,
  240. .fallocate = ext4_fallocate,
  241. };
  242. const struct inode_operations ext4_file_inode_operations = {
  243. .setattr = ext4_setattr,
  244. .getattr = ext4_getattr,
  245. #ifdef CONFIG_EXT4_FS_XATTR
  246. .setxattr = generic_setxattr,
  247. .getxattr = generic_getxattr,
  248. .listxattr = ext4_listxattr,
  249. .removexattr = generic_removexattr,
  250. #endif
  251. .check_acl = ext4_check_acl,
  252. .fiemap = ext4_fiemap,
  253. };