nfs4file.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * linux/fs/nfs/file.c
  3. *
  4. * Copyright (C) 1992 Rick Sladkey
  5. */
  6. #include <linux/fs.h>
  7. #include <linux/falloc.h>
  8. #include <linux/nfs_fs.h>
  9. #include "delegation.h"
  10. #include "internal.h"
  11. #include "iostat.h"
  12. #include "fscache.h"
  13. #include "pnfs.h"
  14. #include "nfstrace.h"
  15. #ifdef CONFIG_NFS_V4_2
  16. #include "nfs42.h"
  17. #endif
  18. #define NFSDBG_FACILITY NFSDBG_FILE
  19. static int
  20. nfs4_file_open(struct inode *inode, struct file *filp)
  21. {
  22. struct nfs_open_context *ctx;
  23. struct dentry *dentry = filp->f_path.dentry;
  24. struct dentry *parent = NULL;
  25. struct inode *dir;
  26. unsigned openflags = filp->f_flags;
  27. struct iattr attr;
  28. int err;
  29. /*
  30. * If no cached dentry exists or if it's negative, NFSv4 handled the
  31. * opens in ->lookup() or ->create().
  32. *
  33. * We only get this far for a cached positive dentry. We skipped
  34. * revalidation, so handle it here by dropping the dentry and returning
  35. * -EOPENSTALE. The VFS will retry the lookup/create/open.
  36. */
  37. dprintk("NFS: open file(%pd2)\n", dentry);
  38. err = nfs_check_flags(openflags);
  39. if (err)
  40. return err;
  41. if ((openflags & O_ACCMODE) == 3)
  42. openflags--;
  43. /* We can't create new files here */
  44. openflags &= ~(O_CREAT|O_EXCL);
  45. parent = dget_parent(dentry);
  46. dir = d_inode(parent);
  47. ctx = alloc_nfs_open_context(filp->f_path.dentry, filp->f_mode);
  48. err = PTR_ERR(ctx);
  49. if (IS_ERR(ctx))
  50. goto out;
  51. attr.ia_valid = ATTR_OPEN;
  52. if (openflags & O_TRUNC) {
  53. attr.ia_valid |= ATTR_SIZE;
  54. attr.ia_size = 0;
  55. nfs_sync_inode(inode);
  56. }
  57. inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, NULL);
  58. if (IS_ERR(inode)) {
  59. err = PTR_ERR(inode);
  60. switch (err) {
  61. case -EPERM:
  62. case -EACCES:
  63. case -EDQUOT:
  64. case -ENOSPC:
  65. case -EROFS:
  66. goto out_put_ctx;
  67. default:
  68. goto out_drop;
  69. }
  70. }
  71. if (inode != d_inode(dentry))
  72. goto out_drop;
  73. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  74. nfs_file_set_open_context(filp, ctx);
  75. nfs_fscache_open_file(inode, filp);
  76. err = 0;
  77. out_put_ctx:
  78. put_nfs_open_context(ctx);
  79. out:
  80. dput(parent);
  81. return err;
  82. out_drop:
  83. d_drop(dentry);
  84. err = -EOPENSTALE;
  85. goto out_put_ctx;
  86. }
  87. /*
  88. * Flush all dirty pages, and check for write errors.
  89. */
  90. static int
  91. nfs4_file_flush(struct file *file, fl_owner_t id)
  92. {
  93. struct inode *inode = file_inode(file);
  94. dprintk("NFS: flush(%pD2)\n", file);
  95. nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
  96. if ((file->f_mode & FMODE_WRITE) == 0)
  97. return 0;
  98. /*
  99. * If we're holding a write delegation, then check if we're required
  100. * to flush the i/o on close. If not, then just start the i/o now.
  101. */
  102. if (!nfs4_delegation_flush_on_close(inode))
  103. return filemap_fdatawrite(file->f_mapping);
  104. /* Flush writes to the server and return any errors */
  105. return vfs_fsync(file, 0);
  106. }
  107. static int
  108. nfs4_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  109. {
  110. int ret;
  111. struct inode *inode = file_inode(file);
  112. trace_nfs_fsync_enter(inode);
  113. nfs_inode_dio_wait(inode);
  114. do {
  115. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  116. if (ret != 0)
  117. break;
  118. mutex_lock(&inode->i_mutex);
  119. ret = nfs_file_fsync_commit(file, start, end, datasync);
  120. if (!ret)
  121. ret = pnfs_sync_inode(inode, !!datasync);
  122. mutex_unlock(&inode->i_mutex);
  123. /*
  124. * If nfs_file_fsync_commit detected a server reboot, then
  125. * resend all dirty pages that might have been covered by
  126. * the NFS_CONTEXT_RESEND_WRITES flag
  127. */
  128. start = 0;
  129. end = LLONG_MAX;
  130. } while (ret == -EAGAIN);
  131. trace_nfs_fsync_exit(inode, ret);
  132. return ret;
  133. }
  134. #ifdef CONFIG_NFS_V4_2
  135. static loff_t nfs4_file_llseek(struct file *filep, loff_t offset, int whence)
  136. {
  137. loff_t ret;
  138. switch (whence) {
  139. case SEEK_HOLE:
  140. case SEEK_DATA:
  141. ret = nfs42_proc_llseek(filep, offset, whence);
  142. if (ret != -ENOTSUPP)
  143. return ret;
  144. default:
  145. return nfs_file_llseek(filep, offset, whence);
  146. }
  147. }
  148. static long nfs42_fallocate(struct file *filep, int mode, loff_t offset, loff_t len)
  149. {
  150. struct inode *inode = file_inode(filep);
  151. long ret;
  152. if (!S_ISREG(inode->i_mode))
  153. return -EOPNOTSUPP;
  154. if ((mode != 0) && (mode != (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)))
  155. return -EOPNOTSUPP;
  156. ret = inode_newsize_ok(inode, offset + len);
  157. if (ret < 0)
  158. return ret;
  159. if (mode & FALLOC_FL_PUNCH_HOLE)
  160. return nfs42_proc_deallocate(filep, offset, len);
  161. return nfs42_proc_allocate(filep, offset, len);
  162. }
  163. #endif /* CONFIG_NFS_V4_2 */
  164. const struct file_operations nfs4_file_operations = {
  165. #ifdef CONFIG_NFS_V4_2
  166. .llseek = nfs4_file_llseek,
  167. #else
  168. .llseek = nfs_file_llseek,
  169. #endif
  170. .read_iter = nfs_file_read,
  171. .write_iter = nfs_file_write,
  172. .mmap = nfs_file_mmap,
  173. .open = nfs4_file_open,
  174. .flush = nfs4_file_flush,
  175. .release = nfs_file_release,
  176. .fsync = nfs4_file_fsync,
  177. .lock = nfs_lock,
  178. .flock = nfs_flock,
  179. .splice_read = nfs_file_splice_read,
  180. .splice_write = iter_file_splice_write,
  181. #ifdef CONFIG_NFS_V4_2
  182. .fallocate = nfs42_fallocate,
  183. #endif /* CONFIG_NFS_V4_2 */
  184. .check_flags = nfs_check_flags,
  185. .setlease = simple_nosetlease,
  186. };