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