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