cache.c 44 KB

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