blob: 356a33bb38a6513f9b76357c120068b7ec77639e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
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>
21
22#define NFS_PARANOIA 1
23
24static kmem_cache_t *nfs_page_cachep;
25
26static inline struct nfs_page *
27nfs_page_alloc(void)
28{
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);
34 }
35 return p;
36}
37
38static inline void
39nfs_page_free(struct nfs_page *p)
40{
41 kmem_cache_free(nfs_page_cachep, p);
42}
43
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
51 *
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.
57 */
58struct nfs_page *
59nfs_create_request(struct nfs_open_context *ctx, struct inode *inode,
60 struct page *page,
61 unsigned int offset, unsigned int count)
62{
63 struct nfs_server *server = NFS_SERVER(inode);
64 struct nfs_page *req;
65
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;
72
73 /* Try to free up at least one request in order to stay
74 * below the hard limit
75 */
76 if (signalled() && (server->flags & NFS_MOUNT_INTR))
77 return ERR_PTR(-ERESTARTSYS);
78 yield();
79 }
80
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 req->wb_offset = offset;
89 req->wb_pgbase = offset;
90 req->wb_bytes = count;
91 atomic_set(&req->wb_count, 1);
92 req->wb_context = get_nfs_open_context(ctx);
93
94 return req;
95}
96
97/**
98 * nfs_unlock_request - Unlock request and wake up sleepers.
99 * @req:
100 */
101void nfs_unlock_request(struct nfs_page *req)
102{
103 if (!NFS_WBACK_BUSY(req)) {
104 printk(KERN_ERR "NFS: Invalid unlock attempted\n");
105 BUG();
106 }
107 smp_mb__before_clear_bit();
108 clear_bit(PG_BUSY, &req->wb_flags);
109 smp_mb__after_clear_bit();
Trond Myklebust464a98b2005-06-22 17:16:21 +0000110 wake_up_bit(&req->wb_flags, PG_BUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 nfs_release_request(req);
112}
113
114/**
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000115 * nfs_set_page_writeback_locked - Lock a request for writeback
116 * @req:
117 */
118int nfs_set_page_writeback_locked(struct nfs_page *req)
119{
120 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
121
122 if (!nfs_lock_request(req))
123 return 0;
124 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK);
125 return 1;
126}
127
128/**
129 * nfs_clear_page_writeback - Unlock request and wake up sleepers
130 */
131void nfs_clear_page_writeback(struct nfs_page *req)
132{
133 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
134
135 spin_lock(&nfsi->req_lock);
136 radix_tree_tag_clear(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK);
137 spin_unlock(&nfsi->req_lock);
138 nfs_unlock_request(req);
139}
140
141/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 * nfs_clear_request - Free up all resources allocated to the request
143 * @req:
144 *
145 * Release page resources associated with a write request after it
146 * has completed.
147 */
148void nfs_clear_request(struct nfs_page *req)
149{
150 if (req->wb_page) {
151 page_cache_release(req->wb_page);
152 req->wb_page = NULL;
153 }
154}
155
156
157/**
158 * nfs_release_request - Release the count on an NFS read/write request
159 * @req: request to release
160 *
161 * Note: Should never be called with the spinlock held!
162 */
163void
164nfs_release_request(struct nfs_page *req)
165{
166 if (!atomic_dec_and_test(&req->wb_count))
167 return;
168
169#ifdef NFS_PARANOIA
170 BUG_ON (!list_empty(&req->wb_list));
171 BUG_ON (NFS_WBACK_BUSY(req));
172#endif
173
174 /* Release struct file or cached credential */
175 nfs_clear_request(req);
176 put_nfs_open_context(req->wb_context);
177 nfs_page_free(req);
178}
179
180/**
181 * nfs_list_add_request - Insert a request into a sorted list
182 * @req: request
183 * @head: head of list into which to insert the request.
184 *
185 * Note that the wb_list is sorted by page index in order to facilitate
186 * coalescing of requests.
187 * We use an insertion sort that is optimized for the case of appended
188 * writes.
189 */
190void
191nfs_list_add_request(struct nfs_page *req, struct list_head *head)
192{
193 struct list_head *pos;
194
195#ifdef NFS_PARANOIA
196 if (!list_empty(&req->wb_list)) {
197 printk(KERN_ERR "NFS: Add to list failed!\n");
198 BUG();
199 }
200#endif
201 list_for_each_prev(pos, head) {
202 struct nfs_page *p = nfs_list_entry(pos);
203 if (p->wb_index < req->wb_index)
204 break;
205 }
206 list_add(&req->wb_list, pos);
207 req->wb_list_head = head;
208}
209
Trond Myklebust464a98b2005-06-22 17:16:21 +0000210static int nfs_wait_bit_interruptible(void *word)
211{
212 int ret = 0;
213
214 if (signal_pending(current))
215 ret = -ERESTARTSYS;
216 else
217 schedule();
218 return ret;
219}
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221/**
222 * nfs_wait_on_request - Wait for a request to complete.
223 * @req: request to wait upon.
224 *
225 * Interruptible by signals only if mounted with intr flag.
226 * The user is responsible for holding a count on the request.
227 */
228int
229nfs_wait_on_request(struct nfs_page *req)
230{
Trond Myklebust464a98b2005-06-22 17:16:21 +0000231 struct rpc_clnt *clnt = NFS_CLIENT(req->wb_context->dentry->d_inode);
232 sigset_t oldmask;
233 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Trond Myklebust464a98b2005-06-22 17:16:21 +0000235 if (!test_bit(PG_BUSY, &req->wb_flags))
236 goto out;
237 /*
238 * Note: the call to rpc_clnt_sigmask() suffices to ensure that we
239 * are not interrupted if intr flag is not set
240 */
241 rpc_clnt_sigmask(clnt, &oldmask);
242 ret = out_of_line_wait_on_bit(&req->wb_flags, PG_BUSY,
243 nfs_wait_bit_interruptible, TASK_INTERRUPTIBLE);
244 rpc_clnt_sigunmask(clnt, &oldmask);
245out:
246 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249/**
250 * nfs_coalesce_requests - Split coalesced requests out from a list.
251 * @head: source list
252 * @dst: destination list
253 * @nmax: maximum number of requests to coalesce
254 *
255 * Moves a maximum of 'nmax' elements from one list to another.
256 * The elements are checked to ensure that they form a contiguous set
257 * of pages, and that the RPC credentials are the same.
258 */
259int
260nfs_coalesce_requests(struct list_head *head, struct list_head *dst,
261 unsigned int nmax)
262{
263 struct nfs_page *req = NULL;
264 unsigned int npages = 0;
265
266 while (!list_empty(head)) {
267 struct nfs_page *prev = req;
268
269 req = nfs_list_entry(head->next);
270 if (prev) {
271 if (req->wb_context->cred != prev->wb_context->cred)
272 break;
273 if (req->wb_context->lockowner != prev->wb_context->lockowner)
274 break;
275 if (req->wb_context->state != prev->wb_context->state)
276 break;
277 if (req->wb_index != (prev->wb_index + 1))
278 break;
279
280 if (req->wb_pgbase != 0)
281 break;
282 }
283 nfs_list_remove_request(req);
284 nfs_list_add_request(req, dst);
285 npages++;
286 if (req->wb_pgbase + req->wb_bytes != PAGE_CACHE_SIZE)
287 break;
288 if (npages >= nmax)
289 break;
290 }
291 return npages;
292}
293
294/**
295 * nfs_scan_list - Scan a list for matching requests
296 * @head: One of the NFS inode request lists
297 * @dst: Destination list
298 * @idx_start: lower bound of page->index to scan
299 * @npages: idx_start + npages sets the upper bound to scan.
300 *
301 * Moves elements from one of the inode request lists.
302 * If the number of requests is set to 0, the entire address_space
303 * starting at index idx_start, is scanned.
304 * The requests are *not* checked to ensure that they form a contiguous set.
305 * You must be holding the inode's req_lock when calling this function
306 */
307int
308nfs_scan_list(struct list_head *head, struct list_head *dst,
309 unsigned long idx_start, unsigned int npages)
310{
311 struct list_head *pos, *tmp;
312 struct nfs_page *req;
313 unsigned long idx_end;
314 int res;
315
316 res = 0;
317 if (npages == 0)
318 idx_end = ~0;
319 else
320 idx_end = idx_start + npages - 1;
321
322 list_for_each_safe(pos, tmp, head) {
323
324 req = nfs_list_entry(pos);
325
326 if (req->wb_index < idx_start)
327 continue;
328 if (req->wb_index > idx_end)
329 break;
330
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000331 if (!nfs_set_page_writeback_locked(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 continue;
333 nfs_list_remove_request(req);
334 nfs_list_add_request(req, dst);
335 res++;
336 }
337 return res;
338}
339
340int nfs_init_nfspagecache(void)
341{
342 nfs_page_cachep = kmem_cache_create("nfs_page",
343 sizeof(struct nfs_page),
344 0, SLAB_HWCACHE_ALIGN,
345 NULL, NULL);
346 if (nfs_page_cachep == NULL)
347 return -ENOMEM;
348
349 return 0;
350}
351
352void nfs_destroy_nfspagecache(void)
353{
354 if (kmem_cache_destroy(nfs_page_cachep))
355 printk(KERN_INFO "nfs_page: not all structures were freed\n");
356}
357