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"
44 /*****************************************************************************
46 * Vvp device and device type functions.
51 * vvp_ prefix stands for "Vfs Vm Posix". It corresponds to historical
52 * "llite_" (var. "ll_") prefix.
55 static struct kmem_cache *ll_thread_kmem;
56 struct kmem_cache *vvp_object_kmem;
57 static struct kmem_cache *vvp_session_kmem;
58 static struct kmem_cache *vvp_thread_kmem;
60 static struct lu_kmem_descr vvp_caches[] = {
62 .ckd_cache = &ll_thread_kmem,
63 .ckd_name = "ll_thread_kmem",
64 .ckd_size = sizeof(struct ll_thread_info),
67 .ckd_cache = &vvp_object_kmem,
68 .ckd_name = "vvp_object_kmem",
69 .ckd_size = sizeof(struct vvp_object),
72 .ckd_cache = &vvp_session_kmem,
73 .ckd_name = "vvp_session_kmem",
74 .ckd_size = sizeof (struct vvp_session)
77 .ckd_cache = &vvp_thread_kmem,
78 .ckd_name = "vvp_thread_kmem",
79 .ckd_size = sizeof(struct vvp_thread_info),
86 static void *ll_thread_key_init(const struct lu_context *ctx,
87 struct lu_context_key *key)
89 struct ll_thread_info *lti;
91 OBD_SLAB_ALLOC_PTR_GFP(lti, ll_thread_kmem, GFP_NOFS);
93 lti = ERR_PTR(-ENOMEM);
98 static void ll_thread_key_fini(const struct lu_context *ctx,
99 struct lu_context_key *key, void *data)
101 struct ll_thread_info *lti = data;
103 OBD_SLAB_FREE_PTR(lti, ll_thread_kmem);
106 struct lu_context_key ll_thread_key = {
107 .lct_tags = LCT_CL_THREAD,
108 .lct_init = ll_thread_key_init,
109 .lct_fini = ll_thread_key_fini,
112 static void *vvp_session_key_init(const struct lu_context *ctx,
113 struct lu_context_key *key)
115 struct vvp_session *session;
117 OBD_SLAB_ALLOC_PTR_GFP(session, vvp_session_kmem, GFP_NOFS);
119 session = ERR_PTR(-ENOMEM);
123 static void vvp_session_key_fini(const struct lu_context *ctx,
124 struct lu_context_key *key, void *data)
126 struct vvp_session *session = data;
127 OBD_SLAB_FREE_PTR(session, vvp_session_kmem);
130 struct lu_context_key vvp_session_key = {
131 .lct_tags = LCT_SESSION,
132 .lct_init = vvp_session_key_init,
133 .lct_fini = vvp_session_key_fini
136 static void *vvp_thread_key_init(const struct lu_context *ctx,
137 struct lu_context_key *key)
139 struct vvp_thread_info *vti;
141 OBD_SLAB_ALLOC_PTR_GFP(vti, vvp_thread_kmem, GFP_NOFS);
143 vti = ERR_PTR(-ENOMEM);
147 static void vvp_thread_key_fini(const struct lu_context *ctx,
148 struct lu_context_key *key, void *data)
150 struct vvp_thread_info *vti = data;
151 OBD_SLAB_FREE_PTR(vti, vvp_thread_kmem);
154 struct lu_context_key vvp_thread_key = {
155 .lct_tags = LCT_CL_THREAD,
156 .lct_init = vvp_thread_key_init,
157 .lct_fini = vvp_thread_key_fini,
160 /* type constructor/destructor: vvp_type_{init,fini,start,stop}(). */
161 LU_TYPE_INIT_FINI(vvp, &ll_thread_key, &vvp_session_key, &vvp_thread_key);
163 static const struct lu_device_operations vvp_lu_ops = {
164 .ldo_object_alloc = vvp_object_alloc
167 static struct lu_device *vvp_device_free(const struct lu_env *env,
170 struct vvp_device *vdv = lu2vvp_dev(d);
171 struct cl_site *site = lu2cl_site(d->ld_site);
172 struct lu_device *next = cl2lu_dev(vdv->vdv_next);
174 if (d->ld_site != NULL) {
179 cl_device_fini(lu2cl_dev(d));
184 static struct lu_device *vvp_device_alloc(const struct lu_env *env,
185 struct lu_device_type *t,
186 struct lustre_cfg *cfg)
188 struct vvp_device *vdv;
189 struct lu_device *lud;
190 struct cl_site *site;
196 RETURN(ERR_PTR(-ENOMEM));
198 lud = &vdv->vdv_cl.cd_lu_dev;
199 cl_device_init(&vdv->vdv_cl, t);
200 vvp2lu_dev(vdv)->ld_ops = &vvp_lu_ops;
204 rc = cl_site_init(site, &vdv->vdv_cl);
206 rc = lu_site_init_finish(&site->cs_lu);
208 LASSERT(lud->ld_site == NULL);
209 CERROR("Cannot init lu_site, rc %d.\n", rc);
215 vvp_device_free(env, lud);
221 static int vvp_device_init(const struct lu_env *env, struct lu_device *d,
222 const char *name, struct lu_device *next)
224 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
269 * A mutex serializing calls to vvp_inode_fini() under extreme memory
270 * pressure, when environments cannot be allocated.
272 int vvp_global_init(void)
276 rc = lu_kmem_init(vvp_caches);
280 rc = lu_device_type_init(&vvp_device_type);
287 lu_kmem_fini(vvp_caches);
292 void vvp_global_fini(void)
294 lu_device_type_fini(&vvp_device_type);
295 lu_kmem_fini(vvp_caches);
298 /*****************************************************************************
300 * mirror obd-devices into cl devices.
304 int cl_sb_init(struct super_block *sb)
306 struct ll_sb_info *sbi;
307 struct cl_device *cl;
313 env = cl_env_get(&refcheck);
315 cl = cl_type_setup(env, NULL, &vvp_device_type,
316 sbi->ll_dt_exp->exp_obd->obd_lu_dev);
319 sbi->ll_site = cl2lu_dev(cl)->ld_site;
321 cl_env_put(env, &refcheck);
327 int cl_sb_fini(struct super_block *sb)
329 struct ll_sb_info *sbi;
331 struct cl_device *cld;
337 env = cl_env_get(&refcheck);
342 cl_stack_fini(env, cld);
346 cl_env_put(env, &refcheck);
349 CERROR("Cannot cleanup cl-stack due to memory shortage.\n");
350 result = PTR_ERR(env);
356 /****************************************************************************
358 * debugfs/lustre/llite/$MNT/dump_page_cache
360 ****************************************************************************/
362 struct vvp_pgcache_id {
368 struct lu_object_header *vpi_obj;
371 struct vvp_seq_private {
372 struct ll_sb_info *vsp_sbi;
373 struct lu_env *vsp_env;
375 struct cl_object *vsp_clob;
376 struct vvp_pgcache_id vvp_id;
378 * prev_pos is the 'pos' of the last object returned
379 * by ->start of ->next.
384 static int vvp_pgcache_obj_get(struct cfs_hash *hs, struct cfs_hash_bd *bd,
385 struct hlist_node *hnode, void *data)
387 struct vvp_pgcache_id *id = data;
388 struct lu_object_header *hdr = cfs_hash_object(hs, hnode);
390 if (lu_object_is_dying(hdr))
393 if (id->vpi_curdep-- > 0)
394 return 0; /* continue */
396 cfs_hash_get(hs, hnode);
401 static struct cl_object *vvp_pgcache_obj(const struct lu_env *env,
402 struct lu_device *dev,
403 struct vvp_pgcache_id *id)
405 LASSERT(lu_device_is_cl(dev));
408 id->vpi_curdep = id->vpi_depth;
410 cfs_hash_hlist_for_each(dev->ld_site->ls_obj_hash, id->vpi_bucket,
411 vvp_pgcache_obj_get, id);
412 if (id->vpi_obj != NULL) {
413 struct lu_object *lu_obj;
415 lu_obj = lu_object_locate(id->vpi_obj, dev->ld_type);
416 if (lu_obj != NULL) {
417 lu_object_ref_add(lu_obj, "dump", current);
418 return lu2cl(lu_obj);
420 lu_object_put(env, lu_object_top(id->vpi_obj));
425 static struct page *vvp_pgcache_current(struct vvp_seq_private *priv)
427 struct lu_device *dev = &priv->vsp_sbi->ll_cl->cd_lu_dev;
434 if (!priv->vsp_clob) {
435 struct cl_object *clob;
437 while ((clob = vvp_pgcache_obj(priv->vsp_env, dev, &priv->vvp_id)) == NULL &&
438 ++(priv->vvp_id.vpi_bucket) < CFS_HASH_NHLIST(dev->ld_site->ls_obj_hash))
439 priv->vvp_id.vpi_depth = 0;
442 priv->vsp_clob = clob;
443 priv->vvp_id.vpi_index = 0;
446 inode = vvp_object_inode(priv->vsp_clob);
447 nr = find_get_pages_contig(inode->i_mapping, priv->vvp_id.vpi_index, 1, &vmpage);
449 priv->vvp_id.vpi_index = vmpage->index;
452 lu_object_ref_del(&priv->vsp_clob->co_lu, "dump", current);
453 cl_object_put(priv->vsp_env, priv->vsp_clob);
454 priv->vsp_clob = NULL;
455 priv->vvp_id.vpi_index = 0;
456 priv->vvp_id.vpi_depth++;
460 #define seq_page_flag(seq, page, flag, has_flags) do { \
461 if (test_bit(PG_##flag, &(page)->flags)) { \
462 seq_printf(seq, "%s"#flag, has_flags ? "|" : ""); \
467 static void vvp_pgcache_page_show(const struct lu_env *env,
468 struct seq_file *seq, struct cl_page *page)
470 struct vvp_page *vpg;
474 vpg = cl2vvp_page(cl_page_at(page, &vvp_device_type));
475 vmpage = vpg->vpg_page;
476 seq_printf(seq, " %5i | %p %p %s %s %s | %p "DFID"(%p) %lu %u [",
480 vpg->vpg_defer_uptodate ? "du" : "- ",
481 PageWriteback(vmpage) ? "wb" : "-",
483 PFID(ll_inode2fid(vmpage->mapping->host)),
484 vmpage->mapping->host, vmpage->index,
487 seq_page_flag(seq, vmpage, locked, has_flags);
488 seq_page_flag(seq, vmpage, error, has_flags);
489 seq_page_flag(seq, vmpage, referenced, has_flags);
490 seq_page_flag(seq, vmpage, uptodate, has_flags);
491 seq_page_flag(seq, vmpage, dirty, has_flags);
492 seq_page_flag(seq, vmpage, writeback, has_flags);
493 seq_printf(seq, "%s]\n", has_flags ? "" : "-");
496 static int vvp_pgcache_show(struct seq_file *f, void *v)
498 struct vvp_seq_private *priv = f->private;
499 struct page *vmpage = v;
500 struct cl_page *page;
502 seq_printf(f, "%8lx@" DFID ": ", vmpage->index,
503 PFID(lu_object_fid(&priv->vsp_clob->co_lu)));
505 page = cl_vmpage_page(vmpage, priv->vsp_clob);
510 vvp_pgcache_page_show(priv->vsp_env, f, page);
511 cl_page_put(priv->vsp_env, page);
513 seq_puts(f, "missing\n");
519 static void vvp_pgcache_rewind(struct vvp_seq_private *priv)
521 if (priv->vvp_prev_pos) {
522 memset(&priv->vvp_id, 0, sizeof(priv->vvp_id));
523 priv->vvp_prev_pos = 0;
524 if (priv->vsp_clob) {
525 lu_object_ref_del(&priv->vsp_clob->co_lu, "dump",
527 cl_object_put(priv->vsp_env, priv->vsp_clob);
529 priv->vsp_clob = NULL;
533 static struct page *vvp_pgcache_next_page(struct vvp_seq_private *priv)
535 priv->vvp_id.vpi_index += 1;
536 return vvp_pgcache_current(priv);
539 static void *vvp_pgcache_start(struct seq_file *f, loff_t *pos)
541 struct vvp_seq_private *priv = f->private;
544 vvp_pgcache_rewind(priv);
545 } else if (*pos == priv->vvp_prev_pos) {
546 /* Return the current item */;
548 WARN_ON(*pos != priv->vvp_prev_pos + 1);
549 priv->vvp_id.vpi_index += 1;
552 priv->vvp_prev_pos = *pos;
553 return vvp_pgcache_current(priv);
556 static void *vvp_pgcache_next(struct seq_file *f, void *v, loff_t *pos)
558 struct vvp_seq_private *priv = f->private;
560 WARN_ON(*pos != priv->vvp_prev_pos);
562 priv->vvp_prev_pos = *pos;
563 return vvp_pgcache_next_page(priv);
566 static void vvp_pgcache_stop(struct seq_file *f, void *v)
571 static struct seq_operations vvp_pgcache_ops = {
572 .start = vvp_pgcache_start,
573 .next = vvp_pgcache_next,
574 .stop = vvp_pgcache_stop,
575 .show = vvp_pgcache_show
578 static int vvp_dump_pgcache_seq_open(struct inode *inode, struct file *filp)
580 struct vvp_seq_private *priv;
582 priv = __seq_open_private(filp, &vvp_pgcache_ops, sizeof(*priv));
586 priv->vsp_sbi = inode->i_private;
587 priv->vsp_env = cl_env_get(&priv->vsp_refcheck);
588 priv->vsp_clob = NULL;
589 memset(&priv->vvp_id, 0, sizeof(priv->vvp_id));
590 if (IS_ERR(priv->vsp_env)) {
591 int err = PTR_ERR(priv->vsp_env);
593 seq_release_private(inode, filp);
600 static int vvp_dump_pgcache_seq_release(struct inode *inode, struct file *file)
602 struct seq_file *seq = file->private_data;
603 struct vvp_seq_private *priv = seq->private;
605 if (priv->vsp_clob) {
606 lu_object_ref_del(&priv->vsp_clob->co_lu, "dump", current);
607 cl_object_put(priv->vsp_env, priv->vsp_clob);
610 cl_env_put(priv->vsp_env, &priv->vsp_refcheck);
611 return seq_release_private(inode, file);
614 const struct file_operations vvp_dump_pgcache_file_ops = {
615 .owner = THIS_MODULE,
616 .open = vvp_dump_pgcache_seq_open,
619 .release = vvp_dump_pgcache_seq_release,