cache.c 43 KB

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