Hi, this time I want to try a 9P session in debian. The following code
is running at Plan 9 and file operations (cat, touch, echo>, and rm)
for the Plan 9 client function as expected:

#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <thread.h>
#include <auth.h>
#include <9p.h>

typedef struct  Ramfile Ramfile;
struct Ramfile {
        char    *data;
        int     ndata;
};

void
fsread(Req *r)
{
        Ramfile *rf;
        int     count;
        long    offset;

        count = r->ifcall.count;
        offset = r->ifcall.offset;
        rf = r->fid->file->aux;

        if(offset >= rf->ndata) {
                r->ofcall.count = 0;
                respond(r, nil);
                return;
        }
        if(offset+count >= rf->ndata)
                count = rf->ndata - offset;

        memmove(r->ofcall.data, rf->data, count);
        r->ofcall.count = count;
        respond(r, nil);
}

void
fswrite(Req *r)
{
        Ramfile *rf;
        int     count;
        long    offset;

        rf =  r->fid->file->aux;
        count = r->ifcall.count;
        offset = r->ifcall.offset;

        void *v;
        if(offset+count >= rf->ndata) {
                v = realloc(rf->data, offset+count);
                if(v == nil) {
                        respond(r, "realloc bad");
                        return;
                }

                rf->ndata = offset+count;
                rf->data = v;
                r->fid->file->length = rf->ndata;
        }

        memmove(rf->data, r->ifcall.data, count);
        r->ofcall.count = count;
        respond(r, nil);
}

void
fscreate(Req *r)
{
        Ramfile *rf;
        File    *f;

        f = createfile(r->fid->file, r->ifcall.name, r->fid->uid, r-
>ifcall.perm, nil);
        if(f) {
                rf = emalloc9p(sizeof *rf);
                f->aux = rf;
                r->fid->file = f;
                r->ofcall.qid = f->qid;
                respond(r, nil);
                return;
        }

        respond(r, "bad file creation");
}

Srv fs = {
        .read = fsread,
        .write =        fswrite,
        .create =       fscreate,
};


void
usage(void)
{
        fprint(2, "usage: %s [-s srvname] [-m mtpt]\n", argv0);
        exits("usage");
}

void
main(int argc, char **argv)
{
        char    *srvname;
        char    *mtpt;
        Tree    *tree;

        srvname = "memfs";
        mtpt = nil;
        tree = alloctree(nil, nil, DMDIR|0777, nil);
        fs.tree = tree;
        ARGBEGIN{
        case 'm':
                mtpt = EARGF(usage());
                break;
        case 's':
                srvname = ARGF();
                break;
        default:
                usage();
        }ARGEND;

        if(argc)
                usage();

        Ramfile *rf;
        rf = emalloc9p(sizeof *rf);
        createfile(tree->root, "foo", nil, 0666, rf);

        if(srvname || mtpt)
                postmountsrv(&fs, srvname, mtpt, MREPL|MCREATE);

        exits(0);
}


In Plan 9, it announces an address through TCP:

   longy# aux/listen1 -tv tcp!*!1114 /bin/exportfs -S /srv/memfs &

Now I drop lines in debian console and cd to mount point:

   kin@debian:~$ sudo mount -t 9p 192.168.1.180 /tmp/memfs/ -o
port=1114
   kin@debian:~$ cd /tmp/memfs
   kin@debian:/tmp/memfs$ ls -l
   total 1
   -rw-rw-rw- 1 4294967294 4294967294 6 Jan  3 08:52 foo
   kin@debian:/tmp/memfs$ echo abcd >foo
   bash: foo: Operation not permitted
   kin@debian:/tmp/memfs$ touch junk
   touch: cannot touch `junk': Unknown error 526
   kin@debian:/tmp/memfs$

CATing and RMing files  are OK. But why I cannot create and write to
9P file in linux?

Thanks,
kin

Reply via email to