Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2) /* Manage a cache of file names' existence */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7) #include "fncache.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9) struct fncache {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) 	struct hlist_node nd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) 	bool res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) 	char name[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define FNHSIZE 61
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static struct hlist_head fncache_hash[FNHSIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) unsigned shash(const unsigned char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) 	unsigned h = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) 	while (*s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) 		h = 65599 * h + *s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) 	return h ^ (h >> 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static bool lookup_fncache(const char *name, bool *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) 	int h = shash((const unsigned char *)name) % FNHSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) 	struct fncache *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) 	hlist_for_each_entry(n, &fncache_hash[h], nd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) 		if (!strcmp(n->name, name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) 			*res = n->res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static void update_fncache(const char *name, bool res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) 	struct fncache *n = malloc(sizeof(struct fncache) + strlen(name) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) 	int h = shash((const unsigned char *)name) % FNHSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) 	if (!n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) 	strcpy(n->name, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) 	n->res = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) 	hlist_add_head(&n->nd, &fncache_hash[h]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /* No LRU, only use when bounded in some other way. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) bool file_available(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) 	bool res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) 	if (lookup_fncache(name, &res))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) 		return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) 	res = access(name, R_OK) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) 	update_fncache(name, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }