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.
37 # define EXPORT_SYMTAB
40 # define DEBUG_SUBSYSTEM S_LNET
42 #include <libcfs/libcfs.h>
43 #include "tracefile.h"
44 #include <lustre_lib.h>
50 * /proc emulator routines ...
53 /* The root node of the proc fs emulation: / */
54 cfs_proc_entry_t * cfs_proc_root = NULL;
56 /* The root node of the proc fs emulation: /proc */
57 cfs_proc_entry_t * cfs_proc_proc = NULL;
59 /* The fs sys directory: /proc/fs */
60 cfs_proc_entry_t * cfs_proc_fs = NULL;
62 /* The sys root: /proc/sys */
63 cfs_proc_entry_t * cfs_proc_sys = NULL;
65 /* The sys root: /proc/dev | to implement misc device */
66 cfs_proc_entry_t * cfs_proc_dev = NULL;
69 /* SLAB object for cfs_proc_entry_t allocation */
70 cfs_mem_cache_t * proc_entry_cache = NULL;
72 /* root node for sysctl table */
73 cfs_sysctl_table_header_t root_table_header;
75 /* The global lock to protect all the access */
77 #if LIBCFS_PROCFS_SPINLOCK
78 cfs_spinlock_t proc_fs_lock;
80 #define INIT_PROCFS_LOCK() cfs_spin_lock_init(&proc_fs_lock)
81 #define LOCK_PROCFS() cfs_spin_lock(&proc_fs_lock)
82 #define UNLOCK_PROCFS() cfs_spin_unlock(&proc_fs_lock)
86 cfs_mutex_t proc_fs_lock;
88 #define INIT_PROCFS_LOCK() cfs_init_mutex(&proc_fs_lock)
89 #define LOCK_PROCFS() cfs_mutex_down(&proc_fs_lock)
90 #define UNLOCK_PROCFS() cfs_mutex_up(&proc_fs_lock)
95 proc_file_read(struct file * file, const char * buf, size_t nbytes, loff_t *ppos)
102 cfs_proc_entry_t * dp;
104 dp = (cfs_proc_entry_t *) file->f_inode->i_priv;
105 if (!(page = (char*) cfs_alloc(CFS_PAGE_SIZE, 0)))
108 while ((nbytes > 0) && !eof) {
110 count = min_t(size_t, PROC_BLOCK_SIZE, nbytes);
114 n = dp->read_proc( page, &start, (long)*ppos,
115 count, &eof, dp->data);
121 * For proc files that are less than 4k
123 start = page + *ppos;
124 n -= (ssize_t)(*ppos);
131 break; /* End of file */
138 n -= cfs_copy_to_user((void *)buf, start, n);
156 proc_file_write(struct file * file, const char * buffer,
157 size_t count, loff_t *ppos)
159 cfs_proc_entry_t * dp;
161 dp = (cfs_proc_entry_t *) file->f_inode->i_priv;
166 /* FIXME: does this routine need ppos? probably... */
167 return dp->write_proc(file, buffer, count, dp->data);
170 struct file_operations proc_file_operations = {
171 /*owner*/ THIS_MODULE,
172 /*lseek:*/ NULL, //proc_file_lseek,
173 /*read:*/ proc_file_read,
174 /*write:*/ proc_file_write,
180 /* allocate proc entry block */
185 cfs_proc_entry_t * entry = NULL;
187 entry = cfs_mem_cache_alloc(proc_entry_cache, 0);
192 memset(entry, 0, sizeof(cfs_proc_entry_t));
194 entry->magic = CFS_PROC_ENTRY_MAGIC;
195 RtlInitializeSplayLinks(&(entry->s_link));
196 entry->proc_fops = &proc_file_operations;
201 /* free the proc entry block */
204 proc_free_entry(cfs_proc_entry_t * entry)
207 ASSERT(entry->magic == CFS_PROC_ENTRY_MAGIC);
209 cfs_mem_cache_free(proc_entry_cache, entry);
212 /* dissect the path string for a given full proc path */
222 int i = 0, j = 0, len = 0;
224 *first = *remain = NULL;
229 while (i < len && (path[i] == '/')) i++;
233 *first = (char *)path + i;
234 while (i < len && (path[i] != '/')) i++;
235 *first_len = (int)(path + i - *first);
238 *remain = (char *)path + i + 1;
243 /* search the children entries of the parent entry */
247 cfs_proc_entry_t * parent,
251 cfs_proc_entry_t * node;
252 PRTL_SPLAY_LINKS link;
254 ASSERT(parent->magic == CFS_PROC_ENTRY_MAGIC);
255 ASSERT(cfs_is_flag_set(parent->flags, CFS_PROC_FLAG_DIRECTORY));
261 ANSI_STRING ename,nname;
264 node = CONTAINING_RECORD(link, cfs_proc_entry_t, s_link);
266 ASSERT(node->magic == CFS_PROC_ENTRY_MAGIC);
268 /* Compare the prefix in the tree with the full name */
270 RtlInitAnsiString(&ename, name);
271 RtlInitAnsiString(&nname, node->name);
273 result = RtlCompareString(&nname, &ename,TRUE);
277 /* The prefix is greater than the full name
278 so we go down the left child */
280 link = RtlLeftChild(link);
282 } else if (result < 0) {
284 /* The prefix is less than the full name
285 so we go down the right child */
287 link = RtlRightChild(link);
291 /* We got the entry in the splay tree and
292 make it root node instead */
294 parent->root = RtlSplay(link);
299 /* we need continue searching down the tree ... */
302 /* There's no the exptected entry in the splay tree */
309 cfs_proc_entry_t * parent,
310 cfs_proc_entry_t * child
313 cfs_proc_entry_t * entry;
315 ASSERT(parent != NULL && child != NULL);
316 ASSERT(parent->magic == CFS_PROC_ENTRY_MAGIC);
317 ASSERT(child->magic == CFS_PROC_ENTRY_MAGIC);
318 ASSERT(cfs_is_flag_set(parent->flags, CFS_PROC_FLAG_DIRECTORY));
321 parent->root = &(child->s_link);
323 entry = CONTAINING_RECORD(parent->root, cfs_proc_entry_t, s_link);
326 ANSI_STRING ename, cname;
328 ASSERT(entry->magic == CFS_PROC_ENTRY_MAGIC);
330 RtlInitAnsiString(&ename, entry->name);
331 RtlInitAnsiString(&cname, child->name);
333 result = RtlCompareString(&ename, &cname,TRUE);
336 cfs_enter_debugger();
337 if (entry == child) {
344 if (RtlLeftChild(&entry->s_link) == NULL) {
345 RtlInsertAsLeftChild(&entry->s_link, &child->s_link);
348 entry = CONTAINING_RECORD( RtlLeftChild(&entry->s_link),
349 cfs_proc_entry_t, s_link);
352 if (RtlRightChild(&entry->s_link) == NULL) {
353 RtlInsertAsRightChild(&entry->s_link, &child->s_link);
356 entry = CONTAINING_RECORD( RtlRightChild(&entry->s_link),
357 cfs_proc_entry_t, s_link );
363 cfs_set_flag(child->flags, CFS_PROC_FLAG_ATTACHED);
365 child->parent = parent;
371 /* remove a child entry from the splay tree */
374 cfs_proc_entry_t * parent,
375 cfs_proc_entry_t * child
378 cfs_proc_entry_t * entry = NULL;
380 ASSERT(parent != NULL && child != NULL);
381 ASSERT(parent->magic == CFS_PROC_ENTRY_MAGIC);
382 ASSERT(child->magic == CFS_PROC_ENTRY_MAGIC);
383 ASSERT(cfs_is_flag_set(parent->flags, CFS_PROC_FLAG_DIRECTORY));
384 ASSERT(cfs_is_flag_set(child->flags, CFS_PROC_FLAG_ATTACHED));
385 ASSERT(child->parent == parent);
387 entry = proc_search_splay(parent, child->name);
390 ASSERT(entry == child);
391 parent->root = RtlDelete(&(entry->s_link));
394 cfs_enter_debugger();
402 /* search a node inside the proc fs tree */
407 cfs_proc_entry_t * root
410 cfs_proc_entry_t * entry;
411 cfs_proc_entry_t * parent;
412 char *first, *remain;
419 ename = cfs_alloc(0x21, CFS_ALLOC_ZERO);
427 /* dissect the file name string */
428 proc_dissect_name(name, &first, &flen, &remain);
433 cfs_enter_debugger();
438 memset(ename, 0, 0x20);
439 memcpy(ename, first, flen);
441 entry = proc_search_splay(parent, ename);
464 /* insert the path nodes to the proc fs tree */
469 cfs_proc_entry_t * root
472 cfs_proc_entry_t *entry;
473 cfs_proc_entry_t *parent;
474 char *first, *remain;
483 proc_dissect_name(name, &first, &flen, &remain);
491 memset(ename, 0, 0x20);
492 memcpy(ename, first, flen);
494 entry = proc_search_splay(parent, ename);
497 entry = proc_alloc_entry();
498 memcpy(entry->name, ename, flen);
501 if(!proc_insert_splay(parent, entry)) {
502 proc_free_entry(entry);
513 entry->mode |= S_IFDIR | S_IRUGO | S_IXUGO;
514 cfs_set_flag(entry->flags, CFS_PROC_FLAG_DIRECTORY);
524 /* remove the path nodes from the proc fs tree */
529 cfs_proc_entry_t * root
532 cfs_proc_entry_t *entry;
533 char *first, *remain;
539 proc_dissect_name(name, &first, &flen, &remain);
543 memset(ename, 0, 0x20);
544 memcpy(ename, first, flen);
546 entry = proc_search_splay(root, ename);
551 ASSERT(S_ISDIR(entry->mode));
552 proc_remove_entry(remain, entry);
556 proc_remove_splay(root, entry);
557 proc_free_entry(entry);
561 cfs_enter_debugger();
565 /* create proc entry and insert it into the proc fs */
571 cfs_proc_entry_t * parent
574 cfs_proc_entry_t *entry = NULL;
577 if ((mode & S_IALLUGO) == 0)
578 mode |= S_IRUGO | S_IXUGO;
580 if ((mode & S_IFMT) == 0)
582 if ((mode & S_IALLUGO) == 0)
587 ASSERT(NULL != cfs_proc_root);
590 if (name[0] == '/') {
591 parent = cfs_proc_root;
593 ASSERT(NULL != cfs_proc_proc);
594 parent = cfs_proc_proc;
598 entry = proc_search_entry(name, parent);
601 entry = proc_insert_entry(name, parent);
603 /* Failed to create/insert the splay node ... */
604 cfs_enter_debugger();
607 /* Initializing entry ... */
611 cfs_set_flag(entry->flags, CFS_PROC_FLAG_DIRECTORY);
623 /* search the specified entry form the proc fs */
628 cfs_proc_entry_t * root
631 cfs_proc_entry_t * entry;
634 ASSERT(cfs_proc_root != NULL);
636 if (name[0] == '/') {
637 root = cfs_proc_root;
639 ASSERT(cfs_proc_proc != NULL);
640 root = cfs_proc_proc;
643 entry = proc_search_entry(name, root);
649 /* remove the entry from the proc fs */
654 cfs_proc_entry_t * parent
658 ASSERT(cfs_proc_root != NULL);
659 if (parent == NULL) {
660 if (name[0] == '/') {
661 parent = cfs_proc_root;
663 ASSERT(cfs_proc_proc != NULL);
664 parent = cfs_proc_proc;
667 proc_remove_entry(name, parent);
672 void proc_destroy_splay(cfs_proc_entry_t * entry)
674 cfs_proc_entry_t * node;
676 if (S_ISDIR(entry->mode)) {
678 while (entry->root) {
679 node = CONTAINING_RECORD(entry->root, cfs_proc_entry_t, s_link);
680 entry->root = RtlDelete(&(node->s_link));
681 proc_destroy_splay(node);
685 proc_free_entry(entry);
688 cfs_proc_entry_t *proc_symlink(
690 cfs_proc_entry_t *parent,
694 cfs_enter_debugger();
698 cfs_proc_entry_t *proc_mkdir(
700 cfs_proc_entry_t *parent)
702 return create_proc_entry((char *)name, S_IFDIR, parent);
705 void proc_destory_subtree(cfs_proc_entry_t *entry)
709 proc_destroy_splay(entry);
713 /* destory the whole proc fs tree */
715 void proc_destroy_fs()
720 proc_destroy_splay(cfs_proc_root);
723 if (proc_entry_cache) {
724 cfs_mem_cache_destroy(proc_entry_cache);
730 static char proc_item_path[512];
733 void proc_show_tree(cfs_proc_entry_t * node);
734 void proc_print_node(cfs_proc_entry_t * node)
736 if (node != cfs_proc_root) {
737 if (S_ISDIR(node->mode)) {
738 printk("%s/%s/\n", proc_item_path, node->name);
740 printk("%s/%s\n", proc_item_path, node->name);
743 printk("%s\n", node->name);
746 if (S_ISDIR(node->mode)) {
747 proc_show_tree(node);
751 void proc_show_child(PRTL_SPLAY_LINKS link)
753 cfs_proc_entry_t * entry = NULL;
759 proc_show_child(link->LeftChild);
760 entry = CONTAINING_RECORD(link, cfs_proc_entry_t, s_link);
761 proc_print_node(entry);
762 proc_show_child(link->RightChild);
765 void proc_show_tree(cfs_proc_entry_t * node)
767 PRTL_SPLAY_LINKS link = NULL;
768 cfs_proc_entry_t * entry = NULL;
772 i = strlen(proc_item_path);
773 ASSERT(S_ISDIR(node->mode));
774 if (node != cfs_proc_root) {
775 strcat(proc_item_path, "/");
776 strcat(proc_item_path, node->name);
778 proc_show_child(link);
779 proc_item_path[i] = 0;
782 void proc_print_splay()
784 printk("=================================================\n");
785 printk("Lustre virtual proc entries:\n");
786 printk("-------------------------------------------------\n");
788 proc_show_tree(cfs_proc_root);
790 printk("=================================================\n");
794 /* initilaize / build the proc fs tree */
797 cfs_proc_entry_t * root = NULL;
799 memset(&(root_table_header), 0, sizeof(struct ctl_table_header));
800 CFS_INIT_LIST_HEAD(&(root_table_header.ctl_entry));
803 proc_entry_cache = cfs_mem_cache_create(
805 sizeof(cfs_proc_entry_t),
810 if (!proc_entry_cache) {
814 root = proc_alloc_entry();
819 root->magic = CFS_PROC_ENTRY_MAGIC;
820 root->flags = CFS_PROC_FLAG_DIRECTORY;
821 root->mode = S_IFDIR | S_IRUGO | S_IXUGO;
822 root->nlink = 3; // root should never be deleted.
825 cfs_proc_root = root;
827 cfs_proc_dev = create_proc_entry("dev", S_IFDIR, root);
831 cfs_proc_dev->nlink = 1;
833 cfs_proc_proc = create_proc_entry("proc", S_IFDIR, root);
834 if (!cfs_proc_proc) {
837 cfs_proc_proc->nlink = 1;
839 cfs_proc_fs = create_proc_entry("fs", S_IFDIR, cfs_proc_proc);
843 cfs_proc_fs->nlink = 1;
845 cfs_proc_sys = create_proc_entry("sys", S_IFDIR, cfs_proc_proc);
849 cfs_proc_sys->nlink = 1;
861 static ssize_t do_rw_proc(int write, struct file * file, char * buf,
862 size_t count, loff_t *ppos)
865 cfs_proc_entry_t *de;
866 struct ctl_table *table;
870 de = (cfs_proc_entry_t *) file->proc_dentry;
872 if (!de || !de->data)
874 table = (struct ctl_table *) de->data;
875 if (!table || !table->proc_handler)
877 op = (write ? 002 : 004);
882 * FIXME: we need to pass on ppos to the handler.
885 error = (*table->proc_handler) (table, write, file, buf, &res);
891 static ssize_t proc_readsys(struct file * file, char * buf,
892 size_t count, loff_t *ppos)
894 return do_rw_proc(0, file, buf, count, ppos);
897 static ssize_t proc_writesys(struct file * file, const char * buf,
898 size_t count, loff_t *ppos)
900 return do_rw_proc(1, file, (char *) buf, count, ppos);
904 struct file_operations proc_sys_file_operations = {
905 /*owner*/ THIS_MODULE,
907 /*read:*/ proc_readsys,
908 /*write:*/ proc_writesys,
915 /* Scan the sysctl entries in table and add them all into /proc */
916 void register_proc_table(cfs_sysctl_table_t * table, cfs_proc_entry_t * root)
918 cfs_proc_entry_t * de;
922 for (; table->ctl_name; table++) {
923 /* Can't do anything without a proc name. */
924 if (!table->procname)
926 /* Maybe we can't do anything with it... */
927 if (!table->proc_handler && !table->child) {
928 printk(CFS_KERN_WARNING "SYSCTL: Can't register %s\n",
933 len = strlen(table->procname);
937 if (table->proc_handler)
940 de = search_proc_entry(table->procname, root);
944 /* If the subdir exists already, de is non-NULL */
949 de = create_proc_entry((char *)table->procname, mode, root);
952 de->data = (void *) table;
953 if (table->proc_handler) {
954 de->proc_fops = &proc_sys_file_operations;
958 if (de->mode & S_IFDIR)
959 register_proc_table(table->child, de);
965 * Unregister a /proc sysctl table and any subdirectories.
967 void unregister_proc_table(cfs_sysctl_table_t * table, cfs_proc_entry_t *root)
969 cfs_proc_entry_t *de;
970 for (; table->ctl_name; table++) {
971 if (!(de = table->de))
973 if (de->mode & S_IFDIR) {
975 printk (CFS_KERN_ALERT "Help- malformed sysctl tree on free\n");
978 unregister_proc_table(table->child, de);
980 /* Don't unregister directories which still have entries.. */
985 /* Don't unregister proc entries that are still being used.. */
990 remove_proc_entry((char *)table->procname, root);
994 /* The generic string strategy routine: */
995 int sysctl_string(cfs_sysctl_table_t *table, int *name, int nlen,
996 void *oldval, size_t *oldlenp,
997 void *newval, size_t newlen, void **context)
1001 if (!table->data || !table->maxlen)
1004 if (oldval && oldlenp) {
1005 if(get_user(len, oldlenp))
1008 l = strlen(table->data);
1009 if (len > l) len = l;
1010 if (len >= table->maxlen)
1011 len = table->maxlen;
1012 if(cfs_copy_to_user(oldval, table->data, len))
1014 if(put_user(0, ((char *) oldval) + len))
1016 if(put_user(len, oldlenp))
1020 if (newval && newlen) {
1022 if (len > table->maxlen)
1023 len = table->maxlen;
1024 if(cfs_copy_from_user(table->data, newval, len))
1026 if (len == table->maxlen)
1028 ((char *) table->data)[len] = 0;
1034 * simple_strtoul - convert a string to an unsigned long
1035 * @cp: The start of the string
1036 * @endp: A pointer to the end of the parsed string will be placed here
1037 * @base: The number base to use
1039 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
1041 unsigned long result = 0, value;
1048 if ((*cp == 'x') && cfs_isxdigit(cp[1])) {
1054 while (cfs_isxdigit(*cp) &&
1055 (value = cfs_isdigit(*cp) ? *cp-'0' : toupper(*cp)-'A'+10) < base) {
1056 result = result*base + value;
1071 static int do_proc_dointvec(cfs_sysctl_table_t *table, int write, struct file *filp,
1072 void *buffer, size_t *lenp, int conv, int op)
1074 int *i, vleft, first=1, neg, val;
1077 #define TMPBUFLEN 20
1078 char buf[TMPBUFLEN], *p;
1080 if (!table->data || !table->maxlen || !*lenp)
1086 i = (int *) table->data;
1087 vleft = table->maxlen / sizeof(int);
1090 for (; left && vleft--; i++, first=0) {
1094 if(get_user(c,(char *) buffer))
1099 ((char *) buffer)++;
1105 if (len > TMPBUFLEN-1)
1107 if(cfs_copy_from_user(buf, buffer, len))
1111 if (*p == '-' && left > 1) {
1115 if (*p < '0' || *p > '9')
1117 val = simple_strtoul(p, &p, 0) * conv;
1119 if ((len < left) && *p && !isspace(*p))
1123 (char *)buffer += len;
1126 case OP_SET: *i = val; break;
1127 case OP_AND: *i &= val; break;
1128 case OP_OR: *i |= val; break;
1129 case OP_MAX: if(*i < val)
1132 case OP_MIN: if(*i > val)
1140 sprintf(p, "%d", (*i) / conv);
1144 if(cfs_copy_to_user(buffer, buf, len))
1147 (char *)buffer += len;
1151 if (!write && !first && left) {
1152 if(put_user('\n', (char *) buffer))
1154 left--, ((char *)buffer)++;
1157 p = (char *) buffer;
1160 if(get_user(c, p++))
1170 memset(&(filp->f_pos) , 0, sizeof(loff_t));
1171 filp->f_pos += (loff_t)(*lenp);
1176 * proc_dointvec - read a vector of integers
1177 * @table: the sysctl table
1178 * @write: %TRUE if this is a write to the sysctl file
1179 * @filp: the file structure
1180 * @buffer: the user buffer
1181 * @lenp: the size of the user buffer
1183 * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
1184 * values from/to the user buffer, treated as an ASCII string.
1186 * Returns 0 on success.
1188 int proc_dointvec(cfs_sysctl_table_t *table, int write, struct file *filp,
1189 void *buffer, size_t *lenp)
1191 return do_proc_dointvec(table,write,filp,buffer,lenp,1,OP_SET);
1196 * proc_dostring - read a string sysctl
1197 * @table: the sysctl table
1198 * @write: %TRUE if this is a write to the sysctl file
1199 * @filp: the file structure
1200 * @buffer: the user buffer
1201 * @lenp: the size of the user buffer
1203 * Reads/writes a string from/to the user buffer. If the kernel
1204 * buffer provided is not large enough to hold the string, the
1205 * string is truncated. The copied string is %NULL-terminated.
1206 * If the string is being read by the user process, it is copied
1207 * and a newline '\n' is added. It is truncated if the buffer is
1210 * Returns 0 on success.
1212 int proc_dostring(cfs_sysctl_table_t *table, int write, struct file *filp,
1213 void *buffer, size_t *lenp)
1218 if (!table->data || !table->maxlen || !*lenp ||
1219 (filp->f_pos && !write)) {
1227 while (len < *lenp) {
1228 if(get_user(c, p++))
1230 if (c == 0 || c == '\n')
1234 if (len >= (size_t)table->maxlen)
1235 len = (size_t)table->maxlen-1;
1236 if(cfs_copy_from_user(table->data, buffer, len))
1238 ((char *) table->data)[len] = 0;
1239 filp->f_pos += *lenp;
1241 len = (size_t)strlen(table->data);
1242 if (len > (size_t)table->maxlen)
1243 len = (size_t)table->maxlen;
1247 if(cfs_copy_to_user(buffer, table->data, len))
1250 if(put_user('\n', ((char *) buffer) + len))
1260 /* Perform the actual read/write of a sysctl table entry. */
1261 int do_sysctl_strategy (cfs_sysctl_table_t *table,
1262 int *name, int nlen,
1263 void *oldval, size_t *oldlenp,
1264 void *newval, size_t newlen, void **context)
1274 if (table->strategy) {
1275 rc = table->strategy(table, name, nlen, oldval, oldlenp,
1276 newval, newlen, context);
1283 /* If there is no strategy routine, or if the strategy returns
1284 * zero, proceed with automatic r/w */
1285 if (table->data && table->maxlen) {
1286 if (oldval && oldlenp) {
1287 get_user(len, oldlenp);
1289 if (len > (size_t)table->maxlen)
1290 len = (size_t)table->maxlen;
1291 if(cfs_copy_to_user(oldval, table->data, len))
1293 if(put_user(len, oldlenp))
1297 if (newval && newlen) {
1299 if (len > (size_t)table->maxlen)
1300 len = (size_t)table->maxlen;
1301 if(cfs_copy_from_user(table->data, newval, len))
1308 static int parse_table(int *name, int nlen,
1309 void *oldval, size_t *oldlenp,
1310 void *newval, size_t newlen,
1311 cfs_sysctl_table_t *table, void **context)
1319 if (get_user(n, name))
1321 for ( ; table->ctl_name; table++) {
1322 if (n == table->ctl_name || table->ctl_name == CTL_ANY) {
1326 if (ctl_perm(table, 001))
1329 if (table->strategy) {
1330 error = table->strategy(
1333 newval, newlen, context);
1339 table = table->child;
1342 error = do_sysctl_strategy(table, name, nlen,
1344 newval, newlen, context);
1351 int do_sysctl(int *name, int nlen, void *oldval, size_t *oldlenp,
1352 void *newval, size_t newlen)
1356 if (nlen <= 0 || nlen >= CTL_MAXNAME)
1360 if (!oldlenp || get_user(old_len, oldlenp))
1363 tmp = &root_table_header.ctl_entry;
1365 struct ctl_table_header *head =
1366 cfs_list_entry(tmp, struct ctl_table_header, ctl_entry);
1367 void *context = NULL;
1368 int error = parse_table(name, nlen, oldval, oldlenp,
1369 newval, newlen, head->ctl_table,
1373 if (error != -ENOTDIR)
1376 } while (tmp != &root_table_header.ctl_entry);
1381 * register_sysctl_table - register a sysctl heirarchy
1382 * @table: the top-level table structure
1383 * @insert_at_head: whether the entry should be inserted in front or at the end
1385 * Register a sysctl table heirarchy. @table should be a filled in ctl_table
1386 * array. An entry with a ctl_name of 0 terminates the table.
1388 * The members of the &ctl_table structure are used as follows:
1390 * ctl_name - This is the numeric sysctl value used by sysctl(2). The number
1391 * must be unique within that level of sysctl
1393 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1394 * enter a sysctl file
1396 * data - a pointer to data for use by proc_handler
1398 * maxlen - the maximum size in bytes of the data
1400 * mode - the file permissions for the /proc/sys file, and for sysctl(2)
1402 * child - a pointer to the child sysctl table if this entry is a directory, or
1405 * proc_handler - the text handler routine (described below)
1407 * strategy - the strategy routine (described below)
1409 * de - for internal use by the sysctl routines
1411 * extra1, extra2 - extra pointers usable by the proc handler routines
1413 * Leaf nodes in the sysctl tree will be represented by a single file
1414 * under /proc; non-leaf nodes will be represented by directories.
1416 * sysctl(2) can automatically manage read and write requests through
1417 * the sysctl table. The data and maxlen fields of the ctl_table
1418 * struct enable minimal validation of the values being written to be
1419 * performed, and the mode field allows minimal authentication.
1421 * More sophisticated management can be enabled by the provision of a
1422 * strategy routine with the table entry. This will be called before
1423 * any automatic read or write of the data is performed.
1425 * The strategy routine may return
1427 * < 0 - Error occurred (error is passed to user process)
1429 * 0 - OK - proceed with automatic read or write.
1431 * > 0 - OK - read or write has been done by the strategy routine, so
1432 * return immediately.
1434 * There must be a proc_handler routine for any terminal nodes
1435 * mirrored under /proc/sys (non-terminals are handled by a built-in
1436 * directory handler). Several default handlers are available to
1437 * cover common cases -
1439 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1440 * proc_dointvec_minmax(), proc_doulongvec_ms_jiffies_minmax(),
1441 * proc_doulongvec_minmax()
1443 * It is the handler's job to read the input buffer from user memory
1444 * and process it. The handler should return 0 on success.
1446 * This routine returns %NULL on a failure to register, and a pointer
1447 * to the table header on success.
1449 struct ctl_table_header *register_sysctl_table(cfs_sysctl_table_t * table,
1452 struct ctl_table_header *tmp;
1453 tmp = cfs_alloc(sizeof(struct ctl_table_header), 0);
1456 tmp->ctl_table = table;
1458 CFS_INIT_LIST_HEAD(&tmp->ctl_entry);
1460 cfs_list_add(&tmp->ctl_entry, &root_table_header.ctl_entry);
1462 cfs_list_add_tail(&tmp->ctl_entry, &root_table_header.ctl_entry);
1463 #ifdef CONFIG_PROC_FS
1464 register_proc_table(table, cfs_proc_sys);
1470 * unregister_sysctl_table - unregister a sysctl table heirarchy
1471 * @header: the header returned from register_sysctl_table
1473 * Unregisters the sysctl table and all children. proc entries may not
1474 * actually be removed until they are no longer used by anyone.
1476 void unregister_sysctl_table(struct ctl_table_header * header)
1478 cfs_list_del(&header->ctl_entry);
1479 #ifdef CONFIG_PROC_FS
1480 unregister_proc_table(header->ctl_table, cfs_proc_sys);
1486 int cfs_psdev_register(cfs_psdev_t * psdev)
1488 cfs_proc_entry_t * entry;
1490 entry = create_proc_entry (
1491 (char *)psdev->name,
1500 entry->flags |= CFS_PROC_FLAG_MISCDEV;
1502 entry->proc_fops = psdev->fops;
1503 entry->data = (void *)psdev;
1508 int cfs_psdev_deregister(cfs_psdev_t * psdev)
1510 cfs_proc_entry_t * entry;
1512 entry = search_proc_entry (
1513 (char *)psdev->name,
1519 ASSERT(entry->data == (void *)psdev);
1520 ASSERT(entry->flags & CFS_PROC_FLAG_MISCDEV);
1523 (char *)psdev->name,
1531 #define PSDEV_LNET (0x100)
1533 PSDEV_DEBUG = 1, /* control debugging */
1534 PSDEV_SUBSYSTEM_DEBUG, /* control debugging */
1535 PSDEV_PRINTK, /* force all messages to console */
1536 PSDEV_CONSOLE_RATELIMIT, /* rate limit console messages */
1537 PSDEV_DEBUG_PATH, /* crashdump log location */
1538 PSDEV_DEBUG_DUMP_PATH, /* crashdump tracelog location */
1539 PSDEV_LIBCFS_MEMUSED, /* bytes currently PORTAL_ALLOCated */
1542 static struct ctl_table lnet_table[] = {
1543 {PSDEV_DEBUG, "debug", &libcfs_debug, sizeof(int), 0644, NULL,
1545 {PSDEV_SUBSYSTEM_DEBUG, "subsystem_debug", &libcfs_subsystem_debug,
1546 sizeof(int), 0644, NULL, &proc_dointvec},
1547 {PSDEV_PRINTK, "printk", &libcfs_printk, sizeof(int), 0644, NULL,
1549 {PSDEV_CONSOLE_RATELIMIT, "console_ratelimit", &libcfs_console_ratelimit,
1550 sizeof(int), 0644, NULL, &proc_dointvec},
1552 {PSDEV_PORTALS_UPCALL, "upcall", portals_upcall,
1553 sizeof(portals_upcall), 0644, NULL, &proc_dostring,
1556 {PSDEV_LIBCFS_MEMUSED, "memused", (int *)&libcfs_kmemory.counter,
1557 sizeof(int), 0644, NULL, &proc_dointvec},
1561 static struct ctl_table top_table[2] = {
1562 {PSDEV_LNET, "lnet", NULL, 0, 0555, lnet_table},
1567 int trace_write_dump_kernel(struct file *file, const char *buffer,
1568 unsigned long count, void *data)
1570 int rc = cfs_trace_dump_debug_buffer_usrstr((void *)buffer, count);
1572 return (rc < 0) ? rc : count;
1575 int trace_write_daemon_file(struct file *file, const char *buffer,
1576 unsigned long count, void *data)
1578 int rc = cfs_trace_daemon_command_usrstr((void *)buffer, count);
1580 return (rc < 0) ? rc : count;
1583 int trace_read_daemon_file(char *page, char **start, off_t off, int count,
1584 int *eof, void *data)
1587 cfs_tracefile_read_lock();
1588 rc = cfs_trace_copyout_string(page, count, cfs_tracefile, "\n");
1589 cfs_tracefile_read_unlock();
1593 int trace_write_debug_mb(struct file *file, const char *buffer,
1594 unsigned long count, void *data)
1596 int rc = 0; /*trace_set_debug_mb_userstr((void *)buffer, count);*/
1598 return (rc < 0) ? rc : count;
1601 int trace_read_debug_mb(char *page, char **start, off_t off, int count,
1602 int *eof, void *data)
1606 snprintf(str, sizeof(str), "%d\n", cfs_trace_get_debug_mb());
1608 return cfs_trace_copyout_string(page, count, str, NULL);
1611 int insert_proc(void)
1613 cfs_proc_entry_t *ent;
1615 ent = create_proc_entry("sys/lnet/dump_kernel", 0, NULL);
1617 CERROR("couldn't register dump_kernel\n");
1620 ent->write_proc = trace_write_dump_kernel;
1622 ent = create_proc_entry("sys/lnet/daemon_file", 0, NULL);
1624 CERROR("couldn't register daemon_file\n");
1627 ent->write_proc = trace_write_daemon_file;
1628 ent->read_proc = trace_read_daemon_file;
1630 ent = create_proc_entry("sys/lnet/debug_mb", 0, NULL);
1632 CERROR("couldn't register debug_mb\n");
1635 ent->write_proc = trace_write_debug_mb;
1636 ent->read_proc = trace_read_debug_mb;
1641 void remove_proc(void)
1643 remove_proc_entry("sys/lnet/dump_kernel", NULL);
1644 remove_proc_entry("sys/lnet/daemon_file", NULL);
1645 remove_proc_entry("sys/lnet/debug_mb", NULL);
1650 * proc process routines of kernel space
1654 lustre_open_file(char * filename)
1657 cfs_file_t * fh = NULL;
1658 cfs_proc_entry_t * fp = NULL;
1660 fp = search_proc_entry(filename, cfs_proc_root);
1665 fh = cfs_alloc(sizeof(cfs_file_t), CFS_ALLOC_ZERO);
1670 fh->f_inode = cfs_alloc(sizeof(struct inode), CFS_ALLOC_ZERO);
1676 fh->f_inode->i_priv = (void *)fp;
1677 fh->f_op = fp->proc_fops;
1679 if (fh->f_op->open) {
1680 rc = (fh->f_op->open)(fh->f_inode, fh);
1686 cfs_free(fh->f_inode);
1695 lustre_close_file(cfs_file_t * fh)
1698 cfs_proc_entry_t * fp = NULL;
1700 fp = (cfs_proc_entry_t *) fh->f_inode->i_priv;
1701 if (fh->f_op->release) {
1702 rc = (fh->f_op->release)(fh->f_inode, fh);
1707 cfs_free(fh->f_inode);
1714 lustre_do_ioctl( cfs_file_t * fh,
1720 if (fh->f_op->ioctl) {
1721 rc = (fh->f_op->ioctl)(fh, cmd, arg);
1728 lustre_ioctl_file(cfs_file_t * fh, PCFS_PROC_IOCTL devctl)
1733 data = (ulong_ptr_t)devctl + sizeof(CFS_PROC_IOCTL);
1735 CLASSERT(sizeof(struct obd_ioctl_data) == 528);
1737 CLASSERT(sizeof(struct obd_ioctl_data) == 576);
1740 /* obd ioctl code */
1741 if (_IOC_TYPE(devctl->cmd) == 'f') {
1743 struct obd_ioctl_data * obd = (struct obd_ioctl_data *) data;
1745 if ( devctl->cmd != (ULONG)OBD_IOC_BRW_WRITE &&
1746 devctl->cmd != (ULONG)OBD_IOC_BRW_READ ) {
1748 unsigned long off = obd->ioc_len;
1750 if (obd->ioc_plen1) {
1751 obd->ioc_pbuf1 = (char *)(data + off);
1752 off += cfs_size_round(obd->ioc_plen1);
1754 obd->ioc_pbuf1 = NULL;
1757 if (obd->ioc_plen2) {
1758 obd->ioc_pbuf2 = (char *)(data + off);
1759 off += cfs_size_round(obd->ioc_plen2);
1761 obd->ioc_pbuf2 = NULL;
1766 rc = lustre_do_ioctl(fh, devctl->cmd, data);
1784 high = (off_t)(off >> 32);
1786 if (fh->f_op->read) {
1787 rc = (fh->f_op->read) (fh, buf, size, &off);
1791 fh->f_pos = off + rc;
1808 if (fh->f_op->write) {
1809 rc = (fh->f_op->write)(fh, buf, size, &off);
1821 * seq_open - initialize sequential file
1822 * @file: file we initialize
1823 * @op: method table describing the sequence
1825 * seq_open() sets @file, associating it with a sequence described
1826 * by @op. @op->start() sets the iterator up and returns the first
1827 * element of sequence. @op->stop() shuts it down. @op->next()
1828 * returns the next element of sequence. @op->show() prints element
1829 * into the buffer. In case of error ->start() and ->next() return
1830 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
1831 * returns 0 in case of success and negative number in case of error.
1833 int seq_open(struct file *file, const struct seq_operations *op)
1835 struct seq_file *p = file->private_data;
1838 p = kmalloc(sizeof(*p), GFP_KERNEL);
1841 file->private_data = p;
1843 memset(p, 0, sizeof(*p));
1844 cfs_mutex_init(&p->lock);
1848 * Wrappers around seq_open(e.g. swaps_open) need to be
1849 * aware of this. If they set f_version themselves, they
1850 * should call seq_open first and then set f_version.
1852 file->f_version = 0;
1854 /* SEQ files support lseek, but not pread/pwrite */
1855 file->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
1858 EXPORT_SYMBOL(seq_open);
1861 * seq_read - ->read() method for sequential files.
1862 * @file: the file to read from
1863 * @buf: the buffer to read to
1864 * @size: the maximum number of bytes to read
1865 * @ppos: the current position in the file
1867 * Ready-made ->f_op->read()
1869 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
1871 struct seq_file *m = (struct seq_file *)file->private_data;
1878 cfs_mutex_lock(&m->lock);
1880 * seq_file->op->..m_start/m_stop/m_next may do special actions
1881 * or optimisations based on the file->f_version, so we want to
1882 * pass the file->f_version to those methods.
1884 * seq_file->version is just copy of f_version, and seq_file
1885 * methods can treat it simply as file version.
1886 * It is copied in first and copied out after all operations.
1887 * It is convenient to have it as part of structure to avoid the
1888 * need of passing another argument to all the seq_file methods.
1890 m->version = file->f_version;
1891 /* grab buffer if we didn't have one */
1893 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
1897 /* if not empty - flush it first */
1899 n = min(m->count, size);
1900 err = cfs_copy_to_user(buf, m->buf + m->from, n);
1913 /* we need at least one record in buffer */
1916 p = m->op->start(m, &pos);
1918 if (!p || IS_ERR(p))
1920 err = m->op->show(m, p);
1923 if (m->count < m->size)
1927 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
1937 /* they want more? let's try to get some more */
1938 while (m->count < size) {
1939 size_t offs = m->count;
1941 p = m->op->next(m, p, &next);
1942 if (!p || IS_ERR(p)) {
1946 err = m->op->show(m, p);
1947 if (err || m->count == m->size) {
1954 n = min(m->count, size);
1955 err = cfs_copy_to_user(buf, m->buf, n);
1970 file->f_version = m->version;
1971 cfs_mutex_unlock(&m->lock);
1980 EXPORT_SYMBOL(seq_read);
1982 static int traverse(struct seq_file *m, loff_t offset)
1984 loff_t pos = 0, index;
1990 m->count = m->from = 0;
1996 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
2000 p = m->op->start(m, &index);
2005 error = m->op->show(m, p);
2008 if (m->count == m->size)
2010 if (pos + (loff_t)(m->count) > offset) {
2011 m->from = (size_t)(offset - pos);
2012 m->count -= m->from;
2018 if (pos == offset) {
2023 p = m->op->next(m, p, &index);
2031 m->buf = cfs_alloc(m->size <<= 1, GFP_KERNEL | CFS_ALLOC_ZERO);
2032 return !m->buf ? -ENOMEM : -EAGAIN;
2036 * seq_lseek - ->llseek() method for sequential files.
2037 * @file: the file in question
2038 * @offset: new position
2039 * @origin: 0 for absolute, 1 for relative position
2041 * Ready-made ->f_op->llseek()
2043 loff_t seq_lseek(struct file *file, loff_t offset, int origin)
2045 struct seq_file *m = (struct seq_file *)file->private_data;
2046 long long retval = -EINVAL;
2048 cfs_mutex_lock(&m->lock);
2049 m->version = file->f_version;
2052 offset += file->f_pos;
2057 if (offset != file->f_pos) {
2058 while ((retval=traverse(m, offset)) == -EAGAIN)
2061 /* with extreme prejudice... */
2067 retval = file->f_pos = offset;
2071 file->f_version = m->version;
2072 cfs_mutex_unlock(&m->lock);
2075 EXPORT_SYMBOL(seq_lseek);
2078 * seq_release - free the structures associated with sequential file.
2079 * @file: file in question
2080 * @inode: file->f_path.dentry->d_inode
2082 * Frees the structures associated with sequential file; can be used
2083 * as ->f_op->release() if you don't have private data to destroy.
2085 int seq_release(struct inode *inode, struct file *file)
2087 struct seq_file *m = (struct seq_file *)file->private_data;
2095 EXPORT_SYMBOL(seq_release);
2098 * seq_escape - print string into buffer, escaping some characters
2101 * @esc: set of characters that need escaping
2103 * Puts string into buffer, replacing each occurrence of character from
2104 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
2107 int seq_escape(struct seq_file *m, const char *s, const char *esc)
2109 char *end = m->buf + m->size;
2113 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
2114 if (!strchr(esc, c)) {
2120 *p++ = '0' + ((c & 0300) >> 6);
2121 *p++ = '0' + ((c & 070) >> 3);
2122 *p++ = '0' + (c & 07);
2128 m->count = p - m->buf;
2131 EXPORT_SYMBOL(seq_escape);
2133 int seq_printf(struct seq_file *m, const char *f, ...)
2138 if (m->count < m->size) {
2140 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
2142 if (m->count + len < m->size) {
2150 EXPORT_SYMBOL(seq_printf);
2152 char *d_path(struct path *p, char *buffer, int buflen)
2154 cfs_enter_debugger();
2155 return ERR_PTR(-ENAMETOOLONG);
2158 int seq_path(struct seq_file *m, struct path *path, char *esc)
2160 if (m->count < m->size) {
2161 char *s = m->buf + m->count;
2162 char *p = d_path(path, s, m->size - m->count);
2167 p = m->buf + m->count;
2168 m->count = s - m->buf;
2169 return (int)(s - p);
2170 } else if (!strchr(esc, c)) {
2172 } else if (s + 4 > p) {
2176 *s++ = '0' + ((c & 0300) >> 6);
2177 *s++ = '0' + ((c & 070) >> 3);
2178 *s++ = '0' + (c & 07);
2186 EXPORT_SYMBOL(seq_path);
2188 static void *single_start(struct seq_file *p, loff_t *pos)
2190 return (void *) (INT_PTR) (*pos == 0);
2193 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
2199 static void single_stop(struct seq_file *p, void *v)
2203 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
2206 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
2210 op->start = single_start;
2211 op->next = single_next;
2212 op->stop = single_stop;
2214 res = seq_open(file, op);
2216 ((struct seq_file *)file->private_data)->private = data;
2222 EXPORT_SYMBOL(single_open);
2224 int single_release(struct inode *inode, struct file *file)
2226 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
2227 int res = seq_release(inode, file);
2228 cfs_free((void *)op);
2231 EXPORT_SYMBOL(single_release);
2233 int seq_release_private(struct inode *inode, struct file *file)
2235 struct seq_file *seq = file->private_data;
2237 cfs_free(seq->private);
2238 seq->private = NULL;
2239 return seq_release(inode, file);
2241 EXPORT_SYMBOL(seq_release_private);
2243 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
2248 struct seq_file *seq;
2250 private = cfs_alloc(psize, GFP_KERNEL | CFS_ALLOC_ZERO);
2251 if (private == NULL)
2254 rc = seq_open(f, ops);
2258 seq = f->private_data;
2259 seq->private = private;
2267 EXPORT_SYMBOL(__seq_open_private);
2269 int seq_open_private(struct file *filp, const struct seq_operations *ops,
2272 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
2274 EXPORT_SYMBOL(seq_open_private);
2276 int seq_putc(struct seq_file *m, char c)
2278 if (m->count < m->size) {
2279 m->buf[m->count++] = c;
2284 EXPORT_SYMBOL(seq_putc);
2286 int seq_puts(struct seq_file *m, const char *s)
2288 int len = strlen(s);
2289 if (m->count + len < m->size) {
2290 memcpy(m->buf + m->count, s, len);
2297 EXPORT_SYMBOL(seq_puts);
2299 cfs_list_t *seq_list_start(cfs_list_t *head, loff_t pos)
2303 cfs_list_for_each(lh, head)
2310 EXPORT_SYMBOL(seq_list_start);
2312 cfs_list_t *seq_list_start_head(cfs_list_t *head,
2318 return seq_list_start(head, pos - 1);
2321 EXPORT_SYMBOL(seq_list_start_head);
2323 cfs_list_t *seq_list_next(void *v, cfs_list_t *head,
2328 lh = ((cfs_list_t *)v)->next;
2330 return lh == head ? NULL : lh;
2333 EXPORT_SYMBOL(seq_list_next);
2335 struct proc_dir_entry *PDE(const struct inode *inode)
2337 return (struct proc_dir_entry *)inode->i_priv;
2341 #endif /* __KERNEL__ */