file.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/posix_acl.h>
  22. #include <linux/quotaops.h>
  23. #include "jfs_incore.h"
  24. #include "jfs_inode.h"
  25. #include "jfs_dmap.h"
  26. #include "jfs_txnmgr.h"
  27. #include "jfs_xattr.h"
  28. #include "jfs_acl.h"
  29. #include "jfs_debug.h"
  30. int jfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  31. {
  32. struct inode *inode = file->f_mapping->host;
  33. int rc = 0;
  34. rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
  35. if (rc)
  36. return rc;
  37. mutex_lock(&inode->i_mutex);
  38. if (!(inode->i_state & I_DIRTY_ALL) ||
  39. (datasync && !(inode->i_state & I_DIRTY_DATASYNC))) {
  40. /* Make sure committed changes hit the disk */
  41. jfs_flush_journal(JFS_SBI(inode->i_sb)->log, 1);
  42. mutex_unlock(&inode->i_mutex);
  43. return rc;
  44. }
  45. rc |= jfs_commit_inode(inode, 1);
  46. mutex_unlock(&inode->i_mutex);
  47. return rc ? -EIO : 0;
  48. }
  49. static int jfs_open(struct inode *inode, struct file *file)
  50. {
  51. int rc;
  52. if ((rc = dquot_file_open(inode, file)))
  53. return rc;
  54. /*
  55. * We attempt to allow only one "active" file open per aggregate
  56. * group. Otherwise, appending to files in parallel can cause
  57. * fragmentation within the files.
  58. *
  59. * If the file is empty, it was probably just created and going
  60. * to be written to. If it has a size, we'll hold off until the
  61. * file is actually grown.
  62. */
  63. if (S_ISREG(inode->i_mode) && file->f_mode & FMODE_WRITE &&
  64. (inode->i_size == 0)) {
  65. struct jfs_inode_info *ji = JFS_IP(inode);
  66. spin_lock_irq(&ji->ag_lock);
  67. if (ji->active_ag == -1) {
  68. struct jfs_sb_info *jfs_sb = JFS_SBI(inode->i_sb);
  69. ji->active_ag = BLKTOAG(addressPXD(&ji->ixpxd), jfs_sb);
  70. atomic_inc( &jfs_sb->bmap->db_active[ji->active_ag]);
  71. }
  72. spin_unlock_irq(&ji->ag_lock);
  73. }
  74. return 0;
  75. }
  76. static int jfs_release(struct inode *inode, struct file *file)
  77. {
  78. struct jfs_inode_info *ji = JFS_IP(inode);
  79. spin_lock_irq(&ji->ag_lock);
  80. if (ji->active_ag != -1) {
  81. struct bmap *bmap = JFS_SBI(inode->i_sb)->bmap;
  82. atomic_dec(&bmap->db_active[ji->active_ag]);
  83. ji->active_ag = -1;
  84. }
  85. spin_unlock_irq(&ji->ag_lock);
  86. return 0;
  87. }
  88. int jfs_setattr(struct dentry *dentry, struct iattr *iattr)
  89. {
  90. struct inode *inode = d_inode(dentry);
  91. int rc;
  92. rc = inode_change_ok(inode, iattr);
  93. if (rc)
  94. return rc;
  95. if (is_quota_modification(inode, iattr))
  96. dquot_initialize(inode);
  97. if ((iattr->ia_valid & ATTR_UID && !uid_eq(iattr->ia_uid, inode->i_uid)) ||
  98. (iattr->ia_valid & ATTR_GID && !gid_eq(iattr->ia_gid, inode->i_gid))) {
  99. rc = dquot_transfer(inode, iattr);
  100. if (rc)
  101. return rc;
  102. }
  103. if ((iattr->ia_valid & ATTR_SIZE) &&
  104. iattr->ia_size != i_size_read(inode)) {
  105. inode_dio_wait(inode);
  106. rc = inode_newsize_ok(inode, iattr->ia_size);
  107. if (rc)
  108. return rc;
  109. truncate_setsize(inode, iattr->ia_size);
  110. jfs_truncate(inode);
  111. }
  112. setattr_copy(inode, iattr);
  113. mark_inode_dirty(inode);
  114. if (iattr->ia_valid & ATTR_MODE)
  115. rc = posix_acl_chmod(inode, inode->i_mode);
  116. return rc;
  117. }
  118. const struct inode_operations jfs_file_inode_operations = {
  119. .setxattr = jfs_setxattr,
  120. .getxattr = jfs_getxattr,
  121. .listxattr = jfs_listxattr,
  122. .removexattr = jfs_removexattr,
  123. .setattr = jfs_setattr,
  124. #ifdef CONFIG_JFS_POSIX_ACL
  125. .get_acl = jfs_get_acl,
  126. .set_acl = jfs_set_acl,
  127. #endif
  128. };
  129. const struct file_operations jfs_file_operations = {
  130. .open = jfs_open,
  131. .llseek = generic_file_llseek,
  132. .read_iter = generic_file_read_iter,
  133. .write_iter = generic_file_write_iter,
  134. .mmap = generic_file_mmap,
  135. .splice_read = generic_file_splice_read,
  136. .splice_write = iter_file_splice_write,
  137. .fsync = jfs_fsync,
  138. .release = jfs_release,
  139. .unlocked_ioctl = jfs_ioctl,
  140. #ifdef CONFIG_COMPAT
  141. .compat_ioctl = jfs_compat_ioctl,
  142. #endif
  143. };