Whamcloud - gitweb
LU-4977 llite: Fix the deadlock in balance_dirty_pages()
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/lustre/llite/rw26.c
37  *
38  * Lustre Lite I/O page cache routines for the 2.5/2.6 kernel version
39  */
40
41 #include <linux/kernel.h>
42 #include <linux/mm.h>
43 #include <linux/string.h>
44 #include <linux/stat.h>
45 #include <linux/errno.h>
46 #include <linux/unistd.h>
47 #include <asm/uaccess.h>
48
49 #ifdef HAVE_MIGRATE_H
50 #include <linux/migrate.h>
51 #elif defined(HAVE_MIGRATE_MODE_H)
52 #include <linux/migrate_mode.h>
53 #endif
54 #include <linux/fs.h>
55 #include <linux/buffer_head.h>
56 #include <linux/mpage.h>
57 #include <linux/writeback.h>
58 #include <linux/stat.h>
59 #include <asm/uaccess.h>
60 #include <linux/mm.h>
61 #include <linux/pagemap.h>
62
63 #define DEBUG_SUBSYSTEM S_LLITE
64
65 #include <lustre_lite.h>
66 #include "llite_internal.h"
67 #include <linux/lustre_compat25.h>
68
69 /**
70  * Implements Linux VM address_space::invalidatepage() method. This method is
71  * called when the page is truncate from a file, either as a result of
72  * explicit truncate, or when inode is removed from memory (as a result of
73  * final iput(), umount, or memory pressure induced icache shrinking).
74  *
75  * [0, offset] bytes of the page remain valid (this is for a case of not-page
76  * aligned truncate). Lustre leaves partially truncated page in the cache,
77  * relying on struct inode::i_size to limit further accesses.
78  */
79 static void ll_invalidatepage(struct page *vmpage,
80 #ifdef HAVE_INVALIDATE_RANGE
81                                 unsigned int offset, unsigned int length
82 #else
83                                 unsigned long offset
84 #endif
85                              )
86 {
87         struct inode     *inode;
88         struct lu_env    *env;
89         struct cl_page   *page;
90         struct cl_object *obj;
91
92         int refcheck;
93
94         LASSERT(PageLocked(vmpage));
95         LASSERT(!PageWriteback(vmpage));
96
97         /*
98          * It is safe to not check anything in invalidatepage/releasepage
99          * below because they are run with page locked and all our io is
100          * happening with locked page too
101          */
102 #ifdef HAVE_INVALIDATE_RANGE
103         if (offset == 0 && length == PAGE_CACHE_SIZE) {
104 #else
105         if (offset == 0) {
106 #endif
107                 env = cl_env_get(&refcheck);
108                 if (!IS_ERR(env)) {
109                         inode = vmpage->mapping->host;
110                         obj = ll_i2info(inode)->lli_clob;
111                         if (obj != NULL) {
112                                 page = cl_vmpage_page(vmpage, obj);
113                                 if (page != NULL) {
114                                         cl_page_delete(env, page);
115                                         cl_page_put(env, page);
116                                 }
117                         } else
118                                 LASSERT(vmpage->private == 0);
119                         cl_env_put(env, &refcheck);
120                 }
121         }
122 }
123
124 #ifdef HAVE_RELEASEPAGE_WITH_INT
125 #define RELEASEPAGE_ARG_TYPE int
126 #else
127 #define RELEASEPAGE_ARG_TYPE gfp_t
128 #endif
129 static int ll_releasepage(struct page *vmpage, RELEASEPAGE_ARG_TYPE gfp_mask)
130 {
131         struct lu_env           *env;
132         void                    *cookie;
133         struct cl_object        *obj;
134         struct cl_page          *page;
135         struct address_space    *mapping;
136         int result = 0;
137
138         LASSERT(PageLocked(vmpage));
139         if (PageWriteback(vmpage) || PageDirty(vmpage))
140                 return 0;
141
142         mapping = vmpage->mapping;
143         if (mapping == NULL)
144                 return 1;
145
146         obj = ll_i2info(mapping->host)->lli_clob;
147         if (obj == NULL)
148                 return 1;
149
150         /* 1 for caller, 1 for cl_page and 1 for page cache */
151         if (page_count(vmpage) > 3)
152                 return 0;
153
154         page = cl_vmpage_page(vmpage, obj);
155         if (page == NULL)
156                 return 1;
157
158         cookie = cl_env_reenter();
159         env = cl_env_percpu_get();
160         LASSERT(!IS_ERR(env));
161
162         if (!cl_page_in_use(page)) {
163                 result = 1;
164                 cl_page_delete(env, page);
165         }
166
167         /* To use percpu env array, the call path can not be rescheduled;
168          * otherwise percpu array will be messed if ll_releaspage() called
169          * again on the same CPU.
170          *
171          * If this page holds the last refc of cl_object, the following
172          * call path may cause reschedule:
173          *   cl_page_put -> cl_page_free -> cl_object_put ->
174          *     lu_object_put -> lu_object_free -> lov_delete_raid0 ->
175          *     cl_locks_prune.
176          *
177          * However, the kernel can't get rid of this inode until all pages have
178          * been cleaned up. Now that we hold page lock here, it's pretty safe
179          * that we won't get into object delete path.
180          */
181         LASSERT(cl_object_refc(obj) > 1);
182         cl_page_put(env, page);
183
184         cl_env_percpu_put(env);
185         cl_env_reexit(cookie);
186         return result;
187 }
188
189 #define MAX_DIRECTIO_SIZE 2*1024*1024*1024UL
190
191 static inline int ll_get_user_pages(int rw, unsigned long user_addr,
192                                     size_t size, struct page ***pages,
193                                     int *max_pages)
194 {
195         int result = -ENOMEM;
196
197         /* set an arbitrary limit to prevent arithmetic overflow */
198         if (size > MAX_DIRECTIO_SIZE) {
199                 *pages = NULL;
200                 return -EFBIG;
201         }
202
203         *max_pages = (user_addr + size + PAGE_CACHE_SIZE - 1) >>
204                      PAGE_CACHE_SHIFT;
205         *max_pages -= user_addr >> PAGE_CACHE_SHIFT;
206
207         OBD_ALLOC_LARGE(*pages, *max_pages * sizeof(**pages));
208         if (*pages) {
209                 down_read(&current->mm->mmap_sem);
210                 result = get_user_pages(current, current->mm, user_addr,
211                                         *max_pages, (rw == READ), 0, *pages,
212                                         NULL);
213                 up_read(&current->mm->mmap_sem);
214                 if (unlikely(result <= 0))
215                         OBD_FREE_LARGE(*pages, *max_pages * sizeof(**pages));
216         }
217
218         return result;
219 }
220
221 /*  ll_free_user_pages - tear down page struct array
222  *  @pages: array of page struct pointers underlying target buffer */
223 static void ll_free_user_pages(struct page **pages, int npages, int do_dirty)
224 {
225         int i;
226
227         for (i = 0; i < npages; i++) {
228                 if (pages[i] == NULL)
229                         break;
230                 if (do_dirty)
231                         set_page_dirty_lock(pages[i]);
232                 page_cache_release(pages[i]);
233         }
234
235         OBD_FREE_LARGE(pages, npages * sizeof(*pages));
236 }
237
238 ssize_t ll_direct_rw_pages(const struct lu_env *env, struct cl_io *io,
239                            int rw, struct inode *inode,
240                            struct ll_dio_pages *pv)
241 {
242         struct cl_page    *clp;
243         struct cl_2queue  *queue;
244         struct cl_object  *obj = io->ci_obj;
245         int i;
246         ssize_t rc = 0;
247         loff_t file_offset  = pv->ldp_start_offset;
248         long size           = pv->ldp_size;
249         int page_count      = pv->ldp_nr;
250         struct page **pages = pv->ldp_pages;
251         long page_size      = cl_page_size(obj);
252         bool do_io;
253         int  io_pages       = 0;
254         ENTRY;
255
256         queue = &io->ci_queue;
257         cl_2queue_init(queue);
258         for (i = 0; i < page_count; i++) {
259                 if (pv->ldp_offsets)
260                     file_offset = pv->ldp_offsets[i];
261
262                 LASSERT(!(file_offset & (page_size - 1)));
263                 clp = cl_page_find(env, obj, cl_index(obj, file_offset),
264                                    pv->ldp_pages[i], CPT_TRANSIENT);
265                 if (IS_ERR(clp)) {
266                         rc = PTR_ERR(clp);
267                         break;
268                 }
269
270                 rc = cl_page_own(env, io, clp);
271                 if (rc) {
272                         LASSERT(clp->cp_state == CPS_FREEING);
273                         cl_page_put(env, clp);
274                         break;
275                 }
276
277                 do_io = true;
278
279                 /* check the page type: if the page is a host page, then do
280                  * write directly */
281                 if (clp->cp_type == CPT_CACHEABLE) {
282                         struct page *vmpage = cl_page_vmpage(clp);
283                         struct page *src_page;
284                         struct page *dst_page;
285                         void       *src;
286                         void       *dst;
287
288                         src_page = (rw == WRITE) ? pages[i] : vmpage;
289                         dst_page = (rw == WRITE) ? vmpage : pages[i];
290
291                         src = ll_kmap_atomic(src_page, KM_USER0);
292                         dst = ll_kmap_atomic(dst_page, KM_USER1);
293                         memcpy(dst, src, min(page_size, size));
294                         ll_kunmap_atomic(dst, KM_USER1);
295                         ll_kunmap_atomic(src, KM_USER0);
296
297                         /* make sure page will be added to the transfer by
298                          * cl_io_submit()->...->vvp_page_prep_write(). */
299                         if (rw == WRITE)
300                                 set_page_dirty(vmpage);
301
302                         if (rw == READ) {
303                                 /* do not issue the page for read, since it
304                                  * may reread a ra page which has NOT uptodate
305                                  * bit set. */
306                                 cl_page_disown(env, io, clp);
307                                 do_io = false;
308                         }
309                 }
310
311                 if (likely(do_io)) {
312                         cl_2queue_add(queue, clp);
313
314                         /*
315                          * Set page clip to tell transfer formation engine
316                          * that page has to be sent even if it is beyond KMS.
317                          */
318                         cl_page_clip(env, clp, 0, min(size, page_size));
319
320                         ++io_pages;
321                 }
322
323                 /* drop the reference count for cl_page_find */
324                 cl_page_put(env, clp);
325                 size -= page_size;
326                 file_offset += page_size;
327         }
328
329         if (rc == 0 && io_pages) {
330                 rc = cl_io_submit_sync(env, io,
331                                        rw == READ ? CRT_READ : CRT_WRITE,
332                                        queue, 0);
333         }
334         if (rc == 0)
335                 rc = pv->ldp_size;
336
337         cl_2queue_discard(env, io, queue);
338         cl_2queue_disown(env, io, queue);
339         cl_2queue_fini(env, queue);
340         RETURN(rc);
341 }
342 EXPORT_SYMBOL(ll_direct_rw_pages);
343
344 static ssize_t ll_direct_IO_26_seg(const struct lu_env *env, struct cl_io *io,
345                                    int rw, struct inode *inode,
346                                    struct address_space *mapping,
347                                    size_t size, loff_t file_offset,
348                                    struct page **pages, int page_count)
349 {
350     struct ll_dio_pages pvec = { .ldp_pages        = pages,
351                                  .ldp_nr           = page_count,
352                                  .ldp_size         = size,
353                                  .ldp_offsets      = NULL,
354                                  .ldp_start_offset = file_offset
355                                };
356
357     return ll_direct_rw_pages(env, io, rw, inode, &pvec);
358 }
359
360 #ifdef KMALLOC_MAX_SIZE
361 #define MAX_MALLOC KMALLOC_MAX_SIZE
362 #else
363 #define MAX_MALLOC (128 * 1024)
364 #endif
365
366 /* This is the maximum size of a single O_DIRECT request, based on the
367  * kmalloc limit.  We need to fit all of the brw_page structs, each one
368  * representing PAGE_SIZE worth of user data, into a single buffer, and
369  * then truncate this to be a full-sized RPC.  For 4kB PAGE_SIZE this is
370  * up to 22MB for 128kB kmalloc and up to 682MB for 4MB kmalloc. */
371 #define MAX_DIO_SIZE ((MAX_MALLOC / sizeof(struct brw_page) * PAGE_CACHE_SIZE) & \
372                       ~(DT_MAX_BRW_SIZE - 1))
373 static ssize_t ll_direct_IO_26(int rw, struct kiocb *iocb,
374                                const struct iovec *iov, loff_t file_offset,
375                                unsigned long nr_segs)
376 {
377         struct lu_env *env;
378         struct cl_io *io;
379         struct file *file = iocb->ki_filp;
380         struct inode *inode = file->f_mapping->host;
381         struct ccc_object *obj = cl_inode2ccc(inode);
382         long count = iov_length(iov, nr_segs);
383         long tot_bytes = 0, result = 0;
384         struct ll_inode_info *lli = ll_i2info(inode);
385         unsigned long seg = 0;
386         long size = MAX_DIO_SIZE;
387         int refcheck;
388         ENTRY;
389
390         if (!lli->lli_has_smd)
391                 RETURN(-EBADF);
392
393         /* FIXME: io smaller than PAGE_SIZE is broken on ia64 ??? */
394         if ((file_offset & ~CFS_PAGE_MASK) || (count & ~CFS_PAGE_MASK))
395                 RETURN(-EINVAL);
396
397         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), size=%lu (max %lu), "
398                "offset=%lld=%llx, pages %lu (max %lu)\n",
399                PFID(ll_inode2fid(inode)), inode, count, MAX_DIO_SIZE,
400                file_offset, file_offset, count >> PAGE_CACHE_SHIFT,
401                MAX_DIO_SIZE >> PAGE_CACHE_SHIFT);
402
403         /* Check that all user buffers are aligned as well */
404         for (seg = 0; seg < nr_segs; seg++) {
405                 if (((unsigned long)iov[seg].iov_base & ~CFS_PAGE_MASK) ||
406                     (iov[seg].iov_len & ~CFS_PAGE_MASK))
407                         RETURN(-EINVAL);
408         }
409
410         env = cl_env_get(&refcheck);
411         LASSERT(!IS_ERR(env));
412         io = ccc_env_io(env)->cui_cl.cis_io;
413         LASSERT(io != NULL);
414
415         /* 0. Need locking between buffered and direct access. and race with
416          *    size changing by concurrent truncates and writes.
417          * 1. Need inode mutex to operate transient pages.
418          */
419         if (rw == READ)
420                 mutex_lock(&inode->i_mutex);
421
422         LASSERT(obj->cob_transient_pages == 0);
423         for (seg = 0; seg < nr_segs; seg++) {
424                 long iov_left = iov[seg].iov_len;
425                 unsigned long user_addr = (unsigned long)iov[seg].iov_base;
426
427                 if (rw == READ) {
428                         if (file_offset >= i_size_read(inode))
429                                 break;
430                         if (file_offset + iov_left > i_size_read(inode))
431                                 iov_left = i_size_read(inode) - file_offset;
432                 }
433
434                 while (iov_left > 0) {
435                         struct page **pages;
436                         int page_count, max_pages = 0;
437                         long bytes;
438
439                         bytes = min(size, iov_left);
440                         page_count = ll_get_user_pages(rw, user_addr, bytes,
441                                                        &pages, &max_pages);
442                         if (likely(page_count > 0)) {
443                                 if (unlikely(page_count <  max_pages))
444                                         bytes = page_count << PAGE_CACHE_SHIFT;
445                                 result = ll_direct_IO_26_seg(env, io, rw, inode,
446                                                              file->f_mapping,
447                                                              bytes, file_offset,
448                                                              pages, page_count);
449                                 ll_free_user_pages(pages, max_pages, rw==READ);
450                         } else if (page_count == 0) {
451                                 GOTO(out, result = -EFAULT);
452                         } else {
453                                 result = page_count;
454                         }
455                         if (unlikely(result <= 0)) {
456                                 /* If we can't allocate a large enough buffer
457                                  * for the request, shrink it to a smaller
458                                  * PAGE_SIZE multiple and try again.
459                                  * We should always be able to kmalloc for a
460                                  * page worth of page pointers = 4MB on i386. */
461                                 if (result == -ENOMEM &&
462                                     size > (PAGE_CACHE_SIZE / sizeof(*pages)) *
463                                            PAGE_CACHE_SIZE) {
464                                         size = ((((size / 2) - 1) |
465                                                  ~CFS_PAGE_MASK) + 1) &
466                                                 CFS_PAGE_MASK;
467                                         CDEBUG(D_VFSTRACE,"DIO size now %lu\n",
468                                                size);
469                                         continue;
470                                 }
471
472                                 GOTO(out, result);
473                         }
474
475                         tot_bytes += result;
476                         file_offset += result;
477                         iov_left -= result;
478                         user_addr += result;
479                 }
480         }
481 out:
482         LASSERT(obj->cob_transient_pages == 0);
483         if (rw == READ)
484                 mutex_unlock(&inode->i_mutex);
485
486         if (tot_bytes > 0) {
487                 struct ccc_io *cio = ccc_env_io(env);
488
489                 /* no commit async for direct IO */
490                 cio->u.write.cui_written += tot_bytes;
491         }
492
493         cl_env_put(env, &refcheck);
494         RETURN(tot_bytes ? tot_bytes : result);
495 }
496
497 /**
498  * Prepare partially written-to page for a write.
499  */
500 static int ll_prepare_partial_page(const struct lu_env *env, struct cl_io *io,
501                                    struct cl_page *pg)
502 {
503         struct cl_attr *attr   = ccc_env_thread_attr(env);
504         struct cl_object *obj  = io->ci_obj;
505         struct ccc_page *cp    = cl_object_page_slice(obj, pg);
506         loff_t          offset = cl_offset(obj, ccc_index(cp));
507         int             result;
508
509         cl_object_attr_lock(obj);
510         result = cl_object_attr_get(env, obj, attr);
511         cl_object_attr_unlock(obj);
512         if (result == 0) {
513                 /*
514                  * If are writing to a new page, no need to read old data.
515                  * The extent locking will have updated the KMS, and for our
516                  * purposes here we can treat it like i_size.
517                  */
518                 if (attr->cat_kms <= offset) {
519                         char *kaddr = ll_kmap_atomic(cp->cpg_page, KM_USER0);
520
521                         memset(kaddr, 0, cl_page_size(obj));
522                         ll_kunmap_atomic(kaddr, KM_USER0);
523                 } else if (cp->cpg_defer_uptodate)
524                         cp->cpg_ra_used = 1;
525                 else
526                         result = ll_page_sync_io(env, io, pg, CRT_READ);
527         }
528         return result;
529 }
530
531 static int ll_write_begin(struct file *file, struct address_space *mapping,
532                           loff_t pos, unsigned len, unsigned flags,
533                           struct page **pagep, void **fsdata)
534 {
535         struct ll_cl_context *lcc;
536         struct lu_env  *env;
537         struct cl_io   *io;
538         struct cl_page *page;
539
540         struct cl_object *clob = ll_i2info(mapping->host)->lli_clob;
541         pgoff_t index = pos >> PAGE_CACHE_SHIFT;
542         struct page *vmpage = NULL;
543         unsigned from = pos & (PAGE_CACHE_SIZE - 1);
544         unsigned to = from + len;
545         int result = 0;
546         ENTRY;
547
548         CDEBUG(D_VFSTRACE, "Writing %lu of %d to %d bytes\n", index, from, len);
549
550         lcc = ll_cl_init(file, NULL);
551         if (IS_ERR(lcc))
552                 GOTO(out, result = PTR_ERR(lcc));
553
554         env = lcc->lcc_env;
555         io  = lcc->lcc_io;
556
557         /* To avoid deadlock, try to lock page first. */
558         vmpage = grab_cache_page_nowait(mapping, index);
559         if (unlikely(vmpage == NULL ||
560                      PageDirty(vmpage) || PageWriteback(vmpage))) {
561                 struct ccc_io *cio = ccc_env_io(env);
562                 struct cl_page_list *plist = &cio->u.write.cui_queue;
563
564                 /* if the page is already in dirty cache, we have to commit
565                  * the pages right now; otherwise, it may cause deadlock
566                  * because it holds page lock of a dirty page and request for
567                  * more grants. It's okay for the dirty page to be the first
568                  * one in commit page list, though. */
569                 if (vmpage != NULL && plist->pl_nr > 0) {
570                         unlock_page(vmpage);
571                         page_cache_release(vmpage);
572                         vmpage = NULL;
573                 }
574
575                 /* commit pages and then wait for page lock */
576                 result = vvp_io_write_commit(env, io);
577                 if (result < 0)
578                         GOTO(out, result);
579
580                 if (vmpage == NULL) {
581                         vmpage = grab_cache_page_write_begin(mapping, index,
582                                                              flags);
583                         if (vmpage == NULL)
584                                 GOTO(out, result = -ENOMEM);
585                 }
586         }
587
588         page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
589         if (IS_ERR(page))
590                 GOTO(out, result = PTR_ERR(page));
591
592         lcc->lcc_page = page;
593         lu_ref_add(&page->cp_reference, "cl_io", io);
594
595         cl_page_assume(env, io, page);
596         if (!PageUptodate(vmpage)) {
597                 /*
598                  * We're completely overwriting an existing page,
599                  * so _don't_ set it up to date until commit_write
600                  */
601                 if (from == 0 && to == PAGE_SIZE) {
602                         CL_PAGE_HEADER(D_PAGE, env, page, "full page write\n");
603                         POISON_PAGE(vmpage, 0x11);
604                 } else {
605                         /* TODO: can be optimized at OSC layer to check if it
606                          * is a lockless IO. In that case, it's not necessary
607                          * to read the data. */
608                         result = ll_prepare_partial_page(env, io, page);
609                         if (result == 0)
610                                 SetPageUptodate(vmpage);
611                 }
612         }
613         if (result < 0)
614                 cl_page_unassume(env, io, page);
615         EXIT;
616 out:
617         if (result < 0) {
618                 if (vmpage != NULL) {
619                         unlock_page(vmpage);
620                         page_cache_release(vmpage);
621                 }
622                 if (!IS_ERR(lcc))
623                         ll_cl_fini(lcc);
624         } else {
625                 *pagep = vmpage;
626                 *fsdata = lcc;
627         }
628         RETURN(result);
629 }
630
631 static int ll_write_end(struct file *file, struct address_space *mapping,
632                         loff_t pos, unsigned len, unsigned copied,
633                         struct page *vmpage, void *fsdata)
634 {
635         struct ll_cl_context *lcc = fsdata;
636         struct lu_env *env;
637         struct cl_io *io;
638         struct ccc_io *cio;
639         struct cl_page *page;
640         unsigned from = pos & (PAGE_CACHE_SIZE - 1);
641         bool unplug = false;
642         int result = 0;
643         ENTRY;
644
645         page_cache_release(vmpage);
646
647         LASSERT(lcc != NULL);
648         env  = lcc->lcc_env;
649         page = lcc->lcc_page;
650         io   = lcc->lcc_io;
651         cio  = ccc_env_io(env);
652
653         LASSERT(cl_page_is_owned(page, io));
654         if (copied > 0) {
655                 struct cl_page_list *plist = &cio->u.write.cui_queue;
656
657                 lcc->lcc_page = NULL; /* page will be queued */
658
659                 /* Add it into write queue */
660                 cl_page_list_add(plist, page);
661                 if (plist->pl_nr == 1) /* first page */
662                         cio->u.write.cui_from = from;
663                 else
664                         LASSERT(from == 0);
665                 cio->u.write.cui_to = from + copied;
666
667                 /* To address the deadlock in balance_dirty_pages() where
668                  * this dirty page may be written back in the same thread. */
669                 if (PageDirty(vmpage))
670                         unplug = true;
671
672                 /* We may have one full RPC, commit it soon */
673                 if (plist->pl_nr >= PTLRPC_MAX_BRW_PAGES)
674                         unplug = true;
675
676                 CL_PAGE_DEBUG(D_VFSTRACE, env, page,
677                               "queued page: %d.\n", plist->pl_nr);
678         } else {
679                 cl_page_disown(env, io, page);
680
681                 /* page list is not contiguous now, commit it now */
682                 unplug = true;
683         }
684
685         if (unplug ||
686             file->f_flags & O_SYNC || IS_SYNC(file->f_dentry->d_inode))
687                 result = vvp_io_write_commit(env, io);
688
689         ll_cl_fini(lcc);
690         RETURN(result >= 0 ? copied : result);
691 }
692
693 #ifdef CONFIG_MIGRATION
694 static int ll_migratepage(struct address_space *mapping,
695                           struct page *newpage, struct page *page
696 #ifdef HAVE_MIGRATEPAGE_4ARGS
697                           , enum migrate_mode mode
698 #endif
699         )
700 {
701         /* Always fail page migration until we have a proper implementation */
702         return -EIO;
703 }
704 #endif
705
706 #ifndef MS_HAS_NEW_AOPS
707 const struct address_space_operations ll_aops = {
708         .readpage       = ll_readpage,
709         .direct_IO      = ll_direct_IO_26,
710         .writepage      = ll_writepage,
711         .writepages     = ll_writepages,
712         .set_page_dirty = __set_page_dirty_nobuffers,
713         .write_begin    = ll_write_begin,
714         .write_end      = ll_write_end,
715         .invalidatepage = ll_invalidatepage,
716         .releasepage    = (void *)ll_releasepage,
717 #ifdef CONFIG_MIGRATION
718         .migratepage    = ll_migratepage,
719 #endif
720 };
721 #else
722 const struct address_space_operations_ext ll_aops = {
723         .orig_aops.readpage             = ll_readpage,
724         .orig_aops.direct_IO            = ll_direct_IO_26,
725         .orig_aops.writepage            = ll_writepage,
726         .orig_aops.writepages           = ll_writepages,
727         .orig_aops.set_page_dirty       = __set_page_dirty_nobuffers,
728         .orig_aops.invalidatepage       = ll_invalidatepage,
729         .orig_aops.releasepage          = ll_releasepage,
730 #ifdef CONFIG_MIGRATION
731         .orig_aops.migratepage          = ll_migratepage,
732 #endif
733         .write_begin                    = ll_write_begin,
734         .write_end                      = ll_write_end
735 };
736 #endif