nfs4file.c 6.4 KB

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