Whamcloud - gitweb
LU-13799 llite: Adjust dio refcounting
[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
56 static void vvp_page_fini_common(struct vvp_page *vpg, struct pagevec *pvec)
57 {
58         struct page *vmpage = vpg->vpg_page;
59
60         LASSERT(vmpage != NULL);
61         if (pvec) {
62                 if (!pagevec_add(pvec, vmpage))
63                         pagevec_release(pvec);
64         } else {
65                 put_page(vmpage);
66         }
67 }
68
69 static void vvp_page_fini(const struct lu_env *env,
70                           struct cl_page_slice *slice,
71                           struct pagevec *pvec)
72 {
73         struct vvp_page *vpg     = cl2vvp_page(slice);
74         struct page     *vmpage  = vpg->vpg_page;
75
76         /*
77          * vmpage->private was already cleared when page was moved into
78          * VPG_FREEING state.
79          */
80         LASSERT((struct cl_page *)vmpage->private != slice->cpl_page);
81         vvp_page_fini_common(vpg, pvec);
82 }
83
84 static int vvp_page_own(const struct lu_env *env,
85                         const struct cl_page_slice *slice, struct cl_io *io,
86                         int nonblock)
87 {
88         struct vvp_page *vpg    = cl2vvp_page(slice);
89         struct page     *vmpage = vpg->vpg_page;
90
91         ENTRY;
92
93         LASSERT(vmpage != NULL);
94         if (nonblock) {
95                 if (!trylock_page(vmpage))
96                         return -EAGAIN;
97
98                 if (unlikely(PageWriteback(vmpage))) {
99                         unlock_page(vmpage);
100                         return -EAGAIN;
101                 }
102
103                 return 0;
104         }
105
106         lock_page(vmpage);
107         wait_on_page_writeback(vmpage);
108
109         RETURN(0);
110 }
111
112 static void vvp_page_assume(const struct lu_env *env,
113                             const struct cl_page_slice *slice,
114                             struct cl_io *unused)
115 {
116         struct page *vmpage = cl2vm_page(slice);
117
118         LASSERT(vmpage != NULL);
119         LASSERT(PageLocked(vmpage));
120         wait_on_page_writeback(vmpage);
121 }
122
123 static void vvp_page_unassume(const struct lu_env *env,
124                               const struct cl_page_slice *slice,
125                               struct cl_io *unused)
126 {
127         struct page *vmpage = cl2vm_page(slice);
128
129         LASSERT(vmpage != NULL);
130         LASSERT(PageLocked(vmpage));
131 }
132
133 static void vvp_page_disown(const struct lu_env *env,
134                             const struct cl_page_slice *slice, struct cl_io *io)
135 {
136         struct page *vmpage = cl2vm_page(slice);
137
138         ENTRY;
139
140         LASSERT(vmpage != NULL);
141         LASSERT(PageLocked(vmpage));
142
143         unlock_page(cl2vm_page(slice));
144
145         EXIT;
146 }
147
148 static void vvp_page_discard(const struct lu_env *env,
149                              const struct cl_page_slice *slice,
150                              struct cl_io *unused)
151 {
152         struct page     *vmpage = cl2vm_page(slice);
153         struct vvp_page *vpg    = cl2vvp_page(slice);
154
155         LASSERT(vmpage != NULL);
156         LASSERT(PageLocked(vmpage));
157
158         if (vpg->vpg_defer_uptodate && !vpg->vpg_ra_used && vmpage->mapping)
159                 ll_ra_stats_inc(vmpage->mapping->host, RA_STAT_DISCARDED);
160
161         generic_error_remove_page(vmpage->mapping, vmpage);
162 }
163
164 static void vvp_page_delete(const struct lu_env *env,
165                             const struct cl_page_slice *slice)
166 {
167         struct page      *vmpage = cl2vm_page(slice);
168         struct cl_page   *page   = slice->cpl_page;
169         int refc;
170
171         LASSERT(PageLocked(vmpage));
172         LASSERT((struct cl_page *)vmpage->private == page);
173
174
175         /* Drop the reference count held in vvp_page_init */
176         refc = atomic_dec_return(&page->cp_ref);
177         LASSERTF(refc >= 1, "page = %p, refc = %d\n", page, refc);
178
179         ClearPagePrivate(vmpage);
180         vmpage->private = 0;
181
182         /**
183          * Vmpage might not be released due page refcount != 2,
184          * clear Page uptodate here to avoid stale data.
185          */
186         ClearPageUptodate(vmpage);
187         /*
188          * Reference from vmpage to cl_page is removed, but the reference back
189          * is still here. It is removed later in vvp_page_fini().
190          */
191 }
192
193 static void vvp_page_export(const struct lu_env *env,
194                             const struct cl_page_slice *slice,
195                             int uptodate)
196 {
197         struct page *vmpage = cl2vm_page(slice);
198
199         LASSERT(vmpage != NULL);
200         LASSERT(PageLocked(vmpage));
201         if (uptodate)
202                 SetPageUptodate(vmpage);
203         else
204                 ClearPageUptodate(vmpage);
205 }
206
207 static int vvp_page_is_vmlocked(const struct lu_env *env,
208                                 const struct cl_page_slice *slice)
209 {
210         return PageLocked(cl2vm_page(slice)) ? -EBUSY : -ENODATA;
211 }
212
213 static int vvp_page_prep_read(const struct lu_env *env,
214                               const struct cl_page_slice *slice,
215                               struct cl_io *unused)
216 {
217         ENTRY;
218         /* Skip the page already marked as PG_uptodate. */
219         RETURN(PageUptodate(cl2vm_page(slice)) ? -EALREADY : 0);
220 }
221
222 static int vvp_page_prep_write(const struct lu_env *env,
223                                const struct cl_page_slice *slice,
224                                struct cl_io *unused)
225 {
226         struct page *vmpage = cl2vm_page(slice);
227         struct cl_page *pg = slice->cpl_page;
228
229         LASSERT(PageLocked(vmpage));
230         LASSERT(!PageDirty(vmpage));
231
232         /* ll_writepage path is not a sync write, so need to set page writeback
233          * flag
234          */
235         if (pg->cp_sync_io == NULL)
236                 set_page_writeback(vmpage);
237
238         return 0;
239 }
240
241 /**
242  * Handles page transfer errors at VM level.
243  *
244  * This takes inode as a separate argument, because inode on which error is to
245  * be set can be different from \a vmpage inode in case of direct-io.
246  */
247 static void vvp_vmpage_error(struct inode *inode, struct page *vmpage,
248                              int ioret)
249 {
250         struct vvp_object *obj = cl_inode2vvp(inode);
251
252         if (ioret == 0) {
253                 ClearPageError(vmpage);
254                 obj->vob_discard_page_warned = 0;
255         } else {
256                 SetPageError(vmpage);
257                 if (ioret == -ENOSPC)
258                         set_bit(AS_ENOSPC, &inode->i_mapping->flags);
259                 else
260                         set_bit(AS_EIO, &inode->i_mapping->flags);
261
262                 if ((ioret == -ESHUTDOWN || ioret == -EINTR ||
263                      ioret == -EIO) && obj->vob_discard_page_warned == 0) {
264                         obj->vob_discard_page_warned = 1;
265                         ll_dirty_page_discard_warn(vmpage, ioret);
266                 }
267         }
268 }
269
270 static void vvp_page_completion_read(const struct lu_env *env,
271                                      const struct cl_page_slice *slice,
272                                      int ioret)
273 {
274         struct vvp_page *vpg    = cl2vvp_page(slice);
275         struct page     *vmpage = vpg->vpg_page;
276         struct cl_page  *page   = slice->cpl_page;
277         struct inode    *inode  = vvp_object_inode(page->cp_obj);
278
279         ENTRY;
280         LASSERT(PageLocked(vmpage));
281         CL_PAGE_HEADER(D_PAGE, env, page, "completing READ with %d\n", ioret);
282
283         if (vpg->vpg_defer_uptodate)
284                 ll_ra_count_put(ll_i2sbi(inode), 1);
285
286         if (ioret == 0)  {
287                 if (!vpg->vpg_defer_uptodate)
288                         cl_page_export(env, page, 1);
289         } else if (vpg->vpg_defer_uptodate) {
290                 vpg->vpg_defer_uptodate = 0;
291                 if (ioret == -EAGAIN) {
292                         /* mirror read failed, it needs to destroy the page
293                          * because subpage would be from wrong osc when trying
294                          * to read from a new mirror
295                          */
296                         generic_error_remove_page(vmpage->mapping, vmpage);
297                 }
298         }
299
300         if (page->cp_sync_io == NULL)
301                 unlock_page(vmpage);
302
303         EXIT;
304 }
305
306 static void vvp_page_completion_write(const struct lu_env *env,
307                                       const struct cl_page_slice *slice,
308                                       int ioret)
309 {
310         struct vvp_page *vpg    = cl2vvp_page(slice);
311         struct cl_page  *pg     = slice->cpl_page;
312         struct page     *vmpage = vpg->vpg_page;
313
314         ENTRY;
315         CL_PAGE_HEADER(D_PAGE, env, pg, "completing WRITE with %d\n", ioret);
316
317         if (pg->cp_sync_io != NULL) {
318                 LASSERT(PageLocked(vmpage));
319                 LASSERT(!PageWriteback(vmpage));
320         } else {
321                 LASSERT(PageWriteback(vmpage));
322                 /*
323                  * Only mark the page error only when it's an async write
324                  * because applications won't wait for IO to finish.
325                  */
326                 vvp_vmpage_error(vvp_object_inode(pg->cp_obj), vmpage, ioret);
327
328                 end_page_writeback(vmpage);
329         }
330         EXIT;
331 }
332
333 /**
334  * Implements cl_page_operations::cpo_make_ready() method.
335  *
336  * This is called to yank a page from the transfer cache and to send it out as
337  * a part of transfer. This function try-locks the page. If try-lock failed,
338  * page is owned by some concurrent IO, and should be skipped (this is bad,
339  * but hopefully rare situation, as it usually results in transfer being
340  * shorter than possible).
341  *
342  * \retval 0      success, page can be placed into transfer
343  *
344  * \retval -EAGAIN page is either used by concurrent IO has been
345  * truncated. Skip it.
346  */
347 static int vvp_page_make_ready(const struct lu_env *env,
348                                const struct cl_page_slice *slice)
349 {
350         struct page *vmpage = cl2vm_page(slice);
351         struct cl_page *pg = slice->cpl_page;
352         int result = 0;
353
354         lock_page(vmpage);
355         if (clear_page_dirty_for_io(vmpage)) {
356                 LASSERT(pg->cp_state == CPS_CACHED);
357                 /* This actually clears the dirty bit in the radix
358                  * tree.
359                  */
360                 set_page_writeback(vmpage);
361                 CL_PAGE_HEADER(D_PAGE, env, pg, "readied\n");
362         } else if (pg->cp_state == CPS_PAGEOUT) {
363                 /* is it possible for osc_flush_async_page() to already
364                  * make it ready?
365                  */
366                 result = -EALREADY;
367         } else {
368                 CL_PAGE_DEBUG(D_ERROR, env, pg, "Unexpecting page state %d.\n",
369                               pg->cp_state);
370                 LBUG();
371         }
372         unlock_page(vmpage);
373         RETURN(result);
374 }
375
376 static int vvp_page_print(const struct lu_env *env,
377                           const struct cl_page_slice *slice,
378                           void *cookie, lu_printer_t printer)
379 {
380         struct vvp_page *vpg    = cl2vvp_page(slice);
381         struct page     *vmpage = vpg->vpg_page;
382
383         (*printer)(env, cookie,
384                    LUSTRE_VVP_NAME"-page@%p(%d:%d) vm@%p ",
385                    vpg, vpg->vpg_defer_uptodate, vpg->vpg_ra_used, vmpage);
386
387         if (vmpage != NULL) {
388                 (*printer)(env, cookie, "%lx %d:%d %lx %lu %slru",
389                            (long)vmpage->flags, page_count(vmpage),
390                            page_mapcount(vmpage), vmpage->private,
391                            page_index(vmpage),
392                            list_empty(&vmpage->lru) ? "not-" : "");
393         }
394
395         (*printer)(env, cookie, "\n");
396
397         return 0;
398 }
399
400 static int vvp_page_fail(const struct lu_env *env,
401                          const struct cl_page_slice *slice)
402 {
403         /*
404          * Cached read?
405          */
406         LBUG();
407
408         return 0;
409 }
410
411 static const struct cl_page_operations vvp_page_ops = {
412         .cpo_own           = vvp_page_own,
413         .cpo_assume        = vvp_page_assume,
414         .cpo_unassume      = vvp_page_unassume,
415         .cpo_disown        = vvp_page_disown,
416         .cpo_discard       = vvp_page_discard,
417         .cpo_delete        = vvp_page_delete,
418         .cpo_export        = vvp_page_export,
419         .cpo_is_vmlocked   = vvp_page_is_vmlocked,
420         .cpo_fini          = vvp_page_fini,
421         .cpo_print         = vvp_page_print,
422         .io = {
423                 [CRT_READ] = {
424                         .cpo_prep       = vvp_page_prep_read,
425                         .cpo_completion = vvp_page_completion_read,
426                         .cpo_make_ready = vvp_page_fail,
427                 },
428                 [CRT_WRITE] = {
429                         .cpo_prep       = vvp_page_prep_write,
430                         .cpo_completion = vvp_page_completion_write,
431                         .cpo_make_ready = vvp_page_make_ready,
432                 },
433         },
434 };
435
436 static void vvp_transient_page_discard(const struct lu_env *env,
437                                        const struct cl_page_slice *slice,
438                                        struct cl_io *unused)
439 {
440         struct cl_page *page = slice->cpl_page;
441
442         /*
443          * For transient pages, remove it from the radix tree.
444          */
445         cl_page_delete(env, page);
446 }
447
448 static int vvp_transient_page_is_vmlocked(const struct lu_env *env,
449                                           const struct cl_page_slice *slice)
450 {
451         return -EBUSY;
452 }
453
454 static void vvp_transient_page_fini(const struct lu_env *env,
455                                     struct cl_page_slice *slice,
456                                     struct pagevec *pvec)
457 {
458         struct vvp_page *vpg = cl2vvp_page(slice);
459
460         vvp_page_fini_common(vpg, pvec);
461 }
462
463 static const struct cl_page_operations vvp_transient_page_ops = {
464         .cpo_discard            = vvp_transient_page_discard,
465         .cpo_fini               = vvp_transient_page_fini,
466         .cpo_is_vmlocked        = vvp_transient_page_is_vmlocked,
467         .cpo_print              = vvp_page_print,
468 };
469
470 int vvp_page_init(const struct lu_env *env, struct cl_object *obj,
471                 struct cl_page *page, pgoff_t index)
472 {
473         struct vvp_page *vpg = cl_object_page_slice(obj, page);
474         struct page     *vmpage = page->cp_vmpage;
475
476         CLOBINVRNT(env, obj, vvp_object_invariant(obj));
477
478         vpg->vpg_page = vmpage;
479         get_page(vmpage);
480
481         if (page->cp_type == CPT_TRANSIENT) {
482                 /* DIO pages are referenced by userspace, we don't need to take
483                  * a reference on them. (contrast with get_page() call above)
484                  */
485                 cl_page_slice_add(page, &vpg->vpg_cl, obj,
486                                   &vvp_transient_page_ops);
487         } else {
488                 /* in cache, decref in vvp_page_delete */
489                 atomic_inc(&page->cp_ref);
490                 SetPagePrivate(vmpage);
491                 vmpage->private = (unsigned long)page;
492                 cl_page_slice_add(page, &vpg->vpg_cl, obj,
493                                 &vvp_page_ops);
494         }
495
496         return 0;
497 }