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