Whamcloud - gitweb
bca4997da0e4ef74c4cbaa81a7647836c87043c3
[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, 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  */
40
41 #define DEBUG_SUBSYSTEM S_LLITE
42
43 #ifndef __KERNEL__
44 # error This file is kernel only.
45 #endif
46
47 #include <obd.h>
48 #include <lustre_lite.h>
49
50 #include "vvp_internal.h"
51
52 /*****************************************************************************
53  *
54  * Vvp device and device type functions.
55  *
56  */
57
58 /*
59  * vvp_ prefix stands for "Vfs Vm Posix". It corresponds to historical
60  * "llite_" (var. "ll_") prefix.
61  */
62
63 cfs_mem_cache_t *vvp_thread_kmem;
64 static cfs_mem_cache_t *vvp_session_kmem;
65 static struct lu_kmem_descr vvp_caches[] = {
66         {
67                 .ckd_cache = &vvp_thread_kmem,
68                 .ckd_name  = "vvp_thread_kmem",
69                 .ckd_size  = sizeof (struct vvp_thread_info),
70         },
71         {
72                 .ckd_cache = &vvp_session_kmem,
73                 .ckd_name  = "vvp_session_kmem",
74                 .ckd_size  = sizeof (struct vvp_session)
75         },
76         {
77                 .ckd_cache = NULL
78         }
79 };
80
81 static void *vvp_key_init(const struct lu_context *ctx,
82                           struct lu_context_key *key)
83 {
84         struct vvp_thread_info *info;
85
86         OBD_SLAB_ALLOC_PTR_GFP(info, vvp_thread_kmem, CFS_ALLOC_IO);
87         if (info == NULL)
88                 info = ERR_PTR(-ENOMEM);
89         return info;
90 }
91
92 static void vvp_key_fini(const struct lu_context *ctx,
93                          struct lu_context_key *key, void *data)
94 {
95         struct vvp_thread_info *info = data;
96         OBD_SLAB_FREE_PTR(info, vvp_thread_kmem);
97 }
98
99 static void *vvp_session_key_init(const struct lu_context *ctx,
100                                   struct lu_context_key *key)
101 {
102         struct vvp_session *session;
103
104         OBD_SLAB_ALLOC_PTR_GFP(session, vvp_session_kmem, CFS_ALLOC_IO);
105         if (session == NULL)
106                 session = ERR_PTR(-ENOMEM);
107         return session;
108 }
109
110 static void vvp_session_key_fini(const struct lu_context *ctx,
111                                  struct lu_context_key *key, void *data)
112 {
113         struct vvp_session *session = data;
114         OBD_SLAB_FREE_PTR(session, vvp_session_kmem);
115 }
116
117
118 struct lu_context_key vvp_key = {
119         .lct_tags = LCT_CL_THREAD,
120         .lct_init = vvp_key_init,
121         .lct_fini = vvp_key_fini
122 };
123
124 struct lu_context_key vvp_session_key = {
125         .lct_tags = LCT_SESSION,
126         .lct_init = vvp_session_key_init,
127         .lct_fini = vvp_session_key_fini
128 };
129
130 /* type constructor/destructor: vvp_type_{init,fini,start,stop}(). */
131 LU_TYPE_INIT_FINI(vvp, &ccc_key, &ccc_session_key, &vvp_key, &vvp_session_key);
132
133 static const struct lu_device_operations vvp_lu_ops = {
134         .ldo_object_alloc      = vvp_object_alloc
135 };
136
137 static const struct cl_device_operations vvp_cl_ops = {
138         .cdo_req_init = ccc_req_init
139 };
140
141 static struct lu_device *vvp_device_alloc(const struct lu_env *env,
142                                           struct lu_device_type *t,
143                                           struct lustre_cfg *cfg)
144 {
145         return ccc_device_alloc(env, t, cfg, &vvp_lu_ops, &vvp_cl_ops);
146 }
147
148 static const struct lu_device_type_operations vvp_device_type_ops = {
149         .ldto_init = vvp_type_init,
150         .ldto_fini = vvp_type_fini,
151
152         .ldto_start = vvp_type_start,
153         .ldto_stop  = vvp_type_stop,
154
155         .ldto_device_alloc = vvp_device_alloc,
156         .ldto_device_free  = ccc_device_free,
157         .ldto_device_init  = ccc_device_init,
158         .ldto_device_fini  = ccc_device_fini
159 };
160
161 struct lu_device_type vvp_device_type = {
162         .ldt_tags     = LU_DEVICE_CL,
163         .ldt_name     = LUSTRE_VVP_NAME,
164         .ldt_ops      = &vvp_device_type_ops,
165         .ldt_ctx_tags = LCT_CL_THREAD
166 };
167
168 /**
169  * A mutex serializing calls to vvp_inode_fini() under extreme memory
170  * pressure, when environments cannot be allocated.
171  */
172 int vvp_global_init(void)
173 {
174         int result;
175
176         result = lu_kmem_init(vvp_caches);
177         if (result == 0) {
178                 result = ccc_global_init(&vvp_device_type);
179                 if (result != 0)
180                         lu_kmem_fini(vvp_caches);
181         }
182         return result;
183 }
184
185 void vvp_global_fini(void)
186 {
187         ccc_global_fini(&vvp_device_type);
188         lu_kmem_fini(vvp_caches);
189 }
190
191
192 /*****************************************************************************
193  *
194  * mirror obd-devices into cl devices.
195  *
196  */
197
198 int cl_sb_init(struct super_block *sb)
199 {
200         struct ll_sb_info *sbi;
201         struct cl_device  *cl;
202         struct lu_env     *env;
203         int rc = 0;
204         int refcheck;
205
206         sbi  = ll_s2sbi(sb);
207         env = cl_env_get(&refcheck);
208         if (!IS_ERR(env)) {
209                 cl = cl_type_setup(env, NULL, &vvp_device_type,
210                                    sbi->ll_dt_exp->exp_obd->obd_lu_dev);
211                 if (!IS_ERR(cl)) {
212                         cl2ccc_dev(cl)->cdv_sb = sb;
213                         sbi->ll_cl = cl;
214                         sbi->ll_site = cl2lu_dev(cl)->ld_site;
215                 }
216                 cl_env_put(env, &refcheck);
217         } else
218                 rc = PTR_ERR(env);
219         RETURN(rc);
220 }
221
222 int cl_sb_fini(struct super_block *sb)
223 {
224         struct ll_sb_info *sbi;
225         struct lu_env     *env;
226         struct cl_device  *cld;
227         int                refcheck;
228         int                result;
229
230         ENTRY;
231         sbi = ll_s2sbi(sb);
232         env = cl_env_get(&refcheck);
233         if (!IS_ERR(env)) {
234                 cld = sbi->ll_cl;
235
236                 if (cld != NULL) {
237                         cl_stack_fini(env, cld);
238                         sbi->ll_cl = NULL;
239                         sbi->ll_site = NULL;
240                 }
241                 cl_env_put(env, &refcheck);
242                 result = 0;
243         } else {
244                 CERROR("Cannot cleanup cl-stack due to memory shortage.\n");
245                 result = PTR_ERR(env);
246         }
247         /*
248          * If mount failed (sbi->ll_cl == NULL), and this there are no other
249          * mounts, stop device types manually (this usually happens
250          * automatically when last device is destroyed).
251          */
252         lu_types_stop();
253         RETURN(result);
254 }
255
256 /****************************************************************************
257  *
258  * /proc/fs/lustre/llite/$MNT/dump_page_cache
259  *
260  ****************************************************************************/
261
262 /*
263  * To represent contents of a page cache as a byte stream, following
264  * information if encoded in 64bit offset:
265  *
266  *       - file hash bucket in lu_site::ls_hash[]       28bits
267  *
268  *       - how far file is from bucket head              4bits
269  *
270  *       - page index                                   32bits
271  *
272  * First two data identify a file in the cache uniquely.
273  */
274
275 #define PGC_OBJ_SHIFT (32 + 4)
276 #define PGC_DEPTH_SHIFT (32)
277
278 struct vvp_pgcache_id {
279         unsigned                 vpi_bucket;
280         unsigned                 vpi_depth;
281         uint32_t                 vpi_index;
282
283         unsigned                 vpi_curdep;
284         struct lu_object_header *vpi_obj;
285 };
286
287 static void vvp_pgcache_id_unpack(loff_t pos, struct vvp_pgcache_id *id)
288 {
289         CLASSERT(sizeof(pos) == sizeof(__u64));
290
291         id->vpi_index  = pos & 0xffffffff;
292         id->vpi_depth  = (pos >> PGC_DEPTH_SHIFT) & 0xf;
293         id->vpi_bucket = ((unsigned long long)pos >> PGC_OBJ_SHIFT);
294 }
295
296 static loff_t vvp_pgcache_id_pack(struct vvp_pgcache_id *id)
297 {
298         return
299                 ((__u64)id->vpi_index) |
300                 ((__u64)id->vpi_depth  << PGC_DEPTH_SHIFT) |
301                 ((__u64)id->vpi_bucket << PGC_OBJ_SHIFT);
302 }
303
304 static int vvp_pgcache_obj_get(cfs_hash_t *hs, cfs_hash_bd_t *bd,
305                                cfs_hlist_node_t *hnode, void *data)
306 {
307         struct vvp_pgcache_id   *id  = data;
308         struct lu_object_header *hdr = cfs_hash_object(hs, hnode);
309
310         if (id->vpi_curdep-- > 0)
311                 return 0; /* continue */
312
313         if (lu_object_is_dying(hdr))
314                 return 1;
315
316         cfs_hash_get(hs, hnode);
317         id->vpi_obj = hdr;
318         return 1;
319 }
320
321 static struct cl_object *vvp_pgcache_obj(const struct lu_env *env,
322                                          struct lu_device *dev,
323                                          struct vvp_pgcache_id *id)
324 {
325         LASSERT(lu_device_is_cl(dev));
326
327         id->vpi_depth &= 0xf;
328         id->vpi_obj    = NULL;
329         id->vpi_curdep = id->vpi_depth;
330
331         cfs_hash_hlist_for_each(dev->ld_site->ls_obj_hash, id->vpi_bucket,
332                                 vvp_pgcache_obj_get, id);
333         if (id->vpi_obj != NULL) {
334                 struct lu_object *lu_obj;
335
336                 lu_obj = lu_object_locate(id->vpi_obj, dev->ld_type);
337                 if (lu_obj != NULL) {
338                         lu_object_ref_add(lu_obj, "dump", cfs_current());
339                         return lu2cl(lu_obj);
340                 }
341                 lu_object_put(env, lu_object_top(id->vpi_obj));
342
343         } else if (id->vpi_curdep > 0) {
344                 id->vpi_depth = 0xf;
345         }
346         return NULL;
347 }
348
349 static loff_t vvp_pgcache_find(const struct lu_env *env,
350                                struct lu_device *dev, loff_t pos)
351 {
352         struct cl_object     *clob;
353         struct lu_site       *site;
354         struct vvp_pgcache_id id;
355
356         site = dev->ld_site;
357         vvp_pgcache_id_unpack(pos, &id);
358
359         while (1) {
360                 if (id.vpi_bucket >= CFS_HASH_NHLIST(site->ls_obj_hash))
361                         return ~0ULL;
362                 clob = vvp_pgcache_obj(env, dev, &id);
363                 if (clob != NULL) {
364                         struct cl_object_header *hdr;
365                         int                      nr;
366                         struct cl_page          *pg;
367
368                         /* got an object. Find next page. */
369                         hdr = cl_object_header(clob);
370
371                         spin_lock(&hdr->coh_page_guard);
372                         nr = radix_tree_gang_lookup(&hdr->coh_tree,
373                                                     (void **)&pg,
374                                                     id.vpi_index, 1);
375                         if (nr > 0) {
376                                 id.vpi_index = pg->cp_index;
377                                 /* Cant support over 16T file */
378                                 nr = !(pg->cp_index > 0xffffffff);
379                         }
380                         spin_unlock(&hdr->coh_page_guard);
381
382                         lu_object_ref_del(&clob->co_lu, "dump", cfs_current());
383                         cl_object_put(env, clob);
384                         if (nr > 0)
385                                 return vvp_pgcache_id_pack(&id);
386                 }
387                 /* to the next object. */
388                 ++id.vpi_depth;
389                 id.vpi_depth &= 0xf;
390                 if (id.vpi_depth == 0 && ++id.vpi_bucket == 0)
391                         return ~0ULL;
392                 id.vpi_index = 0;
393         }
394 }
395
396 #define seq_page_flag(seq, page, flag, has_flags) do {                  \
397         if (test_bit(PG_##flag, &(page)->flags)) {                  \
398                 seq_printf(seq, "%s"#flag, has_flags ? "|" : "");       \
399                 has_flags = 1;                                          \
400         }                                                               \
401 } while(0)
402
403 static void vvp_pgcache_page_show(const struct lu_env *env,
404                                   struct seq_file *seq, struct cl_page *page)
405 {
406         struct ccc_page *cpg;
407         cfs_page_t      *vmpage;
408         int              has_flags;
409
410         cpg = cl2ccc_page(cl_page_at(page, &vvp_device_type));
411         vmpage = cpg->cpg_page;
412         seq_printf(seq," %5i | %p %p %s %s %s %s | %p %lu/%u(%p) %lu %u [",
413                    0 /* gen */,
414                    cpg, page,
415                    "none",
416                    cpg->cpg_write_queued ? "wq" : "- ",
417                    cpg->cpg_defer_uptodate ? "du" : "- ",
418                    PageWriteback(vmpage) ? "wb" : "-",
419                    vmpage, vmpage->mapping->host->i_ino,
420                    vmpage->mapping->host->i_generation,
421                    vmpage->mapping->host, vmpage->index,
422                    page_count(vmpage));
423         has_flags = 0;
424         seq_page_flag(seq, vmpage, locked, has_flags);
425         seq_page_flag(seq, vmpage, error, has_flags);
426         seq_page_flag(seq, vmpage, referenced, has_flags);
427         seq_page_flag(seq, vmpage, uptodate, has_flags);
428         seq_page_flag(seq, vmpage, dirty, has_flags);
429         seq_page_flag(seq, vmpage, writeback, has_flags);
430         seq_printf(seq, "%s]\n", has_flags ? "" : "-");
431 }
432
433 static int vvp_pgcache_show(struct seq_file *f, void *v)
434 {
435         loff_t                   pos;
436         struct ll_sb_info       *sbi;
437         struct cl_object        *clob;
438         struct lu_env           *env;
439         struct cl_page          *page;
440         struct cl_object_header *hdr;
441         struct vvp_pgcache_id    id;
442         int                      refcheck;
443         int                      result;
444
445         env = cl_env_get(&refcheck);
446         if (!IS_ERR(env)) {
447                 pos = *(loff_t *) v;
448                 vvp_pgcache_id_unpack(pos, &id);
449                 sbi = f->private;
450                 clob = vvp_pgcache_obj(env, &sbi->ll_cl->cd_lu_dev, &id);
451                 if (clob != NULL) {
452                         hdr = cl_object_header(clob);
453
454                         spin_lock(&hdr->coh_page_guard);
455                         page = cl_page_lookup(hdr, id.vpi_index);
456                         spin_unlock(&hdr->coh_page_guard);
457
458                         seq_printf(f, "%8x@"DFID": ",
459                                    id.vpi_index, PFID(&hdr->coh_lu.loh_fid));
460                         if (page != NULL) {
461                                 vvp_pgcache_page_show(env, f, page);
462                                 cl_page_put(env, page);
463                         } else
464                                 seq_puts(f, "missing\n");
465                         lu_object_ref_del(&clob->co_lu, "dump", cfs_current());
466                         cl_object_put(env, clob);
467                 } else
468                         seq_printf(f, "%llx missing\n", pos);
469                 cl_env_put(env, &refcheck);
470                 result = 0;
471         } else
472                 result = PTR_ERR(env);
473         return result;
474 }
475
476 static void *vvp_pgcache_start(struct seq_file *f, loff_t *pos)
477 {
478         struct ll_sb_info *sbi;
479         struct lu_env     *env;
480         int                refcheck;
481
482         sbi = f->private;
483
484         env = cl_env_get(&refcheck);
485         if (!IS_ERR(env)) {
486                 sbi = f->private;
487                 if (sbi->ll_site->ls_obj_hash->hs_cur_bits > 64 - PGC_OBJ_SHIFT)
488                         pos = ERR_PTR(-EFBIG);
489                 else {
490                         *pos = vvp_pgcache_find(env, &sbi->ll_cl->cd_lu_dev,
491                                                 *pos);
492                         if (*pos == ~0ULL)
493                                 pos = NULL;
494                 }
495                 cl_env_put(env, &refcheck);
496         }
497         return pos;
498 }
499
500 static void *vvp_pgcache_next(struct seq_file *f, void *v, loff_t *pos)
501 {
502         struct ll_sb_info *sbi;
503         struct lu_env     *env;
504         int                refcheck;
505
506         env = cl_env_get(&refcheck);
507         if (!IS_ERR(env)) {
508                 sbi = f->private;
509                 *pos = vvp_pgcache_find(env, &sbi->ll_cl->cd_lu_dev, *pos + 1);
510                 if (*pos == ~0ULL)
511                         pos = NULL;
512                 cl_env_put(env, &refcheck);
513         }
514         return pos;
515 }
516
517 static void vvp_pgcache_stop(struct seq_file *f, void *v)
518 {
519         /* Nothing to do */
520 }
521
522 static struct seq_operations vvp_pgcache_ops = {
523         .start = vvp_pgcache_start,
524         .next  = vvp_pgcache_next,
525         .stop  = vvp_pgcache_stop,
526         .show  = vvp_pgcache_show
527 };
528
529 static int vvp_dump_pgcache_seq_open(struct inode *inode, struct file *filp)
530 {
531         struct proc_dir_entry *dp  = PDE(inode);
532         struct ll_sb_info     *sbi = dp->data;
533         struct seq_file       *seq;
534         int                    result;
535
536         result = seq_open(filp, &vvp_pgcache_ops);
537         if (result == 0) {
538                 seq = filp->private_data;
539                 seq->private = sbi;
540         }
541         return result;
542 }
543
544 struct file_operations vvp_dump_pgcache_file_ops = {
545         .owner   = THIS_MODULE,
546         .open    = vvp_dump_pgcache_seq_open,
547         .read    = seq_read,
548         .llseek  = seq_lseek,
549         .release = seq_release,
550 };