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