Whamcloud - gitweb
LU-6245 libcfs: cleanup up libcfs hash code for upstream
[fs/lustre-release.git] / lustre / llite / vvp_dev.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * cl_device and cl_device_type implementation for VVP layer.
37  *
38  *   Author: Nikita Danilov <nikita.danilov@sun.com>
39  *   Author: Jinshan Xiong <jinshan.xiong@intel.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_LLITE
43
44 #include <obd.h>
45 #include "llite_internal.h"
46 #include "vvp_internal.h"
47
48 /*****************************************************************************
49  *
50  * Vvp device and device type functions.
51  *
52  */
53
54 /*
55  * vvp_ prefix stands for "Vfs Vm Posix". It corresponds to historical
56  * "llite_" (var. "ll_") prefix.
57  */
58
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;
65
66 static struct lu_kmem_descr vvp_caches[] = {
67         {
68                 .ckd_cache = &ll_thread_kmem,
69                 .ckd_name  = "ll_thread_kmem",
70                 .ckd_size  = sizeof(struct ll_thread_info),
71         },
72         {
73                 .ckd_cache = &vvp_lock_kmem,
74                 .ckd_name  = "vvp_lock_kmem",
75                 .ckd_size  = sizeof(struct vvp_lock),
76         },
77         {
78                 .ckd_cache = &vvp_object_kmem,
79                 .ckd_name  = "vvp_object_kmem",
80                 .ckd_size  = sizeof(struct vvp_object),
81         },
82         {
83                 .ckd_cache = &vvp_req_kmem,
84                 .ckd_name  = "vvp_req_kmem",
85                 .ckd_size  = sizeof(struct vvp_req),
86         },
87         {
88                 .ckd_cache = &vvp_session_kmem,
89                 .ckd_name  = "vvp_session_kmem",
90                 .ckd_size  = sizeof (struct vvp_session)
91         },
92         {
93                 .ckd_cache = &vvp_thread_kmem,
94                 .ckd_name  = "vvp_thread_kmem",
95                 .ckd_size  = sizeof(struct vvp_thread_info),
96         },
97         {
98                 .ckd_cache = NULL
99         }
100 };
101
102 static void *ll_thread_key_init(const struct lu_context *ctx,
103                                 struct lu_context_key *key)
104 {
105         struct ll_thread_info *lti;
106
107         OBD_SLAB_ALLOC_PTR_GFP(lti, ll_thread_kmem, GFP_NOFS);
108         if (lti == NULL)
109                 lti = ERR_PTR(-ENOMEM);
110
111         return lti;
112 }
113
114 static void ll_thread_key_fini(const struct lu_context *ctx,
115                                struct lu_context_key *key, void *data)
116 {
117         struct ll_thread_info *lti = data;
118
119         OBD_SLAB_FREE_PTR(lti, ll_thread_kmem);
120 }
121
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,
126 };
127
128 static void *vvp_session_key_init(const struct lu_context *ctx,
129                                   struct lu_context_key *key)
130 {
131         struct vvp_session *session;
132
133         OBD_SLAB_ALLOC_PTR_GFP(session, vvp_session_kmem, GFP_NOFS);
134         if (session == NULL)
135                 session = ERR_PTR(-ENOMEM);
136         return session;
137 }
138
139 static void vvp_session_key_fini(const struct lu_context *ctx,
140                                  struct lu_context_key *key, void *data)
141 {
142         struct vvp_session *session = data;
143         OBD_SLAB_FREE_PTR(session, vvp_session_kmem);
144 }
145
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
150 };
151
152 static void *vvp_thread_key_init(const struct lu_context *ctx,
153                                  struct lu_context_key *key)
154 {
155         struct vvp_thread_info *vti;
156
157         OBD_SLAB_ALLOC_PTR_GFP(vti, vvp_thread_kmem, GFP_NOFS);
158         if (vti == NULL)
159                 vti = ERR_PTR(-ENOMEM);
160         return vti;
161 }
162
163 static void vvp_thread_key_fini(const struct lu_context *ctx,
164                                 struct lu_context_key *key, void *data)
165 {
166         struct vvp_thread_info *vti = data;
167         OBD_SLAB_FREE_PTR(vti, vvp_thread_kmem);
168 }
169
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,
174 };
175
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);
178
179 static const struct lu_device_operations vvp_lu_ops = {
180         .ldo_object_alloc      = vvp_object_alloc
181 };
182
183 static const struct cl_device_operations vvp_cl_ops = {
184         .cdo_req_init = vvp_req_init,
185 };
186
187 static struct lu_device *vvp_device_free(const struct lu_env *env,
188                                          struct lu_device *d)
189 {
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);
193
194         if (d->ld_site != NULL) {
195                 cl_site_fini(site);
196                 OBD_FREE_PTR(site);
197         }
198
199         cl_device_fini(lu2cl_dev(d));
200         OBD_FREE_PTR(vdv);
201         return next;
202 }
203
204 static struct lu_device *vvp_device_alloc(const struct lu_env *env,
205                                           struct lu_device_type *t,
206                                           struct lustre_cfg *cfg)
207 {
208         struct vvp_device *vdv;
209         struct lu_device *lud;
210         struct cl_site *site;
211         int rc;
212         ENTRY;
213
214         OBD_ALLOC_PTR(vdv);
215         if (vdv == NULL)
216                 RETURN(ERR_PTR(-ENOMEM));
217
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;
222
223         OBD_ALLOC_PTR(site);
224         if (site != NULL) {
225                 rc = cl_site_init(site, &vdv->vdv_cl);
226                 if (rc == 0)
227                         rc = lu_site_init_finish(&site->cs_lu);
228                 else {
229                         LASSERT(lud->ld_site == NULL);
230                         CERROR("Cannot init lu_site, rc %d.\n", rc);
231                         OBD_FREE_PTR(site);
232                 }
233         } else
234                 rc = -ENOMEM;
235         if (rc != 0) {
236                 vvp_device_free(env, lud);
237                 lud = ERR_PTR(rc);
238         }
239         RETURN(lud);
240 }
241
242 static int vvp_device_init(const struct lu_env *env, struct lu_device *d,
243                            const char *name, struct lu_device *next)
244 {
245         struct vvp_device  *vdv;
246         int rc;
247         ENTRY;
248
249         vdv = lu2vvp_dev(d);
250         vdv->vdv_next = lu2cl_dev(next);
251
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);
256         if (rc == 0) {
257                 lu_device_get(next);
258                 lu_ref_add(&next->ld_reference, "lu-stack", &lu_site_init);
259         }
260         RETURN(rc);
261 }
262
263 static struct lu_device *vvp_device_fini(const struct lu_env *env,
264                                          struct lu_device *d)
265 {
266         return cl2lu_dev(lu2vvp_dev(d)->vdv_next);
267 }
268
269 static const struct lu_device_type_operations vvp_device_type_ops = {
270         .ldto_init = vvp_type_init,
271         .ldto_fini = vvp_type_fini,
272
273         .ldto_start = vvp_type_start,
274         .ldto_stop  = vvp_type_stop,
275
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,
280 };
281
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
287 };
288
289 /**
290  * A mutex serializing calls to vvp_inode_fini() under extreme memory
291  * pressure, when environments cannot be allocated.
292  */
293 int vvp_global_init(void)
294 {
295         int rc;
296
297         rc = lu_kmem_init(vvp_caches);
298         if (rc != 0)
299                 return rc;
300
301         rc = lu_device_type_init(&vvp_device_type);
302         if (rc != 0)
303                 goto out_kmem;
304
305         return 0;
306
307 out_kmem:
308         lu_kmem_fini(vvp_caches);
309
310         return rc;
311 }
312
313 void vvp_global_fini(void)
314 {
315         lu_device_type_fini(&vvp_device_type);
316         lu_kmem_fini(vvp_caches);
317 }
318
319 /*****************************************************************************
320  *
321  * mirror obd-devices into cl devices.
322  *
323  */
324
325 int cl_sb_init(struct super_block *sb)
326 {
327         struct ll_sb_info *sbi;
328         struct cl_device  *cl;
329         struct lu_env     *env;
330         int rc = 0;
331         int refcheck;
332
333         sbi  = ll_s2sbi(sb);
334         env = cl_env_get(&refcheck);
335         if (!IS_ERR(env)) {
336                 cl = cl_type_setup(env, NULL, &vvp_device_type,
337                                    sbi->ll_dt_exp->exp_obd->obd_lu_dev);
338                 if (!IS_ERR(cl)) {
339                         cl2vvp_dev(cl)->vdv_sb = sb;
340                         sbi->ll_cl = cl;
341                         sbi->ll_site = cl2lu_dev(cl)->ld_site;
342                 }
343                 cl_env_put(env, &refcheck);
344         } else
345                 rc = PTR_ERR(env);
346         RETURN(rc);
347 }
348
349 int cl_sb_fini(struct super_block *sb)
350 {
351         struct ll_sb_info *sbi;
352         struct lu_env     *env;
353         struct cl_device  *cld;
354         int                refcheck;
355         int                result;
356
357         ENTRY;
358         sbi = ll_s2sbi(sb);
359         env = cl_env_get(&refcheck);
360         if (!IS_ERR(env)) {
361                 cld = sbi->ll_cl;
362
363                 if (cld != NULL) {
364                         cl_stack_fini(env, cld);
365                         sbi->ll_cl = NULL;
366                         sbi->ll_site = NULL;
367                 }
368                 cl_env_put(env, &refcheck);
369                 result = 0;
370         } else {
371                 CERROR("Cannot cleanup cl-stack due to memory shortage.\n");
372                 result = PTR_ERR(env);
373         }
374
375         RETURN(result);
376 }
377
378 /****************************************************************************
379  *
380  * /proc/fs/lustre/llite/$MNT/dump_page_cache
381  *
382  ****************************************************************************/
383
384 /*
385  * To represent contents of a page cache as a byte stream, following
386  * information if encoded in 64bit offset:
387  *
388  *       - file hash bucket in lu_site::ls_hash[]       28bits
389  *
390  *       - how far file is from bucket head              4bits
391  *
392  *       - page index                                   32bits
393  *
394  * First two data identify a file in the cache uniquely.
395  */
396
397 #define PGC_OBJ_SHIFT (32 + 4)
398 #define PGC_DEPTH_SHIFT (32)
399
400 struct vvp_pgcache_id {
401         unsigned                 vpi_bucket;
402         unsigned                 vpi_depth;
403         uint32_t                 vpi_index;
404
405         unsigned                 vpi_curdep;
406         struct lu_object_header *vpi_obj;
407 };
408
409 static void vvp_pgcache_id_unpack(loff_t pos, struct vvp_pgcache_id *id)
410 {
411         CLASSERT(sizeof(pos) == sizeof(__u64));
412
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);
416 }
417
418 static loff_t vvp_pgcache_id_pack(struct vvp_pgcache_id *id)
419 {
420         return
421                 ((__u64)id->vpi_index) |
422                 ((__u64)id->vpi_depth  << PGC_DEPTH_SHIFT) |
423                 ((__u64)id->vpi_bucket << PGC_OBJ_SHIFT);
424 }
425
426 static int vvp_pgcache_obj_get(struct cfs_hash *hs, struct cfs_hash_bd *bd,
427                                struct hlist_node *hnode, void *data)
428 {
429         struct vvp_pgcache_id   *id  = data;
430         struct lu_object_header *hdr = cfs_hash_object(hs, hnode);
431
432         if (id->vpi_curdep-- > 0)
433                 return 0; /* continue */
434
435         if (lu_object_is_dying(hdr))
436                 return 1;
437
438         cfs_hash_get(hs, hnode);
439         id->vpi_obj = hdr;
440         return 1;
441 }
442
443 static struct cl_object *vvp_pgcache_obj(const struct lu_env *env,
444                                          struct lu_device *dev,
445                                          struct vvp_pgcache_id *id)
446 {
447         LASSERT(lu_device_is_cl(dev));
448
449         id->vpi_depth &= 0xf;
450         id->vpi_obj    = NULL;
451         id->vpi_curdep = id->vpi_depth;
452
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;
457
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);
462                 }
463                 lu_object_put(env, lu_object_top(id->vpi_obj));
464
465         } else if (id->vpi_curdep > 0) {
466                 id->vpi_depth = 0xf;
467         }
468         return NULL;
469 }
470
471 static loff_t vvp_pgcache_find(const struct lu_env *env,
472                                struct lu_device *dev, loff_t pos)
473 {
474         struct cl_object     *clob;
475         struct lu_site       *site;
476         struct vvp_pgcache_id id;
477
478         site = dev->ld_site;
479         vvp_pgcache_id_unpack(pos, &id);
480
481         while (1) {
482                 if (id.vpi_bucket >= CFS_HASH_NHLIST(site->ls_obj_hash))
483                         return ~0ULL;
484                 clob = vvp_pgcache_obj(env, dev, &id);
485                 if (clob != NULL) {
486                         struct inode *inode = vvp_object_inode(clob);
487                         struct page *vmpage;
488                         int nr;
489
490                         nr = find_get_pages_contig(inode->i_mapping,
491                                                    id.vpi_index, 1, &vmpage);
492                         if (nr > 0) {
493                                 id.vpi_index = vmpage->index;
494                                 /* Cant support over 16T file */
495                                 nr = !(vmpage->index > 0xffffffff);
496                                 page_cache_release(vmpage);
497                         }
498
499                         lu_object_ref_del(&clob->co_lu, "dump", current);
500                         cl_object_put(env, clob);
501                         if (nr > 0)
502                                 return vvp_pgcache_id_pack(&id);
503                 }
504                 /* to the next object. */
505                 ++id.vpi_depth;
506                 id.vpi_depth &= 0xf;
507                 if (id.vpi_depth == 0 && ++id.vpi_bucket == 0)
508                         return ~0ULL;
509                 id.vpi_index = 0;
510         }
511 }
512
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 ? "|" : "");       \
516                 has_flags = 1;                                          \
517         }                                                               \
518 } while(0)
519
520 static void vvp_pgcache_page_show(const struct lu_env *env,
521                                   struct seq_file *seq, struct cl_page *page)
522 {
523         struct vvp_page *vpg;
524         struct page      *vmpage;
525         int              has_flags;
526
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 [",
530                    0 /* gen */,
531                    vpg, page,
532                    "none",
533                    vpg->vpg_defer_uptodate ? "du" : "- ",
534                    PageWriteback(vmpage) ? "wb" : "-",
535                    vmpage,
536                    PFID(ll_inode2fid(vmpage->mapping->host)),
537                    vmpage->mapping->host, vmpage->index,
538                    page_count(vmpage));
539         has_flags = 0;
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 ? "" : "-");
547 }
548
549 static int vvp_pgcache_show(struct seq_file *f, void *v)
550 {
551         loff_t                   pos;
552         struct ll_sb_info       *sbi;
553         struct cl_object        *clob;
554         struct lu_env           *env;
555         struct vvp_pgcache_id    id;
556         int                      refcheck;
557         int                      result;
558
559         env = cl_env_get(&refcheck);
560         if (!IS_ERR(env)) {
561                 pos = *(loff_t *) v;
562                 vvp_pgcache_id_unpack(pos, &id);
563                 sbi = f->private;
564                 clob = vvp_pgcache_obj(env, &sbi->ll_cl->cd_lu_dev, &id);
565                 if (clob != NULL) {
566                         struct inode *inode = vvp_object_inode(clob);
567                         struct cl_page *page = NULL;
568                         struct page *vmpage;
569
570                         result = find_get_pages_contig(inode->i_mapping,
571                                                       id.vpi_index, 1, &vmpage);
572                         if (result > 0) {
573                                 lock_page(vmpage);
574                                 page = cl_vmpage_page(vmpage, clob);
575                                 unlock_page(vmpage);
576
577                                 page_cache_release(vmpage);
578                         }
579
580                         seq_printf(f, "%8x@"DFID": ", id.vpi_index,
581                                    PFID(lu_object_fid(&clob->co_lu)));
582                         if (page != NULL) {
583                                 vvp_pgcache_page_show(env, f, page);
584                                 cl_page_put(env, page);
585                         } else
586                                 seq_puts(f, "missing\n");
587                         lu_object_ref_del(&clob->co_lu, "dump", current);
588                         cl_object_put(env, clob);
589                 } else
590                         seq_printf(f, "%llx missing\n", pos);
591                 cl_env_put(env, &refcheck);
592                 result = 0;
593         } else
594                 result = PTR_ERR(env);
595         return result;
596 }
597
598 static void *vvp_pgcache_start(struct seq_file *f, loff_t *pos)
599 {
600         struct ll_sb_info *sbi;
601         struct lu_env     *env;
602         int                refcheck;
603
604         sbi = f->private;
605
606         env = cl_env_get(&refcheck);
607         if (!IS_ERR(env)) {
608                 sbi = f->private;
609                 if (sbi->ll_site->ls_obj_hash->hs_cur_bits > 64 - PGC_OBJ_SHIFT)
610                         pos = ERR_PTR(-EFBIG);
611                 else {
612                         *pos = vvp_pgcache_find(env, &sbi->ll_cl->cd_lu_dev,
613                                                 *pos);
614                         if (*pos == ~0ULL)
615                                 pos = NULL;
616                 }
617                 cl_env_put(env, &refcheck);
618         }
619         return pos;
620 }
621
622 static void *vvp_pgcache_next(struct seq_file *f, void *v, loff_t *pos)
623 {
624         struct ll_sb_info *sbi;
625         struct lu_env     *env;
626         int                refcheck;
627
628         env = cl_env_get(&refcheck);
629         if (!IS_ERR(env)) {
630                 sbi = f->private;
631                 *pos = vvp_pgcache_find(env, &sbi->ll_cl->cd_lu_dev, *pos + 1);
632                 if (*pos == ~0ULL)
633                         pos = NULL;
634                 cl_env_put(env, &refcheck);
635         }
636         return pos;
637 }
638
639 static void vvp_pgcache_stop(struct seq_file *f, void *v)
640 {
641         /* Nothing to do */
642 }
643
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
649 };
650
651 static int vvp_dump_pgcache_seq_open(struct inode *inode, struct file *filp)
652 {
653         struct ll_sb_info       *sbi = PDE_DATA(inode);
654         struct seq_file         *seq;
655         int                     result;
656
657         result = seq_open(filp, &vvp_pgcache_ops);
658         if (result == 0) {
659                 seq = filp->private_data;
660                 seq->private = sbi;
661         }
662         return result;
663 }
664
665 const struct file_operations vvp_dump_pgcache_file_ops = {
666         .owner   = THIS_MODULE,
667         .open    = vvp_dump_pgcache_seq_open,
668         .read    = seq_read,
669         .llseek  = seq_lseek,
670         .release = seq_release,
671 };