direct.c 24 KB

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