]>
Commit | Line | Data |
---|---|---|
3e9b4a5e | 1 | /* |
87427da5 | 2 | * Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved. |
3e9b4a5e BS |
3 | * 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 | ||
3e9b4a5e BS |
34 | #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> | |
5a0e3ad6 | 40 | #include <linux/slab.h> |
3e9b4a5e BS |
41 | |
42 | #include "ipath_kernel.h" | |
43 | ||
44 | #define IPATHFS_MAGIC 0x726a77 | |
45 | ||
46 | static struct super_block *ipath_super; | |
47 | ||
48 | static int ipathfs_mknod(struct inode *dir, struct dentry *dentry, | |
f9ec8006 | 49 | umode_t mode, const struct file_operations *fops, |
3e9b4a5e BS |
50 | 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 | ||
85fe4025 | 60 | inode->i_ino = get_next_ino(); |
3e9b4a5e | 61 | inode->i_mode = mode; |
3e9b4a5e | 62 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
8e18e294 | 63 | inode->i_private = data; |
f9ec8006 | 64 | if (S_ISDIR(mode)) { |
3e9b4a5e | 65 | inode->i_op = &simple_dir_inode_operations; |
d8c76e6f DH |
66 | inc_nlink(inode); |
67 | inc_nlink(dir); | |
3e9b4a5e BS |
68 | } |
69 | ||
70 | inode->i_fop = fops; | |
71 | ||
72 | d_instantiate(dentry, inode); | |
73 | error = 0; | |
74 | ||
75 | bail: | |
76 | return error; | |
77 | } | |
78 | ||
f9ec8006 | 79 | static int create_file(const char *name, umode_t mode, |
3e9b4a5e | 80 | struct dentry *parent, struct dentry **dentry, |
2b8693c0 | 81 | const struct file_operations *fops, void *data) |
3e9b4a5e BS |
82 | { |
83 | int error; | |
84 | ||
5955102c | 85 | inode_lock(d_inode(parent)); |
3e9b4a5e | 86 | *dentry = lookup_one_len(name, parent, strlen(name)); |
64f22fa1 | 87 | if (!IS_ERR(*dentry)) |
75c3cfa8 | 88 | error = ipathfs_mknod(d_inode(parent), *dentry, |
3e9b4a5e BS |
89 | mode, fops, data); |
90 | else | |
ef535287 | 91 | error = PTR_ERR(*dentry); |
5955102c | 92 | inode_unlock(d_inode(parent)); |
3e9b4a5e BS |
93 | |
94 | return error; | |
95 | } | |
96 | ||
97 | static ssize_t atomic_stats_read(struct file *file, char __user *buf, | |
98 | size_t count, loff_t *ppos) | |
99 | { | |
100 | return simple_read_from_buffer(buf, count, ppos, &ipath_stats, | |
101 | sizeof ipath_stats); | |
102 | } | |
103 | ||
2b8693c0 | 104 | static const struct file_operations atomic_stats_ops = { |
3e9b4a5e | 105 | .read = atomic_stats_read, |
6038f373 | 106 | .llseek = default_llseek, |
3e9b4a5e BS |
107 | }; |
108 | ||
3e9b4a5e BS |
109 | static ssize_t atomic_counters_read(struct file *file, char __user *buf, |
110 | size_t count, loff_t *ppos) | |
111 | { | |
3029fcc3 | 112 | struct infinipath_counters counters; |
3e9b4a5e BS |
113 | struct ipath_devdata *dd; |
114 | ||
496ad9aa | 115 | dd = file_inode(file)->i_private; |
3029fcc3 | 116 | dd->ipath_f_read_counters(dd, &counters); |
3e9b4a5e | 117 | |
3029fcc3 | 118 | return simple_read_from_buffer(buf, count, ppos, &counters, |
3e9b4a5e BS |
119 | sizeof counters); |
120 | } | |
121 | ||
2b8693c0 | 122 | static const struct file_operations atomic_counters_ops = { |
3e9b4a5e | 123 | .read = atomic_counters_read, |
6038f373 | 124 | .llseek = default_llseek, |
3e9b4a5e BS |
125 | }; |
126 | ||
3e9b4a5e BS |
127 | static 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 | ||
496ad9aa | 156 | dd = file_inode(file)->i_private; |
3e9b4a5e BS |
157 | 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 | ||
171 | bail_tmp: | |
172 | kfree(tmp); | |
173 | ||
174 | bail: | |
175 | return ret; | |
176 | } | |
177 | ||
178 | static 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 | ||
7dae5bff | 188 | if (pos != 0) { |
3e9b4a5e BS |
189 | ret = -EINVAL; |
190 | goto bail; | |
191 | } | |
192 | ||
7dae5bff BS |
193 | if (count != sizeof(struct ipath_flash)) { |
194 | ret = -EINVAL; | |
3e9b4a5e BS |
195 | goto bail; |
196 | } | |
197 | ||
4ecc4a18 KS |
198 | tmp = memdup_user(buf, count); |
199 | if (IS_ERR(tmp)) | |
200 | return PTR_ERR(tmp); | |
3e9b4a5e | 201 | |
496ad9aa | 202 | dd = file_inode(file)->i_private; |
3e9b4a5e BS |
203 | if (ipath_eeprom_write(dd, pos, tmp, count)) { |
204 | ret = -ENXIO; | |
205 | ipath_dev_err(dd, "failed to write to flash\n"); | |
206 | goto bail_tmp; | |
207 | } | |
208 | ||
209 | *ppos = pos + count; | |
210 | ret = count; | |
211 | ||
212 | bail_tmp: | |
213 | kfree(tmp); | |
214 | ||
215 | bail: | |
216 | return ret; | |
217 | } | |
218 | ||
2b8693c0 | 219 | static const struct file_operations flash_ops = { |
3e9b4a5e BS |
220 | .read = flash_read, |
221 | .write = flash_write, | |
6038f373 | 222 | .llseek = default_llseek, |
3e9b4a5e BS |
223 | }; |
224 | ||
225 | static int create_device_files(struct super_block *sb, | |
226 | struct ipath_devdata *dd) | |
227 | { | |
228 | struct dentry *dir, *tmp; | |
229 | char unit[10]; | |
230 | int ret; | |
231 | ||
232 | snprintf(unit, sizeof unit, "%02d", dd->ipath_unit); | |
233 | ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir, | |
f7fca1e8 | 234 | &simple_dir_operations, dd); |
3e9b4a5e BS |
235 | if (ret) { |
236 | printk(KERN_ERR "create_file(%s) failed: %d\n", unit, ret); | |
237 | goto bail; | |
238 | } | |
239 | ||
240 | ret = create_file("atomic_counters", S_IFREG|S_IRUGO, dir, &tmp, | |
241 | &atomic_counters_ops, dd); | |
242 | if (ret) { | |
243 | printk(KERN_ERR "create_file(%s/atomic_counters) " | |
244 | "failed: %d\n", unit, ret); | |
245 | goto bail; | |
246 | } | |
247 | ||
3e9b4a5e BS |
248 | ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp, |
249 | &flash_ops, dd); | |
250 | if (ret) { | |
251 | printk(KERN_ERR "create_file(%s/flash) " | |
252 | "failed: %d\n", unit, ret); | |
253 | goto bail; | |
254 | } | |
255 | ||
256 | bail: | |
257 | return ret; | |
258 | } | |
259 | ||
fae8773b | 260 | static int remove_file(struct dentry *parent, char *name) |
3e9b4a5e BS |
261 | { |
262 | struct dentry *tmp; | |
fae8773b | 263 | int ret; |
3e9b4a5e BS |
264 | |
265 | tmp = lookup_one_len(name, parent, strlen(name)); | |
266 | ||
fae8773b BS |
267 | if (IS_ERR(tmp)) { |
268 | ret = PTR_ERR(tmp); | |
269 | goto bail; | |
270 | } | |
271 | ||
3e9b4a5e | 272 | spin_lock(&tmp->d_lock); |
dc3f4198 | 273 | if (simple_positive(tmp)) { |
dc0474be | 274 | dget_dlock(tmp); |
3e9b4a5e BS |
275 | __d_drop(tmp); |
276 | spin_unlock(&tmp->d_lock); | |
75c3cfa8 | 277 | simple_unlink(d_inode(parent), tmp); |
b5c84bf6 | 278 | } else |
3e9b4a5e | 279 | spin_unlock(&tmp->d_lock); |
fae8773b BS |
280 | |
281 | ret = 0; | |
282 | bail: | |
283 | /* | |
284 | * We don't expect clients to care about the return value, but | |
285 | * it's there if they need it. | |
286 | */ | |
287 | return ret; | |
3e9b4a5e BS |
288 | } |
289 | ||
290 | static int remove_device_files(struct super_block *sb, | |
291 | struct ipath_devdata *dd) | |
292 | { | |
293 | struct dentry *dir, *root; | |
294 | char unit[10]; | |
295 | int ret; | |
296 | ||
297 | root = dget(sb->s_root); | |
5955102c | 298 | inode_lock(d_inode(root)); |
3e9b4a5e BS |
299 | snprintf(unit, sizeof unit, "%02d", dd->ipath_unit); |
300 | dir = lookup_one_len(unit, root, strlen(unit)); | |
301 | ||
302 | if (IS_ERR(dir)) { | |
303 | ret = PTR_ERR(dir); | |
304 | printk(KERN_ERR "Lookup of %s failed\n", unit); | |
305 | goto bail; | |
306 | } | |
307 | ||
308 | remove_file(dir, "flash"); | |
3e9b4a5e BS |
309 | remove_file(dir, "atomic_counters"); |
310 | d_delete(dir); | |
75c3cfa8 | 311 | ret = simple_rmdir(d_inode(root), dir); |
3e9b4a5e BS |
312 | |
313 | bail: | |
5955102c | 314 | inode_unlock(d_inode(root)); |
3e9b4a5e BS |
315 | dput(root); |
316 | return ret; | |
317 | } | |
318 | ||
319 | static int ipathfs_fill_super(struct super_block *sb, void *data, | |
320 | int silent) | |
321 | { | |
322 | struct ipath_devdata *dd, *tmp; | |
323 | unsigned long flags; | |
324 | int ret; | |
325 | ||
326 | static struct tree_descr files[] = { | |
1a1c9bb4 | 327 | [2] = {"atomic_stats", &atomic_stats_ops, S_IRUGO}, |
3e9b4a5e BS |
328 | {""}, |
329 | }; | |
330 | ||
331 | ret = simple_fill_super(sb, IPATHFS_MAGIC, files); | |
332 | if (ret) { | |
333 | printk(KERN_ERR "simple_fill_super failed: %d\n", ret); | |
334 | goto bail; | |
335 | } | |
336 | ||
337 | spin_lock_irqsave(&ipath_devs_lock, flags); | |
338 | ||
339 | list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) { | |
340 | spin_unlock_irqrestore(&ipath_devs_lock, flags); | |
341 | ret = create_device_files(sb, dd); | |
12e9a456 | 342 | if (ret) |
3e9b4a5e | 343 | goto bail; |
3e9b4a5e BS |
344 | spin_lock_irqsave(&ipath_devs_lock, flags); |
345 | } | |
346 | ||
347 | spin_unlock_irqrestore(&ipath_devs_lock, flags); | |
348 | ||
349 | bail: | |
350 | return ret; | |
351 | } | |
352 | ||
fc14f2fe AV |
353 | static struct dentry *ipathfs_mount(struct file_system_type *fs_type, |
354 | int flags, const char *dev_name, void *data) | |
3e9b4a5e | 355 | { |
fc14f2fe AV |
356 | struct dentry *ret; |
357 | ret = mount_single(fs_type, flags, data, ipathfs_fill_super); | |
358 | if (!IS_ERR(ret)) | |
359 | ipath_super = ret->d_sb; | |
454e2398 | 360 | return ret; |
3e9b4a5e BS |
361 | } |
362 | ||
363 | static void ipathfs_kill_super(struct super_block *s) | |
364 | { | |
365 | kill_litter_super(s); | |
366 | ipath_super = NULL; | |
367 | } | |
368 | ||
369 | int ipathfs_add_device(struct ipath_devdata *dd) | |
370 | { | |
371 | int ret; | |
372 | ||
373 | if (ipath_super == NULL) { | |
374 | ret = 0; | |
375 | goto bail; | |
376 | } | |
377 | ||
378 | ret = create_device_files(ipath_super, dd); | |
379 | ||
380 | bail: | |
381 | return ret; | |
382 | } | |
383 | ||
384 | int ipathfs_remove_device(struct ipath_devdata *dd) | |
385 | { | |
386 | int ret; | |
387 | ||
388 | if (ipath_super == NULL) { | |
389 | ret = 0; | |
390 | goto bail; | |
391 | } | |
392 | ||
393 | ret = remove_device_files(ipath_super, dd); | |
394 | ||
395 | bail: | |
396 | return ret; | |
397 | } | |
398 | ||
399 | static struct file_system_type ipathfs_fs_type = { | |
400 | .owner = THIS_MODULE, | |
401 | .name = "ipathfs", | |
fc14f2fe | 402 | .mount = ipathfs_mount, |
3e9b4a5e BS |
403 | .kill_sb = ipathfs_kill_super, |
404 | }; | |
7f78e035 | 405 | MODULE_ALIAS_FS("ipathfs"); |
3e9b4a5e BS |
406 | |
407 | int __init ipath_init_ipathfs(void) | |
408 | { | |
409 | return register_filesystem(&ipathfs_fs_type); | |
410 | } | |
411 | ||
412 | void __exit ipath_exit_ipathfs(void) | |
413 | { | |
414 | unregister_filesystem(&ipathfs_fs_type); | |
415 | } |