tree-wide: convert open calls to remove spaces to skip_spaces() lib function
[safe/jmp/linux-2.6] / arch / um / drivers / mconsole_kern.c
index be61012..5106924 100644 (file)
@@ -1,44 +1,40 @@
 /*
  * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org)
- * Copyright (C) 2001 - 2003 Jeff Dike (jdike@addtoit.com)
+ * Copyright (C) 2001 - 2008 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  * Licensed under the GPL
  */
 
-#include "linux/kernel.h"
-#include "linux/slab.h"
-#include "linux/init.h"
-#include "linux/notifier.h"
-#include "linux/reboot.h"
-#include "linux/utsname.h"
-#include "linux/ctype.h"
-#include "linux/interrupt.h"
-#include "linux/sysrq.h"
-#include "linux/workqueue.h"
-#include "linux/module.h"
-#include "linux/file.h"
-#include "linux/fs.h"
-#include "linux/namei.h"
-#include "linux/proc_fs.h"
-#include "linux/syscalls.h"
-#include "linux/console.h"
-#include "asm/irq.h"
-#include "asm/uaccess.h"
-#include "user_util.h"
+#include <linux/console.h>
+#include <linux/ctype.h>
+#include <linux/string.h>
+#include <linux/interrupt.h>
+#include <linux/list.h>
+#include <linux/mm.h>
+#include <linux/module.h>
+#include <linux/notifier.h>
+#include <linux/reboot.h>
+#include <linux/proc_fs.h>
+#include <linux/slab.h>
+#include <linux/syscalls.h>
+#include <linux/utsname.h>
+#include <linux/socket.h>
+#include <linux/un.h>
+#include <linux/workqueue.h>
+#include <linux/mutex.h>
+#include <asm/uaccess.h>
+
+#include "init.h"
+#include "irq_kern.h"
+#include "irq_user.h"
 #include "kern_util.h"
-#include "kern.h"
 #include "mconsole.h"
 #include "mconsole_kern.h"
-#include "irq_user.h"
-#include "init.h"
 #include "os.h"
-#include "umid.h"
-#include "irq_kern.h"
-#include "choose-mode.h"
 
 static int do_unlink_socket(struct notifier_block *notifier,
                            unsigned long what, void *data)
 {
-       return(mconsole_unlink_socket());
+       return mconsole_unlink_socket();
 }
 
 
