cache.c 43 KB

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