nfs4file.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * linux/fs/nfs/file.c
  3. *
  4. * Copyright (C) 1992 Rick Sladkey
  5. */
  6. #include <linux/nfs_fs.h>
  7. #include "internal.h"
  8. #include "fscache.h"
  9. #include "pnfs.h"
  10. #ifdef CONFIG_NFS_V4_2
  11. #include "nfs42.h"
  12. #endif
  13. #define NFSDBG_FACILITY NFSDBG_FILE
  14. static int
  15. nfs4_file_open(struct inode *inode, struct file *filp)
  16. {
  17. struct nfs_open_context *ctx;
  18. struct dentry *dentry = filp->f_path.dentry;
  19. struct dentry *parent = NULL;
  20. struct inode *dir;
  21. unsigned openflags = filp->f_flags;
  22. struct iattr attr;
  23. int opened = 0;
  24. int err;
  25. /*
  26. * If no cached dentry exists or if it's negative, NFSv4 handled the
  27. * opens in ->lookup() or ->create().
  28. *
  29. * We only get this far for a cached positive dentry. We skipped
  30. * revalidation, so handle it here by dropping the dentry and returning
  31. * -EOPENSTALE. The VFS will retry the lookup/create/open.
  32. */
  33. dprintk("NFS: open file(%pd2)\n", dentry);
  34. if ((openflags & O_ACCMODE) == 3)
  35. openflags--;
  36. /* We can't create new files here */
  37. openflags &= ~(O_CREAT|O_EXCL);
  38. parent = dget_parent(dentry);
  39. dir = parent->d_inode;
  40. ctx = alloc_nfs_open_context(filp->f_path.dentry, filp->f_mode);
  41. err = PTR_ERR(ctx);
  42. if (IS_ERR(ctx))
  43. goto out;
  44. attr.ia_valid = ATTR_OPEN;
  45. if (openflags & O_TRUNC) {
  46. attr.ia_valid |= ATTR_SIZE;
  47. attr.ia_size = 0;
  48. nfs_wb_all(inode);
  49. }
  50. inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, &opened);
  51. if (IS_ERR(inode)) {
  52. err = PTR_ERR(inode);
  53. switch (err) {
  54. case -EPERM:
  55. case -EACCES:
  56. case -EDQUOT:
  57. case -ENOSPC:
  58. case -EROFS:
  59. goto out_put_ctx;
  60. default:
  61. goto out_drop;
  62. }
  63. }
  64. if (inode != dentry->d_inode)
  65. goto out_drop;
  66. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  67. nfs_file_set_open_context(filp, ctx);
  68. nfs_fscache_open_file(inode, filp);
  69. err = 0;
  70. out_put_ctx:
  71. put_nfs_open_context(ctx);
  72. out:
  73. dput(parent);
  74. return err;
  75. out_drop:
  76. d_drop(dentry);
  77. err = -EOPENSTALE;
  78. goto out_put_ctx;
  79. }
  80. static int
  81. nfs4_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  82. {
  83. int ret;
  84. struct inode *inode = file_inode(file);
  85. do {
  86. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  87. if (ret != 0)
  88. break;
  89. mutex_lock(&inode->i_mutex);
  90. ret = nfs_file_fsync_commit(file, start, end, datasync);
  91. if (!ret)
  92. ret = pnfs_layoutcommit_inode(inode, true);
  93. mutex_unlock(&inode->i_mutex);
  94. /*
  95. * If nfs_file_fsync_commit detected a server reboot, then
  96. * resend all dirty pages that might have been covered by
  97. * the NFS_CONTEXT_RESEND_WRITES flag
  98. */
  99. start = 0;
  100. end = LLONG_MAX;
  101. } while (ret == -EAGAIN);
  102. return ret;
  103. }
  104. #ifdef CONFIG_NFS_V4_2
  105. static loff_t nfs4_file_llseek(struct file *filep, loff_t offset, int whence)
  106. {
  107. loff_t ret;
  108. switch (whence) {
  109. case SEEK_HOLE:
  110. case SEEK_DATA:
  111. ret = nfs42_proc_llseek(filep, offset, whence);
  112. if (ret != -ENOTSUPP)
  113. return ret;
  114. default:
  115. return nfs_file_llseek(filep, offset, whence);
  116. }
  117. }
  118. #endif /* CONFIG_NFS_V4_2 */
  119. const struct file_operations nfs4_file_operations = {
  120. #ifdef CONFIG_NFS_V4_2
  121. .llseek = nfs4_file_llseek,
  122. #else
  123. .llseek = nfs_file_llseek,
  124. #endif
  125. .read = new_sync_read,
  126. .write = new_sync_write,
  127. .read_iter = nfs_file_read,
  128. .write_iter = nfs_file_write,
  129. .mmap = nfs_file_mmap,
  130. .open = nfs4_file_open,
  131. .flush = nfs_file_flush,
  132. .release = nfs_file_release,
  133. .fsync = nfs4_file_fsync,
  134. .lock = nfs_lock,
  135. .flock = nfs_flock,
  136. .splice_read = nfs_file_splice_read,
  137. .splice_write = iter_file_splice_write,
  138. .check_flags = nfs_check_flags,
  139. .setlease = simple_nosetlease,
  140. };