nfs4file.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. err = nfs_check_flags(openflags);
  38. if (err)
  39. return err;
  40. if ((openflags & O_ACCMODE) == 3)
  41. openflags--;
  42. /* We can't create new files here */
  43. openflags &= ~(O_CREAT|O_EXCL);
  44. parent = dget_parent(dentry);
  45. dir = d_inode(parent);
  46. ctx = alloc_nfs_open_context(filp->f_path.dentry, filp->f_mode);
  47. err = PTR_ERR(ctx);
  48. if (IS_ERR(ctx))
  49. goto out;
  50. attr.ia_valid = ATTR_OPEN;
  51. if (openflags & O_TRUNC) {
  52. attr.ia_valid |= ATTR_SIZE;
  53. attr.ia_size = 0;
  54. nfs_sync_inode(inode);
  55. }
  56. inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, &opened);
  57. if (IS_ERR(inode)) {
  58. err = PTR_ERR(inode);
  59. switch (err) {
  60. case -EPERM:
  61. case -EACCES:
  62. case -EDQUOT:
  63. case -ENOSPC:
  64. case -EROFS:
  65. goto out_put_ctx;
  66. default:
  67. goto out_drop;
  68. }
  69. }
  70. if (inode != d_inode(dentry))
  71. goto out_drop;
  72. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  73. nfs_file_set_open_context(filp, ctx);
  74. nfs_fscache_open_file(inode, filp);
  75. err = 0;
  76. out_put_ctx:
  77. put_nfs_open_context(ctx);
  78. out:
  79. dput(parent);
  80. return err;
  81. out_drop:
  82. d_drop(dentry);
  83. err = -EOPENSTALE;
  84. goto out_put_ctx;
  85. }
  86. static int
  87. nfs4_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  88. {
  89. int ret;
  90. struct inode *inode = file_inode(file);
  91. trace_nfs_fsync_enter(inode);
  92. nfs_inode_dio_wait(inode);
  93. do {
  94. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  95. if (ret != 0)
  96. break;
  97. mutex_lock(&inode->i_mutex);
  98. ret = nfs_file_fsync_commit(file, start, end, datasync);
  99. if (!ret)
  100. ret = pnfs_sync_inode(inode, !!datasync);
  101. mutex_unlock(&inode->i_mutex);
  102. /*
  103. * If nfs_file_fsync_commit detected a server reboot, then
  104. * resend all dirty pages that might have been covered by
  105. * the NFS_CONTEXT_RESEND_WRITES flag
  106. */
  107. start = 0;
  108. end = LLONG_MAX;
  109. } while (ret == -EAGAIN);
  110. trace_nfs_fsync_exit(inode, ret);
  111. return ret;
  112. }
  113. #ifdef CONFIG_NFS_V4_2
  114. static loff_t nfs4_file_llseek(struct file *filep, loff_t offset, int whence)
  115. {
  116. loff_t ret;
  117. switch (whence) {
  118. case SEEK_HOLE:
  119. case SEEK_DATA:
  120. ret = nfs42_proc_llseek(filep, offset, whence);
  121. if (ret != -ENOTSUPP)
  122. return ret;
  123. default:
  124. return nfs_file_llseek(filep, offset, whence);
  125. }
  126. }
  127. static long nfs42_fallocate(struct file *filep, int mode, loff_t offset, loff_t len)
  128. {
  129. struct inode *inode = file_inode(filep);
  130. long ret;
  131. if (!S_ISREG(inode->i_mode))
  132. return -EOPNOTSUPP;
  133. if ((mode != 0) && (mode != (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)))
  134. return -EOPNOTSUPP;
  135. ret = inode_newsize_ok(inode, offset + len);
  136. if (ret < 0)
  137. return ret;
  138. if (mode & FALLOC_FL_PUNCH_HOLE)
  139. return nfs42_proc_deallocate(filep, offset, len);
  140. return nfs42_proc_allocate(filep, offset, len);
  141. }
  142. #endif /* CONFIG_NFS_V4_2 */
  143. const struct file_operations nfs4_file_operations = {
  144. #ifdef CONFIG_NFS_V4_2
  145. .llseek = nfs4_file_llseek,
  146. #else
  147. .llseek = nfs_file_llseek,
  148. #endif
  149. .read_iter = nfs_file_read,
  150. .write_iter = nfs_file_write,
  151. .mmap = nfs_file_mmap,
  152. .open = nfs4_file_open,
  153. .flush = nfs_file_flush,
  154. .release = nfs_file_release,
  155. .fsync = nfs4_file_fsync,
  156. .lock = nfs_lock,
  157. .flock = nfs_flock,
  158. .splice_read = nfs_file_splice_read,
  159. .splice_write = iter_file_splice_write,
  160. #ifdef CONFIG_NFS_V4_2
  161. .fallocate = nfs42_fallocate,
  162. #endif /* CONFIG_NFS_V4_2 */
  163. .check_flags = nfs_check_flags,
  164. .setlease = simple_nosetlease,
  165. };