Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 1 | /* |
John Gregor | 87427da | 2007-06-11 10:21:14 -0700 | [diff] [blame] | 2 | * Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved. |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 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 | |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 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> |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 40 | |
| 41 | #include "ipath_kernel.h" |
| 42 | |
| 43 | #define IPATHFS_MAGIC 0x726a77 |
| 44 | |
| 45 | static struct super_block *ipath_super; |
| 46 | |
| 47 | static int ipathfs_mknod(struct inode *dir, struct dentry *dentry, |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 48 | int mode, const struct file_operations *fops, |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 49 | 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; |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 60 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 61 | inode->i_private = data; |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 62 | if ((mode & S_IFMT) == S_IFDIR) { |
| 63 | inode->i_op = &simple_dir_inode_operations; |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 64 | inc_nlink(inode); |
| 65 | inc_nlink(dir); |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | inode->i_fop = fops; |
| 69 | |
| 70 | d_instantiate(dentry, inode); |
| 71 | error = 0; |
| 72 | |
| 73 | bail: |
| 74 | return error; |
| 75 | } |
| 76 | |
| 77 | static int create_file(const char *name, mode_t mode, |
| 78 | struct dentry *parent, struct dentry **dentry, |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 79 | const struct file_operations *fops, void *data) |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 80 | { |
| 81 | int error; |
| 82 | |
| 83 | *dentry = NULL; |
| 84 | mutex_lock(&parent->d_inode->i_mutex); |
| 85 | *dentry = lookup_one_len(name, parent, strlen(name)); |
Michael Ellerman | 64f22fa | 2008-12-01 20:59:07 -0800 | [diff] [blame] | 86 | if (!IS_ERR(*dentry)) |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 87 | error = ipathfs_mknod(parent->d_inode, *dentry, |
| 88 | mode, fops, data); |
| 89 | else |
| 90 | error = PTR_ERR(dentry); |
| 91 | mutex_unlock(&parent->d_inode->i_mutex); |
| 92 | |
| 93 | return error; |
| 94 | } |
| 95 | |
| 96 | static ssize_t atomic_stats_read(struct file *file, char __user *buf, |
| 97 | size_t count, loff_t *ppos) |
| 98 | { |
| 99 | return simple_read_from_buffer(buf, count, ppos, &ipath_stats, |
| 100 | sizeof ipath_stats); |
| 101 | } |
| 102 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 103 | static const struct file_operations atomic_stats_ops = { |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 104 | .read = atomic_stats_read, |
| 105 | }; |
| 106 | |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 107 | static ssize_t atomic_counters_read(struct file *file, char __user *buf, |
| 108 | size_t count, loff_t *ppos) |
| 109 | { |
Ralph Campbell | 3029fcc | 2008-01-06 21:02:34 -0800 | [diff] [blame] | 110 | struct infinipath_counters counters; |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 111 | struct ipath_devdata *dd; |
| 112 | |
Josef Sipek | 1cfd6e6 | 2006-12-08 02:37:10 -0800 | [diff] [blame] | 113 | dd = file->f_path.dentry->d_inode->i_private; |
Ralph Campbell | 3029fcc | 2008-01-06 21:02:34 -0800 | [diff] [blame] | 114 | dd->ipath_f_read_counters(dd, &counters); |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 115 | |
Ralph Campbell | 3029fcc | 2008-01-06 21:02:34 -0800 | [diff] [blame] | 116 | return simple_read_from_buffer(buf, count, ppos, &counters, |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 117 | sizeof counters); |
| 118 | } |
| 119 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 120 | static const struct file_operations atomic_counters_ops = { |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 121 | .read = atomic_counters_read, |
| 122 | }; |
| 123 | |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 124 | static ssize_t flash_read(struct file *file, char __user *buf, |
| 125 | size_t count, loff_t *ppos) |
| 126 | { |
| 127 | struct ipath_devdata *dd; |
| 128 | ssize_t ret; |
| 129 | loff_t pos; |
| 130 | char *tmp; |
| 131 | |
| 132 | pos = *ppos; |
| 133 | |
| 134 | if ( pos < 0) { |
| 135 | ret = -EINVAL; |
| 136 | goto bail; |
| 137 | } |
| 138 | |
| 139 | if (pos >= sizeof(struct ipath_flash)) { |
| 140 | ret = 0; |
| 141 | goto bail; |
| 142 | } |
| 143 | |
| 144 | if (count > sizeof(struct ipath_flash) - pos) |
| 145 | count = sizeof(struct ipath_flash) - pos; |
| 146 | |
| 147 | tmp = kmalloc(count, GFP_KERNEL); |
| 148 | if (!tmp) { |
| 149 | ret = -ENOMEM; |
| 150 | goto bail; |
| 151 | } |
| 152 | |
Josef Sipek | 1cfd6e6 | 2006-12-08 02:37:10 -0800 | [diff] [blame] | 153 | dd = file->f_path.dentry->d_inode->i_private; |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 154 | if (ipath_eeprom_read(dd, pos, tmp, count)) { |
| 155 | ipath_dev_err(dd, "failed to read from flash\n"); |
| 156 | ret = -ENXIO; |
| 157 | goto bail_tmp; |
| 158 | } |
| 159 | |
| 160 | if (copy_to_user(buf, tmp, count)) { |
| 161 | ret = -EFAULT; |
| 162 | goto bail_tmp; |
| 163 | } |
| 164 | |
| 165 | *ppos = pos + count; |
| 166 | ret = count; |
| 167 | |
| 168 | bail_tmp: |
| 169 | kfree(tmp); |
| 170 | |
| 171 | bail: |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | static ssize_t flash_write(struct file *file, const char __user *buf, |
| 176 | size_t count, loff_t *ppos) |
| 177 | { |
| 178 | struct ipath_devdata *dd; |
| 179 | ssize_t ret; |
| 180 | loff_t pos; |
| 181 | char *tmp; |
| 182 | |
| 183 | pos = *ppos; |
| 184 | |
Bryan O'Sullivan | 7dae5bf | 2006-09-28 09:00:05 -0700 | [diff] [blame] | 185 | if (pos != 0) { |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 186 | ret = -EINVAL; |
| 187 | goto bail; |
| 188 | } |
| 189 | |
Bryan O'Sullivan | 7dae5bf | 2006-09-28 09:00:05 -0700 | [diff] [blame] | 190 | if (count != sizeof(struct ipath_flash)) { |
| 191 | ret = -EINVAL; |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 192 | goto bail; |
| 193 | } |
| 194 | |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 195 | tmp = kmalloc(count, GFP_KERNEL); |
| 196 | if (!tmp) { |
| 197 | ret = -ENOMEM; |
| 198 | goto bail; |
| 199 | } |
| 200 | |
| 201 | if (copy_from_user(tmp, buf, count)) { |
| 202 | ret = -EFAULT; |
| 203 | goto bail_tmp; |
| 204 | } |
| 205 | |
Josef Sipek | 1cfd6e6 | 2006-12-08 02:37:10 -0800 | [diff] [blame] | 206 | dd = file->f_path.dentry->d_inode->i_private; |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 207 | if (ipath_eeprom_write(dd, pos, tmp, count)) { |
| 208 | ret = -ENXIO; |
| 209 | ipath_dev_err(dd, "failed to write to flash\n"); |
| 210 | goto bail_tmp; |
| 211 | } |
| 212 | |
| 213 | *ppos = pos + count; |
| 214 | ret = count; |
| 215 | |
| 216 | bail_tmp: |
| 217 | kfree(tmp); |
| 218 | |
| 219 | bail: |
| 220 | return ret; |
| 221 | } |
| 222 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 223 | static const struct file_operations flash_ops = { |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 224 | .read = flash_read, |
| 225 | .write = flash_write, |
| 226 | }; |
| 227 | |
| 228 | static int create_device_files(struct super_block *sb, |
| 229 | struct ipath_devdata *dd) |
| 230 | { |
| 231 | struct dentry *dir, *tmp; |
| 232 | char unit[10]; |
| 233 | int ret; |
| 234 | |
| 235 | snprintf(unit, sizeof unit, "%02d", dd->ipath_unit); |
| 236 | ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir, |
Jan Engelhardt | f7fca1e | 2008-01-22 20:45:30 +0100 | [diff] [blame] | 237 | &simple_dir_operations, dd); |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 238 | if (ret) { |
| 239 | printk(KERN_ERR "create_file(%s) failed: %d\n", unit, ret); |
| 240 | goto bail; |
| 241 | } |
| 242 | |
| 243 | ret = create_file("atomic_counters", S_IFREG|S_IRUGO, dir, &tmp, |
| 244 | &atomic_counters_ops, dd); |
| 245 | if (ret) { |
| 246 | printk(KERN_ERR "create_file(%s/atomic_counters) " |
| 247 | "failed: %d\n", unit, ret); |
| 248 | goto bail; |
| 249 | } |
| 250 | |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 251 | ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp, |
| 252 | &flash_ops, dd); |
| 253 | if (ret) { |
| 254 | printk(KERN_ERR "create_file(%s/flash) " |
| 255 | "failed: %d\n", unit, ret); |
| 256 | goto bail; |
| 257 | } |
| 258 | |
| 259 | bail: |
| 260 | return ret; |
| 261 | } |
| 262 | |
Bryan O'Sullivan | fae8773 | 2007-03-21 15:18:14 -0700 | [diff] [blame] | 263 | static int remove_file(struct dentry *parent, char *name) |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 264 | { |
| 265 | struct dentry *tmp; |
Bryan O'Sullivan | fae8773 | 2007-03-21 15:18:14 -0700 | [diff] [blame] | 266 | int ret; |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 267 | |
| 268 | tmp = lookup_one_len(name, parent, strlen(name)); |
| 269 | |
Bryan O'Sullivan | fae8773 | 2007-03-21 15:18:14 -0700 | [diff] [blame] | 270 | if (IS_ERR(tmp)) { |
| 271 | ret = PTR_ERR(tmp); |
| 272 | goto bail; |
| 273 | } |
| 274 | |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 275 | spin_lock(&dcache_lock); |
| 276 | spin_lock(&tmp->d_lock); |
| 277 | if (!(d_unhashed(tmp) && tmp->d_inode)) { |
| 278 | dget_locked(tmp); |
| 279 | __d_drop(tmp); |
| 280 | spin_unlock(&tmp->d_lock); |
| 281 | spin_unlock(&dcache_lock); |
| 282 | simple_unlink(parent->d_inode, tmp); |
| 283 | } else { |
| 284 | spin_unlock(&tmp->d_lock); |
| 285 | spin_unlock(&dcache_lock); |
| 286 | } |
Bryan O'Sullivan | fae8773 | 2007-03-21 15:18:14 -0700 | [diff] [blame] | 287 | |
| 288 | ret = 0; |
| 289 | bail: |
| 290 | /* |
| 291 | * We don't expect clients to care about the return value, but |
| 292 | * it's there if they need it. |
| 293 | */ |
| 294 | return ret; |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | static int remove_device_files(struct super_block *sb, |
| 298 | struct ipath_devdata *dd) |
| 299 | { |
| 300 | struct dentry *dir, *root; |
| 301 | char unit[10]; |
| 302 | int ret; |
| 303 | |
| 304 | root = dget(sb->s_root); |
| 305 | mutex_lock(&root->d_inode->i_mutex); |
| 306 | snprintf(unit, sizeof unit, "%02d", dd->ipath_unit); |
| 307 | dir = lookup_one_len(unit, root, strlen(unit)); |
| 308 | |
| 309 | if (IS_ERR(dir)) { |
| 310 | ret = PTR_ERR(dir); |
| 311 | printk(KERN_ERR "Lookup of %s failed\n", unit); |
| 312 | goto bail; |
| 313 | } |
| 314 | |
| 315 | remove_file(dir, "flash"); |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 316 | remove_file(dir, "atomic_counters"); |
| 317 | d_delete(dir); |
| 318 | ret = simple_rmdir(root->d_inode, dir); |
| 319 | |
| 320 | bail: |
| 321 | mutex_unlock(&root->d_inode->i_mutex); |
| 322 | dput(root); |
| 323 | return ret; |
| 324 | } |
| 325 | |
| 326 | static int ipathfs_fill_super(struct super_block *sb, void *data, |
| 327 | int silent) |
| 328 | { |
| 329 | struct ipath_devdata *dd, *tmp; |
| 330 | unsigned long flags; |
| 331 | int ret; |
| 332 | |
| 333 | static struct tree_descr files[] = { |
Jeff Layton | 1a1c9bb | 2007-05-08 00:32:31 -0700 | [diff] [blame] | 334 | [2] = {"atomic_stats", &atomic_stats_ops, S_IRUGO}, |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 335 | {""}, |
| 336 | }; |
| 337 | |
| 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; |
| 342 | } |
| 343 | |
| 344 | spin_lock_irqsave(&ipath_devs_lock, flags); |
| 345 | |
| 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); |
Al Viro | 12e9a45 | 2010-01-25 18:44:58 -0500 | [diff] [blame^] | 349 | if (ret) |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 350 | goto bail; |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 351 | spin_lock_irqsave(&ipath_devs_lock, flags); |
| 352 | } |
| 353 | |
| 354 | spin_unlock_irqrestore(&ipath_devs_lock, flags); |
| 355 | |
| 356 | bail: |
| 357 | return ret; |
| 358 | } |
| 359 | |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 360 | static int ipathfs_get_sb(struct file_system_type *fs_type, int flags, |
| 361 | const char *dev_name, void *data, struct vfsmount *mnt) |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 362 | { |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 363 | int ret = get_sb_single(fs_type, flags, data, |
| 364 | ipathfs_fill_super, mnt); |
| 365 | if (ret >= 0) |
| 366 | ipath_super = mnt->mnt_sb; |
| 367 | return ret; |
Bryan O'Sullivan | 3e9b4a5 | 2006-03-29 15:23:30 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static void ipathfs_kill_super(struct super_block *s) |
| 371 | { |
| 372 | kill_litter_super(s); |
| 373 | ipath_super = NULL; |
| 374 | } |
| 375 | |
| 376 | int ipathfs_add_device(struct ipath_devdata *dd) |
| 377 | { |
| 378 | int ret; |
| 379 | |
| 380 | if (ipath_super == NULL) { |
| 381 | ret = 0; |
| 382 | goto bail; |
| 383 | } |
| 384 | |
| 385 | ret = create_device_files(ipath_super, dd); |
| 386 | |
| 387 | bail: |
| 388 | return ret; |
| 389 | } |
| 390 | |
| 391 | int ipathfs_remove_device(struct ipath_devdata *dd) |
| 392 | { |
| 393 | int ret; |
| 394 | |
| 395 | if (ipath_super == NULL) { |
| 396 | ret = 0; |
| 397 | goto bail; |
| 398 | } |
| 399 | |
| 400 | ret = remove_device_files(ipath_super, dd); |
| 401 | |
| 402 | bail: |
| 403 | return ret; |
| 404 | } |
| 405 | |
| 406 | static struct file_system_type ipathfs_fs_type = { |
| 407 | .owner = THIS_MODULE, |
| 408 | .name = "ipathfs", |
| 409 | .get_sb = ipathfs_get_sb, |
| 410 | .kill_sb = ipathfs_kill_super, |
| 411 | }; |
| 412 | |
| 413 | int __init ipath_init_ipathfs(void) |
| 414 | { |
| 415 | return register_filesystem(&ipathfs_fs_type); |
| 416 | } |
| 417 | |
| 418 | void __exit ipath_exit_ipathfs(void) |
| 419 | { |
| 420 | unregister_filesystem(&ipathfs_fs_type); |
| 421 | } |