6 * Partial copy of Linus' read cache modifications to fs/nfs/file.c
7 * modified for async RPC by okir@monad.swb.de
9 * We do an ugly hack here in order to return proper error codes to the
10 * user program when a read request failed: since generic_file_read
11 * only checks the return value of inode->i_op->readpage() which is always 0
12 * for async RPC, we set the error bit of the page to 1 when an error occurs,
13 * and make nfs_readpage transmit requests synchronously when encountering this.
14 * This is only a small problem, though, since we now retry all operations
15 * within the RPC code when root squashing is suspected.
18 #include <linux/config.h>
19 #include <linux/time.h>
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/fcntl.h>
23 #include <linux/stat.h>
25 #include <linux/slab.h>
26 #include <linux/pagemap.h>
27 #include <linux/mempool.h>
28 #include <linux/sunrpc/clnt.h>
29 #include <linux/nfs_fs.h>
30 #include <linux/nfs_page.h>
31 #include <linux/smp_lock.h>
33 #include <asm/system.h>
35 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
37 static int nfs_pagein_one(struct list_head
*, struct inode
*);
39 static kmem_cache_t
*nfs_rdata_cachep
;
40 static mempool_t
*nfs_rdata_mempool
;
42 #define MIN_POOL_READ (32)
44 static __inline__
struct nfs_read_data
*nfs_readdata_alloc(void)
46 struct nfs_read_data
*p
;
47 p
= (struct nfs_read_data
*)mempool_alloc(nfs_rdata_mempool
, SLAB_NOFS
);
49 memset(p
, 0, sizeof(*p
));
50 INIT_LIST_HEAD(&p
->pages
);
55 static __inline__
void nfs_readdata_free(struct nfs_read_data
*p
)
57 mempool_free(p
, nfs_rdata_mempool
);
60 void nfs_readdata_release(struct rpc_task
*task
)
62 struct nfs_read_data
*data
= (struct nfs_read_data
*)task
->tk_calldata
;
63 nfs_readdata_free(data
);
67 * Read a page synchronously.
70 nfs_readpage_sync(struct file
*file
, struct inode
*inode
, struct page
*page
)
72 unsigned int rsize
= NFS_SERVER(inode
)->rsize
;
73 unsigned int count
= PAGE_CACHE_SIZE
;
75 struct nfs_read_data rdata
= {
76 .flags
= (IS_SWAPFILE(inode
)? NFS_RPC_SWAPFLAGS
: 0),
86 .fattr
= &rdata
.fattr
,
90 dprintk("NFS: nfs_readpage_sync(%p)\n", page
);
93 * This works now because the socket layer never tries to DMA
94 * into this buffer directly.
98 rdata
.args
.count
= count
;
99 rdata
.res
.count
= rdata
.args
.count
;
100 rdata
.args
.offset
= page_offset(page
) + rdata
.args
.pgbase
;
102 dprintk("NFS: nfs_proc_read(%s, (%s/%Ld), %Lu, %u)\n",
103 NFS_SERVER(inode
)->hostname
,
105 (long long)NFS_FILEID(inode
),
106 (unsigned long long)rdata
.args
.pgbase
,
110 result
= NFS_PROTO(inode
)->read(&rdata
, file
);
114 * Even if we had a partial success we can't mark the page
118 if (result
== -EISDIR
)
123 rdata
.args
.pgbase
+= result
;
124 if (result
< rdata
.args
.count
) /* NFSv2ism */
129 memclear_highpage_flush(page
, rdata
.args
.pgbase
, count
);
130 SetPageUptodate(page
);
132 ClearPageError(page
);
141 nfs_readpage_async(struct file
*file
, struct inode
*inode
, struct page
*page
)
143 LIST_HEAD(one_request
);
144 struct nfs_page
*new;
146 new = nfs_create_request(file
, inode
, page
, 0, PAGE_CACHE_SIZE
);
151 nfs_lock_request(new);
152 nfs_list_add_request(new, &one_request
);
153 nfs_pagein_one(&one_request
, inode
);
158 * Set up the NFS read request struct
161 nfs_read_rpcsetup(struct list_head
*head
, struct nfs_read_data
*data
)
164 struct nfs_page
*req
;
168 pages
= data
->pagevec
;
170 while (!list_empty(head
)) {
171 req
= nfs_list_entry(head
->next
);
172 nfs_list_remove_request(req
);
173 nfs_list_add_request(req
, &data
->pages
);
174 *pages
++ = req
->wb_page
;
175 count
+= req
->wb_bytes
;
177 req
= nfs_list_entry(data
->pages
.next
);
178 data
->inode
= inode
= req
->wb_inode
;
179 data
->cred
= req
->wb_cred
;
181 NFS_PROTO(inode
)->read_setup(data
, count
);
183 dprintk("NFS: %4d initiated read call (req %s/%Ld, %u bytes @ offset %Lu.\n",
186 (long long)NFS_FILEID(inode
),
188 (unsigned long long)req_offset(req
));
192 nfs_async_read_error(struct list_head
*head
)
194 struct nfs_page
*req
;
197 while (!list_empty(head
)) {
198 req
= nfs_list_entry(head
->next
);
200 nfs_list_remove_request(req
);
203 nfs_clear_request(req
);
204 nfs_release_request(req
);
205 nfs_unlock_request(req
);
210 nfs_pagein_one(struct list_head
*head
, struct inode
*inode
)
212 struct rpc_clnt
*clnt
= NFS_CLIENT(inode
);
213 struct nfs_read_data
*data
;
216 data
= nfs_readdata_alloc();
220 nfs_read_rpcsetup(head
, data
);
222 /* Start the async call */
223 rpc_clnt_sigmask(clnt
, &oldset
);
225 rpc_execute(&data
->task
);
227 rpc_clnt_sigunmask(clnt
, &oldset
);
230 nfs_async_read_error(head
);
235 nfs_pagein_list(struct list_head
*head
, int rpages
)
237 LIST_HEAD(one_request
);
238 struct nfs_page
*req
;
240 unsigned int pages
= 0;
242 while (!list_empty(head
)) {
243 pages
+= nfs_coalesce_requests(head
, &one_request
, rpages
);
244 req
= nfs_list_entry(one_request
.next
);
245 error
= nfs_pagein_one(&one_request
, req
->wb_inode
);
252 nfs_async_read_error(head
);
257 * This is the callback from RPC telling us whether a reply was
258 * received or some error occurred (timeout or socket shutdown).
261 nfs_readpage_result(struct rpc_task
*task
)
263 struct nfs_read_data
*data
= (struct nfs_read_data
*)task
->tk_calldata
;
264 unsigned int count
= data
->res
.count
;
266 dprintk("NFS: %4d nfs_readpage_result, (status %d)\n",
267 task
->tk_pid
, task
->tk_status
);
269 while (!list_empty(&data
->pages
)) {
270 struct nfs_page
*req
= nfs_list_entry(data
->pages
.next
);
271 struct page
*page
= req
->wb_page
;
272 nfs_list_remove_request(req
);
274 if (task
->tk_status
>= 0) {
275 if (count
< PAGE_CACHE_SIZE
) {
276 memclear_highpage_flush(page
,
277 req
->wb_pgbase
+ count
,
278 req
->wb_bytes
- count
);
282 count
-= PAGE_CACHE_SIZE
;
283 SetPageUptodate(page
);
288 dprintk("NFS: read (%s/%Ld %d@%Ld)\n",
289 req
->wb_inode
->i_sb
->s_id
,
290 (long long)NFS_FILEID(req
->wb_inode
),
292 (long long)req_offset(req
));
293 nfs_clear_request(req
);
294 nfs_release_request(req
);
295 nfs_unlock_request(req
);
300 * Read a page over NFS.
301 * We read the page synchronously in the following cases:
302 * - The NFS rsize is smaller than PAGE_CACHE_SIZE. We could kludge our way
303 * around this by creating several consecutive read requests, but
304 * that's hardly worth it.
305 * - The error flag is set for this page. This happens only when a
306 * previous async read operation failed.
309 nfs_readpage(struct file
*file
, struct page
*page
)
311 struct inode
*inode
= page
->mapping
->host
;
314 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
315 page
, PAGE_CACHE_SIZE
, page
->index
);
317 * Try to flush any pending writes to the file..
319 * NOTE! Because we own the page lock, there cannot
320 * be any new pending writes generated at this point
321 * for this page (other pages can be written to).
323 error
= nfs_wb_page(inode
, page
);
327 if (!PageError(page
) && NFS_SERVER(inode
)->rsize
>= PAGE_CACHE_SIZE
) {
328 error
= nfs_readpage_async(file
, inode
, page
);
332 error
= nfs_readpage_sync(file
, inode
, page
);
333 if (error
< 0 && IS_SWAPFILE(inode
))
334 printk("Aiee.. nfs swap-in of page failed!\n");
343 struct nfs_readdesc
{
344 struct list_head
*head
;
349 readpage_sync_filler(void *data
, struct page
*page
)
351 struct nfs_readdesc
*desc
= (struct nfs_readdesc
*)data
;
352 return nfs_readpage_sync(desc
->filp
, page
->mapping
->host
, page
);
356 readpage_async_filler(void *data
, struct page
*page
)
358 struct nfs_readdesc
*desc
= (struct nfs_readdesc
*)data
;
359 struct inode
*inode
= page
->mapping
->host
;
360 struct nfs_page
*new;
362 nfs_wb_page(inode
, page
);
363 new = nfs_create_request(desc
->filp
, inode
, page
, 0, PAGE_CACHE_SIZE
);
369 nfs_lock_request(new);
370 nfs_list_add_request(new, desc
->head
);
375 nfs_readpages(struct file
*filp
, struct address_space
*mapping
,
376 struct list_head
*pages
, unsigned nr_pages
)
379 struct nfs_readdesc desc
= {
383 struct nfs_server
*server
= NFS_SERVER(mapping
->host
);
384 int is_sync
= server
->rsize
< PAGE_CACHE_SIZE
;
387 ret
= read_cache_pages(mapping
, pages
,
388 is_sync
? readpage_sync_filler
:
389 readpage_async_filler
,
391 if (!list_empty(&head
)) {
392 int err
= nfs_pagein_list(&head
, server
->rpages
);
399 int nfs_init_readpagecache(void)
401 nfs_rdata_cachep
= kmem_cache_create("nfs_read_data",
402 sizeof(struct nfs_read_data
),
403 0, SLAB_HWCACHE_ALIGN
,
405 if (nfs_rdata_cachep
== NULL
)
408 nfs_rdata_mempool
= mempool_create(MIN_POOL_READ
,
412 if (nfs_rdata_mempool
== NULL
)
418 void nfs_destroy_readpagecache(void)
420 mempool_destroy(nfs_rdata_mempool
);
421 if (kmem_cache_destroy(nfs_rdata_cachep
))
422 printk(KERN_INFO
"nfs_read_data: not all structures were freed\n");