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 void vvp_page_own(const struct lu_env *env,
82 const struct cl_page_slice *slice, struct cl_io *_)
84 struct ccc_page *vpg = cl2ccc_page(slice);
85 cfs_page_t *vmpage = vpg->cpg_page;
87 LASSERT(vmpage != NULL);
89 wait_on_page_writeback(vmpage);
92 static void vvp_page_assume(const struct lu_env *env,
93 const struct cl_page_slice *slice, struct cl_io *_)
95 cfs_page_t *vmpage = cl2vm_page(slice);
97 LASSERT(vmpage != NULL);
98 LASSERT(PageLocked(vmpage));
99 wait_on_page_writeback(vmpage);
102 static void vvp_page_unassume(const struct lu_env *env,
103 const struct cl_page_slice *slice,
106 cfs_page_t *vmpage = cl2vm_page(slice);
108 LASSERT(vmpage != NULL);
109 LASSERT(PageLocked(vmpage));
112 static void vvp_page_disown(const struct lu_env *env,
113 const struct cl_page_slice *slice, struct cl_io *io)
115 cfs_page_t *vmpage = cl2vm_page(slice);
117 LASSERT(vmpage != NULL);
118 LASSERT(PageLocked(vmpage));
120 unlock_page(cl2vm_page(slice));
123 static void vvp_page_discard(const struct lu_env *env,
124 const struct cl_page_slice *slice, struct cl_io *_)
126 cfs_page_t *vmpage = cl2vm_page(slice);
127 struct address_space *mapping = vmpage->mapping;
128 struct ccc_page *cpg = cl2ccc_page(slice);
130 LASSERT(vmpage != NULL);
131 LASSERT(PageLocked(vmpage));
133 if (cpg->cpg_defer_uptodate && !cpg->cpg_ra_used)
134 ll_ra_stats_inc(mapping, RA_STAT_DISCARDED);
137 * truncate_complete_page() calls
138 * a_ops->invalidatepage()->cl_page_delete()->vvp_page_delete().
140 truncate_complete_page(mapping, vmpage);
143 static int vvp_page_unmap(const struct lu_env *env,
144 const struct cl_page_slice *slice, struct cl_io *_)
146 cfs_page_t *vmpage = cl2vm_page(slice);
147 __u64 offset = vmpage->index << CFS_PAGE_SHIFT;
149 LASSERT(vmpage != NULL);
150 LASSERT(PageLocked(vmpage));
152 * XXX is it safe to call this with the page lock held?
154 ll_teardown_mmaps(vmpage->mapping, offset, offset + CFS_PAGE_SIZE);
158 static void vvp_page_delete(const struct lu_env *env,
159 const struct cl_page_slice *slice)
161 cfs_page_t *vmpage = cl2vm_page(slice);
162 struct inode *inode = vmpage->mapping->host;
163 struct cl_object *obj = slice->cpl_obj;
165 LASSERT(PageLocked(vmpage));
166 LASSERT((struct cl_page *)vmpage->private == slice->cpl_page);
167 LASSERT(inode == ccc_object_inode(obj));
169 vvp_write_complete(cl2ccc(obj), cl2ccc_page(slice));
170 ClearPagePrivate(vmpage);
173 * Reference from vmpage to cl_page is removed, but the reference back
174 * is still here. It is removed later in vvp_page_fini().
178 static void vvp_page_export(const struct lu_env *env,
179 const struct cl_page_slice *slice)
181 cfs_page_t *vmpage = cl2vm_page(slice);
183 LASSERT(vmpage != NULL);
184 LASSERT(PageLocked(vmpage));
185 SetPageUptodate(vmpage);
188 static int vvp_page_is_vmlocked(const struct lu_env *env,
189 const struct cl_page_slice *slice)
191 return PageLocked(cl2vm_page(slice)) ? -EBUSY : -ENODATA;
194 static int vvp_page_prep_read(const struct lu_env *env,
195 const struct cl_page_slice *slice,
199 /* Skip the page already marked as PG_uptodate. */
200 RETURN(PageUptodate(cl2vm_page(slice)) ? -EALREADY : 0);
203 static int vvp_page_prep_write(const struct lu_env *env,
204 const struct cl_page_slice *slice,
207 cfs_page_t *vmpage = cl2vm_page(slice);
210 if (clear_page_dirty_for_io(vmpage)) {
211 set_page_writeback(vmpage);
212 vvp_write_pending(cl2ccc(slice->cpl_obj), cl2ccc_page(slice));
220 * Handles page transfer errors at VM level.
222 * This takes inode as a separate argument, because inode on which error is to
223 * be set can be different from \a vmpage inode in case of direct-io.
225 static void vvp_vmpage_error(struct inode *inode, cfs_page_t *vmpage, int ioret)
228 ClearPageError(vmpage);
229 else if (ioret != -EINTR) {
230 SetPageError(vmpage);
231 if (ioret == -ENOSPC)
232 set_bit(AS_ENOSPC, &inode->i_mapping->flags);
234 set_bit(AS_EIO, &inode->i_mapping->flags);
238 static void vvp_page_completion_common(const struct lu_env *env,
239 struct ccc_page *cp, int ioret)
241 struct cl_page *clp = cp->cpg_cl.cpl_page;
242 cfs_page_t *vmpage = cp->cpg_page;
243 struct inode *inode = ccc_object_inode(clp->cp_obj);
244 struct cl_sync_io *anchor = cp->cpg_sync_io;
246 LINVRNT(cl_page_is_vmlocked(env, clp));
248 if (anchor != NULL) {
249 cp->cpg_sync_io = NULL;
250 cl_sync_io_note(anchor, ioret);
251 } else if (clp->cp_type == CPT_CACHEABLE) {
253 * Only mark the page error only when it's a cacheable page
256 * For sync IO and direct IO(CPT_TRANSIENT), the error is able
257 * to be seen by application, so we don't need to mark a page
260 vvp_vmpage_error(inode, vmpage, ioret);
265 static void vvp_page_completion_read(const struct lu_env *env,
266 const struct cl_page_slice *slice,
269 struct ccc_page *cp = cl2ccc_page(slice);
270 struct cl_page *page = cl_page_top(slice->cpl_page);
271 struct inode *inode = ccc_object_inode(page->cp_obj);
274 CL_PAGE_HEADER(D_PAGE, env, page, "completing READ with %d\n", ioret);
276 if (cp->cpg_defer_uptodate)
277 ll_ra_count_put(ll_i2sbi(inode), 1);
280 /* XXX: do we need this for transient pages? */
281 if (!cp->cpg_defer_uptodate)
282 cl_page_export(env, page);
284 cp->cpg_defer_uptodate = 0;
285 vvp_page_completion_common(env, cp, ioret);
290 static void vvp_page_completion_write_common(const struct lu_env *env,
291 const struct cl_page_slice *slice,
294 struct ccc_page *cp = cl2ccc_page(slice);
297 * TODO: Actually it makes sense to add the page into oap pending
298 * list again and so that we don't need to take the page out from
299 * SoM write pending list, if we just meet a recoverable error,
301 * To implement this, we just need to return a non zero value in
302 * ->cpo_completion method. The underlying transfer should be notified
303 * and then re-add the page into pending transfer queue. -jay
305 cp->cpg_write_queued = 0;
306 vvp_write_complete(cl2ccc(slice->cpl_obj), cp);
308 vvp_page_completion_common(env, cp, ioret);
311 static void vvp_page_completion_write(const struct lu_env *env,
312 const struct cl_page_slice *slice,
315 struct ccc_page *cp = cl2ccc_page(slice);
316 struct cl_page *pg = slice->cpl_page;
317 cfs_page_t *vmpage = cp->cpg_page;
321 LINVRNT(cl_page_is_vmlocked(env, pg));
322 LASSERT(PageWriteback(vmpage));
324 CL_PAGE_HEADER(D_PAGE, env, pg, "completing WRITE with %d\n", ioret);
326 vvp_page_completion_write_common(env, slice, ioret);
327 end_page_writeback(vmpage);
332 * Implements cl_page_operations::cpo_make_ready() method.
334 * This is called to yank a page from the transfer cache and to send it out as
335 * a part of transfer. This function try-locks the page. If try-lock failed,
336 * page is owned by some concurrent IO, and should be skipped (this is bad,
337 * but hopefully rare situation, as it usually results in transfer being
338 * shorter than possible).
340 * \retval 0 success, page can be placed into transfer
342 * \retval -EAGAIN page is either used by concurrent IO has been
343 * truncated. Skip it.
345 static int vvp_page_make_ready(const struct lu_env *env,
346 const struct cl_page_slice *slice)
348 cfs_page_t *vmpage = cl2vm_page(slice);
349 struct cl_page *pg = slice->cpl_page;
353 /* we're trying to write, but the page is locked.. come back later */
354 if (!TestSetPageLocked(vmpage)) {
355 if (pg->cp_state == CPS_CACHED) {
357 * We can cancel IO if page wasn't dirty after all.
359 clear_page_dirty_for_io(vmpage);
361 * This actually clears the dirty bit in the radix
364 set_page_writeback(vmpage);
365 vvp_write_pending(cl2ccc(slice->cpl_obj),
367 CL_PAGE_HEADER(D_PAGE, env, pg, "readied\n");
371 * Page was concurrently truncated.
373 LASSERT(pg->cp_state == CPS_FREEING);
378 static int vvp_page_print(const struct lu_env *env,
379 const struct cl_page_slice *slice,
380 void *cookie, lu_printer_t printer)
382 struct ccc_page *vp = cl2ccc_page(slice);
383 cfs_page_t *vmpage = vp->cpg_page;
385 (*printer)(env, cookie, LUSTRE_VVP_NAME"-page@%p(%d:%d:%d) "
387 vp, vp->cpg_defer_uptodate, vp->cpg_ra_used,
388 vp->cpg_write_queued, vmpage);
389 if (vmpage != NULL) {
390 (*printer)(env, cookie, "%lx %d:%d %lx %lu %slru",
391 (long)vmpage->flags, page_count(vmpage),
392 page_mapcount(vmpage), vmpage->private,
394 list_empty(&vmpage->lru) ? "not-" : "");
396 (*printer)(env, cookie, "\n");
400 static const struct cl_page_operations vvp_page_ops = {
401 .cpo_own = vvp_page_own,
402 .cpo_assume = vvp_page_assume,
403 .cpo_unassume = vvp_page_unassume,
404 .cpo_disown = vvp_page_disown,
405 .cpo_vmpage = ccc_page_vmpage,
406 .cpo_discard = vvp_page_discard,
407 .cpo_delete = vvp_page_delete,
408 .cpo_unmap = vvp_page_unmap,
409 .cpo_export = vvp_page_export,
410 .cpo_is_vmlocked = vvp_page_is_vmlocked,
411 .cpo_fini = vvp_page_fini,
412 .cpo_print = vvp_page_print,
413 .cpo_is_under_lock = ccc_page_is_under_lock,
416 .cpo_prep = vvp_page_prep_read,
417 .cpo_completion = vvp_page_completion_read,
418 .cpo_make_ready = ccc_fail,
421 .cpo_prep = vvp_page_prep_write,
422 .cpo_completion = vvp_page_completion_write,
423 .cpo_make_ready = vvp_page_make_ready,
428 static void vvp_transient_page_verify(const struct cl_page *page)
430 struct inode *inode = ccc_object_inode(page->cp_obj);
432 LASSERT(!TRYLOCK_INODE_MUTEX(inode));
433 /* LASSERT_SEM_LOCKED(&inode->i_alloc_sem); */
436 static void vvp_transient_page_own(const struct lu_env *env,
437 const struct cl_page_slice *slice,
440 vvp_transient_page_verify(slice->cpl_page);
443 static void vvp_transient_page_assume(const struct lu_env *env,
444 const struct cl_page_slice *slice,
447 vvp_transient_page_verify(slice->cpl_page);
450 static void vvp_transient_page_unassume(const struct lu_env *env,
451 const struct cl_page_slice *slice,
454 vvp_transient_page_verify(slice->cpl_page);
457 static void vvp_transient_page_disown(const struct lu_env *env,
458 const struct cl_page_slice *slice,
461 vvp_transient_page_verify(slice->cpl_page);
464 static void vvp_transient_page_discard(const struct lu_env *env,
465 const struct cl_page_slice *slice,
468 struct cl_page *page = slice->cpl_page;
470 vvp_transient_page_verify(slice->cpl_page);
473 * For transient pages, remove it from the radix tree.
475 cl_page_delete(env, page);
478 static int vvp_transient_page_is_vmlocked(const struct lu_env *env,
479 const struct cl_page_slice *slice)
481 struct inode *inode = ccc_object_inode(slice->cpl_obj);
484 locked = !TRYLOCK_INODE_MUTEX(inode);
486 UNLOCK_INODE_MUTEX(inode);
487 return locked ? -EBUSY : -ENODATA;
491 vvp_transient_page_completion_write(const struct lu_env *env,
492 const struct cl_page_slice *slice,
495 vvp_transient_page_verify(slice->cpl_page);
496 vvp_page_completion_write_common(env, slice, ioret);
500 static void vvp_transient_page_fini(const struct lu_env *env,
501 struct cl_page_slice *slice)
503 struct ccc_page *cp = cl2ccc_page(slice);
504 struct cl_page *clp = slice->cpl_page;
505 struct ccc_object *clobj = cl2ccc(clp->cp_obj);
507 vvp_page_fini_common(cp);
508 LASSERT(!TRYLOCK_INODE_MUTEX(clobj->cob_inode));
509 clobj->cob_transient_pages--;
512 static const struct cl_page_operations vvp_transient_page_ops = {
513 .cpo_own = vvp_transient_page_own,
514 .cpo_assume = vvp_transient_page_assume,
515 .cpo_unassume = vvp_transient_page_unassume,
516 .cpo_disown = vvp_transient_page_disown,
517 .cpo_discard = vvp_transient_page_discard,
518 .cpo_vmpage = ccc_page_vmpage,
519 .cpo_fini = vvp_transient_page_fini,
520 .cpo_is_vmlocked = vvp_transient_page_is_vmlocked,
521 .cpo_print = vvp_page_print,
522 .cpo_is_under_lock = ccc_page_is_under_lock,
525 .cpo_prep = ccc_transient_page_prep,
526 .cpo_completion = vvp_page_completion_read,
529 .cpo_prep = ccc_transient_page_prep,
530 .cpo_completion = vvp_transient_page_completion_write,
535 struct cl_page *vvp_page_init(const struct lu_env *env, struct cl_object *obj,
536 struct cl_page *page, cfs_page_t *vmpage)
538 struct ccc_page *cpg;
541 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
543 OBD_SLAB_ALLOC_PTR_GFP(cpg, vvp_page_kmem, CFS_ALLOC_IO);
545 cpg->cpg_page = vmpage;
546 page_cache_get(vmpage);
548 CFS_INIT_LIST_HEAD(&cpg->cpg_pending_linkage);
549 if (page->cp_type == CPT_CACHEABLE) {
550 SetPagePrivate(vmpage);
551 vmpage->private = (unsigned long)page;
552 cl_page_slice_add(page, &cpg->cpg_cl, obj,
555 struct ccc_object *clobj = cl2ccc(obj);
557 LASSERT(!TRYLOCK_INODE_MUTEX(clobj->cob_inode));
558 cl_page_slice_add(page, &cpg->cpg_cl, obj,
559 &vvp_transient_page_ops);
560 clobj->cob_transient_pages++;
565 return ERR_PTR(result);