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