direct.c 25 KB

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