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, 2014, 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 struct kmem_cache *vvp_req_kmem;
63 static struct kmem_cache *vvp_session_kmem;
64 static struct kmem_cache *vvp_thread_kmem;
66 static struct lu_kmem_descr vvp_caches[] = {
68 .ckd_cache = &ll_thread_kmem,
69 .ckd_name = "ll_thread_kmem",
70 .ckd_size = sizeof(struct ll_thread_info),
73 .ckd_cache = &vvp_lock_kmem,
74 .ckd_name = "vvp_lock_kmem",
75 .ckd_size = sizeof(struct vvp_lock),
78 .ckd_cache = &vvp_object_kmem,
79 .ckd_name = "vvp_object_kmem",
80 .ckd_size = sizeof(struct vvp_object),
83 .ckd_cache = &vvp_req_kmem,
84 .ckd_name = "vvp_req_kmem",
85 .ckd_size = sizeof(struct vvp_req),
88 .ckd_cache = &vvp_session_kmem,
89 .ckd_name = "vvp_session_kmem",
90 .ckd_size = sizeof (struct vvp_session)
93 .ckd_cache = &vvp_thread_kmem,
94 .ckd_name = "vvp_thread_kmem",
95 .ckd_size = sizeof(struct vvp_thread_info),
102 static void *ll_thread_key_init(const struct lu_context *ctx,
103 struct lu_context_key *key)
105 struct ll_thread_info *lti;
107 OBD_SLAB_ALLOC_PTR_GFP(lti, ll_thread_kmem, GFP_NOFS);
109 lti = ERR_PTR(-ENOMEM);
114 static void ll_thread_key_fini(const struct lu_context *ctx,
115 struct lu_context_key *key, void *data)
117 struct ll_thread_info *lti = data;
119 OBD_SLAB_FREE_PTR(lti, ll_thread_kmem);
122 struct lu_context_key ll_thread_key = {
123 .lct_tags = LCT_CL_THREAD,
124 .lct_init = ll_thread_key_init,
125 .lct_fini = ll_thread_key_fini,
128 static void *vvp_session_key_init(const struct lu_context *ctx,
129 struct lu_context_key *key)
131 struct vvp_session *session;
133 OBD_SLAB_ALLOC_PTR_GFP(session, vvp_session_kmem, GFP_NOFS);
135 session = ERR_PTR(-ENOMEM);
139 static void vvp_session_key_fini(const struct lu_context *ctx,
140 struct lu_context_key *key, void *data)
142 struct vvp_session *session = data;
143 OBD_SLAB_FREE_PTR(session, vvp_session_kmem);
146 struct lu_context_key vvp_session_key = {
147 .lct_tags = LCT_SESSION,
148 .lct_init = vvp_session_key_init,
149 .lct_fini = vvp_session_key_fini
152 static void *vvp_thread_key_init(const struct lu_context *ctx,
153 struct lu_context_key *key)
155 struct vvp_thread_info *vti;
157 OBD_SLAB_ALLOC_PTR_GFP(vti, vvp_thread_kmem, GFP_NOFS);
159 vti = ERR_PTR(-ENOMEM);
163 static void vvp_thread_key_fini(const struct lu_context *ctx,
164 struct lu_context_key *key, void *data)
166 struct vvp_thread_info *vti = data;
167 OBD_SLAB_FREE_PTR(vti, vvp_thread_kmem);
170 struct lu_context_key vvp_thread_key = {
171 .lct_tags = LCT_CL_THREAD,
172 .lct_init = vvp_thread_key_init,
173 .lct_fini = vvp_thread_key_fini,
176 /* type constructor/destructor: vvp_type_{init,fini,start,stop}(). */
177 LU_TYPE_INIT_FINI(vvp, &ll_thread_key, &vvp_session_key, &vvp_thread_key);
179 static const struct lu_device_operations vvp_lu_ops = {
180 .ldo_object_alloc = vvp_object_alloc
183 static const struct cl_device_operations vvp_cl_ops = {
184 .cdo_req_init = vvp_req_init,
187 static struct lu_device *vvp_device_free(const struct lu_env *env,
190 struct vvp_device *vdv = lu2vvp_dev(d);
191 struct cl_site *site = lu2cl_site(d->ld_site);
192 struct lu_device *next = cl2lu_dev(vdv->vdv_next);
194 if (d->ld_site != NULL) {
199 cl_device_fini(lu2cl_dev(d));
204 static struct lu_device *vvp_device_alloc(const struct lu_env *env,
205 struct lu_device_type *t,
206 struct lustre_cfg *cfg)
208 struct vvp_device *vdv;
209 struct lu_device *lud;
210 struct cl_site *site;
216 RETURN(ERR_PTR(-ENOMEM));
218 lud = &vdv->vdv_cl.cd_lu_dev;
219 cl_device_init(&vdv->vdv_cl, t);
220 vvp2lu_dev(vdv)->ld_ops = &vvp_lu_ops;
221 vdv->vdv_cl.cd_ops = &vvp_cl_ops;
225 rc = cl_site_init(site, &vdv->vdv_cl);
227 rc = lu_site_init_finish(&site->cs_lu);
229 LASSERT(lud->ld_site == NULL);
230 CERROR("Cannot init lu_site, rc %d.\n", rc);
236 vvp_device_free(env, lud);
242 static int vvp_device_init(const struct lu_env *env, struct lu_device *d,
243 const char *name, struct lu_device *next)
245 struct vvp_device *vdv;
250 vdv->vdv_next = lu2cl_dev(next);
252 LASSERT(d->ld_site != NULL && next->ld_type != NULL);
253 next->ld_site = d->ld_site;
254 rc = next->ld_type->ldt_ops->ldto_device_init(
255 env, next, next->ld_type->ldt_name, NULL);
258 lu_ref_add(&next->ld_reference, "lu-stack", &lu_site_init);
263 static struct lu_device *vvp_device_fini(const struct lu_env *env,
266 return cl2lu_dev(lu2vvp_dev(d)->vdv_next);
269 static const struct lu_device_type_operations vvp_device_type_ops = {
270 .ldto_init = vvp_type_init,
271 .ldto_fini = vvp_type_fini,
273 .ldto_start = vvp_type_start,
274 .ldto_stop = vvp_type_stop,
276 .ldto_device_alloc = vvp_device_alloc,
277 .ldto_device_free = vvp_device_free,
278 .ldto_device_init = vvp_device_init,
279 .ldto_device_fini = vvp_device_fini,
282 struct lu_device_type vvp_device_type = {
283 .ldt_tags = LU_DEVICE_CL,
284 .ldt_name = LUSTRE_VVP_NAME,
285 .ldt_ops = &vvp_device_type_ops,
286 .ldt_ctx_tags = LCT_CL_THREAD
290 * A mutex serializing calls to vvp_inode_fini() under extreme memory
291 * pressure, when environments cannot be allocated.
293 int vvp_global_init(void)
297 rc = lu_kmem_init(vvp_caches);
301 rc = lu_device_type_init(&vvp_device_type);
308 lu_kmem_fini(vvp_caches);
313 void vvp_global_fini(void)
315 lu_device_type_fini(&vvp_device_type);
316 lu_kmem_fini(vvp_caches);
319 /*****************************************************************************
321 * mirror obd-devices into cl devices.
325 int cl_sb_init(struct super_block *sb)
327 struct ll_sb_info *sbi;
328 struct cl_device *cl;
334 env = cl_env_get(&refcheck);
336 cl = cl_type_setup(env, NULL, &vvp_device_type,
337 sbi->ll_dt_exp->exp_obd->obd_lu_dev);
339 cl2vvp_dev(cl)->vdv_sb = sb;
341 sbi->ll_site = cl2lu_dev(cl)->ld_site;
343 cl_env_put(env, &refcheck);
349 int cl_sb_fini(struct super_block *sb)
351 struct ll_sb_info *sbi;
353 struct cl_device *cld;
359 env = cl_env_get(&refcheck);
364 cl_stack_fini(env, cld);
368 cl_env_put(env, &refcheck);
371 CERROR("Cannot cleanup cl-stack due to memory shortage.\n");
372 result = PTR_ERR(env);
378 /****************************************************************************
380 * /proc/fs/lustre/llite/$MNT/dump_page_cache
382 ****************************************************************************/
385 * To represent contents of a page cache as a byte stream, following
386 * information if encoded in 64bit offset:
388 * - file hash bucket in lu_site::ls_hash[] 28bits
390 * - how far file is from bucket head 4bits
392 * - page index 32bits
394 * First two data identify a file in the cache uniquely.
397 #define PGC_OBJ_SHIFT (32 + 4)
398 #define PGC_DEPTH_SHIFT (32)
400 struct vvp_pgcache_id {
406 struct lu_object_header *vpi_obj;
409 static void vvp_pgcache_id_unpack(loff_t pos, struct vvp_pgcache_id *id)
411 CLASSERT(sizeof(pos) == sizeof(__u64));
413 id->vpi_index = pos & 0xffffffff;
414 id->vpi_depth = (pos >> PGC_DEPTH_SHIFT) & 0xf;
415 id->vpi_bucket = ((unsigned long long)pos >> PGC_OBJ_SHIFT);
418 static loff_t vvp_pgcache_id_pack(struct vvp_pgcache_id *id)
421 ((__u64)id->vpi_index) |
422 ((__u64)id->vpi_depth << PGC_DEPTH_SHIFT) |
423 ((__u64)id->vpi_bucket << PGC_OBJ_SHIFT);
426 static int vvp_pgcache_obj_get(struct cfs_hash *hs, struct cfs_hash_bd *bd,
427 struct hlist_node *hnode, void *data)
429 struct vvp_pgcache_id *id = data;
430 struct lu_object_header *hdr = cfs_hash_object(hs, hnode);
432 if (id->vpi_curdep-- > 0)
433 return 0; /* continue */
435 if (lu_object_is_dying(hdr))
438 cfs_hash_get(hs, hnode);
443 static struct cl_object *vvp_pgcache_obj(const struct lu_env *env,
444 struct lu_device *dev,
445 struct vvp_pgcache_id *id)
447 LASSERT(lu_device_is_cl(dev));
449 id->vpi_depth &= 0xf;
451 id->vpi_curdep = id->vpi_depth;
453 cfs_hash_hlist_for_each(dev->ld_site->ls_obj_hash, id->vpi_bucket,
454 vvp_pgcache_obj_get, id);
455 if (id->vpi_obj != NULL) {
456 struct lu_object *lu_obj;
458 lu_obj = lu_object_locate(id->vpi_obj, dev->ld_type);
459 if (lu_obj != NULL) {
460 lu_object_ref_add(lu_obj, "dump", current);
461 return lu2cl(lu_obj);
463 lu_object_put(env, lu_object_top(id->vpi_obj));
465 } else if (id->vpi_curdep > 0) {
471 static loff_t vvp_pgcache_find(const struct lu_env *env,
472 struct lu_device *dev, loff_t pos)
474 struct cl_object *clob;
475 struct lu_site *site;
476 struct vvp_pgcache_id id;
479 vvp_pgcache_id_unpack(pos, &id);
482 if (id.vpi_bucket >= CFS_HASH_NHLIST(site->ls_obj_hash))
484 clob = vvp_pgcache_obj(env, dev, &id);
486 struct inode *inode = vvp_object_inode(clob);
490 nr = find_get_pages_contig(inode->i_mapping,
491 id.vpi_index, 1, &vmpage);
493 id.vpi_index = vmpage->index;
494 /* Cant support over 16T file */
495 nr = !(vmpage->index > 0xffffffff);
496 page_cache_release(vmpage);
499 lu_object_ref_del(&clob->co_lu, "dump", current);
500 cl_object_put(env, clob);
502 return vvp_pgcache_id_pack(&id);
504 /* to the next object. */
507 if (id.vpi_depth == 0 && ++id.vpi_bucket == 0)
513 #define seq_page_flag(seq, page, flag, has_flags) do { \
514 if (test_bit(PG_##flag, &(page)->flags)) { \
515 seq_printf(seq, "%s"#flag, has_flags ? "|" : ""); \
520 static void vvp_pgcache_page_show(const struct lu_env *env,
521 struct seq_file *seq, struct cl_page *page)
523 struct vvp_page *vpg;
527 vpg = cl2vvp_page(cl_page_at(page, &vvp_device_type));
528 vmpage = vpg->vpg_page;
529 seq_printf(seq, " %5i | %p %p %s %s %s | %p "DFID"(%p) %lu %u [",
533 vpg->vpg_defer_uptodate ? "du" : "- ",
534 PageWriteback(vmpage) ? "wb" : "-",
536 PFID(ll_inode2fid(vmpage->mapping->host)),
537 vmpage->mapping->host, vmpage->index,
540 seq_page_flag(seq, vmpage, locked, has_flags);
541 seq_page_flag(seq, vmpage, error, has_flags);
542 seq_page_flag(seq, vmpage, referenced, has_flags);
543 seq_page_flag(seq, vmpage, uptodate, has_flags);
544 seq_page_flag(seq, vmpage, dirty, has_flags);
545 seq_page_flag(seq, vmpage, writeback, has_flags);
546 seq_printf(seq, "%s]\n", has_flags ? "" : "-");
549 static int vvp_pgcache_show(struct seq_file *f, void *v)
552 struct ll_sb_info *sbi;
553 struct cl_object *clob;
555 struct vvp_pgcache_id id;
559 env = cl_env_get(&refcheck);
562 vvp_pgcache_id_unpack(pos, &id);
564 clob = vvp_pgcache_obj(env, &sbi->ll_cl->cd_lu_dev, &id);
566 struct inode *inode = vvp_object_inode(clob);
567 struct cl_page *page = NULL;
570 result = find_get_pages_contig(inode->i_mapping,
571 id.vpi_index, 1, &vmpage);
574 page = cl_vmpage_page(vmpage, clob);
577 page_cache_release(vmpage);
580 seq_printf(f, "%8x@"DFID": ", id.vpi_index,
581 PFID(lu_object_fid(&clob->co_lu)));
583 vvp_pgcache_page_show(env, f, page);
584 cl_page_put(env, page);
586 seq_puts(f, "missing\n");
587 lu_object_ref_del(&clob->co_lu, "dump", current);
588 cl_object_put(env, clob);
590 seq_printf(f, "%llx missing\n", pos);
591 cl_env_put(env, &refcheck);
594 result = PTR_ERR(env);
598 static void *vvp_pgcache_start(struct seq_file *f, loff_t *pos)
600 struct ll_sb_info *sbi;
606 env = cl_env_get(&refcheck);
609 if (sbi->ll_site->ls_obj_hash->hs_cur_bits > 64 - PGC_OBJ_SHIFT)
610 pos = ERR_PTR(-EFBIG);
612 *pos = vvp_pgcache_find(env, &sbi->ll_cl->cd_lu_dev,
617 cl_env_put(env, &refcheck);
622 static void *vvp_pgcache_next(struct seq_file *f, void *v, loff_t *pos)
624 struct ll_sb_info *sbi;
628 env = cl_env_get(&refcheck);
631 *pos = vvp_pgcache_find(env, &sbi->ll_cl->cd_lu_dev, *pos + 1);
634 cl_env_put(env, &refcheck);
639 static void vvp_pgcache_stop(struct seq_file *f, void *v)
644 static struct seq_operations vvp_pgcache_ops = {
645 .start = vvp_pgcache_start,
646 .next = vvp_pgcache_next,
647 .stop = vvp_pgcache_stop,
648 .show = vvp_pgcache_show
651 static int vvp_dump_pgcache_seq_open(struct inode *inode, struct file *filp)
653 struct ll_sb_info *sbi = PDE_DATA(inode);
654 struct seq_file *seq;
657 result = seq_open(filp, &vvp_pgcache_ops);
659 seq = filp->private_data;
665 const struct file_operations vvp_dump_pgcache_file_ops = {
666 .owner = THIS_MODULE,
667 .open = vvp_dump_pgcache_seq_open,
670 .release = seq_release,