Whamcloud - gitweb
LU-2800 autoconf: remove LC_PROCFS_USERS test
[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 #ifdef __KERNEL__
47 # include <libcfs/libcfs.h>
48 # include <linux/module.h>
49 #else /* __KERNEL__ */
50 # include <liblustre.h>
51 #endif
52
53 #include <dt_object.h>
54 #include <obd_support.h>
55 #include <lustre_fld.h>
56 #include <lustre_fid.h>
57 #include "fld_internal.h"
58
59 #ifdef LPROCFS
60 static int
61 fld_proc_read_targets(char *page, char **start, off_t off,
62                       int count, int *eof, void *data)
63 {
64         struct lu_client_fld *fld = (struct lu_client_fld *)data;
65         struct lu_fld_target *target;
66         int total = 0, rc;
67         ENTRY;
68
69         LASSERT(fld != NULL);
70
71         spin_lock(&fld->lcf_lock);
72         cfs_list_for_each_entry(target,
73                                 &fld->lcf_targets, ft_chain)
74         {
75                 rc = snprintf(page, count, "%s\n",
76                               fld_target_name(target));
77                 page += rc;
78                 count -= rc;
79                 total += rc;
80                 if (count == 0)
81                         break;
82         }
83         spin_unlock(&fld->lcf_lock);
84         RETURN(total);
85 }
86
87 static int
88 fld_proc_read_hash(char *page, char **start, off_t off,
89                    int count, int *eof, void *data)
90 {
91         struct lu_client_fld *fld = (struct lu_client_fld *)data;
92         int rc;
93         ENTRY;
94
95         LASSERT(fld != NULL);
96
97         spin_lock(&fld->lcf_lock);
98         rc = snprintf(page, count, "%s\n", fld->lcf_hash->fh_name);
99         spin_unlock(&fld->lcf_lock);
100
101         RETURN(rc);
102 }
103
104 static int
105 fld_proc_write_hash(struct file *file, const char *buffer,
106                     unsigned long count, void *data)
107 {
108         struct lu_client_fld *fld = (struct lu_client_fld *)data;
109         struct lu_fld_hash *hash = NULL;
110         int i;
111         ENTRY;
112
113         LASSERT(fld != NULL);
114
115         for (i = 0; fld_hash[i].fh_name != NULL; i++) {
116                 if (count != strlen(fld_hash[i].fh_name))
117                         continue;
118
119                 if (!strncmp(fld_hash[i].fh_name, buffer, count)) {
120                         hash = &fld_hash[i];
121                         break;
122                 }
123         }
124
125         if (hash != NULL) {
126                 spin_lock(&fld->lcf_lock);
127                 fld->lcf_hash = hash;
128                 spin_unlock(&fld->lcf_lock);
129
130                 CDEBUG(D_INFO, "%s: Changed hash to \"%s\"\n",
131                        fld->lcf_name, hash->fh_name);
132         }
133
134         RETURN(count);
135 }
136
137 static int
138 fld_proc_write_cache_flush(struct file *file, const char *buffer,
139                            unsigned long count, void *data)
140 {
141         struct lu_client_fld *fld = (struct lu_client_fld *)data;
142         ENTRY;
143
144         LASSERT(fld != NULL);
145
146         fld_cache_flush(fld->lcf_cache);
147
148         CDEBUG(D_INFO, "%s: Lookup cache is flushed\n", fld->lcf_name);
149
150         RETURN(count);
151 }
152
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 struct lprocfs_vars fld_client_proc_list[] = {
161         { "targets", fld_proc_read_targets, NULL, NULL },
162         { "hash", fld_proc_read_hash, fld_proc_write_hash, NULL },
163         { "cache_flush", NULL, fld_proc_write_cache_flush, NULL },
164         { NULL }
165 };
166
167 # ifdef HAVE_SERVER_SUPPORT
168 static void *fldb_seq_start(struct seq_file *p, loff_t *pos)
169 {
170         struct fld_seq_param    *param = p->private;
171         struct lu_server_fld    *fld;
172         struct dt_object        *obj;
173         const struct dt_it_ops  *iops;
174
175         if (param == NULL || param->fsp_stop)
176                 return NULL;
177
178         fld = param->fsp_fld;
179         obj = fld->lsf_obj;
180         LASSERT(obj != NULL);
181         iops = &obj->do_index_ops->dio_it;
182
183         iops->load(&param->fsp_env, param->fsp_it, *pos);
184
185         *pos = be64_to_cpu(*(__u64 *)iops->key(&param->fsp_env, param->fsp_it));
186         return param;
187 }
188
189 static void fldb_seq_stop(struct seq_file *p, void *v)
190 {
191         struct fld_seq_param    *param = p->private;
192         const struct dt_it_ops  *iops;
193         struct lu_server_fld    *fld;
194         struct dt_object        *obj;
195
196         if (param == NULL)
197                 return;
198
199         fld = param->fsp_fld;
200         obj = fld->lsf_obj;
201         LASSERT(obj != NULL);
202         iops = &obj->do_index_ops->dio_it;
203
204         iops->put(&param->fsp_env, param->fsp_it);
205 }
206
207 static void *fldb_seq_next(struct seq_file *p, void *v, loff_t *pos)
208 {
209         struct fld_seq_param    *param = p->private;
210         struct lu_server_fld    *fld;
211         struct dt_object        *obj;
212         const struct dt_it_ops  *iops;
213         int                     rc;
214
215         if (param == NULL || param->fsp_stop)
216                 return NULL;
217
218         fld = param->fsp_fld;
219         obj = fld->lsf_obj;
220         LASSERT(obj != NULL);
221         iops = &obj->do_index_ops->dio_it;
222
223         rc = iops->next(&param->fsp_env, param->fsp_it);
224         if (rc > 0) {
225                 param->fsp_stop = 1;
226                 return NULL;
227         }
228
229         *pos = be64_to_cpu(*(__u64 *)iops->key(&param->fsp_env, param->fsp_it));
230         return param;
231 }
232
233 static int fldb_seq_show(struct seq_file *p, void *v)
234 {
235         struct fld_seq_param    *param = p->private;
236         struct lu_server_fld    *fld;
237         struct dt_object        *obj;
238         const struct dt_it_ops  *iops;
239         struct fld_thread_info  *info;
240         struct lu_seq_range     *fld_rec;
241         int                     rc;
242
243         if (param == NULL || param->fsp_stop)
244                 return 0;
245
246         fld = param->fsp_fld;
247         obj = fld->lsf_obj;
248         LASSERT(obj != NULL);
249         iops = &obj->do_index_ops->dio_it;
250
251         info = lu_context_key_get(&param->fsp_env.le_ctx,
252                                   &fld_thread_key);
253         fld_rec = &info->fti_rec;
254         rc = iops->rec(&param->fsp_env, param->fsp_it,
255                        (struct dt_rec *)fld_rec, 0);
256         if (rc != 0) {
257                 CERROR("%s:read record error: rc %d\n",
258                        fld->lsf_name, rc);
259         } else if (fld_rec->lsr_start != 0) {
260                 range_be_to_cpu(fld_rec, fld_rec);
261                 rc = seq_printf(p, DRANGE"\n", PRANGE(fld_rec));
262         }
263
264         return rc;
265 }
266
267 struct seq_operations fldb_sops = {
268         .start = fldb_seq_start,
269         .stop = fldb_seq_stop,
270         .next = fldb_seq_next,
271         .show = fldb_seq_show,
272 };
273
274 static int fldb_seq_open(struct inode *inode, struct file *file)
275 {
276         struct proc_dir_entry   *dp = PDE(inode);
277         struct seq_file         *seq;
278         struct lu_server_fld    *fld = (struct lu_server_fld *)dp->data;
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         LPROCFS_ENTRY_CHECK(dp);
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(obj != NULL);
347         LASSERT(param->fsp_it != NULL);
348         iops->fini(&param->fsp_env, param->fsp_it);
349         lu_env_fini(&param->fsp_env);
350         OBD_FREE_PTR(param);
351         lprocfs_seq_release(inode, file);
352
353         return 0;
354 }
355
356 const struct file_operations fld_proc_seq_fops = {
357         .owner   = THIS_MODULE,
358         .open    = fldb_seq_open,
359         .read    = seq_read,
360         .release = fldb_seq_release,
361 };
362
363 struct lprocfs_vars fld_server_proc_list[] = {
364         { NULL }
365 };
366
367 # endif /* HAVE_SERVER_SUPPORT */
368
369 #endif /* LPROCFS */