Example #1
0
File: serv.c Project: jjhlzn/6.828
// Write req->req_n bytes from req->req_buf to req_fileid, starting at
// the current seek position, and update the seek position
// accordingly.  Extend the file if necessary.  Returns the number of
// bytes written, or < 0 on error.
int
serve_write(envid_t envid, struct Fsreq_write *req)
{
	if (debug)
		cprintf("serve_write %08x %08x %08x\n", envid, req->req_fileid, req->req_n);

	// LAB 5: Your code here.
	struct OpenFile *o;
	int r;
	
	if ((r = openfile_lookup(envid, req->req_fileid, &o)) < 0) {
		if (debug)
			cprintf("serve_write: openfile_lookup fail\n");
		return r;
	}
	
	if ((r = file_write(o->o_file, 
						req->req_buf,
						req->req_n, 
						o->o_fd->fd_offset)) < 0) {
		if (debug)
			cprintf("serve_write: file_write fail\n");
		return r;
	}
	o->o_fd->fd_offset += r;
	if (debug)
		cprintf("serve_write: fd_offset = %08x\n", o->o_fd->fd_offset);
	return r;
}
Example #2
0
File: serv.c Project: yaobaiwei/JOS
void
serve_map(envid_t envid, struct Fsreq_map *rq)
{
	int r;
	char *blk;
	struct OpenFile *o;
	int perm;

	if (debug)
		cprintf("serve_map %08x %08x %08x\n", envid, rq->req_fileid, rq->req_offset);

	// Map the requested block in the client's address space
	// by using ipc_send.
	// Map read-only unless the file's open mode (o->o_mode) allows writes
	// (see the O_ flags in inc/lib.h).
	
	// LAB 5: Your code here.
	if ((r = openfile_lookup(envid, rq->req_fileid, &o)) < 0)
		goto out;

    if ((r = file_get_block(o->o_file, rq->req_offset/BLKSIZE, &blk)) < 0)
        goto out;
    
    if(o->o_mode & O_RDONLY)
        ipc_send(envid, 0, (void *)blk, PTE_P|PTE_U);
    else
        ipc_send(envid, 0, (void *)blk, PTE_P|PTE_U|PTE_W);

    return;

out:
	ipc_send(envid, r, 0, 0);
	//panic("serve_map not implemented");
}
Example #3
0
// Map the requested block in the client's address space
// by using ipc_send.
void
serve_map(envid_t envid, struct Fsreq_map *rq)
{
	int r;
	char *blk = NULL;
	struct OpenFile *o;
	int perm = 0;

	if (debug)
		cprintf("serve_map %08x %08x %08x\n", envid, rq->req_fileid, rq->req_offset);

	if ((r = openfile_lookup(envid, rq->req_fileid, &o)) < 0)
		goto out;

	// Map read-only unless the file's open mode (o->o_mode) allows writes
	// (see the O_ flags in inc/lib.h).
        perm = PTE_U | PTE_P | PTE_SHARE;
        if (o->o_mode & (O_WRONLY|O_RDWR))
          perm |= PTE_W;
        
	r = file_get_block(o->o_file, rq->req_offset/BLKSIZE, &blk);

out:
	ipc_send(envid, r, blk, perm);
}
Example #4
0
File: serv.c Project: ichaos/jos
void
serve_dirty(envid_t envid, struct Fsreq_dirty *rq)
{
	struct OpenFile *o;
	int r;
        //uint32_t *pdiskbno;
        //char *blk;

	if (debug)
		cprintf("serve_dirty %08x %08x %08x\n", envid,
                        rq->req_fileid, rq->req_offset);

	// Find the file and dirty the file at the requested offset.
	// Send the return value back using ipc_send.
	// LAB 5: Your code here.
	//panic("serve_dirty not implemented");
        r = openfile_lookup(envid, rq->req_fileid, &o);
        if (r<0) {
                cprintf("serv.c:serve_map--openfile_lookup failed.\n");
                goto out;
        }
        r = file_dirty(o->o_file, rq->req_offset);
 out:
        ipc_send(envid, r, 0, 0);

}
Example #5
0
// Read at most ipc->read.req_n bytes from the current seek position
// in ipc->read.req_fileid.  Return the bytes read from the file to
// the caller in ipc->readRet, then update the seek position.  Returns
// the number of bytes successfully read, or < 0 on error.
int
serve_read(envid_t envid, union Fsipc *ipc)
{
	struct Fsreq_read *req = &ipc->read;
	struct Fsret_read *ret = &ipc->readRet;

  struct OpenFile *o;
  int r;
  size_t nbytes;
	if (debug)
		cprintf("serve_read %08x %08x %08x\n", envid, req->req_fileid, req->req_n);

	// Look up the file id, read the bytes into 'ret', and update
	// the seek position.  Be careful if req->req_n > PGSIZE
	// (remember that read is always allowed to return fewer bytes
	// than requested).  Also, be careful because ipc is a union,
	// so filling in ret will overwrite req.
	//
	// Hint: Use file_read.
	// Hint: The seek position is stored in the struct Fd.
	// LAB 5: Your code here 
  if ((r = openfile_lookup(envid, req->req_fileid, &o)) < 0) {
    cprintf("serve_read: failed to lookup open fileid\n");
    return r;
  }
  nbytes = MIN(req->req_n, PGSIZE);
  if ((nbytes = file_read(o->o_file, (void *)ret->ret_buf,
      nbytes, o->o_fd->fd_offset)) >= 0)
    o->o_fd->fd_offset += nbytes;
  return nbytes;
}
Example #6
0
// Write req->req_n bytes from req->req_buf to req_fileid, starting at
// the current seek position, and update the seek position
// accordingly.  Extend the file if necessary.  Returns the number of
// bytes written, or < 0 on error.
    int
