Whamcloud - gitweb
Land b1_2 onto HEAD (20040304_171022)
[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         page = llap->llap_page;
65         LASSERT(PageLocked(page));
66
67         if (rc == 0)  {
68                 if (cmd == OBD_BRW_READ) {
69                         if (!llap->llap_defer_uptodate)
70                                 SetPageUptodate(page);
71                 } else {
72                         llap->llap_write_queued = 0;
73                 }
74         } else {
75                 SetPageError(page);
76         }
77
78         LL_CDEBUG_PAGE(D_PAGE, page, "io complete, unlocking\n");
79
80         unlock_page(page);
81
82         if (0 && cmd == OBD_BRW_WRITE) {
83                 llap_write_complete(page->mapping->host, llap);
84                 ll_try_done_writing(page->mapping->host);
85         }
86
87         page_cache_release(page);
88 }
89
90 static int ll_writepage_24(struct page *page)
91 {
92         struct inode *inode = page->mapping->host;
93         struct obd_export *exp;
94         struct ll_async_page *llap;
95         int rc = 0;
96         ENTRY;
97
98         LASSERT(!PageDirty(page));
99         LASSERT(PageLocked(page));
100
101         exp = ll_i2obdexp(inode);
102         if (exp == NULL)
103                 GOTO(out, rc = -EINVAL);
104
105         llap = llap_from_page(page);
106         if (IS_ERR(llap))
107                 GOTO(out, rc = PTR_ERR(llap));
108
109         page_cache_get(page);
110         if (llap->llap_write_queued) {
111                 LL_CDEBUG_PAGE(D_PAGE, page, "marking urgent\n");
112                 rc = obd_set_async_flags(exp, ll_i2info(inode)->lli_smd, NULL,
113                                          llap->llap_cookie,
114                                          ASYNC_READY | ASYNC_URGENT);
115         } else {
116                 llap->llap_write_queued = 1;
117                 rc = obd_queue_async_io(exp, ll_i2info(inode)->lli_smd, NULL,
118                                         llap->llap_cookie, OBD_BRW_WRITE, 0, 0,
119                                         0, ASYNC_READY | ASYNC_URGENT);
120                 if (rc == 0)
121                         LL_CDEBUG_PAGE(D_PAGE, page, "mmap write queued\n");
122                 else
123                         llap->llap_write_queued = 0;
124         }
125         if (rc)
126                 page_cache_release(page);
127 out:
128         if (rc)
129                 unlock_page(page);
130         RETURN(rc);
131 }
132
133 static int ll_direct_IO_24(int rw,
134 #ifdef HAVE_DIO_FILE
135                            struct file *file,
136 #else
137                            struct inode *inode,
138 #endif
139                            struct kiobuf *iobuf, unsigned long blocknr,
140                            int blocksize)
141 {
142 #ifdef HAVE_DIO_FILE
143         struct inode *inode = file->f_dentry->d_inode;
144 #endif
145         struct ll_inode_info *lli = ll_i2info(inode);
146         struct lov_stripe_md *lsm = lli->lli_smd;
147         struct brw_page *pga;
148         struct ptlrpc_request_set *set;
149         struct obdo oa;
150         int length, i, flags, rc = 0;
151         loff_t offset;
152         ENTRY;
153
154         if (!lsm || !lsm->lsm_object_id)
155                 RETURN(-EBADF);
156
157         /* FIXME: io smaller than PAGE_SIZE is broken on ia64 */
158         if ((iobuf->offset & (PAGE_SIZE - 1)) ||
159             (iobuf->length & (PAGE_SIZE - 1)))
160                 RETURN(-EINVAL);
161
162         set = ptlrpc_prep_set();
163         if (set == NULL)
164                 RETURN(-ENOMEM);
165
166         OBD_ALLOC(pga, sizeof(*pga) * iobuf->nr_pages);
167         if (!pga) {
168                 ptlrpc_set_destroy(set);
169                 RETURN(-ENOMEM);
170         }
171
172         flags = 0 /* | OBD_BRW_DIRECTIO */;
173         offset = ((obd_off)blocknr << inode->i_blkbits);
174         length = iobuf->length;
175
176         for (i = 0, length = iobuf->length; length > 0;
177              length -= pga[i].count, offset += pga[i].count, i++) { /*i last!*/
178                 pga[i].pg = iobuf->maplist[i];
179                 pga[i].off = offset;
180                 /* To the end of the page, or the length, whatever is less */
181                 pga[i].count = min_t(int, PAGE_SIZE - (offset & ~PAGE_MASK),
182                                      length);
183                 pga[i].flag = flags;
184                 if (rw == READ)
185                         POISON_PAGE(iobuf->maplist[i], 0x0d);
186         }
187
188         ll_inode_fill_obdo(inode, rw, &oa);
189
190         if (rw == WRITE)
191                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
192                                     LPROC_LL_DIRECT_WRITE, iobuf->length);
193         else
194                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
195                                     LPROC_LL_DIRECT_READ, iobuf->length);
196         rc = obd_brw_async(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
197                            ll_i2obdexp(inode), &oa, lsm, iobuf->nr_pages, pga,
198                            set, NULL);
199         if (rc) {
200                 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR,
201                        "error from obd_brw_async: rc = %d\n", rc);
202         } else {
203                 rc = ptlrpc_set_wait(set);
204                 if (rc)
205                         CERROR("error from callback: rc = %d\n", rc);
206         }
207         ptlrpc_set_destroy(set);
208         if (rc == 0) {
209                 rc = iobuf->length;
210                 obdo_to_inode(inode, &oa, OBD_MD_FLBLOCKS);
211         }
212
213         OBD_FREE(pga, sizeof(*pga) * iobuf->nr_pages);
214         RETURN(rc);
215 }
216
217 struct address_space_operations ll_aops = {
218         readpage: ll_readpage,
219         direct_IO: ll_direct_IO_24,
220         writepage: ll_writepage_24,
221         prepare_write: ll_prepare_write,
222         commit_write: ll_commit_write,
223         removepage: ll_removepage,
224         sync_page: NULL,
225         bmap: NULL
226 };