4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 # define DEBUG_SUBSYSTEM S_LNET
39 #include <libcfs/libcfs.h>
40 #include "tracefile.h"
41 #include <lustre_lib.h>
47 * /proc emulator routines ...
50 /* The root node of the proc fs emulation: / */
51 cfs_proc_entry_t * cfs_proc_root = NULL;
53 /* The root node of the proc fs emulation: /proc */
54 cfs_proc_entry_t * cfs_proc_proc = NULL;
56 /* The fs sys directory: /proc/fs */
57 cfs_proc_entry_t * cfs_proc_fs = NULL;
59 /* The sys root: /proc/sys */
60 cfs_proc_entry_t * cfs_proc_sys = NULL;
62 /* The sys root: /proc/dev | to implement misc device */
63 cfs_proc_entry_t * cfs_proc_dev = NULL;
66 /* SLAB object for cfs_proc_entry_t allocation */
67 struct kmem_cache *proc_entry_cache;
69 /* root node for sysctl table */
70 struct ctl_table_header root_table_header;
72 /* The global lock to protect all the access */
74 #if LIBCFS_PROCFS_SPINLOCK
75 spinlock_t proc_fs_lock;
77 #define INIT_PROCFS_LOCK() spin_lock_init(&proc_fs_lock)
78 #define LOCK_PROCFS() spin_lock(&proc_fs_lock)
79 #define UNLOCK_PROCFS() spin_unlock(&proc_fs_lock)
83 struct mutex proc_fs_lock;
85 #define INIT_PROCFS_LOCK() cfs_init_mutex(&proc_fs_lock)
86 #define LOCK_PROCFS() cfs_mutex_down(&proc_fs_lock)
87 #define UNLOCK_PROCFS() cfs_mutex_up(&proc_fs_lock)
92 proc_file_read(struct file * file, const char * buf, size_t nbytes, loff_t *ppos)
99 cfs_proc_entry_t * dp;
101 dp = (cfs_proc_entry_t *) file->f_inode->i_priv;
102 page = (char *) kmalloc(PAGE_CACHE_SIZE, 0);
106 while ((nbytes > 0) && !eof) {
108 count = min_t(size_t, PROC_BLOCK_SIZE, nbytes);
112 n = dp->read_proc( page, &start, (long)*ppos,
113 count, &eof, dp->data);
119 * For proc files that are less than 4k
121 start = page + *ppos;
122 n -= (ssize_t)(*ppos);
129 break; /* End of file */
136 n -= copy_to_user((void *)buf, start, n);
154 proc_file_write(struct file * file, const char * buffer,
155 size_t count, loff_t *ppos)
157 cfs_proc_entry_t * dp;
159 dp = (cfs_proc_entry_t *) file->f_inode->i_priv;
164 /* FIXME: does this routine need ppos? probably... */
165 return dp->write_proc(file, buffer, count, dp->data);
168 struct file_operations proc_file_operations = {
169 /*owner*/ THIS_MODULE,
170 /*lseek:*/ NULL, //proc_file_lseek,
171 /*read:*/ proc_file_read,
172 /*write:*/ proc_file_write,
178 /* allocate proc entry block */
183 cfs_proc_entry_t * entry = NULL;
185 entry = kmem_cache_alloc(proc_entry_cache, 0);
189 memset(entry, 0, sizeof(cfs_proc_entry_t));
191 entry->magic = CFS_PROC_ENTRY_MAGIC;
192 RtlInitializeSplayLinks(&(entry->s_link));
193 entry->proc_fops = &proc_file_operations;
198 /* free the proc entry block */
201 proc_free_entry(cfs_proc_entry_t * entry)
203 ASSERT(entry->magic == CFS_PROC_ENTRY_MAGIC);
204 kmem_cache_free(proc_entry_cache, entry);
207 /* dissect the path string for a given full proc path */
217 int i = 0, j = 0, len = 0;
219 *first = *remain = NULL;
224 while (i < len && (path[i] == '/')) i++;
228 *first = (char *)path + i;
229 while (i < len && (path[i] != '/')) i++;
230 *first_len = (int)(path + i - *first);
233 *remain = (char *)path + i + 1;
238 /* search the children entries of the parent entry */
242 cfs_proc_entry_t * parent,
246 cfs_proc_entry_t * node;
247 PRTL_SPLAY_LINKS link;
249 ASSERT(parent->magic == CFS_PROC_ENTRY_MAGIC);
250 ASSERT(cfs_is_flag_set(parent->flags, CFS_PROC_FLAG_DIRECTORY));
256 ANSI_STRING ename,nname;
259 node = CONTAINING_RECORD(link, cfs_proc_entry_t, s_link);
261 ASSERT(node->magic == CFS_PROC_ENTRY_MAGIC);
263 /* Compare the prefix in the tree with the full name */
265 RtlInitAnsiString(&ename, name);
266 RtlInitAnsiString(&nname, node->name);
268 result = RtlCompareString(&nname, &ename,TRUE);
272 /* The prefix is greater than the full name
273 so we go down the left child */
275 link = RtlLeftChild(link);
277 } else if (result < 0) {
279 /* The prefix is less than the full name
280 so we go down the right child */
282 link = RtlRightChild(link);
286 /* We got the entry in the splay tree and
287 make it root node instead */
289 parent->root = RtlSplay(link);
294 /* we need continue searching down the tree ... */
297 /* There's no the exptected entry in the splay tree */
304 cfs_proc_entry_t * parent,
305 cfs_proc_entry_t * child
308 cfs_proc_entry_t * entry;
310 ASSERT(parent != NULL && child != NULL);
311 ASSERT(parent->magic == CFS_PROC_ENTRY_MAGIC);
312 ASSERT(child->magic == CFS_PROC_ENTRY_MAGIC);
313 ASSERT(cfs_is_flag_set(parent->flags, CFS_PROC_FLAG_DIRECTORY));
316 parent->root = &(child->s_link);
318 entry = CONTAINING_RECORD(parent->root, cfs_proc_entry_t, s_link);
321 ANSI_STRING ename, cname;
323 ASSERT(entry->magic == CFS_PROC_ENTRY_MAGIC);
325 RtlInitAnsiString(&ename, entry->name);
326 RtlInitAnsiString(&cname, child->name);
328 result = RtlCompareString(&ename, &cname,TRUE);
331 cfs_enter_debugger();
332 if (entry == child) {
339 if (RtlLeftChild(&entry->s_link) == NULL) {
340 RtlInsertAsLeftChild(&entry->s_link, &child->s_link);
343 entry = CONTAINING_RECORD( RtlLeftChild(&entry->s_link),
344 cfs_proc_entry_t, s_link);
347 if (RtlRightChild(&entry->s_link) == NULL) {
348 RtlInsertAsRightChild(&entry->s_link, &child->s_link);
351 entry = CONTAINING_RECORD( RtlRightChild(&entry->s_link),
352 cfs_proc_entry_t, s_link );
358 cfs_set_flag(child->flags, CFS_PROC_FLAG_ATTACHED);
360 child->parent = parent;
366 /* remove a child entry from the splay tree */
369 cfs_proc_entry_t * parent,
370 cfs_proc_entry_t * child
373 cfs_proc_entry_t * entry = NULL;
375 ASSERT(parent != NULL && child != NULL);
376 ASSERT(parent->magic == CFS_PROC_ENTRY_MAGIC);
377 ASSERT(child->magic == CFS_PROC_ENTRY_MAGIC);
378 ASSERT(cfs_is_flag_set(parent->flags, CFS_PROC_FLAG_DIRECTORY));
379 ASSERT(cfs_is_flag_set(child->flags, CFS_PROC_FLAG_ATTACHED));
380 ASSERT(child->parent == parent);
382 entry = proc_search_splay(parent, child->name);
385 ASSERT(entry == child);
386 parent->root = RtlDelete(&(entry->s_link));
389 cfs_enter_debugger();
397 /* search a node inside the proc fs tree */
402 cfs_proc_entry_t * root
405 cfs_proc_entry_t * entry;
406 cfs_proc_entry_t * parent;
407 char *first, *remain;
414 ename = kmalloc(0x21, __GFP_ZERO);
421 /* dissect the file name string */
422 proc_dissect_name(name, &first, &flen, &remain);
427 cfs_enter_debugger();
432 memset(ename, 0, 0x20);
433 memcpy(ename, first, flen);
435 entry = proc_search_splay(parent, ename);
458 /* insert the path nodes to the proc fs tree */
463 cfs_proc_entry_t * root
466 cfs_proc_entry_t *entry;
467 cfs_proc_entry_t *parent;
468 char *first, *remain;
477 proc_dissect_name(name, &first, &flen, &remain);
485 memset(ename, 0, 0x20);
486 memcpy(ename, first, flen);
488 entry = proc_search_splay(parent, ename);
491 entry = proc_alloc_entry();
492 memcpy(entry->name, ename, flen);
494 if (entry && !proc_insert_splay(parent, entry)) {
495 proc_free_entry(entry);
505 entry->mode |= S_IFDIR | S_IRUGO | S_IXUGO;
506 cfs_set_flag(entry->flags, CFS_PROC_FLAG_DIRECTORY);
516 /* remove the path nodes from the proc fs tree */
521 cfs_proc_entry_t * root
524 cfs_proc_entry_t *entry;
525 char *first, *remain;
531 proc_dissect_name(name, &first, &flen, &remain);
535 memset(ename, 0, 0x20);
536 memcpy(ename, first, flen);
538 entry = proc_search_splay(root, ename);
543 ASSERT(S_ISDIR(entry->mode));
544 proc_remove_entry(remain, entry);
548 proc_remove_splay(root, entry);
549 proc_free_entry(entry);
553 cfs_enter_debugger();
557 /* create proc entry and insert it into the proc fs */
563 cfs_proc_entry_t * parent
566 cfs_proc_entry_t *entry = NULL;
569 if ((mode & S_IALLUGO) == 0)
570 mode |= S_IRUGO | S_IXUGO;
572 if ((mode & S_IFMT) == 0)
574 if ((mode & S_IALLUGO) == 0)
579 ASSERT(NULL != cfs_proc_root);
582 if (name[0] == '/') {
583 parent = cfs_proc_root;
585 ASSERT(NULL != cfs_proc_proc);
586 parent = cfs_proc_proc;
590 entry = proc_search_entry(name, parent);
593 entry = proc_insert_entry(name, parent);
595 /* Failed to create/insert the splay node ... */
596 cfs_enter_debugger();
599 /* Initializing entry ... */
603 cfs_set_flag(entry->flags, CFS_PROC_FLAG_DIRECTORY);
615 /* search the specified entry form the proc fs */
620 cfs_proc_entry_t * root
623 cfs_proc_entry_t * entry;
626 ASSERT(cfs_proc_root != NULL);
628 if (name[0] == '/') {
629 root = cfs_proc_root;
631 ASSERT(cfs_proc_proc != NULL);
632 root = cfs_proc_proc;
635 entry = proc_search_entry(name, root);
641 /* remove the entry from the proc fs */
646 cfs_proc_entry_t * parent
650 ASSERT(cfs_proc_root != NULL);
651 if (parent == NULL) {
652 if (name[0] == '/') {
653 parent = cfs_proc_root;
655 ASSERT(cfs_proc_proc != NULL);
656 parent = cfs_proc_proc;
659 proc_remove_entry(name, parent);
664 void proc_destroy_splay(cfs_proc_entry_t * entry)
666 cfs_proc_entry_t * node;
668 if (S_ISDIR(entry->mode)) {
670 while (entry->root) {
671 node = CONTAINING_RECORD(entry->root, cfs_proc_entry_t, s_link);
672 entry->root = RtlDelete(&(node->s_link));
673 proc_destroy_splay(node);
677 proc_free_entry(entry);
680 cfs_proc_entry_t *proc_symlink(
682 cfs_proc_entry_t *parent,
686 cfs_enter_debugger();
690 cfs_proc_entry_t *proc_mkdir(
692 cfs_proc_entry_t *parent)
694 return create_proc_entry((char *)name, S_IFDIR, parent);
697 void proc_destory_subtree(cfs_proc_entry_t *entry)
701 proc_destroy_splay(entry);
705 /* destory the whole proc fs tree */
707 void proc_destroy_fs()
712 proc_destroy_splay(cfs_proc_root);
714 if (proc_entry_cache)
715 kmem_cache_destroy(proc_entry_cache);
720 static char proc_item_path[512];
723 void proc_show_tree(cfs_proc_entry_t * node);
724 void proc_print_node(cfs_proc_entry_t * node)
726 if (node != cfs_proc_root) {
727 if (S_ISDIR(node->mode)) {
728 printk("%s/%s/\n", proc_item_path, node->name);
730 printk("%s/%s\n", proc_item_path, node->name);
733 printk("%s\n", node->name);
736 if (S_ISDIR(node->mode)) {
737 proc_show_tree(node);
741 void proc_show_child(PRTL_SPLAY_LINKS link)
743 cfs_proc_entry_t * entry = NULL;
749 proc_show_child(link->LeftChild);
750 entry = CONTAINING_RECORD(link, cfs_proc_entry_t, s_link);
751 proc_print_node(entry);
752 proc_show_child(link->RightChild);
755 void proc_show_tree(cfs_proc_entry_t * node)
757 PRTL_SPLAY_LINKS link = NULL;
758 cfs_proc_entry_t * entry = NULL;
762 i = strlen(proc_item_path);
763 ASSERT(S_ISDIR(node->mode));
764 if (node != cfs_proc_root) {
765 strcat(proc_item_path, "/");
766 strcat(proc_item_path, node->name);
768 proc_show_child(link);
769 proc_item_path[i] = 0;
772 void proc_print_splay()
774 printk("=================================================\n");
775 printk("Lustre virtual proc entries:\n");
776 printk("-------------------------------------------------\n");
778 proc_show_tree(cfs_proc_root);
780 printk("=================================================\n");
784 /* initilaize / build the proc fs tree */
787 cfs_proc_entry_t * root = NULL;
789 memset(&(root_table_header), 0, sizeof(struct ctl_table_header));
790 CFS_INIT_LIST_HEAD(&(root_table_header.ctl_entry));
793 proc_entry_cache = kmem_cache_create(NULL, sizeof(cfs_proc_entry_t),
796 if (!proc_entry_cache) {
800 root = proc_alloc_entry();
805 root->magic = CFS_PROC_ENTRY_MAGIC;
806 root->flags = CFS_PROC_FLAG_DIRECTORY;
807 root->mode = S_IFDIR | S_IRUGO | S_IXUGO;
808 root->nlink = 3; // root should never be deleted.
811 cfs_proc_root = root;
813 cfs_proc_dev = create_proc_entry("dev", S_IFDIR, root);
817 cfs_proc_dev->nlink = 1;
819 cfs_proc_proc = create_proc_entry("proc", S_IFDIR, root);
820 if (!cfs_proc_proc) {
823 cfs_proc_proc->nlink = 1;
825 cfs_proc_fs = create_proc_entry("fs", S_IFDIR, cfs_proc_proc);
829 cfs_proc_fs->nlink = 1;
831 cfs_proc_sys = create_proc_entry("sys", S_IFDIR, cfs_proc_proc);
835 cfs_proc_sys->nlink = 1;
847 static ssize_t do_rw_proc(int write, struct file * file, char * buf,
848 size_t count, loff_t *ppos)
851 cfs_proc_entry_t *de;
852 struct ctl_table *table;
856 de = (cfs_proc_entry_t *) file->proc_dentry;
858 if (!de || !de->data)
860 table = (struct ctl_table *) de->data;
861 if (!table || !table->proc_handler)
863 op = (write ? 002 : 004);
868 * FIXME: we need to pass on ppos to the handler.
871 error = (*table->proc_handler) (table, write, file, buf, &res);
877 static ssize_t proc_readsys(struct file * file, char * buf,
878 size_t count, loff_t *ppos)
880 return do_rw_proc(0, file, buf, count, ppos);
883 static ssize_t proc_writesys(struct file * file, const char * buf,
884 size_t count, loff_t *ppos)
886 return do_rw_proc(1, file, (char *) buf, count, ppos);
890 struct file_operations proc_sys_file_operations = {
891 /*owner*/ THIS_MODULE,
893 /*read:*/ proc_readsys,
894 /*write:*/ proc_writesys,
901 /* Scan the sysctl entries in table and add them all into /proc */
902 void register_proc_table(struct ctl_table * table, cfs_proc_entry_t * root)
904 cfs_proc_entry_t * de;
908 for (; table->ctl_name; table++) {
909 /* Can't do anything without a proc name. */
910 if (!table->procname)
912 /* Maybe we can't do anything with it... */
913 if (!table->proc_handler && !table->child) {
914 printk(KERN_WARNING "SYSCTL: Can't register %s\n",
919 len = strlen(table->procname);
923 if (table->proc_handler)
926 de = search_proc_entry(table->procname, root);
930 /* If the subdir exists already, de is non-NULL */
935 de = create_proc_entry((char *)table->procname, mode, root);
938 de->data = (void *) table;
939 if (table->proc_handler) {
940 de->proc_fops = &proc_sys_file_operations;
944 if (de->mode & S_IFDIR)
945 register_proc_table(table->child, de);
951 * Unregister a /proc sysctl table and any subdirectories.
953 void unregister_proc_table(struct ctl_table * table, cfs_proc_entry_t *root)
955 cfs_proc_entry_t *de;
956 for (; table->ctl_name; table++) {
957 if (!(de = table->de))
959 if (de->mode & S_IFDIR) {
961 printk (KERN_ALERT "Help- malformed sysctl tree on free\n");
964 unregister_proc_table(table->child, de);
966 /* Don't unregister directories which still have entries.. */
971 /* Don't unregister proc entries that are still being used.. */
976 remove_proc_entry((char *)table->procname, root);
980 /* The generic string strategy routine: */
981 int sysctl_string(struct ctl_table *table, int *name, int nlen,
982 void *oldval, size_t *oldlenp,
983 void *newval, size_t newlen, void **context)
987 if (!table->data || !table->maxlen)
990 if (oldval && oldlenp) {
991 if (get_user(len, oldlenp))
994 l = strlen(table->data);
997 if (len >= table->maxlen)
999 if (copy_to_user(oldval, table->data, len))
1001 if (put_user(0, ((char *) oldval) + len))
1003 if (put_user(len, oldlenp))
1007 if (newval && newlen) {
1009 if (len > table->maxlen)
1010 len = table->maxlen;
1011 if (copy_from_user(table->data, newval, len))
1013 if (len == table->maxlen)
1015 ((char *) table->data)[len] = 0;
1021 * simple_strtoul - convert a string to an unsigned long
1022 * @cp: The start of the string
1023 * @endp: A pointer to the end of the parsed string will be placed here
1024 * @base: The number base to use
1026 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
1028 unsigned long result = 0, value;
1035 if ((*cp == 'x') && cfs_isxdigit(cp[1])) {
1041 while (cfs_isxdigit(*cp) &&
1042 (value = cfs_isdigit(*cp) ? *cp-'0' : toupper(*cp)-'A'+10) < base) {
1043 result = result*base + value;
1058 static int do_proc_dointvec(struct ctl_table *table, int write, struct file *filp,
1059 void *buffer, size_t *lenp, int conv, int op)
1061 int *i, vleft, first=1, neg, val;
1064 #define TMPBUFLEN 20
1065 char buf[TMPBUFLEN], *p;
1067 if (!table->data || !table->maxlen || !*lenp)
1073 i = (int *) table->data;
1074 vleft = table->maxlen / sizeof(int);
1077 for (; left && vleft--; i++, first=0) {
1081 if (get_user(c, (char *)buffer))
1092 if (len > TMPBUFLEN-1)
1094 if (copy_from_user(buf, buffer, len))
1098 if (*p == '-' && left > 1) {
1102 if (*p < '0' || *p > '9')
1104 val = simple_strtoul(p, &p, 0) * conv;
1106 if ((len < left) && *p && !isspace(*p))
1110 (char *)buffer += len;
1135 sprintf(p, "%d", (*i) / conv);
1139 if (copy_to_user(buffer, buf, len))
1142 (char *)buffer += len;
1146 if (!write && !first && left) {
1147 if (put_user('\n', (char *) buffer))
1149 left--, ((char *)buffer)++;
1152 p = (char *) buffer;
1155 if (get_user(c, p++))
1165 memset(&(filp->f_pos) , 0, sizeof(loff_t));
1166 filp->f_pos += (loff_t)(*lenp);
1171 * proc_dointvec - read a vector of integers
1172 * @table: the sysctl table
1173 * @write: %TRUE if this is a write to the sysctl file
1174 * @filp: the file structure
1175 * @buffer: the user buffer
1176 * @lenp: the size of the user buffer
1178 * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
1179 * values from/to the user buffer, treated as an ASCII string.
1181 * Returns 0 on success.
1183 int proc_dointvec(struct ctl_table *table, int write, struct file *filp,
1184 void *buffer, size_t *lenp)
1186 return do_proc_dointvec(table,write,filp,buffer,lenp,1,OP_SET);
1191 * proc_dostring - read a string sysctl
1192 * @table: the sysctl table
1193 * @write: %TRUE if this is a write to the sysctl file
1194 * @filp: the file structure
1195 * @buffer: the user buffer
1196 * @lenp: the size of the user buffer
1198 * Reads/writes a string from/to the user buffer. If the kernel
1199 * buffer provided is not large enough to hold the string, the
1200 * string is truncated. The copied string is %NULL-terminated.
1201 * If the string is being read by the user process, it is copied
1202 * and a newline '\n' is added. It is truncated if the buffer is
1205 * Returns 0 on success.
1207 int proc_dostring(struct ctl_table *table, int write, struct file *filp,
1208 void *buffer, size_t *lenp)
1213 if (!table->data || !table->maxlen || !*lenp ||
1214 (filp->f_pos && !write)) {
1222 while (len < *lenp) {
1223 if (get_user(c, p++))
1225 if (c == 0 || c == '\n')
1229 if (len >= (size_t)table->maxlen)
1230 len = (size_t)table->maxlen-1;
1231 if (copy_from_user(table->data, buffer, len))
1233 ((char *) table->data)[len] = 0;
1234 filp->f_pos += *lenp;
1236 len = (size_t)strlen(table->data);
1237 if (len > (size_t)table->maxlen)
1238 len = (size_t)table->maxlen;
1242 if (copy_to_user(buffer, table->data, len))
1245 if (put_user('\n', ((char *) buffer) + len))
1255 /* Perform the actual read/write of a sysctl table entry. */
1256 int do_sysctl_strategy(struct ctl_table *table, int *name, int nlen,
1257 void *oldval, size_t *oldlenp, void *newval,
1258 size_t newlen, void **context)
1268 if (table->strategy) {
1269 rc = table->strategy(table, name, nlen, oldval, oldlenp,
1270 newval, newlen, context);
1277 /* If there is no strategy routine, or if the strategy returns
1278 * zero, proceed with automatic r/w */
1279 if (table->data && table->maxlen) {
1280 if (oldval && oldlenp) {
1281 get_user(len, oldlenp);
1283 if (len > (size_t)table->maxlen)
1284 len = (size_t)table->maxlen;
1285 if (copy_to_user(oldval, table->data, len))
1287 if (put_user(len, oldlenp))
1291 if (newval && newlen) {
1293 if (len > (size_t)table->maxlen)
1294 len = (size_t)table->maxlen;
1295 if (copy_from_user(table->data, newval, len))
1302 static int parse_table(int *name, int nlen,
1303 void *oldval, size_t *oldlenp,
1304 void *newval, size_t newlen,
1305 struct ctl_table *table, void **context)
1313 if (get_user(n, name))
1315 for ( ; table->ctl_name; table++) {
1316 if (n == table->ctl_name || table->ctl_name == CTL_ANY) {
1320 if (ctl_perm(table, 001))
1323 if (table->strategy) {
1324 error = table->strategy(
1327 newval, newlen, context);
1333 table = table->child;
1336 error = do_sysctl_strategy(table, name, nlen,
1338 newval, newlen, context);
1345 int do_sysctl(int *name, int nlen, void *oldval, size_t *oldlenp,
1346 void *newval, size_t newlen)
1350 if (nlen <= 0 || nlen >= CTL_MAXNAME)
1354 if (!oldlenp || get_user(old_len, oldlenp))
1357 tmp = &root_table_header.ctl_entry;
1359 struct ctl_table_header *head =
1360 cfs_list_entry(tmp, struct ctl_table_header, ctl_entry);
1361 void *context = NULL;
1362 int error = parse_table(name, nlen, oldval, oldlenp,
1363 newval, newlen, head->ctl_table,
1367 if (error != -ENOTDIR)
1370 } while (tmp != &root_table_header.ctl_entry);
1375 * register_sysctl_table - register a sysctl heirarchy
1376 * @table: the top-level table structure
1377 * @insert_at_head: whether the entry should be inserted in front or at the end
1379 * Register a sysctl table heirarchy. @table should be a filled in ctl_table
1380 * array. An entry with a ctl_name of 0 terminates the table.
1382 * The members of the &ctl_table structure are used as follows:
1384 * ctl_name - This is the numeric sysctl value used by sysctl(2). The number
1385 * must be unique within that level of sysctl
1387 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1388 * enter a sysctl file
1390 * data - a pointer to data for use by proc_handler
1392 * maxlen - the maximum size in bytes of the data
1394 * mode - the file permissions for the /proc/sys file, and for sysctl(2)
1396 * child - a pointer to the child sysctl table if this entry is a directory, or
1399 * proc_handler - the text handler routine (described below)
1401 * strategy - the strategy routine (described below)
1403 * de - for internal use by the sysctl routines
1405 * extra1, extra2 - extra pointers usable by the proc handler routines
1407 * Leaf nodes in the sysctl tree will be represented by a single file
1408 * under /proc; non-leaf nodes will be represented by directories.
1410 * sysctl(2) can automatically manage read and write requests through
1411 * the sysctl table. The data and maxlen fields of the ctl_table
1412 * struct enable minimal validation of the values being written to be
1413 * performed, and the mode field allows minimal authentication.
1415 * More sophisticated management can be enabled by the provision of a
1416 * strategy routine with the table entry. This will be called before
1417 * any automatic read or write of the data is performed.
1419 * The strategy routine may return
1421 * < 0 - Error occurred (error is passed to user process)
1423 * 0 - OK - proceed with automatic read or write.
1425 * > 0 - OK - read or write has been done by the strategy routine, so
1426 * return immediately.
1428 * There must be a proc_handler routine for any terminal nodes
1429 * mirrored under /proc/sys (non-terminals are handled by a built-in
1430 * directory handler). Several default handlers are available to
1431 * cover common cases -
1433 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1434 * proc_dointvec_minmax(), proc_doulongvec_ms_jiffies_minmax(),
1435 * proc_doulongvec_minmax()
1437 * It is the handler's job to read the input buffer from user memory
1438 * and process it. The handler should return 0 on success.
1440 * This routine returns %NULL on a failure to register, and a pointer
1441 * to the table header on success.
1443 struct ctl_table_header *
1444 register_sysctl_table(struct ctl_table *table)
1446 struct ctl_table_header *tmp;
1447 tmp = kmalloc(sizeof(struct ctl_table_header), 0);
1450 tmp->ctl_table = table;
1452 CFS_INIT_LIST_HEAD(&tmp->ctl_entry);
1453 cfs_list_add_tail(&tmp->ctl_entry, &root_table_header.ctl_entry);
1454 #ifdef CONFIG_PROC_FS
1455 register_proc_table(table, cfs_proc_sys);
1461 * unregister_sysctl_table - unregister a sysctl table heirarchy
1462 * @header: the header returned from register_sysctl_table
1464 * Unregisters the sysctl table and all children. proc entries may not
1465 * actually be removed until they are no longer used by anyone.
1467 void unregister_sysctl_table(struct ctl_table_header * header)
1469 cfs_list_del(&header->ctl_entry);
1470 #ifdef CONFIG_PROC_FS
1471 unregister_proc_table(header->ctl_table, cfs_proc_sys);
1477 int misc_register(struct miscdevice * psdev)
1479 cfs_proc_entry_t * entry;
1481 entry = create_proc_entry (
1482 (char *)psdev->name,
1491 entry->flags |= CFS_PROC_FLAG_MISCDEV;
1493 entry->proc_fops = psdev->fops;
1494 entry->data = (void *)psdev;
1499 int misc_deregister(struct miscdevice * psdev)
1501 cfs_proc_entry_t * entry;
1503 entry = search_proc_entry (
1504 (char *)psdev->name,
1510 ASSERT(entry->data == (void *)psdev);
1511 ASSERT(entry->flags & CFS_PROC_FLAG_MISCDEV);
1514 (char *)psdev->name,
1522 #define PSDEV_LNET (0x100)
1524 PSDEV_DEBUG = 1, /* control debugging */
1525 PSDEV_SUBSYSTEM_DEBUG, /* control debugging */
1526 PSDEV_PRINTK, /* force all messages to console */
1527 PSDEV_CONSOLE_RATELIMIT, /* rate limit console messages */
1528 PSDEV_DEBUG_PATH, /* crashdump log location */
1529 PSDEV_DEBUG_DUMP_PATH, /* crashdump tracelog location */
1530 PSDEV_LIBCFS_MEMUSED, /* bytes currently PORTAL_ALLOCated */
1533 static struct ctl_table lnet_table[] = {
1534 {PSDEV_DEBUG, "debug", &libcfs_debug, sizeof(int), 0644, NULL,
1536 {PSDEV_SUBSYSTEM_DEBUG, "subsystem_debug", &libcfs_subsystem_debug,
1537 sizeof(int), 0644, NULL, &proc_dointvec},
1538 {PSDEV_PRINTK, "printk", &libcfs_printk, sizeof(int), 0644, NULL,
1540 {PSDEV_CONSOLE_RATELIMIT, "console_ratelimit", &libcfs_console_ratelimit,
1541 sizeof(int), 0644, NULL, &proc_dointvec},
1543 {PSDEV_PORTALS_UPCALL, "upcall", portals_upcall,
1544 sizeof(portals_upcall), 0644, NULL, &proc_dostring,
1547 {PSDEV_LIBCFS_MEMUSED, "memused", (int *)&libcfs_kmemory.counter,
1548 sizeof(int), 0644, NULL, &proc_dointvec},
1552 static struct ctl_table top_table[2] = {
1553 {PSDEV_LNET, "lnet", NULL, 0, 0555, lnet_table},
1558 int trace_write_dump_kernel(struct file *file, const char *buffer,
1559 unsigned long count, void *data)
1561 int rc = cfs_trace_dump_debug_buffer_usrstr((void *)buffer, count);
1563 return (rc < 0) ? rc : count;
1566 int trace_write_daemon_file(struct file *file, const char *buffer,
1567 unsigned long count, void *data)
1569 int rc = cfs_trace_daemon_command_usrstr((void *)buffer, count);
1571 return (rc < 0) ? rc : count;
1574 int trace_read_daemon_file(char *page, char **start, off_t off, int count,
1575 int *eof, void *data)
1578 cfs_tracefile_read_lock();
1579 rc = cfs_trace_copyout_string(page, count, cfs_tracefile, "\n");
1580 cfs_tracefile_read_unlock();
1584 int trace_write_debug_mb(struct file *file, const char *buffer,
1585 unsigned long count, void *data)
1587 int rc = 0; /*trace_set_debug_mb_userstr((void *)buffer, count);*/
1589 return (rc < 0) ? rc : count;
1592 int trace_read_debug_mb(char *page, char **start, off_t off, int count,
1593 int *eof, void *data)
1597 snprintf(str, sizeof(str), "%d\n", cfs_trace_get_debug_mb());
1599 return cfs_trace_copyout_string(page, count, str, NULL);
1602 int insert_proc(void)
1604 cfs_proc_entry_t *ent;
1606 ent = create_proc_entry("sys/lnet/dump_kernel", 0, NULL);
1608 CERROR("couldn't register dump_kernel\n");
1611 ent->write_proc = trace_write_dump_kernel;
1613 ent = create_proc_entry("sys/lnet/daemon_file", 0, NULL);
1615 CERROR("couldn't register daemon_file\n");
1618 ent->write_proc = trace_write_daemon_file;
1619 ent->read_proc = trace_read_daemon_file;
1621 ent = create_proc_entry("sys/lnet/debug_mb", 0, NULL);
1623 CERROR("couldn't register debug_mb\n");
1626 ent->write_proc = trace_write_debug_mb;
1627 ent->read_proc = trace_read_debug_mb;
1632 void remove_proc(void)
1634 remove_proc_entry("sys/lnet/dump_kernel", NULL);
1635 remove_proc_entry("sys/lnet/daemon_file", NULL);
1636 remove_proc_entry("sys/lnet/debug_mb", NULL);
1641 * proc process routines of kernel space
1645 lustre_open_file(char *filename)
1648 struct file *fh = NULL;
1649 cfs_proc_entry_t *fp = NULL;
1651 fp = search_proc_entry(filename, cfs_proc_root);
1655 fh = kmalloc(sizeof(*fh), __GFP_ZERO);
1659 fh->f_inode = kmalloc(sizeof(struct inode), __GFP_ZERO);
1665 fh->f_inode->i_priv = (void *)fp;
1666 fh->f_op = fp->proc_fops;
1668 if (fh->f_op->open) {
1669 rc = (fh->f_op->open)(fh->f_inode, fh);
1684 lustre_close_file(struct file *fh)
1687 cfs_proc_entry_t *fp = NULL;
1689 fp = (cfs_proc_entry_t *) fh->f_inode->i_priv;
1690 if (fh->f_op->release) {
1691 rc = (fh->f_op->release)(fh->f_inode, fh);
1703 lustre_do_ioctl(struct file *fh, unsigned long cmd, ulong_ptr_t arg)
1707 if (fh->f_op->ioctl)
1708 rc = (fh->f_op->ioctl)(fh, cmd, arg);
1714 lustre_ioctl_file(struct file *fh, PCFS_PROC_IOCTL devctl)
1719 data = (ulong_ptr_t)devctl + sizeof(CFS_PROC_IOCTL);
1721 CLASSERT(sizeof(struct obd_ioctl_data) == 528);
1723 CLASSERT(sizeof(struct obd_ioctl_data) == 576);
1726 /* obd ioctl code */
1727 if (_IOC_TYPE(devctl->cmd) == 'f') {
1729 struct obd_ioctl_data * obd = (struct obd_ioctl_data *) data;
1731 if ( devctl->cmd != (ULONG)OBD_IOC_BRW_WRITE &&
1732 devctl->cmd != (ULONG)OBD_IOC_BRW_READ ) {
1734 unsigned long off = obd->ioc_len;
1736 if (obd->ioc_plen1) {
1737 obd->ioc_pbuf1 = (char *)(data + off);
1738 off += cfs_size_round(obd->ioc_plen1);
1740 obd->ioc_pbuf1 = NULL;
1743 if (obd->ioc_plen2) {
1744 obd->ioc_pbuf2 = (char *)(data + off);
1745 off += cfs_size_round(obd->ioc_plen2);
1747 obd->ioc_pbuf2 = NULL;
1752 rc = lustre_do_ioctl(fh, devctl->cmd, data);
1758 lustre_read_file(struct file *fh, loff_t off, size_t size, char *buf)
1764 high = (off_t)(off >> 32);
1766 if (fh->f_op->read) {
1767 rc = (fh->f_op->read) (fh, buf, size, &off);
1771 fh->f_pos = off + rc;
1778 lustre_write_file(struct file *fh, loff_t off, size_t size, char *buf)
1783 if (fh->f_op->write)
1784 rc = (fh->f_op->write)(fh, buf, size, &off);
1795 * seq_open - initialize sequential file
1796 * @file: file we initialize
1797 * @op: method table describing the sequence
1799 * seq_open() sets @file, associating it with a sequence described
1800 * by @op. @op->start() sets the iterator up and returns the first
1801 * element of sequence. @op->stop() shuts it down. @op->next()
1802 * returns the next element of sequence. @op->show() prints element
1803 * into the buffer. In case of error ->start() and ->next() return
1804 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
1805 * returns 0 in case of success and negative number in case of error.
1807 int seq_open(struct file *file, const struct seq_operations *op)
1809 struct seq_file *p = file->private_data;
1812 p = kmalloc(sizeof(*p), GFP_KERNEL);
1815 file->private_data = p;
1817 memset(p, 0, sizeof(*p));
1818 mutex_init(&p->lock);
1822 * Wrappers around seq_open(e.g. swaps_open) need to be
1823 * aware of this. If they set f_version themselves, they
1824 * should call seq_open first and then set f_version.
1826 file->f_version = 0;
1828 /* SEQ files support lseek, but not pread/pwrite */
1829 file->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1832 EXPORT_SYMBOL(seq_open);
1835 * seq_read - ->read() method for sequential files.
1836 * @file: the file to read from
1837 * @buf: the buffer to read to
1838 * @size: the maximum number of bytes to read
1839 * @ppos: the current position in the file
1841 * Ready-made ->f_op->read()
1843 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
1845 struct seq_file *m = (struct seq_file *)file->private_data;
1852 mutex_lock(&m->lock);
1854 * seq_file->op->..m_start/m_stop/m_next may do special actions
1855 * or optimisations based on the file->f_version, so we want to
1856 * pass the file->f_version to those methods.
1858 * seq_file->version is just copy of f_version, and seq_file
1859 * methods can treat it simply as file version.
1860 * It is copied in first and copied out after all operations.
1861 * It is convenient to have it as part of structure to avoid the
1862 * need of passing another argument to all the seq_file methods.
1864 m->version = file->f_version;
1865 /* grab buffer if we didn't have one */
1867 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
1871 /* if not empty - flush it first */
1873 n = min(m->count, size);
1874 err = copy_to_user(buf, m->buf + m->from, n);
1887 /* we need at least one record in buffer */
1890 p = m->op->start(m, &pos);
1892 if (!p || IS_ERR(p))
1894 err = m->op->show(m, p);
1897 if (m->count < m->size)
1901 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
1911 /* they want more? let's try to get some more */
1912 while (m->count < size) {
1913 size_t offs = m->count;
1915 p = m->op->next(m, p, &next);
1916 if (!p || IS_ERR(p)) {
1920 err = m->op->show(m, p);
1921 if (err || m->count == m->size) {
1928 n = min(m->count, size);
1929 err = copy_to_user(buf, m->buf, n);
1944 file->f_version = m->version;
1945 mutex_unlock(&m->lock);
1954 EXPORT_SYMBOL(seq_read);
1956 static int traverse(struct seq_file *m, loff_t offset)
1958 loff_t pos = 0, index;
1964 m->count = m->from = 0;
1970 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
1974 p = m->op->start(m, &index);
1979 error = m->op->show(m, p);
1982 if (m->count == m->size)
1984 if (pos + (loff_t)(m->count) > offset) {
1985 m->from = (size_t)(offset - pos);
1986 m->count -= m->from;
1992 if (pos == offset) {
1997 p = m->op->next(m, p, &index);
2005 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL | __GFP_ZERO);
2006 return !m->buf ? -ENOMEM : -EAGAIN;
2010 * seq_lseek - ->llseek() method for sequential files.
2011 * @file: the file in question
2012 * @offset: new position
2013 * @origin: 0 for absolute, 1 for relative position
2015 * Ready-made ->f_op->llseek()
2017 loff_t seq_lseek(struct file *file, loff_t offset, int origin)
2019 struct seq_file *m = (struct seq_file *)file->private_data;
2020 long long retval = -EINVAL;
2022 mutex_lock(&m->lock);
2023 m->version = file->f_version;
2026 offset += file->f_pos;
2031 if (offset != file->f_pos) {
2032 while ((retval=traverse(m, offset)) == -EAGAIN)
2035 /* with extreme prejudice... */
2041 retval = file->f_pos = offset;
2045 file->f_version = m->version;
2046 mutex_unlock(&m->lock);
2049 EXPORT_SYMBOL(seq_lseek);
2052 * seq_release - free the structures associated with sequential file.
2053 * @file: file in question
2054 * @inode: file->f_path.dentry->d_inode
2056 * Frees the structures associated with sequential file; can be used
2057 * as ->f_op->release() if you don't have private data to destroy.
2059 int seq_release(struct inode *inode, struct file *file)
2061 struct seq_file *m = (struct seq_file *)file->private_data;
2069 EXPORT_SYMBOL(seq_release);
2072 * seq_escape - print string into buffer, escaping some characters
2075 * @esc: set of characters that need escaping
2077 * Puts string into buffer, replacing each occurrence of character from
2078 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
2081 int seq_escape(struct seq_file *m, const char *s, const char *esc)
2083 char *end = m->buf + m->size;
2087 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
2088 if (!strchr(esc, c)) {
2094 *p++ = '0' + ((c & 0300) >> 6);
2095 *p++ = '0' + ((c & 070) >> 3);
2096 *p++ = '0' + (c & 07);
2102 m->count = p - m->buf;
2105 EXPORT_SYMBOL(seq_escape);
2107 int seq_printf(struct seq_file *m, const char *f, ...)
2112 if (m->count < m->size) {
2114 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
2116 if (m->count + len < m->size) {
2124 EXPORT_SYMBOL(seq_printf);
2126 char *d_path(struct path *p, char *buffer, int buflen)
2128 cfs_enter_debugger();
2129 return ERR_PTR(-ENAMETOOLONG);
2132 int seq_path(struct seq_file *m, struct path *path, char *esc)
2134 if (m->count < m->size) {
2135 char *s = m->buf + m->count;
2136 char *p = d_path(path, s, m->size - m->count);
2141 p = m->buf + m->count;
2142 m->count = s - m->buf;
2143 return (int)(s - p);
2144 } else if (!strchr(esc, c)) {
2146 } else if (s + 4 > p) {
2150 *s++ = '0' + ((c & 0300) >> 6);
2151 *s++ = '0' + ((c & 070) >> 3);
2152 *s++ = '0' + (c & 07);
2160 EXPORT_SYMBOL(seq_path);
2162 static void *single_start(struct seq_file *p, loff_t *pos)
2164 return (void *) (INT_PTR) (*pos == 0);
2167 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
2173 static void single_stop(struct seq_file *p, void *v)
2177 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
2180 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
2184 op->start = single_start;
2185 op->next = single_next;
2186 op->stop = single_stop;
2188 res = seq_open(file, op);
2190 ((struct seq_file *)file->private_data)->private = data;
2196 EXPORT_SYMBOL(single_open);
2198 int single_release(struct inode *inode, struct file *file)
2200 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
2201 int res = seq_release(inode, file);
2205 EXPORT_SYMBOL(single_release);
2207 int seq_release_private(struct inode *inode, struct file *file)
2209 struct seq_file *seq = file->private_data;
2211 kfree(seq->private);
2212 seq->private = NULL;
2213 return seq_release(inode, file);
2215 EXPORT_SYMBOL(seq_release_private);
2217 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
2222 struct seq_file *seq;
2224 private = kmalloc(psize, GFP_KERNEL | __GFP_ZERO);
2225 if (private == NULL)
2228 rc = seq_open(f, ops);
2232 seq = f->private_data;
2233 seq->private = private;
2241 EXPORT_SYMBOL(__seq_open_private);
2243 int seq_open_private(struct file *filp, const struct seq_operations *ops,
2246 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
2248 EXPORT_SYMBOL(seq_open_private);
2250 int seq_putc(struct seq_file *m, char c)
2252 if (m->count < m->size) {
2253 m->buf[m->count++] = c;
2258 EXPORT_SYMBOL(seq_putc);
2260 int seq_puts(struct seq_file *m, const char *s)
2262 int len = strlen(s);
2263 if (m->count + len < m->size) {
2264 memcpy(m->buf + m->count, s, len);
2271 EXPORT_SYMBOL(seq_puts);
2273 cfs_list_t *seq_list_start(cfs_list_t *head, loff_t pos)
2277 cfs_list_for_each(lh, head)
2284 EXPORT_SYMBOL(seq_list_start);
2286 cfs_list_t *seq_list_start_head(cfs_list_t *head,
2292 return seq_list_start(head, pos - 1);
2295 EXPORT_SYMBOL(seq_list_start_head);
2297 cfs_list_t *seq_list_next(void *v, cfs_list_t *head,
2302 lh = ((cfs_list_t *)v)->next;
2304 return lh == head ? NULL : lh;
2307 EXPORT_SYMBOL(seq_list_next);
2309 struct proc_dir_entry *PDE(const struct inode *inode)
2311 return (struct proc_dir_entry *)inode->i_priv;
2315 #endif /* __KERNEL__ */