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