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_fini(const struct lu_env *env,
56 struct cl_page_slice *slice,
59 struct vvp_page *vpg = cl2vvp_page(slice);
60 struct page *vmpage = vpg->vpg_page;
63 * vmpage->private was already cleared when page was moved into
66 LASSERT((struct cl_page *)vmpage->private != slice->cpl_page);
67 LASSERT(vmpage != NULL);
69 if (!pagevec_add(pvec, vmpage))
70 pagevec_release(pvec);
76 static void vvp_page_assume(const struct lu_env *env,
77 const struct cl_page_slice *slice,
80 struct page *vmpage = cl2vm_page(slice);
82 LASSERT(vmpage != NULL);
83 LASSERT(PageLocked(vmpage));
84 wait_on_page_writeback(vmpage);
87 static void vvp_page_unassume(const struct lu_env *env,
88 const struct cl_page_slice *slice,
91 struct page *vmpage = cl2vm_page(slice);
93 LASSERT(vmpage != NULL);
94 LASSERT(PageLocked(vmpage));
97 static void vvp_page_discard(const struct lu_env *env,
98 const struct cl_page_slice *slice,
101 struct cl_page *cp = slice->cpl_page;
102 struct page *vmpage = cp->cp_vmpage;
104 if (cp->cp_defer_uptodate && !cp->cp_ra_used && vmpage->mapping != NULL)
105 ll_ra_stats_inc(vmpage->mapping->host, RA_STAT_DISCARDED);
108 static void vvp_page_delete(const struct lu_env *env,
109 const struct cl_page_slice *slice)
111 struct page *vmpage = cl2vm_page(slice);
112 struct cl_page *page = slice->cpl_page;
115 LASSERT(PageLocked(vmpage));
116 LASSERT((struct cl_page *)vmpage->private == page);
119 /* Drop the reference count held in vvp_page_init */
120 refc = atomic_dec_return(&page->cp_ref);
121 LASSERTF(refc >= 1, "page = %p, refc = %d\n", page, refc);
123 ClearPagePrivate(vmpage);
126 * Reference from vmpage to cl_page is removed, but the reference back
127 * is still here. It is removed later in vvp_page_fini().
131 static int vvp_page_prep_read(const struct lu_env *env,
132 const struct cl_page_slice *slice,
133 struct cl_io *unused)
136 /* Skip the page already marked as PG_uptodate. */
137 RETURN(PageUptodate(cl2vm_page(slice)) ? -EALREADY : 0);
140 static int vvp_page_prep_write(const struct lu_env *env,
141 const struct cl_page_slice *slice,
142 struct cl_io *unused)
144 struct page *vmpage = cl2vm_page(slice);
145 struct cl_page *pg = slice->cpl_page;
147 LASSERT(PageLocked(vmpage));
148 LASSERT(!PageDirty(vmpage));
150 /* ll_writepage path is not a sync write, so need to set page writeback
153 if (pg->cp_sync_io == NULL)
154 set_page_writeback(vmpage);
160 * Handles page transfer errors at VM level.
162 * This takes inode as a separate argument, because inode on which error is to
163 * be set can be different from \a vmpage inode in case of direct-io.
165 static void vvp_vmpage_error(struct inode *inode, struct page *vmpage,
168 struct vvp_object *obj = cl_inode2vvp(inode);
171 ClearPageError(vmpage);
172 obj->vob_discard_page_warned = 0;
174 SetPageError(vmpage);
175 if (ioret == -ENOSPC)
176 set_bit(AS_ENOSPC, &inode->i_mapping->flags);
178 set_bit(AS_EIO, &inode->i_mapping->flags);
180 if ((ioret == -ESHUTDOWN || ioret == -EINTR ||
181 ioret == -EIO) && obj->vob_discard_page_warned == 0) {
182 obj->vob_discard_page_warned = 1;
183 ll_dirty_page_discard_warn(inode, ioret);
188 static void vvp_page_completion_read(const struct lu_env *env,
189 const struct cl_page_slice *slice,
192 struct cl_page *cp = slice->cpl_page;
193 struct page *vmpage = cp->cp_vmpage;
194 struct inode *inode = vvp_object_inode(cp->cp_obj);
197 LASSERT(PageLocked(vmpage));
198 CL_PAGE_HEADER(D_PAGE, env, cp, "completing READ with %d\n", ioret);
200 if (cp->cp_defer_uptodate)
201 ll_ra_count_put(ll_i2sbi(inode), 1);
204 if (!cp->cp_defer_uptodate)
205 SetPageUptodate(vmpage);
206 } else if (cp->cp_defer_uptodate) {
207 cp->cp_defer_uptodate = 0;
208 if (ioret == -EAGAIN) {
209 /* mirror read failed, it needs to destroy the page
210 * because subpage would be from wrong osc when trying
211 * to read from a new mirror
213 generic_error_remove_page(vmpage->mapping, vmpage);
217 if (cp->cp_sync_io == NULL)
223 static void vvp_page_completion_write(const struct lu_env *env,
224 const struct cl_page_slice *slice,
227 struct vvp_page *vpg = cl2vvp_page(slice);
228 struct cl_page *pg = slice->cpl_page;
229 struct page *vmpage = vpg->vpg_page;
232 CL_PAGE_HEADER(D_PAGE, env, pg, "completing WRITE with %d\n", ioret);
234 if (pg->cp_sync_io != NULL) {
235 LASSERT(PageLocked(vmpage));
236 LASSERT(!PageWriteback(vmpage));
238 LASSERT(PageWriteback(vmpage));
240 * Only mark the page error only when it's an async write
241 * because applications won't wait for IO to finish.
243 vvp_vmpage_error(vvp_object_inode(pg->cp_obj), vmpage, ioret);
245 end_page_writeback(vmpage);
251 * Implements cl_page_operations::cpo_make_ready() method.
253 * This is called to yank a page from the transfer cache and to send it out as
254 * a part of transfer. This function try-locks the page. If try-lock failed,
255 * page is owned by some concurrent IO, and should be skipped (this is bad,
256 * but hopefully rare situation, as it usually results in transfer being
257 * shorter than possible).
259 * \retval 0 success, page can be placed into transfer
261 * \retval -EAGAIN page is either used by concurrent IO has been
262 * truncated. Skip it.
264 static int vvp_page_make_ready(const struct lu_env *env,
265 const struct cl_page_slice *slice)
267 struct page *vmpage = cl2vm_page(slice);
268 struct cl_page *pg = slice->cpl_page;
272 if (clear_page_dirty_for_io(vmpage)) {
273 LASSERT(pg->cp_state == CPS_CACHED);
274 /* This actually clears the dirty bit in the radix
277 set_page_writeback(vmpage);
278 CL_PAGE_HEADER(D_PAGE, env, pg, "readied\n");
279 } else if (pg->cp_state == CPS_PAGEOUT) {
280 /* is it possible for osc_flush_async_page() to already
285 CL_PAGE_DEBUG(D_ERROR, env, pg, "Unexpecting page state %d.\n",
293 static int vvp_page_print(const struct lu_env *env,
294 const struct cl_page_slice *slice,
295 void *cookie, lu_printer_t printer)
297 struct vvp_page *vpg = cl2vvp_page(slice);
298 struct page *vmpage = vpg->vpg_page;
300 (*printer)(env, cookie,
301 LUSTRE_VVP_NAME"-page@%p vm@%p ", vpg, vmpage);
303 if (vmpage != NULL) {
304 (*printer)(env, cookie, "%lx %d:%d %lx %lu %slru",
305 (long)vmpage->flags, page_count(vmpage),
306 page_mapcount(vmpage), vmpage->private,
308 list_empty(&vmpage->lru) ? "not-" : "");
311 (*printer)(env, cookie, "\n");
316 static int vvp_page_fail(const struct lu_env *env,
317 const struct cl_page_slice *slice)
327 static const struct cl_page_operations vvp_page_ops = {
328 .cpo_assume = vvp_page_assume,
329 .cpo_unassume = vvp_page_unassume,
330 .cpo_discard = vvp_page_discard,
331 .cpo_delete = vvp_page_delete,
332 .cpo_fini = vvp_page_fini,
333 .cpo_print = vvp_page_print,
336 .cpo_prep = vvp_page_prep_read,
337 .cpo_completion = vvp_page_completion_read,
338 .cpo_make_ready = vvp_page_fail,
341 .cpo_prep = vvp_page_prep_write,
342 .cpo_completion = vvp_page_completion_write,
343 .cpo_make_ready = vvp_page_make_ready,
348 static const struct cl_page_operations vvp_transient_page_ops = {
349 .cpo_print = vvp_page_print,
352 int vvp_page_init(const struct lu_env *env, struct cl_object *obj,
353 struct cl_page *page, pgoff_t index)
355 struct vvp_page *vpg = cl_object_page_slice(obj, page);
356 struct page *vmpage = page->cp_vmpage;
358 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
360 vpg->vpg_page = vmpage;
362 if (page->cp_type == CPT_TRANSIENT) {
363 /* DIO pages are referenced by userspace, we don't need to take
364 * a reference on them. (contrast with get_page() call above)
366 cl_page_slice_add(page, &vpg->vpg_cl, obj,
367 &vvp_transient_page_ops);
370 /* in cache, decref in vvp_page_delete */
371 atomic_inc(&page->cp_ref);
372 SetPagePrivate(vmpage);
373 vmpage->private = (unsigned long)page;
374 cl_page_slice_add(page, &vpg->vpg_cl, obj,