staging: lustre: lustre: mdc: lproc_mdc.c: Fix for potential NULL pointer dereference
[cascardo/linux.git] / drivers / staging / lustre / lustre / mdc / lproc_mdc.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) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 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 #define DEBUG_SUBSYSTEM S_CLASS
37
38 #include <linux/vfs.h>
39 #include "../include/obd_class.h"
40 #include "../include/lprocfs_status.h"
41
42 static int mdc_max_rpcs_in_flight_seq_show(struct seq_file *m, void *v)
43 {
44         struct obd_device *dev = m->private;
45         struct client_obd *cli = &dev->u.cli;
46         int rc;
47
48         client_obd_list_lock(&cli->cl_loi_list_lock);
49         rc = seq_printf(m, "%u\n", cli->cl_max_rpcs_in_flight);
50         client_obd_list_unlock(&cli->cl_loi_list_lock);
51         return rc;
52 }
53
54 static ssize_t mdc_max_rpcs_in_flight_seq_write(struct file *file,
55                                                 const char __user *buffer,
56                                                 size_t count,
57                                                 loff_t *off)
58 {
59         struct obd_device *dev =
60                         ((struct seq_file *)file->private_data)->private;
61         struct client_obd *cli = &dev->u.cli;
62         int val, rc;
63
64         rc = lprocfs_write_helper(buffer, count, &val);
65         if (rc)
66                 return rc;
67
68         if (val < 1 || val > MDC_MAX_RIF_MAX)
69                 return -ERANGE;
70
71         client_obd_list_lock(&cli->cl_loi_list_lock);
72         cli->cl_max_rpcs_in_flight = val;
73         client_obd_list_unlock(&cli->cl_loi_list_lock);
74
75         return count;
76 }
77 LPROC_SEQ_FOPS(mdc_max_rpcs_in_flight);
78
79 static int mdc_kuc_open(struct inode *inode, struct file *file)
80 {
81         return single_open(file, NULL, PDE_DATA(inode));
82 }
83
84 /* temporary for testing */
85 static ssize_t mdc_kuc_write(struct file *file,
86                                 const char __user *buffer,
87                                 size_t count, loff_t *off)
88 {
89         struct obd_device *obd =
90                         ((struct seq_file *)file->private_data)->private;
91         struct kuc_hdr          *lh;
92         struct hsm_action_list  *hal;
93         struct hsm_action_item  *hai;
94         int                      len;
95         int                      fd, rc;
96
97         rc = lprocfs_write_helper(buffer, count, &fd);
98         if (rc)
99                 return rc;
100
101         if (fd < 0)
102                 return -ERANGE;
103         CWARN("message to fd %d\n", fd);
104
105         len = sizeof(*lh) + sizeof(*hal) + MTI_NAME_MAXLEN +
106                 /* for mockup below */ 2 * cfs_size_round(sizeof(*hai));
107
108         OBD_ALLOC(lh, len);
109         if (!lh)
110                 return -ENOMEM;
111
112         lh->kuc_magic = KUC_MAGIC;
113         lh->kuc_transport = KUC_TRANSPORT_HSM;
114         lh->kuc_msgtype = HMT_ACTION_LIST;
115         lh->kuc_msglen = len;
116
117         hal = (struct hsm_action_list *)(lh + 1);
118         hal->hal_version = HAL_VERSION;
119         hal->hal_archive_id = 1;
120         hal->hal_flags = 0;
121         obd_uuid2fsname(hal->hal_fsname, obd->obd_name, MTI_NAME_MAXLEN);
122
123         /* mock up an action list */
124         hal->hal_count = 2;
125         hai = hai_zero(hal);
126         hai->hai_action = HSMA_ARCHIVE;
127         hai->hai_fid.f_oid = 5;
128         hai->hai_len = sizeof(*hai);
129         hai = hai_next(hai);
130         hai->hai_action = HSMA_RESTORE;
131         hai->hai_fid.f_oid = 10;
132         hai->hai_len = sizeof(*hai);
133
134         /* This works for either broadcast or unicast to a single fd */
135         if (fd == 0) {
136                 rc = libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
137         } else {
138                 struct file *fp = fget(fd);
139
140                 rc = libcfs_kkuc_msg_put(fp, lh);
141                 fput(fp);
142         }
143         OBD_FREE(lh, len);
144         if (rc < 0)
145                 return rc;
146         return count;
147 }
148
149 struct file_operations mdc_kuc_fops = {
150         .open           = mdc_kuc_open,
151         .write          = mdc_kuc_write,
152         .release        = single_release,
153 };
154
155 LPROC_SEQ_FOPS_WR_ONLY(mdc, ping);
156
157 LPROC_SEQ_FOPS_RO_TYPE(mdc, uuid);
158 LPROC_SEQ_FOPS_RO_TYPE(mdc, connect_flags);
159 LPROC_SEQ_FOPS_RO_TYPE(mdc, blksize);
160 LPROC_SEQ_FOPS_RO_TYPE(mdc, kbytestotal);
161 LPROC_SEQ_FOPS_RO_TYPE(mdc, kbytesfree);
162 LPROC_SEQ_FOPS_RO_TYPE(mdc, kbytesavail);
163 LPROC_SEQ_FOPS_RO_TYPE(mdc, filestotal);
164 LPROC_SEQ_FOPS_RO_TYPE(mdc, filesfree);
165 LPROC_SEQ_FOPS_RO_TYPE(mdc, server_uuid);
166 LPROC_SEQ_FOPS_RO_TYPE(mdc, conn_uuid);
167 LPROC_SEQ_FOPS_RO_TYPE(mdc, timeouts);
168 LPROC_SEQ_FOPS_RO_TYPE(mdc, state);
169
170 static int mdc_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *v)
171 {
172         return lprocfs_obd_rd_max_pages_per_rpc(m, m->private);
173 }
174 LPROC_SEQ_FOPS_RO(mdc_obd_max_pages_per_rpc);
175
176 LPROC_SEQ_FOPS_RW_TYPE(mdc, import);
177 LPROC_SEQ_FOPS_RW_TYPE(mdc, pinger_recov);
178
179 static struct lprocfs_vars lprocfs_mdc_obd_vars[] = {
180         { "uuid",               &mdc_uuid_fops,                 NULL, 0 },
181         { "ping",               &mdc_ping_fops,                 NULL, 0222 },
182         { "connect_flags",      &mdc_connect_flags_fops,        NULL, 0 },
183         { "blocksize",          &mdc_blksize_fops,              NULL, 0 },
184         { "kbytestotal",        &mdc_kbytestotal_fops,          NULL, 0 },
185         { "kbytesfree",         &mdc_kbytesfree_fops,           NULL, 0 },
186         { "kbytesavail",        &mdc_kbytesavail_fops,          NULL, 0 },
187         { "filestotal",         &mdc_filestotal_fops,           NULL, 0 },
188         { "filesfree",          &mdc_filesfree_fops,            NULL, 0 },
189         /*{ "filegroups",       lprocfs_rd_filegroups,          NULL, 0 },*/
190         { "mds_server_uuid",    &mdc_server_uuid_fops,          NULL, 0 },
191         { "mds_conn_uuid",      &mdc_conn_uuid_fops,            NULL, 0 },
192         /*
193          * FIXME: below proc entry is provided, but not in used, instead
194          * sbi->sb_md_brw_size is used, the per obd variable should be used
195          * when CMD is enabled, and dir pages are managed in MDC layer.
196          * Remember to enable proc write function.
197          */
198         { "max_pages_per_rpc",  &mdc_obd_max_pages_per_rpc_fops, NULL, 0 },
199         { "max_rpcs_in_flight", &mdc_max_rpcs_in_flight_fops,   NULL, 0 },
200         { "timeouts",           &mdc_timeouts_fops,             NULL, 0 },
201         { "import",             &mdc_import_fops,               NULL, 0 },
202         { "state",              &mdc_state_fops,                NULL, 0 },
203         { "hsm_nl",             &mdc_kuc_fops,                  NULL, 0200 },
204         { "pinger_recov",       &mdc_pinger_recov_fops,         NULL, 0 },
205         { NULL }
206 };
207
208 LPROC_SEQ_FOPS_RO_TYPE(mdc, numrefs);
209
210 static struct lprocfs_vars lprocfs_mdc_module_vars[] = {
211         { "num_refs",           &mdc_numrefs_fops,              NULL, 0 },
212         { NULL }
213 };
214
215 void lprocfs_mdc_init_vars(struct lprocfs_static_vars *lvars)
216 {
217         lvars->module_vars  = lprocfs_mdc_module_vars;
218         lvars->obd_vars     = lprocfs_mdc_obd_vars;
219 }