Whamcloud - gitweb
18d7fa50d2545e70da6a2942ef13495f5b78e31e
[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 int vvp_page_own(const struct lu_env *env,
82                         const struct cl_page_slice *slice, struct cl_io *io,
83                         int nonblock)
84 {
85         struct ccc_page *vpg    = cl2ccc_page(slice);
86         cfs_page_t      *vmpage = vpg->cpg_page;
87         int count = 0;
88
89         LASSERT(vmpage != NULL);
90
91         if (nonblock) {
92                 if (TestSetPageLocked(vmpage))
93                         return -EAGAIN;
94
95                 if (unlikely(PageWriteback(vmpage))) {
96                         /* Something gets wrong? */
97                         unlock_page(vmpage);
98                         return -EAGAIN;
99                 }
100
101                 return 0;
102         }
103
104         /* DEBUG CODE FOR #18881 */
105         while (TestSetPageLocked(vmpage)) {
106                 cfs_schedule_timeout(CFS_TASK_INTERRUPTIBLE,
107                                      cfs_time_seconds(1)/10);
108                 if (++count > 1200) {
109                         CL_PAGE_DEBUG(D_ERROR, env,
110                                       cl_page_top(slice->cpl_page),
111                                       "XXX page %p blocked on acquiring the"
112                                       " lock. process %s/%p, flags %lx,io %p\n",
113                                       vmpage, current->comm, current,
114                                       vmpage->flags, io);
115                         libcfs_debug_dumpstack(NULL);
116                         if (slice->cpl_page->cp_task) {
117                                 cfs_task_t *tsk = slice->cpl_page->cp_task;
118                                 LCONSOLE_WARN("The page was owned by %s\n",
119                                               tsk->comm);
120                                 libcfs_debug_dumpstack(tsk);
121                         }
122                         LCONSOLE_WARN("Reproduced bug #18881,please contact:"
123                                "jay <jinshan.xiong@sun.com>, thanks\n");
124
125                         lock_page(vmpage);
126                         break;
127                 }
128         }
129         /* DEBUG CODE END */
130
131         /* lock_page(vmpage); */
132         wait_on_page_writeback(vmpage);
133         return 0;
134 }
135
136 static void vvp_page_assume(const struct lu_env *env,
137                             const struct cl_page_slice *slice,
138                             struct cl_io *unused)
139 {
140         cfs_page_t *vmpage = cl2vm_page(slice);
141
142         LASSERT(vmpage != NULL);
143         LASSERT(PageLocked(vmpage));
144         wait_on_page_writeback(vmpage);
145 }
146
147 static void vvp_page_unassume(const struct lu_env *env,
148                               const struct cl_page_slice *slice,
149                               struct cl_io *unused)
150 {
151         cfs_page_t *vmpage = cl2vm_page(slice);
152
153         LASSERT(vmpage != NULL);
154         LASSERT(PageLocked(vmpage));
155 }
156
157 static void vvp_page_disown(const struct lu_env *env,
158                             const struct cl_page_slice *slice, struct cl_io *io)
159 {
160         cfs_page_t *vmpage = cl2vm_page(slice);
161
162         LASSERT(vmpage != NULL);
163         LASSERT(PageLocked(vmpage));
164
165         unlock_page(cl2vm_page(slice));
166 }
167
168 static void vvp_page_discard(const struct lu_env *env,
169                              const struct cl_page_slice *slice,
170                              struct cl_io *unused)
171 {
172         cfs_page_t           *vmpage  = cl2vm_page(slice);
173         struct address_space *mapping = vmpage->mapping;
174         struct ccc_page      *cpg     = cl2ccc_page(slice);
175
176         LASSERT(vmpage != NULL);
177         LASSERT(PageLocked(vmpage));
178
179         if (cpg->cpg_defer_uptodate && !cpg->cpg_ra_used)
180                 ll_ra_stats_inc(mapping, RA_STAT_DISCARDED);
181
182         /*
183          * truncate_complete_page() calls
184          * a_ops->invalidatepage()->cl_page_delete()->vvp_page_delete().
185          */
186         truncate_complete_page(mapping, vmpage);
187 }
188
189 static int vvp_page_unmap(const struct lu_env *env,
190                           const struct cl_page_slice *slice,
191                           struct cl_io *unused)
192 {
193         cfs_page_t *vmpage = cl2vm_page(slice);
194         __u64       offset = vmpage->index << CFS_PAGE_SHIFT;
195
196         LASSERT(vmpage != NULL);
197         LASSERT(PageLocked(vmpage));
198         /*
199          * XXX is it safe to call this with the page lock held?
200          */
201         ll_teardown_mmaps(vmpage->mapping, offset, offset + CFS_PAGE_SIZE);
202         return 0;
203 }
204
205 static void vvp_page_delete(const struct lu_env *env,
206                             const struct cl_page_slice *slice)
207 {
208         cfs_page_t       *vmpage = cl2vm_page(slice);
209         struct inode     *inode  = vmpage->mapping->host;
210         struct cl_object *obj    = slice->cpl_obj;
211
212         LASSERT(PageLocked(vmpage));
213         LASSERT((struct cl_page *)vmpage->private == slice->cpl_page);
214         LASSERT(inode == ccc_object_inode(obj));
215
216         vvp_write_complete(cl2ccc(obj), cl2ccc_page(slice));
217         ClearPagePrivate(vmpage);
218         vmpage->private = 0;
219         /*
220          * Reference from vmpage to cl_page is removed, but the reference back
221          * is still here. It is removed later in vvp_page_fini().
222          */
223 }
224
225 static void vvp_page_export(const struct lu_env *env,
226                             const struct cl_page_slice *slice,
227                             int uptodate)
228 {
229         cfs_page_t *vmpage = cl2vm_page(slice);
230
231         LASSERT(vmpage != NULL);
232         LASSERT(PageLocked(vmpage));
233         if (uptodate)
234                 SetPageUptodate(vmpage);
235         else
236                 ClearPageUptodate(vmpage);
237 }
238
239 static int vvp_page_is_vmlocked(const struct lu_env *env,
240                                 const struct cl_page_slice *slice)
241 {
242         return PageLocked(cl2vm_page(slice)) ? -EBUSY : -ENODATA;
243 }
244
245 static int vvp_page_prep_read(const struct lu_env *env,
246                               const struct cl_page_slice *slice,
247                               struct cl_io *unused)
248 {
249         ENTRY;
250         /* Skip the page already marked as PG_uptodate. */
251         RETURN(PageUptodate(cl2vm_page(slice)) ? -EALREADY : 0);
252 }
253
254 static int vvp_page_prep_write(const struct lu_env *env,
255                                const struct cl_page_slice *slice,
256                                struct cl_io *unused)
257 {
258         cfs_page_t *vmpage = cl2vm_page(slice);
259         int result;
260
261         if (clear_page_dirty_for_io(vmpage)) {
262                 set_page_writeback(vmpage);
263                 vvp_write_pending(cl2ccc(slice->cpl_obj), cl2ccc_page(slice));
264                 result = 0;
265         } else
266                 result = -EALREADY;
267         return result;
268 }
269
270 /**
271  * Handles page transfer errors at VM level.
272  *
273  * This takes inode as a separate argument, because inode on which error is to
274  * be set can be different from \a vmpage inode in case of direct-io.
275  */
276 static void vvp_vmpage_error(struct inode *inode, cfs_page_t *vmpage, int ioret)
277 {
278         if (ioret == 0)
279                 ClearPageError(vmpage);
280         else if (ioret != -EINTR) {
281                 SetPageError(vmpage);
282                 if (ioret == -ENOSPC)
283                         set_bit(AS_ENOSPC, &inode->i_mapping->flags);
284                 else
285                         set_bit(AS_EIO, &inode->i_mapping->flags);
286         }
287 }
288
289 static void vvp_page_completion_common(const struct lu_env *env,
290                                        struct ccc_page *cp, int ioret)
291 {
292         struct cl_page    *clp    = cp->cpg_cl.cpl_page;
293         cfs_page_t        *vmpage = cp->cpg_page;
294         struct inode      *inode  = ccc_object_inode(clp->cp_obj);
295
296         LINVRNT(cl_page_is_vmlocked(env, clp));
297
298         if (!clp->cp_sync_io && clp->cp_type == CPT_CACHEABLE) {
299                 /*
300                  * Only mark the page error only when it's a cacheable page
301                  * and NOT a sync io.
302                  *
303                  * For sync IO and direct IO(CPT_TRANSIENT), the error is able
304                  * to be seen by application, so we don't need to mark a page
305                  * as error at all.
306                  */
307                 vvp_vmpage_error(inode, vmpage, ioret);
308                 unlock_page(vmpage);
309         }
310 }
311
312 static void vvp_page_completion_read(const struct lu_env *env,
313                                      const struct cl_page_slice *slice,
314                                      int ioret)
315 {
316         struct ccc_page *cp    = cl2ccc_page(slice);
317         struct cl_page  *page  = cl_page_top(slice->cpl_page);
318         struct inode    *inode = ccc_object_inode(page->cp_obj);
319         ENTRY;
320
321         CL_PAGE_HEADER(D_PAGE, env, page, "completing READ with %d\n", ioret);
322
323         if (cp->cpg_defer_uptodate)
324                 ll_ra_count_put(ll_i2sbi(inode), 1);
325
326         if (ioret == 0)  {
327                 /* XXX: do we need this for transient pages? */
328                 if (!cp->cpg_defer_uptodate)
329                         cl_page_export(env, page, 1);
330         } else
331                 cp->cpg_defer_uptodate = 0;
332         vvp_page_completion_common(env, cp, ioret);
333
334         EXIT;
335 }
336
337 static void vvp_page_completion_write_common(const struct lu_env *env,
338                                              const struct cl_page_slice *slice,
339                                              int ioret)
340 {
341         struct ccc_page *cp = cl2ccc_page(slice);
342
343         /*
344          * TODO: Actually it makes sense to add the page into oap pending
345          * list again and so that we don't need to take the page out from
346          * SoM write pending list, if we just meet a recoverable error,
347          * -ENOMEM, etc.
348          * To implement this, we just need to return a non zero value in
349          * ->cpo_completion method. The underlying transfer should be notified
350          * and then re-add the page into pending transfer queue.  -jay
351          */
352         cp->cpg_write_queued = 0;
353         vvp_write_complete(cl2ccc(slice->cpl_obj), cp);
354
355         vvp_page_completion_common(env, cp, ioret);
356 }
357
358 static void vvp_page_completion_write(const struct lu_env *env,
359                                       const struct cl_page_slice *slice,
360                                       int ioret)
361 {
362         struct ccc_page *cp     = cl2ccc_page(slice);
363         struct cl_page  *pg     = slice->cpl_page;
364         cfs_page_t      *vmpage = cp->cpg_page;
365
366         ENTRY;
367
368         LINVRNT(cl_page_is_vmlocked(env, pg));
369         LASSERT(PageWriteback(vmpage));
370
371         CL_PAGE_HEADER(D_PAGE, env, pg, "completing WRITE with %d\n", ioret);
372
373         vvp_page_completion_write_common(env, slice, ioret);
374         end_page_writeback(vmpage);
375         EXIT;
376 }
377
378 /**
379  * Implements cl_page_operations::cpo_make_ready() method.
380  *
381  * This is called to yank a page from the transfer cache and to send it out as
382  * a part of transfer. This function try-locks the page. If try-lock failed,
383  * page is owned by some concurrent IO, and should be skipped (this is bad,
384  * but hopefully rare situation, as it usually results in transfer being
385  * shorter than possible).
386  *
387  * \retval 0      success, page can be placed into transfer
388  *
389  * \retval -EAGAIN page is either used by concurrent IO has been
390  * truncated. Skip it.
391  */
392 static int vvp_page_make_ready(const struct lu_env *env,
393                                const struct cl_page_slice *slice)
394 {
395         cfs_page_t *vmpage = cl2vm_page(slice);
396         struct cl_page *pg = slice->cpl_page;
397         int result;
398
399         result = -EAGAIN;
400         /* we're trying to write, but the page is locked.. come back later */
401         if (!TestSetPageLocked(vmpage)) {
402                 if (pg->cp_state == CPS_CACHED) {
403                         /*
404                          * We can cancel IO if page wasn't dirty after all.
405                          */
406                         clear_page_dirty_for_io(vmpage);
407                         /*
408                          * This actually clears the dirty bit in the radix
409                          * tree.
410                          */
411                         set_page_writeback(vmpage);
412                         vvp_write_pending(cl2ccc(slice->cpl_obj),
413                                           cl2ccc_page(slice));
414                         CL_PAGE_HEADER(D_PAGE, env, pg, "readied\n");
415                         result = 0;
416                 } else
417                         /*
418                          * Page was concurrently truncated.
419                          */
420                         LASSERT(pg->cp_state == CPS_FREEING);
421         }
422         RETURN(result);
423 }
424
425 static int vvp_page_print(const struct lu_env *env,
426                           const struct cl_page_slice *slice,
427                           void *cookie, lu_printer_t printer)
428 {
429         struct ccc_page *vp = cl2ccc_page(slice);
430         cfs_page_t      *vmpage = vp->cpg_page;
431
432         (*printer)(env, cookie, LUSTRE_VVP_NAME"-page@%p(%d:%d:%d) "
433                    "vm@%p ",
434                    vp, vp->cpg_defer_uptodate, vp->cpg_ra_used,
435                    vp->cpg_write_queued, vmpage);
436         if (vmpage != NULL) {
437                 (*printer)(env, cookie, "%lx %d:%d %lx %lu %slru",
438                            (long)vmpage->flags, page_count(vmpage),
439                            page_mapcount(vmpage), vmpage->private,
440                            page_index(vmpage),
441                            list_empty(&vmpage->lru) ? "not-" : "");
442         }
443         (*printer)(env, cookie, "\n");
444         return 0;
445 }
446
447 static const struct cl_page_operations vvp_page_ops = {
448         .cpo_own           = vvp_page_own,
449         .cpo_assume        = vvp_page_assume,
450         .cpo_unassume      = vvp_page_unassume,
451         .cpo_disown        = vvp_page_disown,
452         .cpo_vmpage        = ccc_page_vmpage,
453         .cpo_discard       = vvp_page_discard,
454         .cpo_delete        = vvp_page_delete,
455         .cpo_unmap         = vvp_page_unmap,
456         .cpo_export        = vvp_page_export,
457         .cpo_is_vmlocked   = vvp_page_is_vmlocked,
458         .cpo_fini          = vvp_page_fini,
459         .cpo_print         = vvp_page_print,
460         .cpo_is_under_lock = ccc_page_is_under_lock,
461         .io = {
462                 [CRT_READ] = {
463                         .cpo_prep        = vvp_page_prep_read,
464                         .cpo_completion  = vvp_page_completion_read,
465                         .cpo_make_ready  = ccc_fail,
466                 },
467                 [CRT_WRITE] = {
468                         .cpo_prep        = vvp_page_prep_write,
469                         .cpo_completion  = vvp_page_completion_write,
470                         .cpo_make_ready  = vvp_page_make_ready,
471                 }
472         }
473 };
474
475 static void vvp_transient_page_verify(const struct cl_page *page)
476 {
477         struct inode *inode = ccc_object_inode(page->cp_obj);
478
479         LASSERT(!TRYLOCK_INODE_MUTEX(inode));
480         /* LASSERT_SEM_LOCKED(&inode->i_alloc_sem); */
481 }
482
483 static int vvp_transient_page_own(const struct lu_env *env,
484                                   const struct cl_page_slice *slice,
485                                   struct cl_io *unused, int nonblock)
486 {
487         vvp_transient_page_verify(slice->cpl_page);
488         return 0;
489 }
490
491 static void vvp_transient_page_assume(const struct lu_env *env,
492                                       const struct cl_page_slice *slice,
493                                       struct cl_io *unused)
494 {
495         vvp_transient_page_verify(slice->cpl_page);
496 }
497
498 static void vvp_transient_page_unassume(const struct lu_env *env,
499                                         const struct cl_page_slice *slice,
500                                         struct cl_io *unused)
501 {
502         vvp_transient_page_verify(slice->cpl_page);
503 }
504
505 static void vvp_transient_page_disown(const struct lu_env *env,
506                                       const struct cl_page_slice *slice,
507                                       struct cl_io *unused)
508 {
509         vvp_transient_page_verify(slice->cpl_page);
510 }
511
512 static void vvp_transient_page_discard(const struct lu_env *env,
513                                        const struct cl_page_slice *slice,
514                                        struct cl_io *unused)
515 {
516         struct cl_page *page = slice->cpl_page;
517
518         vvp_transient_page_verify(slice->cpl_page);
519
520         /*
521          * For transient pages, remove it from the radix tree.
522          */
523         cl_page_delete(env, page);
524 }
525
526 static int vvp_transient_page_is_vmlocked(const struct lu_env *env,
527                                           const struct cl_page_slice *slice)
528 {
529         struct inode    *inode = ccc_object_inode(slice->cpl_obj);
530         int              locked;
531
532         locked = !TRYLOCK_INODE_MUTEX(inode);
533         if (!locked)
534                 UNLOCK_INODE_MUTEX(inode);
535         return locked ? -EBUSY : -ENODATA;
536 }
537
538 static void
539 vvp_transient_page_completion_write(const struct lu_env *env,
540                                     const struct cl_page_slice *slice,
541                                     int ioret)
542 {
543         vvp_transient_page_verify(slice->cpl_page);
544         vvp_page_completion_write_common(env, slice, ioret);
545 }
546
547
548 static void vvp_transient_page_fini(const struct lu_env *env,
549                                     struct cl_page_slice *slice)
550 {
551         struct ccc_page *cp = cl2ccc_page(slice);
552         struct cl_page *clp = slice->cpl_page;
553         struct ccc_object *clobj = cl2ccc(clp->cp_obj);
554
555         vvp_page_fini_common(cp);
556         LASSERT(!TRYLOCK_INODE_MUTEX(clobj->cob_inode));
557         clobj->cob_transient_pages--;
558 }
559
560 static const struct cl_page_operations vvp_transient_page_ops = {
561         .cpo_own           = vvp_transient_page_own,
562         .cpo_assume        = vvp_transient_page_assume,
563         .cpo_unassume      = vvp_transient_page_unassume,
564         .cpo_disown        = vvp_transient_page_disown,
565         .cpo_discard       = vvp_transient_page_discard,
566         .cpo_vmpage        = ccc_page_vmpage,
567         .cpo_fini          = vvp_transient_page_fini,
568         .cpo_is_vmlocked   = vvp_transient_page_is_vmlocked,
569         .cpo_print         = vvp_page_print,
570         .cpo_is_under_lock = ccc_page_is_under_lock,
571         .io = {
572                 [CRT_READ] = {
573                         .cpo_prep        = ccc_transient_page_prep,
574                         .cpo_completion  = vvp_page_completion_read,
575                 },
576                 [CRT_WRITE] = {
577                         .cpo_prep        = ccc_transient_page_prep,
578                         .cpo_completion  = vvp_transient_page_completion_write,
579                 }
580         }
581 };
582
583 struct cl_page *vvp_page_init(const struct lu_env *env, struct cl_object *obj,
584                               struct cl_page *page, cfs_page_t *vmpage)
585 {
586         struct ccc_page *cpg;
587         int result;
588
589         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
590
591         OBD_SLAB_ALLOC_PTR_GFP(cpg, vvp_page_kmem, CFS_ALLOC_IO);
592         if (cpg != NULL) {
593                 cpg->cpg_page = vmpage;
594                 page_cache_get(vmpage);
595
596                 CFS_INIT_LIST_HEAD(&cpg->cpg_pending_linkage);
597                 if (page->cp_type == CPT_CACHEABLE) {
598                         SetPagePrivate(vmpage);
599                         vmpage->private = (unsigned long)page;
600                         cl_page_slice_add(page, &cpg->cpg_cl, obj,
601                                           &vvp_page_ops);
602                 } else {
603                         struct ccc_object *clobj = cl2ccc(obj);
604
605                         LASSERT(!TRYLOCK_INODE_MUTEX(clobj->cob_inode));
606                         cl_page_slice_add(page, &cpg->cpg_cl, obj,
607                                           &vvp_transient_page_ops);
608                         clobj->cob_transient_pages++;
609                 }
610                 result = 0;
611         } else
612                 result = -ENOMEM;
613         return ERR_PTR(result);
614 }
615