cache.c 31 KB

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