file.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (C) International Business Machines Corp., 2000-2002
  3. * Portions Copyright (C) Christoph Hellwig, 2001-2002
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  13. * the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/mm.h>
  20. #include <linux/fs.h>
  21. #include <linux/quotaops.h>
  22. #include "jfs_incore.h"
  23. #include "jfs_inode.h"
  24. #include "jfs_dmap.h"
  25. #include "jfs_txnmgr.h"
  26. #include "jfs_xattr.h"
  27. #include "jfs_acl.h"
  28. #include "jfs_debug.h"
  29. int jfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  30. {
  31. struct inode *inode = file->f_mapping->host;
  32. int rc = 0;
  33. rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
  34. if (rc)
  35. return rc;
  36. mutex_lock(&inode->i_mutex);
  37. if (!(inode->i_state & I_DIRTY) ||
  38. (datasync && !(inode->i_state & I_DIRTY_DATASYNC))) {
  39. /* Make sure committed changes hit the disk */
  40. jfs_flush_journal(JFS_SBI(inode->i_sb)->log, 1);
  41. mutex_unlock(&inode->i_mutex);
  42. return rc;
  43. }
  44. rc |= jfs_commit_inode(inode, 1);
  45. mutex_unlock(&inode->i_mutex);
  46. return rc ? -EIO : 0;
  47. }
  48. static int jfs_open(struct inode *inode, struct file *file)
  49. {
  50. int rc;
  51. if ((rc = dquot_file_open(inode, file)))
  52. return rc;
  53. /*
  54. * We attempt to allow only one "active" file open per aggregate
  55. * group. Otherwise, appending to files in parallel can cause
  56. * fragmentation within the files.
  57. *
  58. * If the file is empty, it was probably just created and going
  59. * to be written to. If it has a size, we'll hold off until the
  60. * file is actually grown.
  61. */
  62. if (S_ISREG(inode->i_mode) && file->f_mode & FMODE_WRITE &&
  63. (inode->i_size == 0)) {
  64. struct jfs_inode_info *ji = JFS_IP(inode);
  65. spin_lock_irq(&ji->ag_lock);
  66. if (ji->active_ag == -1) {
  67. struct jfs_sb_info *jfs_sb = JFS_SBI(inode->i_sb);
  68. ji->active_ag = BLKTOAG(addressPXD(&ji->ixpxd), jfs_sb);
  69. atomic_inc( &jfs_sb->bmap->db_active[ji->active_ag]);
  70. }
  71. spin_unlock_irq(&ji->ag_lock);
  72. }
  73. return 0;
  74. }
  75. static int jfs_release(struct inode *inode, struct file *file)
  76. {
  77. struct jfs_inode_info *ji = JFS_IP(inode);
  78. spin_lock_irq(&ji->ag_lock);
  79. if (ji->active_ag != -1) {
  80. struct bmap *bmap = JFS_SBI(inode->i_sb)->bmap;
  81. atomic_dec(&bmap->db_active[ji->active_ag]);
  82. ji->active_ag = -1;
  83. }
  84. spin_unlock_irq(&ji->ag_lock);
  85. return 0;
  86. }
  87. int jfs_setattr(struct dentry *dentry, struct iattr *iattr)
  88. {
  89. struct inode *inode = dentry->d_inode;
  90. int rc;
  91. rc = inode_change_ok(inode, iattr);
  92. if (rc)
  93. return rc;
  94. if (is_quota_modification(inode, iattr))
  95. dquot_initialize(inode);
  96. if ((iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) ||
  97. (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid)) {
  98. rc = dquot_transfer(inode, iattr);
  99. if (rc)
  100. return rc;
  101. }
  102. if ((iattr->ia_valid & ATTR_SIZE) &&
  103. iattr->ia_size != i_size_read(inode)) {
  104. inode_dio_wait(inode);
  105. rc = vmtruncate(inode, iattr->ia_size);
  106. if (rc)
  107. return rc;
  108. }
  109. setattr_copy(inode, iattr);
  110. mark_inode_dirty(inode);
  111. if (iattr->ia_valid & ATTR_MODE)
  112. rc = jfs_acl_chmod(inode);
  113. return rc;
  114. }
  115. const struct inode_operations jfs_file_inode_operations = {
  116. .truncate = jfs_truncate,
  117. .setxattr = jfs_setxattr,
  118. .getxattr = jfs_getxattr,
  119. .listxattr = jfs_listxattr,
  120. .removexattr = jfs_removexattr,
  121. .setattr = jfs_setattr,
  122. #ifdef CONFIG_JFS_POSIX_ACL
  123. .get_acl = jfs_get_acl,
  124. #endif
  125. };
  126. const struct file_operations jfs_file_operations = {
  127. .open = jfs_open,
  128. .llseek = generic_file_llseek,
  129. .write = do_sync_write,
  130. .read = do_sync_read,
  131. .aio_read = generic_file_aio_read,
  132. .aio_write = generic_file_aio_write,
  133. .mmap = generic_file_mmap,
  134. .splice_read = generic_file_splice_read,
  135. .splice_write = generic_file_splice_write,
  136. .fsync = jfs_fsync,
  137. .release = jfs_release,
  138. .unlocked_ioctl = jfs_ioctl,
  139. #ifdef CONFIG_COMPAT
  140. .compat_ioctl = jfs_compat_ioctl,
  141. #endif
  142. };