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