cache.c 31 KB

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