cache.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * net/sunrpc/cache.c
  4. *
  5. * Generic code for various authentication-related caches
  6. * used by sunrpc clients and servers.
  7. *
  8. * Copyright (C) 2002 Neil Brown <neilb@cse.unsw.edu.au>
  9. */
  10. #include <linux/types.h>
  11. #include <linux/fs.h>
  12. #include <linux/file.h>
  13. #include <linux/slab.h>
  14. #include <linux/signal.h>
  15. #include <linux/sched.h>
  16. #include <linux/kmod.h>
  17. #include <linux/list.h>
  18. #include <linux/module.h>
  19. #include <linux/ctype.h>
  20. #include <linux/string_helpers.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/poll.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/proc_fs.h>
  25. #include <linux/net.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/mutex.h>
  28. #include <linux/pagemap.h>
  29. #include <asm/ioctls.h>
  30. #include <linux/sunrpc/types.h>
  31. #include <linux/sunrpc/cache.h>
  32. #include <linux/sunrpc/stats.h>
  33. #include <linux/sunrpc/rpc_pipe_fs.h>
  34. #include <trace/events/sunrpc.h>
  35. #include "netns.h"
  36. #include "fail.h"
  37. #define RPCDBG_FACILITY RPCDBG_CACHE
  38. static bool cache_defer_req(struct cache_req *req, struct cache_head *item);
  39. static void cache_revisit_request(struct cache_head *item);
  40. static void cache_init(struct cache_head *h, struct cache_detail *detail)
  41. {
  42. time64_t now = seconds_since_boot();
  43. INIT_HLIST_NODE(&h->cache_list);
  44. h->flags = 0;
  45. kref_init(&h->ref);
  46. h->expiry_time = now + CACHE_NEW_EXPIRY;
  47. if (now <= detail->flush_time)
  48. /* ensure it isn't already expired */
  49. now = detail->flush_time + 1;
  50. h->last_refresh = now;
  51. }
  52. static void cache_fresh_unlocked(struct cache_head *head,
  53. struct cache_detail *detail);
  54. static struct cache_head *sunrpc_cache_find_rcu(struct cache_detail *detail,
  55. struct cache_head *key,
  56. int hash)
  57. {
  58. struct hlist_head *head = &detail->hash_table[hash];
  59. struct cache_head *tmp;
  60. rcu_read_lock();
  61. hlist_for_each_entry_rcu(tmp, head, cache_list) {
  62. if (!detail->match(tmp, key))
  63. continue;
  64. if (test_bit(CACHE_VALID, &tmp->flags) &&
  65. cache_is_expired(detail, tmp))
  66. continue;
  67. tmp = cache_get_rcu(tmp);
  68. rcu_read_unlock();
  69. return tmp;
  70. }
  71. rcu_read_unlock();
  72. return NULL;
  73. }
  74. static void sunrpc_begin_cache_remove_entry(struct cache_head *ch,
  75. struct cache_detail *cd)
  76. {
  77. /* Must be called under cd->hash_lock */
  78. hlist_del_init_rcu(&ch->cache_list);
  79. set_bit(CACHE_CLEANED, &ch->flags);
  80. cd->entries --;
  81. }
  82. static void sunrpc_end_cache_remove_entry(struct cache_head *ch,
  83. struct cache_detail *cd)
  84. {
  85. cache_fresh_unlocked(ch, cd);
  86. cache_put(ch, cd);
  87. }
  88. static struct cache_head *sunrpc_cache_add_entry(struct cache_detail *detail,
  89. struct cache_head *key,
  90. int hash)
  91. {
  92. struct cache_head *new, *tmp, *freeme = NULL;
  93. struct hlist_head *head = &detail->hash_table[hash];
  94. new = detail->alloc();
  95. if (!new)
  96. return NULL;
  97. /* must fully initialise 'new', else
  98. * we might get lose if we need to
  99. * cache_put it soon.
  100. */
  101. cache_init(new, detail);
  102. detail->init(new, key);
  103. spin_lock(&detail->hash_lock);
  104. /* check if entry appeared while we slept */
  105. hlist_for_each_entry_rcu(tmp, head, cache_list,
  106. lockdep_is_held(&detail->hash_lock)) {
  107. if (!detail->match(tmp, key))
  108. continue;
  109. if (test_bit(CACHE_VALID, &tmp->flags) &&
  110. cache_is_expired(detail, tmp)) {
  111. sunrpc_begin_cache_remove_entry(tmp, detail);
  112. trace_cache_entry_expired(detail, tmp);
  113. freeme = tmp;
  114. break;
  115. }
  116. cache_get(tmp);
  117. spin_unlock(&detail->hash_lock);
  118. cache_put(new, detail);
  119. return tmp;
  120. }
  121. hlist_add_head_rcu(&new->cache_list, head);
  122. detail->entries++;
  123. cache_get(new);
  124. spin_unlock(&detail->hash_lock);
  125. if (freeme)
  126. sunrpc_end_cache_remove_entry(freeme, detail);
  127. return new;
  128. }
  129. struct cache_head *sunrpc_cache_lookup_rcu(struct cache_detail *detail,
  130. struct cache_head *key, int hash)
  131. {
  132. struct cache_head *ret;
  133. ret = sunrpc_cache_find_rcu(detail, key, hash);
  134. if (ret)
  135. return ret;
  136. /* Didn't find anything, insert an empty entry */
  137. return sunrpc_cache_add_entry(detail, key, hash);
  138. }
  139. EXPORT_SYMBOL_GPL(sunrpc_cache_lookup_rcu);
  140. static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
  141. static void cache_fresh_locked(struct cache_head *head, time64_t expiry,
  142. struct cache_detail *detail)
  143. {
  144. time64_t now = seconds_since_boot();
  145. if (now <= detail->flush_time)
  146. /* ensure it isn't immediately treated as expired */
  147. now = detail->flush_time + 1;
  148. head->expiry_time = expiry;
  149. head->last_refresh = now;
  150. smp_wmb(); /* paired with smp_rmb() in cache_is_valid() */
  151. set_bit(CACHE_VALID, &head->flags);
  152. }
  153. static void cache_fresh_unlocked(struct cache_head *head,
  154. struct cache_detail *detail)
  155. {
  156. if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
  157. cache_revisit_request(head);
  158. cache_dequeue(detail, head);
  159. }
  160. }
  161. static void cache_make_negative(struct cache_detail *detail,
  162. struct cache_head *h)
  163. {
  164. set_bit(CACHE_NEGATIVE, &h->flags);
  165. trace_cache_entry_make_negative(detail, h);
  166. }
  167. static void cache_entry_update(struct cache_detail *detail,
  168. struct cache_head *h,
  169. struct cache_head *new)
  170. {
  171. if (!test_bit(CACHE_NEGATIVE, &new->flags)) {
  172. detail->update(h, new);
  173. trace_cache_entry_update(detail, h);
  174. } else {
  175. cache_make_negative(detail, h);
  176. }
  177. }
  178. struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
  179. struct cache_head *new, struct cache_head *old, int hash)
  180. {
  181. /* The 'old' entry is to be replaced by 'new'.
  182. * If 'old' is not VALID, we update it directly,
  183. * otherwise we need to replace it
  184. */
  185. struct cache_head *tmp;
  186. if (!test_bit(CACHE_VALID, &old->flags)) {
  187. spin_lock(&detail->hash_lock);
  188. if (!test_bit(CACHE_VALID, &old->flags)) {
  189. cache_entry_update(detail, old, new);
  190. cache_fresh_locked(old, new->expiry_time, detail);
  191. spin_unlock(&detail->hash_lock);
  192. cache_fresh_unlocked(old, detail);
  193. return old;
  194. }
  195. spin_unlock(&detail->hash_lock);
  196. }
  197. /* We need to insert a new entry */
  198. tmp = detail->alloc();
  199. if (!tmp) {
  200. cache_put(old, detail);
  201. return NULL;
  202. }
  203. cache_init(tmp, detail);
  204. detail->init(tmp, old);
  205. spin_lock(&detail->hash_lock);
  206. cache_entry_update(detail, tmp, new);
  207. hlist_add_head(&tmp->cache_list, &detail->hash_table[hash]);
  208. detail->entries++;
  209. cache_get(tmp);
  210. cache_fresh_locked(tmp, new->expiry_time, detail);
  211. cache_fresh_locked(old, 0, detail);
  212. spin_unlock(&detail->hash_lock);
  213. cache_fresh_unlocked(tmp, detail);
  214. cache_fresh_unlocked(old, detail);
  215. cache_put(old, detail);
  216. return tmp;
  217. }
  218. EXPORT_SYMBOL_GPL(sunrpc_cache_update);
  219. static inline int cache_is_valid(struct cache_head *h)
  220. {
  221. if (!test_bit(CACHE_VALID, &h->flags))
  222. return -EAGAIN;
  223. else {
  224. /* entry is valid */
  225. if (test_bit(CACHE_NEGATIVE, &h->flags))
  226. return -ENOENT;
  227. else {
  228. /*
  229. * In combination with write barrier in
  230. * sunrpc_cache_update, ensures that anyone
  231. * using the cache entry after this sees the
  232. * updated contents:
  233. */
  234. smp_rmb();
  235. return 0;
  236. }
  237. }
  238. }
  239. static int try_to_negate_entry(struct cache_detail *detail, struct cache_head *h)
  240. {
  241. int rv;
  242. spin_lock(&detail->hash_lock);
  243. rv = cache_is_valid(h);
  244. if (rv == -EAGAIN) {
  245. cache_make_negative(detail, h);
  246. cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY,
  247. detail);
  248. rv = -ENOENT;
  249. }
  250. spin_unlock(&detail->hash_lock);
  251. cache_fresh_unlocked(h, detail);
  252. return rv;
  253. }
  254. /*
  255. * This is the generic cache management routine for all
  256. * the authentication caches.
  257. * It checks the currency of a cache item and will (later)
  258. * initiate an upcall to fill it if needed.
  259. *
  260. *
  261. * Returns 0 if the cache_head can be used, or cache_puts it and returns
  262. * -EAGAIN if upcall is pending and request has been queued
  263. * -ETIMEDOUT if upcall failed or request could not be queue or
  264. * upcall completed but item is still invalid (implying that
  265. * the cache item has been replaced with a newer one).
  266. * -ENOENT if cache entry was negative
  267. */
  268. int cache_check(struct cache_detail *detail,
  269. struct cache_head *h, struct cache_req *rqstp)
  270. {
  271. int rv;
  272. time64_t refresh_age, age;
  273. /* First decide return status as best we can */
  274. rv = cache_is_valid(h);
  275. /* now see if we want to start an upcall */
  276. refresh_age = (h->expiry_time - h->last_refresh);
  277. age = seconds_since_boot() - h->last_refresh;
  278. if (rqstp == NULL) {
  279. if (rv == -EAGAIN)
  280. rv = -ENOENT;
  281. } else if (rv == -EAGAIN ||
  282. (h->expiry_time != 0 && age > refresh_age/2)) {
  283. dprintk("RPC: Want update, refage=%lld, age=%lld\n",
  284. refresh_age, age);
  285. switch (detail->cache_upcall(detail, h)) {
  286. case -EINVAL:
  287. rv = try_to_negate_entry(detail, h);
  288. break;
  289. case -EAGAIN:
  290. cache_fresh_unlocked(h, detail);
  291. break;
  292. }
  293. }
  294. if (rv == -EAGAIN) {
  295. if (!cache_defer_req(rqstp, h)) {
  296. /*
  297. * Request was not deferred; handle it as best
  298. * we can ourselves:
  299. */
  300. rv = cache_is_valid(h);
  301. if (rv == -EAGAIN)
  302. rv = -ETIMEDOUT;
  303. }
  304. }
  305. if (rv)
  306. cache_put(h, detail);
  307. return rv;
  308. }
  309. EXPORT_SYMBOL_GPL(cache_check);
  310. /*
  311. * caches need to be periodically cleaned.
  312. * For this we maintain a list of cache_detail and
  313. * a current pointer into that list and into the table
  314. * for that entry.
  315. *
  316. * Each time cache_clean is called it finds the next non-empty entry
  317. * in the current table and walks the list in that entry
  318. * looking for entries that can be removed.
  319. *
  320. * An entry gets removed if:
  321. * - The expiry is before current time
  322. * - The last_refresh time is before the flush_time for that cache
  323. *
  324. * later we might drop old entries with non-NEVER expiry if that table
  325. * is getting 'full' for some definition of 'full'
  326. *
  327. * The question of "how often to scan a table" is an interesting one
  328. * and is answered in part by the use of the "nextcheck" field in the
  329. * cache_detail.
  330. * When a scan of a table begins, the nextcheck field is set to a time
  331. * that is well into the future.
  332. * While scanning, if an expiry time is found that is earlier than the
  333. * current nextcheck time, nextcheck is set to that expiry time.
  334. * If the flush_time is ever set to a time earlier than the nextcheck
  335. * time, the nextcheck time is then set to that flush_time.
  336. *
  337. * A table is then only scanned if the current time is at least
  338. * the nextcheck time.
  339. *
  340. */
  341. static LIST_HEAD(cache_list);
  342. static DEFINE_SPINLOCK(cache_list_lock);
  343. static struct cache_detail *current_detail;
  344. static int current_index;
  345. static void do_cache_clean(struct work_struct *work);
  346. static struct delayed_work cache_cleaner;
  347. void sunrpc_init_cache_detail(struct cache_detail *cd)
  348. {
  349. spin_lock_init(&cd->hash_lock);
  350. INIT_LIST_HEAD(&cd->queue);
  351. spin_lock(&cache_list_lock);
  352. cd->nextcheck = 0;
  353. cd->entries = 0;
  354. atomic_set(&cd->writers, 0);
  355. cd->last_close = 0;
  356. cd->last_warn = -1;
  357. list_add(&cd->others, &cache_list);
  358. spin_unlock(&cache_list_lock);
  359. /* start the cleaning process */
  360. queue_delayed_work(system_power_efficient_wq, &cache_cleaner, 0);
  361. }
  362. EXPORT_SYMBOL_GPL(sunrpc_init_cache_detail);
  363. void sunrpc_destroy_cache_detail(struct cache_detail *cd)
  364. {
  365. cache_purge(cd);
  366. spin_lock(&cache_list_lock);
  367. spin_lock(&cd->hash_lock);
  368. if (current_detail == cd)
  369. current_detail = NULL;
  370. list_del_init(&cd->others);
  371. spin_unlock(&cd->hash_lock);
  372. spin_unlock(&cache_list_lock);
  373. if (list_empty(&cache_list)) {
  374. /* module must be being unloaded so its safe to kill the worker */
  375. cancel_delayed_work_sync(&cache_cleaner);
  376. }
  377. }
  378. EXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail);
  379. /* clean cache tries to find something to clean
  380. * and cleans it.
  381. * It returns 1 if it cleaned something,
  382. * 0 if it didn't find anything this time
  383. * -1 if it fell off the end of the list.
  384. */
  385. static int cache_clean(void)
  386. {
  387. int rv = 0;
  388. struct list_head *next;
  389. spin_lock(&cache_list_lock);
  390. /* find a suitable table if we don't already have one */
  391. while (current_detail == NULL ||
  392. current_index >= current_detail->hash_size) {
  393. if (current_detail)
  394. next = current_detail->others.next;
  395. else
  396. next = cache_list.next;
  397. if (next == &cache_list) {
  398. current_detail = NULL;
  399. spin_unlock(&cache_list_lock);
  400. return -1;
  401. }
  402. current_detail = list_entry(next, struct cache_detail, others);
  403. if (current_detail->nextcheck > seconds_since_boot())
  404. current_index = current_detail->hash_size;
  405. else {
  406. current_index = 0;
  407. current_detail->nextcheck = seconds_since_boot()+30*60;
  408. }
  409. }
  410. /* find a non-empty bucket in the table */
  411. while (current_detail &&
  412. current_index < current_detail->hash_size &&
  413. hlist_empty(&current_detail->hash_table[current_index]))
  414. current_index++;
  415. /* find a cleanable entry in the bucket and clean it, or set to next bucket */
  416. if (current_detail && current_index < current_detail->hash_size) {
  417. struct cache_head *ch = NULL;
  418. struct cache_detail *d;
  419. struct hlist_head *head;
  420. struct hlist_node *tmp;
  421. spin_lock(&current_detail->hash_lock);
  422. /* Ok, now to clean this strand */
  423. head = &current_detail->hash_table[current_index];
  424. hlist_for_each_entry_safe(ch, tmp, head, cache_list) {
  425. if (current_detail->nextcheck > ch->expiry_time)
  426. current_detail->nextcheck = ch->expiry_time+1;
  427. if (!cache_is_expired(current_detail, ch))
  428. continue;
  429. sunrpc_begin_cache_remove_entry(ch, current_detail);
  430. trace_cache_entry_expired(current_detail, ch);
  431. rv = 1;
  432. break;
  433. }
  434. spin_unlock(&current_detail->hash_lock);
  435. d = current_detail;
  436. if (!ch)
  437. current_index ++;
  438. spin_unlock(&cache_list_lock);
  439. if (ch)
  440. sunrpc_end_cache_remove_entry(ch, d);
  441. } else
  442. spin_unlock(&cache_list_lock);
  443. return rv;
  444. }
  445. /*
  446. * We want to regularly clean the cache, so we need to schedule some work ...
  447. */
  448. static void do_cache_clean(struct work_struct *work)
  449. {
  450. int delay;
  451. if (list_empty(&cache_list))
  452. return;
  453. if (cache_clean() == -1)
  454. delay = round_jiffies_relative(30*HZ);
  455. else
  456. delay = 5;
  457. queue_delayed_work(system_power_efficient_wq, &cache_cleaner, delay);
  458. }
  459. /*
  460. * Clean all caches promptly. This just calls cache_clean
  461. * repeatedly until we are sure that every cache has had a chance to
  462. * be fully cleaned
  463. */
  464. void cache_flush(void)
  465. {
  466. while (cache_clean() != -1)
  467. cond_resched();
  468. while (cache_clean() != -1)
  469. cond_resched();
  470. }
  471. EXPORT_SYMBOL_GPL(cache_flush);
  472. void cache_purge(struct cache_detail *detail)
  473. {
  474. struct cache_head *ch = NULL;
  475. struct hlist_head *head = NULL;
  476. int i = 0;
  477. spin_lock(&detail->hash_lock);
  478. if (!detail->entries) {
  479. spin_unlock(&detail->hash_lock);
  480. return;
  481. }
  482. dprintk("RPC: %d entries in %s cache\n", detail->entries, detail->name);
  483. for (i = 0; i < detail->hash_size; i++) {
  484. head = &detail->hash_table[i];
  485. while (!hlist_empty(head)) {
  486. ch = hlist_entry(head->first, struct cache_head,
  487. cache_list);
  488. sunrpc_begin_cache_remove_entry(ch, detail);
  489. spin_unlock(&detail->hash_lock);
  490. sunrpc_end_cache_remove_entry(ch, detail);
  491. spin_lock(&detail->hash_lock);
  492. }
  493. }
  494. spin_unlock(&detail->hash_lock);
  495. }
  496. EXPORT_SYMBOL_GPL(cache_purge);
  497. /*
  498. * Deferral and Revisiting of Requests.
  499. *
  500. * If a cache lookup finds a pending entry, we
  501. * need to defer the request and revisit it later.
  502. * All deferred requests are stored in a hash table,
  503. * indexed by "struct cache_head *".
  504. * As it may be wasteful to store a whole request
  505. * structure, we allow the request to provide a
  506. * deferred form, which must contain a
  507. * 'struct cache_deferred_req'
  508. * This cache_deferred_req contains a method to allow
  509. * it to be revisited when cache info is available
  510. */
  511. #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
  512. #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
  513. #define DFR_MAX 300 /* ??? */
  514. static DEFINE_SPINLOCK(cache_defer_lock);
  515. static LIST_HEAD(cache_defer_list);
  516. static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
  517. static int cache_defer_cnt;
  518. static void __unhash_deferred_req(struct cache_deferred_req *dreq)
  519. {
  520. hlist_del_init(&dreq->hash);
  521. if (!list_empty(&dreq->recent)) {
  522. list_del_init(&dreq->recent);
  523. cache_defer_cnt--;
  524. }
  525. }
  526. static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
  527. {
  528. int hash = DFR_HASH(item);
  529. INIT_LIST_HEAD(&dreq->recent);
  530. hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
  531. }
  532. static void setup_deferral(struct cache_deferred_req *dreq,
  533. struct cache_head *item,
  534. int count_me)
  535. {
  536. dreq->item = item;
  537. spin_lock(&cache_defer_lock);
  538. __hash_deferred_req(dreq, item);
  539. if (count_me) {
  540. cache_defer_cnt++;
  541. list_add(&dreq->recent, &cache_defer_list);
  542. }
  543. spin_unlock(&cache_defer_lock);
  544. }
  545. struct thread_deferred_req {
  546. struct cache_deferred_req handle;
  547. struct completion completion;
  548. };
  549. static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
  550. {
  551. struct thread_deferred_req *dr =
  552. container_of(dreq, struct thread_deferred_req, handle);
  553. complete(&dr->completion);
  554. }
  555. static void cache_wait_req(struct cache_req *req, struct cache_head *item)
  556. {
  557. struct thread_deferred_req sleeper;
  558. struct cache_deferred_req *dreq = &sleeper.handle;
  559. sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
  560. dreq->revisit = cache_restart_thread;
  561. setup_deferral(dreq, item, 0);
  562. if (!test_bit(CACHE_PENDING, &item->flags) ||
  563. wait_for_completion_interruptible_timeout(
  564. &sleeper.completion, req->thread_wait) <= 0) {
  565. /* The completion wasn't completed, so we need
  566. * to clean up
  567. */
  568. spin_lock(&cache_defer_lock);
  569. if (!hlist_unhashed(&sleeper.handle.hash)) {
  570. __unhash_deferred_req(&sleeper.handle);
  571. spin_unlock(&cache_defer_lock);
  572. } else {
  573. /* cache_revisit_request already removed
  574. * this from the hash table, but hasn't
  575. * called ->revisit yet. It will very soon
  576. * and we need to wait for it.
  577. */
  578. spin_unlock(&cache_defer_lock);
  579. wait_for_completion(&sleeper.completion);
  580. }
  581. }
  582. }
  583. static void cache_limit_defers(void)
  584. {
  585. /* Make sure we haven't exceed the limit of allowed deferred
  586. * requests.
  587. */
  588. struct cache_deferred_req *discard = NULL;
  589. if (cache_defer_cnt <= DFR_MAX)
  590. return;
  591. spin_lock(&cache_defer_lock);
  592. /* Consider removing either the first or the last */
  593. if (cache_defer_cnt > DFR_MAX) {
  594. if (get_random_u32_below(2))
  595. discard = list_entry(cache_defer_list.next,
  596. struct cache_deferred_req, recent);
  597. else
  598. discard = list_entry(cache_defer_list.prev,
  599. struct cache_deferred_req, recent);
  600. __unhash_deferred_req(discard);
  601. }
  602. spin_unlock(&cache_defer_lock);
  603. if (discard)
  604. discard->revisit(discard, 1);
  605. }
  606. #if IS_ENABLED(CONFIG_FAIL_SUNRPC)
  607. static inline bool cache_defer_immediately(void)
  608. {
  609. return !fail_sunrpc.ignore_cache_wait &&
  610. should_fail(&fail_sunrpc.attr, 1);
  611. }
  612. #else
  613. static inline bool cache_defer_immediately(void)
  614. {
  615. return false;
  616. }
  617. #endif
  618. /* Return true if and only if a deferred request is queued. */
  619. static bool cache_defer_req(struct cache_req *req, struct cache_head *item)
  620. {
  621. struct cache_deferred_req *dreq;
  622. if (!cache_defer_immediately()) {
  623. cache_wait_req(req, item);
  624. if (!test_bit(CACHE_PENDING, &item->flags))
  625. return false;
  626. }
  627. dreq = req->defer(req);
  628. if (dreq == NULL)
  629. return false;
  630. setup_deferral(dreq, item, 1);
  631. if (!test_bit(CACHE_PENDING, &item->flags))
  632. /* Bit could have been cleared before we managed to
  633. * set up the deferral, so need to revisit just in case
  634. */
  635. cache_revisit_request(item);
  636. cache_limit_defers();
  637. return true;
  638. }
  639. static void cache_revisit_request(struct cache_head *item)
  640. {
  641. struct cache_deferred_req *dreq;
  642. struct hlist_node *tmp;
  643. int hash = DFR_HASH(item);
  644. LIST_HEAD(pending);
  645. spin_lock(&cache_defer_lock);
  646. hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash)
  647. if (dreq->item == item) {
  648. __unhash_deferred_req(dreq);
  649. list_add(&dreq->recent, &pending);
  650. }
  651. spin_unlock(&cache_defer_lock);
  652. while (!list_empty(&pending)) {
  653. dreq = list_entry(pending.next, struct cache_deferred_req, recent);
  654. list_del_init(&dreq->recent);
  655. dreq->revisit(dreq, 0);
  656. }
  657. }
  658. void cache_clean_deferred(void *owner)
  659. {
  660. struct cache_deferred_req *dreq, *tmp;
  661. LIST_HEAD(pending);
  662. spin_lock(&cache_defer_lock);
  663. list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
  664. if (dreq->owner == owner) {
  665. __unhash_deferred_req(dreq);
  666. list_add(&dreq->recent, &pending);
  667. }
  668. }
  669. spin_unlock(&cache_defer_lock);
  670. while (!list_empty(&pending)) {
  671. dreq = list_entry(pending.next, struct cache_deferred_req, recent);
  672. list_del_init(&dreq->recent);
  673. dreq->revisit(dreq, 1);
  674. }
  675. }
  676. /*
  677. * communicate with user-space
  678. *
  679. * We have a magic /proc file - /proc/net/rpc/<cachename>/channel.
  680. * On read, you get a full request, or block.
  681. * On write, an update request is processed.
  682. * Poll works if anything to read, and always allows write.
  683. *
  684. * Implemented by linked list of requests. Each open file has
  685. * a ->private that also exists in this list. New requests are added
  686. * to the end and may wakeup and preceding readers.
  687. * New readers are added to the head. If, on read, an item is found with
  688. * CACHE_UPCALLING clear, we free it from the list.
  689. *
  690. */
  691. static DEFINE_SPINLOCK(queue_lock);
  692. struct cache_queue {
  693. struct list_head list;
  694. int reader; /* if 0, then request */
  695. };
  696. struct cache_request {
  697. struct cache_queue q;
  698. struct cache_head *item;
  699. char * buf;
  700. int len;
  701. int readers;
  702. };
  703. struct cache_reader {
  704. struct cache_queue q;
  705. int offset; /* if non-0, we have a refcnt on next request */
  706. };
  707. static int cache_request(struct cache_detail *detail,
  708. struct cache_request *crq)
  709. {
  710. char *bp = crq->buf;
  711. int len = PAGE_SIZE;
  712. detail->cache_request(detail, crq->item, &bp, &len);
  713. if (len < 0)
  714. return -E2BIG;
  715. return PAGE_SIZE - len;
  716. }
  717. static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
  718. loff_t *ppos, struct cache_detail *cd)
  719. {
  720. struct cache_reader *rp = filp->private_data;
  721. struct cache_request *rq;
  722. struct inode *inode = file_inode(filp);
  723. int err;
  724. if (count == 0)
  725. return 0;
  726. inode_lock(inode); /* protect against multiple concurrent
  727. * readers on this file */
  728. again:
  729. spin_lock(&queue_lock);
  730. /* need to find next request */
  731. while (rp->q.list.next != &cd->queue &&
  732. list_entry(rp->q.list.next, struct cache_queue, list)
  733. ->reader) {
  734. struct list_head *next = rp->q.list.next;
  735. list_move(&rp->q.list, next);
  736. }
  737. if (rp->q.list.next == &cd->queue) {
  738. spin_unlock(&queue_lock);
  739. inode_unlock(inode);
  740. WARN_ON_ONCE(rp->offset);
  741. return 0;
  742. }
  743. rq = container_of(rp->q.list.next, struct cache_request, q.list);
  744. WARN_ON_ONCE(rq->q.reader);
  745. if (rp->offset == 0)
  746. rq->readers++;
  747. spin_unlock(&queue_lock);
  748. if (rq->len == 0) {
  749. err = cache_request(cd, rq);
  750. if (err < 0)
  751. goto out;
  752. rq->len = err;
  753. }
  754. if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
  755. err = -EAGAIN;
  756. spin_lock(&queue_lock);
  757. list_move(&rp->q.list, &rq->q.list);
  758. spin_unlock(&queue_lock);
  759. } else {
  760. if (rp->offset + count > rq->len)
  761. count = rq->len - rp->offset;
  762. err = -EFAULT;
  763. if (copy_to_user(buf, rq->buf + rp->offset, count))
  764. goto out;
  765. rp->offset += count;
  766. if (rp->offset >= rq->len) {
  767. rp->offset = 0;
  768. spin_lock(&queue_lock);
  769. list_move(&rp->q.list, &rq->q.list);
  770. spin_unlock(&queue_lock);
  771. }
  772. err = 0;
  773. }
  774. out:
  775. if (rp->offset == 0) {
  776. /* need to release rq */
  777. spin_lock(&queue_lock);
  778. rq->readers--;
  779. if (rq->readers == 0 &&
  780. !test_bit(CACHE_PENDING, &rq->item->flags)) {
  781. list_del(&rq->q.list);
  782. spin_unlock(&queue_lock);
  783. cache_put(rq->item, cd);
  784. kfree(rq->buf);
  785. kfree(rq);
  786. } else
  787. spin_unlock(&queue_lock);
  788. }
  789. if (err == -EAGAIN)
  790. goto again;
  791. inode_unlock(inode);
  792. return err ? err : count;
  793. }
  794. static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
  795. size_t count, struct cache_detail *cd)
  796. {
  797. ssize_t ret;
  798. if (count == 0)
  799. return -EINVAL;
  800. if (copy_from_user(kaddr, buf, count))
  801. return -EFAULT;
  802. kaddr[count] = '\0';
  803. ret = cd->cache_parse(cd, kaddr, count);
  804. if (!ret)
  805. ret = count;
  806. return ret;
  807. }
  808. static ssize_t cache_downcall(struct address_space *mapping,
  809. const char __user *buf,
  810. size_t count, struct cache_detail *cd)
  811. {
  812. char *write_buf;
  813. ssize_t ret = -ENOMEM;
  814. if (count >= 32768) { /* 32k is max userland buffer, lets check anyway */
  815. ret = -EINVAL;
  816. goto out;
  817. }
  818. write_buf = kvmalloc(count + 1, GFP_KERNEL);
  819. if (!write_buf)
  820. goto out;
  821. ret = cache_do_downcall(write_buf, buf, count, cd);
  822. kvfree(write_buf);
  823. out:
  824. return ret;
  825. }
  826. static ssize_t cache_write(struct file *filp, const char __user *buf,
  827. size_t count, loff_t *ppos,
  828. struct cache_detail *cd)
  829. {
  830. struct address_space *mapping = filp->f_mapping;
  831. struct inode *inode = file_inode(filp);
  832. ssize_t ret = -EINVAL;
  833. if (!cd->cache_parse)
  834. goto out;
  835. inode_lock(inode);
  836. ret = cache_downcall(mapping, buf, count, cd);
  837. inode_unlock(inode);
  838. out:
  839. return ret;
  840. }
  841. static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
  842. static __poll_t cache_poll(struct file *filp, poll_table *wait,
  843. struct cache_detail *cd)
  844. {
  845. __poll_t mask;
  846. struct cache_reader *rp = filp->private_data;
  847. struct cache_queue *cq;
  848. poll_wait(filp, &queue_wait, wait);
  849. /* alway allow write */
  850. mask = EPOLLOUT | EPOLLWRNORM;
  851. if (!rp)
  852. return mask;
  853. spin_lock(&queue_lock);
  854. for (cq= &rp->q; &cq->list != &cd->queue;
  855. cq = list_entry(cq->list.next, struct cache_queue, list))
  856. if (!cq->reader) {
  857. mask |= EPOLLIN | EPOLLRDNORM;
  858. break;
  859. }
  860. spin_unlock(&queue_lock);
  861. return mask;
  862. }
  863. static int cache_ioctl(struct inode *ino, struct file *filp,
  864. unsigned int cmd, unsigned long arg,
  865. struct cache_detail *cd)
  866. {
  867. int len = 0;
  868. struct cache_reader *rp = filp->private_data;
  869. struct cache_queue *cq;
  870. if (cmd != FIONREAD || !rp)
  871. return -EINVAL;
  872. spin_lock(&queue_lock);
  873. /* only find the length remaining in current request,
  874. * or the length of the next request
  875. */
  876. for (cq= &rp->q; &cq->list != &cd->queue;
  877. cq = list_entry(cq->list.next, struct cache_queue, list))
  878. if (!cq->reader) {
  879. struct cache_request *cr =
  880. container_of(cq, struct cache_request, q);
  881. len = cr->len - rp->offset;
  882. break;
  883. }
  884. spin_unlock(&queue_lock);
  885. return put_user(len, (int __user *)arg);
  886. }
  887. static int cache_open(struct inode *inode, struct file *filp,
  888. struct cache_detail *cd)
  889. {
  890. struct cache_reader *rp = NULL;
  891. if (!cd || !try_module_get(cd->owner))
  892. return -EACCES;
  893. nonseekable_open(inode, filp);
  894. if (filp->f_mode & FMODE_READ) {
  895. rp = kmalloc(sizeof(*rp), GFP_KERNEL);
  896. if (!rp) {
  897. module_put(cd->owner);
  898. return -ENOMEM;
  899. }
  900. rp->offset = 0;
  901. rp->q.reader = 1;
  902. spin_lock(&queue_lock);
  903. list_add(&rp->q.list, &cd->queue);
  904. spin_unlock(&queue_lock);
  905. }
  906. if (filp->f_mode & FMODE_WRITE)
  907. atomic_inc(&cd->writers);
  908. filp->private_data = rp;
  909. return 0;
  910. }
  911. static int cache_release(struct inode *inode, struct file *filp,
  912. struct cache_detail *cd)
  913. {
  914. struct cache_reader *rp = filp->private_data;
  915. if (rp) {
  916. spin_lock(&queue_lock);
  917. if (rp->offset) {
  918. struct cache_queue *cq;
  919. for (cq= &rp->q; &cq->list != &cd->queue;
  920. cq = list_entry(cq->list.next, struct cache_queue, list))
  921. if (!cq->reader) {
  922. container_of(cq, struct cache_request, q)
  923. ->readers--;
  924. break;
  925. }
  926. rp->offset = 0;
  927. }
  928. list_del(&rp->q.list);
  929. spin_unlock(&queue_lock);
  930. filp->private_data = NULL;
  931. kfree(rp);
  932. }
  933. if (filp->f_mode & FMODE_WRITE) {
  934. atomic_dec(&cd->writers);
  935. cd->last_close = seconds_since_boot();
  936. }
  937. module_put(cd->owner);
  938. return 0;
  939. }
  940. static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
  941. {
  942. struct cache_queue *cq, *tmp;
  943. struct cache_request *cr;
  944. LIST_HEAD(dequeued);
  945. spin_lock(&queue_lock);
  946. list_for_each_entry_safe(cq, tmp, &detail->queue, list)
  947. if (!cq->reader) {
  948. cr = container_of(cq, struct cache_request, q);
  949. if (cr->item != ch)
  950. continue;
  951. if (test_bit(CACHE_PENDING, &ch->flags))
  952. /* Lost a race and it is pending again */
  953. break;
  954. if (cr->readers != 0)
  955. continue;
  956. list_move(&cr->q.list, &dequeued);
  957. }
  958. spin_unlock(&queue_lock);
  959. while (!list_empty(&dequeued)) {
  960. cr = list_entry(dequeued.next, struct cache_request, q.list);
  961. list_del(&cr->q.list);
  962. cache_put(cr->item, detail);
  963. kfree(cr->buf);
  964. kfree(cr);
  965. }
  966. }
  967. /*
  968. * Support routines for text-based upcalls.
  969. * Fields are separated by spaces.
  970. * Fields are either mangled to quote space tab newline slosh with slosh
  971. * or a hexified with a leading \x
  972. * Record is terminated with newline.
  973. *
  974. */
  975. void qword_add(char **bpp, int *lp, char *str)
  976. {
  977. char *bp = *bpp;
  978. int len = *lp;
  979. int ret;
  980. if (len < 0) return;
  981. ret = string_escape_str(str, bp, len, ESCAPE_OCTAL, "\\ \n\t");
  982. if (ret >= len) {
  983. bp += len;
  984. len = -1;
  985. } else {
  986. bp += ret;
  987. len -= ret;
  988. *bp++ = ' ';
  989. len--;
  990. }
  991. *bpp = bp;
  992. *lp = len;
  993. }
  994. EXPORT_SYMBOL_GPL(qword_add);
  995. void qword_addhex(char **bpp, int *lp, char *buf, int blen)
  996. {
  997. char *bp = *bpp;
  998. int len = *lp;
  999. if (len < 0) return;
  1000. if (len > 2) {
  1001. *bp++ = '\\';
  1002. *bp++ = 'x';
  1003. len -= 2;
  1004. while (blen && len >= 2) {
  1005. bp = hex_byte_pack(bp, *buf++);
  1006. len -= 2;
  1007. blen--;
  1008. }
  1009. }
  1010. if (blen || len<1) len = -1;
  1011. else {
  1012. *bp++ = ' ';
  1013. len--;
  1014. }
  1015. *bpp = bp;
  1016. *lp = len;
  1017. }
  1018. EXPORT_SYMBOL_GPL(qword_addhex);
  1019. static void warn_no_listener(struct cache_detail *detail)
  1020. {
  1021. if (detail->last_warn != detail->last_close) {
  1022. detail->last_warn = detail->last_close;
  1023. if (detail->warn_no_listener)
  1024. detail->warn_no_listener(detail, detail->last_close != 0);
  1025. }
  1026. }
  1027. static bool cache_listeners_exist(struct cache_detail *detail)
  1028. {
  1029. if (atomic_read(&detail->writers))
  1030. return true;
  1031. if (detail->last_close == 0)
  1032. /* This cache was never opened */
  1033. return false;
  1034. if (detail->last_close < seconds_since_boot() - 30)
  1035. /*
  1036. * We allow for the possibility that someone might
  1037. * restart a userspace daemon without restarting the
  1038. * server; but after 30 seconds, we give up.
  1039. */
  1040. return false;
  1041. return true;
  1042. }
  1043. /*
  1044. * register an upcall request to user-space and queue it up for read() by the
  1045. * upcall daemon.
  1046. *
  1047. * Each request is at most one page long.
  1048. */
  1049. static int cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
  1050. {
  1051. char *buf;
  1052. struct cache_request *crq;
  1053. int ret = 0;
  1054. if (test_bit(CACHE_CLEANED, &h->flags))
  1055. /* Too late to make an upcall */
  1056. return -EAGAIN;
  1057. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1058. if (!buf)
  1059. return -EAGAIN;
  1060. crq = kmalloc(sizeof (*crq), GFP_KERNEL);
  1061. if (!crq) {
  1062. kfree(buf);
  1063. return -EAGAIN;
  1064. }
  1065. crq->q.reader = 0;
  1066. crq->buf = buf;
  1067. crq->len = 0;
  1068. crq->readers = 0;
  1069. spin_lock(&queue_lock);
  1070. if (test_bit(CACHE_PENDING, &h->flags)) {
  1071. crq->item = cache_get(h);
  1072. list_add_tail(&crq->q.list, &detail->queue);
  1073. trace_cache_entry_upcall(detail, h);
  1074. } else
  1075. /* Lost a race, no longer PENDING, so don't enqueue */
  1076. ret = -EAGAIN;
  1077. spin_unlock(&queue_lock);
  1078. wake_up(&queue_wait);
  1079. if (ret == -EAGAIN) {
  1080. kfree(buf);
  1081. kfree(crq);
  1082. }
  1083. return ret;
  1084. }
  1085. int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
  1086. {
  1087. if (test_and_set_bit(CACHE_PENDING, &h->flags))
  1088. return 0;
  1089. return cache_pipe_upcall(detail, h);
  1090. }
  1091. EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
  1092. int sunrpc_cache_pipe_upcall_timeout(struct cache_detail *detail,
  1093. struct cache_head *h)
  1094. {
  1095. if (!cache_listeners_exist(detail)) {
  1096. warn_no_listener(detail);
  1097. trace_cache_entry_no_listener(detail, h);
  1098. return -EINVAL;
  1099. }
  1100. return sunrpc_cache_pipe_upcall(detail, h);
  1101. }
  1102. EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall_timeout);
  1103. /*
  1104. * parse a message from user-space and pass it
  1105. * to an appropriate cache
  1106. * Messages are, like requests, separated into fields by
  1107. * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
  1108. *
  1109. * Message is
  1110. * reply cachename expiry key ... content....
  1111. *
  1112. * key and content are both parsed by cache
  1113. */
  1114. int qword_get(char **bpp, char *dest, int bufsize)
  1115. {
  1116. /* return bytes copied, or -1 on error */
  1117. char *bp = *bpp;
  1118. int len = 0;
  1119. while (*bp == ' ') bp++;
  1120. if (bp[0] == '\\' && bp[1] == 'x') {
  1121. /* HEX STRING */
  1122. bp += 2;
  1123. while (len < bufsize - 1) {
  1124. int h, l;
  1125. h = hex_to_bin(bp[0]);
  1126. if (h < 0)
  1127. break;
  1128. l = hex_to_bin(bp[1]);
  1129. if (l < 0)
  1130. break;
  1131. *dest++ = (h << 4) | l;
  1132. bp += 2;
  1133. len++;
  1134. }
  1135. } else {
  1136. /* text with \nnn octal quoting */
  1137. while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
  1138. if (*bp == '\\' &&
  1139. isodigit(bp[1]) && (bp[1] <= '3') &&
  1140. isodigit(bp[2]) &&
  1141. isodigit(bp[3])) {
  1142. int byte = (*++bp -'0');
  1143. bp++;
  1144. byte = (byte << 3) | (*bp++ - '0');
  1145. byte = (byte << 3) | (*bp++ - '0');
  1146. *dest++ = byte;
  1147. len++;
  1148. } else {
  1149. *dest++ = *bp++;
  1150. len++;
  1151. }
  1152. }
  1153. }
  1154. if (*bp != ' ' && *bp != '\n' && *bp != '\0')
  1155. return -1;
  1156. while (*bp == ' ') bp++;
  1157. *bpp = bp;
  1158. *dest = '\0';
  1159. return len;
  1160. }
  1161. EXPORT_SYMBOL_GPL(qword_get);
  1162. /*
  1163. * support /proc/net/rpc/$CACHENAME/content
  1164. * as a seqfile.
  1165. * We call ->cache_show passing NULL for the item to
  1166. * get a header, then pass each real item in the cache
  1167. */
  1168. static void *__cache_seq_start(struct seq_file *m, loff_t *pos)
  1169. {
  1170. loff_t n = *pos;
  1171. unsigned int hash, entry;
  1172. struct cache_head *ch;
  1173. struct cache_detail *cd = m->private;
  1174. if (!n--)
  1175. return SEQ_START_TOKEN;
  1176. hash = n >> 32;
  1177. entry = n & ((1LL<<32) - 1);
  1178. hlist_for_each_entry_rcu(ch, &cd->hash_table[hash], cache_list)
  1179. if (!entry--)
  1180. return ch;
  1181. n &= ~((1LL<<32) - 1);
  1182. do {
  1183. hash++;
  1184. n += 1LL<<32;
  1185. } while(hash < cd->hash_size &&
  1186. hlist_empty(&cd->hash_table[hash]));
  1187. if (hash >= cd->hash_size)
  1188. return NULL;
  1189. *pos = n+1;
  1190. return hlist_entry_safe(rcu_dereference_raw(
  1191. hlist_first_rcu(&cd->hash_table[hash])),
  1192. struct cache_head, cache_list);
  1193. }
  1194. static void *cache_seq_next(struct seq_file *m, void *p, loff_t *pos)
  1195. {
  1196. struct cache_head *ch = p;
  1197. int hash = (*pos >> 32);
  1198. struct cache_detail *cd = m->private;
  1199. if (p == SEQ_START_TOKEN)
  1200. hash = 0;
  1201. else if (ch->cache_list.next == NULL) {
  1202. hash++;
  1203. *pos += 1LL<<32;
  1204. } else {
  1205. ++*pos;
  1206. return hlist_entry_safe(rcu_dereference_raw(
  1207. hlist_next_rcu(&ch->cache_list)),
  1208. struct cache_head, cache_list);
  1209. }
  1210. *pos &= ~((1LL<<32) - 1);
  1211. while (hash < cd->hash_size &&
  1212. hlist_empty(&cd->hash_table[hash])) {
  1213. hash++;
  1214. *pos += 1LL<<32;
  1215. }
  1216. if (hash >= cd->hash_size)
  1217. return NULL;
  1218. ++*pos;
  1219. return hlist_entry_safe(rcu_dereference_raw(
  1220. hlist_first_rcu(&cd->hash_table[hash])),
  1221. struct cache_head, cache_list);
  1222. }
  1223. void *cache_seq_start_rcu(struct seq_file *m, loff_t *pos)
  1224. __acquires(RCU)
  1225. {
  1226. rcu_read_lock();
  1227. return __cache_seq_start(m, pos);
  1228. }
  1229. EXPORT_SYMBOL_GPL(cache_seq_start_rcu);
  1230. void *cache_seq_next_rcu(struct seq_file *file, void *p, loff_t *pos)
  1231. {
  1232. return cache_seq_next(file, p, pos);
  1233. }
  1234. EXPORT_SYMBOL_GPL(cache_seq_next_rcu);
  1235. void cache_seq_stop_rcu(struct seq_file *m, void *p)
  1236. __releases(RCU)
  1237. {
  1238. rcu_read_unlock();
  1239. }
  1240. EXPORT_SYMBOL_GPL(cache_seq_stop_rcu);
  1241. static int c_show(struct seq_file *m, void *p)
  1242. {
  1243. struct cache_head *cp = p;
  1244. struct cache_detail *cd = m->private;
  1245. if (p == SEQ_START_TOKEN)
  1246. return cd->cache_show(m, cd, NULL);
  1247. ifdebug(CACHE)
  1248. seq_printf(m, "# expiry=%lld refcnt=%d flags=%lx\n",
  1249. convert_to_wallclock(cp->expiry_time),
  1250. kref_read(&cp->ref), cp->flags);
  1251. if (!cache_get_rcu(cp))
  1252. return 0;
  1253. if (cache_check(cd, cp, NULL))
  1254. /* cache_check does a cache_put on failure */
  1255. seq_puts(m, "# ");
  1256. else {
  1257. if (cache_is_expired(cd, cp))
  1258. seq_puts(m, "# ");
  1259. cache_put(cp, cd);
  1260. }
  1261. return cd->cache_show(m, cd, cp);
  1262. }
  1263. static const struct seq_operations cache_content_op = {
  1264. .start = cache_seq_start_rcu,
  1265. .next = cache_seq_next_rcu,
  1266. .stop = cache_seq_stop_rcu,
  1267. .show = c_show,
  1268. };
  1269. static int content_open(struct inode *inode, struct file *file,
  1270. struct cache_detail *cd)
  1271. {
  1272. struct seq_file *seq;
  1273. int err;
  1274. if (!cd || !try_module_get(cd->owner))
  1275. return -EACCES;
  1276. err = seq_open(file, &cache_content_op);
  1277. if (err) {
  1278. module_put(cd->owner);
  1279. return err;
  1280. }
  1281. seq = file->private_data;
  1282. seq->private = cd;
  1283. return 0;
  1284. }
  1285. static int content_release(struct inode *inode, struct file *file,
  1286. struct cache_detail *cd)
  1287. {
  1288. int ret = seq_release(inode, file);
  1289. module_put(cd->owner);
  1290. return ret;
  1291. }
  1292. static int open_flush(struct inode *inode, struct file *file,
  1293. struct cache_detail *cd)
  1294. {
  1295. if (!cd || !try_module_get(cd->owner))
  1296. return -EACCES;
  1297. return nonseekable_open(inode, file);
  1298. }
  1299. static int release_flush(struct inode *inode, struct file *file,
  1300. struct cache_detail *cd)
  1301. {
  1302. module_put(cd->owner);
  1303. return 0;
  1304. }
  1305. static ssize_t read_flush(struct file *file, char __user *buf,
  1306. size_t count, loff_t *ppos,
  1307. struct cache_detail *cd)
  1308. {
  1309. char tbuf[22];
  1310. size_t len;
  1311. len = snprintf(tbuf, sizeof(tbuf), "%llu\n",
  1312. convert_to_wallclock(cd->flush_time));
  1313. return simple_read_from_buffer(buf, count, ppos, tbuf, len);
  1314. }
  1315. static ssize_t write_flush(struct file *file, const char __user *buf,
  1316. size_t count, loff_t *ppos,
  1317. struct cache_detail *cd)
  1318. {
  1319. char tbuf[20];
  1320. char *ep;
  1321. time64_t now;
  1322. if (*ppos || count > sizeof(tbuf)-1)
  1323. return -EINVAL;
  1324. if (copy_from_user(tbuf, buf, count))
  1325. return -EFAULT;
  1326. tbuf[count] = 0;
  1327. simple_strtoul(tbuf, &ep, 0);
  1328. if (*ep && *ep != '\n')
  1329. return -EINVAL;
  1330. /* Note that while we check that 'buf' holds a valid number,
  1331. * we always ignore the value and just flush everything.
  1332. * Making use of the number leads to races.
  1333. */
  1334. now = seconds_since_boot();
  1335. /* Always flush everything, so behave like cache_purge()
  1336. * Do this by advancing flush_time to the current time,
  1337. * or by one second if it has already reached the current time.
  1338. * Newly added cache entries will always have ->last_refresh greater
  1339. * that ->flush_time, so they don't get flushed prematurely.
  1340. */
  1341. if (cd->flush_time >= now)
  1342. now = cd->flush_time + 1;
  1343. cd->flush_time = now;
  1344. cd->nextcheck = now;
  1345. cache_flush();
  1346. if (cd->flush)
  1347. cd->flush();
  1348. *ppos += count;
  1349. return count;
  1350. }
  1351. static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
  1352. size_t count, loff_t *ppos)
  1353. {
  1354. struct cache_detail *cd = pde_data(file_inode(filp));
  1355. return cache_read(filp, buf, count, ppos, cd);
  1356. }
  1357. static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
  1358. size_t count, loff_t *ppos)
  1359. {
  1360. struct cache_detail *cd = pde_data(file_inode(filp));
  1361. return cache_write(filp, buf, count, ppos, cd);
  1362. }
  1363. static __poll_t cache_poll_procfs(struct file *filp, poll_table *wait)
  1364. {
  1365. struct cache_detail *cd = pde_data(file_inode(filp));
  1366. return cache_poll(filp, wait, cd);
  1367. }
  1368. static long cache_ioctl_procfs(struct file *filp,
  1369. unsigned int cmd, unsigned long arg)
  1370. {
  1371. struct inode *inode = file_inode(filp);
  1372. struct cache_detail *cd = pde_data(inode);
  1373. return cache_ioctl(inode, filp, cmd, arg, cd);
  1374. }
  1375. static int cache_open_procfs(struct inode *inode, struct file *filp)
  1376. {
  1377. struct cache_detail *cd = pde_data(inode);
  1378. return cache_open(inode, filp, cd);
  1379. }
  1380. static int cache_release_procfs(struct inode *inode, struct file *filp)
  1381. {
  1382. struct cache_detail *cd = pde_data(inode);
  1383. return cache_release(inode, filp, cd);
  1384. }
  1385. static const struct proc_ops cache_channel_proc_ops = {
  1386. .proc_read = cache_read_procfs,
  1387. .proc_write = cache_write_procfs,
  1388. .proc_poll = cache_poll_procfs,
  1389. .proc_ioctl = cache_ioctl_procfs, /* for FIONREAD */
  1390. .proc_open = cache_open_procfs,
  1391. .proc_release = cache_release_procfs,
  1392. };
  1393. static int content_open_procfs(struct inode *inode, struct file *filp)
  1394. {
  1395. struct cache_detail *cd = pde_data(inode);
  1396. return content_open(inode, filp, cd);
  1397. }
  1398. static int content_release_procfs(struct inode *inode, struct file *filp)
  1399. {
  1400. struct cache_detail *cd = pde_data(inode);
  1401. return content_release(inode, filp, cd);
  1402. }
  1403. static const struct proc_ops content_proc_ops = {
  1404. .proc_open = content_open_procfs,
  1405. .proc_read = seq_read,
  1406. .proc_lseek = seq_lseek,
  1407. .proc_release = content_release_procfs,
  1408. };
  1409. static int open_flush_procfs(struct inode *inode, struct file *filp)
  1410. {
  1411. struct cache_detail *cd = pde_data(inode);
  1412. return open_flush(inode, filp, cd);
  1413. }
  1414. static int release_flush_procfs(struct inode *inode, struct file *filp)
  1415. {
  1416. struct cache_detail *cd = pde_data(inode);
  1417. return release_flush(inode, filp, cd);
  1418. }
  1419. static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
  1420. size_t count, loff_t *ppos)
  1421. {
  1422. struct cache_detail *cd = pde_data(file_inode(filp));
  1423. return read_flush(filp, buf, count, ppos, cd);
  1424. }
  1425. static ssize_t write_flush_procfs(struct file *filp,
  1426. const char __user *buf,
  1427. size_t count, loff_t *ppos)
  1428. {
  1429. struct cache_detail *cd = pde_data(file_inode(filp));
  1430. return write_flush(filp, buf, count, ppos, cd);
  1431. }
  1432. static const struct proc_ops cache_flush_proc_ops = {
  1433. .proc_open = open_flush_procfs,
  1434. .proc_read = read_flush_procfs,
  1435. .proc_write = write_flush_procfs,
  1436. .proc_release = release_flush_procfs,
  1437. };
  1438. static void remove_cache_proc_entries(struct cache_detail *cd)
  1439. {
  1440. if (cd->procfs) {
  1441. proc_remove(cd->procfs);
  1442. cd->procfs = NULL;
  1443. }
  1444. }
  1445. static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
  1446. {
  1447. struct proc_dir_entry *p;
  1448. struct sunrpc_net *sn;
  1449. if (!IS_ENABLED(CONFIG_PROC_FS))
  1450. return 0;
  1451. sn = net_generic(net, sunrpc_net_id);
  1452. cd->procfs = proc_mkdir(cd->name, sn->proc_net_rpc);
  1453. if (cd->procfs == NULL)
  1454. goto out_nomem;
  1455. p = proc_create_data("flush", S_IFREG | 0600,
  1456. cd->procfs, &cache_flush_proc_ops, cd);
  1457. if (p == NULL)
  1458. goto out_nomem;
  1459. if (cd->cache_request || cd->cache_parse) {
  1460. p = proc_create_data("channel", S_IFREG | 0600, cd->procfs,
  1461. &cache_channel_proc_ops, cd);
  1462. if (p == NULL)
  1463. goto out_nomem;
  1464. }
  1465. if (cd->cache_show) {
  1466. p = proc_create_data("content", S_IFREG | 0400, cd->procfs,
  1467. &content_proc_ops, cd);
  1468. if (p == NULL)
  1469. goto out_nomem;
  1470. }
  1471. return 0;
  1472. out_nomem:
  1473. remove_cache_proc_entries(cd);
  1474. return -ENOMEM;
  1475. }
  1476. void __init cache_initialize(void)
  1477. {
  1478. INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean);
  1479. }
  1480. int cache_register_net(struct cache_detail *cd, struct net *net)
  1481. {
  1482. int ret;
  1483. sunrpc_init_cache_detail(cd);
  1484. ret = create_cache_proc_entries(cd, net);
  1485. if (ret)
  1486. sunrpc_destroy_cache_detail(cd);
  1487. return ret;
  1488. }
  1489. EXPORT_SYMBOL_GPL(cache_register_net);
  1490. void cache_unregister_net(struct cache_detail *cd, struct net *net)
  1491. {
  1492. remove_cache_proc_entries(cd);
  1493. sunrpc_destroy_cache_detail(cd);
  1494. }
  1495. EXPORT_SYMBOL_GPL(cache_unregister_net);
  1496. struct cache_detail *cache_create_net(const struct cache_detail *tmpl, struct net *net)
  1497. {
  1498. struct cache_detail *cd;
  1499. int i;
  1500. cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL);
  1501. if (cd == NULL)
  1502. return ERR_PTR(-ENOMEM);
  1503. cd->hash_table = kcalloc(cd->hash_size, sizeof(struct hlist_head),
  1504. GFP_KERNEL);
  1505. if (cd->hash_table == NULL) {
  1506. kfree(cd);
  1507. return ERR_PTR(-ENOMEM);
  1508. }
  1509. for (i = 0; i < cd->hash_size; i++)
  1510. INIT_HLIST_HEAD(&cd->hash_table[i]);
  1511. cd->net = net;
  1512. return cd;
  1513. }
  1514. EXPORT_SYMBOL_GPL(cache_create_net);
  1515. void cache_destroy_net(struct cache_detail *cd, struct net *net)
  1516. {
  1517. kfree(cd->hash_table);
  1518. kfree(cd);
  1519. }
  1520. EXPORT_SYMBOL_GPL(cache_destroy_net);
  1521. static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
  1522. size_t count, loff_t *ppos)
  1523. {
  1524. struct cache_detail *cd = RPC_I(file_inode(filp))->private;
  1525. return cache_read(filp, buf, count, ppos, cd);
  1526. }
  1527. static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
  1528. size_t count, loff_t *ppos)
  1529. {
  1530. struct cache_detail *cd = RPC_I(file_inode(filp))->private;
  1531. return cache_write(filp, buf, count, ppos, cd);
  1532. }
  1533. static __poll_t cache_poll_pipefs(struct file *filp, poll_table *wait)
  1534. {
  1535. struct cache_detail *cd = RPC_I(file_inode(filp))->private;
  1536. return cache_poll(filp, wait, cd);
  1537. }
  1538. static long cache_ioctl_pipefs(struct file *filp,
  1539. unsigned int cmd, unsigned long arg)
  1540. {
  1541. struct inode *inode = file_inode(filp);
  1542. struct cache_detail *cd = RPC_I(inode)->private;
  1543. return cache_ioctl(inode, filp, cmd, arg, cd);
  1544. }
  1545. static int cache_open_pipefs(struct inode *inode, struct file *filp)
  1546. {
  1547. struct cache_detail *cd = RPC_I(inode)->private;
  1548. return cache_open(inode, filp, cd);
  1549. }
  1550. static int cache_release_pipefs(struct inode *inode, struct file *filp)
  1551. {
  1552. struct cache_detail *cd = RPC_I(inode)->private;
  1553. return cache_release(inode, filp, cd);
  1554. }
  1555. const struct file_operations cache_file_operations_pipefs = {
  1556. .owner = THIS_MODULE,
  1557. .read = cache_read_pipefs,
  1558. .write = cache_write_pipefs,
  1559. .poll = cache_poll_pipefs,
  1560. .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
  1561. .open = cache_open_pipefs,
  1562. .release = cache_release_pipefs,
  1563. };
  1564. static int content_open_pipefs(struct inode *inode, struct file *filp)
  1565. {
  1566. struct cache_detail *cd = RPC_I(inode)->private;
  1567. return content_open(inode, filp, cd);
  1568. }
  1569. static int content_release_pipefs(struct inode *inode, struct file *filp)
  1570. {
  1571. struct cache_detail *cd = RPC_I(inode)->private;
  1572. return content_release(inode, filp, cd);
  1573. }
  1574. const struct file_operations content_file_operations_pipefs = {
  1575. .open = content_open_pipefs,
  1576. .read = seq_read,
  1577. .llseek = seq_lseek,
  1578. .release = content_release_pipefs,
  1579. };
  1580. static int open_flush_pipefs(struct inode *inode, struct file *filp)
  1581. {
  1582. struct cache_detail *cd = RPC_I(inode)->private;
  1583. return open_flush(inode, filp, cd);
  1584. }
  1585. static int release_flush_pipefs(struct inode *inode, struct file *filp)
  1586. {
  1587. struct cache_detail *cd = RPC_I(inode)->private;
  1588. return release_flush(inode, filp, cd);
  1589. }
  1590. static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
  1591. size_t count, loff_t *ppos)
  1592. {
  1593. struct cache_detail *cd = RPC_I(file_inode(filp))->private;
  1594. return read_flush(filp, buf, count, ppos, cd);
  1595. }
  1596. static ssize_t write_flush_pipefs(struct file *filp,
  1597. const char __user *buf,
  1598. size_t count, loff_t *ppos)
  1599. {
  1600. struct cache_detail *cd = RPC_I(file_inode(filp))->private;
  1601. return write_flush(filp, buf, count, ppos, cd);
  1602. }
  1603. const struct file_operations cache_flush_operations_pipefs = {
  1604. .open = open_flush_pipefs,
  1605. .read = read_flush_pipefs,
  1606. .write = write_flush_pipefs,
  1607. .release = release_flush_pipefs,
  1608. };
  1609. int sunrpc_cache_register_pipefs(struct dentry *parent,
  1610. const char *name, umode_t umode,
  1611. struct cache_detail *cd)
  1612. {
  1613. struct dentry *dir = rpc_create_cache_dir(parent, name, umode, cd);
  1614. if (IS_ERR(dir))
  1615. return PTR_ERR(dir);
  1616. cd->pipefs = dir;
  1617. return 0;
  1618. }
  1619. EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
  1620. void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
  1621. {
  1622. if (cd->pipefs) {
  1623. rpc_remove_cache_dir(cd->pipefs);
  1624. cd->pipefs = NULL;
  1625. }
  1626. }
  1627. EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
  1628. void sunrpc_cache_unhash(struct cache_detail *cd, struct cache_head *h)
  1629. {
  1630. spin_lock(&cd->hash_lock);
  1631. if (!hlist_unhashed(&h->cache_list)){
  1632. sunrpc_begin_cache_remove_entry(h, cd);
  1633. spin_unlock(&cd->hash_lock);
  1634. sunrpc_end_cache_remove_entry(h, cd);
  1635. } else
  1636. spin_unlock(&cd->hash_lock);
  1637. }
  1638. EXPORT_SYMBOL_GPL(sunrpc_cache_unhash);