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