cache.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918
  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 <[email protected]>
  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 (prandom_u32_max(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 list_head pending;
  643. struct hlist_node *tmp;
  644. int hash = DFR_HASH(item);
  645. INIT_LIST_HEAD(&pending);
  646. spin_lock(&cache_defer_lock);
  647. hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash)
  648. if (dreq->item == item) {
  649. __unhash_deferred_req(dreq);
  650. list_add(&dreq->recent, &pending);
  651. }
  652. spin_unlock(&cache_defer_lock);
  653. while (!list_empty(&pending)) {
  654. dreq = list_entry(pending.next, struct cache_deferred_req, recent);
  655. list_del_init(&dreq->recent);
  656. dreq->revisit(dreq, 0);
  657. }
  658. }
  659. void cache_clean_deferred(void *owner)
  660. {
  661. struct cache_deferred_req *dreq, *tmp;
  662. struct list_head pending;
  663. INIT_LIST_HEAD(&pending);
  664. spin_lock(&cache_defer_lock);
  665. list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
  666. if (dreq->owner == owner) {
  667. __unhash_deferred_req(dreq);
  668. list_add(&dreq->recent, &pending);
  669. }
  670. }
  671. spin_unlock(&cache_defer_lock);
  672. while (!list_empty(&pending)) {
  673. dreq = list_entry(pending.next, struct cache_deferred_req, recent);
  674. list_del_init(&dreq->recent);
  675. dreq->revisit(dreq, 1);
  676. }
  677. }
  678. /*
  679. * communicate with user-space
  680. *
  681. * We have a magic /proc file - /proc/net/rpc/<cachename>/channel.
  682. * On read, you get a full request, or block.
  683. * On write, an update request is processed.
  684. * Poll works if anything to read, and always allows write.
  685. *
  686. * Implemented by linked list of requests. Each open file has
  687. * a ->private that also exists in this list. New requests are added
  688. * to the end and may wakeup and preceding readers.
  689. * New readers are added to the head. If, on read, an item is found with
  690. * CACHE_UPCALLING clear, we free it from the list.
  691. *
  692. */
  693. static DEFINE_SPINLOCK(queue_lock);
  694. struct cache_queue {
  695. struct list_head list;
  696. int reader; /* if 0, then request */
  697. };
  698. struct cache_request {
  699. struct cache_queue q;
  700. struct cache_head *item;
  701. char * buf;
  702. int len;
  703. int readers;
  704. };
  705. struct cache_reader {
  706. struct cache_queue q;
  707. int offset; /* if non-0, we have a refcnt on next request */
  708. };
  709. static int cache_request(struct cache_detail *detail,
  710. struct cache_request *crq)
  711. {
  712. char *bp = crq->buf;
  713. int len = PAGE_SIZE;
  714. detail->cache_request(detail, crq->item, &bp, &len);
  715. if (len < 0)
  716. return -E2BIG;
  717. return PAGE_SIZE - len;
  718. }
  719. static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
  720. loff_t *ppos, struct cache_detail *cd)
  721. {
  722. struct cache_reader *rp = filp->private_data;
  723. struct cache_request *rq;
  724. struct inode *inode = file_inode(filp);
  725. int err;
  726. if (count == 0)
  727. return 0;
  728. inode_lock(inode); /* protect against multiple concurrent
  729. * readers on this file */
  730. again:
  731. spin_lock(&queue_lock);
  732. /* need to find next request */
  733. while (rp->q.list.next != &cd->queue &&
  734. list_entry(rp->q.list.next, struct cache_queue, list)
  735. ->reader) {
  736. struct list_head *next = rp->q.list.next;
  737. list_move(&rp->q.list, next);
  738. }
  739. if (rp->q.list.next == &cd->queue) {
  740. spin_unlock(&queue_lock);
  741. inode_unlock(inode);
  742. WARN_ON_ONCE(rp->offset);
  743. return 0;
  744. }
  745. rq = container_of(rp->q.list.next, struct cache_request, q.list);
  746. WARN_ON_ONCE(rq->q.reader);
  747. if (rp->offset == 0)
  748. rq->readers++;
  749. spin_unlock(&queue_lock);
  750. if (rq->len == 0) {
  751. err = cache_request(cd, rq);
  752. if (err < 0)
  753. goto out;
  754. rq->len = err;
  755. }
  756. if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
  757. err = -EAGAIN;
  758. spin_lock(&queue_lock);
  759. list_move(&rp->q.list, &rq->q.list);
  760. spin_unlock(&queue_lock);
  761. } else {
  762. if (rp->offset + count > rq->len)
  763. count = rq->len - rp->offset;
  764. err = -EFAULT;
  765. if (copy_to_user(buf, rq->buf + rp->offset, count))
  766. goto out;
  767. rp->offset += count;
  768. if (rp->offset >= rq->len) {
  769. rp->offset = 0;
  770. spin_lock(&queue_lock);
  771. list_move(&rp->q.list, &rq->q.list);
  772. spin_unlock(&queue_lock);
  773. }
  774. err = 0;
  775. }
  776. out:
  777. if (rp->offset == 0) {
  778. /* need to release rq */
  779. spin_lock(&queue_lock);
  780. rq->readers--;
  781. if (rq->readers == 0 &&
  782. !test_bit(CACHE_PENDING, &rq->item->flags)) {
  783. list_del(&rq->q.list);
  784. spin_unlock(&queue_lock);
  785. cache_put(rq->item, cd);
  786. kfree(rq->buf);
  787. kfree(rq);
  788. } else
  789. spin_unlock(&queue_lock);
  790. }
  791. if (err == -EAGAIN)
  792. goto again;
  793. inode_unlock(inode);
  794. return err ? err : count;
  795. }
  796. static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
  797. size_t count, struct cache_detail *cd)
  798. {
  799. ssize_t ret;
  800. if (count == 0)
  801. return -EINVAL;
  802. if (copy_from_user(kaddr, buf, count))
  803. return -EFAULT;
  804. kaddr[count] = '\0';
  805. ret = cd->cache_parse(cd, kaddr, count);
  806. if (!ret)
  807. ret = count;
  808. return ret;
  809. }
  810. static ssize_t cache_downcall(struct address_space *mapping,
  811. const char __user *buf,
  812. size_t count, struct cache_detail *cd)
  813. {
  814. char *write_buf;
  815. ssize_t ret = -ENOMEM;
  816. if (count >= 32768) { /* 32k is max userland buffer, lets check anyway */
  817. ret = -EINVAL;
  818. goto out;
  819. }
  820. write_buf = kvmalloc(count + 1, GFP_KERNEL);
  821. if (!write_buf)
  822. goto out;
  823. ret = cache_do_downcall(write_buf, buf, count, cd);
  824. kvfree(write_buf);
  825. out:
  826. return ret;
  827. }
  828. static ssize_t cache_write(struct file *filp, const char __user *buf,
  829. size_t count, loff_t *ppos,
  830. struct cache_detail *cd)
  831. {
  832. struct address_space *mapping = filp->f_mapping;
  833. struct inode *inode = file_inode(filp);
  834. ssize_t ret = -EINVAL;
  835. if (!cd->cache_parse)
  836. goto out;
  837. inode_lock(inode);
  838. ret = cache_downcall(mapping, buf, count, cd);
  839. inode_unlock(inode);
  840. out:
  841. return ret;
  842. }
  843. static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
  844. static __poll_t cache_poll(struct file *filp, poll_table *wait,
  845. struct cache_detail *cd)
  846. {
  847. __poll_t mask;
  848. struct cache_reader *rp = filp->private_data;
  849. struct cache_queue *cq;
  850. poll_wait(filp, &queue_wait, wait);
  851. /* alway allow write */
  852. mask = EPOLLOUT | EPOLLWRNORM;
  853. if (!rp)
  854. return mask;
  855. spin_lock(&queue_lock);
  856. for (cq= &rp->q; &cq->list != &cd->queue;
  857. cq = list_entry(cq->list.next, struct cache_queue, list))
  858. if (!cq->reader) {
  859. mask |= EPOLLIN | EPOLLRDNORM;
  860. break;
  861. }
  862. spin_unlock(&queue_lock);
  863. return mask;
  864. }
  865. static int cache_ioctl(struct inode *ino, struct file *filp,
  866. unsigned int cmd, unsigned long arg,
  867. struct cache_detail *cd)
  868. {
  869. int len = 0;
  870. struct cache_reader *rp = filp->private_data;
  871. struct cache_queue *cq;
  872. if (cmd != FIONREAD || !rp)
  873. return -EINVAL;
  874. spin_lock(&queue_lock);
  875. /* only find the length remaining in current request,
  876. * or the length of the next request
  877. */
  878. for (cq= &rp->q; &cq->list != &cd->queue;
  879. cq = list_entry(cq->list.next, struct cache_queue, list))
  880. if (!cq->reader) {
  881. struct cache_request *cr =
  882. container_of(cq, struct cache_request, q);
  883. len = cr->len - rp->offset;
  884. break;
  885. }
  886. spin_unlock(&queue_lock);
  887. return put_user(len, (int __user *)arg);
  888. }
  889. static int cache_open(struct inode *inode, struct file *filp,
  890. struct cache_detail *cd)
  891. {
  892. struct cache_reader *rp = NULL;
  893. if (!cd || !try_module_get(cd->owner))
  894. return -EACCES;
  895. nonseekable_open(inode, filp);
  896. if (filp->f_mode & FMODE_READ) {
  897. rp = kmalloc(sizeof(*rp), GFP_KERNEL);
  898. if (!rp) {
  899. module_put(cd->owner);
  900. return -ENOMEM;
  901. }
  902. rp->offset = 0;
  903. rp->q.reader = 1;
  904. spin_lock(&queue_lock);
  905. list_add(&rp->q.list, &cd->queue);
  906. spin_unlock(&queue_lock);
  907. }
  908. if (filp->f_mode & FMODE_WRITE)
  909. atomic_inc(&cd->writers);
  910. filp->private_data = rp;
  911. return 0;
  912. }
  913. static int cache_release(struct inode *inode, struct file *filp,
  914. struct cache_detail *cd)
  915. {
  916. struct cache_reader *rp = filp->private_data;
  917. if (rp) {
  918. spin_lock(&queue_lock);
  919. if (rp->offset) {
  920. struct cache_queue *cq;
  921. for (cq= &rp->q; &cq->list != &cd->queue;
  922. cq = list_entry(cq->list.next, struct cache_queue, list))
  923. if (!cq->reader) {
  924. container_of(cq, struct cache_request, q)
  925. ->readers--;
  926. break;
  927. }
  928. rp->offset = 0;
  929. }
  930. list_del(&rp->q.list);
  931. spin_unlock(&queue_lock);
  932. filp->private_data = NULL;
  933. kfree(rp);
  934. }
  935. if (filp->f_mode & FMODE_WRITE) {
  936. atomic_dec(&cd->writers);
  937. cd->last_close = seconds_since_boot();
  938. }
  939. module_put(cd->owner);
  940. return 0;
  941. }
  942. static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
  943. {
  944. struct cache_queue *cq, *tmp;
  945. struct cache_request *cr;
  946. struct list_head dequeued;
  947. INIT_LIST_HEAD(&dequeued);
  948. spin_lock(&queue_lock);
  949. list_for_each_entry_safe(cq, tmp, &detail->queue, list)
  950. if (!cq->reader) {
  951. cr = container_of(cq, struct cache_request, q);
  952. if (cr->item != ch)
  953. continue;
  954. if (test_bit(CACHE_PENDING, &ch->flags))
  955. /* Lost a race and it is pending again */
  956. break;
  957. if (cr->readers != 0)
  958. continue;
  959. list_move(&cr->q.list, &dequeued);
  960. }
  961. spin_unlock(&queue_lock);
  962. while (!list_empty(&dequeued)) {
  963. cr = list_entry(dequeued.next, struct cache_request, q.list);
  964. list_del(&cr->q.list);
  965. cache_put(cr->item, detail);
  966. kfree(cr->buf);
  967. kfree(cr);
  968. }
  969. }
  970. /*
  971. * Support routines for text-based upcalls.
  972. * Fields are separated by spaces.
  973. * Fields are either mangled to quote space tab newline slosh with slosh
  974. * or a hexified with a leading \x
  975. * Record is terminated with newline.
  976. *
  977. */
  978. void qword_add(char **bpp, int *lp, char *str)
  979. {
  980. char *bp = *bpp;
  981. int len = *lp;
  982. int ret;
  983. if (len < 0) return;
  984. ret = string_escape_str(str, bp, len, ESCAPE_OCTAL, "\\ \n\t");
  985. if (ret >= len) {
  986. bp += len;
  987. len = -1;
  988. } else {
  989. bp += ret;
  990. len -= ret;
  991. *bp++ = ' ';
  992. len--;
  993. }
  994. *bpp = bp;
  995. *lp = len;
  996. }
  997. EXPORT_SYMBOL_GPL(qword_add);
  998. void qword_addhex(char **bpp, int *lp, char *buf, int blen)
  999. {
  1000. char *bp = *bpp;
  1001. int len = *lp;
  1002. if (len < 0) return;
  1003. if (len > 2) {
  1004. *bp++ = '\\';
  1005. *bp++ = 'x';
  1006. len -= 2;
  1007. while (blen && len >= 2) {
  1008. bp = hex_byte_pack(bp, *buf++);
  1009. len -= 2;
  1010. blen--;
  1011. }
  1012. }
  1013. if (blen || len<1) len = -1;
  1014. else {
  1015. *bp++ = ' ';
  1016. len--;
  1017. }
  1018. *bpp = bp;
  1019. *lp = len;
  1020. }
  1021. EXPORT_SYMBOL_GPL(qword_addhex);
  1022. static void warn_no_listener(struct cache_detail *detail)
  1023. {
  1024. if (detail->last_warn != detail->last_close) {
  1025. detail->last_warn = detail->last_close;
  1026. if (detail->warn_no_listener)
  1027. detail->warn_no_listener(detail, detail->last_close != 0);
  1028. }
  1029. }
  1030. static bool cache_listeners_exist(struct cache_detail *detail)
  1031. {
  1032. if (atomic_read(&detail->writers))
  1033. return true;
  1034. if (detail->last_close == 0)
  1035. /* This cache was never opened */
  1036. return false;
  1037. if (detail->last_close < seconds_since_boot() - 30)
  1038. /*
  1039. * We allow for the possibility that someone might
  1040. * restart a userspace daemon without restarting the
  1041. * server; but after 30 seconds, we give up.
  1042. */
  1043. return false;
  1044. return true;
  1045. }
  1046. /*
  1047. * register an upcall request to user-space and queue it up for read() by the
  1048. * upcall daemon.
  1049. *
  1050. * Each request is at most one page long.
  1051. */
  1052. static int cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
  1053. {
  1054. char *buf;
  1055. struct cache_request *crq;
  1056. int ret = 0;
  1057. if (test_bit(CACHE_CLEANED, &h->flags))
  1058. /* Too late to make an upcall */
  1059. return -EAGAIN;
  1060. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1061. if (!buf)
  1062. return -EAGAIN;
  1063. crq = kmalloc(sizeof (*crq), GFP_KERNEL);
  1064. if (!crq) {
  1065. kfree(buf);
  1066. return -EAGAIN;
  1067. }
  1068. crq->q.reader = 0;
  1069. crq->buf = buf;
  1070. crq->len = 0;
  1071. crq->readers = 0;
  1072. spin_lock(&queue_lock);
  1073. if (test_bit(CACHE_PENDING, &h->flags)) {
  1074. crq->item = cache_get(h);
  1075. list_add_tail(&crq->q.list, &detail->queue);
  1076. trace_cache_entry_upcall(detail, h);
  1077. } else
  1078. /* Lost a race, no longer PENDING, so don't enqueue */
  1079. ret = -EAGAIN;
  1080. spin_unlock(&queue_lock);
  1081. wake_up(&queue_wait);
  1082. if (ret == -EAGAIN) {
  1083. kfree(buf);
  1084. kfree(crq);
  1085. }
  1086. return ret;
  1087. }
  1088. int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
  1089. {
  1090. if (test_and_set_bit(CACHE_PENDING, &h->flags))
  1091. return 0;
  1092. return cache_pipe_upcall(detail, h);
  1093. }
  1094. EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
  1095. int sunrpc_cache_pipe_upcall_timeout(struct cache_detail *detail,
  1096. struct cache_head *h)
  1097. {
  1098. if (!cache_listeners_exist(detail)) {
  1099. warn_no_listener(detail);
  1100. trace_cache_entry_no_listener(detail, h);
  1101. return -EINVAL;
  1102. }
  1103. return sunrpc_cache_pipe_upcall(detail, h);
  1104. }
  1105. EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall_timeout);
  1106. /*
  1107. * parse a message from user-space and pass it
  1108. * to an appropriate cache
  1109. * Messages are, like requests, separated into fields by
  1110. * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
  1111. *
  1112. * Message is
  1113. * reply cachename expiry key ... content....
  1114. *
  1115. * key and content are both parsed by cache
  1116. */
  1117. int qword_get(char **bpp, char *dest, int bufsize)
  1118. {
  1119. /* return bytes copied, or -1 on error */
  1120. char *bp = *bpp;
  1121. int len = 0;
  1122. while (*bp == ' ') bp++;
  1123. if (bp[0] == '\\' && bp[1] == 'x') {
  1124. /* HEX STRING */
  1125. bp += 2;
  1126. while (len < bufsize - 1) {
  1127. int h, l;
  1128. h = hex_to_bin(bp[0]);
  1129. if (h < 0)
  1130. break;
  1131. l = hex_to_bin(bp[1]);
  1132. if (l < 0)
  1133. break;
  1134. *dest++ = (h << 4) | l;
  1135. bp += 2;
  1136. len++;
  1137. }
  1138. } else {
  1139. /* text with \nnn octal quoting */
  1140. while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
  1141. if (*bp == '\\' &&
  1142. isodigit(bp[1]) && (bp[1] <= '3') &&
  1143. isodigit(bp[2]) &&
  1144. isodigit(bp[3])) {
  1145. int byte = (*++bp -'0');
  1146. bp++;
  1147. byte = (byte << 3) | (*bp++ - '0');
  1148. byte = (byte << 3) | (*bp++ - '0');
  1149. *dest++ = byte;
  1150. len++;
  1151. } else {
  1152. *dest++ = *bp++;
  1153. len++;
  1154. }
  1155. }
  1156. }
  1157. if (*bp != ' ' && *bp != '\n' && *bp != '\0')
  1158. return -1;
  1159. while (*bp == ' ') bp++;
  1160. *bpp = bp;
  1161. *dest = '\0';
  1162. return len;
  1163. }
  1164. EXPORT_SYMBOL_GPL(qword_get);
  1165. /*
  1166. * support /proc/net/rpc/$CACHENAME/content
  1167. * as a seqfile.
  1168. * We call ->cache_show passing NULL for the item to
  1169. * get a header, then pass each real item in the cache
  1170. */
  1171. static void *__cache_seq_start(struct seq_file *m, loff_t *pos)
  1172. {
  1173. loff_t n = *pos;
  1174. unsigned int hash, entry;
  1175. struct cache_head *ch;
  1176. struct cache_detail *cd = m->private;
  1177. if (!n--)
  1178. return SEQ_START_TOKEN;
  1179. hash = n >> 32;
  1180. entry = n & ((1LL<<32) - 1);
  1181. hlist_for_each_entry_rcu(ch, &cd->hash_table[hash], cache_list)
  1182. if (!entry--)
  1183. return ch;
  1184. n &= ~((1LL<<32) - 1);
  1185. do {
  1186. hash++;
  1187. n += 1LL<<32;
  1188. } while(hash < cd->hash_size &&
  1189. hlist_empty(&cd->hash_table[hash]));
  1190. if (hash >= cd->hash_size)
  1191. return NULL;
  1192. *pos = n+1;
  1193. return hlist_entry_safe(rcu_dereference_raw(
  1194. hlist_first_rcu(&cd->hash_table[hash])),
  1195. struct cache_head, cache_list);
  1196. }
  1197. static void *cache_seq_next(struct seq_file *m, void *p, loff_t *pos)
  1198. {
  1199. struct cache_head *ch = p;
  1200. int hash = (*pos >> 32);
  1201. struct cache_detail *cd = m->private;
  1202. if (p == SEQ_START_TOKEN)
  1203. hash = 0;
  1204. else if (ch->cache_list.next == NULL) {
  1205. hash++;
  1206. *pos += 1LL<<32;
  1207. } else {
  1208. ++*pos;
  1209. return hlist_entry_safe(rcu_dereference_raw(
  1210. hlist_next_rcu(&ch->cache_list)),
  1211. struct cache_head, cache_list);
  1212. }
  1213. *pos &= ~((1LL<<32) - 1);
  1214. while (hash < cd->hash_size &&
  1215. hlist_empty(&cd->hash_table[hash])) {
  1216. hash++;
  1217. *pos += 1LL<<32;
  1218. }
  1219. if (hash >= cd->hash_size)
  1220. return NULL;
  1221. ++*pos;
  1222. return hlist_entry_safe(rcu_dereference_raw(
  1223. hlist_first_rcu(&cd->hash_table[hash])),
  1224. struct cache_head, cache_list);
  1225. }
  1226. void *cache_seq_start_rcu(struct seq_file *m, loff_t *pos)
  1227. __acquires(RCU)
  1228. {
  1229. rcu_read_lock();
  1230. return __cache_seq_start(m, pos);
  1231. }
  1232. EXPORT_SYMBOL_GPL(cache_seq_start_rcu);
  1233. void *cache_seq_next_rcu(struct seq_file *file, void *p, loff_t *pos)
  1234. {
  1235. return cache_seq_next(file, p, pos);
  1236. }
  1237. EXPORT_SYMBOL_GPL(cache_seq_next_rcu);
  1238. void cache_seq_stop_rcu(struct seq_file *m, void *p)
  1239. __releases(RCU)
  1240. {
  1241. rcu_read_unlock();
  1242. }
  1243. EXPORT_SYMBOL_GPL(cache_seq_stop_rcu);
  1244. static int c_show(struct seq_file *m, void *p)
  1245. {
  1246. struct cache_head *cp = p;
  1247. struct cache_detail *cd = m->private;
  1248. if (p == SEQ_START_TOKEN)
  1249. return cd->cache_show(m, cd, NULL);
  1250. ifdebug(CACHE)
  1251. seq_printf(m, "# expiry=%lld refcnt=%d flags=%lx\n",
  1252. convert_to_wallclock(cp->expiry_time),
  1253. kref_read(&cp->ref), cp->flags);
  1254. cache_get(cp);
  1255. if (cache_check(cd, cp, NULL))
  1256. /* cache_check does a cache_put on failure */
  1257. seq_puts(m, "# ");
  1258. else {
  1259. if (cache_is_expired(cd, cp))
  1260. seq_puts(m, "# ");
  1261. cache_put(cp, cd);
  1262. }
  1263. return cd->cache_show(m, cd, cp);
  1264. }
  1265. static const struct seq_operations cache_content_op = {
  1266. .start = cache_seq_start_rcu,
  1267. .next = cache_seq_next_rcu,
  1268. .stop = cache_seq_stop_rcu,
  1269. .show = c_show,
  1270. };
  1271. static int content_open(struct inode *inode, struct file *file,
  1272. struct cache_detail *cd)
  1273. {
  1274. struct seq_file *seq;
  1275. int err;
  1276. if (!cd || !try_module_get(cd->owner))
  1277. return -EACCES;
  1278. err = seq_open(file, &cache_content_op);
  1279. if (err) {
  1280. module_put(cd->owner);
  1281. return err;
  1282. }
  1283. seq = file->private_data;
  1284. seq->private = cd;
  1285. return 0;
  1286. }
  1287. static int content_release(struct inode *inode, struct file *file,
  1288. struct cache_detail *cd)
  1289. {
  1290. int ret = seq_release(inode, file);
  1291. module_put(cd->owner);
  1292. return ret;
  1293. }
  1294. static int open_flush(struct inode *inode, struct file *file,
  1295. struct cache_detail *cd)
  1296. {
  1297. if (!cd || !try_module_get(cd->owner))
  1298. return -EACCES;
  1299. return nonseekable_open(inode, file);
  1300. }
  1301. static int release_flush(struct inode *inode, struct file *file,
  1302. struct cache_detail *cd)
  1303. {
  1304. module_put(cd->owner);
  1305. return 0;
  1306. }
  1307. static ssize_t read_flush(struct file *file, char __user *buf,
  1308. size_t count, loff_t *ppos,
  1309. struct cache_detail *cd)
  1310. {
  1311. char tbuf[22];
  1312. size_t len;
  1313. len = snprintf(tbuf, sizeof(tbuf), "%llu\n",
  1314. convert_to_wallclock(cd->flush_time));
  1315. return simple_read_from_buffer(buf, count, ppos, tbuf, len);
  1316. }
  1317. static ssize_t write_flush(struct file *file, const char __user *buf,
  1318. size_t count, loff_t *ppos,
  1319. struct cache_detail *cd)
  1320. {
  1321. char tbuf[20];
  1322. char *ep;
  1323. time64_t now;
  1324. if (*ppos || count > sizeof(tbuf)-1)
  1325. return -EINVAL;
  1326. if (copy_from_user(tbuf, buf, count))
  1327. return -EFAULT;
  1328. tbuf[count] = 0;
  1329. simple_strtoul(tbuf, &ep, 0);
  1330. if (*ep && *ep != '\n')
  1331. return -EINVAL;
  1332. /* Note that while we check that 'buf' holds a valid number,
  1333. * we always ignore the value and just flush everything.
  1334. * Making use of the number leads to races.
  1335. */
  1336. now = seconds_since_boot();
  1337. /* Always flush everything, so behave like cache_purge()
  1338. * Do this by advancing flush_time to the current time,
  1339. * or by one second if it has already reached the current time.
  1340. * Newly added cache entries will always have ->last_refresh greater
  1341. * that ->flush_time, so they don't get flushed prematurely.
  1342. */
  1343. if (cd->flush_time >= now)
  1344. now = cd->flush_time + 1;
  1345. cd->flush_time = now;
  1346. cd->nextcheck = now;
  1347. cache_flush();
  1348. if (cd->flush)
  1349. cd->flush();
  1350. *ppos += count;
  1351. return count;
  1352. }
  1353. static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
  1354. size_t count, loff_t *ppos)
  1355. {
  1356. struct cache_detail *cd = pde_data(file_inode(filp));
  1357. return cache_read(filp, buf, count, ppos, cd);
  1358. }
  1359. static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
  1360. size_t count, loff_t *ppos)
  1361. {
  1362. struct cache_detail *cd = pde_data(file_inode(filp));
  1363. return cache_write(filp, buf, count, ppos, cd);
  1364. }
  1365. static __poll_t cache_poll_procfs(struct file *filp, poll_table *wait)
  1366. {
  1367. struct cache_detail *cd = pde_data(file_inode(filp));
  1368. return cache_poll(filp, wait, cd);
  1369. }
  1370. static long cache_ioctl_procfs(struct file *filp,
  1371. unsigned int cmd, unsigned long arg)
  1372. {
  1373. struct inode *inode = file_inode(filp);
  1374. struct cache_detail *cd = pde_data(inode);
  1375. return cache_ioctl(inode, filp, cmd, arg, cd);
  1376. }
  1377. static int cache_open_procfs(struct inode *inode, struct file *filp)
  1378. {
  1379. struct cache_detail *cd = pde_data(inode);
  1380. return cache_open(inode, filp, cd);
  1381. }
  1382. static int cache_release_procfs(struct inode *inode, struct file *filp)
  1383. {
  1384. struct cache_detail *cd = pde_data(inode);
  1385. return cache_release(inode, filp, cd);
  1386. }
  1387. static const struct proc_ops cache_channel_proc_ops = {
  1388. .proc_lseek = no_llseek,
  1389. .proc_read = cache_read_procfs,
  1390. .proc_write = cache_write_procfs,
  1391. .proc_poll = cache_poll_procfs,
  1392. .proc_ioctl = cache_ioctl_procfs, /* for FIONREAD */
  1393. .proc_open = cache_open_procfs,
  1394. .proc_release = cache_release_procfs,
  1395. };
  1396. static int content_open_procfs(struct inode *inode, struct file *filp)
  1397. {
  1398. struct cache_detail *cd = pde_data(inode);
  1399. return content_open(inode, filp, cd);
  1400. }
  1401. static int content_release_procfs(struct inode *inode, struct file *filp)
  1402. {
  1403. struct cache_detail *cd = pde_data(inode);
  1404. return content_release(inode, filp, cd);
  1405. }
  1406. static const struct proc_ops content_proc_ops = {
  1407. .proc_open = content_open_procfs,
  1408. .proc_read = seq_read,
  1409. .proc_lseek = seq_lseek,
  1410. .proc_release = content_release_procfs,
  1411. };
  1412. static int open_flush_procfs(struct inode *inode, struct file *filp)
  1413. {
  1414. struct cache_detail *cd = pde_data(inode);
  1415. return open_flush(inode, filp, cd);
  1416. }
  1417. static int release_flush_procfs(struct inode *inode, struct file *filp)
  1418. {
  1419. struct cache_detail *cd = pde_data(inode);
  1420. return release_flush(inode, filp, cd);
  1421. }
  1422. static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
  1423. size_t count, loff_t *ppos)
  1424. {
  1425. struct cache_detail *cd = pde_data(file_inode(filp));
  1426. return read_flush(filp, buf, count, ppos, cd);
  1427. }
  1428. static ssize_t write_flush_procfs(struct file *filp,
  1429. const char __user *buf,
  1430. size_t count, loff_t *ppos)
  1431. {
  1432. struct cache_detail *cd = pde_data(file_inode(filp));
  1433. return write_flush(filp, buf, count, ppos, cd);
  1434. }
  1435. static const struct proc_ops cache_flush_proc_ops = {
  1436. .proc_open = open_flush_procfs,
  1437. .proc_read = read_flush_procfs,
  1438. .proc_write = write_flush_procfs,
  1439. .proc_release = release_flush_procfs,
  1440. .proc_lseek = no_llseek,
  1441. };
  1442. static void remove_cache_proc_entries(struct cache_detail *cd)
  1443. {
  1444. if (cd->procfs) {
  1445. proc_remove(cd->procfs);
  1446. cd->procfs = NULL;
  1447. }
  1448. }
  1449. #ifdef CONFIG_PROC_FS
  1450. static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
  1451. {
  1452. struct proc_dir_entry *p;
  1453. struct sunrpc_net *sn;
  1454. sn = net_generic(net, sunrpc_net_id);
  1455. cd->procfs = proc_mkdir(cd->name, sn->proc_net_rpc);
  1456. if (cd->procfs == NULL)
  1457. goto out_nomem;
  1458. p = proc_create_data("flush", S_IFREG | 0600,
  1459. cd->procfs, &cache_flush_proc_ops, cd);
  1460. if (p == NULL)
  1461. goto out_nomem;
  1462. if (cd->cache_request || cd->cache_parse) {
  1463. p = proc_create_data("channel", S_IFREG | 0600, cd->procfs,
  1464. &cache_channel_proc_ops, cd);
  1465. if (p == NULL)
  1466. goto out_nomem;
  1467. }
  1468. if (cd->cache_show) {
  1469. p = proc_create_data("content", S_IFREG | 0400, cd->procfs,
  1470. &content_proc_ops, cd);
  1471. if (p == NULL)
  1472. goto out_nomem;
  1473. }
  1474. return 0;
  1475. out_nomem:
  1476. remove_cache_proc_entries(cd);
  1477. return -ENOMEM;
  1478. }
  1479. #else /* CONFIG_PROC_FS */
  1480. static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
  1481. {
  1482. return 0;
  1483. }
  1484. #endif
  1485. void __init cache_initialize(void)
  1486. {
  1487. INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean);
  1488. }
  1489. int cache_register_net(struct cache_detail *cd, struct net *net)
  1490. {
  1491. int ret;
  1492. sunrpc_init_cache_detail(cd);
  1493. ret = create_cache_proc_entries(cd, net);
  1494. if (ret)
  1495. sunrpc_destroy_cache_detail(cd);
  1496. return ret;
  1497. }
  1498. EXPORT_SYMBOL_GPL(cache_register_net);
  1499. void cache_unregister_net(struct cache_detail *cd, struct net *net)
  1500. {
  1501. remove_cache_proc_entries(cd);
  1502. sunrpc_destroy_cache_detail(cd);
  1503. }
  1504. EXPORT_SYMBOL_GPL(cache_unregister_net);
  1505. struct cache_detail *cache_create_net(const struct cache_detail *tmpl, struct net *net)
  1506. {
  1507. struct cache_detail *cd;
  1508. int i;
  1509. cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL);
  1510. if (cd == NULL)
  1511. return ERR_PTR(-ENOMEM);
  1512. cd->hash_table = kcalloc(cd->hash_size, sizeof(struct hlist_head),
  1513. GFP_KERNEL);
  1514. if (cd->hash_table == NULL) {
  1515. kfree(cd);
  1516. return ERR_PTR(-ENOMEM);
  1517. }
  1518. for (i = 0; i < cd->hash_size; i++)
  1519. INIT_HLIST_HEAD(&cd->hash_table[i]);
  1520. cd->net = net;
  1521. return cd;
  1522. }
  1523. EXPORT_SYMBOL_GPL(cache_create_net);
  1524. void cache_destroy_net(struct cache_detail *cd, struct net *net)
  1525. {
  1526. kfree(cd->hash_table);
  1527. kfree(cd);
  1528. }
  1529. EXPORT_SYMBOL_GPL(cache_destroy_net);
  1530. static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
  1531. size_t count, loff_t *ppos)
  1532. {
  1533. struct cache_detail *cd = RPC_I(file_inode(filp))->private;
  1534. return cache_read(filp, buf, count, ppos, cd);
  1535. }
  1536. static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
  1537. size_t count, loff_t *ppos)
  1538. {
  1539. struct cache_detail *cd = RPC_I(file_inode(filp))->private;
  1540. return cache_write(filp, buf, count, ppos, cd);
  1541. }
  1542. static __poll_t cache_poll_pipefs(struct file *filp, poll_table *wait)
  1543. {
  1544. struct cache_detail *cd = RPC_I(file_inode(filp))->private;
  1545. return cache_poll(filp, wait, cd);
  1546. }
  1547. static long cache_ioctl_pipefs(struct file *filp,
  1548. unsigned int cmd, unsigned long arg)
  1549. {
  1550. struct inode *inode = file_inode(filp);
  1551. struct cache_detail *cd = RPC_I(inode)->private;
  1552. return cache_ioctl(inode, filp, cmd, arg, cd);
  1553. }
  1554. static int cache_open_pipefs(struct inode *inode, struct file *filp)
  1555. {
  1556. struct cache_detail *cd = RPC_I(inode)->private;
  1557. return cache_open(inode, filp, cd);
  1558. }
  1559. static int cache_release_pipefs(struct inode *inode, struct file *filp)
  1560. {
  1561. struct cache_detail *cd = RPC_I(inode)->private;
  1562. return cache_release(inode, filp, cd);
  1563. }
  1564. const struct file_operations cache_file_operations_pipefs = {
  1565. .owner = THIS_MODULE,
  1566. .llseek = no_llseek,
  1567. .read = cache_read_pipefs,
  1568. .write = cache_write_pipefs,
  1569. .poll = cache_poll_pipefs,
  1570. .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
  1571. .open = cache_open_pipefs,
  1572. .release = cache_release_pipefs,
  1573. };
  1574. static int content_open_pipefs(struct inode *inode, struct file *filp)
  1575. {
  1576. struct cache_detail *cd = RPC_I(inode)->private;
  1577. return content_open(inode, filp, cd);
  1578. }
  1579. static int content_release_pipefs(struct inode *inode, struct file *filp)
  1580. {
  1581. struct cache_detail *cd = RPC_I(inode)->private;
  1582. return content_release(inode, filp, cd);
  1583. }
  1584. const struct file_operations content_file_operations_pipefs = {
  1585. .open = content_open_pipefs,
  1586. .read = seq_read,
  1587. .llseek = seq_lseek,
  1588. .release = content_release_pipefs,
  1589. };
  1590. static int open_flush_pipefs(struct inode *inode, struct file *filp)
  1591. {
  1592. struct cache_detail *cd = RPC_I(inode)->private;
  1593. return open_flush(inode, filp, cd);
  1594. }
  1595. static int release_flush_pipefs(struct inode *inode, struct file *filp)
  1596. {
  1597. struct cache_detail *cd = RPC_I(inode)->private;
  1598. return release_flush(inode, filp, cd);
  1599. }
  1600. static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
  1601. size_t count, loff_t *ppos)
  1602. {
  1603. struct cache_detail *cd = RPC_I(file_inode(filp))->private;
  1604. return read_flush(filp, buf, count, ppos, cd);
  1605. }
  1606. static ssize_t write_flush_pipefs(struct file *filp,
  1607. const char __user *buf,
  1608. size_t count, loff_t *ppos)
  1609. {
  1610. struct cache_detail *cd = RPC_I(file_inode(filp))->private;
  1611. return write_flush(filp, buf, count, ppos, cd);
  1612. }
  1613. const struct file_operations cache_flush_operations_pipefs = {
  1614. .open = open_flush_pipefs,
  1615. .read = read_flush_pipefs,
  1616. .write = write_flush_pipefs,
  1617. .release = release_flush_pipefs,
  1618. .llseek = no_llseek,
  1619. };
  1620. int sunrpc_cache_register_pipefs(struct dentry *parent,
  1621. const char *name, umode_t umode,
  1622. struct cache_detail *cd)
  1623. {
  1624. struct dentry *dir = rpc_create_cache_dir(parent, name, umode, cd);
  1625. if (IS_ERR(dir))
  1626. return PTR_ERR(dir);
  1627. cd->pipefs = dir;
  1628. return 0;
  1629. }
  1630. EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
  1631. void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
  1632. {
  1633. if (cd->pipefs) {
  1634. rpc_remove_cache_dir(cd->pipefs);
  1635. cd->pipefs = NULL;
  1636. }
  1637. }
  1638. EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
  1639. void sunrpc_cache_unhash(struct cache_detail *cd, struct cache_head *h)
  1640. {
  1641. spin_lock(&cd->hash_lock);
  1642. if (!hlist_unhashed(&h->cache_list)){
  1643. sunrpc_begin_cache_remove_entry(h, cd);
  1644. spin_unlock(&cd->hash_lock);
  1645. sunrpc_end_cache_remove_entry(h, cd);
  1646. } else
  1647. spin_unlock(&cd->hash_lock);
  1648. }
  1649. EXPORT_SYMBOL_GPL(sunrpc_cache_unhash);