direct.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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. spinlock_t lock; /* protect completion state */
  75. int outstanding; /* i/os we're waiting for */
  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. /**
  89. * nfs_direct_IO - NFS address space operation for direct I/O
  90. * @rw: direction (read or write)
  91. * @iocb: target I/O control block
  92. * @iov: array of vectors that define I/O buffer
  93. * @pos: offset in file to begin the operation
  94. * @nr_segs: size of iovec array
  95. *
  96. * The presence of this routine in the address space ops vector means
  97. * the NFS client supports direct I/O. However, we shunt off direct
  98. * read and write requests before the VFS gets them, so this method
  99. * should never be called.
  100. */
  101. ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
  102. {
  103. dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
  104. iocb->ki_filp->f_dentry->d_name.name,
  105. (long long) pos, nr_segs);
  106. return -EINVAL;
  107. }
  108. static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty)
  109. {
  110. int i;
  111. for (i = 0; i < npages; i++) {
  112. struct page *page = pages[i];
  113. if (do_dirty && !PageCompound(page))
  114. set_page_dirty_lock(page);
  115. page_cache_release(page);
  116. }
  117. kfree(pages);
  118. }
  119. static inline int nfs_get_user_pages(int rw, unsigned long user_addr, size_t size, struct page ***pages)
  120. {
  121. int result = -ENOMEM;
  122. unsigned long page_count;
  123. size_t array_size;
  124. page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  125. page_count -= user_addr >> PAGE_SHIFT;
  126. array_size = (page_count * sizeof(struct page *));
  127. *pages = kmalloc(array_size, GFP_KERNEL);
  128. if (*pages) {
  129. down_read(&current->mm->mmap_sem);
  130. result = get_user_pages(current, current->mm, user_addr,
  131. page_count, (rw == READ), 0,
  132. *pages, NULL);
  133. up_read(&current->mm->mmap_sem);
  134. if (result != page_count) {
  135. /*
  136. * If we got fewer pages than expected from
  137. * get_user_pages(), the user buffer runs off the
  138. * end of a mapping; return EFAULT.
  139. */
  140. if (result >= 0) {
  141. nfs_free_user_pages(*pages, result, 0);
  142. result = -EFAULT;
  143. } else
  144. kfree(*pages);
  145. *pages = NULL;
  146. }
  147. }
  148. return result;
  149. }
  150. static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
  151. {
  152. struct nfs_direct_req *dreq;
  153. dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL);
  154. if (!dreq)
  155. return NULL;
  156. kref_init(&dreq->kref);
  157. init_completion(&dreq->completion);
  158. INIT_LIST_HEAD(&dreq->list);
  159. INIT_LIST_HEAD(&dreq->rewrite_list);
  160. dreq->iocb = NULL;
  161. dreq->ctx = NULL;
  162. spin_lock_init(&dreq->lock);
  163. dreq->outstanding = 0;
  164. dreq->count = 0;
  165. dreq->error = 0;
  166. dreq->flags = 0;
  167. return dreq;
  168. }
  169. static void nfs_direct_req_release(struct kref *kref)
  170. {
  171. struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
  172. if (dreq->ctx != NULL)
  173. put_nfs_open_context(dreq->ctx);
  174. kmem_cache_free(nfs_direct_cachep, dreq);
  175. }
  176. /*
  177. * Collects and returns the final error value/byte-count.
  178. */
  179. static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
  180. {
  181. ssize_t result = -EIOCBQUEUED;
  182. /* Async requests don't wait here */
  183. if (dreq->iocb)
  184. goto out;
  185. result = wait_for_completion_interruptible(&dreq->completion);
  186. if (!result)
  187. result = dreq->error;
  188. if (!result)
  189. result = dreq->count;
  190. out:
  191. kref_put(&dreq->kref, nfs_direct_req_release);
  192. return (ssize_t) result;
  193. }
  194. /*
  195. * We must hold a reference to all the pages in this direct read request
  196. * until the RPCs complete. This could be long *after* we are woken up in
  197. * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
  198. *
  199. * In addition, synchronous I/O uses a stack-allocated iocb. Thus we
  200. * can't trust the iocb is still valid here if this is a synchronous
  201. * request. If the waiter is woken prematurely, the iocb is long gone.
  202. */
  203. static void nfs_direct_complete(struct nfs_direct_req *dreq)
  204. {
  205. nfs_free_user_pages(dreq->pages, dreq->npages, 1);
  206. if (dreq->iocb) {
  207. long res = (long) dreq->error;
  208. if (!res)
  209. res = (long) dreq->count;
  210. aio_complete(dreq->iocb, res, 0);
  211. }
  212. complete_all(&dreq->completion);
  213. kref_put(&dreq->kref, nfs_direct_req_release);
  214. }
  215. /*
  216. * Note we also set the number of requests we have in the dreq when we are
  217. * done. This prevents races with I/O completion so we will always wait
  218. * until all requests have been dispatched and completed.
  219. */
  220. static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, size_t rsize)
  221. {
  222. struct list_head *list;
  223. struct nfs_direct_req *dreq;
  224. unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  225. dreq = nfs_direct_req_alloc();
  226. if (!dreq)
  227. return NULL;
  228. list = &dreq->list;
  229. for(;;) {
  230. struct nfs_read_data *data = nfs_readdata_alloc(rpages);
  231. if (unlikely(!data)) {
  232. while (!list_empty(list)) {
  233. data = list_entry(list->next,
  234. struct nfs_read_data, pages);
  235. list_del(&data->pages);
  236. nfs_readdata_free(data);
  237. }
  238. kref_put(&dreq->kref, nfs_direct_req_release);
  239. return NULL;
  240. }
  241. INIT_LIST_HEAD(&data->pages);
  242. list_add(&data->pages, list);
  243. data->req = (struct nfs_page *) dreq;
  244. dreq->outstanding++;
  245. if (nbytes <= rsize)
  246. break;
  247. nbytes -= rsize;
  248. }
  249. kref_get(&dreq->kref);
  250. return dreq;
  251. }
  252. static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
  253. {
  254. struct nfs_read_data *data = calldata;
  255. struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
  256. if (nfs_readpage_result(task, data) != 0)
  257. return;
  258. spin_lock(&dreq->lock);
  259. if (likely(task->tk_status >= 0))
  260. dreq->count += data->res.count;
  261. else
  262. dreq->error = task->tk_status;
  263. if (--dreq->outstanding) {
  264. spin_unlock(&dreq->lock);
  265. return;
  266. }
  267. spin_unlock(&dreq->lock);
  268. nfs_direct_complete(dreq);
  269. }
  270. static const struct rpc_call_ops nfs_read_direct_ops = {
  271. .rpc_call_done = nfs_direct_read_result,
  272. .rpc_release = nfs_readdata_release,
  273. };
  274. /*
  275. * For each nfs_read_data struct that was allocated on the list, dispatch
  276. * an NFS READ operation
  277. */
  278. static void nfs_direct_read_schedule(struct nfs_direct_req *dreq)
  279. {
  280. struct nfs_open_context *ctx = dreq->ctx;
  281. struct inode *inode = ctx->dentry->d_inode;
  282. struct list_head *list = &dreq->list;
  283. struct page **pages = dreq->pages;
  284. size_t count = dreq->user_count;
  285. loff_t pos = dreq->pos;
  286. size_t rsize = NFS_SERVER(inode)->rsize;
  287. unsigned int curpage, pgbase;
  288. curpage = 0;
  289. pgbase = dreq->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->user_addr = user_addr;
  342. dreq->user_count = count;
  343. dreq->pos = pos;
  344. dreq->pages = pages;
  345. dreq->npages = nr_pages;
  346. dreq->inode = inode;
  347. dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
  348. if (!is_sync_kiocb(iocb))
  349. dreq->iocb = iocb;
  350. nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count);
  351. rpc_clnt_sigmask(clnt, &oldset);
  352. nfs_direct_read_schedule(dreq);
  353. result = nfs_direct_wait(dreq);
  354. rpc_clnt_sigunmask(clnt, &oldset);
  355. return result;
  356. }
  357. static void nfs_direct_free_writedata(struct nfs_direct_req *dreq)
  358. {
  359. list_splice_init(&dreq->rewrite_list, &dreq->list);
  360. while (!list_empty(&dreq->list)) {
  361. struct nfs_write_data *data = list_entry(dreq->list.next, struct nfs_write_data, pages);
  362. list_del(&data->pages);
  363. nfs_writedata_release(data);
  364. }
  365. }
  366. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  367. static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
  368. {
  369. struct list_head *pos;
  370. list_splice_init(&dreq->rewrite_list, &dreq->list);
  371. list_for_each(pos, &dreq->list)
  372. dreq->outstanding++;
  373. dreq->count = 0;
  374. nfs_direct_write_schedule(dreq, FLUSH_STABLE);
  375. }
  376. static void nfs_direct_commit_result(struct rpc_task *task, void *calldata)
  377. {
  378. struct nfs_write_data *data = calldata;
  379. struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
  380. /* Call the NFS version-specific code */
  381. if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
  382. return;
  383. if (unlikely(task->tk_status < 0)) {
  384. dreq->error = task->tk_status;
  385. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  386. }
  387. if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
  388. dprintk("NFS: %5u commit verify failed\n", task->tk_pid);
  389. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  390. }
  391. dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status);
  392. nfs_direct_write_complete(dreq, data->inode);
  393. }
  394. static const struct rpc_call_ops nfs_commit_direct_ops = {
  395. .rpc_call_done = nfs_direct_commit_result,
  396. .rpc_release = nfs_commit_release,
  397. };
  398. static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
  399. {
  400. struct nfs_write_data *data = dreq->commit_data;
  401. data->inode = dreq->inode;
  402. data->cred = dreq->ctx->cred;
  403. data->args.fh = NFS_FH(data->inode);
  404. data->args.offset = dreq->pos;
  405. data->args.count = dreq->user_count;
  406. data->res.count = 0;
  407. data->res.fattr = &data->fattr;
  408. data->res.verf = &data->verf;
  409. rpc_init_task(&data->task, NFS_CLIENT(dreq->inode), RPC_TASK_ASYNC,
  410. &nfs_commit_direct_ops, data);
  411. NFS_PROTO(data->inode)->commit_setup(data, 0);
  412. data->task.tk_priority = RPC_PRIORITY_NORMAL;
  413. data->task.tk_cookie = (unsigned long)data->inode;
  414. /* Note: task.tk_ops->rpc_release will free dreq->commit_data */
  415. dreq->commit_data = NULL;
  416. dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
  417. lock_kernel();
  418. rpc_execute(&data->task);
  419. unlock_kernel();
  420. }
  421. static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
  422. {
  423. int flags = dreq->flags;
  424. dreq->flags = 0;
  425. switch (flags) {
  426. case NFS_ODIRECT_DO_COMMIT:
  427. nfs_direct_commit_schedule(dreq);
  428. break;
  429. case NFS_ODIRECT_RESCHED_WRITES:
  430. nfs_direct_write_reschedule(dreq);
  431. break;
  432. default:
  433. nfs_end_data_update(inode);
  434. if (dreq->commit_data != NULL)
  435. nfs_commit_free(dreq->commit_data);
  436. nfs_direct_free_writedata(dreq);
  437. nfs_direct_complete(dreq);
  438. }
  439. }
  440. static void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
  441. {
  442. dreq->commit_data = nfs_commit_alloc(0);
  443. if (dreq->commit_data != NULL)
  444. dreq->commit_data->req = (struct nfs_page *) dreq;
  445. }
  446. #else
  447. static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
  448. {
  449. dreq->commit_data = NULL;
  450. }
  451. static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
  452. {
  453. nfs_end_data_update(inode);
  454. nfs_direct_free_writedata(dreq);
  455. nfs_direct_complete(dreq);
  456. }
  457. #endif
  458. static struct nfs_direct_req *nfs_direct_write_alloc(size_t nbytes, size_t wsize)
  459. {
  460. struct list_head *list;
  461. struct nfs_direct_req *dreq;
  462. unsigned int wpages = (wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  463. dreq = nfs_direct_req_alloc();
  464. if (!dreq)
  465. return NULL;
  466. list = &dreq->list;
  467. for(;;) {
  468. struct nfs_write_data *data = nfs_writedata_alloc(wpages);
  469. if (unlikely(!data)) {
  470. while (!list_empty(list)) {
  471. data = list_entry(list->next,
  472. struct nfs_write_data, pages);
  473. list_del(&data->pages);
  474. nfs_writedata_free(data);
  475. }
  476. kref_put(&dreq->kref, nfs_direct_req_release);
  477. return NULL;
  478. }
  479. INIT_LIST_HEAD(&data->pages);
  480. list_add(&data->pages, list);
  481. data->req = (struct nfs_page *) dreq;
  482. dreq->outstanding++;
  483. if (nbytes <= wsize)
  484. break;
  485. nbytes -= wsize;
  486. }
  487. nfs_alloc_commit_data(dreq);
  488. kref_get(&dreq->kref);
  489. return dreq;
  490. }
  491. static void nfs_direct_write_result(struct rpc_task *task, void *calldata)
  492. {
  493. struct nfs_write_data *data = calldata;
  494. struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
  495. int status = task->tk_status;
  496. if (nfs_writeback_done(task, data) != 0)
  497. return;
  498. spin_lock(&dreq->lock);
  499. if (likely(status >= 0))
  500. dreq->count += data->res.count;
  501. else
  502. dreq->error = task->tk_status;
  503. if (data->res.verf->committed != NFS_FILE_SYNC) {
  504. switch (dreq->flags) {
  505. case 0:
  506. memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf));
  507. dreq->flags = NFS_ODIRECT_DO_COMMIT;
  508. break;
  509. case NFS_ODIRECT_DO_COMMIT:
  510. if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) {
  511. dprintk("NFS: %5u write verify failed\n", task->tk_pid);
  512. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  513. }
  514. }
  515. }
  516. /* In case we have to resend */
  517. data->args.stable = NFS_FILE_SYNC;
  518. spin_unlock(&dreq->lock);
  519. }
  520. /*
  521. * NB: Return the value of the first error return code. Subsequent
  522. * errors after the first one are ignored.
  523. */
  524. static void nfs_direct_write_release(void *calldata)
  525. {
  526. struct nfs_write_data *data = calldata;
  527. struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
  528. spin_lock(&dreq->lock);
  529. if (--dreq->outstanding) {
  530. spin_unlock(&dreq->lock);
  531. return;
  532. }
  533. spin_unlock(&dreq->lock);
  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 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_init_directcache - destroy the slab cache for nfs_direct_req structures
  774. *
  775. */
  776. void 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. }