direct.c 26 KB

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