Whamcloud - gitweb
780acf0a47949bb115bc98efac1762cf3d7e0926
[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_sync_io *anchor = &sdio->csd_sync;
370         struct cl_2queue *queue = &io->ci_queue;
371         struct cl_object *obj = io->ci_obj;
372         struct cl_page *page;
373         int iot = rw == READ ? CRT_READ : CRT_WRITE;
374         loff_t offset = pv->ldp_file_offset;
375         int io_pages = 0;
376         ssize_t rc = 0;
377         int i = 0;
378
379         ENTRY;
380
381         cl_2queue_init(queue);
382         while (size > 0) {
383                 size_t from = offset & ~PAGE_MASK;
384                 size_t to = min(from + size, PAGE_SIZE);
385
386                 page = cl_page_find(env, obj, offset >> PAGE_SHIFT,
387                                     pv->ldp_pages[i], CPT_TRANSIENT);
388                 if (IS_ERR(page))
389                         GOTO(out, rc = PTR_ERR(page));
390
391                 LASSERT(page->cp_type == CPT_TRANSIENT);
392                 rc = cl_page_own(env, io, page);
393                 if (rc) {
394                         cl_page_put(env, page);
395                         break;
396                 }
397
398                 page->cp_sync_io = anchor;
399                 if (inode && IS_ENCRYPTED(inode)) {
400                         /* In case of Direct IO on encrypted file, we need to
401                          * add a reference to the inode on the cl_page.
402                          * This info is required by llcrypt to proceed
403                          * to encryption/decryption.
404                          * This is safe because we know these pages are private
405                          * to the thread doing the Direct IO.
406                          */
407                         page->cp_inode = inode;
408                 }
409                 /* We keep the refcount from cl_page_find, so we don't need
410                  * another one here
411                  */
412                 cl_page_list_add(&queue->c2_qin, page, false);
413                 /*
414                  * Call page clip for incomplete pages, to set range of bytes
415                  * in the page and to tell transfer formation engine to send
416                  * the page even if it is beyond KMS (ie, don't trim IO to KMS)
417                  */
418                 if (from != 0 || to != PAGE_SIZE)
419                         cl_page_clip(env, page, from, to);
420                 ++io_pages;
421                 i++;
422
423                 offset += to - from;
424                 size -= to - from;
425         }
426         /* on success, we should hit every page in the pvec and have no bytes
427          * left in 'size'
428          */
429         LASSERT(i == pv->ldp_count);
430         LASSERT(size == 0);
431
432         atomic_add(io_pages, &anchor->csi_sync_nr);
433         /*
434          * Avoid out-of-order execution of adding inflight
435          * modifications count and io submit.
436          */
437         smp_mb();
438         rc = cl_io_submit_rw(env, io, iot, queue);
439         if (rc == 0) {
440                 cl_page_list_splice(&queue->c2_qout, &sdio->csd_pages);
441         } else {
442                 atomic_add(-queue->c2_qin.pl_nr,
443                            &anchor->csi_sync_nr);
444                 cl_page_list_for_each(page, &queue->c2_qin)
445                         page->cp_sync_io = NULL;
446         }
447         /* handle partially submitted reqs */
448         if (queue->c2_qin.pl_nr > 0) {
449                 CERROR(DFID " failed to submit %d dio pages: %zd\n",
450                        PFID(lu_object_fid(&obj->co_lu)),
451                        queue->c2_qin.pl_nr, rc);
452                 if (rc == 0)
453                         rc = -EIO;
454         }
455
456 out:
457         cl_2queue_discard(env, io, queue);
458         cl_2queue_disown(env, queue);
459         cl_2queue_fini(env, queue);
460         RETURN(rc);
461 }
462
463 #ifdef KMALLOC_MAX_SIZE
464 #define MAX_MALLOC KMALLOC_MAX_SIZE
465 #else
466 #define MAX_MALLOC (128 * 1024)
467 #endif
468
469 /* This is the maximum size of a single O_DIRECT request, based on the
470  * kmalloc limit.  We need to fit all of the brw_page structs, each one
471  * representing PAGE_SIZE worth of user data, into a single buffer, and
472  * then truncate this to be a full-sized RPC.  For 4kB PAGE_SIZE this is
473  * up to 22MB for 128kB kmalloc and up to 682MB for 4MB kmalloc. */
474 #define MAX_DIO_SIZE ((MAX_MALLOC / sizeof(struct brw_page) * PAGE_SIZE) & \
475                       ~((size_t)DT_MAX_BRW_SIZE - 1))
476
477 static ssize_t
478 ll_direct_IO_impl(struct kiocb *iocb, struct iov_iter *iter, int rw)
479 {
480         struct ll_cl_context *lcc;
481         const struct lu_env *env;
482         struct cl_io *io;
483         struct file *file = iocb->ki_filp;
484         struct inode *inode = file->f_mapping->host;
485         struct cl_dio_aio *ll_dio_aio;
486         struct cl_sub_dio *sdio;
487         size_t count = iov_iter_count(iter);
488         ssize_t tot_bytes = 0, result = 0;
489         loff_t file_offset = iocb->ki_pos;
490         bool sync_submit = false;
491         bool unaligned = false;
492         struct vvp_io *vio;
493         ssize_t rc2;
494
495         ENTRY;
496
497         if (file_offset & ~PAGE_MASK)
498                 unaligned = true;
499
500         if (count & ~PAGE_MASK)
501                 unaligned = true;
502
503         /* Check that all user buffers are aligned as well */
504         if (ll_iov_iter_alignment(iter) & ~PAGE_MASK)
505                 unaligned = true;
506
507         lcc = ll_cl_find(inode);
508         if (lcc == NULL)
509                 RETURN(-EIO);
510
511         env = lcc->lcc_env;
512         LASSERT(!IS_ERR(env));
513         vio = vvp_env_io(env);
514         io = lcc->lcc_io;
515         LASSERT(io != NULL);
516
517         CDEBUG(D_VFSTRACE,
518                "VFS Op:inode="DFID"(%p), size=%zd (max %lu), offset=%lld=%llx, pages %zd (max %lu)%s%s%s%s\n",
519                PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
520                file_offset, file_offset,
521                (count >> PAGE_SHIFT) + !!(count & ~PAGE_MASK),
522                MAX_DIO_SIZE >> PAGE_SHIFT,
523                io->ci_dio_lock ? ", locked" : ", lockless",
524                io->ci_parallel_dio ? ", parallel" : "",
525                unaligned ? ", unaligned" : "",
526                io->ci_hybrid_switched ? ", hybrid" : "");
527
528         /* Check EOF by ourselves */
529         if (rw == READ && file_offset >= i_size_read(inode))
530                 RETURN(0);
531
532         /* unaligned DIO support can be turned off, so is it on? */
533         if (unaligned && !ll_sbi_has_unaligned_dio(ll_i2sbi(inode)))
534                 RETURN(-EINVAL);
535
536         /* the requirement to not return EIOCBQUEUED for pipes (see bottom of
537          * this function) plays havoc with the unaligned I/O lifecycle, so
538          * don't allow unaligned I/O on pipes
539          */
540         if (unaligned && iov_iter_is_pipe(iter))
541                 RETURN(0);
542
543         /* this means we encountered an old server which can't safely support
544          * unaligned DIO, so we have to disable it
545          */
546         if (unaligned && !cl_io_top(io)->ci_allow_unaligned_dio)
547                 RETURN(-EINVAL);
548
549         /* if one part of an I/O is unaligned, just handle all of it that way -
550          * otherwise we create significant complexities with managing the iovec
551          * in different ways, etc, all for very marginal benefits
552          */
553         if (unaligned)
554                 io->ci_unaligned_dio = true;
555         if (io->ci_unaligned_dio)
556                 unaligned = true;
557
558         ll_dio_aio = io->ci_dio_aio;
559         LASSERT(ll_dio_aio);
560         LASSERT(ll_dio_aio->cda_iocb == iocb);
561
562         /* We cannot do parallel submission of sub-I/Os - for AIO or regular
563          * DIO - unless lockless because it causes us to release the lock
564          * early.
565          *
566          * There are also several circumstances in which we must disable
567          * parallel DIO, so we check if it is enabled.
568          *
569          * The check for "is_sync_kiocb" excludes AIO, which does not need to
570          * be disabled in these situations.
571          */
572         if (io->ci_dio_lock || (is_sync_kiocb(iocb) && !io->ci_parallel_dio))
573                 sync_submit = true;
574
575         while (iov_iter_count(iter)) {
576                 struct ll_dio_pages *pvec;
577
578                 count = min_t(size_t, iov_iter_count(iter), MAX_DIO_SIZE);
579                 if (rw == READ) {
580                         if (file_offset >= i_size_read(inode))
581                                 break;
582
583                         if (file_offset + count > i_size_read(inode))
584                                 count = i_size_read(inode) - file_offset;
585                 }
586
587                 /* if we are doing sync_submit, then we free this below,
588                  * otherwise it is freed on the final call to cl_sync_io_note
589                  * (either in this function or from a ptlrpcd daemon)
590                  */
591                 sdio = cl_sub_dio_alloc(ll_dio_aio, iter, rw == WRITE,
592                                         unaligned, sync_submit);
593                 if (!sdio)
594                         GOTO(out, result = -ENOMEM);
595
596                 pvec = &sdio->csd_dio_pages;
597                 pvec->ldp_file_offset = file_offset;
598
599                 if (!unaligned) {
600                         result = ll_get_user_pages(rw, iter, pvec, count);
601                         /* ll_get_user_pages returns bytes in the IO or error*/
602                         count = result;
603                 } else {
604                         /* same calculation used in ll_get_user_pages */
605                         count = min_t(size_t, count, iter_iov(iter)->iov_len);
606                         result = ll_allocate_dio_buffer(pvec, count);
607                         /* allocate_dio_buffer returns number of pages or
608                          * error, so do not set count = result
609                          */
610                 }
611
612                 /* now we have the actual count, so store it in the sdio */
613                 sdio->csd_bytes = count;
614
615                 if (unlikely(result <= 0)) {
616                         cl_sync_io_note(env, &sdio->csd_sync, result);
617                         if (sync_submit) {
618                                 LASSERT(sdio->csd_creator_free);
619                                 cl_sub_dio_free(sdio);
620                         }
621                         GOTO(out, result);
622                 }
623
624                 if (unaligned && rw == WRITE) {
625                         result = ll_dio_user_copy(sdio, iter);
626                         if (unlikely(result <= 0)) {
627                                 cl_sync_io_note(env, &sdio->csd_sync, result);
628                                 if (sync_submit) {
629                                         LASSERT(sdio->csd_creator_free);
630                                         cl_sub_dio_free(sdio);
631                                 }
632                                 GOTO(out, result);
633                         }
634                 }
635
636                 result = ll_direct_rw_pages(env, io, count, rw, inode, sdio);
637                 /* if the i/o was unsuccessful, we zero the number of bytes to
638                  * copy back.  Note that partial I/O completion isn't possible
639                  * here - I/O either completes or fails.  So there's no need to
640                  * handle short I/O here by changing 'count' with the result
641                  * from ll_direct_rw_pages.
642                  *
643                  * This must be done before we release the reference
644                  * immediately below, because releasing the reference allows
645                  * i/o completion (and copyback to userspace, if unaligned) to
646                  * start.
647                  */
648                 if (result != 0)
649                         sdio->csd_bytes = 0;
650                 /* We've submitted pages and can now remove the extra
651                  * reference for that
652                  */
653                 cl_sync_io_note(env, &sdio->csd_sync, result);
654
655                 if (sync_submit) {
656                         rc2 = cl_sync_io_wait(env, &sdio->csd_sync,
657                                              0);
658                         if (result == 0 && rc2)
659                                 result = rc2;
660                         LASSERT(sdio->csd_creator_free);
661                         cl_sub_dio_free(sdio);
662                 }
663                 if (unlikely(result < 0))
664                         GOTO(out, result);
665
666                 iov_iter_advance(iter, count);
667
668                 tot_bytes += count;
669                 file_offset += count;
670                 CDEBUG(D_VFSTRACE,
671                        "result %zd tot_bytes %zd count %zd file_offset %lld\n",
672                        result, tot_bytes, count, file_offset);
673         }
674
675 out:
676         if (rw == WRITE)
677                 vio->u.readwrite.vui_written += tot_bytes;
678         else
679                 vio->u.readwrite.vui_read += tot_bytes;
680
681         /* AIO is not supported on pipes, so we cannot return EIOCBQEUED like
682          * we normally would for both DIO and AIO here
683          */
684         if (result == 0 && !iov_iter_is_pipe(iter))
685                 result = -EIOCBQUEUED;
686
687         RETURN(result);
688 }
689
690 #ifdef HAVE_DIO_ITER
691 static ssize_t ll_direct_IO(
692 #ifndef HAVE_IOV_ITER_RW
693              int rw,
694 #endif
695              struct kiocb *iocb, struct iov_iter *iter
696 #ifndef HAVE_DIRECTIO_2ARGS
697              , loff_t file_offset
698 #endif
699              )
700 {
701         int nrw;
702
703 #ifndef HAVE_IOV_ITER_RW
704         nrw = rw;
705 #else
706         nrw = iov_iter_rw(iter);
707 #endif
708
709         return ll_direct_IO_impl(iocb, iter, nrw);
710 }
711
712 #else /* !defined(HAVE_DIO_ITER) */
713
714 static ssize_t
715 ll_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
716              loff_t file_offset, unsigned long nr_segs)
717 {
718         struct iov_iter iter;
719
720         iov_iter_init(&iter, iov, nr_segs, iov_length(iov, nr_segs), 0);
721         return ll_direct_IO_impl(iocb, &iter, rw);
722 }
723
724 #endif /* !defined(HAVE_DIO_ITER) */
725
726 /**
727  * Prepare partially written-to page for a write.
728  * @pg is owned when passed in and disowned when it returns non-zero result to
729  * the caller.
730  */
731 static int ll_prepare_partial_page(const struct lu_env *env, struct cl_io *io,
732                                    struct cl_page *pg, struct file *file)
733 {
734         struct cl_attr *attr   = vvp_env_thread_attr(env);
735         struct cl_object *obj  = io->ci_obj;
736         loff_t offset = cl_page_index(pg) << PAGE_SHIFT;
737         int result;
738         ENTRY;
739
740         cl_object_attr_lock(obj);
741         result = cl_object_attr_get(env, obj, attr);
742         cl_object_attr_unlock(obj);
743         if (result) {
744                 cl_page_disown(env, io, pg);
745                 GOTO(out, result);
746         }
747
748         /*
749          * If are writing to a new page, no need to read old data.
750          * The extent locking will have updated the KMS, and for our
751          * purposes here we can treat it like i_size.
752          */
753         if (attr->cat_kms <= offset) {
754                 char *kaddr = kmap_atomic(pg->cp_vmpage);
755
756                 memset(kaddr, 0, PAGE_SIZE);
757                 kunmap_atomic(kaddr);
758                 GOTO(out, result = 0);
759         }
760
761         if (pg->cp_defer_uptodate) {
762                 pg->cp_ra_used = 1;
763                 GOTO(out, result = 0);
764         }
765
766         result = ll_io_read_page(env, io, pg, file);
767         if (result)
768                 GOTO(out, result);
769
770         /* ll_io_read_page() disowns the page */
771         result = cl_page_own(env, io, pg);
772         if (!result) {
773                 if (!PageUptodate(cl_page_vmpage(pg))) {
774                         cl_page_disown(env, io, pg);
775                         result = -EIO;
776                 }
777         } else if (result == -ENOENT) {
778                 /* page was truncated */
779                 result = -EAGAIN;
780         }
781         EXIT;
782
783 out:
784         return result;
785 }
786
787 static int ll_tiny_write_begin(struct page *vmpage, struct address_space *mapping)
788 {
789         /* Page must be present, up to date, dirty, and not in writeback. */
790         if (!vmpage || !PageUptodate(vmpage) || !PageDirty(vmpage) ||
791             PageWriteback(vmpage) || vmpage->mapping != mapping)
792                 return -ENODATA;
793
794         return 0;
795 }
796
797 static int ll_write_begin(struct file *file, struct address_space *mapping,
798                           loff_t pos, unsigned int len,
799 #ifdef HAVE_GRAB_CACHE_PAGE_WRITE_BEGIN_WITH_FLAGS
800                           unsigned int flags,
801 #endif
802                           struct page **pagep, void **fsdata)
803 {
804         struct ll_cl_context *lcc = NULL;
805         const struct lu_env  *env = NULL;
806         struct vvp_io *vio;
807         struct cl_io   *io = NULL;
808         struct cl_page *page = NULL;
809         struct inode *inode = file_inode(file);
810         struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
811         pgoff_t index = pos >> PAGE_SHIFT;
812         struct page *vmpage = NULL;
813         unsigned from = pos & (PAGE_SIZE - 1);
814         unsigned to = from + len;
815         int result = 0;
816         int iocb_flags;
817         ENTRY;
818
819         CDEBUG(D_VFSTRACE, "Writing %lu of %d to %d bytes\n", index, from, len);
820
821         lcc = ll_cl_find(inode);
822         if (lcc == NULL) {
823                 /* do not allocate a page, only find & lock */
824                 vmpage = find_lock_page(mapping, index);
825                 result = ll_tiny_write_begin(vmpage, mapping);
826                 GOTO(out, result);
827         }
828
829         env = lcc->lcc_env;
830         io  = lcc->lcc_io;
831         vio = vvp_env_io(env);
832
833         iocb_flags = iocb_ki_flags_get(file, vio->vui_iocb);
834         if (iocb_ki_flags_check(iocb_flags, DIRECT)) {
835                 /* direct IO failed because it couldn't clean up cached pages,
836                  * this causes a problem for mirror write because the cached
837                  * page may belong to another mirror, which will result in
838                  * problem submitting the I/O. */
839                 if (io->ci_designated_mirror > 0)
840                         GOTO(out, result = -EBUSY);
841
842                 /**
843                  * Direct write can fall back to buffered read, but DIO is done
844                  * with lockless i/o, and buffered requires LDLM locking, so
845                  * in this case we must restart without lockless.
846                  */
847                 if (!io->ci_dio_lock) {
848                         io->ci_dio_lock = 1;
849                         io->ci_need_restart = 1;
850                         GOTO(out, result = -ENOLCK);
851                 }
852         }
853 again:
854         /* To avoid deadlock, try to lock page first. */
855         vmpage = grab_cache_page_nowait(mapping, index);
856
857         if (unlikely(vmpage == NULL ||
858                      PageDirty(vmpage) || PageWriteback(vmpage))) {
859                 struct vvp_io *vio = vvp_env_io(env);
860                 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
861
862                 /* if the page is already in dirty cache, we have to commit
863                  * the pages right now; otherwise, it may cause deadlock
864                  * because it holds page lock of a dirty page and request for
865                  * more grants. It's okay for the dirty page to be the first
866                  * one in commit page list, though. */
867                 if (vmpage != NULL && plist->pl_nr > 0) {
868                         unlock_page(vmpage);
869                         put_page(vmpage);
870                         vmpage = NULL;
871                 }
872
873                 /* commit pages and then wait for page lock */
874                 result = vvp_io_write_commit(env, io);
875                 if (result < 0)
876                         GOTO(out, result);
877
878                 if (vmpage == NULL) {
879                         vmpage = grab_cache_page_write_begin(mapping, index
880 #ifdef HAVE_GRAB_CACHE_PAGE_WRITE_BEGIN_WITH_FLAGS
881                                                              , flags
882 #endif
883                                                              );
884                         if (vmpage == NULL)
885                                 GOTO(out, result = -ENOMEM);
886                 }
887         }
888
889         /* page was truncated */
890         if (mapping != vmpage->mapping) {
891                 CDEBUG(D_VFSTRACE, "page: %lu was truncated\n", index);
892                 unlock_page(vmpage);
893                 put_page(vmpage);
894                 vmpage = NULL;
895                 goto again;
896         }
897
898         page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
899         if (IS_ERR(page))
900                 GOTO(out, result = PTR_ERR(page));
901
902         lcc->lcc_page = page;
903         lu_ref_add(&page->cp_reference, "cl_io", io);
904
905         cl_page_assume(env, io, page);
906         if (!PageUptodate(vmpage)) {
907                 /*
908                  * We're completely overwriting an existing page,
909                  * so _don't_ set it up to date until commit_write
910                  */
911                 if (from == 0 && to == PAGE_SIZE) {
912                         CL_PAGE_HEADER(D_PAGE, env, page, "full page write\n");
913                         POISON_PAGE(vmpage, 0x11);
914                 } else {
915                         /* TODO: can be optimized at OSC layer to check if it
916                          * is a lockless IO. In that case, it's not necessary
917                          * to read the data. */
918                         result = ll_prepare_partial_page(env, io, page, file);
919                         if (result) {
920                                 /* vmpage should have been unlocked */
921                                 put_page(vmpage);
922                                 vmpage = NULL;
923
924                                 if (result == -EAGAIN)
925                                         goto again;
926                                 GOTO(out, result);
927                         }
928                 }
929         }
930         EXIT;
931 out:
932         if (result < 0) {
933                 if (vmpage != NULL) {
934                         unlock_page(vmpage);
935                         put_page(vmpage);
936                 }
937                 /* On tiny_write failure, page and io are always null. */
938                 if (!IS_ERR_OR_NULL(page)) {
939                         lu_ref_del(&page->cp_reference, "cl_io", io);
940                         cl_page_put(env, page);
941                 }
942                 if (io)
943                         io->ci_result = result;
944         } else {
945                 *pagep = vmpage;
946                 *fsdata = lcc;
947         }
948         RETURN(result);
949 }
950
951 static int ll_tiny_write_end(struct file *file, struct address_space *mapping,
952                              loff_t pos, unsigned int len, unsigned int copied,
953                              struct page *vmpage)
954 {
955         struct cl_page *clpage = (struct cl_page *) vmpage->private;
956         loff_t kms = pos+copied;
957         loff_t to = kms & (PAGE_SIZE-1) ? kms & (PAGE_SIZE-1) : PAGE_SIZE;
958         __u16 refcheck;
959         struct lu_env *env = cl_env_get(&refcheck);
960         int rc = 0;
961
962         ENTRY;
963
964         if (IS_ERR(env)) {
965                 rc = PTR_ERR(env);
966                 goto out;
967         }
968
969         /* This page is dirty in cache, so it should have a cl_page pointer
970          * set in vmpage->private.
971          */
972         LASSERT(clpage != NULL);
973
974         if (copied == 0)
975                 goto out_env;
976
977         /* Update the underlying size information in the OSC/LOV objects this
978          * page is part of.
979          */
980         cl_page_touch(env, clpage, to);
981
982 out_env:
983         cl_env_put(env, &refcheck);
984
985 out:
986         /* Must return page unlocked. */
987         unlock_page(vmpage);
988
989         RETURN(rc);
990 }
991
992 static int ll_write_end(struct file *file, struct address_space *mapping,
993                         loff_t pos, unsigned len, unsigned copied,
994                         struct page *vmpage, void *fsdata)
995 {
996         struct ll_cl_context *lcc = fsdata;
997         const struct lu_env *env;
998         struct cl_io *io;
999         struct vvp_io *vio;
1000         struct cl_page *page;
1001         unsigned from = pos & (PAGE_SIZE - 1);
1002         bool unplug = false;
1003         int result = 0;
1004         ENTRY;
1005
1006         put_page(vmpage);
1007
1008         CDEBUG(D_VFSTRACE, "pos %llu, len %u, copied %u\n", pos, len, copied);
1009
1010         if (lcc == NULL) {
1011                 result = ll_tiny_write_end(file, mapping, pos, len, copied,
1012                                            vmpage);
1013                 GOTO(out, result);
1014         }
1015
1016         LASSERT(lcc != NULL);
1017         env  = lcc->lcc_env;
1018         page = lcc->lcc_page;
1019         io   = lcc->lcc_io;
1020         vio  = vvp_env_io(env);
1021
1022         LASSERT(cl_page_is_owned(page, io));
1023         if (copied > 0) {
1024                 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
1025
1026                 lcc->lcc_page = NULL; /* page will be queued */
1027
1028                 /* Add it into write queue */
1029                 cl_page_list_add(plist, page, true);
1030                 if (plist->pl_nr == 1) /* first page */
1031                         vio->u.readwrite.vui_from = from;
1032                 else
1033                         LASSERT(from == 0);
1034                 vio->u.readwrite.vui_to = from + copied;
1035
1036                 /* To address the deadlock in balance_dirty_pages() where
1037                  * this dirty page may be written back in the same thread. */
1038                 if (PageDirty(vmpage))
1039                         unplug = true;
1040
1041                 /* We may have one full RPC, commit it soon */
1042                 if (plist->pl_nr >= PTLRPC_MAX_BRW_PAGES)
1043                         unplug = true;
1044
1045                 CL_PAGE_DEBUG(D_VFSTRACE, env, page,
1046                               "queued page: %d.\n", plist->pl_nr);
1047         } else {
1048                 cl_page_disown(env, io, page);
1049
1050                 lcc->lcc_page = NULL;
1051                 lu_ref_del(&page->cp_reference, "cl_io", io);
1052                 cl_page_put(env, page);
1053
1054                 /* page list is not contiguous now, commit it now */
1055                 unplug = true;
1056         }
1057         if (unplug || io->u.ci_wr.wr_sync)
1058                 result = vvp_io_write_commit(env, io);
1059
1060         if (result < 0)
1061                 io->ci_result = result;
1062
1063
1064 out:
1065         RETURN(result >= 0 ? copied : result);
1066 }
1067
1068 #ifdef CONFIG_MIGRATION
1069 static int ll_migrate_folio(struct address_space *mapping,
1070                             struct folio_migr *newpage, struct folio_migr *page,
1071                             enum migrate_mode mode)
1072 {
1073         /* Always fail page migration until we have a proper implementation */
1074         return -EIO;
1075 }
1076 #endif
1077
1078 const struct address_space_operations ll_aops = {
1079 #ifdef HAVE_DIRTY_FOLIO
1080         .dirty_folio            = filemap_dirty_folio,
1081 #else
1082         .set_page_dirty         = __set_page_dirty_nobuffers,
1083 #endif
1084 #ifdef HAVE_INVALIDATE_FOLIO
1085         .invalidate_folio       = ll_invalidate_folio,
1086 #else
1087         .invalidatepage         = ll_invalidatepage,
1088 #endif
1089 #ifdef HAVE_AOPS_READ_FOLIO
1090         .read_folio             = ll_read_folio,
1091 #else
1092         .readpage               = ll_readpage,
1093 #endif
1094 #ifdef HAVE_AOPS_RELEASE_FOLIO
1095         .release_folio          = ll_release_folio,
1096 #else
1097         .releasepage            = (void *)ll_releasepage,
1098 #endif
1099         .direct_IO              = ll_direct_IO,
1100         .writepage              = ll_writepage,
1101         .writepages             = ll_writepages,
1102         .write_begin            = ll_write_begin,
1103         .write_end              = ll_write_end,
1104 #ifdef CONFIG_MIGRATION
1105         .migrate_folio          = ll_migrate_folio,
1106 #endif
1107 };