Whamcloud - gitweb
3a985b38686e761a1f85aaf283d090507951ac0c
[fs/lustre-release.git] / lustre / fld / lproc_fld.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) 2007, 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  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/fld/lproc_fld.c
33  *
34  * FLD (FIDs Location Database)
35  *
36  * Author: Yury Umanets <umka@clusterfs.com>
37  *      Di Wang <di.wang@whamcloud.com>
38  */
39
40 #define DEBUG_SUBSYSTEM S_FLD
41
42 #include <libcfs/libcfs.h>
43 #include <linux/module.h>
44 #include <dt_object.h>
45 #include <obd_support.h>
46 #include <lustre_fld.h>
47 #include <lustre_fid.h>
48 #include "fld_internal.h"
49
50 #ifdef CONFIG_PROC_FS
51 static int
52 fld_proc_targets_seq_show(struct seq_file *m, void *unused)
53 {
54         struct lu_client_fld *fld = (struct lu_client_fld *)m->private;
55         struct lu_fld_target *target;
56         ENTRY;
57
58         LASSERT(fld != NULL);
59
60         spin_lock(&fld->lcf_lock);
61         list_for_each_entry(target, &fld->lcf_targets, ft_chain)
62         seq_printf(m, "%s\n", fld_target_name(target));
63         spin_unlock(&fld->lcf_lock);
64         RETURN(0);
65 }
66
67 static int
68 fld_proc_hash_seq_show(struct seq_file *m, void *unused)
69 {
70         struct lu_client_fld *fld = (struct lu_client_fld *)m->private;
71         ENTRY;
72
73         LASSERT(fld != NULL);
74
75         spin_lock(&fld->lcf_lock);
76         seq_printf(m, "%s\n", fld->lcf_hash->fh_name);
77         spin_unlock(&fld->lcf_lock);
78
79         RETURN(0);
80 }
81
82 static ssize_t
83 fld_proc_hash_seq_write(struct file *file, const char __user *buffer,
84                         size_t count, loff_t *off)
85 {
86         struct lu_client_fld *fld;
87         struct lu_fld_hash *hash = NULL;
88         char fh_name[8];
89         int i;
90
91         if (count > sizeof(fh_name))
92                 return -ENAMETOOLONG;
93
94         if (copy_from_user(fh_name, buffer, count) != 0)
95                 return -EFAULT;
96
97         fld = ((struct seq_file *)file->private_data)->private;
98         LASSERT(fld != NULL);
99
100         for (i = 0; fld_hash[i].fh_name != NULL; i++) {
101                 if (count != strlen(fld_hash[i].fh_name))
102                         continue;
103
104                 if (!strncmp(fld_hash[i].fh_name, fh_name, count)) {
105                         hash = &fld_hash[i];
106                         break;
107                 }
108         }
109
110         if (hash != NULL) {
111                 spin_lock(&fld->lcf_lock);
112                 fld->lcf_hash = hash;
113                 spin_unlock(&fld->lcf_lock);
114
115                 CDEBUG(D_INFO, "%s: Changed hash to \"%s\"\n",
116                        fld->lcf_name, hash->fh_name);
117         }
118
119         return count;
120 }
121
122 static ssize_t
123 lprocfs_cache_flush_seq_write(struct file *file, const char __user *buffer,
124                                size_t count, loff_t *pos)
125 {
126         struct lu_client_fld *fld = ((struct seq_file *)file->private_data)->private;
127         ENTRY;
128
129         LASSERT(fld != NULL);
130
131         fld_cache_flush(fld->lcf_cache);
132
133         CDEBUG(D_INFO, "%s: Lookup cache is flushed\n", fld->lcf_name);
134
135         RETURN(count);
136 }
137
138 LPROC_SEQ_FOPS_RO(fld_proc_targets);
139 LPROC_SEQ_FOPS(fld_proc_hash);
140 LPROC_SEQ_FOPS_WR_ONLY(fld, cache_flush);
141
142 struct lprocfs_vars fld_client_proc_list[] = {
143         { .name =       "targets",
144           .fops =       &fld_proc_targets_fops  },
145         { .name =       "hash",
146           .fops =       &fld_proc_hash_fops     },
147         { .name =       "cache_flush",
148           .fops =       &fld_cache_flush_fops   },
149         { NULL }
150 };
151
152 #ifdef HAVE_SERVER_SUPPORT
153 struct fld_seq_param {
154         struct lu_env           fsp_env;
155         struct dt_it            *fsp_it;
156         struct lu_server_fld    *fsp_fld;
157         unsigned int            fsp_stop:1;
158 };
159
160 static void *fldb_seq_start(struct seq_file *p, loff_t *pos)
161 {
162         struct fld_seq_param    *param = p->private;
163         struct lu_server_fld    *fld;
164         struct dt_object        *obj;
165         const struct dt_it_ops  *iops;
166         struct dt_key           *key;
167         int                     rc;
168
169         if (param == NULL || param->fsp_stop)
170                 return NULL;
171
172         fld = param->fsp_fld;
173         obj = fld->lsf_obj;
174         LASSERT(obj != NULL);
175         iops = &obj->do_index_ops->dio_it;
176
177         rc = iops->load(&param->fsp_env, param->fsp_it, *pos);
178         if (rc <= 0)
179                 return NULL;
180
181         key = iops->key(&param->fsp_env, param->fsp_it);
182         if (IS_ERR(key))
183                 return NULL;
184
185         *pos = be64_to_cpu(*(__u64 *)key);
186
187         return param;
188 }
189
190 static void fldb_seq_stop(struct seq_file *p, void *v)
191 {
192         struct fld_seq_param    *param = p->private;
193         const struct dt_it_ops  *iops;
194         struct lu_server_fld    *fld;
195         struct dt_object        *obj;
196
197         if (param == NULL)
198                 return;
199
200         fld = param->fsp_fld;
201         obj = fld->lsf_obj;
202         LASSERT(obj != NULL);
203         iops = &obj->do_index_ops->dio_it;
204
205         iops->put(&param->fsp_env, param->fsp_it);
206 }
207
208 static void *fldb_seq_next(struct seq_file *p, void *v, loff_t *pos)
209 {
210         struct fld_seq_param    *param = p->private;
211         struct lu_server_fld    *fld;
212         struct dt_object        *obj;
213         const struct dt_it_ops  *iops;
214         int                     rc;
215
216         if (param == NULL || param->fsp_stop)
217                 return NULL;
218
219         fld = param->fsp_fld;
220         obj = fld->lsf_obj;
221         LASSERT(obj != NULL);
222         iops = &obj->do_index_ops->dio_it;
223
224         rc = iops->next(&param->fsp_env, param->fsp_it);
225         if (rc > 0) {
226                 param->fsp_stop = 1;
227                 return NULL;
228         }
229
230         *pos = be64_to_cpu(*(__u64 *)iops->key(&param->fsp_env, param->fsp_it));
231         return param;
232 }
233
234 static int fldb_seq_show(struct seq_file *p, void *v)
235 {
236         struct fld_seq_param    *param = p->private;
237         struct lu_server_fld    *fld;
238         struct dt_object        *obj;
239         const struct dt_it_ops  *iops;
240         struct fld_thread_info  *info;
241         struct lu_seq_range     *fld_rec;
242         int                     rc;
243
244         if (param == NULL || param->fsp_stop)
245                 return 0;
246
247         fld = param->fsp_fld;
248         obj = fld->lsf_obj;
249         LASSERT(obj != NULL);
250         iops = &obj->do_index_ops->dio_it;
251
252         info = lu_context_key_get(&param->fsp_env.le_ctx,
253                                   &fld_thread_key);
254         fld_rec = &info->fti_rec;
255         rc = iops->rec(&param->fsp_env, param->fsp_it,
256                        (struct dt_rec *)fld_rec, 0);
257         if (rc != 0) {
258                 CERROR("%s:read record error: rc %d\n",
259                        fld->lsf_name, rc);
260         } else if (fld_rec->lsr_start != 0) {
261                 range_be_to_cpu(fld_rec, fld_rec);
262                 seq_printf(p, DRANGE"\n", PRANGE(fld_rec));
263         }
264
265         return rc;
266 }
267
268 struct seq_operations fldb_sops = {
269         .start = fldb_seq_start,
270         .stop = fldb_seq_stop,
271         .next = fldb_seq_next,
272         .show = fldb_seq_show,
273 };
274
275 static int fldb_seq_open(struct inode *inode, struct file *file)
276 {
277         struct seq_file         *seq;
278         struct lu_server_fld    *fld = (struct lu_server_fld *)PDE_DATA(inode);
279         struct dt_object        *obj;
280         const struct dt_it_ops  *iops;
281         struct fld_seq_param    *param = NULL;
282         int                     env_init = 0;
283         int                     rc;
284
285         rc = LPROCFS_ENTRY_CHECK(inode);
286         if (rc < 0)
287                 return rc;
288
289         rc = seq_open(file, &fldb_sops);
290         if (rc)
291                 GOTO(out, rc);
292
293         obj = fld->lsf_obj;
294         if (obj == NULL) {
295                 seq = file->private_data;
296                 seq->private = NULL;
297                 return 0;
298         }
299
300         OBD_ALLOC_PTR(param);
301         if (param == NULL)
302                 GOTO(out, rc = -ENOMEM);
303
304         rc = lu_env_init(&param->fsp_env, LCT_MD_THREAD);
305         if (rc != 0)
306                 GOTO(out, rc);
307
308         env_init = 1;
309         iops = &obj->do_index_ops->dio_it;
310         param->fsp_it = iops->init(&param->fsp_env, obj, 0);
311         if (IS_ERR(param->fsp_it))
312                 GOTO(out, rc = PTR_ERR(param->fsp_it));
313
314         param->fsp_fld = fld;
315         param->fsp_stop = 0;
316
317         seq = file->private_data;
318         seq->private = param;
319 out:
320         if (rc != 0) {
321                 if (env_init == 1)
322                         lu_env_fini(&param->fsp_env);
323                 if (param != NULL)
324                         OBD_FREE_PTR(param);
325         }
326         return rc;
327 }
328
329 static int fldb_seq_release(struct inode *inode, struct file *file)
330 {
331         struct seq_file         *seq = file->private_data;
332         struct fld_seq_param    *param;
333         struct lu_server_fld    *fld;
334         struct dt_object        *obj;
335         const struct dt_it_ops  *iops;
336
337         param = seq->private;
338         if (param == NULL) {
339                 lprocfs_seq_release(inode, file);
340                 return 0;
341         }
342
343         fld = param->fsp_fld;
344         obj = fld->lsf_obj;
345         LASSERT(obj != NULL);
346         iops = &obj->do_index_ops->dio_it;
347
348         LASSERT(iops != NULL);
349         LASSERT(param->fsp_it != NULL);
350         iops->fini(&param->fsp_env, param->fsp_it);
351         lu_env_fini(&param->fsp_env);
352         OBD_FREE_PTR(param);
353         lprocfs_seq_release(inode, file);
354
355         return 0;
356 }
357
358 const struct file_operations fld_proc_seq_fops = {
359         .owner   = THIS_MODULE,
360         .open    = fldb_seq_open,
361         .read    = seq_read,
362         .release = fldb_seq_release,
363 };
364
365 struct lprocfs_vars fld_server_proc_list[] = {
366         { NULL }
367 };
368
369 # endif /* HAVE_SERVER_SUPPORT */
370
371 #endif /* CONFIG_PROC_FS */