Whamcloud - gitweb
LU-4198 clio: AIO support for direct IO
[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 #ifndef HAVE_AIO_COMPLETE
291 static inline void aio_complete(struct kiocb *iocb, ssize_t res, ssize_t res2)
292 {
293         if (iocb->ki_complete)
294                 iocb->ki_complete(iocb, res, res2);
295 }
296 #endif
297
298 /** direct IO pages */
299 struct ll_dio_pages {
300         struct cl_dio_aio       *ldp_aio;
301         /*
302          * page array to be written. we don't support
303          * partial pages except the last one.
304          */
305         struct page             **ldp_pages;
306         /** # of pages in the array. */
307         size_t                  ldp_count;
308         /* the file offset of the first page. */
309         loff_t                  ldp_file_offset;
310 };
311
312 static void ll_aio_end(const struct lu_env *env, struct cl_sync_io *anchor)
313 {
314         struct cl_dio_aio *aio = container_of(anchor, typeof(*aio), cda_sync);
315         ssize_t ret = anchor->csi_sync_rc;
316
317         ENTRY;
318
319         /* release pages */
320         while (aio->cda_pages.pl_nr > 0) {
321                 struct cl_page *page = cl_page_list_first(&aio->cda_pages);
322
323                 cl_page_get(page);
324                 cl_page_list_del(env, &aio->cda_pages, page);
325                 cl_page_delete(env, page);
326                 cl_page_put(env, page);
327         }
328
329         if (!is_sync_kiocb(aio->cda_iocb))
330                 aio_complete(aio->cda_iocb, ret ?: aio->cda_bytes, 0);
331
332         EXIT;
333 }
334
335 static struct cl_dio_aio *ll_aio_alloc(struct kiocb *iocb)
336 {
337         struct cl_dio_aio *aio;
338
339         OBD_ALLOC_PTR(aio);
340         if (aio != NULL) {
341                 /*
342                  * Hold one ref so that it won't be released until
343                  * every pages is added.
344                  */
345                 cl_sync_io_init_notify(&aio->cda_sync, 1, is_sync_kiocb(iocb) ?
346                                        NULL : aio, ll_aio_end);
347                 cl_page_list_init(&aio->cda_pages);
348                 aio->cda_iocb = iocb;
349         }
350         return aio;
351 }
352
353 static int
354 ll_direct_rw_pages(const struct lu_env *env, struct cl_io *io, size_t size,
355                    int rw, struct inode *inode, struct ll_dio_pages *pv)
356 {
357         struct cl_page    *page;
358         struct cl_2queue  *queue = &io->ci_queue;
359         struct cl_object  *obj = io->ci_obj;
360         struct cl_sync_io *anchor = &pv->ldp_aio->cda_sync;
361         loff_t offset   = pv->ldp_file_offset;
362         int io_pages    = 0;
363         size_t page_size = cl_page_size(obj);
364         int i;
365         ssize_t rc = 0;
366
367         ENTRY;
368
369         cl_2queue_init(queue);
370         for (i = 0; i < pv->ldp_count; i++) {
371                 LASSERT(!(offset & (PAGE_SIZE - 1)));
372                 page = cl_page_find(env, obj, cl_index(obj, offset),
373                                     pv->ldp_pages[i], CPT_TRANSIENT);
374                 if (IS_ERR(page)) {
375                         rc = PTR_ERR(page);
376                         break;
377                 }
378                 LASSERT(page->cp_type == CPT_TRANSIENT);
379                 rc = cl_page_own(env, io, page);
380                 if (rc) {
381                         cl_page_put(env, page);
382                         break;
383                 }
384
385                 page->cp_sync_io = anchor;
386                 cl_2queue_add(queue, page);
387                 /*
388                  * Set page clip to tell transfer formation engine
389                  * that page has to be sent even if it is beyond KMS.
390                  */
391                 cl_page_clip(env, page, 0, min(size, page_size));
392                 ++io_pages;
393
394                 /* drop the reference count for cl_page_find */
395                 cl_page_put(env, page);
396                 offset += page_size;
397                 size -= page_size;
398         }
399         if (rc == 0 && io_pages > 0) {
400                 int iot = rw == READ ? CRT_READ : CRT_WRITE;
401
402                 atomic_add(io_pages, &anchor->csi_sync_nr);
403                 rc = cl_io_submit_rw(env, io, iot, queue);
404                 if (rc == 0) {
405                         cl_page_list_splice(&queue->c2_qout,
406                                         &pv->ldp_aio->cda_pages);
407                 } else {
408                         atomic_add(-queue->c2_qin.pl_nr,
409                                    &anchor->csi_sync_nr);
410                         cl_page_list_for_each(page, &queue->c2_qin)
411                                 page->cp_sync_io = NULL;
412                 }
413                 /* handle partially submitted reqs */
414                 if (queue->c2_qin.pl_nr > 0) {
415                         CERROR(DFID " failed to submit %d dio pages: %zd\n",
416                                PFID(lu_object_fid(&obj->co_lu)),
417                                queue->c2_qin.pl_nr, rc);
418                         if (rc == 0)
419                                 rc = -EIO;
420                 }
421         }
422
423         cl_2queue_discard(env, io, queue);
424         cl_2queue_disown(env, io, queue);
425         cl_2queue_fini(env, queue);
426         RETURN(rc);
427 }
428
429 #ifdef KMALLOC_MAX_SIZE
430 #define MAX_MALLOC KMALLOC_MAX_SIZE
431 #else
432 #define MAX_MALLOC (128 * 1024)
433 #endif
434
435 /* This is the maximum size of a single O_DIRECT request, based on the
436  * kmalloc limit.  We need to fit all of the brw_page structs, each one
437  * representing PAGE_SIZE worth of user data, into a single buffer, and
438  * then truncate this to be a full-sized RPC.  For 4kB PAGE_SIZE this is
439  * up to 22MB for 128kB kmalloc and up to 682MB for 4MB kmalloc. */
440 #define MAX_DIO_SIZE ((MAX_MALLOC / sizeof(struct brw_page) * PAGE_SIZE) & \
441                       ~(DT_MAX_BRW_SIZE - 1))
442
443 static ssize_t
444 ll_direct_IO_impl(struct kiocb *iocb, struct iov_iter *iter, int rw)
445 {
446         struct ll_cl_context *lcc;
447         const struct lu_env *env;
448         struct cl_io *io;
449         struct file *file = iocb->ki_filp;
450         struct inode *inode = file->f_mapping->host;
451         struct cl_dio_aio *aio;
452         size_t count = iov_iter_count(iter);
453         ssize_t tot_bytes = 0, result = 0;
454         loff_t file_offset = iocb->ki_pos;
455
456         /* Check EOF by ourselves */
457         if (rw == READ && file_offset >= i_size_read(inode))
458                 return 0;
459
460         /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
461         if ((file_offset & ~PAGE_MASK) || (count & ~PAGE_MASK))
462                 return -EINVAL;
463
464         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), size=%zd (max %lu), "
465                "offset=%lld=%llx, pages %zd (max %lu)\n",
466                PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
467                file_offset, file_offset, count >> PAGE_SHIFT,
468                MAX_DIO_SIZE >> PAGE_SHIFT);
469
470         /* Check that all user buffers are aligned as well */
471         if (ll_iov_iter_alignment(iter) & ~PAGE_MASK)
472                 return -EINVAL;
473
474         lcc = ll_cl_find(file);
475         if (lcc == NULL)
476                 RETURN(-EIO);
477
478         env = lcc->lcc_env;
479         LASSERT(!IS_ERR(env));
480         io = lcc->lcc_io;
481         LASSERT(io != NULL);
482
483         aio = ll_aio_alloc(iocb);
484         if (!aio)
485                 RETURN(-ENOMEM);
486
487         /* 0. Need locking between buffered and direct access. and race with
488          *    size changing by concurrent truncates and writes.
489          * 1. Need inode mutex to operate transient pages.
490          */
491         if (rw == READ)
492                 inode_lock(inode);
493
494         while (iov_iter_count(iter)) {
495                 struct ll_dio_pages pvec = { .ldp_aio = aio };
496                 struct page **pages;
497
498                 count = min_t(size_t, iov_iter_count(iter), MAX_DIO_SIZE);
499                 if (rw == READ) {
500                         if (file_offset >= i_size_read(inode))
501                                 break;
502
503                         if (file_offset + count > i_size_read(inode))
504                                 count = i_size_read(inode) - file_offset;
505                 }
506
507                 result = ll_get_user_pages(rw, iter, &pages,
508                                            &pvec.ldp_count, count);
509                 if (unlikely(result <= 0))
510                         GOTO(out, result);
511
512                 count = result;
513                 pvec.ldp_file_offset = file_offset;
514                 pvec.ldp_pages = pages;
515
516                 result = ll_direct_rw_pages(env, io, count,
517                                             rw, inode, &pvec);
518                 ll_free_user_pages(pages, pvec.ldp_count);
519
520                 if (unlikely(result < 0))
521                         GOTO(out, result);
522
523                 iov_iter_advance(iter, count);
524                 tot_bytes += count;
525                 file_offset += count;
526         }
527
528 out:
529         aio->cda_bytes = tot_bytes;
530         cl_sync_io_note(env, &aio->cda_sync, result);
531
532         if (is_sync_kiocb(iocb)) {
533                 ssize_t rc2;
534
535                 rc2 = cl_sync_io_wait(env, &aio->cda_sync, 0);
536                 if (result == 0 && rc2)
537                         result = rc2;
538
539                 if (result == 0) {
540                         struct vvp_io *vio = vvp_env_io(env);
541                         /* no commit async for direct IO */
542                         vio->u.write.vui_written += tot_bytes;
543                         result = tot_bytes;
544                 }
545                 OBD_FREE_PTR(aio);
546
547         } else {
548                 result = -EIOCBQUEUED;
549         }
550
551         if (rw == READ)
552                 inode_unlock(inode);
553
554         return result;
555 }
556
557 #if defined(HAVE_DIO_ITER)
558 static ssize_t ll_direct_IO(
559 #ifndef HAVE_IOV_ITER_RW
560              int rw,
561 #endif
562              struct kiocb *iocb, struct iov_iter *iter
563 #ifndef HAVE_DIRECTIO_2ARGS
564              , loff_t file_offset
565 #endif
566              )
567 {
568         int nrw;
569
570 #ifndef HAVE_IOV_ITER_RW
571         nrw = rw;
572 #else
573         nrw = iov_iter_rw(iter);
574 #endif
575
576         return ll_direct_IO_impl(iocb, iter, nrw);
577 }
578
579 #else /* !defined(HAVE_DIO_ITER) */
580
581 static ssize_t
582 ll_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
583              loff_t file_offset, unsigned long nr_segs)
584 {
585         struct iov_iter iter;
586
587         iov_iter_init(&iter, iov, nr_segs, iov_length(iov, nr_segs), 0);
588         return ll_direct_IO_impl(iocb, &iter, rw);
589 }
590
591 #endif /* !defined(HAVE_DIO_ITER) */
592
593 /**
594  * Prepare partially written-to page for a write.
595  * @pg is owned when passed in and disowned when it returns non-zero result to
596  * the caller.
597  */
598 static int ll_prepare_partial_page(const struct lu_env *env, struct cl_io *io,
599                                    struct cl_page *pg, struct file *file)
600 {
601         struct cl_attr *attr   = vvp_env_thread_attr(env);
602         struct cl_object *obj  = io->ci_obj;
603         struct vvp_page *vpg   = cl_object_page_slice(obj, pg);
604         loff_t          offset = cl_offset(obj, vvp_index(vpg));
605         int             result;
606         ENTRY;
607
608         cl_object_attr_lock(obj);
609         result = cl_object_attr_get(env, obj, attr);
610         cl_object_attr_unlock(obj);
611         if (result) {
612                 cl_page_disown(env, io, pg);
613                 GOTO(out, result);
614         }
615
616         /*
617          * If are writing to a new page, no need to read old data.
618          * The extent locking will have updated the KMS, and for our
619          * purposes here we can treat it like i_size.
620          */
621         if (attr->cat_kms <= offset) {
622                 char *kaddr = ll_kmap_atomic(vpg->vpg_page, KM_USER0);
623
624                 memset(kaddr, 0, cl_page_size(obj));
625                 ll_kunmap_atomic(kaddr, KM_USER0);
626                 GOTO(out, result = 0);
627         }
628
629         if (vpg->vpg_defer_uptodate) {
630                 vpg->vpg_ra_used = 1;
631                 GOTO(out, result = 0);
632         }
633
634         result = ll_io_read_page(env, io, pg, file);
635         if (result)
636                 GOTO(out, result);
637
638         /* ll_io_read_page() disowns the page */
639         result = cl_page_own(env, io, pg);
640         if (!result) {
641                 if (!PageUptodate(cl_page_vmpage(pg))) {
642                         cl_page_disown(env, io, pg);
643                         result = -EIO;
644                 }
645         } else if (result == -ENOENT) {
646                 /* page was truncated */
647                 result = -EAGAIN;
648         }
649         EXIT;
650
651 out:
652         return result;
653 }
654
655 static int ll_tiny_write_begin(struct page *vmpage)
656 {
657         /* Page must be present, up to date, dirty, and not in writeback. */
658         if (!vmpage || !PageUptodate(vmpage) || !PageDirty(vmpage) ||
659             PageWriteback(vmpage))
660                 return -ENODATA;
661
662         return 0;
663 }
664
665 static int ll_write_begin(struct file *file, struct address_space *mapping,
666                           loff_t pos, unsigned len, unsigned flags,
667                           struct page **pagep, void **fsdata)
668 {
669         struct ll_cl_context *lcc = NULL;
670         const struct lu_env  *env = NULL;
671         struct cl_io   *io = NULL;
672         struct cl_page *page = NULL;
673
674         struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
675         pgoff_t index = pos >> PAGE_SHIFT;
676         struct page *vmpage = NULL;
677         unsigned from = pos & (PAGE_SIZE - 1);
678         unsigned to = from + len;
679         int result = 0;
680         ENTRY;
681
682         CDEBUG(D_VFSTRACE, "Writing %lu of %d to %d bytes\n", index, from, len);
683
684         lcc = ll_cl_find(file);
685         if (lcc == NULL) {
686                 vmpage = grab_cache_page_nowait(mapping, index);
687                 result = ll_tiny_write_begin(vmpage);
688                 GOTO(out, result);
689         }
690
691         env = lcc->lcc_env;
692         io  = lcc->lcc_io;
693
694         if (file->f_flags & O_DIRECT) {
695                 /* direct IO failed because it couldn't clean up cached pages,
696                  * this causes a problem for mirror write because the cached
697                  * page may belong to another mirror, which will result in
698                  * problem submitting the I/O. */
699                 if (io->ci_designated_mirror > 0)
700                         GOTO(out, result = -EBUSY);
701
702                 /**
703                  * Direct read can fall back to buffered read, but DIO is done
704                  * with lockless i/o, and buffered requires LDLM locking, so
705                  * in this case we must restart without lockless.
706                  */
707                 if (!io->ci_ignore_lockless) {
708                         io->ci_ignore_lockless = 1;
709                         io->ci_need_restart = 1;
710                         GOTO(out, result = -ENOLCK);
711                 }
712         }
713 again:
714         /* To avoid deadlock, try to lock page first. */
715         vmpage = grab_cache_page_nowait(mapping, index);
716
717         if (unlikely(vmpage == NULL ||
718                      PageDirty(vmpage) || PageWriteback(vmpage))) {
719                 struct vvp_io *vio = vvp_env_io(env);
720                 struct cl_page_list *plist = &vio->u.write.vui_queue;
721
722                 /* if the page is already in dirty cache, we have to commit
723                  * the pages right now; otherwise, it may cause deadlock
724                  * because it holds page lock of a dirty page and request for
725                  * more grants. It's okay for the dirty page to be the first
726                  * one in commit page list, though. */
727                 if (vmpage != NULL && plist->pl_nr > 0) {
728                         unlock_page(vmpage);
729                         put_page(vmpage);
730                         vmpage = NULL;
731                 }
732
733                 /* commit pages and then wait for page lock */
734                 result = vvp_io_write_commit(env, io);
735                 if (result < 0)
736                         GOTO(out, result);
737
738                 if (vmpage == NULL) {
739                         vmpage = grab_cache_page_write_begin(mapping, index,
740                                                              flags);
741                         if (vmpage == NULL)
742                                 GOTO(out, result = -ENOMEM);
743                 }
744         }
745
746         page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
747         if (IS_ERR(page))
748                 GOTO(out, result = PTR_ERR(page));
749
750         lcc->lcc_page = page;
751         lu_ref_add(&page->cp_reference, "cl_io", io);
752
753         cl_page_assume(env, io, page);
754         if (!PageUptodate(vmpage)) {
755                 /*
756                  * We're completely overwriting an existing page,
757                  * so _don't_ set it up to date until commit_write
758                  */
759                 if (from == 0 && to == PAGE_SIZE) {
760                         CL_PAGE_HEADER(D_PAGE, env, page, "full page write\n");
761                         POISON_PAGE(vmpage, 0x11);
762                 } else {
763                         /* TODO: can be optimized at OSC layer to check if it
764                          * is a lockless IO. In that case, it's not necessary
765                          * to read the data. */
766                         result = ll_prepare_partial_page(env, io, page, file);
767                         if (result) {
768                                 /* vmpage should have been unlocked */
769                                 put_page(vmpage);
770                                 vmpage = NULL;
771
772                                 if (result == -EAGAIN)
773                                         goto again;
774                                 GOTO(out, result);
775                         }
776                 }
777         }
778         EXIT;
779 out:
780         if (result < 0) {
781                 if (vmpage != NULL) {
782                         unlock_page(vmpage);
783                         put_page(vmpage);
784                 }
785                 /* On tiny_write failure, page and io are always null. */
786                 if (!IS_ERR_OR_NULL(page)) {
787                         lu_ref_del(&page->cp_reference, "cl_io", io);
788                         cl_page_put(env, page);
789                 }
790                 if (io)
791                         io->ci_result = result;
792         } else {
793                 *pagep = vmpage;
794                 *fsdata = lcc;
795         }
796         RETURN(result);
797 }
798
799 static int ll_tiny_write_end(struct file *file, struct address_space *mapping,
800                              loff_t pos, unsigned int len, unsigned int copied,
801                              struct page *vmpage)
802 {
803         struct cl_page *clpage = (struct cl_page *) vmpage->private;
804         loff_t kms = pos+copied;
805         loff_t to = kms & (PAGE_SIZE-1) ? kms & (PAGE_SIZE-1) : PAGE_SIZE;
806         __u16 refcheck;
807         struct lu_env *env = cl_env_get(&refcheck);
808         int rc = 0;
809
810         ENTRY;
811
812         if (IS_ERR(env)) {
813                 rc = PTR_ERR(env);
814                 goto out;
815         }
816
817         /* This page is dirty in cache, so it should have a cl_page pointer
818          * set in vmpage->private.
819          */
820         LASSERT(clpage != NULL);
821
822         if (copied == 0)
823                 goto out_env;
824
825         /* Update the underlying size information in the OSC/LOV objects this
826          * page is part of.
827          */
828         cl_page_touch(env, clpage, to);
829
830 out_env:
831         cl_env_put(env, &refcheck);
832
833 out:
834         /* Must return page unlocked. */
835         unlock_page(vmpage);
836
837         RETURN(rc);
838 }
839
840 static int ll_write_end(struct file *file, struct address_space *mapping,
841                         loff_t pos, unsigned len, unsigned copied,
842                         struct page *vmpage, void *fsdata)
843 {
844         struct ll_cl_context *lcc = fsdata;
845         const struct lu_env *env;
846         struct cl_io *io;
847         struct vvp_io *vio;
848         struct cl_page *page;
849         unsigned from = pos & (PAGE_SIZE - 1);
850         bool unplug = false;
851         int result = 0;
852         ENTRY;
853
854         put_page(vmpage);
855
856         CDEBUG(D_VFSTRACE, "pos %llu, len %u, copied %u\n", pos, len, copied);
857
858         if (lcc == NULL) {
859                 result = ll_tiny_write_end(file, mapping, pos, len, copied,
860                                            vmpage);
861                 GOTO(out, result);
862         }
863
864         LASSERT(lcc != NULL);
865         env  = lcc->lcc_env;
866         page = lcc->lcc_page;
867         io   = lcc->lcc_io;
868         vio  = vvp_env_io(env);
869
870         LASSERT(cl_page_is_owned(page, io));
871         if (copied > 0) {
872                 struct cl_page_list *plist = &vio->u.write.vui_queue;
873
874                 lcc->lcc_page = NULL; /* page will be queued */
875
876                 /* Add it into write queue */
877                 cl_page_list_add(plist, page);
878                 if (plist->pl_nr == 1) /* first page */
879                         vio->u.write.vui_from = from;
880                 else
881                         LASSERT(from == 0);
882                 vio->u.write.vui_to = from + copied;
883
884                 /* To address the deadlock in balance_dirty_pages() where
885                  * this dirty page may be written back in the same thread. */
886                 if (PageDirty(vmpage))
887                         unplug = true;
888
889                 /* We may have one full RPC, commit it soon */
890                 if (plist->pl_nr >= PTLRPC_MAX_BRW_PAGES)
891                         unplug = true;
892
893                 CL_PAGE_DEBUG(D_VFSTRACE, env, page,
894                               "queued page: %d.\n", plist->pl_nr);
895         } else {
896                 cl_page_disown(env, io, page);
897
898                 lcc->lcc_page = NULL;
899                 lu_ref_del(&page->cp_reference, "cl_io", io);
900                 cl_page_put(env, page);
901
902                 /* page list is not contiguous now, commit it now */
903                 unplug = true;
904         }
905         if (unplug || io->u.ci_wr.wr_sync)
906                 result = vvp_io_write_commit(env, io);
907
908         if (result < 0)
909                 io->ci_result = result;
910
911
912 out:
913         RETURN(result >= 0 ? copied : result);
914 }
915
916 #ifdef CONFIG_MIGRATION
917 static int ll_migratepage(struct address_space *mapping,
918                           struct page *newpage, struct page *page
919 #ifdef HAVE_MIGRATEPAGE_4ARGS
920                           , enum migrate_mode mode
921 #endif
922         )
923 {
924         /* Always fail page migration until we have a proper implementation */
925         return -EIO;
926 }
927 #endif
928
929 const struct address_space_operations ll_aops = {
930         .readpage       = ll_readpage,
931         .direct_IO      = ll_direct_IO,
932         .writepage      = ll_writepage,
933         .writepages     = ll_writepages,
934         .set_page_dirty = __set_page_dirty_nobuffers,
935         .write_begin    = ll_write_begin,
936         .write_end      = ll_write_end,
937         .invalidatepage = ll_invalidatepage,
938         .releasepage    = (void *)ll_releasepage,
939 #ifdef CONFIG_MIGRATION
940         .migratepage    = ll_migratepage,
941 #endif
942 };