Whamcloud - gitweb
LU-13826 utils: fix compatibility for LL_IOC_MDC_GETINFO
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * Implementation of cl_page for VVP layer.
33  *
34  *   Author: Nikita Danilov <nikita.danilov@sun.com>
35  *   Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_LLITE
39
40 #include <linux/atomic.h>
41 #include <linux/bitops.h>
42 #include <linux/mm.h>
43 #include <linux/mutex.h>
44 #include <linux/page-flags.h>
45 #include <linux/pagemap.h>
46
47 #include <libcfs/libcfs.h>
48 #include "llite_internal.h"
49 #include "vvp_internal.h"
50
51 /*****************************************************************************
52  *
53  * Page operations.
54  *
55  */
56
57 static void vvp_page_fini_common(struct vvp_page *vpg, struct pagevec *pvec)
58 {
59         struct page *vmpage = vpg->vpg_page;
60
61         LASSERT(vmpage != NULL);
62         if (pvec) {
63                 if (!pagevec_add(pvec, vmpage))
64                         pagevec_release(pvec);
65         } else {
66                 put_page(vmpage);
67         }
68 }
69
70 static void vvp_page_fini(const struct lu_env *env,
71                           struct cl_page_slice *slice,
72                           struct pagevec *pvec)
73 {
74         struct vvp_page *vpg     = cl2vvp_page(slice);
75         struct page     *vmpage  = vpg->vpg_page;
76
77         /*
78          * vmpage->private was already cleared when page was moved into
79          * VPG_FREEING state.
80          */
81         LASSERT((struct cl_page *)vmpage->private != slice->cpl_page);
82         vvp_page_fini_common(vpg, pvec);
83 }
84
85 static int vvp_page_own(const struct lu_env *env,
86                         const struct cl_page_slice *slice, struct cl_io *io,
87                         int nonblock)
88 {
89         struct vvp_page *vpg    = cl2vvp_page(slice);
90         struct page     *vmpage = vpg->vpg_page;
91
92         ENTRY;
93
94         LASSERT(vmpage != NULL);
95         if (nonblock) {
96                 if (!trylock_page(vmpage))
97                         return -EAGAIN;
98
99                 if (unlikely(PageWriteback(vmpage))) {
100                         unlock_page(vmpage);
101                         return -EAGAIN;
102                 }
103
104                 return 0;
105         }
106
107         lock_page(vmpage);
108         wait_on_page_writeback(vmpage);
109
110         RETURN(0);
111 }
112
113 static void vvp_page_assume(const struct lu_env *env,
114                             const struct cl_page_slice *slice,
115                             struct cl_io *unused)
116 {
117         struct page *vmpage = cl2vm_page(slice);
118
119         LASSERT(vmpage != NULL);
120         LASSERT(PageLocked(vmpage));
121         wait_on_page_writeback(vmpage);
122 }
123
124 static void vvp_page_unassume(const struct lu_env *env,
125                               const struct cl_page_slice *slice,
126                               struct cl_io *unused)
127 {
128         struct page *vmpage = cl2vm_page(slice);
129
130         LASSERT(vmpage != NULL);
131         LASSERT(PageLocked(vmpage));
132 }
133
134 static void vvp_page_disown(const struct lu_env *env,
135                             const struct cl_page_slice *slice, struct cl_io *io)
136 {
137         struct page *vmpage = cl2vm_page(slice);
138
139         ENTRY;
140
141         LASSERT(vmpage != NULL);
142         LASSERT(PageLocked(vmpage));
143
144         unlock_page(cl2vm_page(slice));
145
146         EXIT;
147 }
148
149 static void vvp_page_discard(const struct lu_env *env,
150                              const struct cl_page_slice *slice,
151                              struct cl_io *unused)
152 {
153         struct page     *vmpage = cl2vm_page(slice);
154         struct vvp_page *vpg    = cl2vvp_page(slice);
155
156         LASSERT(vmpage != NULL);
157         LASSERT(PageLocked(vmpage));
158
159         if (vpg->vpg_defer_uptodate && !vpg->vpg_ra_used && vmpage->mapping)
160                 ll_ra_stats_inc(vmpage->mapping->host, RA_STAT_DISCARDED);
161
162         generic_error_remove_page(vmpage->mapping, vmpage);
163 }
164
165 static void vvp_page_delete(const struct lu_env *env,
166                             const struct cl_page_slice *slice)
167 {
168         struct page      *vmpage = cl2vm_page(slice);
169         struct cl_page   *page   = slice->cpl_page;
170         int refc;
171
172         LASSERT(PageLocked(vmpage));
173         LASSERT((struct cl_page *)vmpage->private == page);
174
175
176         /* Drop the reference count held in vvp_page_init */
177         refc = atomic_dec_return(&page->cp_ref);
178         LASSERTF(refc >= 1, "page = %p, refc = %d\n", page, refc);
179
180         ClearPagePrivate(vmpage);
181         vmpage->private = 0;
182         /*
183          * Reference from vmpage to cl_page is removed, but the reference back
184          * is still here. It is removed later in vvp_page_fini().
185          */
186 }
187
188 static void vvp_page_export(const struct lu_env *env,
189                             const struct cl_page_slice *slice,
190                             int uptodate)
191 {
192         struct page *vmpage = cl2vm_page(slice);
193
194         LASSERT(vmpage != NULL);
195         LASSERT(PageLocked(vmpage));
196         if (uptodate)
197                 SetPageUptodate(vmpage);
198         else
199                 ClearPageUptodate(vmpage);
200 }
201
202 static int vvp_page_is_vmlocked(const struct lu_env *env,
203                                 const struct cl_page_slice *slice)
204 {
205         return PageLocked(cl2vm_page(slice)) ? -EBUSY : -ENODATA;
206 }
207
208 static int vvp_page_prep_read(const struct lu_env *env,
209                               const struct cl_page_slice *slice,
210                               struct cl_io *unused)
211 {
212         ENTRY;
213         /* Skip the page already marked as PG_uptodate. */
214         RETURN(PageUptodate(cl2vm_page(slice)) ? -EALREADY : 0);
215 }
216
217 static int vvp_page_prep_write(const struct lu_env *env,
218                                const struct cl_page_slice *slice,
219                                struct cl_io *unused)
220 {
221         struct page *vmpage = cl2vm_page(slice);
222         struct cl_page *pg = slice->cpl_page;
223
224         LASSERT(PageLocked(vmpage));
225         LASSERT(!PageDirty(vmpage));
226
227         /* ll_writepage path is not a sync write, so need to set page writeback
228          * flag
229          */
230         if (pg->cp_sync_io == NULL)
231                 set_page_writeback(vmpage);
232
233         return 0;
234 }
235
236 /**
237  * Handles page transfer errors at VM level.
238  *
239  * This takes inode as a separate argument, because inode on which error is to
240  * be set can be different from \a vmpage inode in case of direct-io.
241  */
242 static void vvp_vmpage_error(struct inode *inode, struct page *vmpage,
243                              int ioret)
244 {
245         struct vvp_object *obj = cl_inode2vvp(inode);
246
247         if (ioret == 0) {
248                 ClearPageError(vmpage);
249                 obj->vob_discard_page_warned = 0;
250         } else {
251                 SetPageError(vmpage);
252                 if (ioret == -ENOSPC)
253                         set_bit(AS_ENOSPC, &inode->i_mapping->flags);
254                 else
255                         set_bit(AS_EIO, &inode->i_mapping->flags);
256
257                 if ((ioret == -ESHUTDOWN || ioret == -EINTR ||
258                      ioret == -EIO) && obj->vob_discard_page_warned == 0) {
259                         obj->vob_discard_page_warned = 1;
260                         ll_dirty_page_discard_warn(vmpage, ioret);
261                 }
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 vvp_page *vpg    = cl2vvp_page(slice);
270         struct page     *vmpage = vpg->vpg_page;
271         struct cl_page  *page   = slice->cpl_page;
272         struct inode    *inode  = vvp_object_inode(page->cp_obj);
273
274         ENTRY;
275         LASSERT(PageLocked(vmpage));
276         CL_PAGE_HEADER(D_PAGE, env, page, "completing READ with %d\n", ioret);
277
278         if (vpg->vpg_defer_uptodate)
279                 ll_ra_count_put(ll_i2sbi(inode), 1);
280
281         if (ioret == 0)  {
282                 if (!vpg->vpg_defer_uptodate)
283                         cl_page_export(env, page, 1);
284         } else if (vpg->vpg_defer_uptodate) {
285                 vpg->vpg_defer_uptodate = 0;
286                 if (ioret == -EWOULDBLOCK) {
287                         /* mirror read failed, it needs to destroy the page
288                          * because subpage would be from wrong osc when trying
289                          * to read from a new mirror
290                          */
291                         generic_error_remove_page(vmpage->mapping, vmpage);
292                 }
293         }
294
295         if (page->cp_sync_io == NULL)
296                 unlock_page(vmpage);
297
298         EXIT;
299 }
300
301 static void vvp_page_completion_write(const struct lu_env *env,
302                                       const struct cl_page_slice *slice,
303                                       int ioret)
304 {
305         struct vvp_page *vpg    = cl2vvp_page(slice);
306         struct cl_page  *pg     = slice->cpl_page;
307         struct page     *vmpage = vpg->vpg_page;
308
309         ENTRY;
310         CL_PAGE_HEADER(D_PAGE, env, pg, "completing WRITE with %d\n", ioret);
311
312         if (pg->cp_sync_io != NULL) {
313                 LASSERT(PageLocked(vmpage));
314                 LASSERT(!PageWriteback(vmpage));
315         } else {
316                 LASSERT(PageWriteback(vmpage));
317                 /*
318                  * Only mark the page error only when it's an async write
319                  * because applications won't wait for IO to finish.
320                  */
321                 vvp_vmpage_error(vvp_object_inode(pg->cp_obj), vmpage, ioret);
322
323                 end_page_writeback(vmpage);
324         }
325         EXIT;
326 }
327
328 /**
329  * Implements cl_page_operations::cpo_make_ready() method.
330  *
331  * This is called to yank a page from the transfer cache and to send it out as
332  * a part of transfer. This function try-locks the page. If try-lock failed,
333  * page is owned by some concurrent IO, and should be skipped (this is bad,
334  * but hopefully rare situation, as it usually results in transfer being
335  * shorter than possible).
336  *
337  * \retval 0      success, page can be placed into transfer
338  *
339  * \retval -EAGAIN page is either used by concurrent IO has been
340  * truncated. Skip it.
341  */
342 static int vvp_page_make_ready(const struct lu_env *env,
343                                const struct cl_page_slice *slice)
344 {
345         struct page *vmpage = cl2vm_page(slice);
346         struct cl_page *pg = slice->cpl_page;
347         int result = 0;
348
349         lock_page(vmpage);
350         if (clear_page_dirty_for_io(vmpage)) {
351                 LASSERT(pg->cp_state == CPS_CACHED);
352                 /* This actually clears the dirty bit in the radix
353                  * tree.
354                  */
355                 set_page_writeback(vmpage);
356                 CL_PAGE_HEADER(D_PAGE, env, pg, "readied\n");
357         } else if (pg->cp_state == CPS_PAGEOUT) {
358                 /* is it possible for osc_flush_async_page() to already
359                  * make it ready?
360                  */
361                 result = -EALREADY;
362         } else {
363                 CL_PAGE_DEBUG(D_ERROR, env, pg, "Unexpecting page state %d.\n",
364                               pg->cp_state);
365                 LBUG();
366         }
367         unlock_page(vmpage);
368         RETURN(result);
369 }
370
371 static int vvp_page_print(const struct lu_env *env,
372                           const struct cl_page_slice *slice,
373                           void *cookie, lu_printer_t printer)
374 {
375         struct vvp_page *vpg    = cl2vvp_page(slice);
376         struct page     *vmpage = vpg->vpg_page;
377
378         (*printer)(env, cookie,
379                    LUSTRE_VVP_NAME"-page@%p(%d:%d) vm@%p ",
380                    vpg, vpg->vpg_defer_uptodate, vpg->vpg_ra_used, vmpage);
381
382         if (vmpage != NULL) {
383                 (*printer)(env, cookie, "%lx %d:%d %lx %lu %slru",
384                            (long)vmpage->flags, page_count(vmpage),
385                            page_mapcount(vmpage), vmpage->private,
386                            page_index(vmpage),
387                            list_empty(&vmpage->lru) ? "not-" : "");
388         }
389
390         (*printer)(env, cookie, "\n");
391
392         return 0;
393 }
394
395 static int vvp_page_fail(const struct lu_env *env,
396                          const struct cl_page_slice *slice)
397 {
398         /*
399          * Cached read?
400          */
401         LBUG();
402
403         return 0;
404 }
405
406 static const struct cl_page_operations vvp_page_ops = {
407         .cpo_own           = vvp_page_own,
408         .cpo_assume        = vvp_page_assume,
409         .cpo_unassume      = vvp_page_unassume,
410         .cpo_disown        = vvp_page_disown,
411         .cpo_discard       = vvp_page_discard,
412         .cpo_delete        = vvp_page_delete,
413         .cpo_export        = vvp_page_export,
414         .cpo_is_vmlocked   = vvp_page_is_vmlocked,
415         .cpo_fini          = vvp_page_fini,
416         .cpo_print         = vvp_page_print,
417         .io = {
418                 [CRT_READ] = {
419                         .cpo_prep       = vvp_page_prep_read,
420                         .cpo_completion = vvp_page_completion_read,
421                         .cpo_make_ready = vvp_page_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_discard(const struct lu_env *env,
432                                        const struct cl_page_slice *slice,
433                                        struct cl_io *unused)
434 {
435         struct cl_page *page = slice->cpl_page;
436
437         /*
438          * For transient pages, remove it from the radix tree.
439          */
440         cl_page_delete(env, page);
441 }
442
443 static int vvp_transient_page_is_vmlocked(const struct lu_env *env,
444                                           const struct cl_page_slice *slice)
445 {
446         return -EBUSY;
447 }
448
449 static void vvp_transient_page_fini(const struct lu_env *env,
450                                     struct cl_page_slice *slice,
451                                     struct pagevec *pvec)
452 {
453         struct vvp_page *vpg = cl2vvp_page(slice);
454         struct vvp_object *clobj = cl2vvp(slice->cpl_obj);
455
456         vvp_page_fini_common(vpg, pvec);
457         atomic_dec(&clobj->vob_transient_pages);
458 }
459
460 static const struct cl_page_operations vvp_transient_page_ops = {
461         .cpo_discard            = vvp_transient_page_discard,
462         .cpo_fini               = vvp_transient_page_fini,
463         .cpo_is_vmlocked        = vvp_transient_page_is_vmlocked,
464         .cpo_print              = vvp_page_print,
465 };
466
467 int vvp_page_init(const struct lu_env *env, struct cl_object *obj,
468                 struct cl_page *page, pgoff_t index)
469 {
470         struct vvp_page *vpg = cl_object_page_slice(obj, page);
471         struct page     *vmpage = page->cp_vmpage;
472
473         CLOBINVRNT(env, obj, vvp_object_invariant(obj));
474
475         vpg->vpg_page = vmpage;
476         get_page(vmpage);
477
478         if (page->cp_type == CPT_CACHEABLE) {
479                 /* in cache, decref in vvp_page_delete */
480                 atomic_inc(&page->cp_ref);
481                 SetPagePrivate(vmpage);
482                 vmpage->private = (unsigned long)page;
483                 cl_page_slice_add(page, &vpg->vpg_cl, obj,
484                                 &vvp_page_ops);
485         } else {
486                 struct vvp_object *clobj = cl2vvp(obj);
487
488                 cl_page_slice_add(page, &vpg->vpg_cl, obj,
489                                 &vvp_transient_page_ops);
490                 atomic_inc(&clobj->vob_transient_pages);
491         }
492         return 0;
493 }