cache.c 27 KB

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