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