Whamcloud - gitweb
LU-5971 llite: rename ccc_page to vvp_page
[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         vvp_write_complete(cl2vvp(obj), cl2vvp_page(slice));
171
172         /* Drop the reference count held in vvp_page_init */
173         refc = atomic_dec_return(&page->cp_ref);
174         LASSERTF(refc >= 1, "page = %p, refc = %d\n", page, refc);
175
176         ClearPageUptodate(vmpage);
177         ClearPagePrivate(vmpage);
178         vmpage->private = 0;
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
220         LASSERT(PageLocked(vmpage));
221         LASSERT(!PageDirty(vmpage));
222
223         set_page_writeback(vmpage);
224         vvp_write_pending(cl2vvp(slice->cpl_obj), cl2vvp_page(slice));
225
226         return 0;
227 }
228
229 /**
230  * Handles page transfer errors at VM level.
231  *
232  * This takes inode as a separate argument, because inode on which error is to
233  * be set can be different from \a vmpage inode in case of direct-io.
234  */
235 static void vvp_vmpage_error(struct inode *inode, struct page *vmpage, int ioret)
236 {
237         struct vvp_object *obj = cl_inode2vvp(inode);
238
239         if (ioret == 0) {
240                 ClearPageError(vmpage);
241                 obj->vob_discard_page_warned = 0;
242         } else {
243                 SetPageError(vmpage);
244                 if (ioret == -ENOSPC)
245                         set_bit(AS_ENOSPC, &inode->i_mapping->flags);
246                 else
247                         set_bit(AS_EIO, &inode->i_mapping->flags);
248
249                 if ((ioret == -ESHUTDOWN || ioret == -EINTR) &&
250                      obj->vob_discard_page_warned == 0) {
251                         obj->vob_discard_page_warned = 1;
252                         ll_dirty_page_discard_warn(vmpage, ioret);
253                 }
254         }
255 }
256
257 static void vvp_page_completion_read(const struct lu_env *env,
258                                      const struct cl_page_slice *slice,
259                                      int ioret)
260 {
261         struct vvp_page *vpg    = cl2vvp_page(slice);
262         struct page     *vmpage = vpg->vpg_page;
263         struct cl_page  *page   = slice->cpl_page;
264         struct inode    *inode  = vvp_object_inode(page->cp_obj);
265         ENTRY;
266
267         LASSERT(PageLocked(vmpage));
268         CL_PAGE_HEADER(D_PAGE, env, page, "completing READ with %d\n", ioret);
269
270         if (vpg->vpg_defer_uptodate)
271                 ll_ra_count_put(ll_i2sbi(inode), 1);
272
273         if (ioret == 0)  {
274                 if (!vpg->vpg_defer_uptodate)
275                         cl_page_export(env, page, 1);
276         } else {
277                 vpg->vpg_defer_uptodate = 0;
278         }
279
280         if (page->cp_sync_io == NULL)
281                 unlock_page(vmpage);
282
283         EXIT;
284 }
285
286 static void vvp_page_completion_write(const struct lu_env *env,
287                                       const struct cl_page_slice *slice,
288                                       int ioret)
289 {
290         struct vvp_page *vpg    = cl2vvp_page(slice);
291         struct cl_page  *pg     = slice->cpl_page;
292         struct page     *vmpage = vpg->vpg_page;
293         ENTRY;
294
295         LASSERT(ergo(pg->cp_sync_io != NULL, PageLocked(vmpage)));
296         LASSERT(PageWriteback(vmpage));
297
298         CL_PAGE_HEADER(D_PAGE, env, pg, "completing WRITE with %d\n", ioret);
299
300         /*
301          * TODO: Actually it makes sense to add the page into oap pending
302          * list again and so that we don't need to take the page out from
303          * SoM write pending list, if we just meet a recoverable error,
304          * -ENOMEM, etc.
305          * To implement this, we just need to return a non zero value in
306          * ->cpo_completion method. The underlying transfer should be notified
307          * and then re-add the page into pending transfer queue.  -jay
308          */
309
310         vpg->vpg_write_queued = 0;
311         vvp_write_complete(cl2vvp(slice->cpl_obj), vpg);
312
313         /*
314          * Only mark the page error only when it's an async write because
315          * applications won't wait for IO to finish.
316          */
317         if (pg->cp_sync_io == NULL)
318                 vvp_vmpage_error(vvp_object_inode(pg->cp_obj), vmpage, ioret);
319
320         end_page_writeback(vmpage);
321         EXIT;
322 }
323
324 /**
325  * Implements cl_page_operations::cpo_make_ready() method.
326  *
327  * This is called to yank a page from the transfer cache and to send it out as
328  * a part of transfer. This function try-locks the page. If try-lock failed,
329  * page is owned by some concurrent IO, and should be skipped (this is bad,
330  * but hopefully rare situation, as it usually results in transfer being
331  * shorter than possible).
332  *
333  * \retval 0      success, page can be placed into transfer
334  *
335  * \retval -EAGAIN page is either used by concurrent IO has been
336  * truncated. Skip it.
337  */
338 static int vvp_page_make_ready(const struct lu_env *env,
339                                const struct cl_page_slice *slice)
340 {
341         struct page *vmpage = cl2vm_page(slice);
342         struct cl_page *pg = slice->cpl_page;
343         int result = 0;
344
345         lock_page(vmpage);
346         if (clear_page_dirty_for_io(vmpage)) {
347                 LASSERT(pg->cp_state == CPS_CACHED);
348                 /* This actually clears the dirty bit in the radix
349                  * tree. */
350                 set_page_writeback(vmpage);
351                 vvp_write_pending(cl2vvp(slice->cpl_obj),
352                                 cl2vvp_page(slice));
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                 result = -EALREADY;
358         } else {
359                 CL_PAGE_DEBUG(D_ERROR, env, pg, "Unexpecting page state %d.\n",
360                               pg->cp_state);
361                 LBUG();
362         }
363         unlock_page(vmpage);
364         RETURN(result);
365 }
366
367 static int vvp_page_is_under_lock(const struct lu_env *env,
368                                   const struct cl_page_slice *slice,
369                                   struct cl_io *io, pgoff_t *max_index)
370 {
371         ENTRY;
372
373         if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
374             io->ci_type == CIT_FAULT) {
375                 struct ccc_io *cio = ccc_env_io(env);
376
377                 if (unlikely(cio->cui_fd->fd_flags & LL_FILE_GROUP_LOCKED))
378                         *max_index = CL_PAGE_EOF;
379         }
380         RETURN(0);
381 }
382
383
384 static int vvp_page_print(const struct lu_env *env,
385                           const struct cl_page_slice *slice,
386                           void *cookie, lu_printer_t printer)
387 {
388         struct vvp_page *vpg    = cl2vvp_page(slice);
389         struct page     *vmpage = vpg->vpg_page;
390
391         (*printer)(env, cookie, LUSTRE_VVP_NAME"-page@%p(%d:%d:%d) "
392                    "vm@%p ",
393                    vpg, vpg->vpg_defer_uptodate, vpg->vpg_ra_used,
394                    vpg->vpg_write_queued, vmpage);
395
396         if (vmpage != NULL) {
397                 (*printer)(env, cookie, "%lx %d:%d %lx %lu %slru",
398                            (long)vmpage->flags, page_count(vmpage),
399                            page_mapcount(vmpage), vmpage->private,
400                            page_index(vmpage),
401                            list_empty(&vmpage->lru) ? "not-" : "");
402         }
403
404         (*printer)(env, cookie, "\n");
405
406         return 0;
407 }
408
409 static int vvp_page_fail(const struct lu_env *env,
410                          const struct cl_page_slice *slice)
411 {
412         /*
413          * Cached read?
414          */
415         LBUG();
416
417         return 0;
418 }
419
420 static const struct cl_page_operations vvp_page_ops = {
421         .cpo_own           = vvp_page_own,
422         .cpo_assume        = vvp_page_assume,
423         .cpo_unassume      = vvp_page_unassume,
424         .cpo_disown        = vvp_page_disown,
425         .cpo_discard       = vvp_page_discard,
426         .cpo_delete        = vvp_page_delete,
427         .cpo_export        = vvp_page_export,
428         .cpo_is_vmlocked   = vvp_page_is_vmlocked,
429         .cpo_fini          = vvp_page_fini,
430         .cpo_print         = vvp_page_print,
431         .cpo_is_under_lock = vvp_page_is_under_lock,
432         .io = {
433                 [CRT_READ] = {
434                         .cpo_prep       = vvp_page_prep_read,
435                         .cpo_completion = vvp_page_completion_read,
436                         .cpo_make_ready = vvp_page_fail,
437                 },
438                 [CRT_WRITE] = {
439                         .cpo_prep       = vvp_page_prep_write,
440                         .cpo_completion = vvp_page_completion_write,
441                         .cpo_make_ready = vvp_page_make_ready,
442                 },
443         },
444 };
445
446 static int vvp_transient_page_prep(const struct lu_env *env,
447                                    const struct cl_page_slice *slice,
448                                    struct cl_io *unused)
449 {
450         ENTRY;
451         /* transient page should always be sent. */
452         RETURN(0);
453 }
454
455 static void vvp_transient_page_verify(const struct cl_page *page)
456 {
457 }
458
459 static int vvp_transient_page_own(const struct lu_env *env,
460                                   const struct cl_page_slice *slice,
461                                   struct cl_io *unused, int nonblock)
462 {
463         vvp_transient_page_verify(slice->cpl_page);
464         return 0;
465 }
466
467 static void vvp_transient_page_assume(const struct lu_env *env,
468                                       const struct cl_page_slice *slice,
469                                       struct cl_io *unused)
470 {
471         vvp_transient_page_verify(slice->cpl_page);
472 }
473
474 static void vvp_transient_page_unassume(const struct lu_env *env,
475                                         const struct cl_page_slice *slice,
476                                         struct cl_io *unused)
477 {
478         vvp_transient_page_verify(slice->cpl_page);
479 }
480
481 static void vvp_transient_page_disown(const struct lu_env *env,
482                                       const struct cl_page_slice *slice,
483                                       struct cl_io *unused)
484 {
485         vvp_transient_page_verify(slice->cpl_page);
486 }
487
488 static void vvp_transient_page_discard(const struct lu_env *env,
489                                        const struct cl_page_slice *slice,
490                                        struct cl_io *unused)
491 {
492         struct cl_page *page = slice->cpl_page;
493
494         vvp_transient_page_verify(slice->cpl_page);
495
496         /*
497          * For transient pages, remove it from the radix tree.
498          */
499         cl_page_delete(env, page);
500 }
501
502 static int vvp_transient_page_is_vmlocked(const struct lu_env *env,
503                                           const struct cl_page_slice *slice)
504 {
505         struct inode    *inode = vvp_object_inode(slice->cpl_obj);
506         int     locked;
507
508         locked = !mutex_trylock(&inode->i_mutex);
509         if (!locked)
510                 mutex_unlock(&inode->i_mutex);
511         return locked ? -EBUSY : -ENODATA;
512 }
513
514 static void
515 vvp_transient_page_completion(const struct lu_env *env,
516                               const struct cl_page_slice *slice,
517                               int ioret)
518 {
519         vvp_transient_page_verify(slice->cpl_page);
520 }
521
522 static void vvp_transient_page_fini(const struct lu_env *env,
523                                     struct cl_page_slice *slice)
524 {
525         struct vvp_page *vpg = cl2vvp_page(slice);
526         struct cl_page *clp = slice->cpl_page;
527         struct vvp_object *clobj = cl2vvp(clp->cp_obj);
528
529         vvp_page_fini_common(vpg);
530         atomic_dec(&clobj->vob_transient_pages);
531 }
532
533 static const struct cl_page_operations vvp_transient_page_ops = {
534         .cpo_own                = vvp_transient_page_own,
535         .cpo_assume             = vvp_transient_page_assume,
536         .cpo_unassume           = vvp_transient_page_unassume,
537         .cpo_disown             = vvp_transient_page_disown,
538         .cpo_discard            = vvp_transient_page_discard,
539         .cpo_fini               = vvp_transient_page_fini,
540         .cpo_is_vmlocked        = vvp_transient_page_is_vmlocked,
541         .cpo_print              = vvp_page_print,
542         .cpo_is_under_lock      = vvp_page_is_under_lock,
543         .io = {
544                 [CRT_READ] = {
545                         .cpo_prep       = vvp_transient_page_prep,
546                         .cpo_completion = vvp_transient_page_completion,
547                 },
548                 [CRT_WRITE] = {
549                         .cpo_prep       = vvp_transient_page_prep,
550                         .cpo_completion = vvp_transient_page_completion,
551                 }
552         }
553 };
554
555 int vvp_page_init(const struct lu_env *env, struct cl_object *obj,
556                 struct cl_page *page, pgoff_t index)
557 {
558         struct vvp_page *vpg = cl_object_page_slice(obj, page);
559         struct page     *vmpage = page->cp_vmpage;
560
561         CLOBINVRNT(env, obj, vvp_object_invariant(obj));
562
563         vpg->vpg_page = vmpage;
564         page_cache_get(vmpage);
565
566         INIT_LIST_HEAD(&vpg->vpg_pending_linkage);
567         if (page->cp_type == CPT_CACHEABLE) {
568                 /* in cache, decref in vvp_page_delete */
569                 atomic_inc(&page->cp_ref);
570                 SetPagePrivate(vmpage);
571                 vmpage->private = (unsigned long)page;
572                 cl_page_slice_add(page, &vpg->vpg_cl, obj, index,
573                                 &vvp_page_ops);
574         } else {
575                 struct vvp_object *clobj = cl2vvp(obj);
576
577                 cl_page_slice_add(page, &vpg->vpg_cl, obj, index,
578                                 &vvp_transient_page_ops);
579                 atomic_inc(&clobj->vob_transient_pages);
580         }
581         return 0;
582 }