Example #1
0
int simple_op_local(unsigned int type, const char* path1, off_t offset,
              mode_t mode1, const char *opt_path, char *s_addr)
{
    if (type == COREFS_SIMPLE_RMDIR) {
        server_data *sd=NULL;
        int sdret = check_setup(s_addr, &sd);
        if (sdret == -EHOSTUNREACH) return sdret;
        if (sdret < 0) return -EIO;

        int ret;
        struct stat st;
        char dir_path[MAXPATH+LOCALSIZE];
        sprintf(dir_path, "%s/mnt/%s", local_path, s_addr);
        int stret = stat(dir_path, &st);
        if (stret >= 0) {
            if ((ret = rmdir(dir_path))) {
                perror("Error deleting directory of server");
            }
        }

        shutdown(sd->g_mctx.sock, SHUT_RDWR);
        close(sd->g_mctx.sock);
        delete_server_data(sd);
        remove_info_ptr(&head, &tail, (void *)sd);
        return ret;
    }
    
    return -EACCES;
}
Example #2
0
void RGFlow<Two_scale>::solve()
{
   check_setup();

   unsigned int max_iterations = get_max_iterations();
   if (models.empty() || max_iterations == 0)
      return;

   initial_guess();

   iteration = 0;
   bool accuracy_reached = false;
   while (iteration < max_iterations && !accuracy_reached) {
      update_running_precision();
      run_up();
      run_down();
      accuracy_reached = accuracy_goal_reached();
      ++iteration;
   }

   apply_lowest_constraint();

   if (!accuracy_reached)
      throw NoConvergenceError(max_iterations);

   VERBOSE_MSG("convergence reached after " << iteration << " iterations");
}
Example #3
0
int i_rad_display(const char* input) {
	int status = -1;
	if ((status = check_setup()) != 0) {
		return status;
	}

	signed char i = 0; // char position
	unsigned char j; // register counter

	i = strlen(input) - 1;
	j = 0;
	for(; i >= 0; i--) {
		printf("%c ", (char) input[i]); 
		switch(input[i]) {
			case '-': REGISTER[j++] |= 0xa; break;
			case 'e':
                        case 'E': REGISTER[j++] |= 0xb; break;
			case 'h':
                        case 'H': REGISTER[j++] |= 0xc; break;
			case 'l':
                        case 'L': REGISTER[j++] |= 0xd; break;
			case 'p':
                        case 'P': REGISTER[j++] |= 0xe; break;
			case ' ': REGISTER[j++] |= 0xf; break;
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9': REGISTER[j++] |= (input[i] - 48); break;
			case '.': REGISTER[j] = 0x80; break; // set dot
			default:
				printf("Error value '%s'!\n", input[i]);
				exit(1);
				break;
		}
	
		if( j > DIGITS + 1 ) {
			printf("Input longer then expected!\n");
			exit(1);
		}
	}


  	Init7219( DIGITS, MODE_B, 8 );

	for(i = 0; i<8; i++)
	{
		printf("REGISTER[%d] = '%#x'\n", i, REGISTER[i]);
		MAX7219Send(i+1, REGISTER[i]);
	}

	return 0;
}
Example #4
0
int i_rad_light(int value) {
	int status = -1;
	if ((status = check_setup()) != 0) {
		return status;
	}

	pinMode(LIGHT_PIN, 1);
  	digitalWrite(LIGHT_PIN, value);
	return 0;
}
Example #5
0
/** Remove extended attributes */
int do_removexattr (const char * path, const char *name){
    char buffer[BUFFERSIZE];
    memset(buffer, 0, BUFFERSIZE);
    corefs_packet* packet = (corefs_packet*)buffer;
    int ret;
    unsigned int packet_size;
    server_data *sd=NULL;

    char r_path_[MAXPATH];
    char s_addr[MAXADDR];
    char *r_path = &r_path_[0];
    remove_addr(path, &r_path, s_addr);
    if (r_path[0]=='\0') {
        //there was no path other than the first '/'
        //ie this is for the root directory
        return do_removexattr_local(path, name);
    }
    ret = check_setup(s_addr, &sd);
    if (ret == -EHOSTUNREACH) return -EHOSTUNREACH;
    if (ret < 0) return -EIO;

#ifdef DEBUG
    dprintf(stderr, "REMOVEXATTR: path[%s] name[%s]\n", path, name);
#endif

    /* Get user info from the upper layer */
    if(my_ops.up_get_user_info)
        my_ops.up_get_user_info(&(packet->payload.request.user_ids),
                                path, NULL);

    /* Build the request packet */
    packet_size = build_xattr(packet, COREFS_XATTR_REMOVEXATTR, name,
                              r_path, 0, 0);

    /* encapsulate in network order */
    char request_buf[packet_size];
    memset(request_buf, 0, packet_size);
    ret = encap_corefs_header(request_buf, packet);
    encap_corefs_request(request_buf + ret, packet);
#ifdef DEBUG_NETWORK
    fprintf(stderr, "printing removexattr request\n");
    print_packet(*packet);
#endif 
    if (send_packet(sd->ctx, request_buf, packet_size) <=0) {
        dprintf(stderr, "error sending packet.\n");
        return -EIO;
    }
    memset(buffer, 0, BUFFERSIZE);
    if ((ret = client_receive_specified(sd->ctx, buffer,
                                        COREFS_RESPONSE_STATUS)) < 0) {
        dprintf(stderr, "GETXATTR: error receiving removexattr status.\n");
        return ret;
    }
    return 0;
}
Example #6
0
/*
 * We suck in lots of fsck code, and just pick & choose the stuff we want.
 *
 * fsreadfd is set up to read from the file system, fswritefd to write to
 * the file system.
 */
