cache.c 30 KB

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