cache.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  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 <asm/ioctls.h>
  30. #include <linux/sunrpc/types.h>
  31. #include <linux/sunrpc/cache.h>
  32. #include <linux/sunrpc/stats.h>
  33. #define RPCDBG_FACILITY RPCDBG_CACHE
  34. static void cache_defer_req(struct cache_req *req, struct cache_head *item);
  35. static void cache_revisit_request(struct cache_head *item);
  36. void cache_init(struct cache_head *h)
  37. {
  38. time_t now = get_seconds();
  39. h->next = NULL;
  40. h->flags = 0;
  41. atomic_set(&h->refcnt, 1);
  42. h->expiry_time = now + CACHE_NEW_EXPIRY;
  43. h->last_refresh = now;
  44. }
  45. struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
  46. struct cache_head *key, int hash)
  47. {
  48. struct cache_head **head, **hp;
  49. struct cache_head *new = NULL;
  50. head = &detail->hash_table[hash];
  51. read_lock(&detail->hash_lock);
  52. for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
  53. struct cache_head *tmp = *hp;
  54. if (detail->match(tmp, key)) {
  55. cache_get(tmp);
  56. read_unlock(&detail->hash_lock);
  57. return tmp;
  58. }
  59. }
  60. read_unlock(&detail->hash_lock);
  61. /* Didn't find anything, insert an empty entry */
  62. new = detail->alloc();
  63. if (!new)
  64. return NULL;
  65. cache_init(new);
  66. write_lock(&detail->hash_lock);
  67. /* check if entry appeared while we slept */
  68. for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
  69. struct cache_head *tmp = *hp;
  70. if (detail->match(tmp, key)) {
  71. cache_get(tmp);
  72. write_unlock(&detail->hash_lock);
  73. detail->cache_put(new, detail);
  74. return tmp;
  75. }
  76. }
  77. detail->init(new, key);
  78. new->next = *head;
  79. *head = new;
  80. detail->entries++;
  81. cache_get(new);
  82. write_unlock(&detail->hash_lock);
  83. return new;
  84. }
  85. EXPORT_SYMBOL(sunrpc_cache_lookup);
  86. struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
  87. struct cache_head *new, struct cache_head *old, int hash)
  88. {
  89. /* The 'old' entry is to be replaced by 'new'.
  90. * If 'old' is not VALID, we update it directly,
  91. * otherwise we need to replace it
  92. */
  93. struct cache_head **head;
  94. struct cache_head *tmp;
  95. if (!test_bit(CACHE_VALID, &old->flags)) {
  96. write_lock(&detail->hash_lock);
  97. if (!test_bit(CACHE_VALID, &old->flags)) {
  98. if (test_bit(CACHE_NEGATIVE, &new->flags))
  99. set_bit(CACHE_NEGATIVE, &old->flags);
  100. else
  101. detail->update(old, new);
  102. /* FIXME cache_fresh should come first */
  103. write_unlock(&detail->hash_lock);
  104. cache_fresh(detail, old, new->expiry_time);
  105. return old;
  106. }
  107. write_unlock(&detail->hash_lock);
  108. }
  109. /* We need to insert a new entry */
  110. tmp = detail->alloc();
  111. if (!tmp) {
  112. detail->cache_put(old, detail);
  113. return NULL;
  114. }
  115. cache_init(tmp);
  116. detail->init(tmp, old);
  117. head = &detail->hash_table[hash];
  118. write_lock(&detail->hash_lock);
  119. if (test_bit(CACHE_NEGATIVE, &new->flags))
  120. set_bit(CACHE_NEGATIVE, &tmp->flags);
  121. else
  122. detail->update(tmp, new);
  123. tmp->next = *head;
  124. *head = tmp;
  125. cache_get(tmp);
  126. write_unlock(&detail->hash_lock);
  127. cache_fresh(detail, tmp, new->expiry_time);
  128. cache_fresh(detail, old, 0);
  129. detail->cache_put(old, detail);
  130. return tmp;
  131. }
  132. EXPORT_SYMBOL(sunrpc_cache_update);
  133. static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h);
  134. /*
  135. * This is the generic cache management routine for all
  136. * the authentication caches.
  137. * It checks the currency of a cache item and will (later)
  138. * initiate an upcall to fill it if needed.
  139. *
  140. *
  141. * Returns 0 if the cache_head can be used, or cache_puts it and returns
  142. * -EAGAIN if upcall is pending,
  143. * -ENOENT if cache entry was negative
  144. */
  145. int cache_check(struct cache_detail *detail,
  146. struct cache_head *h, struct cache_req *rqstp)
  147. {
  148. int rv;
  149. long refresh_age, age;
  150. /* First decide return status as best we can */
  151. if (!test_bit(CACHE_VALID, &h->flags) ||
  152. h->expiry_time < get_seconds())
  153. rv = -EAGAIN;
  154. else if (detail->flush_time > h->last_refresh)
  155. rv = -EAGAIN;
  156. else {
  157. /* entry is valid */
  158. if (test_bit(CACHE_NEGATIVE, &h->flags))
  159. rv = -ENOENT;
  160. else rv = 0;
  161. }
  162. /* now see if we want to start an upcall */
  163. refresh_age = (h->expiry_time - h->last_refresh);
  164. age = get_seconds() - h->last_refresh;
  165. if (rqstp == NULL) {
  166. if (rv == -EAGAIN)
  167. rv = -ENOENT;
  168. } else if (rv == -EAGAIN || age > refresh_age/2) {
  169. dprintk("Want update, refage=%ld, age=%ld\n", refresh_age, age);
  170. if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
  171. switch (cache_make_upcall(detail, h)) {
  172. case -EINVAL:
  173. clear_bit(CACHE_PENDING, &h->flags);
  174. if (rv == -EAGAIN) {
  175. set_bit(CACHE_NEGATIVE, &h->flags);
  176. cache_fresh(detail, h, get_seconds()+CACHE_NEW_EXPIRY);
  177. rv = -ENOENT;
  178. }
  179. break;
  180. case -EAGAIN:
  181. clear_bit(CACHE_PENDING, &h->flags);
  182. cache_revisit_request(h);
  183. break;
  184. }
  185. }
  186. }
  187. if (rv == -EAGAIN)
  188. cache_defer_req(rqstp, h);
  189. if (rv && h)
  190. detail->cache_put(h, detail);
  191. return rv;
  192. }
  193. static void queue_loose(struct cache_detail *detail, struct cache_head *ch);
  194. void cache_fresh(struct cache_detail *detail,
  195. struct cache_head *head, time_t expiry)
  196. {
  197. head->expiry_time = expiry;
  198. head->last_refresh = get_seconds();
  199. if (!test_and_set_bit(CACHE_VALID, &head->flags))
  200. cache_revisit_request(head);
  201. if (test_and_clear_bit(CACHE_PENDING, &head->flags))
  202. queue_loose(detail, head);
  203. }
  204. /*
  205. * caches need to be periodically cleaned.
  206. * For this we maintain a list of cache_detail and
  207. * a current pointer into that list and into the table
  208. * for that entry.
  209. *
  210. * Each time clean_cache is called it finds the next non-empty entry
  211. * in the current table and walks the list in that entry
  212. * looking for entries that can be removed.
  213. *
  214. * An entry gets removed if:
  215. * - The expiry is before current time
  216. * - The last_refresh time is before the flush_time for that cache
  217. *
  218. * later we might drop old entries with non-NEVER expiry if that table
  219. * is getting 'full' for some definition of 'full'
  220. *
  221. * The question of "how often to scan a table" is an interesting one
  222. * and is answered in part by the use of the "nextcheck" field in the
  223. * cache_detail.
  224. * When a scan of a table begins, the nextcheck field is set to a time
  225. * that is well into the future.
  226. * While scanning, if an expiry time is found that is earlier than the
  227. * current nextcheck time, nextcheck is set to that expiry time.
  228. * If the flush_time is ever set to a time earlier than the nextcheck
  229. * time, the nextcheck time is then set to that flush_time.
  230. *
  231. * A table is then only scanned if the current time is at least
  232. * the nextcheck time.
  233. *
  234. */
  235. static LIST_HEAD(cache_list);
  236. static DEFINE_SPINLOCK(cache_list_lock);
  237. static struct cache_detail *current_detail;
  238. static int current_index;
  239. static struct file_operations cache_file_operations;
  240. static struct file_operations content_file_operations;
  241. static struct file_operations cache_flush_operations;
  242. static void do_cache_clean(void *data);
  243. static DECLARE_WORK(cache_cleaner, do_cache_clean, NULL);
  244. void cache_register(struct cache_detail *cd)
  245. {
  246. cd->proc_ent = proc_mkdir(cd->name, proc_net_rpc);
  247. if (cd->proc_ent) {
  248. struct proc_dir_entry *p;
  249. cd->proc_ent->owner = cd->owner;
  250. cd->channel_ent = cd->content_ent = NULL;
  251. p = create_proc_entry("flush", S_IFREG|S_IRUSR|S_IWUSR,
  252. cd->proc_ent);
  253. cd->flush_ent = p;
  254. if (p) {
  255. p->proc_fops = &cache_flush_operations;
  256. p->owner = cd->owner;
  257. p->data = cd;
  258. }
  259. if (cd->cache_request || cd->cache_parse) {
  260. p = create_proc_entry("channel", S_IFREG|S_IRUSR|S_IWUSR,
  261. cd->proc_ent);
  262. cd->channel_ent = p;
  263. if (p) {
  264. p->proc_fops = &cache_file_operations;
  265. p->owner = cd->owner;
  266. p->data = cd;
  267. }
  268. }
  269. if (cd->cache_show) {
  270. p = create_proc_entry("content", S_IFREG|S_IRUSR|S_IWUSR,
  271. cd->proc_ent);
  272. cd->content_ent = p;
  273. if (p) {
  274. p->proc_fops = &content_file_operations;
  275. p->owner = cd->owner;
  276. p->data = cd;
  277. }
  278. }
  279. }
  280. rwlock_init(&cd->hash_lock);
  281. INIT_LIST_HEAD(&cd->queue);
  282. spin_lock(&cache_list_lock);
  283. cd->nextcheck = 0;
  284. cd->entries = 0;
  285. atomic_set(&cd->readers, 0);
  286. cd->last_close = 0;
  287. cd->last_warn = -1;
  288. list_add(&cd->others, &cache_list);
  289. spin_unlock(&cache_list_lock);
  290. /* start the cleaning process */
  291. schedule_work(&cache_cleaner);
  292. }
  293. int cache_unregister(struct cache_detail *cd)
  294. {
  295. cache_purge(cd);
  296. spin_lock(&cache_list_lock);
  297. write_lock(&cd->hash_lock);
  298. if (cd->entries || atomic_read(&cd->inuse)) {
  299. write_unlock(&cd->hash_lock);
  300. spin_unlock(&cache_list_lock);
  301. return -EBUSY;
  302. }
  303. if (current_detail == cd)
  304. current_detail = NULL;
  305. list_del_init(&cd->others);
  306. write_unlock(&cd->hash_lock);
  307. spin_unlock(&cache_list_lock);
  308. if (cd->proc_ent) {
  309. if (cd->flush_ent)
  310. remove_proc_entry("flush", cd->proc_ent);
  311. if (cd->channel_ent)
  312. remove_proc_entry("channel", cd->proc_ent);
  313. if (cd->content_ent)
  314. remove_proc_entry("content", cd->proc_ent);
  315. cd->proc_ent = NULL;
  316. remove_proc_entry(cd->name, proc_net_rpc);
  317. }
  318. if (list_empty(&cache_list)) {
  319. /* module must be being unloaded so its safe to kill the worker */
  320. cancel_delayed_work(&cache_cleaner);
  321. flush_scheduled_work();
  322. }
  323. return 0;
  324. }
  325. /* clean cache tries to find something to clean
  326. * and cleans it.
  327. * It returns 1 if it cleaned something,
  328. * 0 if it didn't find anything this time
  329. * -1 if it fell off the end of the list.
  330. */
  331. static int cache_clean(void)
  332. {
  333. int rv = 0;
  334. struct list_head *next;
  335. spin_lock(&cache_list_lock);
  336. /* find a suitable table if we don't already have one */
  337. while (current_detail == NULL ||
  338. current_index >= current_detail->hash_size) {
  339. if (current_detail)
  340. next = current_detail->others.next;
  341. else
  342. next = cache_list.next;
  343. if (next == &cache_list) {
  344. current_detail = NULL;
  345. spin_unlock(&cache_list_lock);
  346. return -1;
  347. }
  348. current_detail = list_entry(next, struct cache_detail, others);
  349. if (current_detail->nextcheck > get_seconds())
  350. current_index = current_detail->hash_size;
  351. else {
  352. current_index = 0;
  353. current_detail->nextcheck = get_seconds()+30*60;
  354. }
  355. }
  356. /* find a non-empty bucket in the table */
  357. while (current_detail &&
  358. current_index < current_detail->hash_size &&
  359. current_detail->hash_table[current_index] == NULL)
  360. current_index++;
  361. /* find a cleanable entry in the bucket and clean it, or set to next bucket */
  362. if (current_detail && current_index < current_detail->hash_size) {
  363. struct cache_head *ch, **cp;
  364. struct cache_detail *d;
  365. write_lock(&current_detail->hash_lock);
  366. /* Ok, now to clean this strand */
  367. cp = & current_detail->hash_table[current_index];
  368. ch = *cp;
  369. for (; ch; cp= & ch->next, ch= *cp) {
  370. if (current_detail->nextcheck > ch->expiry_time)
  371. current_detail->nextcheck = ch->expiry_time+1;
  372. if (ch->expiry_time >= get_seconds()
  373. && ch->last_refresh >= current_detail->flush_time
  374. )
  375. continue;
  376. if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
  377. queue_loose(current_detail, ch);
  378. if (atomic_read(&ch->refcnt) == 1)
  379. break;
  380. }
  381. if (ch) {
  382. *cp = ch->next;
  383. ch->next = NULL;
  384. current_detail->entries--;
  385. rv = 1;
  386. }
  387. write_unlock(&current_detail->hash_lock);
  388. d = current_detail;
  389. if (!ch)
  390. current_index ++;
  391. spin_unlock(&cache_list_lock);
  392. if (ch)
  393. d->cache_put(ch, d);
  394. } else
  395. spin_unlock(&cache_list_lock);
  396. return rv;
  397. }
  398. /*
  399. * We want to regularly clean the cache, so we need to schedule some work ...
  400. */
  401. static void do_cache_clean(void *data)
  402. {
  403. int delay = 5;
  404. if (cache_clean() == -1)
  405. delay = 30*HZ;
  406. if (list_empty(&cache_list))
  407. delay = 0;
  408. if (delay)
  409. schedule_delayed_work(&cache_cleaner, delay);
  410. }
  411. /*
  412. * Clean all caches promptly. This just calls cache_clean
  413. * repeatedly until we are sure that every cache has had a chance to
  414. * be fully cleaned
  415. */
  416. void cache_flush(void)
  417. {
  418. while (cache_clean() != -1)
  419. cond_resched();
  420. while (cache_clean() != -1)
  421. cond_resched();
  422. }
  423. void cache_purge(struct cache_detail *detail)
  424. {
  425. detail->flush_time = LONG_MAX;
  426. detail->nextcheck = get_seconds();
  427. cache_flush();
  428. detail->flush_time = 1;
  429. }
  430. /*
  431. * Deferral and Revisiting of Requests.
  432. *
  433. * If a cache lookup finds a pending entry, we
  434. * need to defer the request and revisit it later.
  435. * All deferred requests are stored in a hash table,
  436. * indexed by "struct cache_head *".
  437. * As it may be wasteful to store a whole request
  438. * structure, we allow the request to provide a
  439. * deferred form, which must contain a
  440. * 'struct cache_deferred_req'
  441. * This cache_deferred_req contains a method to allow
  442. * it to be revisited when cache info is available
  443. */
  444. #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
  445. #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
  446. #define DFR_MAX 300 /* ??? */
  447. static DEFINE_SPINLOCK(cache_defer_lock);
  448. static LIST_HEAD(cache_defer_list);
  449. static struct list_head cache_defer_hash[DFR_HASHSIZE];
  450. static int cache_defer_cnt;
  451. static void cache_defer_req(struct cache_req *req, struct cache_head *item)
  452. {
  453. struct cache_deferred_req *dreq;
  454. int hash = DFR_HASH(item);
  455. dreq = req->defer(req);
  456. if (dreq == NULL)
  457. return;
  458. dreq->item = item;
  459. dreq->recv_time = get_seconds();
  460. spin_lock(&cache_defer_lock);
  461. list_add(&dreq->recent, &cache_defer_list);
  462. if (cache_defer_hash[hash].next == NULL)
  463. INIT_LIST_HEAD(&cache_defer_hash[hash]);
  464. list_add(&dreq->hash, &cache_defer_hash[hash]);
  465. /* it is in, now maybe clean up */
  466. dreq = NULL;
  467. if (++cache_defer_cnt > DFR_MAX) {
  468. /* too much in the cache, randomly drop
  469. * first or last
  470. */
  471. if (net_random()&1)
  472. dreq = list_entry(cache_defer_list.next,
  473. struct cache_deferred_req,
  474. recent);
  475. else
  476. dreq = list_entry(cache_defer_list.prev,
  477. struct cache_deferred_req,
  478. recent);
  479. list_del(&dreq->recent);
  480. list_del(&dreq->hash);
  481. cache_defer_cnt--;
  482. }
  483. spin_unlock(&cache_defer_lock);
  484. if (dreq) {
  485. /* there was one too many */
  486. dreq->revisit(dreq, 1);
  487. }
  488. if (test_bit(CACHE_VALID, &item->flags)) {
  489. /* must have just been validated... */
  490. cache_revisit_request(item);
  491. }
  492. }
  493. static void cache_revisit_request(struct cache_head *item)
  494. {
  495. struct cache_deferred_req *dreq;
  496. struct list_head pending;
  497. struct list_head *lp;
  498. int hash = DFR_HASH(item);
  499. INIT_LIST_HEAD(&pending);
  500. spin_lock(&cache_defer_lock);
  501. lp = cache_defer_hash[hash].next;
  502. if (lp) {
  503. while (lp != &cache_defer_hash[hash]) {
  504. dreq = list_entry(lp, struct cache_deferred_req, hash);
  505. lp = lp->next;
  506. if (dreq->item == item) {
  507. list_del(&dreq->hash);
  508. list_move(&dreq->recent, &pending);
  509. cache_defer_cnt--;
  510. }
  511. }
  512. }
  513. spin_unlock(&cache_defer_lock);
  514. while (!list_empty(&pending)) {
  515. dreq = list_entry(pending.next, struct cache_deferred_req, recent);
  516. list_del_init(&dreq->recent);
  517. dreq->revisit(dreq, 0);
  518. }
  519. }
  520. void cache_clean_deferred(void *owner)
  521. {
  522. struct cache_deferred_req *dreq, *tmp;
  523. struct list_head pending;
  524. INIT_LIST_HEAD(&pending);
  525. spin_lock(&cache_defer_lock);
  526. list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
  527. if (dreq->owner == owner) {
  528. list_del(&dreq->hash);
  529. list_move(&dreq->recent, &pending);
  530. cache_defer_cnt--;
  531. }
  532. }
  533. spin_unlock(&cache_defer_lock);
  534. while (!list_empty(&pending)) {
  535. dreq = list_entry(pending.next, struct cache_deferred_req, recent);
  536. list_del_init(&dreq->recent);
  537. dreq->revisit(dreq, 1);
  538. }
  539. }
  540. /*
  541. * communicate with user-space
  542. *
  543. * We have a magic /proc file - /proc/sunrpc/cache
  544. * On read, you get a full request, or block
  545. * On write, an update request is processed
  546. * Poll works if anything to read, and always allows write
  547. *
  548. * Implemented by linked list of requests. Each open file has
  549. * a ->private that also exists in this list. New request are added
  550. * to the end and may wakeup and preceding readers.
  551. * New readers are added to the head. If, on read, an item is found with
  552. * CACHE_UPCALLING clear, we free it from the list.
  553. *
  554. */
  555. static DEFINE_SPINLOCK(queue_lock);
  556. static DEFINE_MUTEX(queue_io_mutex);
  557. struct cache_queue {
  558. struct list_head list;
  559. int reader; /* if 0, then request */
  560. };
  561. struct cache_request {
  562. struct cache_queue q;
  563. struct cache_head *item;
  564. char * buf;
  565. int len;
  566. int readers;
  567. };
  568. struct cache_reader {
  569. struct cache_queue q;
  570. int offset; /* if non-0, we have a refcnt on next request */
  571. };
  572. static ssize_t
  573. cache_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
  574. {
  575. struct cache_reader *rp = filp->private_data;
  576. struct cache_request *rq;
  577. struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
  578. int err;
  579. if (count == 0)
  580. return 0;
  581. mutex_lock(&queue_io_mutex); /* protect against multiple concurrent
  582. * readers on this file */
  583. again:
  584. spin_lock(&queue_lock);
  585. /* need to find next request */
  586. while (rp->q.list.next != &cd->queue &&
  587. list_entry(rp->q.list.next, struct cache_queue, list)
  588. ->reader) {
  589. struct list_head *next = rp->q.list.next;
  590. list_move(&rp->q.list, next);
  591. }
  592. if (rp->q.list.next == &cd->queue) {
  593. spin_unlock(&queue_lock);
  594. mutex_unlock(&queue_io_mutex);
  595. BUG_ON(rp->offset);
  596. return 0;
  597. }
  598. rq = container_of(rp->q.list.next, struct cache_request, q.list);
  599. BUG_ON(rq->q.reader);
  600. if (rp->offset == 0)
  601. rq->readers++;
  602. spin_unlock(&queue_lock);
  603. if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
  604. err = -EAGAIN;
  605. spin_lock(&queue_lock);
  606. list_move(&rp->q.list, &rq->q.list);
  607. spin_unlock(&queue_lock);
  608. } else {
  609. if (rp->offset + count > rq->len)
  610. count = rq->len - rp->offset;
  611. err = -EFAULT;
  612. if (copy_to_user(buf, rq->buf + rp->offset, count))
  613. goto out;
  614. rp->offset += count;
  615. if (rp->offset >= rq->len) {
  616. rp->offset = 0;
  617. spin_lock(&queue_lock);
  618. list_move(&rp->q.list, &rq->q.list);
  619. spin_unlock(&queue_lock);
  620. }
  621. err = 0;
  622. }
  623. out:
  624. if (rp->offset == 0) {
  625. /* need to release rq */
  626. spin_lock(&queue_lock);
  627. rq->readers--;
  628. if (rq->readers == 0 &&
  629. !test_bit(CACHE_PENDING, &rq->item->flags)) {
  630. list_del(&rq->q.list);
  631. spin_unlock(&queue_lock);
  632. cd->cache_put(rq->item, cd);
  633. kfree(rq->buf);
  634. kfree(rq);
  635. } else
  636. spin_unlock(&queue_lock);
  637. }
  638. if (err == -EAGAIN)
  639. goto again;
  640. mutex_unlock(&queue_io_mutex);
  641. return err ? err : count;
  642. }
  643. static char write_buf[8192]; /* protected by queue_io_mutex */
  644. static ssize_t
  645. cache_write(struct file *filp, const char __user *buf, size_t count,
  646. loff_t *ppos)
  647. {
  648. int err;
  649. struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
  650. if (count == 0)
  651. return 0;
  652. if (count >= sizeof(write_buf))
  653. return -EINVAL;
  654. mutex_lock(&queue_io_mutex);
  655. if (copy_from_user(write_buf, buf, count)) {
  656. mutex_unlock(&queue_io_mutex);
  657. return -EFAULT;
  658. }
  659. write_buf[count] = '\0';
  660. if (cd->cache_parse)
  661. err = cd->cache_parse(cd, write_buf, count);
  662. else
  663. err = -EINVAL;
  664. mutex_unlock(&queue_io_mutex);
  665. return err ? err : count;
  666. }
  667. static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
  668. static unsigned int
  669. cache_poll(struct file *filp, poll_table *wait)
  670. {
  671. unsigned int mask;
  672. struct cache_reader *rp = filp->private_data;
  673. struct cache_queue *cq;
  674. struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
  675. poll_wait(filp, &queue_wait, wait);
  676. /* alway allow write */
  677. mask = POLL_OUT | POLLWRNORM;
  678. if (!rp)
  679. return mask;
  680. spin_lock(&queue_lock);
  681. for (cq= &rp->q; &cq->list != &cd->queue;
  682. cq = list_entry(cq->list.next, struct cache_queue, list))
  683. if (!cq->reader) {
  684. mask |= POLLIN | POLLRDNORM;
  685. break;
  686. }
  687. spin_unlock(&queue_lock);
  688. return mask;
  689. }
  690. static int
  691. cache_ioctl(struct inode *ino, struct file *filp,
  692. unsigned int cmd, unsigned long arg)
  693. {
  694. int len = 0;
  695. struct cache_reader *rp = filp->private_data;
  696. struct cache_queue *cq;
  697. struct cache_detail *cd = PDE(ino)->data;
  698. if (cmd != FIONREAD || !rp)
  699. return -EINVAL;
  700. spin_lock(&queue_lock);
  701. /* only find the length remaining in current request,
  702. * or the length of the next request
  703. */
  704. for (cq= &rp->q; &cq->list != &cd->queue;
  705. cq = list_entry(cq->list.next, struct cache_queue, list))
  706. if (!cq->reader) {
  707. struct cache_request *cr =
  708. container_of(cq, struct cache_request, q);
  709. len = cr->len - rp->offset;
  710. break;
  711. }
  712. spin_unlock(&queue_lock);
  713. return put_user(len, (int __user *)arg);
  714. }
  715. static int
  716. cache_open(struct inode *inode, struct file *filp)
  717. {
  718. struct cache_reader *rp = NULL;
  719. nonseekable_open(inode, filp);
  720. if (filp->f_mode & FMODE_READ) {
  721. struct cache_detail *cd = PDE(inode)->data;
  722. rp = kmalloc(sizeof(*rp), GFP_KERNEL);
  723. if (!rp)
  724. return -ENOMEM;
  725. rp->offset = 0;
  726. rp->q.reader = 1;
  727. atomic_inc(&cd->readers);
  728. spin_lock(&queue_lock);
  729. list_add(&rp->q.list, &cd->queue);
  730. spin_unlock(&queue_lock);
  731. }
  732. filp->private_data = rp;
  733. return 0;
  734. }
  735. static int
  736. cache_release(struct inode *inode, struct file *filp)
  737. {
  738. struct cache_reader *rp = filp->private_data;
  739. struct cache_detail *cd = PDE(inode)->data;
  740. if (rp) {
  741. spin_lock(&queue_lock);
  742. if (rp->offset) {
  743. struct cache_queue *cq;
  744. for (cq= &rp->q; &cq->list != &cd->queue;
  745. cq = list_entry(cq->list.next, struct cache_queue, list))
  746. if (!cq->reader) {
  747. container_of(cq, struct cache_request, q)
  748. ->readers--;
  749. break;
  750. }
  751. rp->offset = 0;
  752. }
  753. list_del(&rp->q.list);
  754. spin_unlock(&queue_lock);
  755. filp->private_data = NULL;
  756. kfree(rp);
  757. cd->last_close = get_seconds();
  758. atomic_dec(&cd->readers);
  759. }
  760. return 0;
  761. }
  762. static struct file_operations cache_file_operations = {
  763. .owner = THIS_MODULE,
  764. .llseek = no_llseek,
  765. .read = cache_read,
  766. .write = cache_write,
  767. .poll = cache_poll,
  768. .ioctl = cache_ioctl, /* for FIONREAD */
  769. .open = cache_open,
  770. .release = cache_release,
  771. };
  772. static void queue_loose(struct cache_detail *detail, struct cache_head *ch)
  773. {
  774. struct cache_queue *cq;
  775. spin_lock(&queue_lock);
  776. list_for_each_entry(cq, &detail->queue, list)
  777. if (!cq->reader) {
  778. struct cache_request *cr = container_of(cq, struct cache_request, q);
  779. if (cr->item != ch)
  780. continue;
  781. if (cr->readers != 0)
  782. break;
  783. list_del(&cr->q.list);
  784. spin_unlock(&queue_lock);
  785. detail->cache_put(cr->item, detail);
  786. kfree(cr->buf);
  787. kfree(cr);
  788. return;
  789. }
  790. spin_unlock(&queue_lock);
  791. }
  792. /*
  793. * Support routines for text-based upcalls.
  794. * Fields are separated by spaces.
  795. * Fields are either mangled to quote space tab newline slosh with slosh
  796. * or a hexified with a leading \x
  797. * Record is terminated with newline.
  798. *
  799. */
  800. void qword_add(char **bpp, int *lp, char *str)
  801. {
  802. char *bp = *bpp;
  803. int len = *lp;
  804. char c;
  805. if (len < 0) return;
  806. while ((c=*str++) && len)
  807. switch(c) {
  808. case ' ':
  809. case '\t':
  810. case '\n':
  811. case '\\':
  812. if (len >= 4) {
  813. *bp++ = '\\';
  814. *bp++ = '0' + ((c & 0300)>>6);
  815. *bp++ = '0' + ((c & 0070)>>3);
  816. *bp++ = '0' + ((c & 0007)>>0);
  817. }
  818. len -= 4;
  819. break;
  820. default:
  821. *bp++ = c;
  822. len--;
  823. }
  824. if (c || len <1) len = -1;
  825. else {
  826. *bp++ = ' ';
  827. len--;
  828. }
  829. *bpp = bp;
  830. *lp = len;
  831. }
  832. void qword_addhex(char **bpp, int *lp, char *buf, int blen)
  833. {
  834. char *bp = *bpp;
  835. int len = *lp;
  836. if (len < 0) return;
  837. if (len > 2) {
  838. *bp++ = '\\';
  839. *bp++ = 'x';
  840. len -= 2;
  841. while (blen && len >= 2) {
  842. unsigned char c = *buf++;
  843. *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
  844. *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
  845. len -= 2;
  846. blen--;
  847. }
  848. }
  849. if (blen || len<1) len = -1;
  850. else {
  851. *bp++ = ' ';
  852. len--;
  853. }
  854. *bpp = bp;
  855. *lp = len;
  856. }
  857. static void warn_no_listener(struct cache_detail *detail)
  858. {
  859. if (detail->last_warn != detail->last_close) {
  860. detail->last_warn = detail->last_close;
  861. if (detail->warn_no_listener)
  862. detail->warn_no_listener(detail);
  863. }
  864. }
  865. /*
  866. * register an upcall request to user-space.
  867. * Each request is at most one page long.
  868. */
  869. static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h)
  870. {
  871. char *buf;
  872. struct cache_request *crq;
  873. char *bp;
  874. int len;
  875. if (detail->cache_request == NULL)
  876. return -EINVAL;
  877. if (atomic_read(&detail->readers) == 0 &&
  878. detail->last_close < get_seconds() - 30) {
  879. warn_no_listener(detail);
  880. return -EINVAL;
  881. }
  882. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  883. if (!buf)
  884. return -EAGAIN;
  885. crq = kmalloc(sizeof (*crq), GFP_KERNEL);
  886. if (!crq) {
  887. kfree(buf);
  888. return -EAGAIN;
  889. }
  890. bp = buf; len = PAGE_SIZE;
  891. detail->cache_request(detail, h, &bp, &len);
  892. if (len < 0) {
  893. kfree(buf);
  894. kfree(crq);
  895. return -EAGAIN;
  896. }
  897. crq->q.reader = 0;
  898. crq->item = cache_get(h);
  899. crq->buf = buf;
  900. crq->len = PAGE_SIZE - len;
  901. crq->readers = 0;
  902. spin_lock(&queue_lock);
  903. list_add_tail(&crq->q.list, &detail->queue);
  904. spin_unlock(&queue_lock);
  905. wake_up(&queue_wait);
  906. return 0;
  907. }
  908. /*
  909. * parse a message from user-space and pass it
  910. * to an appropriate cache
  911. * Messages are, like requests, separated into fields by
  912. * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
  913. *
  914. * Message is
  915. * reply cachename expiry key ... content....
  916. *
  917. * key and content are both parsed by cache
  918. */
  919. #define isodigit(c) (isdigit(c) && c <= '7')
  920. int qword_get(char **bpp, char *dest, int bufsize)
  921. {
  922. /* return bytes copied, or -1 on error */
  923. char *bp = *bpp;
  924. int len = 0;
  925. while (*bp == ' ') bp++;
  926. if (bp[0] == '\\' && bp[1] == 'x') {
  927. /* HEX STRING */
  928. bp += 2;
  929. while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
  930. int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
  931. bp++;
  932. byte <<= 4;
  933. byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
  934. *dest++ = byte;
  935. bp++;
  936. len++;
  937. }
  938. } else {
  939. /* text with \nnn octal quoting */
  940. while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
  941. if (*bp == '\\' &&
  942. isodigit(bp[1]) && (bp[1] <= '3') &&
  943. isodigit(bp[2]) &&
  944. isodigit(bp[3])) {
  945. int byte = (*++bp -'0');
  946. bp++;
  947. byte = (byte << 3) | (*bp++ - '0');
  948. byte = (byte << 3) | (*bp++ - '0');
  949. *dest++ = byte;
  950. len++;
  951. } else {
  952. *dest++ = *bp++;
  953. len++;
  954. }
  955. }
  956. }
  957. if (*bp != ' ' && *bp != '\n' && *bp != '\0')
  958. return -1;
  959. while (*bp == ' ') bp++;
  960. *bpp = bp;
  961. *dest = '\0';
  962. return len;
  963. }
  964. /*
  965. * support /proc/sunrpc/cache/$CACHENAME/content
  966. * as a seqfile.
  967. * We call ->cache_show passing NULL for the item to
  968. * get a header, then pass each real item in the cache
  969. */
  970. struct handle {
  971. struct cache_detail *cd;
  972. };
  973. static void *c_start(struct seq_file *m, loff_t *pos)
  974. {
  975. loff_t n = *pos;
  976. unsigned hash, entry;
  977. struct cache_head *ch;
  978. struct cache_detail *cd = ((struct handle*)m->private)->cd;
  979. read_lock(&cd->hash_lock);
  980. if (!n--)
  981. return SEQ_START_TOKEN;
  982. hash = n >> 32;
  983. entry = n & ((1LL<<32) - 1);
  984. for (ch=cd->hash_table[hash]; ch; ch=ch->next)
  985. if (!entry--)
  986. return ch;
  987. n &= ~((1LL<<32) - 1);
  988. do {
  989. hash++;
  990. n += 1LL<<32;
  991. } while(hash < cd->hash_size &&
  992. cd->hash_table[hash]==NULL);
  993. if (hash >= cd->hash_size)
  994. return NULL;
  995. *pos = n+1;
  996. return cd->hash_table[hash];
  997. }
  998. static void *c_next(struct seq_file *m, void *p, loff_t *pos)
  999. {
  1000. struct cache_head *ch = p;
  1001. int hash = (*pos >> 32);
  1002. struct cache_detail *cd = ((struct handle*)m->private)->cd;
  1003. if (p == SEQ_START_TOKEN)
  1004. hash = 0;
  1005. else if (ch->next == NULL) {
  1006. hash++;
  1007. *pos += 1LL<<32;
  1008. } else {
  1009. ++*pos;
  1010. return ch->next;
  1011. }
  1012. *pos &= ~((1LL<<32) - 1);
  1013. while (hash < cd->hash_size &&
  1014. cd->hash_table[hash] == NULL) {
  1015. hash++;
  1016. *pos += 1LL<<32;
  1017. }
  1018. if (hash >= cd->hash_size)
  1019. return NULL;
  1020. ++*pos;
  1021. return cd->hash_table[hash];
  1022. }
  1023. static void c_stop(struct seq_file *m, void *p)
  1024. {
  1025. struct cache_detail *cd = ((struct handle*)m->private)->cd;
  1026. read_unlock(&cd->hash_lock);
  1027. }
  1028. static int c_show(struct seq_file *m, void *p)
  1029. {
  1030. struct cache_head *cp = p;
  1031. struct cache_detail *cd = ((struct handle*)m->private)->cd;
  1032. if (p == SEQ_START_TOKEN)
  1033. return cd->cache_show(m, cd, NULL);
  1034. ifdebug(CACHE)
  1035. seq_printf(m, "# expiry=%ld refcnt=%d\n",
  1036. cp->expiry_time, atomic_read(&cp->refcnt));
  1037. cache_get(cp);
  1038. if (cache_check(cd, cp, NULL))
  1039. /* cache_check does a cache_put on failure */
  1040. seq_printf(m, "# ");
  1041. else
  1042. cache_put(cp, cd);
  1043. return cd->cache_show(m, cd, cp);
  1044. }
  1045. static struct seq_operations cache_content_op = {
  1046. .start = c_start,
  1047. .next = c_next,
  1048. .stop = c_stop,
  1049. .show = c_show,
  1050. };
  1051. static int content_open(struct inode *inode, struct file *file)
  1052. {
  1053. int res;
  1054. struct handle *han;
  1055. struct cache_detail *cd = PDE(inode)->data;
  1056. han = kmalloc(sizeof(*han), GFP_KERNEL);
  1057. if (han == NULL)
  1058. return -ENOMEM;
  1059. han->cd = cd;
  1060. res = seq_open(file, &cache_content_op);
  1061. if (res)
  1062. kfree(han);
  1063. else
  1064. ((struct seq_file *)file->private_data)->private = han;
  1065. return res;
  1066. }
  1067. static int content_release(struct inode *inode, struct file *file)
  1068. {
  1069. struct seq_file *m = (struct seq_file *)file->private_data;
  1070. struct handle *han = m->private;
  1071. kfree(han);
  1072. m->private = NULL;
  1073. return seq_release(inode, file);
  1074. }
  1075. static struct file_operations content_file_operations = {
  1076. .open = content_open,
  1077. .read = seq_read,
  1078. .llseek = seq_lseek,
  1079. .release = content_release,
  1080. };
  1081. static ssize_t read_flush(struct file *file, char __user *buf,
  1082. size_t count, loff_t *ppos)
  1083. {
  1084. struct cache_detail *cd = PDE(file->f_dentry->d_inode)->data;
  1085. char tbuf[20];
  1086. unsigned long p = *ppos;
  1087. int len;
  1088. sprintf(tbuf, "%lu\n", cd->flush_time);
  1089. len = strlen(tbuf);
  1090. if (p >= len)
  1091. return 0;
  1092. len -= p;
  1093. if (len > count) len = count;
  1094. if (copy_to_user(buf, (void*)(tbuf+p), len))
  1095. len = -EFAULT;
  1096. else
  1097. *ppos += len;
  1098. return len;
  1099. }
  1100. static ssize_t write_flush(struct file * file, const char __user * buf,
  1101. size_t count, loff_t *ppos)
  1102. {
  1103. struct cache_detail *cd = PDE(file->f_dentry->d_inode)->data;
  1104. char tbuf[20];
  1105. char *ep;
  1106. long flushtime;
  1107. if (*ppos || count > sizeof(tbuf)-1)
  1108. return -EINVAL;
  1109. if (copy_from_user(tbuf, buf, count))
  1110. return -EFAULT;
  1111. tbuf[count] = 0;
  1112. flushtime = simple_strtoul(tbuf, &ep, 0);
  1113. if (*ep && *ep != '\n')
  1114. return -EINVAL;
  1115. cd->flush_time = flushtime;
  1116. cd->nextcheck = get_seconds();
  1117. cache_flush();
  1118. *ppos += count;
  1119. return count;
  1120. }
  1121. static struct file_operations cache_flush_operations = {
  1122. .open = nonseekable_open,
  1123. .read = read_flush,
  1124. .write = write_flush,
  1125. };