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