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