Whamcloud - gitweb
LU-13985 lustre: seq_file .next functions must update *pos
[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
45 #ifdef HAVE_SERVER_SUPPORT
46 #include <dt_object.h>
47 #endif
48 #include <obd_support.h>
49 #include <lustre_fld.h>
50 #include <lustre_fid.h>
51 #include "fld_internal.h"
52
53 static int
54 fld_debugfs_targets_seq_show(struct seq_file *m, void *unused)
55 {
56         struct lu_client_fld *fld = (struct lu_client_fld *)m->private;
57         struct lu_fld_target *target;
58
59         ENTRY;
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
65         RETURN(0);
66 }
67
68 static int
69 fld_debugfs_hash_seq_show(struct seq_file *m, void *unused)
70 {
71         struct lu_client_fld *fld = (struct lu_client_fld *)m->private;
72
73         ENTRY;
74         spin_lock(&fld->lcf_lock);
75         seq_printf(m, "%s\n", fld->lcf_hash->fh_name);
76         spin_unlock(&fld->lcf_lock);
77
78         RETURN(0);
79 }
80
81 static ssize_t
82 fld_debugfs_hash_seq_write(struct file *file, const char __user *buffer,
83                         size_t count, loff_t *off)
84 {
85         struct seq_file *m = file->private_data;
86         struct lu_client_fld *fld = m->private;
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         for (i = 0; fld_hash[i].fh_name; i++) {
98                 if (count != strlen(fld_hash[i].fh_name))
99                         continue;
100
101                 if (!strncmp(fld_hash[i].fh_name, fh_name, count)) {
102                         hash = &fld_hash[i];
103                         break;
104                 }
105         }
106
107         if (hash) {
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 ldebugfs_cache_flush_seq_write(struct file *file,
120                                               const char __user *buffer,
121                                               size_t count, loff_t *pos)
122 {
123         struct seq_file *m = file->private_data;
124         struct lu_client_fld *fld = m->private;
125
126         ENTRY;
127         fld_cache_flush(fld->lcf_cache);
128
129         CDEBUG(D_INFO, "%s: Lookup cache is flushed\n", fld->lcf_name);
130
131         RETURN(count);
132 }
133
134 LDEBUGFS_SEQ_FOPS_RO(fld_debugfs_targets);
135 LDEBUGFS_SEQ_FOPS(fld_debugfs_hash);
136 LDEBUGFS_FOPS_WR_ONLY(fld, cache_flush);
137
138 struct ldebugfs_vars fld_client_debugfs_list[] = {
139         { .name =       "targets",
140           .fops =       &fld_debugfs_targets_fops       },
141         { .name =       "hash",
142           .fops =       &fld_debugfs_hash_fops  },
143         { .name =       "cache_flush",
144           .fops =       &fld_cache_flush_fops   },
145         { NULL }
146 };
147
148 #ifdef HAVE_SERVER_SUPPORT
149 struct fld_seq_param {
150         struct lu_env           fsp_env;
151         struct dt_it            *fsp_it;
152         struct lu_server_fld    *fsp_fld;
153         unsigned int            fsp_stop:1;
154 };
155
156 static void *fldb_seq_start(struct seq_file *p, loff_t *pos)
157 {
158         struct fld_seq_param    *param = p->private;
159         struct lu_server_fld    *fld;
160         struct dt_object        *obj;
161         const struct dt_it_ops  *iops;
162         struct dt_key           *key;
163         int                     rc;
164
165         if (param == NULL || param->fsp_stop)
166                 return NULL;
167
168         fld = param->fsp_fld;
169         obj = fld->lsf_obj;
170         LASSERT(obj != NULL);
171         iops = &obj->do_index_ops->dio_it;
172
173         rc = iops->load(&param->fsp_env, param->fsp_it, *pos);
174         if (rc <= 0)
175                 return NULL;
176
177         key = iops->key(&param->fsp_env, param->fsp_it);
178         if (IS_ERR(key))
179                 return NULL;
180
181         *pos = be64_to_cpu(*(__u64 *)key);
182
183         return param;
184 }
185
186 static void fldb_seq_stop(struct seq_file *p, void *v)
187 {
188         struct fld_seq_param    *param = p->private;
189         const struct dt_it_ops  *iops;
190         struct lu_server_fld    *fld;
191         struct dt_object        *obj;
192
193         if (param == NULL)
194                 return;
195
196         fld = param->fsp_fld;
197         obj = fld->lsf_obj;
198         LASSERT(obj != NULL);
199         iops = &obj->do_index_ops->dio_it;
200
201         iops->put(&param->fsp_env, param->fsp_it);
202 }
203
204 static void *fldb_seq_next(struct seq_file *p, void *v, loff_t *pos)
205 {
206         struct fld_seq_param    *param = p->private;
207         struct lu_server_fld    *fld;
208         struct dt_object        *obj;
209         const struct dt_it_ops  *iops;
210         int                     rc;
211
212         ++*pos;
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                 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 = inode->i_private;
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 = seq_open(file, &fldb_sops);
283         if (rc)
284                 GOTO(out, rc);
285
286         obj = fld->lsf_obj;
287         if (obj == NULL) {
288                 seq = file->private_data;
289                 seq->private = NULL;
290                 return 0;
291         }
292
293         OBD_ALLOC_PTR(param);
294         if (param == NULL)
295                 GOTO(out, rc = -ENOMEM);
296
297         rc = lu_env_init(&param->fsp_env, LCT_MD_THREAD);
298         if (rc != 0)
299                 GOTO(out, rc);
300
301         env_init = 1;
302         iops = &obj->do_index_ops->dio_it;
303         param->fsp_it = iops->init(&param->fsp_env, obj, 0);
304         if (IS_ERR(param->fsp_it))
305                 GOTO(out, rc = PTR_ERR(param->fsp_it));
306
307         param->fsp_fld = fld;
308         param->fsp_stop = 0;
309
310         seq = file->private_data;
311         seq->private = param;
312 out:
313         if (rc != 0) {
314                 if (env_init == 1)
315                         lu_env_fini(&param->fsp_env);
316                 if (param != NULL)
317                         OBD_FREE_PTR(param);
318         }
319         return rc;
320 }
321
322 static int fldb_seq_release(struct inode *inode, struct file *file)
323 {
324         struct seq_file         *seq = file->private_data;
325         struct fld_seq_param    *param;
326         struct lu_server_fld    *fld;
327         struct dt_object        *obj;
328         const struct dt_it_ops  *iops;
329
330         param = seq->private;
331         if (param == NULL) {
332                 lprocfs_seq_release(inode, file);
333                 return 0;
334         }
335
336         fld = param->fsp_fld;
337         obj = fld->lsf_obj;
338         LASSERT(obj != NULL);
339         iops = &obj->do_index_ops->dio_it;
340
341         LASSERT(iops != NULL);
342         LASSERT(param->fsp_it != NULL);
343         iops->fini(&param->fsp_env, param->fsp_it);
344         lu_env_fini(&param->fsp_env);
345         OBD_FREE_PTR(param);
346         lprocfs_seq_release(inode, file);
347
348         return 0;
349 }
350
351 const struct file_operations fld_debugfs_seq_fops = {
352         .owner   = THIS_MODULE,
353         .open    = fldb_seq_open,
354         .read    = seq_read,
355         .release = fldb_seq_release,
356 };
357
358 # endif /* HAVE_SERVER_SUPPORT */