cache.c 31 KB

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