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