blob: f9d8c4691149709d894596ea432d213edd25ddf3 [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
Anna Schumaker00bfa302014-05-06 09:12:29 -040032static bool nfs_pgarray_set(struct nfs_page_array *p, unsigned int pagecount)
Fred Isaman30dd3742012-04-20 14:47:45 -040033{
34 p->npages = pagecount;
35 if (pagecount <= ARRAY_SIZE(p->page_array))
36 p->pagevec = p->page_array;
37 else {
38 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_KERNEL);
39 if (!p->pagevec)
40 p->npages = 0;
41 }
42 return p->pagevec != NULL;
43}
44
Fred Isaman4db6e0b2012-04-20 14:47:46 -040045void nfs_pgheader_init(struct nfs_pageio_descriptor *desc,
46 struct nfs_pgio_header *hdr,
47 void (*release)(struct nfs_pgio_header *hdr))
48{
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -040049 struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[desc->pg_mirror_idx];
50
51
52 hdr->req = nfs_list_entry(mirror->pg_list.next);
Fred Isaman4db6e0b2012-04-20 14:47:46 -040053 hdr->inode = desc->pg_inode;
54 hdr->cred = hdr->req->wb_context->cred;
55 hdr->io_start = req_offset(hdr->req);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -040056 hdr->good_bytes = mirror->pg_count;
Fred Isaman584aa812012-04-20 14:47:51 -040057 hdr->dreq = desc->pg_dreq;
Peng Taof6166382012-08-02 15:36:09 +030058 hdr->layout_private = desc->pg_layout_private;
Fred Isaman4db6e0b2012-04-20 14:47:46 -040059 hdr->release = release;
Fred Isaman061ae2e2012-04-20 14:47:48 -040060 hdr->completion_ops = desc->pg_completion_ops;
Fred Isaman584aa812012-04-20 14:47:51 -040061 if (hdr->completion_ops->init_hdr)
62 hdr->completion_ops->init_hdr(hdr);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -040063
64 hdr->pgio_mirror_idx = desc->pg_mirror_idx;
Fred Isaman4db6e0b2012-04-20 14:47:46 -040065}
Bryan Schumaker89d77c82012-07-30 16:05:25 -040066EXPORT_SYMBOL_GPL(nfs_pgheader_init);
Fred Isaman4db6e0b2012-04-20 14:47:46 -040067
68void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos)
69{
70 spin_lock(&hdr->lock);
71 if (pos < hdr->io_start + hdr->good_bytes) {
72 set_bit(NFS_IOHDR_ERROR, &hdr->flags);
73 clear_bit(NFS_IOHDR_EOF, &hdr->flags);
74 hdr->good_bytes = pos - hdr->io_start;
75 hdr->error = error;
76 }
77 spin_unlock(&hdr->lock);
78}
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static inline struct nfs_page *
81nfs_page_alloc(void)
82{
Mel Gorman192e5012012-07-31 16:45:16 -070083 struct nfs_page *p = kmem_cache_zalloc(nfs_page_cachep, GFP_NOIO);
Jesper Juhl72895b12010-12-09 23:17:15 +010084 if (p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 INIT_LIST_HEAD(&p->wb_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return p;
87}
88
89static inline void
90nfs_page_free(struct nfs_page *p)
91{
92 kmem_cache_free(nfs_page_cachep, p);
93}
94
Trond Myklebust577b4232013-04-08 21:38:12 -040095static void
96nfs_iocounter_inc(struct nfs_io_counter *c)
97{
98 atomic_inc(&c->io_count);
99}
100
101static void
102nfs_iocounter_dec(struct nfs_io_counter *c)
103{
104 if (atomic_dec_and_test(&c->io_count)) {
105 clear_bit(NFS_IO_INPROGRESS, &c->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100106 smp_mb__after_atomic();
Trond Myklebust577b4232013-04-08 21:38:12 -0400107 wake_up_bit(&c->flags, NFS_IO_INPROGRESS);
108 }
109}
110
111static int
112__nfs_iocounter_wait(struct nfs_io_counter *c)
113{
114 wait_queue_head_t *wq = bit_waitqueue(&c->flags, NFS_IO_INPROGRESS);
115 DEFINE_WAIT_BIT(q, &c->flags, NFS_IO_INPROGRESS);
116 int ret = 0;
117
118 do {
119 prepare_to_wait(wq, &q.wait, TASK_KILLABLE);
120 set_bit(NFS_IO_INPROGRESS, &c->flags);
121 if (atomic_read(&c->io_count) == 0)
122 break;
NeilBrownc1221322014-07-07 15:16:04 +1000123 ret = nfs_wait_bit_killable(&q.key);
David Jeffery92a56552014-08-05 11:19:42 -0400124 } while (atomic_read(&c->io_count) != 0 && !ret);
Trond Myklebust577b4232013-04-08 21:38:12 -0400125 finish_wait(wq, &q.wait);
126 return ret;
127}
128
129/**
130 * nfs_iocounter_wait - wait for i/o to complete
131 * @c: nfs_io_counter to use
132 *
133 * returns -ERESTARTSYS if interrupted by a fatal signal.
134 * Otherwise returns 0 once the io_count hits 0.
135 */
136int
137nfs_iocounter_wait(struct nfs_io_counter *c)
138{
139 if (atomic_read(&c->io_count) == 0)
140 return 0;
141 return __nfs_iocounter_wait(c);
142}
143
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400144/*
145 * nfs_page_group_lock - lock the head of the page group
146 * @req - request in group that is to be locked
Weston Andros Adamsonfd2f3a02014-08-08 11:00:53 -0400147 * @nonblock - if true don't block waiting for lock
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400148 *
149 * this lock must be held if modifying the page group list
Weston Andros Adamsone7029202014-07-17 20:42:15 -0400150 *
Weston Andros Adamsonbc8a3092014-08-08 11:00:54 -0400151 * return 0 on success, < 0 on error: -EDELAY if nonblocking or the
152 * result from wait_on_bit_lock
153 *
154 * NOTE: calling with nonblock=false should always have set the
155 * lock bit (see fs/buffer.c and other uses of wait_on_bit_lock
156 * with TASK_UNINTERRUPTIBLE), so there is no need to check the result.
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400157 */
Weston Andros Adamsone7029202014-07-17 20:42:15 -0400158int
Weston Andros Adamsonfd2f3a02014-08-08 11:00:53 -0400159nfs_page_group_lock(struct nfs_page *req, bool nonblock)
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400160{
161 struct nfs_page *head = req->wb_head;
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400162
163 WARN_ON_ONCE(head != head->wb_head);
164
Weston Andros Adamsonbc8a3092014-08-08 11:00:54 -0400165 if (!test_and_set_bit(PG_HEADLOCK, &head->wb_flags))
166 return 0;
Weston Andros Adamsone7029202014-07-17 20:42:15 -0400167
Weston Andros Adamsonbc8a3092014-08-08 11:00:54 -0400168 if (!nonblock)
169 return wait_on_bit_lock(&head->wb_flags, PG_HEADLOCK,
170 TASK_UNINTERRUPTIBLE);
171
172 return -EAGAIN;
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400173}
174
175/*
Weston Andros Adamson7c3af972014-08-08 11:00:57 -0400176 * nfs_page_group_lock_wait - wait for the lock to clear, but don't grab it
177 * @req - a request in the group
178 *
179 * This is a blocking call to wait for the group lock to be cleared.
180 */
181void
182nfs_page_group_lock_wait(struct nfs_page *req)
183{
184 struct nfs_page *head = req->wb_head;
185
186 WARN_ON_ONCE(head != head->wb_head);
187
188 wait_on_bit(&head->wb_flags, PG_HEADLOCK,
189 TASK_UNINTERRUPTIBLE);
190}
191
192/*
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400193 * nfs_page_group_unlock - unlock the head of the page group
194 * @req - request in group that is to be unlocked
195 */
196void
197nfs_page_group_unlock(struct nfs_page *req)
198{
199 struct nfs_page *head = req->wb_head;
200
201 WARN_ON_ONCE(head != head->wb_head);
202
Linus Torvaldsd1e1cda2014-06-10 15:02:42 -0700203 smp_mb__before_atomic();
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400204 clear_bit(PG_HEADLOCK, &head->wb_flags);
Linus Torvaldsd1e1cda2014-06-10 15:02:42 -0700205 smp_mb__after_atomic();
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400206 wake_up_bit(&head->wb_flags, PG_HEADLOCK);
207}
208
209/*
210 * nfs_page_group_sync_on_bit_locked
211 *
212 * must be called with page group lock held
213 */
214static bool
215nfs_page_group_sync_on_bit_locked(struct nfs_page *req, unsigned int bit)
216{
217 struct nfs_page *head = req->wb_head;
218 struct nfs_page *tmp;
219
220 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_flags));
221 WARN_ON_ONCE(test_and_set_bit(bit, &req->wb_flags));
222
223 tmp = req->wb_this_page;
224 while (tmp != req) {
225 if (!test_bit(bit, &tmp->wb_flags))
226 return false;
227 tmp = tmp->wb_this_page;
228 }
229
230 /* true! reset all bits */
231 tmp = req;
232 do {
233 clear_bit(bit, &tmp->wb_flags);
234 tmp = tmp->wb_this_page;
235 } while (tmp != req);
236
237 return true;
238}
239
240/*
241 * nfs_page_group_sync_on_bit - set bit on current request, but only
242 * return true if the bit is set for all requests in page group
243 * @req - request in page group
244 * @bit - PG_* bit that is used to sync page group
245 */
246bool nfs_page_group_sync_on_bit(struct nfs_page *req, unsigned int bit)
247{
248 bool ret;
249
Weston Andros Adamsonfd2f3a02014-08-08 11:00:53 -0400250 nfs_page_group_lock(req, false);
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400251 ret = nfs_page_group_sync_on_bit_locked(req, bit);
252 nfs_page_group_unlock(req);
253
254 return ret;
255}
256
257/*
258 * nfs_page_group_init - Initialize the page group linkage for @req
259 * @req - a new nfs request
260 * @prev - the previous request in page group, or NULL if @req is the first
261 * or only request in the group (the head).
262 */
263static inline void
264nfs_page_group_init(struct nfs_page *req, struct nfs_page *prev)
265{
Weston Andros Adamsoncb1410c2014-11-12 12:08:00 -0500266 struct inode *inode;
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400267 WARN_ON_ONCE(prev == req);
268
269 if (!prev) {
Weston Andros Adamson85710a82014-07-11 10:20:46 -0400270 /* a head request */
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400271 req->wb_head = req;
272 req->wb_this_page = req;
273 } else {
Weston Andros Adamson85710a82014-07-11 10:20:46 -0400274 /* a subrequest */
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400275 WARN_ON_ONCE(prev->wb_this_page != prev->wb_head);
276 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &prev->wb_head->wb_flags));
277 req->wb_head = prev->wb_head;
278 req->wb_this_page = prev->wb_this_page;
279 prev->wb_this_page = req;
280
Weston Andros Adamson85710a82014-07-11 10:20:46 -0400281 /* All subrequests take a ref on the head request until
282 * nfs_page_group_destroy is called */
283 kref_get(&req->wb_head->wb_kref);
284
Weston Andros Adamsoncb1410c2014-11-12 12:08:00 -0500285 /* grab extra ref and bump the request count if head request
286 * has extra ref from the write/commit path to handle handoff
287 * between write and commit lists. */
Weston Andros Adamson17089a22014-07-11 10:20:45 -0400288 if (test_bit(PG_INODE_REF, &prev->wb_head->wb_flags)) {
Weston Andros Adamsoncb1410c2014-11-12 12:08:00 -0500289 inode = page_file_mapping(req->wb_page)->host;
Weston Andros Adamson17089a22014-07-11 10:20:45 -0400290 set_bit(PG_INODE_REF, &req->wb_flags);
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400291 kref_get(&req->wb_kref);
Weston Andros Adamsoncb1410c2014-11-12 12:08:00 -0500292 spin_lock(&inode->i_lock);
293 NFS_I(inode)->nrequests++;
294 spin_unlock(&inode->i_lock);
Weston Andros Adamson17089a22014-07-11 10:20:45 -0400295 }
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400296 }
297}
298
299/*
300 * nfs_page_group_destroy - sync the destruction of page groups
301 * @req - request that no longer needs the page group
302 *
303 * releases the page group reference from each member once all
304 * members have called this function.
305 */
306static void
307nfs_page_group_destroy(struct kref *kref)
308{
309 struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
310 struct nfs_page *tmp, *next;
311
Weston Andros Adamson85710a82014-07-11 10:20:46 -0400312 /* subrequests must release the ref on the head request */
313 if (req->wb_head != req)
314 nfs_release_request(req->wb_head);
315
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400316 if (!nfs_page_group_sync_on_bit(req, PG_TEARDOWN))
317 return;
318
319 tmp = req;
320 do {
321 next = tmp->wb_this_page;
322 /* unlink and free */
323 tmp->wb_this_page = tmp;
324 tmp->wb_head = tmp;
325 nfs_free_request(tmp);
326 tmp = next;
327 } while (tmp != req);
328}
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330/**
331 * nfs_create_request - Create an NFS read/write request.
Chuck Leverc02f5572011-10-25 12:17:43 -0400332 * @ctx: open context to use
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 * @page: page to write
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400334 * @last: last nfs request created for this page group or NULL if head
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 * @offset: starting offset within the page for the write
336 * @count: number of bytes to read/write
337 *
338 * The page must be locked by the caller. This makes sure we never
Jason Uhlenkotta19b89c2007-04-26 17:25:51 -0700339 * create two different requests for the same page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 * User should ensure it is safe to sleep in this function.
341 */
342struct nfs_page *
Weston Andros Adamson8c8f1ac12014-05-15 11:56:42 -0400343nfs_create_request(struct nfs_open_context *ctx, struct page *page,
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400344 struct nfs_page *last, unsigned int offset,
345 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct nfs_page *req;
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400348 struct nfs_lock_context *l_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Trond Myklebustc58c8442013-03-18 19:45:14 -0400350 if (test_bit(NFS_CONTEXT_BAD, &ctx->flags))
351 return ERR_PTR(-EBADF);
Trond Myklebust18eb8842010-05-13 12:51:02 -0400352 /* try to allocate the request struct */
353 req = nfs_page_alloc();
354 if (req == NULL)
355 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Jeff Layton015f0212010-10-28 10:10:37 -0400357 /* get lock context early so we can deal with alloc failures */
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400358 l_ctx = nfs_get_lock_context(ctx);
359 if (IS_ERR(l_ctx)) {
Jeff Layton015f0212010-10-28 10:10:37 -0400360 nfs_page_free(req);
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400361 return ERR_CAST(l_ctx);
Jeff Layton015f0212010-10-28 10:10:37 -0400362 }
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400363 req->wb_lock_context = l_ctx;
Trond Myklebust577b4232013-04-08 21:38:12 -0400364 nfs_iocounter_inc(&l_ctx->io_count);
Jeff Layton015f0212010-10-28 10:10:37 -0400365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /* Initialize the request struct. Initially, we assume a
367 * long write-back delay. This will be adjusted in
368 * update_nfs_request below if the region is not locked. */
369 req->wb_page = page;
Mel Gormand56b4dd2012-07-31 16:45:06 -0700370 req->wb_index = page_file_index(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 page_cache_get(page);
372 req->wb_offset = offset;
373 req->wb_pgbase = offset;
374 req->wb_bytes = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 req->wb_context = get_nfs_open_context(ctx);
Trond Myklebustc03b4022007-06-17 13:26:38 -0400376 kref_init(&req->wb_kref);
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400377 nfs_page_group_init(req, last);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return req;
379}
380
381/**
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400382 * nfs_unlock_request - Unlock request and wake up sleepers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 * @req:
384 */
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400385void nfs_unlock_request(struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 if (!NFS_WBACK_BUSY(req)) {
388 printk(KERN_ERR "NFS: Invalid unlock attempted\n");
389 BUG();
390 }
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100391 smp_mb__before_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 clear_bit(PG_BUSY, &req->wb_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100393 smp_mb__after_atomic();
Trond Myklebust464a98b2005-06-22 17:16:21 +0000394 wake_up_bit(&req->wb_flags, PG_BUSY);
Trond Myklebust3aff4eb2012-05-09 14:30:35 -0400395}
396
397/**
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400398 * nfs_unlock_and_release_request - Unlock request and release the nfs_page
399 * @req:
Trond Myklebust3aff4eb2012-05-09 14:30:35 -0400400 */
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400401void nfs_unlock_and_release_request(struct nfs_page *req)
Trond Myklebust3aff4eb2012-05-09 14:30:35 -0400402{
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400403 nfs_unlock_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 nfs_release_request(req);
405}
406
Trond Myklebust4d65c522011-03-25 14:15:11 -0400407/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 * nfs_clear_request - Free up all resources allocated to the request
409 * @req:
410 *
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500411 * Release page and open context resources associated with a read/write
412 * request after it has completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 */
Trond Myklebust4d65c522011-03-25 14:15:11 -0400414static void nfs_clear_request(struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500416 struct page *page = req->wb_page;
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500417 struct nfs_open_context *ctx = req->wb_context;
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400418 struct nfs_lock_context *l_ctx = req->wb_lock_context;
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500419
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500420 if (page != NULL) {
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500421 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 req->wb_page = NULL;
423 }
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400424 if (l_ctx != NULL) {
Trond Myklebust577b4232013-04-08 21:38:12 -0400425 nfs_iocounter_dec(&l_ctx->io_count);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400426 nfs_put_lock_context(l_ctx);
427 req->wb_lock_context = NULL;
428 }
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500429 if (ctx != NULL) {
430 put_nfs_open_context(ctx);
431 req->wb_context = NULL;
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/**
436 * nfs_release_request - Release the count on an NFS read/write request
437 * @req: request to release
438 *
439 * Note: Should never be called with the spinlock held!
440 */
Weston Andros Adamsond4581382014-07-11 10:20:48 -0400441void nfs_free_request(struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400443 WARN_ON_ONCE(req->wb_this_page != req);
444
445 /* extra debug: make sure no sync bits are still set */
446 WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
Weston Andros Adamson67d03382014-05-15 11:56:46 -0400447 WARN_ON_ONCE(test_bit(PG_UNLOCKPAGE, &req->wb_flags));
448 WARN_ON_ONCE(test_bit(PG_UPTODATE, &req->wb_flags));
Weston Andros Adamson20633f02014-05-15 11:56:47 -0400449 WARN_ON_ONCE(test_bit(PG_WB_END, &req->wb_flags));
450 WARN_ON_ONCE(test_bit(PG_REMOVE, &req->wb_flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500452 /* Release struct file and open context */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 nfs_clear_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 nfs_page_free(req);
455}
456
Trond Myklebustc03b4022007-06-17 13:26:38 -0400457void nfs_release_request(struct nfs_page *req)
458{
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -0400459 kref_put(&req->wb_kref, nfs_page_group_destroy);
Trond Myklebust9f557cd2010-02-03 08:27:22 -0500460}
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462/**
463 * nfs_wait_on_request - Wait for a request to complete.
464 * @req: request to wait upon.
465 *
Matthew Wilcox150030b2007-12-06 16:24:39 -0500466 * Interruptible by fatal signals only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 * The user is responsible for holding a count on the request.
468 */
469int
470nfs_wait_on_request(struct nfs_page *req)
471{
NeilBrown74316202014-07-07 15:16:04 +1000472 return wait_on_bit_io(&req->wb_flags, PG_BUSY,
473 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400476/*
477 * nfs_generic_pg_test - determine if requests can be coalesced
478 * @desc: pointer to descriptor
479 * @prev: previous request in desc, or NULL
480 * @req: this request
481 *
482 * Returns zero if @req can be coalesced into @desc, otherwise it returns
483 * the size of the request.
484 */
485size_t nfs_generic_pg_test(struct nfs_pageio_descriptor *desc,
486 struct nfs_page *prev, struct nfs_page *req)
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300487{
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -0400488 struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[desc->pg_mirror_idx];
489
490
491 if (mirror->pg_count > mirror->pg_bsize) {
Weston Andros Adamsonf0cb9ab2014-05-15 11:56:52 -0400492 /* should never happen */
493 WARN_ON_ONCE(1);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300494 return 0;
Weston Andros Adamsonf0cb9ab2014-05-15 11:56:52 -0400495 }
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300496
Christoph Hellwig2e11f822014-08-21 11:09:17 -0500497 /*
498 * Limit the request size so that we can still allocate a page array
499 * for it without upsetting the slab allocator.
500 */
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -0400501 if (((mirror->pg_count + req->wb_bytes) >> PAGE_SHIFT) *
Christoph Hellwig2e11f822014-08-21 11:09:17 -0500502 sizeof(struct page) > PAGE_SIZE)
503 return 0;
504
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -0400505 return min(mirror->pg_bsize - mirror->pg_count, (size_t)req->wb_bytes);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300506}
Benny Halevy19345cb2011-06-19 18:33:46 -0400507EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300508
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -0400509struct nfs_pgio_header *nfs_pgio_header_alloc(const struct nfs_rw_ops *ops)
Anna Schumaker00bfa302014-05-06 09:12:29 -0400510{
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -0400511 struct nfs_pgio_header *hdr = ops->rw_alloc_header();
Anna Schumaker00bfa302014-05-06 09:12:29 -0400512
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -0400513 if (hdr) {
Anna Schumaker4a0de552014-05-06 09:12:30 -0400514 INIT_LIST_HEAD(&hdr->pages);
Anna Schumaker4a0de552014-05-06 09:12:30 -0400515 spin_lock_init(&hdr->lock);
Anna Schumaker4a0de552014-05-06 09:12:30 -0400516 hdr->rw_ops = ops;
517 }
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -0400518 return hdr;
Anna Schumaker4a0de552014-05-06 09:12:30 -0400519}
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -0400520EXPORT_SYMBOL_GPL(nfs_pgio_header_alloc);
Anna Schumaker4a0de552014-05-06 09:12:30 -0400521
522/*
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -0400523 * nfs_pgio_header_free - Free a read or write header
Anna Schumaker4a0de552014-05-06 09:12:30 -0400524 * @hdr: The header to free
525 */
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -0400526void nfs_pgio_header_free(struct nfs_pgio_header *hdr)
Anna Schumaker4a0de552014-05-06 09:12:30 -0400527{
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -0400528 hdr->rw_ops->rw_free_header(hdr);
Anna Schumaker4a0de552014-05-06 09:12:30 -0400529}
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -0400530EXPORT_SYMBOL_GPL(nfs_pgio_header_free);
Anna Schumaker4a0de552014-05-06 09:12:30 -0400531
532/**
Weston Andros Adamson4714fb52014-06-09 11:48:37 -0400533 * nfs_pgio_data_destroy - make @hdr suitable for reuse
534 *
535 * Frees memory and releases refs from nfs_generic_pgio, so that it may
536 * be called again.
537 *
538 * @hdr: A header that has had nfs_generic_pgio called
Anna Schumaker00bfa302014-05-06 09:12:29 -0400539 */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400540void nfs_pgio_data_destroy(struct nfs_pgio_header *hdr)
Anna Schumaker00bfa302014-05-06 09:12:29 -0400541{
Trond Myklebust3caa0c62014-10-13 10:26:43 -0400542 if (hdr->args.context)
543 put_nfs_open_context(hdr->args.context);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400544 if (hdr->page_array.pagevec != hdr->page_array.page_array)
545 kfree(hdr->page_array.pagevec);
Anna Schumaker00bfa302014-05-06 09:12:29 -0400546}
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -0400547EXPORT_SYMBOL_GPL(nfs_pgio_data_destroy);
Anna Schumaker00bfa302014-05-06 09:12:29 -0400548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549/**
Anna Schumakerce595152014-05-06 09:12:34 -0400550 * nfs_pgio_rpcsetup - Set up arguments for a pageio call
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400551 * @hdr: The pageio hdr
Anna Schumakerce595152014-05-06 09:12:34 -0400552 * @count: Number of bytes to read
553 * @offset: Initial offset
554 * @how: How to commit data (writes only)
555 * @cinfo: Commit information for the call (writes only)
556 */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400557static void nfs_pgio_rpcsetup(struct nfs_pgio_header *hdr,
Anna Schumakerce595152014-05-06 09:12:34 -0400558 unsigned int count, unsigned int offset,
559 int how, struct nfs_commit_info *cinfo)
560{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400561 struct nfs_page *req = hdr->req;
Anna Schumakerce595152014-05-06 09:12:34 -0400562
563 /* Set up the RPC argument and reply structs
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400564 * NB: take care not to mess about with hdr->commit et al. */
Anna Schumakerce595152014-05-06 09:12:34 -0400565
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400566 hdr->args.fh = NFS_FH(hdr->inode);
567 hdr->args.offset = req_offset(req) + offset;
Anna Schumakerce595152014-05-06 09:12:34 -0400568 /* pnfs_set_layoutcommit needs this */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400569 hdr->mds_offset = hdr->args.offset;
570 hdr->args.pgbase = req->wb_pgbase + offset;
571 hdr->args.pages = hdr->page_array.pagevec;
572 hdr->args.count = count;
573 hdr->args.context = get_nfs_open_context(req->wb_context);
574 hdr->args.lock_context = req->wb_lock_context;
575 hdr->args.stable = NFS_UNSTABLE;
Anna Schumakerce595152014-05-06 09:12:34 -0400576 switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
577 case 0:
578 break;
579 case FLUSH_COND_STABLE:
580 if (nfs_reqs_to_commit(cinfo))
581 break;
582 default:
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400583 hdr->args.stable = NFS_FILE_SYNC;
Anna Schumakerce595152014-05-06 09:12:34 -0400584 }
585
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400586 hdr->res.fattr = &hdr->fattr;
587 hdr->res.count = count;
588 hdr->res.eof = 0;
Weston Andros Adamsonc65e6252014-06-09 11:48:36 -0400589 hdr->res.verf = &hdr->verf;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400590 nfs_fattr_init(&hdr->fattr);
Anna Schumakerce595152014-05-06 09:12:34 -0400591}
592
593/**
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400594 * nfs_pgio_prepare - Prepare pageio hdr to go over the wire
Anna Schumakera4cdda52014-05-06 09:12:31 -0400595 * @task: The current task
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400596 * @calldata: pageio header to prepare
Anna Schumakera4cdda52014-05-06 09:12:31 -0400597 */
Anna Schumaker6f92fa42014-05-06 09:12:33 -0400598static void nfs_pgio_prepare(struct rpc_task *task, void *calldata)
Anna Schumakera4cdda52014-05-06 09:12:31 -0400599{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400600 struct nfs_pgio_header *hdr = calldata;
Anna Schumakera4cdda52014-05-06 09:12:31 -0400601 int err;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400602 err = NFS_PROTO(hdr->inode)->pgio_rpc_prepare(task, hdr);
Anna Schumakera4cdda52014-05-06 09:12:31 -0400603 if (err)
604 rpc_exit(task, err);
605}
606
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400607int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr,
Peng Tao46a5ab42014-06-13 23:02:25 +0800608 struct rpc_cred *cred, const struct nfs_rpc_ops *rpc_ops,
Anna Schumaker1ed26f32014-05-06 09:12:37 -0400609 const struct rpc_call_ops *call_ops, int how, int flags)
610{
611 struct rpc_task *task;
612 struct rpc_message msg = {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400613 .rpc_argp = &hdr->args,
614 .rpc_resp = &hdr->res,
Peng Tao46a5ab42014-06-13 23:02:25 +0800615 .rpc_cred = cred,
Anna Schumaker1ed26f32014-05-06 09:12:37 -0400616 };
617 struct rpc_task_setup task_setup_data = {
618 .rpc_client = clnt,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400619 .task = &hdr->task,
Anna Schumaker1ed26f32014-05-06 09:12:37 -0400620 .rpc_message = &msg,
621 .callback_ops = call_ops,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400622 .callback_data = hdr,
Anna Schumaker1ed26f32014-05-06 09:12:37 -0400623 .workqueue = nfsiod_workqueue,
624 .flags = RPC_TASK_ASYNC | flags,
625 };
626 int ret = 0;
627
Tom Haynesabde71f2014-06-09 13:12:20 -0700628 hdr->rw_ops->rw_initiate(hdr, &msg, rpc_ops, &task_setup_data, how);
Anna Schumaker1ed26f32014-05-06 09:12:37 -0400629
630 dprintk("NFS: %5u initiated pgio call "
631 "(req %s/%llu, %u bytes @ offset %llu)\n",
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400632 hdr->task.tk_pid,
Anna Schumaker343ae532014-06-20 13:30:26 -0400633 hdr->inode->i_sb->s_id,
634 (unsigned long long)NFS_FILEID(hdr->inode),
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400635 hdr->args.count,
636 (unsigned long long)hdr->args.offset);
Anna Schumaker1ed26f32014-05-06 09:12:37 -0400637
638 task = rpc_run_task(&task_setup_data);
639 if (IS_ERR(task)) {
640 ret = PTR_ERR(task);
641 goto out;
642 }
643 if (how & FLUSH_SYNC) {
644 ret = rpc_wait_for_completion_task(task);
645 if (ret == 0)
646 ret = task->tk_status;
647 }
648 rpc_put_task(task);
649out:
650 return ret;
651}
652EXPORT_SYMBOL_GPL(nfs_initiate_pgio);
653
Anna Schumakera4cdda52014-05-06 09:12:31 -0400654/**
Anna Schumaker844c9e62014-05-06 09:12:35 -0400655 * nfs_pgio_error - Clean up from a pageio error
656 * @desc: IO descriptor
657 * @hdr: pageio header
658 */
Anna Schumakeref2c4882014-05-06 09:12:36 -0400659static int nfs_pgio_error(struct nfs_pageio_descriptor *desc,
Anna Schumaker844c9e62014-05-06 09:12:35 -0400660 struct nfs_pgio_header *hdr)
661{
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -0400662 struct nfs_pgio_mirror *mirror;
663 u32 midx;
664
Anna Schumaker844c9e62014-05-06 09:12:35 -0400665 set_bit(NFS_IOHDR_REDO, &hdr->flags);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400666 nfs_pgio_data_destroy(hdr);
Weston Andros Adamson4714fb52014-06-09 11:48:37 -0400667 hdr->completion_ops->completion(hdr);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -0400668 /* TODO: Make sure it's right to clean up all mirrors here
669 * and not just hdr->pgio_mirror_idx */
670 for (midx = 0; midx < desc->pg_mirror_count; midx++) {
671 mirror = &desc->pg_mirrors[midx];
672 desc->pg_completion_ops->error_cleanup(&mirror->pg_list);
673 }
Anna Schumaker844c9e62014-05-06 09:12:35 -0400674 return -ENOMEM;
675}
676
677/**
Anna Schumakera4cdda52014-05-06 09:12:31 -0400678 * nfs_pgio_release - Release pageio data
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400679 * @calldata: The pageio header to release
Anna Schumakera4cdda52014-05-06 09:12:31 -0400680 */
Anna Schumaker6f92fa42014-05-06 09:12:33 -0400681static void nfs_pgio_release(void *calldata)
Anna Schumakera4cdda52014-05-06 09:12:31 -0400682{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400683 struct nfs_pgio_header *hdr = calldata;
684 if (hdr->rw_ops->rw_release)
685 hdr->rw_ops->rw_release(hdr);
686 nfs_pgio_data_destroy(hdr);
Weston Andros Adamson4714fb52014-06-09 11:48:37 -0400687 hdr->completion_ops->completion(hdr);
Anna Schumakera4cdda52014-05-06 09:12:31 -0400688}
689
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -0400690static void nfs_pageio_mirror_init(struct nfs_pgio_mirror *mirror,
691 unsigned int bsize)
692{
693 INIT_LIST_HEAD(&mirror->pg_list);
694 mirror->pg_bytes_written = 0;
695 mirror->pg_count = 0;
696 mirror->pg_bsize = bsize;
697 mirror->pg_base = 0;
698 mirror->pg_recoalesce = 0;
699}
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701/**
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400702 * nfs_pageio_init - initialise a page io descriptor
703 * @desc: pointer to descriptor
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400704 * @inode: pointer to inode
705 * @doio: pointer to io function
706 * @bsize: io block size
707 * @io_flags: extra parameters for the io function
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400708 */
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400709void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
710 struct inode *inode,
Trond Myklebust1751c362011-06-10 13:30:23 -0400711 const struct nfs_pageio_ops *pg_ops,
Fred Isaman061ae2e2012-04-20 14:47:48 -0400712 const struct nfs_pgio_completion_ops *compl_ops,
Anna Schumaker4a0de552014-05-06 09:12:30 -0400713 const struct nfs_rw_ops *rw_ops,
Trond Myklebust84dde762007-05-04 14:44:06 -0400714 size_t bsize,
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400715 int io_flags)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400716{
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -0400717 struct nfs_pgio_mirror *new;
718 int i;
719
Trond Myklebustb31268a2011-03-21 17:02:00 -0400720 desc->pg_moreio = 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400721 desc->pg_inode = inode;
Trond Myklebust1751c362011-06-10 13:30:23 -0400722 desc->pg_ops = pg_ops;
Fred Isaman061ae2e2012-04-20 14:47:48 -0400723 desc->pg_completion_ops = compl_ops;
Anna Schumaker4a0de552014-05-06 09:12:30 -0400724 desc->pg_rw_ops = rw_ops;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400725 desc->pg_ioflags = io_flags;
726 desc->pg_error = 0;
Fred Isaman94ad1c82011-03-01 01:34:14 +0000727 desc->pg_lseg = NULL;
Fred Isaman584aa812012-04-20 14:47:51 -0400728 desc->pg_dreq = NULL;
Peng Taof6166382012-08-02 15:36:09 +0300729 desc->pg_layout_private = NULL;
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -0400730 desc->pg_bsize = bsize;
731
732 desc->pg_mirror_count = 1;
733 desc->pg_mirror_idx = 0;
734
735 if (pg_ops->pg_get_mirror_count) {
736 /* until we have a request, we don't have an lseg and no
737 * idea how many mirrors there will be */
738 new = kcalloc(NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX,
739 sizeof(struct nfs_pgio_mirror), GFP_KERNEL);
740 desc->pg_mirrors_dynamic = new;
741 desc->pg_mirrors = new;
742
743 for (i = 0; i < NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX; i++)
744 nfs_pageio_mirror_init(&desc->pg_mirrors[i], bsize);
745 } else {
746 desc->pg_mirrors_dynamic = NULL;
747 desc->pg_mirrors = desc->pg_mirrors_static;
748 nfs_pageio_mirror_init(&desc->pg_mirrors[0], bsize);
749 }
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400750}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400751EXPORT_SYMBOL_GPL(nfs_pageio_init);
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400752
Anna Schumaker0eecb212014-05-06 09:12:32 -0400753/**
754 * nfs_pgio_result - Basic pageio error handling
755 * @task: The task that ran
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400756 * @calldata: Pageio header to check
Anna Schumaker0eecb212014-05-06 09:12:32 -0400757 */
Anna Schumaker6f92fa42014-05-06 09:12:33 -0400758static void nfs_pgio_result(struct rpc_task *task, void *calldata)
Anna Schumaker0eecb212014-05-06 09:12:32 -0400759{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400760 struct nfs_pgio_header *hdr = calldata;
761 struct inode *inode = hdr->inode;
Anna Schumaker0eecb212014-05-06 09:12:32 -0400762
763 dprintk("NFS: %s: %5u, (status %d)\n", __func__,
764 task->tk_pid, task->tk_status);
765
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400766 if (hdr->rw_ops->rw_done(task, hdr, inode) != 0)
Anna Schumaker0eecb212014-05-06 09:12:32 -0400767 return;
768 if (task->tk_status < 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400769 nfs_set_pgio_error(hdr, task->tk_status, hdr->args.offset);
Anna Schumaker0eecb212014-05-06 09:12:32 -0400770 else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400771 hdr->rw_ops->rw_result(task, hdr);
Anna Schumaker0eecb212014-05-06 09:12:32 -0400772}
773
Anna Schumakeref2c4882014-05-06 09:12:36 -0400774/*
Anna Schumakeref2c4882014-05-06 09:12:36 -0400775 * Create an RPC task for the given read or write request and kick it.
776 * The page must have been locked by the caller.
777 *
778 * It may happen that the page we're passed is not marked dirty.
779 * This is the case if nfs_updatepage detects a conflicting request
780 * that has been written but not committed.
781 */
Weston Andros Adamsonf0cb9ab2014-05-15 11:56:52 -0400782int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
783 struct nfs_pgio_header *hdr)
Anna Schumakeref2c4882014-05-06 09:12:36 -0400784{
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -0400785 struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[desc->pg_mirror_idx];
786
Anna Schumakeref2c4882014-05-06 09:12:36 -0400787 struct nfs_page *req;
Weston Andros Adamsonbba5c182014-08-14 17:39:32 -0400788 struct page **pages,
789 *last_page;
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -0400790 struct list_head *head = &mirror->pg_list;
Anna Schumakeref2c4882014-05-06 09:12:36 -0400791 struct nfs_commit_info cinfo;
Weston Andros Adamsonbba5c182014-08-14 17:39:32 -0400792 unsigned int pagecount, pageused;
Anna Schumakeref2c4882014-05-06 09:12:36 -0400793
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -0400794 pagecount = nfs_page_array_len(mirror->pg_base, mirror->pg_count);
Weston Andros Adamson4714fb52014-06-09 11:48:37 -0400795 if (!nfs_pgarray_set(&hdr->page_array, pagecount))
Anna Schumakeref2c4882014-05-06 09:12:36 -0400796 return nfs_pgio_error(desc, hdr);
797
798 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400799 pages = hdr->page_array.pagevec;
Weston Andros Adamsonbba5c182014-08-14 17:39:32 -0400800 last_page = NULL;
801 pageused = 0;
Anna Schumakeref2c4882014-05-06 09:12:36 -0400802 while (!list_empty(head)) {
803 req = nfs_list_entry(head->next);
804 nfs_list_remove_request(req);
805 nfs_list_add_request(req, &hdr->pages);
Weston Andros Adamsonbba5c182014-08-14 17:39:32 -0400806
Weston Andros Adamsonbba5c182014-08-14 17:39:32 -0400807 if (!last_page || last_page != req->wb_page) {
Weston Andros Adamsonbba5c182014-08-14 17:39:32 -0400808 pageused++;
Trond Myklebustb8fb9c302014-10-13 10:56:12 -0400809 if (pageused > pagecount)
810 break;
811 *pages++ = last_page = req->wb_page;
Weston Andros Adamsonbba5c182014-08-14 17:39:32 -0400812 }
Anna Schumakeref2c4882014-05-06 09:12:36 -0400813 }
Weston Andros Adamsonbba5c182014-08-14 17:39:32 -0400814 if (WARN_ON_ONCE(pageused != pagecount))
815 return nfs_pgio_error(desc, hdr);
Anna Schumakeref2c4882014-05-06 09:12:36 -0400816
817 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
818 (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
819 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
820
821 /* Set up the argument struct */
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -0400822 nfs_pgio_rpcsetup(hdr, mirror->pg_count, 0, desc->pg_ioflags, &cinfo);
Anna Schumakeref2c4882014-05-06 09:12:36 -0400823 desc->pg_rpc_callops = &nfs_pgio_common_ops;
824 return 0;
825}
Weston Andros Adamsonf0cb9ab2014-05-15 11:56:52 -0400826EXPORT_SYMBOL_GPL(nfs_generic_pgio);
Anna Schumakeref2c4882014-05-06 09:12:36 -0400827
Anna Schumaker41d8d5b2014-05-06 09:12:40 -0400828static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
Anna Schumakercf485fc2014-05-06 09:12:39 -0400829{
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -0400830 struct nfs_pgio_mirror *mirror;
Anna Schumakercf485fc2014-05-06 09:12:39 -0400831 struct nfs_pgio_header *hdr;
832 int ret;
833
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -0400834 mirror = &desc->pg_mirrors[desc->pg_mirror_idx];
835
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -0400836 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
837 if (!hdr) {
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -0400838 /* TODO: make sure this is right with mirroring - or
839 * should it back out all mirrors? */
840 desc->pg_completion_ops->error_cleanup(&mirror->pg_list);
Anna Schumakercf485fc2014-05-06 09:12:39 -0400841 return -ENOMEM;
842 }
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -0400843 nfs_pgheader_init(desc, hdr, nfs_pgio_header_free);
Anna Schumakercf485fc2014-05-06 09:12:39 -0400844 ret = nfs_generic_pgio(desc, hdr);
845 if (ret == 0)
Weston Andros Adamson7f714722014-05-15 11:56:53 -0400846 ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode),
Peng Tao46a5ab42014-06-13 23:02:25 +0800847 hdr,
848 hdr->cred,
849 NFS_PROTO(hdr->inode),
Tom Haynesabde71f2014-06-09 13:12:20 -0700850 desc->pg_rpc_callops,
Weston Andros Adamson7f714722014-05-15 11:56:53 -0400851 desc->pg_ioflags, 0);
Anna Schumakercf485fc2014-05-06 09:12:39 -0400852 return ret;
853}
854
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -0400855/*
856 * nfs_pageio_setup_mirroring - determine if mirroring is to be used
857 * by calling the pg_get_mirror_count op
858 */
859static int nfs_pageio_setup_mirroring(struct nfs_pageio_descriptor *pgio,
860 struct nfs_page *req)
861{
862 int mirror_count = 1;
863
864 if (!pgio->pg_ops->pg_get_mirror_count)
865 return 0;
866
867 mirror_count = pgio->pg_ops->pg_get_mirror_count(pgio, req);
868
869 if (!mirror_count || mirror_count > NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX)
870 return -EINVAL;
871
872 if (WARN_ON_ONCE(!pgio->pg_mirrors_dynamic))
873 return -EINVAL;
874
875 pgio->pg_mirror_count = mirror_count;
876
877 return 0;
878}
879
880/*
881 * nfs_pageio_stop_mirroring - stop using mirroring (set mirror count to 1)
882 */
883void nfs_pageio_stop_mirroring(struct nfs_pageio_descriptor *pgio)
884{
885 pgio->pg_mirror_count = 1;
886 pgio->pg_mirror_idx = 0;
887}
888
889static void nfs_pageio_cleanup_mirroring(struct nfs_pageio_descriptor *pgio)
890{
891 pgio->pg_mirror_count = 1;
892 pgio->pg_mirror_idx = 0;
893 pgio->pg_mirrors = pgio->pg_mirrors_static;
894 kfree(pgio->pg_mirrors_dynamic);
895 pgio->pg_mirrors_dynamic = NULL;
896}
897
Trond Myklebust4109bb72013-09-06 11:09:38 -0400898static bool nfs_match_open_context(const struct nfs_open_context *ctx1,
899 const struct nfs_open_context *ctx2)
900{
901 return ctx1->cred == ctx2->cred && ctx1->state == ctx2->state;
902}
903
904static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
905 const struct nfs_lock_context *l2)
906{
907 return l1->lockowner.l_owner == l2->lockowner.l_owner
908 && l1->lockowner.l_pid == l2->lockowner.l_pid;
909}
910
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400911/**
912 * nfs_can_coalesce_requests - test two requests for compatibility
913 * @prev: pointer to nfs_page
914 * @req: pointer to nfs_page
915 *
916 * The nfs_page structures 'prev' and 'req' are compared to ensure that the
917 * page data area they describe is contiguous, and that their RPC
918 * credentials, NFSv4 open state, and lockowners are the same.
919 *
920 * Return 'true' if this is the case, else return 'false'.
921 */
Benny Halevy18ad0a92011-05-25 21:03:56 +0300922static bool nfs_can_coalesce_requests(struct nfs_page *prev,
923 struct nfs_page *req,
924 struct nfs_pageio_descriptor *pgio)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400925{
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400926 size_t size;
927
Weston Andros Adamsonab75e412014-05-15 11:56:44 -0400928 if (prev) {
929 if (!nfs_match_open_context(req->wb_context, prev->wb_context))
930 return false;
931 if (req->wb_context->dentry->d_inode->i_flock != NULL &&
932 !nfs_match_lock_context(req->wb_lock_context,
933 prev->wb_lock_context))
934 return false;
Weston Andros Adamsonab75e412014-05-15 11:56:44 -0400935 if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
936 return false;
Weston Andros Adamson78270e82014-08-14 17:39:33 -0400937 if (req->wb_page == prev->wb_page) {
938 if (req->wb_pgbase != prev->wb_pgbase + prev->wb_bytes)
939 return false;
940 } else {
941 if (req->wb_pgbase != 0 ||
942 prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
943 return false;
944 }
Weston Andros Adamsonab75e412014-05-15 11:56:44 -0400945 }
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400946 size = pgio->pg_ops->pg_test(pgio, prev, req);
Weston Andros Adamsonf0cb9ab2014-05-15 11:56:52 -0400947 WARN_ON_ONCE(size > req->wb_bytes);
948 if (size && size < req->wb_bytes)
949 req->wb_bytes = size;
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400950 return size > 0;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400951}
952
953/**
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400954 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400955 * @desc: destination io descriptor
956 * @req: request
957 *
958 * Returns true if the request 'req' was successfully coalesced into the
959 * existing list of pages 'desc'.
960 */
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400961static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
962 struct nfs_page *req)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400963{
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -0400964 struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[desc->pg_mirror_idx];
965
Weston Andros Adamsonab75e412014-05-15 11:56:44 -0400966 struct nfs_page *prev = NULL;
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -0400967
968 if (mirror->pg_count != 0) {
969 prev = nfs_list_entry(mirror->pg_list.prev);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300970 } else {
Trond Myklebustd8007d42011-06-10 13:30:23 -0400971 if (desc->pg_ops->pg_init)
972 desc->pg_ops->pg_init(desc, req);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -0400973 mirror->pg_base = req->wb_pgbase;
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300974 }
Weston Andros Adamsonab75e412014-05-15 11:56:44 -0400975 if (!nfs_can_coalesce_requests(prev, req, desc))
976 return 0;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400977 nfs_list_remove_request(req);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -0400978 nfs_list_add_request(req, &mirror->pg_list);
979 mirror->pg_count += req->wb_bytes;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400980 return 1;
981}
982
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400983/*
984 * Helper for nfs_pageio_add_request and nfs_pageio_complete
985 */
986static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
987{
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -0400988 struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[desc->pg_mirror_idx];
989
990
991 if (!list_empty(&mirror->pg_list)) {
Trond Myklebust1751c362011-06-10 13:30:23 -0400992 int error = desc->pg_ops->pg_doio(desc);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400993 if (error < 0)
994 desc->pg_error = error;
995 else
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -0400996 mirror->pg_bytes_written += mirror->pg_count;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400997 }
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -0400998 if (list_empty(&mirror->pg_list)) {
999 mirror->pg_count = 0;
1000 mirror->pg_base = 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -04001001 }
1002}
1003
1004/**
1005 * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
1006 * @desc: destination io descriptor
1007 * @req: request
1008 *
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -04001009 * This may split a request into subrequests which are all part of the
1010 * same page group.
1011 *
Trond Myklebustbcb71bb2007-04-02 18:48:28 -04001012 * Returns true if the request 'req' was successfully coalesced into the
1013 * existing list of pages 'desc'.
1014 */
Trond Myklebustd9156f92011-07-12 13:42:02 -04001015static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
Trond Myklebust8b09bee2007-04-02 18:48:28 -04001016 struct nfs_page *req)
Trond Myklebustbcb71bb2007-04-02 18:48:28 -04001017{
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001018 struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[desc->pg_mirror_idx];
1019
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -04001020 struct nfs_page *subreq;
1021 unsigned int bytes_left = 0;
1022 unsigned int offset, pgbase;
1023
Weston Andros Adamsonbfd484a2014-08-08 11:00:55 -04001024 nfs_page_group_lock(req, false);
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -04001025
1026 subreq = req;
1027 bytes_left = subreq->wb_bytes;
1028 offset = subreq->wb_offset;
1029 pgbase = subreq->wb_pgbase;
1030
1031 do {
1032 if (!nfs_pageio_do_add_request(desc, subreq)) {
1033 /* make sure pg_test call(s) did nothing */
1034 WARN_ON_ONCE(subreq->wb_bytes != bytes_left);
1035 WARN_ON_ONCE(subreq->wb_offset != offset);
1036 WARN_ON_ONCE(subreq->wb_pgbase != pgbase);
1037
1038 nfs_page_group_unlock(req);
1039 desc->pg_moreio = 1;
1040 nfs_pageio_doio(desc);
1041 if (desc->pg_error < 0)
1042 return 0;
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001043 if (mirror->pg_recoalesce)
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -04001044 return 0;
1045 /* retry add_request for this subreq */
Weston Andros Adamsonbfd484a2014-08-08 11:00:55 -04001046 nfs_page_group_lock(req, false);
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -04001047 continue;
1048 }
1049
1050 /* check for buggy pg_test call(s) */
1051 WARN_ON_ONCE(subreq->wb_bytes + subreq->wb_pgbase > PAGE_SIZE);
1052 WARN_ON_ONCE(subreq->wb_bytes > bytes_left);
1053 WARN_ON_ONCE(subreq->wb_bytes == 0);
1054
1055 bytes_left -= subreq->wb_bytes;
1056 offset += subreq->wb_bytes;
1057 pgbase += subreq->wb_bytes;
1058
1059 if (bytes_left) {
1060 subreq = nfs_create_request(req->wb_context,
1061 req->wb_page,
1062 subreq, pgbase, bytes_left);
Trond Myklebustc1109552014-05-29 11:38:15 -04001063 if (IS_ERR(subreq))
1064 goto err_ptr;
Weston Andros Adamson2bfc6e52014-05-15 11:56:45 -04001065 nfs_lock_request(subreq);
1066 subreq->wb_offset = offset;
1067 subreq->wb_index = req->wb_index;
1068 }
1069 } while (bytes_left > 0);
1070
1071 nfs_page_group_unlock(req);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -04001072 return 1;
Trond Myklebustc1109552014-05-29 11:38:15 -04001073err_ptr:
1074 desc->pg_error = PTR_ERR(subreq);
1075 nfs_page_group_unlock(req);
1076 return 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -04001077}
1078
Trond Myklebustd9156f92011-07-12 13:42:02 -04001079static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
1080{
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001081 struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[desc->pg_mirror_idx];
Trond Myklebustd9156f92011-07-12 13:42:02 -04001082 LIST_HEAD(head);
1083
1084 do {
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001085 list_splice_init(&mirror->pg_list, &head);
1086 mirror->pg_bytes_written -= mirror->pg_count;
1087 mirror->pg_count = 0;
1088 mirror->pg_base = 0;
1089 mirror->pg_recoalesce = 0;
1090
Trond Myklebustf563b892014-07-13 15:13:19 -04001091 desc->pg_moreio = 0;
Trond Myklebustd9156f92011-07-12 13:42:02 -04001092
1093 while (!list_empty(&head)) {
1094 struct nfs_page *req;
1095
1096 req = list_first_entry(&head, struct nfs_page, wb_list);
1097 nfs_list_remove_request(req);
1098 if (__nfs_pageio_add_request(desc, req))
1099 continue;
1100 if (desc->pg_error < 0)
1101 return 0;
1102 break;
1103 }
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001104 } while (mirror->pg_recoalesce);
Trond Myklebustd9156f92011-07-12 13:42:02 -04001105 return 1;
1106}
1107
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001108static int nfs_pageio_add_request_mirror(struct nfs_pageio_descriptor *desc,
Trond Myklebustd9156f92011-07-12 13:42:02 -04001109 struct nfs_page *req)
1110{
1111 int ret;
1112
1113 do {
1114 ret = __nfs_pageio_add_request(desc, req);
1115 if (ret)
1116 break;
1117 if (desc->pg_error < 0)
1118 break;
1119 ret = nfs_do_recoalesce(desc);
1120 } while (ret);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001121
Trond Myklebustd9156f92011-07-12 13:42:02 -04001122 return ret;
1123}
1124
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001125int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
1126 struct nfs_page *req)
1127{
1128 u32 midx;
1129 unsigned int pgbase, offset, bytes;
1130 struct nfs_page *dupreq, *lastreq;
1131
1132 pgbase = req->wb_pgbase;
1133 offset = req->wb_offset;
1134 bytes = req->wb_bytes;
1135
1136 nfs_pageio_setup_mirroring(desc, req);
1137
1138 for (midx = 0; midx < desc->pg_mirror_count; midx++) {
1139 if (midx) {
1140 nfs_page_group_lock(req, false);
1141
1142 /* find the last request */
1143 for (lastreq = req->wb_head;
1144 lastreq->wb_this_page != req->wb_head;
1145 lastreq = lastreq->wb_this_page)
1146 ;
1147
1148 dupreq = nfs_create_request(req->wb_context,
1149 req->wb_page, lastreq, pgbase, bytes);
1150
1151 if (IS_ERR(dupreq)) {
1152 nfs_page_group_unlock(req);
1153 return 0;
1154 }
1155
1156 nfs_lock_request(dupreq);
1157 nfs_page_group_unlock(req);
1158 dupreq->wb_offset = offset;
1159 dupreq->wb_index = req->wb_index;
1160 } else
1161 dupreq = req;
1162
Peng Tao47af81f2014-11-10 08:35:34 +08001163 if (nfs_pgio_has_mirroring(desc))
1164 desc->pg_mirror_idx = midx;
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001165 if (!nfs_pageio_add_request_mirror(desc, dupreq))
1166 return 0;
1167 }
1168
1169 return 1;
1170}
1171
1172/*
1173 * nfs_pageio_complete_mirror - Complete I/O on the current mirror of an
1174 * nfs_pageio_descriptor
1175 * @desc: pointer to io descriptor
1176 */
1177static void nfs_pageio_complete_mirror(struct nfs_pageio_descriptor *desc,
1178 u32 mirror_idx)
1179{
1180 struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[mirror_idx];
1181 u32 restore_idx = desc->pg_mirror_idx;
1182
Peng Tao47af81f2014-11-10 08:35:34 +08001183 if (nfs_pgio_has_mirroring(desc))
1184 desc->pg_mirror_idx = mirror_idx;
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001185 for (;;) {
1186 nfs_pageio_doio(desc);
1187 if (!mirror->pg_recoalesce)
1188 break;
1189 if (!nfs_do_recoalesce(desc))
1190 break;
1191 }
1192 desc->pg_mirror_idx = restore_idx;
1193}
1194
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04001195/*
1196 * nfs_pageio_resend - Transfer requests to new descriptor and resend
1197 * @hdr - the pgio header to move request from
1198 * @desc - the pageio descriptor to add requests to
1199 *
1200 * Try to move each request (nfs_page) from @hdr to @desc then attempt
1201 * to send them.
1202 *
1203 * Returns 0 on success and < 0 on error.
1204 */
1205int nfs_pageio_resend(struct nfs_pageio_descriptor *desc,
1206 struct nfs_pgio_header *hdr)
1207{
1208 LIST_HEAD(failed);
1209
1210 desc->pg_dreq = hdr->dreq;
1211 while (!list_empty(&hdr->pages)) {
1212 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
1213
1214 nfs_list_remove_request(req);
1215 if (!nfs_pageio_add_request(desc, req))
1216 nfs_list_add_request(req, &failed);
1217 }
1218 nfs_pageio_complete(desc);
1219 if (!list_empty(&failed)) {
1220 list_move(&failed, &hdr->pages);
1221 return -EIO;
1222 }
1223 return 0;
1224}
1225EXPORT_SYMBOL_GPL(nfs_pageio_resend);
Trond Myklebustd8a5ad72007-04-02 18:48:28 -04001226
1227/**
Weston Andros Adamson2176bf42014-09-10 15:44:18 -04001228 * nfs_pageio_complete - Complete I/O then cleanup an nfs_pageio_descriptor
Trond Myklebustbcb71bb2007-04-02 18:48:28 -04001229 * @desc: pointer to io descriptor
1230 */
1231void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
1232{
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001233 u32 midx;
1234
1235 for (midx = 0; midx < desc->pg_mirror_count; midx++)
1236 nfs_pageio_complete_mirror(desc, midx);
Weston Andros Adamson2176bf42014-09-10 15:44:18 -04001237
1238 if (desc->pg_ops->pg_cleanup)
1239 desc->pg_ops->pg_cleanup(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001240 nfs_pageio_cleanup_mirroring(desc);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -04001241}
1242
Trond Myklebust7fe7f842007-05-20 10:18:27 -04001243/**
1244 * nfs_pageio_cond_complete - Conditional I/O completion
1245 * @desc: pointer to io descriptor
1246 * @index: page index
1247 *
1248 * It is important to ensure that processes don't try to take locks
1249 * on non-contiguous ranges of pages as that might deadlock. This
1250 * function should be called before attempting to wait on a locked
1251 * nfs_page. It will complete the I/O if the page index 'index'
1252 * is not contiguous with the existing list of pages in 'desc'.
1253 */
1254void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
1255{
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001256 struct nfs_pgio_mirror *mirror;
1257 struct nfs_page *prev;
1258 u32 midx;
1259
1260 for (midx = 0; midx < desc->pg_mirror_count; midx++) {
1261 mirror = &desc->pg_mirrors[midx];
1262 if (!list_empty(&mirror->pg_list)) {
1263 prev = nfs_list_entry(mirror->pg_list.prev);
1264 if (index != prev->wb_index + 1)
1265 nfs_pageio_complete_mirror(desc, midx);
1266 }
Trond Myklebust7fe7f842007-05-20 10:18:27 -04001267 }
1268}
1269
David Howellsf7b422b2006-06-09 09:34:33 -04001270int __init nfs_init_nfspagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
1272 nfs_page_cachep = kmem_cache_create("nfs_page",
1273 sizeof(struct nfs_page),
1274 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001275 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (nfs_page_cachep == NULL)
1277 return -ENOMEM;
1278
1279 return 0;
1280}
1281
David Brownell266bee82006-06-27 12:59:15 -07001282void nfs_destroy_nfspagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001284 kmem_cache_destroy(nfs_page_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
Anna Schumakeref2c4882014-05-06 09:12:36 -04001287static const struct rpc_call_ops nfs_pgio_common_ops = {
Anna Schumaker6f92fa42014-05-06 09:12:33 -04001288 .rpc_call_prepare = nfs_pgio_prepare,
1289 .rpc_call_done = nfs_pgio_result,
1290 .rpc_release = nfs_pgio_release,
1291};
Anna Schumaker41d8d5b2014-05-06 09:12:40 -04001292
1293const struct nfs_pageio_ops nfs_pgio_rw_ops = {
1294 .pg_test = nfs_generic_pg_test,
1295 .pg_doio = nfs_generic_pg_pgios,
1296};