Whamcloud - gitweb
b9daf5248b32fb19e4f81039b630cc694f311327
[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 #include <linux/kallsyms.h>
44
45 /*****************************************************************************
46  *
47  * Vvp device and device type functions.
48  *
49  */
50
51 /*
52  * vvp_ prefix stands for "Vfs Vm Posix". It corresponds to historical
53  * "llite_" (var. "ll_") prefix.
54  */
55
56 static struct kmem_cache *ll_thread_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_object_kmem,
69                 .ckd_name  = "vvp_object_kmem",
70                 .ckd_size  = sizeof(struct vvp_object),
71         },
72         {
73                 .ckd_cache = &vvp_session_kmem,
74                 .ckd_name  = "vvp_session_kmem",
75                 .ckd_size  = sizeof (struct vvp_session)
76         },
77         {
78                 .ckd_cache = &vvp_thread_kmem,
79                 .ckd_name  = "vvp_thread_kmem",
80                 .ckd_size  = sizeof(struct vvp_thread_info),
81         },
82         {
83                 .ckd_cache = NULL
84         }
85 };
86
87 static void *ll_thread_key_init(const struct lu_context *ctx,
88                                 struct lu_context_key *key)
89 {
90         struct ll_thread_info *lti;
91
92         OBD_SLAB_ALLOC_PTR_GFP(lti, ll_thread_kmem, GFP_NOFS);
93         if (lti == NULL)
94                 lti = ERR_PTR(-ENOMEM);
95
96         return lti;
97 }
98
99 static void ll_thread_key_fini(const struct lu_context *ctx,
100                                struct lu_context_key *key, void *data)
101 {
102         struct ll_thread_info *lti = data;
103
104         OBD_SLAB_FREE_PTR(lti, ll_thread_kmem);
105 }
106
107 struct lu_context_key ll_thread_key = {
108         .lct_tags = LCT_CL_THREAD,
109         .lct_init = ll_thread_key_init,
110         .lct_fini = ll_thread_key_fini,
111 };
112
113 static void *vvp_session_key_init(const struct lu_context *ctx,
114                                   struct lu_context_key *key)
115 {
116         struct vvp_session *session;
117
118         OBD_SLAB_ALLOC_PTR_GFP(session, vvp_session_kmem, GFP_NOFS);
119         if (session == NULL)
120                 session = ERR_PTR(-ENOMEM);
121         return session;
122 }
123
124 static void vvp_session_key_fini(const struct lu_context *ctx,
125                                  struct lu_context_key *key, void *data)
126 {
127         struct vvp_session *session = data;
128         OBD_SLAB_FREE_PTR(session, vvp_session_kmem);
129 }
130
131 struct lu_context_key vvp_session_key = {
132         .lct_tags = LCT_SESSION,
133         .lct_init = vvp_session_key_init,
134         .lct_fini = vvp_session_key_fini
135 };
136
137 static void *vvp_thread_key_init(const struct lu_context *ctx,
138                                  struct lu_context_key *key)
139 {
140         struct vvp_thread_info *vti;
141
142         OBD_SLAB_ALLOC_PTR_GFP(vti, vvp_thread_kmem, GFP_NOFS);
143         if (vti == NULL)
144                 vti = ERR_PTR(-ENOMEM);
145         return vti;
146 }
147
148 static void vvp_thread_key_fini(const struct lu_context *ctx,
149                                 struct lu_context_key *key, void *data)
150 {
151         struct vvp_thread_info *vti = data;
152         OBD_SLAB_FREE_PTR(vti, vvp_thread_kmem);
153 }
154
155 struct lu_context_key vvp_thread_key = {
156         .lct_tags = LCT_CL_THREAD,
157         .lct_init = vvp_thread_key_init,
158         .lct_fini = vvp_thread_key_fini,
159 };
160
161 /* type constructor/destructor: vvp_type_{init,fini,start,stop}(). */
162 LU_TYPE_INIT_FINI(vvp, &ll_thread_key, &vvp_session_key, &vvp_thread_key);
163
164 static const struct lu_device_operations vvp_lu_ops = {
165         .ldo_object_alloc      = vvp_object_alloc
166 };
167
168 static struct lu_device *vvp_device_free(const struct lu_env *env,
169                                          struct lu_device *d)
170 {
171         struct vvp_device *vdv  = lu2vvp_dev(d);
172         struct cl_site    *site = lu2cl_site(d->ld_site);
173         struct lu_device  *next = cl2lu_dev(vdv->vdv_next);
174
175         if (d->ld_site != NULL) {
176                 cl_site_fini(site);
177                 OBD_FREE_PTR(site);
178         }
179
180         cl_device_fini(lu2cl_dev(d));
181         OBD_FREE_PTR(vdv);
182         return next;
183 }
184
185 static struct lu_device *vvp_device_alloc(const struct lu_env *env,
186                                           struct lu_device_type *t,
187                                           struct lustre_cfg *cfg)
188 {
189         struct vvp_device *vdv;
190         struct lu_device *lud;
191         struct cl_site *site;
192         int rc;
193         ENTRY;
194
195         OBD_ALLOC_PTR(vdv);
196         if (vdv == NULL)
197                 RETURN(ERR_PTR(-ENOMEM));
198
199         lud = &vdv->vdv_cl.cd_lu_dev;
200         cl_device_init(&vdv->vdv_cl, t);
201         vvp2lu_dev(vdv)->ld_ops = &vvp_lu_ops;
202
203         OBD_ALLOC_PTR(site);
204         if (site != NULL) {
205                 rc = cl_site_init(site, &vdv->vdv_cl);
206                 if (rc == 0)
207                         rc = lu_site_init_finish(&site->cs_lu);
208                 else {
209                         LASSERT(lud->ld_site == NULL);
210                         CERROR("Cannot init lu_site, rc %d.\n", rc);
211                         OBD_FREE_PTR(site);
212                 }
213         } else
214                 rc = -ENOMEM;
215         if (rc != 0) {
216                 vvp_device_free(env, lud);
217                 lud = ERR_PTR(rc);
218         }
219         RETURN(lud);
220 }
221
222 static int vvp_device_init(const struct lu_env *env, struct lu_device *d,
223                            const char *name, struct lu_device *next)
224 {
225         struct vvp_device  *vdv;
226         int rc;
227         ENTRY;
228
229         vdv = lu2vvp_dev(d);
230         vdv->vdv_next = lu2cl_dev(next);
231
232         LASSERT(d->ld_site != NULL && next->ld_type != NULL);
233         next->ld_site = d->ld_site;
234         rc = next->ld_type->ldt_ops->ldto_device_init(
235                 env, next, next->ld_type->ldt_name, NULL);
236         if (rc == 0) {
237                 lu_device_get(next);
238                 lu_ref_add(&next->ld_reference, "lu-stack", &lu_site_init);
239         }
240         RETURN(rc);
241 }
242
243 static struct lu_device *vvp_device_fini(const struct lu_env *env,
244                                          struct lu_device *d)
245 {
246         return cl2lu_dev(lu2vvp_dev(d)->vdv_next);
247 }
248
249 static const struct lu_device_type_operations vvp_device_type_ops = {
250         .ldto_init = vvp_type_init,
251         .ldto_fini = vvp_type_fini,
252
253         .ldto_start = vvp_type_start,
254         .ldto_stop  = vvp_type_stop,
255
256         .ldto_device_alloc      = vvp_device_alloc,
257         .ldto_device_free       = vvp_device_free,
258         .ldto_device_init       = vvp_device_init,
259         .ldto_device_fini       = vvp_device_fini,
260 };
261
262 struct lu_device_type vvp_device_type = {
263         .ldt_tags     = LU_DEVICE_CL,
264         .ldt_name     = LUSTRE_VVP_NAME,
265         .ldt_ops      = &vvp_device_type_ops,
266         .ldt_ctx_tags = LCT_CL_THREAD
267 };
268
269 #ifndef HAVE_ACCOUNT_PAGE_DIRTIED_EXPORT
270 unsigned int (*vvp_account_page_dirtied)(struct page *page,
271                                          struct address_space *mapping);
272 #endif
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 #ifndef HAVE_ACCOUNT_PAGE_DIRTIED_EXPORT
291         /*
292          * Kernel v5.2-5678-gac1c3e4 no longer exports account_page_dirtied
293          */
294         vvp_account_page_dirtied = (void *)
295                 kallsyms_lookup_name("account_page_dirtied");
296         BUG_ON(!vvp_account_page_dirtied);
297 #endif
298
299         return 0;
300
301 out_kmem:
302         lu_kmem_fini(vvp_caches);
303
304         return rc;
305 }
306
307 void vvp_global_fini(void)
308 {
309         lu_device_type_fini(&vvp_device_type);
310         lu_kmem_fini(vvp_caches);
311 }
312
313 /*****************************************************************************
314  *
315  * mirror obd-devices into cl devices.
316  *
317  */
318
319 int cl_sb_init(struct super_block *sb)
320 {
321         struct ll_sb_info *sbi;
322         struct cl_device  *cl;
323         struct lu_env     *env;
324         int rc = 0;
325         __u16 refcheck;
326
327         sbi  = ll_s2sbi(sb);
328         env = cl_env_get(&refcheck);
329         if (!IS_ERR(env)) {
330                 cl = cl_type_setup(env, NULL, &vvp_device_type,
331                                    sbi->ll_dt_exp->exp_obd->obd_lu_dev);
332                 if (!IS_ERR(cl)) {
333                         sbi->ll_cl = cl;
334                         sbi->ll_site = cl2lu_dev(cl)->ld_site;
335                 }
336                 cl_env_put(env, &refcheck);
337         } else
338                 rc = PTR_ERR(env);
339         RETURN(rc);
340 }
341
342 int cl_sb_fini(struct super_block *sb)
343 {
344         struct ll_sb_info *sbi;
345         struct lu_env     *env;
346         struct cl_device  *cld;
347         __u16              refcheck;
348         int                result;
349
350         ENTRY;
351         sbi = ll_s2sbi(sb);
352         env = cl_env_get(&refcheck);
353         if (!IS_ERR(env)) {
354                 cld = sbi->ll_cl;
355
356                 if (cld != NULL) {
357                         cl_stack_fini(env, cld);
358                         sbi->ll_cl = NULL;
359                         sbi->ll_site = NULL;
360                 }
361                 cl_env_put(env, &refcheck);
362                 result = 0;
363         } else {
364                 CERROR("Cannot cleanup cl-stack due to memory shortage.\n");
365                 result = PTR_ERR(env);
366         }
367
368         RETURN(result);
369 }
370
371 /****************************************************************************
372  *
373  * debugfs/lustre/llite/$MNT/dump_page_cache
374  *
375  ****************************************************************************/
376
377 struct vvp_seq_private {
378         struct ll_sb_info       *vsp_sbi;
379         struct lu_env           *vsp_env;
380         u16                     vsp_refcheck;
381         struct cl_object        *vsp_clob;
382         struct rhashtable_iter  vsp_iter;
383         u32                     vsp_page_index;
384         /*
385          * prev_pos is the 'pos' of the last object returned
386          * by ->start of ->next.
387          */
388         loff_t                  vvp_prev_pos;
389 };
390
391 static struct page *vvp_pgcache_current(struct vvp_seq_private *priv)
392 {
393         struct lu_device *dev = &priv->vsp_sbi->ll_cl->cd_lu_dev;
394         struct lu_object_header *h;
395         struct page *vmpage = NULL;
396
397         rhashtable_walk_start(&priv->vsp_iter);
398         while ((h = rhashtable_walk_next(&priv->vsp_iter)) != NULL) {
399                 struct inode *inode;
400                 int nr;
401
402                 if (!priv->vsp_clob) {
403                         struct lu_object *lu_obj;
404
405                         lu_obj = lu_object_get_first(h, dev);
406                         if (!lu_obj)
407                                 continue;
408
409                         priv->vsp_clob = lu2cl(lu_obj);
410                         lu_object_ref_add(lu_obj, "dump", current);
411                         priv->vsp_page_index = 0;
412                 }
413
414                 inode = vvp_object_inode(priv->vsp_clob);
415                 nr = find_get_pages_contig(inode->i_mapping,
416                                            priv->vsp_page_index, 1, &vmpage);
417                 if (nr > 0) {
418                         priv->vsp_page_index = vmpage->index;
419                         break;
420                 }
421                 lu_object_ref_del(&priv->vsp_clob->co_lu, "dump", current);
422                 cl_object_put(priv->vsp_env, priv->vsp_clob);
423                 priv->vsp_clob = NULL;
424                 priv->vsp_page_index = 0;
425         }
426         rhashtable_walk_stop(&priv->vsp_iter);
427         return vmpage;
428 }
429
430 #define seq_page_flag(seq, page, flag, has_flags) do {                  \
431         if (test_bit(PG_##flag, &(page)->flags)) {                  \
432                 seq_printf(seq, "%s"#flag, has_flags ? "|" : "");       \
433                 has_flags = 1;                                          \
434         }                                                               \
435 } while(0)
436
437 static void vvp_pgcache_page_show(const struct lu_env *env,
438                                   struct seq_file *seq, struct cl_page *page)
439 {
440         struct vvp_page *vpg;
441         struct page      *vmpage;
442         int              has_flags;
443
444         vpg = cl2vvp_page(cl_page_at(page, &vvp_device_type));
445         vmpage = vpg->vpg_page;
446         seq_printf(seq, " %5i | %p %p %s %s %s | %p "DFID"(%p) %lu %u [",
447                    0 /* gen */,
448                    vpg, page,
449                    "none",
450                    vpg->vpg_defer_uptodate ? "du" : "- ",
451                    PageWriteback(vmpage) ? "wb" : "-",
452                    vmpage,
453                    PFID(ll_inode2fid(vmpage->mapping->host)),
454                    vmpage->mapping->host, vmpage->index,
455                    page_count(vmpage));
456         has_flags = 0;
457         seq_page_flag(seq, vmpage, locked, has_flags);
458         seq_page_flag(seq, vmpage, error, has_flags);
459         seq_page_flag(seq, vmpage, referenced, has_flags);
460         seq_page_flag(seq, vmpage, uptodate, has_flags);
461         seq_page_flag(seq, vmpage, dirty, has_flags);
462         seq_page_flag(seq, vmpage, writeback, has_flags);
463         seq_printf(seq, "%s]\n", has_flags ? "" : "-");
464 }
465
466 static int vvp_pgcache_show(struct seq_file *f, void *v)
467 {
468         struct vvp_seq_private *priv = f->private;
469         struct page *vmpage = v;
470         struct cl_page *page;
471
472         seq_printf(f, "%8lx@" DFID ": ", vmpage->index,
473                    PFID(lu_object_fid(&priv->vsp_clob->co_lu)));
474         lock_page(vmpage);
475         page = cl_vmpage_page(vmpage, priv->vsp_clob);
476         unlock_page(vmpage);
477         put_page(vmpage);
478
479         if (page) {
480                 vvp_pgcache_page_show(priv->vsp_env, f, page);
481                 cl_page_put(priv->vsp_env, page);
482         } else {
483                 seq_puts(f, "missing\n");
484         }
485
486         return 0;
487 }
488
489 static void vvp_pgcache_rewind(struct vvp_seq_private *priv)
490 {
491         if (priv->vvp_prev_pos) {
492                 struct lu_site *s = priv->vsp_sbi->ll_cl->cd_lu_dev.ld_site;
493
494                 rhashtable_walk_exit(&priv->vsp_iter);
495                 rhashtable_walk_enter(&s->ls_obj_hash, &priv->vsp_iter);
496                 priv->vvp_prev_pos = 0;
497                 if (priv->vsp_clob) {
498                         lu_object_ref_del(&priv->vsp_clob->co_lu, "dump",
499                                           current);
500                         cl_object_put(priv->vsp_env, priv->vsp_clob);
501                 }
502                 priv->vsp_clob = NULL;
503         }
504 }
505
506 static struct page *vvp_pgcache_next_page(struct vvp_seq_private *priv)
507 {
508         priv->vsp_page_index += 1;
509         return vvp_pgcache_current(priv);
510 }
511
512 static void *vvp_pgcache_start(struct seq_file *f, loff_t *pos)
513 {
514         struct vvp_seq_private *priv = f->private;
515
516         if (*pos == 0) {
517                 vvp_pgcache_rewind(priv);
518         } else if (*pos == priv->vvp_prev_pos) {
519                 /* Return the current item */;
520         } else {
521                 WARN_ON(*pos != priv->vvp_prev_pos + 1);
522                 priv->vsp_page_index += 1;
523         }
524
525         priv->vvp_prev_pos = *pos;
526         return vvp_pgcache_current(priv);
527 }
528
529 static void *vvp_pgcache_next(struct seq_file *f, void *v, loff_t *pos)
530 {
531         struct vvp_seq_private *priv = f->private;
532
533         WARN_ON(*pos != priv->vvp_prev_pos);
534         *pos += 1;
535         priv->vvp_prev_pos = *pos;
536         return vvp_pgcache_next_page(priv);
537 }
538
539 static void vvp_pgcache_stop(struct seq_file *f, void *v)
540 {
541         /* Nothing to do */
542 }
543
544 static struct seq_operations vvp_pgcache_ops = {
545         .start = vvp_pgcache_start,
546         .next  = vvp_pgcache_next,
547         .stop  = vvp_pgcache_stop,
548         .show  = vvp_pgcache_show
549 };
550
551 static int vvp_dump_pgcache_seq_open(struct inode *inode, struct file *filp)
552 {
553         struct vvp_seq_private *priv;
554         struct lu_site *s;
555
556         priv = __seq_open_private(filp, &vvp_pgcache_ops, sizeof(*priv));
557         if (!priv)
558                 return -ENOMEM;
559
560         priv->vsp_sbi = inode->i_private;
561         priv->vsp_env = cl_env_get(&priv->vsp_refcheck);
562         priv->vsp_clob = NULL;
563         if (IS_ERR(priv->vsp_env)) {
564                 int err = PTR_ERR(priv->vsp_env);
565
566                 seq_release_private(inode, filp);
567                 return err;
568         }
569
570         s = priv->vsp_sbi->ll_cl->cd_lu_dev.ld_site;
571         rhashtable_walk_enter(&s->ls_obj_hash, &priv->vsp_iter);
572
573         return 0;
574 }
575
576 static int vvp_dump_pgcache_seq_release(struct inode *inode, struct file *file)
577 {
578         struct seq_file *seq = file->private_data;
579         struct vvp_seq_private *priv = seq->private;
580
581         if (priv->vsp_clob) {
582                 lu_object_ref_del(&priv->vsp_clob->co_lu, "dump", current);
583                 cl_object_put(priv->vsp_env, priv->vsp_clob);
584         }
585         cl_env_put(priv->vsp_env, &priv->vsp_refcheck);
586         rhashtable_walk_exit(&priv->vsp_iter);
587         return seq_release_private(inode, file);
588 }
589
590 const struct file_operations vvp_dump_pgcache_file_ops = {
591         .owner   = THIS_MODULE,
592         .open    = vvp_dump_pgcache_seq_open,
593         .read    = seq_read,
594         .llseek  = seq_lseek,
595         .release = vvp_dump_pgcache_seq_release,
596 };