/** * * cache_content_crash_recover: recovers the data cache and the associated inode after a crash. * * @param pclient [IN] ressource allocated by the client for the nfs management. * @pstatus [OUT] returned status. * * @return CACHE_CONTENT_SUCCESS is successful. * */ cache_content_status_t cache_content_crash_recover(unsigned short exportid, unsigned int index, unsigned int mod, cache_content_client_t * pclient_data, cache_inode_client_t * pclient_inode, hash_table_t * ht, fsal_op_context_t * pcontext, cache_content_status_t * pstatus) { DIR *cache_directory; cache_content_dirinfo_t export_directory; char cache_exportdir[MAXPATHLEN]; char fullpath[MAXPATHLEN]; struct dirent *direntp; struct dirent dirent_export; int found_export_id; u_int64_t inum; off_t size_in_cache; cache_entry_t inode_entry; cache_entry_t *pentry = NULL; cache_content_entry_t *pentry_content = NULL; cache_inode_status_t cache_inode_status; cache_content_status_t cache_content_status; fsal_attrib_list_t fsal_attr; cache_inode_fsal_data_t fsal_data; *pstatus = CACHE_CONTENT_SUCCESS; /* Open the cache directory */ if((cache_directory = opendir(pclient_data->cache_dir)) == NULL) { *pstatus = CACHE_CONTENT_LOCAL_CACHE_ERROR; return *pstatus; } /* read the cache directory */ while((direntp = readdir(cache_directory)) != NULL) { /* . and .. are of no interest */ if(!strcmp(direntp->d_name, ".") || !strcmp(direntp->d_name, "..")) continue; if((found_export_id = cache_content_get_export_id(direntp->d_name)) >= 0) { LogEvent(COMPONENT_CACHE_CONTENT, "Directory cache for Export ID %d has been found", found_export_id); snprintf(cache_exportdir, MAXPATHLEN, "%s/%s", pclient_data->cache_dir, direntp->d_name); if(cache_content_local_cache_opendir(cache_exportdir, &(export_directory)) == FALSE) { *pstatus = CACHE_CONTENT_LOCAL_CACHE_ERROR; closedir(cache_directory); return *pstatus; } /* Reads the directory content (a single thread for the moment) */ while(cache_content_local_cache_dir_iter (&export_directory, &dirent_export, index, mod)) { /* . and .. are of no interest */ if(!strcmp(dirent_export.d_name, ".") || !strcmp(dirent_export.d_name, "..")) continue; if((inum = cache_content_get_inum(dirent_export.d_name)) > 0) { LogEvent(COMPONENT_CACHE_CONTENT, "Cache entry for File ID %llx has been found", inum); /* Get the content of the file */ sprintf(fullpath, "%s/%s/%s", pclient_data->cache_dir, direntp->d_name, dirent_export.d_name); if((cache_inode_status = cache_inode_reload_content(fullpath, &inode_entry)) != CACHE_INODE_SUCCESS) { LogMajor(COMPONENT_CACHE_CONTENT, "File Content Cache record for File ID %llx is unreadable", inum); continue; } else LogMajor(COMPONENT_CACHE_CONTENT, "File Content Cache record for File ID %llx : READ OK", inum); /* Populating the cache_inode... */ fsal_data.handle = inode_entry.object.file.handle; fsal_data.cookie = 0; if((pentry = cache_inode_get(&fsal_data, &fsal_attr, ht, pclient_inode, pcontext, &cache_inode_status)) == NULL) { LogCrit(COMPONENT_CACHE_CONTENT, "Error adding cached inode for file ID %llx, error=%d", inum, cache_inode_status); continue; } else LogEvent(COMPONENT_CACHE_CONTENT, "Cached inode added successfully for file ID %llx", inum); /* Get the size from the cache */ if((size_in_cache = cache_content_recover_size(cache_exportdir, inum)) == -1) { LogCrit(COMPONENT_CACHE_CONTENT, "Error when recovering size for file ID %llx", inum); } else pentry->object.file.attributes.filesize = (fsal_size_t) size_in_cache; /* Adding the cached entry to the data cache */ if((pentry_content = cache_content_new_entry(pentry, NULL, pclient_data, RECOVER_ENTRY, pcontext, &cache_content_status)) == NULL) { LogCrit(COMPONENT_CACHE_CONTENT, "Error adding cached data for file ID %llx, error=%d", inum, cache_inode_status); continue; } else LogEvent(COMPONENT_CACHE_CONTENT, "Cached data added successfully for file ID %llx", inum); if((cache_content_status = cache_content_valid(pentry_content, CACHE_CONTENT_OP_GET, pclient_data)) != CACHE_CONTENT_SUCCESS) { *pstatus = cache_content_status; return *pstatus; } } } /* while( ( dirent_export = readdir( export_directory ) ) != NULL ) */ /* Close the export cache directory */ cache_content_local_cache_closedir(&export_directory); } /* if( ( found_export_id = cache_content_get_export_id( direntp->d_name ) ) > 0 ) */ } /* while( ( direntp = readdir( cache_directory ) ) != NULL ) */ /* Close the cache directory */ closedir(cache_directory); return *pstatus; } /* cache_content_crash_recover */
cache_inode_status_t cache_inode_rdwr(cache_entry_t * pentry, cache_inode_io_direction_t read_or_write, fsal_seek_t * seek_descriptor, fsal_size_t buffer_size, fsal_size_t * pio_size, fsal_attrib_list_t * pfsal_attr, caddr_t buffer, fsal_boolean_t * p_fsal_eof, hash_table_t * ht, cache_inode_client_t * pclient, fsal_op_context_t * pcontext, uint64_t stable, cache_inode_status_t * pstatus) { int statindex = 0; cache_content_io_direction_t io_direction; cache_content_status_t cache_content_status; fsal_status_t fsal_status; fsal_openflags_t openflags; fsal_size_t io_size; fsal_attrib_list_t post_write_attr; fsal_status_t fsal_status_getattr; struct stat buffstat; /* Set the return default to CACHE_INODE_SUCCESS */ *pstatus = CACHE_INODE_SUCCESS; /* For now, only FSAL_SEEK_SET is supported */ if(seek_descriptor->whence != FSAL_SEEK_SET) { LogCrit(COMPONENT_CACHE_INODE, "Implementation trouble: seek_descriptor was not a 'FSAL_SEEK_SET' cursor"); *pstatus = CACHE_INODE_INVALID_ARGUMENT; return *pstatus; } io_size = buffer_size; LogDebug(COMPONENT_CACHE_INODE, "cache_inode_rdwr: INODE : IO Size = %llu fdsize =%zu seeksize=%zu", buffer_size, sizeof(fsal_file_t), sizeof(fsal_seek_t)); /* stat */ pclient->stat.nb_call_total += 1; if(read_or_write == CACHE_INODE_READ) { statindex = CACHE_INODE_READ_DATA; io_direction = CACHE_CONTENT_READ; openflags = FSAL_O_RDONLY; pclient->stat.func_stats.nb_call[CACHE_INODE_READ_DATA] += 1; } else { statindex = CACHE_INODE_WRITE_DATA; io_direction = CACHE_CONTENT_WRITE; openflags = FSAL_O_WRONLY; pclient->stat.func_stats.nb_call[CACHE_INODE_WRITE_DATA] += 1; } P_w(&pentry->lock); /* IO are done only on REGULAR_FILEs */ if(pentry->internal_md.type != REGULAR_FILE) { *pstatus = CACHE_INODE_BAD_TYPE; V_w(&pentry->lock); /* stats */ pclient->stat.func_stats.nb_err_unrecover[statindex] += 1; return *pstatus; } /* Non absolute address within the file are not supported (we act only like pread/pwrite) */ if(seek_descriptor->whence != FSAL_SEEK_SET) { *pstatus = CACHE_INODE_INVALID_ARGUMENT; V_w(&pentry->lock); /* stats */ pclient->stat.func_stats.nb_err_unrecover[statindex] += 1; return *pstatus; } /* Do we use stable or unstable storage ? */ if(stable == FSAL_UNSAFE_WRITE_TO_GANESHA_BUFFER) { /* Data will be stored in memory and not flush to FSAL */ /* If the unstable_data buffer allocated ? */ if(pentry->object.file.unstable_data.buffer == NULL) { if((pentry->object.file.unstable_data.buffer = Mem_Alloc_Label(CACHE_INODE_UNSTABLE_BUFFERSIZE, "Cache_Inode Unstable Buffer")) == NULL) { *pstatus = CACHE_INODE_MALLOC_ERROR; V_w(&pentry->lock); /* stats */ pclient->stat.func_stats.nb_err_unrecover[statindex] += 1; return *pstatus; } pentry->object.file.unstable_data.offset = seek_descriptor->offset; pentry->object.file.unstable_data.length = buffer_size; memcpy(pentry->object.file.unstable_data.buffer, buffer, buffer_size); /* Set mtime and ctime */ cache_inode_set_time_current( &pentry->attributes.mtime ) ; /* BUGAZOMEU : write operation must NOT modify file's ctime */ pentry->attributes.ctime = pentry->attributes.mtime; *pio_size = buffer_size; } /* if( pentry->object.file.unstable_data.buffer == NULL ) */ else { if((pentry->object.file.unstable_data.offset < seek_descriptor->offset) && (buffer_size + seek_descriptor->offset < CACHE_INODE_UNSTABLE_BUFFERSIZE)) { pentry->object.file.unstable_data.length = buffer_size + seek_descriptor->offset; memcpy((char *)(pentry->object.file.unstable_data.buffer + seek_descriptor->offset), buffer, buffer_size); /* Set mtime and ctime */ cache_inode_set_time_current( &pentry->attributes.mtime ) ; /* BUGAZOMEU : write operation must NOT modify file's ctime */ pentry->attributes.ctime = pentry->attributes.mtime; *pio_size = buffer_size; } else { /* Go back to regular situation */ stable = FSAL_SAFE_WRITE_TO_FS; } } } /* if( stable == FALSE ) */ if(stable == FSAL_SAFE_WRITE_TO_FS || stable == FSAL_UNSAFE_WRITE_TO_FS_BUFFER) { /* Calls file content cache to operate on the cache */ if(pentry->object.file.pentry_content != NULL) { /* Entry is data cached */ cache_content_rdwr(pentry->object.file.pentry_content, io_direction, seek_descriptor, &io_size, pio_size, buffer, p_fsal_eof, &buffstat, (cache_content_client_t *) pclient->pcontent_client, pcontext, &cache_content_status); /* If the entry under resync */ if(cache_content_status == CACHE_CONTENT_LOCAL_CACHE_NOT_FOUND) { /* Data cache gc has removed this entry */ if(cache_content_new_entry(pentry, NULL, (cache_content_client_t *)pclient->pcontent_client, RENEW_ENTRY, pcontext, &cache_content_status) == NULL) { /* Entry could not be recoverd, cache_content_status contains an error, let it be managed by the next block */ LogCrit(COMPONENT_CACHE_INODE, "Read/Write Operation through cache failed with status %d (renew process failed)", cache_content_status); /* Will go to the end of the function on the error clause with cache_content_status describing the error */ } else { /* Entry was successfully renewed */ LogInfo(COMPONENT_CACHE_INODE, "----> File Content Entry %p was successfully renewed", pentry); /* Try to access the content of the file again */ cache_content_rdwr(pentry->object.file.pentry_content, io_direction, seek_descriptor, &io_size, pio_size, buffer, p_fsal_eof, &buffstat, (cache_content_client_t *) pclient->pcontent_client, pcontext, &cache_content_status); /* No management of cache_content_status in case of failure, this will be done * within the next block */ } } if(cache_content_status != CACHE_CONTENT_SUCCESS) { *pstatus = cache_content_error_convert(cache_content_status); V_w(&pentry->lock); LogCrit(COMPONENT_CACHE_INODE, "Read/Write Operation through cache failed with status %d", cache_content_status); /* stats */ pclient->stat.func_stats.nb_err_unrecover[statindex] += 1; return *pstatus; } LogFullDebug(COMPONENT_CACHE_INODE, "cache_inode_rdwr: inode/dc: io_size=%llu, pio_size=%llu, eof=%d, seek=%d.%"PRIu64, io_size, *pio_size, *p_fsal_eof, seek_descriptor->whence, seek_descriptor->offset); LogMidDebug(COMPONENT_CACHE_INODE, "cache_inode_rdwr: INODE AFTER : IO Size = %llu %llu", io_size, *pio_size); /* Use information from the buffstat to update the file metadata */ pentry->attributes.filesize = buffstat.st_size; pentry->attributes.spaceused = buffstat.st_blksize * buffstat.st_blocks; } else { /* No data cache entry, we operated directly on FSAL */ pentry->attributes.asked_attributes = pclient->attrmask; /* We need to open if we don't have a cached * descriptor or our open flags differs. */ if(cache_inode_open(pentry, pclient, openflags, pcontext, pstatus) != CACHE_INODE_SUCCESS) { V_w(&pentry->lock); /* stats */ pclient->stat.func_stats.nb_err_unrecover[statindex] += 1; return *pstatus; } /* Call FSAL_read or FSAL_write */ if(read_or_write == CACHE_INODE_READ) { #ifdef _USE_MFSL fsal_status = MFSL_read(&(pentry->object.file.open_fd.mfsl_fd), seek_descriptor, io_size, buffer, pio_size, p_fsal_eof, &pclient->mfsl_context, NULL); #else fsal_status = FSAL_read(&(pentry->object.file.open_fd.fd), seek_descriptor, io_size, buffer, pio_size, p_fsal_eof); #endif } else { #ifdef _USE_MFSL fsal_status = MFSL_write(&(pentry->object.file.open_fd.mfsl_fd), seek_descriptor, io_size, buffer, pio_size, &pclient->mfsl_context, NULL); #else fsal_status = FSAL_write(&(pentry->object.file.open_fd.fd), seek_descriptor, io_size, buffer, pio_size); #endif #if 0 /* Alright, the unstable write is complete. Now if it was supposed to be a stable write * we can sync to the hard drive. */ if(stable == FSAL_SAFE_WRITE_TO_FS) { #ifdef _USE_MFSL fsal_status = MFSL_commit(&(pentry->object.file.open_fd.mfsl_fd), NULL); #else fsal_status = FSAL_commit(&(pentry->object.file.open_fd.fd)); #endif #endif } LogFullDebug(COMPONENT_FSAL, "cache_inode_rdwr: FSAL IO operation returned %d, asked_size=%llu, effective_size=%llu", fsal_status.major, (unsigned long long)io_size, (unsigned long long)*pio_size); if(FSAL_IS_ERROR(fsal_status)) { if(fsal_status.major == ERR_FSAL_DELAY) LogEvent(COMPONENT_CACHE_INODE, "cache_inode_rdwr: FSAL_write returned EBUSY"); else LogDebug(COMPONENT_CACHE_INODE, "cache_inode_rdwr: fsal_status.major = %d", fsal_status.major); if((fsal_status.major != ERR_FSAL_NOT_OPENED) && (pentry->object.file.open_fd.fileno != 0)) { LogDebug(COMPONENT_CACHE_INODE, "cache_inode_rdwr: CLOSING pentry %p: fd=%d", pentry, pentry->object.file.open_fd.fileno); #ifdef _USE_MFSL MFSL_close(&(pentry->object.file.open_fd.mfsl_fd), &pclient->mfsl_context, NULL); #else FSAL_close(&(pentry->object.file.open_fd.fd)); #endif *pstatus = cache_inode_error_convert(fsal_status); } else { /* the fd has been close by another thread. * return CACHE_INODE_FSAL_DELAY so the client will * retry with a new fd. */ *pstatus = CACHE_INODE_FSAL_DELAY; } pentry->object.file.open_fd.last_op = 0; pentry->object.file.open_fd.fileno = 0; V_w(&pentry->lock); /* stats */ pclient->stat.func_stats.nb_err_unrecover[statindex] += 1; return *pstatus; } LogFullDebug(COMPONENT_CACHE_INODE, "cache_inode_rdwr: inode/direct: io_size=%llu, pio_size=%llu, eof=%d, seek=%d.%"PRIu64, io_size, *pio_size, *p_fsal_eof, seek_descriptor->whence, seek_descriptor->offset); if(cache_inode_close(pentry, pclient, pstatus) != CACHE_INODE_SUCCESS) { LogEvent(COMPONENT_CACHE_INODE, "cache_inode_rdwr: cache_inode_close = %d", *pstatus); V_w(&pentry->lock); /* stats */ pclient->stat.func_stats.nb_err_unrecover[statindex] += 1; return *pstatus; } if(read_or_write == CACHE_INODE_WRITE) { /* Do a getattr in order to have update information on filesize * This query is done directly on FSAL (object is not data cached), and result * will be propagated to cache Inode */ /* WARNING: This operation is to be done AFTER FSAL_close (some FSAL, like POSIX, * may not flush data until the file is closed */ /*post_write_attr.asked_attributes = pclient->attrmask ; */ post_write_attr.asked_attributes = FSAL_ATTR_SIZE | FSAL_ATTR_SPACEUSED; fsal_status_getattr = FSAL_getattrs(&(pentry->handle), pcontext, &post_write_attr); /* if failed, the next block will handle the error */ if(FSAL_IS_ERROR(fsal_status_getattr)) fsal_status = fsal_status_getattr; else { /* Update Cache Inode attributes */ pentry->attributes.filesize = post_write_attr.filesize; pentry->attributes.spaceused = post_write_attr.spaceused; } } } /* IO was successfull (through cache content or not), we manually update the times in the attributes */ switch (read_or_write) { case CACHE_INODE_READ: /* Set the atime */ cache_inode_set_time_current( & pentry->attributes.atime ) ; break; case CACHE_INODE_WRITE: /* Set mtime and ctime */ cache_inode_set_time_current( & pentry->attributes.mtime ) ; /* BUGAZOMEU : write operation must NOT modify file's ctime */ pentry->attributes.ctime = pentry->attributes.mtime; break; } } /* if(stable == TRUE ) */ /* Return attributes to caller */ if(pfsal_attr != NULL) *pfsal_attr = pentry->attributes; *pstatus = CACHE_INODE_SUCCESS; /* stat */ if(read_or_write == CACHE_INODE_READ) { *pstatus = cache_inode_valid(pentry, CACHE_INODE_OP_GET, pclient); if(*pstatus != CACHE_INODE_SUCCESS) pclient->stat.func_stats.nb_err_unrecover[CACHE_INODE_READ] += 1; else pclient->stat.func_stats.nb_success[CACHE_INODE_READ] += 1; } else { *pstatus = cache_inode_valid(pentry, CACHE_INODE_OP_SET, pclient); if(*pstatus != CACHE_INODE_SUCCESS) pclient->stat.func_stats.nb_err_unrecover[CACHE_INODE_WRITE] += 1; else pclient->stat.func_stats.nb_success[CACHE_INODE_WRITE] += 1; } V_w(&pentry->lock); return *pstatus; } /* cache_inode_rdwr */
cache_inode_status_t cache_inode_add_data_cache(cache_entry_t * pentry, hash_table_t * ht, cache_inode_client_t * pclient, fsal_op_context_t * pcontext, cache_inode_status_t * pstatus) { cache_content_status_t cache_content_status; cache_content_entry_t *pentry_content = NULL; cache_content_client_t *pcontent_client; /* Set the return default to CACHE_INODE_SUCCESS */ *pstatus = CACHE_INODE_SUCCESS; /* stats */ pclient->stat.nb_call_total += 1; inc_func_call(pclient, CACHE_INODE_ADD_DATA_CACHE); P_w(&pentry->lock); /* Operate only on a regular file */ if(pentry->internal_md.type != REGULAR_FILE) { *pstatus = CACHE_INODE_BAD_TYPE; V_w(&pentry->lock); /* stats */ inc_func_err_unrecover(pclient, CACHE_INODE_ADD_DATA_CACHE); return *pstatus; } if(pentry->object.file.pentry_content != NULL) { /* The object is already cached */ *pstatus = CACHE_INODE_CACHE_CONTENT_EXISTS; V_w(&pentry->lock); /* stats */ inc_func_err_retryable(pclient, CACHE_INODE_ADD_DATA_CACHE); return *pstatus; } pcontent_client = (cache_content_client_t *) pclient->pcontent_client; pentry_content = cache_content_new_entry(pentry, NULL, pcontent_client, ADD_ENTRY, pcontext, &cache_content_status); if(pentry_content == NULL) { *pstatus = cache_content_error_convert(cache_content_status); V_w(&pentry->lock); /* stats */ inc_func_err_unrecover(pclient, CACHE_INODE_ADD_DATA_CACHE); return *pstatus; } /* Attached the entry to the cache inode */ pentry->object.file.pentry_content = pentry_content; V_w(&pentry->lock); *pstatus = CACHE_INODE_SUCCESS; /* stats */ inc_func_err_unrecover(pclient, CACHE_INODE_ADD_DATA_CACHE); return *pstatus; }