serve_write(envid_t envid, struct Fsreq_write *req)
{
	struct OpenFile *o;
	int r;
	
	if (debug)
		cprintf("serve_write %08x %08x %08x\n", envid, req->req_fileid, req->req_n);
	
	if ((r = openfile_lookup(envid, req->req_fileid, &o)) < 0)
		return r;
	
	if(req->req_n > PGSIZE)
		req->req_n = PGSIZE;
	
	if ((r = file_write(o->o_file, req->req_buf, req->req_n, o->o_fd->fd_offset)) < 0)
		return r;
	
	o->o_fd->fd_offset += r;
	
	//cprintf("served the read %d\n",r);
	return r;
	// LAB 5: Your code here.
	//panic("serve_write not implemented");
}
Example #7
0
// Read at most ipc->read.req_n bytes from the current seek position
// in ipc->read.req_fileid.  Return the bytes read from the file to
// the caller in ipc->readRet, then update the seek position.  Returns
// the number of bytes successfully read, or < 0 on error.
int
serve_read(envid_t envid, union Fsipc *ipc)
{
	struct Fsreq_read *req = &ipc->read;
	struct Fsret_read *ret = &ipc->readRet;

	if (debug)
		cprintf("serve_read %08x %08x %08x\n", envid, req->req_fileid, req->req_n);

	// Look up the file id, read the bytes into 'ret', and update
	// the seek position.  Be careful if req->req_n > PGSIZE
	// (remember that read is always allowed to return fewer bytes
	// than requested).  Also, be careful because ipc is a union,
	// so filling in ret will overwrite req.
	//
	// Hint: Use file_read.
	// Hint: The seek position is stored in the struct Fd.
	// LAB 5: Your code here
	//panic("serve_read not implemented");
	/*stone's solution for lab5*/
	struct OpenFile *o;
	int r;
	if ((r = openfile_lookup(envid, req->req_fileid, &o)) < 0)
		return r;
	if ((r = file_read(o->o_file, ret->ret_buf, MIN(req->req_n, PGSIZE), o->o_fd->fd_offset)) < 0)
		return r;
	o->o_fd->fd_offset += r;
	return r;
}
Example #8
0
// Read at most ipc->read.req_n bytes from the current seek position
// in ipc->read.req_fileid.  Return the bytes read from the file to
// the caller in ipc->readRet, then update the seek position.  Returns
// the number of bytes successfully read, or < 0 on error.
int
serve_read(envid_t envid, union Fsipc *ipc)
{
	struct Fsreq_read *req = &ipc->read;
	struct Fsret_read *ret = &ipc->readRet;

	if (debug)
		cprintf("serve_read %08x %08x %08x\n", envid, req->req_fileid, req->req_n);

	// Look up the file id, read the bytes into 'ret', and update
	// the seek position.  Be careful if req->req_n > PGSIZE
	// (remember that read is always allowed to return fewer bytes
	// than requested).  Also, be careful because ipc is a union,
	// so filling in ret will overwrite req.
	//
	struct OpenFile *o;
	int r;

	if ((r = openfile_lookup(envid, req->req_fileid, &o)) < 0)
		return r;

	if ((r = file_read(o->o_file, ret->ret_buf,
			   MIN(req->req_n, sizeof ret->ret_buf),
			   o->o_fd->fd_offset)) < 0)
		return r;

	o->o_fd->fd_offset += r;
	return r;
}
Example #9
0
File: serv.c Project: ichaos/jos
void
serve_set_size(envid_t envid, struct Fsreq_set_size *rq)
{
	struct OpenFile *o;
	int r;

	if (debug)
		cprintf("serve_set_size %08x %08x %08x\n", envid,
                        rq->req_fileid, rq->req_size);

	// The file system server maintains three structures
	// for each open file.
	//
	// 1. The on-disk 'struct File' is mapped into the part of memory
	//    that maps the disk.  This memory is kept private to the
	//    file server.
	// 2. Each open file has a 'struct Fd' as well,
	//    which sort of corresponds to a Unix file descriptor.
	//    This 'struct Fd' is kept on *its own page* in memory,
	//    and it is shared with any environments that
	//    have the file open.
	//    Part of the 'struct Fd' is a *copy* of the on-disk
	//    'struct File' (struct Fd::fd_file.file), except that the
	//    block pointers are effectively garbage.
	//    This lets environments find out a file's size by examining
	//    struct Fd::fd_file.file.f_size, for example.
	//    *The server must make sure to keep two copies of the
	//    'struct File' in sync!*
	// 3. 'struct OpenFile' links these other two structures,
	//    and is kept private to the file server.
	//    The server maintains an array of all open files, indexed
	//    by "file ID".
	//    (There can be at most MAXFILE files open concurrently.)
	//    The client uses file IDs to communicate with the server.
	//    File IDs are a lot like environment IDs in the kernel.
	//    Use openfile_lookup to translate file IDs to struct OpenFile.

	// Every file system IPC call has the same general structure.
	// Here's how it goes.

	// First, use openfile_lookup to find the relevant open file.
	// On failure, return the error code to the client with ipc_send.
	if ((r = openfile_lookup(envid, rq->req_fileid, &o)) < 0)
		goto out;

	// Second, call the relevant file system function (from fs/fs.c).
	// On failure, return the error code to the client.
	if ((r = file_set_size(o->o_file, rq->req_size)) < 0)
		goto out;

	// Third, update the 'struct Fd' copy of the 'struct File'
	// as appropriate.
	o->o_fd->fd_file.file.f_size = rq->req_size;

	// Finally, return to the client!
	// (We just return r since we know it's 0 at this point.)
out:
	ipc_send(envid, r, 0, 0);
}
Example #10
0
// Flush all data and metadata of req->req_fileid to disk.
int
serve_flush(envid_t envid, struct Fsreq_flush *req)
{
	struct OpenFile *o;
	int r;

	if (debug)
		cprintf("serve_flush %08x %08x\n", envid, req->req_fileid);

	if ((r = openfile_lookup(envid, req->req_fileid, &o)) < 0)
		return r;
	file_flush(o->o_file);
	return 0;
}
Example #11
0
void
serve_close(envid_t envid, struct Fsreq_close *rq)
{
	struct OpenFile *o;
	int r;

	if (debug)
		cprintf("serve_close %08x %08x\n", envid, rq->req_fileid);

	if ((r = openfile_lookup(envid, rq->req_fileid, &o)) < 0)
		goto out;
	file_close(o->o_file);
	r = 0;

out:
	ipc_send(envid, r, 0, 0);
}
Example #12
0
// Mark the page containing the requested file offset as dirty.
void
serve_dirty(envid_t envid, struct Fsreq_dirty *rq)
{
	struct OpenFile *o;
	int r;

	if (debug)
		cprintf("serve_dirty %08x %08x %08x\n", envid, rq->req_fileid, rq->req_offset);

	if ((r = openfile_lookup(envid, rq->req_fileid, &o)) < 0)
		goto out;

	r = file_dirty(o->o_file, rq->req_offset);

out:
	ipc_send(envid, r, 0, 0);
}
Example #13
0
// Write req->req_n bytes from req->req_buf to req_fileid, starting at
// the current seek position, and update the seek position
// accordingly.  Extend the file if necessary.  Returns the number of
// bytes written, or < 0 on error.
int
serve_write(envid_t envid, struct Fsreq_write *req)
{
	if (debug)
		cprintf("serve_write %08x %08x %08x\n", envid, req->req_fileid, req->req_n);

	// LAB 5: Your code here.
  struct OpenFile *o;
  int r;

  if ((r = openfile_lookup(envid, req->req_fileid, &o)) < 0)
    return r;
  r = file_write(o->o_file, req->req_buf,
      MIN(req->req_n, PGSIZE), o->o_fd->fd_offset);
  if (r > 0)
    o->o_fd->fd_offset += r;
  return r;	
}
Example #14
0
// Write req->req_n bytes from req->req_buf to req_fileid, starting at
// the current seek position, and update the seek position
// accordingly.  Extend the file if necessary.  Returns the number of
// bytes written, or < 0 on error.
int
serve_write(envid_t envid, struct Fsreq_write *req)
{
	if (debug)
		cprintf("serve_write %08x %08x %08x\n", envid, req->req_fileid, req->req_n);

	// LAB 5: Your code here.
	//panic("serve_write not implemented");
	/*stone's solution for lab5*/
	struct OpenFile *o;
	int r;
	if ((r = openfile_lookup(envid, req->req_fileid, &o)) < 0)
		return r;
	if ((r = file_write(o->o_file, req->req_buf, req->req_n, o->o_fd->fd_offset)) < 0)
		return r;
	o->o_fd->fd_offset += r;
	return r;
}
// Read at most ipc->read.req_n bytes from the current seek position
// in ipc->read.req_fileid.  Return the bytes read from the file to
// the caller in ipc->readRet, then update the seek position.  Returns
// the number of bytes successfully read, or < 0 on error.
int
serve_read(envid_t envid, union Fsipc *ipc)
{
	struct Fsreq_read *req = &ipc->read;
	struct Fsret_read *ret = &ipc->readRet;

	if (debug)
		cprintf("serve_read %08x %08x %08x\n", envid, req->req_fileid, req->req_n);

	// Lab 5: Your code here:
        struct OpenFile *o;
        int r;
        if((r = openfile_lookup(envid, req->req_fileid, &o)) < 0)
                return r;
        if((r = file_read(o->o_file, ret->ret_buf, MIN(req->req_n, sizeof(ret->ret_buf)), o->o_fd->fd_offset)) < 0)
                return r; 
        o->o_fd->fd_offset += r;      
	return r;
}
Example #16
0
// Write req->req_n bytes from req->req_buf to req_fileid, starting at
// the current seek position, and update the seek position
// accordingly.  Extend the file if necessary.  Returns the number of
// bytes written, or < 0 on error.
int
serve_write(envid_t envid, struct Fsreq_write *req)
{
	struct OpenFile *o;
	int r = -1;
	int count = 0;
	if (debug)
		cprintf("serve_write %08x %08x %08x\n", envid, req->req_fileid, req->req_n);

	// LAB 5: Your code here.
	//panic("serve_write not implemented");
	if ((r = openfile_lookup(envid, req->req_fileid, &o)) < 0)
		return r;
	//cprintf("\ncopying data\n");
	count = file_write(o->o_file, req->req_buf, req->req_n, o->o_fd->fd_offset);
	o->o_fd->fd_offset += count; 
	file_flush(o->o_file);
	return count;
}
Example #17
0
// Stat ipc->stat.req_fileid.  Return the file's struct Stat to the
// caller in ipc->statRet.
int
serve_stat(envid_t envid, union Fsipc *ipc)
{
	struct Fsreq_stat *req = &ipc->stat;
	struct Fsret_stat *ret = &ipc->statRet;
	struct OpenFile *o;
	int r;

	if (debug)
		cprintf("serve_stat %08x %08x\n", envid, req->req_fileid);

	if ((r = openfile_lookup(envid, req->req_fileid, &o)) < 0)
		return r;

	strcpy(ret->ret_name, o->o_file->f_name);
	ret->ret_size = o->o_file->f_size;
	ret->ret_isdir = (o->o_file->f_type == FTYPE_DIR);
	return 0;
}
Example #18
0
// Write req->req_n bytes from req->req_buf to req_fileid, starting at
// the current seek position, and update the seek position
// accordingly.  Extend the file if necessary.  Returns the number of
// bytes written, or < 0 on error.
    int
