cache.c 43 KB

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