nfs4file.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #define NFSDBG_FACILITY NFSDBG_FILE
  11. static int
  12. nfs4_file_open(struct inode *inode, struct file *filp)
  13. {
  14. struct nfs_open_context *ctx;
  15. struct dentry *dentry = filp->f_path.dentry;
  16. struct dentry *parent = NULL;
  17. struct inode *dir;
  18. unsigned openflags = filp->f_flags;
  19. struct iattr attr;
  20. int opened = 0;
  21. int err;
  22. /*
  23. * If no cached dentry exists or if it's negative, NFSv4 handled the
  24. * opens in ->lookup() or ->create().
  25. *
  26. * We only get this far for a cached positive dentry. We skipped
  27. * revalidation, so handle it here by dropping the dentry and returning
  28. * -EOPENSTALE. The VFS will retry the lookup/create/open.
  29. */
  30. dprintk("NFS: open file(%pd2)\n", dentry);
  31. if ((openflags & O_ACCMODE) == 3)
  32. openflags--;
  33. /* We can't create new files here */
  34. openflags &= ~(O_CREAT|O_EXCL);
  35. parent = dget_parent(dentry);
  36. dir = parent->d_inode;
  37. ctx = alloc_nfs_open_context(filp->f_path.dentry, filp->f_mode);
  38. err = PTR_ERR(ctx);
  39. if (IS_ERR(ctx))
  40. goto out;
  41. attr.ia_valid = ATTR_OPEN;
  42. if (openflags & O_TRUNC) {
  43. attr.ia_valid |= ATTR_SIZE;
  44. attr.ia_size = 0;
  45. nfs_wb_all(inode);
  46. }
  47. inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, &opened);
  48. if (IS_ERR(inode)) {
  49. err = PTR_ERR(inode);
  50. switch (err) {
  51. case -EPERM:
  52. case -EACCES:
  53. case -EDQUOT:
  54. case -ENOSPC:
  55. case -EROFS:
  56. goto out_put_ctx;
  57. default:
  58. goto out_drop;
  59. }
  60. }
  61. if (inode != dentry->d_inode)
  62. goto out_drop;
  63. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  64. nfs_file_set_open_context(filp, ctx);
  65. nfs_fscache_open_file(inode, filp);
  66. err = 0;
  67. out_put_ctx:
  68. put_nfs_open_context(ctx);
  69. out:
  70. dput(parent);
  71. return err;
  72. out_drop:
  73. d_drop(dentry);
  74. err = -EOPENSTALE;
  75. goto out_put_ctx;
  76. }
  77. static int
  78. nfs4_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  79. {
  80. int ret;
  81. struct inode *inode = file_inode(file);
  82. do {
  83. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  84. if (ret != 0)
  85. break;
  86. mutex_lock(&inode->i_mutex);
  87. ret = nfs_file_fsync_commit(file, start, end, datasync);
  88. if (!ret && !datasync)
  89. /* application has asked for meta-data sync */
  90. ret = pnfs_layoutcommit_inode(inode, true);
  91. mutex_unlock(&inode->i_mutex);
  92. /*
  93. * If nfs_file_fsync_commit detected a server reboot, then
  94. * resend all dirty pages that might have been covered by
  95. * the NFS_CONTEXT_RESEND_WRITES flag
  96. */
  97. start = 0;
  98. end = LLONG_MAX;
  99. } while (ret == -EAGAIN);
  100. return ret;
  101. }
  102. const struct file_operations nfs4_file_operations = {
  103. .llseek = nfs_file_llseek,
  104. .read = do_sync_read,
  105. .write = do_sync_write,
  106. .aio_read = nfs_file_read,
  107. .aio_write = nfs_file_write,
  108. .mmap = nfs_file_mmap,
  109. .open = nfs4_file_open,
  110. .flush = nfs_file_flush,
  111. .release = nfs_file_release,
  112. .fsync = nfs4_file_fsync,
  113. .lock = nfs_lock,
  114. .flock = nfs_flock,
  115. .splice_read = nfs_file_splice_read,
  116. .splice_write = nfs_file_splice_write,
  117. .check_flags = nfs_check_flags,
  118. .setlease = nfs_setlease,
  119. };