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 <linux/mutex.h>
  29. #include <asm/ioctls.h>
  30. #include <linux/sunrpc/types.h>
  31. #include <linux/sunrpc/cache.h>
  32. #include <linux/sunrpc/stats.h>
  33. #define RPCDBG_FACILITY RPCDBG_CACHE
  34. static void cache_defer_req(struct cache_req *req, struct cache_head *item);
  35. static void cache_revisit_request(struct cache_head *item);
  36. void cache_init(struct cache_head *h)
  37. {
  38. time_t now = get_seconds();
  39. h->next = NULL;
  40. h->flags = 0;
  41. atomic_set(&h->refcnt, 1);
  42. h->expiry_time = now + CACHE_NEW_EXPIRY;
  43. h->last_refresh = now;
  44. }
  45. static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h);
  46. /*
  47. * This is the generic cache management routine for all
  48. * the authentication caches.
  49. * It checks the currency of a cache item and will (later)
  50. * initiate an upcall to fill it if needed.
  51. *
  52. *
  53. * Returns 0 if the cache_head can be used, or cache_puts it and returns
  54. * -EAGAIN if upcall is pending,
  55. * -ENOENT if cache entry was negative
  56. */
  57. int cache_check(struct cache_detail *detail,
  58. struct cache_head *h, struct cache_req *rqstp)
  59. {
  60. int rv;
  61. long refresh_age, age;
  62. /* First decide return status as best we can */
  63. if (!test_bit(CACHE_VALID, &h->flags) ||
  64. h->expiry_time < get_seconds())
  65. rv = -EAGAIN;
  66. else if (detail->flush_time > h->last_refresh)
  67. rv = -EAGAIN;
  68. else {
  69. /* entry is valid */
  70. if (test_bit(CACHE_NEGATIVE, &h->flags))
  71. rv = -ENOENT;
  72. else rv = 0;
  73. }
  74. /* now see if we want to start an upcall */
  75. refresh_age = (h->expiry_time - h->last_refresh);
  76. age = get_seconds() - h->last_refresh;
  77. if (rqstp == NULL) {
  78. if (rv == -EAGAIN)
  79. rv = -ENOENT;
  80. } else if (rv == -EAGAIN || age > refresh_age/2) {
  81. dprintk("Want update, refage=%ld, age=%ld\n", refresh_age, age);
  82. if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
  83. switch (cache_make_upcall(detail, h)) {
  84. case -EINVAL:
  85. clear_bit(CACHE_PENDING, &h->flags);
  86. if (rv == -EAGAIN) {
  87. set_bit(CACHE_NEGATIVE, &h->flags);
  88. cache_fresh(detail, h, get_seconds()+CACHE_NEW_EXPIRY);
  89. rv = -ENOENT;
  90. }
  91. break;
  92. case -EAGAIN:
  93. clear_bit(CACHE_PENDING, &h->flags);
  94. cache_revisit_request(h);
  95. break;
  96. }
  97. }
  98. }
  99. if (rv == -EAGAIN)
  100. cache_defer_req(rqstp, h);
  101. if (rv && h)
  102. detail->cache_put(h, detail);
  103. return rv;
  104. }
  105. static void queue_loose(struct cache_detail *detail, struct cache_head *ch);
  106. void cache_fresh(struct cache_detail *detail,
  107. struct cache_head *head, time_t expiry)
  108. {
  109. head->expiry_time = expiry;
  110. head->last_refresh = get_seconds();
  111. if (!test_and_set_bit(CACHE_VALID, &head->flags))
  112. cache_revisit_request(head);
  113. if (test_and_clear_bit(CACHE_PENDING, &head->flags))
  114. queue_loose(detail, head);
  115. }
  116. /*
  117. * caches need to be periodically cleaned.
  118. * For this we maintain a list of cache_detail and
  119. * a current pointer into that list and into the table
  120. * for that entry.
  121. *
  122. * Each time clean_cache is called it finds the next non-empty entry
  123. * in the current table and walks the list in that entry
  124. * looking for entries that can be removed.
  125. *
  126. * An entry gets removed if:
  127. * - The expiry is before current time
  128. * - The last_refresh time is before the flush_time for that cache
  129. *
  130. * later we might drop old entries with non-NEVER expiry if that table
  131. * is getting 'full' for some definition of 'full'
  132. *
  133. * The question of "how often to scan a table" is an interesting one
  134. * and is answered in part by the use of the "nextcheck" field in the
  135. * cache_detail.
  136. * When a scan of a table begins, the nextcheck field is set to a time
  137. * that is well into the future.
  138. * While scanning, if an expiry time is found that is earlier than the
  139. * current nextcheck time, nextcheck is set to that expiry time.
  140. * If the flush_time is ever set to a time earlier than the nextcheck
  141. * time, the nextcheck time is then set to that flush_time.
  142. *
  143. * A table is then only scanned if the current time is at least
  144. * the nextcheck time.
  145. *
  146. */
  147. static LIST_HEAD(cache_list);
  148. static DEFINE_SPINLOCK(cache_list_lock);
  149. static struct cache_detail *current_detail;
  150. static int current_index;
  151. static struct file_operations cache_file_operations;
  152. static struct file_operations content_file_operations;
  153. static struct file_operations cache_flush_operations;
  154. static void do_cache_clean(void *data);
  155. static DECLARE_WORK(cache_cleaner, do_cache_clean, NULL);
  156. void cache_register(struct cache_detail *cd)
  157. {
  158. cd->proc_ent = proc_mkdir(cd->name, proc_net_rpc);
  159. if (cd->proc_ent) {
  160. struct proc_dir_entry *p;
  161. cd->proc_ent->owner = cd->owner;
  162. cd->channel_ent = cd->content_ent = NULL;
  163. p = create_proc_entry("flush", S_IFREG|S_IRUSR|S_IWUSR,
  164. cd->proc_ent);
  165. cd->flush_ent = p;
  166. if (p) {
  167. p->proc_fops = &cache_flush_operations;
  168. p->owner = cd->owner;
  169. p->data = cd;
  170. }
  171. if (cd->cache_request || cd->cache_parse) {
  172. p = create_proc_entry("channel", S_IFREG|S_IRUSR|S_IWUSR,
  173. cd->proc_ent);
  174. cd->channel_ent = p;
  175. if (p) {
  176. p->proc_fops = &cache_file_operations;
  177. p->owner = cd->owner;
  178. p->data = cd;
  179. }
  180. }
  181. if (cd->cache_show) {
  182. p = create_proc_entry("content", S_IFREG|S_IRUSR|S_IWUSR,
  183. cd->proc_ent);
  184. cd->content_ent = p;
  185. if (p) {
  186. p->proc_fops = &content_file_operations;
  187. p->owner = cd->owner;
  188. p->data = cd;
  189. }
  190. }
  191. }
  192. rwlock_init(&cd->hash_lock);
  193. INIT_LIST_HEAD(&cd->queue);
  194. spin_lock(&cache_list_lock);
  195. cd->nextcheck = 0;
  196. cd->entries = 0;
  197. atomic_set(&cd->readers, 0);
  198. cd->last_close = 0;
  199. cd->last_warn = -1;
  200. list_add(&cd->others, &cache_list);
  201. spin_unlock(&cache_list_lock);
  202. /* start the cleaning process */
  203. schedule_work(&cache_cleaner);
  204. }
  205. int cache_unregister(struct cache_detail *cd)
  206. {
  207. cache_purge(cd);
  208. spin_lock(&cache_list_lock);
  209. write_lock(&cd->hash_lock);
  210. if (cd->entries || atomic_read(&cd->inuse)) {
  211. write_unlock(&cd->hash_lock);
  212. spin_unlock(&cache_list_lock);
  213. return -EBUSY;
  214. }
  215. if (current_detail == cd)
  216. current_detail = NULL;
  217. list_del_init(&cd->others);
  218. write_unlock(&cd->hash_lock);
  219. spin_unlock(&cache_list_lock);
  220. if (cd->proc_ent) {
  221. if (cd->flush_ent)
  222. remove_proc_entry("flush", cd->proc_ent);
  223. if (cd->channel_ent)
  224. remove_proc_entry("channel", cd->proc_ent);
  225. if (cd->content_ent)
  226. remove_proc_entry("content", cd->proc_ent);
  227. cd->proc_ent = NULL;
  228. remove_proc_entry(cd->name, proc_net_rpc);
  229. }
  230. if (list_empty(&cache_list)) {
  231. /* module must be being unloaded so its safe to kill the worker */
  232. cancel_delayed_work(&cache_cleaner);
  233. flush_scheduled_work();
  234. }
  235. return 0;
  236. }
  237. /* clean cache tries to find something to clean
  238. * and cleans it.
  239. * It returns 1 if it cleaned something,
  240. * 0 if it didn't find anything this time
  241. * -1 if it fell off the end of the list.
  242. */
  243. static int cache_clean(void)
  244. {
  245. int rv = 0;
  246. struct list_head *next;
  247. spin_lock(&cache_list_lock);
  248. /* find a suitable table if we don't already have one */
  249. while (current_detail == NULL ||
  250. current_index >= current_detail->hash_size) {
  251. if (current_detail)
  252. next = current_detail->others.next;
  253. else
  254. next = cache_list.next;
  255. if (next == &cache_list) {
  256. current_detail = NULL;
  257. spin_unlock(&cache_list_lock);
  258. return -1;
  259. }
  260. current_detail = list_entry(next, struct cache_detail, others);
  261. if (current_detail->nextcheck > get_seconds())
  262. current_index = current_detail->hash_size;
  263. else {
  264. current_index = 0;
  265. current_detail->nextcheck = get_seconds()+30*60;
  266. }
  267. }
  268. /* find a non-empty bucket in the table */
  269. while (current_detail &&
  270. current_index < current_detail->hash_size &&
  271. current_detail->hash_table[current_index] == NULL)
  272. current_index++;
  273. /* find a cleanable entry in the bucket and clean it, or set to next bucket */
  274. if (current_detail && current_index < current_detail->hash_size) {
  275. struct cache_head *ch, **cp;
  276. struct cache_detail *d;
  277. write_lock(&current_detail->hash_lock);
  278. /* Ok, now to clean this strand */
  279. cp = & current_detail->hash_table[current_index];
  280. ch = *cp;
  281. for (; ch; cp= & ch->next, ch= *cp) {
  282. if (current_detail->nextcheck > ch->expiry_time)
  283. current_detail->nextcheck = ch->expiry_time+1;
  284. if (ch->expiry_time >= get_seconds()
  285. && ch->last_refresh >= current_detail->flush_time
  286. )
  287. continue;
  288. if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
  289. queue_loose(current_detail, ch);
  290. if (atomic_read(&ch->refcnt) == 1)
  291. break;
  292. }
  293. if (ch) {
  294. *cp = ch->next;
  295. ch->next = NULL;
  296. current_detail->entries--;
  297. rv = 1;
  298. }
  299. write_unlock(&current_detail->hash_lock);
  300. d = current_detail;
  301. if (!ch)
  302. current_index ++;
  303. spin_unlock(&cache_list_lock);
  304. if (ch)
  305. d->cache_put(ch, d);
  306. } else
  307. spin_unlock(&cache_list_lock);
  308. return rv;
  309. }
  310. /*
  311. * We want to regularly clean the cache, so we need to schedule some work ...
  312. */
  313. static void do_cache_clean(void *data)
  314. {
  315. int delay = 5;
  316. if (cache_clean() == -1)
  317. delay = 30*HZ;
  318. if (list_empty(&cache_list))
  319. delay = 0;
  320. if (delay)
  321. schedule_delayed_work(&cache_cleaner, delay);
  322. }
  323. /*
  324. * Clean all caches promptly. This just calls cache_clean
  325. * repeatedly until we are sure that every cache has had a chance to
  326. * be fully cleaned
  327. */
  328. void cache_flush(void)
  329. {
  330. while (cache_clean() != -1)
  331. cond_resched();
  332. while (cache_clean() != -1)
  333. cond_resched();
  334. }
  335. void cache_purge(struct cache_detail *detail)
  336. {
  337. detail->flush_time = LONG_MAX;
  338. detail->nextcheck = get_seconds();
  339. cache_flush();
  340. detail->flush_time = 1;
  341. }
  342. /*
  343. * Deferral and Revisiting of Requests.
  344. *
  345. * If a cache lookup finds a pending entry, we
  346. * need to defer the request and revisit it later.
  347. * All deferred requests are stored in a hash table,
  348. * indexed by "struct cache_head *".
  349. * As it may be wasteful to store a whole request
  350. * structure, we allow the request to provide a
  351. * deferred form, which must contain a
  352. * 'struct cache_deferred_req'
  353. * This cache_deferred_req contains a method to allow
  354. * it to be revisited when cache info is available
  355. */
  356. #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
  357. #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
  358. #define DFR_MAX 300 /* ??? */
  359. static DEFINE_SPINLOCK(cache_defer_lock);
  360. static LIST_HEAD(cache_defer_list);
  361. static struct list_head cache_defer_hash[DFR_HASHSIZE];
  362. static int cache_defer_cnt;
  363. static void cache_defer_req(struct cache_req *req, struct cache_head *item)
  364. {
  365. struct cache_deferred_req *dreq;
  366. int hash = DFR_HASH(item);
  367. dreq = req->defer(req);
  368. if (dreq == NULL)
  369. return;
  370. dreq->item = item;
  371. dreq->recv_time = get_seconds();
  372. spin_lock(&cache_defer_lock);
  373. list_add(&dreq->recent, &cache_defer_list);
  374. if (cache_defer_hash[hash].next == NULL)
  375. INIT_LIST_HEAD(&cache_defer_hash[hash]);
  376. list_add(&dreq->hash, &cache_defer_hash[hash]);
  377. /* it is in, now maybe clean up */
  378. dreq = NULL;
  379. if (++cache_defer_cnt > DFR_MAX) {
  380. /* too much in the cache, randomly drop
  381. * first or last
  382. */
  383. if (net_random()&1)
  384. dreq = list_entry(cache_defer_list.next,
  385. struct cache_deferred_req,
  386. recent);
  387. else
  388. dreq = list_entry(cache_defer_list.prev,
  389. struct cache_deferred_req,
  390. recent);
  391. list_del(&dreq->recent);
  392. list_del(&dreq->hash);
  393. cache_defer_cnt--;
  394. }
  395. spin_unlock(&cache_defer_lock);
  396. if (dreq) {
  397. /* there was one too many */
  398. dreq->revisit(dreq, 1);
  399. }
  400. if (test_bit(CACHE_VALID, &item->flags)) {
  401. /* must have just been validated... */
  402. cache_revisit_request(item);
  403. }
  404. }
  405. static void cache_revisit_request(struct cache_head *item)
  406. {
  407. struct cache_deferred_req *dreq;
  408. struct list_head pending;
  409. struct list_head *lp;
  410. int hash = DFR_HASH(item);
  411. INIT_LIST_HEAD(&pending);
  412. spin_lock(&cache_defer_lock);
  413. lp = cache_defer_hash[hash].next;
  414. if (lp) {
  415. while (lp != &cache_defer_hash[hash]) {
  416. dreq = list_entry(lp, struct cache_deferred_req, hash);
  417. lp = lp->next;
  418. if (dreq->item == item) {
  419. list_del(&dreq->hash);
  420. list_move(&dreq->recent, &pending);
  421. cache_defer_cnt--;
  422. }
  423. }
  424. }
  425. spin_unlock(&cache_defer_lock);
  426. while (!list_empty(&pending)) {
  427. dreq = list_entry(pending.next, struct cache_deferred_req, recent);
  428. list_del_init(&dreq->recent);
  429. dreq->revisit(dreq, 0);
  430. }
  431. }
  432. void cache_clean_deferred(void *owner)
  433. {
  434. struct cache_deferred_req *dreq, *tmp;
  435. struct list_head pending;
  436. INIT_LIST_HEAD(&pending);
  437. spin_lock(&cache_defer_lock);
  438. list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
  439. if (dreq->owner == owner) {
  440. list_del(&dreq->hash);
  441. list_move(&dreq->recent, &pending);
  442. cache_defer_cnt--;
  443. }
  444. }
  445. spin_unlock(&cache_defer_lock);
  446. while (!list_empty(&pending)) {
  447. dreq = list_entry(pending.next, struct cache_deferred_req, recent);
  448. list_del_init(&dreq->recent);
  449. dreq->revisit(dreq, 1);
  450. }
  451. }
  452. /*
  453. * communicate with user-space
  454. *
  455. * We have a magic /proc file - /proc/sunrpc/cache
  456. * On read, you get a full request, or block
  457. * On write, an update request is processed
  458. * Poll works if anything to read, and always allows write
  459. *
  460. * Implemented by linked list of requests. Each open file has
  461. * a ->private that also exists in this list. New request are added
  462. * to the end and may wakeup and preceding readers.
  463. * New readers are added to the head. If, on read, an item is found with
  464. * CACHE_UPCALLING clear, we free it from the list.
  465. *
  466. */
  467. static DEFINE_SPINLOCK(queue_lock);
  468. static DEFINE_MUTEX(queue_io_mutex);
  469. struct cache_queue {
  470. struct list_head list;
  471. int reader; /* if 0, then request */
  472. };
  473. struct cache_request {
  474. struct cache_queue q;
  475. struct cache_head *item;
  476. char * buf;
  477. int len;
  478. int readers;
  479. };
  480. struct cache_reader {
  481. struct cache_queue q;
  482. int offset; /* if non-0, we have a refcnt on next request */
  483. };
  484. static ssize_t
  485. cache_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
  486. {
  487. struct cache_reader *rp = filp->private_data;
  488. struct cache_request *rq;
  489. struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
  490. int err;
  491. if (count == 0)
  492. return 0;
  493. mutex_lock(&queue_io_mutex); /* protect against multiple concurrent
  494. * readers on this file */
  495. again:
  496. spin_lock(&queue_lock);
  497. /* need to find next request */
  498. while (rp->q.list.next != &cd->queue &&
  499. list_entry(rp->q.list.next, struct cache_queue, list)
  500. ->reader) {
  501. struct list_head *next = rp->q.list.next;
  502. list_move(&rp->q.list, next);
  503. }
  504. if (rp->q.list.next == &cd->queue) {
  505. spin_unlock(&queue_lock);
  506. mutex_unlock(&queue_io_mutex);
  507. BUG_ON(rp->offset);
  508. return 0;
  509. }
  510. rq = container_of(rp->q.list.next, struct cache_request, q.list);
  511. BUG_ON(rq->q.reader);
  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. mutex_unlock(&queue_io_mutex);
  553. return err ? err : count;
  554. }
  555. static char write_buf[8192]; /* protected by queue_io_mutex */
  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. mutex_lock(&queue_io_mutex);
  567. if (copy_from_user(write_buf, buf, count)) {
  568. mutex_unlock(&queue_io_mutex);
  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. mutex_unlock(&queue_io_mutex);
  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. };