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