cache.c 46 KB

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