ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / nfs / pagelist.c
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
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
24 /*
25  * Spinlock
26  */
27 spinlock_t nfs_wreq_lock = SPIN_LOCK_UNLOCKED;
28
29 static kmem_cache_t *nfs_page_cachep;
30
31 static inline struct nfs_page *
32 nfs_page_alloc(void)
33 {
34         struct nfs_page *p;
35         p = kmem_cache_alloc(nfs_page_cachep, SLAB_KERNEL);
36         if (p) {
37                 memset(p, 0, sizeof(*p));
38                 INIT_LIST_HEAD(&p->wb_list);
39                 init_waitqueue_head(&p->wb_wait);
40         }
41         return p;
42 }
43
44 static inline void
45 nfs_page_free(struct nfs_page *p)
46 {
47         kmem_cache_free(nfs_page_cachep, p);
48 }
49
50 /**
51  * nfs_create_request - Create an NFS read/write request.
52  * @file: file descriptor to use
53  * @inode: inode to which the request is attached
54  * @page: page to write
55  * @offset: starting offset within the page for the write
56  * @count: number of bytes to read/write
57  *
58  * The page must be locked by the caller. This makes sure we never
59  * create two different requests for the same page, and avoids
60  * a possible deadlock when we reach the hard limit on the number
61  * of dirty pages.
62  * User should ensure it is safe to sleep in this function.
63  */
64 struct nfs_page *
65 nfs_create_request(struct file *file, struct inode *inode,
66                    struct page *page,
67                    unsigned int offset, unsigned int count)
68 {
69         struct nfs_server *server = NFS_SERVER(inode);
70         struct nfs_page         *req;
71
72         /* Deal with hard limits.  */
73         for (;;) {
74                 /* try to allocate the request struct */
75                 req = nfs_page_alloc();
76                 if (req != NULL)
77                         break;
78
79                 /* Try to free up at least one request in order to stay
80                  * below the hard limit
81                  */
82                 if (signalled() && (server->flags & NFS_MOUNT_INTR))
83                         return ERR_PTR(-ERESTARTSYS);
84                 yield();
85         }
86
87         /* Initialize the request struct. Initially, we assume a
88          * long write-back delay. This will be adjusted in
89          * update_nfs_request below if the region is not locked. */
90         req->wb_page    = page;
91         atomic_set(&req->wb_complete, 0);
92         req->wb_index   = page->index;
93         page_cache_get(page);
94         req->wb_offset  = offset;
95         req->wb_pgbase  = offset;
96         req->wb_bytes   = count;
97         req->wb_inode   = inode;
98         req->wb_count   = 1;
99         server->rpc_ops->request_init(req, file);
100
101         return req;
102 }
103
104 /**
105  * nfs_clear_request - Free up all resources allocated to the request
106  * @req:
107  *
108  * Release all resources associated with a write request after it
109  * has completed.
110  */
111 void nfs_clear_request(struct nfs_page *req)
112 {
113         if (req->wb_state)
114                 req->wb_state = NULL;
115         /* Release struct file or cached credential */
116         if (req->wb_file) {
117                 fput(req->wb_file);
118                 req->wb_file = NULL;
119         }
120         if (req->wb_cred) {
121                 put_rpccred(req->wb_cred);
122                 req->wb_cred = NULL;
123         }
124         if (req->wb_page) {
125                 page_cache_release(req->wb_page);
126                 req->wb_page = NULL;
127         }
128 }
129
130
131 /**
132  * nfs_release_request - Release the count on an NFS read/write request
133  * @req: request to release
134  *
135  * Note: Should never be called with the spinlock held!
136  */
137 void
138 nfs_release_request(struct nfs_page *req)
139 {
140         spin_lock(&nfs_wreq_lock);
141         if (--req->wb_count) {
142                 spin_unlock(&nfs_wreq_lock);
143                 return;
144         }
145         spin_unlock(&nfs_wreq_lock);
146
147 #ifdef NFS_PARANOIA
148         BUG_ON (!list_empty(&req->wb_list));
149         BUG_ON (NFS_WBACK_BUSY(req));
150 #endif
151
152         /* Release struct file or cached credential */
153         nfs_clear_request(req);
154         nfs_page_free(req);
155 }
156
157 /**
158  * nfs_list_add_request - Insert a request into a sorted list
159  * @req: request
160  * @head: head of list into which to insert the request.
161  *
162  * Note that the wb_list is sorted by page index in order to facilitate
163  * coalescing of requests.
164  * We use an insertion sort that is optimized for the case of appended
165  * writes.
166  */
167 void
168 nfs_list_add_request(struct nfs_page *req, struct list_head *head)
169 {
170         struct list_head *pos;
171
172 #ifdef NFS_PARANOIA
173         if (!list_empty(&req->wb_list)) {
174                 printk(KERN_ERR "NFS: Add to list failed!\n");
175                 BUG();
176         }
177 #endif
178         list_for_each_prev(pos, head) {
179                 struct nfs_page *p = nfs_list_entry(pos);
180                 if (p->wb_index < req->wb_index)
181                         break;
182         }
183         list_add(&req->wb_list, pos);
184         req->wb_list_head = head;
185 }
186
187 /**
188  * nfs_wait_on_request - Wait for a request to complete.
189  * @req: request to wait upon.
190  *
191  * Interruptible by signals only if mounted with intr flag.
192  * The user is responsible for holding a count on the request.
193  */
194 int
195 nfs_wait_on_request(struct nfs_page *req)
196 {
197         struct inode    *inode = req->wb_inode;
198         struct rpc_clnt *clnt = NFS_CLIENT(inode);
199
200         if (!NFS_WBACK_BUSY(req))
201                 return 0;
202         return nfs_wait_event(clnt, req->wb_wait, !NFS_WBACK_BUSY(req));
203 }
204
205 /**
206  * nfs_coalesce_requests - Split coalesced requests out from a list.
207  * @head: source list
208  * @dst: destination list
209  * @nmax: maximum number of requests to coalesce
210  *
211  * Moves a maximum of 'nmax' elements from one list to another.
212  * The elements are checked to ensure that they form a contiguous set
213  * of pages, and that the RPC credentials are the same.
214  */
215 int
216 nfs_coalesce_requests(struct list_head *head, struct list_head *dst,
217                       unsigned int nmax)
218 {
219         struct nfs_page         *req = NULL;
220         unsigned int            npages = 0;
221
222         while (!list_empty(head)) {
223                 struct nfs_page *prev = req;
224
225                 req = nfs_list_entry(head->next);
226                 if (prev) {
227                         if (req->wb_cred != prev->wb_cred)
228                                 break;
229                         if (req->wb_index != (prev->wb_index + 1))
230                                 break;
231
232                         if (req->wb_pgbase != 0)
233                                 break;
234                 }
235                 nfs_list_remove_request(req);
236                 nfs_list_add_request(req, dst);
237                 npages++;
238                 if (req->wb_pgbase + req->wb_bytes != PAGE_CACHE_SIZE)
239                         break;
240                 if (npages >= nmax)
241                         break;
242         }
243         return npages;
244 }
245
246 /**
247  * nfs_scan_list - Scan a list for matching requests
248  * @head: One of the NFS inode request lists
249  * @dst: Destination list
250  * @idx_start: lower bound of page->index to scan
251  * @npages: idx_start + npages sets the upper bound to scan.
252  *
253  * Moves elements from one of the inode request lists.
254  * If the number of requests is set to 0, the entire address_space
255  * starting at index idx_start, is scanned.
256  * The requests are *not* checked to ensure that they form a contiguous set.
257  * You must be holding the nfs_wreq_lock when calling this function
258  */
259 int
260 nfs_scan_list(struct list_head *head, struct list_head *dst,
261               unsigned long idx_start, unsigned int npages)
262 {
263         struct list_head        *pos, *tmp;
264         struct nfs_page         *req;
265         unsigned long           idx_end;
266         int                     res;
267
268         res = 0;
269         if (npages == 0)
270                 idx_end = ~0;
271         else
272                 idx_end = idx_start + npages - 1;
273
274         list_for_each_safe(pos, tmp, head) {
275
276                 req = nfs_list_entry(pos);
277
278                 if (req->wb_index < idx_start)
279                         continue;
280                 if (req->wb_index > idx_end)
281                         break;
282
283                 if (!nfs_lock_request(req))
284                         continue;
285                 nfs_list_remove_request(req);
286                 nfs_list_add_request(req, dst);
287                 res++;
288         }
289         return res;
290 }
291
292 int nfs_init_nfspagecache(void)
293 {
294         nfs_page_cachep = kmem_cache_create("nfs_page",
295                                             sizeof(struct nfs_page),
296                                             0, SLAB_HWCACHE_ALIGN,
297                                             NULL, NULL);
298         if (nfs_page_cachep == NULL)
299                 return -ENOMEM;
300
301         return 0;
302 }
303
304 void nfs_destroy_nfspagecache(void)
305 {
306         if (kmem_cache_destroy(nfs_page_cachep))
307                 printk(KERN_INFO "nfs_page: not all structures were freed\n");
308 }
309