int
main(int argc, char *argv[])
{
    int ch, rval;
    char *fsys = NULL;

    while (-1 != (ch = getopt(argc, argv, "fdr"))) {
        switch (ch) {
        case 'f':
            /* The -f option is left for historical
             * reasons and has no meaning.
             */
            break;
        case 'd':
            check_debug++;
            break;
        case 'r':
            nflag++; /* "no" in fsck, readonly for us */
            break;
        default:
            usage();
        }
    }
    argc -= optind;
    argv += optind;
    if (argc != 1)
        usage();
    else
        fsys = argv[0];

    check_sblock_init();
    if (!check_setup(fsys, 0))
        errx(1, "cannot set up file system `%s'", fsys);
    printf("%s file system `%s'\nLast Mounted on %s\n",
        nflag? "Examining": "Editing", fsys, check_sblk.b_un.b_fs->fs_fsmnt);
    rval = cmdloop();
    if (!nflag) {
        check_sblk.b_un.b_fs->fs_clean = 0;	/* mark it dirty */
        dirty(&check_sblk);
        check_finish(0);
        printf("*** FILE SYSTEM MARKED DIRTY\n");
        printf("*** BE SURE TO RUN FSCK TO CLEAN UP ANY DAMAGE\n");
        printf("*** IF IT WAS MOUNTED, RE-MOUNT WITH -u -o reload\n");
    }
    exit(rval);
}
Example #7
0
/** List extended attributes */
int do_listxattr (const char * path, char * list, size_t size){
    char buffer[BUFFERSIZE];
    memset(buffer, 0, BUFFERSIZE);
    corefs_packet* packet = (corefs_packet*)buffer;
    int ret;
    unsigned int packet_size;
    server_data *sd=NULL;
    
    char r_path_[MAXPATH];
    char s_addr[MAXADDR];
    char *r_path = &r_path_[0];
    remove_addr(path, &r_path, s_addr);
    if (r_path[0]=='\0') {
        //there was no path other than the first '/'
        //ie this is for the root directory
        return do_listxattr_local(path, list, size);
    }
    
    ret = check_setup(s_addr, &sd);
    if (ret == -EHOSTUNREACH) return -EHOSTUNREACH;
    if (ret < 0) return -EIO;
    
#ifdef DEBUG
    dprintf(stderr, "LISTXATTR: path[%s] : requested list of max size[%d]\n",
            path,size);
#endif

    /* Get user info from the upper layer */
    if(my_ops.up_get_user_info)
        my_ops.up_get_user_info(&(packet->payload.request.user_ids),
                                path, NULL);

    /* Build the request packet */
    packet_size = build_xattr(packet, COREFS_XATTR_LISTXATTR, NULL,
                              r_path, size, 0);

    /* encapsulate in network order */
    char request_buf[packet_size];
    memset(request_buf, 0, packet_size);
  
    ret = encap_corefs_header(request_buf, packet);
    encap_corefs_request(request_buf + ret, packet);
#ifdef DEBUG_NETWORK
    fprintf(stderr, "printing listxattr request\n");
    print_packet(*packet);
#endif 
    if (send_packet(sd->ctx, request_buf, packet_size) <=0) {
        dprintf(stderr, "error sending packet.\n");
        return -EIO;
    }
    memset(buffer, 0, BUFFERSIZE);
    if(size == 0){
        if ((ret=client_receive_specified(sd->ctx, buffer,
                                          COREFS_RESPONSE_STATUS)) < 0) {
            dprintf(stderr, "LISTXATTR: error receiving listxattr size.\n");
        }
        return ret;
    }
    else{
        if ((ret=client_receive_specified(sd->ctx, buffer,
                                          COREFS_RESPONSE_DATA)) <= 0) {
            dprintf(stderr, "LISTXATTR: error receiving listxattr value.\n");
            return ret;
        }
    }

    /* Estimate the returned data size */
    corefs_packet * reply = (corefs_packet*)buffer;
    ret = reply->header.payload_size -
        RESPONSE_BASE_SIZE(reply->payload.response);
    memcpy(list, reply->payload.response.rop.raw, ret);
  
    return ret;

}
Example #8
0
int do_getattr(const char* path, struct stat *stbuf)
{
    server_data *sd=NULL;
    int ret;
    int packet_size = 0;
    corefs_packet packet;
    memset(&packet, 0, sizeof(packet));
 
#ifdef DEBUG
    dprintf(stderr, "GETATTR: %s \n", path);
#endif

    char r_path_[MAXPATH];
    char s_addr[MAXADDR];
    char *r_path = &r_path_[0];
    remove_addr(path, &r_path, s_addr);
    if (r_path[0]=='\0') {
        //there was no path other than the first '/'
        //ie this is for the root directory
        return do_getattr_local(path,stbuf);
    }
    ret = check_setup(s_addr, &sd);
    if (ret == -EHOSTUNREACH) return -EHOSTUNREACH;
    if (ret < 0) return -EIO;
  
    /* Get user info from the upper layer */
    if(my_ops.up_get_user_info)
        my_ops.up_get_user_info(&(packet.payload.request.user_ids), path, NULL);

    /* Create the request */
    packet_size = build_fileop(&packet, COREFS_REQUEST_GETATTR, 0, 0, r_path);

    /* Encap the request */
    char buffer[packet_size];

    ret = encap_corefs_header(buffer, &packet);
    
    encap_corefs_request(buffer + ret, &packet);
 
#ifdef DEBUG_NETWORK
    fprintf(stderr, "getattr request\n");
    print_packet(packet);
#endif
    /* Send the request */
    if (send_packet(sd->ctx, buffer, packet_size) <=0) {
        dprintf(stderr, "error sending packet.\n");
        return -EIO;
    }
  
    /* Receive the response */
    memset(buffer, 0, packet_size);
    if ((ret=client_receive_specified(sd->ctx, buffer, COREFS_RESPONSE_ATTR))
        <= 0) {
        dprintf(stderr, "GETATTR: error receiving attr data for file %s.\n",
                path);
        return ret;
    }
    corefs_packet * reply  =(corefs_packet*)buffer;
  
    memset(stbuf, 0, sizeof(struct stat));
    stbuf->st_mode  = reply->payload.response.rop.attr.mode;
    stbuf->st_uid   = reply->payload.response.rop.attr.uid;
    stbuf->st_gid   = reply->payload.response.rop.attr.gid;
    stbuf->st_size  = (unsigned int) reply->payload.response.rop.attr.size;
    stbuf->st_mtime = reply->payload.response.rop.attr.mtime;
    stbuf->st_atime = reply->payload.response.rop.attr.atime;
    stbuf->st_ctime = reply->payload.response.rop.attr.ctime;
    stbuf->st_nlink = reply->payload.response.rop.attr.nlinks;
  
    return 0;
	
}
Example #9
0
int do_readdir(const char* path, void* buf, fuse_fill_dir_t filler,
               off_t offset, struct fuse_file_info *fi)
{
    char buffer[BUFFERSIZE];
    unsigned int packet_size;
    int ret;
    server_data *sd=NULL;    

    char* dirinfo;
    corefs_packet req_packet;
    memset(&req_packet, 0, sizeof(req_packet));
  
    /* Get user info from the upper layer */
    if(my_ops.up_get_user_info)
        my_ops.up_get_user_info(&(req_packet.payload.request.user_ids),
                                path, NULL);
  
    char r_path_[MAXPATH];
    char s_addr[MAXADDR];
    char *r_path = &r_path_[0];
    ret = remove_addr(path, &r_path, s_addr);
    if (r_path[0]=='\0') {
        //there was no path other than the first '/'
        //ie this is for the root directory
        return do_readdir_local(path, buf, filler, offset, fi);
    }

    ret = check_setup(s_addr, &sd);
    if (ret == -EHOSTUNREACH) return -EHOSTUNREACH;
    if (ret < 0) return -EIO;

    while(1) {
        memset(buffer, 0, BUFFERSIZE);
        packet_size = build_fileop(&req_packet, COREFS_REQUEST_READDIR,
                                   offset, 0, r_path);
    
        /* Encapsulate the request */
        ret = encap_corefs_header(buffer, &req_packet);
        encap_corefs_request(buffer + ret, &req_packet);

#ifdef DEBUG
        dprintf(stderr, "READDIR: %s : %llu \n", path, offset);
#endif
    
#ifdef DEBUG_NETWORK
        fprintf(stderr,"readdir request\n");
        print_packet(req_packet);
#endif
    
        if (send_packet(sd->ctx, buffer, packet_size) <=0) {
            dprintf(stderr, "error sending packet.\n");
            return -EIO;
        }
        memset(buffer, 0, BUFFERSIZE);
        if ((ret = client_receive_specified(sd->ctx, buffer,
                                            COREFS_RESPONSE_DATA)) <= 0) {
            dprintf(stderr, "error receiving readdir data.\n");
            return ret;
        }
        
        // use same buffer for reply
        corefs_packet* reply=(corefs_packet*)buffer; 
        dirinfo = reply->payload.response.rop.raw;
    
        /*  Loop and insert all dirents in the filler func */
        while (dirinfo < buffer + header_size + ret) {
            char type;
            char* name;
            struct stat st;
            memset(&st, 0, sizeof(st));
            type = *dirinfo;
            name = (++dirinfo);
      
            st.st_mode = type << 12;
            if(filler(buf, name, &st, 0)) {
                /* FUSE buffer full, lets return */
                return 0;
            }
            dirinfo += strlen(name) + 1;
        }

        if(reply->payload.response.type == COREFS_RESPONSE_MOREDATA){
      
            /*  According to the server, the previous readdir was
             *  incomplete. Lets reissue readdir with the server provided
             *  offset. */
            offset = reply->payload.response.more_offset;
        }
        else /* readdir complete */
            break;
    }
  
    return 0;
}
Example #10
0
int do_write(const char* path, const char* buf, size_t size, off_t offset,
             struct fuse_file_info* fi)
{

