4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * Implementation of cl_page for VVP layer.
38 * Author: Nikita Danilov <nikita.danilov@sun.com>
39 * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
42 #define DEBUG_SUBSYSTEM S_LLITE
46 #include "vvp_internal.h"
48 /*****************************************************************************
54 static void vvp_page_fini_common(struct ccc_page *cp)
56 struct page *vmpage = cp->cpg_page;
58 LASSERT(vmpage != NULL);
59 page_cache_release(vmpage);
62 static void vvp_page_fini(const struct lu_env *env,
63 struct cl_page_slice *slice)
65 struct ccc_page *cp = cl2ccc_page(slice);
66 struct page *vmpage = cp->cpg_page;
69 * vmpage->private was already cleared when page was moved into
72 LASSERT((struct cl_page *)vmpage->private != slice->cpl_page);
73 vvp_page_fini_common(cp);
76 static int vvp_page_own(const struct lu_env *env,
77 const struct cl_page_slice *slice, struct cl_io *io,
80 struct ccc_page *vpg = cl2ccc_page(slice);
81 struct page *vmpage = vpg->cpg_page;
83 LASSERT(vmpage != NULL);
85 if (!trylock_page(vmpage))
88 if (unlikely(PageWriteback(vmpage))) {
97 wait_on_page_writeback(vmpage);
101 static void vvp_page_assume(const struct lu_env *env,
102 const struct cl_page_slice *slice,
103 struct cl_io *unused)
105 struct page *vmpage = cl2vm_page(slice);
107 LASSERT(vmpage != NULL);
108 LASSERT(PageLocked(vmpage));
109 wait_on_page_writeback(vmpage);
112 static void vvp_page_unassume(const struct lu_env *env,
113 const struct cl_page_slice *slice,
114 struct cl_io *unused)
116 struct page *vmpage = cl2vm_page(slice);
118 LASSERT(vmpage != NULL);
119 LASSERT(PageLocked(vmpage));
122 static void vvp_page_disown(const struct lu_env *env,
123 const struct cl_page_slice *slice, struct cl_io *io)
125 struct page *vmpage = cl2vm_page(slice);
127 LASSERT(vmpage != NULL);
128 LASSERT(PageLocked(vmpage));
130 unlock_page(cl2vm_page(slice));
133 static void vvp_page_discard(const struct lu_env *env,
134 const struct cl_page_slice *slice,
135 struct cl_io *unused)
137 struct page *vmpage = cl2vm_page(slice);
138 struct ccc_page *cpg = cl2ccc_page(slice);
140 LASSERT(vmpage != NULL);
141 LASSERT(PageLocked(vmpage));
143 if (cpg->cpg_defer_uptodate && !cpg->cpg_ra_used)
144 ll_ra_stats_inc(vmpage->mapping->host, RA_STAT_DISCARDED);
146 ll_invalidate_page(vmpage);
149 static void vvp_page_delete(const struct lu_env *env,
150 const struct cl_page_slice *slice)
152 struct page *vmpage = cl2vm_page(slice);
153 struct inode *inode = vmpage->mapping->host;
154 struct cl_object *obj = slice->cpl_obj;
155 struct cl_page *page = slice->cpl_page;
158 LASSERT(PageLocked(vmpage));
159 LASSERT((struct cl_page *)vmpage->private == page);
160 LASSERT(inode == ccc_object_inode(obj));
162 vvp_write_complete(cl2ccc(obj), cl2ccc_page(slice));
164 /* Drop the reference count held in vvp_page_init */
165 refc = atomic_dec_return(&page->cp_ref);
166 LASSERTF(refc >= 1, "page = %p, refc = %d\n", page, refc);
168 ClearPageUptodate(vmpage);
169 ClearPagePrivate(vmpage);
172 * Reference from vmpage to cl_page is removed, but the reference back
173 * is still here. It is removed later in vvp_page_fini().
177 static void vvp_page_export(const struct lu_env *env,
178 const struct cl_page_slice *slice,
181 struct page *vmpage = cl2vm_page(slice);
183 LASSERT(vmpage != NULL);
184 LASSERT(PageLocked(vmpage));
186 SetPageUptodate(vmpage);
188 ClearPageUptodate(vmpage);
191 static int vvp_page_is_vmlocked(const struct lu_env *env,
192 const struct cl_page_slice *slice)
194 return PageLocked(cl2vm_page(slice)) ? -EBUSY : -ENODATA;
197 static int vvp_page_prep_read(const struct lu_env *env,
198 const struct cl_page_slice *slice,
199 struct cl_io *unused)
202 /* Skip the page already marked as PG_uptodate. */
203 RETURN(PageUptodate(cl2vm_page(slice)) ? -EALREADY : 0);
206 static int vvp_page_prep_write(const struct lu_env *env,
207 const struct cl_page_slice *slice,
208 struct cl_io *unused)
210 struct page *vmpage = cl2vm_page(slice);
212 LASSERT(PageLocked(vmpage));
213 LASSERT(!PageDirty(vmpage));
215 set_page_writeback(vmpage);
216 vvp_write_pending(cl2ccc(slice->cpl_obj), cl2ccc_page(slice));
222 * Handles page transfer errors at VM level.
224 * This takes inode as a separate argument, because inode on which error is to
225 * be set can be different from \a vmpage inode in case of direct-io.
227 static void vvp_vmpage_error(struct inode *inode, struct page *vmpage, int ioret)
229 struct ccc_object *obj = cl_inode2ccc(inode);
232 ClearPageError(vmpage);
233 obj->cob_discard_page_warned = 0;
235 SetPageError(vmpage);
236 if (ioret == -ENOSPC)
237 set_bit(AS_ENOSPC, &inode->i_mapping->flags);
239 set_bit(AS_EIO, &inode->i_mapping->flags);
241 if ((ioret == -ESHUTDOWN || ioret == -EINTR) &&
242 obj->cob_discard_page_warned == 0) {
243 obj->cob_discard_page_warned = 1;
244 ll_dirty_page_discard_warn(vmpage, ioret);
249 static void vvp_page_completion_read(const struct lu_env *env,
250 const struct cl_page_slice *slice,
253 struct ccc_page *cp = cl2ccc_page(slice);
254 struct page *vmpage = cp->cpg_page;
255 struct cl_page *page = slice->cpl_page;
256 struct inode *inode = ccc_object_inode(page->cp_obj);
259 LASSERT(PageLocked(vmpage));
260 CL_PAGE_HEADER(D_PAGE, env, page, "completing READ with %d\n", ioret);
262 if (cp->cpg_defer_uptodate)
263 ll_ra_count_put(ll_i2sbi(inode), 1);
266 if (!cp->cpg_defer_uptodate)
267 cl_page_export(env, page, 1);
269 cp->cpg_defer_uptodate = 0;
271 if (page->cp_sync_io == NULL)
277 static void vvp_page_completion_write(const struct lu_env *env,
278 const struct cl_page_slice *slice,
281 struct ccc_page *cp = cl2ccc_page(slice);
282 struct cl_page *pg = slice->cpl_page;
283 struct page *vmpage = cp->cpg_page;
286 LASSERT(ergo(pg->cp_sync_io != NULL, PageLocked(vmpage)));
287 LASSERT(PageWriteback(vmpage));
289 CL_PAGE_HEADER(D_PAGE, env, pg, "completing WRITE with %d\n", ioret);
292 * TODO: Actually it makes sense to add the page into oap pending
293 * list again and so that we don't need to take the page out from
294 * SoM write pending list, if we just meet a recoverable error,
296 * To implement this, we just need to return a non zero value in
297 * ->cpo_completion method. The underlying transfer should be notified
298 * and then re-add the page into pending transfer queue. -jay
301 cp->cpg_write_queued = 0;
302 vvp_write_complete(cl2ccc(slice->cpl_obj), cp);
305 * Only mark the page error only when it's an async write because
306 * applications won't wait for IO to finish.
308 if (pg->cp_sync_io == NULL)
309 vvp_vmpage_error(ccc_object_inode(pg->cp_obj), vmpage, ioret);
311 end_page_writeback(vmpage);
316 * Implements cl_page_operations::cpo_make_ready() method.
318 * This is called to yank a page from the transfer cache and to send it out as
319 * a part of transfer. This function try-locks the page. If try-lock failed,
320 * page is owned by some concurrent IO, and should be skipped (this is bad,
321 * but hopefully rare situation, as it usually results in transfer being
322 * shorter than possible).
324 * \retval 0 success, page can be placed into transfer
326 * \retval -EAGAIN page is either used by concurrent IO has been
327 * truncated. Skip it.
329 static int vvp_page_make_ready(const struct lu_env *env,
330 const struct cl_page_slice *slice)
332 struct page *vmpage = cl2vm_page(slice);
333 struct cl_page *pg = slice->cpl_page;
337 if (clear_page_dirty_for_io(vmpage)) {
338 LASSERT(pg->cp_state == CPS_CACHED);
339 /* This actually clears the dirty bit in the radix
341 set_page_writeback(vmpage);
342 vvp_write_pending(cl2ccc(slice->cpl_obj),
344 CL_PAGE_HEADER(D_PAGE, env, pg, "readied\n");
345 } else if (pg->cp_state == CPS_PAGEOUT) {
346 /* is it possible for osc_flush_async_page() to already
350 CL_PAGE_DEBUG(D_ERROR, env, pg, "Unexpecting page state %d.\n",
358 static int vvp_page_is_under_lock(const struct lu_env *env,
359 const struct cl_page_slice *slice,
360 struct cl_io *io, pgoff_t *max_index)
364 if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
365 io->ci_type == CIT_FAULT) {
366 struct ccc_io *cio = ccc_env_io(env);
368 if (unlikely(cio->cui_fd->fd_flags & LL_FILE_GROUP_LOCKED))
369 *max_index = CL_PAGE_EOF;
375 static int vvp_page_print(const struct lu_env *env,
376 const struct cl_page_slice *slice,
377 void *cookie, lu_printer_t printer)
379 struct ccc_page *vp = cl2ccc_page(slice);
380 struct page *vmpage = vp->cpg_page;
382 (*printer)(env, cookie, LUSTRE_VVP_NAME"-page@%p(%d:%d:%d) "
384 vp, vp->cpg_defer_uptodate, vp->cpg_ra_used,
385 vp->cpg_write_queued, vmpage);
386 if (vmpage != NULL) {
387 (*printer)(env, cookie, "%lx %d:%d %lx %lu %slru",
388 (long)vmpage->flags, page_count(vmpage),
389 page_mapcount(vmpage), vmpage->private,
391 list_empty(&vmpage->lru) ? "not-" : "");
393 (*printer)(env, cookie, "\n");
397 static const struct cl_page_operations vvp_page_ops = {
398 .cpo_own = vvp_page_own,
399 .cpo_assume = vvp_page_assume,
400 .cpo_unassume = vvp_page_unassume,
401 .cpo_disown = vvp_page_disown,
402 .cpo_discard = vvp_page_discard,
403 .cpo_delete = vvp_page_delete,
404 .cpo_export = vvp_page_export,
405 .cpo_is_vmlocked = vvp_page_is_vmlocked,
406 .cpo_fini = vvp_page_fini,
407 .cpo_print = vvp_page_print,
408 .cpo_is_under_lock = vvp_page_is_under_lock,
411 .cpo_prep = vvp_page_prep_read,
412 .cpo_completion = vvp_page_completion_read,
413 .cpo_make_ready = ccc_fail,
416 .cpo_prep = vvp_page_prep_write,
417 .cpo_completion = vvp_page_completion_write,
418 .cpo_make_ready = vvp_page_make_ready,
423 static void vvp_transient_page_verify(const struct cl_page *page)
427 static int vvp_transient_page_own(const struct lu_env *env,
428 const struct cl_page_slice *slice,
429 struct cl_io *unused, int nonblock)
431 vvp_transient_page_verify(slice->cpl_page);
435 static void vvp_transient_page_assume(const struct lu_env *env,
436 const struct cl_page_slice *slice,
437 struct cl_io *unused)
439 vvp_transient_page_verify(slice->cpl_page);
442 static void vvp_transient_page_unassume(const struct lu_env *env,
443 const struct cl_page_slice *slice,
444 struct cl_io *unused)
446 vvp_transient_page_verify(slice->cpl_page);
449 static void vvp_transient_page_disown(const struct lu_env *env,
450 const struct cl_page_slice *slice,
451 struct cl_io *unused)
453 vvp_transient_page_verify(slice->cpl_page);
456 static void vvp_transient_page_discard(const struct lu_env *env,
457 const struct cl_page_slice *slice,
458 struct cl_io *unused)
460 struct cl_page *page = slice->cpl_page;
462 vvp_transient_page_verify(slice->cpl_page);
465 * For transient pages, remove it from the radix tree.
467 cl_page_delete(env, page);
470 static int vvp_transient_page_is_vmlocked(const struct lu_env *env,
471 const struct cl_page_slice *slice)
473 struct inode *inode = ccc_object_inode(slice->cpl_obj);
476 locked = !mutex_trylock(&inode->i_mutex);
478 mutex_unlock(&inode->i_mutex);
479 return locked ? -EBUSY : -ENODATA;
483 vvp_transient_page_completion(const struct lu_env *env,
484 const struct cl_page_slice *slice,
487 vvp_transient_page_verify(slice->cpl_page);
490 static void vvp_transient_page_fini(const struct lu_env *env,
491 struct cl_page_slice *slice)
493 struct ccc_page *cp = cl2ccc_page(slice);
494 struct cl_page *clp = slice->cpl_page;
495 struct ccc_object *clobj = cl2ccc(clp->cp_obj);
497 vvp_page_fini_common(cp);
498 atomic_dec(&clobj->cob_transient_pages);
501 static const struct cl_page_operations vvp_transient_page_ops = {
502 .cpo_own = vvp_transient_page_own,
503 .cpo_assume = vvp_transient_page_assume,
504 .cpo_unassume = vvp_transient_page_unassume,
505 .cpo_disown = vvp_transient_page_disown,
506 .cpo_discard = vvp_transient_page_discard,
507 .cpo_fini = vvp_transient_page_fini,
508 .cpo_is_vmlocked = vvp_transient_page_is_vmlocked,
509 .cpo_print = vvp_page_print,
510 .cpo_is_under_lock = vvp_page_is_under_lock,
513 .cpo_prep = ccc_transient_page_prep,
514 .cpo_completion = vvp_transient_page_completion,
517 .cpo_prep = ccc_transient_page_prep,
518 .cpo_completion = vvp_transient_page_completion,
523 int vvp_page_init(const struct lu_env *env, struct cl_object *obj,
524 struct cl_page *page, pgoff_t index)
526 struct ccc_page *cpg = cl_object_page_slice(obj, page);
527 struct page *vmpage = page->cp_vmpage;
529 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
531 cpg->cpg_page = vmpage;
532 page_cache_get(vmpage);
534 INIT_LIST_HEAD(&cpg->cpg_pending_linkage);
535 if (page->cp_type == CPT_CACHEABLE) {
536 /* in cache, decref in vvp_page_delete */
537 atomic_inc(&page->cp_ref);
538 SetPagePrivate(vmpage);
539 vmpage->private = (unsigned long)page;
540 cl_page_slice_add(page, &cpg->cpg_cl, obj, index,
543 struct ccc_object *clobj = cl2ccc(obj);
545 cl_page_slice_add(page, &cpg->cpg_cl, obj, index,
546 &vvp_transient_page_ops);
547 atomic_inc(&clobj->cob_transient_pages);