예제 #1
0
파일: journal.c 프로젝트: alangenfeld/xv6
static void
journal_start()
{
  struct inode *ip;
  int i;
  struct t_start_blk start;
  uint state = READY;

  ip = iget(1, 3);
  ilock(ip);
  start.state = START;
  start.num_blks = b_index;
  for(i=0;i<b_index;i++){
    start.sector[i] = bp[i]->sector;
  }
  // write trans start blk 
  writei(ip, &start, 0, sizeof(start));
  
  // write data blks
  for(i=0;i<b_index;i++){
    writei(ip, bp[i]->data, (i*512) + 512, sizeof(bp[i]->data));
  }
  
  // journal valid
  writei(ip, &state, 0, sizeof(state));
  
  iunlock(ip);
}
예제 #2
0
static void
log_start()
{
  struct inode *ip;
  int i;
  struct trans_start start;
  uint state = LOGGING;

  ip = iget(1, 3);
  ilock(ip);
  start.state = READY;
  start.num_blks = b_index;
  for(i=0;i<b_index;i++){
    start.sector[i] = bp[i]->sector;
  }

  writei(ip, &start, 0, sizeof(start));

  for(i=0;i<b_index;i++){
    writei(ip, bp[i]->data, (i*512) + 512, sizeof(bp[i]->data));
  }

  writei(ip, &state, 0, sizeof(state));

  iunlock(ip);
}
예제 #3
0
uint8_t write_core_image(void)
{
	inoptr parent = NULLINODE;
	inoptr ino;

	udata.u_error = 0;

	/* FIXME: need to think more about the case sp is lala */
	if (uput("core", (uint8_t *)udata.u_syscall_sp - 5, 5))
		return 0;

	ino = n_open((char *)udata.u_syscall_sp - 5, &parent);
	if (ino) {
		i_deref(parent);
		return 0;
	}
	if (parent) {
		i_lock(parent);
		if ((ino = newfile(parent, "core")) != NULL) {
			ino->c_node.i_mode = F_REG | 0400;
			setftime(ino, A_TIME | M_TIME | C_TIME);
			wr_inode(ino);
			f_trunc(ino);
#if 0
	/* FIXME address ranges for different models - move core writer
	   for address spaces into helpers ?  */
			/* FIXME: need to add some arch specific header bits, and
			   also pull stuff like the true sp and registers out of
			   the return stack properly */

			corehdr.ch_base = pagemap_base;
			corehdr.ch_break = udata.u_break;
			corehdr.ch_sp = udata.u_syscall_sp;
			corehdr.ch_top = PROGTOP;
			udata.u_offset = 0;
			udata.u_base = (uint8_t *)&corehdr;
			udata.u_sysio = true;
			udata.u_count = sizeof(corehdr);
			writei(ino, 0);
			udata.u_sysio = false;
			udata.u_base = (uint8_t *)pagemap_base;
			udata.u_count = udata.u_break - pagemap_base;
			writei(ino, 0);
			udata.u_base = udata.u_sp;
			udata.u_count = PROGTOP - (uint32_t)udata.u_sp;
			writei(ino, 0);
#endif
			i_unlock_deref(ino);
			return W_COREDUMP;
		}
	}
	return 0;
}
예제 #4
0
파일: xwritef.c 프로젝트: Ryu-chan/CLIB
bool 		writef(
	float 	f
){
	int part;

	part = (int)f;
	if(!writei(part)) return FALSE;
	if(!writec('.'))  return FALSE;
	f-=part;
	part = ((int)(f*PRECISION));
	if(!writei(part)) return FALSE;
	return TRUE;
}
예제 #5
0
uint8_t write_core_image(void)
{
	inoptr parent = NULLINODE;
	inoptr ino;

	udata.u_error = 0;

	/* FIXME: need to think more about the case sp is lala */
	if (uput("core", udata.u_syscall_sp - 5, 5))
		return 0;

	ino = n_open(udata.u_syscall_sp - 5, &parent);
	if (ino) {
		i_deref(parent);
		return 0;
	}
	if (parent) {
		i_lock(parent);
		if (ino = newfile(parent, "core")) {
			ino->c_node.i_mode = F_REG | 0400;
			setftime(ino, A_TIME | M_TIME | C_TIME);
			wr_inode(ino);
			f_trunc(ino);

			/* FIXME: need to add some arch specific header bits, and
			   also pull stuff like the true sp and registers out of
			   the return stack properly */

			corehdr.ch_base = MAPBASE;
			corehdr.ch_break = udata.u_break;
			corehdr.ch_sp = udata.u_syscall_sp;
			corehdr.ch_top = PROGTOP;
			udata.u_offset = 0;
			udata.u_base = (uint8_t *)&corehdr;
			udata.u_sysio = true;
			udata.u_count = sizeof(corehdr);
			writei(ino, 0);
			udata.u_sysio = false;
			udata.u_base = MAPBASE;
			udata.u_count = udata.u_break - MAPBASE;
			writei(ino, 0);
			udata.u_base = udata.u_sp;
			udata.u_count = PROGTOP - (uint16_t)udata.u_sp;
			writei(ino, 0);
			i_unlock_deref(ino);
			return W_COREDUMP;
		}
	}
	return 0;
}
예제 #6
0
파일: fibonacci.c 프로젝트: dusek/army
void Fib(int argc, char **argv) {
    int x,y,z;

    if (argc < 2) {
        usage(stderr);
        exit();
    }

    int N = strtou32(argv[1]);
    
    x = 1;           //MOV R1, #1
    y = 0;           //MOV R2, #0
    if (N < 2)       //CMP R0, #2
        x = N;       //MOVMI R1, R0; BMI #5
    else {
        N -= 2;
        do {
            z = y;       //MOV R3, R2
            y = x;       //MOV R2, R1
            x = y + z;   //ADD R1, R2, R3
            N--;         //SUBS R0, R0, 1
        } while (N >= 0);//BPL #-6
    }

    writei(stdout, x);
    writes(stdout, "\n");
    exit();
}
예제 #7
0
파일: acc.c 프로젝트: MartinGeisse/vshg01
/*
 * On exit, write a record on the accounting file.
 */