    char buffer[BUFFERSIZE];
    memset(buffer, 0, BUFFERSIZE);
    corefs_packet req_packet;
    memset(&req_packet, 0, sizeof(req_packet));
    int ret;
    unsigned int packet_size;
    server_data *sd=NULL;

    /* Get user info from the upper layer */
    if(my_ops.up_get_user_info)
        my_ops.up_get_user_info(&(req_packet.payload.request.user_ids),
                                path, NULL);
    
    char r_path_[MAXPATH];
    char s_addr[MAXADDR];
    char *r_path = &r_path_[0];
    remove_addr(path, &r_path, s_addr);
    if (r_path[0]=='\0') {
        //there was no path other than the first '/'
        //ie this is for the root directory
        return do_write_local(path, buf, size, offset, fi);
    }

    ret = check_setup(s_addr, &sd);
    if (ret == -EHOSTUNREACH) return -EHOSTUNREACH;
    if (ret < 0) return -EIO;


    /* Send the write command first */
    /* Prepare request */
    packet_size = build_fileop(&req_packet, COREFS_REQUEST_WRITE, offset,
                               size, r_path);

    /* Encapsualte the request */
    ret = encap_corefs_header(buffer, &req_packet);
    encap_corefs_request(buffer + ret, &req_packet);

#ifdef DEBUG
    dprintf(stderr, "WRITE: %s : %llu : %u\n", path, offset, size);
#endif
#ifdef DEBUG_NETWORK
    fprintf(stderr,"write request\n");
    print_packet(req_packet);
#endif
  
