]> Pileus Git - ~andy/linux/blob - drivers/staging/lustre/lustre/llite/rw26.c
lustre: Convert ll_get_user_pages() to use get_user_pages_fast()
[~andy/linux] / drivers / staging / lustre / lustre / llite / rw26.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
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 #include <linux/kernel.h>
42 #include <linux/mm.h>
43 #include <linux/string.h>
44 #include <linux/stat.h>
45 #include <linux/errno.h>
46 #include <linux/unistd.h>
47 #include <asm/uaccess.h>
48
49 #include <linux/migrate.h>
50 #include <linux/fs.h>
51 #include <linux/buffer_head.h>
52 #include <linux/mpage.h>
53 #include <linux/writeback.h>
54 #include <linux/pagemap.h>
55
56 #define DEBUG_SUBSYSTEM S_LLITE
57
58 #include <lustre_lite.h>
59 #include "llite_internal.h"
60 #include <linux/lustre_compat25.h>
61
62 /**
63  * Implements Linux VM address_space::invalidatepage() method. This method is
64  * called when the page is truncate from a file, either as a result of
65  * explicit truncate, or when inode is removed from memory (as a result of
66  * final iput(), umount, or memory pressure induced icache shrinking).
67  *
68  * [0, offset] bytes of the page remain valid (this is for a case of not-page
69  * aligned truncate). Lustre leaves partially truncated page in the cache,
70  * relying on struct inode::i_size to limit further accesses.
71  */
72 static void ll_invalidatepage(struct page *vmpage, unsigned int offset,
73                               unsigned int length)
74 {
75         struct inode     *inode;
76         struct lu_env    *env;
77         struct cl_page   *page;
78         struct cl_object *obj;
79
80         int refcheck;
81
82         LASSERT(PageLocked(vmpage));
83         LASSERT(!PageWriteback(vmpage));
84
85         /*
86          * It is safe to not check anything in invalidatepage/releasepage
87          * below because they are run with page locked and all our io is
88          * happening with locked page too
89          */
90         if (offset == 0 && length == PAGE_CACHE_SIZE) {
91                 env = cl_env_get(&refcheck);
92                 if (!IS_ERR(env)) {
93                         inode = vmpage->mapping->host;
94                         obj = ll_i2info(inode)->lli_clob;
95                         if (obj != NULL) {
96                                 page = cl_vmpage_page(vmpage, obj);
97                                 if (page != NULL) {
98                                         lu_ref_add(&page->cp_reference,
99                                                    "delete", vmpage);
100                                         cl_page_delete(env, page);
101                                         lu_ref_del(&page->cp_reference,
102                                                    "delete", vmpage);
103                                         cl_page_put(env, page);
104                                 }
105                         } else
106                                 LASSERT(vmpage->private == 0);
107                         cl_env_put(env, &refcheck);
108                 }
109         }
110 }
111
112 #ifdef HAVE_RELEASEPAGE_WITH_INT
113 #define RELEASEPAGE_ARG_TYPE int
114 #else
115 #define RELEASEPAGE_ARG_TYPE gfp_t
116 #endif
117 static int ll_releasepage(struct page *vmpage, RELEASEPAGE_ARG_TYPE gfp_mask)
118 {
119         struct cl_env_nest nest;
120         struct lu_env     *env;
121         struct cl_object  *obj;
122         struct cl_page    *page;
123         struct address_space *mapping;
124         int result;
125
126         LASSERT(PageLocked(vmpage));
127         if (PageWriteback(vmpage) || PageDirty(vmpage))
128                 return 0;
129
130         mapping = vmpage->mapping;
131         if (mapping == NULL)
132                 return 1;
133
134         obj = ll_i2info(mapping->host)->lli_clob;
135         if (obj == NULL)
136                 return 1;
137
138         /* 1 for page allocator, 1 for cl_page and 1 for page cache */
139         if (page_count(vmpage) > 3)
140                 return 0;
141
142         /* TODO: determine what gfp should be used by @gfp_mask. */
143         env = cl_env_nested_get(&nest);
144         if (IS_ERR(env))
145                 /* If we can't allocate an env we won't call cl_page_put()
146                  * later on which further means it's impossible to drop
147                  * page refcount by cl_page, so ask kernel to not free
148                  * this page. */
149                 return 0;
150
151         page = cl_vmpage_page(vmpage, obj);
152         result = page == NULL;
153         if (page != NULL) {
154                 if (!cl_page_in_use(page)) {
155                         result = 1;
156                         cl_page_delete(env, page);
157                 }
158                 cl_page_put(env, page);
159         }
160         cl_env_nested_put(&nest, env);
161         return result;
162 }
163
164 static int ll_set_page_dirty(struct page *vmpage)
165 {
166 #if 0
167         struct cl_page    *page = vvp_vmpage_page_transient(vmpage);
168         struct vvp_object *obj  = cl_inode2vvp(vmpage->mapping->host);
169         struct vvp_page   *cpg;
170
171         /*
172          * XXX should page method be called here?
173          */
174         LASSERT(&obj->co_cl == page->cp_obj);
175         cpg = cl2vvp_page(cl_page_at(page, &vvp_device_type));
176         /*
177          * XXX cannot do much here, because page is possibly not locked:
178          * sys_munmap()->...
179          *     ->unmap_page_range()->zap_pte_range()->set_page_dirty().
180          */
181         vvp_write_pending(obj, cpg);
182 #endif
183         return __set_page_dirty_nobuffers(vmpage);
184 }
185
186 #define MAX_DIRECTIO_SIZE 2*1024*1024*1024UL
187
188 static inline int ll_get_user_pages(int rw, unsigned long user_addr,
189                                     size_t size, struct page ***pages,
190                                     int *max_pages)
191 {
192         int result = -ENOMEM;
193
194         /* set an arbitrary limit to prevent arithmetic overflow */
195         if (size > MAX_DIRECTIO_SIZE) {
196                 *pages = NULL;
197                 return -EFBIG;
198         }
199
200         *max_pages = (user_addr + size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
201         *max_pages -= user_addr >> PAGE_CACHE_SHIFT;
202
203         OBD_ALLOC_LARGE(*pages, *max_pages * sizeof(**pages));
204         if (*pages) {
205                 result = get_user_pages_fast(user_addr, *max_pages,
206                                              (rw == READ), *pages);
207                 if (unlikely(result <= 0))
208                         OBD_FREE_LARGE(*pages, *max_pages * sizeof(**pages));
209         }
210
211         return result;
212 }
213
214 /*  ll_free_user_pages - tear down page struct array
215  *  @pages: array of page struct pointers underlying target buffer */
216 static void ll_free_user_pages(struct page **pages, int npages, int do_dirty)
217 {
218         int i;
219
220         for (i = 0; i < npages; i++) {
221                 if (pages[i] == NULL)
222                         break;
223                 if (do_dirty)
224                         set_page_dirty_lock(pages[i]);
225                 page_cache_release(pages[i]);
226         }
227
228         OBD_FREE_LARGE(pages, npages * sizeof(*pages));
229 }
230
231 ssize_t ll_direct_rw_pages(const struct lu_env *env, struct cl_io *io,
232                            int rw, struct inode *inode,
233                            struct ll_dio_pages *pv)
234 {
235         struct cl_page    *clp;
236         struct cl_2queue  *queue;
237         struct cl_object  *obj = io->ci_obj;
238         int i;
239         ssize_t rc = 0;
240         loff_t file_offset  = pv->ldp_start_offset;
241         long size          = pv->ldp_size;
242         int page_count      = pv->ldp_nr;
243         struct page **pages = pv->ldp_pages;
244         long page_size      = cl_page_size(obj);
245         bool do_io;
246         int  io_pages       = 0;
247
248         queue = &io->ci_queue;
249         cl_2queue_init(queue);
250         for (i = 0; i < page_count; i++) {
251                 if (pv->ldp_offsets)
252                     file_offset = pv->ldp_offsets[i];
253
254                 LASSERT(!(file_offset & (page_size - 1)));
255                 clp = cl_page_find(env, obj, cl_index(obj, file_offset),
256                                    pv->ldp_pages[i], CPT_TRANSIENT);
257                 if (IS_ERR(clp)) {
258                         rc = PTR_ERR(clp);
259                         break;
260                 }
261
262                 rc = cl_page_own(env, io, clp);
263                 if (rc) {
264                         LASSERT(clp->cp_state == CPS_FREEING);
265                         cl_page_put(env, clp);
266                         break;
267                 }
268
269                 do_io = true;
270
271                 /* check the page type: if the page is a host page, then do
272                  * write directly */
273                 if (clp->cp_type == CPT_CACHEABLE) {
274                         struct page *vmpage = cl_page_vmpage(env, clp);
275                         struct page *src_page;
276                         struct page *dst_page;
277                         void       *src;
278                         void       *dst;
279
280                         src_page = (rw == WRITE) ? pages[i] : vmpage;
281                         dst_page = (rw == WRITE) ? vmpage : pages[i];
282
283                         src = kmap_atomic(src_page);
284                         dst = kmap_atomic(dst_page);
285                         memcpy(dst, src, min(page_size, size));
286                         kunmap_atomic(dst);
287                         kunmap_atomic(src);
288
289                         /* make sure page will be added to the transfer by
290                          * cl_io_submit()->...->vvp_page_prep_write(). */
291                         if (rw == WRITE)
292                                 set_page_dirty(vmpage);
293
294                         if (rw == READ) {
295                                 /* do not issue the page for read, since it
296                                  * may reread a ra page which has NOT uptodate
297                                  * bit set. */
298                                 cl_page_disown(env, io, clp);
299                                 do_io = false;
300                         }
301                 }
302
303                 if (likely(do_io)) {
304                         cl_2queue_add(queue, clp);
305
306                         /*
307                          * Set page clip to tell transfer formation engine
308                          * that page has to be sent even if it is beyond KMS.
309                          */
310                         cl_page_clip(env, clp, 0, min(size, page_size));
311
312                         ++io_pages;
313                 }
314
315                 /* drop the reference count for cl_page_find */
316                 cl_page_put(env, clp);
317                 size -= page_size;
318                 file_offset += page_size;
319         }
320
321         if (rc == 0 && io_pages) {
322                 rc = cl_io_submit_sync(env, io,
323                                        rw == READ ? CRT_READ : CRT_WRITE,
324                                        queue, 0);
325         }
326         if (rc == 0)
327                 rc = pv->ldp_size;
328
329         cl_2queue_discard(env, io, queue);
330         cl_2queue_disown(env, io, queue);
331         cl_2queue_fini(env, queue);
332         return rc;
333 }
334 EXPORT_SYMBOL(ll_direct_rw_pages);
335
336 static ssize_t ll_direct_IO_26_seg(const struct lu_env *env, struct cl_io *io,
337                                    int rw, struct inode *inode,
338                                    struct address_space *mapping,
339                                    size_t size, loff_t file_offset,
340                                    struct page **pages, int page_count)
341 {
342     struct ll_dio_pages pvec = { .ldp_pages     = pages,
343                                  .ldp_nr           = page_count,
344                                  .ldp_size       = size,
345                                  .ldp_offsets      = NULL,
346                                  .ldp_start_offset = file_offset
347                                };
348
349     return ll_direct_rw_pages(env, io, rw, inode, &pvec);
350 }
351
352 #ifdef KMALLOC_MAX_SIZE
353 #define MAX_MALLOC KMALLOC_MAX_SIZE
354 #else
355 #define MAX_MALLOC (128 * 1024)
356 #endif
357
358 /* This is the maximum size of a single O_DIRECT request, based on the
359  * kmalloc limit.  We need to fit all of the brw_page structs, each one
360  * representing PAGE_SIZE worth of user data, into a single buffer, and
361  * then truncate this to be a full-sized RPC.  For 4kB PAGE_SIZE this is
362  * up to 22MB for 128kB kmalloc and up to 682MB for 4MB kmalloc. */
363 #define MAX_DIO_SIZE ((MAX_MALLOC / sizeof(struct brw_page) * PAGE_CACHE_SIZE) & \
364                       ~(DT_MAX_BRW_SIZE - 1))
365 static ssize_t ll_direct_IO_26(int rw, struct kiocb *iocb,
366                                const struct iovec *iov, loff_t file_offset,
367                                unsigned long nr_segs)
368 {
369         struct lu_env *env;
370         struct cl_io *io;
371         struct file *file = iocb->ki_filp;
372         struct inode *inode = file->f_mapping->host;
373         struct ccc_object *obj = cl_inode2ccc(inode);
374         long count = iov_length(iov, nr_segs);
375         long tot_bytes = 0, result = 0;
376         struct ll_inode_info *lli = ll_i2info(inode);
377         unsigned long seg = 0;
378         long size = MAX_DIO_SIZE;
379         int refcheck;
380
381         if (!lli->lli_has_smd)
382                 return -EBADF;
383
384         /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
385         if ((file_offset & ~CFS_PAGE_MASK) || (count & ~CFS_PAGE_MASK))
386                 return -EINVAL;
387
388         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), size=%lu (max %lu), "
389                "offset=%lld=%llx, pages %lu (max %lu)\n",
390                inode->i_ino, inode->i_generation, inode, count, MAX_DIO_SIZE,
391                file_offset, file_offset, count >> PAGE_CACHE_SHIFT,
392                MAX_DIO_SIZE >> PAGE_CACHE_SHIFT);
393
394         /* Check that all user buffers are aligned as well */
395         for (seg = 0; seg < nr_segs; seg++) {
396                 if (((unsigned long)iov[seg].iov_base & ~CFS_PAGE_MASK) ||
397                     (iov[seg].iov_len & ~CFS_PAGE_MASK))
398                         return -EINVAL;
399         }
400
401         env = cl_env_get(&refcheck);
402         LASSERT(!IS_ERR(env));
403         io = ccc_env_io(env)->cui_cl.cis_io;
404         LASSERT(io != NULL);
405
406         /* 0. Need locking between buffered and direct access. and race with
407          *    size changing by concurrent truncates and writes.
408          * 1. Need inode mutex to operate transient pages.
409          */
410         if (rw == READ)
411                 mutex_lock(&inode->i_mutex);
412
413         LASSERT(obj->cob_transient_pages == 0);
414         for (seg = 0; seg < nr_segs; seg++) {
415                 long iov_left = iov[seg].iov_len;
416                 unsigned long user_addr = (unsigned long)iov[seg].iov_base;
417
418                 if (rw == READ) {
419                         if (file_offset >= i_size_read(inode))
420                                 break;
421                         if (file_offset + iov_left > i_size_read(inode))
422                                 iov_left = i_size_read(inode) - file_offset;
423                 }
424
425                 while (iov_left > 0) {
426                         struct page **pages;
427                         int page_count, max_pages = 0;
428                         long bytes;
429
430                         bytes = min(size, iov_left);
431                         page_count = ll_get_user_pages(rw, user_addr, bytes,
432                                                        &pages, &max_pages);
433                         if (likely(page_count > 0)) {
434                                 if (unlikely(page_count <  max_pages))
435                                         bytes = page_count << PAGE_CACHE_SHIFT;
436                                 result = ll_direct_IO_26_seg(env, io, rw, inode,
437                                                              file->f_mapping,
438                                                              bytes, file_offset,
439                                                              pages, page_count);
440                                 ll_free_user_pages(pages, max_pages, rw==READ);
441                         } else if (page_count == 0) {
442                                 GOTO(out, result = -EFAULT);
443                         } else {
444                                 result = page_count;
445                         }
446                         if (unlikely(result <= 0)) {
447                                 /* If we can't allocate a large enough buffer
448                                  * for the request, shrink it to a smaller
449                                  * PAGE_SIZE multiple and try again.
450                                  * We should always be able to kmalloc for a
451                                  * page worth of page pointers = 4MB on i386. */
452                                 if (result == -ENOMEM &&
453                                     size > (PAGE_CACHE_SIZE / sizeof(*pages)) *
454                                            PAGE_CACHE_SIZE) {
455                                         size = ((((size / 2) - 1) |
456                                                  ~CFS_PAGE_MASK) + 1) &
457                                                 CFS_PAGE_MASK;
458                                         CDEBUG(D_VFSTRACE,"DIO size now %lu\n",
459                                                size);
460                                         continue;
461                                 }
462
463                                 GOTO(out, result);
464                         }
465
466                         tot_bytes += result;
467                         file_offset += result;
468                         iov_left -= result;
469                         user_addr += result;
470                 }
471         }
472 out:
473         LASSERT(obj->cob_transient_pages == 0);
474         if (rw == READ)
475                 mutex_unlock(&inode->i_mutex);
476
477         if (tot_bytes > 0) {
478                 if (rw == WRITE) {
479                         struct lov_stripe_md *lsm;
480
481                         lsm = ccc_inode_lsm_get(inode);
482                         LASSERT(lsm != NULL);
483                         lov_stripe_lock(lsm);
484                         obd_adjust_kms(ll_i2dtexp(inode), lsm, file_offset, 0);
485                         lov_stripe_unlock(lsm);
486                         ccc_inode_lsm_put(inode, lsm);
487                 }
488         }
489
490         cl_env_put(env, &refcheck);
491         return tot_bytes ? : result;
492 }
493
494 static int ll_write_begin(struct file *file, struct address_space *mapping,
495                          loff_t pos, unsigned len, unsigned flags,
496                          struct page **pagep, void **fsdata)
497 {
498         pgoff_t index = pos >> PAGE_CACHE_SHIFT;
499         struct page *page;
500         int rc;
501         unsigned from = pos & (PAGE_CACHE_SIZE - 1);
502
503         page = grab_cache_page_write_begin(mapping, index, flags);
504         if (!page)
505                 return -ENOMEM;
506
507         *pagep = page;
508
509         rc = ll_prepare_write(file, page, from, from + len);
510         if (rc) {
511                 unlock_page(page);
512                 page_cache_release(page);
513         }
514         return rc;
515 }
516
517 static int ll_write_end(struct file *file, struct address_space *mapping,
518                         loff_t pos, unsigned len, unsigned copied,
519                         struct page *page, void *fsdata)
520 {
521         unsigned from = pos & (PAGE_CACHE_SIZE - 1);
522         int rc;
523
524         rc = ll_commit_write(file, page, from, from + copied);
525         unlock_page(page);
526         page_cache_release(page);
527
528         return rc ?: copied;
529 }
530
531 #ifdef CONFIG_MIGRATION
532 int ll_migratepage(struct address_space *mapping,
533                 struct page *newpage, struct page *page
534                 , enum migrate_mode mode
535                 )
536 {
537         /* Always fail page migration until we have a proper implementation */
538         return -EIO;
539 }
540 #endif
541
542 #ifndef MS_HAS_NEW_AOPS
543 struct address_space_operations ll_aops = {
544         .readpage       = ll_readpage,
545 //      .readpages      = ll_readpages,
546         .direct_IO      = ll_direct_IO_26,
547         .writepage      = ll_writepage,
548         .writepages     = ll_writepages,
549         .set_page_dirty = ll_set_page_dirty,
550         .write_begin    = ll_write_begin,
551         .write_end      = ll_write_end,
552         .invalidatepage = ll_invalidatepage,
553         .releasepage    = (void *)ll_releasepage,
554 #ifdef CONFIG_MIGRATION
555         .migratepage    = ll_migratepage,
556 #endif
557         .bmap      = NULL
558 };
559 #else
560 struct address_space_operations_ext ll_aops = {
561         .orig_aops.readpage       = ll_readpage,
562 //      .orig_aops.readpages      = ll_readpages,
563         .orig_aops.direct_IO      = ll_direct_IO_26,
564         .orig_aops.writepage      = ll_writepage,
565         .orig_aops.writepages     = ll_writepages,
566         .orig_aops.set_page_dirty = ll_set_page_dirty,
567         .orig_aops.prepare_write  = ll_prepare_write,
568         .orig_aops.commit_write   = ll_commit_write,
569         .orig_aops.invalidatepage = ll_invalidatepage,
570         .orig_aops.releasepage    = ll_releasepage,
571 #ifdef CONFIG_MIGRATION
572         .orig_aops.migratepage    = ll_migratepage,
573 #endif
574         .orig_aops.bmap    = NULL,
575         .write_begin    = ll_write_begin,
576         .write_end      = ll_write_end
577 };
578 #endif