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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * cl_device and cl_device_type implementation for VVP layer.
34 * Author: Nikita Danilov <nikita.danilov@sun.com>
35 * Author: Jinshan Xiong <jinshan.xiong@intel.com>
38 #define DEBUG_SUBSYSTEM S_LLITE
41 #include "llite_internal.h"
42 #include "vvp_internal.h"
43 #include <linux/kallsyms.h>
45 /*****************************************************************************
47 * Vvp device and device type functions.
52 * vvp_ prefix stands for "Vfs Vm Posix". It corresponds to historical
53 * "llite_" (var. "ll_") prefix.
56 static struct kmem_cache *ll_thread_kmem;
57 struct kmem_cache *vvp_object_kmem;
58 static struct kmem_cache *vvp_session_kmem;
59 static struct kmem_cache *vvp_thread_kmem;
61 static struct lu_kmem_descr vvp_caches[] = {
63 .ckd_cache = &ll_thread_kmem,
64 .ckd_name = "ll_thread_kmem",
65 .ckd_size = sizeof(struct ll_thread_info),
68 .ckd_cache = &vvp_object_kmem,
69 .ckd_name = "vvp_object_kmem",
70 .ckd_size = sizeof(struct vvp_object),
73 .ckd_cache = &vvp_session_kmem,
74 .ckd_name = "vvp_session_kmem",
75 .ckd_size = sizeof (struct vvp_session)
78 .ckd_cache = &vvp_thread_kmem,
79 .ckd_name = "vvp_thread_kmem",
80 .ckd_size = sizeof(struct vvp_thread_info),
87 static void *ll_thread_key_init(const struct lu_context *ctx,
88 struct lu_context_key *key)
90 struct ll_thread_info *lti;
92 OBD_SLAB_ALLOC_PTR_GFP(lti, ll_thread_kmem, GFP_NOFS);
94 lti = ERR_PTR(-ENOMEM);
99 static void ll_thread_key_fini(const struct lu_context *ctx,
100 struct lu_context_key *key, void *data)
102 struct ll_thread_info *lti = data;
104 OBD_SLAB_FREE_PTR(lti, ll_thread_kmem);
107 struct lu_context_key ll_thread_key = {
108 .lct_tags = LCT_CL_THREAD,
109 .lct_init = ll_thread_key_init,
110 .lct_fini = ll_thread_key_fini,
113 static void *vvp_session_key_init(const struct lu_context *ctx,
114 struct lu_context_key *key)
116 struct vvp_session *session;
118 OBD_SLAB_ALLOC_PTR_GFP(session, vvp_session_kmem, GFP_NOFS);
120 session = ERR_PTR(-ENOMEM);
124 static void vvp_session_key_fini(const struct lu_context *ctx,
125 struct lu_context_key *key, void *data)
127 struct vvp_session *session = data;
128 OBD_SLAB_FREE_PTR(session, vvp_session_kmem);
131 struct lu_context_key vvp_session_key = {
132 .lct_tags = LCT_SESSION,
133 .lct_init = vvp_session_key_init,
134 .lct_fini = vvp_session_key_fini
137 static void *vvp_thread_key_init(const struct lu_context *ctx,
138 struct lu_context_key *key)
140 struct vvp_thread_info *vti;
142 OBD_SLAB_ALLOC_PTR_GFP(vti, vvp_thread_kmem, GFP_NOFS);
144 vti = ERR_PTR(-ENOMEM);
148 static void vvp_thread_key_fini(const struct lu_context *ctx,
149 struct lu_context_key *key, void *data)
151 struct vvp_thread_info *vti = data;
152 OBD_SLAB_FREE_PTR(vti, vvp_thread_kmem);
155 struct lu_context_key vvp_thread_key = {
156 .lct_tags = LCT_CL_THREAD,
157 .lct_init = vvp_thread_key_init,
158 .lct_fini = vvp_thread_key_fini,
161 /* type constructor/destructor: vvp_type_{init,fini,start,stop}(). */
162 LU_TYPE_INIT_FINI(vvp, &ll_thread_key, &vvp_session_key, &vvp_thread_key);
164 static const struct lu_device_operations vvp_lu_ops = {
165 .ldo_object_alloc = vvp_object_alloc
168 static struct lu_device *vvp_device_free(const struct lu_env *env,
171 struct vvp_device *vdv = lu2vvp_dev(d);
172 struct cl_site *site = lu2cl_site(d->ld_site);
173 struct lu_device *next = cl2lu_dev(vdv->vdv_next);
175 if (d->ld_site != NULL) {
180 cl_device_fini(lu2cl_dev(d));
185 static struct lu_device *vvp_device_alloc(const struct lu_env *env,
186 struct lu_device_type *t,
187 struct lustre_cfg *cfg)
189 struct vvp_device *vdv;
190 struct lu_device *lud;
191 struct cl_site *site;
197 RETURN(ERR_PTR(-ENOMEM));
199 lud = &vdv->vdv_cl.cd_lu_dev;
200 cl_device_init(&vdv->vdv_cl, t);
201 vvp2lu_dev(vdv)->ld_ops = &vvp_lu_ops;
205 rc = cl_site_init(site, &vdv->vdv_cl);
207 rc = lu_site_init_finish(&site->cs_lu);
209 LASSERT(lud->ld_site == NULL);
210 CERROR("Cannot init lu_site, rc %d.\n", rc);
216 vvp_device_free(env, lud);
222 static int vvp_device_init(const struct lu_env *env, struct lu_device *d,
223 const char *name, struct lu_device *next)
225 struct vvp_device *vdv;
230 vdv->vdv_next = lu2cl_dev(next);
232 LASSERT(d->ld_site != NULL && next->ld_type != NULL);
233 next->ld_site = d->ld_site;
234 rc = next->ld_type->ldt_ops->ldto_device_init(
235 env, next, next->ld_type->ldt_name, NULL);
238 lu_ref_add(&next->ld_reference, "lu-stack", &lu_site_init);
243 static struct lu_device *vvp_device_fini(const struct lu_env *env,
246 return cl2lu_dev(lu2vvp_dev(d)->vdv_next);
249 static const struct lu_device_type_operations vvp_device_type_ops = {
250 .ldto_init = vvp_type_init,
251 .ldto_fini = vvp_type_fini,
253 .ldto_start = vvp_type_start,
254 .ldto_stop = vvp_type_stop,
256 .ldto_device_alloc = vvp_device_alloc,
257 .ldto_device_free = vvp_device_free,
258 .ldto_device_init = vvp_device_init,
259 .ldto_device_fini = vvp_device_fini,
262 struct lu_device_type vvp_device_type = {
263 .ldt_tags = LU_DEVICE_CL,
264 .ldt_name = LUSTRE_VVP_NAME,
265 .ldt_ops = &vvp_device_type_ops,
266 .ldt_ctx_tags = LCT_CL_THREAD
269 #ifndef HAVE_ACCOUNT_PAGE_DIRTIED_EXPORT
270 unsigned int (*vvp_account_page_dirtied)(struct page *page,
271 struct address_space *mapping);
275 * A mutex serializing calls to vvp_inode_fini() under extreme memory
276 * pressure, when environments cannot be allocated.
278 int vvp_global_init(void)
282 rc = lu_kmem_init(vvp_caches);
286 rc = lu_device_type_init(&vvp_device_type);
290 #ifndef HAVE_ACCOUNT_PAGE_DIRTIED_EXPORT
292 * Kernel v5.2-5678-gac1c3e4 no longer exports account_page_dirtied
294 vvp_account_page_dirtied = (void *)
295 kallsyms_lookup_name("account_page_dirtied");
296 BUG_ON(!vvp_account_page_dirtied);
302 lu_kmem_fini(vvp_caches);
307 void vvp_global_fini(void)
309 lu_device_type_fini(&vvp_device_type);
310 lu_kmem_fini(vvp_caches);
313 /*****************************************************************************
315 * mirror obd-devices into cl devices.
319 int cl_sb_init(struct super_block *sb)
321 struct ll_sb_info *sbi;
322 struct cl_device *cl;
328 env = cl_env_get(&refcheck);
330 cl = cl_type_setup(env, NULL, &vvp_device_type,
331 sbi->ll_dt_exp->exp_obd->obd_lu_dev);
334 sbi->ll_site = cl2lu_dev(cl)->ld_site;
336 cl_env_put(env, &refcheck);
342 int cl_sb_fini(struct super_block *sb)
344 struct ll_sb_info *sbi;
346 struct cl_device *cld;
352 env = cl_env_get(&refcheck);
357 cl_stack_fini(env, cld);
361 cl_env_put(env, &refcheck);
364 CERROR("Cannot cleanup cl-stack due to memory shortage.\n");
365 result = PTR_ERR(env);
371 /****************************************************************************
373 * debugfs/lustre/llite/$MNT/dump_page_cache
375 ****************************************************************************/
377 struct vvp_seq_private {
378 struct ll_sb_info *vsp_sbi;
379 struct lu_env *vsp_env;
381 struct cl_object *vsp_clob;
382 struct rhashtable_iter vsp_iter;
385 * prev_pos is the 'pos' of the last object returned
386 * by ->start of ->next.
391 static struct page *vvp_pgcache_current(struct vvp_seq_private *priv)
393 struct lu_device *dev = &priv->vsp_sbi->ll_cl->cd_lu_dev;
394 struct lu_object_header *h;
395 struct page *vmpage = NULL;
397 rhashtable_walk_start(&priv->vsp_iter);
398 while ((h = rhashtable_walk_next(&priv->vsp_iter)) != NULL) {
402 if (!priv->vsp_clob) {
403 struct lu_object *lu_obj;
405 lu_obj = lu_object_get_first(h, dev);
409 priv->vsp_clob = lu2cl(lu_obj);
410 lu_object_ref_add(lu_obj, "dump", current);
411 priv->vsp_page_index = 0;
414 inode = vvp_object_inode(priv->vsp_clob);
415 nr = find_get_pages_contig(inode->i_mapping,
416 priv->vsp_page_index, 1, &vmpage);
418 priv->vsp_page_index = vmpage->index;
421 lu_object_ref_del(&priv->vsp_clob->co_lu, "dump", current);
422 cl_object_put(priv->vsp_env, priv->vsp_clob);
423 priv->vsp_clob = NULL;
424 priv->vsp_page_index = 0;
426 rhashtable_walk_stop(&priv->vsp_iter);
430 #define seq_page_flag(seq, page, flag, has_flags) do { \
431 if (test_bit(PG_##flag, &(page)->flags)) { \
432 seq_printf(seq, "%s"#flag, has_flags ? "|" : ""); \
437 static void vvp_pgcache_page_show(const struct lu_env *env,
438 struct seq_file *seq, struct cl_page *page)
440 struct vvp_page *vpg;
444 vpg = cl2vvp_page(cl_page_at(page, &vvp_device_type));
445 vmpage = vpg->vpg_page;
446 seq_printf(seq, " %5i | %p %p %s %s %s | %p "DFID"(%p) %lu %u [",
450 vpg->vpg_defer_uptodate ? "du" : "- ",
451 PageWriteback(vmpage) ? "wb" : "-",
453 PFID(ll_inode2fid(vmpage->mapping->host)),
454 vmpage->mapping->host, vmpage->index,
457 seq_page_flag(seq, vmpage, locked, has_flags);
458 seq_page_flag(seq, vmpage, error, has_flags);
459 seq_page_flag(seq, vmpage, referenced, has_flags);
460 seq_page_flag(seq, vmpage, uptodate, has_flags);
461 seq_page_flag(seq, vmpage, dirty, has_flags);
462 seq_page_flag(seq, vmpage, writeback, has_flags);
463 seq_printf(seq, "%s]\n", has_flags ? "" : "-");
466 static int vvp_pgcache_show(struct seq_file *f, void *v)
468 struct vvp_seq_private *priv = f->private;
469 struct page *vmpage = v;
470 struct cl_page *page;
472 seq_printf(f, "%8lx@" DFID ": ", vmpage->index,
473 PFID(lu_object_fid(&priv->vsp_clob->co_lu)));
475 page = cl_vmpage_page(vmpage, priv->vsp_clob);
480 vvp_pgcache_page_show(priv->vsp_env, f, page);
481 cl_page_put(priv->vsp_env, page);
483 seq_puts(f, "missing\n");
489 static void vvp_pgcache_rewind(struct vvp_seq_private *priv)
491 if (priv->vvp_prev_pos) {
492 struct lu_site *s = priv->vsp_sbi->ll_cl->cd_lu_dev.ld_site;
494 rhashtable_walk_exit(&priv->vsp_iter);
495 rhashtable_walk_enter(&s->ls_obj_hash, &priv->vsp_iter);
496 priv->vvp_prev_pos = 0;
497 if (priv->vsp_clob) {
498 lu_object_ref_del(&priv->vsp_clob->co_lu, "dump",
500 cl_object_put(priv->vsp_env, priv->vsp_clob);
502 priv->vsp_clob = NULL;
506 static struct page *vvp_pgcache_next_page(struct vvp_seq_private *priv)
508 priv->vsp_page_index += 1;
509 return vvp_pgcache_current(priv);
512 static void *vvp_pgcache_start(struct seq_file *f, loff_t *pos)
514 struct vvp_seq_private *priv = f->private;
517 vvp_pgcache_rewind(priv);
518 } else if (*pos == priv->vvp_prev_pos) {
519 /* Return the current item */;
521 WARN_ON(*pos != priv->vvp_prev_pos + 1);
522 priv->vsp_page_index += 1;
525 priv->vvp_prev_pos = *pos;
526 return vvp_pgcache_current(priv);
529 static void *vvp_pgcache_next(struct seq_file *f, void *v, loff_t *pos)
531 struct vvp_seq_private *priv = f->private;
533 WARN_ON(*pos != priv->vvp_prev_pos);
535 priv->vvp_prev_pos = *pos;
536 return vvp_pgcache_next_page(priv);
539 static void vvp_pgcache_stop(struct seq_file *f, void *v)
544 static struct seq_operations vvp_pgcache_ops = {
545 .start = vvp_pgcache_start,
546 .next = vvp_pgcache_next,
547 .stop = vvp_pgcache_stop,
548 .show = vvp_pgcache_show
551 static int vvp_dump_pgcache_seq_open(struct inode *inode, struct file *filp)
553 struct vvp_seq_private *priv;
556 priv = __seq_open_private(filp, &vvp_pgcache_ops, sizeof(*priv));
560 priv->vsp_sbi = inode->i_private;
561 priv->vsp_env = cl_env_get(&priv->vsp_refcheck);
562 priv->vsp_clob = NULL;
563 if (IS_ERR(priv->vsp_env)) {
564 int err = PTR_ERR(priv->vsp_env);
566 seq_release_private(inode, filp);
570 s = priv->vsp_sbi->ll_cl->cd_lu_dev.ld_site;
571 rhashtable_walk_enter(&s->ls_obj_hash, &priv->vsp_iter);
576 static int vvp_dump_pgcache_seq_release(struct inode *inode, struct file *file)
578 struct seq_file *seq = file->private_data;
579 struct vvp_seq_private *priv = seq->private;
581 if (priv->vsp_clob) {
582 lu_object_ref_del(&priv->vsp_clob->co_lu, "dump", current);
583 cl_object_put(priv->vsp_env, priv->vsp_clob);
585 cl_env_put(priv->vsp_env, &priv->vsp_refcheck);
586 rhashtable_walk_exit(&priv->vsp_iter);
587 return seq_release_private(inode, file);
590 const struct file_operations vvp_dump_pgcache_file_ops = {
591 .owner = THIS_MODULE,
592 .open = vvp_dump_pgcache_seq_open,
595 .release = vvp_dump_pgcache_seq_release,