]>
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, | |
2b8693c0 | 49 | int 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; |
3e9b4a5e BS |
64 | if ((mode & S_IFMT) == S_IFDIR) { |
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 | ||
79 | static int create_file(const char *name, mode_t mode, | |
80 | struct dentry *parent, struct dentry **dentry, | |
2b8693c0 | 81 | const struct file_operations *fops, void *data) |
3e9b4a5e BS |
82 | { |
83 | int error; | |
84 | ||
85 | *dentry = NULL; | |
86 | mutex_lock(&parent->d_inode->i_mutex); | |
87 | *dentry = lookup_one_len(name, parent, strlen(name)); | |
64f22fa1 | 88 | if (!IS_ERR(*dentry)) |
3e9b4a5e BS |
89 | error = ipathfs_mknod(parent->d_inode, *dentry, |
90 | mode, fops, data); | |
91 | else | |
92 | error = PTR_ERR(dentry); | |
93 | mutex_unlock(&parent->d_inode->i_mutex); | |
94 | ||
95 | return error; | |
96 | } | |
97 | ||
98 | static ssize_t atomic_stats_read(struct file *file, char __user *buf, | |
99 | size_t count, loff_t *ppos) | |
100 | { | |
101 | return simple_read_from_buffer(buf, count, ppos, &ipath_stats, | |
102 | sizeof ipath_stats); | |
103 | } | |
104 | ||
2b8693c0 | 105 | static const struct file_operations atomic_stats_ops = { |
3e9b4a5e | 106 | .read = atomic_stats_read, |
6038f373 | 107 | .llseek = default_llseek, |
3e9b4a5e BS |
108 | }; |
109 | ||
3e9b4a5e BS |
110 | static ssize_t atomic_counters_read(struct file *file, char __user *buf, |
111 | size_t count, loff_t *ppos) | |
112 | { | |
3029fcc3 | 113 | struct infinipath_counters counters; |
3e9b4a5e BS |
114 | struct ipath_devdata *dd; |
115 | ||
1cfd6e64 | 116 | dd = file->f_path.dentry->d_inode->i_private; |
3029fcc3 | 117 | dd->ipath_f_read_counters(dd, &counters); |
3e9b4a5e | 118 | |
3029fcc3 | 119 | return simple_read_from_buffer(buf, count, ppos, &counters, |
3e9b4a5e BS |
120 | sizeof counters); |
121 | } | |
122 | ||
2b8693c0 | 123 | static const struct file_operations atomic_counters_ops = { |
3e9b4a5e | 124 | .read = atomic_counters_read, |
6038f373 | 125 | .llseek = default_llseek, |
3e9b4a5e BS |
126 | }; |
127 | ||
3e9b4a5e BS |
128 | static ssize_t flash_read(struct file *file, char __user *buf, |
129 | size_t count, loff_t *ppos) | |
130 | { | |
131 | struct ipath_devdata *dd; | |
132 | ssize_t ret; | |
133 | loff_t pos; | |
134 | char *tmp; | |
135 | ||
136 | pos = *ppos; | |
137 | ||
138 | if ( pos < 0) { | |
139 | ret = -EINVAL; | |
140 | goto bail; | |
141 | } | |
142 | ||
143 | if (pos >= sizeof(struct ipath_flash)) { | |
144 | ret = 0; | |
145 | goto bail; | |
146 | } | |
147 | ||
148 | if (count > sizeof(struct ipath_flash) - pos) | |
149 | count = sizeof(struct ipath_flash) - pos; | |
150 | ||
151 | tmp = kmalloc(count, GFP_KERNEL); | |
152 | if (!tmp) { | |
153 | ret = -ENOMEM; | |
154 | goto bail; | |
155 | } | |
156 | ||
1cfd6e64 | 157 | dd = file->f_path.dentry->d_inode->i_private; |
3e9b4a5e BS |
158 | if (ipath_eeprom_read(dd, pos, tmp, count)) { |
159 | ipath_dev_err(dd, "failed to read from flash\n"); | |
160 | ret = -ENXIO; | |
161 | goto bail_tmp; | |
162 | } | |
163 | ||
164 | if (copy_to_user(buf, tmp, count)) { | |
165 | ret = -EFAULT; | |
166 | goto bail_tmp; | |
167 | } | |
168 | ||
169 | *ppos = pos + count; | |
170 | ret = count; | |
171 | ||
172 | bail_tmp: | |
173 | kfree(tmp); | |
174 | ||
175 | bail: | |
176 | return ret; | |
177 | } | |
178 | ||
179 | static ssize_t flash_write(struct file *file, const char __user *buf, | |
180 | size_t count, loff_t *ppos) | |
181 | { | |
182 | struct ipath_devdata *dd; | |
183 | ssize_t ret; | |
184 | loff_t pos; | |
185 | char *tmp; | |
186 | ||
187 | pos = *ppos; | |
188 | ||
7dae5bff | 189 | if (pos != 0) { |
3e9b4a5e BS |
190 | ret = -EINVAL; |
191 | goto bail; | |
192 | } | |
193 | ||
7dae5bff BS |
194 | if (count != sizeof(struct ipath_flash)) { |
195 | ret = -EINVAL; | |
3e9b4a5e BS |
196 | goto bail; |
197 | } | |
198 | ||
3e9b4a5e BS |
199 | tmp = kmalloc(count, GFP_KERNEL); |
200 | if (!tmp) { | |
201 | ret = -ENOMEM; | |
202 | goto bail; | |
203 | } | |
204 | ||
205 | if (copy_from_user(tmp, buf, count)) { | |
206 | ret = -EFAULT; | |
207 | goto bail_tmp; | |
208 | } | |
209 | ||
1cfd6e64 | 210 | dd = file->f_path.dentry->d_inode->i_private; |
3e9b4a5e BS |
211 | if (ipath_eeprom_write(dd, pos, tmp, count)) { |
212 | ret = -ENXIO; | |
213 | ipath_dev_err(dd, "failed to write to flash\n"); | |
214 | goto bail_tmp; | |
215 | } | |
216 | ||
217 | *ppos = pos + count; | |
218 | ret = count; | |
219 | ||
220 | bail_tmp: | |
221 | kfree(tmp); | |
222 | ||
223 | bail: | |
224 | return ret; | |
225 | } | |
226 | ||
2b8693c0 | 227 | static const struct file_operations flash_ops = { |
3e9b4a5e BS |
228 | .read = flash_read, |
229 | .write = flash_write, | |
6038f373 | 230 | .llseek = default_llseek, |
3e9b4a5e BS |
231 | }; |
232 | ||
233 | static int create_device_files(struct super_block *sb, | |
234 | struct ipath_devdata *dd) | |
235 | { | |
236 | struct dentry *dir, *tmp; | |
237 | char unit[10]; | |
238 | int ret; | |
239 | ||
240 | snprintf(unit, sizeof unit, "%02d", dd->ipath_unit); | |
241 | ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir, | |
f7fca1e8 | 242 | &simple_dir_operations, dd); |
3e9b4a5e BS |
243 | if (ret) { |
244 | printk(KERN_ERR "create_file(%s) failed: %d\n", unit, ret); | |
245 | goto bail; | |
246 | } | |
247 | ||
248 | ret = create_file("atomic_counters", S_IFREG|S_IRUGO, dir, &tmp, | |
249 | &atomic_counters_ops, dd); | |
250 | if (ret) { | |
251 | printk(KERN_ERR "create_file(%s/atomic_counters) " | |
252 | "failed: %d\n", unit, ret); | |
253 | goto bail; | |
254 | } | |
255 | ||
3e9b4a5e BS |
256 | ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp, |
257 | &flash_ops, dd); | |
258 | if (ret) { | |
259 | printk(KERN_ERR "create_file(%s/flash) " | |
260 | "failed: %d\n", unit, ret); | |
261 | goto bail; | |
262 | } | |
263 | ||
264 | bail: | |
265 | return ret; | |
266 | } | |
267 | ||
fae8773b | 268 | static int remove_file(struct dentry *parent, char *name) |
3e9b4a5e BS |
269 | { |
270 | struct dentry *tmp; | |
fae8773b | 271 | int ret; |
3e9b4a5e BS |
272 | |
273 | tmp = lookup_one_len(name, parent, strlen(name)); | |
274 | ||
fae8773b BS |
275 | if (IS_ERR(tmp)) { |
276 | ret = PTR_ERR(tmp); | |
277 | goto bail; | |
278 | } | |
279 | ||
3e9b4a5e BS |
280 | spin_lock(&dcache_lock); |
281 | spin_lock(&tmp->d_lock); | |
282 | if (!(d_unhashed(tmp) && tmp->d_inode)) { | |
283 | dget_locked(tmp); | |
284 | __d_drop(tmp); | |
285 | spin_unlock(&tmp->d_lock); | |
286 | spin_unlock(&dcache_lock); | |
287 | simple_unlink(parent->d_inode, tmp); | |
288 | } else { | |
289 | spin_unlock(&tmp->d_lock); | |
290 | spin_unlock(&dcache_lock); | |
291 | } | |
fae8773b BS |
292 | |
293 | ret = 0; | |
294 | bail: | |
295 | /* | |
296 | * We don't expect clients to care about the return value, but | |
297 | * it's there if they need it. | |
298 | */ | |
299 | return ret; | |
3e9b4a5e BS |
300 | } |
301 | ||
302 | static int remove_device_files(struct super_block *sb, | |
303 | struct ipath_devdata *dd) | |
304 | { | |
305 | struct dentry *dir, *root; | |
306 | char unit[10]; | |
307 | int ret; | |
308 | ||
309 | root = dget(sb->s_root); | |
310 | mutex_lock(&root->d_inode->i_mutex); | |
311 | snprintf(unit, sizeof unit, "%02d", dd->ipath_unit); | |
312 | dir = lookup_one_len(unit, root, strlen(unit)); | |
313 | ||
314 | if (IS_ERR(dir)) { | |
315 | ret = PTR_ERR(dir); | |
316 | printk(KERN_ERR "Lookup of %s failed\n", unit); | |
317 | goto bail; | |
318 | } | |
319 | ||
320 | remove_file(dir, "flash"); | |
3e9b4a5e BS |
321 | remove_file(dir, "atomic_counters"); |
322 | d_delete(dir); | |
323 | ret = simple_rmdir(root->d_inode, dir); | |
324 | ||
325 | bail: | |
326 | mutex_unlock(&root->d_inode->i_mutex); | |
327 | dput(root); | |
328 | return ret; | |
329 | } | |
330 | ||
331 | static int ipathfs_fill_super(struct super_block *sb, void *data, | |
332 | int silent) | |
333 | { | |
334 | struct ipath_devdata *dd, *tmp; | |
335 | unsigned long flags; | |
336 | int ret; | |
337 | ||
338 | static struct tree_descr files[] = { | |
1a1c9bb4 | 339 | [2] = {"atomic_stats", &atomic_stats_ops, S_IRUGO}, |
3e9b4a5e BS |
340 | {""}, |
341 | }; | |
342 | ||
343 | ret = simple_fill_super(sb, IPATHFS_MAGIC, files); | |
344 | if (ret) { | |
345 | printk(KERN_ERR "simple_fill_super failed: %d\n", ret); | |
346 | goto bail; | |
347 | } | |
348 | ||
349 | spin_lock_irqsave(&ipath_devs_lock, flags); | |
350 | ||
351 | list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) { | |
352 | spin_unlock_irqrestore(&ipath_devs_lock, flags); | |
353 | ret = create_device_files(sb, dd); | |
12e9a456 | 354 | if (ret) |
3e9b4a5e | 355 | goto bail; |
3e9b4a5e BS |
356 | spin_lock_irqsave(&ipath_devs_lock, flags); |
357 | } | |
358 | ||
359 | spin_unlock_irqrestore(&ipath_devs_lock, flags); | |
360 | ||
361 | bail: | |
362 | return ret; | |
363 | } | |
364 | ||
454e2398 DH |
365 | static int ipathfs_get_sb(struct file_system_type *fs_type, int flags, |
366 | const char *dev_name, void *data, struct vfsmount *mnt) | |
3e9b4a5e | 367 | { |
454e2398 DH |
368 | 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; | |
3e9b4a5e BS |
373 | } |
374 | ||
375 | static void ipathfs_kill_super(struct super_block *s) | |
376 | { | |
377 | kill_litter_super(s); | |
378 | ipath_super = NULL; | |
379 | } | |
380 | ||
381 | int 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 | ||
392 | bail: | |
393 | return ret; | |
394 | } | |
395 | ||
396 | int 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 | ||
407 | bail: | |
408 | return ret; | |
409 | } | |
410 | ||
411 | static 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 | ||
418 | int __init ipath_init_ipathfs(void) | |
419 | { | |
420 | return register_filesystem(&ipathfs_fs_type); | |
421 | } | |
422 | ||
423 | void __exit ipath_exit_ipathfs(void) | |
424 | { | |
425 | unregister_filesystem(&ipathfs_fs_type); | |
426 | } |