Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / lustre / llite / rw26.c
1 /*
2  * -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
3  * vim:expandtab:shiftwidth=8:tabstop=8:
4  *
5  * GPL HEADER START
6  *
7  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 only,
11  * as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License version 2 for more details (a copy is included
17  * in the LICENSE file that accompanied this code).
18  *
19  * You should have received a copy of the GNU General Public License
20  * version 2 along with this program; If not, see [sun.com URL with a
21  * copy of GPLv2].
22  *
23  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
24  * CA 95054 USA or visit www.sun.com if you need additional information or
25  * have any questions.
26  *
27  * GPL HEADER END
28  */
29 /*
30  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
31  * Use is subject to license terms.
32  */
33 /*
34  * This file is part of Lustre, http://www.lustre.org/
35  * Lustre is a trademark of Sun Microsystems, Inc.
36  *
37  * lustre/lustre/llite/rw26.c
38  *
39  * Lustre Lite I/O page cache routines for the 2.5/2.6 kernel version
40  */
41
42 #ifndef AUTOCONF_INCLUDED
43 #include <linux/config.h>
44 #endif
45 #include <linux/kernel.h>
46 #include <linux/mm.h>
47 #include <linux/string.h>
48 #include <linux/stat.h>
49 #include <linux/errno.h>
50 #include <linux/smp_lock.h>
51 #include <linux/unistd.h>
52 #include <linux/version.h>
53 #include <asm/system.h>
54 #include <asm/uaccess.h>
55
56 #include <linux/fs.h>
57 #include <linux/buffer_head.h>
58 #include <linux/mpage.h>
59 #include <linux/writeback.h>
60 #include <linux/stat.h>
61 #include <asm/uaccess.h>
62 #include <linux/mm.h>
63 #include <linux/pagemap.h>
64 #include <linux/smp_lock.h>
65
66 #define DEBUG_SUBSYSTEM S_LLITE
67
68 //#include <lustre_mdc.h>
69 #include <lustre_lite.h>
70 #include "llite_internal.h"
71 #include <linux/lustre_compat25.h>
72
73 static int ll_writepage_26(struct page *page, struct writeback_control *wbc)
74 {
75         return ll_writepage(page);
76 }
77
78 /* It is safe to not check anything in invalidatepage/releasepage below
79    because they are run with page locked and all our io is happening with
80    locked page too */
81 #ifdef HAVE_INVALIDATEPAGE_RETURN_INT
82 static int ll_invalidatepage(struct page *page, unsigned long offset)
83 {
84         if (offset)
85                 return 0;
86         if (PagePrivate(page))
87                 ll_removepage(page);
88         return 1;
89 }
90 #else
91 static void ll_invalidatepage(struct page *page, unsigned long offset)
92 {
93         if (offset == 0 && PagePrivate(page))
94                 ll_removepage(page);
95 }
96 #endif
97 static int ll_releasepage(struct page *page, gfp_t gfp_mask)
98 {
99         if (PagePrivate(page))
100                 ll_removepage(page);
101         return 1;
102 }
103
104 static int ll_set_page_dirty(struct page *page)
105 {
106         struct ll_async_page *llap;
107         ENTRY;
108         
109         llap = llap_from_page(page, LLAP_ORIGIN_UNKNOWN);
110         if (IS_ERR(llap))
111                 RETURN(PTR_ERR(llap));
112         
113         llap_write_pending(page->mapping->host, llap);
114         RETURN(__set_page_dirty_nobuffers(page));
115 }
116
117 #define MAX_DIRECTIO_SIZE 2*1024*1024*1024UL
118
119 static inline int ll_get_user_pages(int rw, unsigned long user_addr,
120                                     size_t size, struct page ***pages)
121 {
122         int result = -ENOMEM;
123         int page_count;
124
125         /* set an arbitrary limit to prevent arithmetic overflow */
126         if (size > MAX_DIRECTIO_SIZE) {
127                 *pages = NULL;
128                 return -EFBIG;
129         }
130
131         page_count = (user_addr + size + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
132         page_count -= user_addr >> CFS_PAGE_SHIFT;
133
134         OBD_ALLOC_WAIT(*pages, page_count * sizeof(**pages));
135         if (*pages) {
136                 down_read(&current->mm->mmap_sem);
137                 result = get_user_pages(current, current->mm, user_addr,
138                                         page_count, (rw == READ), 0, *pages,
139                                         NULL);
140                 up_read(&current->mm->mmap_sem);
141                 if (result < 0)
142                         OBD_FREE(*pages, page_count * sizeof(**pages));
143         }
144
145         return result;
146 }
147
148 /*  ll_free_user_pages - tear down page struct array
149  *  @pages: array of page struct pointers underlying target buffer */
150 static void ll_free_user_pages(struct page **pages, int npages, int do_dirty)
151 {
152         int i;
153
154         for (i = 0; i < npages; i++) {
155                 if (do_dirty)
156                         set_page_dirty_lock(pages[i]);
157                 page_cache_release(pages[i]);
158         }
159
160         OBD_FREE(pages, npages * sizeof(*pages));
161 }
162
163 static ssize_t ll_direct_IO_26_seg(int rw, struct inode *inode,
164                                    struct address_space *mapping,
165                                    struct obd_info *oinfo,
166                                    struct ptlrpc_request_set *set,
167                                    size_t size, loff_t file_offset,
168                                    struct page **pages, int page_count)
169 {
170         struct brw_page *pga;
171         int i, rc = 0;
172         size_t length;
173         ENTRY;
174
175         OBD_ALLOC(pga, sizeof(*pga) * page_count);
176         if (!pga) {
177                 CDEBUG(D_VFSTRACE, "sizeof(*pga) = %u page_count = %u\n",
178                        (int)sizeof(*pga), page_count);
179                 RETURN(-ENOMEM);
180         }
181
182         for (i = 0, length = size; length > 0;
183              length -=pga[i].count, file_offset +=pga[i].count,i++) {/*i last!*/
184                 pga[i].pg = pages[i];
185                 pga[i].off = file_offset;
186                 /* To the end of the page, or the length, whatever is less */
187                 pga[i].count = min_t(int, CFS_PAGE_SIZE - 
188                                           (file_offset & ~CFS_PAGE_MASK),
189                                      length);
190                 pga[i].flag = 0;
191                 if (rw == READ)
192                         POISON_PAGE(pages[i], 0x0d);
193         }
194
195         rc = obd_brw_async(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
196                                 ll_i2dtexp(inode), oinfo, page_count,
197                                 pga, NULL, set);
198         if (rc == 0)
199                 rc = size;
200
201         OBD_FREE(pga, sizeof(*pga) * page_count);
202         RETURN(rc);
203 }
204
205 /* This is the maximum size of a single O_DIRECT request, based on a 128kB
206  * kmalloc limit.  We need to fit all of the brw_page structs, each one
207  * representing PAGE_SIZE worth of user data, into a single buffer, and
208  * then truncate this to be a full-sized RPC.  This is 22MB for 4kB pages. */
209 #define MAX_DIO_SIZE ((128 * 1024 / sizeof(struct brw_page) * CFS_PAGE_SIZE) & \
210                       ~(PTLRPC_MAX_BRW_SIZE - 1))
211 static ssize_t ll_direct_IO_26(int rw, struct kiocb *iocb,
212                                const struct iovec *iov, loff_t file_offset,
213                                unsigned long nr_segs)
214 {
215         struct file *file = iocb->ki_filp;
216         struct inode *inode = file->f_mapping->host;
217         ssize_t count = iov_length(iov, nr_segs), tot_bytes = 0;
218         struct ll_inode_info *lli = ll_i2info(inode);
219         struct lov_stripe_md *lsm = lli->lli_smd;
220         struct ptlrpc_request_set *set;
221         struct obd_info oinfo;
222         struct obdo oa;
223         unsigned long seg = 0;
224         size_t size = MAX_DIO_SIZE;
225         int opc;
226         ENTRY;
227
228         if (!lli->lli_smd || !lli->lli_smd->lsm_object_id)
229                 RETURN(-EBADF);
230
231         /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
232         if ((file_offset & ~CFS_PAGE_MASK) || (count & ~CFS_PAGE_MASK))
233                 RETURN(-EINVAL);
234
235         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), size="LPSZ" (max %lu), "
236                "offset=%lld=%llx, pages "LPSZ" (max %lu)\n",
237                inode->i_ino, inode->i_generation, inode, count, MAX_DIO_SIZE,
238                file_offset, file_offset, count >> CFS_PAGE_SHIFT,
239                MAX_DIO_SIZE >> CFS_PAGE_SHIFT);
240
241         if (rw == WRITE) {
242                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_DIRECT_WRITE, count);
243                 opc = CAPA_OPC_OSS_WRITE;
244                 llap_write_pending(inode, NULL);
245         } else {
246                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_DIRECT_READ, count);
247                 opc = CAPA_OPC_OSS_RW;
248         }
249
250         /* Check that all user buffers are aligned as well */
251         for (seg = 0; seg < nr_segs; seg++) {
252                 if (((unsigned long)iov[seg].iov_base & ~CFS_PAGE_MASK) ||
253                     (iov[seg].iov_len & ~CFS_PAGE_MASK))
254                         RETURN(-EINVAL);
255         }
256
257         set = ptlrpc_prep_set();
258         if (set == NULL)
259                 RETURN(-ENOMEM);
260
261         ll_inode_fill_obdo(inode, rw, &oa);
262         oinfo.oi_oa = &oa;
263         oinfo.oi_md = lsm;
264         oinfo.oi_capa = ll_osscapa_get(inode, opc);
265
266         /* need locking between buffered and direct access. and race with 
267          *size changing by concurrent truncates and writes. */
268         if (rw == READ)
269                 LOCK_INODE_MUTEX(inode);
270
271         for (seg = 0; seg < nr_segs; seg++) {
272                 size_t iov_left = iov[seg].iov_len;
273                 unsigned long user_addr = (unsigned long)iov[seg].iov_base;
274
275                 if (rw == READ) {
276                         if (file_offset >= inode->i_size)
277                                 break;
278                         if (file_offset + iov_left > inode->i_size)
279                                 iov_left = inode->i_size - file_offset;
280                 }
281
282                 while (iov_left > 0) {
283                         struct page **pages;
284                         int page_count;
285                         ssize_t result;
286
287                         page_count = ll_get_user_pages(rw, user_addr,
288                                                        min(size, iov_left),
289                                                        &pages);
290                         LASSERT(page_count != 0);
291                         if (page_count > 0) {
292                                 result = ll_direct_IO_26_seg(rw, inode,
293                                                              file->f_mapping,
294                                                              &oinfo, set,
295                                                              min(size,iov_left),
296                                                              file_offset, pages,
297                                                              page_count);
298                                 ll_free_user_pages(pages, page_count, rw==READ);
299                         } else {
300                                 result = 0;
301                         }
302                         if (page_count < 0 || result <= 0) {
303                                 /* If we can't allocate a large enough buffer
304                                  * for the request, shrink it to a smaller
305                                  * PAGE_SIZE multiple and try again.
306                                  * We should always be able to kmalloc for a
307                                  * page worth of page pointers = 4MB on i386. */
308                                 if ((page_count == -ENOMEM||result == -ENOMEM)&&
309                                     size > (CFS_PAGE_SIZE / sizeof(*pages)) *
310                                            CFS_PAGE_SIZE) {
311                                         size = ((((size / 2) - 1) |
312                                                  ~CFS_PAGE_MASK) + 1) &
313                                                 CFS_PAGE_MASK;
314                                         CDEBUG(D_VFSTRACE, "DIO size now %u\n",
315                                                (int)size);
316                                         continue;
317                                 }
318
319                                 if (tot_bytes <= 0)
320                                         tot_bytes = page_count < 0 ? page_count : result;
321                                 GOTO(out, tot_bytes);
322                         }
323
324                         tot_bytes += result;
325                         file_offset += result;
326                         iov_left -= result;
327                         user_addr += result;
328                 }
329         }
330 out:
331         if (rw == READ)
332                 UNLOCK_INODE_MUTEX(inode);
333
334         if (tot_bytes > 0) {
335                 int rc;
336                 
337                 rc = ptlrpc_set_wait(set);
338                 if (rc) {
339                         tot_bytes = rc;
340                 } else if (rw == WRITE) {
341                         lov_stripe_lock(lsm);
342                         obd_adjust_kms(ll_i2dtexp(inode), lsm, file_offset, 0);
343                         lov_stripe_unlock(lsm);
344                 }
345         }
346
347         capa_put(oinfo.oi_capa);
348         ptlrpc_set_destroy(set);
349         RETURN(tot_bytes);
350 }
351
352 struct address_space_operations ll_aops = {
353         .readpage       = ll_readpage,
354 //        .readpages      = ll_readpages,
355         .direct_IO      = ll_direct_IO_26,
356         .writepage      = ll_writepage_26,
357         .writepages     = generic_writepages,
358         .set_page_dirty = ll_set_page_dirty,
359         .sync_page      = NULL,
360         .prepare_write  = ll_prepare_write,
361         .commit_write   = ll_commit_write,
362         .invalidatepage = ll_invalidatepage,
363         .releasepage    = ll_releasepage,
364         .bmap           = NULL
365 };