cache.c 30 KB

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