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.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
35 # define DEBUG_SUBSYSTEM S_LNET
37 #include <libcfs/libcfs.h>
38 #include "tracefile.h"
39 #include <lustre_lib.h>
45 * /proc emulator routines ...
48 /* The root node of the proc fs emulation: / */
49 cfs_proc_entry_t * cfs_proc_root = NULL;
51 /* The root node of the proc fs emulation: /proc */
52 cfs_proc_entry_t * cfs_proc_proc = NULL;
54 /* The fs sys directory: /proc/fs */
55 cfs_proc_entry_t * cfs_proc_fs = NULL;
57 /* The sys root: /proc/sys */
58 cfs_proc_entry_t * cfs_proc_sys = NULL;
60 /* The sys root: /proc/dev | to implement misc device */
61 cfs_proc_entry_t * cfs_proc_dev = NULL;
64 /* SLAB object for cfs_proc_entry_t allocation */
65 cfs_mem_cache_t * proc_entry_cache = NULL;
67 /* root node for sysctl table */
68 cfs_sysctl_table_header_t root_table_header;
70 /* The global lock to protect all the access */
72 #if LIBCFS_PROCFS_SPINLOCK
73 spinlock_t proc_fs_lock;
75 #define INIT_PROCFS_LOCK() spin_lock_init(&proc_fs_lock)
76 #define LOCK_PROCFS() spin_lock(&proc_fs_lock)
77 #define UNLOCK_PROCFS() spin_unlock(&proc_fs_lock)
81 struct mutex proc_fs_lock;
83 #define INIT_PROCFS_LOCK() cfs_init_mutex(&proc_fs_lock)
84 #define LOCK_PROCFS() cfs_mutex_down(&proc_fs_lock)
85 #define UNLOCK_PROCFS() cfs_mutex_up(&proc_fs_lock)
90 proc_file_read(struct file * file, const char * buf, size_t nbytes, loff_t *ppos)
97 cfs_proc_entry_t * dp;
99 dp = (cfs_proc_entry_t *) file->f_inode->i_priv;
100 if (!(page = (char*) cfs_alloc(CFS_PAGE_SIZE, 0)))
103 while ((nbytes > 0) && !eof) {
105 count = min_t(size_t, PROC_BLOCK_SIZE, nbytes);
109 n = dp->read_proc( page, &start, (long)*ppos,
110 count, &eof, dp->data);
116 * For proc files that are less than 4k
118 start = page + *ppos;
119 n -= (ssize_t)(*ppos);
126 break; /* End of file */
133 n -= cfs_copy_to_user((void *)buf, start, n);
151 proc_file_write(struct file * file, const char * buffer,
152 size_t count, loff_t *ppos)
154 cfs_proc_entry_t * dp;
156 dp = (cfs_proc_entry_t *) file->f_inode->i_priv;
161 /* FIXME: does this routine need ppos? probably... */
162 return dp->write_proc(file, buffer, count, dp->data);
165 struct file_operations proc_file_operations = {
166 /*owner*/ THIS_MODULE,
167 /*lseek:*/ NULL, //proc_file_lseek,
168 /*read:*/ proc_file_read,
169 /*write:*/ proc_file_write,
175 /* allocate proc entry block */
180 cfs_proc_entry_t * entry = NULL;
182 entry = cfs_mem_cache_alloc(proc_entry_cache, 0);
187 memset(entry, 0, sizeof(cfs_proc_entry_t));
189 entry->magic = CFS_PROC_ENTRY_MAGIC;
190 RtlInitializeSplayLinks(&(entry->s_link));
191 entry->proc_fops = &proc_file_operations;
196 /* free the proc entry block */
199 proc_free_entry(cfs_proc_entry_t * entry)
202 ASSERT(entry->magic == CFS_PROC_ENTRY_MAGIC);
204 cfs_mem_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 = cfs_alloc(0x21, CFS_ALLOC_ZERO);
422 /* dissect the file name string */
423 proc_dissect_name(name, &first, &flen, &remain);
428 cfs_enter_debugger();
433 memset(ename, 0, 0x20);
434 memcpy(ename, first, flen);
436 entry = proc_search_splay(parent, ename);
459 /* insert the path nodes to the proc fs tree */
464 cfs_proc_entry_t * root
467 cfs_proc_entry_t *entry;
468 cfs_proc_entry_t *parent;
469 char *first, *remain;
478 proc_dissect_name(name, &first, &flen, &remain);
486 memset(ename, 0, 0x20);
487 memcpy(ename, first, flen);
489 entry = proc_search_splay(parent, ename);
492 entry = proc_alloc_entry();
493 memcpy(entry->name, ename, flen);
496 if(!proc_insert_splay(parent, entry)) {
497 proc_free_entry(entry);
508 entry->mode |= S_IFDIR | S_IRUGO | S_IXUGO;
509 cfs_set_flag(entry->flags, CFS_PROC_FLAG_DIRECTORY);
519 /* remove the path nodes from the proc fs tree */
524 cfs_proc_entry_t * root
527 cfs_proc_entry_t *entry;
528 char *first, *remain;
534 proc_dissect_name(name, &first, &flen, &remain);
538 memset(ename, 0, 0x20);
539 memcpy(ename, first, flen);
541 entry = proc_search_splay(root, ename);
546 ASSERT(S_ISDIR(entry->mode));
547 proc_remove_entry(remain, entry);
551 proc_remove_splay(root, entry);
552 proc_free_entry(entry);
556 cfs_enter_debugger();
560 /* create proc entry and insert it into the proc fs */
566 cfs_proc_entry_t * parent
569 cfs_proc_entry_t *entry = NULL;
572 if ((mode & S_IALLUGO) == 0)
573 mode |= S_IRUGO | S_IXUGO;
575 if ((mode & S_IFMT) == 0)
577 if ((mode & S_IALLUGO) == 0)
582 ASSERT(NULL != cfs_proc_root);
585 if (name[0] == '/') {
586 parent = cfs_proc_root;
588 ASSERT(NULL != cfs_proc_proc);
589 parent = cfs_proc_proc;
593 entry = proc_search_entry(name, parent);
596 entry = proc_insert_entry(name, parent);
598 /* Failed to create/insert the splay node ... */
599 cfs_enter_debugger();
602 /* Initializing entry ... */
606 cfs_set_flag(entry->flags, CFS_PROC_FLAG_DIRECTORY);
618 /* search the specified entry form the proc fs */
623 cfs_proc_entry_t * root
626 cfs_proc_entry_t * entry;
629 ASSERT(cfs_proc_root != NULL);
631 if (name[0] == '/') {
632 root = cfs_proc_root;
634 ASSERT(cfs_proc_proc != NULL);
635 root = cfs_proc_proc;
638 entry = proc_search_entry(name, root);
644 /* remove the entry from the proc fs */
649 cfs_proc_entry_t * parent
653 ASSERT(cfs_proc_root != NULL);
654 if (parent == NULL) {
655 if (name[0] == '/') {
656 parent = cfs_proc_root;
658 ASSERT(cfs_proc_proc != NULL);
659 parent = cfs_proc_proc;
662 proc_remove_entry(name, parent);
667 void proc_destroy_splay(cfs_proc_entry_t * entry)
669 cfs_proc_entry_t * node;
671 if (S_ISDIR(entry->mode)) {
673 while (entry->root) {
674 node = CONTAINING_RECORD(entry->root, cfs_proc_entry_t, s_link);
675 entry->root = RtlDelete(&(node->s_link));
676 proc_destroy_splay(node);
680 proc_free_entry(entry);
683 cfs_proc_entry_t *proc_symlink(
685 cfs_proc_entry_t *parent,
689 cfs_enter_debugger();
693 cfs_proc_entry_t *proc_mkdir(
695 cfs_proc_entry_t *parent)
697 return create_proc_entry((char *)name, S_IFDIR, parent);
700 void proc_destory_subtree(cfs_proc_entry_t *entry)
704 proc_destroy_splay(entry);
708 /* destory the whole proc fs tree */
710 void proc_destroy_fs()
715 proc_destroy_splay(cfs_proc_root);
718 if (proc_entry_cache) {
719 cfs_mem_cache_destroy(proc_entry_cache);
725 static char proc_item_path[512];
728 void proc_show_tree(cfs_proc_entry_t * node);
729 void proc_print_node(cfs_proc_entry_t * node)
731 if (node != cfs_proc_root) {
732 if (S_ISDIR(node->mode)) {
733 printk("%s/%s/\n", proc_item_path, node->name);
735 printk("%s/%s\n", proc_item_path, node->name);
738 printk("%s\n", node->name);
741 if (S_ISDIR(node->mode)) {
742 proc_show_tree(node);
746 void proc_show_child(PRTL_SPLAY_LINKS link)
748 cfs_proc_entry_t * entry = NULL;
754 proc_show_child(link->LeftChild);
755 entry = CONTAINING_RECORD(link, cfs_proc_entry_t, s_link);
756 proc_print_node(entry);
757 proc_show_child(link->RightChild);
760 void proc_show_tree(cfs_proc_entry_t * node)
762 PRTL_SPLAY_LINKS link = NULL;
763 cfs_proc_entry_t * entry = NULL;
767 i = strlen(proc_item_path);
768 ASSERT(S_ISDIR(node->mode));
769 if (node != cfs_proc_root) {
770 strcat(proc_item_path, "/");
771 strcat(proc_item_path, node->name);
773 proc_show_child(link);
774 proc_item_path[i] = 0;
777 void proc_print_splay()
779 printk("=================================================\n");
780 printk("Lustre virtual proc entries:\n");
781 printk("-------------------------------------------------\n");
783 proc_show_tree(cfs_proc_root);
785 printk("=================================================\n");
789 /* initilaize / build the proc fs tree */
792 cfs_proc_entry_t * root = NULL;
794 memset(&(root_table_header), 0, sizeof(struct ctl_table_header));
795 CFS_INIT_LIST_HEAD(&(root_table_header.ctl_entry));
798 proc_entry_cache = cfs_mem_cache_create(
800 sizeof(cfs_proc_entry_t),
805 if (!proc_entry_cache) {
809 root = proc_alloc_entry();
814 root->magic = CFS_PROC_ENTRY_MAGIC;
815 root->flags = CFS_PROC_FLAG_DIRECTORY;
816 root->mode = S_IFDIR | S_IRUGO | S_IXUGO;
817 root->nlink = 3; // root should never be deleted.
820 cfs_proc_root = root;
822 cfs_proc_dev = create_proc_entry("dev", S_IFDIR, root);
826 cfs_proc_dev->nlink = 1;
828 cfs_proc_proc = create_proc_entry("proc", S_IFDIR, root);
829 if (!cfs_proc_proc) {
832 cfs_proc_proc->nlink = 1;
834 cfs_proc_fs = create_proc_entry("fs", S_IFDIR, cfs_proc_proc);
838 cfs_proc_fs->nlink = 1;
840 cfs_proc_sys = create_proc_entry("sys", S_IFDIR, cfs_proc_proc);
844 cfs_proc_sys->nlink = 1;
856 static ssize_t do_rw_proc(int write, struct file * file, char * buf,
857 size_t count, loff_t *ppos)
860 cfs_proc_entry_t *de;
861 struct ctl_table *table;
865 de = (cfs_proc_entry_t *) file->proc_dentry;
867 if (!de || !de->data)
869 table = (struct ctl_table *) de->data;
870 if (!table || !table->proc_handler)
872 op = (write ? 002 : 004);
877 * FIXME: we need to pass on ppos to the handler.
880 error = (*table->proc_handler) (table, write, file, buf, &res);
886 static ssize_t proc_readsys(struct file * file, char * buf,
887 size_t count, loff_t *ppos)
889 return do_rw_proc(0, file, buf, count, ppos);
892 static ssize_t proc_writesys(struct file * file, const char * buf,
893 size_t count, loff_t *ppos)
895 return do_rw_proc(1, file, (char *) buf, count, ppos);
899 struct file_operations proc_sys_file_operations = {
900 /*owner*/ THIS_MODULE,
902 /*read:*/ proc_readsys,
903 /*write:*/ proc_writesys,
910 /* Scan the sysctl entries in table and add them all into /proc */
911 void register_proc_table(cfs_sysctl_table_t * table, cfs_proc_entry_t * root)
913 cfs_proc_entry_t * de;
917 for (; table->ctl_name; table++) {
918 /* Can't do anything without a proc name. */
919 if (!table->procname)
921 /* Maybe we can't do anything with it... */
922 if (!table->proc_handler && !table->child) {
923 printk(CFS_KERN_WARNING "SYSCTL: Can't register %s\n",
928 len = strlen(table->procname);
932 if (table->proc_handler)
935 de = search_proc_entry(table->procname, root);
939 /* If the subdir exists already, de is non-NULL */
944 de = create_proc_entry((char *)table->procname, mode, root);
947 de->data = (void *) table;
948 if (table->proc_handler) {
949 de->proc_fops = &proc_sys_file_operations;
953 if (de->mode & S_IFDIR)
954 register_proc_table(table->child, de);
960 * Unregister a /proc sysctl table and any subdirectories.
962 void unregister_proc_table(cfs_sysctl_table_t * table, cfs_proc_entry_t *root)
964 cfs_proc_entry_t *de;
965 for (; table->ctl_name; table++) {
966 if (!(de = table->de))
968 if (de->mode & S_IFDIR) {
970 printk (CFS_KERN_ALERT "Help- malformed sysctl tree on free\n");
973 unregister_proc_table(table->child, de);
975 /* Don't unregister directories which still have entries.. */
980 /* Don't unregister proc entries that are still being used.. */
985 remove_proc_entry((char *)table->procname, root);
989 /* The generic string strategy routine: */
990 int sysctl_string(cfs_sysctl_table_t *table, int *name, int nlen,
991 void *oldval, size_t *oldlenp,
992 void *newval, size_t newlen, void **context)
996 if (!table->data || !table->maxlen)
999 if (oldval && oldlenp) {
1000 if(get_user(len, oldlenp))
1003 l = strlen(table->data);
1004 if (len > l) len = l;
1005 if (len >= table->maxlen)
1006 len = table->maxlen;
1007 if(cfs_copy_to_user(oldval, table->data, len))
1009 if(put_user(0, ((char *) oldval) + len))
1011 if(put_user(len, oldlenp))
1015 if (newval && newlen) {
1017 if (len > table->maxlen)
1018 len = table->maxlen;
1019 if(cfs_copy_from_user(table->data, newval, len))
1021 if (len == table->maxlen)
1023 ((char *) table->data)[len] = 0;
1029 * simple_strtoul - convert a string to an unsigned long
1030 * @cp: The start of the string
1031 * @endp: A pointer to the end of the parsed string will be placed here
1032 * @base: The number base to use
1034 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
1036 unsigned long result = 0, value;
1043 if ((*cp == 'x') && cfs_isxdigit(cp[1])) {
1049 while (cfs_isxdigit(*cp) &&
1050 (value = cfs_isdigit(*cp) ? *cp-'0' : toupper(*cp)-'A'+10) < base) {
1051 result = result*base + value;
1066 static int do_proc_dointvec(cfs_sysctl_table_t *table, int write, struct file *filp,
1067 void *buffer, size_t *lenp, int conv, int op)
1069 int *i, vleft, first=1, neg, val;
1072 #define TMPBUFLEN 20
1073 char buf[TMPBUFLEN], *p;
1075 if (!table->data || !table->maxlen || !*lenp)
1081 i = (int *) table->data;
1082 vleft = table->maxlen / sizeof(int);
1085 for (; left && vleft--; i++, first=0) {
1089 if(get_user(c,(char *) buffer))
1094 ((char *) buffer)++;
1100 if (len > TMPBUFLEN-1)
1102 if(cfs_copy_from_user(buf, buffer, len))
1106 if (*p == '-' && left > 1) {
1110 if (*p < '0' || *p > '9')
1112 val = simple_strtoul(p, &p, 0) * conv;
1114 if ((len < left) && *p && !isspace(*p))
1118 (char *)buffer += len;
1121 case OP_SET: *i = val; break;
1122 case OP_AND: *i &= val; break;
1123 case OP_OR: *i |= val; break;
1124 case OP_MAX: if(*i < val)
1127 case OP_MIN: if(*i > val)
1135 sprintf(p, "%d", (*i) / conv);
1139 if(cfs_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(cfs_sysctl_table_t *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(cfs_sysctl_table_t *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(cfs_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(cfs_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 (cfs_sysctl_table_t *table,
1257 int *name, int nlen,
1258 void *oldval, size_t *oldlenp,
1259 void *newval, size_t newlen, void **context)
1269 if (table->strategy) {
1270 rc = table->strategy(table, name, nlen, oldval, oldlenp,
1271 newval, newlen, context);
1278 /* If there is no strategy routine, or if the strategy returns
1279 * zero, proceed with automatic r/w */
1280 if (table->data && table->maxlen) {
1281 if (oldval && oldlenp) {
1282 get_user(len, oldlenp);
1284 if (len > (size_t)table->maxlen)
1285 len = (size_t)table->maxlen;
1286 if(cfs_copy_to_user(oldval, table->data, len))
1288 if(put_user(len, oldlenp))
1292 if (newval && newlen) {
1294 if (len > (size_t)table->maxlen)
1295 len = (size_t)table->maxlen;
1296 if(cfs_copy_from_user(table->data, newval, len))
1303 static int parse_table(int *name, int nlen,
1304 void *oldval, size_t *oldlenp,
1305 void *newval, size_t newlen,
1306 cfs_sysctl_table_t *table, void **context)
1314 if (get_user(n, name))
1316 for ( ; table->ctl_name; table++) {
1317 if (n == table->ctl_name || table->ctl_name == CTL_ANY) {
1321 if (ctl_perm(table, 001))
1324 if (table->strategy) {
1325 error = table->strategy(
1328 newval, newlen, context);
1334 table = table->child;
1337 error = do_sysctl_strategy(table, name, nlen,
1339 newval, newlen, context);
1346 int do_sysctl(int *name, int nlen, void *oldval, size_t *oldlenp,
1347 void *newval, size_t newlen)
1351 if (nlen <= 0 || nlen >= CTL_MAXNAME)
1355 if (!oldlenp || get_user(old_len, oldlenp))
1358 tmp = &root_table_header.ctl_entry;
1360 struct ctl_table_header *head =
1361 cfs_list_entry(tmp, struct ctl_table_header, ctl_entry);
1362 void *context = NULL;
1363 int error = parse_table(name, nlen, oldval, oldlenp,
1364 newval, newlen, head->ctl_table,
1368 if (error != -ENOTDIR)
1371 } while (tmp != &root_table_header.ctl_entry);
1376 * register_sysctl_table - register a sysctl heirarchy
1377 * @table: the top-level table structure
1378 * @insert_at_head: whether the entry should be inserted in front or at the end
1380 * Register a sysctl table heirarchy. @table should be a filled in ctl_table
1381 * array. An entry with a ctl_name of 0 terminates the table.
1383 * The members of the &ctl_table structure are used as follows:
1385 * ctl_name - This is the numeric sysctl value used by sysctl(2). The number
1386 * must be unique within that level of sysctl
1388 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1389 * enter a sysctl file
1391 * data - a pointer to data for use by proc_handler
1393 * maxlen - the maximum size in bytes of the data
1395 * mode - the file permissions for the /proc/sys file, and for sysctl(2)
1397 * child - a pointer to the child sysctl table if this entry is a directory, or
1400 * proc_handler - the text handler routine (described below)
1402 * strategy - the strategy routine (described below)
1404 * de - for internal use by the sysctl routines
1406 * extra1, extra2 - extra pointers usable by the proc handler routines
1408 * Leaf nodes in the sysctl tree will be represented by a single file
1409 * under /proc; non-leaf nodes will be represented by directories.
1411 * sysctl(2) can automatically manage read and write requests through
1412 * the sysctl table. The data and maxlen fields of the ctl_table
1413 * struct enable minimal validation of the values being written to be
1414 * performed, and the mode field allows minimal authentication.
1416 * More sophisticated management can be enabled by the provision of a
1417 * strategy routine with the table entry. This will be called before
1418 * any automatic read or write of the data is performed.
1420 * The strategy routine may return
1422 * < 0 - Error occurred (error is passed to user process)
1424 * 0 - OK - proceed with automatic read or write.
1426 * > 0 - OK - read or write has been done by the strategy routine, so
1427 * return immediately.
1429 * There must be a proc_handler routine for any terminal nodes
1430 * mirrored under /proc/sys (non-terminals are handled by a built-in
1431 * directory handler). Several default handlers are available to
1432 * cover common cases -
1434 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1435 * proc_dointvec_minmax(), proc_doulongvec_ms_jiffies_minmax(),
1436 * proc_doulongvec_minmax()
1438 * It is the handler's job to read the input buffer from user memory
1439 * and process it. The handler should return 0 on success.
1441 * This routine returns %NULL on a failure to register, and a pointer
1442 * to the table header on success.
1444 struct ctl_table_header *register_sysctl_table(cfs_sysctl_table_t * table,
1447 struct ctl_table_header *tmp;
1448 tmp = cfs_alloc(sizeof(struct ctl_table_header), 0);
1451 tmp->ctl_table = table;
1453 CFS_INIT_LIST_HEAD(&tmp->ctl_entry);
1455 cfs_list_add(&tmp->ctl_entry, &root_table_header.ctl_entry);
1457 cfs_list_add_tail(&tmp->ctl_entry, &root_table_header.ctl_entry);
1458 #ifdef CONFIG_PROC_FS
1459 register_proc_table(table, cfs_proc_sys);
1465 * unregister_sysctl_table - unregister a sysctl table heirarchy
1466 * @header: the header returned from register_sysctl_table
1468 * Unregisters the sysctl table and all children. proc entries may not
1469 * actually be removed until they are no longer used by anyone.
1471 void unregister_sysctl_table(struct ctl_table_header * header)
1473 cfs_list_del(&header->ctl_entry);
1474 #ifdef CONFIG_PROC_FS
1475 unregister_proc_table(header->ctl_table, cfs_proc_sys);
1481 int cfs_psdev_register(cfs_psdev_t * psdev)
1483 cfs_proc_entry_t * entry;
1485 entry = create_proc_entry (
1486 (char *)psdev->name,
1495 entry->flags |= CFS_PROC_FLAG_MISCDEV;
1497 entry->proc_fops = psdev->fops;
1498 entry->data = (void *)psdev;
1503 int cfs_psdev_deregister(cfs_psdev_t * psdev)
1505 cfs_proc_entry_t * entry;
1507 entry = search_proc_entry (
1508 (char *)psdev->name,
1514 ASSERT(entry->data == (void *)psdev);
1515 ASSERT(entry->flags & CFS_PROC_FLAG_MISCDEV);
1518 (char *)psdev->name,
1526 #define PSDEV_LNET (0x100)
1528 PSDEV_DEBUG = 1, /* control debugging */
1529 PSDEV_SUBSYSTEM_DEBUG, /* control debugging */
1530 PSDEV_PRINTK, /* force all messages to console */
1531 PSDEV_CONSOLE_RATELIMIT, /* rate limit console messages */
1532 PSDEV_DEBUG_PATH, /* crashdump log location */
1533 PSDEV_DEBUG_DUMP_PATH, /* crashdump tracelog location */
1534 PSDEV_LIBCFS_MEMUSED, /* bytes currently PORTAL_ALLOCated */
1537 static struct ctl_table lnet_table[] = {
1538 {PSDEV_DEBUG, "debug", &libcfs_debug, sizeof(int), 0644, NULL,
1540 {PSDEV_SUBSYSTEM_DEBUG, "subsystem_debug", &libcfs_subsystem_debug,
1541 sizeof(int), 0644, NULL, &proc_dointvec},
1542 {PSDEV_PRINTK, "printk", &libcfs_printk, sizeof(int), 0644, NULL,
1544 {PSDEV_CONSOLE_RATELIMIT, "console_ratelimit", &libcfs_console_ratelimit,
1545 sizeof(int), 0644, NULL, &proc_dointvec},
1547 {PSDEV_PORTALS_UPCALL, "upcall", portals_upcall,
1548 sizeof(portals_upcall), 0644, NULL, &proc_dostring,
1551 {PSDEV_LIBCFS_MEMUSED, "memused", (int *)&libcfs_kmemory.counter,
1552 sizeof(int), 0644, NULL, &proc_dointvec},
1556 static struct ctl_table top_table[2] = {
1557 {PSDEV_LNET, "lnet", NULL, 0, 0555, lnet_table},
1562 int trace_write_dump_kernel(struct file *file, const char *buffer,
1563 unsigned long count, void *data)
1565 int rc = cfs_trace_dump_debug_buffer_usrstr((void *)buffer, count);
1567 return (rc < 0) ? rc : count;
1570 int trace_write_daemon_file(struct file *file, const char *buffer,
1571 unsigned long count, void *data)
1573 int rc = cfs_trace_daemon_command_usrstr((void *)buffer, count);
1575 return (rc < 0) ? rc : count;
1578 int trace_read_daemon_file(char *page, char **start, off_t off, int count,
1579 int *eof, void *data)
1582 cfs_tracefile_read_lock();
1583 rc = cfs_trace_copyout_string(page, count, cfs_tracefile, "\n");
1584 cfs_tracefile_read_unlock();
1588 int trace_write_debug_mb(struct file *file, const char *buffer,
1589 unsigned long count, void *data)
1591 int rc = 0; /*trace_set_debug_mb_userstr((void *)buffer, count);*/
1593 return (rc < 0) ? rc : count;
1596 int trace_read_debug_mb(char *page, char **start, off_t off, int count,
1597 int *eof, void *data)
1601 snprintf(str, sizeof(str), "%d\n", cfs_trace_get_debug_mb());
1603 return cfs_trace_copyout_string(page, count, str, NULL);
1606 int insert_proc(void)
1608 cfs_proc_entry_t *ent;
1610 ent = create_proc_entry("sys/lnet/dump_kernel", 0, NULL);
1612 CERROR("couldn't register dump_kernel\n");
1615 ent->write_proc = trace_write_dump_kernel;
1617 ent = create_proc_entry("sys/lnet/daemon_file", 0, NULL);
1619 CERROR("couldn't register daemon_file\n");
1622 ent->write_proc = trace_write_daemon_file;
1623 ent->read_proc = trace_read_daemon_file;
1625 ent = create_proc_entry("sys/lnet/debug_mb", 0, NULL);
1627 CERROR("couldn't register debug_mb\n");
1630 ent->write_proc = trace_write_debug_mb;
1631 ent->read_proc = trace_read_debug_mb;
1636 void remove_proc(void)
1638 remove_proc_entry("sys/lnet/dump_kernel", NULL);
1639 remove_proc_entry("sys/lnet/daemon_file", NULL);
1640 remove_proc_entry("sys/lnet/debug_mb", NULL);
1645 * proc process routines of kernel space
1649 lustre_open_file(char * filename)
1652 cfs_file_t * fh = NULL;
1653 cfs_proc_entry_t * fp = NULL;
1655 fp = search_proc_entry(filename, cfs_proc_root);
1660 fh = cfs_alloc(sizeof(cfs_file_t), CFS_ALLOC_ZERO);
1665 fh->f_inode = cfs_alloc(sizeof(struct inode), CFS_ALLOC_ZERO);
1671 fh->f_inode->i_priv = (void *)fp;
1672 fh->f_op = fp->proc_fops;
1674 if (fh->f_op->open) {
1675 rc = (fh->f_op->open)(fh->f_inode, fh);
1681 cfs_free(fh->f_inode);
1690 lustre_close_file(cfs_file_t * fh)
1693 cfs_proc_entry_t * fp = NULL;
1695 fp = (cfs_proc_entry_t *) fh->f_inode->i_priv;
1696 if (fh->f_op->release) {
1697 rc = (fh->f_op->release)(fh->f_inode, fh);
1702 cfs_free(fh->f_inode);
1709 lustre_do_ioctl( cfs_file_t * fh,
1715 if (fh->f_op->ioctl) {
1716 rc = (fh->f_op->ioctl)(fh, cmd, arg);
1723 lustre_ioctl_file(cfs_file_t * fh, PCFS_PROC_IOCTL devctl)
1728 data = (ulong_ptr_t)devctl + sizeof(CFS_PROC_IOCTL);
1730 CLASSERT(sizeof(struct obd_ioctl_data) == 528);
1732 CLASSERT(sizeof(struct obd_ioctl_data) == 576);
1735 /* obd ioctl code */
1736 if (_IOC_TYPE(devctl->cmd) == 'f') {
1738 struct obd_ioctl_data * obd = (struct obd_ioctl_data *) data;
1740 if ( devctl->cmd != (ULONG)OBD_IOC_BRW_WRITE &&
1741 devctl->cmd != (ULONG)OBD_IOC_BRW_READ ) {
1743 unsigned long off = obd->ioc_len;
1745 if (obd->ioc_plen1) {
1746 obd->ioc_pbuf1 = (char *)(data + off);
1747 off += cfs_size_round(obd->ioc_plen1);
1749 obd->ioc_pbuf1 = NULL;
1752 if (obd->ioc_plen2) {
1753 obd->ioc_pbuf2 = (char *)(data + off);
1754 off += cfs_size_round(obd->ioc_plen2);
1756 obd->ioc_pbuf2 = NULL;
1761 rc = lustre_do_ioctl(fh, devctl->cmd, data);
1779 high = (off_t)(off >> 32);
1781 if (fh->f_op->read) {
1782 rc = (fh->f_op->read) (fh, buf, size, &off);
1786 fh->f_pos = off + rc;
1803 if (fh->f_op->write) {
1804 rc = (fh->f_op->write)(fh, buf, size, &off);
1816 * seq_open - initialize sequential file
1817 * @file: file we initialize
1818 * @op: method table describing the sequence
1820 * seq_open() sets @file, associating it with a sequence described
1821 * by @op. @op->start() sets the iterator up and returns the first
1822 * element of sequence. @op->stop() shuts it down. @op->next()
1823 * returns the next element of sequence. @op->show() prints element
1824 * into the buffer. In case of error ->start() and ->next() return
1825 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
1826 * returns 0 in case of success and negative number in case of error.
1828 int seq_open(struct file *file, const struct seq_operations *op)
1830 struct seq_file *p = file->private_data;
1833 p = kmalloc(sizeof(*p), GFP_KERNEL);
1836 file->private_data = p;
1838 memset(p, 0, sizeof(*p));
1839 mutex_init(&p->lock);
1843 * Wrappers around seq_open(e.g. swaps_open) need to be
1844 * aware of this. If they set f_version themselves, they
1845 * should call seq_open first and then set f_version.
1847 file->f_version = 0;
1849 /* SEQ files support lseek, but not pread/pwrite */
1850 file->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1853 EXPORT_SYMBOL(seq_open);
1856 * seq_read - ->read() method for sequential files.
1857 * @file: the file to read from
1858 * @buf: the buffer to read to
1859 * @size: the maximum number of bytes to read
1860 * @ppos: the current position in the file
1862 * Ready-made ->f_op->read()
1864 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
1866 struct seq_file *m = (struct seq_file *)file->private_data;
1873 mutex_lock(&m->lock);
1875 * seq_file->op->..m_start/m_stop/m_next may do special actions
1876 * or optimisations based on the file->f_version, so we want to
1877 * pass the file->f_version to those methods.
1879 * seq_file->version is just copy of f_version, and seq_file
1880 * methods can treat it simply as file version.
1881 * It is copied in first and copied out after all operations.
1882 * It is convenient to have it as part of structure to avoid the
1883 * need of passing another argument to all the seq_file methods.
1885 m->version = file->f_version;
1886 /* grab buffer if we didn't have one */
1888 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
1892 /* if not empty - flush it first */
1894 n = min(m->count, size);
1895 err = cfs_copy_to_user(buf, m->buf + m->from, n);
1908 /* we need at least one record in buffer */
1911 p = m->op->start(m, &pos);
1913 if (!p || IS_ERR(p))
1915 err = m->op->show(m, p);
1918 if (m->count < m->size)
1922 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
1932 /* they want more? let's try to get some more */
1933 while (m->count < size) {
1934 size_t offs = m->count;
1936 p = m->op->next(m, p, &next);
1937 if (!p || IS_ERR(p)) {
1941 err = m->op->show(m, p);
1942 if (err || m->count == m->size) {
1949 n = min(m->count, size);
1950 err = cfs_copy_to_user(buf, m->buf, n);
1965 file->f_version = m->version;
1966 mutex_unlock(&m->lock);
1975 EXPORT_SYMBOL(seq_read);
1977 static int traverse(struct seq_file *m, loff_t offset)
1979 loff_t pos = 0, index;
1985 m->count = m->from = 0;
1991 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
1995 p = m->op->start(m, &index);
2000 error = m->op->show(m, p);
2003 if (m->count == m->size)
2005 if (pos + (loff_t)(m->count) > offset) {
2006 m->from = (size_t)(offset - pos);
2007 m->count -= m->from;
2013 if (pos == offset) {
2018 p = m->op->next(m, p, &index);
2026 m->buf = cfs_alloc(m->size <<= 1, CFS_ALLOC_KERNEL | CFS_ALLOC_ZERO);
2027 return !m->buf ? -ENOMEM : -EAGAIN;
2031 * seq_lseek - ->llseek() method for sequential files.
2032 * @file: the file in question
2033 * @offset: new position
2034 * @origin: 0 for absolute, 1 for relative position
2036 * Ready-made ->f_op->llseek()
2038 loff_t seq_lseek(struct file *file, loff_t offset, int origin)
2040 struct seq_file *m = (struct seq_file *)file->private_data;
2041 long long retval = -EINVAL;
2043 mutex_lock(&m->lock);
2044 m->version = file->f_version;
2047 offset += file->f_pos;
2052 if (offset != file->f_pos) {
2053 while ((retval=traverse(m, offset)) == -EAGAIN)
2056 /* with extreme prejudice... */
2062 retval = file->f_pos = offset;
2066 file->f_version = m->version;
2067 mutex_unlock(&m->lock);
2070 EXPORT_SYMBOL(seq_lseek);
2073 * seq_release - free the structures associated with sequential file.
2074 * @file: file in question
2075 * @inode: file->f_path.dentry->d_inode
2077 * Frees the structures associated with sequential file; can be used
2078 * as ->f_op->release() if you don't have private data to destroy.
2080 int seq_release(struct inode *inode, struct file *file)
2082 struct seq_file *m = (struct seq_file *)file->private_data;
2090 EXPORT_SYMBOL(seq_release);
2093 * seq_escape - print string into buffer, escaping some characters
2096 * @esc: set of characters that need escaping
2098 * Puts string into buffer, replacing each occurrence of character from
2099 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
2102 int seq_escape(struct seq_file *m, const char *s, const char *esc)
2104 char *end = m->buf + m->size;
2108 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
2109 if (!strchr(esc, c)) {
2115 *p++ = '0' + ((c & 0300) >> 6);
2116 *p++ = '0' + ((c & 070) >> 3);
2117 *p++ = '0' + (c & 07);
2123 m->count = p - m->buf;
2126 EXPORT_SYMBOL(seq_escape);
2128 int seq_printf(struct seq_file *m, const char *f, ...)
2133 if (m->count < m->size) {
2135 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
2137 if (m->count + len < m->size) {
2145 EXPORT_SYMBOL(seq_printf);
2147 char *d_path(struct path *p, char *buffer, int buflen)
2149 cfs_enter_debugger();
2150 return ERR_PTR(-ENAMETOOLONG);
2153 int seq_path(struct seq_file *m, struct path *path, char *esc)
2155 if (m->count < m->size) {
2156 char *s = m->buf + m->count;
2157 char *p = d_path(path, s, m->size - m->count);
2162 p = m->buf + m->count;
2163 m->count = s - m->buf;
2164 return (int)(s - p);
2165 } else if (!strchr(esc, c)) {
2167 } else if (s + 4 > p) {
2171 *s++ = '0' + ((c & 0300) >> 6);
2172 *s++ = '0' + ((c & 070) >> 3);
2173 *s++ = '0' + (c & 07);
2181 EXPORT_SYMBOL(seq_path);
2183 static void *single_start(struct seq_file *p, loff_t *pos)
2185 return (void *) (INT_PTR) (*pos == 0);
2188 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
2194 static void single_stop(struct seq_file *p, void *v)
2198 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
2201 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
2205 op->start = single_start;
2206 op->next = single_next;
2207 op->stop = single_stop;
2209 res = seq_open(file, op);
2211 ((struct seq_file *)file->private_data)->private = data;
2217 EXPORT_SYMBOL(single_open);
2219 int single_release(struct inode *inode, struct file *file)
2221 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
2222 int res = seq_release(inode, file);
2223 cfs_free((void *)op);
2226 EXPORT_SYMBOL(single_release);
2228 int seq_release_private(struct inode *inode, struct file *file)
2230 struct seq_file *seq = file->private_data;
2232 cfs_free(seq->private);
2233 seq->private = NULL;
2234 return seq_release(inode, file);
2236 EXPORT_SYMBOL(seq_release_private);
2238 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
2243 struct seq_file *seq;
2245 private = cfs_alloc(psize, CFS_ALLOC_KERNEL | CFS_ALLOC_ZERO);
2246 if (private == NULL)
2249 rc = seq_open(f, ops);
2253 seq = f->private_data;
2254 seq->private = private;
2262 EXPORT_SYMBOL(__seq_open_private);
2264 int seq_open_private(struct file *filp, const struct seq_operations *ops,
2267 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
2269 EXPORT_SYMBOL(seq_open_private);
2271 int seq_putc(struct seq_file *m, char c)
2273 if (m->count < m->size) {
2274 m->buf[m->count++] = c;
2279 EXPORT_SYMBOL(seq_putc);
2281 int seq_puts(struct seq_file *m, const char *s)
2283 int len = strlen(s);
2284 if (m->count + len < m->size) {
2285 memcpy(m->buf + m->count, s, len);
2292 EXPORT_SYMBOL(seq_puts);
2294 cfs_list_t *seq_list_start(cfs_list_t *head, loff_t pos)
2298 cfs_list_for_each(lh, head)
2305 EXPORT_SYMBOL(seq_list_start);
2307 cfs_list_t *seq_list_start_head(cfs_list_t *head,
2313 return seq_list_start(head, pos - 1);
2316 EXPORT_SYMBOL(seq_list_start_head);
2318 cfs_list_t *seq_list_next(void *v, cfs_list_t *head,
2323 lh = ((cfs_list_t *)v)->next;
2325 return lh == head ? NULL : lh;
2328 EXPORT_SYMBOL(seq_list_next);
2330 struct proc_dir_entry *PDE(const struct inode *inode)
2332 return (struct proc_dir_entry *)inode->i_priv;
2336 #endif /* __KERNEL__ */