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