Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | ||
1da177e4 LT |
12 | #include <linux/slab.h> |
13 | #include <linux/file.h> | |
14 | #include <linux/sunrpc/clnt.h> | |
15 | #include <linux/nfs3.h> | |
16 | #include <linux/nfs4.h> | |
17 | #include <linux/nfs_page.h> | |
18 | #include <linux/nfs_fs.h> | |
19 | #include <linux/nfs_mount.h> | |
20 | ||
21 | #define NFS_PARANOIA 1 | |
22 | ||
e18b890b | 23 | static struct kmem_cache *nfs_page_cachep; |
1da177e4 LT |
24 | |
25 | static inline struct nfs_page * | |
26 | nfs_page_alloc(void) | |
27 | { | |
28 | struct nfs_page *p; | |
e94b1766 | 29 | p = kmem_cache_alloc(nfs_page_cachep, GFP_KERNEL); |
1da177e4 LT |
30 | if (p) { |
31 | memset(p, 0, sizeof(*p)); | |
32 | INIT_LIST_HEAD(&p->wb_list); | |
33 | } | |
34 | return p; | |
35 | } | |
36 | ||
37 | static inline void | |
38 | nfs_page_free(struct nfs_page *p) | |
39 | { | |
40 | kmem_cache_free(nfs_page_cachep, p); | |
41 | } | |
42 | ||
43 | /** | |
44 | * nfs_create_request - Create an NFS read/write request. | |
45 | * @file: file descriptor to use | |
46 | * @inode: inode to which the request is attached | |
47 | * @page: page to write | |
48 | * @offset: starting offset within the page for the write | |
49 | * @count: number of bytes to read/write | |
50 | * | |
51 | * The page must be locked by the caller. This makes sure we never | |
52 | * create two different requests for the same page, and avoids | |
53 | * a possible deadlock when we reach the hard limit on the number | |
54 | * of dirty pages. | |
55 | * User should ensure it is safe to sleep in this function. | |
56 | */ | |
57 | struct nfs_page * | |
58 | nfs_create_request(struct nfs_open_context *ctx, struct inode *inode, | |
59 | struct page *page, | |
60 | unsigned int offset, unsigned int count) | |
61 | { | |
62 | struct nfs_server *server = NFS_SERVER(inode); | |
63 | struct nfs_page *req; | |
64 | ||
65 | /* Deal with hard limits. */ | |
66 | for (;;) { | |
67 | /* try to allocate the request struct */ | |
68 | req = nfs_page_alloc(); | |
69 | if (req != NULL) | |
70 | break; | |
71 | ||
72 | /* Try to free up at least one request in order to stay | |
73 | * below the hard limit | |
74 | */ | |
75 | if (signalled() && (server->flags & NFS_MOUNT_INTR)) | |
76 | return ERR_PTR(-ERESTARTSYS); | |
77 | yield(); | |
78 | } | |
79 | ||
80 | /* Initialize the request struct. Initially, we assume a | |
81 | * long write-back delay. This will be adjusted in | |
82 | * update_nfs_request below if the region is not locked. */ | |
83 | req->wb_page = page; | |
84 | atomic_set(&req->wb_complete, 0); | |
85 | req->wb_index = page->index; | |
86 | page_cache_get(page); | |
cd52ed35 TM |
87 | BUG_ON(PagePrivate(page)); |
88 | BUG_ON(!PageLocked(page)); | |
89 | BUG_ON(page->mapping->host != inode); | |
1da177e4 LT |
90 | req->wb_offset = offset; |
91 | req->wb_pgbase = offset; | |
92 | req->wb_bytes = count; | |
93 | atomic_set(&req->wb_count, 1); | |
94 | req->wb_context = get_nfs_open_context(ctx); | |
95 | ||
96 | return req; | |
97 | } | |
98 | ||
99 | /** | |
100 | * nfs_unlock_request - Unlock request and wake up sleepers. | |
101 | * @req: | |
102 | */ | |
103 | void nfs_unlock_request(struct nfs_page *req) | |
104 | { | |
105 | if (!NFS_WBACK_BUSY(req)) { | |
106 | printk(KERN_ERR "NFS: Invalid unlock attempted\n"); | |
107 | BUG(); | |
108 | } | |
109 | smp_mb__before_clear_bit(); | |
110 | clear_bit(PG_BUSY, &req->wb_flags); | |
111 | smp_mb__after_clear_bit(); | |
464a98bd | 112 | wake_up_bit(&req->wb_flags, PG_BUSY); |
1da177e4 LT |
113 | nfs_release_request(req); |
114 | } | |
115 | ||
c6a556b8 TM |
116 | /** |
117 | * nfs_set_page_writeback_locked - Lock a request for writeback | |
118 | * @req: | |
119 | */ | |
120 | int nfs_set_page_writeback_locked(struct nfs_page *req) | |
121 | { | |
122 | struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode); | |
123 | ||
124 | if (!nfs_lock_request(req)) | |
125 | return 0; | |
126 | radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK); | |
127 | return 1; | |
128 | } | |
129 | ||
130 | /** | |
131 | * nfs_clear_page_writeback - Unlock request and wake up sleepers | |
132 | */ | |
133 | void nfs_clear_page_writeback(struct nfs_page *req) | |
134 | { | |
135 | struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode); | |
136 | ||
deb7d638 TM |
137 | if (req->wb_page != NULL) { |
138 | spin_lock(&nfsi->req_lock); | |
139 | radix_tree_tag_clear(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK); | |
140 | spin_unlock(&nfsi->req_lock); | |
141 | } | |
c6a556b8 TM |
142 | nfs_unlock_request(req); |
143 | } | |
144 | ||
1da177e4 LT |
145 | /** |
146 | * nfs_clear_request - Free up all resources allocated to the request | |
147 | * @req: | |
148 | * | |
149 | * Release page resources associated with a write request after it | |
150 | * has completed. | |
151 | */ | |
152 | void nfs_clear_request(struct nfs_page *req) | |
153 | { | |
cd52ed35 TM |
154 | struct page *page = req->wb_page; |
155 | if (page != NULL) { | |
cd52ed35 | 156 | page_cache_release(page); |
1da177e4 LT |
157 | req->wb_page = NULL; |
158 | } | |
159 | } | |
160 | ||
161 | ||
162 | /** | |
163 | * nfs_release_request - Release the count on an NFS read/write request | |
164 | * @req: request to release | |
165 | * | |
166 | * Note: Should never be called with the spinlock held! | |
167 | */ | |
168 | void | |
169 | nfs_release_request(struct nfs_page *req) | |
170 | { | |
171 | if (!atomic_dec_and_test(&req->wb_count)) | |
172 | return; | |
173 | ||
174 | #ifdef NFS_PARANOIA | |
175 | BUG_ON (!list_empty(&req->wb_list)); | |
176 | BUG_ON (NFS_WBACK_BUSY(req)); | |
177 | #endif | |
178 | ||
179 | /* Release struct file or cached credential */ | |
180 | nfs_clear_request(req); | |
181 | put_nfs_open_context(req->wb_context); | |
182 | nfs_page_free(req); | |
183 | } | |
184 | ||
464a98bd TM |
185 | static int nfs_wait_bit_interruptible(void *word) |
186 | { | |
187 | int ret = 0; | |
188 | ||
189 | if (signal_pending(current)) | |
190 | ret = -ERESTARTSYS; | |
191 | else | |
192 | schedule(); | |
193 | return ret; | |
194 | } | |
195 | ||
1da177e4 LT |
196 | /** |
197 | * nfs_wait_on_request - Wait for a request to complete. | |
198 | * @req: request to wait upon. | |
199 | * | |
200 | * Interruptible by signals only if mounted with intr flag. | |
201 | * The user is responsible for holding a count on the request. | |
202 | */ | |
203 | int | |
204 | nfs_wait_on_request(struct nfs_page *req) | |
205 | { | |
464a98bd TM |
206 | struct rpc_clnt *clnt = NFS_CLIENT(req->wb_context->dentry->d_inode); |
207 | sigset_t oldmask; | |
208 | int ret = 0; | |
209 | ||
210 | if (!test_bit(PG_BUSY, &req->wb_flags)) | |
211 | goto out; | |
212 | /* | |
213 | * Note: the call to rpc_clnt_sigmask() suffices to ensure that we | |
214 | * are not interrupted if intr flag is not set | |
215 | */ | |
216 | rpc_clnt_sigmask(clnt, &oldmask); | |
217 | ret = out_of_line_wait_on_bit(&req->wb_flags, PG_BUSY, | |
218 | nfs_wait_bit_interruptible, TASK_INTERRUPTIBLE); | |
219 | rpc_clnt_sigunmask(clnt, &oldmask); | |
220 | out: | |
221 | return ret; | |
1da177e4 LT |
222 | } |
223 | ||
224 | /** | |
d8a5ad75 TM |
225 | * nfs_pageio_init - initialise a page io descriptor |
226 | * @desc: pointer to descriptor | |
bcb71bba TM |
227 | * @inode: pointer to inode |
228 | * @doio: pointer to io function | |
229 | * @bsize: io block size | |
230 | * @io_flags: extra parameters for the io function | |
d8a5ad75 | 231 | */ |
bcb71bba TM |
232 | void nfs_pageio_init(struct nfs_pageio_descriptor *desc, |
233 | struct inode *inode, | |
234 | int (*doio)(struct inode *, struct list_head *, size_t, int), | |
235 | unsigned int bsize, | |
236 | int io_flags) | |
d8a5ad75 TM |
237 | { |
238 | INIT_LIST_HEAD(&desc->pg_list); | |
bcb71bba | 239 | desc->pg_bytes_written = 0; |
d8a5ad75 TM |
240 | desc->pg_count = 0; |
241 | desc->pg_bsize = bsize; | |
242 | desc->pg_base = 0; | |
bcb71bba TM |
243 | desc->pg_inode = inode; |
244 | desc->pg_doio = doio; | |
245 | desc->pg_ioflags = io_flags; | |
246 | desc->pg_error = 0; | |
d8a5ad75 TM |
247 | } |
248 | ||
249 | /** | |
250 | * nfs_can_coalesce_requests - test two requests for compatibility | |
251 | * @prev: pointer to nfs_page | |
252 | * @req: pointer to nfs_page | |
253 | * | |
254 | * The nfs_page structures 'prev' and 'req' are compared to ensure that the | |
255 | * page data area they describe is contiguous, and that their RPC | |
256 | * credentials, NFSv4 open state, and lockowners are the same. | |
257 | * | |
258 | * Return 'true' if this is the case, else return 'false'. | |
259 | */ | |
260 | static int nfs_can_coalesce_requests(struct nfs_page *prev, | |
261 | struct nfs_page *req) | |
262 | { | |
263 | if (req->wb_context->cred != prev->wb_context->cred) | |
264 | return 0; | |
265 | if (req->wb_context->lockowner != prev->wb_context->lockowner) | |
266 | return 0; | |
267 | if (req->wb_context->state != prev->wb_context->state) | |
268 | return 0; | |
269 | if (req->wb_index != (prev->wb_index + 1)) | |
270 | return 0; | |
271 | if (req->wb_pgbase != 0) | |
272 | return 0; | |
273 | if (prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE) | |
274 | return 0; | |
275 | return 1; | |
276 | } | |
277 | ||
278 | /** | |
bcb71bba | 279 | * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list. |
d8a5ad75 TM |
280 | * @desc: destination io descriptor |
281 | * @req: request | |
282 | * | |
283 | * Returns true if the request 'req' was successfully coalesced into the | |
284 | * existing list of pages 'desc'. | |
285 | */ | |
bcb71bba TM |
286 | static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc, |
287 | struct nfs_page *req) | |
d8a5ad75 TM |
288 | { |
289 | size_t newlen = req->wb_bytes; | |
290 | ||
291 | if (desc->pg_count != 0) { | |
292 | struct nfs_page *prev; | |
293 | ||
294 | /* | |
295 | * FIXME: ideally we should be able to coalesce all requests | |
296 | * that are not block boundary aligned, but currently this | |
297 | * is problematic for the case of bsize < PAGE_CACHE_SIZE, | |
298 | * since nfs_flush_multi and nfs_pagein_multi assume you | |
299 | * can have only one struct nfs_page. | |
300 | */ | |
301 | newlen += desc->pg_count; | |
302 | if (desc->pg_base + newlen > desc->pg_bsize) | |
303 | return 0; | |
304 | prev = nfs_list_entry(desc->pg_list.prev); | |
305 | if (!nfs_can_coalesce_requests(prev, req)) | |
306 | return 0; | |
307 | } else | |
308 | desc->pg_base = req->wb_pgbase; | |
309 | nfs_list_remove_request(req); | |
310 | nfs_list_add_request(req, &desc->pg_list); | |
311 | desc->pg_count = newlen; | |
312 | return 1; | |
313 | } | |
314 | ||
bcb71bba TM |
315 | /* |
316 | * Helper for nfs_pageio_add_request and nfs_pageio_complete | |
317 | */ | |
318 | static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc) | |
319 | { | |
320 | if (!list_empty(&desc->pg_list)) { | |
321 | int error = desc->pg_doio(desc->pg_inode, | |
322 | &desc->pg_list, | |
323 | desc->pg_count, | |
324 | desc->pg_ioflags); | |
325 | if (error < 0) | |
326 | desc->pg_error = error; | |
327 | else | |
328 | desc->pg_bytes_written += desc->pg_count; | |
329 | } | |
330 | if (list_empty(&desc->pg_list)) { | |
331 | desc->pg_count = 0; | |
332 | desc->pg_base = 0; | |
333 | } | |
334 | } | |
335 | ||
336 | /** | |
337 | * nfs_pageio_add_request - Attempt to coalesce a request into a page list. | |
338 | * @desc: destination io descriptor | |
339 | * @req: request | |
340 | * | |
341 | * Returns true if the request 'req' was successfully coalesced into the | |
342 | * existing list of pages 'desc'. | |
343 | */ | |
8b09bee3 TM |
344 | int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc, |
345 | struct nfs_page *req) | |
bcb71bba TM |
346 | { |
347 | while (!nfs_pageio_do_add_request(desc, req)) { | |
348 | nfs_pageio_doio(desc); | |
349 | if (desc->pg_error < 0) | |
350 | return 0; | |
351 | } | |
352 | return 1; | |
353 | } | |
354 | ||
bcb71bba TM |
355 | /** |
356 | * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor | |
357 | * @desc: pointer to io descriptor | |
358 | */ | |
359 | void nfs_pageio_complete(struct nfs_pageio_descriptor *desc) | |
360 | { | |
361 | nfs_pageio_doio(desc); | |
362 | } | |
363 | ||
3da28eb1 | 364 | #define NFS_SCAN_MAXENTRIES 16 |
1da177e4 LT |
365 | /** |
366 | * nfs_scan_list - Scan a list for matching requests | |
d2ccddf0 | 367 | * @nfsi: NFS inode |
1da177e4 LT |
368 | * @head: One of the NFS inode request lists |
369 | * @dst: Destination list | |
370 | * @idx_start: lower bound of page->index to scan | |
371 | * @npages: idx_start + npages sets the upper bound to scan. | |
372 | * | |
373 | * Moves elements from one of the inode request lists. | |
374 | * If the number of requests is set to 0, the entire address_space | |
375 | * starting at index idx_start, is scanned. | |
376 | * The requests are *not* checked to ensure that they form a contiguous set. | |
377 | * You must be holding the inode's req_lock when calling this function | |
378 | */ | |
d2ccddf0 TM |
379 | int nfs_scan_list(struct nfs_inode *nfsi, struct list_head *head, |
380 | struct list_head *dst, unsigned long idx_start, | |
381 | unsigned int npages) | |
1da177e4 | 382 | { |
d2ccddf0 TM |
383 | struct nfs_page *pgvec[NFS_SCAN_MAXENTRIES]; |
384 | struct nfs_page *req; | |
385 | unsigned long idx_end; | |
386 | int found, i; | |
387 | int res; | |
1da177e4 LT |
388 | |
389 | res = 0; | |
390 | if (npages == 0) | |
391 | idx_end = ~0; | |
392 | else | |
393 | idx_end = idx_start + npages - 1; | |
394 | ||
d2ccddf0 TM |
395 | for (;;) { |
396 | found = radix_tree_gang_lookup(&nfsi->nfs_page_tree, | |
397 | (void **)&pgvec[0], idx_start, | |
398 | NFS_SCAN_MAXENTRIES); | |
399 | if (found <= 0) | |
1da177e4 | 400 | break; |
d2ccddf0 TM |
401 | for (i = 0; i < found; i++) { |
402 | req = pgvec[i]; | |
403 | if (req->wb_index > idx_end) | |
404 | goto out; | |
405 | idx_start = req->wb_index + 1; | |
406 | if (req->wb_list_head != head) | |
407 | continue; | |
408 | if (nfs_set_page_writeback_locked(req)) { | |
409 | nfs_list_remove_request(req); | |
410 | nfs_list_add_request(req, dst); | |
411 | res++; | |
412 | } | |
413 | } | |
1da177e4 | 414 | |
1da177e4 | 415 | } |
d2ccddf0 | 416 | out: |
1da177e4 LT |
417 | return res; |
418 | } | |
419 | ||
f7b422b1 | 420 | int __init nfs_init_nfspagecache(void) |
1da177e4 LT |
421 | { |
422 | nfs_page_cachep = kmem_cache_create("nfs_page", | |
423 | sizeof(struct nfs_page), | |
424 | 0, SLAB_HWCACHE_ALIGN, | |
425 | NULL, NULL); | |
426 | if (nfs_page_cachep == NULL) | |
427 | return -ENOMEM; | |
428 | ||
429 | return 0; | |
430 | } | |
431 | ||
266bee88 | 432 | void nfs_destroy_nfspagecache(void) |
1da177e4 | 433 | { |
1a1d92c1 | 434 | kmem_cache_destroy(nfs_page_cachep); |
1da177e4 LT |
435 | } |
436 |