nfs4file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. filp->f_mode |= FMODE_CAN_ODIRECT;
  83. out_put_ctx:
  84. put_nfs_open_context(ctx);
  85. out:
  86. dput(parent);
  87. return err;
  88. out_drop:
  89. d_drop(dentry);
  90. err = -EOPENSTALE;
  91. goto out_put_ctx;
  92. }
  93. /*
  94. * Flush all dirty pages, and check for write errors.
  95. */
  96. static int
  97. nfs4_file_flush(struct file *file, fl_owner_t id)
  98. {
  99. struct inode *inode = file_inode(file);
  100. errseq_t since;
  101. dprintk("NFS: flush(%pD2)\n", file);
  102. nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
  103. if ((file->f_mode & FMODE_WRITE) == 0)
  104. return 0;
  105. /*
  106. * If we're holding a write delegation, then check if we're required
  107. * to flush the i/o on close. If not, then just start the i/o now.
  108. */
  109. if (!nfs4_delegation_flush_on_close(inode))
  110. return filemap_fdatawrite(file->f_mapping);
  111. /* Flush writes to the server and return any errors */
  112. since = filemap_sample_wb_err(file->f_mapping);
  113. nfs_wb_all(inode);
  114. return filemap_check_wb_err(file->f_mapping, since);
  115. }
  116. #ifdef CONFIG_NFS_V4_2
  117. static ssize_t __nfs4_copy_file_range(struct file *file_in, loff_t pos_in,
  118. struct file *file_out, loff_t pos_out,
  119. size_t count, unsigned int flags)
  120. {
  121. struct nfs42_copy_notify_res *cn_resp = NULL;
  122. struct nl4_server *nss = NULL;
  123. nfs4_stateid *cnrs = NULL;
  124. ssize_t ret;
  125. bool sync = false;
  126. /* Only offload copy if superblock is the same */
  127. if (file_in->f_op != &nfs4_file_operations)
  128. return -EXDEV;
  129. if (!nfs_server_capable(file_inode(file_out), NFS_CAP_COPY) ||
  130. !nfs_server_capable(file_inode(file_in), NFS_CAP_COPY))
  131. return -EOPNOTSUPP;
  132. if (file_inode(file_in) == file_inode(file_out))
  133. return -EOPNOTSUPP;
  134. /* if the copy size if smaller than 2 RPC payloads, make it
  135. * synchronous
  136. */
  137. if (count <= 2 * NFS_SERVER(file_inode(file_in))->rsize)
  138. sync = true;
  139. retry:
  140. if (!nfs42_files_from_same_server(file_in, file_out)) {
  141. /*
  142. * for inter copy, if copy size is too small
  143. * then fallback to generic copy.
  144. */
  145. if (sync)
  146. return -EOPNOTSUPP;
  147. cn_resp = kzalloc(sizeof(struct nfs42_copy_notify_res),
  148. GFP_KERNEL);
  149. if (unlikely(cn_resp == NULL))
  150. return -ENOMEM;
  151. ret = nfs42_proc_copy_notify(file_in, file_out, cn_resp);
  152. if (ret) {
  153. ret = -EOPNOTSUPP;
  154. goto out;
  155. }
  156. nss = &cn_resp->cnr_src;
  157. cnrs = &cn_resp->cnr_stateid;
  158. }
  159. ret = nfs42_proc_copy(file_in, pos_in, file_out, pos_out, count,
  160. nss, cnrs, sync);
  161. out:
  162. kfree(cn_resp);
  163. if (ret == -EAGAIN)
  164. goto retry;
  165. return ret;
  166. }
  167. static ssize_t nfs4_copy_file_range(struct file *file_in, loff_t pos_in,
  168. struct file *file_out, loff_t pos_out,
  169. size_t count, unsigned int flags)
  170. {
  171. ssize_t ret;
  172. ret = __nfs4_copy_file_range(file_in, pos_in, file_out, pos_out, count,
  173. flags);
  174. if (ret == -EOPNOTSUPP || ret == -EXDEV)
  175. ret = generic_copy_file_range(file_in, pos_in, file_out,
  176. pos_out, count, flags);
  177. return ret;
  178. }
  179. static loff_t nfs4_file_llseek(struct file *filep, loff_t offset, int whence)
  180. {
  181. loff_t ret;
  182. switch (whence) {
  183. case SEEK_HOLE:
  184. case SEEK_DATA:
  185. ret = nfs42_proc_llseek(filep, offset, whence);
  186. if (ret != -EOPNOTSUPP)
  187. return ret;
  188. fallthrough;
  189. default:
  190. return nfs_file_llseek(filep, offset, whence);
  191. }
  192. }
  193. static long nfs42_fallocate(struct file *filep, int mode, loff_t offset, loff_t len)
  194. {
  195. struct inode *inode = file_inode(filep);
  196. long ret;
  197. if (!S_ISREG(inode->i_mode))
  198. return -EOPNOTSUPP;
  199. if ((mode != 0) && (mode != (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)))
  200. return -EOPNOTSUPP;
  201. ret = inode_newsize_ok(inode, offset + len);
  202. if (ret < 0)
  203. return ret;
  204. if (mode & FALLOC_FL_PUNCH_HOLE)
  205. return nfs42_proc_deallocate(filep, offset, len);
  206. return nfs42_proc_allocate(filep, offset, len);
  207. }
  208. static loff_t nfs42_remap_file_range(struct file *src_file, loff_t src_off,
  209. struct file *dst_file, loff_t dst_off, loff_t count,
  210. unsigned int remap_flags)
  211. {
  212. struct inode *dst_inode = file_inode(dst_file);
  213. struct nfs_server *server = NFS_SERVER(dst_inode);
  214. struct inode *src_inode = file_inode(src_file);
  215. unsigned int bs = server->clone_blksize;
  216. bool same_inode = false;
  217. int ret;
  218. /* NFS does not support deduplication. */
  219. if (remap_flags & REMAP_FILE_DEDUP)
  220. return -EOPNOTSUPP;
  221. if (remap_flags & ~REMAP_FILE_ADVISORY)
  222. return -EINVAL;
  223. if (IS_SWAPFILE(dst_inode) || IS_SWAPFILE(src_inode))
  224. return -ETXTBSY;
  225. /* check alignment w.r.t. clone_blksize */
  226. ret = -EINVAL;
  227. if (bs) {
  228. if (!IS_ALIGNED(src_off, bs) || !IS_ALIGNED(dst_off, bs))
  229. goto out;
  230. if (!IS_ALIGNED(count, bs) && i_size_read(src_inode) != (src_off + count))
  231. goto out;
  232. }
  233. if (src_inode == dst_inode)
  234. same_inode = true;
  235. /* XXX: do we lock at all? what if server needs CB_RECALL_LAYOUT? */
  236. if (same_inode) {
  237. inode_lock(src_inode);
  238. } else if (dst_inode < src_inode) {
  239. inode_lock_nested(dst_inode, I_MUTEX_PARENT);
  240. inode_lock_nested(src_inode, I_MUTEX_CHILD);
  241. } else {
  242. inode_lock_nested(src_inode, I_MUTEX_PARENT);
  243. inode_lock_nested(dst_inode, I_MUTEX_CHILD);
  244. }
  245. /* flush all pending writes on both src and dst so that server
  246. * has the latest data */
  247. ret = nfs_sync_inode(src_inode);
  248. if (ret)
  249. goto out_unlock;
  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, filep->f_mode,
  315. 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, long arg, struct file_lock **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 = generic_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. };