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