GNU Linux-libre 4.14.332-gnu1
[releases.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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2012, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/lustre/llite/rw26.c
33  *
34  * Lustre Lite I/O page cache routines for the 2.5/2.6 kernel version
35  */
36
37 #include <linux/kernel.h>
38 #include <linux/mm.h>
39 #include <linux/string.h>
40 #include <linux/stat.h>
41 #include <linux/errno.h>
42 #include <linux/unistd.h>
43 #include <linux/uaccess.h>
44
45 #include <linux/migrate.h>
46 #include <linux/fs.h>
47 #include <linux/buffer_head.h>
48 #include <linux/mpage.h>
49 #include <linux/writeback.h>
50 #include <linux/pagemap.h>
51
52 #define DEBUG_SUBSYSTEM S_LLITE
53
54 #include "llite_internal.h"
55
56 /**
57  * Implements Linux VM address_space::invalidatepage() method. This method is
58  * called when the page is truncate from a file, either as a result of
59  * explicit truncate, or when inode is removed from memory (as a result of
60  * final iput(), umount, or memory pressure induced icache shrinking).
61  *
62  * [0, offset] bytes of the page remain valid (this is for a case of not-page
63  * aligned truncate). Lustre leaves partially truncated page in the cache,
64  * relying on struct inode::i_size to limit further accesses.
65  */
66 static void ll_invalidatepage(struct page *vmpage, unsigned int offset,
67                               unsigned int length)
68 {
69         struct inode     *inode;
70         struct lu_env    *env;
71         struct cl_page   *page;
72         struct cl_object *obj;
73
74         LASSERT(PageLocked(vmpage));
75         LASSERT(!PageWriteback(vmpage));
76
77         /*
78          * It is safe to not check anything in invalidatepage/releasepage
79          * below because they are run with page locked and all our io is
80          * happening with locked page too
81          */
82         if (offset == 0 && length == PAGE_SIZE) {
83                 /* See the comment in ll_releasepage() */
84                 env = cl_env_percpu_get();
85                 LASSERT(!IS_ERR(env));
86                 inode = vmpage->mapping->host;
87                 obj = ll_i2info(inode)->lli_clob;
88                 if (obj) {
89                         page = cl_vmpage_page(vmpage, obj);
90                         if (page) {
91                                 cl_page_delete(env, page);
92                                 cl_page_put(env, page);
93                         }
94                 } else {
95                         LASSERT(vmpage->private == 0);
96                 }
97                 cl_env_percpu_put(env);
98         }
99 }
100
101 static int ll_releasepage(struct page *vmpage, gfp_t gfp_mask)
102 {
103         struct lu_env     *env;
104         struct cl_object  *obj;
105         struct cl_page    *page;
106         struct address_space *mapping;
107         int result = 0;
108
109         LASSERT(PageLocked(vmpage));
110         if (PageWriteback(vmpage) || PageDirty(vmpage))
111                 return 0;
112
113         mapping = vmpage->mapping;
114         if (!mapping)
115                 return 1;
116
117         obj = ll_i2info(mapping->host)->lli_clob;
118         if (!obj)
119                 return 1;
120
121         /* 1 for caller, 1 for cl_page and 1 for page cache */
122         if (page_count(vmpage) > 3)
123                 return 0;
124
125         page = cl_vmpage_page(vmpage, obj);
126         if (!page)
127                 return 1;
128
129         env = cl_env_percpu_get();
130         LASSERT(!IS_ERR(env));
131
132         if (!cl_page_in_use(page)) {
133                 result = 1;
134                 cl_page_delete(env, page);
135         }
136
137         /* To use percpu env array, the call path can not be rescheduled;
138          * otherwise percpu array will be messed if ll_releaspage() called
139          * again on the same CPU.
140          *
141          * If this page holds the last refc of cl_object, the following
142          * call path may cause reschedule:
143          *   cl_page_put -> cl_page_free -> cl_object_put ->
144          *     lu_object_put -> lu_object_free -> lov_delete_raid0.
145          *
146          * However, the kernel can't get rid of this inode until all pages have
147          * been cleaned up. Now that we hold page lock here, it's pretty safe
148          * that we won't get into object delete path.
149          */
150         LASSERT(cl_object_refc(obj) > 1);
151         cl_page_put(env, page);
152
153         cl_env_percpu_put(env);
154         return result;
155 }
156
157 #define MAX_DIRECTIO_SIZE (2 * 1024 * 1024 * 1024UL)
158
159 /*  ll_free_user_pages - tear down page struct array
160  *  @pages: array of page struct pointers underlying target buffer
161  */
162 static void ll_free_user_pages(struct page **pages, int npages, int do_dirty)
163 {
164         int i;
165
166         for (i = 0; i < npages; i++) {
167                 if (do_dirty)
168                         set_page_dirty_lock(pages[i]);
169                 put_page(pages[i]);
170         }
171         kvfree(pages);
172 }
173
174 ssize_t ll_direct_rw_pages(const struct lu_env *env, struct cl_io *io,
175                            int rw, struct inode *inode,
176                            struct ll_dio_pages *pv)
177 {
178         struct cl_page    *clp;
179         struct cl_2queue  *queue;
180         struct cl_object  *obj = io->ci_obj;
181         int i;
182         ssize_t rc = 0;
183         loff_t file_offset  = pv->ldp_start_offset;
184         size_t size = pv->ldp_size;
185         int page_count      = pv->ldp_nr;
186         struct page **pages = pv->ldp_pages;
187         size_t page_size = cl_page_size(obj);
188         bool do_io;
189         int  io_pages       = 0;
190
191         queue = &io->ci_queue;
192         cl_2queue_init(queue);
193         for (i = 0; i < page_count; i++) {
194                 if (pv->ldp_offsets)
195                         file_offset = pv->ldp_offsets[i];
196
197                 LASSERT(!(file_offset & (page_size - 1)));
198                 clp = cl_page_find(env, obj, cl_index(obj, file_offset),
199                                    pv->ldp_pages[i], CPT_TRANSIENT);
200                 if (IS_ERR(clp)) {
201                         rc = PTR_ERR(clp);
202                         break;
203                 }
204
205                 rc = cl_page_own(env, io, clp);
206                 if (rc) {
207                         LASSERT(clp->cp_state == CPS_FREEING);
208                         cl_page_put(env, clp);
209                         break;
210                 }
211
212                 do_io = true;
213
214                 /* check the page type: if the page is a host page, then do
215                  * write directly
216                  */
217                 if (clp->cp_type == CPT_CACHEABLE) {
218                         struct page *vmpage = cl_page_vmpage(clp);
219                         struct page *src_page;
220                         struct page *dst_page;
221                         void       *src;
222                         void       *dst;
223
224                         src_page = (rw == WRITE) ? pages[i] : vmpage;
225                         dst_page = (rw == WRITE) ? vmpage : pages[i];
226
227                         src = kmap_atomic(src_page);
228                         dst = kmap_atomic(dst_page);
229                         memcpy(dst, src, min(page_size, size));
230                         kunmap_atomic(dst);
231                         kunmap_atomic(src);
232
233                         /* make sure page will be added to the transfer by
234                          * cl_io_submit()->...->vvp_page_prep_write().
235                          */
236                         if (rw == WRITE)
237                                 set_page_dirty(vmpage);
238
239                         if (rw == READ) {
240                                 /* do not issue the page for read, since it
241                                  * may reread a ra page which has NOT uptodate
242                                  * bit set.
243                                  */
244                                 cl_page_disown(env, io, clp);
245                                 do_io = false;
246                         }
247                 }
248
249                 if (likely(do_io)) {
250                         /*
251                          * Add a page to the incoming page list of 2-queue.
252                          */
253                         cl_page_list_add(&queue->c2_qin, clp);
254
255                         /*
256                          * Set page clip to tell transfer formation engine
257                          * that page has to be sent even if it is beyond KMS.
258                          */
259                         cl_page_clip(env, clp, 0, min(size, page_size));
260
261                         ++io_pages;
262                 }
263
264                 /* drop the reference count for cl_page_find */
265                 cl_page_put(env, clp);
266                 size -= page_size;
267                 file_offset += page_size;
268         }
269
270         if (rc == 0 && io_pages) {
271                 rc = cl_io_submit_sync(env, io,
272                                        rw == READ ? CRT_READ : CRT_WRITE,
273                                        queue, 0);
274         }
275         if (rc == 0)
276                 rc = pv->ldp_size;
277
278         cl_2queue_discard(env, io, queue);
279         cl_2queue_disown(env, io, queue);
280         cl_2queue_fini(env, queue);
281         return rc;
282 }
283 EXPORT_SYMBOL(ll_direct_rw_pages);
284
285 static ssize_t ll_direct_IO_26_seg(const struct lu_env *env, struct cl_io *io,
286                                    int rw, struct inode *inode,
287                                    struct address_space *mapping,
288                                    size_t size, loff_t file_offset,
289                                    struct page **pages, int page_count)
290 {
291         struct ll_dio_pages pvec = {
292                 .ldp_pages      = pages,
293                 .ldp_nr         = page_count,
294                 .ldp_size       = size,
295                 .ldp_offsets    = NULL,
296                 .ldp_start_offset = file_offset
297         };
298
299         return ll_direct_rw_pages(env, io, rw, inode, &pvec);
300 }
301
302 /* This is the maximum size of a single O_DIRECT request, based on the
303  * kmalloc limit.  We need to fit all of the brw_page structs, each one
304  * representing PAGE_SIZE worth of user data, into a single buffer, and
305  * then truncate this to be a full-sized RPC.  For 4kB PAGE_SIZE this is
306  * up to 22MB for 128kB kmalloc and up to 682MB for 4MB kmalloc.
307  */
308 #define MAX_DIO_SIZE ((KMALLOC_MAX_SIZE / sizeof(struct brw_page) *       \
309                        PAGE_SIZE) & ~(DT_MAX_BRW_SIZE - 1))
310 static ssize_t ll_direct_IO_26(struct kiocb *iocb, struct iov_iter *iter)
311 {
312         struct ll_cl_context *lcc;
313         const struct lu_env *env;
314         struct cl_io *io;
315         struct file *file = iocb->ki_filp;
316         struct inode *inode = file->f_mapping->host;
317         loff_t file_offset = iocb->ki_pos;
318         ssize_t count = iov_iter_count(iter);
319         ssize_t tot_bytes = 0, result = 0;
320         long size = MAX_DIO_SIZE;
321
322         /* Check EOF by ourselves */
323         if (iov_iter_rw(iter) == READ && file_offset >= i_size_read(inode))
324                 return 0;
325
326         /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
327         if ((file_offset & ~PAGE_MASK) || (count & ~PAGE_MASK))
328                 return -EINVAL;
329
330         CDEBUG(D_VFSTRACE, "VFS Op:inode=" DFID "(%p), size=%zd (max %lu), offset=%lld=%llx, pages %zd (max %lu)\n",
331                PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
332                file_offset, file_offset, count >> PAGE_SHIFT,
333                MAX_DIO_SIZE >> PAGE_SHIFT);
334
335         /* Check that all user buffers are aligned as well */
336         if (iov_iter_alignment(iter) & ~PAGE_MASK)
337                 return -EINVAL;
338
339         lcc = ll_cl_find(file);
340         if (!lcc)
341                 return -EIO;
342
343         env = lcc->lcc_env;
344         LASSERT(!IS_ERR(env));
345         io = lcc->lcc_io;
346         LASSERT(io);
347
348         while (iov_iter_count(iter)) {
349                 struct page **pages;
350                 size_t offs;
351
352                 count = min_t(size_t, iov_iter_count(iter), size);
353                 if (iov_iter_rw(iter) == READ) {
354                         if (file_offset >= i_size_read(inode))
355                                 break;
356                         if (file_offset + count > i_size_read(inode))
357                                 count = i_size_read(inode) - file_offset;
358                 }
359
360                 result = iov_iter_get_pages_alloc(iter, &pages, count, &offs);
361                 if (likely(result > 0)) {
362                         int n = DIV_ROUND_UP(result + offs, PAGE_SIZE);
363
364                         result = ll_direct_IO_26_seg(env, io, iov_iter_rw(iter),
365                                                      inode, file->f_mapping,
366                                                      result, file_offset, pages,
367                                                      n);
368                         ll_free_user_pages(pages, n, iov_iter_rw(iter) == READ);
369                 }
370                 if (unlikely(result <= 0)) {
371                         /* If we can't allocate a large enough buffer
372                          * for the request, shrink it to a smaller
373                          * PAGE_SIZE multiple and try again.
374                          * We should always be able to kmalloc for a
375                          * page worth of page pointers = 4MB on i386.
376                          */
377                         if (result == -ENOMEM &&
378                             size > (PAGE_SIZE / sizeof(*pages)) *
379                             PAGE_SIZE) {
380                                 size = ((((size / 2) - 1) |
381                                          ~PAGE_MASK) + 1) &
382                                         PAGE_MASK;
383                                 CDEBUG(D_VFSTRACE, "DIO size now %lu\n",
384                                        size);
385                                 continue;
386                         }
387
388                         goto out;
389                 }
390                 iov_iter_advance(iter, result);
391                 tot_bytes += result;
392                 file_offset += result;
393         }
394 out:
395         if (tot_bytes > 0) {
396                 struct vvp_io *vio = vvp_env_io(env);
397
398                 /* no commit async for direct IO */
399                 vio->u.write.vui_written += tot_bytes;
400         }
401
402         return tot_bytes ? tot_bytes : result;
403 }
404
405 /**
406  * Prepare partially written-to page for a write.
407  */
408 static int ll_prepare_partial_page(const struct lu_env *env, struct cl_io *io,
409                                    struct cl_page *pg)
410 {
411         struct cl_attr *attr   = vvp_env_thread_attr(env);
412         struct cl_object *obj  = io->ci_obj;
413         struct vvp_page *vpg   = cl_object_page_slice(obj, pg);
414         loff_t          offset = cl_offset(obj, vvp_index(vpg));
415         int             result;
416
417         cl_object_attr_lock(obj);
418         result = cl_object_attr_get(env, obj, attr);
419         cl_object_attr_unlock(obj);
420         if (result == 0) {
421                 /*
422                  * If are writing to a new page, no need to read old data.
423                  * The extent locking will have updated the KMS, and for our
424                  * purposes here we can treat it like i_size.
425                  */
426                 if (attr->cat_kms <= offset) {
427                         char *kaddr = kmap_atomic(vpg->vpg_page);
428
429                         memset(kaddr, 0, cl_page_size(obj));
430                         kunmap_atomic(kaddr);
431                 } else if (vpg->vpg_defer_uptodate) {
432                         vpg->vpg_ra_used = 1;
433                 } else {
434                         result = ll_page_sync_io(env, io, pg, CRT_READ);
435                 }
436         }
437         return result;
438 }
439
440 static int ll_write_begin(struct file *file, struct address_space *mapping,
441                           loff_t pos, unsigned int len, unsigned int flags,
442                           struct page **pagep, void **fsdata)
443 {
444         struct ll_cl_context *lcc;
445         const struct lu_env *env = NULL;
446         struct cl_io   *io;
447         struct cl_page *page = NULL;
448         struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
449         pgoff_t index = pos >> PAGE_SHIFT;
450         struct page *vmpage = NULL;
451         unsigned int from = pos & (PAGE_SIZE - 1);
452         unsigned int to = from + len;
453         int result = 0;
454
455         CDEBUG(D_VFSTRACE, "Writing %lu of %d to %d bytes\n", index, from, len);
456
457         lcc = ll_cl_find(file);
458         if (!lcc) {
459                 io = NULL;
460                 result = -EIO;
461                 goto out;
462         }
463
464         env = lcc->lcc_env;
465         io  = lcc->lcc_io;
466
467         /* To avoid deadlock, try to lock page first. */
468         vmpage = grab_cache_page_nowait(mapping, index);
469         if (unlikely(!vmpage || PageDirty(vmpage) || PageWriteback(vmpage))) {
470                 struct vvp_io *vio = vvp_env_io(env);
471                 struct cl_page_list *plist = &vio->u.write.vui_queue;
472
473                 /* if the page is already in dirty cache, we have to commit
474                  * the pages right now; otherwise, it may cause deadlock
475                  * because it holds page lock of a dirty page and request for
476                  * more grants. It's okay for the dirty page to be the first
477                  * one in commit page list, though.
478                  */
479                 if (vmpage && plist->pl_nr > 0) {
480                         unlock_page(vmpage);
481                         put_page(vmpage);
482                         vmpage = NULL;
483                 }
484
485                 /* commit pages and then wait for page lock */
486                 result = vvp_io_write_commit(env, io);
487                 if (result < 0)
488                         goto out;
489
490                 if (!vmpage) {
491                         vmpage = grab_cache_page_write_begin(mapping, index,
492                                                              flags);
493                         if (!vmpage) {
494                                 result = -ENOMEM;
495                                 goto out;
496                         }
497                 }
498         }
499
500         page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
501         if (IS_ERR(page)) {
502                 result = PTR_ERR(page);
503                 goto out;
504         }
505
506         lcc->lcc_page = page;
507         lu_ref_add(&page->cp_reference, "cl_io", io);
508
509         cl_page_assume(env, io, page);
510         if (!PageUptodate(vmpage)) {
511                 /*
512                  * We're completely overwriting an existing page,
513                  * so _don't_ set it up to date until commit_write
514                  */
515                 if (from == 0 && to == PAGE_SIZE) {
516                         CL_PAGE_HEADER(D_PAGE, env, page, "full page write\n");
517                         POISON_PAGE(vmpage, 0x11);
518                 } else {
519                         /* TODO: can be optimized at OSC layer to check if it
520                          * is a lockless IO. In that case, it's not necessary
521                          * to read the data.
522                          */
523                         result = ll_prepare_partial_page(env, io, page);
524                         if (result == 0)
525                                 SetPageUptodate(vmpage);
526                 }
527         }
528         if (result < 0)
529                 cl_page_unassume(env, io, page);
530 out:
531         if (result < 0) {
532                 if (vmpage) {
533                         unlock_page(vmpage);
534                         put_page(vmpage);
535                 }
536                 if (!IS_ERR_OR_NULL(page)) {
537                         lu_ref_del(&page->cp_reference, "cl_io", io);
538                         cl_page_put(env, page);
539                 }
540                 if (io)
541                         io->ci_result = result;
542         } else {
543                 *pagep = vmpage;
544                 *fsdata = lcc;
545         }
546         return result;
547 }
548
549 static int ll_write_end(struct file *file, struct address_space *mapping,
550                         loff_t pos, unsigned int len, unsigned int copied,
551                         struct page *vmpage, void *fsdata)
552 {
553         struct ll_cl_context *lcc = fsdata;
554         const struct lu_env *env;
555         struct cl_io *io;
556         struct vvp_io *vio;
557         struct cl_page *page;
558         unsigned int from = pos & (PAGE_SIZE - 1);
559         bool unplug = false;
560         int result = 0;
561
562         put_page(vmpage);
563
564         env  = lcc->lcc_env;
565         page = lcc->lcc_page;
566         io   = lcc->lcc_io;
567         vio  = vvp_env_io(env);
568
569         LASSERT(cl_page_is_owned(page, io));
570         if (copied > 0) {
571                 struct cl_page_list *plist = &vio->u.write.vui_queue;
572
573                 lcc->lcc_page = NULL; /* page will be queued */
574
575                 /* Add it into write queue */
576                 cl_page_list_add(plist, page);
577                 if (plist->pl_nr == 1) /* first page */
578                         vio->u.write.vui_from = from;
579                 else
580                         LASSERT(from == 0);
581                 vio->u.write.vui_to = from + copied;
582
583                 /*
584                  * To address the deadlock in balance_dirty_pages() where
585                  * this dirty page may be written back in the same thread.
586                  */
587                 if (PageDirty(vmpage))
588                         unplug = true;
589
590                 /* We may have one full RPC, commit it soon */
591                 if (plist->pl_nr >= PTLRPC_MAX_BRW_PAGES)
592                         unplug = true;
593
594                 CL_PAGE_DEBUG(D_VFSTRACE, env, page,
595                               "queued page: %d.\n", plist->pl_nr);
596         } else {
597                 cl_page_disown(env, io, page);
598
599                 lcc->lcc_page = NULL;
600                 lu_ref_del(&page->cp_reference, "cl_io", io);
601                 cl_page_put(env, page);
602
603                 /* page list is not contiguous now, commit it now */
604                 unplug = true;
605         }
606
607         if (unplug ||
608             file->f_flags & O_SYNC || IS_SYNC(file_inode(file)))
609                 result = vvp_io_write_commit(env, io);
610
611         if (result < 0)
612                 io->ci_result = result;
613         return result >= 0 ? copied : result;
614 }
615
616 #ifdef CONFIG_MIGRATION
617 static int ll_migratepage(struct address_space *mapping,
618                           struct page *newpage, struct page *page,
619                           enum migrate_mode mode
620                 )
621 {
622         /* Always fail page migration until we have a proper implementation */
623         return -EIO;
624 }
625 #endif
626
627 const struct address_space_operations ll_aops = {
628         .readpage       = ll_readpage,
629         .direct_IO      = ll_direct_IO_26,
630         .writepage      = ll_writepage,
631         .writepages     = ll_writepages,
632         .set_page_dirty = __set_page_dirty_nobuffers,
633         .write_begin    = ll_write_begin,
634         .write_end      = ll_write_end,
635         .invalidatepage = ll_invalidatepage,
636         .releasepage    = (void *)ll_releasepage,
637 #ifdef CONFIG_MIGRATION
638         .migratepage    = ll_migratepage,
639 #endif
640 };