nfs4file.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. 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. mutex_lock(&inode->i_mutex);
  121. ret = nfs_file_fsync_commit(file, start, end, datasync);
  122. if (!ret)
  123. ret = pnfs_sync_inode(inode, !!datasync);
  124. mutex_unlock(&inode->i_mutex);
  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 noinline long
  166. nfs42_ioctl_clone(struct file *dst_file, unsigned long srcfd,
  167. u64 src_off, u64 dst_off, u64 count)
  168. {
  169. struct inode *dst_inode = file_inode(dst_file);
  170. struct nfs_server *server = NFS_SERVER(dst_inode);
  171. struct fd src_file;
  172. struct inode *src_inode;
  173. unsigned int bs = server->clone_blksize;
  174. bool same_inode = false;
  175. int ret;
  176. /* dst file must be opened for writing */
  177. if (!(dst_file->f_mode & FMODE_WRITE))
  178. return -EINVAL;
  179. ret = mnt_want_write_file(dst_file);
  180. if (ret)
  181. return ret;
  182. src_file = fdget(srcfd);
  183. if (!src_file.file) {
  184. ret = -EBADF;
  185. goto out_drop_write;
  186. }
  187. src_inode = file_inode(src_file.file);
  188. if (src_inode == dst_inode)
  189. same_inode = true;
  190. /* src file must be opened for reading */
  191. if (!(src_file.file->f_mode & FMODE_READ))
  192. goto out_fput;
  193. /* src and dst must be regular files */
  194. ret = -EISDIR;
  195. if (!S_ISREG(src_inode->i_mode) || !S_ISREG(dst_inode->i_mode))
  196. goto out_fput;
  197. ret = -EXDEV;
  198. if (src_file.file->f_path.mnt != dst_file->f_path.mnt ||
  199. src_inode->i_sb != dst_inode->i_sb)
  200. goto out_fput;
  201. /* check alignment w.r.t. clone_blksize */
  202. ret = -EINVAL;
  203. if (bs) {
  204. if (!IS_ALIGNED(src_off, bs) || !IS_ALIGNED(dst_off, bs))
  205. goto out_fput;
  206. if (!IS_ALIGNED(count, bs) && i_size_read(src_inode) != (src_off + count))
  207. goto out_fput;
  208. }
  209. /* verify if ranges are overlapped within the same file */
  210. if (same_inode) {
  211. if (dst_off + count > src_off && dst_off < src_off + count)
  212. goto out_fput;
  213. }
  214. /* XXX: do we lock at all? what if server needs CB_RECALL_LAYOUT? */
  215. if (same_inode) {
  216. mutex_lock(&src_inode->i_mutex);
  217. } else if (dst_inode < src_inode) {
  218. mutex_lock_nested(&dst_inode->i_mutex, I_MUTEX_PARENT);
  219. mutex_lock_nested(&src_inode->i_mutex, I_MUTEX_CHILD);
  220. } else {
  221. mutex_lock_nested(&src_inode->i_mutex, I_MUTEX_PARENT);
  222. mutex_lock_nested(&dst_inode->i_mutex, I_MUTEX_CHILD);
  223. }
  224. /* flush all pending writes on both src and dst so that server
  225. * has the latest data */
  226. ret = nfs_sync_inode(src_inode);
  227. if (ret)
  228. goto out_unlock;
  229. ret = nfs_sync_inode(dst_inode);
  230. if (ret)
  231. goto out_unlock;
  232. ret = nfs42_proc_clone(src_file.file, dst_file, src_off, dst_off, count);
  233. /* truncate inode page cache of the dst range so that future reads can fetch
  234. * new data from server */
  235. if (!ret)
  236. truncate_inode_pages_range(&dst_inode->i_data, dst_off, dst_off + count - 1);
  237. out_unlock:
  238. if (same_inode) {
  239. mutex_unlock(&src_inode->i_mutex);
  240. } else if (dst_inode < src_inode) {
  241. mutex_unlock(&src_inode->i_mutex);
  242. mutex_unlock(&dst_inode->i_mutex);
  243. } else {
  244. mutex_unlock(&dst_inode->i_mutex);
  245. mutex_unlock(&src_inode->i_mutex);
  246. }
  247. out_fput:
  248. fdput(src_file);
  249. out_drop_write:
  250. mnt_drop_write_file(dst_file);
  251. return ret;
  252. }
  253. static long nfs42_ioctl_clone_range(struct file *dst_file, void __user *argp)
  254. {
  255. struct btrfs_ioctl_clone_range_args args;
  256. if (copy_from_user(&args, argp, sizeof(args)))
  257. return -EFAULT;
  258. return nfs42_ioctl_clone(dst_file, args.src_fd, args.src_offset,
  259. args.dest_offset, args.src_length);
  260. }
  261. long nfs4_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  262. {
  263. void __user *argp = (void __user *)arg;
  264. switch (cmd) {
  265. case BTRFS_IOC_CLONE:
  266. return nfs42_ioctl_clone(file, arg, 0, 0, 0);
  267. case BTRFS_IOC_CLONE_RANGE:
  268. return nfs42_ioctl_clone_range(file, argp);
  269. }
  270. return -ENOTTY;
  271. }
  272. #endif /* CONFIG_NFS_V4_2 */
  273. const struct file_operations nfs4_file_operations = {
  274. .read_iter = nfs_file_read,
  275. .write_iter = nfs_file_write,
  276. .mmap = nfs_file_mmap,
  277. .open = nfs4_file_open,
  278. .flush = nfs4_file_flush,
  279. .release = nfs_file_release,
  280. .fsync = nfs4_file_fsync,
  281. .lock = nfs_lock,
  282. .flock = nfs_flock,
  283. .splice_read = nfs_file_splice_read,
  284. .splice_write = iter_file_splice_write,
  285. .check_flags = nfs_check_flags,
  286. .setlease = simple_nosetlease,
  287. #ifdef CONFIG_NFS_V4_2
  288. .llseek = nfs4_file_llseek,
  289. .fallocate = nfs42_fallocate,
  290. .unlocked_ioctl = nfs4_ioctl,
  291. .compat_ioctl = nfs4_ioctl,
  292. #else
  293. .llseek = nfs_file_llseek,
  294. #endif
  295. };