cache.c 43 KB

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