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