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_pgcache_id {
383 struct lu_object_header *vpi_obj;
386 struct vvp_seq_private {
387 struct ll_sb_info *vsp_sbi;
388 struct lu_env *vsp_env;
390 struct cl_object *vsp_clob;
391 struct vvp_pgcache_id vvp_id;
393 * prev_pos is the 'pos' of the last object returned
394 * by ->start of ->next.
399 static int vvp_pgcache_obj_get(struct cfs_hash *hs, struct cfs_hash_bd *bd,
400 struct hlist_node *hnode, void *data)
402 struct vvp_pgcache_id *id = data;
403 struct lu_object_header *hdr = cfs_hash_object(hs, hnode);
405 if (lu_object_is_dying(hdr))
408 if (id->vpi_curdep-- > 0)
409 return 0; /* continue */
411 cfs_hash_get(hs, hnode);
416 static struct cl_object *vvp_pgcache_obj(const struct lu_env *env,
417 struct lu_device *dev,
418 struct vvp_pgcache_id *id)
420 LASSERT(lu_device_is_cl(dev));
423 id->vpi_curdep = id->vpi_depth;
425 cfs_hash_hlist_for_each(dev->ld_site->ls_obj_hash, id->vpi_bucket,
426 vvp_pgcache_obj_get, id);
427 if (id->vpi_obj != NULL) {
428 struct lu_object *lu_obj;
430 lu_obj = lu_object_locate(id->vpi_obj, dev->ld_type);
431 if (lu_obj != NULL) {
432 lu_object_ref_add(lu_obj, "dump", current);
433 return lu2cl(lu_obj);
435 lu_object_put(env, lu_object_top(id->vpi_obj));
440 static struct page *vvp_pgcache_current(struct vvp_seq_private *priv)
442 struct lu_device *dev = &priv->vsp_sbi->ll_cl->cd_lu_dev;
449 if (!priv->vsp_clob) {
450 struct cl_object *clob;
452 while ((clob = vvp_pgcache_obj(priv->vsp_env, dev, &priv->vvp_id)) == NULL &&
453 ++(priv->vvp_id.vpi_bucket) < CFS_HASH_NHLIST(dev->ld_site->ls_obj_hash))
454 priv->vvp_id.vpi_depth = 0;
457 priv->vsp_clob = clob;
458 priv->vvp_id.vpi_index = 0;
461 inode = vvp_object_inode(priv->vsp_clob);
462 nr = find_get_pages_contig(inode->i_mapping, priv->vvp_id.vpi_index, 1, &vmpage);
464 priv->vvp_id.vpi_index = vmpage->index;
467 lu_object_ref_del(&priv->vsp_clob->co_lu, "dump", current);
468 cl_object_put(priv->vsp_env, priv->vsp_clob);
469 priv->vsp_clob = NULL;
470 priv->vvp_id.vpi_index = 0;
471 priv->vvp_id.vpi_depth++;
475 #define seq_page_flag(seq, page, flag, has_flags) do { \
476 if (test_bit(PG_##flag, &(page)->flags)) { \
477 seq_printf(seq, "%s"#flag, has_flags ? "|" : ""); \
482 static void vvp_pgcache_page_show(const struct lu_env *env,
483 struct seq_file *seq, struct cl_page *page)
485 struct vvp_page *vpg;
489 vpg = cl2vvp_page(cl_page_at(page, &vvp_device_type));
490 vmpage = vpg->vpg_page;
491 seq_printf(seq, " %5i | %p %p %s %s %s | %p "DFID"(%p) %lu %u [",
495 vpg->vpg_defer_uptodate ? "du" : "- ",
496 PageWriteback(vmpage) ? "wb" : "-",
498 PFID(ll_inode2fid(vmpage->mapping->host)),
499 vmpage->mapping->host, vmpage->index,
502 seq_page_flag(seq, vmpage, locked, has_flags);
503 seq_page_flag(seq, vmpage, error, has_flags);
504 seq_page_flag(seq, vmpage, referenced, has_flags);
505 seq_page_flag(seq, vmpage, uptodate, has_flags);
506 seq_page_flag(seq, vmpage, dirty, has_flags);
507 seq_page_flag(seq, vmpage, writeback, has_flags);
508 seq_printf(seq, "%s]\n", has_flags ? "" : "-");
511 static int vvp_pgcache_show(struct seq_file *f, void *v)
513 struct vvp_seq_private *priv = f->private;
514 struct page *vmpage = v;
515 struct cl_page *page;
517 seq_printf(f, "%8lx@" DFID ": ", vmpage->index,
518 PFID(lu_object_fid(&priv->vsp_clob->co_lu)));
520 page = cl_vmpage_page(vmpage, priv->vsp_clob);
525 vvp_pgcache_page_show(priv->vsp_env, f, page);
526 cl_page_put(priv->vsp_env, page);
528 seq_puts(f, "missing\n");
534 static void vvp_pgcache_rewind(struct vvp_seq_private *priv)
536 if (priv->vvp_prev_pos) {
537 memset(&priv->vvp_id, 0, sizeof(priv->vvp_id));
538 priv->vvp_prev_pos = 0;
539 if (priv->vsp_clob) {
540 lu_object_ref_del(&priv->vsp_clob->co_lu, "dump",
542 cl_object_put(priv->vsp_env, priv->vsp_clob);
544 priv->vsp_clob = NULL;
548 static struct page *vvp_pgcache_next_page(struct vvp_seq_private *priv)
550 priv->vvp_id.vpi_index += 1;
551 return vvp_pgcache_current(priv);
554 static void *vvp_pgcache_start(struct seq_file *f, loff_t *pos)
556 struct vvp_seq_private *priv = f->private;
559 vvp_pgcache_rewind(priv);
560 } else if (*pos == priv->vvp_prev_pos) {
561 /* Return the current item */;
563 WARN_ON(*pos != priv->vvp_prev_pos + 1);
564 priv->vvp_id.vpi_index += 1;
567 priv->vvp_prev_pos = *pos;
568 return vvp_pgcache_current(priv);
571 static void *vvp_pgcache_next(struct seq_file *f, void *v, loff_t *pos)
573 struct vvp_seq_private *priv = f->private;
575 WARN_ON(*pos != priv->vvp_prev_pos);
577 priv->vvp_prev_pos = *pos;
578 return vvp_pgcache_next_page(priv);
581 static void vvp_pgcache_stop(struct seq_file *f, void *v)
586 static struct seq_operations vvp_pgcache_ops = {
587 .start = vvp_pgcache_start,
588 .next = vvp_pgcache_next,
589 .stop = vvp_pgcache_stop,
590 .show = vvp_pgcache_show
593 static int vvp_dump_pgcache_seq_open(struct inode *inode, struct file *filp)
595 struct vvp_seq_private *priv;
597 priv = __seq_open_private(filp, &vvp_pgcache_ops, sizeof(*priv));
601 priv->vsp_sbi = inode->i_private;
602 priv->vsp_env = cl_env_get(&priv->vsp_refcheck);
603 priv->vsp_clob = NULL;
604 memset(&priv->vvp_id, 0, sizeof(priv->vvp_id));
605 if (IS_ERR(priv->vsp_env)) {
606 int err = PTR_ERR(priv->vsp_env);
608 seq_release_private(inode, filp);
615 static int vvp_dump_pgcache_seq_release(struct inode *inode, struct file *file)
617 struct seq_file *seq = file->private_data;
618 struct vvp_seq_private *priv = seq->private;
620 if (priv->vsp_clob) {
621 lu_object_ref_del(&priv->vsp_clob->co_lu, "dump", current);
622 cl_object_put(priv->vsp_env, priv->vsp_clob);
625 cl_env_put(priv->vsp_env, &priv->vsp_refcheck);
626 return seq_release_private(inode, file);
629 const struct file_operations vvp_dump_pgcache_file_ops = {
630 .owner = THIS_MODULE,
631 .open = vvp_dump_pgcache_seq_open,
634 .release = vvp_dump_pgcache_seq_release,