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