file.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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_dio_write(struct kiocb *iocb, const struct iovec *iov,
  84. unsigned long nr_segs, loff_t pos)
  85. {
  86. struct file *file = iocb->ki_filp;
  87. struct inode *inode = file->f_mapping->host;
  88. struct blk_plug plug;
  89. int unaligned_aio = 0;
  90. ssize_t ret;
  91. int overwrite = 0;
  92. size_t length = iov_length(iov, nr_segs);
  93. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) &&
  94. !is_sync_kiocb(iocb))
  95. unaligned_aio = ext4_unaligned_aio(inode, iov, nr_segs, pos);
  96. /* Unaligned direct AIO must be serialized; see comment above */
  97. if (unaligned_aio) {
  98. static unsigned long unaligned_warn_time;
  99. /* Warn about this once per day */
  100. if (printk_timed_ratelimit(&unaligned_warn_time, 60*60*24*HZ))
  101. ext4_msg(inode->i_sb, KERN_WARNING,
  102. "Unaligned AIO/DIO on inode %ld by %s; "
  103. "performance will be poor.",
  104. inode->i_ino, current->comm);
  105. mutex_lock(ext4_aio_mutex(inode));
  106. ext4_aiodio_wait(inode);
  107. }
  108. BUG_ON(iocb->ki_pos != pos);
  109. mutex_lock(&inode->i_mutex);
  110. blk_start_plug(&plug);
  111. iocb->private = &overwrite;
  112. /* check whether we do a DIO overwrite or not */
  113. if (ext4_should_dioread_nolock(inode) && !unaligned_aio &&
  114. !file->f_mapping->nrpages && pos + length <= i_size_read(inode)) {
  115. struct ext4_map_blocks map;
  116. unsigned int blkbits = inode->i_blkbits;
  117. int err, len;
  118. map.m_lblk = pos >> blkbits;
  119. map.m_len = (EXT4_BLOCK_ALIGN(pos + length, blkbits) >> blkbits)
  120. - map.m_lblk;
  121. len = map.m_len;
  122. err = ext4_map_blocks(NULL, inode, &map, 0);
  123. /*
  124. * 'err==len' means that all of blocks has been preallocated no
  125. * matter they are initialized or not. For excluding
  126. * uninitialized extents, we need to check m_flags. There are
  127. * two conditions that indicate for initialized extents.
  128. * 1) If we hit extent cache, EXT4_MAP_MAPPED flag is returned;
  129. * 2) If we do a real lookup, non-flags are returned.
  130. * So we should check these two conditions.
  131. */
  132. if (err == len && (map.m_flags & EXT4_MAP_MAPPED))
  133. overwrite = 1;
  134. }
  135. ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
  136. mutex_unlock(&inode->i_mutex);
  137. if (ret > 0 || ret == -EIOCBQUEUED) {
  138. ssize_t err;
  139. err = generic_write_sync(file, pos, ret);
  140. if (err < 0 && ret > 0)
  141. ret = err;
  142. }
  143. blk_finish_plug(&plug);
  144. if (unaligned_aio)
  145. mutex_unlock(ext4_aio_mutex(inode));
  146. return ret;
  147. }
  148. static ssize_t
  149. ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
  150. unsigned long nr_segs, loff_t pos)
  151. {
  152. struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
  153. ssize_t ret;
  154. /*
  155. * If we have encountered a bitmap-format file, the size limit
  156. * is smaller than s_maxbytes, which is for extent-mapped files.
  157. */
  158. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
  159. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  160. size_t length = iov_length(iov, nr_segs);
  161. if ((pos > sbi->s_bitmap_maxbytes ||
  162. (pos == sbi->s_bitmap_maxbytes && length > 0)))
  163. return -EFBIG;
  164. if (pos + length > sbi->s_bitmap_maxbytes) {
  165. nr_segs = iov_shorten((struct iovec *)iov, nr_segs,
  166. sbi->s_bitmap_maxbytes - pos);
  167. }
  168. }
  169. if (unlikely(iocb->ki_filp->f_flags & O_DIRECT))
  170. ret = ext4_file_dio_write(iocb, iov, nr_segs, pos);
  171. else
  172. ret = generic_file_aio_write(iocb, iov, nr_segs, pos);
  173. return ret;
  174. }
  175. static const struct vm_operations_struct ext4_file_vm_ops = {
  176. .fault = filemap_fault,
  177. .page_mkwrite = ext4_page_mkwrite,
  178. };
  179. static int ext4_file_mmap(struct file *file, struct vm_area_struct *vma)
  180. {
  181. struct address_space *mapping = file->f_mapping;
  182. if (!mapping->a_ops->readpage)
  183. return -ENOEXEC;
  184. file_accessed(file);
  185. vma->vm_ops = &ext4_file_vm_ops;
  186. vma->vm_flags |= VM_CAN_NONLINEAR;
  187. return 0;
  188. }
  189. static int ext4_file_open(struct inode * inode, struct file * filp)
  190. {
  191. struct super_block *sb = inode->i_sb;
  192. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  193. struct ext4_inode_info *ei = EXT4_I(inode);
  194. struct vfsmount *mnt = filp->f_path.mnt;
  195. struct path path;
  196. char buf[64], *cp;
  197. if (unlikely(!(sbi->s_mount_flags & EXT4_MF_MNTDIR_SAMPLED) &&
  198. !(sb->s_flags & MS_RDONLY))) {
  199. sbi->s_mount_flags |= EXT4_MF_MNTDIR_SAMPLED;
  200. /*
  201. * Sample where the filesystem has been mounted and
  202. * store it in the superblock for sysadmin convenience
  203. * when trying to sort through large numbers of block
  204. * devices or filesystem images.
  205. */
  206. memset(buf, 0, sizeof(buf));
  207. path.mnt = mnt;
  208. path.dentry = mnt->mnt_root;
  209. cp = d_path(&path, buf, sizeof(buf));
  210. if (!IS_ERR(cp)) {
  211. handle_t *handle;
  212. int err;
  213. handle = ext4_journal_start_sb(sb, 1);
  214. if (IS_ERR(handle))
  215. return PTR_ERR(handle);
  216. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  217. if (err) {
  218. ext4_journal_stop(handle);
  219. return err;
  220. }
  221. strlcpy(sbi->s_es->s_last_mounted, cp,
  222. sizeof(sbi->s_es->s_last_mounted));
  223. ext4_handle_dirty_super(handle, sb);
  224. ext4_journal_stop(handle);
  225. }
  226. }
  227. /*
  228. * Set up the jbd2_inode if we are opening the inode for
  229. * writing and the journal is present
  230. */
  231. if (sbi->s_journal && !ei->jinode && (filp->f_mode & FMODE_WRITE)) {
  232. struct jbd2_inode *jinode = jbd2_alloc_inode(GFP_KERNEL);
  233. spin_lock(&inode->i_lock);
  234. if (!ei->jinode) {
  235. if (!jinode) {
  236. spin_unlock(&inode->i_lock);
  237. return -ENOMEM;
  238. }
  239. ei->jinode = jinode;
  240. jbd2_journal_init_jbd_inode(ei->jinode, inode);
  241. jinode = NULL;
  242. }
  243. spin_unlock(&inode->i_lock);
  244. if (unlikely(jinode != NULL))
  245. jbd2_free_inode(jinode);
  246. }
  247. return dquot_file_open(inode, filp);
  248. }
  249. /*
  250. * ext4_llseek() handles both block-mapped and extent-mapped maxbytes values
  251. * by calling generic_file_llseek_size() with the appropriate maxbytes
  252. * value for each.
  253. */
  254. loff_t ext4_llseek(struct file *file, loff_t offset, int origin)
  255. {
  256. struct inode *inode = file->f_mapping->host;
  257. loff_t maxbytes;
  258. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  259. maxbytes = EXT4_SB(inode->i_sb)->s_bitmap_maxbytes;
  260. else
  261. maxbytes = inode->i_sb->s_maxbytes;
  262. return generic_file_llseek_size(file, offset, origin,
  263. maxbytes, i_size_read(inode));
  264. }
  265. const struct file_operations ext4_file_operations = {
  266. .llseek = ext4_llseek,
  267. .read = do_sync_read,
  268. .write = do_sync_write,
  269. .aio_read = generic_file_aio_read,
  270. .aio_write = ext4_file_write,
  271. .unlocked_ioctl = ext4_ioctl,
  272. #ifdef CONFIG_COMPAT
  273. .compat_ioctl = ext4_compat_ioctl,
  274. #endif
  275. .mmap = ext4_file_mmap,
  276. .open = ext4_file_open,
  277. .release = ext4_release_file,
  278. .fsync = ext4_sync_file,
  279. .splice_read = generic_file_splice_read,
  280. .splice_write = generic_file_splice_write,
  281. .fallocate = ext4_fallocate,
  282. };
  283. const struct inode_operations ext4_file_inode_operations = {
  284. .setattr = ext4_setattr,
  285. .getattr = ext4_getattr,
  286. #ifdef CONFIG_EXT4_FS_XATTR
  287. .setxattr = generic_setxattr,
  288. .getxattr = generic_getxattr,
  289. .listxattr = ext4_listxattr,
  290. .removexattr = generic_removexattr,
  291. #endif
  292. .get_acl = ext4_get_acl,
  293. .fiemap = ext4_fiemap,
  294. };