cache.c 43 KB

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