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