nfs4file.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/nfs/file.c
  4. *
  5. * Copyright (C) 1992 Rick Sladkey
  6. */
  7. #include <linux/fs.h>
  8. #include <linux/file.h>
  9. #include <linux/falloc.h>
  10. #include <linux/nfs_fs.h>
  11. #include "delegation.h"
  12. #include "internal.h"
  13. #include "iostat.h"
  14. #include "fscache.h"
  15. #include "pnfs.h"
  16. #include "nfstrace.h"
  17. #ifdef CONFIG_NFS_V4_2
  18. #include "nfs42.h"
  19. #endif
  20. #define NFSDBG_FACILITY NFSDBG_FILE
  21. static int
  22. nfs4_file_open(struct inode *inode, struct file *filp)
  23. {
  24. struct nfs_open_context *ctx;
  25. struct dentry *dentry = file_dentry(filp);
  26. struct dentry *parent = NULL;
  27. struct inode *dir;
  28. unsigned openflags = filp->f_flags;
  29. struct iattr attr;
  30. int err;
  31. /*
  32. * If no cached dentry exists or if it's negative, NFSv4 handled the
  33. * opens in ->lookup() or ->create().
  34. *
  35. * We only get this far for a cached positive dentry. We skipped
  36. * revalidation, so handle it here by dropping the dentry and returning
  37. * -EOPENSTALE. The VFS will retry the lookup/create/open.
  38. */
  39. dprintk("NFS: open file(%pd2)\n", dentry);
  40. err = nfs_check_flags(openflags);
  41. if (err)
  42. return err;
  43. if ((openflags & O_ACCMODE) == 3)
  44. return nfs_open(inode, filp);
  45. /* We can't create new files here */
  46. openflags &= ~(O_CREAT|O_EXCL);
  47. parent = dget_parent(dentry);
  48. dir = d_inode(parent);
  49. ctx = alloc_nfs_open_context(file_dentry(filp), filp->f_mode, filp);
  50. err = PTR_ERR(ctx);
  51. if (IS_ERR(ctx))
  52. goto out;
  53. attr.ia_valid = ATTR_OPEN;
  54. if (openflags & O_TRUNC) {
  55. attr.ia_valid |= ATTR_SIZE;
  56. attr.ia_size = 0;
  57. filemap_write_and_wait(inode->i_mapping);
  58. }
  59. inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, NULL);
  60. if (IS_ERR(inode)) {
  61. err = PTR_ERR(inode);
  62. switch (err) {
  63. default:
  64. goto out_put_ctx;
  65. case -ENOENT:
  66. case -ESTALE:
  67. case -EISDIR:
  68. case -ENOTDIR:
  69. case -ELOOP:
  70. goto out_drop;
  71. }
  72. }
  73. if (inode != d_inode(dentry))
  74. goto out_drop;
  75. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  76. nfs_file_set_open_context(filp, ctx);
  77. nfs_fscache_open_file(inode, filp);
  78. err = 0;
  79. out_put_ctx:
  80. put_nfs_open_context(ctx);
  81. out:
  82. dput(parent);
  83. return err;
  84. out_drop:
  85. d_drop(dentry);
  86. err = -EOPENSTALE;
  87. goto out_put_ctx;
  88. }
  89. /*
  90. * Flush all dirty pages, and check for write errors.
  91. */
  92. static int
  93. nfs4_file_flush(struct file *file, fl_owner_t id)
  94. {
  95. struct inode *inode = file_inode(file);
  96. dprintk("NFS: flush(%pD2)\n", file);
  97. nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
  98. if ((file->f_mode & FMODE_WRITE) == 0)
  99. return 0;
  100. /*
  101. * If we're holding a write delegation, then check if we're required
  102. * to flush the i/o on close. If not, then just start the i/o now.
  103. */
  104. if (!nfs4_delegation_flush_on_close(inode))
  105. return filemap_fdatawrite(file->f_mapping);
  106. /* Flush writes to the server and return any errors */
  107. return vfs_fsync(file, 0);
  108. }
  109. #ifdef CONFIG_NFS_V4_2
  110. static ssize_t nfs4_copy_file_range(struct file *file_in, loff_t pos_in,
  111. struct file *file_out, loff_t pos_out,
  112. size_t count, unsigned int flags)
  113. {
  114. if (!nfs_server_capable(file_inode(file_out), NFS_CAP_COPY))
  115. return -EOPNOTSUPP;
  116. if (file_inode(file_in) == file_inode(file_out))
  117. return -EOPNOTSUPP;
  118. return nfs42_proc_copy(file_in, pos_in, file_out, pos_out, count);
  119. }
  120. static loff_t nfs4_file_llseek(struct file *filep, loff_t offset, int whence)
  121. {
  122. loff_t ret;
  123. switch (whence) {
  124. case SEEK_HOLE:
  125. case SEEK_DATA:
  126. ret = nfs42_proc_llseek(filep, offset, whence);
  127. if (ret != -ENOTSUPP)
  128. return ret;
  129. /* Fall through */
  130. default:
  131. return nfs_file_llseek(filep, offset, whence);
  132. }
  133. }
  134. static long nfs42_fallocate(struct file *filep, int mode, loff_t offset, loff_t len)
  135. {
  136. struct inode *inode = file_inode(filep);
  137. long ret;
  138. if (!S_ISREG(inode->i_mode))
  139. return -EOPNOTSUPP;
  140. if ((mode != 0) && (mode != (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)))
  141. return -EOPNOTSUPP;
  142. ret = inode_newsize_ok(inode, offset + len);
  143. if (ret < 0)
  144. return ret;
  145. if (mode & FALLOC_FL_PUNCH_HOLE)
  146. return nfs42_proc_deallocate(filep, offset, len);
  147. return nfs42_proc_allocate(filep, offset, len);
  148. }
  149. static int nfs42_clone_file_range(struct file *src_file, loff_t src_off,
  150. struct file *dst_file, loff_t dst_off, u64 count)
  151. {
  152. struct inode *dst_inode = file_inode(dst_file);
  153. struct nfs_server *server = NFS_SERVER(dst_inode);
  154. struct inode *src_inode = file_inode(src_file);
  155. unsigned int bs = server->clone_blksize;
  156. bool same_inode = false;
  157. int ret;
  158. /* check alignment w.r.t. clone_blksize */
  159. ret = -EINVAL;
  160. if (bs) {
  161. if (!IS_ALIGNED(src_off, bs) || !IS_ALIGNED(dst_off, bs))
  162. goto out;
  163. if (!IS_ALIGNED(count, bs) && i_size_read(src_inode) != (src_off + count))
  164. goto out;
  165. }
  166. if (src_inode == dst_inode)
  167. same_inode = true;
  168. /* XXX: do we lock at all? what if server needs CB_RECALL_LAYOUT? */
  169. if (same_inode) {
  170. inode_lock(src_inode);
  171. } else if (dst_inode < src_inode) {
  172. inode_lock_nested(dst_inode, I_MUTEX_PARENT);
  173. inode_lock_nested(src_inode, I_MUTEX_CHILD);
  174. } else {
  175. inode_lock_nested(src_inode, I_MUTEX_PARENT);
  176. inode_lock_nested(dst_inode, I_MUTEX_CHILD);
  177. }
  178. /* flush all pending writes on both src and dst so that server
  179. * has the latest data */
  180. ret = nfs_sync_inode(src_inode);
  181. if (ret)
  182. goto out_unlock;
  183. ret = nfs_sync_inode(dst_inode);
  184. if (ret)
  185. goto out_unlock;
  186. ret = nfs42_proc_clone(src_file, dst_file, src_off, dst_off, count);
  187. /* truncate inode page cache of the dst range so that future reads can fetch
  188. * new data from server */
  189. if (!ret)
  190. truncate_inode_pages_range(&dst_inode->i_data, dst_off, dst_off + count - 1);
  191. out_unlock:
  192. if (same_inode) {
  193. inode_unlock(src_inode);
  194. } else if (dst_inode < src_inode) {
  195. inode_unlock(src_inode);
  196. inode_unlock(dst_inode);
  197. } else {
  198. inode_unlock(dst_inode);
  199. inode_unlock(src_inode);
  200. }
  201. out:
  202. return ret;
  203. }
  204. #endif /* CONFIG_NFS_V4_2 */
  205. const struct file_operations nfs4_file_operations = {
  206. .read_iter = nfs_file_read,
  207. .write_iter = nfs_file_write,
  208. .mmap = nfs_file_mmap,
  209. .open = nfs4_file_open,
  210. .flush = nfs4_file_flush,
  211. .release = nfs_file_release,
  212. .fsync = nfs_file_fsync,
  213. .lock = nfs_lock,
  214. .flock = nfs_flock,
  215. .splice_read = generic_file_splice_read,
  216. .splice_write = iter_file_splice_write,
  217. .check_flags = nfs_check_flags,
  218. .setlease = simple_nosetlease,
  219. #ifdef CONFIG_NFS_V4_2
  220. .copy_file_range = nfs4_copy_file_range,
  221. .llseek = nfs4_file_llseek,
  222. .fallocate = nfs42_fallocate,
  223. .clone_file_range = nfs42_clone_file_range,
  224. #else
  225. .llseek = nfs_file_llseek,
  226. #endif
  227. };