nfs4file.c 3.1 KB

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