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