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