Whamcloud - gitweb
land v0.9.1 on HEAD, in preparation for a 1.0.x branch
[fs/lustre-release.git] / lustre / llite / rw24.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Lite I/O page cache for the 2.4 kernel version
5  *
6  *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include <linux/config.h>
25 #include <linux/kernel.h>
26 #include <linux/mm.h>
27 #include <linux/string.h>
28 #include <linux/stat.h>
29 #include <linux/errno.h>
30 #include <linux/smp_lock.h>
31 #include <linux/unistd.h>
32 #include <linux/version.h>
33 #include <asm/system.h>
34 #include <asm/uaccess.h>
35
36 #include <linux/fs.h>
37 #include <linux/iobuf.h>
38 #include <linux/stat.h>
39 #include <asm/uaccess.h>
40 #include <asm/segment.h>
41 #include <linux/mm.h>
42 #include <linux/pagemap.h>
43 #include <linux/smp_lock.h>
44
45 #define DEBUG_SUBSYSTEM S_LLITE
46
47 #include <linux/lustre_mds.h>
48 #include <linux/lustre_lite.h>
49 #include "llite_internal.h"
50 #include <linux/lustre_compat25.h>
51
52 /* called for each page in a completed rpc.*/
53 void ll_ap_completion_24(void *data, int cmd, int rc)
54 {
55         struct ll_async_page *llap;
56         struct page *page;
57
58         llap = llap_from_cookie(data);
59         if (IS_ERR(llap)) {
60                 EXIT;
61                 return;
62         }
63
64         llap->llap_queued = 0;
65         page = llap->llap_page;
66
67         LASSERT(PageLocked(page));
68
69         if (rc == 0)  {
70                 if (cmd == OBD_BRW_READ)
71                         SetPageUptodate(page);
72         } else { 
73                 SetPageError(page);
74         }
75
76         LL_CDEBUG_PAGE(page, "io complete, unlocking\n");
77
78         unlock_page(page);
79
80         if (0 && cmd == OBD_BRW_WRITE) {
81                 llap_write_complete(page->mapping->host, llap);
82                 ll_try_done_writing(page->mapping->host);
83         }
84
85         page_cache_release(page);
86 }
87
88 static int ll_writepage_24(struct page *page)
89 {
90         struct inode *inode = page->mapping->host;
91         struct obd_export *exp;
92         struct ll_async_page *llap;
93         int rc = 0;
94         ENTRY;
95
96         LASSERT(!PageDirty(page));
97         LASSERT(PageLocked(page));
98
99         exp = ll_i2obdexp(inode);
100         if (exp == NULL)
101                 GOTO(out, rc = -EINVAL);
102
103         llap = llap_from_page(page);
104         if (IS_ERR(llap))
105                 GOTO(out, rc = PTR_ERR(llap));
106
107         page_cache_get(page);
108         if (llap->llap_queued) {
109                 LL_CDEBUG_PAGE(page, "marking urgent\n");
110                 rc = obd_set_async_flags(exp, ll_i2info(inode)->lli_smd, NULL, 
111                                          llap->llap_cookie, ASYNC_READY | 
112                                          ASYNC_URGENT);
113         } else {
114                 rc = obd_queue_async_io(exp, ll_i2info(inode)->lli_smd, NULL, 
115                                         llap->llap_cookie, OBD_BRW_WRITE, 0, 0, 
116                                         OBD_BRW_CREATE, ASYNC_READY | 
117                                         ASYNC_URGENT);
118                 if (rc == 0) {
119                         LL_CDEBUG_PAGE(page, "mmap write queued\n");
120                         llap->llap_queued = 1;
121                 }
122         }
123         if (rc)
124                 page_cache_release(page);
125 out:
126         if (rc)
127                 unlock_page(page);
128         RETURN(rc);
129 }
130
131 static int ll_direct_IO_24(int rw, struct inode *inode, struct kiobuf *iobuf,
132                            unsigned long blocknr, int blocksize)
133 {
134         struct ll_inode_info *lli = ll_i2info(inode);
135         struct lov_stripe_md *lsm = lli->lli_smd;
136         struct brw_page *pga;
137         struct ptlrpc_request_set *set;
138         struct obdo oa;
139         int length, i, flags, rc = 0;
140         loff_t offset;
141         ENTRY;
142
143         if (!lsm || !lsm->lsm_object_id)
144                 RETURN(-EBADF);
145
146         /* FIXME: io smaller than PAGE_SIZE is broken on ia64 */
147         if ((iobuf->offset & (PAGE_SIZE - 1)) ||
148             (iobuf->length & (PAGE_SIZE - 1)))
149                 RETURN(-EINVAL);
150
151         set = ptlrpc_prep_set();
152         if (set == NULL)
153                 RETURN(-ENOMEM);
154
155         OBD_ALLOC(pga, sizeof(*pga) * iobuf->nr_pages);
156         if (!pga) {
157                 ptlrpc_set_destroy(set);
158                 RETURN(-ENOMEM);
159         }
160
161         flags = (rw == WRITE ? OBD_BRW_CREATE : 0) /* | OBD_BRW_DIRECTIO */;
162         offset = ((obd_off)blocknr << inode->i_blkbits);
163         length = iobuf->length;
164
165         for (i = 0, length = iobuf->length; length > 0;
166              length -= pga[i].count, offset += pga[i].count, i++) { /*i last!*/
167                 pga[i].pg = iobuf->maplist[i];
168                 pga[i].off = offset;
169                 /* To the end of the page, or the length, whatever is less */
170                 pga[i].count = min_t(int, PAGE_SIZE - (offset & ~PAGE_MASK),
171                                      length);
172                 pga[i].flag = flags;
173                 if (rw == READ)
174                         POISON_PAGE(iobuf->maplist[i], 0x0d);
175         }
176
177         ll_inode_fill_obdo(inode, rw, &oa);
178
179         if (rw == WRITE)
180                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
181                                     LPROC_LL_DIRECT_WRITE, iobuf->length);
182         else
183                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
184                                     LPROC_LL_DIRECT_READ, iobuf->length);
185         rc = obd_brw_async(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
186                            ll_i2obdexp(inode), &oa, lsm, iobuf->nr_pages, pga,
187                            set, NULL);
188         if (rc) {
189                 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR,
190                        "error from obd_brw_async: rc = %d\n", rc);
191         } else {
192                 rc = ptlrpc_set_wait(set);
193                 if (rc)
194                         CERROR("error from callback: rc = %d\n", rc);
195         }
196         ptlrpc_set_destroy(set);
197         if (rc == 0) {
198                 rc = iobuf->length;
199                 obdo_to_inode(inode, &oa, OBD_MD_FLBLOCKS);
200         }
201
202         OBD_FREE(pga, sizeof(*pga) * iobuf->nr_pages);
203         RETURN(rc);
204 }
205
206 struct address_space_operations ll_aops = {
207         readpage: ll_readpage,
208         direct_IO: ll_direct_IO_24,
209         writepage: ll_writepage_24,
210         prepare_write: ll_prepare_write,
211         commit_write: ll_commit_write,
212         removepage: ll_removepage,
213         bmap: NULL
214 };