cache.c 30 KB

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