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, 2013, 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 # error This file is kernel only.
49 #include <lustre_lite.h>
50 #include "llite_internal.h"
51 #include "vvp_internal.h"
53 /*****************************************************************************
55 * Vvp device and device type functions.
60 * vvp_ prefix stands for "Vfs Vm Posix". It corresponds to historical
61 * "llite_" (var. "ll_") prefix.
64 static struct kmem_cache *vvp_thread_kmem;
65 static struct kmem_cache *vvp_session_kmem;
66 static struct lu_kmem_descr vvp_caches[] = {
68 .ckd_cache = &vvp_thread_kmem,
69 .ckd_name = "vvp_thread_kmem",
70 .ckd_size = sizeof (struct vvp_thread_info),
73 .ckd_cache = &vvp_session_kmem,
74 .ckd_name = "vvp_session_kmem",
75 .ckd_size = sizeof (struct vvp_session)
82 static void *vvp_key_init(const struct lu_context *ctx,
83 struct lu_context_key *key)
85 struct vvp_thread_info *info;
87 OBD_SLAB_ALLOC_PTR_GFP(info, vvp_thread_kmem, GFP_NOFS);
89 info = ERR_PTR(-ENOMEM);
93 static void vvp_key_fini(const struct lu_context *ctx,
94 struct lu_context_key *key, void *data)
96 struct vvp_thread_info *info = data;
97 OBD_SLAB_FREE_PTR(info, vvp_thread_kmem);
100 static void *vvp_session_key_init(const struct lu_context *ctx,
101 struct lu_context_key *key)
103 struct vvp_session *session;
105 OBD_SLAB_ALLOC_PTR_GFP(session, vvp_session_kmem, GFP_NOFS);
107 session = ERR_PTR(-ENOMEM);
111 static void vvp_session_key_fini(const struct lu_context *ctx,
112 struct lu_context_key *key, void *data)
114 struct vvp_session *session = data;
115 OBD_SLAB_FREE_PTR(session, vvp_session_kmem);
119 struct lu_context_key vvp_key = {
120 .lct_tags = LCT_CL_THREAD,
121 .lct_init = vvp_key_init,
122 .lct_fini = vvp_key_fini
125 struct lu_context_key vvp_session_key = {
126 .lct_tags = LCT_SESSION,
127 .lct_init = vvp_session_key_init,
128 .lct_fini = vvp_session_key_fini
131 /* type constructor/destructor: vvp_type_{init,fini,start,stop}(). */
132 LU_TYPE_INIT_FINI(vvp, &ccc_key, &ccc_session_key, &vvp_key, &vvp_session_key);
134 static const struct lu_device_operations vvp_lu_ops = {
135 .ldo_object_alloc = vvp_object_alloc
138 static const struct cl_device_operations vvp_cl_ops = {
139 .cdo_req_init = ccc_req_init
142 static struct lu_device *vvp_device_alloc(const struct lu_env *env,
143 struct lu_device_type *t,
144 struct lustre_cfg *cfg)
146 return ccc_device_alloc(env, t, cfg, &vvp_lu_ops, &vvp_cl_ops);
149 static const struct lu_device_type_operations vvp_device_type_ops = {
150 .ldto_init = vvp_type_init,
151 .ldto_fini = vvp_type_fini,
153 .ldto_start = vvp_type_start,
154 .ldto_stop = vvp_type_stop,
156 .ldto_device_alloc = vvp_device_alloc,
157 .ldto_device_free = ccc_device_free,
158 .ldto_device_init = ccc_device_init,
159 .ldto_device_fini = ccc_device_fini
162 struct lu_device_type vvp_device_type = {
163 .ldt_tags = LU_DEVICE_CL,
164 .ldt_name = LUSTRE_VVP_NAME,
165 .ldt_ops = &vvp_device_type_ops,
166 .ldt_ctx_tags = LCT_CL_THREAD
170 * A mutex serializing calls to vvp_inode_fini() under extreme memory
171 * pressure, when environments cannot be allocated.
173 int vvp_global_init(void)
177 result = lu_kmem_init(vvp_caches);
179 result = ccc_global_init(&vvp_device_type);
181 lu_kmem_fini(vvp_caches);
186 void vvp_global_fini(void)
188 ccc_global_fini(&vvp_device_type);
189 lu_kmem_fini(vvp_caches);
193 /*****************************************************************************
195 * mirror obd-devices into cl devices.
199 int cl_sb_init(struct super_block *sb)
201 struct ll_sb_info *sbi;
202 struct cl_device *cl;
208 env = cl_env_get(&refcheck);
210 cl = cl_type_setup(env, NULL, &vvp_device_type,
211 sbi->ll_dt_exp->exp_obd->obd_lu_dev);
213 cl2ccc_dev(cl)->cdv_sb = sb;
215 sbi->ll_site = cl2lu_dev(cl)->ld_site;
217 cl_env_put(env, &refcheck);
223 int cl_sb_fini(struct super_block *sb)
225 struct ll_sb_info *sbi;
227 struct cl_device *cld;
233 env = cl_env_get(&refcheck);
238 cl_stack_fini(env, cld);
242 cl_env_put(env, &refcheck);
245 CERROR("Cannot cleanup cl-stack due to memory shortage.\n");
246 result = PTR_ERR(env);
252 /****************************************************************************
254 * /proc/fs/lustre/llite/$MNT/dump_page_cache
256 ****************************************************************************/
259 * To represent contents of a page cache as a byte stream, following
260 * information if encoded in 64bit offset:
262 * - file hash bucket in lu_site::ls_hash[] 28bits
264 * - how far file is from bucket head 4bits
266 * - page index 32bits
268 * First two data identify a file in the cache uniquely.
271 #define PGC_OBJ_SHIFT (32 + 4)
272 #define PGC_DEPTH_SHIFT (32)
274 struct vvp_pgcache_id {
280 struct lu_object_header *vpi_obj;
283 static void vvp_pgcache_id_unpack(loff_t pos, struct vvp_pgcache_id *id)
285 CLASSERT(sizeof(pos) == sizeof(__u64));
287 id->vpi_index = pos & 0xffffffff;
288 id->vpi_depth = (pos >> PGC_DEPTH_SHIFT) & 0xf;
289 id->vpi_bucket = ((unsigned long long)pos >> PGC_OBJ_SHIFT);
292 static loff_t vvp_pgcache_id_pack(struct vvp_pgcache_id *id)
295 ((__u64)id->vpi_index) |
296 ((__u64)id->vpi_depth << PGC_DEPTH_SHIFT) |
297 ((__u64)id->vpi_bucket << PGC_OBJ_SHIFT);
300 static int vvp_pgcache_obj_get(cfs_hash_t *hs, cfs_hash_bd_t *bd,
301 struct hlist_node *hnode, void *data)
303 struct vvp_pgcache_id *id = data;
304 struct lu_object_header *hdr = cfs_hash_object(hs, hnode);
306 if (id->vpi_curdep-- > 0)
307 return 0; /* continue */
309 if (lu_object_is_dying(hdr))
312 cfs_hash_get(hs, hnode);
317 static struct cl_object *vvp_pgcache_obj(const struct lu_env *env,
318 struct lu_device *dev,
319 struct vvp_pgcache_id *id)
321 LASSERT(lu_device_is_cl(dev));
323 id->vpi_depth &= 0xf;
325 id->vpi_curdep = id->vpi_depth;
327 cfs_hash_hlist_for_each(dev->ld_site->ls_obj_hash, id->vpi_bucket,
328 vvp_pgcache_obj_get, id);
329 if (id->vpi_obj != NULL) {
330 struct lu_object *lu_obj;
332 lu_obj = lu_object_locate(id->vpi_obj, dev->ld_type);
333 if (lu_obj != NULL) {
334 lu_object_ref_add(lu_obj, "dump", current);
335 return lu2cl(lu_obj);
337 lu_object_put(env, lu_object_top(id->vpi_obj));
339 } else if (id->vpi_curdep > 0) {
345 static loff_t vvp_pgcache_find(const struct lu_env *env,
346 struct lu_device *dev, loff_t pos)
348 struct cl_object *clob;
349 struct lu_site *site;
350 struct vvp_pgcache_id id;
353 vvp_pgcache_id_unpack(pos, &id);
356 if (id.vpi_bucket >= CFS_HASH_NHLIST(site->ls_obj_hash))
358 clob = vvp_pgcache_obj(env, dev, &id);
360 struct inode *inode = ccc_object_inode(clob);
364 nr = find_get_pages_contig(inode->i_mapping,
365 id.vpi_index, 1, &vmpage);
367 id.vpi_index = vmpage->index;
368 /* Cant support over 16T file */
369 nr = !(vmpage->index > 0xffffffff);
370 page_cache_release(vmpage);
373 lu_object_ref_del(&clob->co_lu, "dump", current);
374 cl_object_put(env, clob);
376 return vvp_pgcache_id_pack(&id);
378 /* to the next object. */
381 if (id.vpi_depth == 0 && ++id.vpi_bucket == 0)
387 #define seq_page_flag(seq, page, flag, has_flags) do { \
388 if (test_bit(PG_##flag, &(page)->flags)) { \
389 seq_printf(seq, "%s"#flag, has_flags ? "|" : ""); \
394 static void vvp_pgcache_page_show(const struct lu_env *env,
395 struct seq_file *seq, struct cl_page *page)
397 struct ccc_page *cpg;
401 cpg = cl2ccc_page(cl_page_at(page, &vvp_device_type));
402 vmpage = cpg->cpg_page;
403 seq_printf(seq, " %5i | %p %p %s %s %s %s | %p "DFID"(%p) %lu %u [",
407 cpg->cpg_write_queued ? "wq" : "- ",
408 cpg->cpg_defer_uptodate ? "du" : "- ",
409 PageWriteback(vmpage) ? "wb" : "-",
411 PFID(ll_inode2fid(vmpage->mapping->host)),
412 vmpage->mapping->host, vmpage->index,
415 seq_page_flag(seq, vmpage, locked, has_flags);
416 seq_page_flag(seq, vmpage, error, has_flags);
417 seq_page_flag(seq, vmpage, referenced, has_flags);
418 seq_page_flag(seq, vmpage, uptodate, has_flags);
419 seq_page_flag(seq, vmpage, dirty, has_flags);
420 seq_page_flag(seq, vmpage, writeback, has_flags);
421 seq_printf(seq, "%s]\n", has_flags ? "" : "-");
424 static int vvp_pgcache_show(struct seq_file *f, void *v)
427 struct ll_sb_info *sbi;
428 struct cl_object *clob;
430 struct vvp_pgcache_id id;
434 env = cl_env_get(&refcheck);
437 vvp_pgcache_id_unpack(pos, &id);
439 clob = vvp_pgcache_obj(env, &sbi->ll_cl->cd_lu_dev, &id);
441 struct inode *inode = ccc_object_inode(clob);
442 struct cl_page *page = NULL;
445 result = find_get_pages_contig(inode->i_mapping,
446 id.vpi_index, 1, &vmpage);
449 page = cl_vmpage_page(vmpage, clob);
452 page_cache_release(vmpage);
455 seq_printf(f, "%8x@"DFID": ", id.vpi_index,
456 PFID(lu_object_fid(&clob->co_lu)));
458 vvp_pgcache_page_show(env, f, page);
459 cl_page_put(env, page);
461 seq_puts(f, "missing\n");
462 lu_object_ref_del(&clob->co_lu, "dump", current);
463 cl_object_put(env, clob);
465 seq_printf(f, "%llx missing\n", pos);
466 cl_env_put(env, &refcheck);
469 result = PTR_ERR(env);
473 static void *vvp_pgcache_start(struct seq_file *f, loff_t *pos)
475 struct ll_sb_info *sbi;
481 env = cl_env_get(&refcheck);
484 if (sbi->ll_site->ls_obj_hash->hs_cur_bits > 64 - PGC_OBJ_SHIFT)
485 pos = ERR_PTR(-EFBIG);
487 *pos = vvp_pgcache_find(env, &sbi->ll_cl->cd_lu_dev,
492 cl_env_put(env, &refcheck);
497 static void *vvp_pgcache_next(struct seq_file *f, void *v, loff_t *pos)
499 struct ll_sb_info *sbi;
503 env = cl_env_get(&refcheck);
506 *pos = vvp_pgcache_find(env, &sbi->ll_cl->cd_lu_dev, *pos + 1);
509 cl_env_put(env, &refcheck);
514 static void vvp_pgcache_stop(struct seq_file *f, void *v)
519 static struct seq_operations vvp_pgcache_ops = {
520 .start = vvp_pgcache_start,
521 .next = vvp_pgcache_next,
522 .stop = vvp_pgcache_stop,
523 .show = vvp_pgcache_show
526 static int vvp_dump_pgcache_seq_open(struct inode *inode, struct file *filp)
528 struct ll_sb_info *sbi = PDE_DATA(inode);
529 struct seq_file *seq;
532 result = seq_open(filp, &vvp_pgcache_ops);
534 seq = filp->private_data;
540 const struct file_operations vvp_dump_pgcache_file_ops = {
541 .owner = THIS_MODULE,
542 .open = vvp_dump_pgcache_seq_open,
545 .release = seq_release,