    if (send_packet(sd->ctx, buffer, packet_size) <=0) {
        dprintf(stderr, "error sending packet.\n");
        return -EIO;
    }

    /* Now send the data */
    packet_size =  build_request_data((corefs_packet*) buffer, buf, size);
    char out_buffer[packet_size];
    ret = encap_corefs_header(out_buffer,(corefs_packet*)buffer);
    encap_corefs_request(out_buffer + ret, (corefs_packet*)buffer);
  
    if (send_packet(sd->ctx, out_buffer, packet_size) <=0) {
        dprintf(stderr, "error sending packet.\n");
        return -EIO;
    }
    memset(buffer, 0, BUFFERSIZE);
    if ((ret=client_receive_specified(sd->ctx, buffer, COREFS_RESPONSE_STATUS))
        < 0) {
        dprintf(stderr, "write returned error.\n");
        return ret;
    }
    return size;
}
Example #11
0
int do_read(const char* path, char* buf, size_t size, off_t offset,
            struct fuse_file_info* fi)
{

    char buffer[BUFFERSIZE];
    corefs_packet req_packet;
    memset(&req_packet, 0, sizeof(req_packet));
    int ret;
    unsigned int packet_size;
    server_data *sd=NULL;
    
    /* Get user info from the upper layer */
    if(my_ops.up_get_user_info)
        my_ops.up_get_user_info(&(req_packet.payload.request.user_ids),
                                path, NULL);
    
