nfs4file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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_obj(struct nfs42_copy_notify_res);
  146. if (unlikely(cn_resp == NULL))
  147. return -ENOMEM;
  148. ret = nfs42_proc_copy_notify(file_in, file_out, cn_resp);
  149. if (ret) {
  150. ret = -EOPNOTSUPP;
  151. goto out;
  152. }
  153. nss = &cn_resp->cnr_src;
  154. cnrs = &cn_resp->cnr_stateid;
  155. }
  156. ret = nfs42_proc_copy(file_in, pos_in, file_out, pos_out, count,
  157. nss, cnrs, sync);
  158. out:
  159. kfree(cn_resp);
  160. if (ret == -EAGAIN)
  161. goto retry;
  162. return ret;
  163. }
  164. static ssize_t nfs4_copy_file_range(struct file *file_in, loff_t pos_in,
  165. struct file *file_out, loff_t pos_out,
  166. size_t count, unsigned int flags)
  167. {
  168. ssize_t ret;
  169. ret = __nfs4_copy_file_range(file_in, pos_in, file_out, pos_out, count,
  170. flags);
  171. if (ret == -EOPNOTSUPP || ret == -EXDEV)
  172. ret = splice_copy_file_range(file_in, pos_in, file_out,
  173. pos_out, count);
  174. return ret;
  175. }
  176. static loff_t nfs4_file_llseek(struct file *filep, loff_t offset, int whence)
  177. {
  178. loff_t ret;
  179. switch (whence) {
  180. case SEEK_HOLE:
  181. case SEEK_DATA:
  182. ret = nfs42_proc_llseek(filep, offset, whence);
  183. if (ret != -EOPNOTSUPP)
  184. return ret;
  185. fallthrough;
  186. default:
  187. return nfs_file_llseek(filep, offset, whence);
  188. }
  189. }
  190. static long nfs42_fallocate(struct file *filep, int mode, loff_t offset, loff_t len)
  191. {
  192. struct inode *inode = file_inode(filep);
  193. long ret;
  194. if (!S_ISREG(inode->i_mode))
  195. return -EOPNOTSUPP;
  196. switch (mode) {
  197. case 0:
  198. case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE:
  199. case FALLOC_FL_ZERO_RANGE:
  200. break;
  201. default:
  202. return -EOPNOTSUPP;
  203. }
  204. ret = inode_newsize_ok(inode, offset + len);
  205. if (ret < 0)
  206. return ret;
  207. if (mode & FALLOC_FL_PUNCH_HOLE)
  208. return nfs42_proc_deallocate(filep, offset, len);
  209. else if (mode & FALLOC_FL_ZERO_RANGE)
  210. return nfs42_proc_zero_range(filep, offset ,len);
  211. return nfs42_proc_allocate(filep, offset, len);
  212. }
  213. static loff_t nfs42_remap_file_range(struct file *src_file, loff_t src_off,
  214. struct file *dst_file, loff_t dst_off, loff_t count,
  215. unsigned int remap_flags)
  216. {
  217. struct inode *dst_inode = file_inode(dst_file);
  218. struct nfs_server *server = NFS_SERVER(dst_inode);
  219. struct inode *src_inode = file_inode(src_file);
  220. unsigned int bs = server->clone_blksize;
  221. int ret;
  222. /* NFS does not support deduplication. */
  223. if (remap_flags & REMAP_FILE_DEDUP)
  224. return -EOPNOTSUPP;
  225. if (remap_flags & ~REMAP_FILE_ADVISORY)
  226. return -EINVAL;
  227. if (IS_SWAPFILE(dst_inode) || IS_SWAPFILE(src_inode))
  228. return -ETXTBSY;
  229. /* check alignment w.r.t. clone_blksize */
  230. ret = -EINVAL;
  231. if (bs) {
  232. if (!IS_ALIGNED(src_off, bs) || !IS_ALIGNED(dst_off, bs))
  233. goto out;
  234. if (!IS_ALIGNED(count, bs) && i_size_read(src_inode) != (src_off + count))
  235. goto out;
  236. }
  237. /* XXX: do we lock at all? what if server needs CB_RECALL_LAYOUT? */
  238. lock_two_nondirectories(src_inode, dst_inode);
  239. /* flush all pending writes on both src and dst so that server
  240. * has the latest data */
  241. nfs_file_block_o_direct(NFS_I(src_inode));
  242. ret = nfs_sync_inode(src_inode);
  243. if (ret)
  244. goto out_unlock;
  245. nfs_file_block_o_direct(NFS_I(dst_inode));
  246. ret = nfs_sync_inode(dst_inode);
  247. if (ret)
  248. goto out_unlock;
  249. ret = nfs42_proc_clone(src_file, dst_file, src_off, dst_off, count);
  250. /* truncate inode page cache of the dst range so that future reads can fetch
  251. * new data from server */
  252. if (!ret)
  253. truncate_inode_pages_range(&dst_inode->i_data, dst_off, dst_off + count - 1);
  254. out_unlock:
  255. unlock_two_nondirectories(src_inode, dst_inode);
  256. out:
  257. return ret < 0 ? ret : count;
  258. }
  259. static int read_name_gen = 1;
  260. #define SSC_READ_NAME_BODY "ssc_read_%d"
  261. static struct file *__nfs42_ssc_open(struct vfsmount *ss_mnt,
  262. struct nfs_fh *src_fh, nfs4_stateid *stateid)
  263. {
  264. struct nfs_fattr *fattr = nfs_alloc_fattr();
  265. struct file *filep, *res;
  266. struct nfs_server *server;
  267. struct inode *r_ino = NULL;
  268. struct nfs_open_context *ctx;
  269. struct nfs4_state_owner *sp;
  270. char *read_name = NULL;
  271. int len, status = 0;
  272. server = NFS_SB(ss_mnt->mnt_sb);
  273. if (!fattr)
  274. return ERR_PTR(-ENOMEM);
  275. status = nfs4_proc_getattr(server, src_fh, fattr, NULL);
  276. if (status < 0) {
  277. res = ERR_PTR(status);
  278. goto out;
  279. }
  280. if (!S_ISREG(fattr->mode)) {
  281. res = ERR_PTR(-EBADF);
  282. goto out;
  283. }
  284. res = ERR_PTR(-ENOMEM);
  285. len = strlen(SSC_READ_NAME_BODY) + 16;
  286. read_name = kzalloc(len, GFP_KERNEL);
  287. if (read_name == NULL)
  288. goto out;
  289. snprintf(read_name, len, SSC_READ_NAME_BODY, read_name_gen++);
  290. r_ino = nfs_fhget(ss_mnt->mnt_sb, src_fh, fattr);
  291. if (IS_ERR(r_ino)) {
  292. res = ERR_CAST(r_ino);
  293. goto out_free_name;
  294. }
  295. filep = alloc_file_pseudo(r_ino, ss_mnt, read_name, O_RDONLY,
  296. r_ino->i_fop);
  297. if (IS_ERR(filep)) {
  298. res = ERR_CAST(filep);
  299. iput(r_ino);
  300. goto out_free_name;
  301. }
  302. ctx = alloc_nfs_open_context(filep->f_path.dentry,
  303. flags_to_mode(filep->f_flags), filep);
  304. if (IS_ERR(ctx)) {
  305. res = ERR_CAST(ctx);
  306. goto out_filep;
  307. }
  308. res = ERR_PTR(-EINVAL);
  309. sp = nfs4_get_state_owner(server, ctx->cred, GFP_KERNEL);
  310. if (sp == NULL)
  311. goto out_ctx;
  312. ctx->state = nfs4_get_open_state(r_ino, sp);
  313. if (ctx->state == NULL)
  314. goto out_stateowner;
  315. set_bit(NFS_SRV_SSC_COPY_STATE, &ctx->state->flags);
  316. memcpy(&ctx->state->open_stateid.other, &stateid->other,
  317. NFS4_STATEID_OTHER_SIZE);
  318. update_open_stateid(ctx->state, stateid, NULL, filep->f_mode);
  319. set_bit(NFS_OPEN_STATE, &ctx->state->flags);
  320. nfs_file_set_open_context(filep, ctx);
  321. put_nfs_open_context(ctx);
  322. file_ra_state_init(&filep->f_ra, filep->f_mapping->host->i_mapping);
  323. res = filep;
  324. out_free_name:
  325. kfree(read_name);
  326. out:
  327. nfs_free_fattr(fattr);
  328. return res;
  329. out_stateowner:
  330. nfs4_put_state_owner(sp);
  331. out_ctx:
  332. put_nfs_open_context(ctx);
  333. out_filep:
  334. fput(filep);
  335. goto out_free_name;
  336. }
  337. static void __nfs42_ssc_close(struct file *filep)
  338. {
  339. struct nfs_open_context *ctx = nfs_file_open_context(filep);
  340. ctx->state->flags = 0;
  341. }
  342. static const struct nfs4_ssc_client_ops nfs4_ssc_clnt_ops_tbl = {
  343. .sco_open = __nfs42_ssc_open,
  344. .sco_close = __nfs42_ssc_close,
  345. };
  346. /**
  347. * nfs42_ssc_register_ops - Wrapper to register NFS_V4 ops in nfs_common
  348. *
  349. * Return values:
  350. * None
  351. */
  352. void nfs42_ssc_register_ops(void)
  353. {
  354. nfs42_ssc_register(&nfs4_ssc_clnt_ops_tbl);
  355. }
  356. /**
  357. * nfs42_ssc_unregister_ops - wrapper to un-register NFS_V4 ops in nfs_common
  358. *
  359. * Return values:
  360. * None.
  361. */
  362. void nfs42_ssc_unregister_ops(void)
  363. {
  364. nfs42_ssc_unregister(&nfs4_ssc_clnt_ops_tbl);
  365. }
  366. #endif /* CONFIG_NFS_V4_2 */
  367. static int nfs4_setlease(struct file *file, int arg, struct file_lease **lease,
  368. void **priv)
  369. {
  370. return nfs4_proc_setlease(file, arg, lease, priv);
  371. }
  372. const struct file_operations nfs4_file_operations = {
  373. .read_iter = nfs_file_read,
  374. .write_iter = nfs_file_write,
  375. .mmap_prepare = nfs_file_mmap_prepare,
  376. .open = nfs4_file_open,
  377. .flush = nfs4_file_flush,
  378. .release = nfs_file_release,
  379. .fsync = nfs_file_fsync,
  380. .lock = nfs_lock,
  381. .flock = nfs_flock,
  382. .splice_read = nfs_file_splice_read,
  383. .splice_write = iter_file_splice_write,
  384. .check_flags = nfs_check_flags,
  385. .setlease = nfs4_setlease,
  386. #ifdef CONFIG_NFS_V4_2
  387. .copy_file_range = nfs4_copy_file_range,
  388. .llseek = nfs4_file_llseek,
  389. .fallocate = nfs42_fallocate,
  390. .remap_file_range = nfs42_remap_file_range,
  391. #else
  392. .llseek = nfs_file_llseek,
  393. #endif
  394. .fop_flags = FOP_DONTCACHE,
  395. };