blob: 262c25db05cde8a4888096f7ae7e92f592a88052 [file] [log] [blame]
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -08001/*
John Gregor87427da2007-06-11 10:21:14 -07002 * Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved.
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -08003 * Copyright (c) 2006 PathScale, Inc. All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
34#include <linux/version.h>
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -080035#include <linux/module.h>
36#include <linux/fs.h>
37#include <linux/mount.h>
38#include <linux/pagemap.h>
39#include <linux/init.h>
40#include <linux/namei.h>
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -080041
42#include "ipath_kernel.h"
43
44#define IPATHFS_MAGIC 0x726a77
45
46static struct super_block *ipath_super;
47
48static int ipathfs_mknod(struct inode *dir, struct dentry *dentry,
Arjan van de Ven2b8693c2007-02-12 00:55:32 -080049 int mode, const struct file_operations *fops,
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -080050 void *data)
51{
52 int error;
53 struct inode *inode = new_inode(dir->i_sb);
54
55 if (!inode) {
56 error = -EPERM;
57 goto bail;
58 }
59
60 inode->i_mode = mode;
61 inode->i_uid = 0;
62 inode->i_gid = 0;
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -080063 inode->i_blocks = 0;
64 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Theodore Ts'o8e18e292006-09-27 01:50:46 -070065 inode->i_private = data;
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -080066 if ((mode & S_IFMT) == S_IFDIR) {
67 inode->i_op = &simple_dir_inode_operations;
Dave Hansend8c76e62006-09-30 23:29:04 -070068 inc_nlink(inode);
69 inc_nlink(dir);
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -080070 }
71
72 inode->i_fop = fops;
73
74 d_instantiate(dentry, inode);
75 error = 0;
76
77bail:
78 return error;
79}
80
81static int create_file(const char *name, mode_t mode,
82 struct dentry *parent, struct dentry **dentry,
Arjan van de Ven2b8693c2007-02-12 00:55:32 -080083 const struct file_operations *fops, void *data)
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -080084{
85 int error;
86
87 *dentry = NULL;
88 mutex_lock(&parent->d_inode->i_mutex);
89 *dentry = lookup_one_len(name, parent, strlen(name));
90 if (!IS_ERR(dentry))
91 error = ipathfs_mknod(parent->d_inode, *dentry,
92 mode, fops, data);
93 else
94 error = PTR_ERR(dentry);
95 mutex_unlock(&parent->d_inode->i_mutex);
96
97 return error;
98}
99
100static ssize_t atomic_stats_read(struct file *file, char __user *buf,
101 size_t count, loff_t *ppos)
102{
103 return simple_read_from_buffer(buf, count, ppos, &ipath_stats,
104 sizeof ipath_stats);
105}
106
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800107static const struct file_operations atomic_stats_ops = {
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800108 .read = atomic_stats_read,
109};
110
111#define NUM_COUNTERS sizeof(struct infinipath_counters) / sizeof(u64)
112
113static ssize_t atomic_counters_read(struct file *file, char __user *buf,
114 size_t count, loff_t *ppos)
115{
116 u64 counters[NUM_COUNTERS];
117 u16 i;
118 struct ipath_devdata *dd;
119
Josef Sipek1cfd6e62006-12-08 02:37:10 -0800120 dd = file->f_path.dentry->d_inode->i_private;
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800121
122 for (i = 0; i < NUM_COUNTERS; i++)
123 counters[i] = ipath_snap_cntr(dd, i);
124
125 return simple_read_from_buffer(buf, count, ppos, counters,
126 sizeof counters);
127}
128
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800129static const struct file_operations atomic_counters_ops = {
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800130 .read = atomic_counters_read,
131};
132
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800133static ssize_t flash_read(struct file *file, char __user *buf,
134 size_t count, loff_t *ppos)
135{
136 struct ipath_devdata *dd;
137 ssize_t ret;
138 loff_t pos;
139 char *tmp;
140
141 pos = *ppos;
142
143 if ( pos < 0) {
144 ret = -EINVAL;
145 goto bail;
146 }
147
148 if (pos >= sizeof(struct ipath_flash)) {
149 ret = 0;
150 goto bail;
151 }
152
153 if (count > sizeof(struct ipath_flash) - pos)
154 count = sizeof(struct ipath_flash) - pos;
155
156 tmp = kmalloc(count, GFP_KERNEL);
157 if (!tmp) {
158 ret = -ENOMEM;
159 goto bail;
160 }
161
Josef Sipek1cfd6e62006-12-08 02:37:10 -0800162 dd = file->f_path.dentry->d_inode->i_private;
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800163 if (ipath_eeprom_read(dd, pos, tmp, count)) {
164 ipath_dev_err(dd, "failed to read from flash\n");
165 ret = -ENXIO;
166 goto bail_tmp;
167 }
168
169 if (copy_to_user(buf, tmp, count)) {
170 ret = -EFAULT;
171 goto bail_tmp;
172 }
173
174 *ppos = pos + count;
175 ret = count;
176
177bail_tmp:
178 kfree(tmp);
179
180bail:
181 return ret;
182}
183
184static ssize_t flash_write(struct file *file, const char __user *buf,
185 size_t count, loff_t *ppos)
186{
187 struct ipath_devdata *dd;
188 ssize_t ret;
189 loff_t pos;
190 char *tmp;
191
192 pos = *ppos;
193
Bryan O'Sullivan7dae5bf2006-09-28 09:00:05 -0700194 if (pos != 0) {
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800195 ret = -EINVAL;
196 goto bail;
197 }
198
Bryan O'Sullivan7dae5bf2006-09-28 09:00:05 -0700199 if (count != sizeof(struct ipath_flash)) {
200 ret = -EINVAL;
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800201 goto bail;
202 }
203
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800204 tmp = kmalloc(count, GFP_KERNEL);
205 if (!tmp) {
206 ret = -ENOMEM;
207 goto bail;
208 }
209
210 if (copy_from_user(tmp, buf, count)) {
211 ret = -EFAULT;
212 goto bail_tmp;
213 }
214
Josef Sipek1cfd6e62006-12-08 02:37:10 -0800215 dd = file->f_path.dentry->d_inode->i_private;
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800216 if (ipath_eeprom_write(dd, pos, tmp, count)) {
217 ret = -ENXIO;
218 ipath_dev_err(dd, "failed to write to flash\n");
219 goto bail_tmp;
220 }
221
222 *ppos = pos + count;
223 ret = count;
224
225bail_tmp:
226 kfree(tmp);
227
228bail:
229 return ret;
230}
231
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800232static const struct file_operations flash_ops = {
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800233 .read = flash_read,
234 .write = flash_write,
235};
236
237static int create_device_files(struct super_block *sb,
238 struct ipath_devdata *dd)
239{
240 struct dentry *dir, *tmp;
241 char unit[10];
242 int ret;
243
244 snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
245 ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir,
246 (struct file_operations *) &simple_dir_operations,
247 dd);
248 if (ret) {
249 printk(KERN_ERR "create_file(%s) failed: %d\n", unit, ret);
250 goto bail;
251 }
252
253 ret = create_file("atomic_counters", S_IFREG|S_IRUGO, dir, &tmp,
254 &atomic_counters_ops, dd);
255 if (ret) {
256 printk(KERN_ERR "create_file(%s/atomic_counters) "
257 "failed: %d\n", unit, ret);
258 goto bail;
259 }
260
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800261 ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp,
262 &flash_ops, dd);
263 if (ret) {
264 printk(KERN_ERR "create_file(%s/flash) "
265 "failed: %d\n", unit, ret);
266 goto bail;
267 }
268
269bail:
270 return ret;
271}
272
Bryan O'Sullivanfae87732007-03-21 15:18:14 -0700273static int remove_file(struct dentry *parent, char *name)
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800274{
275 struct dentry *tmp;
Bryan O'Sullivanfae87732007-03-21 15:18:14 -0700276 int ret;
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800277
278 tmp = lookup_one_len(name, parent, strlen(name));
279
Bryan O'Sullivanfae87732007-03-21 15:18:14 -0700280 if (IS_ERR(tmp)) {
281 ret = PTR_ERR(tmp);
282 goto bail;
283 }
284
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800285 spin_lock(&dcache_lock);
286 spin_lock(&tmp->d_lock);
287 if (!(d_unhashed(tmp) && tmp->d_inode)) {
288 dget_locked(tmp);
289 __d_drop(tmp);
290 spin_unlock(&tmp->d_lock);
291 spin_unlock(&dcache_lock);
292 simple_unlink(parent->d_inode, tmp);
293 } else {
294 spin_unlock(&tmp->d_lock);
295 spin_unlock(&dcache_lock);
296 }
Bryan O'Sullivanfae87732007-03-21 15:18:14 -0700297
298 ret = 0;
299bail:
300 /*
301 * We don't expect clients to care about the return value, but
302 * it's there if they need it.
303 */
304 return ret;
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800305}
306
307static int remove_device_files(struct super_block *sb,
308 struct ipath_devdata *dd)
309{
310 struct dentry *dir, *root;
311 char unit[10];
312 int ret;
313
314 root = dget(sb->s_root);
315 mutex_lock(&root->d_inode->i_mutex);
316 snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
317 dir = lookup_one_len(unit, root, strlen(unit));
318
319 if (IS_ERR(dir)) {
320 ret = PTR_ERR(dir);
321 printk(KERN_ERR "Lookup of %s failed\n", unit);
322 goto bail;
323 }
324
325 remove_file(dir, "flash");
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800326 remove_file(dir, "atomic_counters");
327 d_delete(dir);
328 ret = simple_rmdir(root->d_inode, dir);
329
330bail:
331 mutex_unlock(&root->d_inode->i_mutex);
332 dput(root);
333 return ret;
334}
335
336static int ipathfs_fill_super(struct super_block *sb, void *data,
337 int silent)
338{
339 struct ipath_devdata *dd, *tmp;
340 unsigned long flags;
341 int ret;
342
343 static struct tree_descr files[] = {
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700344 [2] = {"atomic_stats", &atomic_stats_ops, S_IRUGO},
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800345 {""},
346 };
347
348 ret = simple_fill_super(sb, IPATHFS_MAGIC, files);
349 if (ret) {
350 printk(KERN_ERR "simple_fill_super failed: %d\n", ret);
351 goto bail;
352 }
353
354 spin_lock_irqsave(&ipath_devs_lock, flags);
355
356 list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
357 spin_unlock_irqrestore(&ipath_devs_lock, flags);
358 ret = create_device_files(sb, dd);
359 if (ret) {
360 deactivate_super(sb);
361 goto bail;
362 }
363 spin_lock_irqsave(&ipath_devs_lock, flags);
364 }
365
366 spin_unlock_irqrestore(&ipath_devs_lock, flags);
367
368bail:
369 return ret;
370}
371
David Howells454e2392006-06-23 02:02:57 -0700372static int ipathfs_get_sb(struct file_system_type *fs_type, int flags,
373 const char *dev_name, void *data, struct vfsmount *mnt)
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800374{
David Howells454e2392006-06-23 02:02:57 -0700375 int ret = get_sb_single(fs_type, flags, data,
376 ipathfs_fill_super, mnt);
377 if (ret >= 0)
378 ipath_super = mnt->mnt_sb;
379 return ret;
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800380}
381
382static void ipathfs_kill_super(struct super_block *s)
383{
384 kill_litter_super(s);
385 ipath_super = NULL;
386}
387
388int ipathfs_add_device(struct ipath_devdata *dd)
389{
390 int ret;
391
392 if (ipath_super == NULL) {
393 ret = 0;
394 goto bail;
395 }
396
397 ret = create_device_files(ipath_super, dd);
398
399bail:
400 return ret;
401}
402
403int ipathfs_remove_device(struct ipath_devdata *dd)
404{
405 int ret;
406
407 if (ipath_super == NULL) {
408 ret = 0;
409 goto bail;
410 }
411
412 ret = remove_device_files(ipath_super, dd);
413
414bail:
415 return ret;
416}
417
418static struct file_system_type ipathfs_fs_type = {
419 .owner = THIS_MODULE,
420 .name = "ipathfs",
421 .get_sb = ipathfs_get_sb,
422 .kill_sb = ipathfs_kill_super,
423};
424
425int __init ipath_init_ipathfs(void)
426{
427 return register_filesystem(&ipathfs_fs_type);
428}
429
430void __exit ipath_exit_ipathfs(void)
431{
432 unregister_filesystem(&ipathfs_fs_type);
433}