Whamcloud - gitweb
LU-14306 sec: get rid of bad rss-counter state messages
[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  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/lustre/llite/rw26.c
33  *
34  * Lustre Lite I/O page cache routines for the 2.5/2.6 kernel version
35  */
36
37 #include <linux/buffer_head.h>
38 #include <linux/errno.h>
39 #include <linux/fs.h>
40 #include <linux/kernel.h>
41 #include <linux/mm.h>
42 #include <linux/mpage.h>
43 #include <linux/pagemap.h>
44 #include <linux/string.h>
45 #include <linux/unistd.h>
46 #include <linux/writeback.h>
47 #include <linux/migrate.h>
48
49 #define DEBUG_SUBSYSTEM S_LLITE
50
51 #include "llite_internal.h"
52 #include <lustre_compat.h>
53
54 /**
55  * Implements Linux VM address_space::invalidatepage() method. This method is
56  * called when the page is truncate 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, offset] bytes of the page remain valid (this is for a case of not-page
61  * aligned truncate). Lustre leaves partially truncated page in the cache,
62  * relying on struct inode::i_size to limit further accesses.
63  */
64 static void ll_invalidatepage(struct page *vmpage,
65 #ifdef HAVE_INVALIDATE_RANGE
66                                 unsigned int offset, unsigned int length
67 #else
68                                 unsigned long offset
69 #endif
70                              )
71 {
72         struct inode     *inode;
73         struct lu_env    *env;
74         struct cl_page   *page;
75         struct cl_object *obj;
76
77         LASSERT(PageLocked(vmpage));
78         LASSERT(!PageWriteback(vmpage));
79
80         /*
81          * It is safe to not check anything in invalidatepage/releasepage
82          * below because they are run with page locked and all our io is
83          * happening with locked page too
84          */
85 #ifdef HAVE_INVALIDATE_RANGE
86         if (offset == 0 && length == PAGE_SIZE) {
87 #else
88         if (offset == 0) {
89 #endif
90                 /* See the comment in ll_releasepage() */
91                 env = cl_env_percpu_get();
92                 LASSERT(!IS_ERR(env));
93
94                 inode = vmpage->mapping->host;
95                 obj = ll_i2info(inode)->lli_clob;
96                 if (obj != NULL) {
97                         page = cl_vmpage_page(vmpage, obj);
98                         if (page != NULL) {
99                                 cl_page_delete(env, page);
100                                 cl_page_put(env, page);
101                         }
102                 } else
103                         LASSERT(vmpage->private == 0);
104
105                 cl_env_percpu_put(env);
106         }
107 }
108
109 #ifdef HAVE_RELEASEPAGE_WITH_INT
110 #define RELEASEPAGE_ARG_TYPE int
111 #else
112 #define RELEASEPAGE_ARG_TYPE gfp_t
113 #endif
114 static int ll_releasepage(struct page *vmpage, RELEASEPAGE_ARG_TYPE gfp_mask)
115 {
116         struct lu_env           *env;
117         struct cl_object        *obj;
118         struct cl_page          *page;
119         struct address_space    *mapping;
120         int result = 0;
121
122         LASSERT(PageLocked(vmpage));
123         if (PageWriteback(vmpage) || PageDirty(vmpage))
124                 return 0;
125
126         mapping = vmpage->mapping;
127         if (mapping == NULL)
128                 return 1;
129
130         obj = ll_i2info(mapping->host)->lli_clob;
131         if (obj == NULL)
132                 return 1;
133
134         page = cl_vmpage_page(vmpage, obj);
135         if (page == NULL)
136                 return 1;
137
138         env = cl_env_percpu_get();
139         LASSERT(!IS_ERR(env));
140
141         if (!cl_page_in_use(page)) {
142                 result = 1;
143                 cl_page_delete(env, page);
144         }
145
146         /* To use percpu env array, the call path can not be rescheduled;
147          * otherwise percpu array will be messed if ll_releaspage() called
148          * again on the same CPU.
149          *
150          * If this page holds the last refc of cl_object, the following
151          * call path may cause reschedule:
152          *   cl_page_put -> cl_page_free -> cl_object_put ->
153          *     lu_object_put -> lu_object_free -> lov_delete_raid0.
154          *
155          * However, the kernel can't get rid of this inode until all pages have
156          * been cleaned up. Now that we hold page lock here, it's pretty safe
157          * that we won't get into object delete path.
158          */
159         LASSERT(cl_object_refc(obj) > 1);
160         cl_page_put(env, page);
161
162         cl_env_percpu_put(env);
163         return result;
164 }
165
166 #if defined(HAVE_DIRECTIO_ITER) || defined(HAVE_IOV_ITER_RW) || \
167         defined(HAVE_DIRECTIO_2ARGS)
168 #define HAVE_DIO_ITER 1
169 #endif
170
171 /*
172  * ll_free_user_pages - tear down page struct array
173  * @pages: array of page struct pointers underlying target buffer
174  */
175 static void ll_free_user_pages(struct page **pages, int npages)
176 {
177         int i;
178
179         for (i = 0; i < npages; i++) {
180                 if (!pages[i])
181                         break;
182                 put_page(pages[i]);
183         }
184
185 #if defined(HAVE_DIO_ITER)
186         kvfree(pages);
187 #else
188         OBD_FREE_PTR_ARRAY_LARGE(pages, npages);
189 #endif
190 }
191
192 static ssize_t ll_get_user_pages(int rw, struct iov_iter *iter,
193                                 struct page ***pages, ssize_t *npages,
194                                 size_t maxsize)
195 {
196 #if defined(HAVE_DIO_ITER)
197         size_t start;
198         size_t result;
199
200         /*
201          * iov_iter_get_pages_alloc() is introduced in 3.16 similar
202          * to HAVE_DIO_ITER.
203          */
204         result = iov_iter_get_pages_alloc(iter, pages, maxsize, &start);
205         if (result > 0)
206                 *npages = DIV_ROUND_UP(result + start, PAGE_SIZE);
207
208         return result;
209 #else
210         unsigned long addr;
211         size_t page_count;
212         size_t size;
213         long result;
214
215         if (!maxsize)
216                 return 0;
217
218         if (!iter->nr_segs)
219                 return 0;
220
221         addr = (unsigned long)iter->iov->iov_base + iter->iov_offset;
222         if (addr & ~PAGE_MASK)
223                 return -EINVAL;
224
225         size = min_t(size_t, maxsize, iter->iov->iov_len);
226         page_count = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
227         OBD_ALLOC_PTR_ARRAY_LARGE(*pages, page_count);
228         if (*pages == NULL)
229                 return -ENOMEM;
230
231         down_read(&current->mm->mmap_sem);
232         result = get_user_pages(current, current->mm, addr, page_count,
233                                 rw == READ, 0, *pages, NULL);
234         up_read(&current->mm->mmap_sem);
235
236         if (unlikely(result != page_count)) {
237                 ll_free_user_pages(*pages, page_count);
238                 *pages = NULL;
239
240                 if (result >= 0)
241                         return -EFAULT;
242
243                 return result;
244         }
245         *npages = page_count;
246
247         return size;
248 #endif
249 }
250
251 /* iov_iter_alignment() is introduced in 3.16 similar to HAVE_DIO_ITER */
252 #if defined(HAVE_DIO_ITER)
253 static unsigned long iov_iter_alignment_vfs(const struct iov_iter *i)
254 {
255         return iov_iter_alignment(i);
256 }
257 #else /* copied from alignment_iovec() */
258 static unsigned long iov_iter_alignment_vfs(const struct iov_iter *i)
259 {
260         const struct iovec *iov = i->iov;
261         unsigned long res;
262         size_t size = i->count;
263         size_t n;
264
265         if (!size)
266                 return 0;
267
268         res = (unsigned long)iov->iov_base + i->iov_offset;
269         n = iov->iov_len - i->iov_offset;
270         if (n >= size)
271                 return res | size;
272
273         size -= n;
274         res |= n;
275         while (size > (++iov)->iov_len) {
276                 res |= (unsigned long)iov->iov_base | iov->iov_len;
277                 size -= iov->iov_len;
278         }
279         res |= (unsigned long)iov->iov_base | size;
280
281         return res;
282 }
283 #endif
284
285 /*
286  * Lustre could relax a bit for alignment, io count is not
287  * necessary page alignment.
288  */
289 static unsigned long ll_iov_iter_alignment(struct iov_iter *i)
290 {
291         size_t orig_size = i->count;
292         size_t count = orig_size & ~PAGE_MASK;
293         unsigned long res;
294
295         if (!count)
296                 return iov_iter_alignment_vfs(i);
297
298         if (orig_size > PAGE_SIZE) {
299                 iov_iter_truncate(i, orig_size - count);
300                 res = iov_iter_alignment_vfs(i);
301                 iov_iter_reexpand(i, orig_size);
302
303                 return res;
304         }
305
306         res = iov_iter_alignment_vfs(i);
307         /* start address is page aligned */
308         if ((res & ~PAGE_MASK) == orig_size)
309                 return PAGE_SIZE;
310
311         return res;
312 }
313
314 /** direct IO pages */
315 struct ll_dio_pages {
316         struct cl_dio_aio       *ldp_aio;
317         /*
318          * page array to be written. we don't support
319          * partial pages except the last one.
320          */
321         struct page             **ldp_pages;
322         /** # of pages in the array. */
323         size_t                  ldp_count;
324         /* the file offset of the first page. */
325         loff_t                  ldp_file_offset;
326 };
327
328 static int
329 ll_direct_rw_pages(const struct lu_env *env, struct cl_io *io, size_t size,
330                    int rw, struct inode *inode, struct ll_dio_pages *pv)
331 {
332         struct cl_page    *page;
333         struct cl_2queue  *queue = &io->ci_queue;
334         struct cl_object  *obj = io->ci_obj;
335         struct cl_sync_io *anchor = &pv->ldp_aio->cda_sync;
336         loff_t offset   = pv->ldp_file_offset;
337         int io_pages    = 0;
338         size_t page_size = cl_page_size(obj);
339         int i;
340         ssize_t rc = 0;
341
342         ENTRY;
343
344         cl_2queue_init(queue);
345         for (i = 0; i < pv->ldp_count; i++) {
346                 LASSERT(!(offset & (PAGE_SIZE - 1)));
347                 page = cl_page_find(env, obj, cl_index(obj, offset),
348                                     pv->ldp_pages[i], CPT_TRANSIENT);
349                 if (IS_ERR(page)) {
350                         rc = PTR_ERR(page);
351                         break;
352                 }
353                 LASSERT(page->cp_type == CPT_TRANSIENT);
354                 rc = cl_page_own(env, io, page);
355                 if (rc) {
356                         cl_page_put(env, page);
357                         break;
358                 }
359
360                 page->cp_sync_io = anchor;
361                 if (inode && IS_ENCRYPTED(inode)) {
362                         /* In case of Direct IO on encrypted file, we need to
363                          * add a reference to the inode on the cl_page.
364                          * This info is required by llcrypt to proceed
365                          * to encryption/decryption.
366                          * This is safe because we know these pages are private
367                          * to the thread doing the Direct IO.
368                          */
369                         page->cp_inode = inode;
370                 }
371                 cl_2queue_add(queue, page);
372                 /*
373                  * Set page clip to tell transfer formation engine
374                  * that page has to be sent even if it is beyond KMS.
375                  */
376                 cl_page_clip(env, page, 0, min(size, page_size));
377                 ++io_pages;
378
379                 /* drop the reference count for cl_page_find */
380                 cl_page_put(env, page);
381                 offset += page_size;
382                 size -= page_size;
383         }
384         if (rc == 0 && io_pages > 0) {
385                 int iot = rw == READ ? CRT_READ : CRT_WRITE;
386
387                 atomic_add(io_pages, &anchor->csi_sync_nr);
388                 /*
389                  * Avoid out-of-order execution of adding inflight
390                  * modifications count and io submit.
391                  */
392                 smp_mb();
393                 rc = cl_io_submit_rw(env, io, iot, queue);
394                 if (rc == 0) {
395                         cl_page_list_splice(&queue->c2_qout,
396                                         &pv->ldp_aio->cda_pages);
397                 } else {
398                         atomic_add(-queue->c2_qin.pl_nr,
399                                    &anchor->csi_sync_nr);
400                         cl_page_list_for_each(page, &queue->c2_qin)
401                                 page->cp_sync_io = NULL;
402                 }
403                 /* handle partially submitted reqs */
404                 if (queue->c2_qin.pl_nr > 0) {
405                         CERROR(DFID " failed to submit %d dio pages: %zd\n",
406                                PFID(lu_object_fid(&obj->co_lu)),
407                                queue->c2_qin.pl_nr, rc);
408                         if (rc == 0)
409                                 rc = -EIO;
410                 }
411         }
412
413         cl_2queue_discard(env, io, queue);
414         cl_2queue_disown(env, io, queue);
415         cl_2queue_fini(env, queue);
416         RETURN(rc);
417 }
418
419 #ifdef KMALLOC_MAX_SIZE
420 #define MAX_MALLOC KMALLOC_MAX_SIZE
421 #else
422 #define MAX_MALLOC (128 * 1024)
423 #endif
424
425 /* This is the maximum size of a single O_DIRECT request, based on the
426  * kmalloc limit.  We need to fit all of the brw_page structs, each one
427  * representing PAGE_SIZE worth of user data, into a single buffer, and
428  * then truncate this to be a full-sized RPC.  For 4kB PAGE_SIZE this is
429  * up to 22MB for 128kB kmalloc and up to 682MB for 4MB kmalloc. */
430 #define MAX_DIO_SIZE ((MAX_MALLOC / sizeof(struct brw_page) * PAGE_SIZE) & \
431                       ~((size_t)DT_MAX_BRW_SIZE - 1))
432
433 static ssize_t
434 ll_direct_IO_impl(struct kiocb *iocb, struct iov_iter *iter, int rw)
435 {
436         struct ll_cl_context *lcc;
437         const struct lu_env *env;
438         struct cl_io *io;
439         struct file *file = iocb->ki_filp;
440         struct inode *inode = file->f_mapping->host;
441         struct cl_dio_aio *aio;
442         size_t count = iov_iter_count(iter);
443         ssize_t tot_bytes = 0, result = 0;
444         loff_t file_offset = iocb->ki_pos;
445         struct vvp_io *vio;
446
447         /* Check EOF by ourselves */
448         if (rw == READ && file_offset >= i_size_read(inode))
449                 return 0;
450
451         /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
452         if (file_offset & ~PAGE_MASK)
453                 RETURN(-EINVAL);
454
455         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), size=%zd (max %lu), "
456                "offset=%lld=%llx, pages %zd (max %lu)\n",
457                PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
458                file_offset, file_offset, count >> PAGE_SHIFT,
459                MAX_DIO_SIZE >> PAGE_SHIFT);
460
461         /* Check that all user buffers are aligned as well */
462         if (ll_iov_iter_alignment(iter) & ~PAGE_MASK)
463                 RETURN(-EINVAL);
464
465         lcc = ll_cl_find(file);
466         if (lcc == NULL)
467                 RETURN(-EIO);
468
469         env = lcc->lcc_env;
470         LASSERT(!IS_ERR(env));
471         vio = vvp_env_io(env);
472         io = lcc->lcc_io;
473         LASSERT(io != NULL);
474
475         aio = io->ci_aio;
476         LASSERT(aio);
477         LASSERT(aio->cda_iocb == iocb);
478
479         while (iov_iter_count(iter)) {
480                 struct ll_dio_pages pvec = { .ldp_aio = aio };
481                 struct page **pages;
482
483                 count = min_t(size_t, iov_iter_count(iter), MAX_DIO_SIZE);
484                 if (rw == READ) {
485                         if (file_offset >= i_size_read(inode))
486                                 break;
487
488                         if (file_offset + count > i_size_read(inode))
489                                 count = i_size_read(inode) - file_offset;
490                 }
491
492                 result = ll_get_user_pages(rw, iter, &pages,
493                                            &pvec.ldp_count, count);
494                 if (unlikely(result <= 0))
495                         GOTO(out, result);
496
497                 count = result;
498                 pvec.ldp_file_offset = file_offset;
499                 pvec.ldp_pages = pages;
500
501                 result = ll_direct_rw_pages(env, io, count,
502                                             rw, inode, &pvec);
503                 ll_free_user_pages(pages, pvec.ldp_count);
504
505                 if (unlikely(result < 0))
506                         GOTO(out, result);
507
508                 iov_iter_advance(iter, count);
509                 tot_bytes += count;
510                 file_offset += count;
511         }
512
513 out:
514         aio->cda_bytes += tot_bytes;
515
516         if (is_sync_kiocb(iocb)) {
517                 struct cl_sync_io *anchor = &aio->cda_sync;
518                 ssize_t rc2;
519
520                 /**
521                  * @anchor was inited as 1 to prevent end_io to be
522                  * called before we add all pages for IO, so drop
523                  * one extra reference to make sure we could wait
524                  * count to be zero.
525                  */
526                 cl_sync_io_note(env, anchor, result);
527
528                 rc2 = cl_sync_io_wait(env, anchor, 0);
529                 if (result == 0 && rc2)
530                         result = rc2;
531                 /**
532                  * One extra reference again, as if @anchor is
533                  * reused we assume it as 1 before using.
534                  */
535                 atomic_add(1, &anchor->csi_sync_nr);
536                 if (result == 0) {
537                         /* no commit async for direct IO */
538                         vio->u.readwrite.vui_written += tot_bytes;
539                         result = tot_bytes;
540                 }
541         } else {
542                 if (rw == WRITE)
543                         vio->u.readwrite.vui_written += tot_bytes;
544                 else
545                         vio->u.readwrite.vui_read += tot_bytes;
546                 result = -EIOCBQUEUED;
547         }
548
549         return result;
550 }
551
552 #if defined(HAVE_DIO_ITER)
553 static ssize_t ll_direct_IO(
554 #ifndef HAVE_IOV_ITER_RW
555              int rw,
556 #endif
557              struct kiocb *iocb, struct iov_iter *iter
558 #ifndef HAVE_DIRECTIO_2ARGS
559              , loff_t file_offset
560 #endif
561              )
562 {
563         int nrw;
564
565 #ifndef HAVE_IOV_ITER_RW
566         nrw = rw;
567 #else
568         nrw = iov_iter_rw(iter);
569 #endif
570
571         return ll_direct_IO_impl(iocb, iter, nrw);
572 }
573
574 #else /* !defined(HAVE_DIO_ITER) */
575
576 static ssize_t
577 ll_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
578              loff_t file_offset, unsigned long nr_segs)
579 {
580         struct iov_iter iter;
581
582         iov_iter_init(&iter, iov, nr_segs, iov_length(iov, nr_segs), 0);
583         return ll_direct_IO_impl(iocb, &iter, rw);
584 }
585
586 #endif /* !defined(HAVE_DIO_ITER) */
587
588 /**
589  * Prepare partially written-to page for a write.
590  * @pg is owned when passed in and disowned when it returns non-zero result to
591  * the caller.
592  */
593 static int ll_prepare_partial_page(const struct lu_env *env, struct cl_io *io,
594                                    struct cl_page *pg, struct file *file)
595 {
596         struct cl_attr *attr   = vvp_env_thread_attr(env);
597         struct cl_object *obj  = io->ci_obj;
598         struct vvp_page *vpg   = cl_object_page_slice(obj, pg);
599         loff_t          offset = cl_offset(obj, vvp_index(vpg));
600         int             result;
601         ENTRY;
602
603         cl_object_attr_lock(obj);
604         result = cl_object_attr_get(env, obj, attr);
605         cl_object_attr_unlock(obj);
606         if (result) {
607                 cl_page_disown(env, io, pg);
608                 GOTO(out, result);
609         }
610
611         /*
612          * If are writing to a new page, no need to read old data.
613          * The extent locking will have updated the KMS, and for our
614          * purposes here we can treat it like i_size.
615          */
616         if (attr->cat_kms <= offset) {
617                 char *kaddr = kmap_atomic(vpg->vpg_page);
618
619                 memset(kaddr, 0, cl_page_size(obj));
620                 kunmap_atomic(kaddr);
621                 GOTO(out, result = 0);
622         }
623
624         if (vpg->vpg_defer_uptodate) {
625                 vpg->vpg_ra_used = 1;
626                 GOTO(out, result = 0);
627         }
628
629         result = ll_io_read_page(env, io, pg, file);
630         if (result)
631                 GOTO(out, result);
632
633         /* ll_io_read_page() disowns the page */
634         result = cl_page_own(env, io, pg);
635         if (!result) {
636                 if (!PageUptodate(cl_page_vmpage(pg))) {
637                         cl_page_disown(env, io, pg);
638                         result = -EIO;
639                 }
640         } else if (result == -ENOENT) {
641                 /* page was truncated */
642                 result = -EAGAIN;
643         }
644         EXIT;
645
646 out:
647         return result;
648 }
649
650 static int ll_tiny_write_begin(struct page *vmpage, struct address_space *mapping)
651 {
652         /* Page must be present, up to date, dirty, and not in writeback. */
653         if (!vmpage || !PageUptodate(vmpage) || !PageDirty(vmpage) ||
654             PageWriteback(vmpage) || vmpage->mapping != mapping)
655                 return -ENODATA;
656
657         return 0;
658 }
659
660 static int ll_write_begin(struct file *file, struct address_space *mapping,
661                           loff_t pos, unsigned len, unsigned flags,
662                           struct page **pagep, void **fsdata)
663 {
664         struct ll_cl_context *lcc = NULL;
665         const struct lu_env  *env = NULL;
666         struct cl_io   *io = NULL;
667         struct cl_page *page = NULL;
668
669         struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
670         pgoff_t index = pos >> PAGE_SHIFT;
671         struct page *vmpage = NULL;
672         unsigned from = pos & (PAGE_SIZE - 1);
673         unsigned to = from + len;
674         int result = 0;
675         ENTRY;
676
677         CDEBUG(D_VFSTRACE, "Writing %lu of %d to %d bytes\n", index, from, len);
678
679         lcc = ll_cl_find(file);
680         if (lcc == NULL) {
681                 vmpage = grab_cache_page_nowait(mapping, index);
682                 result = ll_tiny_write_begin(vmpage, mapping);
683                 GOTO(out, result);
684         }
685
686         env = lcc->lcc_env;
687         io  = lcc->lcc_io;
688
689         if (file->f_flags & O_DIRECT) {
690                 /* direct IO failed because it couldn't clean up cached pages,
691                  * this causes a problem for mirror write because the cached
692                  * page may belong to another mirror, which will result in
693                  * problem submitting the I/O. */
694                 if (io->ci_designated_mirror > 0)
695                         GOTO(out, result = -EBUSY);
696
697                 /**
698                  * Direct write can fall back to buffered read, but DIO is done
699                  * with lockless i/o, and buffered requires LDLM locking, so
700                  * in this case we must restart without lockless.
701                  */
702                 if (!io->ci_dio_lock) {
703                         io->ci_dio_lock = 1;
704                         io->ci_need_restart = 1;
705                         GOTO(out, result = -ENOLCK);
706                 }
707         }
708 again:
709         /* To avoid deadlock, try to lock page first. */
710         vmpage = grab_cache_page_nowait(mapping, index);
711
712         if (unlikely(vmpage == NULL ||
713                      PageDirty(vmpage) || PageWriteback(vmpage))) {
714                 struct vvp_io *vio = vvp_env_io(env);
715                 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
716
717                 /* if the page is already in dirty cache, we have to commit
718                  * the pages right now; otherwise, it may cause deadlock
719                  * because it holds page lock of a dirty page and request for
720                  * more grants. It's okay for the dirty page to be the first
721                  * one in commit page list, though. */
722                 if (vmpage != NULL && plist->pl_nr > 0) {
723                         unlock_page(vmpage);
724                         put_page(vmpage);
725                         vmpage = NULL;
726                 }
727
728                 /* commit pages and then wait for page lock */
729                 result = vvp_io_write_commit(env, io);
730                 if (result < 0)
731                         GOTO(out, result);
732
733                 if (vmpage == NULL) {
734                         vmpage = grab_cache_page_write_begin(mapping, index,
735                                                              flags);
736                         if (vmpage == NULL)
737                                 GOTO(out, result = -ENOMEM);
738                 }
739         }
740
741         /* page was truncated */
742         if (mapping != vmpage->mapping) {
743                 CDEBUG(D_VFSTRACE, "page: %lu was truncated\n", index);
744                 unlock_page(vmpage);
745                 put_page(vmpage);
746                 vmpage = NULL;
747                 goto again;
748         }
749
750         page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
751         if (IS_ERR(page))
752                 GOTO(out, result = PTR_ERR(page));
753
754         lcc->lcc_page = page;
755         lu_ref_add(&page->cp_reference, "cl_io", io);
756
757         cl_page_assume(env, io, page);
758         if (!PageUptodate(vmpage)) {
759                 /*
760                  * We're completely overwriting an existing page,
761                  * so _don't_ set it up to date until commit_write
762                  */
763                 if (from == 0 && to == PAGE_SIZE) {
764                         CL_PAGE_HEADER(D_PAGE, env, page, "full page write\n");
765                         POISON_PAGE(vmpage, 0x11);
766                 } else {
767                         /* TODO: can be optimized at OSC layer to check if it
768                          * is a lockless IO. In that case, it's not necessary
769                          * to read the data. */
770                         result = ll_prepare_partial_page(env, io, page, file);
771                         if (result) {
772                                 /* vmpage should have been unlocked */
773                                 put_page(vmpage);
774                                 vmpage = NULL;
775
776                                 if (result == -EAGAIN)
777                                         goto again;
778                                 GOTO(out, result);
779                         }
780                 }
781         }
782         EXIT;
783 out:
784         if (result < 0) {
785                 if (vmpage != NULL) {
786                         unlock_page(vmpage);
787                         put_page(vmpage);
788                 }
789                 /* On tiny_write failure, page and io are always null. */
790                 if (!IS_ERR_OR_NULL(page)) {
791                         lu_ref_del(&page->cp_reference, "cl_io", io);
792                         cl_page_put(env, page);
793                 }
794                 if (io)
795                         io->ci_result = result;
796         } else {
797                 *pagep = vmpage;
798                 *fsdata = lcc;
799         }
800         RETURN(result);
801 }
802
803 static int ll_tiny_write_end(struct file *file, struct address_space *mapping,
804                              loff_t pos, unsigned int len, unsigned int copied,
805                              struct page *vmpage)
806 {
807         struct cl_page *clpage = (struct cl_page *) vmpage->private;
808         loff_t kms = pos+copied;
809         loff_t to = kms & (PAGE_SIZE-1) ? kms & (PAGE_SIZE-1) : PAGE_SIZE;
810         __u16 refcheck;
811         struct lu_env *env = cl_env_get(&refcheck);
812         int rc = 0;
813
814         ENTRY;
815
816         if (IS_ERR(env)) {
817                 rc = PTR_ERR(env);
818                 goto out;
819         }
820
821         /* This page is dirty in cache, so it should have a cl_page pointer
822          * set in vmpage->private.
823          */
824         LASSERT(clpage != NULL);
825
826         if (copied == 0)
827                 goto out_env;
828
829         /* Update the underlying size information in the OSC/LOV objects this
830          * page is part of.
831          */
832         cl_page_touch(env, clpage, to);
833
834 out_env:
835         cl_env_put(env, &refcheck);
836
837 out:
838         /* Must return page unlocked. */
839         unlock_page(vmpage);
840
841         RETURN(rc);
842 }
843
844 static int ll_write_end(struct file *file, struct address_space *mapping,
845                         loff_t pos, unsigned len, unsigned copied,
846                         struct page *vmpage, void *fsdata)
847 {
848         struct ll_cl_context *lcc = fsdata;
849         const struct lu_env *env;
850         struct cl_io *io;
851         struct vvp_io *vio;
852         struct cl_page *page;
853         unsigned from = pos & (PAGE_SIZE - 1);
854         bool unplug = false;
855         int result = 0;
856         ENTRY;
857
858         put_page(vmpage);
859
860         CDEBUG(D_VFSTRACE, "pos %llu, len %u, copied %u\n", pos, len, copied);
861
862         if (lcc == NULL) {
863                 result = ll_tiny_write_end(file, mapping, pos, len, copied,
864                                            vmpage);
865                 GOTO(out, result);
866         }
867
868         LASSERT(lcc != NULL);
869         env  = lcc->lcc_env;
870         page = lcc->lcc_page;
871         io   = lcc->lcc_io;
872         vio  = vvp_env_io(env);
873
874         LASSERT(cl_page_is_owned(page, io));
875         if (copied > 0) {
876                 struct cl_page_list *plist = &vio->u.readwrite.vui_queue;
877
878                 lcc->lcc_page = NULL; /* page will be queued */
879
880                 /* Add it into write queue */
881                 cl_page_list_add(plist, page);
882                 if (plist->pl_nr == 1) /* first page */
883                         vio->u.readwrite.vui_from = from;
884                 else
885                         LASSERT(from == 0);
886                 vio->u.readwrite.vui_to = from + copied;
887
888                 /* To address the deadlock in balance_dirty_pages() where
889                  * this dirty page may be written back in the same thread. */
890                 if (PageDirty(vmpage))
891                         unplug = true;
892
893                 /* We may have one full RPC, commit it soon */
894                 if (plist->pl_nr >= PTLRPC_MAX_BRW_PAGES)
895                         unplug = true;
896
897                 CL_PAGE_DEBUG(D_VFSTRACE, env, page,
898                               "queued page: %d.\n", plist->pl_nr);
899         } else {
900                 cl_page_disown(env, io, page);
901
902                 lcc->lcc_page = NULL;
903                 lu_ref_del(&page->cp_reference, "cl_io", io);
904                 cl_page_put(env, page);
905
906                 /* page list is not contiguous now, commit it now */
907                 unplug = true;
908         }
909         if (unplug || io->u.ci_wr.wr_sync)
910                 result = vvp_io_write_commit(env, io);
911
912         if (result < 0)
913                 io->ci_result = result;
914
915
916 out:
917         RETURN(result >= 0 ? copied : result);
918 }
919
920 #ifdef CONFIG_MIGRATION
921 static int ll_migratepage(struct address_space *mapping,
922                           struct page *newpage, struct page *page,
923                           enum migrate_mode mode)
924 {
925         /* Always fail page migration until we have a proper implementation */
926         return -EIO;
927 }
928 #endif
929
930 const struct address_space_operations ll_aops = {
931         .readpage       = ll_readpage,
932         .direct_IO      = ll_direct_IO,
933         .writepage      = ll_writepage,
934         .writepages     = ll_writepages,
935         .set_page_dirty = __set_page_dirty_nobuffers,
936         .write_begin    = ll_write_begin,
937         .write_end      = ll_write_end,
938         .invalidatepage = ll_invalidatepage,
939         .releasepage    = (void *)ll_releasepage,
940 #ifdef CONFIG_MIGRATION
941         .migratepage    = ll_migratepage,
942 #endif
943 };