Whamcloud - gitweb
b=20317
[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                                     int *max_pages)
117 {
118         int result = -ENOMEM;
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         *max_pages = ((user_addr + size + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT)-
127                       (user_addr >> CFS_PAGE_SHIFT);
128
129         OBD_ALLOC_WAIT(*pages, *max_pages * sizeof(**pages));
130         if (*pages) {
131                 down_read(&current->mm->mmap_sem);
132                 result = get_user_pages(current, current->mm, user_addr,
133                                         *max_pages, (rw == READ), 0, *pages,
134                                         NULL);
135                 up_read(&current->mm->mmap_sem);
136                 if (unlikely(result <= 0))
137                         OBD_FREE(*pages, *max_pages * 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 (pages[i] == NULL)
151                         break;
152                 if (do_dirty)
153                         set_page_dirty_lock(pages[i]);
154                 page_cache_release(pages[i]);
155         }
156         OBD_FREE(pages, npages * sizeof(*pages));
157 }
158
159 static ssize_t ll_direct_IO_26_seg(int rw, struct inode *inode,
160                                    struct address_space *mapping,
161                                    struct obd_info *oinfo,
162                                    struct ptlrpc_request_set *set,
163                                    size_t size, loff_t file_offset,
164                                    struct page **pages, int page_count,
165                                    unsigned long user_addr, int locked)
166 {
167         struct brw_page *pga;
168         int i, rc = 0, pshift;
169         size_t length;
170         ENTRY;
171
172         OBD_ALLOC(pga, sizeof(*pga) * page_count);
173         if (!pga) {
174                 CDEBUG(D_VFSTRACE, "sizeof(*pga) = %u page_count = %u\n",
175                       (int)sizeof(*pga), page_count);
176                 RETURN(-ENOMEM);
177         }
178
179         /*
180          * pshift is something we'll add to ->off to get the in-memory offset,
181          * also see the OSC_FILE2MEM_OFF macro
182          */
183         pshift = (user_addr & ~CFS_PAGE_MASK) - (file_offset & ~CFS_PAGE_MASK);
184
185         for (i = 0, length = size; length > 0; i++) {/*i last!*/
186                 LASSERT(i < page_count);
187
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 -(user_addr & ~CFS_PAGE_MASK),
192                                      length);
193
194                 pga[i].flag = OBD_BRW_SYNC;
195                 if (!locked)
196                         pga[i].flag |= OBD_BRW_SRVLOCK;
197
198                 if (rw == READ)
199                         POISON_PAGE(pages[i], 0x0d);
200
201                 length -= pga[i].count;
202                 file_offset += pga[i].count;
203                 user_addr += pga[i].count;
204         }
205
206         rc = obd_brw_async(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
207                            ll_i2obdexp(inode), oinfo, page_count,
208                            pga, NULL, set, pshift);
209         if (rc == 0)
210                 rc = size;
211
212         OBD_FREE(pga, sizeof(*pga) * page_count);
213         RETURN(rc);
214 }
215
216 /* This is the maximum size of a single O_DIRECT request, based on a 128kB
217  * kmalloc limit.  We need to fit all of the brw_page structs, each one
218  * representing PAGE_SIZE worth of user data, into a single buffer, and
219  * then truncate this to be a full-sized RPC.  This is 22MB for 4kB pages. */
220 #define MAX_DIO_SIZE ((128 * 1024 / sizeof(struct brw_page) * CFS_PAGE_SIZE) & \
221                       ~(PTLRPC_MAX_BRW_SIZE - 1))
222
223 ssize_t ll_direct_IO(int rw, struct file *file,
224                      const struct iovec *iov, loff_t file_offset,
225                      unsigned long nr_segs, int locked)
226 {
227         struct inode *inode = file->f_mapping->host;
228         ssize_t count = iov_length(iov, nr_segs);
229         ssize_t tot_bytes = 0, result = 0;
230         struct ll_inode_info *lli = ll_i2info(inode);
231         struct lov_stripe_md *lsm = lli->lli_smd;
232         struct ptlrpc_request_set *set;
233         struct obd_info oinfo;
234         struct obdo oa = { 0 };
235         unsigned long seg;
236         size_t size = MAX_DIO_SIZE;
237         ENTRY;
238
239         if (!lli->lli_smd || !lli->lli_smd->lsm_object_id)
240                 RETURN(-EBADF);
241
242         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), size="LPSZ" (max %lu), "
243                "offset=%lld=%llx, pages "LPSZ" (max %lu)\n",
244                inode->i_ino, inode->i_generation, inode, count, MAX_DIO_SIZE,
245                file_offset, file_offset, count >> CFS_PAGE_SHIFT,
246                MAX_DIO_SIZE >> CFS_PAGE_SHIFT);
247
248         if (rw == WRITE)
249                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_DIRECT_WRITE, count);
250         else
251                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_DIRECT_READ, count);
252
253         set = ptlrpc_prep_set();
254         if (set == NULL)
255                 RETURN(-ENOMEM);
256
257         ll_inode_fill_obdo(inode, rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ, &oa);
258         oinfo.oi_oa = &oa;
259         oinfo.oi_md = lsm;
260
261         /* need locking between buffered and direct access. and race with 
262          *size changing by concurrent truncates and writes. */
263         if (rw == READ)
264                 LOCK_INODE_MUTEX(inode);
265         for (seg = 0; seg < nr_segs; seg++) {
266                 size_t iov_left = iov[seg].iov_len;
267                 unsigned long user_addr = (unsigned long)iov[seg].iov_base;
268
269                 if (rw == READ) {
270                         if (file_offset >= inode->i_size)
271                                 break;
272                         if (file_offset + iov_left > inode->i_size)
273                                 iov_left = inode->i_size - file_offset;
274                 }
275
276                 while (iov_left > 0) {
277                         struct page **pages;
278                         int page_count, max_pages = 0;
279                         size_t bytes;
280
281                         bytes = min(size,iov_left);
282                         page_count = ll_get_user_pages(rw, user_addr,
283                                                        bytes,
284                                                        &pages, &max_pages);
285                         if (likely(page_count > 0)) {
286                                 if (unlikely(page_count <  max_pages))
287                                         bytes = page_count << CFS_PAGE_SHIFT;
288                                 result = ll_direct_IO_26_seg(rw, inode,
289                                                              file->f_mapping,
290                                                              &oinfo, set,
291                                                              bytes,
292                                                              file_offset, pages,
293                                                              page_count,
294                                                              user_addr, locked);
295                                 ll_free_user_pages(pages, max_pages, rw==READ);
296                         } else if (page_count == 0) {
297                                 GOTO(out, result = -EFAULT);
298                         } else {
299                                 result = page_count;
300                         }
301                         if (unlikely(result <= 0)) {
302                                 /* If we can't allocate a large enough buffer
303                                  * for the request, shrink it to a smaller
304                                  * PAGE_SIZE multiple and try again.
305                                  * We should always be able to kmalloc for a
306                                  * page worth of page pointers = 4MB on i386. */
307                                 if (result == -ENOMEM &&
308                                     size > (CFS_PAGE_SIZE / sizeof(*pages)) *
309                                            CFS_PAGE_SIZE) {
310                                         size = ((((size / 2) - 1) |
311                                                  ~CFS_PAGE_MASK) + 1) &
312                                                 CFS_PAGE_MASK;
313                                         CDEBUG(D_VFSTRACE, "DIO size now %u\n",
314                                                (int)size);
315                                         continue;
316                                 }
317                                 GOTO(out, result);
318                         }
319                         tot_bytes += result;
320                         file_offset += result;
321                         iov_left -= result;
322                         user_addr += result;
323                 }
324         }
325 out:
326         if (likely(tot_bytes > 0)) {
327                 int rc;
328
329                 rc = ptlrpc_set_wait(set);
330                 if (unlikely(rc != 0))
331                         GOTO(unlock_mutex, tot_bytes = rc);
332                 if (rw == WRITE && locked) {
333                         lov_stripe_lock(lsm);
334                         obd_adjust_kms(ll_i2obdexp(inode),
335                                        lsm, file_offset, 0);
336                         lov_stripe_unlock(lsm);
337                 }
338         } else {
339                 tot_bytes = result;
340         }
341 unlock_mutex:
342         if (rw == READ)
343                 UNLOCK_INODE_MUTEX(inode);
344
345         ptlrpc_set_destroy(set);
346         RETURN(tot_bytes);
347 }
348
349 static ssize_t ll_direct_IO_26(int rw, struct kiocb *kiocb,
350                                const struct iovec *iov, loff_t file_offset,
351                                unsigned long nr_segs)
352 {
353         return ll_direct_IO(rw, kiocb->ki_filp, iov, file_offset, nr_segs, 1);
354 }
355
356 #ifdef HAVE_KERNEL_WRITE_BEGIN_END
357 static int ll_write_begin(struct file *file, struct address_space *mapping,
358                          loff_t pos, unsigned len, unsigned flags,
359                          struct page **pagep, void **fsdata)
360 {
361         pgoff_t index = pos >> PAGE_CACHE_SHIFT;
362         struct page *page;
363         int rc;
364         unsigned from = pos & (PAGE_CACHE_SIZE - 1);
365         ENTRY;
366
367         page = grab_cache_page_write_begin(mapping, index, flags);
368         if (!page)
369                 RETURN(-ENOMEM);
370
371         *pagep = page;
372  
373         rc = ll_prepare_write(file, page, from, from + len);
374         if (rc) {
375                 unlock_page(page);
376                 page_cache_release(page);
377         }
378         RETURN(rc);
379 }
380
381 static int ll_write_end(struct file *file, struct address_space *mapping,
382                         loff_t pos, unsigned len, unsigned copied,
383                         struct page *page, void *fsdata)
384 {
385         unsigned from = pos & (PAGE_CACHE_SIZE - 1);
386         int rc;
387         rc = ll_commit_write(file, page, from, from + copied);
388
389         unlock_page(page);
390         page_cache_release(page);
391         return rc?rc:copied;
392 }
393 #endif
394
395 struct address_space_operations ll_aops = {
396         .readpage       = ll_readpage,
397 //        .readpages      = ll_readpages,
398         .direct_IO      = ll_direct_IO_26,
399         .writepage      = ll_writepage_26,
400         .writepages     = generic_writepages,
401         .set_page_dirty = __set_page_dirty_nobuffers,
402         .sync_page      = NULL,
403 #ifdef HAVE_KERNEL_WRITE_BEGIN_END
404         .write_begin    = ll_write_begin,
405         .write_end      = ll_write_end,
406 #else
407         .prepare_write  = ll_prepare_write,
408         .commit_write   = ll_commit_write,
409 #endif
410         .invalidatepage = ll_invalidatepage,
411         .releasepage    = ll_releasepage,
412         .bmap           = NULL
413 };