Whamcloud - gitweb
LU-13288 llite: Find account_page_dirtied on module init
[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 /**
270  * A mutex serializing calls to vvp_inode_fini() under extreme memory
271  * pressure, when environments cannot be allocated.
272  */
273 int vvp_global_init(void)
274 {
275         int rc;
276
277         rc = lu_kmem_init(vvp_caches);
278         if (rc != 0)
279                 return rc;
280
281         rc = lu_device_type_init(&vvp_device_type);
282         if (rc != 0)
283                 goto out_kmem;
284
285 #ifndef HAVE_ACCOUNT_PAGE_DIRTIED_EXPORT
286         /*
287          * Kernel v5.2-5678-gac1c3e4 no longer exports account_page_dirtied
288          */
289         vvp_account_page_dirtied = (void *)
290                 kallsyms_lookup_name("account_page_dirtied");
291         BUG_ON(!vvp_account_page_dirtied);
292 #endif
293
294         return 0;
295
296 out_kmem:
297         lu_kmem_fini(vvp_caches);
298
299         return rc;
300 }
301
302 void vvp_global_fini(void)
303 {
304         lu_device_type_fini(&vvp_device_type);
305         lu_kmem_fini(vvp_caches);
306 }
307
308 /*****************************************************************************
309  *
310  * mirror obd-devices into cl devices.
311  *
312  */
313
314 int cl_sb_init(struct super_block *sb)
315 {
316         struct ll_sb_info *sbi;
317         struct cl_device  *cl;
318         struct lu_env     *env;
319         int rc = 0;
320         __u16 refcheck;
321
322         sbi  = ll_s2sbi(sb);
323         env = cl_env_get(&refcheck);
324         if (!IS_ERR(env)) {
325                 cl = cl_type_setup(env, NULL, &vvp_device_type,
326                                    sbi->ll_dt_exp->exp_obd->obd_lu_dev);
327                 if (!IS_ERR(cl)) {
328                         sbi->ll_cl = cl;
329                         sbi->ll_site = cl2lu_dev(cl)->ld_site;
330                 }
331                 cl_env_put(env, &refcheck);
332         } else
333                 rc = PTR_ERR(env);
334         RETURN(rc);
335 }
336
337 int cl_sb_fini(struct super_block *sb)
338 {
339         struct ll_sb_info *sbi;
340         struct lu_env     *env;
341         struct cl_device  *cld;
342         __u16              refcheck;
343         int                result;
344
345         ENTRY;
346         sbi = ll_s2sbi(sb);
347         env = cl_env_get(&refcheck);
348         if (!IS_ERR(env)) {
349                 cld = sbi->ll_cl;
350
351                 if (cld != NULL) {
352                         cl_stack_fini(env, cld);
353                         sbi->ll_cl = NULL;
354                         sbi->ll_site = NULL;
355                 }
356                 cl_env_put(env, &refcheck);
357                 result = 0;
358         } else {
359                 CERROR("Cannot cleanup cl-stack due to memory shortage.\n");
360                 result = PTR_ERR(env);
361         }
362
363         RETURN(result);
364 }
365
366 /****************************************************************************
367  *
368  * debugfs/lustre/llite/$MNT/dump_page_cache
369  *
370  ****************************************************************************/
371
372 struct vvp_pgcache_id {
373         unsigned                 vpi_bucket;
374         unsigned                 vpi_depth;
375         uint32_t                 vpi_index;
376
377         unsigned                 vpi_curdep;
378         struct lu_object_header *vpi_obj;
379 };
380
381 struct vvp_seq_private {
382         struct ll_sb_info       *vsp_sbi;
383         struct lu_env           *vsp_env;
384         u16                     vsp_refcheck;
385         struct cl_object        *vsp_clob;
386         struct vvp_pgcache_id   vvp_id;
387         /*
388          * prev_pos is the 'pos' of the last object returned
389          * by ->start of ->next.
390          */
391         loff_t                  vvp_prev_pos;
392 };
393
394 static int vvp_pgcache_obj_get(struct cfs_hash *hs, struct cfs_hash_bd *bd,
395                                struct hlist_node *hnode, void *data)
396 {
397         struct vvp_pgcache_id   *id  = data;
398         struct lu_object_header *hdr = cfs_hash_object(hs, hnode);
399
400         if (lu_object_is_dying(hdr))
401                 return 0;
402
403         if (id->vpi_curdep-- > 0)
404                 return 0; /* continue */
405
406         cfs_hash_get(hs, hnode);
407         id->vpi_obj = hdr;
408         return 1;
409 }
410
411 static struct cl_object *vvp_pgcache_obj(const struct lu_env *env,
412                                          struct lu_device *dev,
413                                          struct vvp_pgcache_id *id)
414 {
415         LASSERT(lu_device_is_cl(dev));
416
417         id->vpi_obj = NULL;
418         id->vpi_curdep = id->vpi_depth;
419
420         cfs_hash_hlist_for_each(dev->ld_site->ls_obj_hash, id->vpi_bucket,
421                                 vvp_pgcache_obj_get, id);
422         if (id->vpi_obj != NULL) {
423                 struct lu_object *lu_obj;
424
425                 lu_obj = lu_object_locate(id->vpi_obj, dev->ld_type);
426                 if (lu_obj != NULL) {
427                         lu_object_ref_add(lu_obj, "dump", current);
428                         return lu2cl(lu_obj);
429                 }
430                 lu_object_put(env, lu_object_top(id->vpi_obj));
431         }
432         return NULL;
433 }
434
435 static struct page *vvp_pgcache_current(struct vvp_seq_private *priv)
436 {
437         struct lu_device *dev = &priv->vsp_sbi->ll_cl->cd_lu_dev;
438
439         while (1) {
440                 struct inode *inode;
441                 struct page *vmpage;
442                 int nr;
443
444                 if (!priv->vsp_clob) {
445                         struct cl_object *clob;
446
447                         while ((clob = vvp_pgcache_obj(priv->vsp_env, dev, &priv->vvp_id)) == NULL &&
448                                ++(priv->vvp_id.vpi_bucket) < CFS_HASH_NHLIST(dev->ld_site->ls_obj_hash))
449                                 priv->vvp_id.vpi_depth = 0;
450                         if (!clob)
451                                 return NULL;
452                         priv->vsp_clob = clob;
453                         priv->vvp_id.vpi_index = 0;
454                 }
455
456                 inode = vvp_object_inode(priv->vsp_clob);
457                 nr = find_get_pages_contig(inode->i_mapping, priv->vvp_id.vpi_index, 1, &vmpage);
458                 if (nr > 0) {
459                         priv->vvp_id.vpi_index = vmpage->index;
460                         return vmpage;
461                 }
462                 lu_object_ref_del(&priv->vsp_clob->co_lu, "dump", current);
463                 cl_object_put(priv->vsp_env, priv->vsp_clob);
464                 priv->vsp_clob = NULL;
465                 priv->vvp_id.vpi_index = 0;
466                 priv->vvp_id.vpi_depth++;
467         }
468 }
469
470 #define seq_page_flag(seq, page, flag, has_flags) do {                  \
471         if (test_bit(PG_##flag, &(page)->flags)) {                  \
472                 seq_printf(seq, "%s"#flag, has_flags ? "|" : "");       \
473                 has_flags = 1;                                          \
474         }                                                               \
475 } while(0)
476
477 static void vvp_pgcache_page_show(const struct lu_env *env,
478                                   struct seq_file *seq, struct cl_page *page)
479 {
480         struct vvp_page *vpg;
481         struct page      *vmpage;
482         int              has_flags;
483
484         vpg = cl2vvp_page(cl_page_at(page, &vvp_device_type));
485         vmpage = vpg->vpg_page;
486         seq_printf(seq, " %5i | %p %p %s %s %s | %p "DFID"(%p) %lu %u [",
487                    0 /* gen */,
488                    vpg, page,
489                    "none",
490                    vpg->vpg_defer_uptodate ? "du" : "- ",
491                    PageWriteback(vmpage) ? "wb" : "-",
492                    vmpage,
493                    PFID(ll_inode2fid(vmpage->mapping->host)),
494                    vmpage->mapping->host, vmpage->index,
495                    page_count(vmpage));
496         has_flags = 0;
497         seq_page_flag(seq, vmpage, locked, has_flags);
498         seq_page_flag(seq, vmpage, error, has_flags);
499         seq_page_flag(seq, vmpage, referenced, has_flags);
500         seq_page_flag(seq, vmpage, uptodate, has_flags);
501         seq_page_flag(seq, vmpage, dirty, has_flags);
502         seq_page_flag(seq, vmpage, writeback, has_flags);
503         seq_printf(seq, "%s]\n", has_flags ? "" : "-");
504 }
505
506 static int vvp_pgcache_show(struct seq_file *f, void *v)
507 {
508         struct vvp_seq_private *priv = f->private;
509         struct page *vmpage = v;
510         struct cl_page *page;
511
512         seq_printf(f, "%8lx@" DFID ": ", vmpage->index,
513                    PFID(lu_object_fid(&priv->vsp_clob->co_lu)));
514         lock_page(vmpage);
515         page = cl_vmpage_page(vmpage, priv->vsp_clob);
516         unlock_page(vmpage);
517         put_page(vmpage);
518
519         if (page) {
520                 vvp_pgcache_page_show(priv->vsp_env, f, page);
521                 cl_page_put(priv->vsp_env, page);
522         } else {
523                 seq_puts(f, "missing\n");
524         }
525
526         return 0;
527 }
528
529 static void vvp_pgcache_rewind(struct vvp_seq_private *priv)
530 {
531         if (priv->vvp_prev_pos) {
532                 memset(&priv->vvp_id, 0, sizeof(priv->vvp_id));
533                 priv->vvp_prev_pos = 0;
534                 if (priv->vsp_clob) {
535                         lu_object_ref_del(&priv->vsp_clob->co_lu, "dump",
536                                           current);
537                         cl_object_put(priv->vsp_env, priv->vsp_clob);
538                 }
539                 priv->vsp_clob = NULL;
540         }
541 }
542
543 static struct page *vvp_pgcache_next_page(struct vvp_seq_private *priv)
544 {
545         priv->vvp_id.vpi_index += 1;
546         return vvp_pgcache_current(priv);
547 }
548
549 static void *vvp_pgcache_start(struct seq_file *f, loff_t *pos)
550 {
551         struct vvp_seq_private *priv = f->private;
552
553         if (*pos == 0) {
554                 vvp_pgcache_rewind(priv);
555         } else if (*pos == priv->vvp_prev_pos) {
556                 /* Return the current item */;
557         } else {
558                 WARN_ON(*pos != priv->vvp_prev_pos + 1);
559                 priv->vvp_id.vpi_index += 1;
560         }
561
562         priv->vvp_prev_pos = *pos;
563         return vvp_pgcache_current(priv);
564 }
565
566 static void *vvp_pgcache_next(struct seq_file *f, void *v, loff_t *pos)
567 {
568         struct vvp_seq_private *priv = f->private;
569
570         WARN_ON(*pos != priv->vvp_prev_pos);
571         *pos += 1;
572         priv->vvp_prev_pos = *pos;
573         return vvp_pgcache_next_page(priv);
574 }
575
576 static void vvp_pgcache_stop(struct seq_file *f, void *v)
577 {
578         /* Nothing to do */
579 }
580
581 static struct seq_operations vvp_pgcache_ops = {
582         .start = vvp_pgcache_start,
583         .next  = vvp_pgcache_next,
584         .stop  = vvp_pgcache_stop,
585         .show  = vvp_pgcache_show
586 };
587
588 static int vvp_dump_pgcache_seq_open(struct inode *inode, struct file *filp)
589 {
590         struct vvp_seq_private *priv;
591
592         priv = __seq_open_private(filp, &vvp_pgcache_ops, sizeof(*priv));
593         if (!priv)
594                 return -ENOMEM;
595
596         priv->vsp_sbi = inode->i_private;
597         priv->vsp_env = cl_env_get(&priv->vsp_refcheck);
598         priv->vsp_clob = NULL;
599         memset(&priv->vvp_id, 0, sizeof(priv->vvp_id));
600         if (IS_ERR(priv->vsp_env)) {
601                 int err = PTR_ERR(priv->vsp_env);
602
603                 seq_release_private(inode, filp);
604                 return err;
605         }
606
607         return 0;
608 }
609
610 static int vvp_dump_pgcache_seq_release(struct inode *inode, struct file *file)
611 {
612         struct seq_file *seq = file->private_data;
613         struct vvp_seq_private *priv = seq->private;
614
615         if (priv->vsp_clob) {
616                 lu_object_ref_del(&priv->vsp_clob->co_lu, "dump", current);
617                 cl_object_put(priv->vsp_env, priv->vsp_clob);
618         }
619
620         cl_env_put(priv->vsp_env, &priv->vsp_refcheck);
621         return seq_release_private(inode, file);
622 }
623
624 const struct file_operations vvp_dump_pgcache_file_ops = {
625         .owner   = THIS_MODULE,
626         .open    = vvp_dump_pgcache_seq_open,
627         .read    = seq_read,
628         .llseek  = seq_lseek,
629         .release = vvp_dump_pgcache_seq_release,
630 };