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);
165 nfs_async_read_error(struct list_head *head)
167 struct nfs_page *req;
169 while (!list_empty(head)) {
170 req = nfs_list_entry(head->next);
171 nfs_list_remove_request(req);
172 nfs_readpage_release(req);
176 static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
177 .error_cleanup = nfs_async_read_error,
178 .completion = nfs_read_completion,
181 static const struct nfs_pageio_ops nfs_pageio_read_ops = {
182 .pg_test = nfs_generic_pg_test,
183 .pg_doio = nfs_generic_pg_pgios,
187 * This is the callback from RPC telling us whether a reply was
188 * received or some error occurred (timeout or socket shutdown).
190 static int nfs_readpage_done(struct rpc_task *task, struct nfs_pgio_data *data,
193 int status = NFS_PROTO(inode)->read_done(task, data);
197 nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, data->res.count);
199 if (task->tk_status == -ESTALE) {
200 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
201 nfs_mark_for_revalidate(inode);
206 static void nfs_readpage_retry(struct rpc_task *task, struct nfs_pgio_data *data)
208 struct nfs_pgio_args *argp = &data->args;
209 struct nfs_pgio_res *resp = &data->res;
211 /* This is a short read! */
212 nfs_inc_stats(data->header->inode, NFSIOS_SHORTREAD);
213 /* Has the server at least made some progress? */
214 if (resp->count == 0) {
215 nfs_set_pgio_error(data->header, -EIO, argp->offset);
218 /* Yes, so retry the read at the end of the data */
219 data->mds_offset += resp->count;
220 argp->offset += resp->count;
221 argp->pgbase += resp->count;
222 argp->count -= resp->count;
223 rpc_restart_call_prepare(task);
226 static void nfs_readpage_result(struct rpc_task *task, struct nfs_pgio_data *data)
228 struct nfs_pgio_header *hdr = data->header;
233 bound = data->args.offset + data->res.count;
234 spin_lock(&hdr->lock);
235 if (bound < hdr->io_start + hdr->good_bytes) {
236 set_bit(NFS_IOHDR_EOF, &hdr->flags);
237 clear_bit(NFS_IOHDR_ERROR, &hdr->flags);
238 hdr->good_bytes = bound - hdr->io_start;
240 spin_unlock(&hdr->lock);
241 } else if (data->res.count != data->args.count)
242 nfs_readpage_retry(task, data);
246 * Read a page over NFS.
247 * We read the page synchronously in the following case:
248 * - The error flag is set for this page. This happens only when a
249 * previous async read operation failed.
251 int nfs_readpage(struct file *file, struct page *page)
253 struct nfs_open_context *ctx;
254 struct inode *inode = page_file_mapping(page)->host;
257 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
258 page, PAGE_CACHE_SIZE, page_file_index(page));
259 nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
260 nfs_add_stats(inode, NFSIOS_READPAGES, 1);
263 * Try to flush any pending writes to the file..
265 * NOTE! Because we own the page lock, there cannot
266 * be any new pending writes generated at this point
267 * for this page (other pages can be written to).
269 error = nfs_wb_page(inode, page);
272 if (PageUptodate(page))
276 if (NFS_STALE(inode))
281 ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
285 ctx = get_nfs_open_context(nfs_file_open_context(file));
287 if (!IS_SYNC(inode)) {
288 error = nfs_readpage_from_fscache(ctx, inode, page);
293 error = nfs_readpage_async(ctx, inode, page);
296 put_nfs_open_context(ctx);
303 struct nfs_readdesc {
304 struct nfs_pageio_descriptor *pgio;
305 struct nfs_open_context *ctx;
309 readpage_async_filler(void *data, struct page *page)
311 struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
312 struct inode *inode = page_file_mapping(page)->host;
313 struct nfs_page *new;
317 len = nfs_page_length(page);
319 return nfs_return_empty_page(page);
321 new = nfs_create_request(desc->ctx, inode, page, 0, len);
325 if (len < PAGE_CACHE_SIZE)
326 zero_user_segment(page, len, PAGE_CACHE_SIZE);
327 if (!nfs_pageio_add_request(desc->pgio, new)) {
328 error = desc->pgio->pg_error;
333 error = PTR_ERR(new);
339 int nfs_readpages(struct file *filp, struct address_space *mapping,
340 struct list_head *pages, unsigned nr_pages)
342 struct nfs_pageio_descriptor pgio;
343 struct nfs_readdesc desc = {
346 struct inode *inode = mapping->host;
347 unsigned long npages;
350 dprintk("NFS: nfs_readpages (%s/%Lu %d)\n",
352 (unsigned long long)NFS_FILEID(inode),
354 nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
356 if (NFS_STALE(inode))
360 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
361 if (desc.ctx == NULL)
364 desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
366 /* attempt to read as many of the pages as possible from the cache
367 * - this returns -ENOBUFS immediately if the cookie is negative
369 ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
372 goto read_complete; /* all pages were read */
374 nfs_pageio_init_read(&pgio, inode, false,
375 &nfs_async_read_completion_ops);
377 ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
379 nfs_pageio_complete(&pgio);
380 NFS_I(inode)->read_io += pgio.pg_bytes_written;
381 npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
382 nfs_add_stats(inode, NFSIOS_READPAGES, npages);
384 put_nfs_open_context(desc.ctx);
389 int __init nfs_init_readpagecache(void)
391 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
392 sizeof(struct nfs_rw_header),
393 0, SLAB_HWCACHE_ALIGN,
395 if (nfs_rdata_cachep == NULL)
401 void nfs_destroy_readpagecache(void)
403 kmem_cache_destroy(nfs_rdata_cachep);
406 static const struct nfs_rw_ops nfs_rw_read_ops = {
407 .rw_mode = FMODE_READ,
408 .rw_alloc_header = nfs_readhdr_alloc,
409 .rw_free_header = nfs_readhdr_free,
410 .rw_done = nfs_readpage_done,
411 .rw_result = nfs_readpage_result,
412 .rw_initiate = nfs_initiate_read,