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