cache.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  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. if (rp->offset)
  507. BUG();
  508. return 0;
  509. }
  510. rq = container_of(rp->q.list.next, struct cache_request, q.list);
  511. if (rq->q.reader) BUG();
  512. if (rp->offset == 0)
  513. rq->readers++;
  514. spin_unlock(&queue_lock);
  515. if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
  516. err = -EAGAIN;
  517. spin_lock(&queue_lock);
  518. list_move(&rp->q.list, &rq->q.list);
  519. spin_unlock(&queue_lock);
  520. } else {
  521. if (rp->offset + count > rq->len)
  522. count = rq->len - rp->offset;
  523. err = -EFAULT;
  524. if (copy_to_user(buf, rq->buf + rp->offset, count))
  525. goto out;
  526. rp->offset += count;
  527. if (rp->offset >= rq->len) {
  528. rp->offset = 0;
  529. spin_lock(&queue_lock);
  530. list_move(&rp->q.list, &rq->q.list);
  531. spin_unlock(&queue_lock);
  532. }
  533. err = 0;
  534. }
  535. out:
  536. if (rp->offset == 0) {
  537. /* need to release rq */
  538. spin_lock(&queue_lock);
  539. rq->readers--;
  540. if (rq->readers == 0 &&
  541. !test_bit(CACHE_PENDING, &rq->item->flags)) {
  542. list_del(&rq->q.list);
  543. spin_unlock(&queue_lock);
  544. cd->cache_put(rq->item, cd);
  545. kfree(rq->buf);
  546. kfree(rq);
  547. } else
  548. spin_unlock(&queue_lock);
  549. }
  550. if (err == -EAGAIN)
  551. goto again;
  552. up(&queue_io_sem);
  553. return err ? err : count;
  554. }
  555. static char write_buf[8192]; /* protected by queue_io_sem */
  556. static ssize_t
  557. cache_write(struct file *filp, const char __user *buf, size_t count,
  558. loff_t *ppos)
  559. {
  560. int err;
  561. struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
  562. if (count == 0)
  563. return 0;
  564. if (count >= sizeof(write_buf))
  565. return -EINVAL;
  566. down(&queue_io_sem);
  567. if (copy_from_user(write_buf, buf, count)) {
  568. up(&queue_io_sem);
  569. return -EFAULT;
  570. }
  571. write_buf[count] = '\0';
  572. if (cd->cache_parse)
  573. err = cd->cache_parse(cd, write_buf, count);
  574. else
  575. err = -EINVAL;
  576. up(&queue_io_sem);
  577. return err ? err : count;
  578. }
  579. static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
  580. static unsigned int
  581. cache_poll(struct file *filp, poll_table *wait)
  582. {
  583. unsigned int mask;
  584. struct cache_reader *rp = filp->private_data;
  585. struct cache_queue *cq;
  586. struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
  587. poll_wait(filp, &queue_wait, wait);
  588. /* alway allow write */
  589. mask = POLL_OUT | POLLWRNORM;
  590. if (!rp)
  591. return mask;
  592. spin_lock(&queue_lock);
  593. for (cq= &rp->q; &cq->list != &cd->queue;
  594. cq = list_entry(cq->list.next, struct cache_queue, list))
  595. if (!cq->reader) {
  596. mask |= POLLIN | POLLRDNORM;
  597. break;
  598. }
  599. spin_unlock(&queue_lock);
  600. return mask;
  601. }
  602. static int
  603. cache_ioctl(struct inode *ino, struct file *filp,
  604. unsigned int cmd, unsigned long arg)
  605. {
  606. int len = 0;
  607. struct cache_reader *rp = filp->private_data;
  608. struct cache_queue *cq;
  609. struct cache_detail *cd = PDE(ino)->data;
  610. if (cmd != FIONREAD || !rp)
  611. return -EINVAL;
  612. spin_lock(&queue_lock);
  613. /* only find the length remaining in current request,
  614. * or the length of the next request
  615. */
  616. for (cq= &rp->q; &cq->list != &cd->queue;
  617. cq = list_entry(cq->list.next, struct cache_queue, list))
  618. if (!cq->reader) {
  619. struct cache_request *cr =
  620. container_of(cq, struct cache_request, q);
  621. len = cr->len - rp->offset;
  622. break;
  623. }
  624. spin_unlock(&queue_lock);
  625. return put_user(len, (int __user *)arg);
  626. }
  627. static int
  628. cache_open(struct inode *inode, struct file *filp)
  629. {
  630. struct cache_reader *rp = NULL;
  631. nonseekable_open(inode, filp);
  632. if (filp->f_mode & FMODE_READ) {
  633. struct cache_detail *cd = PDE(inode)->data;
  634. rp = kmalloc(sizeof(*rp), GFP_KERNEL);
  635. if (!rp)
  636. return -ENOMEM;
  637. rp->offset = 0;
  638. rp->q.reader = 1;
  639. atomic_inc(&cd->readers);
  640. spin_lock(&queue_lock);
  641. list_add(&rp->q.list, &cd->queue);
  642. spin_unlock(&queue_lock);
  643. }
  644. filp->private_data = rp;
  645. return 0;
  646. }
  647. static int
  648. cache_release(struct inode *inode, struct file *filp)
  649. {
  650. struct cache_reader *rp = filp->private_data;
  651. struct cache_detail *cd = PDE(inode)->data;
  652. if (rp) {
  653. spin_lock(&queue_lock);
  654. if (rp->offset) {
  655. struct cache_queue *cq;
  656. for (cq= &rp->q; &cq->list != &cd->queue;
  657. cq = list_entry(cq->list.next, struct cache_queue, list))
  658. if (!cq->reader) {
  659. container_of(cq, struct cache_request, q)
  660. ->readers--;
  661. break;
  662. }
  663. rp->offset = 0;
  664. }
  665. list_del(&rp->q.list);
  666. spin_unlock(&queue_lock);
  667. filp->private_data = NULL;
  668. kfree(rp);
  669. cd->last_close = get_seconds();
  670. atomic_dec(&cd->readers);
  671. }
  672. return 0;
  673. }
  674. static struct file_operations cache_file_operations = {
  675. .owner = THIS_MODULE,
  676. .llseek = no_llseek,
  677. .read = cache_read,
  678. .write = cache_write,
  679. .poll = cache_poll,
  680. .ioctl = cache_ioctl, /* for FIONREAD */
  681. .open = cache_open,
  682. .release = cache_release,
  683. };
  684. static void queue_loose(struct cache_detail *detail, struct cache_head *ch)
  685. {
  686. struct cache_queue *cq;
  687. spin_lock(&queue_lock);
  688. list_for_each_entry(cq, &detail->queue, list)
  689. if (!cq->reader) {
  690. struct cache_request *cr = container_of(cq, struct cache_request, q);
  691. if (cr->item != ch)
  692. continue;
  693. if (cr->readers != 0)
  694. break;
  695. list_del(&cr->q.list);
  696. spin_unlock(&queue_lock);
  697. detail->cache_put(cr->item, detail);
  698. kfree(cr->buf);
  699. kfree(cr);
  700. return;
  701. }
  702. spin_unlock(&queue_lock);
  703. }
  704. /*
  705. * Support routines for text-based upcalls.
  706. * Fields are separated by spaces.
  707. * Fields are either mangled to quote space tab newline slosh with slosh
  708. * or a hexified with a leading \x
  709. * Record is terminated with newline.
  710. *
  711. */
  712. void qword_add(char **bpp, int *lp, char *str)
  713. {
  714. char *bp = *bpp;
  715. int len = *lp;
  716. char c;
  717. if (len < 0) return;
  718. while ((c=*str++) && len)
  719. switch(c) {
  720. case ' ':
  721. case '\t':
  722. case '\n':
  723. case '\\':
  724. if (len >= 4) {
  725. *bp++ = '\\';
  726. *bp++ = '0' + ((c & 0300)>>6);
  727. *bp++ = '0' + ((c & 0070)>>3);
  728. *bp++ = '0' + ((c & 0007)>>0);
  729. }
  730. len -= 4;
  731. break;
  732. default:
  733. *bp++ = c;
  734. len--;
  735. }
  736. if (c || len <1) len = -1;
  737. else {
  738. *bp++ = ' ';
  739. len--;
  740. }
  741. *bpp = bp;
  742. *lp = len;
  743. }
  744. void qword_addhex(char **bpp, int *lp, char *buf, int blen)
  745. {
  746. char *bp = *bpp;
  747. int len = *lp;
  748. if (len < 0) return;
  749. if (len > 2) {
  750. *bp++ = '\\';
  751. *bp++ = 'x';
  752. len -= 2;
  753. while (blen && len >= 2) {
  754. unsigned char c = *buf++;
  755. *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
  756. *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
  757. len -= 2;
  758. blen--;
  759. }
  760. }
  761. if (blen || len<1) len = -1;
  762. else {
  763. *bp++ = ' ';
  764. len--;
  765. }
  766. *bpp = bp;
  767. *lp = len;
  768. }
  769. static void warn_no_listener(struct cache_detail *detail)
  770. {
  771. if (detail->last_warn != detail->last_close) {
  772. detail->last_warn = detail->last_close;
  773. if (detail->warn_no_listener)
  774. detail->warn_no_listener(detail);
  775. }
  776. }
  777. /*
  778. * register an upcall request to user-space.
  779. * Each request is at most one page long.
  780. */
  781. static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h)
  782. {
  783. char *buf;
  784. struct cache_request *crq;
  785. char *bp;
  786. int len;
  787. if (detail->cache_request == NULL)
  788. return -EINVAL;
  789. if (atomic_read(&detail->readers) == 0 &&
  790. detail->last_close < get_seconds() - 30) {
  791. warn_no_listener(detail);
  792. return -EINVAL;
  793. }
  794. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  795. if (!buf)
  796. return -EAGAIN;
  797. crq = kmalloc(sizeof (*crq), GFP_KERNEL);
  798. if (!crq) {
  799. kfree(buf);
  800. return -EAGAIN;
  801. }
  802. bp = buf; len = PAGE_SIZE;
  803. detail->cache_request(detail, h, &bp, &len);
  804. if (len < 0) {
  805. kfree(buf);
  806. kfree(crq);
  807. return -EAGAIN;
  808. }
  809. crq->q.reader = 0;
  810. crq->item = cache_get(h);
  811. crq->buf = buf;
  812. crq->len = PAGE_SIZE - len;
  813. crq->readers = 0;
  814. spin_lock(&queue_lock);
  815. list_add_tail(&crq->q.list, &detail->queue);
  816. spin_unlock(&queue_lock);
  817. wake_up(&queue_wait);
  818. return 0;
  819. }
  820. /*
  821. * parse a message from user-space and pass it
  822. * to an appropriate cache
  823. * Messages are, like requests, separated into fields by
  824. * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
  825. *
  826. * Message is
  827. * reply cachename expiry key ... content....
  828. *
  829. * key and content are both parsed by cache
  830. */
  831. #define isodigit(c) (isdigit(c) && c <= '7')
  832. int qword_get(char **bpp, char *dest, int bufsize)
  833. {
  834. /* return bytes copied, or -1 on error */
  835. char *bp = *bpp;
  836. int len = 0;
  837. while (*bp == ' ') bp++;
  838. if (bp[0] == '\\' && bp[1] == 'x') {
  839. /* HEX STRING */
  840. bp += 2;
  841. while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
  842. int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
  843. bp++;
  844. byte <<= 4;
  845. byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
  846. *dest++ = byte;
  847. bp++;
  848. len++;
  849. }
  850. } else {
  851. /* text with \nnn octal quoting */
  852. while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
  853. if (*bp == '\\' &&
  854. isodigit(bp[1]) && (bp[1] <= '3') &&
  855. isodigit(bp[2]) &&
  856. isodigit(bp[3])) {
  857. int byte = (*++bp -'0');
  858. bp++;
  859. byte = (byte << 3) | (*bp++ - '0');
  860. byte = (byte << 3) | (*bp++ - '0');
  861. *dest++ = byte;
  862. len++;
  863. } else {
  864. *dest++ = *bp++;
  865. len++;
  866. }
  867. }
  868. }
  869. if (*bp != ' ' && *bp != '\n' && *bp != '\0')
  870. return -1;
  871. while (*bp == ' ') bp++;
  872. *bpp = bp;
  873. *dest = '\0';
  874. return len;
  875. }
  876. /*
  877. * support /proc/sunrpc/cache/$CACHENAME/content
  878. * as a seqfile.
  879. * We call ->cache_show passing NULL for the item to
  880. * get a header, then pass each real item in the cache
  881. */
  882. struct handle {
  883. struct cache_detail *cd;
  884. };
  885. static void *c_start(struct seq_file *m, loff_t *pos)
  886. {
  887. loff_t n = *pos;
  888. unsigned hash, entry;
  889. struct cache_head *ch;
  890. struct cache_detail *cd = ((struct handle*)m->private)->cd;
  891. read_lock(&cd->hash_lock);
  892. if (!n--)
  893. return SEQ_START_TOKEN;
  894. hash = n >> 32;
  895. entry = n & ((1LL<<32) - 1);
  896. for (ch=cd->hash_table[hash]; ch; ch=ch->next)
  897. if (!entry--)
  898. return ch;
  899. n &= ~((1LL<<32) - 1);
  900. do {
  901. hash++;
  902. n += 1LL<<32;
  903. } while(hash < cd->hash_size &&
  904. cd->hash_table[hash]==NULL);
  905. if (hash >= cd->hash_size)
  906. return NULL;
  907. *pos = n+1;
  908. return cd->hash_table[hash];
  909. }
  910. static void *c_next(struct seq_file *m, void *p, loff_t *pos)
  911. {
  912. struct cache_head *ch = p;
  913. int hash = (*pos >> 32);
  914. struct cache_detail *cd = ((struct handle*)m->private)->cd;
  915. if (p == SEQ_START_TOKEN)
  916. hash = 0;
  917. else if (ch->next == NULL) {
  918. hash++;
  919. *pos += 1LL<<32;
  920. } else {
  921. ++*pos;
  922. return ch->next;
  923. }
  924. *pos &= ~((1LL<<32) - 1);
  925. while (hash < cd->hash_size &&
  926. cd->hash_table[hash] == NULL) {
  927. hash++;
  928. *pos += 1LL<<32;
  929. }
  930. if (hash >= cd->hash_size)
  931. return NULL;
  932. ++*pos;
  933. return cd->hash_table[hash];
  934. }
  935. static void c_stop(struct seq_file *m, void *p)
  936. {
  937. struct cache_detail *cd = ((struct handle*)m->private)->cd;
  938. read_unlock(&cd->hash_lock);
  939. }
  940. static int c_show(struct seq_file *m, void *p)
  941. {
  942. struct cache_head *cp = p;
  943. struct cache_detail *cd = ((struct handle*)m->private)->cd;
  944. if (p == SEQ_START_TOKEN)
  945. return cd->cache_show(m, cd, NULL);
  946. ifdebug(CACHE)
  947. seq_printf(m, "# expiry=%ld refcnt=%d\n",
  948. cp->expiry_time, atomic_read(&cp->refcnt));
  949. cache_get(cp);
  950. if (cache_check(cd, cp, NULL))
  951. /* cache_check does a cache_put on failure */
  952. seq_printf(m, "# ");
  953. else
  954. cache_put(cp, cd);
  955. return cd->cache_show(m, cd, cp);
  956. }
  957. static struct seq_operations cache_content_op = {
  958. .start = c_start,
  959. .next = c_next,
  960. .stop = c_stop,
  961. .show = c_show,
  962. };
  963. static int content_open(struct inode *inode, struct file *file)
  964. {
  965. int res;
  966. struct handle *han;
  967. struct cache_detail *cd = PDE(inode)->data;
  968. han = kmalloc(sizeof(*han), GFP_KERNEL);
  969. if (han == NULL)
  970. return -ENOMEM;
  971. han->cd = cd;
  972. res = seq_open(file, &cache_content_op);
  973. if (res)
  974. kfree(han);
  975. else
  976. ((struct seq_file *)file->private_data)->private = han;
  977. return res;
  978. }
  979. static int content_release(struct inode *inode, struct file *file)
  980. {
  981. struct seq_file *m = (struct seq_file *)file->private_data;
  982. struct handle *han = m->private;
  983. kfree(han);
  984. m->private = NULL;
  985. return seq_release(inode, file);
  986. }
  987. static struct file_operations content_file_operations = {
  988. .open = content_open,
  989. .read = seq_read,
  990. .llseek = seq_lseek,
  991. .release = content_release,
  992. };
  993. static ssize_t read_flush(struct file *file, char __user *buf,
  994. size_t count, loff_t *ppos)
  995. {
  996. struct cache_detail *cd = PDE(file->f_dentry->d_inode)->data;
  997. char tbuf[20];
  998. unsigned long p = *ppos;
  999. int len;
  1000. sprintf(tbuf, "%lu\n", cd->flush_time);
  1001. len = strlen(tbuf);
  1002. if (p >= len)
  1003. return 0;
  1004. len -= p;
  1005. if (len > count) len = count;
  1006. if (copy_to_user(buf, (void*)(tbuf+p), len))
  1007. len = -EFAULT;
  1008. else
  1009. *ppos += len;
  1010. return len;
  1011. }
  1012. static ssize_t write_flush(struct file * file, const char __user * buf,
  1013. size_t count, loff_t *ppos)
  1014. {
  1015. struct cache_detail *cd = PDE(file->f_dentry->d_inode)->data;
  1016. char tbuf[20];
  1017. char *ep;
  1018. long flushtime;
  1019. if (*ppos || count > sizeof(tbuf)-1)
  1020. return -EINVAL;
  1021. if (copy_from_user(tbuf, buf, count))
  1022. return -EFAULT;
  1023. tbuf[count] = 0;
  1024. flushtime = simple_strtoul(tbuf, &ep, 0);
  1025. if (*ep && *ep != '\n')
  1026. return -EINVAL;
  1027. cd->flush_time = flushtime;
  1028. cd->nextcheck = get_seconds();
  1029. cache_flush();
  1030. *ppos += count;
  1031. return count;
  1032. }
  1033. static struct file_operations cache_flush_operations = {
  1034. .open = nonseekable_open,
  1035. .read = read_flush,
  1036. .write = write_flush,
  1037. };