file.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * file.c - NTFS kernel file operations. Part of the Linux-NTFS project.
  3. *
  4. * Copyright (c) 2001-2004 Anton Altaparmakov
  5. *
  6. * This program/include file is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as published
  8. * by the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program/include file is distributed in the hope that it will be
  12. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program (in the main directory of the Linux-NTFS
  18. * distribution in the file COPYING); if not, write to the Free Software
  19. * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/pagemap.h>
  22. #include <linux/buffer_head.h>
  23. #include "inode.h"
  24. #include "debug.h"
  25. #include "ntfs.h"
  26. /**
  27. * ntfs_file_open - called when an inode is about to be opened
  28. * @vi: inode to be opened
  29. * @filp: file structure describing the inode
  30. *
  31. * Limit file size to the page cache limit on architectures where unsigned long
  32. * is 32-bits. This is the most we can do for now without overflowing the page
  33. * cache page index. Doing it this way means we don't run into problems because
  34. * of existing too large files. It would be better to allow the user to read
  35. * the beginning of the file but I doubt very much anyone is going to hit this
  36. * check on a 32-bit architecture, so there is no point in adding the extra
  37. * complexity required to support this.
  38. *
  39. * On 64-bit architectures, the check is hopefully optimized away by the
  40. * compiler.
  41. *
  42. * After the check passes, just call generic_file_open() to do its work.
  43. */
  44. static int ntfs_file_open(struct inode *vi, struct file *filp)
  45. {
  46. if (sizeof(unsigned long) < 8) {
  47. if (vi->i_size > MAX_LFS_FILESIZE)
  48. return -EFBIG;
  49. }
  50. return generic_file_open(vi, filp);
  51. }
  52. #ifdef NTFS_RW
  53. /**
  54. * ntfs_file_fsync - sync a file to disk
  55. * @filp: file to be synced
  56. * @dentry: dentry describing the file to sync
  57. * @datasync: if non-zero only flush user data and not metadata
  58. *
  59. * Data integrity sync of a file to disk. Used for fsync, fdatasync, and msync
  60. * system calls. This function is inspired by fs/buffer.c::file_fsync().
  61. *
  62. * If @datasync is false, write the mft record and all associated extent mft
  63. * records as well as the $DATA attribute and then sync the block device.
  64. *
  65. * If @datasync is true and the attribute is non-resident, we skip the writing
  66. * of the mft record and all associated extent mft records (this might still
  67. * happen due to the write_inode_now() call).
  68. *
  69. * Also, if @datasync is true, we do not wait on the inode to be written out
  70. * but we always wait on the page cache pages to be written out.
  71. *
  72. * Note: In the past @filp could be NULL so we ignore it as we don't need it
  73. * anyway.
  74. *
  75. * Locking: Caller must hold i_sem on the inode.
  76. *
  77. * TODO: We should probably also write all attribute/index inodes associated
  78. * with this inode but since we have no simple way of getting to them we ignore
  79. * this problem for now.
  80. */
  81. static int ntfs_file_fsync(struct file *filp, struct dentry *dentry,
  82. int datasync)
  83. {
  84. struct inode *vi = dentry->d_inode;
  85. int err, ret = 0;
  86. ntfs_debug("Entering for inode 0x%lx.", vi->i_ino);
  87. BUG_ON(S_ISDIR(vi->i_mode));
  88. if (!datasync || !NInoNonResident(NTFS_I(vi)))
  89. ret = ntfs_write_inode(vi, 1);
  90. write_inode_now(vi, !datasync);
  91. err = sync_blockdev(vi->i_sb->s_bdev);
  92. if (unlikely(err && !ret))
  93. ret = err;
  94. if (likely(!ret))
  95. ntfs_debug("Done.");
  96. else
  97. ntfs_warning(vi->i_sb, "Failed to f%ssync inode 0x%lx. Error "
  98. "%u.", datasync ? "data" : "", vi->i_ino, -ret);
  99. return ret;
  100. }
  101. #endif /* NTFS_RW */
  102. struct file_operations ntfs_file_ops = {
  103. .llseek = generic_file_llseek, /* Seek inside file. */
  104. .read = generic_file_read, /* Read from file. */
  105. .aio_read = generic_file_aio_read, /* Async read from file. */
  106. .readv = generic_file_readv, /* Read from file. */
  107. #ifdef NTFS_RW
  108. .write = generic_file_write, /* Write to file. */
  109. .aio_write = generic_file_aio_write, /* Async write to file. */
  110. .writev = generic_file_writev, /* Write to file. */
  111. /*.release = ,*/ /* Last file is closed. See
  112. fs/ext2/file.c::
  113. ext2_release_file() for
  114. how to use this to discard
  115. preallocated space for
  116. write opened files. */
  117. .fsync = ntfs_file_fsync, /* Sync a file to disk. */
  118. /*.aio_fsync = ,*/ /* Sync all outstanding async
  119. i/o operations on a
  120. kiocb. */
  121. #endif /* NTFS_RW */
  122. /*.ioctl = ,*/ /* Perform function on the
  123. mounted filesystem. */
  124. .mmap = generic_file_mmap, /* Mmap file. */
  125. .open = ntfs_file_open, /* Open file. */
  126. .sendfile = generic_file_sendfile, /* Zero-copy data send with
  127. the data source being on
  128. the ntfs partition. We
  129. do not need to care about
  130. the data destination. */
  131. /*.sendpage = ,*/ /* Zero-copy data send with
  132. the data destination being
  133. on the ntfs partition. We
  134. do not need to care about
  135. the data source. */
  136. };
  137. struct inode_operations ntfs_file_inode_ops = {
  138. #ifdef NTFS_RW
  139. .truncate = ntfs_truncate_vfs,
  140. .setattr = ntfs_setattr,
  141. #endif /* NTFS_RW */
  142. };
  143. struct file_operations ntfs_empty_file_ops = {};
  144. struct inode_operations ntfs_empty_inode_ops = {};