1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
30 * Use is subject to license terms.
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>
41 #define DEBUG_SUBSYSTEM S_LLITE
44 # error This file is kernel only.
48 #include <lustre_lite.h>
50 #include "vvp_internal.h"
52 /*****************************************************************************
58 static void vvp_page_fini_common(struct ccc_page *cp)
60 cfs_page_t *vmpage = cp->cpg_page;
62 LASSERT(vmpage != NULL);
63 page_cache_release(vmpage);
64 OBD_SLAB_FREE_PTR(cp, vvp_page_kmem);
67 static void vvp_page_fini(const struct lu_env *env,
68 struct cl_page_slice *slice)
70 struct ccc_page *cp = cl2ccc_page(slice);
71 cfs_page_t *vmpage = cp->cpg_page;
74 * vmpage->private was already cleared when page was moved into
77 LASSERT((struct cl_page *)vmpage->private != slice->cpl_page);
78 vvp_page_fini_common(cp);
81 static int vvp_page_own(const struct lu_env *env,
82 const struct cl_page_slice *slice, struct cl_io *io,
85 struct ccc_page *vpg = cl2ccc_page(slice);
86 cfs_page_t *vmpage = vpg->cpg_page;
89 LASSERT(vmpage != NULL);
92 if (TestSetPageLocked(vmpage))
95 if (unlikely(PageWriteback(vmpage))) {
96 /* Something gets wrong? */
104 /* DEBUG CODE FOR #18881 */
105 while (TestSetPageLocked(vmpage)) {
106 cfs_schedule_timeout(CFS_TASK_INTERRUPTIBLE,
107 cfs_time_seconds(1)/10);
108 if (++count > 1200) {
109 CL_PAGE_DEBUG(D_ERROR, env,
110 cl_page_top(slice->cpl_page),
111 "XXX page %p blocked on acquiring the"
112 " lock. process %s/%p, flags %lx,io %p\n",
113 vmpage, current->comm, current,
115 libcfs_debug_dumpstack(NULL);
116 if (slice->cpl_page->cp_task) {
117 cfs_task_t *tsk = slice->cpl_page->cp_task;
118 LCONSOLE_WARN("The page was owned by %s\n",
120 libcfs_debug_dumpstack(tsk);
122 LCONSOLE_WARN("Reproduced bug #18881,please contact:"
123 "jay <jinshan.xiong@sun.com>, thanks\n");
131 /* lock_page(vmpage); */
132 wait_on_page_writeback(vmpage);
136 static void vvp_page_assume(const struct lu_env *env,
137 const struct cl_page_slice *slice,
138 struct cl_io *unused)
140 cfs_page_t *vmpage = cl2vm_page(slice);
142 LASSERT(vmpage != NULL);
143 LASSERT(PageLocked(vmpage));
144 wait_on_page_writeback(vmpage);
147 static void vvp_page_unassume(const struct lu_env *env,
148 const struct cl_page_slice *slice,
149 struct cl_io *unused)
151 cfs_page_t *vmpage = cl2vm_page(slice);
153 LASSERT(vmpage != NULL);
154 LASSERT(PageLocked(vmpage));
157 static void vvp_page_disown(const struct lu_env *env,
158 const struct cl_page_slice *slice, struct cl_io *io)
160 cfs_page_t *vmpage = cl2vm_page(slice);
162 LASSERT(vmpage != NULL);
163 LASSERT(PageLocked(vmpage));
165 unlock_page(cl2vm_page(slice));
168 static void vvp_page_discard(const struct lu_env *env,
169 const struct cl_page_slice *slice,
170 struct cl_io *unused)
172 cfs_page_t *vmpage = cl2vm_page(slice);
173 struct address_space *mapping = vmpage->mapping;
174 struct ccc_page *cpg = cl2ccc_page(slice);
176 LASSERT(vmpage != NULL);
177 LASSERT(PageLocked(vmpage));
179 if (cpg->cpg_defer_uptodate && !cpg->cpg_ra_used)
180 ll_ra_stats_inc(mapping, RA_STAT_DISCARDED);
183 * truncate_complete_page() calls
184 * a_ops->invalidatepage()->cl_page_delete()->vvp_page_delete().
186 truncate_complete_page(mapping, vmpage);
189 static int vvp_page_unmap(const struct lu_env *env,
190 const struct cl_page_slice *slice,
191 struct cl_io *unused)
193 cfs_page_t *vmpage = cl2vm_page(slice);
194 __u64 offset = vmpage->index << CFS_PAGE_SHIFT;
196 LASSERT(vmpage != NULL);
197 LASSERT(PageLocked(vmpage));
199 * XXX is it safe to call this with the page lock held?
201 ll_teardown_mmaps(vmpage->mapping, offset, offset + CFS_PAGE_SIZE);
205 static void vvp_page_delete(const struct lu_env *env,
206 const struct cl_page_slice *slice)
208 cfs_page_t *vmpage = cl2vm_page(slice);
209 struct inode *inode = vmpage->mapping->host;
210 struct cl_object *obj = slice->cpl_obj;
212 LASSERT(PageLocked(vmpage));
213 LASSERT((struct cl_page *)vmpage->private == slice->cpl_page);
214 LASSERT(inode == ccc_object_inode(obj));
216 vvp_write_complete(cl2ccc(obj), cl2ccc_page(slice));
217 ClearPagePrivate(vmpage);
220 * Reference from vmpage to cl_page is removed, but the reference back
221 * is still here. It is removed later in vvp_page_fini().
225 static void vvp_page_export(const struct lu_env *env,
226 const struct cl_page_slice *slice,
229 cfs_page_t *vmpage = cl2vm_page(slice);
231 LASSERT(vmpage != NULL);
232 LASSERT(PageLocked(vmpage));
234 SetPageUptodate(vmpage);
236 ClearPageUptodate(vmpage);
239 static int vvp_page_is_vmlocked(const struct lu_env *env,
240 const struct cl_page_slice *slice)
242 return PageLocked(cl2vm_page(slice)) ? -EBUSY : -ENODATA;
245 static int vvp_page_prep_read(const struct lu_env *env,
246 const struct cl_page_slice *slice,
247 struct cl_io *unused)
250 /* Skip the page already marked as PG_uptodate. */
251 RETURN(PageUptodate(cl2vm_page(slice)) ? -EALREADY : 0);
254 static int vvp_page_prep_write(const struct lu_env *env,
255 const struct cl_page_slice *slice,
256 struct cl_io *unused)
258 cfs_page_t *vmpage = cl2vm_page(slice);
261 if (clear_page_dirty_for_io(vmpage)) {
262 set_page_writeback(vmpage);
263 vvp_write_pending(cl2ccc(slice->cpl_obj), cl2ccc_page(slice));
271 * Handles page transfer errors at VM level.
273 * This takes inode as a separate argument, because inode on which error is to
274 * be set can be different from \a vmpage inode in case of direct-io.
276 static void vvp_vmpage_error(struct inode *inode, cfs_page_t *vmpage, int ioret)
279 ClearPageError(vmpage);
280 else if (ioret != -EINTR) {
281 SetPageError(vmpage);
282 if (ioret == -ENOSPC)
283 set_bit(AS_ENOSPC, &inode->i_mapping->flags);
285 set_bit(AS_EIO, &inode->i_mapping->flags);
289 static void vvp_page_completion_common(const struct lu_env *env,
290 struct ccc_page *cp, int ioret)
292 struct cl_page *clp = cp->cpg_cl.cpl_page;
293 cfs_page_t *vmpage = cp->cpg_page;
294 struct inode *inode = ccc_object_inode(clp->cp_obj);
296 LINVRNT(cl_page_is_vmlocked(env, clp));
298 if (!clp->cp_sync_io && clp->cp_type == CPT_CACHEABLE) {
300 * Only mark the page error only when it's a cacheable page
303 * For sync IO and direct IO(CPT_TRANSIENT), the error is able
304 * to be seen by application, so we don't need to mark a page
307 vvp_vmpage_error(inode, vmpage, ioret);
312 static void vvp_page_completion_read(const struct lu_env *env,
313 const struct cl_page_slice *slice,
316 struct ccc_page *cp = cl2ccc_page(slice);
317 struct cl_page *page = cl_page_top(slice->cpl_page);
318 struct inode *inode = ccc_object_inode(page->cp_obj);
321 CL_PAGE_HEADER(D_PAGE, env, page, "completing READ with %d\n", ioret);
323 if (cp->cpg_defer_uptodate)
324 ll_ra_count_put(ll_i2sbi(inode), 1);
327 /* XXX: do we need this for transient pages? */
328 if (!cp->cpg_defer_uptodate)
329 cl_page_export(env, page, 1);
331 cp->cpg_defer_uptodate = 0;
332 vvp_page_completion_common(env, cp, ioret);
337 static void vvp_page_completion_write_common(const struct lu_env *env,
338 const struct cl_page_slice *slice,
341 struct ccc_page *cp = cl2ccc_page(slice);
344 * TODO: Actually it makes sense to add the page into oap pending
345 * list again and so that we don't need to take the page out from
346 * SoM write pending list, if we just meet a recoverable error,
348 * To implement this, we just need to return a non zero value in
349 * ->cpo_completion method. The underlying transfer should be notified
350 * and then re-add the page into pending transfer queue. -jay
352 cp->cpg_write_queued = 0;
353 vvp_write_complete(cl2ccc(slice->cpl_obj), cp);
355 vvp_page_completion_common(env, cp, ioret);
358 static void vvp_page_completion_write(const struct lu_env *env,
359 const struct cl_page_slice *slice,
362 struct ccc_page *cp = cl2ccc_page(slice);
363 struct cl_page *pg = slice->cpl_page;
364 cfs_page_t *vmpage = cp->cpg_page;
368 LINVRNT(cl_page_is_vmlocked(env, pg));
369 LASSERT(PageWriteback(vmpage));
371 CL_PAGE_HEADER(D_PAGE, env, pg, "completing WRITE with %d\n", ioret);
373 vvp_page_completion_write_common(env, slice, ioret);
374 end_page_writeback(vmpage);
379 * Implements cl_page_operations::cpo_make_ready() method.
381 * This is called to yank a page from the transfer cache and to send it out as
382 * a part of transfer. This function try-locks the page. If try-lock failed,
383 * page is owned by some concurrent IO, and should be skipped (this is bad,
384 * but hopefully rare situation, as it usually results in transfer being
385 * shorter than possible).
387 * \retval 0 success, page can be placed into transfer
389 * \retval -EAGAIN page is either used by concurrent IO has been
390 * truncated. Skip it.
392 static int vvp_page_make_ready(const struct lu_env *env,
393 const struct cl_page_slice *slice)
395 cfs_page_t *vmpage = cl2vm_page(slice);
396 struct cl_page *pg = slice->cpl_page;
400 /* we're trying to write, but the page is locked.. come back later */
401 if (!TestSetPageLocked(vmpage)) {
402 if (pg->cp_state == CPS_CACHED) {
404 * We can cancel IO if page wasn't dirty after all.
406 clear_page_dirty_for_io(vmpage);
408 * This actually clears the dirty bit in the radix
411 set_page_writeback(vmpage);
412 vvp_write_pending(cl2ccc(slice->cpl_obj),
414 CL_PAGE_HEADER(D_PAGE, env, pg, "readied\n");
418 * Page was concurrently truncated.
420 LASSERT(pg->cp_state == CPS_FREEING);
425 static int vvp_page_print(const struct lu_env *env,
426 const struct cl_page_slice *slice,
427 void *cookie, lu_printer_t printer)
429 struct ccc_page *vp = cl2ccc_page(slice);
430 cfs_page_t *vmpage = vp->cpg_page;
432 (*printer)(env, cookie, LUSTRE_VVP_NAME"-page@%p(%d:%d:%d) "
434 vp, vp->cpg_defer_uptodate, vp->cpg_ra_used,
435 vp->cpg_write_queued, vmpage);
436 if (vmpage != NULL) {
437 (*printer)(env, cookie, "%lx %d:%d %lx %lu %slru",
438 (long)vmpage->flags, page_count(vmpage),
439 page_mapcount(vmpage), vmpage->private,
441 list_empty(&vmpage->lru) ? "not-" : "");
443 (*printer)(env, cookie, "\n");
447 static const struct cl_page_operations vvp_page_ops = {
448 .cpo_own = vvp_page_own,
449 .cpo_assume = vvp_page_assume,
450 .cpo_unassume = vvp_page_unassume,
451 .cpo_disown = vvp_page_disown,
452 .cpo_vmpage = ccc_page_vmpage,
453 .cpo_discard = vvp_page_discard,
454 .cpo_delete = vvp_page_delete,
455 .cpo_unmap = vvp_page_unmap,
456 .cpo_export = vvp_page_export,
457 .cpo_is_vmlocked = vvp_page_is_vmlocked,
458 .cpo_fini = vvp_page_fini,
459 .cpo_print = vvp_page_print,
460 .cpo_is_under_lock = ccc_page_is_under_lock,
463 .cpo_prep = vvp_page_prep_read,
464 .cpo_completion = vvp_page_completion_read,
465 .cpo_make_ready = ccc_fail,
468 .cpo_prep = vvp_page_prep_write,
469 .cpo_completion = vvp_page_completion_write,
470 .cpo_make_ready = vvp_page_make_ready,
475 static void vvp_transient_page_verify(const struct cl_page *page)
477 struct inode *inode = ccc_object_inode(page->cp_obj);
479 LASSERT(!TRYLOCK_INODE_MUTEX(inode));
480 /* LASSERT_SEM_LOCKED(&inode->i_alloc_sem); */
483 static int vvp_transient_page_own(const struct lu_env *env,
484 const struct cl_page_slice *slice,
485 struct cl_io *unused, int nonblock)
487 vvp_transient_page_verify(slice->cpl_page);
491 static void vvp_transient_page_assume(const struct lu_env *env,
492 const struct cl_page_slice *slice,
493 struct cl_io *unused)
495 vvp_transient_page_verify(slice->cpl_page);
498 static void vvp_transient_page_unassume(const struct lu_env *env,
499 const struct cl_page_slice *slice,
500 struct cl_io *unused)
502 vvp_transient_page_verify(slice->cpl_page);
505 static void vvp_transient_page_disown(const struct lu_env *env,
506 const struct cl_page_slice *slice,
507 struct cl_io *unused)
509 vvp_transient_page_verify(slice->cpl_page);
512 static void vvp_transient_page_discard(const struct lu_env *env,
513 const struct cl_page_slice *slice,
514 struct cl_io *unused)
516 struct cl_page *page = slice->cpl_page;
518 vvp_transient_page_verify(slice->cpl_page);
521 * For transient pages, remove it from the radix tree.
523 cl_page_delete(env, page);
526 static int vvp_transient_page_is_vmlocked(const struct lu_env *env,
527 const struct cl_page_slice *slice)
529 struct inode *inode = ccc_object_inode(slice->cpl_obj);
532 locked = !TRYLOCK_INODE_MUTEX(inode);
534 UNLOCK_INODE_MUTEX(inode);
535 return locked ? -EBUSY : -ENODATA;
539 vvp_transient_page_completion_write(const struct lu_env *env,
540 const struct cl_page_slice *slice,
543 vvp_transient_page_verify(slice->cpl_page);
544 vvp_page_completion_write_common(env, slice, ioret);
548 static void vvp_transient_page_fini(const struct lu_env *env,
549 struct cl_page_slice *slice)
551 struct ccc_page *cp = cl2ccc_page(slice);
552 struct cl_page *clp = slice->cpl_page;
553 struct ccc_object *clobj = cl2ccc(clp->cp_obj);
555 vvp_page_fini_common(cp);
556 LASSERT(!TRYLOCK_INODE_MUTEX(clobj->cob_inode));
557 clobj->cob_transient_pages--;
560 static const struct cl_page_operations vvp_transient_page_ops = {
561 .cpo_own = vvp_transient_page_own,
562 .cpo_assume = vvp_transient_page_assume,
563 .cpo_unassume = vvp_transient_page_unassume,
564 .cpo_disown = vvp_transient_page_disown,
565 .cpo_discard = vvp_transient_page_discard,
566 .cpo_vmpage = ccc_page_vmpage,
567 .cpo_fini = vvp_transient_page_fini,
568 .cpo_is_vmlocked = vvp_transient_page_is_vmlocked,
569 .cpo_print = vvp_page_print,
570 .cpo_is_under_lock = ccc_page_is_under_lock,
573 .cpo_prep = ccc_transient_page_prep,
574 .cpo_completion = vvp_page_completion_read,
577 .cpo_prep = ccc_transient_page_prep,
578 .cpo_completion = vvp_transient_page_completion_write,
583 struct cl_page *vvp_page_init(const struct lu_env *env, struct cl_object *obj,
584 struct cl_page *page, cfs_page_t *vmpage)
586 struct ccc_page *cpg;
589 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
591 OBD_SLAB_ALLOC_PTR_GFP(cpg, vvp_page_kmem, CFS_ALLOC_IO);
593 cpg->cpg_page = vmpage;
594 page_cache_get(vmpage);
596 CFS_INIT_LIST_HEAD(&cpg->cpg_pending_linkage);
597 if (page->cp_type == CPT_CACHEABLE) {
598 SetPagePrivate(vmpage);
599 vmpage->private = (unsigned long)page;
600 cl_page_slice_add(page, &cpg->cpg_cl, obj,
603 struct ccc_object *clobj = cl2ccc(obj);
605 LASSERT(!TRYLOCK_INODE_MUTEX(clobj->cob_inode));
606 cl_page_slice_add(page, &cpg->cpg_cl, obj,
607 &vvp_transient_page_ops);
608 clobj->cob_transient_pages++;
613 return ERR_PTR(result);