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