Whamcloud - gitweb
LU-5162 mdc: Add exception entry check for radix_tree
[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 #include "llite_internal.h"
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 static 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_NOFS);
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_NOFS);
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         RETURN(result);
250 }
251
252 /****************************************************************************
253  *
254  * /proc/fs/lustre/llite/$MNT/dump_page_cache
255  *
256  ****************************************************************************/
257
258 /*
259  * To represent contents of a page cache as a byte stream, following
260  * information if encoded in 64bit offset:
261  *
262  *       - file hash bucket in lu_site::ls_hash[]       28bits
263  *
264  *       - how far file is from bucket head              4bits
265  *
266  *       - page index                                   32bits
267  *
268  * First two data identify a file in the cache uniquely.
269  */
270
271 #define PGC_OBJ_SHIFT (32 + 4)
272 #define PGC_DEPTH_SHIFT (32)
273
274 struct vvp_pgcache_id {
275         unsigned                 vpi_bucket;
276         unsigned                 vpi_depth;
277         uint32_t                 vpi_index;
278
279         unsigned                 vpi_curdep;
280         struct lu_object_header *vpi_obj;
281 };
282
283 static void vvp_pgcache_id_unpack(loff_t pos, struct vvp_pgcache_id *id)
284 {
285         CLASSERT(sizeof(pos) == sizeof(__u64));
286
287         id->vpi_index  = pos & 0xffffffff;
288         id->vpi_depth  = (pos >> PGC_DEPTH_SHIFT) & 0xf;
289         id->vpi_bucket = ((unsigned long long)pos >> PGC_OBJ_SHIFT);
290 }
291
292 static loff_t vvp_pgcache_id_pack(struct vvp_pgcache_id *id)
293 {
294         return
295                 ((__u64)id->vpi_index) |
296                 ((__u64)id->vpi_depth  << PGC_DEPTH_SHIFT) |
297                 ((__u64)id->vpi_bucket << PGC_OBJ_SHIFT);
298 }
299
300 static int vvp_pgcache_obj_get(cfs_hash_t *hs, cfs_hash_bd_t *bd,
301                                struct hlist_node *hnode, void *data)
302 {
303         struct vvp_pgcache_id   *id  = data;
304         struct lu_object_header *hdr = cfs_hash_object(hs, hnode);
305
306         if (id->vpi_curdep-- > 0)
307                 return 0; /* continue */
308
309         if (lu_object_is_dying(hdr))
310                 return 1;
311
312         cfs_hash_get(hs, hnode);
313         id->vpi_obj = hdr;
314         return 1;
315 }
316
317 static struct cl_object *vvp_pgcache_obj(const struct lu_env *env,
318                                          struct lu_device *dev,
319                                          struct vvp_pgcache_id *id)
320 {
321         LASSERT(lu_device_is_cl(dev));
322
323         id->vpi_depth &= 0xf;
324         id->vpi_obj    = NULL;
325         id->vpi_curdep = id->vpi_depth;
326
327         cfs_hash_hlist_for_each(dev->ld_site->ls_obj_hash, id->vpi_bucket,
328                                 vvp_pgcache_obj_get, id);
329         if (id->vpi_obj != NULL) {
330                 struct lu_object *lu_obj;
331
332                 lu_obj = lu_object_locate(id->vpi_obj, dev->ld_type);
333                 if (lu_obj != NULL) {
334                         lu_object_ref_add(lu_obj, "dump", current);
335                         return lu2cl(lu_obj);
336                 }
337                 lu_object_put(env, lu_object_top(id->vpi_obj));
338
339         } else if (id->vpi_curdep > 0) {
340                 id->vpi_depth = 0xf;
341         }
342         return NULL;
343 }
344
345 static loff_t vvp_pgcache_find(const struct lu_env *env,
346                                struct lu_device *dev, loff_t pos)
347 {
348         struct cl_object     *clob;
349         struct lu_site       *site;
350         struct vvp_pgcache_id id;
351
352         site = dev->ld_site;
353         vvp_pgcache_id_unpack(pos, &id);
354
355         while (1) {
356                 if (id.vpi_bucket >= CFS_HASH_NHLIST(site->ls_obj_hash))
357                         return ~0ULL;
358                 clob = vvp_pgcache_obj(env, dev, &id);
359                 if (clob != NULL) {
360                         struct inode *inode = ccc_object_inode(clob);
361                         struct page *vmpage;
362                         int nr;
363
364                         nr = find_get_pages_contig(inode->i_mapping,
365                                                    id.vpi_index, 1, &vmpage);
366                         if (nr > 0) {
367                                 id.vpi_index = vmpage->index;
368                                 /* Cant support over 16T file */
369                                 nr = !(vmpage->index > 0xffffffff);
370                                 page_cache_release(vmpage);
371                         }
372
373                         lu_object_ref_del(&clob->co_lu, "dump", current);
374                         cl_object_put(env, clob);
375                         if (nr > 0)
376                                 return vvp_pgcache_id_pack(&id);
377                 }
378                 /* to the next object. */
379                 ++id.vpi_depth;
380                 id.vpi_depth &= 0xf;
381                 if (id.vpi_depth == 0 && ++id.vpi_bucket == 0)
382                         return ~0ULL;
383                 id.vpi_index = 0;
384         }
385 }
386
387 #define seq_page_flag(seq, page, flag, has_flags) do {                  \
388         if (test_bit(PG_##flag, &(page)->flags)) {                  \
389                 seq_printf(seq, "%s"#flag, has_flags ? "|" : "");       \
390                 has_flags = 1;                                          \
391         }                                                               \
392 } while(0)
393
394 static void vvp_pgcache_page_show(const struct lu_env *env,
395                                   struct seq_file *seq, struct cl_page *page)
396 {
397         struct ccc_page *cpg;
398         struct page      *vmpage;
399         int              has_flags;
400
401         cpg = cl2ccc_page(cl_page_at(page, &vvp_device_type));
402         vmpage = cpg->cpg_page;
403         seq_printf(seq, " %5i | %p %p %s %s %s %s | %p "DFID"(%p) %lu %u [",
404                    0 /* gen */,
405                    cpg, page,
406                    "none",
407                    cpg->cpg_write_queued ? "wq" : "- ",
408                    cpg->cpg_defer_uptodate ? "du" : "- ",
409                    PageWriteback(vmpage) ? "wb" : "-",
410                    vmpage,
411                    PFID(ll_inode2fid(vmpage->mapping->host)),
412                    vmpage->mapping->host, vmpage->index,
413                    page_count(vmpage));
414         has_flags = 0;
415         seq_page_flag(seq, vmpage, locked, has_flags);
416         seq_page_flag(seq, vmpage, error, has_flags);
417         seq_page_flag(seq, vmpage, referenced, has_flags);
418         seq_page_flag(seq, vmpage, uptodate, has_flags);
419         seq_page_flag(seq, vmpage, dirty, has_flags);
420         seq_page_flag(seq, vmpage, writeback, has_flags);
421         seq_printf(seq, "%s]\n", has_flags ? "" : "-");
422 }
423
424 static int vvp_pgcache_show(struct seq_file *f, void *v)
425 {
426         loff_t                   pos;
427         struct ll_sb_info       *sbi;
428         struct cl_object        *clob;
429         struct lu_env           *env;
430         struct vvp_pgcache_id    id;
431         int                      refcheck;
432         int                      result;
433
434         env = cl_env_get(&refcheck);
435         if (!IS_ERR(env)) {
436                 pos = *(loff_t *) v;
437                 vvp_pgcache_id_unpack(pos, &id);
438                 sbi = f->private;
439                 clob = vvp_pgcache_obj(env, &sbi->ll_cl->cd_lu_dev, &id);
440                 if (clob != NULL) {
441                         struct inode *inode = ccc_object_inode(clob);
442                         struct cl_page *page = NULL;
443                         struct page *vmpage;
444
445                         result = find_get_pages_contig(inode->i_mapping,
446                                                       id.vpi_index, 1, &vmpage);
447                         if (result > 0) {
448                                 lock_page(vmpage);
449                                 page = cl_vmpage_page(vmpage, clob);
450                                 unlock_page(vmpage);
451
452                                 page_cache_release(vmpage);
453                         }
454
455                         seq_printf(f, "%8x@"DFID": ", id.vpi_index,
456                                    PFID(lu_object_fid(&clob->co_lu)));
457                         if (page != NULL) {
458                                 vvp_pgcache_page_show(env, f, page);
459                                 cl_page_put(env, page);
460                         } else
461                                 seq_puts(f, "missing\n");
462                         lu_object_ref_del(&clob->co_lu, "dump", current);
463                         cl_object_put(env, clob);
464                 } else
465                         seq_printf(f, "%llx missing\n", pos);
466                 cl_env_put(env, &refcheck);
467                 result = 0;
468         } else
469                 result = PTR_ERR(env);
470         return result;
471 }
472
473 static void *vvp_pgcache_start(struct seq_file *f, loff_t *pos)
474 {
475         struct ll_sb_info *sbi;
476         struct lu_env     *env;
477         int                refcheck;
478
479         sbi = f->private;
480
481         env = cl_env_get(&refcheck);
482         if (!IS_ERR(env)) {
483                 sbi = f->private;
484                 if (sbi->ll_site->ls_obj_hash->hs_cur_bits > 64 - PGC_OBJ_SHIFT)
485                         pos = ERR_PTR(-EFBIG);
486                 else {
487                         *pos = vvp_pgcache_find(env, &sbi->ll_cl->cd_lu_dev,
488                                                 *pos);
489                         if (*pos == ~0ULL)
490                                 pos = NULL;
491                 }
492                 cl_env_put(env, &refcheck);
493         }
494         return pos;
495 }
496
497 static void *vvp_pgcache_next(struct seq_file *f, void *v, loff_t *pos)
498 {
499         struct ll_sb_info *sbi;
500         struct lu_env     *env;
501         int                refcheck;
502
503         env = cl_env_get(&refcheck);
504         if (!IS_ERR(env)) {
505                 sbi = f->private;
506                 *pos = vvp_pgcache_find(env, &sbi->ll_cl->cd_lu_dev, *pos + 1);
507                 if (*pos == ~0ULL)
508                         pos = NULL;
509                 cl_env_put(env, &refcheck);
510         }
511         return pos;
512 }
513
514 static void vvp_pgcache_stop(struct seq_file *f, void *v)
515 {
516         /* Nothing to do */
517 }
518
519 static struct seq_operations vvp_pgcache_ops = {
520         .start = vvp_pgcache_start,
521         .next  = vvp_pgcache_next,
522         .stop  = vvp_pgcache_stop,
523         .show  = vvp_pgcache_show
524 };
525
526 static int vvp_dump_pgcache_seq_open(struct inode *inode, struct file *filp)
527 {
528         struct ll_sb_info       *sbi = PDE_DATA(inode);
529         struct seq_file         *seq;
530         int                     result;
531
532         result = seq_open(filp, &vvp_pgcache_ops);
533         if (result == 0) {
534                 seq = filp->private_data;
535                 seq->private = sbi;
536         }
537         return result;
538 }
539
540 const struct file_operations vvp_dump_pgcache_file_ops = {
541         .owner   = THIS_MODULE,
542         .open    = vvp_dump_pgcache_seq_open,
543         .read    = seq_read,
544         .llseek  = seq_lseek,
545         .release = seq_release,
546 };