Whamcloud - gitweb
8db2b38d7e6f2b85216314d0306bafd0a280efef
[fs/lustre-release.git] / lustre / llite / vvp_page.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
30  * Use is subject to license terms.
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  */
40
41 #define DEBUG_SUBSYSTEM S_LLITE
42
43 #ifndef __KERNEL__
44 # error This file is kernel only.
45 #endif
46
47 #include <obd.h>
48 #include <lustre_lite.h>
49
50 #include "vvp_internal.h"
51
52 /*****************************************************************************
53  *
54  * Page operations.
55  *
56  */
57
58 static void vvp_page_fini_common(struct ccc_page *cp)
59 {
60         cfs_page_t *vmpage = cp->cpg_page;
61
62         LASSERT(vmpage != NULL);
63         page_cache_release(vmpage);
64         OBD_SLAB_FREE_PTR(cp, vvp_page_kmem);
65 }
66
67 static void vvp_page_fini(const struct lu_env *env,
68                           struct cl_page_slice *slice)
69 {
70         struct ccc_page *cp = cl2ccc_page(slice);
71         cfs_page_t *vmpage  = cp->cpg_page;
72
73         /*
74          * vmpage->private was already cleared when page was moved into
75          * VPG_FREEING state.
76          */
77         LASSERT((struct cl_page *)vmpage->private != slice->cpl_page);
78         vvp_page_fini_common(cp);
79 }
80
81 static void vvp_page_own(const struct lu_env *env,
82                          const struct cl_page_slice *slice, struct cl_io *_)
83 {
84         struct ccc_page *vpg    = cl2ccc_page(slice);
85         cfs_page_t      *vmpage = vpg->cpg_page;
86
87         LASSERT(vmpage != NULL);
88         lock_page(vmpage);
89         wait_on_page_writeback(vmpage);
90 }
91
92 static void vvp_page_assume(const struct lu_env *env,
93                             const struct cl_page_slice *slice, struct cl_io *_)
94 {
95         cfs_page_t *vmpage = cl2vm_page(slice);
96
97         LASSERT(vmpage != NULL);
98         LASSERT(PageLocked(vmpage));
99         wait_on_page_writeback(vmpage);
100 }
101
102 static void vvp_page_unassume(const struct lu_env *env,
103                               const struct cl_page_slice *slice,
104                               struct cl_io *_)
105 {
106         cfs_page_t *vmpage = cl2vm_page(slice);
107
108         LASSERT(vmpage != NULL);
109         LASSERT(PageLocked(vmpage));
110 }
111
112 static void vvp_page_disown(const struct lu_env *env,
113                             const struct cl_page_slice *slice, struct cl_io *io)
114 {
115         cfs_page_t *vmpage = cl2vm_page(slice);
116
117         LASSERT(vmpage != NULL);
118         LASSERT(PageLocked(vmpage));
119
120         unlock_page(cl2vm_page(slice));
121 }
122
123 static void vvp_page_discard(const struct lu_env *env,
124                              const struct cl_page_slice *slice, struct cl_io *_)
125 {
126         cfs_page_t           *vmpage  = cl2vm_page(slice);
127         struct address_space *mapping = vmpage->mapping;
128         struct ccc_page      *cpg     = cl2ccc_page(slice);
129
130         LASSERT(vmpage != NULL);
131         LASSERT(PageLocked(vmpage));
132
133         if (cpg->cpg_defer_uptodate && !cpg->cpg_ra_used)
134                 ll_ra_stats_inc(mapping, RA_STAT_DISCARDED);
135
136         /*
137          * truncate_complete_page() calls
138          * a_ops->invalidatepage()->cl_page_delete()->vvp_page_delete().
139          */
140         truncate_complete_page(mapping, vmpage);
141 }
142
143 static int vvp_page_unmap(const struct lu_env *env,
144                           const struct cl_page_slice *slice, struct cl_io *_)
145 {
146         cfs_page_t *vmpage = cl2vm_page(slice);
147         __u64       offset = vmpage->index << CFS_PAGE_SHIFT;
148
149         LASSERT(vmpage != NULL);
150         LASSERT(PageLocked(vmpage));
151         /*
152          * XXX is it safe to call this with the page lock held?
153          */
154         ll_teardown_mmaps(vmpage->mapping, offset, offset + CFS_PAGE_SIZE);
155         return 0;
156 }
157
158 static void vvp_page_delete(const struct lu_env *env,
159                             const struct cl_page_slice *slice)
160 {
161         cfs_page_t       *vmpage = cl2vm_page(slice);
162         struct inode     *inode  = vmpage->mapping->host;
163         struct cl_object *obj    = slice->cpl_obj;
164
165         LASSERT(PageLocked(vmpage));
166         LASSERT((struct cl_page *)vmpage->private == slice->cpl_page);
167         LASSERT(inode == ccc_object_inode(obj));
168
169         vvp_write_complete(cl2ccc(obj), cl2ccc_page(slice));
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 {
181         cfs_page_t *vmpage = cl2vm_page(slice);
182
183         LASSERT(vmpage != NULL);
184         LASSERT(PageLocked(vmpage));
185         SetPageUptodate(vmpage);
186 }
187
188 static int vvp_page_is_vmlocked(const struct lu_env *env,
189                                 const struct cl_page_slice *slice)
190 {
191         return PageLocked(cl2vm_page(slice)) ? -EBUSY : -ENODATA;
192 }
193
194 static int vvp_page_prep_read(const struct lu_env *env,
195                               const struct cl_page_slice *slice,
196                               struct cl_io *_)
197 {
198         ENTRY;
199         /* Skip the page already marked as PG_uptodate. */
200         RETURN(PageUptodate(cl2vm_page(slice)) ? -EALREADY : 0);
201 }
202
203 static int vvp_page_prep_write(const struct lu_env *env,
204                                const struct cl_page_slice *slice,
205                                struct cl_io *_)
206 {
207         cfs_page_t *vmpage = cl2vm_page(slice);
208         int result;
209
210         if (clear_page_dirty_for_io(vmpage)) {
211                 set_page_writeback(vmpage);
212                 vvp_write_pending(cl2ccc(slice->cpl_obj), cl2ccc_page(slice));
213                 result = 0;
214         } else
215                 result = -EALREADY;
216         return result;
217 }
218
219 /**
220  * Handles page transfer errors at VM level.
221  *
222  * This takes inode as a separate argument, because inode on which error is to
223  * be set can be different from \a vmpage inode in case of direct-io.
224  */
225 static void vvp_vmpage_error(struct inode *inode, cfs_page_t *vmpage, int ioret)
226 {
227         if (ioret == 0)
228                 ClearPageError(vmpage);
229         else if (ioret != -EINTR) {
230                 SetPageError(vmpage);
231                 if (ioret == -ENOSPC)
232                         set_bit(AS_ENOSPC, &inode->i_mapping->flags);
233                 else
234                         set_bit(AS_EIO, &inode->i_mapping->flags);
235         }
236 }
237
238 static void vvp_page_completion_common(const struct lu_env *env,
239                                        struct ccc_page *cp, int ioret)
240 {
241         struct cl_page    *clp    = cp->cpg_cl.cpl_page;
242         cfs_page_t        *vmpage = cp->cpg_page;
243         struct inode      *inode  = ccc_object_inode(clp->cp_obj);
244         struct cl_sync_io *anchor = cp->cpg_sync_io;
245
246         LINVRNT(cl_page_is_vmlocked(env, clp));
247
248         if (anchor != NULL) {
249                 cp->cpg_sync_io  = NULL;
250                 cl_sync_io_note(anchor, ioret);
251         } else if (clp->cp_type == CPT_CACHEABLE) {
252                 /*
253                  * Only mark the page error only when it's a cacheable page
254                  * and NOT a sync io.
255                  *
256                  * For sync IO and direct IO(CPT_TRANSIENT), the error is able
257                  * to be seen by application, so we don't need to mark a page
258                  * as error at all.
259                  */
260                 vvp_vmpage_error(inode, vmpage, ioret);
261                 unlock_page(vmpage);
262         }
263 }
264
265 static void vvp_page_completion_read(const struct lu_env *env,
266                                      const struct cl_page_slice *slice,
267                                      int ioret)
268 {
269         struct ccc_page *cp    = cl2ccc_page(slice);
270         struct cl_page  *page  = cl_page_top(slice->cpl_page);
271         struct inode    *inode = ccc_object_inode(page->cp_obj);
272         ENTRY;
273
274         CL_PAGE_HEADER(D_PAGE, env, page, "completing READ with %d\n", ioret);
275
276         if (cp->cpg_defer_uptodate)
277                 ll_ra_count_put(ll_i2sbi(inode), 1);
278
279         if (ioret == 0)  {
280                 /* XXX: do we need this for transient pages? */
281                 if (!cp->cpg_defer_uptodate)
282                         cl_page_export(env, page);
283         } else
284                 cp->cpg_defer_uptodate = 0;
285         vvp_page_completion_common(env, cp, ioret);
286
287         EXIT;
288 }
289
290 static void vvp_page_completion_write_common(const struct lu_env *env,
291                                              const struct cl_page_slice *slice,
292                                              int ioret)
293 {
294         struct ccc_page *cp = cl2ccc_page(slice);
295
296         /*
297          * TODO: Actually it makes sense to add the page into oap pending
298          * list again and so that we don't need to take the page out from
299          * SoM write pending list, if we just meet a recoverable error,
300          * -ENOMEM, etc.
301          * To implement this, we just need to return a non zero value in
302          * ->cpo_completion method. The underlying transfer should be notified
303          * and then re-add the page into pending transfer queue.  -jay
304          */
305         cp->cpg_write_queued = 0;
306         vvp_write_complete(cl2ccc(slice->cpl_obj), cp);
307
308         vvp_page_completion_common(env, cp, ioret);
309 }
310
311 static void vvp_page_completion_write(const struct lu_env *env,
312                                       const struct cl_page_slice *slice,
313                                       int ioret)
314 {
315         struct ccc_page *cp     = cl2ccc_page(slice);
316         struct cl_page  *pg     = slice->cpl_page;
317         cfs_page_t      *vmpage = cp->cpg_page;
318
319         ENTRY;
320
321         LINVRNT(cl_page_is_vmlocked(env, pg));
322         LASSERT(PageWriteback(vmpage));
323
324         CL_PAGE_HEADER(D_PAGE, env, pg, "completing WRITE with %d\n", ioret);
325
326         vvp_page_completion_write_common(env, slice, ioret);
327         end_page_writeback(vmpage);
328         EXIT;
329 }
330
331 /**
332  * Implements cl_page_operations::cpo_make_ready() method.
333  *
334  * This is called to yank a page from the transfer cache and to send it out as
335  * a part of transfer. This function try-locks the page. If try-lock failed,
336  * page is owned by some concurrent IO, and should be skipped (this is bad,
337  * but hopefully rare situation, as it usually results in transfer being
338  * shorter than possible).
339  *
340  * \retval 0      success, page can be placed into transfer
341  *
342  * \retval -EAGAIN page is either used by concurrent IO has been
343  * truncated. Skip it.
344  */
345 static int vvp_page_make_ready(const struct lu_env *env,
346                                const struct cl_page_slice *slice)
347 {
348         cfs_page_t *vmpage = cl2vm_page(slice);
349         struct cl_page *pg = slice->cpl_page;
350         int result;
351
352         result = -EAGAIN;
353         /* we're trying to write, but the page is locked.. come back later */
354         if (!TestSetPageLocked(vmpage)) {
355                 if (pg->cp_state == CPS_CACHED) {
356                         /*
357                          * We can cancel IO if page wasn't dirty after all.
358                          */
359                         clear_page_dirty_for_io(vmpage);
360                         /*
361                          * This actually clears the dirty bit in the radix
362                          * tree.
363                          */
364                         set_page_writeback(vmpage);
365                         vvp_write_pending(cl2ccc(slice->cpl_obj),
366                                           cl2ccc_page(slice));
367                         CL_PAGE_HEADER(D_PAGE, env, pg, "readied\n");
368                         result = 0;
369                 } else
370                         /*
371                          * Page was concurrently truncated.
372                          */
373                         LASSERT(pg->cp_state == CPS_FREEING);
374         }
375         RETURN(result);
376 }
377
378 static int vvp_page_print(const struct lu_env *env,
379                           const struct cl_page_slice *slice,
380                           void *cookie, lu_printer_t printer)
381 {
382         struct ccc_page *vp = cl2ccc_page(slice);
383         cfs_page_t      *vmpage = vp->cpg_page;
384
385         (*printer)(env, cookie, LUSTRE_VVP_NAME"-page@%p(%d:%d:%d) "
386                    "vm@%p ",
387                    vp, vp->cpg_defer_uptodate, vp->cpg_ra_used,
388                    vp->cpg_write_queued, vmpage);
389         if (vmpage != NULL) {
390                 (*printer)(env, cookie, "%lx %d:%d %lx %lu %slru",
391                            (long)vmpage->flags, page_count(vmpage),
392                            page_mapcount(vmpage), vmpage->private,
393                            page_index(vmpage),
394                            list_empty(&vmpage->lru) ? "not-" : "");
395         }
396         (*printer)(env, cookie, "\n");
397         return 0;
398 }
399
400 static const struct cl_page_operations vvp_page_ops = {
401         .cpo_own           = vvp_page_own,
402         .cpo_assume        = vvp_page_assume,
403         .cpo_unassume      = vvp_page_unassume,
404         .cpo_disown        = vvp_page_disown,
405         .cpo_vmpage        = ccc_page_vmpage,
406         .cpo_discard       = vvp_page_discard,
407         .cpo_delete        = vvp_page_delete,
408         .cpo_unmap         = vvp_page_unmap,
409         .cpo_export        = vvp_page_export,
410         .cpo_is_vmlocked   = vvp_page_is_vmlocked,
411         .cpo_fini          = vvp_page_fini,
412         .cpo_print         = vvp_page_print,
413         .cpo_is_under_lock = ccc_page_is_under_lock,
414         .io = {
415                 [CRT_READ] = {
416                         .cpo_prep        = vvp_page_prep_read,
417                         .cpo_completion  = vvp_page_completion_read,
418                         .cpo_make_ready  = ccc_fail,
419                 },
420                 [CRT_WRITE] = {
421                         .cpo_prep        = vvp_page_prep_write,
422                         .cpo_completion  = vvp_page_completion_write,
423                         .cpo_make_ready  = vvp_page_make_ready,
424                 }
425         }
426 };
427
428 static void vvp_transient_page_verify(const struct cl_page *page)
429 {
430         struct inode *inode = ccc_object_inode(page->cp_obj);
431
432         LASSERT(!TRYLOCK_INODE_MUTEX(inode));
433         /* LASSERT_SEM_LOCKED(&inode->i_alloc_sem); */
434 }
435
436 static void vvp_transient_page_own(const struct lu_env *env,
437                                    const struct cl_page_slice *slice,
438                                    struct cl_io *_)
439 {
440         vvp_transient_page_verify(slice->cpl_page);
441 }
442
443 static void vvp_transient_page_assume(const struct lu_env *env,
444                                       const struct cl_page_slice *slice,
445                                       struct cl_io *_)
446 {
447         vvp_transient_page_verify(slice->cpl_page);
448 }
449
450 static void vvp_transient_page_unassume(const struct lu_env *env,
451                                         const struct cl_page_slice *slice,
452                                         struct cl_io *_)
453 {
454         vvp_transient_page_verify(slice->cpl_page);
455 }
456
457 static void vvp_transient_page_disown(const struct lu_env *env,
458                                       const struct cl_page_slice *slice,
459                                       struct cl_io *_)
460 {
461         vvp_transient_page_verify(slice->cpl_page);
462 }
463
464 static void vvp_transient_page_discard(const struct lu_env *env,
465                                        const struct cl_page_slice *slice,
466                                        struct cl_io *_)
467 {
468         struct cl_page *page = slice->cpl_page;
469
470         vvp_transient_page_verify(slice->cpl_page);
471
472         /*
473          * For transient pages, remove it from the radix tree.
474          */
475         cl_page_delete(env, page);
476 }
477
478 static int vvp_transient_page_is_vmlocked(const struct lu_env *env,
479                                           const struct cl_page_slice *slice)
480 {
481         struct inode    *inode = ccc_object_inode(slice->cpl_obj);
482         int              locked;
483
484         locked = !TRYLOCK_INODE_MUTEX(inode);
485         if (!locked)
486                 UNLOCK_INODE_MUTEX(inode);
487         return locked ? -EBUSY : -ENODATA;
488 }
489
490 static void
491 vvp_transient_page_completion_write(const struct lu_env *env,
492                                     const struct cl_page_slice *slice,
493                                     int ioret)
494 {
495         vvp_transient_page_verify(slice->cpl_page);
496         vvp_page_completion_write_common(env, slice, ioret);
497 }
498
499
500 static void vvp_transient_page_fini(const struct lu_env *env,
501                                     struct cl_page_slice *slice)
502 {
503         struct ccc_page *cp = cl2ccc_page(slice);
504         struct cl_page *clp = slice->cpl_page;
505         struct ccc_object *clobj = cl2ccc(clp->cp_obj);
506
507         vvp_page_fini_common(cp);
508         LASSERT(!TRYLOCK_INODE_MUTEX(clobj->cob_inode));
509         clobj->cob_transient_pages--;
510 }
511
512 static const struct cl_page_operations vvp_transient_page_ops = {
513         .cpo_own           = vvp_transient_page_own,
514         .cpo_assume        = vvp_transient_page_assume,
515         .cpo_unassume      = vvp_transient_page_unassume,
516         .cpo_disown        = vvp_transient_page_disown,
517         .cpo_discard       = vvp_transient_page_discard,
518         .cpo_vmpage        = ccc_page_vmpage,
519         .cpo_fini          = vvp_transient_page_fini,
520         .cpo_is_vmlocked   = vvp_transient_page_is_vmlocked,
521         .cpo_print         = vvp_page_print,
522         .cpo_is_under_lock = ccc_page_is_under_lock,
523         .io = {
524                 [CRT_READ] = {
525                         .cpo_prep        = ccc_transient_page_prep,
526                         .cpo_completion  = vvp_page_completion_read,
527                 },
528                 [CRT_WRITE] = {
529                         .cpo_prep        = ccc_transient_page_prep,
530                         .cpo_completion  = vvp_transient_page_completion_write,
531                 }
532         }
533 };
534
535 struct cl_page *vvp_page_init(const struct lu_env *env, struct cl_object *obj,
536                               struct cl_page *page, cfs_page_t *vmpage)
537 {
538         struct ccc_page *cpg;
539         int result;
540
541         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
542
543         OBD_SLAB_ALLOC_PTR_GFP(cpg, vvp_page_kmem, CFS_ALLOC_IO);
544         if (cpg != NULL) {
545                 cpg->cpg_page = vmpage;
546                 page_cache_get(vmpage);
547
548                 CFS_INIT_LIST_HEAD(&cpg->cpg_pending_linkage);
549                 if (page->cp_type == CPT_CACHEABLE) {
550                         SetPagePrivate(vmpage);
551                         vmpage->private = (unsigned long)page;
552                         cl_page_slice_add(page, &cpg->cpg_cl, obj,
553                                           &vvp_page_ops);
554                 } else {
555                         struct ccc_object *clobj = cl2ccc(obj);
556
557                         LASSERT(!TRYLOCK_INODE_MUTEX(clobj->cob_inode));
558                         cl_page_slice_add(page, &cpg->cpg_cl, obj,
559                                           &vvp_transient_page_ops);
560                         clobj->cob_transient_pages++;
561                 }
562                 result = 0;
563         } else
564                 result = -ENOMEM;
565         return ERR_PTR(result);
566 }
567