nfs4file.c 11 KB

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