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, 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>
41 #define DEBUG_SUBSYSTEM S_LLITE
43 #include "../include/obd.h"
44 #include "../include/lustre_lite.h"
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 *vvp_thread_kmem;
60 static struct kmem_cache *vvp_session_kmem;
61 static struct lu_kmem_descr vvp_caches[] = {
63 .ckd_cache = &vvp_thread_kmem,
64 .ckd_name = "vvp_thread_kmem",
65 .ckd_size = sizeof(struct vvp_thread_info),
68 .ckd_cache = &vvp_session_kmem,
69 .ckd_name = "vvp_session_kmem",
70 .ckd_size = sizeof(struct vvp_session)
77 static void *vvp_key_init(const struct lu_context *ctx,
78 struct lu_context_key *key)
80 struct vvp_thread_info *info;
82 info = kmem_cache_alloc(vvp_thread_kmem, GFP_NOFS | __GFP_ZERO);
84 info = ERR_PTR(-ENOMEM);
88 static void vvp_key_fini(const struct lu_context *ctx,
89 struct lu_context_key *key, void *data)
91 struct vvp_thread_info *info = data;
93 kmem_cache_free(vvp_thread_kmem, info);
96 static void *vvp_session_key_init(const struct lu_context *ctx,
97 struct lu_context_key *key)
99 struct vvp_session *session;
101 session = kmem_cache_alloc(vvp_session_kmem, GFP_NOFS | __GFP_ZERO);
103 session = ERR_PTR(-ENOMEM);
107 static void vvp_session_key_fini(const struct lu_context *ctx,
108 struct lu_context_key *key, void *data)
110 struct vvp_session *session = data;
112 kmem_cache_free(vvp_session_kmem, session);
115 struct lu_context_key vvp_key = {
116 .lct_tags = LCT_CL_THREAD,
117 .lct_init = vvp_key_init,
118 .lct_fini = vvp_key_fini
121 struct lu_context_key vvp_session_key = {
122 .lct_tags = LCT_SESSION,
123 .lct_init = vvp_session_key_init,
124 .lct_fini = vvp_session_key_fini
127 /* type constructor/destructor: vvp_type_{init,fini,start,stop}(). */
128 LU_TYPE_INIT_FINI(vvp, &ccc_key, &ccc_session_key, &vvp_key, &vvp_session_key);
130 static const struct lu_device_operations vvp_lu_ops = {
131 .ldo_object_alloc = vvp_object_alloc
134 static const struct cl_device_operations vvp_cl_ops = {
135 .cdo_req_init = ccc_req_init
138 static struct lu_device *vvp_device_alloc(const struct lu_env *env,
139 struct lu_device_type *t,
140 struct lustre_cfg *cfg)
142 return ccc_device_alloc(env, t, cfg, &vvp_lu_ops, &vvp_cl_ops);
145 static const struct lu_device_type_operations vvp_device_type_ops = {
146 .ldto_init = vvp_type_init,
147 .ldto_fini = vvp_type_fini,
149 .ldto_start = vvp_type_start,
150 .ldto_stop = vvp_type_stop,
152 .ldto_device_alloc = vvp_device_alloc,
153 .ldto_device_free = ccc_device_free,
154 .ldto_device_init = ccc_device_init,
155 .ldto_device_fini = ccc_device_fini
158 struct lu_device_type vvp_device_type = {
159 .ldt_tags = LU_DEVICE_CL,
160 .ldt_name = LUSTRE_VVP_NAME,
161 .ldt_ops = &vvp_device_type_ops,
162 .ldt_ctx_tags = LCT_CL_THREAD
166 * A mutex serializing calls to vvp_inode_fini() under extreme memory
167 * pressure, when environments cannot be allocated.
169 int vvp_global_init(void)
173 result = lu_kmem_init(vvp_caches);
175 result = ccc_global_init(&vvp_device_type);
177 lu_kmem_fini(vvp_caches);
182 void vvp_global_fini(void)
184 ccc_global_fini(&vvp_device_type);
185 lu_kmem_fini(vvp_caches);
188 /*****************************************************************************
190 * mirror obd-devices into cl devices.
194 int cl_sb_init(struct super_block *sb)
196 struct ll_sb_info *sbi;
197 struct cl_device *cl;
203 env = cl_env_get(&refcheck);
205 cl = cl_type_setup(env, NULL, &vvp_device_type,
206 sbi->ll_dt_exp->exp_obd->obd_lu_dev);
208 cl2ccc_dev(cl)->cdv_sb = sb;
210 sbi->ll_site = cl2lu_dev(cl)->ld_site;
212 cl_env_put(env, &refcheck);
218 int cl_sb_fini(struct super_block *sb)
220 struct ll_sb_info *sbi;
222 struct cl_device *cld;
227 env = cl_env_get(&refcheck);
232 cl_stack_fini(env, cld);
236 cl_env_put(env, &refcheck);
239 CERROR("Cannot cleanup cl-stack due to memory shortage.\n");
240 result = PTR_ERR(env);
243 * If mount failed (sbi->ll_cl == NULL), and this there are no other
244 * mounts, stop device types manually (this usually happens
245 * automatically when last device is destroyed).
251 /****************************************************************************
253 * debugfs/lustre/llite/$MNT/dump_page_cache
255 ****************************************************************************/
258 * To represent contents of a page cache as a byte stream, following
259 * information if encoded in 64bit offset:
261 * - file hash bucket in lu_site::ls_hash[] 28bits
263 * - how far file is from bucket head 4bits
265 * - page index 32bits
267 * First two data identify a file in the cache uniquely.
270 #define PGC_OBJ_SHIFT (32 + 4)
271 #define PGC_DEPTH_SHIFT (32)
273 struct vvp_pgcache_id {
279 struct lu_object_header *vpi_obj;
282 static void vvp_pgcache_id_unpack(loff_t pos, struct vvp_pgcache_id *id)
284 CLASSERT(sizeof(pos) == sizeof(__u64));
286 id->vpi_index = pos & 0xffffffff;
287 id->vpi_depth = (pos >> PGC_DEPTH_SHIFT) & 0xf;
288 id->vpi_bucket = (unsigned long long)pos >> PGC_OBJ_SHIFT;
291 static loff_t vvp_pgcache_id_pack(struct vvp_pgcache_id *id)
294 ((__u64)id->vpi_index) |
295 ((__u64)id->vpi_depth << PGC_DEPTH_SHIFT) |
296 ((__u64)id->vpi_bucket << PGC_OBJ_SHIFT);
299 static int vvp_pgcache_obj_get(struct cfs_hash *hs, struct cfs_hash_bd *bd,
300 struct hlist_node *hnode, void *data)
302 struct vvp_pgcache_id *id = data;
303 struct lu_object_header *hdr = cfs_hash_object(hs, hnode);
305 if (id->vpi_curdep-- > 0)
306 return 0; /* continue */
308 if (lu_object_is_dying(hdr))
311 cfs_hash_get(hs, hnode);
316 static struct cl_object *vvp_pgcache_obj(const struct lu_env *env,
317 struct lu_device *dev,
318 struct vvp_pgcache_id *id)
320 LASSERT(lu_device_is_cl(dev));
322 id->vpi_depth &= 0xf;
324 id->vpi_curdep = id->vpi_depth;
326 cfs_hash_hlist_for_each(dev->ld_site->ls_obj_hash, id->vpi_bucket,
327 vvp_pgcache_obj_get, id);
328 if (id->vpi_obj != NULL) {
329 struct lu_object *lu_obj;
331 lu_obj = lu_object_locate(id->vpi_obj, dev->ld_type);
332 if (lu_obj != NULL) {
333 lu_object_ref_add(lu_obj, "dump", current);
334 return lu2cl(lu_obj);
336 lu_object_put(env, lu_object_top(id->vpi_obj));
338 } else if (id->vpi_curdep > 0) {
344 static loff_t vvp_pgcache_find(const struct lu_env *env,
345 struct lu_device *dev, loff_t pos)
347 struct cl_object *clob;
348 struct lu_site *site;
349 struct vvp_pgcache_id id;
352 vvp_pgcache_id_unpack(pos, &id);
355 if (id.vpi_bucket >= CFS_HASH_NHLIST(site->ls_obj_hash))
357 clob = vvp_pgcache_obj(env, dev, &id);
359 struct cl_object_header *hdr;
363 /* got an object. Find next page. */
364 hdr = cl_object_header(clob);
366 spin_lock(&hdr->coh_page_guard);
367 nr = radix_tree_gang_lookup(&hdr->coh_tree,
371 id.vpi_index = pg->cp_index;
372 /* Cant support over 16T file */
373 nr = !(pg->cp_index > 0xffffffff);
375 spin_unlock(&hdr->coh_page_guard);
377 lu_object_ref_del(&clob->co_lu, "dump", current);
378 cl_object_put(env, clob);
380 return vvp_pgcache_id_pack(&id);
382 /* to the next object. */
385 if (id.vpi_depth == 0 && ++id.vpi_bucket == 0)
391 #define seq_page_flag(seq, page, flag, has_flags) do { \
392 if (test_bit(PG_##flag, &(page)->flags)) { \
393 seq_printf(seq, "%s"#flag, has_flags ? "|" : ""); \
398 static void vvp_pgcache_page_show(const struct lu_env *env,
399 struct seq_file *seq, struct cl_page *page)
401 struct ccc_page *cpg;
405 cpg = cl2ccc_page(cl_page_at(page, &vvp_device_type));
406 vmpage = cpg->cpg_page;
407 seq_printf(seq, " %5i | %p %p %s %s %s %s | %p %lu/%u(%p) %lu %u [",
411 cpg->cpg_write_queued ? "wq" : "- ",
412 cpg->cpg_defer_uptodate ? "du" : "- ",
413 PageWriteback(vmpage) ? "wb" : "-",
414 vmpage, vmpage->mapping->host->i_ino,
415 vmpage->mapping->host->i_generation,
416 vmpage->mapping->host, vmpage->index,
419 seq_page_flag(seq, vmpage, locked, has_flags);
420 seq_page_flag(seq, vmpage, error, has_flags);
421 seq_page_flag(seq, vmpage, referenced, has_flags);
422 seq_page_flag(seq, vmpage, uptodate, has_flags);
423 seq_page_flag(seq, vmpage, dirty, has_flags);
424 seq_page_flag(seq, vmpage, writeback, has_flags);
425 seq_printf(seq, "%s]\n", has_flags ? "" : "-");
428 static int vvp_pgcache_show(struct seq_file *f, void *v)
431 struct ll_sb_info *sbi;
432 struct cl_object *clob;
434 struct cl_page *page;
435 struct cl_object_header *hdr;
436 struct vvp_pgcache_id id;
440 env = cl_env_get(&refcheck);
443 vvp_pgcache_id_unpack(pos, &id);
445 clob = vvp_pgcache_obj(env, &sbi->ll_cl->cd_lu_dev, &id);
447 hdr = cl_object_header(clob);
449 spin_lock(&hdr->coh_page_guard);
450 page = cl_page_lookup(hdr, id.vpi_index);
451 spin_unlock(&hdr->coh_page_guard);
453 seq_printf(f, "%8x@"DFID": ",
454 id.vpi_index, PFID(&hdr->coh_lu.loh_fid));
456 vvp_pgcache_page_show(env, f, page);
457 cl_page_put(env, page);
459 seq_puts(f, "missing\n");
460 lu_object_ref_del(&clob->co_lu, "dump", current);
461 cl_object_put(env, clob);
463 seq_printf(f, "%llx missing\n", pos);
464 cl_env_put(env, &refcheck);
467 result = PTR_ERR(env);
471 static void *vvp_pgcache_start(struct seq_file *f, loff_t *pos)
473 struct ll_sb_info *sbi;
479 env = cl_env_get(&refcheck);
482 if (sbi->ll_site->ls_obj_hash->hs_cur_bits > 64 - PGC_OBJ_SHIFT)
483 pos = ERR_PTR(-EFBIG);
485 *pos = vvp_pgcache_find(env, &sbi->ll_cl->cd_lu_dev,
490 cl_env_put(env, &refcheck);
495 static void *vvp_pgcache_next(struct seq_file *f, void *v, loff_t *pos)
497 struct ll_sb_info *sbi;
501 env = cl_env_get(&refcheck);
504 *pos = vvp_pgcache_find(env, &sbi->ll_cl->cd_lu_dev, *pos + 1);
507 cl_env_put(env, &refcheck);
512 static void vvp_pgcache_stop(struct seq_file *f, void *v)
517 static const struct seq_operations vvp_pgcache_ops = {
518 .start = vvp_pgcache_start,
519 .next = vvp_pgcache_next,
520 .stop = vvp_pgcache_stop,
521 .show = vvp_pgcache_show
524 static int vvp_dump_pgcache_seq_open(struct inode *inode, struct file *filp)
526 struct seq_file *seq;
529 rc = seq_open(filp, &vvp_pgcache_ops);
533 seq = filp->private_data;
534 seq->private = inode->i_private;
539 const struct file_operations vvp_dump_pgcache_file_ops = {
540 .owner = THIS_MODULE,
541 .open = vvp_dump_pgcache_seq_open,
544 .release = seq_release,