Whamcloud - gitweb
land b1_5 onto HEAD
[fs/lustre-release.git] / lustre / llite / rw26.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 routines for the 2.5/2.6 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 #ifdef HAVE_KERNEL_CONFIG_H
24 #include <linux/config.h>
25 #endif
26 #include <linux/kernel.h>
27 #include <linux/mm.h>
28 #include <linux/string.h>
29 #include <linux/stat.h>
30 #include <linux/errno.h>
31 #include <linux/smp_lock.h>
32 #include <linux/unistd.h>
33 #include <linux/version.h>
34 #include <asm/system.h>
35 #include <asm/uaccess.h>
36
37 #include <linux/fs.h>
38 #include <linux/buffer_head.h>
39 #include <linux/mpage.h>
40 #include <linux/writeback.h>
41 #include <linux/stat.h>
42 #include <asm/uaccess.h>
43 #include <asm/segment.h>
44 #include <linux/mm.h>
45 #include <linux/pagemap.h>
46 #include <linux/smp_lock.h>
47
48 #define DEBUG_SUBSYSTEM S_LLITE
49
50 #include <lustre_lite.h>
51 #include "llite_internal.h"
52 #include <linux/lustre_compat25.h>
53
54 static int ll_writepage_26(struct page *page, struct writeback_control *wbc)
55 {
56         return ll_writepage(page);
57 }
58
59 /* It is safe to not check anything in invalidatepage/releasepage below
60    because they are run with page locked and all our io is happening with
61    locked page too */
62 #ifdef HAVE_INVALIDATEPAGE_RETURN_INT
63 static int ll_invalidatepage(struct page *page, unsigned long offset)
64 {
65         if (offset)
66                 return 0;
67         if (PagePrivate(page))
68                 ll_removepage(page);
69         return 1;
70 }
71 #else
72 static void ll_invalidatepage(struct page *page, unsigned long offset)
73 {
74         if (offset)
75                 return;
76         if (PagePrivate(page))
77                 ll_removepage(page);
78 }
79 #endif
80
81 static int ll_releasepage(struct page *page, gfp_t gfp_mask)
82 {
83         if (PagePrivate(page))
84                 ll_removepage(page);
85         return 1;
86 }
87
88 #define MAX_DIRECTIO_SIZE 2*1024*1024*1024UL
89
90 static inline int ll_get_user_pages(int rw, unsigned long user_addr,
91                                     size_t size, struct page ***pages)
92 {
93         int result = -ENOMEM;
94         unsigned long page_count;
95
96         /* set an arbitrary limit to prevent arithmetic overflow */
97         if (size > MAX_DIRECTIO_SIZE) {
98                 *pages = NULL;
99                 return -EFBIG;
100         }
101
102         page_count = (user_addr + size + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
103         page_count -= user_addr >> CFS_PAGE_SHIFT;
104
105         OBD_ALLOC_GFP(*pages, page_count * sizeof(**pages), GFP_KERNEL);
106         if (*pages) {
107                 down_read(&current->mm->mmap_sem);
108                 result = get_user_pages(current, current->mm, user_addr,
109                                         page_count, (rw == READ), 0, *pages,
110                                         NULL);
111                 up_read(&current->mm->mmap_sem);
112         }
113
114         return result;
115 }
116
117 /*  ll_free_user_pages - tear down page struct array
118  *  @pages: array of page struct pointers underlying target buffer */
119 static void ll_free_user_pages(struct page **pages, int npages, int do_dirty)
120 {
121         int i;
122
123         for (i = 0; i < npages; i++) {
124                 if (do_dirty)
125                         set_page_dirty_lock(pages[i]);
126                 page_cache_release(pages[i]);
127         }
128
129         OBD_FREE(pages, npages * sizeof(*pages));
130 }
131
132 static ssize_t ll_direct_IO_26_seg(int rw, struct file *file,
133                                    struct address_space *mapping,
134                                    struct inode *inode,
135                                    struct lov_stripe_md *lsm,
136                                    unsigned long user_addr, size_t size,
137                                    loff_t file_offset, struct page **pages,
138                                    int page_count)
139 {
140         struct brw_page *pga;
141         struct obdo oa;
142         int i, rc = 0;
143         size_t length;
144         ENTRY;
145
146         OBD_ALLOC(pga, sizeof(*pga) * page_count);
147         if (!pga)
148                 RETURN(-ENOMEM);
149
150         for (i = 0, length = size; length > 0;
151              length -=pga[i].count, file_offset +=pga[i].count,i++) {/*i last!*/
152                 pga[i].pg = pages[i];
153                 pga[i].off = file_offset;
154                 /* To the end of the page, or the length, whatever is less */
155                 pga[i].count = min_t(int, CFS_PAGE_SIZE -(file_offset & ~CFS_PAGE_MASK),
156                                      length);
157                 pga[i].flag = 0;
158                 if (rw == READ)
159                         POISON_PAGE(pages[i], 0x0d);
160         }
161
162         ll_inode_fill_obdo(inode, rw, &oa);
163
164         if (rw == WRITE)
165                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
166                                     LPROC_LL_DIRECT_WRITE, size);
167         else
168                 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats,
169                                     LPROC_LL_DIRECT_READ, size);
170         rc = obd_brw_rqset(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
171                            ll_i2obdexp(inode), &oa, lsm, page_count, pga, NULL);
172         if (rc == 0) {
173                 rc = size;
174                 if (rw == WRITE) {
175                         lov_stripe_lock(lsm);
176                         obd_adjust_kms(ll_i2obdexp(inode), lsm, file_offset, 0);
177                         lov_stripe_unlock(lsm);
178                 }
179         }
180
181         OBD_FREE(pga, sizeof(*pga) * page_count);
182         RETURN(rc);
183 }
184
185 static ssize_t ll_direct_IO_26(int rw, struct kiocb *iocb,
186                                const struct iovec *iov, loff_t file_offset,
187                                unsigned long nr_segs)
188 {
189         struct file *file = iocb->ki_filp;
190         ssize_t count = iov_length(iov, nr_segs), tot_bytes = 0;
191         struct ll_inode_info *lli = ll_i2info(file->f_mapping->host);
192         unsigned long seg;
193         ENTRY;
194
195         if (!lli->lli_smd || !lli->lli_smd->lsm_object_id)
196                 RETURN(-EBADF);
197
198         /* FIXME: io smaller than CFS_PAGE_SIZE is broken on ia64 ??? */
199         if ((file_offset & (~CFS_PAGE_MASK)) || (count & ~CFS_PAGE_MASK))
200                 RETURN(-EINVAL);
201
202         /* Check that all user buffers are aligned as well */
203         for (seg = 0; seg < nr_segs; seg++) {
204                 unsigned long user_addr = (unsigned long)iov[seg].iov_base;
205                 size_t size = iov[seg].iov_len;
206                 if ((user_addr & ~CFS_PAGE_MASK) || (size & ~CFS_PAGE_MASK))
207                         RETURN(-EINVAL);
208         }
209
210         seg = 0;
211         while ((seg < nr_segs) && (tot_bytes >= 0)) {
212                 const struct iovec *vec = &iov[seg++];
213                 unsigned long user_addr = (unsigned long)vec->iov_base;
214                 size_t size = vec->iov_len;
215                 struct page **pages;
216                 int page_count;
217                 ssize_t result;
218
219                 page_count = ll_get_user_pages(rw, user_addr, size, &pages);
220                 if (page_count < 0) {
221                         ll_free_user_pages(pages, 0, 0);
222                         if (tot_bytes > 0)
223                                 break;
224                         return page_count;
225                 }
226
227                 result = ll_direct_IO_26_seg(rw, file, file->f_mapping,
228                                              file->f_mapping->host,
229                                              lli->lli_smd, user_addr, size,
230                                              file_offset, pages, page_count);
231                 ll_free_user_pages(pages, page_count, rw == READ);
232
233                 if (result <= 0) {
234                         if (tot_bytes > 0)
235                                 break;
236                         return result;
237                 }
238
239                 tot_bytes += result;
240                 file_offset += result;
241                 if (result < size)
242                         break;
243         }
244         return tot_bytes;
245 }
246
247 struct address_space_operations ll_aops = {
248         .readpage       = ll_readpage,
249 //        .readpages      = ll_readpages,
250         .direct_IO      = ll_direct_IO_26,
251         .writepage      = ll_writepage_26,
252         .writepages     = generic_writepages,
253         .set_page_dirty = __set_page_dirty_nobuffers,
254         .sync_page      = NULL,
255         .prepare_write  = ll_prepare_write,
256         .commit_write   = ll_commit_write,
257         .invalidatepage = ll_invalidatepage,
258         .releasepage    = ll_releasepage,
259         .bmap           = NULL
260 };
261
262 static int wait_on_page_locked_range(struct address_space *mapping,
263                                      pgoff_t start, pgoff_t end)
264 {
265         pgoff_t index;
266         struct page *page;
267         int ret = 0;
268
269         if (end < start)
270                 return 0;
271
272         for(index = start; index < end; index++) {
273                 page = find_get_page(mapping, index);
274                 if (page == NULL)
275                         continue;
276
277                 wait_on_page_locked(page);
278                 if (PageError(page))
279                         ret = -EIO;
280                 cond_resched();
281         }
282
283         /* Check for outstanding write errors */
284         if (test_and_clear_bit(AS_ENOSPC, &mapping->flags))
285                 ret = -ENOSPC;
286         if (test_and_clear_bit(AS_EIO, &mapping->flags))
287                 ret = -EIO;
288
289         return ret;
290 }
291
292 int ll_sync_page_range(struct inode *inode, struct address_space *mapping,
293                        loff_t pos, size_t count)
294 {
295         pgoff_t start = pos >> CFS_PAGE_SHIFT;
296         pgoff_t end = (pos + count - 1) >> CFS_PAGE_SHIFT;
297         struct writeback_control wbc;
298         int ret;
299
300         wbc.sync_mode = WB_SYNC_ALL;
301         wbc.nr_to_write = mapping->nrpages * 2;
302         wbc.start = start;
303         wbc.end = end;
304         ret = generic_writepages(mapping, &wbc);
305
306         if (ret == 0)
307                 ret = wait_on_page_locked_range(mapping, start, end);
308         return ret;
309 }
310