direct.c 26 KB

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