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