Whamcloud - gitweb
b=17167 libcfs: ensure all libcfs exported symbols to have cfs_ prefix
[fs/lustre-release.git] / lustre / llite / vvp_dev.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
30  * Use is subject to license terms.
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_page_kmem;
64 cfs_mem_cache_t *vvp_thread_kmem;
65 static cfs_mem_cache_t *vvp_session_kmem;
66 static struct lu_kmem_descr vvp_caches[] = {
67         {
68                 .ckd_cache = &vvp_page_kmem,
69                 .ckd_name  = "vvp_page_kmem",
70                 .ckd_size  = sizeof (struct ccc_page)
71         },
72         {
73                 .ckd_cache = &vvp_thread_kmem,
74                 .ckd_name  = "vvp_thread_kmem",
75                 .ckd_size  = sizeof (struct vvp_thread_info),
76         },
77         {
78                 .ckd_cache = &vvp_session_kmem,
79                 .ckd_name  = "vvp_session_kmem",
80                 .ckd_size  = sizeof (struct vvp_session)
81         },
82         {
83                 .ckd_cache = NULL
84         }
85 };
86
87 static void *vvp_key_init(const struct lu_context *ctx,
88                           struct lu_context_key *key)
89 {
90         struct vvp_thread_info *info;
91
92         OBD_SLAB_ALLOC_PTR_GFP(info, vvp_thread_kmem, CFS_ALLOC_IO);
93         if (info == NULL)
94                 info = ERR_PTR(-ENOMEM);
95         return info;
96 }
97
98 static void vvp_key_fini(const struct lu_context *ctx,
99                          struct lu_context_key *key, void *data)
100 {
101         struct vvp_thread_info *info = data;
102         OBD_SLAB_FREE_PTR(info, vvp_thread_kmem);
103 }
104
105 static void *vvp_session_key_init(const struct lu_context *ctx,
106                                   struct lu_context_key *key)
107 {
108         struct vvp_session *session;
109
110         OBD_SLAB_ALLOC_PTR_GFP(session, vvp_session_kmem, CFS_ALLOC_IO);
111         if (session == NULL)
112                 session = ERR_PTR(-ENOMEM);
113         return session;
114 }
115
116 static void vvp_session_key_fini(const struct lu_context *ctx,
117                                  struct lu_context_key *key, void *data)
118 {
119         struct vvp_session *session = data;
120         OBD_SLAB_FREE_PTR(session, vvp_session_kmem);
121 }
122
123
124 struct lu_context_key vvp_key = {
125         .lct_tags = LCT_CL_THREAD,
126         .lct_init = vvp_key_init,
127         .lct_fini = vvp_key_fini
128 };
129
130 struct lu_context_key vvp_session_key = {
131         .lct_tags = LCT_SESSION,
132         .lct_init = vvp_session_key_init,
133         .lct_fini = vvp_session_key_fini
134 };
135
136 /* type constructor/destructor: vvp_type_{init,fini,start,stop}(). */
137 LU_TYPE_INIT_FINI(vvp, &ccc_key, &ccc_session_key, &vvp_key, &vvp_session_key);
138
139 static const struct lu_device_operations vvp_lu_ops = {
140         .ldo_object_alloc      = vvp_object_alloc
141 };
142
143 static const struct cl_device_operations vvp_cl_ops = {
144         .cdo_req_init = ccc_req_init
145 };
146
147 static struct lu_device *vvp_device_alloc(const struct lu_env *env,
148                                           struct lu_device_type *t,
149                                           struct lustre_cfg *cfg)
150 {
151         return ccc_device_alloc(env, t, cfg, &vvp_lu_ops, &vvp_cl_ops);
152 }
153
154 static const struct lu_device_type_operations vvp_device_type_ops = {
155         .ldto_init = vvp_type_init,
156         .ldto_fini = vvp_type_fini,
157
158         .ldto_start = vvp_type_start,
159         .ldto_stop  = vvp_type_stop,
160
161         .ldto_device_alloc = vvp_device_alloc,
162         .ldto_device_free  = ccc_device_free,
163         .ldto_device_init  = ccc_device_init,
164         .ldto_device_fini  = ccc_device_fini
165 };
166
167 struct lu_device_type vvp_device_type = {
168         .ldt_tags     = LU_DEVICE_CL,
169         .ldt_name     = LUSTRE_VVP_NAME,
170         .ldt_ops      = &vvp_device_type_ops,
171         .ldt_ctx_tags = LCT_CL_THREAD
172 };
173
174 /**
175  * A mutex serializing calls to vvp_inode_fini() under extreme memory
176  * pressure, when environments cannot be allocated.
177  */
178 int vvp_global_init(void)
179 {
180         int result;
181
182         result = lu_kmem_init(vvp_caches);
183         if (result == 0) {
184                 result = ccc_global_init(&vvp_device_type);
185                 if (result != 0)
186                         lu_kmem_fini(vvp_caches);
187         }
188         return result;
189 }
190
191 void vvp_global_fini(void)
192 {
193         ccc_global_fini(&vvp_device_type);
194         lu_kmem_fini(vvp_caches);
195 }
196
197
198 /*****************************************************************************
199  *
200  * mirror obd-devices into cl devices.
201  *
202  */
203
204 int cl_sb_init(struct super_block *sb)
205 {
206         struct ll_sb_info *sbi;
207         struct cl_device  *cl;
208         struct lu_env     *env;
209         int rc = 0;
210         int refcheck;
211
212         sbi  = ll_s2sbi(sb);
213         env = cl_env_get(&refcheck);
214         if (!IS_ERR(env)) {
215                 cl = cl_type_setup(env, NULL, &vvp_device_type,
216                                    sbi->ll_dt_exp->exp_obd->obd_lu_dev);
217                 if (!IS_ERR(cl)) {
218                         cl2ccc_dev(cl)->cdv_sb = sb;
219                         sbi->ll_cl = cl;
220                         sbi->ll_site = cl2lu_dev(cl)->ld_site;
221                 }
222                 cl_env_put(env, &refcheck);
223         } else
224                 rc = PTR_ERR(env);
225         RETURN(rc);
226 }
227
228 int cl_sb_fini(struct super_block *sb)
229 {
230         struct ll_sb_info *sbi;
231         struct lu_env     *env;
232         struct cl_device  *cld;
233         int                refcheck;
234         int                result;
235
236         ENTRY;
237         sbi = ll_s2sbi(sb);
238         env = cl_env_get(&refcheck);
239         if (!IS_ERR(env)) {
240                 cld = sbi->ll_cl;
241
242                 if (cld != NULL) {
243                         cl_stack_fini(env, cld);
244                         sbi->ll_cl = NULL;
245                         sbi->ll_site = NULL;
246                 }
247                 cl_env_put(env, &refcheck);
248                 result = 0;
249         } else {
250                 CERROR("Cannot cleanup cl-stack due to memory shortage.\n");
251                 result = PTR_ERR(env);
252         }
253         /*
254          * If mount failed (sbi->ll_cl == NULL), and this there are no other
255          * mounts, stop device types manually (this usually happens
256          * automatically when last device is destroyed).
257          */
258         lu_types_stop();
259         RETURN(result);
260 }
261
262 /****************************************************************************
263  *
264  * /proc/fs/lustre/llite/$MNT/dump_page_cache
265  *
266  ****************************************************************************/
267
268 /*
269  * To represent contents of a page cache as a byte stream, following
270  * information if encoded in 64bit offset:
271  *
272  *       - file hash bucket in lu_site::ls_hash[]       28bits
273  *
274  *       - how far file is from bucket head              4bits
275  *
276  *       - page index                                   32bits
277  *
278  * First two data identify a file in the cache uniquely.
279  */
280
281 #define PGC_OBJ_SHIFT (32 + 4)
282 #define PGC_DEPTH_SHIFT (32)
283
284 struct vvp_pgcache_id {
285         unsigned vpi_bucket;
286         unsigned vpi_depth;
287         uint32_t vpi_index;
288 };
289
290 static void vvp_pgcache_id_unpack(loff_t pos, struct vvp_pgcache_id *id)
291 {
292         CLASSERT(sizeof(pos) == sizeof(__u64));
293
294         id->vpi_index  = pos & 0xffffffff;
295         id->vpi_depth  = (pos >> PGC_DEPTH_SHIFT) & 0xf;
296         id->vpi_bucket = ((unsigned long long)pos >> PGC_OBJ_SHIFT);
297 }
298
299 static loff_t vvp_pgcache_id_pack(struct vvp_pgcache_id *id)
300 {
301         return
302                 ((__u64)id->vpi_index) |
303                 ((__u64)id->vpi_depth  << PGC_DEPTH_SHIFT) |
304                 ((__u64)id->vpi_bucket << PGC_OBJ_SHIFT);
305 }
306
307 static struct cl_object *vvp_pgcache_obj(const struct lu_env *env,
308                                          struct lu_device *dev,
309                                          struct vvp_pgcache_id *id)
310 {
311         cfs_hlist_head_t        *bucket;
312         struct lu_object_header *hdr;
313         struct lu_site          *site;
314         cfs_hlist_node_t        *scan;
315         struct lu_object_header *found;
316         struct cl_object        *clob;
317         unsigned                 depth;
318
319         LASSERT(lu_device_is_cl(dev));
320
321         site   = dev->ld_site;
322         bucket = site->ls_hash + (id->vpi_bucket & site->ls_hash_mask);
323         depth  = id->vpi_depth & 0xf;
324         found  = NULL;
325         clob   = NULL;
326
327         /* XXX copy of lu_object.c:htable_lookup() */
328         cfs_read_lock(&site->ls_guard);
329         cfs_hlist_for_each_entry(hdr, scan, bucket, loh_hash) {
330                 if (depth-- == 0) {
331                         if (!lu_object_is_dying(hdr)) {
332                                 if (cfs_atomic_add_return(1,
333                                                           &hdr->loh_ref) == 1)
334                                         ++ site->ls_busy;
335                                 found = hdr;
336                         }
337                         break;
338                 }
339         }
340         cfs_read_unlock(&site->ls_guard);
341
342         if (found != NULL) {
343                 struct lu_object *lu_obj;
344
345                 lu_obj = lu_object_locate(found, dev->ld_type);
346                 if (lu_obj != NULL) {
347                         lu_object_ref_add(lu_obj, "dump", cfs_current());
348                         clob = lu2cl(lu_obj);
349                 } else
350                         lu_object_put(env, lu_object_top(found));
351         } else if (depth > 0)
352                 id->vpi_depth = 0xf;
353         return clob;
354 }
355
356 static loff_t vvp_pgcache_find(const struct lu_env *env,
357                                struct lu_device *dev, loff_t pos)
358 {
359         struct cl_object     *clob;
360         struct lu_site       *site;
361         struct vvp_pgcache_id id;
362
363         site = dev->ld_site;
364         vvp_pgcache_id_unpack(pos, &id);
365
366         while (1) {
367                 if (id.vpi_bucket >= site->ls_hash_size)
368                         return ~0ULL;
369                 clob = vvp_pgcache_obj(env, dev, &id);
370                 if (clob != NULL) {
371                         struct cl_object_header *hdr;
372                         int                      nr;
373                         struct cl_page          *pg;
374
375                         /* got an object. Find next page. */
376                         hdr = cl_object_header(clob);
377
378                         cfs_spin_lock(&hdr->coh_page_guard);
379                         nr = radix_tree_gang_lookup(&hdr->coh_tree,
380                                                     (void **)&pg,
381                                                     id.vpi_index, 1);
382                         if (nr > 0) {
383                                 id.vpi_index = pg->cp_index;
384                                 /* Cant support over 16T file */
385                                 nr = !(pg->cp_index > 0xffffffff);
386                         }
387                         cfs_spin_unlock(&hdr->coh_page_guard);
388
389                         lu_object_ref_del(&clob->co_lu, "dump", cfs_current());
390                         cl_object_put(env, clob);
391                         if (nr > 0)
392                                 return vvp_pgcache_id_pack(&id);
393                 }
394                 /* to the next object. */
395                 ++id.vpi_depth;
396                 id.vpi_depth &= 0xf;
397                 if (id.vpi_depth == 0 && ++id.vpi_bucket == 0)
398                         return ~0ULL;
399                 id.vpi_index = 0;
400         }
401 }
402
403 #define seq_page_flag(seq, page, flag, has_flags) do {                  \
404         if (cfs_test_bit(PG_##flag, &(page)->flags)) {                  \
405                 seq_printf(seq, "%s"#flag, has_flags ? "|" : "");       \
406                 has_flags = 1;                                          \
407         }                                                               \
408 } while(0)
409
410 static void vvp_pgcache_page_show(const struct lu_env *env,
411                                   struct seq_file *seq, struct cl_page *page)
412 {
413         struct ccc_page *cpg;
414         cfs_page_t      *vmpage;
415         int              has_flags;
416
417         cpg = cl2ccc_page(cl_page_at(page, &vvp_device_type));
418         vmpage = cpg->cpg_page;
419         seq_printf(seq," %5i | %p %p %s %s %s %s | %p %lu/%u(%p) %lu %u [",
420                    0 /* gen */,
421                    cpg, page,
422                    "none",
423                    cpg->cpg_write_queued ? "wq" : "- ",
424                    cpg->cpg_defer_uptodate ? "du" : "- ",
425                    PageWriteback(vmpage) ? "wb" : "-",
426                    vmpage, vmpage->mapping->host->i_ino,
427                    vmpage->mapping->host->i_generation,
428                    vmpage->mapping->host, vmpage->index,
429                    page_count(vmpage));
430         has_flags = 0;
431         seq_page_flag(seq, vmpage, locked, has_flags);
432         seq_page_flag(seq, vmpage, error, has_flags);
433         seq_page_flag(seq, vmpage, referenced, has_flags);
434         seq_page_flag(seq, vmpage, uptodate, has_flags);
435         seq_page_flag(seq, vmpage, dirty, has_flags);
436 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,12))
437         seq_page_flag(seq, vmpage, highmem, has_flags);
438 #endif
439         seq_page_flag(seq, vmpage, writeback, has_flags);
440         seq_printf(seq, "%s]\n", has_flags ? "" : "-");
441 }
442
443 static int vvp_pgcache_show(struct seq_file *f, void *v)
444 {
445         loff_t                   pos;
446         struct ll_sb_info       *sbi;
447         struct cl_object        *clob;
448         struct lu_env           *env;
449         struct cl_page          *page;
450         struct cl_object_header *hdr;
451         struct vvp_pgcache_id    id;
452         int                      refcheck;
453         int                      result;
454
455         env = cl_env_get(&refcheck);
456         if (!IS_ERR(env)) {
457                 pos = *(loff_t *) v;
458                 vvp_pgcache_id_unpack(pos, &id);
459                 sbi = f->private;
460                 clob = vvp_pgcache_obj(env, &sbi->ll_cl->cd_lu_dev, &id);
461                 if (clob != NULL) {
462                         hdr = cl_object_header(clob);
463
464                         cfs_spin_lock(&hdr->coh_page_guard);
465                         page = cl_page_lookup(hdr, id.vpi_index);
466                         cfs_spin_unlock(&hdr->coh_page_guard);
467
468                         seq_printf(f, "%8x@"DFID": ",
469                                    id.vpi_index, PFID(&hdr->coh_lu.loh_fid));
470                         if (page != NULL) {
471                                 vvp_pgcache_page_show(env, f, page);
472                                 cl_page_put(env, page);
473                         } else
474                                 seq_puts(f, "missing\n");
475                         lu_object_ref_del(&clob->co_lu, "dump", cfs_current());
476                         cl_object_put(env, clob);
477                 } else
478                         seq_printf(f, "%llx missing\n", pos);
479                 cl_env_put(env, &refcheck);
480                 result = 0;
481         } else
482                 result = PTR_ERR(env);
483         return result;
484 }
485
486 static void *vvp_pgcache_start(struct seq_file *f, loff_t *pos)
487 {
488         struct ll_sb_info *sbi;
489         struct lu_env     *env;
490         int                refcheck;
491
492         sbi = f->private;
493
494         env = cl_env_get(&refcheck);
495         if (!IS_ERR(env)) {
496                 sbi = f->private;
497                 if (sbi->ll_site->ls_hash_bits > 64 - PGC_OBJ_SHIFT)
498                         pos = ERR_PTR(-EFBIG);
499                 else {
500                         *pos = vvp_pgcache_find(env, &sbi->ll_cl->cd_lu_dev,
501                                                 *pos);
502                         if (*pos == ~0ULL)
503                                 pos = NULL;
504                 }
505                 cl_env_put(env, &refcheck);
506         }
507         return pos;
508 }
509
510 static void *vvp_pgcache_next(struct seq_file *f, void *v, loff_t *pos)
511 {
512         struct ll_sb_info *sbi;
513         struct lu_env     *env;
514         int                refcheck;
515
516         env = cl_env_get(&refcheck);
517         if (!IS_ERR(env)) {
518                 sbi = f->private;
519                 *pos = vvp_pgcache_find(env, &sbi->ll_cl->cd_lu_dev, *pos + 1);
520                 if (*pos == ~0ULL)
521                         pos = NULL;
522                 cl_env_put(env, &refcheck);
523         }
524         return pos;
525 }
526
527 static void vvp_pgcache_stop(struct seq_file *f, void *v)
528 {
529         /* Nothing to do */
530 }
531
532 static struct seq_operations vvp_pgcache_ops = {
533         .start = vvp_pgcache_start,
534         .next  = vvp_pgcache_next,
535         .stop  = vvp_pgcache_stop,
536         .show  = vvp_pgcache_show
537 };
538
539 static int vvp_dump_pgcache_seq_open(struct inode *inode, struct file *filp)
540 {
541         struct proc_dir_entry *dp  = PDE(inode);
542         struct ll_sb_info     *sbi = dp->data;
543         struct seq_file       *seq;
544         int                    result;
545
546         result = seq_open(filp, &vvp_pgcache_ops);
547         if (result == 0) {
548                 seq = filp->private_data;
549                 seq->private = sbi;
550         }
551         return result;
552 }
553
554 struct file_operations vvp_dump_pgcache_file_ops = {
555         .owner   = THIS_MODULE,
556         .open    = vvp_dump_pgcache_seq_open,
557         .read    = seq_read,
558         .llseek  = seq_lseek,
559         .release = seq_release,
560 };