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