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