cache.c 30 KB

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