2 * linux/kernel/vserver/proc.c
4 * Virtual Context Support
6 * Copyright (C) 2003-2005 Herbert Pƶtzl
8 * V0.01 basic structure
9 * V0.02 adaptation vs1.3.0
10 * V0.03 proc permissions
11 * V0.04 locking/generic
12 * V0.05 next generation procfs
13 * V0.06 inode validation
14 * V0.07 generic rewrite vid
18 #include <linux/errno.h>
19 #include <linux/proc_fs.h>
20 #include <linux/sched.h>
21 #include <linux/vs_context.h>
22 #include <linux/vs_network.h>
23 #include <linux/vs_cvirt.h>
25 #include <linux/vserver/switch.h>
27 #include <asm/uaccess.h>
28 #include <asm/unistd.h>
30 #include "cvirt_proc.h"
31 #include "limit_proc.h"
32 #include "sched_proc.h"
33 #include "vci_config.h"
35 static struct proc_dir_entry *proc_virtual;
37 static struct proc_dir_entry *proc_vnet;
40 enum vid_directory_inos {
54 #define PROC_VID_MASK 0x60
57 /* first the actual feeds */
60 static int proc_virtual_info(int vid, char *buffer)
62 return sprintf(buffer,
63 "VCIVersion:\t%04x:%04x\n"
74 int proc_xid_info (int vid, char *buffer)
79 vxi = lookup_vx_info(vid);
82 length = sprintf(buffer,
94 int proc_xid_status (int vid, char *buffer)
99 vxi = lookup_vx_info(vid);
102 length = sprintf(buffer,
109 ,atomic_read(&vxi->vx_usecnt)
110 ,atomic_read(&vxi->vx_tasks)
111 ,(unsigned long long)vxi->vx_flags
112 ,(unsigned long long)vxi->vx_bcaps
113 ,(unsigned long long)vxi->vx_ccaps
114 ,atomic_read(&vxi->limit.ticks)
120 int proc_xid_limit (int vid, char *buffer)
125 vxi = lookup_vx_info(vid);
128 length = vx_info_proc_limit(&vxi->limit, buffer);
133 int proc_xid_sched (int vid, char *buffer)
138 vxi = lookup_vx_info(vid);
141 length = vx_info_proc_sched(&vxi->sched, buffer);
146 int proc_xid_cvirt (int vid, char *buffer)
151 vxi = lookup_vx_info(vid);
155 length = vx_info_proc_cvirt(&vxi->cvirt, buffer);
160 int proc_xid_cacct (int vid, char *buffer)
165 vxi = lookup_vx_info(vid);
168 length = vx_info_proc_cacct(&vxi->cacct, buffer);
174 static int proc_vnet_info(int vid, char *buffer)
176 return sprintf(buffer,
177 "VCIVersion:\t%04x:%04x\n"
180 ,VCI_VERSION & 0xFFFF
186 int proc_nid_info (int vid, char *buffer)
191 nxi = lookup_nx_info(vid);
194 length = sprintf(buffer,
200 for (i=0; i<nxi->nbipv4; i++) {
201 length += sprintf(buffer + length,
202 "%d:\t" NIPQUAD_FMT "/" NIPQUAD_FMT "\n", i,
203 NIPQUAD(nxi->ipv4[i]), NIPQUAD(nxi->mask[i]));
209 int proc_nid_status (int vid, char *buffer)
214 nxi = lookup_nx_info(vid);
217 length = sprintf(buffer,
220 ,atomic_read(&nxi->nx_usecnt)
221 ,atomic_read(&nxi->nx_tasks)
227 /* here the inode helpers */
230 #define fake_ino(id,nr) (((nr) & 0xFFFF) | \
231 (((id) & 0xFFFF) << 16))
233 #define inode_vid(i) (((i)->i_ino >> 16) & 0xFFFF)
234 #define inode_type(i) ((i)->i_ino & 0xFFFF)
236 #define MAX_MULBY10 ((~0U-9)/10)
239 static struct inode *proc_vid_make_inode(struct super_block * sb,
242 struct inode *inode = new_inode(sb);
247 inode->i_mtime = inode->i_atime =
248 inode->i_ctime = CURRENT_TIME;
249 inode->i_ino = fake_ino(vid, ino);
257 static int proc_vid_revalidate(struct dentry * dentry, struct nameidata *nd)
259 struct inode * inode = dentry->d_inode;
262 vid = inode_vid(inode);
263 switch (inode_type(inode) & PROC_VID_MASK) {
265 hashed = xid_is_hashed(vid);
268 hashed = nid_is_hashed(vid);
278 #define PROC_BLOCK_SIZE (PAGE_SIZE - 1024)
280 static ssize_t proc_vid_info_read(struct file * file, char __user * buf,
281 size_t count, loff_t *ppos)
283 struct inode * inode = file->f_dentry->d_inode;
288 if (count > PROC_BLOCK_SIZE)
289 count = PROC_BLOCK_SIZE;
290 if (!(page = __get_free_page(GFP_KERNEL)))
293 vid = inode_vid(inode);
294 length = PROC_I(inode)->op.proc_vid_read(vid, (char*)page);
297 length = simple_read_from_buffer(buf, count, ppos,
298 (char *)page, length);
307 /* here comes the lower level (vid) */
309 static struct file_operations proc_vid_info_file_operations = {
310 .read = proc_vid_info_read,
313 static struct dentry_operations proc_vid_dentry_operations = {
314 .d_revalidate = proc_vid_revalidate,
325 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
327 static struct vid_entry vx_base_stuff[] = {
328 E(PROC_XID_INFO, "info", S_IFREG|S_IRUGO),
329 E(PROC_XID_STATUS, "status", S_IFREG|S_IRUGO),
330 E(PROC_XID_LIMIT, "limit", S_IFREG|S_IRUGO),
331 E(PROC_XID_SCHED, "sched", S_IFREG|S_IRUGO),
332 E(PROC_XID_CVIRT, "cvirt", S_IFREG|S_IRUGO),
333 E(PROC_XID_CACCT, "cacct", S_IFREG|S_IRUGO),
337 static struct vid_entry vn_base_stuff[] = {
338 E(PROC_NID_INFO, "info", S_IFREG|S_IRUGO),
339 E(PROC_NID_STATUS, "status", S_IFREG|S_IRUGO),
345 static struct dentry *proc_vid_lookup(struct inode *dir,
346 struct dentry *dentry, struct nameidata *nd)
355 switch (inode_type(dir)) {
366 for (; p->name; p++) {
367 if (p->len != dentry->d_name.len)
369 if (!memcmp(dentry->d_name.name, p->name, p->len))
376 inode = proc_vid_make_inode(dir->i_sb, inode_vid(dir), p->type);
382 PROC_I(inode)->op.proc_vid_read = proc_xid_info;
384 case PROC_XID_STATUS:
385 PROC_I(inode)->op.proc_vid_read = proc_xid_status;
388 PROC_I(inode)->op.proc_vid_read = proc_xid_limit;
391 PROC_I(inode)->op.proc_vid_read = proc_xid_sched;
394 PROC_I(inode)->op.proc_vid_read = proc_xid_cvirt;
397 PROC_I(inode)->op.proc_vid_read = proc_xid_cacct;
401 PROC_I(inode)->op.proc_vid_read = proc_nid_info;
403 case PROC_NID_STATUS:
404 PROC_I(inode)->op.proc_vid_read = proc_nid_status;
408 printk("procfs: impossible type (%d)",p->type);
410 return ERR_PTR(-EINVAL);
412 inode->i_mode = p->mode;
413 inode->i_fop = &proc_vid_info_file_operations;
415 inode->i_flags|=S_IMMUTABLE;
417 dentry->d_op = &proc_vid_dentry_operations;
418 d_add(dentry, inode);
421 return ERR_PTR(error);
425 static int proc_vid_readdir(struct file * filp,
426 void * dirent, filldir_t filldir)
429 struct inode *inode = filp->f_dentry->d_inode;
435 if (filldir(dirent, ".", 1, i,
436 inode->i_ino, DT_DIR) < 0)
442 if (filldir(dirent, "..", 2, i,
443 PROC_ROOT_INO, DT_DIR) < 0)
450 switch (inode_type(inode)) {
452 size = sizeof(vx_base_stuff);
453 p = vx_base_stuff + i;
456 size = sizeof(vn_base_stuff);
457 p = vn_base_stuff + i;
462 if (i >= size/sizeof(struct vid_entry))
465 if (filldir(dirent, p->name, p->len,
466 filp->f_pos, fake_ino(inode_vid(inode),
467 p->type), p->mode >> 12) < 0)
479 /* now the upper level (virtual) */
481 static struct file_operations proc_vid_file_operations = {
482 .read = generic_read_dir,
483 .readdir = proc_vid_readdir,
486 static struct inode_operations proc_vid_inode_operations = {
487 .lookup = proc_vid_lookup,
492 static __inline__ int atovid(const char *str, int len)
502 if (vid >= MAX_MULBY10)
513 struct dentry *proc_virtual_lookup(struct inode *dir,
514 struct dentry * dentry, struct nameidata *nd)
521 name = dentry->d_name.name;
522 len = dentry->d_name.len;
525 if (len == 7 && !memcmp(name, "current", 7)) {
526 inode = new_inode(dir->i_sb);
529 inode->i_mtime = inode->i_atime =
530 inode->i_ctime = CURRENT_TIME;
531 inode->i_ino = fake_ino(1, PROC_XID_INO);
532 inode->i_mode = S_IFLNK|S_IRWXUGO;
533 inode->i_uid = inode->i_gid = 0;
534 d_add(dentry, inode);
537 if (len == 4 && !memcmp(name, "info", 4)) {
538 inode = proc_vid_make_inode(dir->i_sb, 0, PROC_XID_INFO);
541 inode->i_fop = &proc_vid_info_file_operations;
542 PROC_I(inode)->op.proc_vid_read = proc_virtual_info;
543 inode->i_mode = S_IFREG|S_IRUGO;
544 d_add(dentry, inode);
549 xid = atovid(name, len);
552 vxi = lookup_vx_info(xid);
557 if (vx_check(xid, VX_ADMIN|VX_WATCH|VX_IDENT))
558 inode = proc_vid_make_inode(dir->i_sb,
559 vxi->vx_id, PROC_XID_INO);
563 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
564 inode->i_op = &proc_vid_inode_operations;
565 inode->i_fop = &proc_vid_file_operations;
567 inode->i_flags|=S_IMMUTABLE;
569 dentry->d_op = &proc_vid_dentry_operations;
570 d_add(dentry, inode);
580 struct dentry *proc_vnet_lookup(struct inode *dir,
581 struct dentry * dentry, struct nameidata *nd)
588 name = dentry->d_name.name;
589 len = dentry->d_name.len;
591 if (len == 7 && !memcmp(name, "current", 7)) {
592 inode = new_inode(dir->i_sb);
595 inode->i_mtime = inode->i_atime =
596 inode->i_ctime = CURRENT_TIME;
597 inode->i_ino = fake_ino(1, PROC_NID_INO);
598 inode->i_mode = S_IFLNK|S_IRWXUGO;
599 inode->i_uid = inode->i_gid = 0;
600 d_add(dentry, inode);
603 if (len == 4 && !memcmp(name, "info", 4)) {
604 inode = proc_vid_make_inode(dir->i_sb, 0, PROC_NID_INFO);
607 inode->i_fop = &proc_vid_info_file_operations;
608 PROC_I(inode)->op.proc_vid_read = proc_vnet_info;
609 inode->i_mode = S_IFREG|S_IRUGO;
610 d_add(dentry, inode);
615 nid = atovid(name, len);
618 nxi = lookup_nx_info(nid);
624 inode = proc_vid_make_inode(dir->i_sb,
625 nxi->nx_id, PROC_NID_INO);
629 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
630 inode->i_op = &proc_vid_inode_operations;
631 inode->i_fop = &proc_vid_file_operations;
633 inode->i_flags|=S_IMMUTABLE;
635 dentry->d_op = &proc_vid_dentry_operations;
636 d_add(dentry, inode);
648 #define PROC_NUMBUF 10
649 #define PROC_MAXVIDS 32
651 int proc_virtual_readdir(struct file * filp,
652 void * dirent, filldir_t filldir)
654 unsigned int xid_array[PROC_MAXVIDS];
655 char buf[PROC_NUMBUF];
656 unsigned int nr = filp->f_pos-3;
657 unsigned int nr_xids, i;
660 switch ((long)filp->f_pos) {
662 ino = fake_ino(0, PROC_XID_INO);
663 if (filldir(dirent, ".", 1,
664 filp->f_pos, ino, DT_DIR) < 0)
669 ino = filp->f_dentry->d_parent->d_inode->i_ino;
670 if (filldir(dirent, "..", 2,
671 filp->f_pos, ino, DT_DIR) < 0)
676 ino = fake_ino(0, PROC_XID_INFO);
677 if (filldir(dirent, "info", 4,
678 filp->f_pos, ino, DT_LNK) < 0)
683 if (vx_current_xid() > 1) {
684 ino = fake_ino(1, PROC_XID_INO);
685 if (filldir(dirent, "current", 7,
686 filp->f_pos, ino, DT_LNK) < 0)
692 nr_xids = get_xid_list(nr, xid_array, PROC_MAXVIDS);
693 for (i = 0; i < nr_xids; i++) {
694 int xid = xid_array[i];
695 ino_t ino = fake_ino(xid, PROC_XID_INO);
696 unsigned int j = PROC_NUMBUF;
698 do buf[--j] = '0' + (xid % 10); while (xid/=10);
700 if (filldir(dirent, buf+j, PROC_NUMBUF-j,
701 filp->f_pos, ino, DT_DIR) < 0)
709 static struct file_operations proc_virtual_dir_operations = {
710 .read = generic_read_dir,
711 .readdir = proc_virtual_readdir,
714 static struct inode_operations proc_virtual_dir_inode_operations = {
715 .lookup = proc_virtual_lookup,
719 int proc_vnet_readdir(struct file * filp,
720 void * dirent, filldir_t filldir)
722 unsigned int nid_array[PROC_MAXVIDS];
723 char buf[PROC_NUMBUF];
724 unsigned int nr = filp->f_pos-2;
725 unsigned int nr_nids, i;
728 switch ((long)filp->f_pos) {
730 ino = fake_ino(0, PROC_NID_INO);
731 if (filldir(dirent, ".", 1,
732 filp->f_pos, ino, DT_DIR) < 0)
737 ino = filp->f_dentry->d_parent->d_inode->i_ino;
738 if (filldir(dirent, "..", 2,
739 filp->f_pos, ino, DT_DIR) < 0)
744 ino = fake_ino(0, PROC_NID_INFO);
745 if (filldir(dirent, "info", 4,
746 filp->f_pos, ino, DT_REG) < 0)
752 nr_nids = get_nid_list(nr, nid_array, PROC_MAXVIDS);
753 for (i = 0; i < nr_nids; i++) {
754 int nid = nid_array[i];
755 ino_t ino = fake_ino(nid, PROC_NID_INO);
756 unsigned long j = PROC_NUMBUF;
758 do buf[--j] = '0' + (nid % 10); while (nid/=10);
760 if (filldir(dirent, buf+j, PROC_NUMBUF-j,
761 filp->f_pos, ino, DT_DIR) < 0)
769 static struct file_operations proc_vnet_dir_operations = {
770 .read = generic_read_dir,
771 .readdir = proc_vnet_readdir,
774 static struct inode_operations proc_vnet_dir_inode_operations = {
775 .lookup = proc_vnet_lookup,
780 void proc_vx_init(void)
782 struct proc_dir_entry *ent;
784 ent = proc_mkdir("virtual", 0);
786 ent->proc_fops = &proc_virtual_dir_operations;
787 ent->proc_iops = &proc_virtual_dir_inode_operations;
791 ent = proc_mkdir("virtnet", 0);
793 ent->proc_fops = &proc_vnet_dir_operations;
794 ent->proc_iops = &proc_vnet_dir_inode_operations;
805 int proc_pid_vx_info(struct task_struct *p, char *buffer)
808 char * orig = buffer;
810 buffer += sprintf (buffer,"XID:\t%d\n", vx_task_xid(p));
811 if (vx_flags(VXF_INFO_HIDE, 0))
814 vxi = task_get_vx_info(p);
818 buffer += sprintf (buffer,"BCaps:\t%016llx\n"
819 ,(unsigned long long)vxi->vx_bcaps);
820 buffer += sprintf (buffer,"CCaps:\t%016llx\n"
821 ,(unsigned long long)vxi->vx_ccaps);
822 buffer += sprintf (buffer,"CFlags:\t%016llx\n"
823 ,(unsigned long long)vxi->vx_flags);
824 buffer += sprintf (buffer,"CIPid:\t%d\n"
829 return buffer - orig;
833 int proc_pid_nx_info(struct task_struct *p, char *buffer)
836 char * orig = buffer;
839 buffer += sprintf (buffer,"NID:\t%d\n", nx_task_nid(p));
840 if (vx_flags(VXF_INFO_HIDE, 0))
842 nxi = task_get_nx_info(p);
846 for (i=0; i<nxi->nbipv4; i++){
847 buffer += sprintf (buffer,
848 "V4Root[%d]:\t%d.%d.%d.%d/%d.%d.%d.%d\n", i
849 ,NIPQUAD(nxi->ipv4[i])
850 ,NIPQUAD(nxi->mask[i]));
852 buffer += sprintf (buffer,
853 "V4Root[bcast]:\t%d.%d.%d.%d\n"
854 ,NIPQUAD(nxi->v4_bcast));
858 return buffer - orig;