localio.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * NFS client support for local clients to bypass network stack
  4. *
  5. * Copyright (C) 2014 Weston Andros Adamson <dros@primarydata.com>
  6. * Copyright (C) 2019 Trond Myklebust <trond.myklebust@hammerspace.com>
  7. * Copyright (C) 2024 Mike Snitzer <snitzer@hammerspace.com>
  8. * Copyright (C) 2024 NeilBrown <neilb@suse.de>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/errno.h>
  12. #include <linux/vfs.h>
  13. #include <linux/file.h>
  14. #include <linux/inet.h>
  15. #include <linux/sunrpc/addr.h>
  16. #include <linux/inetdevice.h>
  17. #include <net/addrconf.h>
  18. #include <linux/nfs_common.h>
  19. #include <linux/nfslocalio.h>
  20. #include <linux/bvec.h>
  21. #include <linux/nfs.h>
  22. #include <linux/nfs_fs.h>
  23. #include <linux/nfs_xdr.h>
  24. #include "internal.h"
  25. #include "pnfs.h"
  26. #include "nfstrace.h"
  27. #define NFSDBG_FACILITY NFSDBG_VFS
  28. struct nfs_local_kiocb {
  29. struct kiocb kiocb;
  30. struct bio_vec *bvec;
  31. struct nfs_pgio_header *hdr;
  32. struct work_struct work;
  33. void (*aio_complete_work)(struct work_struct *);
  34. struct nfsd_file *localio;
  35. };
  36. struct nfs_local_fsync_ctx {
  37. struct nfsd_file *localio;
  38. struct nfs_commit_data *data;
  39. struct work_struct work;
  40. struct kref kref;
  41. struct completion *done;
  42. };
  43. static void nfs_local_fsync_work(struct work_struct *work);
  44. static bool localio_enabled __read_mostly = true;
  45. module_param(localio_enabled, bool, 0644);
  46. static bool localio_O_DIRECT_semantics __read_mostly = false;
  47. module_param(localio_O_DIRECT_semantics, bool, 0644);
  48. MODULE_PARM_DESC(localio_O_DIRECT_semantics,
  49. "LOCALIO will use O_DIRECT semantics to filesystem.");
  50. static inline bool nfs_client_is_local(const struct nfs_client *clp)
  51. {
  52. return !!test_bit(NFS_CS_LOCAL_IO, &clp->cl_flags);
  53. }
  54. bool nfs_server_is_local(const struct nfs_client *clp)
  55. {
  56. return nfs_client_is_local(clp) && localio_enabled;
  57. }
  58. EXPORT_SYMBOL_GPL(nfs_server_is_local);
  59. /*
  60. * UUID_IS_LOCAL XDR functions
  61. */
  62. static void localio_xdr_enc_uuidargs(struct rpc_rqst *req,
  63. struct xdr_stream *xdr,
  64. const void *data)
  65. {
  66. const u8 *uuid = data;
  67. encode_opaque_fixed(xdr, uuid, UUID_SIZE);
  68. }
  69. static int localio_xdr_dec_uuidres(struct rpc_rqst *req,
  70. struct xdr_stream *xdr,
  71. void *result)
  72. {
  73. /* void return */
  74. return 0;
  75. }
  76. static const struct rpc_procinfo nfs_localio_procedures[] = {
  77. [LOCALIOPROC_UUID_IS_LOCAL] = {
  78. .p_proc = LOCALIOPROC_UUID_IS_LOCAL,
  79. .p_encode = localio_xdr_enc_uuidargs,
  80. .p_decode = localio_xdr_dec_uuidres,
  81. .p_arglen = XDR_QUADLEN(UUID_SIZE),
  82. .p_replen = 0,
  83. .p_statidx = LOCALIOPROC_UUID_IS_LOCAL,
  84. .p_name = "UUID_IS_LOCAL",
  85. },
  86. };
  87. static unsigned int nfs_localio_counts[ARRAY_SIZE(nfs_localio_procedures)];
  88. static const struct rpc_version nfslocalio_version1 = {
  89. .number = 1,
  90. .nrprocs = ARRAY_SIZE(nfs_localio_procedures),
  91. .procs = nfs_localio_procedures,
  92. .counts = nfs_localio_counts,
  93. };
  94. static const struct rpc_version *nfslocalio_version[] = {
  95. [1] = &nfslocalio_version1,
  96. };
  97. extern const struct rpc_program nfslocalio_program;
  98. static struct rpc_stat nfslocalio_rpcstat = { &nfslocalio_program };
  99. const struct rpc_program nfslocalio_program = {
  100. .name = "nfslocalio",
  101. .number = NFS_LOCALIO_PROGRAM,
  102. .nrvers = ARRAY_SIZE(nfslocalio_version),
  103. .version = nfslocalio_version,
  104. .stats = &nfslocalio_rpcstat,
  105. };
  106. /*
  107. * nfs_local_enable - enable local i/o for an nfs_client
  108. */
  109. static void nfs_local_enable(struct nfs_client *clp)
  110. {
  111. spin_lock(&clp->cl_localio_lock);
  112. set_bit(NFS_CS_LOCAL_IO, &clp->cl_flags);
  113. trace_nfs_local_enable(clp);
  114. spin_unlock(&clp->cl_localio_lock);
  115. }
  116. /*
  117. * nfs_local_disable - disable local i/o for an nfs_client
  118. */
  119. void nfs_local_disable(struct nfs_client *clp)
  120. {
  121. spin_lock(&clp->cl_localio_lock);
  122. if (test_and_clear_bit(NFS_CS_LOCAL_IO, &clp->cl_flags)) {
  123. trace_nfs_local_disable(clp);
  124. nfs_uuid_invalidate_one_client(&clp->cl_uuid);
  125. }
  126. spin_unlock(&clp->cl_localio_lock);
  127. }
  128. /*
  129. * nfs_init_localioclient - Initialise an NFS localio client connection
  130. */
  131. static struct rpc_clnt *nfs_init_localioclient(struct nfs_client *clp)
  132. {
  133. struct rpc_clnt *rpcclient_localio;
  134. rpcclient_localio = rpc_bind_new_program(clp->cl_rpcclient,
  135. &nfslocalio_program, 1);
  136. dprintk_rcu("%s: server (%s) %s NFS LOCALIO.\n",
  137. __func__, rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
  138. (IS_ERR(rpcclient_localio) ? "does not support" : "supports"));
  139. return rpcclient_localio;
  140. }
  141. static bool nfs_server_uuid_is_local(struct nfs_client *clp)
  142. {
  143. u8 uuid[UUID_SIZE];
  144. struct rpc_message msg = {
  145. .rpc_argp = &uuid,
  146. };
  147. struct rpc_clnt *rpcclient_localio;
  148. int status;
  149. rpcclient_localio = nfs_init_localioclient(clp);
  150. if (IS_ERR(rpcclient_localio))
  151. return false;
  152. export_uuid(uuid, &clp->cl_uuid.uuid);
  153. msg.rpc_proc = &nfs_localio_procedures[LOCALIOPROC_UUID_IS_LOCAL];
  154. status = rpc_call_sync(rpcclient_localio, &msg, 0);
  155. dprintk("%s: NFS reply UUID_IS_LOCAL: status=%d\n",
  156. __func__, status);
  157. rpc_shutdown_client(rpcclient_localio);
  158. /* Server is only local if it initialized required struct members */
  159. if (status || !clp->cl_uuid.net || !clp->cl_uuid.dom)
  160. return false;
  161. return true;
  162. }
  163. /*
  164. * nfs_local_probe - probe local i/o support for an nfs_server and nfs_client
  165. * - called after alloc_client and init_client (so cl_rpcclient exists)
  166. * - this function is idempotent, it can be called for old or new clients
  167. */
  168. void nfs_local_probe(struct nfs_client *clp)
  169. {
  170. /* Disallow localio if disabled via sysfs or AUTH_SYS isn't used */
  171. if (!localio_enabled ||
  172. clp->cl_rpcclient->cl_auth->au_flavor != RPC_AUTH_UNIX) {
  173. nfs_local_disable(clp);
  174. return;
  175. }
  176. if (nfs_client_is_local(clp)) {
  177. /* If already enabled, disable and re-enable */
  178. nfs_local_disable(clp);
  179. }
  180. if (!nfs_uuid_begin(&clp->cl_uuid))
  181. return;
  182. if (nfs_server_uuid_is_local(clp))
  183. nfs_local_enable(clp);
  184. nfs_uuid_end(&clp->cl_uuid);
  185. }
  186. EXPORT_SYMBOL_GPL(nfs_local_probe);
  187. /*
  188. * nfs_local_open_fh - open a local filehandle in terms of nfsd_file
  189. *
  190. * Returns a pointer to a struct nfsd_file or NULL
  191. */
  192. struct nfsd_file *
  193. nfs_local_open_fh(struct nfs_client *clp, const struct cred *cred,
  194. struct nfs_fh *fh, const fmode_t mode)
  195. {
  196. struct nfsd_file *localio;
  197. int status;
  198. if (!nfs_server_is_local(clp))
  199. return NULL;
  200. if (mode & ~(FMODE_READ | FMODE_WRITE))
  201. return NULL;
  202. localio = nfs_open_local_fh(&clp->cl_uuid, clp->cl_rpcclient,
  203. cred, fh, mode);
  204. if (IS_ERR(localio)) {
  205. status = PTR_ERR(localio);
  206. trace_nfs_local_open_fh(fh, mode, status);
  207. switch (status) {
  208. case -ENOMEM:
  209. case -ENXIO:
  210. case -ENOENT:
  211. /* Revalidate localio, will disable if unsupported */
  212. nfs_local_probe(clp);
  213. }
  214. return NULL;
  215. }
  216. return localio;
  217. }
  218. EXPORT_SYMBOL_GPL(nfs_local_open_fh);
  219. static struct bio_vec *
  220. nfs_bvec_alloc_and_import_pagevec(struct page **pagevec,
  221. unsigned int npages, gfp_t flags)
  222. {
  223. struct bio_vec *bvec, *p;
  224. bvec = kmalloc_array(npages, sizeof(*bvec), flags);
  225. if (bvec != NULL) {
  226. for (p = bvec; npages > 0; p++, pagevec++, npages--) {
  227. p->bv_page = *pagevec;
  228. p->bv_len = PAGE_SIZE;
  229. p->bv_offset = 0;
  230. }
  231. }
  232. return bvec;
  233. }
  234. static void
  235. nfs_local_iocb_free(struct nfs_local_kiocb *iocb)
  236. {
  237. kfree(iocb->bvec);
  238. kfree(iocb);
  239. }
  240. static struct nfs_local_kiocb *
  241. nfs_local_iocb_alloc(struct nfs_pgio_header *hdr,
  242. struct file *file, gfp_t flags)
  243. {
  244. struct nfs_local_kiocb *iocb;
  245. iocb = kmalloc(sizeof(*iocb), flags);
  246. if (iocb == NULL)
  247. return NULL;
  248. iocb->bvec = nfs_bvec_alloc_and_import_pagevec(hdr->page_array.pagevec,
  249. hdr->page_array.npages, flags);
  250. if (iocb->bvec == NULL) {
  251. kfree(iocb);
  252. return NULL;
  253. }
  254. if (localio_O_DIRECT_semantics &&
  255. test_bit(NFS_IOHDR_ODIRECT, &hdr->flags)) {
  256. iocb->kiocb.ki_filp = file;
  257. iocb->kiocb.ki_flags = IOCB_DIRECT;
  258. } else
  259. init_sync_kiocb(&iocb->kiocb, file);
  260. iocb->kiocb.ki_pos = hdr->args.offset;
  261. iocb->hdr = hdr;
  262. iocb->kiocb.ki_flags &= ~IOCB_APPEND;
  263. iocb->aio_complete_work = NULL;
  264. return iocb;
  265. }
  266. static void
  267. nfs_local_iter_init(struct iov_iter *i, struct nfs_local_kiocb *iocb, int dir)
  268. {
  269. struct nfs_pgio_header *hdr = iocb->hdr;
  270. iov_iter_bvec(i, dir, iocb->bvec, hdr->page_array.npages,
  271. hdr->args.count + hdr->args.pgbase);
  272. if (hdr->args.pgbase != 0)
  273. iov_iter_advance(i, hdr->args.pgbase);
  274. }
  275. static void
  276. nfs_local_hdr_release(struct nfs_pgio_header *hdr,
  277. const struct rpc_call_ops *call_ops)
  278. {
  279. call_ops->rpc_call_done(&hdr->task, hdr);
  280. call_ops->rpc_release(hdr);
  281. }
  282. static void
  283. nfs_local_pgio_init(struct nfs_pgio_header *hdr,
  284. const struct rpc_call_ops *call_ops)
  285. {
  286. hdr->task.tk_ops = call_ops;
  287. if (!hdr->task.tk_start)
  288. hdr->task.tk_start = ktime_get();
  289. }
  290. static void
  291. nfs_local_pgio_done(struct nfs_pgio_header *hdr, long status)
  292. {
  293. if (status >= 0) {
  294. hdr->res.count = status;
  295. hdr->res.op_status = NFS4_OK;
  296. hdr->task.tk_status = 0;
  297. } else {
  298. hdr->res.op_status = nfs_localio_errno_to_nfs4_stat(status);
  299. hdr->task.tk_status = status;
  300. }
  301. }
  302. static void
  303. nfs_local_pgio_release(struct nfs_local_kiocb *iocb)
  304. {
  305. struct nfs_pgio_header *hdr = iocb->hdr;
  306. nfs_to_nfsd_file_put_local(iocb->localio);
  307. nfs_local_iocb_free(iocb);
  308. nfs_local_hdr_release(hdr, hdr->task.tk_ops);
  309. }
  310. /*
  311. * Complete the I/O from iocb->kiocb.ki_complete()
  312. *
  313. * Note that this function can be called from a bottom half context,
  314. * hence we need to queue the rpc_call_done() etc to a workqueue
  315. */
  316. static inline void nfs_local_pgio_aio_complete(struct nfs_local_kiocb *iocb)
  317. {
  318. INIT_WORK(&iocb->work, iocb->aio_complete_work);
  319. queue_work(nfsiod_workqueue, &iocb->work);
  320. }
  321. static void
  322. nfs_local_read_done(struct nfs_local_kiocb *iocb, long status)
  323. {
  324. struct nfs_pgio_header *hdr = iocb->hdr;
  325. struct file *filp = iocb->kiocb.ki_filp;
  326. nfs_local_pgio_done(hdr, status);
  327. /*
  328. * Must clear replen otherwise NFSv3 data corruption will occur
  329. * if/when switching from LOCALIO back to using normal RPC.
  330. */
  331. hdr->res.replen = 0;
  332. if (hdr->res.count != hdr->args.count ||
  333. hdr->args.offset + hdr->res.count >= i_size_read(file_inode(filp)))
  334. hdr->res.eof = true;
  335. dprintk("%s: read %ld bytes eof %d.\n", __func__,
  336. status > 0 ? status : 0, hdr->res.eof);
  337. }
  338. static void nfs_local_read_aio_complete_work(struct work_struct *work)
  339. {
  340. struct nfs_local_kiocb *iocb =
  341. container_of(work, struct nfs_local_kiocb, work);
  342. nfs_local_pgio_release(iocb);
  343. }
  344. static void nfs_local_read_aio_complete(struct kiocb *kiocb, long ret)
  345. {
  346. struct nfs_local_kiocb *iocb =
  347. container_of(kiocb, struct nfs_local_kiocb, kiocb);
  348. nfs_local_read_done(iocb, ret);
  349. nfs_local_pgio_aio_complete(iocb); /* Calls nfs_local_read_aio_complete_work */
  350. }
  351. static void nfs_local_call_read(struct work_struct *work)
  352. {
  353. struct nfs_local_kiocb *iocb =
  354. container_of(work, struct nfs_local_kiocb, work);
  355. struct file *filp = iocb->kiocb.ki_filp;
  356. const struct cred *save_cred;
  357. struct iov_iter iter;
  358. ssize_t status;
  359. save_cred = override_creds(filp->f_cred);
  360. nfs_local_iter_init(&iter, iocb, READ);
  361. status = filp->f_op->read_iter(&iocb->kiocb, &iter);
  362. revert_creds(save_cred);
  363. if (status != -EIOCBQUEUED) {
  364. nfs_local_read_done(iocb, status);
  365. nfs_local_pgio_release(iocb);
  366. }
  367. }
  368. static int
  369. nfs_do_local_read(struct nfs_pgio_header *hdr,
  370. struct nfsd_file *localio,
  371. const struct rpc_call_ops *call_ops)
  372. {
  373. struct nfs_local_kiocb *iocb;
  374. struct file *file = nfs_to->nfsd_file_file(localio);
  375. /* Don't support filesystems without read_iter */
  376. if (!file->f_op->read_iter)
  377. return -EAGAIN;
  378. dprintk("%s: vfs_read count=%u pos=%llu\n",
  379. __func__, hdr->args.count, hdr->args.offset);
  380. iocb = nfs_local_iocb_alloc(hdr, file, GFP_KERNEL);
  381. if (iocb == NULL)
  382. return -ENOMEM;
  383. iocb->localio = localio;
  384. nfs_local_pgio_init(hdr, call_ops);
  385. hdr->res.eof = false;
  386. if (iocb->kiocb.ki_flags & IOCB_DIRECT) {
  387. iocb->kiocb.ki_complete = nfs_local_read_aio_complete;
  388. iocb->aio_complete_work = nfs_local_read_aio_complete_work;
  389. }
  390. INIT_WORK(&iocb->work, nfs_local_call_read);
  391. queue_work(nfslocaliod_workqueue, &iocb->work);
  392. return 0;
  393. }
  394. static void
  395. nfs_copy_boot_verifier(struct nfs_write_verifier *verifier, struct inode *inode)
  396. {
  397. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  398. u32 *verf = (u32 *)verifier->data;
  399. int seq = 0;
  400. do {
  401. read_seqbegin_or_lock(&clp->cl_boot_lock, &seq);
  402. verf[0] = (u32)clp->cl_nfssvc_boot.tv_sec;
  403. verf[1] = (u32)clp->cl_nfssvc_boot.tv_nsec;
  404. } while (need_seqretry(&clp->cl_boot_lock, seq));
  405. done_seqretry(&clp->cl_boot_lock, seq);
  406. }
  407. static void
  408. nfs_reset_boot_verifier(struct inode *inode)
  409. {
  410. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  411. write_seqlock(&clp->cl_boot_lock);
  412. ktime_get_real_ts64(&clp->cl_nfssvc_boot);
  413. write_sequnlock(&clp->cl_boot_lock);
  414. }
  415. static void
  416. nfs_set_local_verifier(struct inode *inode,
  417. struct nfs_writeverf *verf,
  418. enum nfs3_stable_how how)
  419. {
  420. nfs_copy_boot_verifier(&verf->verifier, inode);
  421. verf->committed = how;
  422. }
  423. /* Factored out from fs/nfsd/vfs.h:fh_getattr() */
  424. static int __vfs_getattr(struct path *p, struct kstat *stat, int version)
  425. {
  426. u32 request_mask = STATX_BASIC_STATS;
  427. if (version == 4)
  428. request_mask |= (STATX_BTIME | STATX_CHANGE_COOKIE);
  429. return vfs_getattr(p, stat, request_mask, AT_STATX_SYNC_AS_STAT);
  430. }
  431. /* Copied from fs/nfsd/nfsfh.c:nfsd4_change_attribute() */
  432. static u64 __nfsd4_change_attribute(const struct kstat *stat,
  433. const struct inode *inode)
  434. {
  435. u64 chattr;
  436. if (stat->result_mask & STATX_CHANGE_COOKIE) {
  437. chattr = stat->change_cookie;
  438. if (S_ISREG(inode->i_mode) &&
  439. !(stat->attributes & STATX_ATTR_CHANGE_MONOTONIC)) {
  440. chattr += (u64)stat->ctime.tv_sec << 30;
  441. chattr += stat->ctime.tv_nsec;
  442. }
  443. } else {
  444. chattr = time_to_chattr(&stat->ctime);
  445. }
  446. return chattr;
  447. }
  448. static void nfs_local_vfs_getattr(struct nfs_local_kiocb *iocb)
  449. {
  450. struct kstat stat;
  451. struct file *filp = iocb->kiocb.ki_filp;
  452. struct nfs_pgio_header *hdr = iocb->hdr;
  453. struct nfs_fattr *fattr = hdr->res.fattr;
  454. int version = NFS_PROTO(hdr->inode)->version;
  455. if (unlikely(!fattr) || __vfs_getattr(&filp->f_path, &stat, version))
  456. return;
  457. fattr->valid = (NFS_ATTR_FATTR_FILEID |
  458. NFS_ATTR_FATTR_CHANGE |
  459. NFS_ATTR_FATTR_SIZE |
  460. NFS_ATTR_FATTR_ATIME |
  461. NFS_ATTR_FATTR_MTIME |
  462. NFS_ATTR_FATTR_CTIME |
  463. NFS_ATTR_FATTR_SPACE_USED);
  464. fattr->fileid = stat.ino;
  465. fattr->size = stat.size;
  466. fattr->atime = stat.atime;
  467. fattr->mtime = stat.mtime;
  468. fattr->ctime = stat.ctime;
  469. if (version == 4) {
  470. fattr->change_attr =
  471. __nfsd4_change_attribute(&stat, file_inode(filp));
  472. } else
  473. fattr->change_attr = nfs_timespec_to_change_attr(&fattr->ctime);
  474. fattr->du.nfs3.used = stat.blocks << 9;
  475. }
  476. static void
  477. nfs_local_write_done(struct nfs_local_kiocb *iocb, long status)
  478. {
  479. struct nfs_pgio_header *hdr = iocb->hdr;
  480. struct inode *inode = hdr->inode;
  481. dprintk("%s: wrote %ld bytes.\n", __func__, status > 0 ? status : 0);
  482. /* Handle short writes as if they are ENOSPC */
  483. if (status > 0 && status < hdr->args.count) {
  484. hdr->mds_offset += status;
  485. hdr->args.offset += status;
  486. hdr->args.pgbase += status;
  487. hdr->args.count -= status;
  488. nfs_set_pgio_error(hdr, -ENOSPC, hdr->args.offset);
  489. status = -ENOSPC;
  490. }
  491. if (status < 0)
  492. nfs_reset_boot_verifier(inode);
  493. else if (nfs_should_remove_suid(inode)) {
  494. /* Deal with the suid/sgid bit corner case */
  495. spin_lock(&inode->i_lock);
  496. nfs_set_cache_invalid(inode, NFS_INO_INVALID_MODE);
  497. spin_unlock(&inode->i_lock);
  498. }
  499. nfs_local_pgio_done(hdr, status);
  500. }
  501. static void nfs_local_write_aio_complete_work(struct work_struct *work)
  502. {
  503. struct nfs_local_kiocb *iocb =
  504. container_of(work, struct nfs_local_kiocb, work);
  505. nfs_local_vfs_getattr(iocb);
  506. nfs_local_pgio_release(iocb);
  507. }
  508. static void nfs_local_write_aio_complete(struct kiocb *kiocb, long ret)
  509. {
  510. struct nfs_local_kiocb *iocb =
  511. container_of(kiocb, struct nfs_local_kiocb, kiocb);
  512. nfs_local_write_done(iocb, ret);
  513. nfs_local_pgio_aio_complete(iocb); /* Calls nfs_local_write_aio_complete_work */
  514. }
  515. static void nfs_local_call_write(struct work_struct *work)
  516. {
  517. struct nfs_local_kiocb *iocb =
  518. container_of(work, struct nfs_local_kiocb, work);
  519. struct file *filp = iocb->kiocb.ki_filp;
  520. unsigned long old_flags = current->flags;
  521. const struct cred *save_cred;
  522. struct iov_iter iter;
  523. ssize_t status;
  524. current->flags |= PF_LOCAL_THROTTLE | PF_MEMALLOC_NOIO;
  525. save_cred = override_creds(filp->f_cred);
  526. nfs_local_iter_init(&iter, iocb, WRITE);
  527. file_start_write(filp);
  528. status = filp->f_op->write_iter(&iocb->kiocb, &iter);
  529. file_end_write(filp);
  530. revert_creds(save_cred);
  531. current->flags = old_flags;
  532. if (status != -EIOCBQUEUED) {
  533. nfs_local_write_done(iocb, status);
  534. nfs_local_vfs_getattr(iocb);
  535. nfs_local_pgio_release(iocb);
  536. }
  537. }
  538. static int
  539. nfs_do_local_write(struct nfs_pgio_header *hdr,
  540. struct nfsd_file *localio,
  541. const struct rpc_call_ops *call_ops)
  542. {
  543. struct nfs_local_kiocb *iocb;
  544. struct file *file = nfs_to->nfsd_file_file(localio);
  545. /* Don't support filesystems without write_iter */
  546. if (!file->f_op->write_iter)
  547. return -EAGAIN;
  548. dprintk("%s: vfs_write count=%u pos=%llu %s\n",
  549. __func__, hdr->args.count, hdr->args.offset,
  550. (hdr->args.stable == NFS_UNSTABLE) ? "unstable" : "stable");
  551. iocb = nfs_local_iocb_alloc(hdr, file, GFP_NOIO);
  552. if (iocb == NULL)
  553. return -ENOMEM;
  554. iocb->localio = localio;
  555. switch (hdr->args.stable) {
  556. default:
  557. break;
  558. case NFS_DATA_SYNC:
  559. iocb->kiocb.ki_flags |= IOCB_DSYNC;
  560. break;
  561. case NFS_FILE_SYNC:
  562. iocb->kiocb.ki_flags |= IOCB_DSYNC|IOCB_SYNC;
  563. }
  564. nfs_local_pgio_init(hdr, call_ops);
  565. nfs_set_local_verifier(hdr->inode, hdr->res.verf, hdr->args.stable);
  566. if (iocb->kiocb.ki_flags & IOCB_DIRECT) {
  567. iocb->kiocb.ki_complete = nfs_local_write_aio_complete;
  568. iocb->aio_complete_work = nfs_local_write_aio_complete_work;
  569. }
  570. INIT_WORK(&iocb->work, nfs_local_call_write);
  571. queue_work(nfslocaliod_workqueue, &iocb->work);
  572. return 0;
  573. }
  574. int nfs_local_doio(struct nfs_client *clp, struct nfsd_file *localio,
  575. struct nfs_pgio_header *hdr,
  576. const struct rpc_call_ops *call_ops)
  577. {
  578. int status = 0;
  579. if (!hdr->args.count)
  580. return 0;
  581. switch (hdr->rw_mode) {
  582. case FMODE_READ:
  583. status = nfs_do_local_read(hdr, localio, call_ops);
  584. break;
  585. case FMODE_WRITE:
  586. status = nfs_do_local_write(hdr, localio, call_ops);
  587. break;
  588. default:
  589. dprintk("%s: invalid mode: %d\n", __func__,
  590. hdr->rw_mode);
  591. status = -EINVAL;
  592. }
  593. if (status != 0) {
  594. if (status == -EAGAIN)
  595. nfs_local_disable(clp);
  596. nfs_to_nfsd_file_put_local(localio);
  597. hdr->task.tk_status = status;
  598. nfs_local_hdr_release(hdr, call_ops);
  599. }
  600. return status;
  601. }
  602. static void
  603. nfs_local_init_commit(struct nfs_commit_data *data,
  604. const struct rpc_call_ops *call_ops)
  605. {
  606. data->task.tk_ops = call_ops;
  607. }
  608. static int
  609. nfs_local_run_commit(struct file *filp, struct nfs_commit_data *data)
  610. {
  611. loff_t start = data->args.offset;
  612. loff_t end = LLONG_MAX;
  613. if (data->args.count > 0) {
  614. end = start + data->args.count - 1;
  615. if (end < start)
  616. end = LLONG_MAX;
  617. }
  618. dprintk("%s: commit %llu - %llu\n", __func__, start, end);
  619. return vfs_fsync_range(filp, start, end, 0);
  620. }
  621. static void
  622. nfs_local_commit_done(struct nfs_commit_data *data, int status)
  623. {
  624. if (status >= 0) {
  625. nfs_set_local_verifier(data->inode,
  626. data->res.verf,
  627. NFS_FILE_SYNC);
  628. data->res.op_status = NFS4_OK;
  629. data->task.tk_status = 0;
  630. } else {
  631. nfs_reset_boot_verifier(data->inode);
  632. data->res.op_status = nfs_localio_errno_to_nfs4_stat(status);
  633. data->task.tk_status = status;
  634. }
  635. }
  636. static void
  637. nfs_local_release_commit_data(struct nfsd_file *localio,
  638. struct nfs_commit_data *data,
  639. const struct rpc_call_ops *call_ops)
  640. {
  641. nfs_to_nfsd_file_put_local(localio);
  642. call_ops->rpc_call_done(&data->task, data);
  643. call_ops->rpc_release(data);
  644. }
  645. static struct nfs_local_fsync_ctx *
  646. nfs_local_fsync_ctx_alloc(struct nfs_commit_data *data,
  647. struct nfsd_file *localio, gfp_t flags)
  648. {
  649. struct nfs_local_fsync_ctx *ctx = kmalloc(sizeof(*ctx), flags);
  650. if (ctx != NULL) {
  651. ctx->localio = localio;
  652. ctx->data = data;
  653. INIT_WORK(&ctx->work, nfs_local_fsync_work);
  654. kref_init(&ctx->kref);
  655. ctx->done = NULL;
  656. }
  657. return ctx;
  658. }
  659. static void
  660. nfs_local_fsync_ctx_kref_free(struct kref *kref)
  661. {
  662. kfree(container_of(kref, struct nfs_local_fsync_ctx, kref));
  663. }
  664. static void
  665. nfs_local_fsync_ctx_put(struct nfs_local_fsync_ctx *ctx)
  666. {
  667. kref_put(&ctx->kref, nfs_local_fsync_ctx_kref_free);
  668. }
  669. static void
  670. nfs_local_fsync_ctx_free(struct nfs_local_fsync_ctx *ctx)
  671. {
  672. nfs_local_release_commit_data(ctx->localio, ctx->data,
  673. ctx->data->task.tk_ops);
  674. nfs_local_fsync_ctx_put(ctx);
  675. }
  676. static void
  677. nfs_local_fsync_work(struct work_struct *work)
  678. {
  679. struct nfs_local_fsync_ctx *ctx;
  680. int status;
  681. ctx = container_of(work, struct nfs_local_fsync_ctx, work);
  682. status = nfs_local_run_commit(nfs_to->nfsd_file_file(ctx->localio),
  683. ctx->data);
  684. nfs_local_commit_done(ctx->data, status);
  685. if (ctx->done != NULL)
  686. complete(ctx->done);
  687. nfs_local_fsync_ctx_free(ctx);
  688. }
  689. int nfs_local_commit(struct nfsd_file *localio,
  690. struct nfs_commit_data *data,
  691. const struct rpc_call_ops *call_ops, int how)
  692. {
  693. struct nfs_local_fsync_ctx *ctx;
  694. ctx = nfs_local_fsync_ctx_alloc(data, localio, GFP_KERNEL);
  695. if (!ctx) {
  696. nfs_local_commit_done(data, -ENOMEM);
  697. nfs_local_release_commit_data(localio, data, call_ops);
  698. return -ENOMEM;
  699. }
  700. nfs_local_init_commit(data, call_ops);
  701. kref_get(&ctx->kref);
  702. if (how & FLUSH_SYNC) {
  703. DECLARE_COMPLETION_ONSTACK(done);
  704. ctx->done = &done;
  705. queue_work(nfsiod_workqueue, &ctx->work);
  706. wait_for_completion(&done);
  707. } else
  708. queue_work(nfsiod_workqueue, &ctx->work);
  709. nfs_local_fsync_ctx_put(ctx);
  710. return 0;
  711. }