direct.c 24 KB

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