cache.c 30 KB

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