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