cache.c 46 KB

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