Whamcloud - gitweb
LU-10994 clio: remove cpo_own and cpo_disown
[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_fini(const struct lu_env *env,
56                           struct cl_page_slice *slice,
57                           struct pagevec *pvec)
58 {
59         struct vvp_page *vpg     = cl2vvp_page(slice);
60         struct page     *vmpage  = vpg->vpg_page;
61
62         /*
63          * vmpage->private was already cleared when page was moved into
64          * VPG_FREEING state.
65          */
66         LASSERT((struct cl_page *)vmpage->private != slice->cpl_page);
67         LASSERT(vmpage != NULL);
68         if (pvec) {
69                 if (!pagevec_add(pvec, vmpage))
70                         pagevec_release(pvec);
71         } else {
72                 put_page(vmpage);
73         }
74 }
75
76 static void vvp_page_assume(const struct lu_env *env,
77                             const struct cl_page_slice *slice,
78                             struct cl_io *unused)
79 {
80         struct page *vmpage = cl2vm_page(slice);
81
82         LASSERT(vmpage != NULL);
83         LASSERT(PageLocked(vmpage));
84         wait_on_page_writeback(vmpage);
85 }
86
87 static void vvp_page_unassume(const struct lu_env *env,
88                               const struct cl_page_slice *slice,
89                               struct cl_io *unused)
90 {
91         struct page *vmpage = cl2vm_page(slice);
92
93         LASSERT(vmpage != NULL);
94         LASSERT(PageLocked(vmpage));
95 }
96
97 static void vvp_page_discard(const struct lu_env *env,
98                              const struct cl_page_slice *slice,
99                              struct cl_io *unused)
100 {
101         struct cl_page *cp = slice->cpl_page;
102         struct page *vmpage = cp->cp_vmpage;
103
104         if (cp->cp_defer_uptodate && !cp->cp_ra_used && vmpage->mapping != NULL)
105                 ll_ra_stats_inc(vmpage->mapping->host, RA_STAT_DISCARDED);
106 }
107
108 static void vvp_page_delete(const struct lu_env *env,
109                             const struct cl_page_slice *slice)
110 {
111         struct page      *vmpage = cl2vm_page(slice);
112         struct cl_page   *page   = slice->cpl_page;
113         int refc;
114
115         LASSERT(PageLocked(vmpage));
116         LASSERT((struct cl_page *)vmpage->private == page);
117
118
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);
122
123         ClearPagePrivate(vmpage);
124         vmpage->private = 0;
125         /*
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().
128          */
129 }
130
131 static int vvp_page_prep_read(const struct lu_env *env,
132                               const struct cl_page_slice *slice,
133                               struct cl_io *unused)
134 {
135         ENTRY;
136         /* Skip the page already marked as PG_uptodate. */
137         RETURN(PageUptodate(cl2vm_page(slice)) ? -EALREADY : 0);
138 }
139
140 static int vvp_page_prep_write(const struct lu_env *env,
141                                const struct cl_page_slice *slice,
142                                struct cl_io *unused)
143 {
144         struct page *vmpage = cl2vm_page(slice);
145         struct cl_page *pg = slice->cpl_page;
146
147         LASSERT(PageLocked(vmpage));
148         LASSERT(!PageDirty(vmpage));
149
150         /* ll_writepage path is not a sync write, so need to set page writeback
151          * flag
152          */
153         if (pg->cp_sync_io == NULL)
154                 set_page_writeback(vmpage);
155
156         return 0;
157 }
158
159 /**
160  * Handles page transfer errors at VM level.
161  *
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.
164  */
165 static void vvp_vmpage_error(struct inode *inode, struct page *vmpage,
166                              int ioret)
167 {
168         struct vvp_object *obj = cl_inode2vvp(inode);
169
170         if (ioret == 0) {
171                 ClearPageError(vmpage);
172                 obj->vob_discard_page_warned = 0;
173         } else {
174                 SetPageError(vmpage);
175                 if (ioret == -ENOSPC)
176                         set_bit(AS_ENOSPC, &inode->i_mapping->flags);
177                 else
178                         set_bit(AS_EIO, &inode->i_mapping->flags);
179
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);
184                 }
185         }
186 }
187
188 static void vvp_page_completion_read(const struct lu_env *env,
189                                      const struct cl_page_slice *slice,
190                                      int ioret)
191 {
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);
195
196         ENTRY;
197         LASSERT(PageLocked(vmpage));
198         CL_PAGE_HEADER(D_PAGE, env, cp, "completing READ with %d\n", ioret);
199
200         if (cp->cp_defer_uptodate)
201                 ll_ra_count_put(ll_i2sbi(inode), 1);
202
203         if (ioret == 0)  {
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
212                          */
213                         generic_error_remove_page(vmpage->mapping, vmpage);
214                 }
215         }
216
217         if (cp->cp_sync_io == NULL)
218                 unlock_page(vmpage);
219
220         EXIT;
221 }
222
223 static void vvp_page_completion_write(const struct lu_env *env,
224                                       const struct cl_page_slice *slice,
225                                       int ioret)
226 {
227         struct vvp_page *vpg    = cl2vvp_page(slice);
228         struct cl_page  *pg     = slice->cpl_page;
229         struct page     *vmpage = vpg->vpg_page;
230
231         ENTRY;
232         CL_PAGE_HEADER(D_PAGE, env, pg, "completing WRITE with %d\n", ioret);
233
234         if (pg->cp_sync_io != NULL) {
235                 LASSERT(PageLocked(vmpage));
236                 LASSERT(!PageWriteback(vmpage));
237         } else {
238                 LASSERT(PageWriteback(vmpage));
239                 /*
240                  * Only mark the page error only when it's an async write
241                  * because applications won't wait for IO to finish.
242                  */
243                 vvp_vmpage_error(vvp_object_inode(pg->cp_obj), vmpage, ioret);
244
245                 end_page_writeback(vmpage);
246         }
247         EXIT;
248 }
249
250 /**
251  * Implements cl_page_operations::cpo_make_ready() method.
252  *
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).
258  *
259  * \retval 0      success, page can be placed into transfer
260  *
261  * \retval -EAGAIN page is either used by concurrent IO has been
262  * truncated. Skip it.
263  */
264 static int vvp_page_make_ready(const struct lu_env *env,
265                                const struct cl_page_slice *slice)
266 {
267         struct page *vmpage = cl2vm_page(slice);
268         struct cl_page *pg = slice->cpl_page;
269         int result = 0;
270
271         lock_page(vmpage);
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
275                  * tree.
276                  */
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
281                  * make it ready?
282                  */
283                 result = -EALREADY;
284         } else {
285                 CL_PAGE_DEBUG(D_ERROR, env, pg, "Unexpecting page state %d.\n",
286                               pg->cp_state);
287                 LBUG();
288         }
289         unlock_page(vmpage);
290         RETURN(result);
291 }
292
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)
296 {
297         struct vvp_page *vpg    = cl2vvp_page(slice);
298         struct page     *vmpage = vpg->vpg_page;
299
300         (*printer)(env, cookie,
301                    LUSTRE_VVP_NAME"-page@%p vm@%p ", vpg, vmpage);
302
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,
307                            page_index(vmpage),
308                            list_empty(&vmpage->lru) ? "not-" : "");
309         }
310
311         (*printer)(env, cookie, "\n");
312
313         return 0;
314 }
315
316 static int vvp_page_fail(const struct lu_env *env,
317                          const struct cl_page_slice *slice)
318 {
319         /*
320          * Cached read?
321          */
322         LBUG();
323
324         return 0;
325 }
326
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,
334         .io = {
335                 [CRT_READ] = {
336                         .cpo_prep       = vvp_page_prep_read,
337                         .cpo_completion = vvp_page_completion_read,
338                         .cpo_make_ready = vvp_page_fail,
339                 },
340                 [CRT_WRITE] = {
341                         .cpo_prep       = vvp_page_prep_write,
342                         .cpo_completion = vvp_page_completion_write,
343                         .cpo_make_ready = vvp_page_make_ready,
344                 },
345         },
346 };
347
348 static const struct cl_page_operations vvp_transient_page_ops = {
349         .cpo_print              = vvp_page_print,
350 };
351
352 int vvp_page_init(const struct lu_env *env, struct cl_object *obj,
353                 struct cl_page *page, pgoff_t index)
354 {
355         struct vvp_page *vpg = cl_object_page_slice(obj, page);
356         struct page     *vmpage = page->cp_vmpage;
357
358         CLOBINVRNT(env, obj, vvp_object_invariant(obj));
359
360         vpg->vpg_page = vmpage;
361
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)
365                  */
366                 cl_page_slice_add(page, &vpg->vpg_cl, obj,
367                                   &vvp_transient_page_ops);
368         } else {
369                 get_page(vmpage);
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,
375                                 &vvp_page_ops);
376         }
377
378         return 0;
379 }