blob: 53912c327bfea25dc4bab902d788042973441140 [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
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -080034#include <linux/module.h>
35#include <linux/fs.h>
36#include <linux/mount.h>
37#include <linux/pagemap.h>
38#include <linux/init.h>
39#include <linux/namei.h>
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -080040
41#include "ipath_kernel.h"
42
43#define IPATHFS_MAGIC 0x726a77
44
45static struct super_block *ipath_super;
46
47static int ipathfs_mknod(struct inode *dir, struct dentry *dentry,
Arjan van de Ven2b8693c2007-02-12 00:55:32 -080048 int mode, const struct file_operations *fops,
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -080049 void *data)
50{
51 int error;
52 struct inode *inode = new_inode(dir->i_sb);
53
54 if (!inode) {
55 error = -EPERM;
56 goto bail;
57 }
58
59 inode->i_mode = mode;
60 inode->i_uid = 0;
61 inode->i_gid = 0;
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -080062 inode->i_blocks = 0;
63 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Theodore Ts'o8e18e292006-09-27 01:50:46 -070064 inode->i_private = data;
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -080065 if ((mode & S_IFMT) == S_IFDIR) {
66 inode->i_op = &simple_dir_inode_operations;
Dave Hansend8c76e62006-09-30 23:29:04 -070067 inc_nlink(inode);
68 inc_nlink(dir);
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -080069 }
70
71 inode->i_fop = fops;
72
73 d_instantiate(dentry, inode);
74 error = 0;
75
76bail:
77 return error;
78}
79
80static int create_file(const char *name, mode_t mode,
81 struct dentry *parent, struct dentry **dentry,
Arjan van de Ven2b8693c2007-02-12 00:55:32 -080082 const struct file_operations *fops, void *data)
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -080083{
84 int error;
85
86 *dentry = NULL;
87 mutex_lock(&parent->d_inode->i_mutex);
88 *dentry = lookup_one_len(name, parent, strlen(name));
Michael Ellerman64f22fa2008-12-01 20:59:07 -080089 if (!IS_ERR(*dentry))
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -080090 error = ipathfs_mknod(parent->d_inode, *dentry,
91 mode, fops, data);
92 else
93 error = PTR_ERR(dentry);
94 mutex_unlock(&parent->d_inode->i_mutex);
95
96 return error;
97}
98
99static ssize_t atomic_stats_read(struct file *file, char __user *buf,
100 size_t count, loff_t *ppos)
101{
102 return simple_read_from_buffer(buf, count, ppos, &ipath_stats,
103 sizeof ipath_stats);
104}
105
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800106static const struct file_operations atomic_stats_ops = {
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800107 .read = atomic_stats_read,
108};
109
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800110static ssize_t atomic_counters_read(struct file *file, char __user *buf,
111 size_t count, loff_t *ppos)
112{
Ralph Campbell3029fcc2008-01-06 21:02:34 -0800113 struct infinipath_counters counters;
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800114 struct ipath_devdata *dd;
115
Josef Sipek1cfd6e62006-12-08 02:37:10 -0800116 dd = file->f_path.dentry->d_inode->i_private;
Ralph Campbell3029fcc2008-01-06 21:02:34 -0800117 dd->ipath_f_read_counters(dd, &counters);
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800118
Ralph Campbell3029fcc2008-01-06 21:02:34 -0800119 return simple_read_from_buffer(buf, count, ppos, &counters,
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800120 sizeof counters);
121}
122
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800123static const struct file_operations atomic_counters_ops = {
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800124 .read = atomic_counters_read,
125};
126
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800127static ssize_t flash_read(struct file *file, char __user *buf,
128 size_t count, loff_t *ppos)
129{
130 struct ipath_devdata *dd;
131 ssize_t ret;
132 loff_t pos;
133 char *tmp;
134
135 pos = *ppos;
136
137 if ( pos < 0) {
138 ret = -EINVAL;
139 goto bail;
140 }
141
142 if (pos >= sizeof(struct ipath_flash)) {
143 ret = 0;
144 goto bail;
145 }
146
147 if (count > sizeof(struct ipath_flash) - pos)
148 count = sizeof(struct ipath_flash) - pos;
149
150 tmp = kmalloc(count, GFP_KERNEL);
151 if (!tmp) {
152 ret = -ENOMEM;
153 goto bail;
154 }
155
Josef Sipek1cfd6e62006-12-08 02:37:10 -0800156 dd = file->f_path.dentry->d_inode->i_private;
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800157 if (ipath_eeprom_read(dd, pos, tmp, count)) {
158 ipath_dev_err(dd, "failed to read from flash\n");
159 ret = -ENXIO;
160 goto bail_tmp;
161 }
162
163 if (copy_to_user(buf, tmp, count)) {
164 ret = -EFAULT;
165 goto bail_tmp;
166 }
167
168 *ppos = pos + count;
169 ret = count;
170
171bail_tmp:
172 kfree(tmp);
173
174bail:
175 return ret;
176}
177
178static ssize_t flash_write(struct file *file, const char __user *buf,
179 size_t count, loff_t *ppos)
180{
181 struct ipath_devdata *dd;
182 ssize_t ret;
183 loff_t pos;
184 char *tmp;
185
186 pos = *ppos;
187
Bryan O'Sullivan7dae5bf2006-09-28 09:00:05 -0700188 if (pos != 0) {
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800189 ret = -EINVAL;
190 goto bail;
191 }
192
Bryan O'Sullivan7dae5bf2006-09-28 09:00:05 -0700193 if (count != sizeof(struct ipath_flash)) {
194 ret = -EINVAL;
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800195 goto bail;
196 }
197
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800198 tmp = kmalloc(count, GFP_KERNEL);
199 if (!tmp) {
200 ret = -ENOMEM;
201 goto bail;
202 }
203
204 if (copy_from_user(tmp, buf, count)) {
205 ret = -EFAULT;
206 goto bail_tmp;
207 }
208
Josef Sipek1cfd6e62006-12-08 02:37:10 -0800209 dd = file->f_path.dentry->d_inode->i_private;
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800210 if (ipath_eeprom_write(dd, pos, tmp, count)) {
211 ret = -ENXIO;
212 ipath_dev_err(dd, "failed to write to flash\n");
213 goto bail_tmp;
214 }
215
216 *ppos = pos + count;
217 ret = count;
218
219bail_tmp:
220 kfree(tmp);
221
222bail:
223 return ret;
224}
225
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800226static const struct file_operations flash_ops = {
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800227 .read = flash_read,
228 .write = flash_write,
229};
230
231static int create_device_files(struct super_block *sb,
232 struct ipath_devdata *dd)
233{
234 struct dentry *dir, *tmp;
235 char unit[10];
236 int ret;
237
238 snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
239 ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir,
Jan Engelhardtf7fca1e2008-01-22 20:45:30 +0100240 &simple_dir_operations, dd);
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800241 if (ret) {
242 printk(KERN_ERR "create_file(%s) failed: %d\n", unit, ret);
243 goto bail;
244 }
245
246 ret = create_file("atomic_counters", S_IFREG|S_IRUGO, dir, &tmp,
247 &atomic_counters_ops, dd);
248 if (ret) {
249 printk(KERN_ERR "create_file(%s/atomic_counters) "
250 "failed: %d\n", unit, ret);
251 goto bail;
252 }
253
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800254 ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp,
255 &flash_ops, dd);
256 if (ret) {
257 printk(KERN_ERR "create_file(%s/flash) "
258 "failed: %d\n", unit, ret);
259 goto bail;
260 }
261
262bail:
263 return ret;
264}
265
Bryan O'Sullivanfae87732007-03-21 15:18:14 -0700266static int remove_file(struct dentry *parent, char *name)
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800267{
268 struct dentry *tmp;
Bryan O'Sullivanfae87732007-03-21 15:18:14 -0700269 int ret;
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800270
271 tmp = lookup_one_len(name, parent, strlen(name));
272
Bryan O'Sullivanfae87732007-03-21 15:18:14 -0700273 if (IS_ERR(tmp)) {
274 ret = PTR_ERR(tmp);
275 goto bail;
276 }
277
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800278 spin_lock(&dcache_lock);
279 spin_lock(&tmp->d_lock);
280 if (!(d_unhashed(tmp) && tmp->d_inode)) {
281 dget_locked(tmp);
282 __d_drop(tmp);
283 spin_unlock(&tmp->d_lock);
284 spin_unlock(&dcache_lock);
285 simple_unlink(parent->d_inode, tmp);
286 } else {
287 spin_unlock(&tmp->d_lock);
288 spin_unlock(&dcache_lock);
289 }
Bryan O'Sullivanfae87732007-03-21 15:18:14 -0700290
291 ret = 0;
292bail:
293 /*
294 * We don't expect clients to care about the return value, but
295 * it's there if they need it.
296 */
297 return ret;
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800298}
299
300static int remove_device_files(struct super_block *sb,
301 struct ipath_devdata *dd)
302{
303 struct dentry *dir, *root;
304 char unit[10];
305 int ret;
306
307 root = dget(sb->s_root);
308 mutex_lock(&root->d_inode->i_mutex);
309 snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
310 dir = lookup_one_len(unit, root, strlen(unit));
311
312 if (IS_ERR(dir)) {
313 ret = PTR_ERR(dir);
314 printk(KERN_ERR "Lookup of %s failed\n", unit);
315 goto bail;
316 }
317
318 remove_file(dir, "flash");
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800319 remove_file(dir, "atomic_counters");
320 d_delete(dir);
321 ret = simple_rmdir(root->d_inode, dir);
322
323bail:
324 mutex_unlock(&root->d_inode->i_mutex);
325 dput(root);
326 return ret;
327}
328
329static int ipathfs_fill_super(struct super_block *sb, void *data,
330 int silent)
331{
332 struct ipath_devdata *dd, *tmp;
333 unsigned long flags;
334 int ret;
335
336 static struct tree_descr files[] = {
Jeff Layton1a1c9bb2007-05-08 00:32:31 -0700337 [2] = {"atomic_stats", &atomic_stats_ops, S_IRUGO},
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800338 {""},
339 };
340
341 ret = simple_fill_super(sb, IPATHFS_MAGIC, files);
342 if (ret) {
343 printk(KERN_ERR "simple_fill_super failed: %d\n", ret);
344 goto bail;
345 }
346
347 spin_lock_irqsave(&ipath_devs_lock, flags);
348
349 list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
350 spin_unlock_irqrestore(&ipath_devs_lock, flags);
351 ret = create_device_files(sb, dd);
352 if (ret) {
353 deactivate_super(sb);
354 goto bail;
355 }
356 spin_lock_irqsave(&ipath_devs_lock, flags);
357 }
358
359 spin_unlock_irqrestore(&ipath_devs_lock, flags);
360
361bail:
362 return ret;
363}
364
David Howells454e2392006-06-23 02:02:57 -0700365static int ipathfs_get_sb(struct file_system_type *fs_type, int flags,
366 const char *dev_name, void *data, struct vfsmount *mnt)
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800367{
David Howells454e2392006-06-23 02:02:57 -0700368 int ret = get_sb_single(fs_type, flags, data,
369 ipathfs_fill_super, mnt);
370 if (ret >= 0)
371 ipath_super = mnt->mnt_sb;
372 return ret;
Bryan O'Sullivan3e9b4a52006-03-29 15:23:30 -0800373}
374
375static void ipathfs_kill_super(struct super_block *s)
376{
377 kill_litter_super(s);
378 ipath_super = NULL;
379}
380
381int ipathfs_add_device(struct ipath_devdata *dd)
382{
383 int ret;
384
385 if (ipath_super == NULL) {
386 ret = 0;
387 goto bail;
388 }
389
390 ret = create_device_files(ipath_super, dd);
391
392bail:
393 return ret;
394}
395
396int ipathfs_remove_device(struct ipath_devdata *dd)
397{
398 int ret;
399
400 if (ipath_super == NULL) {
401 ret = 0;
402 goto bail;
403 }
404
405 ret = remove_device_files(ipath_super, dd);
406
407bail:
408 return ret;
409}
410
411static struct file_system_type ipathfs_fs_type = {
412 .owner = THIS_MODULE,
413 .name = "ipathfs",
414 .get_sb = ipathfs_get_sb,
415 .kill_sb = ipathfs_kill_super,
416};
417
418int __init ipath_init_ipathfs(void)
419{
420 return register_filesystem(&ipathfs_fs_type);
421}
422
423void __exit ipath_exit_ipathfs(void)
424{
425 unregister_filesystem(&ipathfs_fs_type);
426}