@@ -54,15 +50,14 @@ static struct notifier_block reboot_notifier = {
 
 static LIST_HEAD(mc_requests);
 
-static void mc_work_proc(void *unused)
+static void mc_work_proc(struct work_struct *unused)
 {
        struct mconsole_entry *req;
        unsigned long flags;
 
-       while(!list_empty(&mc_requests)){
-               local_save_flags(flags);
-               req = list_entry(mc_requests.next, struct mconsole_entry,
-                                list);
+       while (!list_empty(&mc_requests)) {
+               local_irq_save(flags);
+               req = list_entry(mc_requests.next, struct mconsole_entry, list);
                list_del(&req->list);
                local_irq_restore(flags);
                req->request.cmd->handler(&req->request);
@@ -70,43 +65,43 @@ static void mc_work_proc(void *unused)
        }
 }
 
-static DECLARE_WORK(mconsole_work, mc_work_proc, NULL);
+static DECLARE_WORK(mconsole_work, mc_work_proc);
 
-static irqreturn_t mconsole_interrupt(int irq, void *dev_id,
-                                     struct pt_regs *regs)
+static irqreturn_t mconsole_interrupt(int irq, void *dev_id)
 {
        /* long to avoid size mismatch warnings from gcc */
        long fd;
        struct mconsole_entry *new;
-       struct mc_request req;
+       static struct mc_request req;   /* that's OK */
 
        fd = (long) dev_id;
-       while (mconsole_get_request(fd, &req)){
-               if(req.cmd->context == MCONSOLE_INTR)
+       while (mconsole_get_request(fd, &req)) {
+               if (req.cmd->context == MCONSOLE_INTR)
                        (*req.cmd->handler)(&req);
                else {
-                       new = kmalloc(sizeof(*new), GFP_ATOMIC);
-                       if(new == NULL)
+                       new = kmalloc(sizeof(*new), GFP_NOWAIT);
+                       if (new == NULL)
                                mconsole_reply(&req, "Out of memory", 1, 0);
                        else {
                                new->request = req;
+                               new->request.regs = get_irq_regs()->regs;
                                list_add(&new->list, &mc_requests);
                        }
                }
        }
-       if(!list_empty(&mc_requests))
+       if (!list_empty(&mc_requests))
                schedule_work(&mconsole_work);
        reactivate_fd(fd, MCONSOLE_IRQ);
-       return(IRQ_HANDLED);
+       return IRQ_HANDLED;
 }
 
 void mconsole_version(struct mc_request *req)
 {
        char version[256];
 
-       sprintf(version, "%s %s %s %s %s", system_utsname.sysname,
-               system_utsname.nodename, system_utsname.release,
-               system_utsname.version, system_utsname.machine);
+       sprintf(version, "%s %s %s %s %s", utsname()->sysname,
+               utsname()->nodename, utsname()->release, utsname()->version,
+               utsname()->machine);
        mconsole_reply(req, version, 0, 0);
 }
 
@@ -118,7 +113,7 @@ void mconsole_log(struct mc_request *req)
        ptr += strlen("log ");
 
        len = req->len - (ptr - req->request.data);
-       printk("%.*s", len, ptr);
+       printk(KERN_WARNING "%.*s", len, ptr);
        mconsole_reply(req, "", 0, 0);
 }
 
@@ -137,24 +132,24 @@ void mconsole_proc(struct mc_request *req)
        char *ptr = req->request.data, *buf;
 
        ptr += strlen("proc");
-       while(isspace(*ptr)) ptr++;
+       ptr = skip_spaces(ptr);
 
        proc = get_fs_type("proc");
-       if(proc == NULL){
+       if (proc == NULL) {
                mconsole_reply(req, "procfs not registered", 1, 0);
                goto out;
        }
 
        super = (*proc->get_sb)(proc, 0, NULL, NULL);
        put_filesystem(proc);
-       if(super == NULL){
+       if (super == NULL) {
                mconsole_reply(req, "Failed to get procfs superblock", 1, 0);
                goto out;
        }
        up_write(&super->s_umount);
 
-       nd.dentry = super->s_root;
-       nd.mnt = NULL;
+       nd.path.dentry = super->s_root;
+       nd.path.mnt = NULL;
        nd.flags = O_RDONLY + 1;
        nd.last_type = LAST_ROOT;
 
@@ -162,29 +157,30 @@ void mconsole_proc(struct mc_request *req)
         * if commenting out these two calls + the below read cycle. To
         * make UML crash again, it was enough to readd either one.*/
        err = link_path_walk(ptr, &nd);
-       if(err){
+       if (err) {
                mconsole_reply(req, "Failed to look up file", 1, 0);
                goto out_kill;
        }
 
-       file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
-       if(IS_ERR(file)){
+       file = dentry_open(nd.path.dentry, nd.path.mnt, O_RDONLY,
+                          current_cred());
+       if (IS_ERR(file)) {
                mconsole_reply(req, "Failed to open file", 1, 0);
                goto out_kill;
        }
        /*END*/
 
        buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
-       if(buf == NULL){
+       if (buf == NULL) {
                mconsole_reply(req, "Failed to allocate buffer", 1, 0);
                goto out_fput;
        }
 
-       if((file->f_op != NULL) && (file->f_op->read != NULL)){
+       if ((file->f_op != NULL) && (file->f_op->read != NULL)) {
                do {
                        n = (*file->f_op->read)(file, buf, PAGE_SIZE - 1,
                                                &file->f_pos);
-                       if(n >= 0){
+                       if (n >= 0) {
                                buf[n] = '\0';
                                mconsole_reply(req, buf, 0, (n > 0));
                        }
@@ -193,7 +189,7 @@ void mconsole_proc(struct mc_request *req)
                                               1, 0);
                                goto out_free;
                        }
-               } while(n > 0);
+               } while (n > 0);
        }
        else mconsole_reply(req, "", 0, 0);
 
@@ -217,18 +213,18 @@ void mconsole_proc(struct mc_request *req)
        char *ptr = req->request.data;
 
        ptr += strlen("proc");
-       while(isspace(*ptr)) ptr++;
+       ptr = skip_spaces(ptr);
        snprintf(path, sizeof(path), "/proc/%s", ptr);
 
        fd = sys_open(path, 0, 0);
        if (fd < 0) {
                mconsole_reply(req, "Failed to open file", 1, 0);
-               printk("open %s: %d\n",path,fd);
+               printk(KERN_ERR "open %s: %d\n",path,fd);
                goto out;
        }
 
        buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
-       if(buf == NULL){
+       if (buf == NULL) {
                mconsole_reply(req, "Failed to allocate buffer", 1, 0);
                goto out_close;
        }
@@ -239,7 +235,7 @@ void mconsole_proc(struct mc_request *req)
                        mconsole_reply(req, "Read of file failed", 1, 0);
                        goto out_free;
                }
-               /*Begin the file content on his own line.*/
+               /* Begin the file content on his own line. */
                if (first_chunk) {
                        mconsole_reply(req, "\n", 0, 1);
                        first_chunk = 0;
@@ -273,7 +269,7 @@ void mconsole_proc(struct mc_request *req)
     config <dev> - Query the configuration of a device \n\
     remove <dev> - Remove a device from UML \n\
     sysrq <letter> - Performs the SysRq action controlled by the letter \n\
-    cad - invoke the Ctl-Alt-Del handler \n\
+    cad - invoke the Ctrl-Alt-Del handler \n\
     stop - pause the UML; it will do nothing until it receives a 'go' \n\
     go - continue the UML after a 'stop' \n\
     log <string> - make UML enter <string> into the kernel log\n\
@@ -298,8 +294,6 @@ void mconsole_reboot(struct mc_request *req)
        machine_restart(NULL);
 }
 
-extern void ctrl_alt_del(void);
-
 void mconsole_cad(struct mc_request *req)
 {
        mconsole_reply(req, "", 0, 0);
@@ -315,9 +309,23 @@ void mconsole_stop(struct mc_request *req)
 {
        deactivate_fd(req->originating_fd, MCONSOLE_IRQ);
        os_set_fd_block(req->originating_fd, 1);
-       mconsole_reply(req, "", 0, 0);
-       while(mconsole_get_request(req->originating_fd, req)){
-               if(req->cmd->handler == mconsole_go) break;
+       mconsole_reply(req, "stopped", 0, 0);
+       for (;;) {
+               if (!mconsole_get_request(req->originating_fd, req))
+                       continue;
+               if (req->cmd->handler == mconsole_go)
+                       break;
+               if (req->cmd->handler == mconsole_stop) {
+                       mconsole_reply(req, "Already stopped", 1, 0);
+                       continue;
+               }
+               if (req->cmd->handler == mconsole_sysrq) {
+                       struct pt_regs *old_regs;
+                       old_regs = set_irq_regs((struct pt_regs *)&req->regs);
+                       mconsole_sysrq(req);
+                       set_irq_regs(old_regs);
+                       continue;
+               }
                (*req->cmd->handler)(req);
        }
        os_set_fd_block(req->originating_fd, 0);
@@ -325,13 +333,15 @@ void mconsole_stop(struct mc_request *req)
        mconsole_reply(req, "", 0, 0);
 }
 
-/* This list is populated by __initcall routines. */
-
-LIST_HEAD(mconsole_devices);
+static DEFINE_SPINLOCK(mc_devices_lock);
+static LIST_HEAD(mconsole_devices);
 
 void mconsole_register_dev(struct mc_device *new)
 {
+       spin_lock(&mc_devices_lock);
+       BUG_ON(!list_empty(&new->list));
        list_add(&new->list, &mconsole_devices);
+       spin_unlock(&mc_devices_lock);
 }
 
 static struct mc_device *mconsole_find_dev(char *name)
@@ -339,14 +349,165 @@ static struct mc_device *mconsole_find_dev(char *name)
        struct list_head *ele;
        struct mc_device *dev;
 
-       list_for_each(ele, &mconsole_devices){
+       list_for_each(ele, &mconsole_devices) {
                dev = list_entry(ele, struct mc_device, list);
-               if(!strncmp(name, dev->name, strlen(dev->name)))
-                       return(dev);
+               if (!strncmp(name, dev->name, strlen(dev->name)))
+                       return dev;
+       }
+       return NULL;
+}
+
+#define UNPLUGGED_PER_PAGE \
+       ((PAGE_SIZE - sizeof(struct list_head)) / sizeof(unsigned long))
+
+struct unplugged_pages {
+       struct list_head list;
+       void *pages[UNPLUGGED_PER_PAGE];
+};
+
+static DEFINE_MUTEX(plug_mem_mutex);
+static unsigned long long unplugged_pages_count = 0;
+static LIST_HEAD(unplugged_pages);
+static int unplug_index = UNPLUGGED_PER_PAGE;
+
+static int mem_config(char *str, char **error_out)
+{
+       unsigned long long diff;
+       int err = -EINVAL, i, add;
+       char *ret;
+
+       if (str[0] != '=') {
+               *error_out = "Expected '=' after 'mem'";
+               goto out;
+       }
+
+       str++;
+       if (str[0] == '-')
+               add = 0;
+       else if (str[0] == '+') {
+               add = 1;
+       }
+       else {
+               *error_out = "Expected increment to start with '-' or '+'";
+               goto out;
        }
-       return(NULL);
+
+       str++;
+       diff = memparse(str, &ret);
+       if (*ret != '\0') {
+               *error_out = "Failed to parse memory increment";
+               goto out;
+       }
+
+       diff /= PAGE_SIZE;
+
+       mutex_lock(&plug_mem_mutex);
+       for (i = 0; i < diff; i++) {
+               struct unplugged_pages *unplugged;
+               void *addr;
+
+               if (add) {
+                       if (list_empty(&unplugged_pages))
+                               break;
+
+                       unplugged = list_entry(unplugged_pages.next,
+                                              struct unplugged_pages, list);
+                       if (unplug_index > 0)
+                               addr = unplugged->pages[--unplug_index];
+                       else {
+                               list_del(&unplugged->list);
+                               addr = unplugged;
+                               unplug_index = UNPLUGGED_PER_PAGE;
+                       }
+
+                       free_page((unsigned long) addr);
+                       unplugged_pages_count--;
+               }
+               else {
+                       struct page *page;
+
+                       page = alloc_page(GFP_ATOMIC);
+                       if (page == NULL)
+                               break;
+
+                       unplugged = page_address(page);
+                       if (unplug_index == UNPLUGGED_PER_PAGE) {
+                               list_add(&unplugged->list, &unplugged_pages);
+                               unplug_index = 0;
+                       }
+                       else {
+                               struct list_head *entry = unplugged_pages.next;
+                               addr = unplugged;
+
+                               unplugged = list_entry(entry,
+                                                      struct unplugged_pages,
+                                                      list);
+                               err = os_drop_memory(addr, PAGE_SIZE);
+                               if (err) {
+                                       printk(KERN_ERR "Failed to release "
+                                              "memory - errno = %d\n", err);
+                                       *error_out = "Failed to release memory";
+                                       goto out_unlock;
+                               }
+                               unplugged->pages[unplug_index++] = addr;
+                       }
+
+                       unplugged_pages_count++;
+               }
+       }
+
+       err = 0;
+out_unlock:
+       mutex_unlock(&plug_mem_mutex);
+out:
+       return err;
 }
 
+static int mem_get_config(char *name, char *str, int size, char **error_out)
+{
+       char buf[sizeof("18446744073709551615")];
+       int len = 0;
+
+       sprintf(buf, "%ld", uml_physmem);
+       CONFIG_CHUNK(str, size, len, buf, 1);
+
+       return len;
+}
+
+static int mem_id(char **str, int *start_out, int *end_out)
+{
+       *start_out = 0;
+       *end_out = 0;
+
+       return 0;
+}
+
+static int mem_remove(int n, char **error_out)
+{
+       *error_out = "Memory doesn't support the remove operation";
+       return -EBUSY;
+}
+
+static struct mc_device mem_mc = {
+       .list           = LIST_HEAD_INIT(mem_mc.list),
+       .name           = "mem",
+       .config         = mem_config,
+       .get_config     = mem_get_config,
+       .id             = mem_id,
+       .remove         = mem_remove,
+};
+
+static int __init mem_mc_init(void)
+{
+       if (can_drop_memory())
+               mconsole_register_dev(&mem_mc);
+       else printk(KERN_ERR "Can't release memory to the host - memory "
+                   "hotplug won't be supported\n");
+       return 0;
+}
+
+__initcall(mem_mc_init);
+
 #define CONFIG_BUF_SIZE 64
 
 static void mconsole_get_config(int (*get_config)(char *, char *, int,
@@ -356,64 +517,64 @@ static void mconsole_get_config(int (*get_config)(char *, char *, int,
        char default_buf[CONFIG_BUF_SIZE], *error, *buf;
        int n, size;
 
-       if(get_config == NULL){
+       if (get_config == NULL) {
                mconsole_reply(req, "No get_config routine defined", 1, 0);
                return;
        }
 
        error = NULL;
-       size = sizeof(default_buf)/sizeof(default_buf[0]);
+       size = ARRAY_SIZE(default_buf);
        buf = default_buf;
 
-       while(1){
+       while (1) {
                n = (*get_config)(name, buf, size, &error);
-               if(error != NULL){
+               if (error != NULL) {
                        mconsole_reply(req, error, 1, 0);
                        goto out;
                }
 
-               if(n <= size){
+               if (n <= size) {
                        mconsole_reply(req, buf, 0, 0);
                        goto out;
                }
 
-               if(buf != default_buf)
+               if (buf != default_buf)
                        kfree(buf);
 
                size = n;
                buf = kmalloc(size, GFP_KERNEL);
-               if(buf == NULL){
+               if (buf == NULL) {
                        mconsole_reply(req, "Failed to allocate buffer", 1, 0);
                        return;
                }
        }
  out:
-       if(buf != default_buf)
+       if (buf != default_buf)
                kfree(buf);
 }
 
 void mconsole_config(struct mc_request *req)
 {
        struct mc_device *dev;
-       char *ptr = req->request.data, *name;
+       char *ptr = req->request.data, *name, *error_string = "";
        int err;
 
        ptr += strlen("config");
-       while(isspace(*ptr)) ptr++;
+       ptr = skip_spaces(ptr);
        dev = mconsole_find_dev(ptr);
-       if(dev == NULL){
+       if (dev == NULL) {
                mconsole_reply(req, "Bad configuration option", 1, 0);
                return;
        }
 
        name = &ptr[strlen(dev->name)];
        ptr = name;
-       while((*ptr != '=') && (*ptr != '\0'))
+       while ((*ptr != '=') && (*ptr != '\0'))
                ptr++;
 
-       if(*ptr == '='){
-               err = (*dev->config)(name);
-               mconsole_reply(req, "", err, 0);
+       if (*ptr == '=') {
+               err = (*dev->config)(name, &error_string);
+               mconsole_reply(req, error_string, err, 0);
        }
        else mconsole_get_config(dev->get_config, req, name);
 }
@@ -426,9 +587,9 @@ void mconsole_remove(struct mc_request *req)
        int err, start, end, n;
 
        ptr += strlen("remove");
-       while(isspace(*ptr)) ptr++;
+       ptr = skip_spaces(ptr);
        dev = mconsole_find_dev(ptr);
-       if(dev == NULL){
+       if (dev == NULL) {
                mconsole_reply(req, "Bad remove option", 1, 0);
                return;
        }
@@ -437,24 +598,30 @@ void mconsole_remove(struct mc_request *req)
 
        err = 1;
        n = (*dev->id)(&ptr, &start, &end);
-       if(n < 0){
+       if (n < 0) {
                err_msg = "Couldn't parse device number";
                goto out;
        }
-       else if((n < start) || (n > end)){
+       else if ((n < start) || (n > end)) {
                sprintf(error, "Invalid device number - must be between "
                        "%d and %d", start, end);
                err_msg = error;
                goto out;
        }
 
-       err = (*dev->remove)(n);
-       switch(err){
+       err_msg = NULL;
+       err = (*dev->remove)(n, &err_msg);
+       switch(err) {
+       case 0:
+               err_msg = "";
+               break;
        case -ENODEV:
-               err_msg = "Device doesn't exist";
+               if (err_msg == NULL)
+                       err_msg = "Device doesn't exist";
                break;
        case -EBUSY:
-               err_msg = "Device is currently open";
+               if (err_msg == NULL)
+                       err_msg = "Device is currently open";
                break;
        default:
                break;
@@ -463,44 +630,42 @@ out:
        mconsole_reply(req, err_msg, err, 0);
 }
 
-static DEFINE_SPINLOCK(console_lock);
+struct mconsole_output {
+       struct list_head list;
+       struct mc_request *req;
+};
+
+static DEFINE_SPINLOCK(client_lock);
 static LIST_HEAD(clients);
 static char console_buf[MCONSOLE_MAX_DATA];
-static int console_index = 0;
 
 static void console_write(struct console *console, const char *string,
-                         unsigned len)
+                         unsigned int len)
 {
        struct list_head *ele;
        int n;
 
-       if(list_empty(&clients))
+       if (list_empty(&clients))
                return;
 
-       while(1){
-               n = min(len, ARRAY_SIZE(console_buf) - console_index);
-               strncpy(&console_buf[console_index], string, n);
-               console_index += n;
+       while (len > 0) {
+               n = min((size_t) len, ARRAY_SIZE(console_buf));
+               strncpy(console_buf, string, n);
                string += n;
                len -= n;
-               if(len == 0)
-                       return;
 
-               list_for_each(ele, &clients){
-                       struct mconsole_entry *entry;
+               list_for_each(ele, &clients) {
+                       struct mconsole_output *entry;
 
-                       entry = list_entry(ele, struct mconsole_entry, list);
-                       mconsole_reply_len(&entry->request, console_buf,
-                                          console_index, 0, 1);
+                       entry = list_entry(ele, struct mconsole_output, list);
+                       mconsole_reply_len(entry->req, console_buf, n, 0, 1);
                }
-
-               console_index = 0;
        }
 }
 
 static struct console mc_console = { .name     = "mc",
                                     .write     = console_write,
-                                    .flags     = CON_PRINTBUFFER | CON_ENABLED,
+                                    .flags     = CON_ENABLED,
                                     .index     = -1 };
 
 static int mc_add_console(void)
@@ -514,29 +679,31 @@ late_initcall(mc_add_console);
 static void with_console(struct mc_request *req, void (*proc)(void *),
                         void *arg)
 {
-       struct mconsole_entry entry;
+       struct mconsole_output entry;
        unsigned long flags;
 
-       INIT_LIST_HEAD(&entry.list);
-       entry.request = *req;
+       entry.req = req;
+       spin_lock_irqsave(&client_lock, flags);
        list_add(&entry.list, &clients);
-       spin_lock_irqsave(&console_lock, flags);
+       spin_unlock_irqrestore(&client_lock, flags);
 
        (*proc)(arg);
 
-       mconsole_reply_len(req, console_buf, console_index, 0, 0);
-       console_index = 0;
+       mconsole_reply_len(req, "", 0, 0, 0);
 
-       spin_unlock_irqrestore(&console_lock, flags);
+       spin_lock_irqsave(&client_lock, flags);
        list_del(&entry.list);
+       spin_unlock_irqrestore(&client_lock, flags);
 }
 
 #ifdef CONFIG_MAGIC_SYSRQ
+
+#include <linux/sysrq.h>
+
 static void sysrq_proc(void *arg)
 {
        char *op = arg;
-
-       handle_sysrq(*op, &current->thread.regs, NULL);
+       handle_sysrq(*op, NULL);
 }
 
 void mconsole_sysrq(struct mc_request *req)
@@ -544,12 +711,13 @@ void mconsole_sysrq(struct mc_request *req)
        char *ptr = req->request.data;
 
        ptr += strlen("sysrq");
-       while(isspace(*ptr)) ptr++;
+       ptr = skip_spaces(ptr);
 
-       /* With 'b', the system will shut down without a chance to reply,
+       /*
+        * With 'b', the system will shut down without a chance to reply,
         * so in this case, we reply first.
         */
-       if(*ptr == 'b')
+       if (*ptr == 'b')
                mconsole_reply(req, "", 0, 0);
 
        with_console(req, sysrq_proc, ptr);
@@ -569,87 +737,82 @@ static void stack_proc(void *arg)
        switch_to(from, to, from);
 }
 
-/* Mconsole stack trace
+/*
+ * Mconsole stack trace
  *  Added by Allan Graves, Jeff Dike
  *  Dumps a stacks registers to the linux console.
  *  Usage stack <pid>.
  */
-void do_stack(struct mc_request *req)
+void mconsole_stack(struct mc_request *req)
 {
        char *ptr = req->request.data;
        int pid_requested= -1;
-       struct task_struct *from = NULL;
        struct task_struct *to = NULL;
 
-       /* Would be nice:
+       /*
+        * Would be nice:
         * 1) Send showregs output to mconsole.
         * 2) Add a way to stack dump all pids.
         */
 
        ptr += strlen("stack");
-       while(isspace(*ptr)) ptr++;
+       ptr = skip_spaces(ptr);
 
-       /* Should really check for multiple pids or reject bad args here */
+       /*
+        * Should really check for multiple pids or reject bad args here
+        */
        /* What do the arguments in mconsole_reply mean? */
-       if(sscanf(ptr, "%d", &pid_requested) == 0){
+       if (sscanf(ptr, "%d", &pid_requested) == 0) {
                mconsole_reply(req, "Please specify a pid", 1, 0);
                return;
        }
 
-       from = current;
-
-       to = find_task_by_pid(pid_requested);
-       if((to == NULL) || (pid_requested == 0)) {
+       to = find_task_by_pid_ns(pid_requested, &init_pid_ns);
+       if ((to == NULL) || (pid_requested == 0)) {
                mconsole_reply(req, "Couldn't find that pid", 1, 0);
                return;
        }
        with_console(req, stack_proc, to);
 }
 
-void mconsole_stack(struct mc_request *req)
-{
-       /* This command doesn't work in TT mode, so let's check and then
-        * get out of here
-        */
-       CHOOSE_MODE(mconsole_reply(req, "Sorry, this doesn't work in TT mode",
-                                  1, 0),
-                   do_stack(req));
-}
-
-/* Changed by mconsole_setup, which is __setup, and called before SMP is
+/*
+ * Changed by mconsole_setup, which is __setup, and called before SMP is
  * active.
  */
 static char *notify_socket = NULL;
 
-static int mconsole_init(void)
+static int __init mconsole_init(void)
 {
        /* long to avoid size mismatch warnings from gcc */
        long sock;
        int err;
-       char file[256];
+       char file[UNIX_PATH_MAX];
 
-       if(umid_file_name("mconsole", file, sizeof(file))) return(-1);
+       if (umid_file_name("mconsole", file, sizeof(file)))
+               return -1;
        snprintf(mconsole_socket_name, sizeof(file), "%s", file);
 
        sock = os_create_unix_socket(file, sizeof(file), 1);
-       if (sock < 0){
-               printk("Failed to initialize management console\n");
-               return(1);
+       if (sock < 0) {
+               printk(KERN_ERR "Failed to initialize management console\n");
+               return 1;
        }
+       if (os_set_fd_block(sock, 0))
+               goto out;
 
        register_reboot_notifier(&reboot_notifier);
 
        err = um_request_irq(MCONSOLE_IRQ, sock, IRQ_READ, mconsole_interrupt,
-                            SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM,
+                            IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
                             "mconsole", (void *)sock);
-       if (err){
-               printk("Failed to get IRQ for management console\n");
-               return(1);
+       if (err) {
+               printk(KERN_ERR "Failed to get IRQ for management console\n");
+               goto out;
        }
 
-       if(notify_socket != NULL){
+       if (notify_socket != NULL) {
                notify_socket = kstrdup(notify_socket, GFP_KERNEL);
-               if(notify_socket != NULL)
+               if (notify_socket != NULL)
                        mconsole_notify(notify_socket, MCONSOLE_SOCKET,
                                        mconsole_socket_name,
                                        strlen(mconsole_socket_name) + 1);
@@ -657,23 +820,27 @@ static int mconsole_init(void)
                            "string\n");
        }
 
-       printk("mconsole (version %d) initialized on %s\n",
+       printk(KERN_INFO "mconsole (version %d) initialized on %s\n",
               MCONSOLE_VERSION, mconsole_socket_name);
-       return(0);
+       return 0;
+
+ out:
+       os_close_file(sock);
+       return 1;
 }
 
 __initcall(mconsole_init);
 
-static int write_proc_mconsole(struct file *file, const char __user *buffer,
-                              unsigned long count, void *data)
+static ssize_t mconsole_proc_write(struct file *file,
+               const char __user *buffer, size_t count, loff_t *pos)
 {
        char *buf;
 
        buf = kmalloc(count + 1, GFP_KERNEL);
-       if(buf == NULL)
-               return(-ENOMEM);
+       if (buf == NULL)
+               return -ENOMEM;
 
-       if(copy_from_user(buf, buffer, count)){
+       if (copy_from_user(buf, buffer, count)) {
                count = -EFAULT;
                goto out;
        }
@@ -683,24 +850,28 @@ static int write_proc_mconsole(struct file *file, const char __user *buffer,
        mconsole_notify(notify_socket, MCONSOLE_USER_NOTIFY, buf, count);
  out:
        kfree(buf);
-       return(count);
+       return count;
 }
 
+static const struct file_operations mconsole_proc_fops = {
+       .owner          = THIS_MODULE,
+       .write          = mconsole_proc_write,
+};
+
 static int create_proc_mconsole(void)
 {
        struct proc_dir_entry *ent;
 
-       if(notify_socket == NULL) return(0);
+       if (notify_socket == NULL)
+               return 0;
 
-       ent = create_proc_entry("mconsole", S_IFREG | 0200, NULL);
-       if(ent == NULL){
-               printk(KERN_INFO "create_proc_mconsole : create_proc_entry failed\n");
-               return(0);
+       ent = proc_create("mconsole", 0200, NULL, &mconsole_proc_fops);
+       if (ent == NULL) {
+               printk(KERN_INFO "create_proc_mconsole : create_proc_entry "
+                      "failed\n");
+               return 0;
        }
-
-       ent->read_proc = NULL;
-       ent->write_proc = write_proc_mconsole;
-       return(0);
+       return 0;
 }
 
 static DEFINE_SPINLOCK(notify_spinlock);
@@ -717,19 +888,19 @@ void unlock_notify(void)
 
 __initcall(create_proc_mconsole);
 
-#define NOTIFY "=notify:"
+#define NOTIFY "notify:"
 
 static int mconsole_setup(char *str)
 {
-       if(!strncmp(str, NOTIFY, strlen(NOTIFY))){
+       if (!strncmp(str, NOTIFY, strlen(NOTIFY))) {
                str += strlen(NOTIFY);
                notify_socket = str;
        }
        else printk(KERN_ERR "mconsole_setup : Unknown option - '%s'\n", str);
-       return(1);
+       return 1;
 }
 
-__setup("mconsole", mconsole_setup);
+__setup("mconsole=", mconsole_setup);
 
 __uml_help(mconsole_setup,
 "mconsole=notify:<socket>\n"
@@ -744,11 +915,12 @@ static int notify_panic(struct notifier_block *self, unsigned long unused1,
 {
        char *message = ptr;
 
-       if(notify_socket == NULL) return(0);
+       if (notify_socket == NULL)
+               return 0;
 
        mconsole_notify(notify_socket, MCONSOLE_PANIC, message,
                        strlen(message) + 1);
-       return(0);
+       return 0;
 }
 
 static struct notifier_block panic_exit_notifier = {
@@ -759,15 +931,16 @@ static struct notifier_block panic_exit_notifier = {
 
 static int add_notifier(void)
 {
-       notifier_chain_register(&panic_notifier_list, &panic_exit_notifier);
-       return(0);
+       atomic_notifier_chain_register(&panic_notifier_list,
+                       &panic_exit_notifier);
+       return 0;
 }
 
 __initcall(add_notifier);
 
 char *mconsole_notify_socket(void)
 {
-       return(notify_socket);
+       return notify_socket;
 }
 
 EXPORT_SYMBOL(mconsole_notify_socket);