cache.c 30 KB

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