nfs4file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/nfs/file.c
  4. *
  5. * Copyright (C) 1992 Rick Sladkey
  6. */
  7. #include <linux/fs.h>
  8. #include <linux/file.h>
  9. #include <linux/falloc.h>
  10. #include <linux/mount.h>
  11. #include <linux/nfs_fs.h>
  12. #include <linux/nfs_ssc.h>
  13. #include <linux/splice.h>
  14. #include "delegation.h"
  15. #include "internal.h"
  16. #include "iostat.h"
  17. #include "fscache.h"
  18. #include "pnfs.h"
  19. #include "nfstrace.h"
  20. #ifdef CONFIG_NFS_V4_2
  21. #include "nfs42.h"
  22. #endif
  23. #define NFSDBG_FACILITY NFSDBG_FILE
  24. static int
  25. nfs4_file_open(struct inode *inode, struct file *filp)
  26. {
  27. struct nfs_open_context *ctx;
  28. struct dentry *dentry = file_dentry(filp);
  29. struct dentry *parent = NULL;
  30. struct inode *dir;
  31. unsigned openflags = filp->f_flags;
  32. struct iattr attr;
  33. int err;
  34. /*
  35. * If no cached dentry exists or if it's negative, NFSv4 handled the
  36. * opens in ->lookup() or ->create().
  37. *
  38. * We only get this far for a cached positive dentry. We skipped
  39. * revalidation, so handle it here by dropping the dentry and returning
  40. * -EOPENSTALE. The VFS will retry the lookup/create/open.
  41. */
  42. dprintk("NFS: open file(%pd2)\n", dentry);
  43. err = nfs_check_flags(openflags);
  44. if (err)
  45. return err;
  46. /* We can't create new files here */
  47. openflags &= ~(O_CREAT|O_EXCL);
  48. parent = dget_parent(dentry);
  49. dir = d_inode(parent);
  50. ctx = alloc_nfs_open_context(file_dentry(filp),
  51. flags_to_mode(openflags), filp);
  52. err = PTR_ERR(ctx);
  53. if (IS_ERR(ctx))
  54. goto out;
  55. attr.ia_valid = ATTR_OPEN;
  56. if (openflags & O_TRUNC) {
  57. attr.ia_valid |= ATTR_SIZE;
  58. attr.ia_size = 0;
  59. filemap_write_and_wait(inode->i_mapping);
  60. }
  61. inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, NULL);
  62. if (IS_ERR(inode)) {
  63. err = PTR_ERR(inode);
  64. switch (err) {
  65. default:
  66. goto out_put_ctx;
  67. case -ENOENT:
  68. case -ESTALE:
  69. case -EISDIR:
  70. case -ENOTDIR:
  71. case -ELOOP:
  72. goto out_drop;
  73. }
  74. }
  75. if (inode != d_inode(dentry))
  76. goto out_drop;
  77. nfs_file_set_open_context(filp, ctx);
  78. nfs_fscache_open_file(inode, filp);
  79. err = 0;
  80. filp->f_mode |= FMODE_CAN_ODIRECT;
  81. out_put_ctx:
  82. put_nfs_open_context(ctx);
  83. out:
  84. dput(parent);
  85. return err;
  86. out_drop:
  87. d_drop(dentry);
  88. err = -EOPENSTALE;
  89. goto out_put_ctx;
  90. }
  91. /*
  92. * Flush all dirty pages, and check for write errors.
  93. */
  94. static int
  95. nfs4_file_flush(struct file *file, fl_owner_t id)
  96. {
  97. struct inode *inode = file_inode(file);
  98. errseq_t since;
  99. dprintk("NFS: flush(%pD2)\n", file);
  100. nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
  101. if ((file->f_mode & FMODE_WRITE) == 0)
  102. return 0;
  103. /*
  104. * If we're holding a write delegation, then check if we're required
  105. * to flush the i/o on close. If not, then just start the i/o now.
  106. */
  107. if (!nfs4_delegation_flush_on_close(inode))
  108. return filemap_fdatawrite(file->f_mapping);
  109. /* Flush writes to the server and return any errors */
  110. since = filemap_sample_wb_err(file->f_mapping);
  111. nfs_wb_all(inode);
  112. return filemap_check_wb_err(file->f_mapping, since);
  113. }
  114. #ifdef CONFIG_NFS_V4_2
  115. static ssize_t __nfs4_copy_file_range(struct file *file_in, loff_t pos_in,
  116. struct file *file_out, loff_t pos_out,
  117. size_t count, unsigned int flags)
  118. {
  119. struct nfs42_copy_notify_res *cn_resp = NULL;
  120. struct nl4_server *nss = NULL;
  121. nfs4_stateid *cnrs = NULL;
  122. ssize_t ret;
  123. bool sync = false;
  124. /* Only offload copy if superblock is the same */
  125. if (file_in->f_op != &nfs4_file_operations)
  126. return -EXDEV;
  127. if (!nfs_server_capable(file_inode(file_out), NFS_CAP_COPY) ||
  128. !nfs_server_capable(file_inode(file_in), NFS_CAP_COPY))
  129. return -EOPNOTSUPP;
  130. if (file_inode(file_in) == file_inode(file_out))
  131. return -EOPNOTSUPP;
  132. /* if the copy size if smaller than 2 RPC payloads, make it
  133. * synchronous
  134. */
  135. if (count <= 2 * NFS_SERVER(file_inode(file_in))->rsize)
  136. sync = true;
  137. retry:
  138. if (!nfs42_files_from_same_server(file_in, file_out)) {
  139. /*
  140. * for inter copy, if copy size is too small
  141. * then fallback to generic copy.
  142. */
  143. if (sync)
  144. return -EOPNOTSUPP;
  145. cn_resp = kzalloc(sizeof(struct nfs42_copy_notify_res),
  146. GFP_KERNEL);
  147. if (unlikely(cn_resp == NULL))
  148. return -ENOMEM;
  149. ret = nfs42_proc_copy_notify(file_in, file_out, cn_resp);
  150. if (ret) {
  151. ret = -EOPNOTSUPP;
  152. goto out;
  153. }
  154. nss = &cn_resp->cnr_src;
  155. cnrs = &cn_resp->cnr_stateid;
  156. }
  157. ret = nfs42_proc_copy(file_in, pos_in, file_out, pos_out, count,
  158. nss, cnrs, sync);
  159. out:
  160. kfree(cn_resp);
  161. if (ret == -EAGAIN)
  162. goto retry;
  163. return ret;
  164. }
  165. static ssize_t nfs4_copy_file_range(struct file *file_in, loff_t pos_in,
  166. struct file *file_out, loff_t pos_out,
  167. size_t count, unsigned int flags)
  168. {
  169. ssize_t ret;
  170. ret = __nfs4_copy_file_range(file_in, pos_in, file_out, pos_out, count,
  171. flags);
  172. if (ret == -EOPNOTSUPP || ret == -EXDEV)
  173. ret = splice_copy_file_range(file_in, pos_in, file_out,
  174. pos_out, count);
  175. return ret;
  176. }
  177. static loff_t nfs4_file_llseek(struct file *filep, loff_t offset, int whence)
  178. {
  179. loff_t ret;
  180. switch (whence) {
  181. case SEEK_HOLE:
  182. case SEEK_DATA:
  183. ret = nfs42_proc_llseek(filep, offset, whence);
  184. if (ret != -EOPNOTSUPP)
  185. return ret;
  186. fallthrough;
  187. default:
  188. return nfs_file_llseek(filep, offset, whence);
  189. }
  190. }
  191. static long nfs42_fallocate(struct file *filep, int mode, loff_t offset, loff_t len)
  192. {
  193. struct inode *inode = file_inode(filep);
  194. long ret;
  195. if (!S_ISREG(inode->i_mode))
  196. return -EOPNOTSUPP;
  197. if ((mode != 0) && (mode != (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)))
  198. return -EOPNOTSUPP;
  199. ret = inode_newsize_ok(inode, offset + len);
  200. if (ret < 0)
  201. return ret;
  202. if (mode & FALLOC_FL_PUNCH_HOLE)
  203. return nfs42_proc_deallocate(filep, offset, len);
  204. return nfs42_proc_allocate(filep, offset, len);
  205. }
  206. static loff_t nfs42_remap_file_range(struct file *src_file, loff_t src_off,
  207. struct file *dst_file, loff_t dst_off, loff_t count,
  208. unsigned int remap_flags)
  209. {
  210. struct inode *dst_inode = file_inode(dst_file);
  211. struct nfs_server *server = NFS_SERVER(dst_inode);
  212. struct inode *src_inode = file_inode(src_file);
  213. unsigned int bs = server->clone_blksize;
  214. bool same_inode = false;
  215. int ret;
  216. /* NFS does not support deduplication. */
  217. if (remap_flags & REMAP_FILE_DEDUP)
  218. return -EOPNOTSUPP;
  219. if (remap_flags & ~REMAP_FILE_ADVISORY)
  220. return -EINVAL;
  221. if (IS_SWAPFILE(dst_inode) || IS_SWAPFILE(src_inode))
  222. return -ETXTBSY;
  223. /* check alignment w.r.t. clone_blksize */
  224. ret = -EINVAL;
  225. if (bs) {
  226. if (!IS_ALIGNED(src_off, bs) || !IS_ALIGNED(dst_off, bs))
  227. goto out;
  228. if (!IS_ALIGNED(count, bs) && i_size_read(src_inode) != (src_off + count))
  229. goto out;
  230. }
  231. if (src_inode == dst_inode)
  232. same_inode = true;
  233. /* XXX: do we lock at all? what if server needs CB_RECALL_LAYOUT? */
  234. if (same_inode) {
  235. inode_lock(src_inode);
  236. } else if (dst_inode < src_inode) {
  237. inode_lock_nested(dst_inode, I_MUTEX_PARENT);
  238. inode_lock_nested(src_inode, I_MUTEX_CHILD);
  239. } else {
  240. inode_lock_nested(src_inode, I_MUTEX_PARENT);
  241. inode_lock_nested(dst_inode, I_MUTEX_CHILD);
  242. }
  243. /* flush all pending writes on both src and dst so that server
  244. * has the latest data */
  245. nfs_file_block_o_direct(NFS_I(src_inode));
  246. ret = nfs_sync_inode(src_inode);
  247. if (ret)
  248. goto out_unlock;
  249. nfs_file_block_o_direct(NFS_I(dst_inode));
  250. ret = nfs_sync_inode(dst_inode);
  251. if (ret)
  252. goto out_unlock;
  253. ret = nfs42_proc_clone(src_file, dst_file, src_off, dst_off, count);
  254. /* truncate inode page cache of the dst range so that future reads can fetch
  255. * new data from server */
  256. if (!ret)
  257. truncate_inode_pages_range(&dst_inode->i_data, dst_off, dst_off + count - 1);
  258. out_unlock:
  259. if (same_inode) {
  260. inode_unlock(src_inode);
  261. } else if (dst_inode < src_inode) {
  262. inode_unlock(src_inode);
  263. inode_unlock(dst_inode);
  264. } else {
  265. inode_unlock(dst_inode);
  266. inode_unlock(src_inode);
  267. }
  268. out:
  269. return ret < 0 ? ret : count;
  270. }
  271. static int read_name_gen = 1;
  272. #define SSC_READ_NAME_BODY "ssc_read_%d"
  273. static struct file *__nfs42_ssc_open(struct vfsmount *ss_mnt,
  274. struct nfs_fh *src_fh, nfs4_stateid *stateid)
  275. {
  276. struct nfs_fattr *fattr = nfs_alloc_fattr();
  277. struct file *filep, *res;
  278. struct nfs_server *server;
  279. struct inode *r_ino = NULL;
  280. struct nfs_open_context *ctx;
  281. struct nfs4_state_owner *sp;
  282. char *read_name = NULL;
  283. int len, status = 0;
  284. server = NFS_SB(ss_mnt->mnt_sb);
  285. if (!fattr)
  286. return ERR_PTR(-ENOMEM);
  287. status = nfs4_proc_getattr(server, src_fh, fattr, NULL);
  288. if (status < 0) {
  289. res = ERR_PTR(status);
  290. goto out;
  291. }
  292. if (!S_ISREG(fattr->mode)) {
  293. res = ERR_PTR(-EBADF);
  294. goto out;
  295. }
  296. res = ERR_PTR(-ENOMEM);
  297. len = strlen(SSC_READ_NAME_BODY) + 16;
  298. read_name = kzalloc(len, GFP_KERNEL);
  299. if (read_name == NULL)
  300. goto out;
  301. snprintf(read_name, len, SSC_READ_NAME_BODY, read_name_gen++);
  302. r_ino = nfs_fhget(ss_mnt->mnt_sb, src_fh, fattr);
  303. if (IS_ERR(r_ino)) {
  304. res = ERR_CAST(r_ino);
  305. goto out_free_name;
  306. }
  307. filep = alloc_file_pseudo(r_ino, ss_mnt, read_name, O_RDONLY,
  308. r_ino->i_fop);
  309. if (IS_ERR(filep)) {
  310. res = ERR_CAST(filep);
  311. iput(r_ino);
  312. goto out_free_name;
  313. }
  314. ctx = alloc_nfs_open_context(filep->f_path.dentry,
  315. flags_to_mode(filep->f_flags), filep);
  316. if (IS_ERR(ctx)) {
  317. res = ERR_CAST(ctx);
  318. goto out_filep;
  319. }
  320. res = ERR_PTR(-EINVAL);
  321. sp = nfs4_get_state_owner(server, ctx->cred, GFP_KERNEL);
  322. if (sp == NULL)
  323. goto out_ctx;
  324. ctx->state = nfs4_get_open_state(r_ino, sp);
  325. if (ctx->state == NULL)
  326. goto out_stateowner;
  327. set_bit(NFS_SRV_SSC_COPY_STATE, &ctx->state->flags);
  328. memcpy(&ctx->state->open_stateid.other, &stateid->other,
  329. NFS4_STATEID_OTHER_SIZE);
  330. update_open_stateid(ctx->state, stateid, NULL, filep->f_mode);
  331. set_bit(NFS_OPEN_STATE, &ctx->state->flags);
  332. nfs_file_set_open_context(filep, ctx);
  333. put_nfs_open_context(ctx);
  334. file_ra_state_init(&filep->f_ra, filep->f_mapping->host->i_mapping);
  335. res = filep;
  336. out_free_name:
  337. kfree(read_name);
  338. out:
  339. nfs_free_fattr(fattr);
  340. return res;
  341. out_stateowner:
  342. nfs4_put_state_owner(sp);
  343. out_ctx:
  344. put_nfs_open_context(ctx);
  345. out_filep:
  346. fput(filep);
  347. goto out_free_name;
  348. }
  349. static void __nfs42_ssc_close(struct file *filep)
  350. {
  351. struct nfs_open_context *ctx = nfs_file_open_context(filep);
  352. ctx->state->flags = 0;
  353. }
  354. static const struct nfs4_ssc_client_ops nfs4_ssc_clnt_ops_tbl = {
  355. .sco_open = __nfs42_ssc_open,
  356. .sco_close = __nfs42_ssc_close,
  357. };
  358. /**
  359. * nfs42_ssc_register_ops - Wrapper to register NFS_V4 ops in nfs_common
  360. *
  361. * Return values:
  362. * None
  363. */
  364. void nfs42_ssc_register_ops(void)
  365. {
  366. nfs42_ssc_register(&nfs4_ssc_clnt_ops_tbl);
  367. }
  368. /**
  369. * nfs42_ssc_unregister_ops - wrapper to un-register NFS_V4 ops in nfs_common
  370. *
  371. * Return values:
  372. * None.
  373. */
  374. void nfs42_ssc_unregister_ops(void)
  375. {
  376. nfs42_ssc_unregister(&nfs4_ssc_clnt_ops_tbl);
  377. }
  378. #endif /* CONFIG_NFS_V4_2 */
  379. static int nfs4_setlease(struct file *file, int arg, struct file_lease **lease,
  380. void **priv)
  381. {
  382. return nfs4_proc_setlease(file, arg, lease, priv);
  383. }
  384. const struct file_operations nfs4_file_operations = {
  385. .read_iter = nfs_file_read,
  386. .write_iter = nfs_file_write,
  387. .mmap = nfs_file_mmap,
  388. .open = nfs4_file_open,
  389. .flush = nfs4_file_flush,
  390. .release = nfs_file_release,
  391. .fsync = nfs_file_fsync,
  392. .lock = nfs_lock,
  393. .flock = nfs_flock,
  394. .splice_read = nfs_file_splice_read,
  395. .splice_write = iter_file_splice_write,
  396. .check_flags = nfs_check_flags,
  397. .setlease = nfs4_setlease,
  398. #ifdef CONFIG_NFS_V4_2
  399. .copy_file_range = nfs4_copy_file_range,
  400. .llseek = nfs4_file_llseek,
  401. .fallocate = nfs42_fallocate,
  402. .remap_file_range = nfs42_remap_file_range,
  403. #else
  404. .llseek = nfs_file_llseek,
  405. #endif
  406. };