Whamcloud - gitweb
LU-14975 dne: dir migration in non-recursive mode
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * cl_device and cl_device_type implementation for VVP layer.
32  *
33  *   Author: Nikita Danilov <nikita.danilov@sun.com>
34  *   Author: Jinshan Xiong <jinshan.xiong@intel.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_LLITE
38
39 #include <obd.h>
40 #include "llite_internal.h"
41 #include "vvp_internal.h"
42 #include <linux/kallsyms.h>
43
44 /*****************************************************************************
45  *
46  * Vvp device and device type functions.
47  *
48  */
49
50 /*
51  * vvp_ prefix stands for "Vfs Vm Posix". It corresponds to historical
52  * "llite_" (var. "ll_") prefix.
53  */
54
55 static struct kmem_cache *ll_thread_kmem;
56 struct kmem_cache *vvp_object_kmem;
57 static struct kmem_cache *vvp_session_kmem;
58 static struct kmem_cache *vvp_thread_kmem;
59
60 static struct lu_kmem_descr vvp_caches[] = {
61         {
62                 .ckd_cache = &ll_thread_kmem,
63                 .ckd_name  = "ll_thread_kmem",
64                 .ckd_size  = sizeof(struct ll_thread_info),
65         },
66         {
67                 .ckd_cache = &vvp_object_kmem,
68                 .ckd_name  = "vvp_object_kmem",
69                 .ckd_size  = sizeof(struct vvp_object),
70         },
71         {
72                 .ckd_cache = &vvp_session_kmem,
73                 .ckd_name  = "vvp_session_kmem",
74                 .ckd_size  = sizeof (struct vvp_session)
75         },
76         {
77                 .ckd_cache = &vvp_thread_kmem,
78                 .ckd_name  = "vvp_thread_kmem",
79                 .ckd_size  = sizeof(struct vvp_thread_info),
80         },
81         {
82                 .ckd_cache = NULL
83         }
84 };
85
86 static void *ll_thread_key_init(const struct lu_context *ctx,
87                                 struct lu_context_key *key)
88 {
89         struct ll_thread_info *lti;
90
91         OBD_SLAB_ALLOC_PTR_GFP(lti, ll_thread_kmem, GFP_NOFS);
92         if (lti == NULL)
93                 lti = ERR_PTR(-ENOMEM);
94
95         return lti;
96 }
97
98 static void ll_thread_key_fini(const struct lu_context *ctx,
99                                struct lu_context_key *key, void *data)
100 {
101         struct ll_thread_info *lti = data;
102
103         OBD_SLAB_FREE_PTR(lti, ll_thread_kmem);
104 }
105
106 struct lu_context_key ll_thread_key = {
107         .lct_tags = LCT_CL_THREAD,
108         .lct_init = ll_thread_key_init,
109         .lct_fini = ll_thread_key_fini,
110 };
111
112 static void *vvp_session_key_init(const struct lu_context *ctx,
113                                   struct lu_context_key *key)
114 {
115         struct vvp_session *session;
116
117         OBD_SLAB_ALLOC_PTR_GFP(session, vvp_session_kmem, GFP_NOFS);
118         if (session == NULL)
119                 session = ERR_PTR(-ENOMEM);
120         return session;
121 }
122
123 static void vvp_session_key_fini(const struct lu_context *ctx,
124                                  struct lu_context_key *key, void *data)
125 {
126         struct vvp_session *session = data;
127         OBD_SLAB_FREE_PTR(session, vvp_session_kmem);
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 static void *vvp_thread_key_init(const struct lu_context *ctx,
137                                  struct lu_context_key *key)
138 {
139         struct vvp_thread_info *vti;
140
141         OBD_SLAB_ALLOC_PTR_GFP(vti, vvp_thread_kmem, GFP_NOFS);
142         if (vti == NULL)
143                 vti = ERR_PTR(-ENOMEM);
144         return vti;
145 }
146
147 static void vvp_thread_key_fini(const struct lu_context *ctx,
148                                 struct lu_context_key *key, void *data)
149 {
150         struct vvp_thread_info *vti = data;
151         OBD_SLAB_FREE_PTR(vti, vvp_thread_kmem);
152 }
153
154 struct lu_context_key vvp_thread_key = {
155         .lct_tags = LCT_CL_THREAD,
156         .lct_init = vvp_thread_key_init,
157         .lct_fini = vvp_thread_key_fini,
158 };
159
160 /* type constructor/destructor: vvp_type_{init,fini,start,stop}(). */
161 LU_TYPE_INIT_FINI(vvp, &ll_thread_key, &vvp_session_key, &vvp_thread_key);
162
163 static const struct lu_device_operations vvp_lu_ops = {
164         .ldo_object_alloc      = vvp_object_alloc
165 };
166
167 static struct lu_device *vvp_device_free(const struct lu_env *env,
168                                          struct lu_device *d)
169 {
170         struct vvp_device *vdv  = lu2vvp_dev(d);
171         struct cl_site    *site = lu2cl_site(d->ld_site);
172         struct lu_device  *next = cl2lu_dev(vdv->vdv_next);
173
174         if (d->ld_site != NULL) {
175                 cl_site_fini(site);
176                 OBD_FREE_PTR(site);
177         }
178
179         cl_device_fini(lu2cl_dev(d));
180         OBD_FREE_PTR(vdv);
181         return next;
182 }
183
184 static struct lu_device *vvp_device_alloc(const struct lu_env *env,
185                                           struct lu_device_type *t,
186                                           struct lustre_cfg *cfg)
187 {
188         struct vvp_device *vdv;
189         struct lu_device *lud;
190         struct cl_site *site;
191         int rc;
192         ENTRY;
193
194         OBD_ALLOC_PTR(vdv);
195         if (vdv == NULL)
196                 RETURN(ERR_PTR(-ENOMEM));
197
198         lud = &vdv->vdv_cl.cd_lu_dev;
199         cl_device_init(&vdv->vdv_cl, t);
200         vvp2lu_dev(vdv)->ld_ops = &vvp_lu_ops;
201
202         OBD_ALLOC_PTR(site);
203         if (site != NULL) {
204                 rc = cl_site_init(site, &vdv->vdv_cl);
205                 if (rc == 0)
206                         rc = lu_site_init_finish(&site->cs_lu);
207                 else {
208                         LASSERT(lud->ld_site == NULL);
209                         CERROR("Cannot init lu_site, rc %d.\n", rc);
210                         OBD_FREE_PTR(site);
211                 }
212         } else
213                 rc = -ENOMEM;
214         if (rc != 0) {
215                 vvp_device_free(env, lud);
216                 lud = ERR_PTR(rc);
217         }
218         RETURN(lud);
219 }
220
221 static int vvp_device_init(const struct lu_env *env, struct lu_device *d,
222                            const char *name, struct lu_device *next)
223 {
224         struct vvp_device  *vdv;
225         int rc;
226         ENTRY;
227
228         vdv = lu2vvp_dev(d);
229         vdv->vdv_next = lu2cl_dev(next);
230
231         LASSERT(d->ld_site != NULL && next->ld_type != NULL);
232         next->ld_site = d->ld_site;
233         rc = next->ld_type->ldt_ops->ldto_device_init(
234                 env, next, next->ld_type->ldt_name, NULL);
235         if (rc == 0) {
236                 lu_device_get(next);
237                 lu_ref_add(&next->ld_reference, "lu-stack", &lu_site_init);
238         }
239         RETURN(rc);
240 }
241
242 static struct lu_device *vvp_device_fini(const struct lu_env *env,
243                                          struct lu_device *d)
244 {
245         return cl2lu_dev(lu2vvp_dev(d)->vdv_next);
246 }
247
248 static const struct lu_device_type_operations vvp_device_type_ops = {
249         .ldto_init = vvp_type_init,
250         .ldto_fini = vvp_type_fini,
251
252         .ldto_start = vvp_type_start,
253         .ldto_stop  = vvp_type_stop,
254
255         .ldto_device_alloc      = vvp_device_alloc,
256         .ldto_device_free       = vvp_device_free,
257         .ldto_device_init       = vvp_device_init,
258         .ldto_device_fini       = vvp_device_fini,
259 };
260
261 struct lu_device_type vvp_device_type = {
262         .ldt_tags     = LU_DEVICE_CL,
263         .ldt_name     = LUSTRE_VVP_NAME,
264         .ldt_ops      = &vvp_device_type_ops,
265         .ldt_ctx_tags = LCT_CL_THREAD
266 };
267
268 #ifndef HAVE_ACCOUNT_PAGE_DIRTIED_EXPORT
269 unsigned int (*vvp_account_page_dirtied)(struct page *page,
270                                          struct address_space *mapping);
271
272 unsigned int ll_account_page_dirtied(struct page *page,
273                                      struct address_space *mapping)
274 {
275         /* must use __set_page_dirty, which means unlocking and
276          * relocking, which hurts performance.
277          */
278         ll_xa_unlock(&mapping->i_pages);
279         __set_page_dirty(page, mapping, 0);
280         ll_xa_lock(&mapping->i_pages);
281         return 0;
282 }
283 #endif
284
285 /**
286  * A mutex serializing calls to vvp_inode_fini() under extreme memory
287  * pressure, when environments cannot be allocated.
288  */
289 int vvp_global_init(void)
290 {
291         int rc;
292
293         rc = lu_kmem_init(vvp_caches);
294         if (rc != 0)
295                 return rc;
296
297         rc = lu_device_type_init(&vvp_device_type);
298         if (rc != 0)
299                 goto out_kmem;
300
301 #ifndef HAVE_ACCOUNT_PAGE_DIRTIED_EXPORT
302         /*
303          * Kernel v5.2-5678-gac1c3e4 no longer exports account_page_dirtied
304          */
305         vvp_account_page_dirtied = (void *)
306                 cfs_kallsyms_lookup_name("account_page_dirtied");
307         if (!vvp_account_page_dirtied)
308                 vvp_account_page_dirtied = ll_account_page_dirtied;
309 #endif
310
311         return 0;
312
313 out_kmem:
314         lu_kmem_fini(vvp_caches);
315
316         return rc;
317 }
318
319 void vvp_global_fini(void)
320 {
321         lu_device_type_fini(&vvp_device_type);
322         lu_kmem_fini(vvp_caches);
323 }
324
325 /*****************************************************************************
326  *
327  * mirror obd-devices into cl devices.
328  *
329  */
330
331 int cl_sb_init(struct super_block *sb)
332 {
333         struct ll_sb_info *sbi;
334         struct cl_device  *cl;
335         struct lu_env     *env;
336         int rc = 0;
337         __u16 refcheck;
338
339         sbi  = ll_s2sbi(sb);
340         env = cl_env_get(&refcheck);
341         if (!IS_ERR(env)) {
342                 cl = cl_type_setup(env, NULL, &vvp_device_type,
343                                    sbi->ll_dt_exp->exp_obd->obd_lu_dev);
344                 if (!IS_ERR(cl)) {
345                         sbi->ll_cl = cl;
346                         sbi->ll_site = cl2lu_dev(cl)->ld_site;
347                 }
348                 cl_env_put(env, &refcheck);
349         } else
350                 rc = PTR_ERR(env);
351         RETURN(rc);
352 }
353
354 int cl_sb_fini(struct super_block *sb)
355 {
356         struct ll_sb_info *sbi;
357         struct lu_env     *env;
358         struct cl_device  *cld;
359         __u16              refcheck;
360         int                result;
361
362         ENTRY;
363         sbi = ll_s2sbi(sb);
364         env = cl_env_get(&refcheck);
365         if (!IS_ERR(env)) {
366                 cld = sbi->ll_cl;
367
368                 if (cld != NULL) {
369                         cl_stack_fini(env, cld);
370                         sbi->ll_cl = NULL;
371                         sbi->ll_site = NULL;
372                 }
373                 cl_env_put(env, &refcheck);
374                 result = 0;
375         } else {
376                 CERROR("Cannot cleanup cl-stack due to memory shortage.\n");
377                 result = PTR_ERR(env);
378         }
379
380         RETURN(result);
381 }
382
383 /****************************************************************************
384  *
385  * debugfs/lustre/llite/$MNT/dump_page_cache
386  *
387  ****************************************************************************/
388
389 struct vvp_seq_private {
390         struct ll_sb_info       *vsp_sbi;
391         struct lu_env           *vsp_env;
392         u16                     vsp_refcheck;
393         struct cl_object        *vsp_clob;
394         struct rhashtable_iter  vsp_iter;
395         u32                     vsp_page_index;
396         /*
397          * prev_pos is the 'pos' of the last object returned
398          * by ->start of ->next.
399          */
400         loff_t                  vvp_prev_pos;
401 };
402
403 static struct page *vvp_pgcache_current(struct vvp_seq_private *priv)
404 {
405         struct lu_device *dev = &priv->vsp_sbi->ll_cl->cd_lu_dev;
406         struct lu_object_header *h;
407         struct page *vmpage = NULL;
408
409         rhashtable_walk_start(&priv->vsp_iter);
410         while ((h = rhashtable_walk_next(&priv->vsp_iter)) != NULL) {
411                 struct inode *inode;
412                 int nr;
413
414                 if (IS_ERR(h)) {
415                         if (PTR_ERR(h) == -EAGAIN)
416                                 continue;
417                         break;
418                 }
419
420                 if (!priv->vsp_clob) {
421                         struct lu_object *lu_obj;
422
423                         lu_obj = lu_object_get_first(h, dev);
424                         if (!lu_obj)
425                                 continue;
426
427                         priv->vsp_clob = lu2cl(lu_obj);
428                         lu_object_ref_add_atomic(lu_obj, "dump", current);
429                         priv->vsp_page_index = 0;
430                 }
431
432                 inode = vvp_object_inode(priv->vsp_clob);
433                 nr = find_get_pages_contig(inode->i_mapping,
434                                            priv->vsp_page_index, 1, &vmpage);
435                 if (nr > 0) {
436                         priv->vsp_page_index = vmpage->index;
437                         break;
438                 }
439                 lu_object_ref_del(&priv->vsp_clob->co_lu, "dump", current);
440                 cl_object_put(priv->vsp_env, priv->vsp_clob);
441                 priv->vsp_clob = NULL;
442                 priv->vsp_page_index = 0;
443         }
444         rhashtable_walk_stop(&priv->vsp_iter);
445         return vmpage;
446 }
447
448 #define seq_page_flag(seq, page, flag, has_flags) do {                  \
449         if (test_bit(PG_##flag, &(page)->flags)) {                  \
450                 seq_printf(seq, "%s"#flag, has_flags ? "|" : "");       \
451                 has_flags = 1;                                          \
452         }                                                               \
453 } while(0)
454
455 static void vvp_pgcache_page_show(const struct lu_env *env,
456                                   struct seq_file *seq, struct cl_page *page)
457 {
458         struct vvp_page *vpg;
459         struct page      *vmpage;
460         int              has_flags;
461
462         vpg = cl2vvp_page(cl_page_at(page, &vvp_device_type));
463         vmpage = vpg->vpg_page;
464         seq_printf(seq, " %5i | %p %p %s %s %s | %p "DFID"(%p) %lu %u [",
465                    0 /* gen */,
466                    vpg, page,
467                    "none",
468                    vpg->vpg_defer_uptodate ? "du" : "- ",
469                    PageWriteback(vmpage) ? "wb" : "-",
470                    vmpage,
471                    PFID(ll_inode2fid(vmpage->mapping->host)),
472                    vmpage->mapping->host, vmpage->index,
473                    page_count(vmpage));
474         has_flags = 0;
475         seq_page_flag(seq, vmpage, locked, has_flags);
476         seq_page_flag(seq, vmpage, error, has_flags);
477         seq_page_flag(seq, vmpage, referenced, has_flags);
478         seq_page_flag(seq, vmpage, uptodate, has_flags);
479         seq_page_flag(seq, vmpage, dirty, has_flags);
480         seq_page_flag(seq, vmpage, writeback, has_flags);
481         seq_printf(seq, "%s]\n", has_flags ? "" : "-");
482 }
483
484 static int vvp_pgcache_show(struct seq_file *f, void *v)
485 {
486         struct vvp_seq_private *priv = f->private;
487         struct page *vmpage = v;
488         struct cl_page *page;
489
490         seq_printf(f, "%8lx@" DFID ": ", vmpage->index,
491                    PFID(lu_object_fid(&priv->vsp_clob->co_lu)));
492         lock_page(vmpage);
493         page = cl_vmpage_page(vmpage, priv->vsp_clob);
494         unlock_page(vmpage);
495         put_page(vmpage);
496
497         if (page) {
498                 vvp_pgcache_page_show(priv->vsp_env, f, page);
499                 cl_page_put(priv->vsp_env, page);
500         } else {
501                 seq_puts(f, "missing\n");
502         }
503
504         return 0;
505 }
506
507 static void vvp_pgcache_rewind(struct vvp_seq_private *priv)
508 {
509         if (priv->vvp_prev_pos) {
510                 struct lu_site *s = priv->vsp_sbi->ll_cl->cd_lu_dev.ld_site;
511
512                 rhashtable_walk_exit(&priv->vsp_iter);
513                 rhashtable_walk_enter(&s->ls_obj_hash, &priv->vsp_iter);
514                 priv->vvp_prev_pos = 0;
515                 if (priv->vsp_clob) {
516                         lu_object_ref_del(&priv->vsp_clob->co_lu, "dump",
517                                           current);
518                         cl_object_put(priv->vsp_env, priv->vsp_clob);
519                 }
520                 priv->vsp_clob = NULL;
521         }
522 }
523
524 static struct page *vvp_pgcache_next_page(struct vvp_seq_private *priv)
525 {
526         priv->vsp_page_index += 1;
527         return vvp_pgcache_current(priv);
528 }
529
530 static void *vvp_pgcache_start(struct seq_file *f, loff_t *pos)
531 {
532         struct vvp_seq_private *priv = f->private;
533
534         if (*pos == 0) {
535                 vvp_pgcache_rewind(priv);
536         } else if (*pos == priv->vvp_prev_pos) {
537                 /* Return the current item */;
538         } else {
539                 WARN_ON(*pos != priv->vvp_prev_pos + 1);
540                 priv->vsp_page_index += 1;
541         }
542
543         priv->vvp_prev_pos = *pos;
544         return vvp_pgcache_current(priv);
545 }
546
547 static void *vvp_pgcache_next(struct seq_file *f, void *v, loff_t *pos)
548 {
549         struct vvp_seq_private *priv = f->private;
550
551         WARN_ON(*pos != priv->vvp_prev_pos);
552         *pos += 1;
553         priv->vvp_prev_pos = *pos;
554         return vvp_pgcache_next_page(priv);
555 }
556
557 static void vvp_pgcache_stop(struct seq_file *f, void *v)
558 {
559         /* Nothing to do */
560 }
561
562 static const struct seq_operations vvp_pgcache_ops = {
563         .start = vvp_pgcache_start,
564         .next  = vvp_pgcache_next,
565         .stop  = vvp_pgcache_stop,
566         .show  = vvp_pgcache_show
567 };
568
569 static int vvp_dump_pgcache_seq_open(struct inode *inode, struct file *filp)
570 {
571         struct vvp_seq_private *priv;
572         struct lu_site *s;
573
574         priv = __seq_open_private(filp, &vvp_pgcache_ops, sizeof(*priv));
575         if (!priv)
576                 return -ENOMEM;
577
578         priv->vsp_sbi = inode->i_private;
579         priv->vsp_env = cl_env_get(&priv->vsp_refcheck);
580         priv->vsp_clob = NULL;
581         if (IS_ERR(priv->vsp_env)) {
582                 int err = PTR_ERR(priv->vsp_env);
583
584                 seq_release_private(inode, filp);
585                 return err;
586         }
587
588         s = priv->vsp_sbi->ll_cl->cd_lu_dev.ld_site;
589         rhashtable_walk_enter(&s->ls_obj_hash, &priv->vsp_iter);
590
591         return 0;
592 }
593
594 static int vvp_dump_pgcache_seq_release(struct inode *inode, struct file *file)
595 {
596         struct seq_file *seq = file->private_data;
597         struct vvp_seq_private *priv = seq->private;
598
599         if (priv->vsp_clob) {
600                 lu_object_ref_del(&priv->vsp_clob->co_lu, "dump", current);
601                 cl_object_put(priv->vsp_env, priv->vsp_clob);
602         }
603         cl_env_put(priv->vsp_env, &priv->vsp_refcheck);
604         rhashtable_walk_exit(&priv->vsp_iter);
605         return seq_release_private(inode, file);
606 }
607
608 const struct file_operations vvp_dump_pgcache_file_ops = {
609         .owner   = THIS_MODULE,
610         .open    = vvp_dump_pgcache_seq_open,
611         .read    = seq_read,
612         .llseek  = seq_lseek,
613         .release = vvp_dump_pgcache_seq_release,
614 };