Whamcloud - gitweb
LU-17783 statahead: disable batch statahead for old server
[fs/lustre-release.git] / lustre / llite / vvp_dev.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) 2012, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * cl_device and cl_device_type implementation for VVP layer.
32  *
33  *   Author: Nikita Danilov <nikita.danilov@sun.com>
34  *   Author: Jinshan Xiong <jinshan.xiong@intel.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_LLITE
38
39 #include <obd.h>
40 #include "llite_internal.h"
41 #include "vvp_internal.h"
42 #include <linux/kallsyms.h>
43
44 /*
45  * Vvp device and device type functions.
46  *
47  * vvp_ prefix stands for "Vfs Vm Posix". It corresponds to historical
48  * "llite_" (var. "ll_") prefix.
49  */
50
51 static struct kmem_cache *ll_thread_kmem;
52 struct kmem_cache *vvp_object_kmem;
53 static struct kmem_cache *vvp_session_kmem;
54 static struct kmem_cache *vvp_thread_kmem;
55
56 static struct lu_kmem_descr vvp_caches[] = {
57         {
58                 .ckd_cache = &ll_thread_kmem,
59                 .ckd_name  = "ll_thread_kmem",
60                 .ckd_size  = sizeof(struct ll_thread_info),
61         },
62         {
63                 .ckd_cache = &vvp_object_kmem,
64                 .ckd_name  = "vvp_object_kmem",
65                 .ckd_size  = sizeof(struct vvp_object),
66         },
67         {
68                 .ckd_cache = &vvp_session_kmem,
69                 .ckd_name  = "vvp_session_kmem",
70                 .ckd_size  = sizeof(struct vvp_session)
71         },
72         {
73                 .ckd_cache = &vvp_thread_kmem,
74                 .ckd_name  = "vvp_thread_kmem",
75                 .ckd_size  = sizeof(struct vvp_thread_info),
76         },
77         {
78                 .ckd_cache = NULL
79         }
80 };
81
82 static void *ll_thread_key_init(const struct lu_context *ctx,
83                                 struct lu_context_key *key)
84 {
85         struct ll_thread_info *lti;
86
87         OBD_SLAB_ALLOC_PTR_GFP(lti, ll_thread_kmem, GFP_NOFS);
88         if (lti == NULL)
89                 lti = ERR_PTR(-ENOMEM);
90
91         return lti;
92 }
93
94 static void ll_thread_key_fini(const struct lu_context *ctx,
95                                struct lu_context_key *key, void *data)
96 {
97         struct ll_thread_info *lti = data;
98
99         OBD_SLAB_FREE_PTR(lti, ll_thread_kmem);
100 }
101
102 struct lu_context_key ll_thread_key = {
103         .lct_tags = LCT_CL_THREAD,
104         .lct_init = ll_thread_key_init,
105         .lct_fini = ll_thread_key_fini,
106 };
107
108 static void *vvp_session_key_init(const struct lu_context *ctx,
109                                   struct lu_context_key *key)
110 {
111         struct vvp_session *session;
112
113         OBD_SLAB_ALLOC_PTR_GFP(session, vvp_session_kmem, GFP_NOFS);
114         if (session == NULL)
115                 session = ERR_PTR(-ENOMEM);
116         return session;
117 }
118
119 static void vvp_session_key_fini(const struct lu_context *ctx,
120                                  struct lu_context_key *key, void *data)
121 {
122         struct vvp_session *session = data;
123
124         OBD_SLAB_FREE_PTR(session, vvp_session_kmem);
125 }
126
127 struct lu_context_key vvp_session_key = {
128         .lct_tags = LCT_SESSION,
129         .lct_init = vvp_session_key_init,
130         .lct_fini = vvp_session_key_fini
131 };
132
133 static void *vvp_thread_key_init(const struct lu_context *ctx,
134                                  struct lu_context_key *key)
135 {
136         struct vvp_thread_info *vti;
137
138         OBD_SLAB_ALLOC_PTR_GFP(vti, vvp_thread_kmem, GFP_NOFS);
139         if (vti == NULL)
140                 vti = ERR_PTR(-ENOMEM);
141         return vti;
142 }
143
144 static void vvp_thread_key_fini(const struct lu_context *ctx,
145                                 struct lu_context_key *key, void *data)
146 {
147         struct vvp_thread_info *vti = data;
148
149         OBD_SLAB_FREE_PTR(vti, vvp_thread_kmem);
150 }
151
152 struct lu_context_key vvp_thread_key = {
153         .lct_tags = LCT_CL_THREAD,
154         .lct_init = vvp_thread_key_init,
155         .lct_fini = vvp_thread_key_fini,
156 };
157
158 /* type constructor/destructor: vvp_type_{init,fini,start,stop}(). */
159 LU_TYPE_INIT_FINI(vvp, &ll_thread_key, &vvp_session_key, &vvp_thread_key);
160
161 static const struct lu_device_operations vvp_lu_ops = {
162         .ldo_object_alloc = vvp_object_alloc
163 };
164
165 static struct lu_device *vvp_device_free(const struct lu_env *env,
166                                          struct lu_device *d)
167 {
168         struct vvp_device *vdv  = lu2vvp_dev(d);
169         struct cl_site    *site = lu2cl_site(d->ld_site);
170         struct lu_device  *next = cl2lu_dev(vdv->vdv_next);
171
172         if (d->ld_site != NULL) {
173                 cl_site_fini(site);
174                 OBD_FREE_PTR(site);
175         }
176
177         cl_device_fini(lu2cl_dev(d));
178         OBD_FREE_PTR(vdv);
179         return next;
180 }
181
182 static struct lu_device *vvp_device_alloc(const struct lu_env *env,
183                                           struct lu_device_type *t,
184                                           struct lustre_cfg *cfg)
185 {
186         struct vvp_device *vdv;
187         struct lu_device *lud;
188         struct cl_site *site;
189         int rc;
190
191         ENTRY;
192
193         OBD_ALLOC_PTR(vdv);
194         if (vdv == NULL)
195                 RETURN(ERR_PTR(-ENOMEM));
196
197         lud = &vdv->vdv_cl.cd_lu_dev;
198         cl_device_init(&vdv->vdv_cl, t);
199         vvp2lu_dev(vdv)->ld_ops = &vvp_lu_ops;
200
201         OBD_ALLOC_PTR(site);
202         if (site != NULL) {
203                 rc = cl_site_init(site, &vdv->vdv_cl);
204                 if (rc == 0)
205                         rc = lu_site_init_finish(&site->cs_lu);
206                 else {
207                         LASSERT(lud->ld_site == NULL);
208                         CERROR("Cannot init lu_site, rc %d.\n", rc);
209                         OBD_FREE_PTR(site);
210                 }
211         } else
212                 rc = -ENOMEM;
213         if (rc != 0) {
214                 vvp_device_free(env, lud);
215                 lud = ERR_PTR(rc);
216         }
217         RETURN(lud);
218 }
219
220 static int vvp_device_init(const struct lu_env *env, struct lu_device *d,
221                            const char *name, struct lu_device *next)
222 {
223         struct vvp_device  *vdv;
224         int rc;
225
226         ENTRY;
227
228         vdv = lu2vvp_dev(d);
229         vdv->vdv_next = lu2cl_dev(next);
230
231         LASSERT(d->ld_site != NULL && next->ld_type != NULL);
232         next->ld_site = d->ld_site;
233         rc = next->ld_type->ldt_ops->ldto_device_init(
234                 env, next, next->ld_type->ldt_name, NULL);
235         if (rc == 0) {
236                 lu_device_get(next);
237                 lu_ref_add(&next->ld_reference, "lu-stack", &lu_site_init);
238         }
239         RETURN(rc);
240 }
241
242 static struct lu_device *vvp_device_fini(const struct lu_env *env,
243                                          struct lu_device *d)
244 {
245         return cl2lu_dev(lu2vvp_dev(d)->vdv_next);
246 }
247
248 static const struct lu_device_type_operations vvp_device_type_ops = {
249         .ldto_init = vvp_type_init,
250         .ldto_fini = vvp_type_fini,
251
252         .ldto_start = vvp_type_start,
253         .ldto_stop  = vvp_type_stop,
254
255         .ldto_device_alloc      = vvp_device_alloc,
256         .ldto_device_free       = vvp_device_free,
257         .ldto_device_init       = vvp_device_init,
258         .ldto_device_fini       = vvp_device_fini,
259 };
260
261 struct lu_device_type vvp_device_type = {
262         .ldt_tags     = LU_DEVICE_CL,
263         .ldt_name     = LUSTRE_VVP_NAME,
264         .ldt_ops      = &vvp_device_type_ops,
265         .ldt_ctx_tags = LCT_CL_THREAD
266 };
267
268 unsigned int (*vvp_account_page_dirtied)(struct page *page,
269                                          struct address_space *mapping);
270
271 /**
272  * A mutex serializing calls to vvp_inode_fini() under extreme memory
273  * pressure, when environments cannot be allocated.
274  */
275 int vvp_global_init(void)
276 {
277         int rc;
278
279         rc = lu_kmem_init(vvp_caches);
280         if (rc != 0)
281                 return rc;
282
283         rc = lu_device_type_init(&vvp_device_type);
284         if (rc != 0)
285                 goto out_kmem;
286
287 #ifndef HAVE_ACCOUNT_PAGE_DIRTIED_EXPORT
288 #ifdef HAVE_KALLSYMS_LOOKUP_NAME
289         /*
290          * Kernel v5.2-5678-gac1c3e4 no longer exports account_page_dirtied
291          */
292         vvp_account_page_dirtied = (void *)
293                 cfs_kallsyms_lookup_name("account_page_dirtied");
294 #endif
295 #endif
296
297         return 0;
298
299 out_kmem:
300         lu_kmem_fini(vvp_caches);
301
302         return rc;
303 }
304
305 void vvp_global_fini(void)
306 {
307         lu_device_type_fini(&vvp_device_type);
308         lu_kmem_fini(vvp_caches);
309 }
310
311 /*
312  * mirror obd-devices into cl devices.
313  */
314
315 int cl_sb_init(struct super_block *sb)
316 {
317         struct ll_sb_info *sbi;
318         struct cl_device  *cl;
319         struct lu_env     *env;
320         int rc = 0;
321         __u16 refcheck;
322
323         sbi  = ll_s2sbi(sb);
324         env = cl_env_get(&refcheck);
325         if (!IS_ERR(env)) {
326                 cl = cl_type_setup(env, NULL, &vvp_device_type,
327                                    sbi->ll_dt_exp->exp_obd->obd_lu_dev);
328                 if (!IS_ERR(cl)) {
329                         sbi->ll_cl = cl;
330                         sbi->ll_site = cl2lu_dev(cl)->ld_site;
331                 }
332                 cl_env_put(env, &refcheck);
333         } else
334                 rc = PTR_ERR(env);
335         RETURN(rc);
336 }
337
338 int cl_sb_fini(struct super_block *sb)
339 {
340         struct ll_sb_info *sbi;
341         struct lu_env     *env;
342         struct cl_device  *cld;
343         __u16              refcheck;
344         int                result;
345
346         ENTRY;
347         sbi = ll_s2sbi(sb);
348         env = cl_env_get(&refcheck);
349         if (!IS_ERR(env)) {
350                 cld = sbi->ll_cl;
351
352                 if (cld != NULL) {
353                         cl_stack_fini(env, cld);
354                         sbi->ll_cl = NULL;
355                         sbi->ll_site = NULL;
356                 }
357                 cl_env_put(env, &refcheck);
358                 result = 0;
359         } else {
360                 CERROR("Cannot cleanup cl-stack due to memory shortage.\n");
361                 result = PTR_ERR(env);
362         }
363
364         RETURN(result);
365 }
366
367 /* debugfs/lustre/llite/$MNT/dump_page_cache */
368 struct vvp_seq_private {
369         struct ll_sb_info       *vsp_sbi;
370         struct lu_env           *vsp_env;
371         u16                     vsp_refcheck;
372         struct cl_object        *vsp_clob;
373         struct rhashtable_iter  vsp_iter;
374         u32                     vsp_page_index;
375         /*
376          * prev_pos is the 'pos' of the last object returned
377          * by ->start of ->next.
378          */
379         loff_t                  vvp_prev_pos;
380 };
381
382 static unsigned int
383 ll_filemap_get_one_page_contig(struct address_space *mapping,
384                                pgoff_t start, struct page **pg)
385 {
386 #ifdef HAVE_FILEMAP_GET_FOLIOS_CONTIG
387         struct folio_batch fbatch;
388         int nr;
389
390         folio_batch_init(&fbatch);
391         *pg = NULL;
392
393         nr = filemap_get_folios_contig(mapping, &start, start, &fbatch);
394         if (nr == PAGEVEC_SIZE) {
395                 --nr;
396                 *pg = folio_page(fbatch.folios[nr], 0);
397                 return 1;
398         }
399         return 0;
400 #else /* !HAVE_FILEMAP_GET_FOLIOS_CONTIG */
401         return find_get_pages_contig(mapping, start, 1, pg);
402 #endif
403 }
404
405 static struct page *vvp_pgcache_current(struct vvp_seq_private *priv)
406 {
407         struct lu_device *dev = &priv->vsp_sbi->ll_cl->cd_lu_dev;
408         struct lu_object_header *h;
409         struct page *vmpage = NULL;
410
411         rhashtable_walk_start(&priv->vsp_iter);
412         while ((h = rhashtable_walk_next(&priv->vsp_iter)) != NULL) {
413                 struct inode *inode;
414                 int nr;
415
416                 if (IS_ERR(h)) {
417                         if (PTR_ERR(h) == -EAGAIN)
418                                 continue;
419                         break;
420                 }
421
422                 if (!priv->vsp_clob) {
423                         struct lu_object *lu_obj;
424
425                         lu_obj = lu_object_get_first(h, dev);
426                         if (!lu_obj)
427                                 continue;
428
429                         priv->vsp_clob = lu2cl(lu_obj);
430                         lu_object_ref_add_atomic(lu_obj, "dump", current);
431                         priv->vsp_page_index = 0;
432                 }
433
434                 inode = vvp_object_inode(priv->vsp_clob);
435                 nr = ll_filemap_get_one_page_contig(inode->i_mapping,
436                                                     priv->vsp_page_index,
437                                                     &vmpage);
438                 if (nr > 0) {
439                         priv->vsp_page_index = vmpage->index;
440                         break;
441                 }
442                 lu_object_ref_del(&priv->vsp_clob->co_lu, "dump", current);
443                 cl_object_put(priv->vsp_env, priv->vsp_clob);
444                 priv->vsp_clob = NULL;
445                 priv->vsp_page_index = 0;
446         }
447         rhashtable_walk_stop(&priv->vsp_iter);
448         return vmpage;
449 }
450
451 #define seq_page_flag(seq, page, flag, has_flags) do {                  \
452         if (test_bit(PG_##flag, &(page)->flags)) {                      \
453                 seq_printf(seq, "%s"#flag, has_flags ? "|" : "");       \
454                 has_flags = 1;                                          \
455         }                                                               \
456 } while (0)
457
458 static void vvp_pgcache_page_show(const struct lu_env *env,
459                                   struct seq_file *seq, struct cl_page *page)
460 {
461         struct page *vmpage;
462         int has_flags;
463
464         vmpage = page->cp_vmpage;
465         seq_printf(seq, " %5i | %pK %pK %s %s | %pK "DFID"(%pK) %lu %u [",
466                    0 /* gen */,
467                    NULL, /* was vvp_page */
468                    page,
469                    "none",
470                    PageWriteback(vmpage) ? "wb" : "-",
471                    vmpage,
472                    PFID(ll_inode2fid(vmpage->mapping->host)),
473                    vmpage->mapping->host, vmpage->index,
474                    page_count(vmpage));
475         has_flags = 0;
476         seq_page_flag(seq, vmpage, locked, has_flags);
477         seq_page_flag(seq, vmpage, error, has_flags);
478         seq_page_flag(seq, vmpage, referenced, has_flags);
479         seq_page_flag(seq, vmpage, uptodate, has_flags);
480         seq_page_flag(seq, vmpage, dirty, has_flags);
481         seq_page_flag(seq, vmpage, writeback, has_flags);
482         seq_printf(seq, "%s]\n", has_flags ? "" : "-");
483 }
484
485 static int vvp_pgcache_show(struct seq_file *f, void *v)
486 {
487         struct vvp_seq_private *priv = f->private;
488         struct page *vmpage = v;
489         struct cl_page *page;
490
491         seq_printf(f, "%8lx@" DFID ": ", vmpage->index,
492                    PFID(lu_object_fid(&priv->vsp_clob->co_lu)));
493         lock_page(vmpage);
494         page = cl_vmpage_page(vmpage, priv->vsp_clob);
495         unlock_page(vmpage);
496         put_page(vmpage);
497
498         if (page) {
499                 vvp_pgcache_page_show(priv->vsp_env, f, page);
500                 cl_page_put(priv->vsp_env, page);
501         } else {
502                 seq_puts(f, "missing\n");
503         }
504
505         return 0;
506 }
507
508 static void vvp_pgcache_rewind(struct vvp_seq_private *priv)
509 {
510         if (priv->vvp_prev_pos) {
511                 struct lu_site *s = priv->vsp_sbi->ll_cl->cd_lu_dev.ld_site;
512
513                 rhashtable_walk_exit(&priv->vsp_iter);
514                 rhashtable_walk_enter(&s->ls_obj_hash, &priv->vsp_iter);
515                 priv->vvp_prev_pos = 0;
516                 if (priv->vsp_clob) {
517                         lu_object_ref_del(&priv->vsp_clob->co_lu, "dump",
518                                           current);
519                         cl_object_put(priv->vsp_env, priv->vsp_clob);
520                 }
521                 priv->vsp_clob = NULL;
522         }
523 }
524
525 static struct page *vvp_pgcache_next_page(struct vvp_seq_private *priv)
526 {
527         priv->vsp_page_index += 1;
528         return vvp_pgcache_current(priv);
529 }
530
531 static void *vvp_pgcache_start(struct seq_file *f, loff_t *pos)
532 {
533         struct vvp_seq_private *priv = f->private;
534
535         if (*pos == 0) {
536                 vvp_pgcache_rewind(priv);
537         } else if (*pos == priv->vvp_prev_pos) {
538                 /* Return the current item */;
539         } else {
540                 WARN_ON(*pos != priv->vvp_prev_pos + 1);
541                 priv->vsp_page_index += 1;
542         }
543
544         priv->vvp_prev_pos = *pos;
545         return vvp_pgcache_current(priv);
546 }
547
548 static void *vvp_pgcache_next(struct seq_file *f, void *v, loff_t *pos)
549 {
550         struct vvp_seq_private *priv = f->private;
551
552         WARN_ON(*pos != priv->vvp_prev_pos);
553         *pos += 1;
554         priv->vvp_prev_pos = *pos;
555         return vvp_pgcache_next_page(priv);
556 }
557
558 static void vvp_pgcache_stop(struct seq_file *f, void *v)
559 {
560         /* Nothing to do */
561 }
562
563 static const struct seq_operations vvp_pgcache_ops = {
564         .start = vvp_pgcache_start,
565         .next  = vvp_pgcache_next,
566         .stop  = vvp_pgcache_stop,
567         .show  = vvp_pgcache_show
568 };
569
570 static int vvp_dump_pgcache_seq_open(struct inode *inode, struct file *filp)
571 {
572         struct vvp_seq_private *priv;
573         struct lu_site *s;
574
575         priv = __seq_open_private(filp, &vvp_pgcache_ops, sizeof(*priv));
576         if (!priv)
577                 return -ENOMEM;
578
579         priv->vsp_sbi = inode->i_private;
580         priv->vsp_env = cl_env_get(&priv->vsp_refcheck);
581         priv->vsp_clob = NULL;
582         if (IS_ERR(priv->vsp_env)) {
583                 int err = PTR_ERR(priv->vsp_env);
584
585                 seq_release_private(inode, filp);
586                 return err;
587         }
588
589         s = priv->vsp_sbi->ll_cl->cd_lu_dev.ld_site;
590         rhashtable_walk_enter(&s->ls_obj_hash, &priv->vsp_iter);
591
592         return 0;
593 }
594
595 static int vvp_dump_pgcache_seq_release(struct inode *inode, struct file *file)
596 {
597         struct seq_file *seq = file->private_data;
598         struct vvp_seq_private *priv = seq->private;
599
600         if (priv->vsp_clob) {
601                 lu_object_ref_del(&priv->vsp_clob->co_lu, "dump", current);
602                 cl_object_put(priv->vsp_env, priv->vsp_clob);
603         }
604         cl_env_put(priv->vsp_env, &priv->vsp_refcheck);
605         rhashtable_walk_exit(&priv->vsp_iter);
606         return seq_release_private(inode, file);
607 }
608
609 const struct file_operations vvp_dump_pgcache_file_ops = {
610         .owner   = THIS_MODULE,
611         .open    = vvp_dump_pgcache_seq_open,
612         .read    = seq_read,
613         .llseek  = seq_lseek,
614         .release = vvp_dump_pgcache_seq_release,
615 };