nfs4file.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * linux/fs/nfs/file.c
  3. *
  4. * Copyright (C) 1992 Rick Sladkey
  5. */
  6. #include <linux/fs.h>
  7. #include <linux/file.h>
  8. #include <linux/falloc.h>
  9. #include <linux/nfs_fs.h>
  10. #include <uapi/linux/btrfs.h> /* BTRFS_IOC_CLONE/BTRFS_IOC_CLONE_RANGE */
  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. openflags--;
  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);
  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. case -EPERM:
  64. case -EACCES:
  65. case -EDQUOT:
  66. case -ENOSPC:
  67. case -EROFS:
  68. goto out_put_ctx;
  69. default:
  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 (file_inode(file_in) == file_inode(file_out))
  115. return -EINVAL;
  116. return nfs42_proc_copy(file_in, pos_in, file_out, pos_out, count);
  117. }
  118. static loff_t nfs4_file_llseek(struct file *filep, loff_t offset, int whence)
  119. {
  120. loff_t ret;
  121. switch (whence) {
  122. case SEEK_HOLE:
  123. case SEEK_DATA:
  124. ret = nfs42_proc_llseek(filep, offset, whence);
  125. if (ret != -ENOTSUPP)
  126. return ret;
  127. default:
  128. return nfs_file_llseek(filep, offset, whence);
  129. }
  130. }
  131. static long nfs42_fallocate(struct file *filep, int mode, loff_t offset, loff_t len)
  132. {
  133. struct inode *inode = file_inode(filep);
  134. long ret;
  135. if (!S_ISREG(inode->i_mode))
  136. return -EOPNOTSUPP;
  137. if ((mode != 0) && (mode != (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)))
  138. return -EOPNOTSUPP;
  139. ret = inode_newsize_ok(inode, offset + len);
  140. if (ret < 0)
  141. return ret;
  142. if (mode & FALLOC_FL_PUNCH_HOLE)
  143. return nfs42_proc_deallocate(filep, offset, len);
  144. return nfs42_proc_allocate(filep, offset, len);
  145. }
  146. static int nfs42_clone_file_range(struct file *src_file, loff_t src_off,
  147. struct file *dst_file, loff_t dst_off, u64 count)
  148. {
  149. struct inode *dst_inode = file_inode(dst_file);
  150. struct nfs_server *server = NFS_SERVER(dst_inode);
  151. struct inode *src_inode = file_inode(src_file);
  152. unsigned int bs = server->clone_blksize;
  153. bool same_inode = false;
  154. int ret;
  155. /* check alignment w.r.t. clone_blksize */
  156. ret = -EINVAL;
  157. if (bs) {
  158. if (!IS_ALIGNED(src_off, bs) || !IS_ALIGNED(dst_off, bs))
  159. goto out;
  160. if (!IS_ALIGNED(count, bs) && i_size_read(src_inode) != (src_off + count))
  161. goto out;
  162. }
  163. if (src_inode == dst_inode)
  164. same_inode = true;
  165. /* XXX: do we lock at all? what if server needs CB_RECALL_LAYOUT? */
  166. if (same_inode) {
  167. inode_lock(src_inode);
  168. } else if (dst_inode < src_inode) {
  169. inode_lock_nested(dst_inode, I_MUTEX_PARENT);
  170. inode_lock_nested(src_inode, I_MUTEX_CHILD);
  171. } else {
  172. inode_lock_nested(src_inode, I_MUTEX_PARENT);
  173. inode_lock_nested(dst_inode, I_MUTEX_CHILD);
  174. }
  175. /* flush all pending writes on both src and dst so that server
  176. * has the latest data */
  177. ret = nfs_sync_inode(src_inode);
  178. if (ret)
  179. goto out_unlock;
  180. ret = nfs_sync_inode(dst_inode);
  181. if (ret)
  182. goto out_unlock;
  183. ret = nfs42_proc_clone(src_file, dst_file, src_off, dst_off, count);
  184. /* truncate inode page cache of the dst range so that future reads can fetch
  185. * new data from server */
  186. if (!ret)
  187. truncate_inode_pages_range(&dst_inode->i_data, dst_off, dst_off + count - 1);
  188. out_unlock:
  189. if (same_inode) {
  190. inode_unlock(src_inode);
  191. } else if (dst_inode < src_inode) {
  192. inode_unlock(src_inode);
  193. inode_unlock(dst_inode);
  194. } else {
  195. inode_unlock(dst_inode);
  196. inode_unlock(src_inode);
  197. }
  198. out:
  199. return ret;
  200. }
  201. #endif /* CONFIG_NFS_V4_2 */
  202. const struct file_operations nfs4_file_operations = {
  203. .read_iter = nfs_file_read,
  204. .write_iter = nfs_file_write,
  205. .mmap = nfs_file_mmap,
  206. .open = nfs4_file_open,
  207. .flush = nfs4_file_flush,
  208. .release = nfs_file_release,
  209. .fsync = nfs_file_fsync,
  210. .lock = nfs_lock,
  211. .flock = nfs_flock,
  212. .splice_read = generic_file_splice_read,
  213. .splice_write = iter_file_splice_write,
  214. .check_flags = nfs_check_flags,
  215. .setlease = simple_nosetlease,
  216. #ifdef CONFIG_NFS_V4_2
  217. .copy_file_range = nfs4_copy_file_range,
  218. .llseek = nfs4_file_llseek,
  219. .fallocate = nfs42_fallocate,
  220. .clone_file_range = nfs42_clone_file_range,
  221. #else
  222. .llseek = nfs_file_llseek,
  223. #endif
  224. };