cache.c 45 KB

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