[PATCH] x86_64: iommu_gart_bitmap search to cross next_bit
[linux-2.6/next.git] / fs / nfs / pagelist.c
blobef9429643ebcc8e231268e58551cbf899df0336c
1 /*
2 * linux/fs/nfs/pagelist.c
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.
8 * Copyright 2000, 2001 (c) Trond Myklebust <trond.myklebust@fys.uio.no>
12 #include <linux/config.h>
13 #include <linux/slab.h>
14 #include <linux/file.h>
15 #include <linux/sunrpc/clnt.h>
16 #include <linux/nfs3.h>
17 #include <linux/nfs4.h>
18 #include <linux/nfs_page.h>
19 #include <linux/nfs_fs.h>
20 #include <linux/nfs_mount.h>
22 #define NFS_PARANOIA 1
24 static kmem_cache_t *nfs_page_cachep;
26 static inline struct nfs_page *
27 nfs_page_alloc(void)
29 struct nfs_page *p;
30 p = kmem_cache_alloc(nfs_page_cachep, SLAB_KERNEL);
31 if (p) {
32 memset(p, 0, sizeof(*p));
33 INIT_LIST_HEAD(&p->wb_list);
35 return p;
38 static inline void
39 nfs_page_free(struct nfs_page *p)
41 kmem_cache_free(nfs_page_cachep, p);
44 /**
45 * nfs_create_request - Create an NFS read/write request.
46 * @file: file descriptor to use
47 * @inode: inode to which the request is attached
48 * @page: page to write
49 * @offset: starting offset within the page for the write
50 * @count: number of bytes to read/write
52 * The page must be locked by the caller. This makes sure we never
53 * create two different requests for the same page, and avoids
54 * a possible deadlock when we reach the hard limit on the number
55 * of dirty pages.
56 * User should ensure it is safe to sleep in this function.
58 struct nfs_page *
59 nfs_create_request(struct nfs_open_context *ctx, struct inode *inode,
60 struct page *page,
61 unsigned int offset, unsigned int count)
63 struct nfs_server *server = NFS_SERVER(inode);
64 struct nfs_page *req;
66 /* Deal with hard limits. */
67 for (;;) {
68 /* try to allocate the request struct */
69 req = nfs_page_alloc();
70 if (req != NULL)
71 break;
73 /* Try to free up at least one request in order to stay
74 * below the hard limit
76 if (signalled() && (server->flags & NFS_MOUNT_INTR))
77 return ERR_PTR(-ERESTARTSYS);
78 yield();
81 /* Initialize the request struct. Initially, we assume a
82 * long write-back delay. This will be adjusted in
83 * update_nfs_request below if the region is not locked. */
84 req->wb_page = page;
85 atomic_set(&req->wb_complete, 0);
86 req->wb_index = page->index;
87 page_cache_get(page);
88 BUG_ON(PagePrivate(page));
89 BUG_ON(!PageLocked(page));
90 BUG_ON(page->mapping->host != inode);
91 req->wb_offset = offset;
92 req->wb_pgbase = offset;
93 req->wb_bytes = count;
94 atomic_set(&req->wb_count, 1);
95 req->wb_context = get_nfs_open_context(ctx);
97 return req;
101 * nfs_unlock_request - Unlock request and wake up sleepers.
102 * @req:
104 void nfs_unlock_request(struct nfs_page *req)
106 if (!NFS_WBACK_BUSY(req)) {
107 printk(KERN_ERR "NFS: Invalid unlock attempted\n");
108 BUG();
110 smp_mb__before_clear_bit();
111 clear_bit(PG_BUSY, &req->wb_flags);
112 smp_mb__after_clear_bit();
113 wake_up_bit(&req->wb_flags, PG_BUSY);
114 nfs_release_request(req);
118 * nfs_set_page_writeback_locked - Lock a request for writeback
119 * @req:
121 int nfs_set_page_writeback_locked(struct nfs_page *req)
123 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
125 if (!nfs_lock_request(req))
126 return 0;
127 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK);
128 return 1;
132 * nfs_clear_page_writeback - Unlock request and wake up sleepers
134 void nfs_clear_page_writeback(struct nfs_page *req)
136 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
138 if (req->wb_page != NULL) {
139 spin_lock(&nfsi->req_lock);
140 radix_tree_tag_clear(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK);
141 spin_unlock(&nfsi->req_lock);
143 nfs_unlock_request(req);
147 * nfs_clear_request - Free up all resources allocated to the request
148 * @req:
150 * Release page resources associated with a write request after it
151 * has completed.
153 void nfs_clear_request(struct nfs_page *req)
155 struct page *page = req->wb_page;
156 if (page != NULL) {
157 page_cache_release(page);
158 req->wb_page = NULL;
164 * nfs_release_request - Release the count on an NFS read/write request
165 * @req: request to release
167 * Note: Should never be called with the spinlock held!
169 void
170 nfs_release_request(struct nfs_page *req)
172 if (!atomic_dec_and_test(&req->wb_count))
173 return;
175 #ifdef NFS_PARANOIA
176 BUG_ON (!list_empty(&req->wb_list));
177 BUG_ON (NFS_WBACK_BUSY(req));
178 #endif
180 /* Release struct file or cached credential */
181 nfs_clear_request(req);
182 put_nfs_open_context(req->wb_context);
183 nfs_page_free(req);
186 static int nfs_wait_bit_interruptible(void *word)
188 int ret = 0;
190 if (signal_pending(current))
191 ret = -ERESTARTSYS;
192 else
193 schedule();
194 return ret;
198 * nfs_wait_on_request - Wait for a request to complete.
199 * @req: request to wait upon.
201 * Interruptible by signals only if mounted with intr flag.
202 * The user is responsible for holding a count on the request.
205 nfs_wait_on_request(struct nfs_page *req)
207 struct rpc_clnt *clnt = NFS_CLIENT(req->wb_context->dentry->d_inode);
208 sigset_t oldmask;
209 int ret = 0;
211 if (!test_bit(PG_BUSY, &req->wb_flags))
212 goto out;
214 * Note: the call to rpc_clnt_sigmask() suffices to ensure that we
215 * are not interrupted if intr flag is not set
217 rpc_clnt_sigmask(clnt, &oldmask);
218 ret = out_of_line_wait_on_bit(&req->wb_flags, PG_BUSY,
219 nfs_wait_bit_interruptible, TASK_INTERRUPTIBLE);
220 rpc_clnt_sigunmask(clnt, &oldmask);
221 out:
222 return ret;
226 * nfs_coalesce_requests - Split coalesced requests out from a list.
227 * @head: source list
228 * @dst: destination list
229 * @nmax: maximum number of requests to coalesce
231 * Moves a maximum of 'nmax' elements from one list to another.
232 * The elements are checked to ensure that they form a contiguous set
233 * of pages, and that the RPC credentials are the same.
236 nfs_coalesce_requests(struct list_head *head, struct list_head *dst,
237 unsigned int nmax)
239 struct nfs_page *req = NULL;
240 unsigned int npages = 0;
242 while (!list_empty(head)) {
243 struct nfs_page *prev = req;
245 req = nfs_list_entry(head->next);
246 if (prev) {
247 if (req->wb_context->cred != prev->wb_context->cred)
248 break;
249 if (req->wb_context->lockowner != prev->wb_context->lockowner)
250 break;
251 if (req->wb_context->state != prev->wb_context->state)
252 break;
253 if (req->wb_index != (prev->wb_index + 1))
254 break;
256 if (req->wb_pgbase != 0)
257 break;
259 nfs_list_remove_request(req);
260 nfs_list_add_request(req, dst);
261 npages++;
262 if (req->wb_pgbase + req->wb_bytes != PAGE_CACHE_SIZE)
263 break;
264 if (npages >= nmax)
265 break;
267 return npages;
270 #define NFS_SCAN_MAXENTRIES 16
272 * nfs_scan_lock_dirty - Scan the radix tree for dirty requests
273 * @nfsi: NFS inode
274 * @dst: Destination list
275 * @idx_start: lower bound of page->index to scan
276 * @npages: idx_start + npages sets the upper bound to scan.
278 * Moves elements from one of the inode request lists.
279 * If the number of requests is set to 0, the entire address_space
280 * starting at index idx_start, is scanned.
281 * The requests are *not* checked to ensure that they form a contiguous set.
282 * You must be holding the inode's req_lock when calling this function
285 nfs_scan_lock_dirty(struct nfs_inode *nfsi, struct list_head *dst,
286 unsigned long idx_start, unsigned int npages)
288 struct nfs_page *pgvec[NFS_SCAN_MAXENTRIES];
289 struct nfs_page *req;
290 unsigned long idx_end;
291 int found, i;
292 int res;
294 res = 0;
295 if (npages == 0)
296 idx_end = ~0;
297 else
298 idx_end = idx_start + npages - 1;
300 for (;;) {
301 found = radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree,
302 (void **)&pgvec[0], idx_start, NFS_SCAN_MAXENTRIES,
303 NFS_PAGE_TAG_DIRTY);
304 if (found <= 0)
305 break;
306 for (i = 0; i < found; i++) {
307 req = pgvec[i];
308 if (req->wb_index > idx_end)
309 goto out;
311 idx_start = req->wb_index + 1;
313 if (nfs_set_page_writeback_locked(req)) {
314 radix_tree_tag_clear(&nfsi->nfs_page_tree,
315 req->wb_index, NFS_PAGE_TAG_DIRTY);
316 nfs_list_remove_request(req);
317 nfs_list_add_request(req, dst);
318 res++;
322 out:
323 return res;
327 * nfs_scan_list - Scan a list for matching requests
328 * @nfsi: NFS inode
329 * @head: One of the NFS inode request lists
330 * @dst: Destination list
331 * @idx_start: lower bound of page->index to scan
332 * @npages: idx_start + npages sets the upper bound to scan.
334 * Moves elements from one of the inode request lists.
335 * If the number of requests is set to 0, the entire address_space
336 * starting at index idx_start, is scanned.
337 * The requests are *not* checked to ensure that they form a contiguous set.
338 * You must be holding the inode's req_lock when calling this function
340 int nfs_scan_list(struct nfs_inode *nfsi, struct list_head *head,
341 struct list_head *dst, unsigned long idx_start,
342 unsigned int npages)
344 struct nfs_page *pgvec[NFS_SCAN_MAXENTRIES];
345 struct nfs_page *req;
346 unsigned long idx_end;
347 int found, i;
348 int res;
350 res = 0;
351 if (npages == 0)
352 idx_end = ~0;
353 else
354 idx_end = idx_start + npages - 1;
356 for (;;) {
357 found = radix_tree_gang_lookup(&nfsi->nfs_page_tree,
358 (void **)&pgvec[0], idx_start,
359 NFS_SCAN_MAXENTRIES);
360 if (found <= 0)
361 break;
362 for (i = 0; i < found; i++) {
363 req = pgvec[i];
364 if (req->wb_index > idx_end)
365 goto out;
366 idx_start = req->wb_index + 1;
367 if (req->wb_list_head != head)
368 continue;
369 if (nfs_set_page_writeback_locked(req)) {
370 nfs_list_remove_request(req);
371 nfs_list_add_request(req, dst);
372 res++;
377 out:
378 return res;
381 int __init nfs_init_nfspagecache(void)
383 nfs_page_cachep = kmem_cache_create("nfs_page",
384 sizeof(struct nfs_page),
385 0, SLAB_HWCACHE_ALIGN,
386 NULL, NULL);
387 if (nfs_page_cachep == NULL)
388 return -ENOMEM;
390 return 0;
393 void __exit nfs_destroy_nfspagecache(void)
395 if (kmem_cache_destroy(nfs_page_cachep))
396 printk(KERN_INFO "nfs_page: not all structures were freed\n");