file.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/ext4_fs.h>
  24. #include <linux/ext4_jbd2.h>
  25. #include "xattr.h"
  26. #include "acl.h"
  27. /*
  28. * Called when an inode is released. Note that this is different
  29. * from ext4_file_open: open gets called at every open, but release
  30. * gets called only when /all/ the files are closed.
  31. */
  32. static int ext4_release_file (struct inode * inode, struct file * filp)
  33. {
  34. /* if we are the last writer on the inode, drop the block reservation */
  35. if ((filp->f_mode & FMODE_WRITE) &&
  36. (atomic_read(&inode->i_writecount) == 1))
  37. {
  38. mutex_lock(&EXT4_I(inode)->truncate_mutex);
  39. ext4_discard_reservation(inode);
  40. mutex_unlock(&EXT4_I(inode)->truncate_mutex);
  41. }
  42. if (is_dx(inode) && filp->private_data)
  43. ext4_htree_free_dir_info(filp->private_data);
  44. return 0;
  45. }
  46. static ssize_t
  47. ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
  48. unsigned long nr_segs, loff_t pos)
  49. {
  50. struct file *file = iocb->ki_filp;
  51. struct inode *inode = file->f_path.dentry->d_inode;
  52. ssize_t ret;
  53. int err;
  54. ret = generic_file_aio_write(iocb, iov, nr_segs, pos);
  55. /*
  56. * Skip flushing if there was an error, or if nothing was written.
  57. */
  58. if (ret <= 0)
  59. return ret;
  60. /*
  61. * If the inode is IS_SYNC, or is O_SYNC and we are doing data
  62. * journalling then we need to make sure that we force the transaction
  63. * to disk to keep all metadata uptodate synchronously.
  64. */
  65. if (file->f_flags & O_SYNC) {
  66. /*
  67. * If we are non-data-journaled, then the dirty data has
  68. * already been flushed to backing store by generic_osync_inode,
  69. * and the inode has been flushed too if there have been any
  70. * modifications other than mere timestamp updates.
  71. *
  72. * Open question --- do we care about flushing timestamps too
  73. * if the inode is IS_SYNC?
  74. */
  75. if (!ext4_should_journal_data(inode))
  76. return ret;
  77. goto force_commit;
  78. }
  79. /*
  80. * So we know that there has been no forced data flush. If the inode
  81. * is marked IS_SYNC, we need to force one ourselves.
  82. */
  83. if (!IS_SYNC(inode))
  84. return ret;
  85. /*
  86. * Open question #2 --- should we force data to disk here too? If we
  87. * don't, the only impact is that data=writeback filesystems won't
  88. * flush data to disk automatically on IS_SYNC, only metadata (but
  89. * historically, that is what ext2 has done.)
  90. */
  91. force_commit:
  92. err = ext4_force_commit(inode->i_sb);
  93. if (err)
  94. return err;
  95. return ret;
  96. }
  97. const struct file_operations ext4_file_operations = {
  98. .llseek = generic_file_llseek,
  99. .read = do_sync_read,
  100. .write = do_sync_write,
  101. .aio_read = generic_file_aio_read,
  102. .aio_write = ext4_file_write,
  103. .ioctl = ext4_ioctl,
  104. #ifdef CONFIG_COMPAT
  105. .compat_ioctl = ext4_compat_ioctl,
  106. #endif
  107. .mmap = generic_file_mmap,
  108. .open = generic_file_open,
  109. .release = ext4_release_file,
  110. .fsync = ext4_sync_file,
  111. .splice_read = generic_file_splice_read,
  112. .splice_write = generic_file_splice_write,
  113. };
  114. const struct inode_operations ext4_file_inode_operations = {
  115. .truncate = ext4_truncate,
  116. .setattr = ext4_setattr,
  117. #ifdef CONFIG_EXT4DEV_FS_XATTR
  118. .setxattr = generic_setxattr,
  119. .getxattr = generic_getxattr,
  120. .listxattr = ext4_listxattr,
  121. .removexattr = generic_removexattr,
  122. #endif
  123. .permission = ext4_permission,
  124. .fallocate = ext4_fallocate,
  125. };