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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * cl_device and cl_device_type implementation for VVP layer.
38 * Author: Nikita Danilov <nikita.danilov@sun.com>
39 * Author: Jinshan Xiong <jinshan.xiong@intel.com>
42 #define DEBUG_SUBSYSTEM S_LLITE
45 #include "llite_internal.h"
46 #include "vvp_internal.h"
48 /*****************************************************************************
50 * Vvp device and device type functions.
55 * vvp_ prefix stands for "Vfs Vm Posix". It corresponds to historical
56 * "llite_" (var. "ll_") prefix.
59 static struct kmem_cache *ll_thread_kmem;
60 struct kmem_cache *vvp_lock_kmem;
61 struct kmem_cache *vvp_object_kmem;
62 static struct kmem_cache *vvp_session_kmem;
63 static struct kmem_cache *vvp_thread_kmem;
65 static struct lu_kmem_descr vvp_caches[] = {
67 .ckd_cache = &ll_thread_kmem,
68 .ckd_name = "ll_thread_kmem",
69 .ckd_size = sizeof(struct ll_thread_info),
72 .ckd_cache = &vvp_lock_kmem,
73 .ckd_name = "vvp_lock_kmem",
74 .ckd_size = sizeof(struct vvp_lock),
77 .ckd_cache = &vvp_object_kmem,
78 .ckd_name = "vvp_object_kmem",
79 .ckd_size = sizeof(struct vvp_object),
82 .ckd_cache = &vvp_session_kmem,
83 .ckd_name = "vvp_session_kmem",
84 .ckd_size = sizeof (struct vvp_session)
87 .ckd_cache = &vvp_thread_kmem,
88 .ckd_name = "vvp_thread_kmem",
89 .ckd_size = sizeof(struct vvp_thread_info),
96 static void *ll_thread_key_init(const struct lu_context *ctx,
97 struct lu_context_key *key)
99 struct ll_thread_info *lti;
101 OBD_SLAB_ALLOC_PTR_GFP(lti, ll_thread_kmem, GFP_NOFS);
103 lti = ERR_PTR(-ENOMEM);
108 static void ll_thread_key_fini(const struct lu_context *ctx,
109 struct lu_context_key *key, void *data)
111 struct ll_thread_info *lti = data;
113 OBD_SLAB_FREE_PTR(lti, ll_thread_kmem);
116 struct lu_context_key ll_thread_key = {
117 .lct_tags = LCT_CL_THREAD,
118 .lct_init = ll_thread_key_init,
119 .lct_fini = ll_thread_key_fini,
122 static void *vvp_session_key_init(const struct lu_context *ctx,
123 struct lu_context_key *key)
125 struct vvp_session *session;
127 OBD_SLAB_ALLOC_PTR_GFP(session, vvp_session_kmem, GFP_NOFS);
129 session = ERR_PTR(-ENOMEM);
133 static void vvp_session_key_fini(const struct lu_context *ctx,
134 struct lu_context_key *key, void *data)
136 struct vvp_session *session = data;
137 OBD_SLAB_FREE_PTR(session, vvp_session_kmem);
140 struct lu_context_key vvp_session_key = {
141 .lct_tags = LCT_SESSION,
142 .lct_init = vvp_session_key_init,
143 .lct_fini = vvp_session_key_fini
146 static void *vvp_thread_key_init(const struct lu_context *ctx,
147 struct lu_context_key *key)
149 struct vvp_thread_info *vti;
151 OBD_SLAB_ALLOC_PTR_GFP(vti, vvp_thread_kmem, GFP_NOFS);
153 vti = ERR_PTR(-ENOMEM);
157 static void vvp_thread_key_fini(const struct lu_context *ctx,
158 struct lu_context_key *key, void *data)
160 struct vvp_thread_info *vti = data;
161 OBD_SLAB_FREE_PTR(vti, vvp_thread_kmem);
164 struct lu_context_key vvp_thread_key = {
165 .lct_tags = LCT_CL_THREAD,
166 .lct_init = vvp_thread_key_init,
167 .lct_fini = vvp_thread_key_fini,
170 /* type constructor/destructor: vvp_type_{init,fini,start,stop}(). */
171 LU_TYPE_INIT_FINI(vvp, &ll_thread_key, &vvp_session_key, &vvp_thread_key);
173 static const struct lu_device_operations vvp_lu_ops = {
174 .ldo_object_alloc = vvp_object_alloc
177 static struct lu_device *vvp_device_free(const struct lu_env *env,
180 struct vvp_device *vdv = lu2vvp_dev(d);
181 struct cl_site *site = lu2cl_site(d->ld_site);
182 struct lu_device *next = cl2lu_dev(vdv->vdv_next);
184 if (d->ld_site != NULL) {
189 cl_device_fini(lu2cl_dev(d));
194 static struct lu_device *vvp_device_alloc(const struct lu_env *env,
195 struct lu_device_type *t,
196 struct lustre_cfg *cfg)
198 struct vvp_device *vdv;
199 struct lu_device *lud;
200 struct cl_site *site;
206 RETURN(ERR_PTR(-ENOMEM));
208 lud = &vdv->vdv_cl.cd_lu_dev;
209 cl_device_init(&vdv->vdv_cl, t);
210 vvp2lu_dev(vdv)->ld_ops = &vvp_lu_ops;
214 rc = cl_site_init(site, &vdv->vdv_cl);
216 rc = lu_site_init_finish(&site->cs_lu);
218 LASSERT(lud->ld_site == NULL);
219 CERROR("Cannot init lu_site, rc %d.\n", rc);
225 vvp_device_free(env, lud);
231 static int vvp_device_init(const struct lu_env *env, struct lu_device *d,
232 const char *name, struct lu_device *next)
234 struct vvp_device *vdv;
239 vdv->vdv_next = lu2cl_dev(next);
241 LASSERT(d->ld_site != NULL && next->ld_type != NULL);
242 next->ld_site = d->ld_site;
243 rc = next->ld_type->ldt_ops->ldto_device_init(
244 env, next, next->ld_type->ldt_name, NULL);
247 lu_ref_add(&next->ld_reference, "lu-stack", &lu_site_init);
252 static struct lu_device *vvp_device_fini(const struct lu_env *env,
255 return cl2lu_dev(lu2vvp_dev(d)->vdv_next);
258 static const struct lu_device_type_operations vvp_device_type_ops = {
259 .ldto_init = vvp_type_init,
260 .ldto_fini = vvp_type_fini,
262 .ldto_start = vvp_type_start,
263 .ldto_stop = vvp_type_stop,
265 .ldto_device_alloc = vvp_device_alloc,
266 .ldto_device_free = vvp_device_free,
267 .ldto_device_init = vvp_device_init,
268 .ldto_device_fini = vvp_device_fini,
271 struct lu_device_type vvp_device_type = {
272 .ldt_tags = LU_DEVICE_CL,
273 .ldt_name = LUSTRE_VVP_NAME,
274 .ldt_ops = &vvp_device_type_ops,
275 .ldt_ctx_tags = LCT_CL_THREAD
279 * A mutex serializing calls to vvp_inode_fini() under extreme memory
280 * pressure, when environments cannot be allocated.
282 int vvp_global_init(void)
286 rc = lu_kmem_init(vvp_caches);
290 rc = lu_device_type_init(&vvp_device_type);
297 lu_kmem_fini(vvp_caches);
302 void vvp_global_fini(void)
304 lu_device_type_fini(&vvp_device_type);
305 lu_kmem_fini(vvp_caches);
308 /*****************************************************************************
310 * mirror obd-devices into cl devices.
314 int cl_sb_init(struct super_block *sb)
316 struct ll_sb_info *sbi;
317 struct cl_device *cl;
323 env = cl_env_get(&refcheck);
325 cl = cl_type_setup(env, NULL, &vvp_device_type,
326 sbi->ll_dt_exp->exp_obd->obd_lu_dev);
329 sbi->ll_site = cl2lu_dev(cl)->ld_site;
331 cl_env_put(env, &refcheck);
337 int cl_sb_fini(struct super_block *sb)
339 struct ll_sb_info *sbi;
341 struct cl_device *cld;
347 env = cl_env_get(&refcheck);
352 cl_stack_fini(env, cld);
356 cl_env_put(env, &refcheck);
359 CERROR("Cannot cleanup cl-stack due to memory shortage.\n");
360 result = PTR_ERR(env);
366 /****************************************************************************
368 * /proc/fs/lustre/llite/$MNT/dump_page_cache
370 ****************************************************************************/
373 * To represent contents of a page cache as a byte stream, following
374 * information if encoded in 64bit offset:
376 * - file hash bucket in lu_site::ls_hash[] 28bits
378 * - how far file is from bucket head 4bits
380 * - page index 32bits
382 * First two data identify a file in the cache uniquely.
385 #define PGC_OBJ_SHIFT (32 + 4)
386 #define PGC_DEPTH_SHIFT (32)
388 struct vvp_pgcache_id {
394 struct lu_object_header *vpi_obj;
397 static void vvp_pgcache_id_unpack(loff_t pos, struct vvp_pgcache_id *id)
399 CLASSERT(sizeof(pos) == sizeof(__u64));
401 id->vpi_index = pos & 0xffffffff;
402 id->vpi_depth = (pos >> PGC_DEPTH_SHIFT) & 0xf;
403 id->vpi_bucket = ((unsigned long long)pos >> PGC_OBJ_SHIFT);
406 static loff_t vvp_pgcache_id_pack(struct vvp_pgcache_id *id)
409 ((__u64)id->vpi_index) |
410 ((__u64)id->vpi_depth << PGC_DEPTH_SHIFT) |
411 ((__u64)id->vpi_bucket << PGC_OBJ_SHIFT);
414 static int vvp_pgcache_obj_get(struct cfs_hash *hs, struct cfs_hash_bd *bd,
415 struct hlist_node *hnode, void *data)
417 struct vvp_pgcache_id *id = data;
418 struct lu_object_header *hdr = cfs_hash_object(hs, hnode);
420 if (id->vpi_curdep-- > 0)
421 return 0; /* continue */
423 if (lu_object_is_dying(hdr))
426 cfs_hash_get(hs, hnode);
431 static struct cl_object *vvp_pgcache_obj(const struct lu_env *env,
432 struct lu_device *dev,
433 struct vvp_pgcache_id *id)
435 LASSERT(lu_device_is_cl(dev));
437 id->vpi_depth &= 0xf;
439 id->vpi_curdep = id->vpi_depth;
441 cfs_hash_hlist_for_each(dev->ld_site->ls_obj_hash, id->vpi_bucket,
442 vvp_pgcache_obj_get, id);
443 if (id->vpi_obj != NULL) {
444 struct lu_object *lu_obj;
446 lu_obj = lu_object_locate(id->vpi_obj, dev->ld_type);
447 if (lu_obj != NULL) {
448 lu_object_ref_add(lu_obj, "dump", current);
449 return lu2cl(lu_obj);
451 lu_object_put(env, lu_object_top(id->vpi_obj));
453 } else if (id->vpi_curdep > 0) {
459 static loff_t vvp_pgcache_find(const struct lu_env *env,
460 struct lu_device *dev, loff_t pos)
462 struct cl_object *clob;
463 struct lu_site *site;
464 struct vvp_pgcache_id id;
467 vvp_pgcache_id_unpack(pos, &id);
470 if (id.vpi_bucket >= CFS_HASH_NHLIST(site->ls_obj_hash))
472 clob = vvp_pgcache_obj(env, dev, &id);
474 struct inode *inode = vvp_object_inode(clob);
478 nr = find_get_pages_contig(inode->i_mapping,
479 id.vpi_index, 1, &vmpage);
481 id.vpi_index = vmpage->index;
482 /* Cant support over 16T file */
483 nr = !(vmpage->index > 0xffffffff);
484 page_cache_release(vmpage);
487 lu_object_ref_del(&clob->co_lu, "dump", current);
488 cl_object_put(env, clob);
490 return vvp_pgcache_id_pack(&id);
492 /* to the next object. */
495 if (id.vpi_depth == 0 && ++id.vpi_bucket == 0)
501 #define seq_page_flag(seq, page, flag, has_flags) do { \
502 if (test_bit(PG_##flag, &(page)->flags)) { \
503 seq_printf(seq, "%s"#flag, has_flags ? "|" : ""); \
508 static void vvp_pgcache_page_show(const struct lu_env *env,
509 struct seq_file *seq, struct cl_page *page)
511 struct vvp_page *vpg;
515 vpg = cl2vvp_page(cl_page_at(page, &vvp_device_type));
516 vmpage = vpg->vpg_page;
517 seq_printf(seq, " %5i | %p %p %s %s %s | %p "DFID"(%p) %lu %u [",
521 vpg->vpg_defer_uptodate ? "du" : "- ",
522 PageWriteback(vmpage) ? "wb" : "-",
524 PFID(ll_inode2fid(vmpage->mapping->host)),
525 vmpage->mapping->host, vmpage->index,
528 seq_page_flag(seq, vmpage, locked, has_flags);
529 seq_page_flag(seq, vmpage, error, has_flags);
530 seq_page_flag(seq, vmpage, referenced, has_flags);
531 seq_page_flag(seq, vmpage, uptodate, has_flags);
532 seq_page_flag(seq, vmpage, dirty, has_flags);
533 seq_page_flag(seq, vmpage, writeback, has_flags);
534 seq_printf(seq, "%s]\n", has_flags ? "" : "-");
537 static int vvp_pgcache_show(struct seq_file *f, void *v)
540 struct ll_sb_info *sbi;
541 struct cl_object *clob;
543 struct vvp_pgcache_id id;
547 env = cl_env_get(&refcheck);
550 vvp_pgcache_id_unpack(pos, &id);
552 clob = vvp_pgcache_obj(env, &sbi->ll_cl->cd_lu_dev, &id);
554 struct inode *inode = vvp_object_inode(clob);
555 struct cl_page *page = NULL;
558 result = find_get_pages_contig(inode->i_mapping,
559 id.vpi_index, 1, &vmpage);
562 page = cl_vmpage_page(vmpage, clob);
565 page_cache_release(vmpage);
568 seq_printf(f, "%8x@"DFID": ", id.vpi_index,
569 PFID(lu_object_fid(&clob->co_lu)));
571 vvp_pgcache_page_show(env, f, page);
572 cl_page_put(env, page);
574 seq_puts(f, "missing\n");
575 lu_object_ref_del(&clob->co_lu, "dump", current);
576 cl_object_put(env, clob);
578 seq_printf(f, "%llx missing\n", pos);
579 cl_env_put(env, &refcheck);
582 result = PTR_ERR(env);
586 static void *vvp_pgcache_start(struct seq_file *f, loff_t *pos)
588 struct ll_sb_info *sbi;
594 env = cl_env_get(&refcheck);
597 if (sbi->ll_site->ls_obj_hash->hs_cur_bits > 64 - PGC_OBJ_SHIFT)
598 pos = ERR_PTR(-EFBIG);
600 *pos = vvp_pgcache_find(env, &sbi->ll_cl->cd_lu_dev,
605 cl_env_put(env, &refcheck);
610 static void *vvp_pgcache_next(struct seq_file *f, void *v, loff_t *pos)
612 struct ll_sb_info *sbi;
616 env = cl_env_get(&refcheck);
619 *pos = vvp_pgcache_find(env, &sbi->ll_cl->cd_lu_dev, *pos + 1);
622 cl_env_put(env, &refcheck);
627 static void vvp_pgcache_stop(struct seq_file *f, void *v)
632 static struct seq_operations vvp_pgcache_ops = {
633 .start = vvp_pgcache_start,
634 .next = vvp_pgcache_next,
635 .stop = vvp_pgcache_stop,
636 .show = vvp_pgcache_show
639 static int vvp_dump_pgcache_seq_open(struct inode *inode, struct file *filp)
641 struct ll_sb_info *sbi = PDE_DATA(inode);
642 struct seq_file *seq;
645 result = seq_open(filp, &vvp_pgcache_ops);
647 seq = filp->private_data;
653 const struct file_operations vvp_dump_pgcache_file_ops = {
654 .owner = THIS_MODULE,
655 .open = vvp_dump_pgcache_seq_open,
658 .release = seq_release,