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