Whamcloud - gitweb
LU-4788 lfsck: replace cfs_list_t with list_head
[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, 2013, 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
45 #include <obd.h>
46 #include <lustre_lite.h>
47
48 #include "vvp_internal.h"
49
50 /*****************************************************************************
51  *
52  * Page operations.
53  *
54  */
55
56 static void vvp_page_fini_common(struct ccc_page *cp)
57 {
58         struct page *vmpage = cp->cpg_page;
59
60         LASSERT(vmpage != NULL);
61         page_cache_release(vmpage);
62 }
63
64 static void vvp_page_fini(const struct lu_env *env,
65                           struct cl_page_slice *slice)
66 {
67         struct ccc_page *cp = cl2ccc_page(slice);
68         struct page *vmpage  = cp->cpg_page;
69
70         /*
71          * vmpage->private was already cleared when page was moved into
72          * VPG_FREEING state.
73          */
74         LASSERT((struct cl_page *)vmpage->private != slice->cpl_page);
75         vvp_page_fini_common(cp);
76 }
77
78 static int vvp_page_own(const struct lu_env *env,
79                         const struct cl_page_slice *slice, struct cl_io *io,
80                         int nonblock)
81 {
82         struct ccc_page *vpg    = cl2ccc_page(slice);
83         struct page      *vmpage = vpg->cpg_page;
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         return 0;
101 }
102
103 static void vvp_page_assume(const struct lu_env *env,
104                             const struct cl_page_slice *slice,
105                             struct cl_io *unused)
106 {
107         struct page *vmpage = cl2vm_page(slice);
108
109         LASSERT(vmpage != NULL);
110         LASSERT(PageLocked(vmpage));
111         wait_on_page_writeback(vmpage);
112 }
113
114 static void vvp_page_unassume(const struct lu_env *env,
115                               const struct cl_page_slice *slice,
116                               struct cl_io *unused)
117 {
118         struct page *vmpage = cl2vm_page(slice);
119
120         LASSERT(vmpage != NULL);
121         LASSERT(PageLocked(vmpage));
122 }
123
124 static void vvp_page_disown(const struct lu_env *env,
125                             const struct cl_page_slice *slice, struct cl_io *io)
126 {
127         struct page *vmpage = cl2vm_page(slice);
128
129         LASSERT(vmpage != NULL);
130         LASSERT(PageLocked(vmpage));
131
132         unlock_page(cl2vm_page(slice));
133 }
134
135 static void vvp_page_discard(const struct lu_env *env,
136                              const struct cl_page_slice *slice,
137                              struct cl_io *unused)
138 {
139         struct page     *vmpage = cl2vm_page(slice);
140         struct ccc_page *cpg    = cl2ccc_page(slice);
141
142         LASSERT(vmpage != NULL);
143         LASSERT(PageLocked(vmpage));
144
145         if (cpg->cpg_defer_uptodate && !cpg->cpg_ra_used)
146                 ll_ra_stats_inc(vmpage->mapping->host, RA_STAT_DISCARDED);
147
148         ll_invalidate_page(vmpage);
149 }
150
151 static void vvp_page_delete(const struct lu_env *env,
152                             const struct cl_page_slice *slice)
153 {
154         struct page      *vmpage = cl2vm_page(slice);
155         struct inode     *inode  = vmpage->mapping->host;
156         struct cl_object *obj    = slice->cpl_obj;
157         struct cl_page   *page   = slice->cpl_page;
158         int refc;
159
160         LASSERT(PageLocked(vmpage));
161         LASSERT((struct cl_page *)vmpage->private == page);
162         LASSERT(inode == ccc_object_inode(obj));
163
164         vvp_write_complete(cl2ccc(obj), cl2ccc_page(slice));
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         ClearPageUptodate(vmpage);
171         ClearPagePrivate(vmpage);
172         vmpage->private = 0;
173         /*
174          * Reference from vmpage to cl_page is removed, but the reference back
175          * is still here. It is removed later in vvp_page_fini().
176          */
177 }
178
179 static void vvp_page_export(const struct lu_env *env,
180                             const struct cl_page_slice *slice,
181                             int uptodate)
182 {
183         struct page *vmpage = cl2vm_page(slice);
184
185         LASSERT(vmpage != NULL);
186         LASSERT(PageLocked(vmpage));
187         if (uptodate)
188                 SetPageUptodate(vmpage);
189         else
190                 ClearPageUptodate(vmpage);
191 }
192
193 static int vvp_page_is_vmlocked(const struct lu_env *env,
194                                 const struct cl_page_slice *slice)
195 {
196         return PageLocked(cl2vm_page(slice)) ? -EBUSY : -ENODATA;
197 }
198
199 static int vvp_page_prep_read(const struct lu_env *env,
200                               const struct cl_page_slice *slice,
201                               struct cl_io *unused)
202 {
203         ENTRY;
204         /* Skip the page already marked as PG_uptodate. */
205         RETURN(PageUptodate(cl2vm_page(slice)) ? -EALREADY : 0);
206 }
207
208 static int vvp_page_prep_write(const struct lu_env *env,
209                                const struct cl_page_slice *slice,
210                                struct cl_io *unused)
211 {
212         struct page *vmpage = cl2vm_page(slice);
213
214         LASSERT(PageLocked(vmpage));
215         LASSERT(!PageDirty(vmpage));
216
217         set_page_writeback(vmpage);
218         vvp_write_pending(cl2ccc(slice->cpl_obj), cl2ccc_page(slice));
219
220         return 0;
221 }
222
223 /**
224  * Handles page transfer errors at VM level.
225  *
226  * This takes inode as a separate argument, because inode on which error is to
227  * be set can be different from \a vmpage inode in case of direct-io.
228  */
229 static void vvp_vmpage_error(struct inode *inode, struct page *vmpage, int ioret)
230 {
231         struct ccc_object *obj = cl_inode2ccc(inode);
232
233         if (ioret == 0) {
234                 ClearPageError(vmpage);
235                 obj->cob_discard_page_warned = 0;
236         } else {
237                 SetPageError(vmpage);
238                 if (ioret == -ENOSPC)
239                         set_bit(AS_ENOSPC, &inode->i_mapping->flags);
240                 else
241                         set_bit(AS_EIO, &inode->i_mapping->flags);
242
243                 if ((ioret == -ESHUTDOWN || ioret == -EINTR) &&
244                      obj->cob_discard_page_warned == 0) {
245                         obj->cob_discard_page_warned = 1;
246                         ll_dirty_page_discard_warn(vmpage, ioret);
247                 }
248         }
249 }
250
251 static void vvp_page_completion_read(const struct lu_env *env,
252                                      const struct cl_page_slice *slice,
253                                      int ioret)
254 {
255         struct ccc_page *cp     = cl2ccc_page(slice);
256         struct page     *vmpage = cp->cpg_page;
257         struct cl_page  *page   = slice->cpl_page;
258         struct inode    *inode  = ccc_object_inode(page->cp_obj);
259         ENTRY;
260
261         LASSERT(PageLocked(vmpage));
262         CL_PAGE_HEADER(D_PAGE, env, page, "completing READ with %d\n", ioret);
263
264         if (cp->cpg_defer_uptodate)
265                 ll_ra_count_put(ll_i2sbi(inode), 1);
266
267         if (ioret == 0)  {
268                 if (!cp->cpg_defer_uptodate)
269                         cl_page_export(env, page, 1);
270         } else
271                 cp->cpg_defer_uptodate = 0;
272
273         if (page->cp_sync_io == NULL)
274                 unlock_page(vmpage);
275
276         EXIT;
277 }
278
279 static void vvp_page_completion_write(const struct lu_env *env,
280                                       const struct cl_page_slice *slice,
281                                       int ioret)
282 {
283         struct ccc_page *cp     = cl2ccc_page(slice);
284         struct cl_page  *pg     = slice->cpl_page;
285         struct page      *vmpage = cp->cpg_page;
286         ENTRY;
287
288         LASSERT(ergo(pg->cp_sync_io != NULL, PageLocked(vmpage)));
289         LASSERT(PageWriteback(vmpage));
290
291         CL_PAGE_HEADER(D_PAGE, env, pg, "completing WRITE with %d\n", ioret);
292
293         /*
294          * TODO: Actually it makes sense to add the page into oap pending
295          * list again and so that we don't need to take the page out from
296          * SoM write pending list, if we just meet a recoverable error,
297          * -ENOMEM, etc.
298          * To implement this, we just need to return a non zero value in
299          * ->cpo_completion method. The underlying transfer should be notified
300          * and then re-add the page into pending transfer queue.  -jay
301          */
302
303         cp->cpg_write_queued = 0;
304         vvp_write_complete(cl2ccc(slice->cpl_obj), cp);
305
306         /*
307          * Only mark the page error only when it's an async write because
308          * applications won't wait for IO to finish.
309          */
310         if (pg->cp_sync_io == NULL)
311                 vvp_vmpage_error(ccc_object_inode(pg->cp_obj), vmpage, ioret);
312
313         end_page_writeback(vmpage);
314         EXIT;
315 }
316
317 /**
318  * Implements cl_page_operations::cpo_make_ready() method.
319  *
320  * This is called to yank a page from the transfer cache and to send it out as
321  * a part of transfer. This function try-locks the page. If try-lock failed,
322  * page is owned by some concurrent IO, and should be skipped (this is bad,
323  * but hopefully rare situation, as it usually results in transfer being
324  * shorter than possible).
325  *
326  * \retval 0      success, page can be placed into transfer
327  *
328  * \retval -EAGAIN page is either used by concurrent IO has been
329  * truncated. Skip it.
330  */
331 static int vvp_page_make_ready(const struct lu_env *env,
332                                const struct cl_page_slice *slice)
333 {
334         struct page *vmpage = cl2vm_page(slice);
335         struct cl_page *pg = slice->cpl_page;
336         int result = 0;
337
338         lock_page(vmpage);
339         if (clear_page_dirty_for_io(vmpage)) {
340                 LASSERT(pg->cp_state == CPS_CACHED);
341                 /* This actually clears the dirty bit in the radix
342                  * tree. */
343                 set_page_writeback(vmpage);
344                 vvp_write_pending(cl2ccc(slice->cpl_obj),
345                                 cl2ccc_page(slice));
346                 CL_PAGE_HEADER(D_PAGE, env, pg, "readied\n");
347         } else if (pg->cp_state == CPS_PAGEOUT) {
348                 /* is it possible for osc_flush_async_page() to already
349                  * make it ready? */
350                 result = -EALREADY;
351         } else {
352                 CL_PAGE_DEBUG(D_ERROR, env, pg, "Unexpecting page state %d.\n",
353                               pg->cp_state);
354                 LBUG();
355         }
356         unlock_page(vmpage);
357         RETURN(result);
358 }
359
360 static int vvp_page_is_under_lock(const struct lu_env *env,
361                                   const struct cl_page_slice *slice,
362                                   struct cl_io *io, pgoff_t *max_index)
363 {
364         ENTRY;
365
366         if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
367             io->ci_type == CIT_FAULT) {
368                 struct ccc_io *cio = ccc_env_io(env);
369
370                 if (unlikely(cio->cui_fd->fd_flags & LL_FILE_GROUP_LOCKED))
371                         *max_index = CL_PAGE_EOF;
372         }
373         RETURN(0);
374 }
375
376
377 static int vvp_page_print(const struct lu_env *env,
378                           const struct cl_page_slice *slice,
379                           void *cookie, lu_printer_t printer)
380 {
381         struct ccc_page *vp = cl2ccc_page(slice);
382         struct page      *vmpage = vp->cpg_page;
383
384         (*printer)(env, cookie, LUSTRE_VVP_NAME"-page@%p(%d:%d:%d) "
385                    "vm@%p ",
386                    vp, vp->cpg_defer_uptodate, vp->cpg_ra_used,
387                    vp->cpg_write_queued, vmpage);
388         if (vmpage != NULL) {
389                 (*printer)(env, cookie, "%lx %d:%d %lx %lu %slru",
390                            (long)vmpage->flags, page_count(vmpage),
391                            page_mapcount(vmpage), vmpage->private,
392                            page_index(vmpage),
393                            list_empty(&vmpage->lru) ? "not-" : "");
394         }
395         (*printer)(env, cookie, "\n");
396         return 0;
397 }
398
399 static const struct cl_page_operations vvp_page_ops = {
400         .cpo_own           = vvp_page_own,
401         .cpo_assume        = vvp_page_assume,
402         .cpo_unassume      = vvp_page_unassume,
403         .cpo_disown        = vvp_page_disown,
404         .cpo_discard       = vvp_page_discard,
405         .cpo_delete        = vvp_page_delete,
406         .cpo_export        = vvp_page_export,
407         .cpo_is_vmlocked   = vvp_page_is_vmlocked,
408         .cpo_fini          = vvp_page_fini,
409         .cpo_print         = vvp_page_print,
410         .cpo_is_under_lock = vvp_page_is_under_lock,
411         .io = {
412                 [CRT_READ] = {
413                         .cpo_prep        = vvp_page_prep_read,
414                         .cpo_completion  = vvp_page_completion_read,
415                         .cpo_make_ready  = ccc_fail,
416                 },
417                 [CRT_WRITE] = {
418                         .cpo_prep        = vvp_page_prep_write,
419                         .cpo_completion  = vvp_page_completion_write,
420                         .cpo_make_ready  = vvp_page_make_ready,
421                 }
422         }
423 };
424
425 static void vvp_transient_page_verify(const struct cl_page *page)
426 {
427         struct inode *inode = ccc_object_inode(page->cp_obj);
428
429         LASSERT(!mutex_trylock(&inode->i_mutex));
430 }
431
432 static int vvp_transient_page_own(const struct lu_env *env,
433                                   const struct cl_page_slice *slice,
434                                   struct cl_io *unused, int nonblock)
435 {
436         vvp_transient_page_verify(slice->cpl_page);
437         return 0;
438 }
439
440 static void vvp_transient_page_assume(const struct lu_env *env,
441                                       const struct cl_page_slice *slice,
442                                       struct cl_io *unused)
443 {
444         vvp_transient_page_verify(slice->cpl_page);
445 }
446
447 static void vvp_transient_page_unassume(const struct lu_env *env,
448                                         const struct cl_page_slice *slice,
449                                         struct cl_io *unused)
450 {
451         vvp_transient_page_verify(slice->cpl_page);
452 }
453
454 static void vvp_transient_page_disown(const struct lu_env *env,
455                                       const struct cl_page_slice *slice,
456                                       struct cl_io *unused)
457 {
458         vvp_transient_page_verify(slice->cpl_page);
459 }
460
461 static void vvp_transient_page_discard(const struct lu_env *env,
462                                        const struct cl_page_slice *slice,
463                                        struct cl_io *unused)
464 {
465         struct cl_page *page = slice->cpl_page;
466
467         vvp_transient_page_verify(slice->cpl_page);
468
469         /*
470          * For transient pages, remove it from the radix tree.
471          */
472         cl_page_delete(env, page);
473 }
474
475 static int vvp_transient_page_is_vmlocked(const struct lu_env *env,
476                                           const struct cl_page_slice *slice)
477 {
478         struct inode    *inode = ccc_object_inode(slice->cpl_obj);
479         int     locked;
480
481         locked = !mutex_trylock(&inode->i_mutex);
482         if (!locked)
483                 mutex_unlock(&inode->i_mutex);
484         return locked ? -EBUSY : -ENODATA;
485 }
486
487 static void
488 vvp_transient_page_completion(const struct lu_env *env,
489                               const struct cl_page_slice *slice,
490                               int ioret)
491 {
492         vvp_transient_page_verify(slice->cpl_page);
493 }
494
495 static void vvp_transient_page_fini(const struct lu_env *env,
496                                     struct cl_page_slice *slice)
497 {
498         struct ccc_page *cp = cl2ccc_page(slice);
499         struct cl_page *clp = slice->cpl_page;
500         struct ccc_object *clobj = cl2ccc(clp->cp_obj);
501
502         vvp_page_fini_common(cp);
503         LASSERT(!mutex_trylock(&clobj->cob_inode->i_mutex));
504         clobj->cob_transient_pages--;
505 }
506
507 static const struct cl_page_operations vvp_transient_page_ops = {
508         .cpo_own                = vvp_transient_page_own,
509         .cpo_assume             = vvp_transient_page_assume,
510         .cpo_unassume           = vvp_transient_page_unassume,
511         .cpo_disown             = vvp_transient_page_disown,
512         .cpo_discard            = vvp_transient_page_discard,
513         .cpo_fini               = vvp_transient_page_fini,
514         .cpo_is_vmlocked        = vvp_transient_page_is_vmlocked,
515         .cpo_print              = vvp_page_print,
516         .cpo_is_under_lock      = vvp_page_is_under_lock,
517         .io = {
518                 [CRT_READ] = {
519                         .cpo_prep       = ccc_transient_page_prep,
520                         .cpo_completion = vvp_transient_page_completion,
521                 },
522                 [CRT_WRITE] = {
523                         .cpo_prep       = ccc_transient_page_prep,
524                         .cpo_completion = vvp_transient_page_completion,
525                 }
526         }
527 };
528
529 int vvp_page_init(const struct lu_env *env, struct cl_object *obj,
530                 struct cl_page *page, pgoff_t index)
531 {
532         struct ccc_page *cpg = cl_object_page_slice(obj, page);
533         struct page     *vmpage = page->cp_vmpage;
534
535         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
536
537         cpg->cpg_page = vmpage;
538         page_cache_get(vmpage);
539
540         INIT_LIST_HEAD(&cpg->cpg_pending_linkage);
541         if (page->cp_type == CPT_CACHEABLE) {
542                 /* in cache, decref in vvp_page_delete */
543                 atomic_inc(&page->cp_ref);
544                 SetPagePrivate(vmpage);
545                 vmpage->private = (unsigned long)page;
546                 cl_page_slice_add(page, &cpg->cpg_cl, obj, index,
547                                 &vvp_page_ops);
548         } else {
549                 struct ccc_object *clobj = cl2ccc(obj);
550
551                 LASSERT(!mutex_trylock(&clobj->cob_inode->i_mutex));
552                 cl_page_slice_add(page, &cpg->cpg_cl, obj, index,
553                                 &vvp_transient_page_ops);
554                 clobj->cob_transient_pages++;
555         }
556         return 0;
557 }
558