serve_write(envid_t envid, struct Fsreq_write *req)
{
    // Ashish
	struct OpenFile *o;
        int r;
	size_t numBytesWritten;

	if (debug)
		cprintf("serve_write %08x %08x %08x\n", envid, req->req_fileid, req->req_n);

	if ((r = openfile_lookup(envid, req->req_fileid, &o)) < 0)
        	return r;

	numBytesWritten = file_write(o->o_file, (void*)req->req_buf, req->req_n, o->o_fd->fd_offset);
	o->o_fd->fd_offset+=numBytesWritten;
	return numBytesWritten;
    panic("serve_write not implemented");
}
Example #19
0
// Read at most ipc->read.req_n bytes from the current seek position
// in ipc->read.req_fileid.  Return the bytes read from the file to
// the caller in ipc->readRet, then update the seek position.  Returns
// the number of bytes successfully read, or < 0 on error.
int
serve_read(envid_t envid, union Fsipc *ipc)
{
	struct Fsreq_read *req = &ipc->read;
	struct Fsret_read *ret = &ipc->readRet;
	struct OpenFile *o;
	int r;
	int count=0;

	if (debug)
		cprintf("serve_read %08x %08x %08x\n", envid, req->req_fileid, req->req_n);

	// Lab 5: Your code here:
	if ((r = openfile_lookup(envid, req->req_fileid, &o)) < 0)
		return r;
	count = file_read(o->o_file, ret->ret_buf, req->req_n, o->o_fd->fd_offset);
	o->o_fd->fd_offset += count; 
	file_flush(o->o_file);
	return count;
}
Example #20
0
// Read at most ipc->read.req_n bytes from the current seek position
// in ipc->read.req_fileid.  Return the bytes read from the file to
// the caller in ipc->readRet, then update the seek position.  Returns
// the number of bytes successfully read, or < 0 on error.
int
serve_read(envid_t envid, union Fsipc *ipc)
{
	struct Fsreq_read *req = &ipc->read;
	struct Fsret_read *ret = &ipc->readRet;
	struct OpenFile *o;
	int r;

	if (debug)
		cprintf("serve_read %08x %08x %08x\n", envid, req->req_fileid, req->req_n);

	// Look up the file id, read the bytes into 'ret', and update
	// the seek position.  Be careful if req->req_n > PGSIZE
	// (remember that read is always allowed to return fewer bytes
	// than requested).  Also, be careful because ipc is a union,
	// so filling in ret will overwrite req.
	//
	// Hint: Use file_read.
	// Hint: The seek position is stored in the struct Fd.
	// LAB 5: Your code here
	if ((r = openfile_lookup(envid, req->req_fileid, &o)) < 0)
		return r;
		
	//Nitin
	//memset(ret->ret_buf,0,PGSIZE);

	if(req->req_n < PGSIZE)
	{
		r = file_read(o->o_file,ret->ret_buf,req->req_n,o->o_fd->fd_offset);	
		o->o_fd->fd_offset += r;	//req->req_n;
//		cprintf("File read less than PGSIZE\n");
	}
	else
	{
		r = file_read(o->o_file,ret->ret_buf,PGSIZE,o->o_fd->fd_offset);	
		o->o_fd->fd_offset += r;	//PGSIZE;
//		cprintf("Greater than PGSIZE\n");
	}
	return r;
	//panic("serve_read not implemented");
}
Example #21
0
// Set the size of req->req_fileid to req->req_size bytes, truncating
// or extending the file as necessary.
int
serve_set_size(envid_t envid, struct Fsreq_set_size *req)
{
	struct OpenFile *o;
	int r;

	if (debug)
		cprintf("serve_set_size %08x %08x %08x\n", envid, req->req_fileid, req->req_size);

	// Every file system IPC call has the same general structure.
	// Here's how it goes.

	// First, use openfile_lookup to find the relevant open file.
	// On failure, return the error code to the client with ipc_send.
	if ((r = openfile_lookup(envid, req->req_fileid, &o)) < 0)
		return r;

	// Second, call the relevant file system function (from fs/fs.c).
	// On failure, return the error code to the client.
	return file_set_size(o->o_file, req->req_size);
}
Example #22
0
int
serve_write(envid_t envid, struct Fsreq_write *req)
{
        struct OpenFile *o;
	int r;

	if (debug)
		cprintf("serve_write %08x %08x %08x\n", envid, req->req_fileid, req->req_n);

	// LAB 5: Your code here.
         
        if ((r = openfile_lookup(envid, req->req_fileid, &o)) < 0)
		return r;
                 //int n=0;
            // if(sizeof(req->req_buf)<sizeof(req->req_n))
              //       n=
        if((r = file_write(o->o_file,req->req_buf,req->req_n,o->o_fd->fd_offset))<0)
              return r;
            o->o_fd->fd_offset+=r;
          return r;
}
Example #23
0
File: serv.c Project: yaobaiwei/JOS
void
serve_dirty(envid_t envid, struct Fsreq_dirty *rq)
{
	struct OpenFile *o;
	int r;

	if (debug)
		cprintf("serve_dirty %08x %08x %08x\n", envid, rq->req_fileid, rq->req_offset);

	// Find the file and dirty the file at the requested offset.
	// Send the return value back using ipc_send.
	// LAB 5: Your code here.
	if ((r = openfile_lookup(envid, rq->req_fileid, &o)) < 0)
		goto out;

    r = file_dirty(o->o_file, rq->req_offset);

out:
	ipc_send(envid, r, 0, 0);
	//panic("serve_dirty not implemented");
}
Example #24
0
// Write req->req_n bytes from req->req_buf to req_fileid, starting at
// the current seek position, and update the seek position
// accordingly.  Extend the file if necessary.  Returns the number of
// bytes written, or < 0 on error.
int
serve_write(envid_t envid, struct Fsreq_write *req)
{
	if (debug)
		cprintf("serve_write %08x %08x %08x\n", envid, req->req_fileid, req->req_n);

	// LAB 5: Your code here.

	struct OpenFile *o;
	int ret;

	ret = openfile_lookup(envid, req->req_fileid, &o);
	if(ret) return ret;

	//find read from curr offset
	ret = file_write(o->o_file,req->req_buf,req->req_n,o->o_fd->fd_offset);
	if(ret<0) return ret;
	
	o->o_fd->fd_offset+= ret;
	return ret;	
}
Example #25
0
// Read at most ipc->read.req_n bytes from the current seek position
// in ipc->read.req_fileid.  Return the bytes read from the file to
// the caller in ipc->readRet, then update the seek position.  Returns
// the number of bytes successfully read, or < 0 on error.
int
serve_read(envid_t envid, union Fsipc *ipc)
{
    struct Fsreq_read *req = &ipc->read;
    struct Fsret_read *ret = &ipc->readRet;

    if (debug)
        cprintf("serve_read %08x %08x %08x\n", envid, req->req_fileid, req->req_n);

    // Lab 5: Your code here:
    struct OpenFile *o;
    int r;

    if ((r = openfile_lookup(envid, req->req_fileid, &o)) < 0)
        return r;
    ssize_t t = file_read(o->o_file, (void*)ret, req->req_n, o->o_fd->fd_offset);
    if (t < 0) return t;
    else {
        o->o_fd->fd_offset += t;
    }
    return t;
}
Example #26
0
// Write req->req_n bytes from req->req_buf to req_fileid, starting at
// the current seek position, and update the seek position
// accordingly.  Extend the file if necessary.  Returns the number of
// bytes written, or < 0 on error.
int
serve_write(envid_t envid, struct Fsreq_write *req)
{
	struct OpenFile *o;
	int r,n;
	if (debug)
		cprintf("serve_write %08x %08x %08x\n", envid, req->req_fileid, req->req_n);

	// LAB 5: Your code here.
	if((r = openfile_lookup(envid, req->req_fileid, &o)) < 0)
		return r;
	if(req->req_n > sizeof(req->req_buf))
		n = sizeof(req->req_buf);
	else
		n = req->req_n;
	r = file_write(o->o_file,req->req_buf,n,o->o_fd->fd_offset);
	if(r >= 0)
	{
		o->o_fd->fd_offset +=r;
	}
	return r;
	//panic("serve_write not implemented");
}
Example #27
0
File: serv.c Project: ichaos/jos
void
serve_map(envid_t envid, struct Fsreq_map *rq)
{
	int r;
	char *blk;
	struct OpenFile *o;
	int perm = PTE_U | PTE_P | PTE_AVAIL;
        uint32_t *pdiskbno;

	if (debug)
		cprintf("serve_map %08x %08x %08x\n", envid,
                        rq->req_fileid, rq->req_offset);

	// Map the requested block in the client's address space
	// by using ipc_send.
	// Map read-only unless the file's open mode (o->o_mode) allows writes
	// (see the O_ flags in inc/lib.h).

	// LAB 5: Your code here.
        r = openfile_lookup(envid, rq->req_fileid, &o);
        if (r<0) {
                cprintf("serv.c:serve_map--openfile_lookup failed.\n");
                ipc_send(envid, r, 0, 0);
                return;
        }
        r = file_get_block(o->o_file, rq->req_offset, &blk);
        if (r<0) {
                cprintf("serv.c:serve_map--file_get_walk failed.\n");
                goto out;
        }
        if (o->o_mode&O_RDWR) {//what happens if O_WRONLY?
                perm = PTE_USER;
        }
 out:
        ipc_send(envid, r, blk, perm);
	//panic("serve_map not implemented");
}
Example #28
0
// Read at most ipc->read.req_n bytes from the current seek position
// in ipc->read.req_fileid.  Return the bytes read from the file to
// the caller in ipc->readRet, then update the seek position.  Returns
// the number of bytes successfully read, or < 0 on error.
    int
