Whamcloud - gitweb
LU-13307 nodemap: have nodemap_add_member support large NIDs
[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         /* 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                 vmpage = grab_cache_page_nowait(mapping, index);
824                 result = ll_tiny_write_begin(vmpage, mapping);
825                 GOTO(out, result);
826         }
827
828         env = lcc->lcc_env;
829         io  = lcc->lcc_io;
830         vio = vvp_env_io(env);
831
832         iocb_flags = iocb_ki_flags_get(file, vio->vui_iocb);
833         if (iocb_ki_flags_check(iocb_flags, DIRECT)) {
834                 /* direct IO failed because it couldn't clean up cached pages,
835                  * this causes a problem for mirror write because the cached
836                  * page may belong to another mirror, which will result in
837                  * problem submitting the I/O. */
838                 if (io->ci_designated_mirror > 0)
839                         GOTO(out, result = -EBUSY);
840
841                 /**
842                  * Direct write can fall back to buffered read, but DIO is done
843                  * with lockless i/o, and buffered requires LDLM locking, so
844                  * in this case we must restart without lockless.
845                  */
846                 if (!io->ci_dio_lock) {
847                         io->ci_dio_lock = 1;
848                         io->ci_need_restart = 1;
849                         GOTO(out, result = -ENOLCK);
850                 }
851         }
852 again:
853         /* To avoid deadlock, try to lock page first. */
854         vmpage = grab_cache_page_nowait(mapping, index);
855
856         if (unlikely(vmpage == NULL ||
857                      PageDirty(vmpage) || PageWriteback(vmpage))) {
858                 struct vvp_io *vio = vvp_env_io(env);
859                 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
860
861                 /* if the page is already in dirty cache, we have to commit
862                  * the pages right now; otherwise, it may cause deadlock
863                  * because it holds page lock of a dirty page and request for
864                  * more grants. It's okay for the dirty page to be the first
865                  * one in commit page list, though. */
866                 if (vmpage != NULL && plist->pl_nr > 0) {
867                         unlock_page(vmpage);
868                         put_page(vmpage);
869                         vmpage = NULL;
870                 }
871
872                 /* commit pages and then wait for page lock */
873                 result = vvp_io_write_commit(env, io);
874                 if (result < 0)
875                         GOTO(out, result);
876
877                 if (vmpage == NULL) {
878                         vmpage = grab_cache_page_write_begin(mapping, index
879 #ifdef HAVE_GRAB_CACHE_PAGE_WRITE_BEGIN_WITH_FLAGS
880                                                              , flags
881 #endif
882                                                              );
883                         if (vmpage == NULL)
884                                 GOTO(out, result = -ENOMEM);
885                 }
886         }
887
888         /* page was truncated */
889         if (mapping != vmpage->mapping) {
890                 CDEBUG(D_VFSTRACE, "page: %lu was truncated\n", index);
891                 unlock_page(vmpage);
892                 put_page(vmpage);
893                 vmpage = NULL;
894                 goto again;
895         }
896
897         page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
898         if (IS_ERR(page))
899                 GOTO(out, result = PTR_ERR(page));
900
901         lcc->lcc_page = page;
902         lu_ref_add(&page->cp_reference, "cl_io", io);
903
904         cl_page_assume(env, io, page);
905         if (!PageUptodate(vmpage)) {
906                 /*
907                  * We're completely overwriting an existing page,
908                  * so _don't_ set it up to date until commit_write
909                  */
910                 if (from == 0 && to == PAGE_SIZE) {
911                         CL_PAGE_HEADER(D_PAGE, env, page, "full page write\n");
912                         POISON_PAGE(vmpage, 0x11);
913                 } else {
914                         /* TODO: can be optimized at OSC layer to check if it
915                          * is a lockless IO. In that case, it's not necessary
916                          * to read the data. */
917                         result = ll_prepare_partial_page(env, io, page, file);
918                         if (result) {
919                                 /* vmpage should have been unlocked */
920                                 put_page(vmpage);
921                                 vmpage = NULL;
922
923                                 if (result == -EAGAIN)
924                                         goto again;
925                                 GOTO(out, result);
926                         }
927                 }
928         }
929         EXIT;
930 out:
931         if (result < 0) {
932                 if (vmpage != NULL) {
933                         unlock_page(vmpage);
934                         put_page(vmpage);
935                 }
936                 /* On tiny_write failure, page and io are always null. */
937                 if (!IS_ERR_OR_NULL(page)) {
938                         lu_ref_del(&page->cp_reference, "cl_io", io);
939                         cl_page_put(env, page);
940                 }
941                 if (io)
942                         io->ci_result = result;
943         } else {
944                 *pagep = vmpage;
945                 *fsdata = lcc;
946         }
947         RETURN(result);
948 }
949
950 static int ll_tiny_write_end(struct file *file, struct address_space *mapping,
951                              loff_t pos, unsigned int len, unsigned int copied,
952                              struct page *vmpage)
953 {
954         struct cl_page *clpage = (struct cl_page *) vmpage->private;
955         loff_t kms = pos+copied;
956         loff_t to = kms & (PAGE_SIZE-1) ? kms & (PAGE_SIZE-1) : PAGE_SIZE;
957         __u16 refcheck;
958         struct lu_env *env = cl_env_get(&refcheck);
959         int rc = 0;
960
961         ENTRY;
962
963         if (IS_ERR(env)) {
964                 rc = PTR_ERR(env);
965                 goto out;
966         }
967
968         /* This page is dirty in cache, so it should have a cl_page pointer
969          * set in vmpage->private.
970          */
971         LASSERT(clpage != NULL);
972
973         if (copied == 0)
974                 goto out_env;
975
976         /* Update the underlying size information in the OSC/LOV objects this
977          * page is part of.
978          */
979         cl_page_touch(env, clpage, to);
980
981 out_env:
982         cl_env_put(env, &refcheck);
983
984 out:
985         /* Must return page unlocked. */
986         unlock_page(vmpage);
987
988         RETURN(rc);
989 }
990
991 static int ll_write_end(struct file *file, struct address_space *mapping,
992                         loff_t pos, unsigned len, unsigned copied,
993                         struct page *vmpage, void *fsdata)
994 {
995         struct ll_cl_context *lcc = fsdata;
996         const struct lu_env *env;
997         struct cl_io *io;
998         struct vvp_io *vio;
999         struct cl_page *page;
1000         unsigned from = pos & (PAGE_SIZE - 1);
1001         bool unplug = false;
1002         int result = 0;
1003         ENTRY;
1004
1005         put_page(vmpage);
1006
1007         CDEBUG(D_VFSTRACE, "pos %llu, len %u, copied %u\n", pos, len, copied);
1008
1009         if (lcc == NULL) {
1010                 result = ll_tiny_write_end(file, mapping, pos, len, copied,
1011                                            vmpage);
1012                 GOTO(out, result);
1013         }
1014
1015         LASSERT(lcc != NULL);
1016         env  = lcc->lcc_env;
1017         page = lcc->lcc_page;
1018         io   = lcc->lcc_io;
1019         vio  = vvp_env_io(env);
1020
1021         LASSERT(cl_page_is_owned(page, io));
1022         if (copied > 0) {
1023                 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
1024
1025                 lcc->lcc_page = NULL; /* page will be queued */
1026
1027                 /* Add it into write queue */
1028                 cl_page_list_add(plist, page, true);
1029                 if (plist->pl_nr == 1) /* first page */
1030                         vio->u.readwrite.vui_from = from;
1031                 else
1032                         LASSERT(from == 0);
1033                 vio->u.readwrite.vui_to = from + copied;
1034
1035                 /* To address the deadlock in balance_dirty_pages() where
1036                  * this dirty page may be written back in the same thread. */
1037                 if (PageDirty(vmpage))
1038                         unplug = true;
1039
1040                 /* We may have one full RPC, commit it soon */
1041                 if (plist->pl_nr >= PTLRPC_MAX_BRW_PAGES)
1042                         unplug = true;
1043
1044                 CL_PAGE_DEBUG(D_VFSTRACE, env, page,
1045                               "queued page: %d.\n", plist->pl_nr);
1046         } else {
1047                 cl_page_disown(env, io, page);
1048
1049                 lcc->lcc_page = NULL;
1050                 lu_ref_del(&page->cp_reference, "cl_io", io);
1051                 cl_page_put(env, page);
1052
1053                 /* page list is not contiguous now, commit it now */
1054                 unplug = true;
1055         }
1056         if (unplug || io->u.ci_wr.wr_sync)
1057                 result = vvp_io_write_commit(env, io);
1058
1059         if (result < 0)
1060                 io->ci_result = result;
1061
1062
1063 out:
1064         RETURN(result >= 0 ? copied : result);
1065 }
1066
1067 #ifdef CONFIG_MIGRATION
1068 static int ll_migrate_folio(struct address_space *mapping,
1069                             struct folio_migr *newpage, struct folio_migr *page,
1070                             enum migrate_mode mode)
1071 {
1072         /* Always fail page migration until we have a proper implementation */
1073         return -EIO;
1074 }
1075 #endif
1076
1077 const struct address_space_operations ll_aops = {
1078 #ifdef HAVE_DIRTY_FOLIO
1079         .dirty_folio            = filemap_dirty_folio,
1080 #else
1081         .set_page_dirty         = __set_page_dirty_nobuffers,
1082 #endif
1083 #ifdef HAVE_INVALIDATE_FOLIO
1084         .invalidate_folio       = ll_invalidate_folio,
1085 #else
1086         .invalidatepage         = ll_invalidatepage,
1087 #endif
1088 #ifdef HAVE_AOPS_READ_FOLIO
1089         .read_folio             = ll_read_folio,
1090 #else
1091         .readpage               = ll_readpage,
1092 #endif
1093 #ifdef HAVE_AOPS_RELEASE_FOLIO
1094         .release_folio          = ll_release_folio,
1095 #else
1096         .releasepage            = (void *)ll_releasepage,
1097 #endif
1098         .direct_IO              = ll_direct_IO,
1099         .writepage              = ll_writepage,
1100         .writepages             = ll_writepages,
1101         .write_begin            = ll_write_begin,
1102         .write_end              = ll_write_end,
1103 #ifdef CONFIG_MIGRATION
1104         .migrate_folio          = ll_migrate_folio,
1105 #endif
1106 };