terom@24: terom@24: /* terom@24: * A simple PostgreSQL-based filesystem. terom@24: */ terom@24: terom@24: #include terom@24: #include terom@24: terom@24: #include terom@24: terom@24: #include "evfuse.h" terom@24: #include "evsql.h" terom@24: #include "dirbuf.h" terom@24: #include "lib/log.h" terom@24: #include "lib/signals.h" terom@24: #include "lib/misc.h" terom@24: terom@24: #define SERROR(val) do { (val); goto error; } while(0) terom@24: terom@24: struct dbfs { terom@24: struct event_base *ev_base; terom@24: struct signals *signals; terom@24: terom@24: const char *db_conninfo; terom@24: struct evsql *db; terom@24: terom@24: struct evfuse *ev_fuse; terom@24: }; terom@24: terom@24: #define CONNINFO_DEFAULT "dbname=test" terom@24: terom@24: // XXX: not sure how this should work terom@24: #define CACHE_TIMEOUT 1.0 terom@24: terom@24: mode_t _dbfs_mode (const char *type) { terom@24: if (!strcmp(type, "DIR")) terom@24: return S_IFDIR; terom@24: terom@24: if (!strcmp(type, "REG")) terom@24: return S_IFREG; terom@24: terom@24: else { terom@24: WARNING("[dbfs] weird mode-type: %s", type); terom@24: return 0; terom@24: } terom@24: } terom@24: terom@24: void dbfs_init (void *userdata, struct fuse_conn_info *conn) { terom@24: INFO("[dbfs.init] userdata=%p, conn=%p", userdata, conn); terom@24: terom@24: } terom@24: terom@24: void dbfs_destroy (void *userdata) { terom@24: INFO("[dbfs.destroy] userdata=%p", userdata); terom@24: terom@24: terom@24: } terom@24: terom@25: /* terom@25: * Check the result set. terom@25: * terom@25: * Returns; terom@25: * -1 if the query failed, the columns do not match, or there are too many/few rows terom@25: * 0 the results match terom@25: * 1 there were no results terom@25: */ terom@25: int _dbfs_check_res (const struct evsql_result_info *res, size_t rows, size_t cols) { terom@25: int err = 0; terom@25: terom@25: // check if it failed terom@25: if (res->error) terom@25: NERROR(evsql_result_error(res)); terom@25: terom@25: // not found? terom@25: if (evsql_result_rows(res) == 0) terom@25: SERROR(err = 1); terom@25: terom@25: // duplicate rows? terom@25: if (evsql_result_rows(res) != rows) terom@25: ERROR("multiple rows returned"); terom@25: terom@25: // correct number of columns terom@25: if (evsql_result_cols(res) != 5) terom@25: ERROR("wrong number of columns: %zu", evsql_result_cols(res)); terom@25: terom@25: // good terom@25: return 0; terom@25: terom@25: error: terom@25: if (!err) terom@25: err = -1; terom@25: terom@25: return err; terom@25: } terom@25: terom@25: int _dbfs_stat_info (struct stat *st, const struct evsql_result_info *res, size_t row, size_t col_offset) { terom@25: int err = 0; terom@25: terom@25: uint16_t mode; terom@25: uint64_t size, nlink; terom@25: const char *type; terom@25: terom@25: // extract the data terom@25: if (0 terom@25: || evsql_result_string(res, row, col_offset + 0, &type, 0 ) // inodes.type terom@25: || evsql_result_uint16(res, row, col_offset + 1, &mode, 0 ) // inodes.mode terom@25: || evsql_result_uint64(res, row, col_offset + 2, &size, 0 ) // inodes.size terom@25: || evsql_result_uint64(res, row, col_offset + 3, &nlink, 0 ) // count(*) terom@25: ) terom@25: EERROR(err = EIO, "invalid db data"); terom@25: terom@25: INFO("\tst_mode=S_IF%s | %ho, st_nlink=%llu, st_size=%llu", type, mode, (long long unsigned int) nlink, (long long unsigned int) size); terom@25: terom@25: // convert and store terom@25: st->st_mode = _dbfs_mode(type) | mode; terom@25: st->st_nlink = nlink; terom@25: st->st_size = size; terom@25: terom@25: // good terom@25: return 0; terom@25: terom@25: error: terom@25: return -1; terom@25: } terom@25: terom@24: void _dbfs_lookup_result (const struct evsql_result_info *res, void *arg) { terom@24: struct fuse_req *req = arg; terom@24: struct fuse_entry_param e; ZINIT(e); terom@24: int err = 0; terom@24: terom@24: uint32_t ino; terom@24: terom@25: // check the results terom@25: if ((err = _dbfs_check_res(res, 1, 5))) terom@25: SERROR(err = (err == 1 ? ENOENT : EIO)); terom@24: terom@24: // get the data terom@24: if (0 terom@24: || evsql_result_uint32(res, 0, 0, &ino, 0 ) // inodes.ino terom@24: ) terom@24: EERROR(err = EIO, "invalid db data"); terom@25: terom@25: INFO("[dbfs.lookup] -> ion=%u", ino); terom@24: terom@25: // stat attrs terom@25: if (_dbfs_stat_info(&e.attr, res, 0, 1)) terom@25: goto error; terom@24: terom@25: // other attrs terom@24: e.ino = e.attr.st_ino = ino; terom@24: e.attr_timeout = CACHE_TIMEOUT; terom@24: e.entry_timeout = CACHE_TIMEOUT; terom@24: terom@24: // reply terom@24: if ((err = fuse_reply_entry(req, &e))) terom@24: EERROR(err, "fuse_reply_entry"); terom@24: terom@24: error: terom@24: if (err && (err = fuse_reply_err(req, err))) terom@24: EWARNING(err, "fuse_reply_err"); terom@24: terom@24: // free terom@24: evsql_result_free(res); terom@24: } terom@24: terom@24: void dbfs_lookup (struct fuse_req *req, fuse_ino_t parent, const char *name) { terom@24: struct dbfs *ctx = fuse_req_userdata(req); terom@24: int err; terom@24: terom@24: INFO("[dbfs.lookup] parent=%lu name=%s", parent, name); terom@24: terom@24: // query and params terom@24: const char *sql = terom@24: "SELECT" terom@24: " inodes.ino, inodes.type, inodes.mode, inodes.size, count(*)" terom@24: " FROM file_tree INNER JOIN inodes ON (file_tree.inode = inodes.ino)" terom@25: " WHERE file_tree.parent = $1::int4 AND file_tree.name = $2::varchar" terom@24: " GROUP BY inodes.ino, inodes.type, inodes.mode, inodes.size"; terom@24: terom@24: static struct evsql_query_params params = EVSQL_PARAMS(EVSQL_FMT_BINARY) { terom@24: EVSQL_PARAM ( UINT32 ), terom@24: EVSQL_PARAM ( STRING ), terom@24: terom@24: EVSQL_PARAMS_END terom@24: }; terom@24: terom@24: // build params terom@24: if (0 terom@24: || evsql_param_uint32(¶ms, 0, parent) terom@24: || evsql_param_string(¶ms, 1, name) terom@24: ) terom@24: EERROR(err = EIO, "evsql_param_*"); terom@24: terom@24: // query terom@25: if (evsql_query_params(ctx->db, NULL, sql, ¶ms, _dbfs_lookup_result, req) == NULL) terom@24: EERROR(err = EIO, "evsql_query_params"); terom@24: terom@24: // XXX: handle interrupts terom@24: terom@24: // wait terom@24: return; terom@24: terom@24: error: terom@24: if ((err = fuse_reply_err(req, err))) terom@24: EWARNING(err, "fuse_reply_err"); terom@24: } terom@24: terom@25: void _dbfs_getattr_result (const struct evsql_result_info *res, void *arg) { terom@25: struct fuse_req *req = arg; terom@25: struct stat st; ZINIT(st); terom@25: int err = 0; terom@25: terom@25: // check the results terom@25: if ((err = _dbfs_check_res(res, 1, 4))) terom@25: SERROR(err = (err == 1 ? ENOENT : EIO)); terom@25: terom@25: INFO("[dbfs.getattr %p] -> (stat follows)", req); terom@25: terom@25: // stat attrs terom@25: if (_dbfs_stat_info(&st, res, 0, 0)) terom@25: goto error; terom@25: terom@25: // XXX: we don't have the ino terom@25: st.st_ino = 0; terom@25: terom@25: // reply terom@25: if ((err = fuse_reply_attr(req, &st, CACHE_TIMEOUT))) terom@25: EERROR(err, "fuse_reply_entry"); terom@25: terom@25: error: terom@25: if (err && (err = fuse_reply_err(req, err))) terom@25: EWARNING(err, "fuse_reply_err"); terom@25: terom@25: // free terom@25: evsql_result_free(res); terom@25: } terom@25: terom@25: static void dbfs_getattr (struct fuse_req *req, fuse_ino_t ino, struct fuse_file_info *fi) { terom@25: struct dbfs *ctx = fuse_req_userdata(req); terom@25: int err; terom@25: terom@25: (void) fi; terom@25: terom@25: INFO("[dbfs.getattr %p] ino=%lu", req, ino); terom@25: terom@25: const char *sql = terom@25: "SELECT" terom@25: " inodes.type, inodes.mode, inodes.size, count(*)" terom@25: " FROM inodes" terom@25: " WHERE inodes.ino = ‰1::int4" terom@25: " GROUP BY inodes.type, inodes.mode, inodes.size"; terom@25: terom@25: static struct evsql_query_params params = EVSQL_PARAMS(EVSQL_FMT_BINARY) { terom@25: EVSQL_PARAM ( UINT32 ), terom@25: terom@25: EVSQL_PARAMS_END terom@25: }; terom@25: terom@25: // build params terom@25: if (0 terom@25: || evsql_param_uint32(¶ms, 0, ino) terom@25: ) terom@25: SERROR(err = EIO); terom@25: terom@25: // query terom@25: if (evsql_query_params(ctx->db, NULL, sql, ¶ms, _dbfs_getattr_result, req) == NULL) terom@25: SERROR(err = EIO); terom@25: terom@25: // XXX: handle interrupts terom@25: terom@25: // wait terom@25: return; terom@25: terom@25: error: terom@25: if ((err = fuse_reply_err(req, err))) terom@25: EWARNING(err, "fuse_reply_err"); terom@25: } terom@25: terom@24: struct fuse_lowlevel_ops dbfs_llops = { terom@25: terom@24: .init = dbfs_init, terom@24: .destroy = dbfs_destroy, terom@24: terom@24: .lookup = dbfs_lookup, terom@25: terom@25: .getattr = dbfs_getattr, terom@24: }; terom@24: terom@24: void dbfs_sql_error (struct evsql *evsql, void *arg) { terom@24: struct dbfs *ctx = arg; terom@24: terom@24: // AAAAAAAAAA.... panic terom@24: WARNING("[dbfs] SQL error: BREAKING MAIN LOOP LIEK NAO"); terom@24: terom@24: event_base_loopbreak(ctx->ev_base); terom@24: } terom@24: terom@24: int main (int argc, char **argv) { terom@24: struct fuse_args fuse_args = FUSE_ARGS_INIT(argc, argv); terom@24: struct dbfs ctx; ZINIT(ctx); terom@24: terom@24: // parse args, XXX: fuse_args terom@24: ctx.db_conninfo = CONNINFO_DEFAULT; terom@24: terom@24: // init libevent terom@24: if ((ctx.ev_base = event_base_new()) == NULL) terom@24: ERROR("event_base_new"); terom@24: terom@24: // setup signals terom@24: if ((ctx.signals = signals_default(ctx.ev_base)) == NULL) terom@24: ERROR("signals_default"); terom@24: terom@24: // open sql terom@24: if ((ctx.db = evsql_new_pq(ctx.ev_base, ctx.db_conninfo, dbfs_sql_error, &ctx)) == NULL) terom@24: ERROR("evsql_new_pq"); terom@24: terom@24: // open fuse terom@24: if ((ctx.ev_fuse = evfuse_new(ctx.ev_base, &fuse_args, &dbfs_llops, &ctx)) == NULL) terom@24: ERROR("evfuse_new"); terom@24: terom@24: // run libevent terom@24: INFO("running libevent loop"); terom@24: terom@24: if (event_base_dispatch(ctx.ev_base)) terom@24: PERROR("event_base_dispatch"); terom@24: terom@24: // clean shutdown terom@24: terom@24: error : terom@24: // cleanup terom@24: if (ctx.ev_fuse) terom@24: evfuse_close(ctx.ev_fuse); terom@24: terom@24: // XXX: ctx.db terom@24: terom@24: if (ctx.signals) terom@24: signals_free(ctx.signals); terom@24: terom@24: if (ctx.ev_base) terom@24: event_base_free(ctx.ev_base); terom@24: terom@24: fuse_opt_free_args(&fuse_args); terom@24: } terom@24: