Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mfashe...
[sfrench/cifs-2.6.git] / drivers / infiniband / hw / ipath / ipath_fs.c
1 /*
2  * Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved.
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
34 #include <linux/version.h>
35 #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>
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,
49                          int mode, const struct file_operations *fops,
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
60         inode->i_mode = mode;
61         inode->i_uid = 0;
62         inode->i_gid = 0;
63         inode->i_blocks = 0;
64         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
65         inode->i_private = data;
66         if ((mode & S_IFMT) == S_IFDIR) {
67                 inode->i_op = &simple_dir_inode_operations;
68                 inc_nlink(inode);
69                 inc_nlink(dir);
70         }
71
72         inode->i_fop = fops;
73
74         d_instantiate(dentry, inode);
75         error = 0;
76
77 bail:
78         return error;
79 }
80
81 static int create_file(const char *name, mode_t mode,
82                        struct dentry *parent, struct dentry **dentry,
83                        const struct file_operations *fops, void *data)
84 {
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
100 static 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
107 static const struct file_operations atomic_stats_ops = {
108         .read = atomic_stats_read,
109 };
110
111 static ssize_t atomic_counters_read(struct file *file, char __user *buf,
112                                     size_t count, loff_t *ppos)
113 {
114         struct infinipath_counters counters;
115         struct ipath_devdata *dd;
116
117         dd = file->f_path.dentry->d_inode->i_private;
118         dd->ipath_f_read_counters(dd, &counters);
119
120         return simple_read_from_buffer(buf, count, ppos, &counters,
121                                        sizeof counters);
122 }
123
124 static const struct file_operations atomic_counters_ops = {
125         .read = atomic_counters_read,
126 };
127
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
157         dd = file->f_path.dentry->d_inode->i_private;
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
189         if (pos != 0) {
190                 ret = -EINVAL;
191                 goto bail;
192         }
193
194         if (count != sizeof(struct ipath_flash)) {
195                 ret = -EINVAL;
196                 goto bail;
197         }
198
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
210         dd = file->f_path.dentry->d_inode->i_private;
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
227 static const struct file_operations flash_ops = {
228         .read = flash_read,
229         .write = flash_write,
230 };
231
232 static int create_device_files(struct super_block *sb,
233                                struct ipath_devdata *dd)
234 {
235         struct dentry *dir, *tmp;
236         char unit[10];
237         int ret;
238
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;
245         }
246
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;
253         }
254
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;
261         }
262
263 bail:
264         return ret;
265 }
266
267 static int remove_file(struct dentry *parent, char *name)
268 {
269         struct dentry *tmp;
270         int ret;
271
272         tmp = lookup_one_len(name, parent, strlen(name));
273
274         if (IS_ERR(tmp)) {
275                 ret = PTR_ERR(tmp);
276                 goto bail;
277         }
278
279         spin_lock(&dcache_lock);
280         spin_lock(&tmp->d_lock);
281         if (!(d_unhashed(tmp) && tmp->d_inode)) {
282                 dget_locked(tmp);
283                 __d_drop(tmp);
284                 spin_unlock(&tmp->d_lock);
285                 spin_unlock(&dcache_lock);
286                 simple_unlink(parent->d_inode, tmp);
287         } else {
288                 spin_unlock(&tmp->d_lock);
289                 spin_unlock(&dcache_lock);
290         }
291
292         ret = 0;
293 bail:
294         /*
295          * We don't expect clients to care about the return value, but
296          * it's there if they need it.
297          */
298         return ret;
299 }
300
301 static int remove_device_files(struct super_block *sb,
302                                struct ipath_devdata *dd)
303 {
304         struct dentry *dir, *root;
305         char unit[10];
306         int ret;
307
308         root = dget(sb->s_root);
309         mutex_lock(&root->d_inode->i_mutex);
310         snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
311         dir = lookup_one_len(unit, root, strlen(unit));
312
313         if (IS_ERR(dir)) {
314                 ret = PTR_ERR(dir);
315                 printk(KERN_ERR "Lookup of %s failed\n", unit);
316                 goto bail;
317         }
318
319         remove_file(dir, "flash");
320         remove_file(dir, "atomic_counters");
321         d_delete(dir);
322         ret = simple_rmdir(root->d_inode, dir);
323
324 bail:
325         mutex_unlock(&root->d_inode->i_mutex);
326         dput(root);
327         return ret;
328 }
329
330 static int ipathfs_fill_super(struct super_block *sb, void *data,
331                               int silent)
332 {
333         struct ipath_devdata *dd, *tmp;
334         unsigned long flags;
335         int ret;
336
337         static struct tree_descr files[] = {
338                 [2] = {"atomic_stats", &atomic_stats_ops, S_IRUGO},
339                 {""},
340         };
341
342         ret = simple_fill_super(sb, IPATHFS_MAGIC, files);
343         if (ret) {
344                 printk(KERN_ERR "simple_fill_super failed: %d\n", ret);
345                 goto bail;
346         }
347
348         spin_lock_irqsave(&ipath_devs_lock, flags);
349
350         list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
351                 spin_unlock_irqrestore(&ipath_devs_lock, flags);
352                 ret = create_device_files(sb, dd);
353                 if (ret) {
354                         deactivate_super(sb);
355                         goto bail;
356                 }
357                 spin_lock_irqsave(&ipath_devs_lock, flags);
358         }
359
360         spin_unlock_irqrestore(&ipath_devs_lock, flags);
361
362 bail:
363         return ret;
364 }
365
366 static int ipathfs_get_sb(struct file_system_type *fs_type, int flags,
367                         const char *dev_name, void *data, struct vfsmount *mnt)
368 {
369         int ret = get_sb_single(fs_type, flags, data,
370                                     ipathfs_fill_super, mnt);
371         if (ret >= 0)
372                 ipath_super = mnt->mnt_sb;
373         return ret;
374 }
375
376 static void ipathfs_kill_super(struct super_block *s)
377 {
378         kill_litter_super(s);
379         ipath_super = NULL;
380 }
381
382 int ipathfs_add_device(struct ipath_devdata *dd)
383 {
384         int ret;
385
386         if (ipath_super == NULL) {
387                 ret = 0;
388                 goto bail;
389         }
390
391         ret = create_device_files(ipath_super, dd);
392
393 bail:
394         return ret;
395 }
396
397 int ipathfs_remove_device(struct ipath_devdata *dd)
398 {
399         int ret;
400
401         if (ipath_super == NULL) {
402                 ret = 0;
403                 goto bail;
404         }
405
406         ret = remove_device_files(ipath_super, dd);
407
408 bail:
409         return ret;
410 }
411
412 static struct file_system_type ipathfs_fs_type = {
413         .owner =        THIS_MODULE,
414         .name =         "ipathfs",
415         .get_sb =       ipathfs_get_sb,
416         .kill_sb =      ipathfs_kill_super,
417 };
418
419 int __init ipath_init_ipathfs(void)
420 {
421         return register_filesystem(&ipathfs_fs_type);
422 }
423
424 void __exit ipath_exit_ipathfs(void)
425 {
426         unregister_filesystem(&ipathfs_fs_type);
427 }