Whamcloud - gitweb
LU-16335 test: add fail_abort_cleanup()
[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 /**
67  * Handles page transfer errors at VM level.
68  *
69  * This takes inode as a separate argument, because inode on which error is to
70  * be set can be different from \a vmpage inode in case of direct-io.
71  */
72 static void vvp_vmpage_error(struct inode *inode, struct page *vmpage,
73                              int ioret)
74 {
75         struct vvp_object *obj = cl_inode2vvp(inode);
76
77         if (ioret == 0) {
78                 ClearPageError(vmpage);
79                 obj->vob_discard_page_warned = 0;
80         } else {
81                 SetPageError(vmpage);
82                 if (ioret == -ENOSPC)
83                         set_bit(AS_ENOSPC, &inode->i_mapping->flags);
84                 else
85                         set_bit(AS_EIO, &inode->i_mapping->flags);
86
87                 if ((ioret == -ESHUTDOWN || ioret == -EINTR ||
88                      ioret == -EIO) && obj->vob_discard_page_warned == 0) {
89                         obj->vob_discard_page_warned = 1;
90                         ll_dirty_page_discard_warn(inode, ioret);
91                 }
92         }
93 }
94
95 static void vvp_page_completion_read(const struct lu_env *env,
96                                      const struct cl_page_slice *slice,
97                                      int ioret)
98 {
99         struct cl_page *cp = slice->cpl_page;
100         struct page *vmpage = cp->cp_vmpage;
101         struct inode *inode = vvp_object_inode(cp->cp_obj);
102
103         ENTRY;
104         LASSERT(PageLocked(vmpage));
105         CL_PAGE_HEADER(D_PAGE, env, cp, "completing READ with %d\n", ioret);
106
107         if (cp->cp_defer_uptodate)
108                 ll_ra_count_put(ll_i2sbi(inode), 1);
109
110         if (ioret == 0)  {
111                 /**
112                  * cp_defer_uptodate is used for readahead page, and the
113                  * vmpage Uptodate bit is deferred to set in ll_readpage/
114                  * ll_io_read_page.
115                  */
116                 if (!cp->cp_defer_uptodate)
117                         SetPageUptodate(vmpage);
118         } else if (cp->cp_defer_uptodate) {
119                 cp->cp_defer_uptodate = 0;
120                 if (ioret == -EAGAIN) {
121                         /* mirror read failed, it needs to destroy the page
122                          * because subpage would be from wrong osc when trying
123                          * to read from a new mirror
124                          */
125                         generic_error_remove_page(vmpage->mapping, vmpage);
126                 }
127         }
128
129         if (cp->cp_sync_io == NULL)
130                 unlock_page(vmpage);
131
132         EXIT;
133 }
134
135 static void vvp_page_completion_write(const struct lu_env *env,
136                                       const struct cl_page_slice *slice,
137                                       int ioret)
138 {
139         struct cl_page *cp = slice->cpl_page;
140         struct page *vmpage = cp->cp_vmpage;
141
142         ENTRY;
143         CL_PAGE_HEADER(D_PAGE, env, cp, "completing WRITE with %d\n", ioret);
144
145         if (cp->cp_sync_io != NULL) {
146                 LASSERT(PageLocked(vmpage));
147                 LASSERT(!PageWriteback(vmpage));
148         } else {
149                 LASSERT(PageWriteback(vmpage));
150                 /*
151                  * Only mark the page error only when it's an async write
152                  * because applications won't wait for IO to finish.
153                  */
154                 vvp_vmpage_error(vvp_object_inode(cp->cp_obj), vmpage, ioret);
155
156                 end_page_writeback(vmpage);
157         }
158         EXIT;
159 }
160
161 static const struct cl_page_operations vvp_page_ops = {
162         .cpo_discard       = vvp_page_discard,
163         .io = {
164                 [CRT_READ] = {
165                         .cpo_completion = vvp_page_completion_read,
166                 },
167                 [CRT_WRITE] = {
168                         .cpo_completion = vvp_page_completion_write,
169                 },
170         },
171 };
172
173 static const struct cl_page_operations vvp_transient_page_ops = {
174 };
175
176 int vvp_page_init(const struct lu_env *env, struct cl_object *obj,
177                 struct cl_page *page, pgoff_t index)
178 {
179         struct cl_page_slice *cpl = cl_object_page_slice(obj, page);
180         struct page *vmpage = page->cp_vmpage;
181
182         CLOBINVRNT(env, obj, vvp_object_invariant(obj));
183
184         if (page->cp_type == CPT_TRANSIENT) {
185                 /* DIO pages are referenced by userspace, we don't need to take
186                  * a reference on them. (contrast with get_page() call above)
187                  */
188                 cl_page_slice_add(page, cpl, obj,
189                                   &vvp_transient_page_ops);
190         } else {
191                 get_page(vmpage);
192                 /* in cache, decref in cl_page_delete() */
193                 refcount_inc(&page->cp_ref);
194                 SetPagePrivate(vmpage);
195                 vmpage->private = (unsigned long)page;
196                 cl_page_slice_add(page, cpl, obj, &vvp_page_ops);
197         }
198
199         return 0;
200 }