void acct(void)
{
	register i;
	register struct inode *ip;
	off_t siz;

	if ((ip=acctp)==NULL)
		return;
	plock(ip);
	for (i=0; i<sizeof(acctbuf.ac_comm); i++)
		acctbuf.ac_comm[i] = u.u_comm[i];
	acctbuf.ac_utime = compress(u.u_utime);
	acctbuf.ac_stime = compress(u.u_stime);
	acctbuf.ac_etime = compress(time - u.u_start);
	acctbuf.ac_btime = u.u_start;
	acctbuf.ac_uid = u.u_ruid;
	acctbuf.ac_gid = u.u_rgid;
	acctbuf.ac_mem = 0;
	acctbuf.ac_io = 0;
	acctbuf.ac_tty = u.u_ttyd;
	acctbuf.ac_flag = u.u_acflag;
	siz = ip->i_size;
	u.u_offset = siz;
	u.u_base = (caddr_t)&acctbuf;
	u.u_count = sizeof(acctbuf);
	u.u_segflg = 1;
	u.u_error = 0;
	writei(ip);
	if(u.u_error)
		ip->i_size = siz;
	prele(ip);
}
예제 #8
0
파일: sys4.c 프로젝트: tangyi/UNIX-System-V
unlink()
{
	register *ip, *pp;
	extern uchar;

	pp = namei(&uchar, 2);
	if(pp == NULL)
		return;
	prele(pp);
	ip = iget(pp->i_dev, u.u_dent.u_ino);
	if(ip == NULL)
		panic("unlink -- iget");
	if((ip->i_mode&IFMT)==IFDIR && !suser())
		goto out;
	u.u_offset[1] =- DIRSIZ+2;
	u.u_base = &u.u_dent;
	u.u_count = DIRSIZ+2;
	u.u_dent.u_ino = 0;
	writei(pp);
	ip->i_nlink--;
	ip->i_flag =| IUPD;

out:
	iput(pp);
	iput(ip);
}
예제 #9
0
파일: dirent.c 프로젝트: Turingwy/turinux
int dirlink(struct inode *dp, char *dirname, uint32_t ino)
{
    if(!ISDIR(dp->mode) || ino < 1) {
        return -1;
    }
    
    if(dirlookup(dp, dirname, 0)) 
        return -1;

    struct dirent dir;
    int off;
    for(off = 0; off < dp->size; off+=sizeof(struct dirent))
    {
        if(!readi(dp, (char *)&dir, off, sizeof(struct dirent))) {
            return -1;
        }
        // found a new entry
        if(!dir.ino)
            break;
    }
    
    dir.ino = ino;
    strcpy(dir.name, dirname);
    writei(dp, (char *)&dir, off, sizeof(struct dirent));
    return 0;
}
예제 #10
0
파일: fs.c 프로젝트: gaohannk/xv6-system
// Write a new directory entry (name, inum) into the directory dp.
int
dirlink(struct inode *dp, char *name, uint inum)
{
  int off;
  struct dirent de;
  struct inode *ip;

  // Check that name is not present.
  if((ip = dirlookup(dp, name, 0)) != 0){
    iput(ip);
    return -1;
  }

  // Look for an empty dirent.
  for(off = 0; off < dp->size; off += sizeof(de)){
    if(readi(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
      panic("dirlink read");
    if(de.inum == 0)
      break;
  }

  strncpy(de.name, name, DIRSIZ);
  de.inum = inum;
  if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
    panic("dirlink");
  
  return 0;
}
예제 #11
0
static void
log_end()
{
  uint state = LOGGED;
  struct inode *ip;
  ip = iget(1, 3);
  ilock(ip);
  writei(ip, &state, 0, sizeof(state));
  iunlock(ip);
}
//PAGEBREAK!
// Write to file f.
int
filewrite(struct file *f, char *addr, int n) {
	int r;

	if (f->writable == 0) {
		return -1;
	}

	if (f->type == FD_PIPE) {
		return pipewrite(f->pipe, addr, n);
	}

	if (f->type == FD_INODE) {
		// write a few blocks at a time to avoid exceeding
		// the maximum log transaction size, including
		// i-node, indirect block, allocation blocks,
		// and 2 blocks of slop for non-aligned writes.
		// this really belongs lower down, since writei()
		// might be writing a device like the console.
		int max = ((LOGSIZE - 1 - 1 - 2) / 2) * 512;
		int i = 0;

		while (i < n) {
			int n1 = n - i;

			if (n1 > max) {
				n1 = max;
			}

			begin_trans();
			ilock(f->ip);

			if ((r = writei(f->ip, addr + i, f->off, n1)) > 0) {
				f->off += r;
			}

			iunlock(f->ip);
			commit_trans();

			if (r < 0) {
				break;
			}

			if (r != n1) {
				panic("short filewrite");
			}

			i += r;
		}

		return i == n ? n : -1;
	}

	panic("filewrite");
}
예제 #13
0
파일: proc.c 프로젝트: yonatana/OS
int
kernel_unlink(char* path)
{
  struct inode *ip, *dp;
  struct dirent de;
  char name[DIRSIZ];
  uint off;

//   if(argstr(0, &path) < 0)
//     return -1;
  if((dp = nameiparent(path, name)) == 0)
    return -1;

  begin_trans();

  ilock(dp);

  // Cannot unlink "." or "..".
  if(namecmp(name, ".") == 0 || namecmp(name, "..") == 0)
    goto bad;

  if((ip = dirlookup(dp, name, &off)) == 0)
    goto bad;
  ilock(ip);

  if(ip->nlink < 1)
    panic("unlink: nlink < 1");
  /*
  if(ip->type == T_DIR && !isdirempty(ip)){
    iunlockput(ip);
    goto bad;
  }
*/
  memset(&de, 0, sizeof(de));
  if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
    panic("unlink: writei");
  if(ip->type == T_DIR){
    dp->nlink--;
    iupdate(dp);
  }
  iunlockput(dp);

  ip->nlink--;
  iupdate(ip);
  iunlockput(ip);

  commit_trans();

  return 0;

bad:
  iunlockput(dp);
  commit_trans();
  return -1;
}
예제 #14
0
void
log_initialize()
{
  struct inode *ip;
  uchar buffer[512];
  int i, j;
  struct buf *bp;
  struct trans_start t_blk;

  for(j=0;j<512;j++)
    buffer[j] = 0;

  ip = iget(1, 3);
  ilock(ip);
  if(ip->size < 512*20){
    cprintf("Creating Journal...\n");
    for(i=0;i<20;i++){
      writei(ip, buffer, i*512, sizeof(buffer));
    }
  }
  else {
    readi(ip, &t_blk, 0, sizeof(t_blk));

    if(t_blk.state == LOGGING){
      cprintf("Inconsistency found in file system. Attempting to recover.\n");

      for(i = 0; i < t_blk.num_blks; i++){
    	  readi(ip, buffer, i*512+512, sizeof(buffer));
    	  bp = bread(1, t_blk.sector[i]);
    	  cprintf("Recovering data in sector: %d\n", t_blk.sector[i]);
    	  memmove(bp->data, buffer, sizeof(buffer));
    	  bwrite(bp);
    	  brelse(bp);
      }

      writei(ip, buffer, 0, sizeof(buffer));
      cprintf("Recovery Complete.\n");
    }
  }

  iunlock(ip);
}
예제 #15
0
파일: journal.c 프로젝트: alangenfeld/xv6
static void
journal_end()
{
  uint state = END;
  struct inode *ip;
  ip = iget(1, 3);
  ilock(ip);
  // write trans start blk 
  writei(ip, &state, 0, sizeof(state));
  iunlock(ip);
}
예제 #16
0
파일: journal.c 프로젝트: alangenfeld/xv6
void
j_init()
{
  struct inode *ip;
  uchar buffer[512];
  int i, j;
  struct buf *bp;
  struct t_start_blk t_blk;
  
  for(j=0;j<512;j++)
    buffer[j] = 0;
  
  ip = iget(1, 3);
  ilock(ip);
  if(ip->size < 512*20){ 
    cprintf("alloc journal\n");
    // allocate journal if too small
    for(i=0;i<20;i++){
      writei(ip, buffer, i*512, sizeof(buffer));
    }
  }
  else {
    readi(ip, &t_blk, 0, sizeof(t_blk));
    // check if there is a valid not commited transaction
    if(t_blk.state == READY){
      cprintf("~~~~~~~~~~RECOVERING!!!!!~~~~~~~~~~\n");
      for(i = 0; i < t_blk.num_blks; i++){
	readi(ip, buffer, i*512+512, sizeof(buffer));
	bp = bread(1, t_blk.sector[i]);
	cprintf("recovering sector %d\n", t_blk.sector[i]);
	memmove(bp->data, buffer, sizeof(buffer));
	bwrite(bp);
	brelse(bp);
      }
      writei(ip, buffer, 0, sizeof(buffer));
      cprintf("~~~~~RECOVERY COMPLETE~~~~~~~~\n");
    }
  }
  
  iunlock(ip);
}
예제 #17
0
void Node::write2Blk(Block &blk, int typeId, int strLen, BufferManager* bufferManager) {
    char* mem = blk.data;
    
    mem[0] = isleaf; mem[1] = isroot;
    mem += 2*sizeof(char);
    writei(getPSize(), mem), writei(getKSize(), mem), writei(typeId, mem), writei(strLen, mem);
    
    std::cout << "getPSize()" << getPSize() <<std::endl;
    std::cout << "getKSize()" << getKSize() <<std::endl;
    
    for( int i = 0; i < getPSize(); i++ ) {
        std::cout << "P[i]" << P[i] << std::endl;
        writei(P[i], mem);
    }
    
    for( int i = 0; i < getKSize(); i++ ) {
        std::cout << "K[i]" << K[i].datai << std::endl;
        if( typeId == 0 ) {
            writei(K[i].datai, mem);
        }
        else if( typeId == 1 ) {
            writef(K[i].dataf, mem);
        }
        else if( typeId == 2 ) {
            writes(K[i].datas, strLen, mem);
        }
    }
    
    std::cout << "Node::write2Blk " << blk.pos/BLOCK_SIZE << std::endl;
    
    //BufferManager bufferManager;
    bufferManager->writeBlock(blk);
}
예제 #18
0
/*
 * Unlink system call.
 * Hard to avoid races here, especially
 * in unlinking directories.
 */
unlink()
{
	register struct inode *ip, *pp;
	struct a {
		char	*fname;
	};

	pp = namei(uchar, 2);
	if(pp == NULL)
		return;
	/*
	 * Check for unlink(".")
	 * to avoid hanging on the iget
	 */
	if (pp->i_number == u.u_dent.d_ino) {
		ip = pp;
		ip->i_count++;
	} else
		ip = iget(pp->i_dev, u.u_dent.d_ino);
	if(ip == NULL)
		goto out1;
	if((ip->i_mode&IFMT)==IFDIR && !suser())
		goto out;
	/*
	 * Don't unlink a mounted file.
	 */
	if (ip->i_dev != pp->i_dev) {
		u.u_error = EBUSY;
		goto out;
	}
	if (ip->i_flag&ITEXT)
		xrele(ip);	/* try once to free text */
	if (ip->i_flag&ITEXT && ip->i_nlink==1) {
		u.u_error = ETXTBSY;
		goto out;
	}
	u.u_offset -= sizeof(struct direct);
	u.u_base = (caddr_t)&u.u_dent;
	u.u_count = sizeof(struct direct);
	u.u_dent.d_ino = 0;
	writei(pp);
	ip->i_nlink--;
	ip->i_flag |= ICHG;

out:
	iput(ip);
out1:
	iput(pp);
}
예제 #19
0
파일: syscall_fs.c 프로젝트: Abioy/FUZIX
int16_t _write(void)
{
	inoptr ino;
	uint8_t flag;

	if (!valaddr(buf, nbytes))
	        return -1;
	/* Set up u_base, u_offset, ino; check permissions, file num. */
	if ((ino = rwsetup(false, &flag)) == NULLINODE)
		return (-1);	/* bomb out if error */

	writei(ino, flag);
	updoff();

	return (udata.u_count);
}
예제 #20
0
파일: file.c 프로젝트: tmfahey/cs537
// Write to file f.  Addr is kernel address.
int
filewrite(struct file *f, char *addr, int n)
{
  int r;

  if(f->writable == 0)
    return -1;
  if(f->type == FD_PIPE)
    return pipewrite(f->pipe, addr, n);
  if(f->type == FD_INODE){
    ilock(f->ip);
    if((r = writei(f->ip, addr, f->off, n)) > 0)
      f->off += r;
    iunlock(f->ip);
    return r;
  }
  panic("filewrite");
}
예제 #21
0
파일: file.c 프로젝트: SteveXiSong/cs537
// Write to file f.  Addr is kernel address.
int
filewrite(struct file *f, char *addr, int n)
{
	//cprintf("filewrite...\n");
  int r;
  //verified
  if(f->writable == 0)
    return -1;
  if(f->type == FD_PIPE)
    return pipewrite(f->pipe, addr, n);
  if(f->type == FD_INODE){
    ilock(f->ip);
    if((r = writei(f->ip, addr, f->off, n)) > 0)
      f->off += r;
    iunlock(f->ip);
		//cprintf("filwrite: r(%d)\n");
    return r;
  }
  panic("filewrite");
}
예제 #22
0
파일: file.c 프로젝트: hosanli/os4
// Write to file f.  Addr is kernel address.
int
filewrite(struct file *f, char *addr, int n)
{
//	cprintf(" -- filewrite -- f-type %d\n", f->type);
  int r;

  if(f->writable == 0)
    return -1;
  if(f->type == FD_PIPE)
    return pipewrite(f->pipe, addr, n);
  if(f->type == FD_INODE){
    ilock(f->ip);
    if((r = writei(f->ip, addr, f->off, n)) > 0)
      f->off += r;
    iunlock(f->ip);
//	cprintf(" -- filewrite -- after iunlock \n");
    return r;
  }
  panic("filewrite");
}
예제 #23
0
void acctexit(ptptr p)
{
	struct oft *oftp;
	inoptr ino;

	if (acct_fh == -1)
		return;

	oftp = &of_tab[acct_fh];
	ino = oftp->o_inode;

	/* Reuse the field before we write out accounting data */
	udata.u_mask = p->p_uid;
	/* More useful information is u_top */
	udata.u_break = udata.u_top;

	udata.u_sysio = true;
	udata.u_base = (char *) &udata.u_mask;
	udata.u_count = 48;	/* Includes some spare for expansion */

	/* Append to the end of the file */
	oftp->o_ptr = ino->c_node.i_size;
	writei(ino, 0);
}
예제 #24
0
파일: filesys.c 프로젝트: ljalves/FUZIX
bool ch_link(inoptr wd, char *oldname, char *newname, inoptr nindex)
{
    struct direct curentry;
    int i;

    if(!(getperm(wd) & OTH_WR))
    {
        udata.u_error = EPERM;
        return false;
    }
    /* Inserting a new blank entry ? */
    if (!*newname && nindex != NULLINODE) {
        udata.u_error = EEXIST;
        return false;
    }

    /* Search the directory for the desired slot. */

    udata.u_offset = 0;

    for(;;)
    {
        udata.u_count = DIR_LEN;
        udata.u_base  =(char *)&curentry;
        udata.u_sysio = true;
        readi(wd, 0);

        /* Read until EOF or name is found.  readi() advances udata.u_offset */
        if(udata.u_count == 0 || namecomp(oldname, curentry.d_name))
            break;
    }

    if(udata.u_count == 0 && *oldname)
        return false;                  /* Entry not found */

    memcpy(curentry.d_name, newname, FILENAME_LEN);
    // pad name with NULLs
    for(i = 0; i < FILENAME_LEN; ++i)
        if(curentry.d_name[i] == '\0')
            break;
    for(; i < FILENAME_LEN; ++i)
        curentry.d_name[i] = '\0';

    if(nindex)
        curentry.d_ino = nindex->c_num;
    else
        curentry.d_ino = 0;

    /* If an existing slot is being used, we must back up the file offset */
    if(udata.u_count){
        udata.u_offset -= DIR_LEN;
    }

    udata.u_count = DIR_LEN;
    udata.u_base  = (char*)&curentry;
    udata.u_sysio = true;
    writei(wd, 0);

    if(udata.u_error)
        return false;

    setftime(wd, A_TIME|M_TIME|C_TIME);     /* Sets c_dirty */

    /* Update file length to next block */
    if(wd->c_node.i_size & BLKMASK)
        wd->c_node.i_size += BLKSIZE - (wd->c_node.i_size & BLKMASK);

    return true; // success
}
예제 #25
0
파일: prec2.c 프로젝트: dusek/army
int main (int argc, char *argv[]) {
	int poradi, poradi_zpetne;
	int i;

	int * permutace;

	int digits = 0;
	int power_of_ten = 1;

	int permutace_len;
	int permutace_count;
	char * tail;

	if (argc == 1)
		permutace_len = 9;
	else if (argc == 2)
		permutace_len = (int) strtou32 (argv[1]);
	else {
		writes (stderr, "Pouziti: prec <delka permutace>\n");
        exit();
		return 1;
	}

	permutace_count = faktorial (permutace_len);

	/**
	 * Spocteme pocet cislic v desitkovem zapisu poctu permutaci,
	 * abychom mohli zarovnat vypis
	 */
	//while (permutace_count > power_of_ten) {
	//	power_of_ten *= 10;
	//	digits++;
	//}

	/**
	 * Test konzistence indexovani permutaci hrubou silou. 
	 *
	 * Projdeme vsechna poradi, dekodujeme permutaci, vytiskneme permutaci,
	 * permutaci zpet zakodujeme na poradi a porovname toto poradi
	 * s puvodnim. Jestli se lisi, tiskneme na konci radku "!!!FAIL!!!",
	 * jestli ne, tak "OK".
	 */
    permutace = (int *) 0x90000000;
	for (poradi = 1; poradi <= permutace_count; poradi++) {
		writes (stderr, "Permutace ");
        writei (stderr, poradi);
		writes (stderr, ": ");

  	    poradi_na_permutace (poradi, permutace, permutace_len);
		PRINT_INT_ARRAY (permutace, permutace_len, i);

		poradi_zpetne = permutace_na_poradi (permutace, permutace_len);
		if (poradi_zpetne == poradi)
			writes (stderr, " OK");
		else {
			writes (stderr, " FAILED, permutace_na_poradi vratila ");
            writei (stderr, poradi_zpetne);
        }
		writes (stderr, "\n");
	}
	
    exit();
	return 0;
}