cache.c 42 KB

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