serve_read(envid_t envid, union Fsipc *ipc)
{
	struct Fsreq_read *req = &ipc->read;
	struct Fsret_read *ret = &ipc->readRet;
        struct OpenFile *o;
        int r;

	if (debug)
		cprintf("serve_read %08x %08x %08x\n", envid, req->req_fileid, req->req_n);

	// Look up the file id, read the bytes into 'ret', and update
	// the seek position.  Be careful if req->req_n > PGSIZE
	// (remember that read is always allowed to return fewer bytes
	// than requested).  Also, be careful because ipc is a union,
	// so filling in ret will overwrite req.
	//
	// Hint: Use file_read.
	// Hint: The seek position is stored in the struct Fd.
	// LAB 5: Your code here
       size_t toReadBytes, numBytesRead;
       if (ipc->read.req_n > PGSIZE)
               toReadBytes = PGSIZE;
       else
               toReadBytes = req->req_n;


       if ((r = openfile_lookup(envid, req->req_fileid, &o)) < 0)
                return r;

//	cprintf("toReadBytes=%d r=%x ret=%x o->o_fd->fd_offset=%d\n",toReadBytes,r,ret,o->o_fd->fd_offset);
       numBytesRead = file_read(o->o_file, (void*)ret, toReadBytes, o->o_fd->fd_offset);
       o->o_fd->fd_offset+=numBytesRead;
       return numBytesRead;

	panic("serve_read not implemented");
}
Example #29
0
File: serv.c Project: Hisham-A/JOS
// Write req->req_n bytes from req->req_buf to req_fileid, starting at
// the current seek position, and update the seek position
// accordingly.  Extend the file if necessary.  Returns the number of
// bytes written, or < 0 on error.
int
serve_write(envid_t envid, struct Fsreq_write *req)
{
	if (debug)
		cprintf("serve_write %08x %08x %08x\n", envid, req->req_fileid, req->req_n);

	// LAB 5: Your code here.
	//panic("serve_write not implemented");
	struct OpenFile *o;
	int r;
	int bytes = req->req_n;
	char buf[512] = {0};

	if(bytes > PGSIZE)
		bytes = PGSIZE;
	if ((r = openfile_lookup(envid, req->req_fileid, &o)) < 0)
		return r;
	if((r = file_write(o->o_file, req->req_buf, bytes,o->o_fd->fd_offset)) >=0) {
		o->o_fd->fd_offset += r;
	}
	file_read(o->o_file, buf, bytes, 0);
		//o->o_fd->fd_offset += r;
	return r;
}
Example #30
0
// Read at most ipc->read.req_n bytes from the current seek position
// in ipc->read.req_fileid.  Return the bytes read from the file to
// the caller in ipc->readRet, then update the seek position.  Returns
// the number of bytes successfully read, or < 0 on error.
int
serve_read(envid_t envid, union Fsipc *ipc)
{
	struct Fsreq_read *req = &ipc->read;
	struct Fsret_read *ret = &ipc->readRet;
	struct OpenFile *o;
	int r;
	char buf[PGSIZE];

	if (debug)
		cprintf("serve_read %08x %08x %08x\n", envid, req->req_fileid, req->req_n);

	// Lab 5: Your code here:
	
	r = openfile_lookup(envid, req->req_fileid, &o);
	if(r<0) return r;

	//find read from curr offset
	r = file_read(o->o_file, ret->ret_buf, req->req_n, o->o_fd->fd_offset);
	if(r<0) return r;
	
	o->o_fd->fd_offset += r;
	return r;
}