cache.c 31 KB

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