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