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