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