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