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