packagelist.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. /*
  2. * fs/sdcardfs/packagelist.c
  3. *
  4. * Copyright (c) 2013 Samsung Electronics Co. Ltd
  5. * Authors: Daeho Jeong, Woojoong Lee, Seunghwan Hyun,
  6. * Sunghwan Yun, Sungjong Seo
  7. *
  8. * This program has been developed as a stackable file system based on
  9. * the WrapFS which written by
  10. *
  11. * Copyright (c) 1998-2011 Erez Zadok
  12. * Copyright (c) 2009 Shrikar Archak
  13. * Copyright (c) 2003-2011 Stony Brook University
  14. * Copyright (c) 2003-2011 The Research Foundation of SUNY
  15. *
  16. * This file is dual licensed. It may be redistributed and/or modified
  17. * under the terms of the Apache 2.0 License OR version 2 of the GNU
  18. * General Public License.
  19. */
  20. #include "sdcardfs.h"
  21. #include <linux/hashtable.h>
  22. #include <linux/ctype.h>
  23. #include <linux/delay.h>
  24. #include <linux/radix-tree.h>
  25. #include <linux/dcache.h>
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/slab.h>
  29. #include <linux/configfs.h>
  30. struct hashtable_entry {
  31. struct hlist_node hlist;
  32. struct hlist_node dlist; /* for deletion cleanup */
  33. struct qstr key;
  34. atomic_t value;
  35. };
  36. static DEFINE_HASHTABLE(package_to_appid, 8);
  37. static DEFINE_HASHTABLE(package_to_userid, 8);
  38. static DEFINE_HASHTABLE(ext_to_groupid, 8);
  39. static struct kmem_cache *hashtable_entry_cachep;
  40. static unsigned int full_name_case_hash(const unsigned char *name, unsigned int len)
  41. {
  42. unsigned long hash = init_name_hash();
  43. while (len--)
  44. hash = partial_name_hash(tolower(*name++), hash);
  45. return end_name_hash(hash);
  46. }
  47. static inline void qstr_init(struct qstr *q, const char *name)
  48. {
  49. q->name = name;
  50. q->len = strlen(q->name);
  51. q->hash = full_name_case_hash(q->name, q->len);
  52. }
  53. static inline int qstr_copy(const struct qstr *src, struct qstr *dest)
  54. {
  55. dest->name = kstrdup(src->name, GFP_KERNEL);
  56. dest->len = src->len;
  57. dest->hash = src->hash;
  58. return !!dest->name;
  59. }
  60. static appid_t __get_appid(const struct qstr *key)
  61. {
  62. struct hashtable_entry *hash_cur;
  63. unsigned int hash = key->hash;
  64. appid_t ret_id;
  65. rcu_read_lock();
  66. hash_for_each_possible_rcu_new(package_to_appid, hash_cur, hlist, hash) {
  67. if (qstr_case_eq(key, &hash_cur->key)) {
  68. ret_id = atomic_read(&hash_cur->value);
  69. rcu_read_unlock();
  70. return ret_id;
  71. }
  72. }
  73. rcu_read_unlock();
  74. return 0;
  75. }
  76. appid_t get_appid(const char *key)
  77. {
  78. struct qstr q;
  79. qstr_init(&q, key);
  80. return __get_appid(&q);
  81. }
  82. static appid_t __get_ext_gid(const struct qstr *key)
  83. {
  84. struct hashtable_entry *hash_cur;
  85. unsigned int hash = key->hash;
  86. appid_t ret_id;
  87. rcu_read_lock();
  88. hash_for_each_possible_rcu_new(ext_to_groupid, hash_cur, hlist, hash) {
  89. if (qstr_case_eq(key, &hash_cur->key)) {
  90. ret_id = atomic_read(&hash_cur->value);
  91. rcu_read_unlock();
  92. return ret_id;
  93. }
  94. }
  95. rcu_read_unlock();
  96. return 0;
  97. }
  98. appid_t get_ext_gid(const char *key)
  99. {
  100. struct qstr q;
  101. qstr_init(&q, key);
  102. return __get_ext_gid(&q);
  103. }
  104. static appid_t __is_excluded(const struct qstr *app_name, userid_t user)
  105. {
  106. struct hashtable_entry *hash_cur;
  107. unsigned int hash = app_name->hash;
  108. rcu_read_lock();
  109. hash_for_each_possible_rcu_new(package_to_userid, hash_cur, hlist, hash) {
  110. if (atomic_read(&hash_cur->value) == user &&
  111. qstr_case_eq(app_name, &hash_cur->key)) {
  112. rcu_read_unlock();
  113. return 1;
  114. }
  115. }
  116. rcu_read_unlock();
  117. return 0;
  118. }
  119. appid_t is_excluded(const char *key, userid_t user)
  120. {
  121. struct qstr q;
  122. qstr_init(&q, key);
  123. return __is_excluded(&q, user);
  124. }
  125. /* Kernel has already enforced everything we returned through
  126. * derive_permissions_locked(), so this is used to lock down access
  127. * even further, such as enforcing that apps hold sdcard_rw.
  128. */
  129. int check_caller_access_to_name(struct inode *parent_node, const struct qstr *name)
  130. {
  131. struct qstr q_autorun = QSTR_LITERAL("autorun.inf");
  132. struct qstr q__android_secure = QSTR_LITERAL(".android_secure");
  133. struct qstr q_android_secure = QSTR_LITERAL("android_secure");
  134. /* Always block security-sensitive files at root */
  135. if (parent_node && SDCARDFS_I(parent_node)->data->perm == PERM_ROOT) {
  136. if (qstr_case_eq(name, &q_autorun)
  137. || qstr_case_eq(name, &q__android_secure)
  138. || qstr_case_eq(name, &q_android_secure)) {
  139. return 0;
  140. }
  141. }
  142. /* Root always has access; access for any other UIDs should always
  143. * be controlled through packages.list.
  144. */
  145. if (from_kuid(&init_user_ns, current_fsuid()) == 0)
  146. return 1;
  147. /* No extra permissions to enforce */
  148. return 1;
  149. }
  150. static struct hashtable_entry *alloc_hashtable_entry(const struct qstr *key,
  151. appid_t value)
  152. {
  153. struct hashtable_entry *ret = kmem_cache_alloc(hashtable_entry_cachep,
  154. GFP_KERNEL);
  155. if (!ret)
  156. return NULL;
  157. INIT_HLIST_NODE(&ret->dlist);
  158. INIT_HLIST_NODE(&ret->hlist);
  159. if (!qstr_copy(key, &ret->key)) {
  160. kmem_cache_free(hashtable_entry_cachep, ret);
  161. return NULL;
  162. }
  163. atomic_set(&ret->value, value);
  164. return ret;
  165. }
  166. static int insert_packagelist_appid_entry_locked(const struct qstr *key, appid_t value)
  167. {
  168. struct hashtable_entry *hash_cur;
  169. struct hashtable_entry *new_entry;
  170. unsigned int hash = key->hash;
  171. hash_for_each_possible_rcu_new(package_to_appid, hash_cur, hlist, hash) {
  172. if (qstr_case_eq(key, &hash_cur->key)) {
  173. atomic_set(&hash_cur->value, value);
  174. return 0;
  175. }
  176. }
  177. new_entry = alloc_hashtable_entry(key, value);
  178. if (!new_entry)
  179. return -ENOMEM;
  180. hash_add_rcu(package_to_appid, &new_entry->hlist, hash);
  181. return 0;
  182. }
  183. static int insert_ext_gid_entry_locked(const struct qstr *key, appid_t value)
  184. {
  185. struct hashtable_entry *hash_cur;
  186. struct hashtable_entry *new_entry;
  187. unsigned int hash = key->hash;
  188. /* An extension can only belong to one gid */
  189. hash_for_each_possible_rcu_new(ext_to_groupid, hash_cur, hlist, hash) {
  190. if (qstr_case_eq(key, &hash_cur->key))
  191. return -EINVAL;
  192. }
  193. new_entry = alloc_hashtable_entry(key, value);
  194. if (!new_entry)
  195. return -ENOMEM;
  196. hash_add_rcu(ext_to_groupid, &new_entry->hlist, hash);
  197. return 0;
  198. }
  199. static int insert_userid_exclude_entry_locked(const struct qstr *key, userid_t value)
  200. {
  201. struct hashtable_entry *hash_cur;
  202. struct hashtable_entry *new_entry;
  203. unsigned int hash = key->hash;
  204. /* Only insert if not already present */
  205. hash_for_each_possible_rcu_new(package_to_userid, hash_cur, hlist, hash) {
  206. if (atomic_read(&hash_cur->value) == value &&
  207. qstr_case_eq(key, &hash_cur->key))
  208. return 0;
  209. }
  210. new_entry = alloc_hashtable_entry(key, value);
  211. if (!new_entry)
  212. return -ENOMEM;
  213. hash_add_rcu(package_to_userid, &new_entry->hlist, hash);
  214. return 0;
  215. }
  216. static void fixup_all_perms_name(const struct qstr *key)
  217. {
  218. struct sdcardfs_sb_info *sbinfo;
  219. struct limit_search limit = {
  220. .flags = BY_NAME,
  221. .name = QSTR_INIT(key->name, key->len),
  222. };
  223. list_for_each_entry(sbinfo, &sdcardfs_super_list, list) {
  224. if (sbinfo_has_sdcard_magic(sbinfo))
  225. fixup_perms_recursive(sbinfo->sb->s_root, &limit);
  226. }
  227. }
  228. static void fixup_all_perms_name_userid(const struct qstr *key, userid_t userid)
  229. {
  230. struct sdcardfs_sb_info *sbinfo;
  231. struct limit_search limit = {
  232. .flags = BY_NAME | BY_USERID,
  233. .name = QSTR_INIT(key->name, key->len),
  234. .userid = userid,
  235. };
  236. list_for_each_entry(sbinfo, &sdcardfs_super_list, list) {
  237. if (sbinfo_has_sdcard_magic(sbinfo))
  238. fixup_perms_recursive(sbinfo->sb->s_root, &limit);
  239. }
  240. }
  241. static void fixup_all_perms_userid(userid_t userid)
  242. {
  243. struct sdcardfs_sb_info *sbinfo;
  244. struct limit_search limit = {
  245. .flags = BY_USERID,
  246. .userid = userid,
  247. };
  248. list_for_each_entry(sbinfo, &sdcardfs_super_list, list) {
  249. if (sbinfo_has_sdcard_magic(sbinfo))
  250. fixup_perms_recursive(sbinfo->sb->s_root, &limit);
  251. }
  252. }
  253. static int insert_packagelist_entry(const struct qstr *key, appid_t value)
  254. {
  255. int err;
  256. mutex_lock(&sdcardfs_super_list_lock);
  257. err = insert_packagelist_appid_entry_locked(key, value);
  258. if (!err)
  259. fixup_all_perms_name(key);
  260. mutex_unlock(&sdcardfs_super_list_lock);
  261. return err;
  262. }
  263. static int insert_ext_gid_entry(const struct qstr *key, appid_t value)
  264. {
  265. int err;
  266. mutex_lock(&sdcardfs_super_list_lock);
  267. err = insert_ext_gid_entry_locked(key, value);
  268. mutex_unlock(&sdcardfs_super_list_lock);
  269. return err;
  270. }
  271. static int insert_userid_exclude_entry(const struct qstr *key, userid_t value)
  272. {
  273. int err;
  274. mutex_lock(&sdcardfs_super_list_lock);
  275. err = insert_userid_exclude_entry_locked(key, value);
  276. if (!err)
  277. fixup_all_perms_name_userid(key, value);
  278. mutex_unlock(&sdcardfs_super_list_lock);
  279. return err;
  280. }
  281. static void free_hashtable_entry(struct hashtable_entry *entry)
  282. {
  283. kfree(entry->key.name);
  284. kmem_cache_free(hashtable_entry_cachep, entry);
  285. }
  286. static void remove_packagelist_entry_locked(const struct qstr *key)
  287. {
  288. struct hashtable_entry *hash_cur;
  289. unsigned int hash = key->hash;
  290. struct hlist_node *h_t;
  291. HLIST_HEAD(free_list);
  292. hash_for_each_possible_rcu_new(package_to_userid, hash_cur, hlist, hash) {
  293. if (qstr_case_eq(key, &hash_cur->key)) {
  294. hash_del_rcu(&hash_cur->hlist);
  295. hlist_add_head(&hash_cur->dlist, &free_list);
  296. }
  297. }
  298. hash_for_each_possible_rcu_new(package_to_appid, hash_cur, hlist, hash) {
  299. if (qstr_case_eq(key, &hash_cur->key)) {
  300. hash_del_rcu(&hash_cur->hlist);
  301. hlist_add_head(&hash_cur->dlist, &free_list);
  302. break;
  303. }
  304. }
  305. synchronize_rcu();
  306. hlist_for_each_entry_safe_new(hash_cur, h_t, &free_list, dlist)
  307. free_hashtable_entry(hash_cur);
  308. }
  309. static void remove_packagelist_entry(const struct qstr *key)
  310. {
  311. mutex_lock(&sdcardfs_super_list_lock);
  312. remove_packagelist_entry_locked(key);
  313. fixup_all_perms_name(key);
  314. mutex_unlock(&sdcardfs_super_list_lock);
  315. }
  316. static void remove_ext_gid_entry_locked(const struct qstr *key, gid_t group)
  317. {
  318. struct hashtable_entry *hash_cur;
  319. unsigned int hash = key->hash;
  320. hash_for_each_possible_rcu_new(ext_to_groupid, hash_cur, hlist, hash) {
  321. if (qstr_case_eq(key, &hash_cur->key) && atomic_read(&hash_cur->value) == group) {
  322. hash_del_rcu(&hash_cur->hlist);
  323. synchronize_rcu();
  324. free_hashtable_entry(hash_cur);
  325. break;
  326. }
  327. }
  328. }
  329. static void remove_ext_gid_entry(const struct qstr *key, gid_t group)
  330. {
  331. mutex_lock(&sdcardfs_super_list_lock);
  332. remove_ext_gid_entry_locked(key, group);
  333. mutex_unlock(&sdcardfs_super_list_lock);
  334. }
  335. static void remove_userid_all_entry_locked(userid_t userid)
  336. {
  337. struct hashtable_entry *hash_cur;
  338. struct hlist_node *h_t;
  339. HLIST_HEAD(free_list);
  340. int i;
  341. hash_for_each_rcu_new(package_to_userid, i, hash_cur, hlist) {
  342. if (atomic_read(&hash_cur->value) == userid) {
  343. hash_del_rcu(&hash_cur->hlist);
  344. hlist_add_head(&hash_cur->dlist, &free_list);
  345. }
  346. }
  347. synchronize_rcu();
  348. hlist_for_each_entry_safe_new(hash_cur, h_t, &free_list, dlist) {
  349. free_hashtable_entry(hash_cur);
  350. }
  351. }
  352. static void remove_userid_all_entry(userid_t userid)
  353. {
  354. mutex_lock(&sdcardfs_super_list_lock);
  355. remove_userid_all_entry_locked(userid);
  356. fixup_all_perms_userid(userid);
  357. mutex_unlock(&sdcardfs_super_list_lock);
  358. }
  359. static void remove_userid_exclude_entry_locked(const struct qstr *key, userid_t userid)
  360. {
  361. struct hashtable_entry *hash_cur;
  362. unsigned int hash = key->hash;
  363. hash_for_each_possible_rcu_new(package_to_userid, hash_cur, hlist, hash) {
  364. if (qstr_case_eq(key, &hash_cur->key) &&
  365. atomic_read(&hash_cur->value) == userid) {
  366. hash_del_rcu(&hash_cur->hlist);
  367. synchronize_rcu();
  368. free_hashtable_entry(hash_cur);
  369. break;
  370. }
  371. }
  372. }
  373. static void remove_userid_exclude_entry(const struct qstr *key, userid_t userid)
  374. {
  375. mutex_lock(&sdcardfs_super_list_lock);
  376. remove_userid_exclude_entry_locked(key, userid);
  377. fixup_all_perms_name_userid(key, userid);
  378. mutex_unlock(&sdcardfs_super_list_lock);
  379. }
  380. static void packagelist_destroy(void)
  381. {
  382. struct hashtable_entry *hash_cur;
  383. struct hlist_node *h_t;
  384. HLIST_HEAD(free_list);
  385. int i;
  386. mutex_lock(&sdcardfs_super_list_lock);
  387. hash_for_each_rcu_new(package_to_appid, i, hash_cur, hlist) {
  388. hash_del_rcu(&hash_cur->hlist);
  389. hlist_add_head(&hash_cur->dlist, &free_list);
  390. }
  391. hash_for_each_rcu_new(package_to_userid, i, hash_cur, hlist) {
  392. hash_del_rcu(&hash_cur->hlist);
  393. hlist_add_head(&hash_cur->dlist, &free_list);
  394. }
  395. synchronize_rcu();
  396. hlist_for_each_entry_safe_new(hash_cur, h_t, &free_list, dlist)
  397. free_hashtable_entry(hash_cur);
  398. mutex_unlock(&sdcardfs_super_list_lock);
  399. pr_info("sdcardfs: destroyed packagelist pkgld\n");
  400. }
  401. struct package_details {
  402. struct config_item item;
  403. struct qstr name;
  404. };
  405. static inline struct package_details *to_package_details(struct config_item *item)
  406. {
  407. return item ? container_of(item, struct package_details, item) : NULL;
  408. }
  409. CONFIGFS_ATTR_STRUCT(package_details);
  410. #define PACKAGE_DETAILS_ATTR(_name, _mode, _show, _store) \
  411. struct package_details_attribute package_details_attr_##_name = __CONFIGFS_ATTR(_name, _mode, _show, _store)
  412. #define PACKAGE_DETAILS_ATTRIBUTE(name) (&package_details_attr_##name.attr)
  413. static ssize_t package_details_appid_show(struct package_details *package_details,
  414. char *page)
  415. {
  416. return scnprintf(page, PAGE_SIZE, "%u\n", __get_appid(&package_details->name));
  417. }
  418. static ssize_t package_details_appid_store(struct package_details *package_details,
  419. const char *page, size_t count)
  420. {
  421. unsigned int tmp;
  422. int ret;
  423. ret = kstrtouint(page, 10, &tmp);
  424. if (ret)
  425. return ret;
  426. ret = insert_packagelist_entry(&package_details->name, tmp);
  427. if (ret)
  428. return ret;
  429. return count;
  430. }
  431. static ssize_t package_details_excluded_userids_show(struct package_details *package_details,
  432. char *page)
  433. {
  434. struct hashtable_entry *hash_cur;
  435. unsigned int hash = package_details->name.hash;
  436. int count = 0;
  437. rcu_read_lock();
  438. hash_for_each_possible_rcu_new(package_to_userid, hash_cur, hlist, hash) {
  439. if (qstr_case_eq(&package_details->name, &hash_cur->key))
  440. count += scnprintf(page + count, PAGE_SIZE - count,
  441. "%d ", atomic_read(&hash_cur->value));
  442. }
  443. rcu_read_unlock();
  444. if (count)
  445. count--;
  446. count += scnprintf(page + count, PAGE_SIZE - count, "\n");
  447. return count;
  448. }
  449. static ssize_t package_details_excluded_userids_store(struct package_details *package_details,
  450. const char *page, size_t count)
  451. {
  452. unsigned int tmp;
  453. int ret;
  454. ret = kstrtouint(page, 10, &tmp);
  455. if (ret)
  456. return ret;
  457. ret = insert_userid_exclude_entry(&package_details->name, tmp);
  458. if (ret)
  459. return ret;
  460. return count;
  461. }
  462. static ssize_t package_details_clear_userid_store(struct package_details *package_details,
  463. const char *page, size_t count)
  464. {
  465. unsigned int tmp;
  466. int ret;
  467. ret = kstrtouint(page, 10, &tmp);
  468. if (ret)
  469. return ret;
  470. remove_userid_exclude_entry(&package_details->name, tmp);
  471. return count;
  472. }
  473. static void package_details_release(struct config_item *item)
  474. {
  475. struct package_details *package_details = to_package_details(item);
  476. pr_info("sdcardfs: removing %s\n", package_details->name.name);
  477. remove_packagelist_entry(&package_details->name);
  478. kfree(package_details->name.name);
  479. kfree(package_details);
  480. }
  481. PACKAGE_DETAILS_ATTR(appid, S_IRUGO | S_IWUGO, package_details_appid_show, package_details_appid_store);
  482. PACKAGE_DETAILS_ATTR(excluded_userids, S_IRUGO | S_IWUGO,
  483. package_details_excluded_userids_show, package_details_excluded_userids_store);
  484. PACKAGE_DETAILS_ATTR(clear_userid, S_IWUGO, NULL, package_details_clear_userid_store);
  485. static struct configfs_attribute *package_details_attrs[] = {
  486. PACKAGE_DETAILS_ATTRIBUTE(appid),
  487. PACKAGE_DETAILS_ATTRIBUTE(excluded_userids),
  488. PACKAGE_DETAILS_ATTRIBUTE(clear_userid),
  489. NULL,
  490. };
  491. CONFIGFS_ATTR_OPS(package_details);
  492. static struct configfs_item_operations package_details_item_ops = {
  493. .release = package_details_release,
  494. .show_attribute = package_details_attr_show,
  495. .store_attribute = package_details_attr_store,
  496. };
  497. static struct config_item_type package_appid_type = {
  498. .ct_item_ops = &package_details_item_ops,
  499. .ct_attrs = package_details_attrs,
  500. .ct_owner = THIS_MODULE,
  501. };
  502. struct extensions_value {
  503. struct config_group group;
  504. unsigned int num;
  505. };
  506. struct extension_details {
  507. struct config_item item;
  508. struct qstr name;
  509. unsigned int num;
  510. };
  511. static inline struct extensions_value *to_extensions_value(struct config_item *item)
  512. {
  513. return item ? container_of(to_config_group(item), struct extensions_value, group) : NULL;
  514. }
  515. static inline struct extension_details *to_extension_details(struct config_item *item)
  516. {
  517. return item ? container_of(item, struct extension_details, item) : NULL;
  518. }
  519. static void extension_details_release(struct config_item *item)
  520. {
  521. struct extension_details *extension_details = to_extension_details(item);
  522. pr_info("sdcardfs: No longer mapping %s files to gid %d\n",
  523. extension_details->name.name, extension_details->num);
  524. remove_ext_gid_entry(&extension_details->name, extension_details->num);
  525. kfree(extension_details->name.name);
  526. kfree(extension_details);
  527. }
  528. static struct configfs_item_operations extension_details_item_ops = {
  529. .release = extension_details_release,
  530. };
  531. static struct config_item_type extension_details_type = {
  532. .ct_item_ops = &extension_details_item_ops,
  533. .ct_owner = THIS_MODULE,
  534. };
  535. static struct config_item *extension_details_make_item(struct config_group *group, const char *name)
  536. {
  537. struct extensions_value *extensions_value = to_extensions_value(&group->cg_item);
  538. struct extension_details *extension_details = kzalloc(sizeof(struct extension_details), GFP_KERNEL);
  539. const char *tmp;
  540. int ret;
  541. if (!extension_details)
  542. return ERR_PTR(-ENOMEM);
  543. tmp = kstrdup(name, GFP_KERNEL);
  544. if (!tmp) {
  545. kfree(extension_details);
  546. return ERR_PTR(-ENOMEM);
  547. }
  548. qstr_init(&extension_details->name, tmp);
  549. extension_details->num = extensions_value->num;
  550. ret = insert_ext_gid_entry(&extension_details->name, extensions_value->num);
  551. if (ret) {
  552. kfree(extension_details->name.name);
  553. kfree(extension_details);
  554. return ERR_PTR(ret);
  555. }
  556. config_item_init_type_name(&extension_details->item, name, &extension_details_type);
  557. return &extension_details->item;
  558. }
  559. static struct configfs_group_operations extensions_value_group_ops = {
  560. .make_item = extension_details_make_item,
  561. };
  562. static struct config_item_type extensions_name_type = {
  563. .ct_group_ops = &extensions_value_group_ops,
  564. .ct_owner = THIS_MODULE,
  565. };
  566. static struct config_group *extensions_make_group(struct config_group *group, const char *name)
  567. {
  568. struct extensions_value *extensions_value;
  569. unsigned int tmp;
  570. int ret;
  571. extensions_value = kzalloc(sizeof(struct extensions_value), GFP_KERNEL);
  572. if (!extensions_value)
  573. return ERR_PTR(-ENOMEM);
  574. ret = kstrtouint(name, 10, &tmp);
  575. if (ret) {
  576. kfree(extensions_value);
  577. return ERR_PTR(ret);
  578. }
  579. extensions_value->num = tmp;
  580. config_group_init_type_name(&extensions_value->group, name,
  581. &extensions_name_type);
  582. return &extensions_value->group;
  583. }
  584. static void extensions_drop_group(struct config_group *group, struct config_item *item)
  585. {
  586. struct extensions_value *value = to_extensions_value(item);
  587. pr_info("sdcardfs: No longer mapping any files to gid %d\n", value->num);
  588. kfree(value);
  589. }
  590. static struct configfs_group_operations extensions_group_ops = {
  591. .make_group = extensions_make_group,
  592. .drop_item = extensions_drop_group,
  593. };
  594. static struct config_item_type extensions_type = {
  595. .ct_group_ops = &extensions_group_ops,
  596. .ct_owner = THIS_MODULE,
  597. };
  598. struct config_group extension_group = {
  599. .cg_item = {
  600. .ci_namebuf = "extensions",
  601. .ci_type = &extensions_type,
  602. },
  603. };
  604. struct packages {
  605. struct configfs_subsystem subsystem;
  606. };
  607. static inline struct packages *to_packages(struct config_item *item)
  608. {
  609. return item ? container_of(to_configfs_subsystem(to_config_group(item)), struct packages, subsystem) : NULL;
  610. }
  611. CONFIGFS_ATTR_STRUCT(packages);
  612. #define PACKAGES_ATTR(_name, _mode, _show, _store) \
  613. struct packages_attribute packages_attr_##_name = __CONFIGFS_ATTR(_name, _mode, _show, _store)
  614. #define PACKAGES_ATTR_RO(_name, _show) \
  615. struct packages_attribute packages_attr_##_name = __CONFIGFS_ATTR_RO(_name, _show)
  616. static struct config_item *packages_make_item(struct config_group *group, const char *name)
  617. {
  618. struct package_details *package_details;
  619. const char *tmp;
  620. package_details = kzalloc(sizeof(struct package_details), GFP_KERNEL);
  621. if (!package_details)
  622. return ERR_PTR(-ENOMEM);
  623. tmp = kstrdup(name, GFP_KERNEL);
  624. if (!tmp) {
  625. kfree(package_details);
  626. return ERR_PTR(-ENOMEM);
  627. }
  628. qstr_init(&package_details->name, tmp);
  629. config_item_init_type_name(&package_details->item, name,
  630. &package_appid_type);
  631. return &package_details->item;
  632. }
  633. static ssize_t packages_list_show(struct packages *packages,
  634. char *page)
  635. {
  636. struct hashtable_entry *hash_cur_app;
  637. struct hashtable_entry *hash_cur_user;
  638. int i;
  639. int count = 0, written = 0;
  640. const char errormsg[] = "<truncated>\n";
  641. unsigned int hash;
  642. rcu_read_lock();
  643. hash_for_each_rcu_new(package_to_appid, i, hash_cur_app, hlist) {
  644. written = scnprintf(page + count, PAGE_SIZE - sizeof(errormsg) - count, "%s %d\n",
  645. hash_cur_app->key.name, atomic_read(&hash_cur_app->value));
  646. hash = hash_cur_app->key.hash;
  647. hash_for_each_possible_rcu_new(package_to_userid, hash_cur_user, hlist, hash) {
  648. if (qstr_case_eq(&hash_cur_app->key, &hash_cur_user->key)) {
  649. written += scnprintf(page + count + written - 1,
  650. PAGE_SIZE - sizeof(errormsg) - count - written + 1,
  651. " %d\n", atomic_read(&hash_cur_user->value)) - 1;
  652. }
  653. }
  654. if (count + written == PAGE_SIZE - sizeof(errormsg) - 1) {
  655. count += scnprintf(page + count, PAGE_SIZE - count, errormsg);
  656. break;
  657. }
  658. count += written;
  659. }
  660. rcu_read_unlock();
  661. return count;
  662. }
  663. static ssize_t packages_remove_userid_store(struct packages *packages,
  664. const char *page, size_t count)
  665. {
  666. unsigned int tmp;
  667. int ret;
  668. ret = kstrtouint(page, 10, &tmp);
  669. if (ret)
  670. return ret;
  671. remove_userid_all_entry(tmp);
  672. return count;
  673. }
  674. struct packages_attribute packages_attr_packages_gid_list = __CONFIGFS_ATTR_RO(packages_gid.list, packages_list_show);
  675. PACKAGES_ATTR(remove_userid, S_IWUGO, NULL, packages_remove_userid_store);
  676. static struct configfs_attribute *packages_attrs[] = {
  677. &packages_attr_packages_gid_list.attr,
  678. &packages_attr_remove_userid.attr,
  679. NULL,
  680. };
  681. CONFIGFS_ATTR_OPS(packages)
  682. static struct configfs_item_operations packages_item_ops = {
  683. .show_attribute = packages_attr_show,
  684. .store_attribute = packages_attr_store,
  685. };
  686. /*
  687. * Note that, since no extra work is required on ->drop_item(),
  688. * no ->drop_item() is provided.
  689. */
  690. static struct configfs_group_operations packages_group_ops = {
  691. .make_item = packages_make_item,
  692. };
  693. static struct config_item_type packages_type = {
  694. .ct_item_ops = &packages_item_ops,
  695. .ct_group_ops = &packages_group_ops,
  696. .ct_attrs = packages_attrs,
  697. .ct_owner = THIS_MODULE,
  698. };
  699. struct config_group *sd_default_groups[] = {
  700. &extension_group,
  701. NULL,
  702. };
  703. static struct packages sdcardfs_packages = {
  704. .subsystem = {
  705. .su_group = {
  706. .cg_item = {
  707. .ci_namebuf = "sdcardfs",
  708. .ci_type = &packages_type,
  709. },
  710. .default_groups = sd_default_groups,
  711. },
  712. },
  713. };
  714. static int configfs_sdcardfs_init(void)
  715. {
  716. int ret, i;
  717. struct configfs_subsystem *subsys = &sdcardfs_packages.subsystem;
  718. for (i = 0; sd_default_groups[i]; i++)
  719. config_group_init(sd_default_groups[i]);
  720. config_group_init(&subsys->su_group);
  721. mutex_init(&subsys->su_mutex);
  722. ret = configfs_register_subsystem(subsys);
  723. if (ret) {
  724. pr_err("Error %d while registering subsystem %s\n",
  725. ret,
  726. subsys->su_group.cg_item.ci_namebuf);
  727. }
  728. return ret;
  729. }
  730. static void configfs_sdcardfs_exit(void)
  731. {
  732. configfs_unregister_subsystem(&sdcardfs_packages.subsystem);
  733. }
  734. int packagelist_init(void)
  735. {
  736. hashtable_entry_cachep =
  737. kmem_cache_create("packagelist_hashtable_entry",
  738. sizeof(struct hashtable_entry), 0, 0, NULL);
  739. if (!hashtable_entry_cachep) {
  740. pr_err("sdcardfs: failed creating pkgl_hashtable entry slab cache\n");
  741. return -ENOMEM;
  742. }
  743. configfs_sdcardfs_init();
  744. return 0;
  745. }
  746. void packagelist_exit(void)
  747. {
  748. configfs_sdcardfs_exit();
  749. packagelist_destroy();
  750. kmem_cache_destroy(hashtable_entry_cachep);
  751. }