static int has_discard(const char *devname, struct sysfs_cxt *wholedisk) { struct sysfs_cxt cxt, *parent = NULL; uint64_t dg = 0; dev_t disk = 0, dev; int rc; dev = sysfs_devname_to_devno(devname, NULL); if (!dev) return 1; /* * This is tricky to read the info from sys/, because the queue * atrributes are provided for whole devices (disk) only. We're trying * to reuse the whole-disk sysfs context to optimize this stuff (as * system usually have just one disk only). */ if (sysfs_devno_to_wholedisk(dev, NULL, 0, &disk) || !disk) return 1; if (dev != disk) { if (wholedisk->devno != disk) { sysfs_deinit(wholedisk); if (sysfs_init(wholedisk, disk, NULL)) return 1; } parent = wholedisk; } rc = sysfs_init(&cxt, dev, parent); if (!rc) rc = sysfs_read_u64(&cxt, "queue/discard_granularity", &dg); sysfs_deinit(&cxt); return rc == 0 && dg > 0; }
static int get_partition_start(int fd, int partno, uint64_t *start) { struct stat st; struct sysfs_cxt disk = UL_SYSFSCXT_EMPTY, part = UL_SYSFSCXT_EMPTY; dev_t devno = 0; int rc = -1; /* * wholedisk */ if (fstat(fd, &st) || !S_ISBLK(st.st_mode)) goto done; devno = st.st_rdev; if (sysfs_init(&disk, devno, NULL)) goto done; /* * partition */ devno = sysfs_partno_to_devno(&disk, partno); if (!devno) goto done; if (sysfs_init(&part, devno, &disk)) goto done; if (sysfs_read_u64(&part, "start", start)) goto done; rc = 0; done: sysfs_deinit(&part); sysfs_deinit(&disk); return rc; }
// FIXME: uses same number for device and FME (may not be true in future) fpga_result sysfs_get_bitstream_id(int dev, uint64_t *id) { char spath[SYSFS_PATH_MAX]; snprintf_s_ii(spath, SYSFS_PATH_MAX, SYSFS_FPGA_CLASS_PATH SYSFS_FME_PATH_FMT "/" FPGA_SYSFS_BITSTREAM_ID, dev, dev); return sysfs_read_u64(spath, id); }
int main(int argc, char *argv[]) { struct sysfs_cxt cxt = UL_SYSFSCXT_EMPTY; char *devname; dev_t devno; char path[PATH_MAX]; int i; uint64_t u64; ssize_t len; if (argc != 2) errx(EXIT_FAILURE, "usage: %s <devname>", argv[0]); devname = argv[1]; devno = sysfs_devname_to_devno(devname, NULL); if (!devno) err(EXIT_FAILURE, "failed to read devno"); printf("NAME: %s\n", devname); printf("DEVNO: %u\n", (unsigned int) devno); printf("DEVNOPATH: %s\n", sysfs_devno_path(devno, path, sizeof(path))); printf("DEVPATH: %s\n", sysfs_devno_to_devpath(devno, path, sizeof(path))); printf("PARTITION: %s\n", sysfs_devno_has_attribute(devno, "partition") ? "YES" : "NOT"); if (sysfs_init(&cxt, devno, NULL)) return EXIT_FAILURE; len = sysfs_readlink(&cxt, NULL, path, sizeof(path) - 1); if (len > 0) { path[len] = '\0'; printf("DEVNOLINK: %s\n", path); } printf("SLAVES: %d\n", sysfs_count_dirents(&cxt, "slaves")); if (sysfs_read_u64(&cxt, "size", &u64)) printf("read SIZE failed\n"); else printf("SIZE: %jd\n", u64); if (sysfs_read_int(&cxt, "queue/hw_sector_size", &i)) printf("read SECTOR failed\n"); else printf("SECTOR: %d\n", i); printf("DEVNAME: %s\n", sysfs_get_devname(&cxt, path, sizeof(path))); sysfs_deinit(&cxt); return EXIT_SUCCESS; }
/* * @lc: context * @sizelimit: returns size limit for the given device * * Returns: <0 on error, 0 on success */ int loopcxt_get_sizelimit(struct loopdev_cxt *lc, uint64_t *size) { struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc); int rc = -EINVAL; if (sysfs) rc = sysfs_read_u64(sysfs, "loop/sizelimit", size); if (rc && loopcxt_ioctl_enabled(lc)) { struct loop_info64 *lo = loopcxt_get_info(lc); if (lo) { if (size) *size = lo->lo_sizelimit; rc = 0; } } DBG(lc, loopdev_debug("get_sizelimit [rc=%d]", rc)); return rc; }
/* * @lc: context * @offset: returns offset number for the given device * * Returns: <0 on error, 0 on success */ int loopcxt_get_offset(struct loopdev_cxt *lc, uint64_t *offset) { struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc); int rc = -EINVAL; if (sysfs) rc = sysfs_read_u64(sysfs, "loop/offset", offset); if (rc && loopcxt_ioctl_enabled(lc)) { struct loop_info64 *lo = loopcxt_get_info(lc); if (lo) { if (offset) *offset = lo->lo_offset; rc = 0; } } DBG(lc, loopdev_debug("get_offset [rc=%d]", rc)); return rc; }
// get fpga device id using the sysfs path fpga_result sysfs_deviceid_from_path(const char *sysfspath, uint64_t *deviceid) { char sysfs_path[SYSFS_PATH_MAX] = {0}; char *p = NULL; int device_instance = 0; fpga_result result = FPGA_OK; if (deviceid == NULL) { FPGA_ERR("Invalid input Parameters"); return FPGA_INVALID_PARAM; } p = strstr(sysfspath, FPGA_SYSFS_FME); if (p == NULL) { FPGA_ERR("Failed to read sysfs path"); return FPGA_NOT_SUPPORTED; } p = strrchr(sysfspath, '.'); if (p == NULL) { FPGA_ERR("Failed to read sysfs path"); return FPGA_NOT_SUPPORTED; } device_instance = atoi(p + 1); snprintf_s_is(sysfs_path, SYSFS_PATH_MAX, SYSFS_FPGA_CLASS_PATH SYSFS_FPGA_FMT "/%s", device_instance, FPGA_SYSFS_DEVICEID); result = sysfs_read_u64(sysfs_path, deviceid); if (result != 0) FPGA_ERR("Failed to read device ID"); return result; }
// get fpga device id fpga_result get_fpga_deviceid(fpga_handle handle, uint64_t *deviceid) { struct _fpga_token *_token = NULL; struct _fpga_handle *_handle = (struct _fpga_handle *)handle; char sysfs_path[SYSFS_PATH_MAX] = {0}; char *p = NULL; int device_instance = 0; fpga_result result = FPGA_OK; int err = 0; if (_handle == NULL) { FPGA_ERR("Invalid handle"); return FPGA_INVALID_PARAM; } if (deviceid == NULL) { FPGA_ERR("Invalid input Parameters"); return FPGA_INVALID_PARAM; } if (pthread_mutex_lock(&_handle->lock)) { FPGA_MSG("Failed to lock handle mutex"); return FPGA_EXCEPTION; } _token = (struct _fpga_token *)_handle->token; if (_token == NULL) { FPGA_ERR("Token not found"); result = FPGA_INVALID_PARAM; goto out_unlock; } p = strstr(_token->sysfspath, FPGA_SYSFS_FME); if (p == NULL) { FPGA_ERR("Failed to read sysfs path"); result = FPGA_NOT_SUPPORTED; goto out_unlock; } p = strrchr(_token->sysfspath, '.'); if (p == NULL) { FPGA_ERR("Failed to read sysfs path"); result = FPGA_NOT_SUPPORTED; goto out_unlock; } device_instance = atoi(p + 1); snprintf_s_is(sysfs_path, SYSFS_PATH_MAX, SYSFS_FPGA_CLASS_PATH SYSFS_FPGA_FMT "/%s", device_instance, FPGA_SYSFS_DEVICEID); result = sysfs_read_u64(sysfs_path, deviceid); if (result != 0) { FPGA_ERR("Failed to read device ID"); goto out_unlock; } out_unlock: err = pthread_mutex_unlock(&_handle->lock); if (err) FPGA_ERR("pthread_mutex_unlock() failed: %s", strerror(err)); return result; }