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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * Implementation of cl_page for VVP layer.
34 * Author: Nikita Danilov <nikita.danilov@sun.com>
35 * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
38 #define DEBUG_SUBSYSTEM S_LLITE
40 #include <linux/atomic.h>
41 #include <linux/bitops.h>
43 #include <linux/mutex.h>
44 #include <linux/page-flags.h>
45 #include <linux/pagemap.h>
47 #include <libcfs/libcfs.h>
48 #include "llite_internal.h"
49 #include "vvp_internal.h"
51 /*****************************************************************************
57 static void vvp_page_fini_common(struct vvp_page *vpg)
59 struct page *vmpage = vpg->vpg_page;
61 LASSERT(vmpage != NULL);
65 static void vvp_page_fini(const struct lu_env *env,
66 struct cl_page_slice *slice)
68 struct vvp_page *vpg = cl2vvp_page(slice);
69 struct page *vmpage = vpg->vpg_page;
72 * vmpage->private was already cleared when page was moved into
75 LASSERT((struct cl_page *)vmpage->private != slice->cpl_page);
76 vvp_page_fini_common(vpg);
79 static int vvp_page_own(const struct lu_env *env,
80 const struct cl_page_slice *slice, struct cl_io *io,
83 struct vvp_page *vpg = cl2vvp_page(slice);
84 struct page *vmpage = vpg->vpg_page;
86 LASSERT(vmpage != NULL);
88 if (!trylock_page(vmpage))
91 if (unlikely(PageWriteback(vmpage))) {
100 wait_on_page_writeback(vmpage);
105 static void vvp_page_assume(const struct lu_env *env,
106 const struct cl_page_slice *slice,
107 struct cl_io *unused)
109 struct page *vmpage = cl2vm_page(slice);
111 LASSERT(vmpage != NULL);
112 LASSERT(PageLocked(vmpage));
113 wait_on_page_writeback(vmpage);
116 static void vvp_page_unassume(const struct lu_env *env,
117 const struct cl_page_slice *slice,
118 struct cl_io *unused)
120 struct page *vmpage = cl2vm_page(slice);
122 LASSERT(vmpage != NULL);
123 LASSERT(PageLocked(vmpage));
126 static void vvp_page_disown(const struct lu_env *env,
127 const struct cl_page_slice *slice, struct cl_io *io)
129 struct page *vmpage = cl2vm_page(slice);
131 LASSERT(vmpage != NULL);
132 LASSERT(PageLocked(vmpage));
134 unlock_page(cl2vm_page(slice));
137 static void vvp_page_discard(const struct lu_env *env,
138 const struct cl_page_slice *slice,
139 struct cl_io *unused)
141 struct page *vmpage = cl2vm_page(slice);
142 struct vvp_page *vpg = cl2vvp_page(slice);
144 LASSERT(vmpage != NULL);
145 LASSERT(PageLocked(vmpage));
147 if (vpg->vpg_defer_uptodate && !vpg->vpg_ra_used)
148 ll_ra_stats_inc(vmpage->mapping->host, RA_STAT_DISCARDED);
150 ll_invalidate_page(vmpage);
153 static void vvp_page_delete(const struct lu_env *env,
154 const struct cl_page_slice *slice)
156 struct page *vmpage = cl2vm_page(slice);
157 struct inode *inode = vmpage->mapping->host;
158 struct cl_object *obj = slice->cpl_obj;
159 struct cl_page *page = slice->cpl_page;
162 LASSERT(PageLocked(vmpage));
163 LASSERT((struct cl_page *)vmpage->private == page);
164 LASSERT(inode == vvp_object_inode(obj));
166 /* Drop the reference count held in vvp_page_init */
167 refc = atomic_dec_return(&page->cp_ref);
168 LASSERTF(refc >= 1, "page = %p, refc = %d\n", page, refc);
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,
182 struct page *vmpage = cl2vm_page(slice);
184 LASSERT(vmpage != NULL);
185 LASSERT(PageLocked(vmpage));
187 SetPageUptodate(vmpage);
189 ClearPageUptodate(vmpage);
192 static int vvp_page_is_vmlocked(const struct lu_env *env,
193 const struct cl_page_slice *slice)
195 return PageLocked(cl2vm_page(slice)) ? -EBUSY : -ENODATA;
198 static int vvp_page_prep_read(const struct lu_env *env,
199 const struct cl_page_slice *slice,
200 struct cl_io *unused)
203 /* Skip the page already marked as PG_uptodate. */
204 RETURN(PageUptodate(cl2vm_page(slice)) ? -EALREADY : 0);
207 static int vvp_page_prep_write(const struct lu_env *env,
208 const struct cl_page_slice *slice,
209 struct cl_io *unused)
211 struct page *vmpage = cl2vm_page(slice);
212 struct cl_page *pg = slice->cpl_page;
214 LASSERT(PageLocked(vmpage));
215 LASSERT(!PageDirty(vmpage));
217 /* ll_writepage path is not a sync write, so need to set page writeback
219 if (pg->cp_sync_io == NULL)
220 set_page_writeback(vmpage);
226 * Handles page transfer errors at VM level.
228 * This takes inode as a separate argument, because inode on which error is to
229 * be set can be different from \a vmpage inode in case of direct-io.
231 static void vvp_vmpage_error(struct inode *inode, struct page *vmpage, int ioret)
233 struct vvp_object *obj = cl_inode2vvp(inode);
236 ClearPageError(vmpage);
237 obj->vob_discard_page_warned = 0;
239 SetPageError(vmpage);
240 if (ioret == -ENOSPC)
241 set_bit(AS_ENOSPC, &inode->i_mapping->flags);
243 set_bit(AS_EIO, &inode->i_mapping->flags);
245 if ((ioret == -ESHUTDOWN || ioret == -EINTR ||
246 ioret == -EIO) && obj->vob_discard_page_warned == 0) {
247 obj->vob_discard_page_warned = 1;
248 ll_dirty_page_discard_warn(vmpage, ioret);
253 static void vvp_page_completion_read(const struct lu_env *env,
254 const struct cl_page_slice *slice,
257 struct vvp_page *vpg = cl2vvp_page(slice);
258 struct page *vmpage = vpg->vpg_page;
259 struct cl_page *page = slice->cpl_page;
260 struct inode *inode = vvp_object_inode(page->cp_obj);
263 LASSERT(PageLocked(vmpage));
264 CL_PAGE_HEADER(D_PAGE, env, page, "completing READ with %d\n", ioret);
266 if (vpg->vpg_defer_uptodate)
267 ll_ra_count_put(ll_i2sbi(inode), 1);
270 if (!vpg->vpg_defer_uptodate)
271 cl_page_export(env, page, 1);
272 } else if (vpg->vpg_defer_uptodate) {
273 vpg->vpg_defer_uptodate = 0;
274 if (ioret == -EWOULDBLOCK) {
275 /* mirror read failed, it needs to destroy the page
276 * because subpage would be from wrong osc when trying
277 * to read from a new mirror */
278 ll_invalidate_page(vmpage);
282 if (page->cp_sync_io == NULL)
288 static void vvp_page_completion_write(const struct lu_env *env,
289 const struct cl_page_slice *slice,
292 struct vvp_page *vpg = cl2vvp_page(slice);
293 struct cl_page *pg = slice->cpl_page;
294 struct page *vmpage = vpg->vpg_page;
297 CL_PAGE_HEADER(D_PAGE, env, pg, "completing WRITE with %d\n", ioret);
299 if (pg->cp_sync_io != NULL) {
300 LASSERT(PageLocked(vmpage));
301 LASSERT(!PageWriteback(vmpage));
303 LASSERT(PageWriteback(vmpage));
305 * Only mark the page error only when it's an async write
306 * because applications won't wait for IO to finish.
308 vvp_vmpage_error(vvp_object_inode(pg->cp_obj), vmpage, ioret);
310 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 CL_PAGE_HEADER(D_PAGE, env, pg, "readied\n");
343 } else if (pg->cp_state == CPS_PAGEOUT) {
344 /* is it possible for osc_flush_async_page() to already
348 CL_PAGE_DEBUG(D_ERROR, env, pg, "Unexpecting page state %d.\n",
356 static int vvp_page_print(const struct lu_env *env,
357 const struct cl_page_slice *slice,
358 void *cookie, lu_printer_t printer)
360 struct vvp_page *vpg = cl2vvp_page(slice);
361 struct page *vmpage = vpg->vpg_page;
363 (*printer)(env, cookie, LUSTRE_VVP_NAME"-page@%p(%d:%d) "
365 vpg, vpg->vpg_defer_uptodate, vpg->vpg_ra_used, vmpage);
367 if (vmpage != NULL) {
368 (*printer)(env, cookie, "%lx %d:%d %lx %lu %slru",
369 (long)vmpage->flags, page_count(vmpage),
370 page_mapcount(vmpage), vmpage->private,
372 list_empty(&vmpage->lru) ? "not-" : "");
375 (*printer)(env, cookie, "\n");
380 static int vvp_page_fail(const struct lu_env *env,
381 const struct cl_page_slice *slice)
391 static const struct cl_page_operations vvp_page_ops = {
392 .cpo_own = vvp_page_own,
393 .cpo_assume = vvp_page_assume,
394 .cpo_unassume = vvp_page_unassume,
395 .cpo_disown = vvp_page_disown,
396 .cpo_discard = vvp_page_discard,
397 .cpo_delete = vvp_page_delete,
398 .cpo_export = vvp_page_export,
399 .cpo_is_vmlocked = vvp_page_is_vmlocked,
400 .cpo_fini = vvp_page_fini,
401 .cpo_print = vvp_page_print,
404 .cpo_prep = vvp_page_prep_read,
405 .cpo_completion = vvp_page_completion_read,
406 .cpo_make_ready = vvp_page_fail,
409 .cpo_prep = vvp_page_prep_write,
410 .cpo_completion = vvp_page_completion_write,
411 .cpo_make_ready = vvp_page_make_ready,
416 static int vvp_transient_page_prep(const struct lu_env *env,
417 const struct cl_page_slice *slice,
418 struct cl_io *unused)
421 /* transient page should always be sent. */
425 static void vvp_transient_page_verify(const struct cl_page *page)
429 static int vvp_transient_page_own(const struct lu_env *env,
430 const struct cl_page_slice *slice,
431 struct cl_io *unused, int nonblock)
433 vvp_transient_page_verify(slice->cpl_page);
437 static void vvp_transient_page_assume(const struct lu_env *env,
438 const struct cl_page_slice *slice,
439 struct cl_io *unused)
441 vvp_transient_page_verify(slice->cpl_page);
444 static void vvp_transient_page_unassume(const struct lu_env *env,
445 const struct cl_page_slice *slice,
446 struct cl_io *unused)
448 vvp_transient_page_verify(slice->cpl_page);
451 static void vvp_transient_page_disown(const struct lu_env *env,
452 const struct cl_page_slice *slice,
453 struct cl_io *unused)
455 vvp_transient_page_verify(slice->cpl_page);
458 static void vvp_transient_page_discard(const struct lu_env *env,
459 const struct cl_page_slice *slice,
460 struct cl_io *unused)
462 struct cl_page *page = slice->cpl_page;
464 vvp_transient_page_verify(slice->cpl_page);
467 * For transient pages, remove it from the radix tree.
469 cl_page_delete(env, page);
472 static int vvp_transient_page_is_vmlocked(const struct lu_env *env,
473 const struct cl_page_slice *slice)
475 struct inode *inode = vvp_object_inode(slice->cpl_obj);
478 locked = !inode_trylock(inode);
481 return locked ? -EBUSY : -ENODATA;
485 vvp_transient_page_completion(const struct lu_env *env,
486 const struct cl_page_slice *slice,
489 vvp_transient_page_verify(slice->cpl_page);
492 static void vvp_transient_page_fini(const struct lu_env *env,
493 struct cl_page_slice *slice)
495 struct vvp_page *vpg = cl2vvp_page(slice);
496 struct cl_page *clp = slice->cpl_page;
497 struct vvp_object *clobj = cl2vvp(clp->cp_obj);
499 vvp_page_fini_common(vpg);
500 atomic_dec(&clobj->vob_transient_pages);
503 static const struct cl_page_operations vvp_transient_page_ops = {
504 .cpo_own = vvp_transient_page_own,
505 .cpo_assume = vvp_transient_page_assume,
506 .cpo_unassume = vvp_transient_page_unassume,
507 .cpo_disown = vvp_transient_page_disown,
508 .cpo_discard = vvp_transient_page_discard,
509 .cpo_fini = vvp_transient_page_fini,
510 .cpo_is_vmlocked = vvp_transient_page_is_vmlocked,
511 .cpo_print = vvp_page_print,
514 .cpo_prep = vvp_transient_page_prep,
515 .cpo_completion = vvp_transient_page_completion,
518 .cpo_prep = vvp_transient_page_prep,
519 .cpo_completion = vvp_transient_page_completion,
524 int vvp_page_init(const struct lu_env *env, struct cl_object *obj,
525 struct cl_page *page, pgoff_t index)
527 struct vvp_page *vpg = cl_object_page_slice(obj, page);
528 struct page *vmpage = page->cp_vmpage;
530 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
532 vpg->vpg_page = vmpage;
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, &vpg->vpg_cl, obj, index,
543 struct vvp_object *clobj = cl2vvp(obj);
545 cl_page_slice_add(page, &vpg->vpg_cl, obj, index,
546 &vvp_transient_page_ops);
547 atomic_inc(&clobj->vob_transient_pages);