Whamcloud - gitweb
Branch b1_8
[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 #ifndef AUTOCONF_INCLUDED
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 #ifdef HAVE_SEGMENT_H
44 # include <asm/segment.h>
45 #endif
46 #include <linux/mm.h>
47 #include <linux/pagemap.h>
48 #include <linux/smp_lock.h>
49
50 #define DEBUG_SUBSYSTEM S_LLITE
51
52 #include <lustre_lite.h>
53 #include "llite_internal.h"
54 #include <linux/lustre_compat25.h>
55
56 static int ll_writepage_26(struct page *page, struct writeback_control *wbc)
57 {
58         return ll_writepage(page);
59 }
60
61 /* It is safe to not check anything in invalidatepage/releasepage below
62    because they are run with page locked and all our io is happening with
63    locked page too */
64 #ifdef HAVE_INVALIDATEPAGE_RETURN_INT
65 static int ll_invalidatepage(struct page *page, unsigned long offset)
66 {
67         if (offset)
68                 return 0;
69         if (PagePrivate(page))
70                 ll_removepage(page);
71         return 1;
72 }
73 #else
74 static void ll_invalidatepage(struct page *page, unsigned long offset)
75 {
76         if (offset)
77                 return;
78         if (PagePrivate(page))
79                 ll_removepage(page);
80 }
81 #endif
82
83 static int ll_releasepage(struct page *page, gfp_t gfp_mask)
84 {
85         if (PagePrivate(page))
86                 ll_removepage(page);
87         return 1;
88 }
89
90 #define MAX_DIRECTIO_SIZE 2*1024*1024*1024UL
91
92 static inline int ll_get_user_pages(int rw, unsigned long user_addr,
93                                     size_t size, struct page ***pages)
94 {
95         int result = -ENOMEM;
96         int page_count;
97
98         /* set an arbitrary limit to prevent arithmetic overflow */
99         if (size > MAX_DIRECTIO_SIZE) {
100                 *pages = NULL;
101                 return -EFBIG;
102         }
103
104         page_count = ((user_addr + size + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT)-
105                       (user_addr >> CFS_PAGE_SHIFT);
106
107         OBD_ALLOC_WAIT(*pages, page_count * sizeof(**pages));
108         if (*pages) {
109                 down_read(&current->mm->mmap_sem);
110                 result = get_user_pages(current, current->mm, user_addr,
111                                         page_count, (rw == READ), 0, *pages,
112                                         NULL);
113                 up_read(&current->mm->mmap_sem);
114                 if (result < 0)
115                         OBD_FREE(*pages, page_count * sizeof(**pages));
116         }
117
118         return result;
119 }
120
121 /*  ll_free_user_pages - tear down page struct array
122  *  @pages: array of page struct pointers underlying target buffer */
123 static void ll_free_user_pages(struct page **pages, int npages, int do_dirty)
124 {
125         int i;
126
127         for (i = 0; i < npages; i++) {
128                 if (do_dirty)
129                         set_page_dirty_lock(pages[i]);
130                 page_cache_release(pages[i]);
131         }
132
133         OBD_FREE(pages, npages * sizeof(*pages));
134 }
135
136 static ssize_t ll_direct_IO_26_seg(int rw, struct inode *inode,
137                                    struct address_space *mapping,
138                                    struct obd_info *oinfo,
139                                    struct ptlrpc_request_set *set,
140                                    size_t size, loff_t file_offset,
141                                    struct page **pages, int page_count)
142 {
143         struct brw_page *pga;
144         int i, rc = 0;
145         size_t length;
146         ENTRY;
147
148         OBD_ALLOC(pga, sizeof(*pga) * page_count);
149         if (!pga) {
150                 CDEBUG(D_VFSTRACE, "sizeof(*pga) = %u page_count = %u\n",
151                       (int)sizeof(*pga), page_count);
152                 RETURN(-ENOMEM);
153         }
154
155         for (i = 0, length = size; length > 0;
156              length -=pga[i].count, file_offset +=pga[i].count,i++) {/*i last!*/
157                 pga[i].pg = pages[i];
158                 pga[i].off = file_offset;
159                 /* To the end of the page, or the length, whatever is less */
160                 pga[i].count = min_t(int, CFS_PAGE_SIZE -(file_offset & ~CFS_PAGE_MASK),
161                                      length);
162                 pga[i].flag = 0;
163                 if (rw == READ)
164                         POISON_PAGE(pages[i], 0x0d);
165         }
166
167         rc = obd_brw_async(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
168                            ll_i2obdexp(inode), oinfo, page_count,
169                            pga, NULL, set);
170         if (rc == 0)
171                 rc = size;
172
173         OBD_FREE(pga, sizeof(*pga) * page_count);
174         RETURN(rc);
175 }
176
177 /* This is the maximum size of a single O_DIRECT request, based on a 128kB
178  * kmalloc limit.  We need to fit all of the brw_page structs, each one
179  * representing PAGE_SIZE worth of user data, into a single buffer, and
180  * then truncate this to be a full-sized RPC.  This is 22MB for 4kB pages. */
181 #define MAX_DIO_SIZE ((128 * 1024 / sizeof(struct brw_page) * CFS_PAGE_SIZE) & \
182                       ~(PTLRPC_MAX_BRW_SIZE - 1))
183 static ssize_t ll_direct_IO_26(int rw, struct kiocb *iocb,
184                                const struct iovec *iov, loff_t file_offset,
185                                unsigned long nr_segs)
186 {
187         struct file *file = iocb->ki_filp;
188         struct inode *inode = file->f_mapping->host;
189         ssize_t count = iov_length(iov, nr_segs), tot_bytes = 0;
190         struct ll_inode_info *lli = ll_i2info(inode);
191         struct lov_stripe_md *lsm = lli->lli_smd;
192         struct ptlrpc_request_set *set;
193         struct obd_info oinfo;
194         struct obdo oa;
195         unsigned long seg;
196         size_t size = MAX_DIO_SIZE;
197         ENTRY;
198
199         if (!lli->lli_smd || !lli->lli_smd->lsm_object_id)
200                 RETURN(-EBADF);
201
202         /* FIXME: io smaller than CFS_PAGE_SIZE is broken on ia64 ??? */
203         if ((file_offset & (~CFS_PAGE_MASK)) || (count & ~CFS_PAGE_MASK))
204                 RETURN(-EINVAL);
205
206         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), size="LPSZ" (max %lu), "
207                "offset=%lld=%llx, pages "LPSZ" (max %lu)\n",
208                inode->i_ino, inode->i_generation, inode, count, MAX_DIO_SIZE,
209                file_offset, file_offset, count >> CFS_PAGE_SHIFT,
210                MAX_DIO_SIZE >> CFS_PAGE_SHIFT);
211
212         if (rw == WRITE)
213                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_DIRECT_WRITE, count);
214         else
215                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_DIRECT_READ, count);
216
217         /* Check that all user buffers are aligned as well */
218         for (seg = 0; seg < nr_segs; seg++) {
219                 if (((unsigned long)iov[seg].iov_base & ~CFS_PAGE_MASK) ||
220                     (iov[seg].iov_len & ~CFS_PAGE_MASK))
221                         RETURN(-EINVAL);
222         }
223
224         set = ptlrpc_prep_set();
225         if (set == NULL)
226                 RETURN(-ENOMEM);
227
228         ll_inode_fill_obdo(inode, rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ, &oa);
229         oinfo.oi_oa = &oa;
230         oinfo.oi_md = lsm;
231
232         /* need locking between buffered and direct access. and race with 
233          *size changing by concurrent truncates and writes. */
234         if (rw == READ)
235                 LOCK_INODE_MUTEX(inode);
236
237         for (seg = 0; seg < nr_segs; seg++) {
238                 size_t iov_left = iov[seg].iov_len;
239                 unsigned long user_addr = (unsigned long)iov[seg].iov_base;
240
241                 if (rw == READ) {
242                         if (file_offset >= inode->i_size)
243                                 break;
244                         if (file_offset + iov_left > inode->i_size)
245                                 iov_left = inode->i_size - file_offset;
246                 }
247
248                 while (iov_left > 0) {
249                         struct page **pages;
250                         int page_count;
251                         ssize_t result;
252
253                         page_count = ll_get_user_pages(rw, user_addr,
254                                                        min(size, iov_left),
255                                                        &pages);
256                         LASSERT(page_count != 0);
257                         if (page_count > 0) {
258                                 result = ll_direct_IO_26_seg(rw, inode,
259                                                              file->f_mapping,
260                                                              &oinfo, set,
261                                                              min(size,iov_left),
262                                                              file_offset, pages,
263                                                              page_count);
264                                 ll_free_user_pages(pages, page_count, rw==READ);
265                         } else {
266                                 result = 0;
267                         }
268                         if (page_count < 0 || result <= 0) {
269                                 /* If we can't allocate a large enough buffer
270                                  * for the request, shrink it to a smaller
271                                  * PAGE_SIZE multiple and try again.
272                                  * We should always be able to kmalloc for a
273                                  * page worth of page pointers = 4MB on i386. */
274                                 if ((page_count == -ENOMEM||result == -ENOMEM)&&
275                                     size > (CFS_PAGE_SIZE / sizeof(*pages)) *
276                                            CFS_PAGE_SIZE) {
277                                         size = ((((size / 2) - 1) |
278                                                  ~CFS_PAGE_MASK) + 1) &
279                                                 CFS_PAGE_MASK;
280                                         CDEBUG(D_VFSTRACE, "DIO size now %u\n",
281                                                (int)size);
282                                         continue;
283                                 }
284                                 if (tot_bytes > 0)
285                                         GOTO(wait_io, tot_bytes);
286                                 GOTO(out, tot_bytes = page_count < 0 ? page_count : result);
287                         }
288
289                         tot_bytes += result;
290                         file_offset += result;
291                         iov_left -= result;
292                         user_addr += result;
293                 }
294         }
295
296         if (tot_bytes > 0) {
297                 int rc;
298         wait_io:
299                 rc = ptlrpc_set_wait(set);
300                 if (rc)
301                         GOTO(out, tot_bytes = rc);
302                 if (rw == WRITE) {
303                         lov_stripe_lock(lsm);
304                         obd_adjust_kms(ll_i2obdexp(inode), lsm, file_offset, 0);
305                         lov_stripe_unlock(lsm);
306                 }
307         }
308 out:
309         if (rw == READ)
310                 UNLOCK_INODE_MUTEX(inode);
311
312         ptlrpc_set_destroy(set);
313         RETURN(tot_bytes);
314 }
315
316 struct address_space_operations ll_aops = {
317         .readpage       = ll_readpage,
318 //        .readpages      = ll_readpages,
319         .direct_IO      = ll_direct_IO_26,
320         .writepage      = ll_writepage_26,
321         .writepages     = generic_writepages,
322         .set_page_dirty = __set_page_dirty_nobuffers,
323         .sync_page      = NULL,
324         .prepare_write  = ll_prepare_write,
325         .commit_write   = ll_commit_write,
326         .invalidatepage = ll_invalidatepage,
327         .releasepage    = ll_releasepage,
328         .bmap           = NULL
329 };