nfs4file.c 6.6 KB

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