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