Whamcloud - gitweb
LU-1187 lmv: allocate lmv tgts array by index
[fs/lustre-release.git] / lustre / lmv / lproc_lmv.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) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 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
37 #define DEBUG_SUBSYSTEM S_CLASS
38
39 #include <linux/version.h>
40 #include <linux/seq_file.h>
41 #include <asm/statfs.h>
42 #include <lprocfs_status.h>
43 #include <obd_class.h>
44
45 #ifndef LPROCFS
46 static struct lprocfs_vars lprocfs_module_vars[] = { {0} };
47 static struct lprocfs_vars lprocfs_obd_vars[] = { {0} };
48 #else
49 static int lmv_rd_numobd(char *page, char **start, off_t off, int count,
50                          int *eof, void *data)
51 {
52         struct obd_device       *dev = (struct obd_device*)data;
53         struct lmv_desc         *desc;
54
55         LASSERT(dev != NULL);
56         desc = &dev->u.lmv.desc;
57         *eof = 1;
58         return snprintf(page, count, "%u\n", desc->ld_tgt_count);
59
60 }
61
62 static const char *placement_name[] = {
63         [PLACEMENT_CHAR_POLICY] = "CHAR",
64         [PLACEMENT_NID_POLICY]  = "NID"
65 };
66
67 static placement_policy_t placement_name2policy(char *name, int len)
68 {
69         int                     i;
70
71         for (i = 0; i < PLACEMENT_MAX_POLICY; i++) {
72                 if (!strncmp(placement_name[i], name, len))
73                         return i;
74         }
75         return PLACEMENT_INVAL_POLICY;
76 }
77
78 static const char *placement_policy2name(placement_policy_t placement)
79 {
80         LASSERT(placement < PLACEMENT_MAX_POLICY);
81         return placement_name[placement];
82 }
83
84 static int lmv_rd_placement(char *page, char **start, off_t off, int count,
85                             int *eof, void *data)
86 {
87         struct obd_device       *dev = (struct obd_device*)data;
88         struct lmv_obd          *lmv;
89
90         LASSERT(dev != NULL);
91         lmv = &dev->u.lmv;
92         *eof = 1;
93         return snprintf(page, count, "%s\n",
94                         placement_policy2name(lmv->lmv_placement));
95
96 }
97
98 #define MAX_POLICY_STRING_SIZE 64
99
100 static int lmv_wr_placement(struct file *file, const char *buffer,
101                             unsigned long count, void *data)
102 {
103         struct obd_device       *dev = (struct obd_device *)data;
104         char                     dummy[MAX_POLICY_STRING_SIZE + 1];
105         int                      len = count;
106         placement_policy_t       policy;
107         struct lmv_obd          *lmv;
108
109         if (cfs_copy_from_user(dummy, buffer, MAX_POLICY_STRING_SIZE))
110                 return -EFAULT;
111
112         LASSERT(dev != NULL);
113         lmv = &dev->u.lmv;
114
115         if (len > MAX_POLICY_STRING_SIZE)
116                 len = MAX_POLICY_STRING_SIZE;
117
118         if (dummy[len - 1] == '\n')
119                 len--;
120         dummy[len] = '\0';
121
122         policy = placement_name2policy(dummy, len);
123         if (policy != PLACEMENT_INVAL_POLICY) {
124                 spin_lock(&lmv->lmv_lock);
125                 lmv->lmv_placement = policy;
126                 spin_unlock(&lmv->lmv_lock);
127         } else {
128                 CERROR("Invalid placement policy \"%s\"!\n", dummy);
129                 return -EINVAL;
130         }
131         return count;
132 }
133
134 static int lmv_rd_activeobd(char *page, char **start, off_t off, int count,
135                             int *eof, void *data)
136 {
137         struct obd_device       *dev = (struct obd_device*)data;
138         struct lmv_desc         *desc;
139
140         LASSERT(dev != NULL);
141         desc = &dev->u.lmv.desc;
142         *eof = 1;
143         return snprintf(page, count, "%u\n", desc->ld_active_tgt_count);
144 }
145
146 static int lmv_rd_desc_uuid(char *page, char **start, off_t off, int count,
147                             int *eof, void *data)
148 {
149         struct obd_device       *dev = (struct obd_device*) data;
150         struct lmv_obd          *lmv;
151
152         LASSERT(dev != NULL);
153         lmv = &dev->u.lmv;
154         *eof = 1;
155         return snprintf(page, count, "%s\n", lmv->desc.ld_uuid.uuid);
156 }
157
158 static void *lmv_tgt_seq_start(struct seq_file *p, loff_t *pos)
159 {
160         struct obd_device       *dev = p->private;
161         struct lmv_obd          *lmv = &dev->u.lmv;
162         return (*pos >= lmv->desc.ld_tgt_count) ? NULL : lmv->tgts[*pos];
163 }
164
165 static void lmv_tgt_seq_stop(struct seq_file *p, void *v)
166 {
167         return;
168 }
169
170 static void *lmv_tgt_seq_next(struct seq_file *p, void *v, loff_t *pos)
171 {
172         struct obd_device       *dev = p->private;
173         struct lmv_obd          *lmv = &dev->u.lmv;
174         ++*pos;
175         return (*pos >= lmv->desc.ld_tgt_count) ? NULL : lmv->tgts[*pos];
176 }
177
178 static int lmv_tgt_seq_show(struct seq_file *p, void *v)
179 {
180         struct lmv_tgt_desc     *tgt = v;
181         struct obd_device       *dev = p->private;
182         struct lmv_obd          *lmv = &dev->u.lmv;
183         int                      idx = tgt - lmv->tgts[0];
184
185         if (tgt == NULL)
186                 return 0;
187         return seq_printf(p, "%d: %s %sACTIVE\n", idx, tgt->ltd_uuid.uuid,
188                           tgt->ltd_active ? "" : "IN");
189 }
190
191 struct seq_operations lmv_tgt_sops = {
192         .start                 = lmv_tgt_seq_start,
193         .stop                  = lmv_tgt_seq_stop,
194         .next                  = lmv_tgt_seq_next,
195         .show                  = lmv_tgt_seq_show,
196 };
197
198 static int lmv_target_seq_open(struct inode *inode, struct file *file)
199 {
200         struct proc_dir_entry   *dp = PDE(inode);
201         struct seq_file         *seq;
202         int                     rc;
203
204         rc = seq_open(file, &lmv_tgt_sops);
205         if (rc)
206                 return rc;
207
208         seq = file->private_data;
209         seq->private = dp->data;
210
211         return 0;
212 }
213
214 struct lprocfs_vars lprocfs_lmv_obd_vars[] = {
215         { "numobd",             lmv_rd_numobd,          0, 0 },
216         { "placement",          lmv_rd_placement,       lmv_wr_placement, 0 },
217         { "activeobd",          lmv_rd_activeobd,       0, 0 },
218         { "uuid",               lprocfs_rd_uuid,        0, 0 },
219         { "desc_uuid",          lmv_rd_desc_uuid,       0, 0 },
220         { 0 }
221 };
222
223 static struct lprocfs_vars lprocfs_lmv_module_vars[] = {
224         { "num_refs",           lprocfs_rd_numrefs,     0, 0 },
225         { 0 }
226 };
227
228 struct file_operations lmv_proc_target_fops = {
229         .owner                = THIS_MODULE,
230         .open                 = lmv_target_seq_open,
231         .read                 = seq_read,
232         .llseek               = seq_lseek,
233         .release              = seq_release,
234 };
235
236 #endif /* LPROCFS */
237 void lprocfs_lmv_init_vars(struct lprocfs_static_vars *lvars)
238 {
239         lvars->module_vars    = lprocfs_lmv_module_vars;
240         lvars->obd_vars       = lprocfs_lmv_obd_vars;
241 }