    char r_path_[MAXPATH];
    char s_addr[MAXADDR];
    char *r_path = &r_path_[0];
    remove_addr(path, &r_path, s_addr);
    if (r_path[0]=='\0') {
        //there was no path other than the first '/'
        //ie this is for the root directory
        return do_read_local(path, buf, size, offset, fi);
    }

    ret = check_setup(s_addr, &sd);
    if (ret == -EHOSTUNREACH) return -EHOSTUNREACH;
    if (ret < 0) return -EIO;
    
    memset(buffer, 0, BUFFERSIZE);
    /* Prepare request */
    packet_size = build_fileop(&req_packet, COREFS_REQUEST_READ, offset,
                               size, r_path);

    /* Encapsualte the request */
    ret = encap_corefs_header(buffer, &req_packet);
    encap_corefs_request(buffer + ret, &req_packet);

#ifdef DEBUG
    dprintf(stderr, "READ: %s : %llu : %u\n", path, offset, size);
#endif
#ifdef DEBUG_NETWORK
    fprintf(stderr,"read request\n");
    print_packet(req_packet);
#endif
  
    if (send_packet(sd->ctx, buffer, packet_size) <=0) {
        dprintf(stderr, "error sending packet.\n");
        return -EIO;
    }
    /* Buffer will contain the received packet in the correct host format */
    memset(buffer, 0, BUFFERSIZE);
    if ((ret = client_receive_specified(sd->ctx, buffer, COREFS_RESPONSE_DATA))
        <= 0) {
        dprintf(stderr, "error receiving read data.\n");
        return ret;
    }
    /* Estimate the returned data size */
    corefs_packet * reply = (corefs_packet*)buffer;
  
    ret = reply->header.payload_size -
        RESPONSE_BASE_SIZE(reply->payload.response);
    memcpy(buf, reply->payload.response.rop.raw, ret);
  
