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, struct pagevec *pvec)
59 struct page *vmpage = vpg->vpg_page;
61 LASSERT(vmpage != NULL);
63 if (!pagevec_add(pvec, vmpage))
64 pagevec_release(pvec);
70 static void vvp_page_fini(const struct lu_env *env,
71 struct cl_page_slice *slice,
74 struct vvp_page *vpg = cl2vvp_page(slice);
75 struct page *vmpage = vpg->vpg_page;
78 * vmpage->private was already cleared when page was moved into
81 LASSERT((struct cl_page *)vmpage->private != slice->cpl_page);
82 vvp_page_fini_common(vpg, pvec);
85 static int vvp_page_own(const struct lu_env *env,
86 const struct cl_page_slice *slice, struct cl_io *io,
89 struct vvp_page *vpg = cl2vvp_page(slice);
90 struct page *vmpage = vpg->vpg_page;
94 LASSERT(vmpage != NULL);
96 if (!trylock_page(vmpage))
99 if (unlikely(PageWriteback(vmpage))) {
108 wait_on_page_writeback(vmpage);
113 static void vvp_page_assume(const struct lu_env *env,
114 const struct cl_page_slice *slice,
115 struct cl_io *unused)
117 struct page *vmpage = cl2vm_page(slice);
119 LASSERT(vmpage != NULL);
120 LASSERT(PageLocked(vmpage));
121 wait_on_page_writeback(vmpage);
124 static void vvp_page_unassume(const struct lu_env *env,
125 const struct cl_page_slice *slice,
126 struct cl_io *unused)
128 struct page *vmpage = cl2vm_page(slice);
130 LASSERT(vmpage != NULL);
131 LASSERT(PageLocked(vmpage));
134 static void vvp_page_disown(const struct lu_env *env,
135 const struct cl_page_slice *slice, struct cl_io *io)
137 struct page *vmpage = cl2vm_page(slice);
141 LASSERT(vmpage != NULL);
142 LASSERT(PageLocked(vmpage));
144 unlock_page(cl2vm_page(slice));
149 static void vvp_page_discard(const struct lu_env *env,
150 const struct cl_page_slice *slice,
151 struct cl_io *unused)
153 struct page *vmpage = cl2vm_page(slice);
154 struct vvp_page *vpg = cl2vvp_page(slice);
156 LASSERT(vmpage != NULL);
157 LASSERT(PageLocked(vmpage));
159 if (vpg->vpg_defer_uptodate && !vpg->vpg_ra_used)
160 ll_ra_stats_inc(vmpage->mapping->host, RA_STAT_DISCARDED);
162 generic_error_remove_page(vmpage->mapping, vmpage);
165 static void vvp_page_delete(const struct lu_env *env,
166 const struct cl_page_slice *slice)
168 struct page *vmpage = cl2vm_page(slice);
169 struct inode *inode = vmpage->mapping->host;
170 struct cl_object *obj = slice->cpl_obj;
171 struct cl_page *page = slice->cpl_page;
174 LASSERT(PageLocked(vmpage));
175 LASSERT((struct cl_page *)vmpage->private == page);
176 LASSERT(inode == vvp_object_inode(obj));
178 /* Drop the reference count held in vvp_page_init */
179 refc = atomic_dec_return(&page->cp_ref);
180 LASSERTF(refc >= 1, "page = %p, refc = %d\n", page, refc);
182 ClearPagePrivate(vmpage);
185 * Reference from vmpage to cl_page is removed, but the reference back
186 * is still here. It is removed later in vvp_page_fini().
190 static void vvp_page_export(const struct lu_env *env,
191 const struct cl_page_slice *slice,
194 struct page *vmpage = cl2vm_page(slice);
196 LASSERT(vmpage != NULL);
197 LASSERT(PageLocked(vmpage));
199 SetPageUptodate(vmpage);
201 ClearPageUptodate(vmpage);
204 static int vvp_page_is_vmlocked(const struct lu_env *env,
205 const struct cl_page_slice *slice)
207 return PageLocked(cl2vm_page(slice)) ? -EBUSY : -ENODATA;
210 static int vvp_page_prep_read(const struct lu_env *env,
211 const struct cl_page_slice *slice,
212 struct cl_io *unused)
215 /* Skip the page already marked as PG_uptodate. */
216 RETURN(PageUptodate(cl2vm_page(slice)) ? -EALREADY : 0);
219 static int vvp_page_prep_write(const struct lu_env *env,
220 const struct cl_page_slice *slice,
221 struct cl_io *unused)
223 struct page *vmpage = cl2vm_page(slice);
224 struct cl_page *pg = slice->cpl_page;
226 LASSERT(PageLocked(vmpage));
227 LASSERT(!PageDirty(vmpage));
229 /* ll_writepage path is not a sync write, so need to set page writeback
231 if (pg->cp_sync_io == NULL)
232 set_page_writeback(vmpage);
238 * Handles page transfer errors at VM level.
240 * This takes inode as a separate argument, because inode on which error is to
241 * be set can be different from \a vmpage inode in case of direct-io.
243 static void vvp_vmpage_error(struct inode *inode, struct page *vmpage, int ioret)
245 struct vvp_object *obj = cl_inode2vvp(inode);
248 ClearPageError(vmpage);
249 obj->vob_discard_page_warned = 0;
251 SetPageError(vmpage);
252 if (ioret == -ENOSPC)
253 set_bit(AS_ENOSPC, &inode->i_mapping->flags);
255 set_bit(AS_EIO, &inode->i_mapping->flags);
257 if ((ioret == -ESHUTDOWN || ioret == -EINTR ||
258 ioret == -EIO) && obj->vob_discard_page_warned == 0) {
259 obj->vob_discard_page_warned = 1;
260 ll_dirty_page_discard_warn(vmpage, ioret);
265 static void vvp_page_completion_read(const struct lu_env *env,
266 const struct cl_page_slice *slice,
269 struct vvp_page *vpg = cl2vvp_page(slice);
270 struct page *vmpage = vpg->vpg_page;
271 struct cl_page *page = slice->cpl_page;
272 struct inode *inode = vvp_object_inode(page->cp_obj);
275 LASSERT(PageLocked(vmpage));
276 CL_PAGE_HEADER(D_PAGE, env, page, "completing READ with %d\n", ioret);
278 if (vpg->vpg_defer_uptodate)
279 ll_ra_count_put(ll_i2sbi(inode), 1);
282 if (!vpg->vpg_defer_uptodate)
283 cl_page_export(env, page, 1);
284 } else if (vpg->vpg_defer_uptodate) {
285 vpg->vpg_defer_uptodate = 0;
286 if (ioret == -EWOULDBLOCK) {
287 /* mirror read failed, it needs to destroy the page
288 * because subpage would be from wrong osc when trying
289 * to read from a new mirror */
290 generic_error_remove_page(vmpage->mapping, vmpage);
294 if (page->cp_sync_io == NULL)
300 static void vvp_page_completion_write(const struct lu_env *env,
301 const struct cl_page_slice *slice,
304 struct vvp_page *vpg = cl2vvp_page(slice);
305 struct cl_page *pg = slice->cpl_page;
306 struct page *vmpage = vpg->vpg_page;
309 CL_PAGE_HEADER(D_PAGE, env, pg, "completing WRITE with %d\n", ioret);
311 if (pg->cp_sync_io != NULL) {
312 LASSERT(PageLocked(vmpage));
313 LASSERT(!PageWriteback(vmpage));
315 LASSERT(PageWriteback(vmpage));
317 * Only mark the page error only when it's an async write
318 * because applications won't wait for IO to finish.
320 vvp_vmpage_error(vvp_object_inode(pg->cp_obj), vmpage, ioret);
322 end_page_writeback(vmpage);
328 * Implements cl_page_operations::cpo_make_ready() method.
330 * This is called to yank a page from the transfer cache and to send it out as
331 * a part of transfer. This function try-locks the page. If try-lock failed,
332 * page is owned by some concurrent IO, and should be skipped (this is bad,
333 * but hopefully rare situation, as it usually results in transfer being
334 * shorter than possible).
336 * \retval 0 success, page can be placed into transfer
338 * \retval -EAGAIN page is either used by concurrent IO has been
339 * truncated. Skip it.
341 static int vvp_page_make_ready(const struct lu_env *env,
342 const struct cl_page_slice *slice)
344 struct page *vmpage = cl2vm_page(slice);
345 struct cl_page *pg = slice->cpl_page;
349 if (clear_page_dirty_for_io(vmpage)) {
350 LASSERT(pg->cp_state == CPS_CACHED);
351 /* This actually clears the dirty bit in the radix
353 set_page_writeback(vmpage);
354 CL_PAGE_HEADER(D_PAGE, env, pg, "readied\n");
355 } else if (pg->cp_state == CPS_PAGEOUT) {
356 /* is it possible for osc_flush_async_page() to already
360 CL_PAGE_DEBUG(D_ERROR, env, pg, "Unexpecting page state %d.\n",
368 static int vvp_page_print(const struct lu_env *env,
369 const struct cl_page_slice *slice,
370 void *cookie, lu_printer_t printer)
372 struct vvp_page *vpg = cl2vvp_page(slice);
373 struct page *vmpage = vpg->vpg_page;
375 (*printer)(env, cookie, LUSTRE_VVP_NAME"-page@%p(%d:%d) "
377 vpg, vpg->vpg_defer_uptodate, vpg->vpg_ra_used, vmpage);
379 if (vmpage != NULL) {
380 (*printer)(env, cookie, "%lx %d:%d %lx %lu %slru",
381 (long)vmpage->flags, page_count(vmpage),
382 page_mapcount(vmpage), vmpage->private,
384 list_empty(&vmpage->lru) ? "not-" : "");
387 (*printer)(env, cookie, "\n");
392 static int vvp_page_fail(const struct lu_env *env,
393 const struct cl_page_slice *slice)
403 static const struct cl_page_operations vvp_page_ops = {
404 .cpo_own = vvp_page_own,
405 .cpo_assume = vvp_page_assume,
406 .cpo_unassume = vvp_page_unassume,
407 .cpo_disown = vvp_page_disown,
408 .cpo_discard = vvp_page_discard,
409 .cpo_delete = vvp_page_delete,
410 .cpo_export = vvp_page_export,
411 .cpo_is_vmlocked = vvp_page_is_vmlocked,
412 .cpo_fini = vvp_page_fini,
413 .cpo_print = vvp_page_print,
416 .cpo_prep = vvp_page_prep_read,
417 .cpo_completion = vvp_page_completion_read,
418 .cpo_make_ready = vvp_page_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_discard(const struct lu_env *env,
429 const struct cl_page_slice *slice,
430 struct cl_io *unused)
432 struct cl_page *page = slice->cpl_page;
435 * For transient pages, remove it from the radix tree.
437 cl_page_delete(env, page);
440 static int vvp_transient_page_is_vmlocked(const struct lu_env *env,
441 const struct cl_page_slice *slice)
446 static void vvp_transient_page_fini(const struct lu_env *env,
447 struct cl_page_slice *slice,
448 struct pagevec *pvec)
450 struct vvp_page *vpg = cl2vvp_page(slice);
451 struct vvp_object *clobj = cl2vvp(slice->cpl_obj);
453 vvp_page_fini_common(vpg, pvec);
454 atomic_dec(&clobj->vob_transient_pages);
457 static const struct cl_page_operations vvp_transient_page_ops = {
458 .cpo_discard = vvp_transient_page_discard,
459 .cpo_fini = vvp_transient_page_fini,
460 .cpo_is_vmlocked = vvp_transient_page_is_vmlocked,
461 .cpo_print = vvp_page_print,
464 int vvp_page_init(const struct lu_env *env, struct cl_object *obj,
465 struct cl_page *page, pgoff_t index)
467 struct vvp_page *vpg = cl_object_page_slice(obj, page);
468 struct page *vmpage = page->cp_vmpage;
470 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
472 vpg->vpg_page = vmpage;
475 if (page->cp_type == CPT_CACHEABLE) {
476 /* in cache, decref in vvp_page_delete */
477 atomic_inc(&page->cp_ref);
478 SetPagePrivate(vmpage);
479 vmpage->private = (unsigned long)page;
480 cl_page_slice_add(page, &vpg->vpg_cl, obj, index,
483 struct vvp_object *clobj = cl2vvp(obj);
485 cl_page_slice_add(page, &vpg->vpg_cl, obj, index,
486 &vvp_transient_page_ops);
487 atomic_inc(&clobj->vob_transient_pages);