nfs4file.c 4.2 KB

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