4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * cl_device and cl_device_type implementation for VVP layer.
33 * Author: Nikita Danilov <nikita.danilov@sun.com>
34 * Author: Jinshan Xiong <jinshan.xiong@intel.com>
37 #define DEBUG_SUBSYSTEM S_LLITE
40 #include "llite_internal.h"
41 #include "vvp_internal.h"
42 #include <linux/kallsyms.h>
45 * Vvp device and device type functions.
47 * vvp_ prefix stands for "Vfs Vm Posix". It corresponds to historical
48 * "llite_" (var. "ll_") prefix.
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;
56 static struct lu_kmem_descr vvp_caches[] = {
58 .ckd_cache = &ll_thread_kmem,
59 .ckd_name = "ll_thread_kmem",
60 .ckd_size = sizeof(struct ll_thread_info),
63 .ckd_cache = &vvp_object_kmem,
64 .ckd_name = "vvp_object_kmem",
65 .ckd_size = sizeof(struct vvp_object),
68 .ckd_cache = &vvp_session_kmem,
69 .ckd_name = "vvp_session_kmem",
70 .ckd_size = sizeof(struct vvp_session)
73 .ckd_cache = &vvp_thread_kmem,
74 .ckd_name = "vvp_thread_kmem",
75 .ckd_size = sizeof(struct vvp_thread_info),
82 static void *ll_thread_key_init(const struct lu_context *ctx,
83 struct lu_context_key *key)
85 struct ll_thread_info *lti;
87 OBD_SLAB_ALLOC_PTR_GFP(lti, ll_thread_kmem, GFP_NOFS);
89 lti = ERR_PTR(-ENOMEM);
94 static void ll_thread_key_fini(const struct lu_context *ctx,
95 struct lu_context_key *key, void *data)
97 struct ll_thread_info *lti = data;
99 OBD_SLAB_FREE_PTR(lti, ll_thread_kmem);
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,
108 static void *vvp_session_key_init(const struct lu_context *ctx,
109 struct lu_context_key *key)
111 struct vvp_session *session;
113 OBD_SLAB_ALLOC_PTR_GFP(session, vvp_session_kmem, GFP_NOFS);
115 session = ERR_PTR(-ENOMEM);
119 static void vvp_session_key_fini(const struct lu_context *ctx,
120 struct lu_context_key *key, void *data)
122 struct vvp_session *session = data;
124 OBD_SLAB_FREE_PTR(session, vvp_session_kmem);
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
133 static void *vvp_thread_key_init(const struct lu_context *ctx,
134 struct lu_context_key *key)
136 struct vvp_thread_info *vti;
138 OBD_SLAB_ALLOC_PTR_GFP(vti, vvp_thread_kmem, GFP_NOFS);
140 vti = ERR_PTR(-ENOMEM);
144 static void vvp_thread_key_fini(const struct lu_context *ctx,
145 struct lu_context_key *key, void *data)
147 struct vvp_thread_info *vti = data;
149 OBD_SLAB_FREE_PTR(vti, vvp_thread_kmem);
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,
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);
161 static const struct lu_device_operations vvp_lu_ops = {
162 .ldo_object_alloc = vvp_object_alloc
165 static struct lu_device *vvp_device_free(const struct lu_env *env,
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);
172 if (d->ld_site != NULL) {
177 cl_device_fini(lu2cl_dev(d));
182 static struct lu_device *vvp_device_alloc(const struct lu_env *env,
183 struct lu_device_type *t,
184 struct lustre_cfg *cfg)
186 struct vvp_device *vdv;
187 struct lu_device *lud;
188 struct cl_site *site;
195 RETURN(ERR_PTR(-ENOMEM));
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;
203 rc = cl_site_init(site, &vdv->vdv_cl);
205 rc = lu_site_init_finish(&site->cs_lu);
207 LASSERT(lud->ld_site == NULL);
208 CERROR("Cannot init lu_site, rc %d.\n", rc);
214 vvp_device_free(env, lud);
220 static int vvp_device_init(const struct lu_env *env, struct lu_device *d,
221 const char *name, struct lu_device *next)
223 struct vvp_device *vdv;
229 vdv->vdv_next = lu2cl_dev(next);
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);
237 lu_ref_add(&next->ld_reference, "lu-stack", &lu_site_init);
242 static struct lu_device *vvp_device_fini(const struct lu_env *env,
245 return cl2lu_dev(lu2vvp_dev(d)->vdv_next);
248 static const struct lu_device_type_operations vvp_device_type_ops = {
249 .ldto_init = vvp_type_init,
250 .ldto_fini = vvp_type_fini,
252 .ldto_start = vvp_type_start,
253 .ldto_stop = vvp_type_stop,
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,
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
268 unsigned int (*vvp_account_page_dirtied)(struct page *page,
269 struct address_space *mapping);
272 * A mutex serializing calls to vvp_inode_fini() under extreme memory
273 * pressure, when environments cannot be allocated.
275 int vvp_global_init(void)
279 rc = lu_kmem_init(vvp_caches);
283 rc = lu_device_type_init(&vvp_device_type);
287 #ifndef HAVE_ACCOUNT_PAGE_DIRTIED_EXPORT
288 #ifdef HAVE_KALLSYMS_LOOKUP_NAME
290 * Kernel v5.2-5678-gac1c3e4 no longer exports account_page_dirtied
292 vvp_account_page_dirtied = (void *)
293 cfs_kallsyms_lookup_name("account_page_dirtied");
300 lu_kmem_fini(vvp_caches);
305 void vvp_global_fini(void)
307 lu_device_type_fini(&vvp_device_type);
308 lu_kmem_fini(vvp_caches);
312 * mirror obd-devices into cl devices.
315 int cl_sb_init(struct super_block *sb)
317 struct ll_sb_info *sbi;
318 struct cl_device *cl;
324 env = cl_env_get(&refcheck);
326 cl = cl_type_setup(env, NULL, &vvp_device_type,
327 sbi->ll_dt_exp->exp_obd->obd_lu_dev);
330 sbi->ll_site = cl2lu_dev(cl)->ld_site;
332 cl_env_put(env, &refcheck);
338 int cl_sb_fini(struct super_block *sb)
340 struct ll_sb_info *sbi;
342 struct cl_device *cld;
348 env = cl_env_get(&refcheck);
353 cl_stack_fini(env, cld);
357 cl_env_put(env, &refcheck);
360 CERROR("Cannot cleanup cl-stack due to memory shortage.\n");
361 result = PTR_ERR(env);
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;
372 struct cl_object *vsp_clob;
373 struct rhashtable_iter vsp_iter;
376 * prev_pos is the 'pos' of the last object returned
377 * by ->start of ->next.
383 ll_filemap_get_one_page_contig(struct address_space *mapping,
384 pgoff_t start, struct page **pg)
386 #ifdef HAVE_FILEMAP_GET_FOLIOS_CONTIG
387 struct folio_batch fbatch;
390 folio_batch_init(&fbatch);
393 nr = filemap_get_folios_contig(mapping, &start, start, &fbatch);
394 if (nr == PAGEVEC_SIZE) {
396 *pg = folio_page(fbatch.folios[nr], 0);
400 #else /* !HAVE_FILEMAP_GET_FOLIOS_CONTIG */
401 return find_get_pages_contig(mapping, start, 1, pg);
405 static struct page *vvp_pgcache_current(struct vvp_seq_private *priv)
407 struct lu_device *dev = &priv->vsp_sbi->ll_cl->cd_lu_dev;
408 struct lu_object_header *h;
409 struct page *vmpage = NULL;
411 rhashtable_walk_start(&priv->vsp_iter);
412 while ((h = rhashtable_walk_next(&priv->vsp_iter)) != NULL) {
417 if (PTR_ERR(h) == -EAGAIN)
422 if (!priv->vsp_clob) {
423 struct lu_object *lu_obj;
425 lu_obj = lu_object_get_first(h, dev);
429 priv->vsp_clob = lu2cl(lu_obj);
430 lu_object_ref_add_atomic(lu_obj, "dump", current);
431 priv->vsp_page_index = 0;
434 inode = vvp_object_inode(priv->vsp_clob);
435 nr = ll_filemap_get_one_page_contig(inode->i_mapping,
436 priv->vsp_page_index,
439 priv->vsp_page_index = vmpage->index;
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;
447 rhashtable_walk_stop(&priv->vsp_iter);
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 ? "|" : ""); \
458 static void vvp_pgcache_page_show(const struct lu_env *env,
459 struct seq_file *seq, struct cl_page *page)
464 vmpage = page->cp_vmpage;
465 seq_printf(seq, " %5i | %pK %pK %s %s | %pK "DFID"(%pK) %lu %u [",
467 NULL, /* was vvp_page */
470 PageWriteback(vmpage) ? "wb" : "-",
472 PFID(ll_inode2fid(vmpage->mapping->host)),
473 vmpage->mapping->host, vmpage->index,
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 ? "" : "-");
485 static int vvp_pgcache_show(struct seq_file *f, void *v)
487 struct vvp_seq_private *priv = f->private;
488 struct page *vmpage = v;
489 struct cl_page *page;
491 seq_printf(f, "%8lx@" DFID ": ", vmpage->index,
492 PFID(lu_object_fid(&priv->vsp_clob->co_lu)));
494 page = cl_vmpage_page(vmpage, priv->vsp_clob);
499 vvp_pgcache_page_show(priv->vsp_env, f, page);
500 cl_page_put(priv->vsp_env, page);
502 seq_puts(f, "missing\n");
508 static void vvp_pgcache_rewind(struct vvp_seq_private *priv)
510 if (priv->vvp_prev_pos) {
511 struct lu_site *s = priv->vsp_sbi->ll_cl->cd_lu_dev.ld_site;
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",
519 cl_object_put(priv->vsp_env, priv->vsp_clob);
521 priv->vsp_clob = NULL;
525 static struct page *vvp_pgcache_next_page(struct vvp_seq_private *priv)
527 priv->vsp_page_index += 1;
528 return vvp_pgcache_current(priv);
531 static void *vvp_pgcache_start(struct seq_file *f, loff_t *pos)
533 struct vvp_seq_private *priv = f->private;
536 vvp_pgcache_rewind(priv);
537 } else if (*pos == priv->vvp_prev_pos) {
538 /* Return the current item */;
540 WARN_ON(*pos != priv->vvp_prev_pos + 1);
541 priv->vsp_page_index += 1;
544 priv->vvp_prev_pos = *pos;
545 return vvp_pgcache_current(priv);
548 static void *vvp_pgcache_next(struct seq_file *f, void *v, loff_t *pos)
550 struct vvp_seq_private *priv = f->private;
552 WARN_ON(*pos != priv->vvp_prev_pos);
554 priv->vvp_prev_pos = *pos;
555 return vvp_pgcache_next_page(priv);
558 static void vvp_pgcache_stop(struct seq_file *f, void *v)
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
570 static int vvp_dump_pgcache_seq_open(struct inode *inode, struct file *filp)
572 struct vvp_seq_private *priv;
575 priv = __seq_open_private(filp, &vvp_pgcache_ops, sizeof(*priv));
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);
585 seq_release_private(inode, filp);
589 s = priv->vsp_sbi->ll_cl->cd_lu_dev.ld_site;
590 rhashtable_walk_enter(&s->ls_obj_hash, &priv->vsp_iter);
595 static int vvp_dump_pgcache_seq_release(struct inode *inode, struct file *file)
597 struct seq_file *seq = file->private_data;
598 struct vvp_seq_private *priv = seq->private;
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);
604 cl_env_put(priv->vsp_env, &priv->vsp_refcheck);
605 rhashtable_walk_exit(&priv->vsp_iter);
606 return seq_release_private(inode, file);
609 const struct file_operations vvp_dump_pgcache_file_ops = {
610 .owner = THIS_MODULE,
611 .open = vvp_dump_pgcache_seq_open,
614 .release = vvp_dump_pgcache_seq_release,