blob: 7368b2130a412cc6ab40b2e17af952ef354383ef [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/pagelist.c
3 *
4 * A set of helper functions for managing NFS read and write requests.
5 * The main purpose of these routines is to provide support for the
6 * coalescing of several requests into a single RPC call.
7 *
8 * Copyright 2000, 2001 (c) Trond Myklebust <trond.myklebust@fys.uio.no>
9 *
10 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/slab.h>
13#include <linux/file.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040014#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/sunrpc/clnt.h>
Trond Myklebust1313e602012-01-17 22:04:24 -050016#include <linux/nfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/nfs3.h>
18#include <linux/nfs4.h>
19#include <linux/nfs_page.h>
20#include <linux/nfs_fs.h>
21#include <linux/nfs_mount.h>
Paul Gortmakerafeacc82011-05-26 16:00:52 -040022#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Trond Myklebust8d5658c2007-04-10 09:26:35 -040024#include "internal.h"
Fred Isamanbae724e2011-03-01 01:34:15 +000025#include "pnfs.h"
Trond Myklebust8d5658c2007-04-10 09:26:35 -040026
Anna Schumaker0eecb212014-05-06 09:12:32 -040027#define NFSDBG_FACILITY NFSDBG_PAGECACHE
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static struct kmem_cache *nfs_page_cachep;
Anna Schumakeref2c4882014-05-06 09:12:36 -040030static const struct rpc_call_ops nfs_pgio_common_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -040032static void nfs_free_request(struct nfs_page *);
33
Anna Schumaker00bfa302014-05-06 09:12:29 -040034static bool nfs_pgarray_set(struct nfs_page_array *p, unsigned int pagecount)
Fred Isaman30dd3742012-04-20 14:47:45 -040035{
36 p->npages = pagecount;
37 if (pagecount <= ARRAY_SIZE(p->page_array))
38 p->pagevec = p->page_array;
39 else {
40 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_KERNEL);
41 if (!p->pagevec)
42 p->npages = 0;
43 }
44 return p->pagevec != NULL;
45}
46
Fred Isaman4db6e0b2012-04-20 14:47:46 -040047void nfs_pgheader_init(struct nfs_pageio_descriptor *desc,
48 struct nfs_pgio_header *hdr,
49 void (*release)(struct nfs_pgio_header *hdr))
50{
51 hdr->req = nfs_list_entry(desc->pg_list.next);
52 hdr->inode = desc->pg_inode;
53 hdr->cred = hdr->req->wb_context->cred;
54 hdr->io_start = req_offset(hdr->req);
55 hdr->good_bytes = desc->pg_count;
Fred Isaman584aa812012-04-20 14:47:51 -040056 hdr->dreq = desc->pg_dreq;
Peng Taof6166382012-08-02 15:36:09 +030057 hdr->layout_private = desc->pg_layout_private;
Fred Isaman4db6e0b2012-04-20 14:47:46 -040058 hdr->release = release;
Fred Isaman061ae2e2012-04-20 14:47:48 -040059 hdr->completion_ops = desc->pg_completion_ops;
Fred Isaman584aa812012-04-20 14:47:51 -040060 if (hdr->completion_ops->init_hdr)
61 hdr->completion_ops->init_hdr(hdr);
Fred Isaman4db6e0b2012-04-20 14:47:46 -040062}
Bryan Schumaker89d77c82012-07-30 16:05:25 -040063EXPORT_SYMBOL_GPL(nfs_pgheader_init);
Fred Isaman4db6e0b2012-04-20 14:47:46 -040064
65void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos)
66{
67 spin_lock(&hdr->lock);
68 if (pos < hdr->io_start + hdr->good_bytes) {
69 set_bit(NFS_IOHDR_ERROR, &hdr->flags);
70 clear_bit(NFS_IOHDR_EOF, &hdr->flags);
71 hdr->good_bytes = pos - hdr->io_start;
72 hdr->error = error;
73 }
74 spin_unlock(&hdr->lock);
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static inline struct nfs_page *
78nfs_page_alloc(void)
79{
Mel Gorman192e5012012-07-31 16:45:16 -070080 struct nfs_page *p = kmem_cache_zalloc(nfs_page_cachep, GFP_NOIO);
Jesper Juhl72895b12010-12-09 23:17:15 +010081 if (p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 INIT_LIST_HEAD(&p->wb_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return p;
84}
85
86static inline void
87nfs_page_free(struct nfs_page *p)
88{
89 kmem_cache_free(nfs_page_cachep, p);
90}
91
Trond Myklebust577b4232013-04-08 21:38:12 -040092static void
93nfs_iocounter_inc(struct nfs_io_counter *c)
94{
95 atomic_inc(&c->io_count);
96}
97
98static void
99nfs_iocounter_dec(struct nfs_io_counter *c)
100{
101 if (atomic_dec_and_test(&c->io_count)) {
102 clear_bit(NFS_IO_INPROGRESS, &c->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100103 smp_mb__after_atomic();
Trond Myklebust577b4232013-04-08 21:38:12 -0400104 wake_up_bit(&c->flags, NFS_IO_INPROGRESS);
105 }
106}
107
108static int
109__nfs_iocounter_wait(struct nfs_io_counter *c)
110{
111 wait_queue_head_t *wq = bit_waitqueue(&c->flags, NFS_IO_INPROGRESS);
112 DEFINE_WAIT_BIT(q, &c->flags, NFS_IO_INPROGRESS);
113 int ret = 0;
114
115 do {
116 prepare_to_wait(wq, &q.wait, TASK_KILLABLE);
117 set_bit(NFS_IO_INPROGRESS, &c->flags);
118 if (atomic_read(&c->io_count) == 0)
119 break;
120 ret = nfs_wait_bit_killable(&c->flags);
121 } while (atomic_read(&c->io_count) != 0);
122 finish_wait(wq, &q.wait);
123 return ret;
124}
125
126/**
127 * nfs_iocounter_wait - wait for i/o to complete
128 * @c: nfs_io_counter to use
129 *
130 * returns -ERESTARTSYS if interrupted by a fatal signal.
131 * Otherwise returns 0 once the io_count hits 0.
132 */
133int
134nfs_iocounter_wait(struct nfs_io_counter *c)
135{
136 if (atomic_read(&c->io_count) == 0)
137 return 0;
138 return __nfs_iocounter_wait(c);
139}
140
Trond Myklebustf8680892014-05-29 11:45:57 -0400141static int nfs_wait_bit_uninterruptible(void *word)
142{
143 io_schedule();
144 return 0;
145}
146
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400147/*
148 * nfs_page_group_lock - lock the head of the page group
149 * @req - request in group that is to be locked
150 *
151 * this lock must be held if modifying the page group list
152 */
153void
154nfs_page_group_lock(struct nfs_page *req)
155{
156 struct nfs_page *head = req->wb_head;
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400157
158 WARN_ON_ONCE(head != head->wb_head);
159
Trond Myklebustf8680892014-05-29 11:45:57 -0400160 wait_on_bit_lock(&head->wb_flags, PG_HEADLOCK,
161 nfs_wait_bit_uninterruptible,
162 TASK_UNINTERRUPTIBLE);
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400163}
164
165/*
166 * nfs_page_group_unlock - unlock the head of the page group
167 * @req - request in group that is to be unlocked
168 */
169void
170nfs_page_group_unlock(struct nfs_page *req)
171{
172 struct nfs_page *head = req->wb_head;
173
174 WARN_ON_ONCE(head != head->wb_head);
175
Linus Torvaldsd1e1cda2014-06-10 15:02:42 -0700176 smp_mb__before_atomic();
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400177 clear_bit(PG_HEADLOCK, &head->wb_flags);
Linus Torvaldsd1e1cda2014-06-10 15:02:42 -0700178 smp_mb__after_atomic();
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400179 wake_up_bit(&head->wb_flags, PG_HEADLOCK);
180}
181
182/*
183 * nfs_page_group_sync_on_bit_locked
184 *
185 * must be called with page group lock held
186 */
187static bool
188nfs_page_group_sync_on_bit_locked(struct nfs_page *req, unsigned int bit)
189{
190 struct nfs_page *head = req->wb_head;
191 struct nfs_page *tmp;
192
193 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_flags));
194 WARN_ON_ONCE(test_and_set_bit(bit, &req->wb_flags));
195
196 tmp = req->wb_this_page;
197 while (tmp != req) {
198 if (!test_bit(bit, &tmp->wb_flags))
199 return false;
200 tmp = tmp->wb_this_page;
201 }
202
203 /* true! reset all bits */
204 tmp = req;
205 do {
206 clear_bit(bit, &tmp->wb_flags);
207 tmp = tmp->wb_this_page;
208 } while (tmp != req);
209
210 return true;
211}
212
213/*
214 * nfs_page_group_sync_on_bit - set bit on current request, but only
215 * return true if the bit is set for all requests in page group
216 * @req - request in page group
217 * @bit - PG_* bit that is used to sync page group
218 */
219bool nfs_page_group_sync_on_bit(struct nfs_page *req, unsigned int bit)
220{
221 bool ret;
222
223 nfs_page_group_lock(req);
224 ret = nfs_page_group_sync_on_bit_locked(req, bit);
225 nfs_page_group_unlock(req);
226
227 return ret;
228}
229
230/*
231 * nfs_page_group_init - Initialize the page group linkage for @req
232 * @req - a new nfs request
233 * @prev - the previous request in page group, or NULL if @req is the first
234 * or only request in the group (the head).
235 */
236static inline void
237nfs_page_group_init(struct nfs_page *req, struct nfs_page *prev)
238{
239 WARN_ON_ONCE(prev == req);
240
241 if (!prev) {
242 req->wb_head = req;
243 req->wb_this_page = req;
244 } else {
245 WARN_ON_ONCE(prev->wb_this_page != prev->wb_head);
246 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &prev->wb_head->wb_flags));
247 req->wb_head = prev->wb_head;
248 req->wb_this_page = prev->wb_this_page;
249 prev->wb_this_page = req;
250
251 /* grab extra ref if head request has extra ref from
252 * the write/commit path to handle handoff between write
253 * and commit lists */
Weston Andros Adamson17089a22014-07-11 10:20:45 -0400254 if (test_bit(PG_INODE_REF, &prev->wb_head->wb_flags)) {
255 set_bit(PG_INODE_REF, &req->wb_flags);
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400256 kref_get(&req->wb_kref);
Weston Andros Adamson17089a22014-07-11 10:20:45 -0400257 }
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400258 }
259}
260
261/*
262 * nfs_page_group_destroy - sync the destruction of page groups
263 * @req - request that no longer needs the page group
264 *
265 * releases the page group reference from each member once all
266 * members have called this function.
267 */
268static void
269nfs_page_group_destroy(struct kref *kref)
270{
271 struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
272 struct nfs_page *tmp, *next;
273
274 if (!nfs_page_group_sync_on_bit(req, PG_TEARDOWN))
275 return;
276
277 tmp = req;
278 do {
279 next = tmp->wb_this_page;
280 /* unlink and free */
281 tmp->wb_this_page = tmp;
282 tmp->wb_head = tmp;
283 nfs_free_request(tmp);
284 tmp = next;
285 } while (tmp != req);
286}
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288/**
289 * nfs_create_request - Create an NFS read/write request.
Chuck Leverc02f5572011-10-25 12:17:43 -0400290 * @ctx: open context to use
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 * @page: page to write
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400292 * @last: last nfs request created for this page group or NULL if head
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * @offset: starting offset within the page for the write
294 * @count: number of bytes to read/write
295 *
296 * The page must be locked by the caller. This makes sure we never
Jason Uhlenkotta19b89c2007-04-26 17:25:51 -0700297 * create two different requests for the same page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 * User should ensure it is safe to sleep in this function.
299 */
300struct nfs_page *
Weston Andros Adamson8c8f1ac12014-05-15 11:56:42 -0400301nfs_create_request(struct nfs_open_context *ctx, struct page *page,
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400302 struct nfs_page *last, unsigned int offset,
303 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 struct nfs_page *req;
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400306 struct nfs_lock_context *l_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Trond Myklebustc58c8442013-03-18 19:45:14 -0400308 if (test_bit(NFS_CONTEXT_BAD, &ctx->flags))
309 return ERR_PTR(-EBADF);
Trond Myklebust18eb8842010-05-13 12:51:02 -0400310 /* try to allocate the request struct */
311 req = nfs_page_alloc();
312 if (req == NULL)
313 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Jeff Layton015f0212010-10-28 10:10:37 -0400315 /* get lock context early so we can deal with alloc failures */
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400316 l_ctx = nfs_get_lock_context(ctx);
317 if (IS_ERR(l_ctx)) {
Jeff Layton015f0212010-10-28 10:10:37 -0400318 nfs_page_free(req);
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400319 return ERR_CAST(l_ctx);
Jeff Layton015f0212010-10-28 10:10:37 -0400320 }
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400321 req->wb_lock_context = l_ctx;
Trond Myklebust577b4232013-04-08 21:38:12 -0400322 nfs_iocounter_inc(&l_ctx->io_count);
Jeff Layton015f0212010-10-28 10:10:37 -0400323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 /* Initialize the request struct. Initially, we assume a
325 * long write-back delay. This will be adjusted in
326 * update_nfs_request below if the region is not locked. */
327 req->wb_page = page;
Mel Gormand56b4dd2012-07-31 16:45:06 -0700328 req->wb_index = page_file_index(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 page_cache_get(page);
330 req->wb_offset = offset;
331 req->wb_pgbase = offset;
332 req->wb_bytes = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 req->wb_context = get_nfs_open_context(ctx);
Trond Myklebustc03b4022007-06-17 13:26:38 -0400334 kref_init(&req->wb_kref);
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400335 nfs_page_group_init(req, last);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return req;
337}
338
339/**
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400340 * nfs_unlock_request - Unlock request and wake up sleepers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 * @req:
342 */
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400343void nfs_unlock_request(struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 if (!NFS_WBACK_BUSY(req)) {
346 printk(KERN_ERR "NFS: Invalid unlock attempted\n");
347 BUG();
348 }
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100349 smp_mb__before_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 clear_bit(PG_BUSY, &req->wb_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100351 smp_mb__after_atomic();
Trond Myklebust464a98b2005-06-22 17:16:21 +0000352 wake_up_bit(&req->wb_flags, PG_BUSY);
Trond Myklebust3aff4eb2012-05-09 14:30:35 -0400353}
354
355/**
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400356 * nfs_unlock_and_release_request - Unlock request and release the nfs_page
357 * @req:
Trond Myklebust3aff4eb2012-05-09 14:30:35 -0400358 */
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400359void nfs_unlock_and_release_request(struct nfs_page *req)
Trond Myklebust3aff4eb2012-05-09 14:30:35 -0400360{
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400361 nfs_unlock_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 nfs_release_request(req);
363}
364
Trond Myklebust4d65c522011-03-25 14:15:11 -0400365/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 * nfs_clear_request - Free up all resources allocated to the request
367 * @req:
368 *
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500369 * Release page and open context resources associated with a read/write
370 * request after it has completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 */
Trond Myklebust4d65c522011-03-25 14:15:11 -0400372static void nfs_clear_request(struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500374 struct page *page = req->wb_page;
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500375 struct nfs_open_context *ctx = req->wb_context;
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400376 struct nfs_lock_context *l_ctx = req->wb_lock_context;
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500377
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500378 if (page != NULL) {
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500379 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 req->wb_page = NULL;
381 }
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400382 if (l_ctx != NULL) {
Trond Myklebust577b4232013-04-08 21:38:12 -0400383 nfs_iocounter_dec(&l_ctx->io_count);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400384 nfs_put_lock_context(l_ctx);
385 req->wb_lock_context = NULL;
386 }
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500387 if (ctx != NULL) {
388 put_nfs_open_context(ctx);
389 req->wb_context = NULL;
390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393/**
394 * nfs_release_request - Release the count on an NFS read/write request
395 * @req: request to release
396 *
397 * Note: Should never be called with the spinlock held!
398 */
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400399static void nfs_free_request(struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400401 WARN_ON_ONCE(req->wb_this_page != req);
402
403 /* extra debug: make sure no sync bits are still set */
404 WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
Weston Andros Adamson67d03382014-05-15 11:56:46 -0400405 WARN_ON_ONCE(test_bit(PG_UNLOCKPAGE, &req->wb_flags));
406 WARN_ON_ONCE(test_bit(PG_UPTODATE, &req->wb_flags));
Weston Andros Adamson20633f02014-05-15 11:56:47 -0400407 WARN_ON_ONCE(test_bit(PG_WB_END, &req->wb_flags));
408 WARN_ON_ONCE(test_bit(PG_REMOVE, &req->wb_flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500410 /* Release struct file and open context */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 nfs_clear_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 nfs_page_free(req);
413}
414
Trond Myklebustc03b4022007-06-17 13:26:38 -0400415void nfs_release_request(struct nfs_page *req)
416{
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400417 kref_put(&req->wb_kref, nfs_page_group_destroy);
Trond Myklebust9f557cd2010-02-03 08:27:22 -0500418}
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420/**
421 * nfs_wait_on_request - Wait for a request to complete.
422 * @req: request to wait upon.
423 *
Matthew Wilcox150030b2007-12-06 16:24:39 -0500424 * Interruptible by fatal signals only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 * The user is responsible for holding a count on the request.
426 */
427int
428nfs_wait_on_request(struct nfs_page *req)
429{
Trond Myklebust9f557cd2010-02-03 08:27:22 -0500430 return wait_on_bit(&req->wb_flags, PG_BUSY,
431 nfs_wait_bit_uninterruptible,
432 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400435/*
436 * nfs_generic_pg_test - determine if requests can be coalesced
437 * @desc: pointer to descriptor
438 * @prev: previous request in desc, or NULL
439 * @req: this request
440 *
441 * Returns zero if @req can be coalesced into @desc, otherwise it returns
442 * the size of the request.
443 */
444size_t nfs_generic_pg_test(struct nfs_pageio_descriptor *desc,
445 struct nfs_page *prev, struct nfs_page *req)
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300446{
Weston Andros Adamsonf0cb9ab2014-05-15 11:56:52 -0400447 if (desc->pg_count > desc->pg_bsize) {
448 /* should never happen */
449 WARN_ON_ONCE(1);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300450 return 0;
Weston Andros Adamsonf0cb9ab2014-05-15 11:56:52 -0400451 }
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300452
Weston Andros Adamsonf0cb9ab2014-05-15 11:56:52 -0400453 return min(desc->pg_bsize - desc->pg_count, (size_t)req->wb_bytes);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300454}
Benny Halevy19345cb2011-06-19 18:33:46 -0400455EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300456
Anna Schumaker00bfa302014-05-06 09:12:29 -0400457static inline struct nfs_rw_header *NFS_RW_HEADER(struct nfs_pgio_header *hdr)
458{
459 return container_of(hdr, struct nfs_rw_header, header);
460}
461
462/**
Anna Schumaker4a0de552014-05-06 09:12:30 -0400463 * nfs_rw_header_alloc - Allocate a header for a read or write
464 * @ops: Read or write function vector
465 */
466struct nfs_rw_header *nfs_rw_header_alloc(const struct nfs_rw_ops *ops)
467{
468 struct nfs_rw_header *header = ops->rw_alloc_header();
469
470 if (header) {
471 struct nfs_pgio_header *hdr = &header->header;
472
473 INIT_LIST_HEAD(&hdr->pages);
Anna Schumaker4a0de552014-05-06 09:12:30 -0400474 spin_lock_init(&hdr->lock);
475 atomic_set(&hdr->refcnt, 0);
476 hdr->rw_ops = ops;
477 }
478 return header;
479}
480EXPORT_SYMBOL_GPL(nfs_rw_header_alloc);
481
482/*
483 * nfs_rw_header_free - Free a read or write header
484 * @hdr: The header to free
485 */
486void nfs_rw_header_free(struct nfs_pgio_header *hdr)
487{
488 hdr->rw_ops->rw_free_header(NFS_RW_HEADER(hdr));
489}
490EXPORT_SYMBOL_GPL(nfs_rw_header_free);
491
492/**
Anna Schumaker00bfa302014-05-06 09:12:29 -0400493 * nfs_pgio_data_alloc - Allocate pageio data
494 * @hdr: The header making a request
495 * @pagecount: Number of pages to create
496 */
Anna Schumakeref2c4882014-05-06 09:12:36 -0400497static struct nfs_pgio_data *nfs_pgio_data_alloc(struct nfs_pgio_header *hdr,
498 unsigned int pagecount)
Anna Schumaker00bfa302014-05-06 09:12:29 -0400499{
500 struct nfs_pgio_data *data, *prealloc;
501
502 prealloc = &NFS_RW_HEADER(hdr)->rpc_data;
503 if (prealloc->header == NULL)
504 data = prealloc;
505 else
506 data = kzalloc(sizeof(*data), GFP_KERNEL);
507 if (!data)
508 goto out;
509
510 if (nfs_pgarray_set(&data->pages, pagecount)) {
511 data->header = hdr;
512 atomic_inc(&hdr->refcnt);
513 } else {
514 if (data != prealloc)
515 kfree(data);
516 data = NULL;
517 }
518out:
519 return data;
520}
521
522/**
523 * nfs_pgio_data_release - Properly free pageio data
524 * @data: The data to release
525 */
526void nfs_pgio_data_release(struct nfs_pgio_data *data)
527{
528 struct nfs_pgio_header *hdr = data->header;
529 struct nfs_rw_header *pageio_header = NFS_RW_HEADER(hdr);
530
531 put_nfs_open_context(data->args.context);
532 if (data->pages.pagevec != data->pages.page_array)
533 kfree(data->pages.pagevec);
534 if (data == &pageio_header->rpc_data) {
535 data->header = NULL;
536 data = NULL;
537 }
538 if (atomic_dec_and_test(&hdr->refcnt))
539 hdr->completion_ops->completion(hdr);
540 /* Note: we only free the rpc_task after callbacks are done.
541 * See the comment in rpc_free_task() for why
542 */
543 kfree(data);
544}
545EXPORT_SYMBOL_GPL(nfs_pgio_data_release);
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547/**
Anna Schumakerce595152014-05-06 09:12:34 -0400548 * nfs_pgio_rpcsetup - Set up arguments for a pageio call
549 * @data: The pageio data
550 * @count: Number of bytes to read
551 * @offset: Initial offset
552 * @how: How to commit data (writes only)
553 * @cinfo: Commit information for the call (writes only)
554 */
Anna Schumakeref2c4882014-05-06 09:12:36 -0400555static void nfs_pgio_rpcsetup(struct nfs_pgio_data *data,
Anna Schumakerce595152014-05-06 09:12:34 -0400556 unsigned int count, unsigned int offset,
557 int how, struct nfs_commit_info *cinfo)
558{
559 struct nfs_page *req = data->header->req;
560
561 /* Set up the RPC argument and reply structs
562 * NB: take care not to mess about with data->commit et al. */
563
564 data->args.fh = NFS_FH(data->header->inode);
565 data->args.offset = req_offset(req) + offset;
566 /* pnfs_set_layoutcommit needs this */
567 data->mds_offset = data->args.offset;
568 data->args.pgbase = req->wb_pgbase + offset;
569 data->args.pages = data->pages.pagevec;
570 data->args.count = count;
571 data->args.context = get_nfs_open_context(req->wb_context);
572 data->args.lock_context = req->wb_lock_context;
573 data->args.stable = NFS_UNSTABLE;
574 switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
575 case 0:
576 break;
577 case FLUSH_COND_STABLE:
578 if (nfs_reqs_to_commit(cinfo))
579 break;
580 default:
581 data->args.stable = NFS_FILE_SYNC;
582 }
583
584 data->res.fattr = &data->fattr;
585 data->res.count = count;
586 data->res.eof = 0;
587 data->res.verf = &data->verf;
588 nfs_fattr_init(&data->fattr);
589}
590
591/**
Anna Schumakera4cdda52014-05-06 09:12:31 -0400592 * nfs_pgio_prepare - Prepare pageio data to go over the wire
593 * @task: The current task
594 * @calldata: pageio data to prepare
595 */
Anna Schumaker6f92fa42014-05-06 09:12:33 -0400596static void nfs_pgio_prepare(struct rpc_task *task, void *calldata)
Anna Schumakera4cdda52014-05-06 09:12:31 -0400597{
598 struct nfs_pgio_data *data = calldata;
599 int err;
600 err = NFS_PROTO(data->header->inode)->pgio_rpc_prepare(task, data);
601 if (err)
602 rpc_exit(task, err);
603}
604
Anna Schumaker1ed26f32014-05-06 09:12:37 -0400605int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_data *data,
606 const struct rpc_call_ops *call_ops, int how, int flags)
607{
608 struct rpc_task *task;
609 struct rpc_message msg = {
610 .rpc_argp = &data->args,
611 .rpc_resp = &data->res,
612 .rpc_cred = data->header->cred,
613 };
614 struct rpc_task_setup task_setup_data = {
615 .rpc_client = clnt,
616 .task = &data->task,
617 .rpc_message = &msg,
618 .callback_ops = call_ops,
619 .callback_data = data,
620 .workqueue = nfsiod_workqueue,
621 .flags = RPC_TASK_ASYNC | flags,
622 };
623 int ret = 0;
624
625 data->header->rw_ops->rw_initiate(data, &msg, &task_setup_data, how);
626
627 dprintk("NFS: %5u initiated pgio call "
628 "(req %s/%llu, %u bytes @ offset %llu)\n",
629 data->task.tk_pid,
630 data->header->inode->i_sb->s_id,
631 (unsigned long long)NFS_FILEID(data->header->inode),
632 data->args.count,
633 (unsigned long long)data->args.offset);
634
635 task = rpc_run_task(&task_setup_data);
636 if (IS_ERR(task)) {
637 ret = PTR_ERR(task);
638 goto out;
639 }
640 if (how & FLUSH_SYNC) {
641 ret = rpc_wait_for_completion_task(task);
642 if (ret == 0)
643 ret = task->tk_status;
644 }
645 rpc_put_task(task);
646out:
647 return ret;
648}
649EXPORT_SYMBOL_GPL(nfs_initiate_pgio);
650
Anna Schumakera4cdda52014-05-06 09:12:31 -0400651/**
Anna Schumaker844c9e62014-05-06 09:12:35 -0400652 * nfs_pgio_error - Clean up from a pageio error
653 * @desc: IO descriptor
654 * @hdr: pageio header
655 */
Anna Schumakeref2c4882014-05-06 09:12:36 -0400656static int nfs_pgio_error(struct nfs_pageio_descriptor *desc,
Anna Schumaker844c9e62014-05-06 09:12:35 -0400657 struct nfs_pgio_header *hdr)
658{
Anna Schumaker844c9e62014-05-06 09:12:35 -0400659 set_bit(NFS_IOHDR_REDO, &hdr->flags);
Weston Andros Adamson7f714722014-05-15 11:56:53 -0400660 nfs_pgio_data_release(hdr->data);
661 hdr->data = NULL;
Anna Schumaker844c9e62014-05-06 09:12:35 -0400662 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
663 return -ENOMEM;
664}
665
666/**
Anna Schumakera4cdda52014-05-06 09:12:31 -0400667 * nfs_pgio_release - Release pageio data
668 * @calldata: The pageio data to release
669 */
Anna Schumaker6f92fa42014-05-06 09:12:33 -0400670static void nfs_pgio_release(void *calldata)
Anna Schumakera4cdda52014-05-06 09:12:31 -0400671{
672 struct nfs_pgio_data *data = calldata;
673 if (data->header->rw_ops->rw_release)
674 data->header->rw_ops->rw_release(data);
675 nfs_pgio_data_release(data);
676}
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678/**
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400679 * nfs_pageio_init - initialise a page io descriptor
680 * @desc: pointer to descriptor
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400681 * @inode: pointer to inode
682 * @doio: pointer to io function
683 * @bsize: io block size
684 * @io_flags: extra parameters for the io function
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400685 */
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400686void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
687 struct inode *inode,
Trond Myklebust1751c362011-06-10 13:30:23 -0400688 const struct nfs_pageio_ops *pg_ops,
Fred Isaman061ae2e2012-04-20 14:47:48 -0400689 const struct nfs_pgio_completion_ops *compl_ops,
Anna Schumaker4a0de552014-05-06 09:12:30 -0400690 const struct nfs_rw_ops *rw_ops,
Trond Myklebust84dde762007-05-04 14:44:06 -0400691 size_t bsize,
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400692 int io_flags)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400693{
694 INIT_LIST_HEAD(&desc->pg_list);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400695 desc->pg_bytes_written = 0;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400696 desc->pg_count = 0;
697 desc->pg_bsize = bsize;
698 desc->pg_base = 0;
Trond Myklebustb31268a2011-03-21 17:02:00 -0400699 desc->pg_moreio = 0;
Trond Myklebustd9156f92011-07-12 13:42:02 -0400700 desc->pg_recoalesce = 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400701 desc->pg_inode = inode;
Trond Myklebust1751c362011-06-10 13:30:23 -0400702 desc->pg_ops = pg_ops;
Fred Isaman061ae2e2012-04-20 14:47:48 -0400703 desc->pg_completion_ops = compl_ops;
Anna Schumaker4a0de552014-05-06 09:12:30 -0400704 desc->pg_rw_ops = rw_ops;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400705 desc->pg_ioflags = io_flags;
706 desc->pg_error = 0;
Fred Isaman94ad1c82011-03-01 01:34:14 +0000707 desc->pg_lseg = NULL;
Fred Isaman584aa812012-04-20 14:47:51 -0400708 desc->pg_dreq = NULL;
Peng Taof6166382012-08-02 15:36:09 +0300709 desc->pg_layout_private = NULL;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400710}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400711EXPORT_SYMBOL_GPL(nfs_pageio_init);
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400712
Anna Schumaker0eecb212014-05-06 09:12:32 -0400713/**
714 * nfs_pgio_result - Basic pageio error handling
715 * @task: The task that ran
716 * @calldata: Pageio data to check
717 */
Anna Schumaker6f92fa42014-05-06 09:12:33 -0400718static void nfs_pgio_result(struct rpc_task *task, void *calldata)
Anna Schumaker0eecb212014-05-06 09:12:32 -0400719{
720 struct nfs_pgio_data *data = calldata;
721 struct inode *inode = data->header->inode;
722
723 dprintk("NFS: %s: %5u, (status %d)\n", __func__,
724 task->tk_pid, task->tk_status);
725
726 if (data->header->rw_ops->rw_done(task, data, inode) != 0)
727 return;
728 if (task->tk_status < 0)
729 nfs_set_pgio_error(data->header, task->tk_status, data->args.offset);
730 else
731 data->header->rw_ops->rw_result(task, data);
732}
733
Anna Schumakeref2c4882014-05-06 09:12:36 -0400734/*
Anna Schumakeref2c4882014-05-06 09:12:36 -0400735 * Create an RPC task for the given read or write request and kick it.
736 * The page must have been locked by the caller.
737 *
738 * It may happen that the page we're passed is not marked dirty.
739 * This is the case if nfs_updatepage detects a conflicting request
740 * that has been written but not committed.
741 */
Weston Andros Adamsonf0cb9ab2014-05-15 11:56:52 -0400742int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
743 struct nfs_pgio_header *hdr)
Anna Schumakeref2c4882014-05-06 09:12:36 -0400744{
745 struct nfs_page *req;
746 struct page **pages;
747 struct nfs_pgio_data *data;
748 struct list_head *head = &desc->pg_list;
749 struct nfs_commit_info cinfo;
750
751 data = nfs_pgio_data_alloc(hdr, nfs_page_array_len(desc->pg_base,
752 desc->pg_count));
753 if (!data)
754 return nfs_pgio_error(desc, hdr);
755
756 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
757 pages = data->pages.pagevec;
758 while (!list_empty(head)) {
759 req = nfs_list_entry(head->next);
760 nfs_list_remove_request(req);
761 nfs_list_add_request(req, &hdr->pages);
762 *pages++ = req->wb_page;
763 }
764
765 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
766 (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
767 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
768
769 /* Set up the argument struct */
770 nfs_pgio_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo);
Weston Andros Adamson7f714722014-05-15 11:56:53 -0400771 hdr->data = data;
Anna Schumakeref2c4882014-05-06 09:12:36 -0400772 desc->pg_rpc_callops = &nfs_pgio_common_ops;
773 return 0;
774}
Weston Andros Adamsonf0cb9ab2014-05-15 11:56:52 -0400775EXPORT_SYMBOL_GPL(nfs_generic_pgio);
Anna Schumakeref2c4882014-05-06 09:12:36 -0400776
Anna Schumaker41d8d5b2014-05-06 09:12:40 -0400777static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
Anna Schumakercf485fc2014-05-06 09:12:39 -0400778{
779 struct nfs_rw_header *rw_hdr;
780 struct nfs_pgio_header *hdr;
781 int ret;
782
783 rw_hdr = nfs_rw_header_alloc(desc->pg_rw_ops);
784 if (!rw_hdr) {
785 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
786 return -ENOMEM;
787 }
788 hdr = &rw_hdr->header;
789 nfs_pgheader_init(desc, hdr, nfs_rw_header_free);
790 atomic_inc(&hdr->refcnt);
791 ret = nfs_generic_pgio(desc, hdr);
792 if (ret == 0)
Weston Andros Adamson7f714722014-05-15 11:56:53 -0400793 ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode),
794 hdr->data, desc->pg_rpc_callops,
795 desc->pg_ioflags, 0);
Anna Schumakercf485fc2014-05-06 09:12:39 -0400796 if (atomic_dec_and_test(&hdr->refcnt))
797 hdr->completion_ops->completion(hdr);
798 return ret;
799}
800
Trond Myklebust4109bb72013-09-06 11:09:38 -0400801static bool nfs_match_open_context(const struct nfs_open_context *ctx1,
802 const struct nfs_open_context *ctx2)
803{
804 return ctx1->cred == ctx2->cred && ctx1->state == ctx2->state;
805}
806
807static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
808 const struct nfs_lock_context *l2)
809{
810 return l1->lockowner.l_owner == l2->lockowner.l_owner
811 && l1->lockowner.l_pid == l2->lockowner.l_pid;
812}
813
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400814/**
815 * nfs_can_coalesce_requests - test two requests for compatibility
816 * @prev: pointer to nfs_page
817 * @req: pointer to nfs_page
818 *
819 * The nfs_page structures 'prev' and 'req' are compared to ensure that the
820 * page data area they describe is contiguous, and that their RPC
821 * credentials, NFSv4 open state, and lockowners are the same.
822 *
823 * Return 'true' if this is the case, else return 'false'.
824 */
Benny Halevy18ad0a92011-05-25 21:03:56 +0300825static bool nfs_can_coalesce_requests(struct nfs_page *prev,
826 struct nfs_page *req,
827 struct nfs_pageio_descriptor *pgio)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400828{
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400829 size_t size;
830
Weston Andros Adamsonab75e412014-05-15 11:56:44 -0400831 if (prev) {
832 if (!nfs_match_open_context(req->wb_context, prev->wb_context))
833 return false;
834 if (req->wb_context->dentry->d_inode->i_flock != NULL &&
835 !nfs_match_lock_context(req->wb_lock_context,
836 prev->wb_lock_context))
837 return false;
Weston Andros Adamsonab75e412014-05-15 11:56:44 -0400838 if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
839 return false;
840 }
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400841 size = pgio->pg_ops->pg_test(pgio, prev, req);
Weston Andros Adamsonf0cb9ab2014-05-15 11:56:52 -0400842 WARN_ON_ONCE(size > req->wb_bytes);
843 if (size && size < req->wb_bytes)
844 req->wb_bytes = size;
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400845 return size > 0;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400846}
847
848/**
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400849 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400850 * @desc: destination io descriptor
851 * @req: request
852 *
853 * Returns true if the request 'req' was successfully coalesced into the
854 * existing list of pages 'desc'.
855 */
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400856static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
857 struct nfs_page *req)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400858{
Weston Andros Adamsonab75e412014-05-15 11:56:44 -0400859 struct nfs_page *prev = NULL;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400860 if (desc->pg_count != 0) {
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400861 prev = nfs_list_entry(desc->pg_list.prev);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300862 } else {
Trond Myklebustd8007d42011-06-10 13:30:23 -0400863 if (desc->pg_ops->pg_init)
864 desc->pg_ops->pg_init(desc, req);
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400865 desc->pg_base = req->wb_pgbase;
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300866 }
Weston Andros Adamsonab75e412014-05-15 11:56:44 -0400867 if (!nfs_can_coalesce_requests(prev, req, desc))
868 return 0;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400869 nfs_list_remove_request(req);
870 nfs_list_add_request(req, &desc->pg_list);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300871 desc->pg_count += req->wb_bytes;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400872 return 1;
873}
874
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400875/*
876 * Helper for nfs_pageio_add_request and nfs_pageio_complete
877 */
878static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
879{
880 if (!list_empty(&desc->pg_list)) {
Trond Myklebust1751c362011-06-10 13:30:23 -0400881 int error = desc->pg_ops->pg_doio(desc);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400882 if (error < 0)
883 desc->pg_error = error;
884 else
885 desc->pg_bytes_written += desc->pg_count;
886 }
887 if (list_empty(&desc->pg_list)) {
888 desc->pg_count = 0;
889 desc->pg_base = 0;
890 }
891}
892
893/**
894 * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
895 * @desc: destination io descriptor
896 * @req: request
897 *
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400898 * This may split a request into subrequests which are all part of the
899 * same page group.
900 *
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400901 * Returns true if the request 'req' was successfully coalesced into the
902 * existing list of pages 'desc'.
903 */
Trond Myklebustd9156f92011-07-12 13:42:02 -0400904static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400905 struct nfs_page *req)
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400906{
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400907 struct nfs_page *subreq;
908 unsigned int bytes_left = 0;
909 unsigned int offset, pgbase;
910
911 nfs_page_group_lock(req);
912
913 subreq = req;
914 bytes_left = subreq->wb_bytes;
915 offset = subreq->wb_offset;
916 pgbase = subreq->wb_pgbase;
917
918 do {
919 if (!nfs_pageio_do_add_request(desc, subreq)) {
920 /* make sure pg_test call(s) did nothing */
921 WARN_ON_ONCE(subreq->wb_bytes != bytes_left);
922 WARN_ON_ONCE(subreq->wb_offset != offset);
923 WARN_ON_ONCE(subreq->wb_pgbase != pgbase);
924
925 nfs_page_group_unlock(req);
926 desc->pg_moreio = 1;
927 nfs_pageio_doio(desc);
928 if (desc->pg_error < 0)
929 return 0;
930 desc->pg_moreio = 0;
931 if (desc->pg_recoalesce)
932 return 0;
933 /* retry add_request for this subreq */
934 nfs_page_group_lock(req);
935 continue;
936 }
937
938 /* check for buggy pg_test call(s) */
939 WARN_ON_ONCE(subreq->wb_bytes + subreq->wb_pgbase > PAGE_SIZE);
940 WARN_ON_ONCE(subreq->wb_bytes > bytes_left);
941 WARN_ON_ONCE(subreq->wb_bytes == 0);
942
943 bytes_left -= subreq->wb_bytes;
944 offset += subreq->wb_bytes;
945 pgbase += subreq->wb_bytes;
946
947 if (bytes_left) {
948 subreq = nfs_create_request(req->wb_context,
949 req->wb_page,
950 subreq, pgbase, bytes_left);
Trond Myklebustc1109552014-05-29 11:38:15 -0400951 if (IS_ERR(subreq))
952 goto err_ptr;
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400953 nfs_lock_request(subreq);
954 subreq->wb_offset = offset;
955 subreq->wb_index = req->wb_index;
956 }
957 } while (bytes_left > 0);
958
959 nfs_page_group_unlock(req);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400960 return 1;
Trond Myklebustc1109552014-05-29 11:38:15 -0400961err_ptr:
962 desc->pg_error = PTR_ERR(subreq);
963 nfs_page_group_unlock(req);
964 return 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400965}
966
Trond Myklebustd9156f92011-07-12 13:42:02 -0400967static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
968{
969 LIST_HEAD(head);
970
971 do {
972 list_splice_init(&desc->pg_list, &head);
973 desc->pg_bytes_written -= desc->pg_count;
974 desc->pg_count = 0;
975 desc->pg_base = 0;
976 desc->pg_recoalesce = 0;
977
978 while (!list_empty(&head)) {
979 struct nfs_page *req;
980
981 req = list_first_entry(&head, struct nfs_page, wb_list);
982 nfs_list_remove_request(req);
983 if (__nfs_pageio_add_request(desc, req))
984 continue;
985 if (desc->pg_error < 0)
986 return 0;
987 break;
988 }
989 } while (desc->pg_recoalesce);
990 return 1;
991}
992
993int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
994 struct nfs_page *req)
995{
996 int ret;
997
998 do {
999 ret = __nfs_pageio_add_request(desc, req);
1000 if (ret)
1001 break;
1002 if (desc->pg_error < 0)
1003 break;
1004 ret = nfs_do_recoalesce(desc);
1005 } while (ret);
1006 return ret;
1007}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001008EXPORT_SYMBOL_GPL(nfs_pageio_add_request);
Trond Myklebustd9156f92011-07-12 13:42:02 -04001009
Trond Myklebustd8a5ad72007-04-02 18:48:28 -04001010/**
Trond Myklebustbcb71bb2007-04-02 18:48:28 -04001011 * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
1012 * @desc: pointer to io descriptor
1013 */
1014void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
1015{
Trond Myklebustd9156f92011-07-12 13:42:02 -04001016 for (;;) {
1017 nfs_pageio_doio(desc);
1018 if (!desc->pg_recoalesce)
1019 break;
1020 if (!nfs_do_recoalesce(desc))
1021 break;
1022 }
Trond Myklebustbcb71bb2007-04-02 18:48:28 -04001023}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001024EXPORT_SYMBOL_GPL(nfs_pageio_complete);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -04001025
Trond Myklebust7fe7f842007-05-20 10:18:27 -04001026/**
1027 * nfs_pageio_cond_complete - Conditional I/O completion
1028 * @desc: pointer to io descriptor
1029 * @index: page index
1030 *
1031 * It is important to ensure that processes don't try to take locks
1032 * on non-contiguous ranges of pages as that might deadlock. This
1033 * function should be called before attempting to wait on a locked
1034 * nfs_page. It will complete the I/O if the page index 'index'
1035 * is not contiguous with the existing list of pages in 'desc'.
1036 */
1037void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
1038{
1039 if (!list_empty(&desc->pg_list)) {
1040 struct nfs_page *prev = nfs_list_entry(desc->pg_list.prev);
1041 if (index != prev->wb_index + 1)
Trond Myklebustd9156f92011-07-12 13:42:02 -04001042 nfs_pageio_complete(desc);
Trond Myklebust7fe7f842007-05-20 10:18:27 -04001043 }
1044}
1045
David Howellsf7b422b2006-06-09 09:34:33 -04001046int __init nfs_init_nfspagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
1048 nfs_page_cachep = kmem_cache_create("nfs_page",
1049 sizeof(struct nfs_page),
1050 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001051 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (nfs_page_cachep == NULL)
1053 return -ENOMEM;
1054
1055 return 0;
1056}
1057
David Brownell266bee82006-06-27 12:59:15 -07001058void nfs_destroy_nfspagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001060 kmem_cache_destroy(nfs_page_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
1062
Anna Schumakeref2c4882014-05-06 09:12:36 -04001063static const struct rpc_call_ops nfs_pgio_common_ops = {
Anna Schumaker6f92fa42014-05-06 09:12:33 -04001064 .rpc_call_prepare = nfs_pgio_prepare,
1065 .rpc_call_done = nfs_pgio_result,
1066 .rpc_release = nfs_pgio_release,
1067};
Anna Schumaker41d8d5b2014-05-06 09:12:40 -04001068
1069const struct nfs_pageio_ops nfs_pgio_rw_ops = {
1070 .pg_test = nfs_generic_pg_test,
1071 .pg_doio = nfs_generic_pg_pgios,
1072};