    return ret;
}
Example #12
0
int parse_arguments(int argc, char** argv,char * config_path)
{
    int ch, option_index=0;
    const char argstr[]="P:o:S:dsh";
    //extern char* optarg;
    int i;
    char g_server_addr[4];
    memset(g_server_addr, 0, 4);

    opterr=0; // ignore unknown options

    static struct option long_options[] = {
        {"server", 1, 0, 'S'},
        {"port", 1, 0, 'P'},
        {"debug", 0, 0, 'd'},
        {"fuse_arg", 1, 0, 'o'},
        {"single_threaded", 0, 0, 's'},
        {"help", 0, 0, 'h'},
        {0,0,0,0}
    };

    add_fuse_arg(argv[0]);
    add_fuse_arg("-s"); // For now, must run FUSE single-threaded!
    /* Next two lines cause FUSE to run in direct_io mode.
       Useful for benchmarking, but doesn't allow execution of files */
    //add_fuse_arg("-o");
    //add_fuse_arg("direct_io");
  
    /*  Check if upper layer has got all of its command line args */
    if(my_ops.up_parse_arguments){    
        if(my_ops.up_parse_arguments((char*)argstr, argc, argv) != PROCEED) {
            usage(argv[0]);
            exit(1);
        }
    }

    optind = 0;
  
    while ((ch=getopt_long(argc, argv, argstr, long_options, &option_index)) != -1) {
        switch (ch) {
        case 'S': {
            char server_addr[MAXADDR];
            strncpy(server_addr, optarg, MAXADDR);
            int ret = check_setup(server_addr, NULL);
            if (ret == -EHOSTUNREACH) return -EHOSTUNREACH;
            if (ret < 0) return -EIO;
            
            break;
        }
        case 'P': 
            if(optarg){
                server_port = atoi(optarg);
            }
            break;
        case 'd': {
            add_fuse_arg("-d");
            break;
        }
        case 'o':
            if(optarg){
                add_fuse_arg("-o");
                add_fuse_arg(optarg);
            }
            break;
        case 's':
            add_fuse_arg("-s");
            break;
        case 'h':
            usage(argv[0]);
            exit(0);
        default:
            fprintf(stderr, "ignoring option character %c\n", optopt);
            break;
        }
    }
    while (optind < argc) {
        fprintf(stderr, "non-option element: %s\n",argv[optind]);
        add_fuse_arg(argv[optind]);
        optind++;
    }

    fprintf(stderr, "arguments for FUSE: ");
    for (i=0; i<fuse_argc; i++) {
        fprintf(stderr, "%s ", fuse_argv[i]);
    }
    fprintf(stderr, "\n");
    return 0;
}
Example #13
0
/*  all these functions return simple status and can be handled
 *  together. */
