2020-06-07 07:23:09 +02:00
|
|
|
// The Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) The Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2020 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
#include <RB_INC_SYS_SYSMACROS_H
|
|
|
|
|
2020-06-08 21:22:35 +02:00
|
|
|
bool
|
|
|
|
ircd::fs::dev::for_each(const blk_closure &closure)
|
|
|
|
{
|
|
|
|
return for_each(string_view{}, closure);
|
|
|
|
}
|
2020-06-07 08:26:28 +02:00
|
|
|
|
2020-06-08 21:22:35 +02:00
|
|
|
bool
|
|
|
|
ircd::fs::dev::for_each(const string_view &type,
|
|
|
|
const blk_closure &closure)
|
2020-06-07 08:26:28 +02:00
|
|
|
{
|
2020-06-10 03:41:36 +02:00
|
|
|
for(const auto &dir : fs::ls(blk::BASE_PATH)) try
|
2020-06-07 08:26:28 +02:00
|
|
|
{
|
|
|
|
const auto &[major, minor]
|
|
|
|
{
|
|
|
|
split(filename(path_scratch, dir), ':')
|
|
|
|
};
|
|
|
|
|
2021-03-02 01:38:50 +01:00
|
|
|
if(!major || !minor)
|
|
|
|
continue;
|
|
|
|
|
2020-06-08 21:22:35 +02:00
|
|
|
const ulong id
|
2020-06-07 08:26:28 +02:00
|
|
|
{
|
2020-06-08 21:22:35 +02:00
|
|
|
dev::id({lex_cast<ulong>(major), lex_cast<ulong>(minor)})
|
2020-06-07 08:26:28 +02:00
|
|
|
};
|
|
|
|
|
2020-06-08 21:22:35 +02:00
|
|
|
char dtbuf[32];
|
|
|
|
if(type && blk::devtype(dtbuf, id) != type)
|
|
|
|
continue;
|
2020-06-07 08:26:28 +02:00
|
|
|
|
2020-06-08 21:22:35 +02:00
|
|
|
if(!closure(id, blk(id)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch(const ctx::interrupted &)
|
|
|
|
{
|
|
|
|
throw;
|
2020-06-07 08:26:28 +02:00
|
|
|
}
|
2020-06-07 22:42:13 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2020-06-08 21:22:35 +02:00
|
|
|
log::error
|
2020-06-07 22:42:13 +02:00
|
|
|
{
|
|
|
|
log, "%s :%s",
|
|
|
|
dir,
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
}
|
2020-06-07 08:26:28 +02:00
|
|
|
|
2020-06-08 21:22:35 +02:00
|
|
|
return true;
|
2020-06-07 08:26:28 +02:00
|
|
|
}
|
|
|
|
|
2020-06-07 07:23:09 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::dev::sysfs(const mutable_buffer &out,
|
|
|
|
const ulong &id,
|
|
|
|
const string_view &relpath)
|
|
|
|
{
|
2020-06-11 09:43:36 +02:00
|
|
|
thread_local char path_buf[1024];
|
2020-06-07 07:23:09 +02:00
|
|
|
const string_view path{fmt::sprintf
|
|
|
|
{
|
2020-06-11 09:43:36 +02:00
|
|
|
path_buf, "dev/block/%s/%s",
|
2020-06-07 07:23:09 +02:00
|
|
|
sysfs_id(name_scratch, id),
|
|
|
|
relpath
|
|
|
|
}};
|
|
|
|
|
2020-06-11 09:43:36 +02:00
|
|
|
return sys::get(out, path);
|
2020-06-07 22:42:13 +02:00
|
|
|
}
|
2020-06-07 07:23:09 +02:00
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::dev::sysfs_id(const mutable_buffer &out,
|
|
|
|
const ulong &id)
|
|
|
|
{
|
|
|
|
return sysfs_id(out, dev::id(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::dev::sysfs_id(const mutable_buffer &out,
|
|
|
|
const major_minor &id)
|
|
|
|
{
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
out, "%lu:%lu", id.first, id.second
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong
|
|
|
|
ircd::fs::dev::id(const major_minor &id)
|
|
|
|
{
|
|
|
|
return makedev(id.first, id.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::fs::dev::major_minor
|
|
|
|
ircd::fs::dev::id(const ulong &id)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
{
|
|
|
|
major(id), minor(id)
|
|
|
|
};
|
|
|
|
}
|
2020-06-07 08:26:28 +02:00
|
|
|
|
|
|
|
//
|
2020-06-08 21:22:35 +02:00
|
|
|
// dev::blk
|
2020-06-07 08:26:28 +02:00
|
|
|
//
|
|
|
|
|
2020-06-10 03:41:36 +02:00
|
|
|
decltype(ircd::fs::dev::blk::SECTOR_SIZE)
|
|
|
|
ircd::fs::dev::blk::SECTOR_SIZE
|
|
|
|
{
|
|
|
|
512
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::fs::dev::blk::BASE_PATH)
|
|
|
|
ircd::fs::dev::blk::BASE_PATH
|
|
|
|
{
|
|
|
|
"/sys/dev/block"
|
|
|
|
};
|
|
|
|
|
2020-06-08 21:22:35 +02:00
|
|
|
ircd::fs::dev::blk::blk(const ulong &id)
|
|
|
|
:type
|
2020-06-07 08:26:28 +02:00
|
|
|
{
|
2020-06-08 21:22:35 +02:00
|
|
|
ircd::string(15, [&id]
|
2020-06-07 08:26:28 +02:00
|
|
|
(const mutable_buffer &buf)
|
|
|
|
{
|
2020-06-08 21:22:35 +02:00
|
|
|
return devtype(buf, id);
|
2020-06-07 08:26:28 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
,vendor
|
|
|
|
{
|
2020-06-08 21:22:35 +02:00
|
|
|
ircd::string(15, [&id]
|
2020-06-07 08:26:28 +02:00
|
|
|
(const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
return sysfs(buf, id, "device/vendor");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
,model
|
|
|
|
{
|
|
|
|
ircd::string(64, [&id]
|
|
|
|
(const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
return sysfs(buf, id, "device/model");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
,rev
|
|
|
|
{
|
2020-06-08 21:22:35 +02:00
|
|
|
ircd::string(15, [&id]
|
2020-06-07 08:26:28 +02:00
|
|
|
(const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
return sysfs(buf, id, "device/rev");
|
|
|
|
})
|
|
|
|
}
|
2020-06-10 03:41:36 +02:00
|
|
|
,sector_size
|
|
|
|
{
|
|
|
|
sysfs(id, "queue/hw_sector_size")
|
|
|
|
}
|
|
|
|
,physical_block
|
|
|
|
{
|
|
|
|
sysfs(id, "queue/physical_block_size")
|
|
|
|
}
|
|
|
|
,logical_block
|
|
|
|
{
|
|
|
|
sysfs(id, "queue/logical_block_size")
|
|
|
|
}
|
|
|
|
,minimum_io
|
|
|
|
{
|
|
|
|
sysfs(id, "queue/minimum_io_size")
|
|
|
|
}
|
|
|
|
,optimal_io
|
|
|
|
{
|
|
|
|
sysfs(id, "queue/optimal_io_size")
|
|
|
|
}
|
|
|
|
,sectors
|
2020-06-07 08:26:28 +02:00
|
|
|
{
|
|
|
|
sysfs(id, "size")
|
|
|
|
}
|
|
|
|
,queue_depth
|
|
|
|
{
|
2020-06-07 22:42:13 +02:00
|
|
|
sysfs(id, "device/queue_depth")
|
2020-06-07 08:26:28 +02:00
|
|
|
}
|
|
|
|
,nr_requests
|
|
|
|
{
|
2020-06-07 22:42:13 +02:00
|
|
|
sysfs(id, "queue/nr_requests")
|
2020-06-07 08:26:28 +02:00
|
|
|
}
|
2020-06-10 03:41:36 +02:00
|
|
|
,scheduler
|
|
|
|
{
|
|
|
|
ircd::string(64, [&id]
|
|
|
|
(const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
return sysfs(buf, id, "queue/scheduler");
|
|
|
|
})
|
|
|
|
}
|
2020-06-07 08:26:28 +02:00
|
|
|
,rotational
|
|
|
|
{
|
2020-06-08 21:22:35 +02:00
|
|
|
sysfs<bool>(id, "queue/rotational", false)
|
2020-06-07 08:26:28 +02:00
|
|
|
}
|
2020-06-10 03:41:36 +02:00
|
|
|
,merges
|
|
|
|
{
|
|
|
|
!sysfs<bool>(id, "queue/nomerges", true)
|
|
|
|
}
|
2020-06-07 08:26:28 +02:00
|
|
|
{
|
|
|
|
}
|
2020-06-08 21:22:35 +02:00
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::fs::dev::blk::devtype(const mutable_buffer &buf,
|
|
|
|
const ulong &id)
|
|
|
|
{
|
|
|
|
char tmp[128];
|
|
|
|
string_view ret;
|
|
|
|
tokens(sysfs(tmp, id, "uevent"), '\n', [&buf, &ret]
|
|
|
|
(const string_view &kv)
|
|
|
|
{
|
|
|
|
const auto &[key, value]
|
|
|
|
{
|
|
|
|
split(kv, '=')
|
|
|
|
};
|
|
|
|
|
|
|
|
if(key == "DEVTYPE")
|
|
|
|
ret = strlcpy(buf, value);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|