direct.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /*
  2. * linux/fs/nfs/direct.c
  3. *
  4. * Copyright (C) 2003 by Chuck Lever <cel@netapp.com>
  5. *
  6. * High-performance uncached I/O for the Linux NFS client
  7. *
  8. * There are important applications whose performance or correctness
  9. * depends on uncached access to file data. Database clusters
  10. * (multiple copies of the same instance running on separate hosts)
  11. * implement their own cache coherency protocol that subsumes file
  12. * system cache protocols. Applications that process datasets
  13. * considerably larger than the client's memory do not always benefit
  14. * from a local cache. A streaming video server, for instance, has no
  15. * need to cache the contents of a file.
  16. *
  17. * When an application requests uncached I/O, all read and write requests
  18. * are made directly to the server; data stored or fetched via these
  19. * requests is not cached in the Linux page cache. The client does not
  20. * correct unaligned requests from applications. All requested bytes are
  21. * held on permanent storage before a direct write system call returns to
  22. * an application.
  23. *
  24. * Solaris implements an uncached I/O facility called directio() that
  25. * is used for backups and sequential I/O to very large files. Solaris
  26. * also supports uncaching whole NFS partitions with "-o forcedirectio,"
  27. * an undocumented mount option.
  28. *
  29. * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with
  30. * help from Andrew Morton.
  31. *
  32. * 18 Dec 2001 Initial implementation for 2.4 --cel
  33. * 08 Jul 2002 Version for 2.4.19, with bug fixes --trondmy
  34. * 08 Jun 2003 Port to 2.5 APIs --cel
  35. * 31 Mar 2004 Handle direct I/O without VFS support --cel
  36. * 15 Sep 2004 Parallel async reads --cel
  37. * 04 May 2005 support O_DIRECT with aio --cel
  38. *
  39. */
  40. #include <linux/config.h>
  41. #include <linux/errno.h>
  42. #include <linux/sched.h>
  43. #include <linux/kernel.h>
  44. #include <linux/smp_lock.h>
  45. #include <linux/file.h>
  46. #include <linux/pagemap.h>
  47. #include <linux/kref.h>
  48. #include <linux/nfs_fs.h>
  49. #include <linux/nfs_page.h>
  50. #include <linux/sunrpc/clnt.h>
  51. #include <asm/system.h>
  52. #include <asm/uaccess.h>
  53. #include <asm/atomic.h>
  54. #include "iostat.h"
  55. #define NFSDBG_FACILITY NFSDBG_VFS
  56. static kmem_cache_t *nfs_direct_cachep;
  57. /*
  58. * This represents a set of asynchronous requests that we're waiting on
  59. */
  60. struct nfs_direct_req {
  61. struct kref kref; /* release manager */
  62. /* I/O parameters */
  63. struct list_head list, /* nfs_read/write_data structs */
  64. rewrite_list; /* saved nfs_write_data structs */
  65. struct nfs_open_context *ctx; /* file open context info */
  66. struct kiocb * iocb; /* controlling i/o request */
  67. struct inode * inode; /* target file of i/o */
  68. unsigned long user_addr; /* location of user's buffer */
  69. size_t user_count; /* total bytes to move */
  70. loff_t pos; /* starting offset in file */
  71. struct page ** pages; /* pages in our buffer */
  72. unsigned int npages; /* count of pages */
  73. /* completion state */
  74. atomic_t io_count; /* i/os we're waiting for */
  75. spinlock_t lock; /* protect completion state */
  76. ssize_t count, /* bytes actually processed */
  77. error; /* any reported error */
  78. struct completion completion; /* wait for i/o completion */
  79. /* commit state */
  80. struct nfs_write_data * commit_data; /* special write_data for commits */
  81. int flags;
  82. #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */
  83. #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */
  84. struct nfs_writeverf verf; /* unstable write verifier */
  85. };
  86. static void nfs_direct_write_schedule(struct nfs_direct_req *dreq, int sync);
  87. static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
  88. static inline void get_dreq(struct nfs_direct_req *dreq)
  89. {
  90. atomic_inc(&dreq->io_count);
  91. }
  92. static inline int put_dreq(struct nfs_direct_req *dreq)
  93. {
  94. return atomic_dec_and_test(&dreq->io_count);
  95. }
  96. /**
  97. * nfs_direct_IO - NFS address space operation for direct I/O
  98. * @rw: direction (read or write)
  99. * @iocb: target I/O control block
  100. * @iov: array of vectors that define I/O buffer
  101. * @pos: offset in file to begin the operation
  102. * @nr_segs: size of iovec array
  103. *
  104. * The presence of this routine in the address space ops vector means
  105. * the NFS client supports direct I/O. However, we shunt off direct
  106. * read and write requests before the VFS gets them, so this method
  107. * should never be called.
  108. */
  109. ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
  110. {
  111. dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
  112. iocb->ki_filp->f_dentry->d_name.name,
  113. (long long) pos, nr_segs);
  114. return -EINVAL;
  115. }
  116. static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty)
  117. {
  118. int i;
  119. for (i = 0; i < npages; i++) {
  120. struct page *page = pages[i];
  121. if (do_dirty && !PageCompound(page))
  122. set_page_dirty_lock(page);
  123. page_cache_release(page);
  124. }
  125. kfree(pages);
  126. }
  127. static inline int nfs_get_user_pages(int rw, unsigned long user_addr, size_t size, struct page ***pages)
  128. {
  129. int result = -ENOMEM;
  130. unsigned long page_count;
  131. size_t array_size;
  132. page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  133. page_count -= user_addr >> PAGE_SHIFT;
  134. array_size = (page_count * sizeof(struct page *));
  135. *pages = kmalloc(array_size, GFP_KERNEL);
  136. if (*pages) {
  137. down_read(&current->mm->mmap_sem);
  138. result = get_user_pages(current, current->mm, user_addr,
  139. page_count, (rw == READ), 0,
  140. *pages, NULL);
  141. up_read(&current->mm->mmap_sem);
  142. if (result != page_count) {
  143. /*
  144. * If we got fewer pages than expected from
  145. * get_user_pages(), the user buffer runs off the
  146. * end of a mapping; return EFAULT.
  147. */
  148. if (result >= 0) {
  149. nfs_free_user_pages(*pages, result, 0);
  150. result = -EFAULT;
  151. } else
  152. kfree(*pages);
  153. *pages = NULL;
  154. }
  155. }
  156. return result;
  157. }
  158. static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
  159. {
  160. struct nfs_direct_req *dreq;
  161. dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL);
  162. if (!dreq)
  163. return NULL;
  164. kref_init(&dreq->kref);
  165. init_completion(&dreq->completion);
  166. INIT_LIST_HEAD(&dreq->list);
  167. INIT_LIST_HEAD(&dreq->rewrite_list);
  168. dreq->iocb = NULL;
  169. dreq->ctx = NULL;
  170. spin_lock_init(&dreq->lock);
  171. atomic_set(&dreq->io_count, 0);
  172. dreq->count = 0;
  173. dreq->error = 0;
  174. dreq->flags = 0;
  175. return dreq;
  176. }
  177. static void nfs_direct_req_release(struct kref *kref)
  178. {
  179. struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
  180. if (dreq->ctx != NULL)
  181. put_nfs_open_context(dreq->ctx);
  182. kmem_cache_free(nfs_direct_cachep, dreq);
  183. }
  184. /*
  185. * Collects and returns the final error value/byte-count.
  186. */
  187. static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
  188. {
  189. ssize_t result = -EIOCBQUEUED;
  190. /* Async requests don't wait here */
  191. if (dreq->iocb)
  192. goto out;
  193. result = wait_for_completion_interruptible(&dreq->completion);
  194. if (!result)
  195. result = dreq->error;
  196. if (!result)
  197. result = dreq->count;
  198. out:
  199. kref_put(&dreq->kref, nfs_direct_req_release);
  200. return (ssize_t) result;
  201. }
  202. /*
  203. * We must hold a reference to all the pages in this direct read request
  204. * until the RPCs complete. This could be long *after* we are woken up in
  205. * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
  206. *
  207. * In addition, synchronous I/O uses a stack-allocated iocb. Thus we
  208. * can't trust the iocb is still valid here if this is a synchronous
  209. * request. If the waiter is woken prematurely, the iocb is long gone.
  210. */
  211. static void nfs_direct_complete(struct nfs_direct_req *dreq)
  212. {
  213. nfs_free_user_pages(dreq->pages, dreq->npages, 1);
  214. if (dreq->iocb) {
  215. long res = (long) dreq->error;
  216. if (!res)
  217. res = (long) dreq->count;
  218. aio_complete(dreq->iocb, res, 0);
  219. }
  220. complete_all(&dreq->completion);
  221. kref_put(&dreq->kref, nfs_direct_req_release);
  222. }
  223. /*
  224. * Note we also set the number of requests we have in the dreq when we are
  225. * done. This prevents races with I/O completion so we will always wait
  226. * until all requests have been dispatched and completed.
  227. */
  228. static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, size_t rsize)
  229. {
  230. struct list_head *list;
  231. struct nfs_direct_req *dreq;
  232. unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  233. dreq = nfs_direct_req_alloc();
  234. if (!dreq)
  235. return NULL;
  236. list = &dreq->list;
  237. for(;;) {
  238. struct nfs_read_data *data = nfs_readdata_alloc(rpages);
  239. if (unlikely(!data)) {
  240. while (!list_empty(list)) {
  241. data = list_entry(list->next,
  242. struct nfs_read_data, pages);
  243. list_del(&data->pages);
  244. nfs_readdata_free(data);
  245. }
  246. kref_put(&dreq->kref, nfs_direct_req_release);
  247. return NULL;
  248. }
  249. INIT_LIST_HEAD(&data->pages);
  250. list_add(&data->pages, list);
  251. data->req = (struct nfs_page *) dreq;
  252. get_dreq(dreq);
  253. if (nbytes <= rsize)
  254. break;
  255. nbytes -= rsize;
  256. }
  257. kref_get(&dreq->kref);
  258. return dreq;
  259. }
  260. static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
  261. {
  262. struct nfs_read_data *data = calldata;
  263. struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
  264. if (nfs_readpage_result(task, data) != 0)
  265. return;
  266. spin_lock(&dreq->lock);
  267. if (likely(task->tk_status >= 0))
  268. dreq->count += data->res.count;
  269. else
  270. dreq->error = task->tk_status;
  271. spin_unlock(&dreq->lock);
  272. if (put_dreq(dreq))
  273. nfs_direct_complete(dreq);
  274. }
  275. static const struct rpc_call_ops nfs_read_direct_ops = {
  276. .rpc_call_done = nfs_direct_read_result,
  277. .rpc_release = nfs_readdata_release,
  278. };
  279. /*
  280. * For each nfs_read_data struct that was allocated on the list, dispatch
  281. * an NFS READ operation
  282. */
  283. static void nfs_direct_read_schedule(struct nfs_direct_req *dreq)
  284. {
  285. struct nfs_open_context *ctx = dreq->ctx;
  286. struct inode *inode = ctx->dentry->d_inode;
  287. struct list_head *list = &dreq->list;
  288. struct page **pages = dreq->pages;
  289. size_t count = dreq->user_count;
  290. loff_t pos = dreq->pos;
  291. size_t rsize = NFS_SERVER(inode)->rsize;
  292. unsigned int curpage, pgbase;
  293. curpage = 0;
  294. pgbase = dreq->user_addr & ~PAGE_MASK;
  295. do {
  296. struct nfs_read_data *data;
  297. size_t bytes;
  298. bytes = rsize;
  299. if (count < rsize)
  300. bytes = count;
  301. BUG_ON(list_empty(list));
  302. data = list_entry(list->next, struct nfs_read_data, pages);
  303. list_del_init(&data->pages);
  304. data->inode = inode;
  305. data->cred = ctx->cred;
  306. data->args.fh = NFS_FH(inode);
  307. data->args.context = ctx;
  308. data->args.offset = pos;
  309. data->args.pgbase = pgbase;
  310. data->args.pages = &pages[curpage];
  311. data->args.count = bytes;
  312. data->res.fattr = &data->fattr;
  313. data->res.eof = 0;
  314. data->res.count = bytes;
  315. rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
  316. &nfs_read_direct_ops, data);
  317. NFS_PROTO(inode)->read_setup(data);
  318. data->task.tk_cookie = (unsigned long) inode;
  319. lock_kernel();
  320. rpc_execute(&data->task);
  321. unlock_kernel();
  322. dfprintk(VFS, "NFS: %5u initiated direct read call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
  323. data->task.tk_pid,
  324. inode->i_sb->s_id,
  325. (long long)NFS_FILEID(inode),
  326. bytes,
  327. (unsigned long long)data->args.offset);
  328. pos += bytes;
  329. pgbase += bytes;
  330. curpage += pgbase >> PAGE_SHIFT;
  331. pgbase &= ~PAGE_MASK;
  332. count -= bytes;
  333. } while (count != 0);
  334. BUG_ON(!list_empty(list));
  335. }
  336. static ssize_t nfs_direct_read(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos, struct page **pages, unsigned int nr_pages)
  337. {
  338. ssize_t result;
  339. sigset_t oldset;
  340. struct inode *inode = iocb->ki_filp->f_mapping->host;
  341. struct rpc_clnt *clnt = NFS_CLIENT(inode);
  342. struct nfs_direct_req *dreq;
  343. dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize);
  344. if (!dreq)
  345. return -ENOMEM;
  346. dreq->user_addr = user_addr;
  347. dreq->user_count = count;
  348. dreq->pos = pos;
  349. dreq->pages = pages;
  350. dreq->npages = nr_pages;
  351. dreq->inode = inode;
  352. dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
  353. if (!is_sync_kiocb(iocb))
  354. dreq->iocb = iocb;
  355. nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count);
  356. rpc_clnt_sigmask(clnt, &oldset);
  357. nfs_direct_read_schedule(dreq);
  358. result = nfs_direct_wait(dreq);
  359. rpc_clnt_sigunmask(clnt, &oldset);
  360. return result;
  361. }
  362. static void nfs_direct_free_writedata(struct nfs_direct_req *dreq)
  363. {
  364. list_splice_init(&dreq->rewrite_list, &dreq->list);
  365. while (!list_empty(&dreq->list)) {
  366. struct nfs_write_data *data = list_entry(dreq->list.next, struct nfs_write_data, pages);
  367. list_del(&data->pages);
  368. nfs_writedata_release(data);
  369. }
  370. }
  371. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  372. static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
  373. {
  374. struct list_head *pos;
  375. list_splice_init(&dreq->rewrite_list, &dreq->list);
  376. list_for_each(pos, &dreq->list)
  377. get_dreq(dreq);
  378. dreq->count = 0;
  379. nfs_direct_write_schedule(dreq, FLUSH_STABLE);
  380. }
  381. static void nfs_direct_commit_result(struct rpc_task *task, void *calldata)
  382. {
  383. struct nfs_write_data *data = calldata;
  384. struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
  385. /* Call the NFS version-specific code */
  386. if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
  387. return;
  388. if (unlikely(task->tk_status < 0)) {
  389. dreq->error = task->tk_status;
  390. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  391. }
  392. if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
  393. dprintk("NFS: %5u commit verify failed\n", task->tk_pid);
  394. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  395. }
  396. dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status);
  397. nfs_direct_write_complete(dreq, data->inode);
  398. }
  399. static const struct rpc_call_ops nfs_commit_direct_ops = {
  400. .rpc_call_done = nfs_direct_commit_result,
  401. .rpc_release = nfs_commit_release,
  402. };
  403. static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
  404. {
  405. struct nfs_write_data *data = dreq->commit_data;
  406. data->inode = dreq->inode;
  407. data->cred = dreq->ctx->cred;
  408. data->args.fh = NFS_FH(data->inode);
  409. data->args.offset = dreq->pos;
  410. data->args.count = dreq->user_count;
  411. data->res.count = 0;
  412. data->res.fattr = &data->fattr;
  413. data->res.verf = &data->verf;
  414. rpc_init_task(&data->task, NFS_CLIENT(dreq->inode), RPC_TASK_ASYNC,
  415. &nfs_commit_direct_ops, data);
  416. NFS_PROTO(data->inode)->commit_setup(data, 0);
  417. data->task.tk_priority = RPC_PRIORITY_NORMAL;
  418. data->task.tk_cookie = (unsigned long)data->inode;
  419. /* Note: task.tk_ops->rpc_release will free dreq->commit_data */
  420. dreq->commit_data = NULL;
  421. dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
  422. lock_kernel();
  423. rpc_execute(&data->task);
  424. unlock_kernel();
  425. }
  426. static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
  427. {
  428. int flags = dreq->flags;
  429. dreq->flags = 0;
  430. switch (flags) {
  431. case NFS_ODIRECT_DO_COMMIT:
  432. nfs_direct_commit_schedule(dreq);
  433. break;
  434. case NFS_ODIRECT_RESCHED_WRITES:
  435. nfs_direct_write_reschedule(dreq);
  436. break;
  437. default:
  438. nfs_end_data_update(inode);
  439. if (dreq->commit_data != NULL)
  440. nfs_commit_free(dreq->commit_data);
  441. nfs_direct_free_writedata(dreq);
  442. nfs_direct_complete(dreq);
  443. }
  444. }
  445. static void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
  446. {
  447. dreq->commit_data = nfs_commit_alloc(0);
  448. if (dreq->commit_data != NULL)
  449. dreq->commit_data->req = (struct nfs_page *) dreq;
  450. }
  451. #else
  452. static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
  453. {
  454. dreq->commit_data = NULL;
  455. }
  456. static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
  457. {
  458. nfs_end_data_update(inode);
  459. nfs_direct_free_writedata(dreq);
  460. nfs_direct_complete(dreq);
  461. }
  462. #endif
  463. static struct nfs_direct_req *nfs_direct_write_alloc(size_t nbytes, size_t wsize)
  464. {
  465. struct list_head *list;
  466. struct nfs_direct_req *dreq;
  467. unsigned int wpages = (wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  468. dreq = nfs_direct_req_alloc();
  469. if (!dreq)
  470. return NULL;
  471. list = &dreq->list;
  472. for(;;) {
  473. struct nfs_write_data *data = nfs_writedata_alloc(wpages);
  474. if (unlikely(!data)) {
  475. while (!list_empty(list)) {
  476. data = list_entry(list->next,
  477. struct nfs_write_data, pages);
  478. list_del(&data->pages);
  479. nfs_writedata_free(data);
  480. }
  481. kref_put(&dreq->kref, nfs_direct_req_release);
  482. return NULL;
  483. }
  484. INIT_LIST_HEAD(&data->pages);
  485. list_add(&data->pages, list);
  486. data->req = (struct nfs_page *) dreq;
  487. get_dreq(dreq);
  488. if (nbytes <= wsize)
  489. break;
  490. nbytes -= wsize;
  491. }
  492. nfs_alloc_commit_data(dreq);
  493. kref_get(&dreq->kref);
  494. return dreq;
  495. }
  496. static void nfs_direct_write_result(struct rpc_task *task, void *calldata)
  497. {
  498. struct nfs_write_data *data = calldata;
  499. struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
  500. int status = task->tk_status;
  501. if (nfs_writeback_done(task, data) != 0)
  502. return;
  503. spin_lock(&dreq->lock);
  504. if (likely(status >= 0))
  505. dreq->count += data->res.count;
  506. else
  507. dreq->error = task->tk_status;
  508. if (data->res.verf->committed != NFS_FILE_SYNC) {
  509. switch (dreq->flags) {
  510. case 0:
  511. memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf));
  512. dreq->flags = NFS_ODIRECT_DO_COMMIT;
  513. break;
  514. case NFS_ODIRECT_DO_COMMIT:
  515. if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) {
  516. dprintk("NFS: %5u write verify failed\n", task->tk_pid);
  517. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  518. }
  519. }
  520. }
  521. /* In case we have to resend */
  522. data->args.stable = NFS_FILE_SYNC;
  523. spin_unlock(&dreq->lock);
  524. }
  525. /*
  526. * NB: Return the value of the first error return code. Subsequent
  527. * errors after the first one are ignored.
  528. */
  529. static void nfs_direct_write_release(void *calldata)
  530. {
  531. struct nfs_write_data *data = calldata;
  532. struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
  533. if (put_dreq(dreq))
  534. nfs_direct_write_complete(dreq, data->inode);
  535. }
  536. static const struct rpc_call_ops nfs_write_direct_ops = {
  537. .rpc_call_done = nfs_direct_write_result,
  538. .rpc_release = nfs_direct_write_release,
  539. };
  540. /*
  541. * For each nfs_write_data struct that was allocated on the list, dispatch
  542. * an NFS WRITE operation
  543. */
  544. static void nfs_direct_write_schedule(struct nfs_direct_req *dreq, int sync)
  545. {
  546. struct nfs_open_context *ctx = dreq->ctx;
  547. struct inode *inode = ctx->dentry->d_inode;
  548. struct list_head *list = &dreq->list;
  549. struct page **pages = dreq->pages;
  550. size_t count = dreq->user_count;
  551. loff_t pos = dreq->pos;
  552. size_t wsize = NFS_SERVER(inode)->wsize;
  553. unsigned int curpage, pgbase;
  554. curpage = 0;
  555. pgbase = dreq->user_addr & ~PAGE_MASK;
  556. do {
  557. struct nfs_write_data *data;
  558. size_t bytes;
  559. bytes = wsize;
  560. if (count < wsize)
  561. bytes = count;
  562. BUG_ON(list_empty(list));
  563. data = list_entry(list->next, struct nfs_write_data, pages);
  564. list_move_tail(&data->pages, &dreq->rewrite_list);
  565. data->inode = inode;
  566. data->cred = ctx->cred;
  567. data->args.fh = NFS_FH(inode);
  568. data->args.context = ctx;
  569. data->args.offset = pos;
  570. data->args.pgbase = pgbase;
  571. data->args.pages = &pages[curpage];
  572. data->args.count = bytes;
  573. data->res.fattr = &data->fattr;
  574. data->res.count = bytes;
  575. data->res.verf = &data->verf;
  576. rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
  577. &nfs_write_direct_ops, data);
  578. NFS_PROTO(inode)->write_setup(data, sync);
  579. data->task.tk_priority = RPC_PRIORITY_NORMAL;
  580. data->task.tk_cookie = (unsigned long) inode;
  581. lock_kernel();
  582. rpc_execute(&data->task);
  583. unlock_kernel();
  584. dfprintk(VFS, "NFS: %5u initiated direct write call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
  585. data->task.tk_pid,
  586. inode->i_sb->s_id,
  587. (long long)NFS_FILEID(inode),
  588. bytes,
  589. (unsigned long long)data->args.offset);
  590. pos += bytes;
  591. pgbase += bytes;
  592. curpage += pgbase >> PAGE_SHIFT;
  593. pgbase &= ~PAGE_MASK;
  594. count -= bytes;
  595. } while (count != 0);
  596. BUG_ON(!list_empty(list));
  597. }
  598. 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)
  599. {
  600. ssize_t result;
  601. sigset_t oldset;
  602. struct inode *inode = iocb->ki_filp->f_mapping->host;
  603. struct rpc_clnt *clnt = NFS_CLIENT(inode);
  604. struct nfs_direct_req *dreq;
  605. size_t wsize = NFS_SERVER(inode)->wsize;
  606. int sync = 0;
  607. dreq = nfs_direct_write_alloc(count, wsize);
  608. if (!dreq)
  609. return -ENOMEM;
  610. if (dreq->commit_data == NULL || count < wsize)
  611. sync = FLUSH_STABLE;
  612. dreq->user_addr = user_addr;
  613. dreq->user_count = count;
  614. dreq->pos = pos;
  615. dreq->pages = pages;
  616. dreq->npages = nr_pages;
  617. dreq->inode = inode;
  618. dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
  619. if (!is_sync_kiocb(iocb))
  620. dreq->iocb = iocb;
  621. nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, count);
  622. nfs_begin_data_update(inode);
  623. rpc_clnt_sigmask(clnt, &oldset);
  624. nfs_direct_write_schedule(dreq, sync);
  625. result = nfs_direct_wait(dreq);
  626. rpc_clnt_sigunmask(clnt, &oldset);
  627. return result;
  628. }
  629. /**
  630. * nfs_file_direct_read - file direct read operation for NFS files
  631. * @iocb: target I/O control block
  632. * @buf: user's buffer into which to read data
  633. * @count: number of bytes to read
  634. * @pos: byte offset in file where reading starts
  635. *
  636. * We use this function for direct reads instead of calling
  637. * generic_file_aio_read() in order to avoid gfar's check to see if
  638. * the request starts before the end of the file. For that check
  639. * to work, we must generate a GETATTR before each direct read, and
  640. * even then there is a window between the GETATTR and the subsequent
  641. * READ where the file size could change. Our preference is simply
  642. * to do all reads the application wants, and the server will take
  643. * care of managing the end of file boundary.
  644. *
  645. * This function also eliminates unnecessarily updating the file's
  646. * atime locally, as the NFS server sets the file's atime, and this
  647. * client must read the updated atime from the server back into its
  648. * cache.
  649. */
  650. ssize_t nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
  651. {
  652. ssize_t retval = -EINVAL;
  653. int page_count;
  654. struct page **pages;
  655. struct file *file = iocb->ki_filp;
  656. struct address_space *mapping = file->f_mapping;
  657. dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
  658. file->f_dentry->d_parent->d_name.name,
  659. file->f_dentry->d_name.name,
  660. (unsigned long) count, (long long) pos);
  661. if (count < 0)
  662. goto out;
  663. retval = -EFAULT;
  664. if (!access_ok(VERIFY_WRITE, buf, count))
  665. goto out;
  666. retval = 0;
  667. if (!count)
  668. goto out;
  669. retval = nfs_sync_mapping(mapping);
  670. if (retval)
  671. goto out;
  672. retval = nfs_get_user_pages(READ, (unsigned long) buf,
  673. count, &pages);
  674. if (retval < 0)
  675. goto out;
  676. page_count = retval;
  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. retval = nfs_get_user_pages(WRITE, (unsigned long) buf,
  736. count, &pages);
  737. if (retval < 0)
  738. goto out;
  739. page_count = retval;
  740. retval = nfs_direct_write(iocb, (unsigned long) buf, count,
  741. pos, pages, page_count);
  742. /*
  743. * XXX: nfs_end_data_update() already ensures this file's
  744. * cached data is subsequently invalidated. Do we really
  745. * need to call invalidate_inode_pages2() again here?
  746. *
  747. * For aio writes, this invalidation will almost certainly
  748. * occur before the writes complete. Kind of racey.
  749. */
  750. if (mapping->nrpages)
  751. invalidate_inode_pages2(mapping);
  752. if (retval > 0)
  753. iocb->ki_pos = pos + retval;
  754. out:
  755. return retval;
  756. }
  757. /**
  758. * nfs_init_directcache - create a slab cache for nfs_direct_req structures
  759. *
  760. */
  761. int __init nfs_init_directcache(void)
  762. {
  763. nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
  764. sizeof(struct nfs_direct_req),
  765. 0, (SLAB_RECLAIM_ACCOUNT|
  766. SLAB_MEM_SPREAD),
  767. NULL, NULL);
  768. if (nfs_direct_cachep == NULL)
  769. return -ENOMEM;
  770. return 0;
  771. }
  772. /**
  773. * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
  774. *
  775. */
  776. void __exit nfs_destroy_directcache(void)
  777. {
  778. if (kmem_cache_destroy(nfs_direct_cachep))
  779. printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n");
  780. }