int simple_op(unsigned int type, const char* path1, off_t offset,
              mode_t mode1, const char *opt_path)
{
    unsigned int packet_size;
    int ret;
  
    corefs_packet packet;
    memset(&packet, 0, sizeof(packet));
    server_data *sd=NULL;

    char r_path_[MAXPATH];
    char s_addr[MAXADDR];
    char *r_path1 = &r_path_[0];
    char r_opt_path_[MAXPATH];
    char *r_opt_path = &r_opt_path_[0];

    if (type == COREFS_SIMPLE_SYMLINK) {
        remove_addr(opt_path, &r_opt_path, s_addr);
        strncpy(r_path1, path1, MAXPATH);
    }
    remove_addr(path1, &r_path1, s_addr);
    remove_addr(opt_path, &r_opt_path, NULL);
    
    if (r_path1[0]=='\0' || r_path1[1]=='\0') {
        //there was no path other than the first '/'
        //ie this is for the root directory
        //OR this is a simple_op on something in the root directory
        //and we will take that to be local (rmdir / not good anyways:P)
        return simple_op_local(type, path1, offset, mode1, opt_path, s_addr);
    }

    ret = check_setup(s_addr, &sd);
    if (ret == -EHOSTUNREACH) return -EHOSTUNREACH;
    if (ret < 0) return -EIO;

    
    /* Get user info from the upper layer */
    if(my_ops.up_get_user_info)
        my_ops.up_get_user_info(&(packet.payload.request.user_ids),
                                path1, opt_path);

    packet_size = build_simple(&packet, type, r_path1, offset, mode1,
                               r_opt_path);
    char buffer[packet_size];
    memset(buffer, 0, packet_size);  
    ret = encap_corefs_header(buffer, &packet);
    encap_corefs_request(buffer + ret, &packet);
#ifdef DEBUG_NETWORK
    fprintf(stderr, "simple_op: printing request\n");
    print_packet(packet);
#endif
    if (send_packet(sd->ctx, buffer, packet_size) <=0) {
        dprintf(stderr, "error sending packet.\n");
        return -EIO;
    }
    memset(buffer, 0, packet_size);
    if ((ret = client_receive_specified(sd->ctx, buffer,
                                        COREFS_RESPONSE_STATUS)) < 0) {
        dprintf(stderr, "error receiving simple status.\n");
        return ret;
    }
    return 0;
}
Example #14
0
/* ARGSUSED */
static int
checkfilesys(char *filesys)
{
    ufs2_daddr_t n_ffree, n_bfree;
    struct dups *dp;
    int cylno;
    intmax_t blks, files;

    check_filename = filesys;
    if (check_debug && check_clean)
        check_warn("starting\n");

    check_sblock_init();
    if (check_clean && check_skipclean) {
        /*
         * If file system is gjournaled, check it here.
         */
        if ((check_fsreadfd = open(filesys, O_RDONLY)) < 0 || check_readsb(0) == 0)
            exit(3);    /* Cannot read superblock */
        close(check_fsreadfd);
        if ((check_sblk.b_un.b_fs->fs_flags & FS_GJOURNAL) != 0) {
            //printf("GJournaled file system detected on %s.\n",
            //    filesys);
            if (check_sblk.b_un.b_fs->fs_clean == 1) {
                check_warn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
                exit(0);
            }
            if ((check_sblk.b_un.b_fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) {
                check_gjournal(filesys);
                exit(0);
            } else {
                check_fatal(
                "UNEXPECTED INCONSISTENCY, CANNOT RUN FAST FSCK\n");
            }
        }
    }

    switch (check_setup(filesys, 0)) {
    case 0:
        if (check_preen)
            check_fatal("CAN'T CHECK FILE SYSTEM.");
        return (0);
    case -1:
        check_warn("clean, %ld free ", (long)(check_sblk.b_un.b_fs->fs_cstotal.cs_nffree +
            check_sblk.b_un.b_fs->fs_frag * check_sblk.b_un.b_fs->fs_cstotal.cs_nbfree));
        printf("(%jd frags, %jd blocks, %.1f%% fragmentation)\n",
            (intmax_t)check_sblk.b_un.b_fs->fs_cstotal.cs_nffree,
            (intmax_t)check_sblk.b_un.b_fs->fs_cstotal.cs_nbfree,
            check_sblk.b_un.b_fs->fs_cstotal.cs_nffree * 100.0 / check_sblk.b_un.b_fs->fs_dsize);
        return (0);
    }
    /*
     * Determine if we can and should do journal recovery.
     */
    if ((check_sblk.b_un.b_fs->fs_flags & FS_SUJ) == FS_SUJ) {
        if ((check_sblk.b_un.b_fs->fs_flags & FS_NEEDSFSCK) != FS_NEEDSFSCK && check_skipclean) {
            if (check_preen || check_reply("USE JOURNAL")) {
                if (check_suj(filesys) == 0) {
                    printf("\n***** FILE SYSTEM MARKED CLEAN *****\n");
                    exit(0);
                }
            }
            printf("** Skipping journal, falling through to full fsck\n\n");
        }
        /*
         * Write the superblock so we don't try to recover the
         * journal on another pass.
         */
        check_sblk.b_un.b_fs->fs_mtime = time(NULL);
        dirty(&check_sblk);
    }

    /*
     * Cleared if any questions answered no. Used to decide if
     * the superblock should be marked clean.
     */
    check_resolved = 1;
    /*
     * 1: scan inodes tallying blocks used
     */
    if (check_preen == 0) {
        printf("** Last Mounted on %s\n", check_sblk.b_un.b_fs->fs_fsmnt);
        printf("** Phase 1 - Check Blocks and Sizes\n");
    }
    check_pass1();

    /*
     * 1b: locate first references to duplicates, if any
     */
    if (check_duplist) {
        if (check_preen || check_usedsoftdep)
            check_fatal("INTERNAL ERROR: dups with %s%s%s",
                check_preen ? "-p" : "",
                (check_preen && check_usedsoftdep) ? " and " : "",
                check_usedsoftdep ? "softupdates" : "");
        printf("** Phase 1b - Rescan For More DUPS\n");
        check_pass1b();
    }

    /*
     * 2: traverse directories from root to mark all connected directories
     */
    if (check_preen == 0)
        printf("** Phase 2 - Check Pathnames\n");
    check_pass2();

    /*
     * 3: scan inodes looking for disconnected directories
     */
    if (check_preen == 0)
        printf("** Phase 3 - Check Connectivity\n");
    check_pass3();

    /*
     * 4: scan inodes looking for disconnected files; check reference counts
     */
    if (check_preen == 0)
        printf("** Phase 4 - Check Reference Counts\n");
    check_pass4();

    /*
     * 5: check and repair resource counts in cylinder groups
     */
    if (check_preen == 0)
        printf("** Phase 5 - Check Cyl groups\n");
    check_pass5();

    /*
     * print out summary statistics
     */
    n_ffree = check_sblk.b_un.b_fs->fs_cstotal.cs_nffree;
    n_bfree = check_sblk.b_un.b_fs->fs_cstotal.cs_nbfree;
    files = check_maxino - ROOTINO - check_sblk.b_un.b_fs->fs_cstotal.cs_nifree - check_n_files;
    blks = check_n_blks +
        check_sblk.b_un.b_fs->fs_ncg * (cgdmin(check_sblk.b_un.b_fs, 0) - cgsblock(check_sblk.b_un.b_fs, 0));
    blks += cgsblock(check_sblk.b_un.b_fs, 0) - cgbase(check_sblk.b_un.b_fs, 0);
    blks += howmany(check_sblk.b_un.b_fs->fs_cssize, check_sblk.b_un.b_fs->fs_fsize);
    blks = check_maxfsblock - (n_ffree + check_sblk.b_un.b_fs->fs_frag * n_bfree) - blks;
    check_warn("%ld files, %jd used, %ju free ",
        (long)check_n_files, (intmax_t)check_n_blks,
        (uintmax_t)(n_ffree + check_sblk.b_un.b_fs->fs_frag * n_bfree));
    printf("(%ju frags, %ju blocks, %.1f%% fragmentation)\n",
        (uintmax_t)n_ffree, (uintmax_t)n_bfree,
        n_ffree * 100.0 / check_sblk.b_un.b_fs->fs_dsize);
    if (check_debug) {
        if (files < 0)
            printf("%jd inodes missing\n", -files);
        if (blks < 0)
            printf("%jd blocks missing\n", -blks);
        if (check_duplist != NULL) {
            printf("The following duplicate blocks remain:");
            for (dp = check_duplist; dp; dp = dp->next)
                printf(" %jd,", (intmax_t)dp->dup);
            printf("\n");
        }
    }
    check_duplist = (struct dups *)0;
    check_muldup = (struct dups *)0;
    check_inocleanup();
    if (check_fsmodified) {
        check_sblk.b_un.b_fs->fs_time = time(NULL);
        dirty(&check_sblk);
    }
    if (check_cvtlevel && check_sblk.b_dirty) {
        /*
         * Write out the duplicate super blocks
         */
        for (cylno = 0; cylno < check_sblk.b_un.b_fs->fs_ncg; cylno++)
            check_blwrite(check_fswritefd, (char *)check_sblk.b_un.b_fs,
                fsbtodb(check_sblk.b_un.b_fs, cgsblock(check_sblk.b_un.b_fs, cylno)),
                SBLOCKSIZE);
    }
    if (check_rerun)
        check_resolved = 0;

    /*
     * Check to see if the file system is mounted read-write.
     */
    check_finish(check_resolved);

    for (cylno = 0; cylno < check_sblk.b_un.b_fs->fs_ncg; cylno++)
        if (check_inostathead[cylno].il_stat != NULL)
            free((char *)check_inostathead[cylno].il_stat);
    free((char *)check_inostathead);
    check_inostathead = NULL;
    if (check_fsmodified && !check_preen)
        printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
    if (check_rerun)
        printf("\n***** PLEASE RERUN FSCK *****\n");
    return (0);
}