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/
31 * Implementation of cl_page for VVP layer.
33 * Author: Nikita Danilov <nikita.danilov@sun.com>
34 * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
37 #define DEBUG_SUBSYSTEM S_LLITE
39 #include <linux/atomic.h>
40 #include <linux/bitops.h>
42 #include <linux/mutex.h>
43 #include <linux/page-flags.h>
44 #include <linux/pagemap.h>
46 #include <libcfs/libcfs.h>
47 #include "llite_internal.h"
48 #include "vvp_internal.h"
50 /*****************************************************************************
55 static void vvp_page_discard(const struct lu_env *env,
56 const struct cl_page_slice *slice,
59 struct cl_page *cp = slice->cpl_page;
60 struct page *vmpage = cp->cp_vmpage;
62 if (cp->cp_defer_uptodate && !cp->cp_ra_used && vmpage->mapping != NULL)
63 ll_ra_stats_inc(vmpage->mapping->host, RA_STAT_DISCARDED);
66 static int vvp_page_prep_read(const struct lu_env *env,
67 const struct cl_page_slice *slice,
71 /* Skip the page already marked as PG_uptodate. */
72 RETURN(PageUptodate(cl2vm_page(slice)) ? -EALREADY : 0);
75 static int vvp_page_prep_write(const struct lu_env *env,
76 const struct cl_page_slice *slice,
79 struct page *vmpage = cl2vm_page(slice);
80 struct cl_page *pg = slice->cpl_page;
82 LASSERT(PageLocked(vmpage));
83 LASSERT(!PageDirty(vmpage));
85 /* ll_writepage path is not a sync write, so need to set page writeback
88 if (pg->cp_sync_io == NULL)
89 set_page_writeback(vmpage);
95 * Handles page transfer errors at VM level.
97 * This takes inode as a separate argument, because inode on which error is to
98 * be set can be different from \a vmpage inode in case of direct-io.
100 static void vvp_vmpage_error(struct inode *inode, struct page *vmpage,
103 struct vvp_object *obj = cl_inode2vvp(inode);
106 ClearPageError(vmpage);
107 obj->vob_discard_page_warned = 0;
109 SetPageError(vmpage);
110 if (ioret == -ENOSPC)
111 set_bit(AS_ENOSPC, &inode->i_mapping->flags);
113 set_bit(AS_EIO, &inode->i_mapping->flags);
115 if ((ioret == -ESHUTDOWN || ioret == -EINTR ||
116 ioret == -EIO) && obj->vob_discard_page_warned == 0) {
117 obj->vob_discard_page_warned = 1;
118 ll_dirty_page_discard_warn(inode, ioret);
123 static void vvp_page_completion_read(const struct lu_env *env,
124 const struct cl_page_slice *slice,
127 struct cl_page *cp = slice->cpl_page;
128 struct page *vmpage = cp->cp_vmpage;
129 struct inode *inode = vvp_object_inode(cp->cp_obj);
132 LASSERT(PageLocked(vmpage));
133 CL_PAGE_HEADER(D_PAGE, env, cp, "completing READ with %d\n", ioret);
135 if (cp->cp_defer_uptodate)
136 ll_ra_count_put(ll_i2sbi(inode), 1);
139 if (!cp->cp_defer_uptodate)
140 SetPageUptodate(vmpage);
141 } else if (cp->cp_defer_uptodate) {
142 cp->cp_defer_uptodate = 0;
143 if (ioret == -EAGAIN) {
144 /* mirror read failed, it needs to destroy the page
145 * because subpage would be from wrong osc when trying
146 * to read from a new mirror
148 generic_error_remove_page(vmpage->mapping, vmpage);
152 if (cp->cp_sync_io == NULL)
158 static void vvp_page_completion_write(const struct lu_env *env,
159 const struct cl_page_slice *slice,
162 struct vvp_page *vpg = cl2vvp_page(slice);
163 struct cl_page *pg = slice->cpl_page;
164 struct page *vmpage = vpg->vpg_page;
167 CL_PAGE_HEADER(D_PAGE, env, pg, "completing WRITE with %d\n", ioret);
169 if (pg->cp_sync_io != NULL) {
170 LASSERT(PageLocked(vmpage));
171 LASSERT(!PageWriteback(vmpage));
173 LASSERT(PageWriteback(vmpage));
175 * Only mark the page error only when it's an async write
176 * because applications won't wait for IO to finish.
178 vvp_vmpage_error(vvp_object_inode(pg->cp_obj), vmpage, ioret);
180 end_page_writeback(vmpage);
186 * Implements cl_page_operations::cpo_make_ready() method.
188 * This is called to yank a page from the transfer cache and to send it out as
189 * a part of transfer. This function try-locks the page. If try-lock failed,
190 * page is owned by some concurrent IO, and should be skipped (this is bad,
191 * but hopefully rare situation, as it usually results in transfer being
192 * shorter than possible).
194 * \retval 0 success, page can be placed into transfer
196 * \retval -EAGAIN page is either used by concurrent IO has been
197 * truncated. Skip it.
199 static int vvp_page_make_ready(const struct lu_env *env,
200 const struct cl_page_slice *slice)
202 struct page *vmpage = cl2vm_page(slice);
203 struct cl_page *pg = slice->cpl_page;
207 if (clear_page_dirty_for_io(vmpage)) {
208 LASSERT(pg->cp_state == CPS_CACHED);
209 /* This actually clears the dirty bit in the radix
212 set_page_writeback(vmpage);
213 CL_PAGE_HEADER(D_PAGE, env, pg, "readied\n");
214 } else if (pg->cp_state == CPS_PAGEOUT) {
215 /* is it possible for osc_flush_async_page() to already
220 CL_PAGE_DEBUG(D_ERROR, env, pg, "Unexpecting page state %d.\n",
228 static int vvp_page_print(const struct lu_env *env,
229 const struct cl_page_slice *slice,
230 void *cookie, lu_printer_t printer)
232 struct vvp_page *vpg = cl2vvp_page(slice);
233 struct page *vmpage = vpg->vpg_page;
235 (*printer)(env, cookie,
236 LUSTRE_VVP_NAME"-page@%p vm@%p ", vpg, vmpage);
238 if (vmpage != NULL) {
239 (*printer)(env, cookie, "%lx %d:%d %lx %lu %slru",
240 (long)vmpage->flags, page_count(vmpage),
241 page_mapcount(vmpage), vmpage->private,
243 list_empty(&vmpage->lru) ? "not-" : "");
246 (*printer)(env, cookie, "\n");
251 static int vvp_page_fail(const struct lu_env *env,
252 const struct cl_page_slice *slice)
262 static const struct cl_page_operations vvp_page_ops = {
263 .cpo_discard = vvp_page_discard,
264 .cpo_print = vvp_page_print,
267 .cpo_prep = vvp_page_prep_read,
268 .cpo_completion = vvp_page_completion_read,
269 .cpo_make_ready = vvp_page_fail,
272 .cpo_prep = vvp_page_prep_write,
273 .cpo_completion = vvp_page_completion_write,
274 .cpo_make_ready = vvp_page_make_ready,
279 static const struct cl_page_operations vvp_transient_page_ops = {
280 .cpo_print = vvp_page_print,
283 int vvp_page_init(const struct lu_env *env, struct cl_object *obj,
284 struct cl_page *page, pgoff_t index)
286 struct vvp_page *vpg = cl_object_page_slice(obj, page);
287 struct page *vmpage = page->cp_vmpage;
289 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
291 vpg->vpg_page = vmpage;
293 if (page->cp_type == CPT_TRANSIENT) {
294 /* DIO pages are referenced by userspace, we don't need to take
295 * a reference on them. (contrast with get_page() call above)
297 cl_page_slice_add(page, &vpg->vpg_cl, obj,
298 &vvp_transient_page_ops);
301 /* in cache, decref in cl_page_delete() */
302 atomic_inc(&page->cp_ref);
303 SetPagePrivate(vmpage);
304 vmpage->private = (unsigned long)page;
305 cl_page_slice_add(page, &vpg->vpg_cl, obj,