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