cache.c 44 KB

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