/* Callback for nfs_create / nfs_lookup */ static void _swap_file_ready(uintptr_t token, enum nfs_stat status, fhandle_t *fh, fattr_t *fattr) { int err; sattr_t sattr; swap_file_init_data *data = (swap_file_init_data *) token; if (status == NFSERR_NOENT) { dprintf(1, "Swap file does not exist. Creating...\n"); sattr = (sattr_t) { .mode = 0b110110110, // RW .uid = 1, .gid = 1, .size = 0, .atime = { 0 }, .mtime = { 0 } }; err = nfs_create(&mnt_point, SWAP_FILE_NAME, &sattr, &_swap_file_ready, 0); if (err) { dprintf(1, "Could not create swap file. (%d)\n", err); _swap_file_initialised = false; } } else if (status) {
int main(int argc, char *argv[]) { CLIENT *clnt = NULL; if (argc != 5) { printf("Usage: nodeid diskid mapid filename\n"); return(1); } int nodeid = atoi(argv[1]); int diskid = atoi(argv[2]); int mapid = atoi(argv[3]); fhdb_data_t *db = NULL; fhandle3 filehandle; char fhdata[FHSIZE3]; filehandle.fhandle3_len = 0; filehandle.fhandle3_val = fhdata; clnt = create_nfs_connection(nodeid, "tcp"); if (!clnt) { fprintf(stderr, "create_nfs_connection failed\n"); return(1); } db = fhdb_init(NULL, 1); if (get_handle(nodeid, diskid, mapid, &filehandle)) { fprintf(stderr, "get_handle failed\n"); return(1); } fhdb_key_t key; key.nodeid = nodeid; key.diskid = diskid; key.mapid = mapid; fhdb_set(db, &key, &filehandle); int error = nfs_create(nodeid, diskid, &clnt, db, mapid, argv[4], &filehandle); if (error) { fprintf(stderr, "Failed to create file [%d]\n", error); } else { printf("File has been created\n"); print_fh(-1, 0, 0, &filehandle); } destroy_connection(clnt); clnt = NULL; fhdb_destroy(db); return(error); }
/* Run the actual open/lookup request */ static void rq_lookup_run(NFS_LookupRequest *rq) { dprintf(2, "run NFS Lookup request\n"); // If open mode is write, then create new file since we want to start again. if (rq->mode & FM_WRITE && !(rq->mode & FM_NOTRUNC)) { sattr_t sat = DEFAULT_SATTR; nfs_create(&nfs_mnt, rq->p.vnode->path, &sat, lookup_cb, rq->p.token); } else { nfs_lookup(&nfs_mnt, rq->p.vnode->path, lookup_cb, rq->p.token); } }
/* * Swap creation callback function after nfs_lookup of swapfile */ void initialise_swap_callback(uintptr_t token,int status,struct cookie *fh, fattr_t *attr) { //Now we need to call nfs int create_issued = 0; if(status != 0) { //Swapfile does not exist so create it sattr_t attributes; attributes.size = 0; attributes.mode = 0x06; nfs_create(&mnt_point,SWAPFILE,&attributes,initialise_swap_callback,token); create_issued = 1; //Add some delay here return; } if(create_issued || status == 0) { printf("\nSwapfile created !!\n"); //Need to copy the cookie into a memory location swapcookie = (struct cookie *)malloc(sizeof(struct cookie)); swapInitialised = 1; memcpy(swapcookie,fh,sizeof(struct cookie)); } }
/* NFS_LookUp Callback */ static void lookup_cb(uintptr_t token, int status, struct cookie *fh, fattr_t *attr) { dprintf(1, "*** nfsfs_lookup_cb: %d, %d, %p, %p\n", token, status, fh, attr); NFS_LookupRequest *rq = (NFS_LookupRequest *) get_request(token); if (rq == NULL) { dprintf(0, "!!! nfsfs: Corrupt lookup callback, no matching token: %d\n", token); return; } // open done if (status == NFS_OK) { NFS_File *nf = (NFS_File *) rq->p.vnode->extra; memcpy(&(nf->fh), fh, sizeof(struct cookie)); cp_stats(&(rq->p.vnode->vstat), attr); dprintf(2, "nfsfs: Sending: %d, %d\n", token, rq->p.pid); rq->open_done(rq->p.pid, rq->p.vnode, rq->mode, SOS_VFS_OK); remove_request((NFS_BaseRequest *) rq); } // create the file else if ((status == NFSERR_NOENT) && (rq->mode & FM_WRITE)) { dprintf(2, "nfsfs: Create new file!\n"); // reuse current rq struct, has all we need and is hot and ready sattr_t sat = DEFAULT_SATTR; nfs_create(&nfs_mnt, rq->p.vnode->path, &sat, lookup_cb, rq->p.token); } // error else { dprintf(0, "!!! nfsfs: lookup_cb: Error occured! (%d)\n", status); free_nfsfile(rq->p.vnode); rq->open_done(rq->p.pid, rq->p.vnode, rq->mode, status_nfs2vfs(status)); remove_request((NFS_BaseRequest *) rq); } }