fs: dcache rationalise dget variants
[deliverable/linux.git] / drivers / infiniband / hw / qib / qib_fs.c
CommitLineData
f931551b
RC
1/*
2 * Copyright (c) 2006, 2007, 2008, 2009 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
41#include "qib.h"
42
43#define QIBFS_MAGIC 0x726a77
44
45static struct super_block *qib_super;
46
47#define private2dd(file) ((file)->f_dentry->d_inode->i_private)
48
49static int qibfs_mknod(struct inode *dir, struct dentry *dentry,
50 int mode, const struct file_operations *fops,
51 void *data)
52{
53 int error;
54 struct inode *inode = new_inode(dir->i_sb);
55
56 if (!inode) {
57 error = -EPERM;
58 goto bail;
59 }
60
85fe4025 61 inode->i_ino = get_next_ino();
f931551b
RC
62 inode->i_mode = mode;
63 inode->i_uid = 0;
64 inode->i_gid = 0;
65 inode->i_blocks = 0;
66 inode->i_atime = CURRENT_TIME;
67 inode->i_mtime = inode->i_atime;
68 inode->i_ctime = inode->i_atime;
69 inode->i_private = data;
70 if ((mode & S_IFMT) == S_IFDIR) {
71 inode->i_op = &simple_dir_inode_operations;
72 inc_nlink(inode);
73 inc_nlink(dir);
74 }
75
76 inode->i_fop = fops;
77
78 d_instantiate(dentry, inode);
79 error = 0;
80
81bail:
82 return error;
83}
84
85static int create_file(const char *name, mode_t mode,
86 struct dentry *parent, struct dentry **dentry,
87 const struct file_operations *fops, void *data)
88{
89 int error;
90
91 *dentry = NULL;
92 mutex_lock(&parent->d_inode->i_mutex);
93 *dentry = lookup_one_len(name, parent, strlen(name));
94 if (!IS_ERR(*dentry))
95 error = qibfs_mknod(parent->d_inode, *dentry,
96 mode, fops, data);
97 else
98 error = PTR_ERR(*dentry);
99 mutex_unlock(&parent->d_inode->i_mutex);
100
101 return error;
102}
103
104static ssize_t driver_stats_read(struct file *file, char __user *buf,
105 size_t count, loff_t *ppos)
106{
107 return simple_read_from_buffer(buf, count, ppos, &qib_stats,
108 sizeof qib_stats);
109}
110
111/*
112 * driver stats field names, one line per stat, single string. Used by
113 * programs like ipathstats to print the stats in a way which works for
114 * different versions of drivers, without changing program source.
115 * if qlogic_ib_stats changes, this needs to change. Names need to be
116 * 12 chars or less (w/o newline), for proper display by ipathstats utility.
117 */
118static const char qib_statnames[] =
119 "KernIntr\n"
120 "ErrorIntr\n"
121 "Tx_Errs\n"
122 "Rcv_Errs\n"
123 "H/W_Errs\n"
124 "NoPIOBufs\n"
125 "CtxtsOpen\n"
126 "RcvLen_Errs\n"
127 "EgrBufFull\n"
128 "EgrHdrFull\n"
129 ;
130
131static ssize_t driver_names_read(struct file *file, char __user *buf,
132 size_t count, loff_t *ppos)
133{
134 return simple_read_from_buffer(buf, count, ppos, qib_statnames,
135 sizeof qib_statnames - 1); /* no null */
136}
137
138static const struct file_operations driver_ops[] = {
dd378c21
AB
139 { .read = driver_stats_read, .llseek = generic_file_llseek, },
140 { .read = driver_names_read, .llseek = generic_file_llseek, },
f931551b
RC
141};
142
143/* read the per-device counters */
144static ssize_t dev_counters_read(struct file *file, char __user *buf,
145 size_t count, loff_t *ppos)
146{
147 u64 *counters;
f27ec1d6 148 size_t avail;
f931551b
RC
149 struct qib_devdata *dd = private2dd(file);
150
f27ec1d6
RD
151 avail = dd->f_read_cntrs(dd, *ppos, NULL, &counters);
152 return simple_read_from_buffer(buf, count, ppos, counters, avail);
f931551b
RC
153}
154
155/* read the per-device counters */
156static ssize_t dev_names_read(struct file *file, char __user *buf,
157 size_t count, loff_t *ppos)
158{
159 char *names;
f27ec1d6 160 size_t avail;
f931551b
RC
161 struct qib_devdata *dd = private2dd(file);
162
f27ec1d6
RD
163 avail = dd->f_read_cntrs(dd, *ppos, &names, NULL);
164 return simple_read_from_buffer(buf, count, ppos, names, avail);
f931551b
RC
165}
166
167static const struct file_operations cntr_ops[] = {
dd378c21
AB
168 { .read = dev_counters_read, .llseek = generic_file_llseek, },
169 { .read = dev_names_read, .llseek = generic_file_llseek, },
f931551b
RC
170};
171
172/*
173 * Could use file->f_dentry->d_inode->i_ino to figure out which file,
174 * instead of separate routine for each, but for now, this works...
175 */
176
177/* read the per-port names (same for each port) */
178static ssize_t portnames_read(struct file *file, char __user *buf,
179 size_t count, loff_t *ppos)
180{
181 char *names;
f27ec1d6 182 size_t avail;
f931551b
RC
183 struct qib_devdata *dd = private2dd(file);
184
f27ec1d6
RD
185 avail = dd->f_read_portcntrs(dd, *ppos, 0, &names, NULL);
186 return simple_read_from_buffer(buf, count, ppos, names, avail);
f931551b
RC
187}
188
189/* read the per-port counters for port 1 (pidx 0) */
190static ssize_t portcntrs_1_read(struct file *file, char __user *buf,
191 size_t count, loff_t *ppos)
192{
193 u64 *counters;
f27ec1d6 194 size_t avail;
f931551b
RC
195 struct qib_devdata *dd = private2dd(file);
196
f27ec1d6
RD
197 avail = dd->f_read_portcntrs(dd, *ppos, 0, NULL, &counters);
198 return simple_read_from_buffer(buf, count, ppos, counters, avail);
f931551b
RC
199}
200
201/* read the per-port counters for port 2 (pidx 1) */
202static ssize_t portcntrs_2_read(struct file *file, char __user *buf,
203 size_t count, loff_t *ppos)
204{
205 u64 *counters;
f27ec1d6 206 size_t avail;
f931551b
RC
207 struct qib_devdata *dd = private2dd(file);
208
f27ec1d6
RD
209 avail = dd->f_read_portcntrs(dd, *ppos, 1, NULL, &counters);
210 return simple_read_from_buffer(buf, count, ppos, counters, avail);
f931551b
RC
211}
212
213static const struct file_operations portcntr_ops[] = {
dd378c21
AB
214 { .read = portnames_read, .llseek = generic_file_llseek, },
215 { .read = portcntrs_1_read, .llseek = generic_file_llseek, },
216 { .read = portcntrs_2_read, .llseek = generic_file_llseek, },
f931551b
RC
217};
218
219/*
220 * read the per-port QSFP data for port 1 (pidx 0)
221 */
222static ssize_t qsfp_1_read(struct file *file, char __user *buf,
223 size_t count, loff_t *ppos)
224{
225 struct qib_devdata *dd = private2dd(file);
226 char *tmp;
227 int ret;
228
229 tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
230 if (!tmp)
231 return -ENOMEM;
232
233 ret = qib_qsfp_dump(dd->pport, tmp, PAGE_SIZE);
234 if (ret > 0)
235 ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
236 kfree(tmp);
237 return ret;
238}
239
240/*
241 * read the per-port QSFP data for port 2 (pidx 1)
242 */
243static ssize_t qsfp_2_read(struct file *file, char __user *buf,
244 size_t count, loff_t *ppos)
245{
246 struct qib_devdata *dd = private2dd(file);
247 char *tmp;
248 int ret;
249
250 if (dd->num_pports < 2)
251 return -ENODEV;
252
253 tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
254 if (!tmp)
255 return -ENOMEM;
256
257 ret = qib_qsfp_dump(dd->pport + 1, tmp, PAGE_SIZE);
258 if (ret > 0)
259 ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
260 kfree(tmp);
261 return ret;
262}
263
264static const struct file_operations qsfp_ops[] = {
dd378c21
AB
265 { .read = qsfp_1_read, .llseek = generic_file_llseek, },
266 { .read = qsfp_2_read, .llseek = generic_file_llseek, },
f931551b
RC
267};
268
269static ssize_t flash_read(struct file *file, char __user *buf,
270 size_t count, loff_t *ppos)
271{
272 struct qib_devdata *dd;
273 ssize_t ret;
274 loff_t pos;
275 char *tmp;
276
277 pos = *ppos;
278
279 if (pos < 0) {
280 ret = -EINVAL;
281 goto bail;
282 }
283
284 if (pos >= sizeof(struct qib_flash)) {
285 ret = 0;
286 goto bail;
287 }
288
289 if (count > sizeof(struct qib_flash) - pos)
290 count = sizeof(struct qib_flash) - pos;
291
292 tmp = kmalloc(count, GFP_KERNEL);
293 if (!tmp) {
294 ret = -ENOMEM;
295 goto bail;
296 }
297
298 dd = private2dd(file);
299 if (qib_eeprom_read(dd, pos, tmp, count)) {
300 qib_dev_err(dd, "failed to read from flash\n");
301 ret = -ENXIO;
302 goto bail_tmp;
303 }
304
305 if (copy_to_user(buf, tmp, count)) {
306 ret = -EFAULT;
307 goto bail_tmp;
308 }
309
310 *ppos = pos + count;
311 ret = count;
312
313bail_tmp:
314 kfree(tmp);
315
316bail:
317 return ret;
318}
319
320static ssize_t flash_write(struct file *file, const char __user *buf,
321 size_t count, loff_t *ppos)
322{
323 struct qib_devdata *dd;
324 ssize_t ret;
325 loff_t pos;
326 char *tmp;
327
328 pos = *ppos;
329
330 if (pos != 0) {
331 ret = -EINVAL;
332 goto bail;
333 }
334
335 if (count != sizeof(struct qib_flash)) {
336 ret = -EINVAL;
337 goto bail;
338 }
339
340 tmp = kmalloc(count, GFP_KERNEL);
341 if (!tmp) {
342 ret = -ENOMEM;
343 goto bail;
344 }
345
346 if (copy_from_user(tmp, buf, count)) {
347 ret = -EFAULT;
348 goto bail_tmp;
349 }
350
351 dd = private2dd(file);
352 if (qib_eeprom_write(dd, pos, tmp, count)) {
353 ret = -ENXIO;
354 qib_dev_err(dd, "failed to write to flash\n");
355 goto bail_tmp;
356 }
357
358 *ppos = pos + count;
359 ret = count;
360
361bail_tmp:
362 kfree(tmp);
363
364bail:
365 return ret;
366}
367
368static const struct file_operations flash_ops = {
369 .read = flash_read,
370 .write = flash_write,
6038f373 371 .llseek = default_llseek,
f931551b
RC
372};
373
374static int add_cntr_files(struct super_block *sb, struct qib_devdata *dd)
375{
376 struct dentry *dir, *tmp;
377 char unit[10];
378 int ret, i;
379
380 /* create the per-unit directory */
381 snprintf(unit, sizeof unit, "%u", dd->unit);
382 ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir,
383 &simple_dir_operations, dd);
384 if (ret) {
385 printk(KERN_ERR "create_file(%s) failed: %d\n", unit, ret);
386 goto bail;
387 }
388
389 /* create the files in the new directory */
390 ret = create_file("counters", S_IFREG|S_IRUGO, dir, &tmp,
391 &cntr_ops[0], dd);
392 if (ret) {
393 printk(KERN_ERR "create_file(%s/counters) failed: %d\n",
394 unit, ret);
395 goto bail;
396 }
397 ret = create_file("counter_names", S_IFREG|S_IRUGO, dir, &tmp,
398 &cntr_ops[1], dd);
399 if (ret) {
400 printk(KERN_ERR "create_file(%s/counter_names) failed: %d\n",
401 unit, ret);
402 goto bail;
403 }
404 ret = create_file("portcounter_names", S_IFREG|S_IRUGO, dir, &tmp,
405 &portcntr_ops[0], dd);
406 if (ret) {
407 printk(KERN_ERR "create_file(%s/%s) failed: %d\n",
408 unit, "portcounter_names", ret);
409 goto bail;
410 }
411 for (i = 1; i <= dd->num_pports; i++) {
412 char fname[24];
413
414 sprintf(fname, "port%dcounters", i);
415 /* create the files in the new directory */
416 ret = create_file(fname, S_IFREG|S_IRUGO, dir, &tmp,
417 &portcntr_ops[i], dd);
418 if (ret) {
419 printk(KERN_ERR "create_file(%s/%s) failed: %d\n",
420 unit, fname, ret);
421 goto bail;
422 }
423 if (!(dd->flags & QIB_HAS_QSFP))
424 continue;
425 sprintf(fname, "qsfp%d", i);
426 ret = create_file(fname, S_IFREG|S_IRUGO, dir, &tmp,
427 &qsfp_ops[i - 1], dd);
428 if (ret) {
429 printk(KERN_ERR "create_file(%s/%s) failed: %d\n",
430 unit, fname, ret);
431 goto bail;
432 }
433 }
434
435 ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp,
436 &flash_ops, dd);
437 if (ret)
438 printk(KERN_ERR "create_file(%s/flash) failed: %d\n",
439 unit, ret);
440bail:
441 return ret;
442}
443
444static int remove_file(struct dentry *parent, char *name)
445{
446 struct dentry *tmp;
447 int ret;
448
449 tmp = lookup_one_len(name, parent, strlen(name));
450
451 if (IS_ERR(tmp)) {
452 ret = PTR_ERR(tmp);
453 goto bail;
454 }
455
f931551b
RC
456 spin_lock(&tmp->d_lock);
457 if (!(d_unhashed(tmp) && tmp->d_inode)) {
dc0474be 458 dget_dlock(tmp);
f931551b
RC
459 __d_drop(tmp);
460 spin_unlock(&tmp->d_lock);
f931551b
RC
461 simple_unlink(parent->d_inode, tmp);
462 } else {
463 spin_unlock(&tmp->d_lock);
f931551b
RC
464 }
465
466 ret = 0;
467bail:
468 /*
469 * We don't expect clients to care about the return value, but
470 * it's there if they need it.
471 */
472 return ret;
473}
474
475static int remove_device_files(struct super_block *sb,
476 struct qib_devdata *dd)
477{
478 struct dentry *dir, *root;
479 char unit[10];
480 int ret, i;
481
482 root = dget(sb->s_root);
483 mutex_lock(&root->d_inode->i_mutex);
484 snprintf(unit, sizeof unit, "%u", dd->unit);
485 dir = lookup_one_len(unit, root, strlen(unit));
486
487 if (IS_ERR(dir)) {
488 ret = PTR_ERR(dir);
489 printk(KERN_ERR "Lookup of %s failed\n", unit);
490 goto bail;
491 }
492
493 remove_file(dir, "counters");
494 remove_file(dir, "counter_names");
495 remove_file(dir, "portcounter_names");
496 for (i = 0; i < dd->num_pports; i++) {
497 char fname[24];
498
499 sprintf(fname, "port%dcounters", i + 1);
500 remove_file(dir, fname);
501 if (dd->flags & QIB_HAS_QSFP) {
502 sprintf(fname, "qsfp%d", i + 1);
503 remove_file(dir, fname);
504 }
505 }
506 remove_file(dir, "flash");
507 d_delete(dir);
508 ret = simple_rmdir(root->d_inode, dir);
509
510bail:
511 mutex_unlock(&root->d_inode->i_mutex);
512 dput(root);
513 return ret;
514}
515
516/*
517 * This fills everything in when the fs is mounted, to handle umount/mount
518 * after device init. The direct add_cntr_files() call handles adding
519 * them from the init code, when the fs is already mounted.
520 */
521static int qibfs_fill_super(struct super_block *sb, void *data, int silent)
522{
523 struct qib_devdata *dd, *tmp;
524 unsigned long flags;
525 int ret;
526
527 static struct tree_descr files[] = {
528 [2] = {"driver_stats", &driver_ops[0], S_IRUGO},
529 [3] = {"driver_stats_names", &driver_ops[1], S_IRUGO},
530 {""},
531 };
532
533 ret = simple_fill_super(sb, QIBFS_MAGIC, files);
534 if (ret) {
535 printk(KERN_ERR "simple_fill_super failed: %d\n", ret);
536 goto bail;
537 }
538
539 spin_lock_irqsave(&qib_devs_lock, flags);
540
541 list_for_each_entry_safe(dd, tmp, &qib_dev_list, list) {
542 spin_unlock_irqrestore(&qib_devs_lock, flags);
543 ret = add_cntr_files(sb, dd);
971b2e8a 544 if (ret)
f931551b 545 goto bail;
f931551b
RC
546 spin_lock_irqsave(&qib_devs_lock, flags);
547 }
548
549 spin_unlock_irqrestore(&qib_devs_lock, flags);
550
551bail:
552 return ret;
553}
554
fc14f2fe
AV
555static struct dentry *qibfs_mount(struct file_system_type *fs_type, int flags,
556 const char *dev_name, void *data)
f931551b 557{
fc14f2fe
AV
558 struct dentry *ret;
559 ret = mount_single(fs_type, flags, data, qibfs_fill_super);
560 if (!IS_ERR(ret))
561 qib_super = ret->d_sb;
f931551b
RC
562 return ret;
563}
564
565static void qibfs_kill_super(struct super_block *s)
566{
567 kill_litter_super(s);
568 qib_super = NULL;
569}
570
571int qibfs_add(struct qib_devdata *dd)
572{
573 int ret;
574
575 /*
576 * On first unit initialized, qib_super will not yet exist
577 * because nobody has yet tried to mount the filesystem, so
578 * we can't consider that to be an error; if an error occurs
579 * during the mount, that will get a complaint, so this is OK.
580 * add_cntr_files() for all units is done at mount from
581 * qibfs_fill_super(), so one way or another, everything works.
582 */
583 if (qib_super == NULL)
584 ret = 0;
585 else
586 ret = add_cntr_files(qib_super, dd);
587 return ret;
588}
589
590int qibfs_remove(struct qib_devdata *dd)
591{
592 int ret = 0;
593
594 if (qib_super)
595 ret = remove_device_files(qib_super, dd);
596
597 return ret;
598}
599
600static struct file_system_type qibfs_fs_type = {
601 .owner = THIS_MODULE,
602 .name = "ipathfs",
fc14f2fe 603 .mount = qibfs_mount,
f931551b
RC
604 .kill_sb = qibfs_kill_super,
605};
606
607int __init qib_init_qibfs(void)
608{
609 return register_filesystem(&qibfs_fs_type);
610}
611
612int __exit qib_exit_qibfs(void)
613{
614 return unregister_filesystem(&qibfs_fs_type);
615}
This page took 0.09382 seconds and 5 git commands to generate.