Whamcloud - gitweb
LU-16515 clio: Remove cl_page_size()
[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.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, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/lustre/llite/rw26.c
32  *
33  * Lustre Lite I/O page cache routines for the 2.5/2.6 kernel version
34  */
35
36 #include <linux/buffer_head.h>
37 #include <linux/errno.h>
38 #include <linux/fs.h>
39 #include <linux/kernel.h>
40 #include <linux/mm.h>
41 #include <linux/mpage.h>
42 #include <linux/pagemap.h>
43 #include <linux/string.h>
44 #include <linux/unistd.h>
45 #include <linux/writeback.h>
46 #include <linux/migrate.h>
47
48 #define DEBUG_SUBSYSTEM S_LLITE
49
50 #include "llite_internal.h"
51 #include <lustre_compat.h>
52
53 #ifdef HAVE_INVALIDATE_FOLIO
54 /**
55  * Implements Linux VM address_space::invalidate_folio() method. This method is
56  * called when the folio is truncated from a file, either as a result of
57  * explicit truncate, or when inode is removed from memory (as a result of
58  * final iput(), umount, or memory pressure induced icache shrinking).
59  *
60  * [0, off] bytes of the folio remain valid (this is for a case of non-page
61  * aligned truncate). Lustre leaves partially truncated folios in the cache,
62  * relying on struct inode::i_size to limit further accesses.
63  */
64 static void ll_invalidate_folio(struct folio *folio, size_t offset, size_t len)
65 {
66         struct inode *inode;
67         struct lu_env *env;
68         struct cl_page *page;
69         struct cl_object *obj;
70
71         LASSERT(!folio_test_writeback(folio));
72         LASSERT(folio_test_locked(folio));
73
74         if (!(offset == 0 && len == folio_size(folio)) &&
75             !folio_test_large(folio))
76                 return;
77
78         /* Drop the pages from the folio */
79         env = cl_env_percpu_get();
80         LASSERT(!IS_ERR(env));
81
82         inode = folio_inode(folio);
83         obj = ll_i2info(inode)->lli_clob;
84         if (obj != NULL) {
85                 int n, npgs = folio_nr_pages(folio);
86
87                 for (n = 0; n < npgs; n++) {
88                         struct page *vmpage = folio_page(folio, n);
89
90                         LASSERT(PageLocked(vmpage));
91                         LASSERT(!PageWriteback(vmpage));
92
93                         page = cl_vmpage_page(vmpage, obj);
94                         if (page != NULL) {
95                                 cl_page_delete(env, page);
96                                 cl_page_put(env, page);
97                         }
98                 }
99         } else {
100                 LASSERT(!folio_get_private(folio));
101         }
102         cl_env_percpu_put(env);
103 }
104 #else
105
106 /**
107  * Implements Linux VM address_space::invalidatepage() method. This method is
108  * called when the page is truncate from a file, either as a result of
109  * explicit truncate, or when inode is removed from memory (as a result of
110  * final iput(), umount, or memory pressure induced icache shrinking).
111  *
112  * [0, offset] bytes of the page remain valid (this is for a case of not-page
113  * aligned truncate). Lustre leaves partially truncated page in the cache,
114  * relying on struct inode::i_size to limit further accesses.
115  */
116 static void ll_invalidatepage(struct page *vmpage,
117 #ifdef HAVE_INVALIDATE_RANGE
118                                 unsigned int offset, unsigned int length
119 #else
120                                 unsigned long offset
121 #endif
122                              )
123 {
124         struct inode     *inode;
125         struct lu_env    *env;
126         struct cl_page   *page;
127         struct cl_object *obj;
128
129         LASSERT(PageLocked(vmpage));
130         LASSERT(!PageWriteback(vmpage));
131
132         /*
133          * It is safe to not check anything in invalidatepage/releasepage
134          * below because they are run with page locked and all our io is
135          * happening with locked page too
136          */
137 #ifdef HAVE_INVALIDATE_RANGE
138         if (offset == 0 && length == PAGE_SIZE) {
139 #else
140         if (offset == 0) {
141 #endif
142                 /* See the comment in ll_releasepage() */
143                 env = cl_env_percpu_get();
144                 LASSERT(!IS_ERR(env));
145
146                 inode = vmpage->mapping->host;
147                 obj = ll_i2info(inode)->lli_clob;
148                 if (obj != NULL) {
149                         page = cl_vmpage_page(vmpage, obj);
150                         if (page != NULL) {
151                                 cl_page_delete(env, page);
152                                 cl_page_put(env, page);
153                         }
154                 } else
155                         LASSERT(vmpage->private == 0);
156
157                 cl_env_percpu_put(env);
158         }
159
160         if (OBD_FAIL_PRECHECK(OBD_FAIL_LLITE_PAGE_INVALIDATE_PAUSE)) {
161                 unlock_page(vmpage);
162                 OBD_FAIL_TIMEOUT(OBD_FAIL_LLITE_PAGE_INVALIDATE_PAUSE,
163                                  cfs_fail_val);
164                 lock_page(vmpage);
165         }
166 }
167 #endif
168
169 #ifdef HAVE_RELEASEPAGE_WITH_INT
170 #define RELEASEPAGE_ARG_TYPE int
171 #else
172 #define RELEASEPAGE_ARG_TYPE gfp_t
173 #endif
174 static int ll_releasepage(struct page *vmpage, RELEASEPAGE_ARG_TYPE gfp_mask)
175 {
176         struct lu_env           *env;
177         struct cl_object        *obj;
178         struct cl_page          *clpage;
179         struct address_space    *mapping;
180         int result = 0;
181
182         LASSERT(PageLocked(vmpage));
183         if (PageWriteback(vmpage) || PageDirty(vmpage))
184                 return 0;
185
186         mapping = vmpage->mapping;
187         if (mapping == NULL)
188                 return 1;
189
190         obj = ll_i2info(mapping->host)->lli_clob;
191         if (obj == NULL)
192                 return 1;
193
194         clpage = cl_vmpage_page(vmpage, obj);
195         if (clpage == NULL)
196                 return 1;
197
198         env = cl_env_percpu_get();
199         LASSERT(!IS_ERR(env));
200
201         /* we must not delete the cl_page if the vmpage is in use, otherwise we
202          * disconnect the vmpage from Lustre while it's still alive(!), which
203          * means we won't find it to discard on lock cancellation.
204          *
205          * References here are: caller + cl_page + page cache.
206          * Any other references are potentially transient and must be ignored.
207          */
208         if (!cl_page_in_use(clpage) && !vmpage_in_use(vmpage, 1)) {
209                 result = 1;
210                 cl_page_delete(env, clpage);
211         }
212
213         /* To use percpu env array, the call path can not be rescheduled;
214          * otherwise percpu array will be messed if ll_releaspage() called
215          * again on the same CPU.
216          *
217          * If this page holds the last refc of cl_object, the following
218          * call path may cause reschedule:
219          *   cl_page_put -> cl_page_free -> cl_object_put ->
220          *     lu_object_put -> lu_object_free -> lov_delete_raid0.
221          *
222          * However, the kernel can't get rid of this inode until all pages have
223          * been cleaned up. Now that we hold page lock here, it's pretty safe
224          * that we won't get into object delete path.
225          */
226         LASSERT(cl_object_refc(obj) > 1);
227         cl_page_put(env, clpage);
228
229         cl_env_percpu_put(env);
230         return result;
231 }
232
233 static ssize_t ll_get_user_pages(int rw, struct iov_iter *iter,
234                                 struct page ***pages, ssize_t *npages,
235                                 size_t maxsize)
236 {
237 #if defined(HAVE_DIO_ITER)
238         size_t start;
239         size_t result;
240
241         /*
242          * iov_iter_get_pages_alloc() is introduced in 3.16 similar
243          * to HAVE_DIO_ITER.
244          */
245         result = iov_iter_get_pages_alloc(iter, pages, maxsize, &start);
246         if (result > 0)
247                 *npages = DIV_ROUND_UP(result + start, PAGE_SIZE);
248
249         return result;
250 #else
251         unsigned long addr;
252         size_t page_count;
253         size_t size;
254         long result;
255
256         if (!maxsize)
257                 return 0;
258
259         if (!iter->nr_segs)
260                 return 0;
261
262         addr = (unsigned long)iter->iov->iov_base + iter->iov_offset;
263         if (addr & ~PAGE_MASK)
264                 return -EINVAL;
265
266         size = min_t(size_t, maxsize, iter->iov->iov_len);
267         page_count = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
268         OBD_ALLOC_PTR_ARRAY_LARGE(*pages, page_count);
269         if (*pages == NULL)
270                 return -ENOMEM;
271
272         mmap_read_lock(current->mm);
273         result = get_user_pages(current, current->mm, addr, page_count,
274                                 rw == READ, 0, *pages, NULL);
275         mmap_read_unlock(current->mm);
276
277         if (unlikely(result != page_count)) {
278                 ll_release_user_pages(*pages, page_count);
279                 *pages = NULL;
280
281                 if (result >= 0)
282                         return -EFAULT;
283
284                 return result;
285         }
286         *npages = page_count;
287
288         return size;
289 #endif
290 }
291
292 /* iov_iter_alignment() is introduced in 3.16 similar to HAVE_DIO_ITER */
293 #if defined(HAVE_DIO_ITER)
294 static unsigned long iov_iter_alignment_vfs(const struct iov_iter *i)
295 {
296         return iov_iter_alignment(i);
297 }
298 #else /* copied from alignment_iovec() */
299 static unsigned long iov_iter_alignment_vfs(const struct iov_iter *i)
300 {
301         const struct iovec *iov = i->iov;
302         unsigned long res;
303         size_t size = i->count;
304         size_t n;
305
306         if (!size)
307                 return 0;
308
309         res = (unsigned long)iov->iov_base + i->iov_offset;
310         n = iov->iov_len - i->iov_offset;
311         if (n >= size)
312                 return res | size;
313
314         size -= n;
315         res |= n;
316         while (size > (++iov)->iov_len) {
317                 res |= (unsigned long)iov->iov_base | iov->iov_len;
318                 size -= iov->iov_len;
319         }
320         res |= (unsigned long)iov->iov_base | size;
321
322         return res;
323 }
324 #endif
325
326 /*
327  * Lustre could relax a bit for alignment, io count is not
328  * necessary page alignment.
329  */
330 static unsigned long ll_iov_iter_alignment(struct iov_iter *i)
331 {
332         size_t orig_size = i->count;
333         size_t count = orig_size & ~PAGE_MASK;
334         unsigned long res;
335
336         if (!count)
337                 return iov_iter_alignment_vfs(i);
338
339         if (orig_size > PAGE_SIZE) {
340                 iov_iter_truncate(i, orig_size - count);
341                 res = iov_iter_alignment_vfs(i);
342                 iov_iter_reexpand(i, orig_size);
343
344                 return res;
345         }
346
347         res = iov_iter_alignment_vfs(i);
348         /* start address is page aligned */
349         if ((res & ~PAGE_MASK) == orig_size)
350                 return PAGE_SIZE;
351
352         return res;
353 }
354
355 static int
356 ll_direct_rw_pages(const struct lu_env *env, struct cl_io *io, size_t size,
357                    int rw, struct inode *inode, struct cl_sub_dio *sdio)
358 {
359         struct ll_dio_pages *pv = &sdio->csd_dio_pages;
360         struct cl_page    *page;
361         struct cl_2queue  *queue = &io->ci_queue;
362         struct cl_object  *obj = io->ci_obj;
363         struct cl_sync_io *anchor = &sdio->csd_sync;
364         loff_t offset   = pv->ldp_file_offset;
365         int io_pages    = 0;
366         int i;
367         ssize_t rc = 0;
368
369         ENTRY;
370
371         cl_2queue_init(queue);
372         for (i = 0; i < pv->ldp_count; i++) {
373                 LASSERT(!(offset & (PAGE_SIZE - 1)));
374                 page = cl_page_find(env, obj, cl_index(obj, offset),
375                                     pv->ldp_pages[i], CPT_TRANSIENT);
376                 if (IS_ERR(page)) {
377                         rc = PTR_ERR(page);
378                         break;
379                 }
380                 LASSERT(page->cp_type == CPT_TRANSIENT);
381                 rc = cl_page_own(env, io, page);
382                 if (rc) {
383                         cl_page_put(env, page);
384                         break;
385                 }
386
387                 page->cp_sync_io = anchor;
388                 if (inode && IS_ENCRYPTED(inode)) {
389                         /* In case of Direct IO on encrypted file, we need to
390                          * add a reference to the inode on the cl_page.
391                          * This info is required by llcrypt to proceed
392                          * to encryption/decryption.
393                          * This is safe because we know these pages are private
394                          * to the thread doing the Direct IO.
395                          */
396                         page->cp_inode = inode;
397                 }
398                 /* We keep the refcount from cl_page_find, so we don't need
399                  * another one here
400                  */
401                 cl_page_list_add(&queue->c2_qin, page, false);
402                 /*
403                  * Set page clip to tell transfer formation engine
404                  * that page has to be sent even if it is beyond KMS.
405                  */
406                 if (size < PAGE_SIZE)
407                         cl_page_clip(env, page, 0, size);
408                 ++io_pages;
409
410                 offset += PAGE_SIZE;
411                 size -= PAGE_SIZE;
412         }
413         if (rc == 0 && io_pages > 0) {
414                 int iot = rw == READ ? CRT_READ : CRT_WRITE;
415
416                 atomic_add(io_pages, &anchor->csi_sync_nr);
417                 /*
418                  * Avoid out-of-order execution of adding inflight
419                  * modifications count and io submit.
420                  */
421                 smp_mb();
422                 rc = cl_io_submit_rw(env, io, iot, queue);
423                 if (rc == 0) {
424                         cl_page_list_splice(&queue->c2_qout, &sdio->csd_pages);
425                 } else {
426                         atomic_add(-queue->c2_qin.pl_nr,
427                                    &anchor->csi_sync_nr);
428                         cl_page_list_for_each(page, &queue->c2_qin)
429                                 page->cp_sync_io = NULL;
430                 }
431                 /* handle partially submitted reqs */
432                 if (queue->c2_qin.pl_nr > 0) {
433                         CERROR(DFID " failed to submit %d dio pages: %zd\n",
434                                PFID(lu_object_fid(&obj->co_lu)),
435                                queue->c2_qin.pl_nr, rc);
436                         if (rc == 0)
437                                 rc = -EIO;
438                 }
439         }
440
441         cl_2queue_discard(env, io, queue);
442         cl_2queue_disown(env, queue);
443         cl_2queue_fini(env, queue);
444         RETURN(rc);
445 }
446
447 #ifdef KMALLOC_MAX_SIZE
448 #define MAX_MALLOC KMALLOC_MAX_SIZE
449 #else
450 #define MAX_MALLOC (128 * 1024)
451 #endif
452
453 /* This is the maximum size of a single O_DIRECT request, based on the
454  * kmalloc limit.  We need to fit all of the brw_page structs, each one
455  * representing PAGE_SIZE worth of user data, into a single buffer, and
456  * then truncate this to be a full-sized RPC.  For 4kB PAGE_SIZE this is
457  * up to 22MB for 128kB kmalloc and up to 682MB for 4MB kmalloc. */
458 #define MAX_DIO_SIZE ((MAX_MALLOC / sizeof(struct brw_page) * PAGE_SIZE) & \
459                       ~((size_t)DT_MAX_BRW_SIZE - 1))
460
461 static ssize_t
462 ll_direct_IO_impl(struct kiocb *iocb, struct iov_iter *iter, int rw)
463 {
464         struct ll_cl_context *lcc;
465         const struct lu_env *env;
466         struct cl_io *io;
467         struct file *file = iocb->ki_filp;
468         struct inode *inode = file->f_mapping->host;
469         struct cl_dio_aio *ll_dio_aio;
470         struct cl_sub_dio *ldp_aio;
471         size_t count = iov_iter_count(iter);
472         ssize_t tot_bytes = 0, result = 0;
473         loff_t file_offset = iocb->ki_pos;
474         bool sync_submit = false;
475         struct vvp_io *vio;
476         ssize_t rc2;
477
478         /* Check EOF by ourselves */
479         if (rw == READ && file_offset >= i_size_read(inode))
480                 return 0;
481
482         /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
483         if (file_offset & ~PAGE_MASK)
484                 RETURN(-EINVAL);
485
486         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), size=%zd (max %lu), "
487                "offset=%lld=%llx, pages %zd (max %lu)\n",
488                PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
489                file_offset, file_offset, count >> PAGE_SHIFT,
490                MAX_DIO_SIZE >> PAGE_SHIFT);
491
492         /* Check that all user buffers are aligned as well */
493         if (ll_iov_iter_alignment(iter) & ~PAGE_MASK)
494                 RETURN(-EINVAL);
495
496         lcc = ll_cl_find(inode);
497         if (lcc == NULL)
498                 RETURN(-EIO);
499
500         env = lcc->lcc_env;
501         LASSERT(!IS_ERR(env));
502         vio = vvp_env_io(env);
503         io = lcc->lcc_io;
504         LASSERT(io != NULL);
505
506         ll_dio_aio = io->ci_dio_aio;
507         LASSERT(ll_dio_aio);
508         LASSERT(ll_dio_aio->cda_iocb == iocb);
509
510         /* We cannot do parallel submission of sub-I/Os - for AIO or regular
511          * DIO - unless lockless because it causes us to release the lock
512          * early.
513          *
514          * There are also several circumstances in which we must disable
515          * parallel DIO, so we check if it is enabled.
516          *
517          * The check for "is_sync_kiocb" excludes AIO, which does not need to
518          * be disabled in these situations.
519          */
520         if (io->ci_dio_lock || (is_sync_kiocb(iocb) && !io->ci_parallel_dio))
521                 sync_submit = true;
522
523         while (iov_iter_count(iter)) {
524                 struct ll_dio_pages *pvec;
525                 struct page **pages;
526
527                 count = min_t(size_t, iov_iter_count(iter), MAX_DIO_SIZE);
528                 if (rw == READ) {
529                         if (file_offset >= i_size_read(inode))
530                                 break;
531
532                         if (file_offset + count > i_size_read(inode))
533                                 count = i_size_read(inode) - file_offset;
534                 }
535
536                 /* if we are doing sync_submit, then we free this below,
537                  * otherwise it is freed on the final call to cl_sync_io_note
538                  * (either in this function or from a ptlrpcd daemon)
539                  */
540                 ldp_aio = cl_sub_dio_alloc(ll_dio_aio, sync_submit);
541                 if (!ldp_aio)
542                         GOTO(out, result = -ENOMEM);
543
544                 pvec = &ldp_aio->csd_dio_pages;
545
546                 result = ll_get_user_pages(rw, iter, &pages,
547                                            &pvec->ldp_count, count);
548                 if (unlikely(result <= 0)) {
549                         cl_sync_io_note(env, &ldp_aio->csd_sync, result);
550                         if (sync_submit) {
551                                 LASSERT(ldp_aio->csd_creator_free);
552                                 cl_sub_dio_free(ldp_aio);
553                         }
554                         GOTO(out, result);
555                 }
556
557                 count = result;
558                 pvec->ldp_file_offset = file_offset;
559                 pvec->ldp_pages = pages;
560
561                 result = ll_direct_rw_pages(env, io, count,
562                                             rw, inode, ldp_aio);
563                 /* We've submitted pages and can now remove the extra
564                  * reference for that
565                  */
566                 cl_sync_io_note(env, &ldp_aio->csd_sync, result);
567
568                 if (sync_submit) {
569                         rc2 = cl_sync_io_wait(env, &ldp_aio->csd_sync,
570                                              0);
571                         if (result == 0 && rc2)
572                                 result = rc2;
573                         LASSERT(ldp_aio->csd_creator_free);
574                         cl_sub_dio_free(ldp_aio);
575                 }
576                 if (unlikely(result < 0))
577                         GOTO(out, result);
578
579                 iov_iter_advance(iter, count);
580                 tot_bytes += count;
581                 file_offset += count;
582         }
583
584 out:
585         ll_dio_aio->cda_bytes += tot_bytes;
586
587         if (rw == WRITE)
588                 vio->u.readwrite.vui_written += tot_bytes;
589         else
590                 vio->u.readwrite.vui_read += tot_bytes;
591
592         /* AIO is not supported on pipes, so we cannot return EIOCBQEUED like
593          * we normally would for both DIO and AIO here
594          */
595         if (result == 0 && !iov_iter_is_pipe(iter))
596                 result = -EIOCBQUEUED;
597
598         return result;
599 }
600
601 #if defined(HAVE_DIO_ITER)
602 static ssize_t ll_direct_IO(
603 #ifndef HAVE_IOV_ITER_RW
604              int rw,
605 #endif
606              struct kiocb *iocb, struct iov_iter *iter
607 #ifndef HAVE_DIRECTIO_2ARGS
608              , loff_t file_offset
609 #endif
610              )
611 {
612         int nrw;
613
614 #ifndef HAVE_IOV_ITER_RW
615         nrw = rw;
616 #else
617         nrw = iov_iter_rw(iter);
618 #endif
619
620         return ll_direct_IO_impl(iocb, iter, nrw);
621 }
622
623 #else /* !defined(HAVE_DIO_ITER) */
624
625 static ssize_t
626 ll_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
627              loff_t file_offset, unsigned long nr_segs)
628 {
629         struct iov_iter iter;
630
631         iov_iter_init(&iter, iov, nr_segs, iov_length(iov, nr_segs), 0);
632         return ll_direct_IO_impl(iocb, &iter, rw);
633 }
634
635 #endif /* !defined(HAVE_DIO_ITER) */
636
637 /**
638  * Prepare partially written-to page for a write.
639  * @pg is owned when passed in and disowned when it returns non-zero result to
640  * the caller.
641  */
642 static int ll_prepare_partial_page(const struct lu_env *env, struct cl_io *io,
643                                    struct cl_page *pg, struct file *file)
644 {
645         struct cl_attr *attr   = vvp_env_thread_attr(env);
646         struct cl_object *obj  = io->ci_obj;
647         loff_t          offset = cl_offset(obj, cl_page_index(pg));
648         int             result;
649         ENTRY;
650
651         cl_object_attr_lock(obj);
652         result = cl_object_attr_get(env, obj, attr);
653         cl_object_attr_unlock(obj);
654         if (result) {
655                 cl_page_disown(env, io, pg);
656                 GOTO(out, result);
657         }
658
659         /*
660          * If are writing to a new page, no need to read old data.
661          * The extent locking will have updated the KMS, and for our
662          * purposes here we can treat it like i_size.
663          */
664         if (attr->cat_kms <= offset) {
665                 char *kaddr = kmap_atomic(pg->cp_vmpage);
666
667                 memset(kaddr, 0, PAGE_SIZE);
668                 kunmap_atomic(kaddr);
669                 GOTO(out, result = 0);
670         }
671
672         if (pg->cp_defer_uptodate) {
673                 pg->cp_ra_used = 1;
674                 GOTO(out, result = 0);
675         }
676
677         result = ll_io_read_page(env, io, pg, file);
678         if (result)
679                 GOTO(out, result);
680
681         /* ll_io_read_page() disowns the page */
682         result = cl_page_own(env, io, pg);
683         if (!result) {
684                 if (!PageUptodate(cl_page_vmpage(pg))) {
685                         cl_page_disown(env, io, pg);
686                         result = -EIO;
687                 }
688         } else if (result == -ENOENT) {
689                 /* page was truncated */
690                 result = -EAGAIN;
691         }
692         EXIT;
693
694 out:
695         return result;
696 }
697
698 static int ll_tiny_write_begin(struct page *vmpage, struct address_space *mapping)
699 {
700         /* Page must be present, up to date, dirty, and not in writeback. */
701         if (!vmpage || !PageUptodate(vmpage) || !PageDirty(vmpage) ||
702             PageWriteback(vmpage) || vmpage->mapping != mapping)
703                 return -ENODATA;
704
705         return 0;
706 }
707
708 static int ll_write_begin(struct file *file, struct address_space *mapping,
709                           loff_t pos, unsigned len, unsigned flags,
710                           struct page **pagep, void **fsdata)
711 {
712         struct ll_cl_context *lcc = NULL;
713         const struct lu_env  *env = NULL;
714         struct cl_io   *io = NULL;
715         struct cl_page *page = NULL;
716         struct inode *inode = file_inode(file);
717         struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
718         pgoff_t index = pos >> PAGE_SHIFT;
719         struct page *vmpage = NULL;
720         unsigned from = pos & (PAGE_SIZE - 1);
721         unsigned to = from + len;
722         int result = 0;
723         ENTRY;
724
725         CDEBUG(D_VFSTRACE, "Writing %lu of %d to %d bytes\n", index, from, len);
726
727         lcc = ll_cl_find(inode);
728         if (lcc == NULL) {
729                 vmpage = grab_cache_page_nowait(mapping, index);
730                 result = ll_tiny_write_begin(vmpage, mapping);
731                 GOTO(out, result);
732         }
733
734         env = lcc->lcc_env;
735         io  = lcc->lcc_io;
736
737         if (file->f_flags & O_DIRECT) {
738                 /* direct IO failed because it couldn't clean up cached pages,
739                  * this causes a problem for mirror write because the cached
740                  * page may belong to another mirror, which will result in
741                  * problem submitting the I/O. */
742                 if (io->ci_designated_mirror > 0)
743                         GOTO(out, result = -EBUSY);
744
745                 /**
746                  * Direct write can fall back to buffered read, but DIO is done
747                  * with lockless i/o, and buffered requires LDLM locking, so
748                  * in this case we must restart without lockless.
749                  */
750                 if (!io->ci_dio_lock) {
751                         io->ci_dio_lock = 1;
752                         io->ci_need_restart = 1;
753                         GOTO(out, result = -ENOLCK);
754                 }
755         }
756 again:
757         /* To avoid deadlock, try to lock page first. */
758         vmpage = grab_cache_page_nowait(mapping, index);
759
760         if (unlikely(vmpage == NULL ||
761                      PageDirty(vmpage) || PageWriteback(vmpage))) {
762                 struct vvp_io *vio = vvp_env_io(env);
763                 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
764
765                 /* if the page is already in dirty cache, we have to commit
766                  * the pages right now; otherwise, it may cause deadlock
767                  * because it holds page lock of a dirty page and request for
768                  * more grants. It's okay for the dirty page to be the first
769                  * one in commit page list, though. */
770                 if (vmpage != NULL && plist->pl_nr > 0) {
771                         unlock_page(vmpage);
772                         put_page(vmpage);
773                         vmpage = NULL;
774                 }
775
776                 /* commit pages and then wait for page lock */
777                 result = vvp_io_write_commit(env, io);
778                 if (result < 0)
779                         GOTO(out, result);
780
781                 if (vmpage == NULL) {
782                         vmpage = grab_cache_page_write_begin(mapping, index,
783                                                              flags);
784                         if (vmpage == NULL)
785                                 GOTO(out, result = -ENOMEM);
786                 }
787         }
788
789         /* page was truncated */
790         if (mapping != vmpage->mapping) {
791                 CDEBUG(D_VFSTRACE, "page: %lu was truncated\n", index);
792                 unlock_page(vmpage);
793                 put_page(vmpage);
794                 vmpage = NULL;
795                 goto again;
796         }
797
798         page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
799         if (IS_ERR(page))
800                 GOTO(out, result = PTR_ERR(page));
801
802         lcc->lcc_page = page;
803         lu_ref_add(&page->cp_reference, "cl_io", io);
804
805         cl_page_assume(env, io, page);
806         if (!PageUptodate(vmpage)) {
807                 /*
808                  * We're completely overwriting an existing page,
809                  * so _don't_ set it up to date until commit_write
810                  */
811                 if (from == 0 && to == PAGE_SIZE) {
812                         CL_PAGE_HEADER(D_PAGE, env, page, "full page write\n");
813                         POISON_PAGE(vmpage, 0x11);
814                 } else {
815                         /* TODO: can be optimized at OSC layer to check if it
816                          * is a lockless IO. In that case, it's not necessary
817                          * to read the data. */
818                         result = ll_prepare_partial_page(env, io, page, file);
819                         if (result) {
820                                 /* vmpage should have been unlocked */
821                                 put_page(vmpage);
822                                 vmpage = NULL;
823
824                                 if (result == -EAGAIN)
825                                         goto again;
826                                 GOTO(out, result);
827                         }
828                 }
829         }
830         EXIT;
831 out:
832         if (result < 0) {
833                 if (vmpage != NULL) {
834                         unlock_page(vmpage);
835                         put_page(vmpage);
836                 }
837                 /* On tiny_write failure, page and io are always null. */
838                 if (!IS_ERR_OR_NULL(page)) {
839                         lu_ref_del(&page->cp_reference, "cl_io", io);
840                         cl_page_put(env, page);
841                 }
842                 if (io)
843                         io->ci_result = result;
844         } else {
845                 *pagep = vmpage;
846                 *fsdata = lcc;
847         }
848         RETURN(result);
849 }
850
851 static int ll_tiny_write_end(struct file *file, struct address_space *mapping,
852                              loff_t pos, unsigned int len, unsigned int copied,
853                              struct page *vmpage)
854 {
855         struct cl_page *clpage = (struct cl_page *) vmpage->private;
856         loff_t kms = pos+copied;
857         loff_t to = kms & (PAGE_SIZE-1) ? kms & (PAGE_SIZE-1) : PAGE_SIZE;
858         __u16 refcheck;
859         struct lu_env *env = cl_env_get(&refcheck);
860         int rc = 0;
861
862         ENTRY;
863
864         if (IS_ERR(env)) {
865                 rc = PTR_ERR(env);
866                 goto out;
867         }
868
869         /* This page is dirty in cache, so it should have a cl_page pointer
870          * set in vmpage->private.
871          */
872         LASSERT(clpage != NULL);
873
874         if (copied == 0)
875                 goto out_env;
876
877         /* Update the underlying size information in the OSC/LOV objects this
878          * page is part of.
879          */
880         cl_page_touch(env, clpage, to);
881
882 out_env:
883         cl_env_put(env, &refcheck);
884
885 out:
886         /* Must return page unlocked. */
887         unlock_page(vmpage);
888
889         RETURN(rc);
890 }
891
892 static int ll_write_end(struct file *file, struct address_space *mapping,
893                         loff_t pos, unsigned len, unsigned copied,
894                         struct page *vmpage, void *fsdata)
895 {
896         struct ll_cl_context *lcc = fsdata;
897         const struct lu_env *env;
898         struct cl_io *io;
899         struct vvp_io *vio;
900         struct cl_page *page;
901         unsigned from = pos & (PAGE_SIZE - 1);
902         bool unplug = false;
903         int result = 0;
904         ENTRY;
905
906         put_page(vmpage);
907
908         CDEBUG(D_VFSTRACE, "pos %llu, len %u, copied %u\n", pos, len, copied);
909
910         if (lcc == NULL) {
911                 result = ll_tiny_write_end(file, mapping, pos, len, copied,
912                                            vmpage);
913                 GOTO(out, result);
914         }
915
916         LASSERT(lcc != NULL);
917         env  = lcc->lcc_env;
918         page = lcc->lcc_page;
919         io   = lcc->lcc_io;
920         vio  = vvp_env_io(env);
921
922         LASSERT(cl_page_is_owned(page, io));
923         if (copied > 0) {
924                 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
925
926                 lcc->lcc_page = NULL; /* page will be queued */
927
928                 /* Add it into write queue */
929                 cl_page_list_add(plist, page, true);
930                 if (plist->pl_nr == 1) /* first page */
931                         vio->u.readwrite.vui_from = from;
932                 else
933                         LASSERT(from == 0);
934                 vio->u.readwrite.vui_to = from + copied;
935
936                 /* To address the deadlock in balance_dirty_pages() where
937                  * this dirty page may be written back in the same thread. */
938                 if (PageDirty(vmpage))
939                         unplug = true;
940
941                 /* We may have one full RPC, commit it soon */
942                 if (plist->pl_nr >= PTLRPC_MAX_BRW_PAGES)
943                         unplug = true;
944
945                 CL_PAGE_DEBUG(D_VFSTRACE, env, page,
946                               "queued page: %d.\n", plist->pl_nr);
947         } else {
948                 cl_page_disown(env, io, page);
949
950                 lcc->lcc_page = NULL;
951                 lu_ref_del(&page->cp_reference, "cl_io", io);
952                 cl_page_put(env, page);
953
954                 /* page list is not contiguous now, commit it now */
955                 unplug = true;
956         }
957         if (unplug || io->u.ci_wr.wr_sync)
958                 result = vvp_io_write_commit(env, io);
959
960         if (result < 0)
961                 io->ci_result = result;
962
963
964 out:
965         RETURN(result >= 0 ? copied : result);
966 }
967
968 #ifdef CONFIG_MIGRATION
969 static int ll_migratepage(struct address_space *mapping,
970                           struct page *newpage, struct page *page,
971                           enum migrate_mode mode)
972 {
973         /* Always fail page migration until we have a proper implementation */
974         return -EIO;
975 }
976 #endif
977
978 const struct address_space_operations ll_aops = {
979 #ifdef HAVE_DIRTY_FOLIO
980         .dirty_folio            = filemap_dirty_folio,
981 #else
982         .set_page_dirty         = __set_page_dirty_nobuffers,
983 #endif
984 #ifdef HAVE_INVALIDATE_FOLIO
985         .invalidate_folio       = ll_invalidate_folio,
986 #else
987         .invalidatepage         = ll_invalidatepage,
988 #endif
989         .readpage               = ll_readpage,
990         .releasepage            = (void *)ll_releasepage,
991         .direct_IO              = ll_direct_IO,
992         .writepage              = ll_writepage,
993         .writepages             = ll_writepages,
994         .write_begin            = ll_write_begin,
995         .write_end              = ll_write_end,
996 #ifdef CONFIG_MIGRATION
997         .migratepage            = ll_migratepage,
998 #endif
999 };