cache.c 30 KB

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