direct.c 25 KB

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