Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[fs/lustre-release.git] / lustre / llite / vvp_page.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * Implementation of cl_page for VVP layer.
32  *
33  *   Author: Nikita Danilov <nikita.danilov@sun.com>
34  *   Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_LLITE
38
39 #include <linux/atomic.h>
40 #include <linux/bitops.h>
41 #include <linux/mm.h>
42 #include <linux/mutex.h>
43 #include <linux/page-flags.h>
44 #include <linux/pagemap.h>
45
46 #include <libcfs/libcfs.h>
47 #include "llite_internal.h"
48 #include "vvp_internal.h"
49
50 /*****************************************************************************
51  *
52  * Page operations.
53  *
54  */
55 static void vvp_page_discard(const struct lu_env *env,
56                              const struct cl_page_slice *slice,
57                              struct cl_io *unused)
58 {
59         struct cl_page *cp = slice->cpl_page;
60         struct page *vmpage = cp->cp_vmpage;
61
62         if (cp->cp_defer_uptodate && !cp->cp_ra_used && vmpage->mapping != NULL)
63                 ll_ra_stats_inc(vmpage->mapping->host, RA_STAT_DISCARDED);
64 }
65
66 static void vvp_page_delete(const struct lu_env *env,
67                             const struct cl_page_slice *slice)
68 {
69         struct cl_page *cp = slice->cpl_page;
70
71         if (cp->cp_type == CPT_CACHEABLE) {
72                 struct page *vmpage = cp->cp_vmpage;
73                 struct inode *inode = vmpage->mapping->host;
74
75                 LASSERT(PageLocked(vmpage));
76                 LASSERT((struct cl_page *)vmpage->private == cp);
77
78                 /* Drop the reference count held in vvp_page_init */
79                 refcount_dec(&cp->cp_ref);
80
81                 ClearPagePrivate(vmpage);
82                 vmpage->private = 0;
83
84                 /* clearpageuptodate prevents the page being read by the
85                  * kernel after it has been deleted from Lustre, which avoids
86                  * potential stale data reads.  The seqlock allows us to see
87                  * that a page was potentially deleted and catch the resulting
88                  * SIGBUS - see ll_filemap_fault() (LU-16160)
89                  */
90                 if (PageUptodate(vmpage)) {
91                         write_seqlock(&ll_i2info(inode)->lli_page_inv_lock);
92                         ClearPageUptodate(vmpage);
93                         write_sequnlock(&ll_i2info(inode)->lli_page_inv_lock);
94                 }
95                 /* The reference from vmpage to cl_page is removed,
96                  * but the reference back is still here. It is removed
97                  * later in cl_page_free().
98                  */
99         }
100 }
101
102 /**
103  * Handles page transfer errors at VM level.
104  *
105  * This takes inode as a separate argument, because inode on which error is to
106  * be set can be different from \a vmpage inode in case of direct-io.
107  */
108 static void vvp_vmpage_error(struct inode *inode, struct page *vmpage,
109                              int ioret)
110 {
111         struct vvp_object *obj = cl_inode2vvp(inode);
112
113         if (ioret == 0) {
114                 ClearPageError(vmpage);
115                 obj->vob_discard_page_warned = 0;
116         } else {
117                 SetPageError(vmpage);
118                 if (ioret != -ENOSPC &&
119                     CFS_FAIL_CHECK(OBD_FAIL_LLITE_PANIC_ON_ESTALE))
120                         LBUG();
121
122                 mapping_set_error(inode->i_mapping, ioret);
123
124                 if ((ioret == -ESHUTDOWN || ioret == -EINTR ||
125                      ioret == -EIO) && obj->vob_discard_page_warned == 0) {
126                         obj->vob_discard_page_warned = 1;
127                         ll_dirty_page_discard_warn(inode, ioret);
128                 }
129         }
130 }
131
132 static void vvp_page_completion_read(const struct lu_env *env,
133                                      const struct cl_page_slice *slice,
134                                      int ioret)
135 {
136         struct cl_page *cp = slice->cpl_page;
137         struct page *vmpage = cp->cp_vmpage;
138         struct inode *inode = vvp_object_inode(cp->cp_obj);
139
140         ENTRY;
141         LASSERT(PageLocked(vmpage));
142         CL_PAGE_HEADER(D_PAGE, env, cp, "completing READ with %d\n", ioret);
143
144         if (cp->cp_defer_uptodate)
145                 ll_ra_count_put(ll_i2sbi(inode), 1);
146
147         if (ioret == 0)  {
148                 /**
149                  * cp_defer_uptodate is used for readahead page, and the
150                  * vmpage Uptodate bit is deferred to set in ll_readpage/
151                  * ll_io_read_page.
152                  */
153                 if (!cp->cp_defer_uptodate)
154                         SetPageUptodate(vmpage);
155         } else if (cp->cp_defer_uptodate) {
156                 cp->cp_defer_uptodate = 0;
157                 if (ioret == -EAGAIN) {
158                         /* mirror read failed, it needs to destroy the page
159                          * because subpage would be from wrong osc when trying
160                          * to read from a new mirror
161                          */
162                         generic_error_remove_folio(vmpage->mapping,
163                                                    page_folio(vmpage));
164                 }
165         }
166
167         if (cp->cp_sync_io == NULL)
168                 unlock_page(vmpage);
169
170         EXIT;
171 }
172
173 static void vvp_page_completion_write(const struct lu_env *env,
174                                       const struct cl_page_slice *slice,
175                                       int ioret)
176 {
177         struct cl_page *cp = slice->cpl_page;
178         struct page *vmpage = cp->cp_vmpage;
179
180         ENTRY;
181         CL_PAGE_HEADER(D_PAGE, env, cp, "completing WRITE with %d\n", ioret);
182
183         if (cp->cp_sync_io != NULL) {
184                 LASSERT(PageLocked(vmpage));
185                 LASSERT(!PageWriteback(vmpage));
186         } else {
187                 LASSERT(PageWriteback(vmpage));
188                 /*
189                  * Only mark the page error only when it's an async write
190                  * because applications won't wait for IO to finish.
191                  */
192                 vvp_vmpage_error(vvp_object_inode(cp->cp_obj), vmpage, ioret);
193
194                 end_page_writeback(vmpage);
195         }
196         EXIT;
197 }
198
199 static const struct cl_page_operations vvp_page_ops = {
200         .cpo_delete        = vvp_page_delete,
201         .cpo_discard       = vvp_page_discard,
202         .io = {
203                 [CRT_READ] = {
204                         .cpo_completion = vvp_page_completion_read,
205                 },
206                 [CRT_WRITE] = {
207                         .cpo_completion = vvp_page_completion_write,
208                 },
209         },
210 };
211
212 static const struct cl_page_operations vvp_transient_page_ops = {
213 };
214
215 int vvp_page_init(const struct lu_env *env, struct cl_object *obj,
216                 struct cl_page *page, pgoff_t index)
217 {
218         struct cl_page_slice *cpl = cl_object_page_slice(obj, page);
219         struct page *vmpage = page->cp_vmpage;
220
221         CLOBINVRNT(env, obj, vvp_object_invariant(obj));
222
223         if (page->cp_type == CPT_TRANSIENT) {
224                 /* DIO pages are referenced by userspace, we don't need to take
225                  * a reference on them. (contrast with get_page() call above)
226                  */
227                 cl_page_slice_add(page, cpl, obj,
228                                   &vvp_transient_page_ops);
229         } else {
230                 get_page(vmpage);
231                 /* in cache, decref in cl_page_delete() */
232                 refcount_inc(&page->cp_ref);
233                 SetPagePrivate(vmpage);
234                 vmpage->private = (unsigned long)page;
235                 cl_page_slice_add(page, cpl, obj, &vvp_page_ops);
236         }
237
238         return 0;
239 }