nfs4file.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 "delegation.h"
  11. #include "internal.h"
  12. #include "iostat.h"
  13. #include "fscache.h"
  14. #include "pnfs.h"
  15. #include "nfstrace.h"
  16. #ifdef CONFIG_NFS_V4_2
  17. #include "nfs42.h"
  18. #endif
  19. #define NFSDBG_FACILITY NFSDBG_FILE
  20. static int
  21. nfs4_file_open(struct inode *inode, struct file *filp)
  22. {
  23. struct nfs_open_context *ctx;
  24. struct dentry *dentry = filp->f_path.dentry;
  25. struct dentry *parent = NULL;
  26. struct inode *dir;
  27. unsigned openflags = filp->f_flags;
  28. struct iattr attr;
  29. int err;
  30. /*
  31. * If no cached dentry exists or if it's negative, NFSv4 handled the
  32. * opens in ->lookup() or ->create().
  33. *
  34. * We only get this far for a cached positive dentry. We skipped
  35. * revalidation, so handle it here by dropping the dentry and returning
  36. * -EOPENSTALE. The VFS will retry the lookup/create/open.
  37. */
  38. dprintk("NFS: open file(%pd2)\n", dentry);
  39. err = nfs_check_flags(openflags);
  40. if (err)
  41. return err;
  42. if ((openflags & O_ACCMODE) == 3)
  43. openflags--;
  44. /* We can't create new files here */
  45. openflags &= ~(O_CREAT|O_EXCL);
  46. parent = dget_parent(dentry);
  47. dir = d_inode(parent);
  48. ctx = alloc_nfs_open_context(filp->f_path.dentry, filp->f_mode);
  49. err = PTR_ERR(ctx);
  50. if (IS_ERR(ctx))
  51. goto out;
  52. attr.ia_valid = ATTR_OPEN;
  53. if (openflags & O_TRUNC) {
  54. attr.ia_valid |= ATTR_SIZE;
  55. attr.ia_size = 0;
  56. nfs_sync_inode(inode);
  57. }
  58. inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, NULL);
  59. if (IS_ERR(inode)) {
  60. err = PTR_ERR(inode);
  61. switch (err) {
  62. case -EPERM:
  63. case -EACCES:
  64. case -EDQUOT:
  65. case -ENOSPC:
  66. case -EROFS:
  67. goto out_put_ctx;
  68. default:
  69. goto out_drop;
  70. }
  71. }
  72. if (inode != d_inode(dentry))
  73. goto out_drop;
  74. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  75. nfs_file_set_open_context(filp, ctx);
  76. nfs_fscache_open_file(inode, filp);
  77. err = 0;
  78. out_put_ctx:
  79. put_nfs_open_context(ctx);
  80. out:
  81. dput(parent);
  82. return err;
  83. out_drop:
  84. d_drop(dentry);
  85. err = -EOPENSTALE;
  86. goto out_put_ctx;
  87. }
  88. /*
  89. * Flush all dirty pages, and check for write errors.
  90. */
  91. static int
  92. nfs4_file_flush(struct file *file, fl_owner_t id)
  93. {
  94. struct inode *inode = file_inode(file);
  95. dprintk("NFS: flush(%pD2)\n", file);
  96. nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
  97. if ((file->f_mode & FMODE_WRITE) == 0)
  98. return 0;
  99. /*
  100. * If we're holding a write delegation, then check if we're required
  101. * to flush the i/o on close. If not, then just start the i/o now.
  102. */
  103. if (!nfs4_delegation_flush_on_close(inode))
  104. return filemap_fdatawrite(file->f_mapping);
  105. /* Flush writes to the server and return any errors */
  106. return vfs_fsync(file, 0);
  107. }
  108. static int
  109. nfs4_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  110. {
  111. int ret;
  112. struct inode *inode = file_inode(file);
  113. trace_nfs_fsync_enter(inode);
  114. nfs_inode_dio_wait(inode);
  115. do {
  116. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  117. if (ret != 0)
  118. break;
  119. mutex_lock(&inode->i_mutex);
  120. ret = nfs_file_fsync_commit(file, start, end, datasync);
  121. if (!ret)
  122. ret = pnfs_sync_inode(inode, !!datasync);
  123. mutex_unlock(&inode->i_mutex);
  124. /*
  125. * If nfs_file_fsync_commit detected a server reboot, then
  126. * resend all dirty pages that might have been covered by
  127. * the NFS_CONTEXT_RESEND_WRITES flag
  128. */
  129. start = 0;
  130. end = LLONG_MAX;
  131. } while (ret == -EAGAIN);
  132. trace_nfs_fsync_exit(inode, ret);
  133. return ret;
  134. }
  135. #ifdef CONFIG_NFS_V4_2
  136. static loff_t nfs4_file_llseek(struct file *filep, loff_t offset, int whence)
  137. {
  138. loff_t ret;
  139. switch (whence) {
  140. case SEEK_HOLE:
  141. case SEEK_DATA:
  142. ret = nfs42_proc_llseek(filep, offset, whence);
  143. if (ret != -ENOTSUPP)
  144. return ret;
  145. default:
  146. return nfs_file_llseek(filep, offset, whence);
  147. }
  148. }
  149. static long nfs42_fallocate(struct file *filep, int mode, loff_t offset, loff_t len)
  150. {
  151. struct inode *inode = file_inode(filep);
  152. long ret;
  153. if (!S_ISREG(inode->i_mode))
  154. return -EOPNOTSUPP;
  155. if ((mode != 0) && (mode != (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)))
  156. return -EOPNOTSUPP;
  157. ret = inode_newsize_ok(inode, offset + len);
  158. if (ret < 0)
  159. return ret;
  160. if (mode & FALLOC_FL_PUNCH_HOLE)
  161. return nfs42_proc_deallocate(filep, offset, len);
  162. return nfs42_proc_allocate(filep, offset, len);
  163. }
  164. static noinline long
  165. nfs42_ioctl_clone(struct file *dst_file, unsigned long srcfd,
  166. u64 src_off, u64 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 fd src_file;
  171. struct inode *src_inode;
  172. unsigned int bs = server->clone_blksize;
  173. int ret;
  174. /* dst file must be opened for writing */
  175. if (!(dst_file->f_mode & FMODE_WRITE))
  176. return -EINVAL;
  177. ret = mnt_want_write_file(dst_file);
  178. if (ret)
  179. return ret;
  180. src_file = fdget(srcfd);
  181. if (!src_file.file) {
  182. ret = -EBADF;
  183. goto out_drop_write;
  184. }
  185. src_inode = file_inode(src_file.file);
  186. /* src and dst must be different files */
  187. ret = -EINVAL;
  188. if (src_inode == dst_inode)
  189. goto out_fput;
  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. /* XXX: do we lock at all? what if server needs CB_RECALL_LAYOUT? */
  210. if (dst_inode < src_inode) {
  211. mutex_lock_nested(&dst_inode->i_mutex, I_MUTEX_PARENT);
  212. mutex_lock_nested(&src_inode->i_mutex, I_MUTEX_CHILD);
  213. } else {
  214. mutex_lock_nested(&src_inode->i_mutex, I_MUTEX_PARENT);
  215. mutex_lock_nested(&dst_inode->i_mutex, I_MUTEX_CHILD);
  216. }
  217. /* flush all pending writes on both src and dst so that server
  218. * has the latest data */
  219. ret = nfs_sync_inode(src_inode);
  220. if (ret)
  221. goto out_unlock;
  222. ret = nfs_sync_inode(dst_inode);
  223. if (ret)
  224. goto out_unlock;
  225. ret = nfs42_proc_clone(src_file.file, dst_file, src_off, dst_off, count);
  226. /* truncate inode page cache of the dst range so that future reads can fetch
  227. * new data from server */
  228. if (!ret)
  229. truncate_inode_pages_range(&dst_inode->i_data, dst_off, dst_off + count - 1);
  230. out_unlock:
  231. if (dst_inode < src_inode) {
  232. mutex_unlock(&src_inode->i_mutex);
  233. mutex_unlock(&dst_inode->i_mutex);
  234. } else {
  235. mutex_unlock(&dst_inode->i_mutex);
  236. mutex_unlock(&src_inode->i_mutex);
  237. }
  238. out_fput:
  239. fdput(src_file);
  240. out_drop_write:
  241. mnt_drop_write_file(dst_file);
  242. return ret;
  243. }
  244. static long nfs42_ioctl_clone_range(struct file *dst_file, void __user *argp)
  245. {
  246. struct nfs_ioctl_clone_range_args args;
  247. if (copy_from_user(&args, argp, sizeof(args)))
  248. return -EFAULT;
  249. return nfs42_ioctl_clone(dst_file, args.src_fd, args.src_off, args.dst_off, args.count);
  250. }
  251. #else
  252. static long nfs42_ioctl_clone(struct file *dst_file, unsigned long srcfd,
  253. u64 src_off, u64 dst_off, u64 count)
  254. {
  255. return -ENOTTY;
  256. }
  257. static long nfs42_ioctl_clone_range(struct file *dst_file, void __user *argp)
  258. {
  259. return -ENOTTY;
  260. }
  261. #endif /* CONFIG_NFS_V4_2 */
  262. long nfs4_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  263. {
  264. void __user *argp = (void __user *)arg;
  265. switch (cmd) {
  266. case NFS_IOC_CLONE:
  267. return nfs42_ioctl_clone(file, arg, 0, 0, 0);
  268. case NFS_IOC_CLONE_RANGE:
  269. return nfs42_ioctl_clone_range(file, argp);
  270. }
  271. return -ENOTTY;
  272. }
  273. const struct file_operations nfs4_file_operations = {
  274. #ifdef CONFIG_NFS_V4_2
  275. .llseek = nfs4_file_llseek,
  276. #else
  277. .llseek = nfs_file_llseek,
  278. #endif
  279. .read_iter = nfs_file_read,
  280. .write_iter = nfs_file_write,
  281. .mmap = nfs_file_mmap,
  282. .open = nfs4_file_open,
  283. .flush = nfs4_file_flush,
  284. .release = nfs_file_release,
  285. .fsync = nfs4_file_fsync,
  286. .lock = nfs_lock,
  287. .flock = nfs_flock,
  288. .splice_read = nfs_file_splice_read,
  289. .splice_write = iter_file_splice_write,
  290. #ifdef CONFIG_NFS_V4_2
  291. .fallocate = nfs42_fallocate,
  292. #endif /* CONFIG_NFS_V4_2 */
  293. .check_flags = nfs_check_flags,
  294. .setlease = simple_nosetlease,
  295. #ifdef CONFIG_COMPAT
  296. .unlocked_ioctl = nfs4_ioctl,
  297. #else
  298. .compat_ioctl = nfs4_ioctl,
  299. #endif /* CONFIG_COMPAT */
  300. };