staging/lustre/llite: rename ccc_lock to vvp_lock
[cascardo/linux.git] / drivers / staging / lustre / 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 "../include/obd.h"
45 #include "../include/lustre_lite.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         info = kmem_cache_zalloc(vvp_thread_kmem, GFP_NOFS);
96         if (!info)
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
106         kmem_cache_free(vvp_thread_kmem, info);
107 }
108
109 static void *vvp_session_key_init(const struct lu_context *ctx,
110                                   struct lu_context_key *key)
111 {
112         struct vvp_session *session;
113
114         session = kmem_cache_zalloc(vvp_session_kmem, GFP_NOFS);
115         if (!session)
116                 session = ERR_PTR(-ENOMEM);
117         return session;
118 }
119
120 static void vvp_session_key_fini(const struct lu_context *ctx,
121                                  struct lu_context_key *key, void *data)
122 {
123         struct vvp_session *session = data;
124
125         kmem_cache_free(vvp_session_kmem, session);
126 }
127
128 struct lu_context_key vvp_key = {
129         .lct_tags = LCT_CL_THREAD,
130         .lct_init = vvp_key_init,
131         .lct_fini = vvp_key_fini
132 };
133
134 struct lu_context_key vvp_session_key = {
135         .lct_tags = LCT_SESSION,
136         .lct_init = vvp_session_key_init,
137         .lct_fini = vvp_session_key_fini
138 };
139
140 /* type constructor/destructor: vvp_type_{init,fini,start,stop}(). */
141 LU_TYPE_INIT_FINI(vvp, &ccc_key, &ccc_session_key, &vvp_key, &vvp_session_key);
142
143 static const struct lu_device_operations vvp_lu_ops = {
144         .ldo_object_alloc      = vvp_object_alloc
145 };
146
147 static const struct cl_device_operations vvp_cl_ops = {
148         .cdo_req_init = ccc_req_init
149 };
150
151 static struct lu_device *vvp_device_free(const struct lu_env *env,
152                                          struct lu_device *d)
153 {
154         struct vvp_device *vdv  = lu2vvp_dev(d);
155         struct cl_site    *site = lu2cl_site(d->ld_site);
156         struct lu_device  *next = cl2lu_dev(vdv->vdv_next);
157
158         if (d->ld_site) {
159                 cl_site_fini(site);
160                 kfree(site);
161         }
162         cl_device_fini(lu2cl_dev(d));
163         kfree(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
176         vdv = kzalloc(sizeof(*vdv), GFP_NOFS);
177         if (!vdv)
178                 return ERR_PTR(-ENOMEM);
179
180         lud = &vdv->vdv_cl.cd_lu_dev;
181         cl_device_init(&vdv->vdv_cl, t);
182         vvp2lu_dev(vdv)->ld_ops = &vvp_lu_ops;
183         vdv->vdv_cl.cd_ops = &vvp_cl_ops;
184
185         site = kzalloc(sizeof(*site), GFP_NOFS);
186         if (site) {
187                 rc = cl_site_init(site, &vdv->vdv_cl);
188                 if (rc == 0) {
189                         rc = lu_site_init_finish(&site->cs_lu);
190                 } else {
191                         LASSERT(!lud->ld_site);
192                         CERROR("Cannot init lu_site, rc %d.\n", rc);
193                         kfree(site);
194                 }
195         } else {
196                 rc = -ENOMEM;
197         }
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
211         vdv = lu2vvp_dev(d);
212         vdv->vdv_next = lu2cl_dev(next);
213
214         LASSERT(d->ld_site && next->ld_type);
215         next->ld_site = d->ld_site;
216         rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
217                                                       next->ld_type->ldt_name,
218                                                       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  * mirror obd-devices into cl devices.
278  *
279  */
280
281 int cl_sb_init(struct super_block *sb)
282 {
283         struct ll_sb_info *sbi;
284         struct cl_device  *cl;
285         struct lu_env     *env;
286         int rc = 0;
287         int refcheck;
288
289         sbi  = ll_s2sbi(sb);
290         env = cl_env_get(&refcheck);
291         if (!IS_ERR(env)) {
292                 cl = cl_type_setup(env, NULL, &vvp_device_type,
293                                    sbi->ll_dt_exp->exp_obd->obd_lu_dev);
294                 if (!IS_ERR(cl)) {
295                         cl2vvp_dev(cl)->vdv_sb = sb;
296                         sbi->ll_cl = cl;
297                         sbi->ll_site = cl2lu_dev(cl)->ld_site;
298                 }
299                 cl_env_put(env, &refcheck);
300         } else
301                 rc = PTR_ERR(env);
302         return rc;
303 }
304
305 int cl_sb_fini(struct super_block *sb)
306 {
307         struct ll_sb_info *sbi;
308         struct lu_env     *env;
309         struct cl_device  *cld;
310         int             refcheck;
311         int             result;
312
313         sbi = ll_s2sbi(sb);
314         env = cl_env_get(&refcheck);
315         if (!IS_ERR(env)) {
316                 cld = sbi->ll_cl;
317
318                 if (cld) {
319                         cl_stack_fini(env, cld);
320                         sbi->ll_cl = NULL;
321                         sbi->ll_site = NULL;
322                 }
323                 cl_env_put(env, &refcheck);
324                 result = 0;
325         } else {
326                 CERROR("Cannot cleanup cl-stack due to memory shortage.\n");
327                 result = PTR_ERR(env);
328         }
329         /*
330          * If mount failed (sbi->ll_cl == NULL), and this there are no other
331          * mounts, stop device types manually (this usually happens
332          * automatically when last device is destroyed).
333          */
334         lu_types_stop();
335         return result;
336 }
337
338 /****************************************************************************
339  *
340  * debugfs/lustre/llite/$MNT/dump_page_cache
341  *
342  ****************************************************************************/
343
344 /*
345  * To represent contents of a page cache as a byte stream, following
346  * information if encoded in 64bit offset:
347  *
348  *       - file hash bucket in lu_site::ls_hash[]       28bits
349  *
350  *       - how far file is from bucket head           4bits
351  *
352  *       - page index                              32bits
353  *
354  * First two data identify a file in the cache uniquely.
355  */
356
357 #define PGC_OBJ_SHIFT (32 + 4)
358 #define PGC_DEPTH_SHIFT (32)
359
360 struct vvp_pgcache_id {
361         unsigned                 vpi_bucket;
362         unsigned                 vpi_depth;
363         uint32_t                 vpi_index;
364
365         unsigned                 vpi_curdep;
366         struct lu_object_header *vpi_obj;
367 };
368
369 static void vvp_pgcache_id_unpack(loff_t pos, struct vvp_pgcache_id *id)
370 {
371         CLASSERT(sizeof(pos) == sizeof(__u64));
372
373         id->vpi_index  = pos & 0xffffffff;
374         id->vpi_depth  = (pos >> PGC_DEPTH_SHIFT) & 0xf;
375         id->vpi_bucket = (unsigned long long)pos >> PGC_OBJ_SHIFT;
376 }
377
378 static loff_t vvp_pgcache_id_pack(struct vvp_pgcache_id *id)
379 {
380         return
381                 ((__u64)id->vpi_index) |
382                 ((__u64)id->vpi_depth  << PGC_DEPTH_SHIFT) |
383                 ((__u64)id->vpi_bucket << PGC_OBJ_SHIFT);
384 }
385
386 static int vvp_pgcache_obj_get(struct cfs_hash *hs, struct cfs_hash_bd *bd,
387                                struct hlist_node *hnode, void *data)
388 {
389         struct vvp_pgcache_id   *id  = data;
390         struct lu_object_header *hdr = cfs_hash_object(hs, hnode);
391
392         if (id->vpi_curdep-- > 0)
393                 return 0; /* continue */
394
395         if (lu_object_is_dying(hdr))
396                 return 1;
397
398         cfs_hash_get(hs, hnode);
399         id->vpi_obj = hdr;
400         return 1;
401 }
402
403 static struct cl_object *vvp_pgcache_obj(const struct lu_env *env,
404                                          struct lu_device *dev,
405                                          struct vvp_pgcache_id *id)
406 {
407         LASSERT(lu_device_is_cl(dev));
408
409         id->vpi_depth &= 0xf;
410         id->vpi_obj    = NULL;
411         id->vpi_curdep = id->vpi_depth;
412
413         cfs_hash_hlist_for_each(dev->ld_site->ls_obj_hash, id->vpi_bucket,
414                                 vvp_pgcache_obj_get, id);
415         if (id->vpi_obj) {
416                 struct lu_object *lu_obj;
417
418                 lu_obj = lu_object_locate(id->vpi_obj, dev->ld_type);
419                 if (lu_obj) {
420                         lu_object_ref_add(lu_obj, "dump", current);
421                         return lu2cl(lu_obj);
422                 }
423                 lu_object_put(env, lu_object_top(id->vpi_obj));
424
425         } else if (id->vpi_curdep > 0) {
426                 id->vpi_depth = 0xf;
427         }
428         return NULL;
429 }
430
431 static loff_t vvp_pgcache_find(const struct lu_env *env,
432                                struct lu_device *dev, loff_t pos)
433 {
434         struct cl_object     *clob;
435         struct lu_site       *site;
436         struct vvp_pgcache_id id;
437
438         site = dev->ld_site;
439         vvp_pgcache_id_unpack(pos, &id);
440
441         while (1) {
442                 if (id.vpi_bucket >= CFS_HASH_NHLIST(site->ls_obj_hash))
443                         return ~0ULL;
444                 clob = vvp_pgcache_obj(env, dev, &id);
445                 if (clob) {
446                         struct inode *inode = vvp_object_inode(clob);
447                         struct page *vmpage;
448                         int nr;
449
450                         nr = find_get_pages_contig(inode->i_mapping,
451                                                    id.vpi_index, 1, &vmpage);
452                         if (nr > 0) {
453                                 id.vpi_index = vmpage->index;
454                                 /* Cant support over 16T file */
455                                 nr = !(vmpage->index > 0xffffffff);
456                                 page_cache_release(vmpage);
457                         }
458
459                         lu_object_ref_del(&clob->co_lu, "dump", current);
460                         cl_object_put(env, clob);
461                         if (nr > 0)
462                                 return vvp_pgcache_id_pack(&id);
463                 }
464                 /* to the next object. */
465                 ++id.vpi_depth;
466                 id.vpi_depth &= 0xf;
467                 if (id.vpi_depth == 0 && ++id.vpi_bucket == 0)
468                         return ~0ULL;
469                 id.vpi_index = 0;
470         }
471 }
472
473 #define seq_page_flag(seq, page, flag, has_flags) do {            \
474         if (test_bit(PG_##flag, &(page)->flags)) {                \
475                 seq_printf(seq, "%s"#flag, has_flags ? "|" : "");       \
476                 has_flags = 1;                                    \
477         }                                                              \
478 } while (0)
479
480 static void vvp_pgcache_page_show(const struct lu_env *env,
481                                   struct seq_file *seq, struct cl_page *page)
482 {
483         struct vvp_page *vpg;
484         struct page      *vmpage;
485         int           has_flags;
486
487         vpg = cl2vvp_page(cl_page_at(page, &vvp_device_type));
488         vmpage = vpg->vpg_page;
489         seq_printf(seq, " %5i | %p %p %s %s %s %s | %p %lu/%u(%p) %lu %u [",
490                    0 /* gen */,
491                    vpg, page,
492                    "none",
493                    vpg->vpg_write_queued ? "wq" : "- ",
494                    vpg->vpg_defer_uptodate ? "du" : "- ",
495                    PageWriteback(vmpage) ? "wb" : "-",
496                    vmpage, vmpage->mapping->host->i_ino,
497                    vmpage->mapping->host->i_generation,
498                    vmpage->mapping->host, vmpage->index,
499                    page_count(vmpage));
500         has_flags = 0;
501         seq_page_flag(seq, vmpage, locked, has_flags);
502         seq_page_flag(seq, vmpage, error, has_flags);
503         seq_page_flag(seq, vmpage, referenced, has_flags);
504         seq_page_flag(seq, vmpage, uptodate, has_flags);
505         seq_page_flag(seq, vmpage, dirty, has_flags);
506         seq_page_flag(seq, vmpage, writeback, has_flags);
507         seq_printf(seq, "%s]\n", has_flags ? "" : "-");
508 }
509
510 static int vvp_pgcache_show(struct seq_file *f, void *v)
511 {
512         loff_t             pos;
513         struct ll_sb_info       *sbi;
514         struct cl_object        *clob;
515         struct lu_env      *env;
516         struct vvp_pgcache_id    id;
517         int                   refcheck;
518         int                   result;
519
520         env = cl_env_get(&refcheck);
521         if (!IS_ERR(env)) {
522                 pos = *(loff_t *) v;
523                 vvp_pgcache_id_unpack(pos, &id);
524                 sbi = f->private;
525                 clob = vvp_pgcache_obj(env, &sbi->ll_cl->cd_lu_dev, &id);
526                 if (clob) {
527                         struct inode *inode = vvp_object_inode(clob);
528                         struct cl_page *page = NULL;
529                         struct page *vmpage;
530
531                         result = find_get_pages_contig(inode->i_mapping,
532                                                        id.vpi_index, 1,
533                                                        &vmpage);
534                         if (result > 0) {
535                                 lock_page(vmpage);
536                                 page = cl_vmpage_page(vmpage, clob);
537                                 unlock_page(vmpage);
538
539                                 page_cache_release(vmpage);
540                         }
541
542                         seq_printf(f, "%8x@" DFID ": ", id.vpi_index,
543                                    PFID(lu_object_fid(&clob->co_lu)));
544                         if (page) {
545                                 vvp_pgcache_page_show(env, f, page);
546                                 cl_page_put(env, page);
547                         } else
548                                 seq_puts(f, "missing\n");
549                         lu_object_ref_del(&clob->co_lu, "dump", current);
550                         cl_object_put(env, clob);
551                 } else
552                         seq_printf(f, "%llx missing\n", pos);
553                 cl_env_put(env, &refcheck);
554                 result = 0;
555         } else
556                 result = PTR_ERR(env);
557         return result;
558 }
559
560 static void *vvp_pgcache_start(struct seq_file *f, loff_t *pos)
561 {
562         struct ll_sb_info *sbi;
563         struct lu_env     *env;
564         int             refcheck;
565
566         sbi = f->private;
567
568         env = cl_env_get(&refcheck);
569         if (!IS_ERR(env)) {
570                 sbi = f->private;
571                 if (sbi->ll_site->ls_obj_hash->hs_cur_bits > 64 - PGC_OBJ_SHIFT)
572                         pos = ERR_PTR(-EFBIG);
573                 else {
574                         *pos = vvp_pgcache_find(env, &sbi->ll_cl->cd_lu_dev,
575                                                 *pos);
576                         if (*pos == ~0ULL)
577                                 pos = NULL;
578                 }
579                 cl_env_put(env, &refcheck);
580         }
581         return pos;
582 }
583
584 static void *vvp_pgcache_next(struct seq_file *f, void *v, loff_t *pos)
585 {
586         struct ll_sb_info *sbi;
587         struct lu_env     *env;
588         int             refcheck;
589
590         env = cl_env_get(&refcheck);
591         if (!IS_ERR(env)) {
592                 sbi = f->private;
593                 *pos = vvp_pgcache_find(env, &sbi->ll_cl->cd_lu_dev, *pos + 1);
594                 if (*pos == ~0ULL)
595                         pos = NULL;
596                 cl_env_put(env, &refcheck);
597         }
598         return pos;
599 }
600
601 static void vvp_pgcache_stop(struct seq_file *f, void *v)
602 {
603         /* Nothing to do */
604 }
605
606 static const struct seq_operations vvp_pgcache_ops = {
607         .start = vvp_pgcache_start,
608         .next  = vvp_pgcache_next,
609         .stop  = vvp_pgcache_stop,
610         .show  = vvp_pgcache_show
611 };
612
613 static int vvp_dump_pgcache_seq_open(struct inode *inode, struct file *filp)
614 {
615         struct seq_file *seq;
616         int rc;
617
618         rc = seq_open(filp, &vvp_pgcache_ops);
619         if (rc)
620                 return rc;
621
622         seq = filp->private_data;
623         seq->private = inode->i_private;
624
625         return 0;
626 }
627
628 const struct file_operations vvp_dump_pgcache_file_ops = {
629         .owner   = THIS_MODULE,
630         .open    = vvp_dump_pgcache_seq_open,
631         .read    = seq_read,
632         .llseek  = seq_lseek,
633         .release = seq_release,
634 };