nfs4file.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "pnfs.h"
  9. #define NFSDBG_FACILITY NFSDBG_FILE
  10. static int
  11. nfs4_file_open(struct inode *inode, struct file *filp)
  12. {
  13. struct nfs_open_context *ctx;
  14. struct dentry *dentry = filp->f_path.dentry;
  15. struct dentry *parent = NULL;
  16. struct inode *dir;
  17. unsigned openflags = filp->f_flags;
  18. struct iattr attr;
  19. int err;
  20. BUG_ON(inode != dentry->d_inode);
  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. err = 0;
  68. out_put_ctx:
  69. put_nfs_open_context(ctx);
  70. out:
  71. dput(parent);
  72. return err;
  73. out_drop:
  74. d_drop(dentry);
  75. err = -EOPENSTALE;
  76. goto out_put_ctx;
  77. }
  78. static int
  79. nfs4_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  80. {
  81. int ret;
  82. struct inode *inode = file->f_path.dentry->d_inode;
  83. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  84. mutex_lock(&inode->i_mutex);
  85. ret = nfs_file_fsync_commit(file, start, end, datasync);
  86. if (!ret && !datasync)
  87. /* application has asked for meta-data sync */
  88. ret = pnfs_layoutcommit_inode(inode, true);
  89. mutex_unlock(&inode->i_mutex);
  90. return ret;
  91. }
  92. const struct file_operations nfs4_file_operations = {
  93. .llseek = nfs_file_llseek,
  94. .read = do_sync_read,
  95. .write = do_sync_write,
  96. .aio_read = nfs_file_read,
  97. .aio_write = nfs_file_write,
  98. .mmap = nfs_file_mmap,
  99. .open = nfs4_file_open,
  100. .flush = nfs_file_flush,
  101. .release = nfs_file_release,
  102. .fsync = nfs4_file_fsync,
  103. .lock = nfs_lock,
  104. .flock = nfs_flock,
  105. .splice_read = nfs_file_splice_read,
  106. .splice_write = nfs_file_splice_write,
  107. .check_flags = nfs_check_flags,
  108. .setlease = nfs_setlease,
  109. };