Linux 4.1.16
[linux/fpc-iii.git] / drivers / infiniband / hw / ipath / ipath_fs.c
blob1ca8e32a95927961a326f33bae480c11a02a226f
1 /*
2 * Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved.
3 * Copyright (c) 2006 PathScale, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
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.
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.
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>
40 #include <linux/slab.h>
42 #include "ipath_kernel.h"
44 #define IPATHFS_MAGIC 0x726a77
46 static struct super_block *ipath_super;
48 static int ipathfs_mknod(struct inode *dir, struct dentry *dentry,
49 umode_t mode, const struct file_operations *fops,
50 void *data)
52 int error;
53 struct inode *inode = new_inode(dir->i_sb);
55 if (!inode) {
56 error = -EPERM;
57 goto bail;
60 inode->i_ino = get_next_ino();
61 inode->i_mode = mode;
62 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
63 inode->i_private = data;
64 if (S_ISDIR(mode)) {
65 inode->i_op = &simple_dir_inode_operations;
66 inc_nlink(inode);
67 inc_nlink(dir);
70 inode->i_fop = fops;
72 d_instantiate(dentry, inode);
73 error = 0;
75 bail:
76 return error;
79 static int create_file(const char *name, umode_t mode,
80 struct dentry *parent, struct dentry **dentry,
81 const struct file_operations *fops, void *data)
83 int error;
85 mutex_lock(&d_inode(parent)->i_mutex);
86 *dentry = lookup_one_len(name, parent, strlen(name));
87 if (!IS_ERR(*dentry))
88 error = ipathfs_mknod(d_inode(parent), *dentry,
89 mode, fops, data);
90 else
91 error = PTR_ERR(*dentry);
92 mutex_unlock(&d_inode(parent)->i_mutex);
94 return error;
97 static ssize_t atomic_stats_read(struct file *file, char __user *buf,
98 size_t count, loff_t *ppos)
100 return simple_read_from_buffer(buf, count, ppos, &ipath_stats,
101 sizeof ipath_stats);
104 static const struct file_operations atomic_stats_ops = {
105 .read = atomic_stats_read,
106 .llseek = default_llseek,
109 static ssize_t atomic_counters_read(struct file *file, char __user *buf,
110 size_t count, loff_t *ppos)
112 struct infinipath_counters counters;
113 struct ipath_devdata *dd;
115 dd = file_inode(file)->i_private;
116 dd->ipath_f_read_counters(dd, &counters);
118 return simple_read_from_buffer(buf, count, ppos, &counters,
119 sizeof counters);
122 static const struct file_operations atomic_counters_ops = {
123 .read = atomic_counters_read,
124 .llseek = default_llseek,
127 static ssize_t flash_read(struct file *file, char __user *buf,
128 size_t count, loff_t *ppos)
130 struct ipath_devdata *dd;
131 ssize_t ret;
132 loff_t pos;
133 char *tmp;
135 pos = *ppos;
137 if ( pos < 0) {
138 ret = -EINVAL;
139 goto bail;
142 if (pos >= sizeof(struct ipath_flash)) {
143 ret = 0;
144 goto bail;
147 if (count > sizeof(struct ipath_flash) - pos)
148 count = sizeof(struct ipath_flash) - pos;
150 tmp = kmalloc(count, GFP_KERNEL);
151 if (!tmp) {
152 ret = -ENOMEM;
153 goto bail;
156 dd = file_inode(file)->i_private;
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;
163 if (copy_to_user(buf, tmp, count)) {
164 ret = -EFAULT;
165 goto bail_tmp;
168 *ppos = pos + count;
169 ret = count;
171 bail_tmp:
172 kfree(tmp);
174 bail:
175 return ret;
178 static ssize_t flash_write(struct file *file, const char __user *buf,
179 size_t count, loff_t *ppos)
181 struct ipath_devdata *dd;
182 ssize_t ret;
183 loff_t pos;
184 char *tmp;
186 pos = *ppos;
188 if (pos != 0) {
189 ret = -EINVAL;
190 goto bail;
193 if (count != sizeof(struct ipath_flash)) {
194 ret = -EINVAL;
195 goto bail;
198 tmp = kmalloc(count, GFP_KERNEL);
199 if (!tmp) {
200 ret = -ENOMEM;
201 goto bail;
204 if (copy_from_user(tmp, buf, count)) {
205 ret = -EFAULT;
206 goto bail_tmp;
209 dd = file_inode(file)->i_private;
210 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;
216 *ppos = pos + count;
217 ret = count;
219 bail_tmp:
220 kfree(tmp);
222 bail:
223 return ret;
226 static const struct file_operations flash_ops = {
227 .read = flash_read,
228 .write = flash_write,
229 .llseek = default_llseek,
232 static int create_device_files(struct super_block *sb,
233 struct ipath_devdata *dd)
235 struct dentry *dir, *tmp;
236 char unit[10];
237 int ret;
239 snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
240 ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir,
241 &simple_dir_operations, dd);
242 if (ret) {
243 printk(KERN_ERR "create_file(%s) failed: %d\n", unit, ret);
244 goto bail;
247 ret = create_file("atomic_counters", S_IFREG|S_IRUGO, dir, &tmp,
248 &atomic_counters_ops, dd);
249 if (ret) {
250 printk(KERN_ERR "create_file(%s/atomic_counters) "
251 "failed: %d\n", unit, ret);
252 goto bail;
255 ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp,
256 &flash_ops, dd);
257 if (ret) {
258 printk(KERN_ERR "create_file(%s/flash) "
259 "failed: %d\n", unit, ret);
260 goto bail;
263 bail:
264 return ret;
267 static int remove_file(struct dentry *parent, char *name)
269 struct dentry *tmp;
270 int ret;
272 tmp = lookup_one_len(name, parent, strlen(name));
274 if (IS_ERR(tmp)) {
275 ret = PTR_ERR(tmp);
276 goto bail;
279 spin_lock(&tmp->d_lock);
280 if (!d_unhashed(tmp) && d_really_is_positive(tmp)) {
281 dget_dlock(tmp);
282 __d_drop(tmp);
283 spin_unlock(&tmp->d_lock);
284 simple_unlink(d_inode(parent), tmp);
285 } else
286 spin_unlock(&tmp->d_lock);
288 ret = 0;
289 bail:
291 * We don't expect clients to care about the return value, but
292 * it's there if they need it.
294 return ret;
297 static int remove_device_files(struct super_block *sb,
298 struct ipath_devdata *dd)
300 struct dentry *dir, *root;
301 char unit[10];
302 int ret;
304 root = dget(sb->s_root);
305 mutex_lock(&d_inode(root)->i_mutex);
306 snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
307 dir = lookup_one_len(unit, root, strlen(unit));
309 if (IS_ERR(dir)) {
310 ret = PTR_ERR(dir);
311 printk(KERN_ERR "Lookup of %s failed\n", unit);
312 goto bail;
315 remove_file(dir, "flash");
316 remove_file(dir, "atomic_counters");
317 d_delete(dir);
318 ret = simple_rmdir(d_inode(root), dir);
320 bail:
321 mutex_unlock(&d_inode(root)->i_mutex);
322 dput(root);
323 return ret;
326 static int ipathfs_fill_super(struct super_block *sb, void *data,
327 int silent)
329 struct ipath_devdata *dd, *tmp;
330 unsigned long flags;
331 int ret;
333 static struct tree_descr files[] = {
334 [2] = {"atomic_stats", &atomic_stats_ops, S_IRUGO},
335 {""},
338 ret = simple_fill_super(sb, IPATHFS_MAGIC, files);
339 if (ret) {
340 printk(KERN_ERR "simple_fill_super failed: %d\n", ret);
341 goto bail;
344 spin_lock_irqsave(&ipath_devs_lock, flags);
346 list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
347 spin_unlock_irqrestore(&ipath_devs_lock, flags);
348 ret = create_device_files(sb, dd);
349 if (ret)
350 goto bail;
351 spin_lock_irqsave(&ipath_devs_lock, flags);
354 spin_unlock_irqrestore(&ipath_devs_lock, flags);
356 bail:
357 return ret;
360 static struct dentry *ipathfs_mount(struct file_system_type *fs_type,
361 int flags, const char *dev_name, void *data)
363 struct dentry *ret;
364 ret = mount_single(fs_type, flags, data, ipathfs_fill_super);
365 if (!IS_ERR(ret))
366 ipath_super = ret->d_sb;
367 return ret;
370 static void ipathfs_kill_super(struct super_block *s)
372 kill_litter_super(s);
373 ipath_super = NULL;
376 int ipathfs_add_device(struct ipath_devdata *dd)
378 int ret;
380 if (ipath_super == NULL) {
381 ret = 0;
382 goto bail;
385 ret = create_device_files(ipath_super, dd);
387 bail:
388 return ret;
391 int ipathfs_remove_device(struct ipath_devdata *dd)
393 int ret;
395 if (ipath_super == NULL) {
396 ret = 0;
397 goto bail;
400 ret = remove_device_files(ipath_super, dd);
402 bail:
403 return ret;
406 static struct file_system_type ipathfs_fs_type = {
407 .owner = THIS_MODULE,
408 .name = "ipathfs",
409 .mount = ipathfs_mount,
410 .kill_sb = ipathfs_kill_super,
412 MODULE_ALIAS_FS("ipathfs");
414 int __init ipath_init_ipathfs(void)
416 return register_filesystem(&ipathfs_fs_type);
419 void __exit ipath_exit_ipathfs(void)
421 unregister_filesystem(&ipathfs_fs_type);