cache.c 45 KB

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