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