cache.c 44 KB

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