cache.c 44 KB

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