blob: 9a70ebdec56e11357ba1f78c0938385881c7d4b3 [file] [log] [blame]
Venkateswararao Jujjuri (JV)022cae362011-01-28 14:11:13 -08001/*
2 * Copyright IBM Corporation, 2010
3 * Author Venkateswararao Jujjuri <jvrao@linux.vnet.ibm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2.1 of the GNU Lesser General Public License
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 */
14
15#include <linux/slab.h>
16#include <linux/module.h>
17#include <net/9p/9p.h>
18#include <net/9p/client.h>
19#include <linux/scatterlist.h>
20#include "trans_common.h"
21
22/**
23 * p9_release_req_pages - Release pages after the transaction.
24 * @*private: PDU's private page of struct trans_rpage_info
25 */
26void
27p9_release_req_pages(struct trans_rpage_info *rpinfo)
28{
29 int i = 0;
30
31 while (rpinfo->rp_data[i] && rpinfo->rp_nr_pages--) {
32 put_page(rpinfo->rp_data[i]);
33 i++;
34 }
35}
36EXPORT_SYMBOL(p9_release_req_pages);
37
38/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -030039 * p9_nr_pages - Return number of pages needed to accommodate the payload.
Venkateswararao Jujjuri (JV)022cae362011-01-28 14:11:13 -080040 */
41int
42p9_nr_pages(struct p9_req_t *req)
43{
Aneesh Kumar K.V472e7f92011-03-08 16:39:47 +053044 unsigned long start_page, end_page;
45 start_page = (unsigned long)req->tc->pubuf >> PAGE_SHIFT;
46 end_page = ((unsigned long)req->tc->pubuf + req->tc->pbuf_size +
Venkateswararao Jujjuri (JV)022cae362011-01-28 14:11:13 -080047 PAGE_SIZE - 1) >> PAGE_SHIFT;
48 return end_page - start_page;
49}
50EXPORT_SYMBOL(p9_nr_pages);
51
52/**
53 * payload_gup - Translates user buffer into kernel pages and
54 * pins them either for read/write through get_user_pages_fast().
55 * @req: Request to be sent to server.
56 * @pdata_off: data offset into the first page after translation (gup).
57 * @pdata_len: Total length of the IO. gup may not return requested # of pages.
Lucas De Marchi25985ed2011-03-30 22:57:33 -030058 * @nr_pages: number of pages to accommodate the payload
Venkateswararao Jujjuri (JV)022cae362011-01-28 14:11:13 -080059 * @rw: Indicates if the pages are for read or write.
60 */
61int
62p9_payload_gup(struct p9_req_t *req, size_t *pdata_off, int *pdata_len,
63 int nr_pages, u8 rw)
64{
65 uint32_t first_page_bytes = 0;
M. Mohan Kumar3cd79672011-04-15 13:59:33 +053066 int32_t pdata_mapped_pages;
Venkateswararao Jujjuri (JV)022cae362011-01-28 14:11:13 -080067 struct trans_rpage_info *rpinfo;
68
Aneesh Kumar K.Vbd8c8ad2011-03-24 23:14:46 +053069 *pdata_off = (__force size_t)req->tc->pubuf & (PAGE_SIZE-1);
Venkateswararao Jujjuri (JV)022cae362011-01-28 14:11:13 -080070
71 if (*pdata_off)
Aneesh Kumar K.V472e7f92011-03-08 16:39:47 +053072 first_page_bytes = min(((size_t)PAGE_SIZE - *pdata_off),
73 req->tc->pbuf_size);
Venkateswararao Jujjuri (JV)022cae362011-01-28 14:11:13 -080074
75 rpinfo = req->tc->private;
76 pdata_mapped_pages = get_user_pages_fast((unsigned long)req->tc->pubuf,
77 nr_pages, rw, &rpinfo->rp_data[0]);
M. Mohan Kumar3cd79672011-04-15 13:59:33 +053078 if (pdata_mapped_pages <= 0)
79 return pdata_mapped_pages;
Venkateswararao Jujjuri (JV)022cae362011-01-28 14:11:13 -080080
Venkateswararao Jujjuri (JV)022cae362011-01-28 14:11:13 -080081 rpinfo->rp_nr_pages = pdata_mapped_pages;
82 if (*pdata_off) {
83 *pdata_len = first_page_bytes;
84 *pdata_len += min((req->tc->pbuf_size - *pdata_len),
85 ((size_t)pdata_mapped_pages - 1) << PAGE_SHIFT);
86 } else {
87 *pdata_len = min(req->tc->pbuf_size,
88 (size_t)pdata_mapped_pages << PAGE_SHIFT);
89 }
90 return 0;
91}
92EXPORT_SYMBOL(p9_payload_gup);