direct.c 24 KB

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