incrementalfs.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
  2. /*
  3. * Userspace interface for Incremental FS.
  4. *
  5. * Incremental FS is special-purpose Linux virtual file system that allows
  6. * execution of a program while its binary and resource files are still being
  7. * lazily downloaded over the network, USB etc.
  8. *
  9. * Copyright 2019 Google LLC
  10. */
  11. #ifndef _UAPI_LINUX_INCREMENTALFS_H
  12. #define _UAPI_LINUX_INCREMENTALFS_H
  13. #include <linux/limits.h>
  14. #include <linux/ioctl.h>
  15. #include <linux/types.h>
  16. #include <linux/xattr.h>
  17. /* ===== constants ===== */
  18. #define INCFS_NAME "incremental-fs"
  19. /*
  20. * Magic number used in file header and in memory superblock
  21. * Note that it is a 5 byte unsigned long. Thus on 32 bit kernels, it is
  22. * truncated to a 4 byte number
  23. */
  24. #define INCFS_MAGIC_NUMBER (0x5346434e49ul & ULONG_MAX)
  25. #define INCFS_DATA_FILE_BLOCK_SIZE 4096
  26. #define INCFS_HEADER_VER 1
  27. /* TODO: This value is assumed in incfs_copy_signature_info_from_user to be the
  28. * actual signature length. Set back to 64 when fixed.
  29. */
  30. #define INCFS_MAX_HASH_SIZE 32
  31. #define INCFS_MAX_FILE_ATTR_SIZE 512
  32. #define INCFS_INDEX_NAME ".index"
  33. #define INCFS_INCOMPLETE_NAME ".incomplete"
  34. #define INCFS_PENDING_READS_FILENAME ".pending_reads"
  35. #define INCFS_LOG_FILENAME ".log"
  36. #define INCFS_BLOCKS_WRITTEN_FILENAME ".blocks_written"
  37. #define INCFS_XATTR_ID_NAME (XATTR_USER_PREFIX "incfs.id")
  38. #define INCFS_XATTR_SIZE_NAME (XATTR_USER_PREFIX "incfs.size")
  39. #define INCFS_XATTR_METADATA_NAME (XATTR_USER_PREFIX "incfs.metadata")
  40. #define INCFS_XATTR_VERITY_NAME (XATTR_USER_PREFIX "incfs.verity")
  41. #define INCFS_MAX_SIGNATURE_SIZE 8096
  42. #define INCFS_SIGNATURE_VERSION 2
  43. #define INCFS_SIGNATURE_SECTIONS 2
  44. #define INCFS_IOCTL_BASE_CODE 'g'
  45. /* ===== ioctl requests on the command dir ===== */
  46. /*
  47. * Create a new file
  48. * May only be called on .pending_reads file
  49. */
  50. #define INCFS_IOC_CREATE_FILE \
  51. _IOWR(INCFS_IOCTL_BASE_CODE, 30, struct incfs_new_file_args)
  52. /* Read file signature */
  53. #define INCFS_IOC_READ_FILE_SIGNATURE \
  54. _IOR(INCFS_IOCTL_BASE_CODE, 31, struct incfs_get_file_sig_args)
  55. /*
  56. * Fill in one or more data block. This may only be called on a handle
  57. * passed as a parameter to INCFS_IOC_PERMIT_FILLING
  58. *
  59. * Returns number of blocks filled in, or error if none were
  60. */
  61. #define INCFS_IOC_FILL_BLOCKS \
  62. _IOR(INCFS_IOCTL_BASE_CODE, 32, struct incfs_fill_blocks)
  63. /*
  64. * Permit INCFS_IOC_FILL_BLOCKS on the given file descriptor
  65. * May only be called on .pending_reads file
  66. *
  67. * Returns 0 on success or error
  68. */
  69. #define INCFS_IOC_PERMIT_FILL \
  70. _IOW(INCFS_IOCTL_BASE_CODE, 33, struct incfs_permit_fill)
  71. /*
  72. * Fills buffer with ranges of populated blocks
  73. *
  74. * Returns 0 if all ranges written
  75. * error otherwise
  76. *
  77. * Either way, range_buffer_size_out is set to the number
  78. * of bytes written. Should be set to 0 by caller. The ranges
  79. * filled are valid, but if an error was returned there might
  80. * be more ranges to come.
  81. *
  82. * Ranges are ranges of filled blocks:
  83. *
  84. * 1 2 7 9
  85. *
  86. * means blocks 1, 2, 7, 8, 9 are filled, 0, 3, 4, 5, 6 and 10 on
  87. * are not
  88. *
  89. * If hashing is enabled for the file, the hash blocks are simply
  90. * treated as though they immediately followed the data blocks.
  91. */
  92. #define INCFS_IOC_GET_FILLED_BLOCKS \
  93. _IOR(INCFS_IOCTL_BASE_CODE, 34, struct incfs_get_filled_blocks_args)
  94. /*
  95. * Creates a new mapped file
  96. * May only be called on .pending_reads file
  97. */
  98. #define INCFS_IOC_CREATE_MAPPED_FILE \
  99. _IOWR(INCFS_IOCTL_BASE_CODE, 35, struct incfs_create_mapped_file_args)
  100. /*
  101. * Get number of blocks, total and filled
  102. * May only be called on .pending_reads file
  103. */
  104. #define INCFS_IOC_GET_BLOCK_COUNT \
  105. _IOR(INCFS_IOCTL_BASE_CODE, 36, struct incfs_get_block_count_args)
  106. /*
  107. * Get per UID read timeouts
  108. * May only be called on .pending_reads file
  109. */
  110. #define INCFS_IOC_GET_READ_TIMEOUTS \
  111. _IOR(INCFS_IOCTL_BASE_CODE, 37, struct incfs_get_read_timeouts_args)
  112. /*
  113. * Set per UID read timeouts
  114. * May only be called on .pending_reads file
  115. */
  116. #define INCFS_IOC_SET_READ_TIMEOUTS \
  117. _IOW(INCFS_IOCTL_BASE_CODE, 38, struct incfs_set_read_timeouts_args)
  118. /*
  119. * Get last read error
  120. * May only be called on .pending_reads file
  121. */
  122. #define INCFS_IOC_GET_LAST_READ_ERROR \
  123. _IOW(INCFS_IOCTL_BASE_CODE, 39, struct incfs_get_last_read_error_args)
  124. /* ===== sysfs feature flags ===== */
  125. /*
  126. * Each flag is represented by a file in /sys/fs/incremental-fs/features
  127. * If the file exists the feature is supported
  128. * Also the file contents will be the line "supported"
  129. */
  130. /*
  131. * Basic flag stating that the core incfs file system is available
  132. */
  133. #define INCFS_FEATURE_FLAG_COREFS "corefs"
  134. /*
  135. * zstd compression support
  136. */
  137. #define INCFS_FEATURE_FLAG_ZSTD "zstd"
  138. /*
  139. * v2 feature set support. Covers:
  140. * INCFS_IOC_CREATE_MAPPED_FILE
  141. * INCFS_IOC_GET_BLOCK_COUNT
  142. * INCFS_IOC_GET_READ_TIMEOUTS/INCFS_IOC_SET_READ_TIMEOUTS
  143. * .blocks_written status file
  144. * .incomplete folder
  145. * report_uid mount option
  146. */
  147. #define INCFS_FEATURE_FLAG_V2 "v2"
  148. enum incfs_compression_alg {
  149. COMPRESSION_NONE = 0,
  150. COMPRESSION_LZ4 = 1,
  151. COMPRESSION_ZSTD = 2,
  152. };
  153. enum incfs_block_flags {
  154. INCFS_BLOCK_FLAGS_NONE = 0,
  155. INCFS_BLOCK_FLAGS_HASH = 1,
  156. };
  157. typedef struct {
  158. __u8 bytes[16];
  159. } incfs_uuid_t __attribute__((aligned (8)));
  160. /*
  161. * Description of a pending read. A pending read - a read call by
  162. * a userspace program for which the filesystem currently doesn't have data.
  163. *
  164. * Reads from .pending_reads and .log return an array of these structure
  165. */
  166. struct incfs_pending_read_info {
  167. /* Id of a file that is being read from. */
  168. incfs_uuid_t file_id;
  169. /* A number of microseconds since system boot to the read. */
  170. __aligned_u64 timestamp_us;
  171. /* Index of a file block that is being read. */
  172. __u32 block_index;
  173. /* A serial number of this pending read. */
  174. __u32 serial_number;
  175. };
  176. /*
  177. * Description of a pending read. A pending read - a read call by
  178. * a userspace program for which the filesystem currently doesn't have data.
  179. *
  180. * This version of incfs_pending_read_info is used whenever the file system is
  181. * mounted with the report_uid flag
  182. */
  183. struct incfs_pending_read_info2 {
  184. /* Id of a file that is being read from. */
  185. incfs_uuid_t file_id;
  186. /* A number of microseconds since system boot to the read. */
  187. __aligned_u64 timestamp_us;
  188. /* Index of a file block that is being read. */
  189. __u32 block_index;
  190. /* A serial number of this pending read. */
  191. __u32 serial_number;
  192. /* The UID of the reading process */
  193. __u32 uid;
  194. __u32 reserved;
  195. };
  196. /*
  197. * Description of a data or hash block to add to a data file.
  198. */
  199. struct incfs_fill_block {
  200. /* Index of a data block. */
  201. __u32 block_index;
  202. /* Length of data */
  203. __u32 data_len;
  204. /*
  205. * A pointer to an actual data for the block.
  206. *
  207. * Equivalent to: __u8 *data;
  208. */
  209. __aligned_u64 data;
  210. /*
  211. * Compression algorithm used to compress the data block.
  212. * Values from enum incfs_compression_alg.
  213. */
  214. __u8 compression;
  215. /* Values from enum incfs_block_flags */
  216. __u8 flags;
  217. __u16 reserved1;
  218. __u32 reserved2;
  219. __aligned_u64 reserved3;
  220. };
  221. /*
  222. * Description of a number of blocks to add to a data file
  223. *
  224. * Argument for INCFS_IOC_FILL_BLOCKS
  225. */
  226. struct incfs_fill_blocks {
  227. /* Number of blocks */
  228. __u64 count;
  229. /* A pointer to an array of incfs_fill_block structs */
  230. __aligned_u64 fill_blocks;
  231. };
  232. /*
  233. * Permit INCFS_IOC_FILL_BLOCKS on the given file descriptor
  234. * May only be called on .pending_reads file
  235. *
  236. * Argument for INCFS_IOC_PERMIT_FILL
  237. */
  238. struct incfs_permit_fill {
  239. /* File to permit fills on */
  240. __u32 file_descriptor;
  241. };
  242. enum incfs_hash_tree_algorithm {
  243. INCFS_HASH_TREE_NONE = 0,
  244. INCFS_HASH_TREE_SHA256 = 1
  245. };
  246. /*
  247. * Create a new file or directory.
  248. */
  249. struct incfs_new_file_args {
  250. /* Id of a file to create. */
  251. incfs_uuid_t file_id;
  252. /*
  253. * Total size of the new file. Ignored if S_ISDIR(mode).
  254. */
  255. __aligned_u64 size;
  256. /*
  257. * File mode. Permissions and dir flag.
  258. */
  259. __u16 mode;
  260. __u16 reserved1;
  261. __u32 reserved2;
  262. /*
  263. * A pointer to a null-terminated relative path to the file's parent
  264. * dir.
  265. * Max length: PATH_MAX
  266. *
  267. * Equivalent to: char *directory_path;
  268. */
  269. __aligned_u64 directory_path;
  270. /*
  271. * A pointer to a null-terminated file's name.
  272. * Max length: PATH_MAX
  273. *
  274. * Equivalent to: char *file_name;
  275. */
  276. __aligned_u64 file_name;
  277. /*
  278. * A pointer to a file attribute to be set on creation.
  279. *
  280. * Equivalent to: u8 *file_attr;
  281. */
  282. __aligned_u64 file_attr;
  283. /*
  284. * Length of the data buffer specfied by file_attr.
  285. * Max value: INCFS_MAX_FILE_ATTR_SIZE
  286. */
  287. __u32 file_attr_len;
  288. __u32 reserved4;
  289. /*
  290. * Points to an APK V4 Signature data blob
  291. * Signature must have two sections
  292. * Format is:
  293. * u32 version
  294. * u32 size_of_hash_info_section
  295. * u8 hash_info_section[]
  296. * u32 size_of_signing_info_section
  297. * u8 signing_info_section[]
  298. *
  299. * Note that incfs does not care about what is in signing_info_section
  300. *
  301. * hash_info_section has following format:
  302. * u32 hash_algorithm; // Must be SHA256 == 1
  303. * u8 log2_blocksize; // Must be 12 for 4096 byte blocks
  304. * u32 salt_size;
  305. * u8 salt[];
  306. * u32 hash_size;
  307. * u8 root_hash[];
  308. */
  309. __aligned_u64 signature_info;
  310. /* Size of signature_info */
  311. __aligned_u64 signature_size;
  312. __aligned_u64 reserved6;
  313. };
  314. /*
  315. * Request a digital signature blob for a given file.
  316. * Argument for INCFS_IOC_READ_FILE_SIGNATURE ioctl
  317. */
  318. struct incfs_get_file_sig_args {
  319. /*
  320. * A pointer to the data buffer to save an signature blob to.
  321. *
  322. * Equivalent to: u8 *file_signature;
  323. */
  324. __aligned_u64 file_signature;
  325. /* Size of the buffer at file_signature. */
  326. __u32 file_signature_buf_size;
  327. /*
  328. * Number of bytes save file_signature buffer.
  329. * It is set after ioctl done.
  330. */
  331. __u32 file_signature_len_out;
  332. };
  333. struct incfs_filled_range {
  334. __u32 begin;
  335. __u32 end;
  336. };
  337. /*
  338. * Request ranges of filled blocks
  339. * Argument for INCFS_IOC_GET_FILLED_BLOCKS
  340. */
  341. struct incfs_get_filled_blocks_args {
  342. /*
  343. * A buffer to populate with ranges of filled blocks
  344. *
  345. * Equivalent to struct incfs_filled_ranges *range_buffer
  346. */
  347. __aligned_u64 range_buffer;
  348. /* Size of range_buffer */
  349. __u32 range_buffer_size;
  350. /* Start index to read from */
  351. __u32 start_index;
  352. /*
  353. * End index to read to. 0 means read to end. This is a range,
  354. * so incfs will read from start_index to end_index - 1
  355. */
  356. __u32 end_index;
  357. /* Actual number of blocks in file */
  358. __u32 total_blocks_out;
  359. /* The number of data blocks in file */
  360. __u32 data_blocks_out;
  361. /* Number of bytes written to range buffer */
  362. __u32 range_buffer_size_out;
  363. /* Sector scanned up to, if the call was interrupted */
  364. __u32 index_out;
  365. };
  366. /*
  367. * Create a new mapped file
  368. * Argument for INCFS_IOC_CREATE_MAPPED_FILE
  369. */
  370. struct incfs_create_mapped_file_args {
  371. /*
  372. * Total size of the new file.
  373. */
  374. __aligned_u64 size;
  375. /*
  376. * File mode. Permissions and dir flag.
  377. */
  378. __u16 mode;
  379. __u16 reserved1;
  380. __u32 reserved2;
  381. /*
  382. * A pointer to a null-terminated relative path to the incfs mount
  383. * point
  384. * Max length: PATH_MAX
  385. *
  386. * Equivalent to: char *directory_path;
  387. */
  388. __aligned_u64 directory_path;
  389. /*
  390. * A pointer to a null-terminated file name.
  391. * Max length: PATH_MAX
  392. *
  393. * Equivalent to: char *file_name;
  394. */
  395. __aligned_u64 file_name;
  396. /* Id of source file to map. */
  397. incfs_uuid_t source_file_id;
  398. /*
  399. * Offset in source file to start mapping. Must be a multiple of
  400. * INCFS_DATA_FILE_BLOCK_SIZE
  401. */
  402. __aligned_u64 source_offset;
  403. };
  404. /*
  405. * Get information about the blocks in this file
  406. * Argument for INCFS_IOC_GET_BLOCK_COUNT
  407. */
  408. struct incfs_get_block_count_args {
  409. /* Total number of data blocks in the file */
  410. __u32 total_data_blocks_out;
  411. /* Number of filled data blocks in the file */
  412. __u32 filled_data_blocks_out;
  413. /* Total number of hash blocks in the file */
  414. __u32 total_hash_blocks_out;
  415. /* Number of filled hash blocks in the file */
  416. __u32 filled_hash_blocks_out;
  417. };
  418. /* Description of timeouts for one UID */
  419. struct incfs_per_uid_read_timeouts {
  420. /* UID to apply these timeouts to */
  421. __u32 uid;
  422. /*
  423. * Min time in microseconds to read any block. Note that this doesn't
  424. * apply to reads which are satisfied from the page cache.
  425. */
  426. __u32 min_time_us;
  427. /*
  428. * Min time in microseconds to satisfy a pending read. Any pending read
  429. * which is filled before this time will be delayed so that the total
  430. * read time >= this value.
  431. */
  432. __u32 min_pending_time_us;
  433. /*
  434. * Max time in microseconds to satisfy a pending read before the read
  435. * times out. If set to U32_MAX, defaults to mount options
  436. * read_timeout_ms * 1000. Must be >= min_pending_time_us
  437. */
  438. __u32 max_pending_time_us;
  439. };
  440. /*
  441. * Get the read timeouts array
  442. * Argument for INCFS_IOC_GET_READ_TIMEOUTS
  443. */
  444. struct incfs_get_read_timeouts_args {
  445. /*
  446. * A pointer to a buffer to fill with the current timeouts
  447. *
  448. * Equivalent to struct incfs_per_uid_read_timeouts *
  449. */
  450. __aligned_u64 timeouts_array;
  451. /* Size of above buffer in bytes */
  452. __u32 timeouts_array_size;
  453. /* Size used in bytes, or size needed if -ENOMEM returned */
  454. __u32 timeouts_array_size_out;
  455. };
  456. /*
  457. * Set the read timeouts array
  458. * Arguments for INCFS_IOC_SET_READ_TIMEOUTS
  459. */
  460. struct incfs_set_read_timeouts_args {
  461. /*
  462. * A pointer to an array containing the new timeouts
  463. * This will replace any existing timeouts
  464. *
  465. * Equivalent to struct incfs_per_uid_read_timeouts *
  466. */
  467. __aligned_u64 timeouts_array;
  468. /* Size of above array in bytes. Must be < 256 */
  469. __u32 timeouts_array_size;
  470. };
  471. /*
  472. * Get last read error struct
  473. * Arguments for INCFS_IOC_GET_LAST_READ_ERROR
  474. */
  475. struct incfs_get_last_read_error_args {
  476. /* File id of last file that had a read error */
  477. incfs_uuid_t file_id_out;
  478. /* Time of last read error, in us, from CLOCK_MONOTONIC */
  479. __u64 time_us_out;
  480. /* Index of page that was being read at last read error */
  481. __u32 page_out;
  482. /* errno of last read error */
  483. __u32 errno_out;
  484. /* uid of last read error */
  485. __u32 uid_out;
  486. __u32 reserved1;
  487. __u64 reserved2;
  488. };
  489. #endif /* _UAPI_LINUX_INCREMENTALFS_H */