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