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/slab.h>
13 #include <linux/file.h>
14 #include <linux/sched.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 #include "internal.h"
23 #include "pnfs.h"
24 
25 static struct kmem_cache *nfs_page_cachep;
26 
27 static inline struct nfs_page *
nfs_page_alloc(void)28 nfs_page_alloc(void)
29 {
30 	struct nfs_page	*p = kmem_cache_zalloc(nfs_page_cachep, GFP_KERNEL);
31 	if (p)
32 		INIT_LIST_HEAD(&p->wb_list);
33 	return p;
34 }
35 
36 static inline void
nfs_page_free(struct nfs_page * p)37 nfs_page_free(struct nfs_page *p)
38 {
39 	kmem_cache_free(nfs_page_cachep, p);
40 }
41 
42 /**
43  * nfs_create_request - Create an NFS read/write request.
44  * @file: file descriptor to use
45  * @inode: inode to which the request is attached
46  * @page: page to write
47  * @offset: starting offset within the page for the write
48  * @count: number of bytes to read/write
49  *
50  * The page must be locked by the caller. This makes sure we never
51  * create two different requests for the same page.
52  * User should ensure it is safe to sleep in this function.
53  */
54 struct nfs_page *
nfs_create_request(struct nfs_open_context * ctx,struct inode * inode,struct page * page,unsigned int offset,unsigned int count)55 nfs_create_request(struct nfs_open_context *ctx, struct inode *inode,
56 		   struct page *page,
57 		   unsigned int offset, unsigned int count)
58 {
59 	struct nfs_page		*req;
60 
61 	/* try to allocate the request struct */
62 	req = nfs_page_alloc();
63 	if (req == NULL)
64 		return ERR_PTR(-ENOMEM);
65 
66 	/* get lock context early so we can deal with alloc failures */
67 	req->wb_lock_context = nfs_get_lock_context(ctx);
68 	if (req->wb_lock_context == NULL) {
69 		nfs_page_free(req);
70 		return ERR_PTR(-ENOMEM);
71 	}
72 
73 	/* Initialize the request struct. Initially, we assume a
74 	 * long write-back delay. This will be adjusted in
75 	 * update_nfs_request below if the region is not locked. */
76 	req->wb_page    = page;
77 	atomic_set(&req->wb_complete, 0);
78 	req->wb_index	= page->index;
79 	page_cache_get(page);
80 	BUG_ON(PagePrivate(page));
81 	BUG_ON(!PageLocked(page));
82 	BUG_ON(page->mapping->host != inode);
83 	req->wb_offset  = offset;
84 	req->wb_pgbase	= offset;
85 	req->wb_bytes   = count;
86 	req->wb_context = get_nfs_open_context(ctx);
87 	kref_init(&req->wb_kref);
88 	return req;
89 }
90 
91 /**
92  * nfs_unlock_request - Unlock request and wake up sleepers.
93  * @req:
94  */
nfs_unlock_request(struct nfs_page * req)95 void nfs_unlock_request(struct nfs_page *req)
96 {
97 	if (!NFS_WBACK_BUSY(req)) {
98 		printk(KERN_ERR "NFS: Invalid unlock attempted\n");
99 		BUG();
100 	}
101 	smp_mb__before_clear_bit();
102 	clear_bit(PG_BUSY, &req->wb_flags);
103 	smp_mb__after_clear_bit();
104 	wake_up_bit(&req->wb_flags, PG_BUSY);
105 	nfs_release_request(req);
106 }
107 
108 /**
109  * nfs_set_page_tag_locked - Tag a request as locked
110  * @req:
111  */
nfs_set_page_tag_locked(struct nfs_page * req)112 int nfs_set_page_tag_locked(struct nfs_page *req)
113 {
114 	if (!nfs_lock_request_dontget(req))
115 		return 0;
116 	if (test_bit(PG_MAPPED, &req->wb_flags))
117 		radix_tree_tag_set(&NFS_I(req->wb_context->path.dentry->d_inode)->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_LOCKED);
118 	return 1;
119 }
120 
121 /**
122  * nfs_clear_page_tag_locked - Clear request tag and wake up sleepers
123  */
nfs_clear_page_tag_locked(struct nfs_page * req)124 void nfs_clear_page_tag_locked(struct nfs_page *req)
125 {
126 	if (test_bit(PG_MAPPED, &req->wb_flags)) {
127 		struct inode *inode = req->wb_context->path.dentry->d_inode;
128 		struct nfs_inode *nfsi = NFS_I(inode);
129 
130 		spin_lock(&inode->i_lock);
131 		radix_tree_tag_clear(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_LOCKED);
132 		nfs_unlock_request(req);
133 		spin_unlock(&inode->i_lock);
134 	} else
135 		nfs_unlock_request(req);
136 }
137 
138 /*
139  * nfs_clear_request - Free up all resources allocated to the request
140  * @req:
141  *
142  * Release page and open context resources associated with a read/write
143  * request after it has completed.
144  */
nfs_clear_request(struct nfs_page * req)145 static void nfs_clear_request(struct nfs_page *req)
146 {
147 	struct page *page = req->wb_page;
148 	struct nfs_open_context *ctx = req->wb_context;
149 	struct nfs_lock_context *l_ctx = req->wb_lock_context;
150 
151 	if (page != NULL) {
152 		page_cache_release(page);
153 		req->wb_page = NULL;
154 	}
155 	if (l_ctx != NULL) {
156 		nfs_put_lock_context(l_ctx);
157 		req->wb_lock_context = NULL;
158 	}
159 	if (ctx != NULL) {
160 		put_nfs_open_context(ctx);
161 		req->wb_context = NULL;
162 	}
163 }
164 
165 
166 /**
167  * nfs_release_request - Release the count on an NFS read/write request
168  * @req: request to release
169  *
170  * Note: Should never be called with the spinlock held!
171  */
nfs_free_request(struct kref * kref)172 static void nfs_free_request(struct kref *kref)
173 {
174 	struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
175 
176 	/* Release struct file and open context */
177 	nfs_clear_request(req);
178 	nfs_page_free(req);
179 }
180 
nfs_release_request(struct nfs_page * req)181 void nfs_release_request(struct nfs_page *req)
182 {
183 	kref_put(&req->wb_kref, nfs_free_request);
184 }
185 
nfs_wait_bit_uninterruptible(void * word)186 static int nfs_wait_bit_uninterruptible(void *word)
187 {
188 	io_schedule();
189 	return 0;
190 }
191 
192 /**
193  * nfs_wait_on_request - Wait for a request to complete.
194  * @req: request to wait upon.
195  *
196  * Interruptible by fatal signals only.
197  * The user is responsible for holding a count on the request.
198  */
199 int
nfs_wait_on_request(struct nfs_page * req)200 nfs_wait_on_request(struct nfs_page *req)
201 {
202 	return wait_on_bit(&req->wb_flags, PG_BUSY,
203 			nfs_wait_bit_uninterruptible,
204 			TASK_UNINTERRUPTIBLE);
205 }
206 
207 /**
208  * nfs_pageio_init - initialise a page io descriptor
209  * @desc: pointer to descriptor
210  * @inode: pointer to inode
211  * @doio: pointer to io function
212  * @bsize: io block size
213  * @io_flags: extra parameters for the io function
214  */
nfs_pageio_init(struct nfs_pageio_descriptor * desc,struct inode * inode,int (* doio)(struct nfs_pageio_descriptor *),size_t bsize,int io_flags)215 void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
216 		     struct inode *inode,
217 		     int (*doio)(struct nfs_pageio_descriptor *),
218 		     size_t bsize,
219 		     int io_flags)
220 {
221 	INIT_LIST_HEAD(&desc->pg_list);
222 	desc->pg_bytes_written = 0;
223 	desc->pg_count = 0;
224 	desc->pg_bsize = bsize;
225 	desc->pg_base = 0;
226 	desc->pg_moreio = 0;
227 	desc->pg_inode = inode;
228 	desc->pg_doio = doio;
229 	desc->pg_ioflags = io_flags;
230 	desc->pg_error = 0;
231 	desc->pg_lseg = NULL;
232 }
233 
234 /**
235  * nfs_can_coalesce_requests - test two requests for compatibility
236  * @prev: pointer to nfs_page
237  * @req: pointer to nfs_page
238  *
239  * The nfs_page structures 'prev' and 'req' are compared to ensure that the
240  * page data area they describe is contiguous, and that their RPC
241  * credentials, NFSv4 open state, and lockowners are the same.
242  *
243  * Return 'true' if this is the case, else return 'false'.
244  */
nfs_can_coalesce_requests(struct nfs_page * prev,struct nfs_page * req,struct nfs_pageio_descriptor * pgio)245 static int nfs_can_coalesce_requests(struct nfs_page *prev,
246 				     struct nfs_page *req,
247 				     struct nfs_pageio_descriptor *pgio)
248 {
249 	if (req->wb_context->cred != prev->wb_context->cred)
250 		return 0;
251 	if (req->wb_lock_context->lockowner != prev->wb_lock_context->lockowner)
252 		return 0;
253 	if (req->wb_context->state != prev->wb_context->state)
254 		return 0;
255 	if (req->wb_index != (prev->wb_index + 1))
256 		return 0;
257 	if (req->wb_pgbase != 0)
258 		return 0;
259 	if (prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
260 		return 0;
261 	/*
262 	 * Non-whole file layouts need to check that req is inside of
263 	 * pgio->pg_lseg.
264 	 */
265 	if (pgio->pg_test && !pgio->pg_test(pgio, prev, req))
266 		return 0;
267 	return 1;
268 }
269 
270 /**
271  * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
272  * @desc: destination io descriptor
273  * @req: request
274  *
275  * Returns true if the request 'req' was successfully coalesced into the
276  * existing list of pages 'desc'.
277  */
nfs_pageio_do_add_request(struct nfs_pageio_descriptor * desc,struct nfs_page * req)278 static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
279 				     struct nfs_page *req)
280 {
281 	size_t newlen = req->wb_bytes;
282 
283 	if (desc->pg_count != 0) {
284 		struct nfs_page *prev;
285 
286 		/*
287 		 * FIXME: ideally we should be able to coalesce all requests
288 		 * that are not block boundary aligned, but currently this
289 		 * is problematic for the case of bsize < PAGE_CACHE_SIZE,
290 		 * since nfs_flush_multi and nfs_pagein_multi assume you
291 		 * can have only one struct nfs_page.
292 		 */
293 		if (desc->pg_bsize < PAGE_SIZE)
294 			return 0;
295 		newlen += desc->pg_count;
296 		if (newlen > desc->pg_bsize)
297 			return 0;
298 		prev = nfs_list_entry(desc->pg_list.prev);
299 		if (!nfs_can_coalesce_requests(prev, req, desc))
300 			return 0;
301 	} else
302 		desc->pg_base = req->wb_pgbase;
303 	nfs_list_remove_request(req);
304 	nfs_list_add_request(req, &desc->pg_list);
305 	desc->pg_count = newlen;
306 	return 1;
307 }
308 
309 /*
310  * Helper for nfs_pageio_add_request and nfs_pageio_complete
311  */
nfs_pageio_doio(struct nfs_pageio_descriptor * desc)312 static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
313 {
314 	if (!list_empty(&desc->pg_list)) {
315 		int error = desc->pg_doio(desc);
316 		if (error < 0)
317 			desc->pg_error = error;
318 		else
319 			desc->pg_bytes_written += desc->pg_count;
320 	}
321 	if (list_empty(&desc->pg_list)) {
322 		desc->pg_count = 0;
323 		desc->pg_base = 0;
324 	}
325 }
326 
327 /**
328  * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
329  * @desc: destination io descriptor
330  * @req: request
331  *
332  * Returns true if the request 'req' was successfully coalesced into the
333  * existing list of pages 'desc'.
334  */
nfs_pageio_add_request(struct nfs_pageio_descriptor * desc,struct nfs_page * req)335 int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
336 			   struct nfs_page *req)
337 {
338 	while (!nfs_pageio_do_add_request(desc, req)) {
339 		desc->pg_moreio = 1;
340 		nfs_pageio_doio(desc);
341 		if (desc->pg_error < 0)
342 			return 0;
343 		desc->pg_moreio = 0;
344 	}
345 	return 1;
346 }
347 
348 /**
349  * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
350  * @desc: pointer to io descriptor
351  */
nfs_pageio_complete(struct nfs_pageio_descriptor * desc)352 void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
353 {
354 	nfs_pageio_doio(desc);
355 }
356 
357 /**
358  * nfs_pageio_cond_complete - Conditional I/O completion
359  * @desc: pointer to io descriptor
360  * @index: page index
361  *
362  * It is important to ensure that processes don't try to take locks
363  * on non-contiguous ranges of pages as that might deadlock. This
364  * function should be called before attempting to wait on a locked
365  * nfs_page. It will complete the I/O if the page index 'index'
366  * is not contiguous with the existing list of pages in 'desc'.
367  */
nfs_pageio_cond_complete(struct nfs_pageio_descriptor * desc,pgoff_t index)368 void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
369 {
370 	if (!list_empty(&desc->pg_list)) {
371 		struct nfs_page *prev = nfs_list_entry(desc->pg_list.prev);
372 		if (index != prev->wb_index + 1)
373 			nfs_pageio_doio(desc);
374 	}
375 }
376 
377 #define NFS_SCAN_MAXENTRIES 16
378 /**
379  * nfs_scan_list - Scan a list for matching requests
380  * @nfsi: NFS inode
381  * @dst: Destination list
382  * @idx_start: lower bound of page->index to scan
383  * @npages: idx_start + npages sets the upper bound to scan.
384  * @tag: tag to scan for
385  *
386  * Moves elements from one of the inode request lists.
387  * If the number of requests is set to 0, the entire address_space
388  * starting at index idx_start, is scanned.
389  * The requests are *not* checked to ensure that they form a contiguous set.
390  * You must be holding the inode's i_lock when calling this function
391  */
nfs_scan_list(struct nfs_inode * nfsi,struct list_head * dst,pgoff_t idx_start,unsigned int npages,int tag)392 int nfs_scan_list(struct nfs_inode *nfsi,
393 		struct list_head *dst, pgoff_t idx_start,
394 		unsigned int npages, int tag)
395 {
396 	struct nfs_page *pgvec[NFS_SCAN_MAXENTRIES];
397 	struct nfs_page *req;
398 	pgoff_t idx_end;
399 	int found, i;
400 	int res;
401 	struct list_head *list;
402 
403 	res = 0;
404 	if (npages == 0)
405 		idx_end = ~0;
406 	else
407 		idx_end = idx_start + npages - 1;
408 
409 	for (;;) {
410 		found = radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree,
411 				(void **)&pgvec[0], idx_start,
412 				NFS_SCAN_MAXENTRIES, tag);
413 		if (found <= 0)
414 			break;
415 		for (i = 0; i < found; i++) {
416 			req = pgvec[i];
417 			if (req->wb_index > idx_end)
418 				goto out;
419 			idx_start = req->wb_index + 1;
420 			if (nfs_set_page_tag_locked(req)) {
421 				kref_get(&req->wb_kref);
422 				radix_tree_tag_clear(&nfsi->nfs_page_tree,
423 						req->wb_index, tag);
424 				list = pnfs_choose_commit_list(req, dst);
425 				nfs_list_add_request(req, list);
426 				res++;
427 				if (res == INT_MAX)
428 					goto out;
429 			}
430 		}
431 		/* for latency reduction */
432 		cond_resched_lock(&nfsi->vfs_inode.i_lock);
433 	}
434 out:
435 	return res;
436 }
437 
nfs_init_nfspagecache(void)438 int __init nfs_init_nfspagecache(void)
439 {
440 	nfs_page_cachep = kmem_cache_create("nfs_page",
441 					    sizeof(struct nfs_page),
442 					    0, SLAB_HWCACHE_ALIGN,
443 					    NULL);
444 	if (nfs_page_cachep == NULL)
445 		return -ENOMEM;
446 
447 	return 0;
448 }
449 
nfs_destroy_nfspagecache(void)450 void nfs_destroy_nfspagecache(void)
451 {
452 	kmem_cache_destroy(nfs_page_cachep);
453 }
454 
455