direct.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. /*
  2. * linux/fs/nfs/direct.c
  3. *
  4. * Copyright (C) 2003 by Chuck Lever <cel@netapp.com>
  5. *
  6. * High-performance uncached I/O for the Linux NFS client
  7. *
  8. * There are important applications whose performance or correctness
  9. * depends on uncached access to file data. Database clusters
  10. * (multiple copies of the same instance running on separate hosts)
  11. * implement their own cache coherency protocol that subsumes file
  12. * system cache protocols. Applications that process datasets
  13. * considerably larger than the client's memory do not always benefit
  14. * from a local cache. A streaming video server, for instance, has no
  15. * need to cache the contents of a file.
  16. *
  17. * When an application requests uncached I/O, all read and write requests
  18. * are made directly to the server; data stored or fetched via these
  19. * requests is not cached in the Linux page cache. The client does not
  20. * correct unaligned requests from applications. All requested bytes are
  21. * held on permanent storage before a direct write system call returns to
  22. * an application.
  23. *
  24. * Solaris implements an uncached I/O facility called directio() that
  25. * is used for backups and sequential I/O to very large files. Solaris
  26. * also supports uncaching whole NFS partitions with "-o forcedirectio,"
  27. * an undocumented mount option.
  28. *
  29. * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with
  30. * help from Andrew Morton.
  31. *
  32. * 18 Dec 2001 Initial implementation for 2.4 --cel
  33. * 08 Jul 2002 Version for 2.4.19, with bug fixes --trondmy
  34. * 08 Jun 2003 Port to 2.5 APIs --cel
  35. * 31 Mar 2004 Handle direct I/O without VFS support --cel
  36. * 15 Sep 2004 Parallel async reads --cel
  37. * 04 May 2005 support O_DIRECT with aio --cel
  38. *
  39. */
  40. #include <linux/errno.h>
  41. #include <linux/sched.h>
  42. #include <linux/kernel.h>
  43. #include <linux/file.h>
  44. #include <linux/pagemap.h>
  45. #include <linux/kref.h>
  46. #include <linux/nfs_fs.h>
  47. #include <linux/nfs_page.h>
  48. #include <linux/sunrpc/clnt.h>
  49. #include <asm/system.h>
  50. #include <asm/uaccess.h>
  51. #include <asm/atomic.h>
  52. #include "internal.h"
  53. #include "iostat.h"
  54. #define NFSDBG_FACILITY NFSDBG_VFS
  55. static struct kmem_cache *nfs_direct_cachep;
  56. /*
  57. * This represents a set of asynchronous requests that we're waiting on
  58. */
  59. struct nfs_direct_req {
  60. struct kref kref; /* release manager */
  61. /* I/O parameters */
  62. struct nfs_open_context *ctx; /* file open context info */
  63. struct kiocb * iocb; /* controlling i/o request */
  64. struct inode * inode; /* target file of i/o */
  65. /* completion state */
  66. atomic_t io_count; /* i/os we're waiting for */
  67. spinlock_t lock; /* protect completion state */
  68. ssize_t count, /* bytes actually processed */
  69. error; /* any reported error */
  70. struct completion completion; /* wait for i/o completion */
  71. /* commit state */
  72. struct list_head rewrite_list; /* saved nfs_write_data structs */
  73. struct nfs_write_data * commit_data; /* special write_data for commits */
  74. int flags;
  75. #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */
  76. #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */
  77. struct nfs_writeverf verf; /* unstable write verifier */
  78. };
  79. static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
  80. static const struct rpc_call_ops nfs_write_direct_ops;
  81. static inline void get_dreq(struct nfs_direct_req *dreq)
  82. {
  83. atomic_inc(&dreq->io_count);
  84. }
  85. static inline int put_dreq(struct nfs_direct_req *dreq)
  86. {
  87. return atomic_dec_and_test(&dreq->io_count);
  88. }
  89. /**
  90. * nfs_direct_IO - NFS address space operation for direct I/O
  91. * @rw: direction (read or write)
  92. * @iocb: target I/O control block
  93. * @iov: array of vectors that define I/O buffer
  94. * @pos: offset in file to begin the operation
  95. * @nr_segs: size of iovec array
  96. *
  97. * The presence of this routine in the address space ops vector means
  98. * the NFS client supports direct I/O. However, we shunt off direct
  99. * read and write requests before the VFS gets them, so this method
  100. * should never be called.
  101. */
  102. ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
  103. {
  104. dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
  105. iocb->ki_filp->f_path.dentry->d_name.name,
  106. (long long) pos, nr_segs);
  107. return -EINVAL;
  108. }
  109. static void nfs_direct_dirty_pages(struct page **pages, unsigned int pgbase, size_t count)
  110. {
  111. unsigned int npages;
  112. unsigned int i;
  113. if (count == 0)
  114. return;
  115. pages += (pgbase >> PAGE_SHIFT);
  116. npages = (count + (pgbase & ~PAGE_MASK) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  117. for (i = 0; i < npages; i++) {
  118. struct page *page = pages[i];
  119. if (!PageCompound(page))
  120. set_page_dirty(page);
  121. }
  122. }
  123. static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
  124. {
  125. unsigned int i;
  126. for (i = 0; i < npages; i++)
  127. page_cache_release(pages[i]);
  128. }
  129. static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
  130. {
  131. struct nfs_direct_req *dreq;
  132. dreq = kmem_cache_alloc(nfs_direct_cachep, GFP_KERNEL);
  133. if (!dreq)
  134. return NULL;
  135. kref_init(&dreq->kref);
  136. kref_get(&dreq->kref);
  137. init_completion(&dreq->completion);
  138. INIT_LIST_HEAD(&dreq->rewrite_list);
  139. dreq->iocb = NULL;
  140. dreq->ctx = NULL;
  141. spin_lock_init(&dreq->lock);
  142. atomic_set(&dreq->io_count, 0);
  143. dreq->count = 0;
  144. dreq->error = 0;
  145. dreq->flags = 0;
  146. return dreq;
  147. }
  148. static void nfs_direct_req_free(struct kref *kref)
  149. {
  150. struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
  151. if (dreq->ctx != NULL)
  152. put_nfs_open_context(dreq->ctx);
  153. kmem_cache_free(nfs_direct_cachep, dreq);
  154. }
  155. static void nfs_direct_req_release(struct nfs_direct_req *dreq)
  156. {
  157. kref_put(&dreq->kref, nfs_direct_req_free);
  158. }
  159. /*
  160. * Collects and returns the final error value/byte-count.
  161. */
  162. static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
  163. {
  164. ssize_t result = -EIOCBQUEUED;
  165. /* Async requests don't wait here */
  166. if (dreq->iocb)
  167. goto out;
  168. result = wait_for_completion_interruptible(&dreq->completion);
  169. if (!result)
  170. result = dreq->error;
  171. if (!result)
  172. result = dreq->count;
  173. out:
  174. return (ssize_t) result;
  175. }
  176. /*
  177. * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust
  178. * the iocb is still valid here if this is a synchronous request.
  179. */
  180. static void nfs_direct_complete(struct nfs_direct_req *dreq)
  181. {
  182. if (dreq->iocb) {
  183. long res = (long) dreq->error;
  184. if (!res)
  185. res = (long) dreq->count;
  186. aio_complete(dreq->iocb, res, 0);
  187. }
  188. complete_all(&dreq->completion);
  189. nfs_direct_req_release(dreq);
  190. }
  191. /*
  192. * We must hold a reference to all the pages in this direct read request
  193. * until the RPCs complete. This could be long *after* we are woken up in
  194. * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
  195. */
  196. static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
  197. {
  198. struct nfs_read_data *data = calldata;
  199. struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
  200. if (nfs_readpage_result(task, data) != 0)
  201. return;
  202. spin_lock(&dreq->lock);
  203. if (unlikely(task->tk_status < 0)) {
  204. dreq->error = task->tk_status;
  205. spin_unlock(&dreq->lock);
  206. } else {
  207. dreq->count += data->res.count;
  208. spin_unlock(&dreq->lock);
  209. nfs_direct_dirty_pages(data->pagevec,
  210. data->args.pgbase,
  211. data->res.count);
  212. }
  213. nfs_direct_release_pages(data->pagevec, data->npages);
  214. if (put_dreq(dreq))
  215. nfs_direct_complete(dreq);
  216. }
  217. static const struct rpc_call_ops nfs_read_direct_ops = {
  218. .rpc_call_done = nfs_direct_read_result,
  219. .rpc_release = nfs_readdata_release,
  220. };
  221. /*
  222. * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
  223. * operation. If nfs_readdata_alloc() or get_user_pages() fails,
  224. * bail and stop sending more reads. Read length accounting is
  225. * handled automatically by nfs_direct_read_result(). Otherwise, if
  226. * no requests have been sent, just return an error.
  227. */
  228. static ssize_t nfs_direct_read_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t pos)
  229. {
  230. struct nfs_open_context *ctx = dreq->ctx;
  231. struct inode *inode = ctx->path.dentry->d_inode;
  232. size_t rsize = NFS_SERVER(inode)->rsize;
  233. unsigned int pgbase;
  234. int result;
  235. ssize_t started = 0;
  236. get_dreq(dreq);
  237. do {
  238. struct nfs_read_data *data;
  239. size_t bytes;
  240. pgbase = user_addr & ~PAGE_MASK;
  241. bytes = min(rsize,count);
  242. result = -ENOMEM;
  243. data = nfs_readdata_alloc(nfs_page_array_len(pgbase, bytes));
  244. if (unlikely(!data))
  245. break;
  246. down_read(&current->mm->mmap_sem);
  247. result = get_user_pages(current, current->mm, user_addr,
  248. data->npages, 1, 0, data->pagevec, NULL);
  249. up_read(&current->mm->mmap_sem);
  250. if (result < 0) {
  251. nfs_readdata_release(data);
  252. break;
  253. }
  254. if ((unsigned)result < data->npages) {
  255. bytes = result * PAGE_SIZE;
  256. if (bytes <= pgbase) {
  257. nfs_direct_release_pages(data->pagevec, result);
  258. nfs_readdata_release(data);
  259. break;
  260. }
  261. bytes -= pgbase;
  262. data->npages = result;
  263. }
  264. get_dreq(dreq);
  265. data->req = (struct nfs_page *) dreq;
  266. data->inode = inode;
  267. data->cred = ctx->cred;
  268. data->args.fh = NFS_FH(inode);
  269. data->args.context = ctx;
  270. data->args.offset = pos;
  271. data->args.pgbase = pgbase;
  272. data->args.pages = data->pagevec;
  273. data->args.count = bytes;
  274. data->res.fattr = &data->fattr;
  275. data->res.eof = 0;
  276. data->res.count = bytes;
  277. rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
  278. &nfs_read_direct_ops, data);
  279. NFS_PROTO(inode)->read_setup(data);
  280. data->task.tk_cookie = (unsigned long) inode;
  281. rpc_execute(&data->task);
  282. dprintk("NFS: %5u initiated direct read call "
  283. "(req %s/%Ld, %zu bytes @ offset %Lu)\n",
  284. data->task.tk_pid,
  285. inode->i_sb->s_id,
  286. (long long)NFS_FILEID(inode),
  287. bytes,
  288. (unsigned long long)data->args.offset);
  289. started += bytes;
  290. user_addr += bytes;
  291. pos += bytes;
  292. /* FIXME: Remove this unnecessary math from final patch */
  293. pgbase += bytes;
  294. pgbase &= ~PAGE_MASK;
  295. BUG_ON(pgbase != (user_addr & ~PAGE_MASK));
  296. count -= bytes;
  297. } while (count != 0);
  298. if (put_dreq(dreq))
  299. nfs_direct_complete(dreq);
  300. if (started)
  301. return 0;
  302. return result < 0 ? (ssize_t) result : -EFAULT;
  303. }
  304. static ssize_t nfs_direct_read(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos)
  305. {
  306. ssize_t result = 0;
  307. sigset_t oldset;
  308. struct inode *inode = iocb->ki_filp->f_mapping->host;
  309. struct rpc_clnt *clnt = NFS_CLIENT(inode);
  310. struct nfs_direct_req *dreq;
  311. dreq = nfs_direct_req_alloc();
  312. if (!dreq)
  313. return -ENOMEM;
  314. dreq->inode = inode;
  315. dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
  316. if (!is_sync_kiocb(iocb))
  317. dreq->iocb = iocb;
  318. nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count);
  319. rpc_clnt_sigmask(clnt, &oldset);
  320. result = nfs_direct_read_schedule(dreq, user_addr, count, pos);
  321. if (!result)
  322. result = nfs_direct_wait(dreq);
  323. rpc_clnt_sigunmask(clnt, &oldset);
  324. nfs_direct_req_release(dreq);
  325. return result;
  326. }
  327. static void nfs_direct_free_writedata(struct nfs_direct_req *dreq)
  328. {
  329. while (!list_empty(&dreq->rewrite_list)) {
  330. struct nfs_write_data *data = list_entry(dreq->rewrite_list.next, struct nfs_write_data, pages);
  331. list_del(&data->pages);
  332. nfs_direct_release_pages(data->pagevec, data->npages);
  333. nfs_writedata_release(data);
  334. }
  335. }
  336. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  337. static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
  338. {
  339. struct inode *inode = dreq->inode;
  340. struct list_head *p;
  341. struct nfs_write_data *data;
  342. dreq->count = 0;
  343. get_dreq(dreq);
  344. list_for_each(p, &dreq->rewrite_list) {
  345. data = list_entry(p, struct nfs_write_data, pages);
  346. get_dreq(dreq);
  347. /*
  348. * Reset data->res.
  349. */
  350. nfs_fattr_init(&data->fattr);
  351. data->res.count = data->args.count;
  352. memset(&data->verf, 0, sizeof(data->verf));
  353. /*
  354. * Reuse data->task; data->args should not have changed
  355. * since the original request was sent.
  356. */
  357. rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
  358. &nfs_write_direct_ops, data);
  359. NFS_PROTO(inode)->write_setup(data, FLUSH_STABLE);
  360. data->task.tk_priority = RPC_PRIORITY_NORMAL;
  361. data->task.tk_cookie = (unsigned long) inode;
  362. /*
  363. * We're called via an RPC callback, so BKL is already held.
  364. */
  365. rpc_execute(&data->task);
  366. dprintk("NFS: %5u rescheduled direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
  367. data->task.tk_pid,
  368. inode->i_sb->s_id,
  369. (long long)NFS_FILEID(inode),
  370. data->args.count,
  371. (unsigned long long)data->args.offset);
  372. }
  373. if (put_dreq(dreq))
  374. nfs_direct_write_complete(dreq, inode);
  375. }
  376. static void nfs_direct_commit_result(struct rpc_task *task, void *calldata)
  377. {
  378. struct nfs_write_data *data = calldata;
  379. struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
  380. /* Call the NFS version-specific code */
  381. if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
  382. return;
  383. if (unlikely(task->tk_status < 0)) {
  384. dprintk("NFS: %5u commit failed with error %d.\n",
  385. task->tk_pid, task->tk_status);
  386. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  387. } else if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
  388. dprintk("NFS: %5u commit verify failed\n", task->tk_pid);
  389. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  390. }
  391. dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status);
  392. nfs_direct_write_complete(dreq, data->inode);
  393. }
  394. static const struct rpc_call_ops nfs_commit_direct_ops = {
  395. .rpc_call_done = nfs_direct_commit_result,
  396. .rpc_release = nfs_commit_release,
  397. };
  398. static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
  399. {
  400. struct nfs_write_data *data = dreq->commit_data;
  401. data->inode = dreq->inode;
  402. data->cred = dreq->ctx->cred;
  403. data->args.fh = NFS_FH(data->inode);
  404. data->args.offset = 0;
  405. data->args.count = 0;
  406. data->res.count = 0;
  407. data->res.fattr = &data->fattr;
  408. data->res.verf = &data->verf;
  409. rpc_init_task(&data->task, NFS_CLIENT(dreq->inode), RPC_TASK_ASYNC,
  410. &nfs_commit_direct_ops, data);
  411. NFS_PROTO(data->inode)->commit_setup(data, 0);
  412. data->task.tk_priority = RPC_PRIORITY_NORMAL;
  413. data->task.tk_cookie = (unsigned long)data->inode;
  414. /* Note: task.tk_ops->rpc_release will free dreq->commit_data */
  415. dreq->commit_data = NULL;
  416. dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
  417. rpc_execute(&data->task);
  418. }
  419. static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
  420. {
  421. int flags = dreq->flags;
  422. dreq->flags = 0;
  423. switch (flags) {
  424. case NFS_ODIRECT_DO_COMMIT:
  425. nfs_direct_commit_schedule(dreq);
  426. break;
  427. case NFS_ODIRECT_RESCHED_WRITES:
  428. nfs_direct_write_reschedule(dreq);
  429. break;
  430. default:
  431. if (dreq->commit_data != NULL)
  432. nfs_commit_free(dreq->commit_data);
  433. nfs_direct_free_writedata(dreq);
  434. nfs_zap_mapping(inode, inode->i_mapping);
  435. nfs_direct_complete(dreq);
  436. }
  437. }
  438. static void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
  439. {
  440. dreq->commit_data = nfs_commit_alloc();
  441. if (dreq->commit_data != NULL)
  442. dreq->commit_data->req = (struct nfs_page *) dreq;
  443. }
  444. #else
  445. static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
  446. {
  447. dreq->commit_data = NULL;
  448. }
  449. static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
  450. {
  451. nfs_direct_free_writedata(dreq);
  452. nfs_zap_mapping(inode, inode->i_mapping);
  453. nfs_direct_complete(dreq);
  454. }
  455. #endif
  456. static void nfs_direct_write_result(struct rpc_task *task, void *calldata)
  457. {
  458. struct nfs_write_data *data = calldata;
  459. struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
  460. int status = task->tk_status;
  461. if (nfs_writeback_done(task, data) != 0)
  462. return;
  463. spin_lock(&dreq->lock);
  464. if (unlikely(dreq->error != 0))
  465. goto out_unlock;
  466. if (unlikely(status < 0)) {
  467. /* An error has occured, so we should not commit */
  468. dreq->flags = 0;
  469. dreq->error = status;
  470. }
  471. dreq->count += data->res.count;
  472. if (data->res.verf->committed != NFS_FILE_SYNC) {
  473. switch (dreq->flags) {
  474. case 0:
  475. memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf));
  476. dreq->flags = NFS_ODIRECT_DO_COMMIT;
  477. break;
  478. case NFS_ODIRECT_DO_COMMIT:
  479. if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) {
  480. dprintk("NFS: %5u write verify failed\n", task->tk_pid);
  481. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  482. }
  483. }
  484. }
  485. out_unlock:
  486. spin_unlock(&dreq->lock);
  487. }
  488. /*
  489. * NB: Return the value of the first error return code. Subsequent
  490. * errors after the first one are ignored.
  491. */
  492. static void nfs_direct_write_release(void *calldata)
  493. {
  494. struct nfs_write_data *data = calldata;
  495. struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
  496. if (put_dreq(dreq))
  497. nfs_direct_write_complete(dreq, data->inode);
  498. }
  499. static const struct rpc_call_ops nfs_write_direct_ops = {
  500. .rpc_call_done = nfs_direct_write_result,
  501. .rpc_release = nfs_direct_write_release,
  502. };
  503. /*
  504. * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
  505. * operation. If nfs_writedata_alloc() or get_user_pages() fails,
  506. * bail and stop sending more writes. Write length accounting is
  507. * handled automatically by nfs_direct_write_result(). Otherwise, if
  508. * no requests have been sent, just return an error.
  509. */
  510. static ssize_t nfs_direct_write_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t pos, int sync)
  511. {
  512. struct nfs_open_context *ctx = dreq->ctx;
  513. struct inode *inode = ctx->path.dentry->d_inode;
  514. size_t wsize = NFS_SERVER(inode)->wsize;
  515. unsigned int pgbase;
  516. int result;
  517. ssize_t started = 0;
  518. get_dreq(dreq);
  519. do {
  520. struct nfs_write_data *data;
  521. size_t bytes;
  522. pgbase = user_addr & ~PAGE_MASK;
  523. bytes = min(wsize,count);
  524. result = -ENOMEM;
  525. data = nfs_writedata_alloc(nfs_page_array_len(pgbase, bytes));
  526. if (unlikely(!data))
  527. break;
  528. down_read(&current->mm->mmap_sem);
  529. result = get_user_pages(current, current->mm, user_addr,
  530. data->npages, 0, 0, data->pagevec, NULL);
  531. up_read(&current->mm->mmap_sem);
  532. if (result < 0) {
  533. nfs_writedata_release(data);
  534. break;
  535. }
  536. if ((unsigned)result < data->npages) {
  537. bytes = result * PAGE_SIZE;
  538. if (bytes <= pgbase) {
  539. nfs_direct_release_pages(data->pagevec, result);
  540. nfs_writedata_release(data);
  541. break;
  542. }
  543. bytes -= pgbase;
  544. data->npages = result;
  545. }
  546. get_dreq(dreq);
  547. list_move_tail(&data->pages, &dreq->rewrite_list);
  548. data->req = (struct nfs_page *) dreq;
  549. data->inode = inode;
  550. data->cred = ctx->cred;
  551. data->args.fh = NFS_FH(inode);
  552. data->args.context = ctx;
  553. data->args.offset = pos;
  554. data->args.pgbase = pgbase;
  555. data->args.pages = data->pagevec;
  556. data->args.count = bytes;
  557. data->res.fattr = &data->fattr;
  558. data->res.count = bytes;
  559. data->res.verf = &data->verf;
  560. rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
  561. &nfs_write_direct_ops, data);
  562. NFS_PROTO(inode)->write_setup(data, sync);
  563. data->task.tk_priority = RPC_PRIORITY_NORMAL;
  564. data->task.tk_cookie = (unsigned long) inode;
  565. rpc_execute(&data->task);
  566. dprintk("NFS: %5u initiated direct write call "
  567. "(req %s/%Ld, %zu bytes @ offset %Lu)\n",
  568. data->task.tk_pid,
  569. inode->i_sb->s_id,
  570. (long long)NFS_FILEID(inode),
  571. bytes,
  572. (unsigned long long)data->args.offset);
  573. started += bytes;
  574. user_addr += bytes;
  575. pos += bytes;
  576. /* FIXME: Remove this useless math from the final patch */
  577. pgbase += bytes;
  578. pgbase &= ~PAGE_MASK;
  579. BUG_ON(pgbase != (user_addr & ~PAGE_MASK));
  580. count -= bytes;
  581. } while (count != 0);
  582. if (put_dreq(dreq))
  583. nfs_direct_write_complete(dreq, inode);
  584. if (started)
  585. return 0;
  586. return result < 0 ? (ssize_t) result : -EFAULT;
  587. }
  588. static ssize_t nfs_direct_write(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos)
  589. {
  590. ssize_t result = 0;
  591. sigset_t oldset;
  592. struct inode *inode = iocb->ki_filp->f_mapping->host;
  593. struct rpc_clnt *clnt = NFS_CLIENT(inode);
  594. struct nfs_direct_req *dreq;
  595. size_t wsize = NFS_SERVER(inode)->wsize;
  596. int sync = 0;
  597. dreq = nfs_direct_req_alloc();
  598. if (!dreq)
  599. return -ENOMEM;
  600. nfs_alloc_commit_data(dreq);
  601. if (dreq->commit_data == NULL || count < wsize)
  602. sync = FLUSH_STABLE;
  603. dreq->inode = inode;
  604. dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
  605. if (!is_sync_kiocb(iocb))
  606. dreq->iocb = iocb;
  607. nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, count);
  608. rpc_clnt_sigmask(clnt, &oldset);
  609. result = nfs_direct_write_schedule(dreq, user_addr, count, pos, sync);
  610. if (!result)
  611. result = nfs_direct_wait(dreq);
  612. rpc_clnt_sigunmask(clnt, &oldset);
  613. nfs_direct_req_release(dreq);
  614. return result;
  615. }
  616. /**
  617. * nfs_file_direct_read - file direct read operation for NFS files
  618. * @iocb: target I/O control block
  619. * @iov: vector of user buffers into which to read data
  620. * @nr_segs: size of iov vector
  621. * @pos: byte offset in file where reading starts
  622. *
  623. * We use this function for direct reads instead of calling
  624. * generic_file_aio_read() in order to avoid gfar's check to see if
  625. * the request starts before the end of the file. For that check
  626. * to work, we must generate a GETATTR before each direct read, and
  627. * even then there is a window between the GETATTR and the subsequent
  628. * READ where the file size could change. Our preference is simply
  629. * to do all reads the application wants, and the server will take
  630. * care of managing the end of file boundary.
  631. *
  632. * This function also eliminates unnecessarily updating the file's
  633. * atime locally, as the NFS server sets the file's atime, and this
  634. * client must read the updated atime from the server back into its
  635. * cache.
  636. */
  637. ssize_t nfs_file_direct_read(struct kiocb *iocb, const struct iovec *iov,
  638. unsigned long nr_segs, loff_t pos)
  639. {
  640. ssize_t retval = -EINVAL;
  641. struct file *file = iocb->ki_filp;
  642. struct address_space *mapping = file->f_mapping;
  643. /* XXX: temporary */
  644. const char __user *buf = iov[0].iov_base;
  645. size_t count = iov[0].iov_len;
  646. dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
  647. file->f_path.dentry->d_parent->d_name.name,
  648. file->f_path.dentry->d_name.name,
  649. (unsigned long) count, (long long) pos);
  650. if (nr_segs != 1)
  651. goto out;
  652. retval = -EFAULT;
  653. if (!access_ok(VERIFY_WRITE, buf, count))
  654. goto out;
  655. retval = 0;
  656. if (!count)
  657. goto out;
  658. retval = nfs_sync_mapping(mapping);
  659. if (retval)
  660. goto out;
  661. retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos);
  662. if (retval > 0)
  663. iocb->ki_pos = pos + retval;
  664. out:
  665. return retval;
  666. }
  667. /**
  668. * nfs_file_direct_write - file direct write operation for NFS files
  669. * @iocb: target I/O control block
  670. * @iov: vector of user buffers from which to write data
  671. * @nr_segs: size of iov vector
  672. * @pos: byte offset in file where writing starts
  673. *
  674. * We use this function for direct writes instead of calling
  675. * generic_file_aio_write() in order to avoid taking the inode
  676. * semaphore and updating the i_size. The NFS server will set
  677. * the new i_size and this client must read the updated size
  678. * back into its cache. We let the server do generic write
  679. * parameter checking and report problems.
  680. *
  681. * We also avoid an unnecessary invocation of generic_osync_inode(),
  682. * as it is fairly meaningless to sync the metadata of an NFS file.
  683. *
  684. * We eliminate local atime updates, see direct read above.
  685. *
  686. * We avoid unnecessary page cache invalidations for normal cached
  687. * readers of this file.
  688. *
  689. * Note that O_APPEND is not supported for NFS direct writes, as there
  690. * is no atomic O_APPEND write facility in the NFS protocol.
  691. */
  692. ssize_t nfs_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
  693. unsigned long nr_segs, loff_t pos)
  694. {
  695. ssize_t retval = -EINVAL;
  696. struct file *file = iocb->ki_filp;
  697. struct address_space *mapping = file->f_mapping;
  698. /* XXX: temporary */
  699. const char __user *buf = iov[0].iov_base;
  700. size_t count = iov[0].iov_len;
  701. dprintk("nfs: direct write(%s/%s, %lu@%Ld)\n",
  702. file->f_path.dentry->d_parent->d_name.name,
  703. file->f_path.dentry->d_name.name,
  704. (unsigned long) count, (long long) pos);
  705. if (nr_segs != 1)
  706. goto out;
  707. retval = generic_write_checks(file, &pos, &count, 0);
  708. if (retval)
  709. goto out;
  710. retval = -EINVAL;
  711. if ((ssize_t) count < 0)
  712. goto out;
  713. retval = 0;
  714. if (!count)
  715. goto out;
  716. retval = -EFAULT;
  717. if (!access_ok(VERIFY_READ, buf, count))
  718. goto out;
  719. retval = nfs_sync_mapping(mapping);
  720. if (retval)
  721. goto out;
  722. retval = nfs_direct_write(iocb, (unsigned long) buf, count, pos);
  723. if (retval > 0)
  724. iocb->ki_pos = pos + retval;
  725. out:
  726. return retval;
  727. }
  728. /**
  729. * nfs_init_directcache - create a slab cache for nfs_direct_req structures
  730. *
  731. */
  732. int __init nfs_init_directcache(void)
  733. {
  734. nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
  735. sizeof(struct nfs_direct_req),
  736. 0, (SLAB_RECLAIM_ACCOUNT|
  737. SLAB_MEM_SPREAD),
  738. NULL);
  739. if (nfs_direct_cachep == NULL)
  740. return -ENOMEM;
  741. return 0;
  742. }
  743. /**
  744. * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
  745. *
  746. */
  747. void nfs_destroy_directcache(void)
  748. {
  749. kmem_cache_destroy(nfs_direct_cachep);
  750. }