cache.c 44 KB

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