6 * Partial copy of Linus' read cache modifications to fs/nfs/file.c
7 * modified for async RPC by okir@monad.swb.de
10 #include <linux/time.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/fcntl.h>
14 #include <linux/stat.h>
16 #include <linux/slab.h>
17 #include <linux/pagemap.h>
18 #include <linux/sunrpc/clnt.h>
19 #include <linux/nfs_fs.h>
20 #include <linux/nfs_page.h>
21 #include <linux/module.h>
29 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
31 static const struct nfs_pageio_ops nfs_pageio_read_ops;
32 static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops;
33 static const struct nfs_rw_ops nfs_rw_read_ops;
35 static struct kmem_cache *nfs_rdata_cachep;
37 static struct nfs_rw_header *nfs_readhdr_alloc(void)
39 return kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL);
42 static void nfs_readhdr_free(struct nfs_rw_header *rhdr)
44 kmem_cache_free(nfs_rdata_cachep, rhdr);
48 int nfs_return_empty_page(struct page *page)
50 zero_user(page, 0, PAGE_CACHE_SIZE);
51 SetPageUptodate(page);
56 void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
57 struct inode *inode, bool force_mds,
58 const struct nfs_pgio_completion_ops *compl_ops)
60 struct nfs_server *server = NFS_SERVER(inode);
61 const struct nfs_pageio_ops *pg_ops = &nfs_pageio_read_ops;
63 #ifdef CONFIG_NFS_V4_1
64 if (server->pnfs_curr_ld && !force_mds)
65 pg_ops = server->pnfs_curr_ld->pg_read_ops;
67 nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_read_ops,
70 EXPORT_SYMBOL_GPL(nfs_pageio_init_read);
72 void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
74 pgio->pg_ops = &nfs_pageio_read_ops;
75 pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
77 EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
79 int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
84 struct nfs_pageio_descriptor pgio;
86 len = nfs_page_length(page);
88 return nfs_return_empty_page(page);
89 new = nfs_create_request(ctx, inode, page, 0, len);
94 if (len < PAGE_CACHE_SIZE)
95 zero_user_segment(page, len, PAGE_CACHE_SIZE);
97 nfs_pageio_init_read(&pgio, inode, false,
98 &nfs_async_read_completion_ops);
99 nfs_pageio_add_request(&pgio, new);
100 nfs_pageio_complete(&pgio);
101 NFS_I(inode)->read_io += pgio.pg_bytes_written;
105 static void nfs_readpage_release(struct nfs_page *req)
107 struct inode *d_inode = req->wb_context->dentry->d_inode;
109 if (PageUptodate(req->wb_page))
110 nfs_readpage_to_fscache(d_inode, req->wb_page, 0);
112 unlock_page(req->wb_page);
114 dprintk("NFS: read done (%s/%Lu %d@%Ld)\n",
115 req->wb_context->dentry->d_inode->i_sb->s_id,
116 (unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
118 (long long)req_offset(req));
119 nfs_release_request(req);
122 /* Note io was page aligned */
123 static void nfs_read_completion(struct nfs_pgio_header *hdr)
125 unsigned long bytes = 0;
127 if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
129 while (!list_empty(&hdr->pages)) {
130 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
131 struct page *page = req->wb_page;
133 if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
134 if (bytes > hdr->good_bytes)
135 zero_user(page, 0, PAGE_SIZE);
136 else if (hdr->good_bytes - bytes < PAGE_SIZE)
137 zero_user_segment(page,
138 hdr->good_bytes & ~PAGE_MASK,
141 bytes += req->wb_bytes;
142 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
143 if (bytes <= hdr->good_bytes)
144 SetPageUptodate(page);
146 SetPageUptodate(page);
147 nfs_list_remove_request(req);
148 nfs_readpage_release(req);
154 static void nfs_initiate_read(struct nfs_pgio_data *data, struct rpc_message *msg,
155 struct rpc_task_setup *task_setup_data, int how)
157 struct inode *inode = data->header->inode;
158 int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
160 task_setup_data->flags |= swap_flags;
161 NFS_PROTO(inode)->read_setup(data, msg);
164 static int nfs_do_read(struct nfs_pgio_data *data,
165 const struct rpc_call_ops *call_ops)
167 struct inode *inode = data->header->inode;
169 return nfs_initiate_pgio(NFS_CLIENT(inode), data, call_ops, 0, 0);
173 nfs_do_multiple_reads(struct list_head *head,
174 const struct rpc_call_ops *call_ops)
176 struct nfs_pgio_data *data;
179 while (!list_empty(head)) {
182 data = list_first_entry(head, struct nfs_pgio_data, list);
183 list_del_init(&data->list);
185 ret2 = nfs_do_read(data, call_ops);
193 nfs_async_read_error(struct list_head *head)
195 struct nfs_page *req;
197 while (!list_empty(head)) {
198 req = nfs_list_entry(head->next);
199 nfs_list_remove_request(req);
200 nfs_readpage_release(req);
204 static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
205 .error_cleanup = nfs_async_read_error,
206 .completion = nfs_read_completion,
209 static int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
211 struct nfs_rw_header *rhdr;
212 struct nfs_pgio_header *hdr;
215 rhdr = nfs_rw_header_alloc(desc->pg_rw_ops);
217 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
221 nfs_pgheader_init(desc, hdr, nfs_rw_header_free);
222 atomic_inc(&hdr->refcnt);
223 ret = nfs_generic_pgio(desc, hdr);
225 ret = nfs_do_multiple_reads(&hdr->rpc_list,
226 desc->pg_rpc_callops);
227 if (atomic_dec_and_test(&hdr->refcnt))
228 hdr->completion_ops->completion(hdr);
232 static const struct nfs_pageio_ops nfs_pageio_read_ops = {
233 .pg_test = nfs_generic_pg_test,
234 .pg_doio = nfs_generic_pg_readpages,
238 * This is the callback from RPC telling us whether a reply was
239 * received or some error occurred (timeout or socket shutdown).
241 static int nfs_readpage_done(struct rpc_task *task, struct nfs_pgio_data *data,
244 int status = NFS_PROTO(inode)->read_done(task, data);
248 nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, data->res.count);
250 if (task->tk_status == -ESTALE) {
251 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
252 nfs_mark_for_revalidate(inode);
257 static void nfs_readpage_retry(struct rpc_task *task, struct nfs_pgio_data *data)
259 struct nfs_pgio_args *argp = &data->args;
260 struct nfs_pgio_res *resp = &data->res;
262 /* This is a short read! */
263 nfs_inc_stats(data->header->inode, NFSIOS_SHORTREAD);
264 /* Has the server at least made some progress? */
265 if (resp->count == 0) {
266 nfs_set_pgio_error(data->header, -EIO, argp->offset);
269 /* Yes, so retry the read at the end of the data */
270 data->mds_offset += resp->count;
271 argp->offset += resp->count;
272 argp->pgbase += resp->count;
273 argp->count -= resp->count;
274 rpc_restart_call_prepare(task);
277 static void nfs_readpage_result(struct rpc_task *task, struct nfs_pgio_data *data)
279 struct nfs_pgio_header *hdr = data->header;
284 bound = data->args.offset + data->res.count;
285 spin_lock(&hdr->lock);
286 if (bound < hdr->io_start + hdr->good_bytes) {
287 set_bit(NFS_IOHDR_EOF, &hdr->flags);
288 clear_bit(NFS_IOHDR_ERROR, &hdr->flags);
289 hdr->good_bytes = bound - hdr->io_start;
291 spin_unlock(&hdr->lock);
292 } else if (data->res.count != data->args.count)
293 nfs_readpage_retry(task, data);
297 * Read a page over NFS.
298 * We read the page synchronously in the following case:
299 * - The error flag is set for this page. This happens only when a
300 * previous async read operation failed.
302 int nfs_readpage(struct file *file, struct page *page)
304 struct nfs_open_context *ctx;
305 struct inode *inode = page_file_mapping(page)->host;
308 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
309 page, PAGE_CACHE_SIZE, page_file_index(page));
310 nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
311 nfs_add_stats(inode, NFSIOS_READPAGES, 1);
314 * Try to flush any pending writes to the file..
316 * NOTE! Because we own the page lock, there cannot
317 * be any new pending writes generated at this point
318 * for this page (other pages can be written to).
320 error = nfs_wb_page(inode, page);
323 if (PageUptodate(page))
327 if (NFS_STALE(inode))
332 ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
336 ctx = get_nfs_open_context(nfs_file_open_context(file));
338 if (!IS_SYNC(inode)) {
339 error = nfs_readpage_from_fscache(ctx, inode, page);
344 error = nfs_readpage_async(ctx, inode, page);
347 put_nfs_open_context(ctx);
354 struct nfs_readdesc {
355 struct nfs_pageio_descriptor *pgio;
356 struct nfs_open_context *ctx;
360 readpage_async_filler(void *data, struct page *page)
362 struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
363 struct inode *inode = page_file_mapping(page)->host;
364 struct nfs_page *new;
368 len = nfs_page_length(page);
370 return nfs_return_empty_page(page);
372 new = nfs_create_request(desc->ctx, inode, page, 0, len);
376 if (len < PAGE_CACHE_SIZE)
377 zero_user_segment(page, len, PAGE_CACHE_SIZE);
378 if (!nfs_pageio_add_request(desc->pgio, new)) {
379 error = desc->pgio->pg_error;
384 error = PTR_ERR(new);
390 int nfs_readpages(struct file *filp, struct address_space *mapping,
391 struct list_head *pages, unsigned nr_pages)
393 struct nfs_pageio_descriptor pgio;
394 struct nfs_readdesc desc = {
397 struct inode *inode = mapping->host;
398 unsigned long npages;
401 dprintk("NFS: nfs_readpages (%s/%Lu %d)\n",
403 (unsigned long long)NFS_FILEID(inode),
405 nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
407 if (NFS_STALE(inode))
411 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
412 if (desc.ctx == NULL)
415 desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
417 /* attempt to read as many of the pages as possible from the cache
418 * - this returns -ENOBUFS immediately if the cookie is negative
420 ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
423 goto read_complete; /* all pages were read */
425 nfs_pageio_init_read(&pgio, inode, false,
426 &nfs_async_read_completion_ops);
428 ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
430 nfs_pageio_complete(&pgio);
431 NFS_I(inode)->read_io += pgio.pg_bytes_written;
432 npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
433 nfs_add_stats(inode, NFSIOS_READPAGES, npages);
435 put_nfs_open_context(desc.ctx);
440 int __init nfs_init_readpagecache(void)
442 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
443 sizeof(struct nfs_rw_header),
444 0, SLAB_HWCACHE_ALIGN,
446 if (nfs_rdata_cachep == NULL)
452 void nfs_destroy_readpagecache(void)
454 kmem_cache_destroy(nfs_rdata_cachep);
457 static const struct nfs_rw_ops nfs_rw_read_ops = {
458 .rw_mode = FMODE_READ,
459 .rw_alloc_header = nfs_readhdr_alloc,
460 .rw_free_header = nfs_readhdr_free,
461 .rw_done = nfs_readpage_done,
462 .rw_result = nfs_readpage_result,
463 .rw_initiate = nfs_initiate_read,