cache.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  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 <linux/string_helpers.h>
  23. #include <asm/uaccess.h>
  24. #include <linux/poll.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/proc_fs.h>
  27. #include <linux/net.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/mutex.h>
  30. #include <linux/pagemap.h>
  31. #include <asm/ioctls.h>
  32. #include <linux/sunrpc/types.h>
  33. #include <linux/sunrpc/cache.h>
  34. #include <linux/sunrpc/stats.h>
  35. #include <linux/sunrpc/rpc_pipe_fs.h>
  36. #include "netns.h"
  37. #define RPCDBG_FACILITY RPCDBG_CACHE
  38. static bool cache_defer_req(struct cache_req *req, struct cache_head *item);
  39. static void cache_revisit_request(struct cache_head *item);
  40. static void cache_init(struct cache_head *h, struct cache_detail *detail)
  41. {
  42. time_t now = seconds_since_boot();
  43. INIT_HLIST_NODE(&h->cache_list);
  44. h->flags = 0;
  45. kref_init(&h->ref);
  46. h->expiry_time = now + CACHE_NEW_EXPIRY;
  47. if (now <= detail->flush_time)
  48. /* ensure it isn't already expired */
  49. now = detail->flush_time + 1;
  50. h->last_refresh = now;
  51. }
  52. static void cache_fresh_locked(struct cache_head *head, time_t expiry,
  53. struct cache_detail *detail);
  54. static void cache_fresh_unlocked(struct cache_head *head,
  55. struct cache_detail *detail);
  56. struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
  57. struct cache_head *key, int hash)
  58. {
  59. struct cache_head *new = NULL, *freeme = NULL, *tmp = NULL;
  60. struct hlist_head *head;
  61. head = &detail->hash_table[hash];
  62. read_lock(&detail->hash_lock);
  63. hlist_for_each_entry(tmp, head, cache_list) {
  64. if (detail->match(tmp, key)) {
  65. if (cache_is_expired(detail, tmp))
  66. /* This entry is expired, we will discard it. */
  67. break;
  68. cache_get(tmp);
  69. read_unlock(&detail->hash_lock);
  70. return tmp;
  71. }
  72. }
  73. read_unlock(&detail->hash_lock);
  74. /* Didn't find anything, insert an empty entry */
  75. new = detail->alloc();
  76. if (!new)
  77. return NULL;
  78. /* must fully initialise 'new', else
  79. * we might get lose if we need to
  80. * cache_put it soon.
  81. */
  82. cache_init(new, detail);
  83. detail->init(new, key);
  84. write_lock(&detail->hash_lock);
  85. /* check if entry appeared while we slept */
  86. hlist_for_each_entry(tmp, head, cache_list) {
  87. if (detail->match(tmp, key)) {
  88. if (cache_is_expired(detail, tmp)) {
  89. hlist_del_init(&tmp->cache_list);
  90. detail->entries --;
  91. cache_fresh_locked(tmp, 0, detail);
  92. freeme = tmp;
  93. break;
  94. }
  95. cache_get(tmp);
  96. write_unlock(&detail->hash_lock);
  97. cache_put(new, detail);
  98. return tmp;
  99. }
  100. }
  101. hlist_add_head(&new->cache_list, head);
  102. detail->entries++;
  103. cache_get(new);
  104. write_unlock(&detail->hash_lock);
  105. if (freeme) {
  106. cache_fresh_unlocked(freeme, detail);
  107. cache_put(freeme, detail);
  108. }
  109. return new;
  110. }
  111. EXPORT_SYMBOL_GPL(sunrpc_cache_lookup);
  112. static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
  113. static void cache_fresh_locked(struct cache_head *head, time_t expiry,
  114. struct cache_detail *detail)
  115. {
  116. time_t now = seconds_since_boot();
  117. if (now <= detail->flush_time)
  118. /* ensure it isn't immediately treated as expired */
  119. now = detail->flush_time + 1;
  120. head->expiry_time = expiry;
  121. head->last_refresh = now;
  122. smp_wmb(); /* paired with smp_rmb() in cache_is_valid() */
  123. set_bit(CACHE_VALID, &head->flags);
  124. }
  125. static void cache_fresh_unlocked(struct cache_head *head,
  126. struct cache_detail *detail)
  127. {
  128. if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
  129. cache_revisit_request(head);
  130. cache_dequeue(detail, head);
  131. }
  132. }
  133. struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
  134. struct cache_head *new, struct cache_head *old, int hash)
  135. {
  136. /* The 'old' entry is to be replaced by 'new'.
  137. * If 'old' is not VALID, we update it directly,
  138. * otherwise we need to replace it
  139. */
  140. struct cache_head *tmp;
  141. if (!test_bit(CACHE_VALID, &old->flags)) {
  142. write_lock(&detail->hash_lock);
  143. if (!test_bit(CACHE_VALID, &old->flags)) {
  144. if (test_bit(CACHE_NEGATIVE, &new->flags))
  145. set_bit(CACHE_NEGATIVE, &old->flags);
  146. else
  147. detail->update(old, new);
  148. cache_fresh_locked(old, new->expiry_time, detail);
  149. write_unlock(&detail->hash_lock);
  150. cache_fresh_unlocked(old, detail);
  151. return old;
  152. }
  153. write_unlock(&detail->hash_lock);
  154. }
  155. /* We need to insert a new entry */
  156. tmp = detail->alloc();
  157. if (!tmp) {
  158. cache_put(old, detail);
  159. return NULL;
  160. }
  161. cache_init(tmp, detail);
  162. detail->init(tmp, old);
  163. write_lock(&detail->hash_lock);
  164. if (test_bit(CACHE_NEGATIVE, &new->flags))
  165. set_bit(CACHE_NEGATIVE, &tmp->flags);
  166. else
  167. detail->update(tmp, new);
  168. hlist_add_head(&tmp->cache_list, &detail->hash_table[hash]);
  169. detail->entries++;
  170. cache_get(tmp);
  171. cache_fresh_locked(tmp, new->expiry_time, detail);
  172. cache_fresh_locked(old, 0, detail);
  173. write_unlock(&detail->hash_lock);
  174. cache_fresh_unlocked(tmp, detail);
  175. cache_fresh_unlocked(old, detail);
  176. cache_put(old, detail);
  177. return tmp;
  178. }
  179. EXPORT_SYMBOL_GPL(sunrpc_cache_update);
  180. static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
  181. {
  182. if (cd->cache_upcall)
  183. return cd->cache_upcall(cd, h);
  184. return sunrpc_cache_pipe_upcall(cd, h);
  185. }
  186. static inline int cache_is_valid(struct cache_head *h)
  187. {
  188. if (!test_bit(CACHE_VALID, &h->flags))
  189. return -EAGAIN;
  190. else {
  191. /* entry is valid */
  192. if (test_bit(CACHE_NEGATIVE, &h->flags))
  193. return -ENOENT;
  194. else {
  195. /*
  196. * In combination with write barrier in
  197. * sunrpc_cache_update, ensures that anyone
  198. * using the cache entry after this sees the
  199. * updated contents:
  200. */
  201. smp_rmb();
  202. return 0;
  203. }
  204. }
  205. }
  206. static int try_to_negate_entry(struct cache_detail *detail, struct cache_head *h)
  207. {
  208. int rv;
  209. write_lock(&detail->hash_lock);
  210. rv = cache_is_valid(h);
  211. if (rv == -EAGAIN) {
  212. set_bit(CACHE_NEGATIVE, &h->flags);
  213. cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY,
  214. detail);
  215. rv = -ENOENT;
  216. }
  217. write_unlock(&detail->hash_lock);
  218. cache_fresh_unlocked(h, detail);
  219. return rv;
  220. }
  221. /*
  222. * This is the generic cache management routine for all
  223. * the authentication caches.
  224. * It checks the currency of a cache item and will (later)
  225. * initiate an upcall to fill it if needed.
  226. *
  227. *
  228. * Returns 0 if the cache_head can be used, or cache_puts it and returns
  229. * -EAGAIN if upcall is pending and request has been queued
  230. * -ETIMEDOUT if upcall failed or request could not be queue or
  231. * upcall completed but item is still invalid (implying that
  232. * the cache item has been replaced with a newer one).
  233. * -ENOENT if cache entry was negative
  234. */
  235. int cache_check(struct cache_detail *detail,
  236. struct cache_head *h, struct cache_req *rqstp)
  237. {
  238. int rv;
  239. long refresh_age, age;
  240. /* First decide return status as best we can */
  241. rv = cache_is_valid(h);
  242. /* now see if we want to start an upcall */
  243. refresh_age = (h->expiry_time - h->last_refresh);
  244. age = seconds_since_boot() - h->last_refresh;
  245. if (rqstp == NULL) {
  246. if (rv == -EAGAIN)
  247. rv = -ENOENT;
  248. } else if (rv == -EAGAIN ||
  249. (h->expiry_time != 0 && age > refresh_age/2)) {
  250. dprintk("RPC: Want update, refage=%ld, age=%ld\n",
  251. refresh_age, age);
  252. if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
  253. switch (cache_make_upcall(detail, h)) {
  254. case -EINVAL:
  255. rv = try_to_negate_entry(detail, h);
  256. break;
  257. case -EAGAIN:
  258. cache_fresh_unlocked(h, detail);
  259. break;
  260. }
  261. }
  262. }
  263. if (rv == -EAGAIN) {
  264. if (!cache_defer_req(rqstp, h)) {
  265. /*
  266. * Request was not deferred; handle it as best
  267. * we can ourselves:
  268. */
  269. rv = cache_is_valid(h);
  270. if (rv == -EAGAIN)
  271. rv = -ETIMEDOUT;
  272. }
  273. }
  274. if (rv)
  275. cache_put(h, detail);
  276. return rv;
  277. }
  278. EXPORT_SYMBOL_GPL(cache_check);
  279. /*
  280. * caches need to be periodically cleaned.
  281. * For this we maintain a list of cache_detail and
  282. * a current pointer into that list and into the table
  283. * for that entry.
  284. *
  285. * Each time cache_clean is called it finds the next non-empty entry
  286. * in the current table and walks the list in that entry
  287. * looking for entries that can be removed.
  288. *
  289. * An entry gets removed if:
  290. * - The expiry is before current time
  291. * - The last_refresh time is before the flush_time for that cache
  292. *
  293. * later we might drop old entries with non-NEVER expiry if that table
  294. * is getting 'full' for some definition of 'full'
  295. *
  296. * The question of "how often to scan a table" is an interesting one
  297. * and is answered in part by the use of the "nextcheck" field in the
  298. * cache_detail.
  299. * When a scan of a table begins, the nextcheck field is set to a time
  300. * that is well into the future.
  301. * While scanning, if an expiry time is found that is earlier than the
  302. * current nextcheck time, nextcheck is set to that expiry time.
  303. * If the flush_time is ever set to a time earlier than the nextcheck
  304. * time, the nextcheck time is then set to that flush_time.
  305. *
  306. * A table is then only scanned if the current time is at least
  307. * the nextcheck time.
  308. *
  309. */
  310. static LIST_HEAD(cache_list);
  311. static DEFINE_SPINLOCK(cache_list_lock);
  312. static struct cache_detail *current_detail;
  313. static int current_index;
  314. static void do_cache_clean(struct work_struct *work);
  315. static struct delayed_work cache_cleaner;
  316. void sunrpc_init_cache_detail(struct cache_detail *cd)
  317. {
  318. rwlock_init(&cd->hash_lock);
  319. INIT_LIST_HEAD(&cd->queue);
  320. spin_lock(&cache_list_lock);
  321. cd->nextcheck = 0;
  322. cd->entries = 0;
  323. atomic_set(&cd->readers, 0);
  324. cd->last_close = 0;
  325. cd->last_warn = -1;
  326. list_add(&cd->others, &cache_list);
  327. spin_unlock(&cache_list_lock);
  328. /* start the cleaning process */
  329. schedule_delayed_work(&cache_cleaner, 0);
  330. }
  331. EXPORT_SYMBOL_GPL(sunrpc_init_cache_detail);
  332. void sunrpc_destroy_cache_detail(struct cache_detail *cd)
  333. {
  334. cache_purge(cd);
  335. spin_lock(&cache_list_lock);
  336. write_lock(&cd->hash_lock);
  337. if (cd->entries || atomic_read(&cd->inuse)) {
  338. write_unlock(&cd->hash_lock);
  339. spin_unlock(&cache_list_lock);
  340. goto out;
  341. }
  342. if (current_detail == cd)
  343. current_detail = NULL;
  344. list_del_init(&cd->others);
  345. write_unlock(&cd->hash_lock);
  346. spin_unlock(&cache_list_lock);
  347. if (list_empty(&cache_list)) {
  348. /* module must be being unloaded so its safe to kill the worker */
  349. cancel_delayed_work_sync(&cache_cleaner);
  350. }
  351. return;
  352. out:
  353. printk(KERN_ERR "RPC: failed to unregister %s cache\n", cd->name);
  354. }
  355. EXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail);
  356. /* clean cache tries to find something to clean
  357. * and cleans it.
  358. * It returns 1 if it cleaned something,
  359. * 0 if it didn't find anything this time
  360. * -1 if it fell off the end of the list.
  361. */
  362. static int cache_clean(void)
  363. {
  364. int rv = 0;
  365. struct list_head *next;
  366. spin_lock(&cache_list_lock);
  367. /* find a suitable table if we don't already have one */
  368. while (current_detail == NULL ||
  369. current_index >= current_detail->hash_size) {
  370. if (current_detail)
  371. next = current_detail->others.next;
  372. else
  373. next = cache_list.next;
  374. if (next == &cache_list) {
  375. current_detail = NULL;
  376. spin_unlock(&cache_list_lock);
  377. return -1;
  378. }
  379. current_detail = list_entry(next, struct cache_detail, others);
  380. if (current_detail->nextcheck > seconds_since_boot())
  381. current_index = current_detail->hash_size;
  382. else {
  383. current_index = 0;
  384. current_detail->nextcheck = seconds_since_boot()+30*60;
  385. }
  386. }
  387. /* find a non-empty bucket in the table */
  388. while (current_detail &&
  389. current_index < current_detail->hash_size &&
  390. hlist_empty(&current_detail->hash_table[current_index]))
  391. current_index++;
  392. /* find a cleanable entry in the bucket and clean it, or set to next bucket */
  393. if (current_detail && current_index < current_detail->hash_size) {
  394. struct cache_head *ch = NULL;
  395. struct cache_detail *d;
  396. struct hlist_head *head;
  397. struct hlist_node *tmp;
  398. write_lock(&current_detail->hash_lock);
  399. /* Ok, now to clean this strand */
  400. head = &current_detail->hash_table[current_index];
  401. hlist_for_each_entry_safe(ch, tmp, head, cache_list) {
  402. if (current_detail->nextcheck > ch->expiry_time)
  403. current_detail->nextcheck = ch->expiry_time+1;
  404. if (!cache_is_expired(current_detail, ch))
  405. continue;
  406. hlist_del_init(&ch->cache_list);
  407. current_detail->entries--;
  408. rv = 1;
  409. break;
  410. }
  411. write_unlock(&current_detail->hash_lock);
  412. d = current_detail;
  413. if (!ch)
  414. current_index ++;
  415. spin_unlock(&cache_list_lock);
  416. if (ch) {
  417. set_bit(CACHE_CLEANED, &ch->flags);
  418. cache_fresh_unlocked(ch, d);
  419. cache_put(ch, d);
  420. }
  421. } else
  422. spin_unlock(&cache_list_lock);
  423. return rv;
  424. }
  425. /*
  426. * We want to regularly clean the cache, so we need to schedule some work ...
  427. */
  428. static void do_cache_clean(struct work_struct *work)
  429. {
  430. int delay = 5;
  431. if (cache_clean() == -1)
  432. delay = round_jiffies_relative(30*HZ);
  433. if (list_empty(&cache_list))
  434. delay = 0;
  435. if (delay)
  436. schedule_delayed_work(&cache_cleaner, delay);
  437. }
  438. /*
  439. * Clean all caches promptly. This just calls cache_clean
  440. * repeatedly until we are sure that every cache has had a chance to
  441. * be fully cleaned
  442. */
  443. void cache_flush(void)
  444. {
  445. while (cache_clean() != -1)
  446. cond_resched();
  447. while (cache_clean() != -1)
  448. cond_resched();
  449. }
  450. EXPORT_SYMBOL_GPL(cache_flush);
  451. void cache_purge(struct cache_detail *detail)
  452. {
  453. time_t now = seconds_since_boot();
  454. if (detail->flush_time >= now)
  455. now = detail->flush_time + 1;
  456. /* 'now' is the maximum value any 'last_refresh' can have */
  457. detail->flush_time = now;
  458. detail->nextcheck = seconds_since_boot();
  459. cache_flush();
  460. }
  461. EXPORT_SYMBOL_GPL(cache_purge);
  462. /*
  463. * Deferral and Revisiting of Requests.
  464. *
  465. * If a cache lookup finds a pending entry, we
  466. * need to defer the request and revisit it later.
  467. * All deferred requests are stored in a hash table,
  468. * indexed by "struct cache_head *".
  469. * As it may be wasteful to store a whole request
  470. * structure, we allow the request to provide a
  471. * deferred form, which must contain a
  472. * 'struct cache_deferred_req'
  473. * This cache_deferred_req contains a method to allow
  474. * it to be revisited when cache info is available
  475. */
  476. #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
  477. #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
  478. #define DFR_MAX 300 /* ??? */
  479. static DEFINE_SPINLOCK(cache_defer_lock);
  480. static LIST_HEAD(cache_defer_list);
  481. static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
  482. static int cache_defer_cnt;
  483. static void __unhash_deferred_req(struct cache_deferred_req *dreq)
  484. {
  485. hlist_del_init(&dreq->hash);
  486. if (!list_empty(&dreq->recent)) {
  487. list_del_init(&dreq->recent);
  488. cache_defer_cnt--;
  489. }
  490. }
  491. static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
  492. {
  493. int hash = DFR_HASH(item);
  494. INIT_LIST_HEAD(&dreq->recent);
  495. hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
  496. }
  497. static void setup_deferral(struct cache_deferred_req *dreq,
  498. struct cache_head *item,
  499. int count_me)
  500. {
  501. dreq->item = item;
  502. spin_lock(&cache_defer_lock);
  503. __hash_deferred_req(dreq, item);
  504. if (count_me) {
  505. cache_defer_cnt++;
  506. list_add(&dreq->recent, &cache_defer_list);
  507. }
  508. spin_unlock(&cache_defer_lock);
  509. }
  510. struct thread_deferred_req {
  511. struct cache_deferred_req handle;
  512. struct completion completion;
  513. };
  514. static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
  515. {
  516. struct thread_deferred_req *dr =
  517. container_of(dreq, struct thread_deferred_req, handle);
  518. complete(&dr->completion);
  519. }
  520. static void cache_wait_req(struct cache_req *req, struct cache_head *item)
  521. {
  522. struct thread_deferred_req sleeper;
  523. struct cache_deferred_req *dreq = &sleeper.handle;
  524. sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
  525. dreq->revisit = cache_restart_thread;
  526. setup_deferral(dreq, item, 0);
  527. if (!test_bit(CACHE_PENDING, &item->flags) ||
  528. wait_for_completion_interruptible_timeout(
  529. &sleeper.completion, req->thread_wait) <= 0) {
  530. /* The completion wasn't completed, so we need
  531. * to clean up
  532. */
  533. spin_lock(&cache_defer_lock);
  534. if (!hlist_unhashed(&sleeper.handle.hash)) {
  535. __unhash_deferred_req(&sleeper.handle);
  536. spin_unlock(&cache_defer_lock);
  537. } else {
  538. /* cache_revisit_request already removed
  539. * this from the hash table, but hasn't
  540. * called ->revisit yet. It will very soon
  541. * and we need to wait for it.
  542. */
  543. spin_unlock(&cache_defer_lock);
  544. wait_for_completion(&sleeper.completion);
  545. }
  546. }
  547. }
  548. static void cache_limit_defers(void)
  549. {
  550. /* Make sure we haven't exceed the limit of allowed deferred
  551. * requests.
  552. */
  553. struct cache_deferred_req *discard = NULL;
  554. if (cache_defer_cnt <= DFR_MAX)
  555. return;
  556. spin_lock(&cache_defer_lock);
  557. /* Consider removing either the first or the last */
  558. if (cache_defer_cnt > DFR_MAX) {
  559. if (prandom_u32() & 1)
  560. discard = list_entry(cache_defer_list.next,
  561. struct cache_deferred_req, recent);
  562. else
  563. discard = list_entry(cache_defer_list.prev,
  564. struct cache_deferred_req, recent);
  565. __unhash_deferred_req(discard);
  566. }
  567. spin_unlock(&cache_defer_lock);
  568. if (discard)
  569. discard->revisit(discard, 1);
  570. }
  571. /* Return true if and only if a deferred request is queued. */
  572. static bool cache_defer_req(struct cache_req *req, struct cache_head *item)
  573. {
  574. struct cache_deferred_req *dreq;
  575. if (req->thread_wait) {
  576. cache_wait_req(req, item);
  577. if (!test_bit(CACHE_PENDING, &item->flags))
  578. return false;
  579. }
  580. dreq = req->defer(req);
  581. if (dreq == NULL)
  582. return false;
  583. setup_deferral(dreq, item, 1);
  584. if (!test_bit(CACHE_PENDING, &item->flags))
  585. /* Bit could have been cleared before we managed to
  586. * set up the deferral, so need to revisit just in case
  587. */
  588. cache_revisit_request(item);
  589. cache_limit_defers();
  590. return true;
  591. }
  592. static void cache_revisit_request(struct cache_head *item)
  593. {
  594. struct cache_deferred_req *dreq;
  595. struct list_head pending;
  596. struct hlist_node *tmp;
  597. int hash = DFR_HASH(item);
  598. INIT_LIST_HEAD(&pending);
  599. spin_lock(&cache_defer_lock);
  600. hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash)
  601. if (dreq->item == item) {
  602. __unhash_deferred_req(dreq);
  603. list_add(&dreq->recent, &pending);
  604. }
  605. spin_unlock(&cache_defer_lock);
  606. while (!list_empty(&pending)) {
  607. dreq = list_entry(pending.next, struct cache_deferred_req, recent);
  608. list_del_init(&dreq->recent);
  609. dreq->revisit(dreq, 0);
  610. }
  611. }
  612. void cache_clean_deferred(void *owner)
  613. {
  614. struct cache_deferred_req *dreq, *tmp;
  615. struct list_head pending;
  616. INIT_LIST_HEAD(&pending);
  617. spin_lock(&cache_defer_lock);
  618. list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
  619. if (dreq->owner == owner) {
  620. __unhash_deferred_req(dreq);
  621. list_add(&dreq->recent, &pending);
  622. }
  623. }
  624. spin_unlock(&cache_defer_lock);
  625. while (!list_empty(&pending)) {
  626. dreq = list_entry(pending.next, struct cache_deferred_req, recent);
  627. list_del_init(&dreq->recent);
  628. dreq->revisit(dreq, 1);
  629. }
  630. }
  631. /*
  632. * communicate with user-space
  633. *
  634. * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
  635. * On read, you get a full request, or block.
  636. * On write, an update request is processed.
  637. * Poll works if anything to read, and always allows write.
  638. *
  639. * Implemented by linked list of requests. Each open file has
  640. * a ->private that also exists in this list. New requests are added
  641. * to the end and may wakeup and preceding readers.
  642. * New readers are added to the head. If, on read, an item is found with
  643. * CACHE_UPCALLING clear, we free it from the list.
  644. *
  645. */
  646. static DEFINE_SPINLOCK(queue_lock);
  647. static DEFINE_MUTEX(queue_io_mutex);
  648. struct cache_queue {
  649. struct list_head list;
  650. int reader; /* if 0, then request */
  651. };
  652. struct cache_request {
  653. struct cache_queue q;
  654. struct cache_head *item;
  655. char * buf;
  656. int len;
  657. int readers;
  658. };
  659. struct cache_reader {
  660. struct cache_queue q;
  661. int offset; /* if non-0, we have a refcnt on next request */
  662. };
  663. static int cache_request(struct cache_detail *detail,
  664. struct cache_request *crq)
  665. {
  666. char *bp = crq->buf;
  667. int len = PAGE_SIZE;
  668. detail->cache_request(detail, crq->item, &bp, &len);
  669. if (len < 0)
  670. return -EAGAIN;
  671. return PAGE_SIZE - len;
  672. }
  673. static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
  674. loff_t *ppos, struct cache_detail *cd)
  675. {
  676. struct cache_reader *rp = filp->private_data;
  677. struct cache_request *rq;
  678. struct inode *inode = file_inode(filp);
  679. int err;
  680. if (count == 0)
  681. return 0;
  682. mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
  683. * readers on this file */
  684. again:
  685. spin_lock(&queue_lock);
  686. /* need to find next request */
  687. while (rp->q.list.next != &cd->queue &&
  688. list_entry(rp->q.list.next, struct cache_queue, list)
  689. ->reader) {
  690. struct list_head *next = rp->q.list.next;
  691. list_move(&rp->q.list, next);
  692. }
  693. if (rp->q.list.next == &cd->queue) {
  694. spin_unlock(&queue_lock);
  695. mutex_unlock(&inode->i_mutex);
  696. WARN_ON_ONCE(rp->offset);
  697. return 0;
  698. }
  699. rq = container_of(rp->q.list.next, struct cache_request, q.list);
  700. WARN_ON_ONCE(rq->q.reader);
  701. if (rp->offset == 0)
  702. rq->readers++;
  703. spin_unlock(&queue_lock);
  704. if (rq->len == 0) {
  705. err = cache_request(cd, rq);
  706. if (err < 0)
  707. goto out;
  708. rq->len = err;
  709. }
  710. if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
  711. err = -EAGAIN;
  712. spin_lock(&queue_lock);
  713. list_move(&rp->q.list, &rq->q.list);
  714. spin_unlock(&queue_lock);
  715. } else {
  716. if (rp->offset + count > rq->len)
  717. count = rq->len - rp->offset;
  718. err = -EFAULT;
  719. if (copy_to_user(buf, rq->buf + rp->offset, count))
  720. goto out;
  721. rp->offset += count;
  722. if (rp->offset >= rq->len) {
  723. rp->offset = 0;
  724. spin_lock(&queue_lock);
  725. list_move(&rp->q.list, &rq->q.list);
  726. spin_unlock(&queue_lock);
  727. }
  728. err = 0;
  729. }
  730. out:
  731. if (rp->offset == 0) {
  732. /* need to release rq */
  733. spin_lock(&queue_lock);
  734. rq->readers--;
  735. if (rq->readers == 0 &&
  736. !test_bit(CACHE_PENDING, &rq->item->flags)) {
  737. list_del(&rq->q.list);
  738. spin_unlock(&queue_lock);
  739. cache_put(rq->item, cd);
  740. kfree(rq->buf);
  741. kfree(rq);
  742. } else
  743. spin_unlock(&queue_lock);
  744. }
  745. if (err == -EAGAIN)
  746. goto again;
  747. mutex_unlock(&inode->i_mutex);
  748. return err ? err : count;
  749. }
  750. static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
  751. size_t count, struct cache_detail *cd)
  752. {
  753. ssize_t ret;
  754. if (count == 0)
  755. return -EINVAL;
  756. if (copy_from_user(kaddr, buf, count))
  757. return -EFAULT;
  758. kaddr[count] = '\0';
  759. ret = cd->cache_parse(cd, kaddr, count);
  760. if (!ret)
  761. ret = count;
  762. return ret;
  763. }
  764. static ssize_t cache_slow_downcall(const char __user *buf,
  765. size_t count, struct cache_detail *cd)
  766. {
  767. static char write_buf[8192]; /* protected by queue_io_mutex */
  768. ssize_t ret = -EINVAL;
  769. if (count >= sizeof(write_buf))
  770. goto out;
  771. mutex_lock(&queue_io_mutex);
  772. ret = cache_do_downcall(write_buf, buf, count, cd);
  773. mutex_unlock(&queue_io_mutex);
  774. out:
  775. return ret;
  776. }
  777. static ssize_t cache_downcall(struct address_space *mapping,
  778. const char __user *buf,
  779. size_t count, struct cache_detail *cd)
  780. {
  781. struct page *page;
  782. char *kaddr;
  783. ssize_t ret = -ENOMEM;
  784. if (count >= PAGE_CACHE_SIZE)
  785. goto out_slow;
  786. page = find_or_create_page(mapping, 0, GFP_KERNEL);
  787. if (!page)
  788. goto out_slow;
  789. kaddr = kmap(page);
  790. ret = cache_do_downcall(kaddr, buf, count, cd);
  791. kunmap(page);
  792. unlock_page(page);
  793. page_cache_release(page);
  794. return ret;
  795. out_slow:
  796. return cache_slow_downcall(buf, count, cd);
  797. }
  798. static ssize_t cache_write(struct file *filp, const char __user *buf,
  799. size_t count, loff_t *ppos,
  800. struct cache_detail *cd)
  801. {
  802. struct address_space *mapping = filp->f_mapping;
  803. struct inode *inode = file_inode(filp);
  804. ssize_t ret = -EINVAL;
  805. if (!cd->cache_parse)
  806. goto out;
  807. mutex_lock(&inode->i_mutex);
  808. ret = cache_downcall(mapping, buf, count, cd);
  809. mutex_unlock(&inode->i_mutex);
  810. out:
  811. return ret;
  812. }
  813. static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
  814. static unsigned int cache_poll(struct file *filp, poll_table *wait,
  815. struct cache_detail *cd)
  816. {
  817. unsigned int mask;
  818. struct cache_reader *rp = filp->private_data;
  819. struct cache_queue *cq;
  820. poll_wait(filp, &queue_wait, wait);
  821. /* alway allow write */
  822. mask = POLLOUT | POLLWRNORM;
  823. if (!rp)
  824. return mask;
  825. spin_lock(&queue_lock);
  826. for (cq= &rp->q; &cq->list != &cd->queue;
  827. cq = list_entry(cq->list.next, struct cache_queue, list))
  828. if (!cq->reader) {
  829. mask |= POLLIN | POLLRDNORM;
  830. break;
  831. }
  832. spin_unlock(&queue_lock);
  833. return mask;
  834. }
  835. static int cache_ioctl(struct inode *ino, struct file *filp,
  836. unsigned int cmd, unsigned long arg,
  837. struct cache_detail *cd)
  838. {
  839. int len = 0;
  840. struct cache_reader *rp = filp->private_data;
  841. struct cache_queue *cq;
  842. if (cmd != FIONREAD || !rp)
  843. return -EINVAL;
  844. spin_lock(&queue_lock);
  845. /* only find the length remaining in current request,
  846. * or the length of the next request
  847. */
  848. for (cq= &rp->q; &cq->list != &cd->queue;
  849. cq = list_entry(cq->list.next, struct cache_queue, list))
  850. if (!cq->reader) {
  851. struct cache_request *cr =
  852. container_of(cq, struct cache_request, q);
  853. len = cr->len - rp->offset;
  854. break;
  855. }
  856. spin_unlock(&queue_lock);
  857. return put_user(len, (int __user *)arg);
  858. }
  859. static int cache_open(struct inode *inode, struct file *filp,
  860. struct cache_detail *cd)
  861. {
  862. struct cache_reader *rp = NULL;
  863. if (!cd || !try_module_get(cd->owner))
  864. return -EACCES;
  865. nonseekable_open(inode, filp);
  866. if (filp->f_mode & FMODE_READ) {
  867. rp = kmalloc(sizeof(*rp), GFP_KERNEL);
  868. if (!rp) {
  869. module_put(cd->owner);
  870. return -ENOMEM;
  871. }
  872. rp->offset = 0;
  873. rp->q.reader = 1;
  874. atomic_inc(&cd->readers);
  875. spin_lock(&queue_lock);
  876. list_add(&rp->q.list, &cd->queue);
  877. spin_unlock(&queue_lock);
  878. }
  879. filp->private_data = rp;
  880. return 0;
  881. }
  882. static int cache_release(struct inode *inode, struct file *filp,
  883. struct cache_detail *cd)
  884. {
  885. struct cache_reader *rp = filp->private_data;
  886. if (rp) {
  887. spin_lock(&queue_lock);
  888. if (rp->offset) {
  889. struct cache_queue *cq;
  890. for (cq= &rp->q; &cq->list != &cd->queue;
  891. cq = list_entry(cq->list.next, struct cache_queue, list))
  892. if (!cq->reader) {
  893. container_of(cq, struct cache_request, q)
  894. ->readers--;
  895. break;
  896. }
  897. rp->offset = 0;
  898. }
  899. list_del(&rp->q.list);
  900. spin_unlock(&queue_lock);
  901. filp->private_data = NULL;
  902. kfree(rp);
  903. cd->last_close = seconds_since_boot();
  904. atomic_dec(&cd->readers);
  905. }
  906. module_put(cd->owner);
  907. return 0;
  908. }
  909. static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
  910. {
  911. struct cache_queue *cq, *tmp;
  912. struct cache_request *cr;
  913. struct list_head dequeued;
  914. INIT_LIST_HEAD(&dequeued);
  915. spin_lock(&queue_lock);
  916. list_for_each_entry_safe(cq, tmp, &detail->queue, list)
  917. if (!cq->reader) {
  918. cr = container_of(cq, struct cache_request, q);
  919. if (cr->item != ch)
  920. continue;
  921. if (test_bit(CACHE_PENDING, &ch->flags))
  922. /* Lost a race and it is pending again */
  923. break;
  924. if (cr->readers != 0)
  925. continue;
  926. list_move(&cr->q.list, &dequeued);
  927. }
  928. spin_unlock(&queue_lock);
  929. while (!list_empty(&dequeued)) {
  930. cr = list_entry(dequeued.next, struct cache_request, q.list);
  931. list_del(&cr->q.list);
  932. cache_put(cr->item, detail);
  933. kfree(cr->buf);
  934. kfree(cr);
  935. }
  936. }
  937. /*
  938. * Support routines for text-based upcalls.
  939. * Fields are separated by spaces.
  940. * Fields are either mangled to quote space tab newline slosh with slosh
  941. * or a hexified with a leading \x
  942. * Record is terminated with newline.
  943. *
  944. */
  945. void qword_add(char **bpp, int *lp, char *str)
  946. {
  947. char *bp = *bpp;
  948. int len = *lp;
  949. int ret;
  950. if (len < 0) return;
  951. ret = string_escape_str(str, bp, len, ESCAPE_OCTAL, "\\ \n\t");
  952. if (ret >= len) {
  953. bp += len;
  954. len = -1;
  955. } else {
  956. bp += ret;
  957. len -= ret;
  958. *bp++ = ' ';
  959. len--;
  960. }
  961. *bpp = bp;
  962. *lp = len;
  963. }
  964. EXPORT_SYMBOL_GPL(qword_add);
  965. void qword_addhex(char **bpp, int *lp, char *buf, int blen)
  966. {
  967. char *bp = *bpp;
  968. int len = *lp;
  969. if (len < 0) return;
  970. if (len > 2) {
  971. *bp++ = '\\';
  972. *bp++ = 'x';
  973. len -= 2;
  974. while (blen && len >= 2) {
  975. bp = hex_byte_pack(bp, *buf++);
  976. len -= 2;
  977. blen--;
  978. }
  979. }
  980. if (blen || len<1) len = -1;
  981. else {
  982. *bp++ = ' ';
  983. len--;
  984. }
  985. *bpp = bp;
  986. *lp = len;
  987. }
  988. EXPORT_SYMBOL_GPL(qword_addhex);
  989. static void warn_no_listener(struct cache_detail *detail)
  990. {
  991. if (detail->last_warn != detail->last_close) {
  992. detail->last_warn = detail->last_close;
  993. if (detail->warn_no_listener)
  994. detail->warn_no_listener(detail, detail->last_close != 0);
  995. }
  996. }
  997. static bool cache_listeners_exist(struct cache_detail *detail)
  998. {
  999. if (atomic_read(&detail->readers))
  1000. return true;
  1001. if (detail->last_close == 0)
  1002. /* This cache was never opened */
  1003. return false;
  1004. if (detail->last_close < seconds_since_boot() - 30)
  1005. /*
  1006. * We allow for the possibility that someone might
  1007. * restart a userspace daemon without restarting the
  1008. * server; but after 30 seconds, we give up.
  1009. */
  1010. return false;
  1011. return true;
  1012. }
  1013. /*
  1014. * register an upcall request to user-space and queue it up for read() by the
  1015. * upcall daemon.
  1016. *
  1017. * Each request is at most one page long.
  1018. */
  1019. int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
  1020. {
  1021. char *buf;
  1022. struct cache_request *crq;
  1023. int ret = 0;
  1024. if (!detail->cache_request)
  1025. return -EINVAL;
  1026. if (!cache_listeners_exist(detail)) {
  1027. warn_no_listener(detail);
  1028. return -EINVAL;
  1029. }
  1030. if (test_bit(CACHE_CLEANED, &h->flags))
  1031. /* Too late to make an upcall */
  1032. return -EAGAIN;
  1033. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1034. if (!buf)
  1035. return -EAGAIN;
  1036. crq = kmalloc(sizeof (*crq), GFP_KERNEL);
  1037. if (!crq) {
  1038. kfree(buf);
  1039. return -EAGAIN;
  1040. }
  1041. crq->q.reader = 0;
  1042. crq->buf = buf;
  1043. crq->len = 0;
  1044. crq->readers = 0;
  1045. spin_lock(&queue_lock);
  1046. if (test_bit(CACHE_PENDING, &h->flags)) {
  1047. crq->item = cache_get(h);
  1048. list_add_tail(&crq->q.list, &detail->queue);
  1049. } else
  1050. /* Lost a race, no longer PENDING, so don't enqueue */
  1051. ret = -EAGAIN;
  1052. spin_unlock(&queue_lock);
  1053. wake_up(&queue_wait);
  1054. if (ret == -EAGAIN) {
  1055. kfree(buf);
  1056. kfree(crq);
  1057. }
  1058. return ret;
  1059. }
  1060. EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
  1061. /*
  1062. * parse a message from user-space and pass it
  1063. * to an appropriate cache
  1064. * Messages are, like requests, separated into fields by
  1065. * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
  1066. *
  1067. * Message is
  1068. * reply cachename expiry key ... content....
  1069. *
  1070. * key and content are both parsed by cache
  1071. */
  1072. int qword_get(char **bpp, char *dest, int bufsize)
  1073. {
  1074. /* return bytes copied, or -1 on error */
  1075. char *bp = *bpp;
  1076. int len = 0;
  1077. while (*bp == ' ') bp++;
  1078. if (bp[0] == '\\' && bp[1] == 'x') {
  1079. /* HEX STRING */
  1080. bp += 2;
  1081. while (len < bufsize - 1) {
  1082. int h, l;
  1083. h = hex_to_bin(bp[0]);
  1084. if (h < 0)
  1085. break;
  1086. l = hex_to_bin(bp[1]);
  1087. if (l < 0)
  1088. break;
  1089. *dest++ = (h << 4) | l;
  1090. bp += 2;
  1091. len++;
  1092. }
  1093. } else {
  1094. /* text with \nnn octal quoting */
  1095. while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
  1096. if (*bp == '\\' &&
  1097. isodigit(bp[1]) && (bp[1] <= '3') &&
  1098. isodigit(bp[2]) &&
  1099. isodigit(bp[3])) {
  1100. int byte = (*++bp -'0');
  1101. bp++;
  1102. byte = (byte << 3) | (*bp++ - '0');
  1103. byte = (byte << 3) | (*bp++ - '0');
  1104. *dest++ = byte;
  1105. len++;
  1106. } else {
  1107. *dest++ = *bp++;
  1108. len++;
  1109. }
  1110. }
  1111. }
  1112. if (*bp != ' ' && *bp != '\n' && *bp != '\0')
  1113. return -1;
  1114. while (*bp == ' ') bp++;
  1115. *bpp = bp;
  1116. *dest = '\0';
  1117. return len;
  1118. }
  1119. EXPORT_SYMBOL_GPL(qword_get);
  1120. /*
  1121. * support /proc/sunrpc/cache/$CACHENAME/content
  1122. * as a seqfile.
  1123. * We call ->cache_show passing NULL for the item to
  1124. * get a header, then pass each real item in the cache
  1125. */
  1126. void *cache_seq_start(struct seq_file *m, loff_t *pos)
  1127. __acquires(cd->hash_lock)
  1128. {
  1129. loff_t n = *pos;
  1130. unsigned int hash, entry;
  1131. struct cache_head *ch;
  1132. struct cache_detail *cd = m->private;
  1133. read_lock(&cd->hash_lock);
  1134. if (!n--)
  1135. return SEQ_START_TOKEN;
  1136. hash = n >> 32;
  1137. entry = n & ((1LL<<32) - 1);
  1138. hlist_for_each_entry(ch, &cd->hash_table[hash], cache_list)
  1139. if (!entry--)
  1140. return ch;
  1141. n &= ~((1LL<<32) - 1);
  1142. do {
  1143. hash++;
  1144. n += 1LL<<32;
  1145. } while(hash < cd->hash_size &&
  1146. hlist_empty(&cd->hash_table[hash]));
  1147. if (hash >= cd->hash_size)
  1148. return NULL;
  1149. *pos = n+1;
  1150. return hlist_entry_safe(cd->hash_table[hash].first,
  1151. struct cache_head, cache_list);
  1152. }
  1153. EXPORT_SYMBOL_GPL(cache_seq_start);
  1154. void *cache_seq_next(struct seq_file *m, void *p, loff_t *pos)
  1155. {
  1156. struct cache_head *ch = p;
  1157. int hash = (*pos >> 32);
  1158. struct cache_detail *cd = m->private;
  1159. if (p == SEQ_START_TOKEN)
  1160. hash = 0;
  1161. else if (ch->cache_list.next == NULL) {
  1162. hash++;
  1163. *pos += 1LL<<32;
  1164. } else {
  1165. ++*pos;
  1166. return hlist_entry_safe(ch->cache_list.next,
  1167. struct cache_head, cache_list);
  1168. }
  1169. *pos &= ~((1LL<<32) - 1);
  1170. while (hash < cd->hash_size &&
  1171. hlist_empty(&cd->hash_table[hash])) {
  1172. hash++;
  1173. *pos += 1LL<<32;
  1174. }
  1175. if (hash >= cd->hash_size)
  1176. return NULL;
  1177. ++*pos;
  1178. return hlist_entry_safe(cd->hash_table[hash].first,
  1179. struct cache_head, cache_list);
  1180. }
  1181. EXPORT_SYMBOL_GPL(cache_seq_next);
  1182. void cache_seq_stop(struct seq_file *m, void *p)
  1183. __releases(cd->hash_lock)
  1184. {
  1185. struct cache_detail *cd = m->private;
  1186. read_unlock(&cd->hash_lock);
  1187. }
  1188. EXPORT_SYMBOL_GPL(cache_seq_stop);
  1189. static int c_show(struct seq_file *m, void *p)
  1190. {
  1191. struct cache_head *cp = p;
  1192. struct cache_detail *cd = m->private;
  1193. if (p == SEQ_START_TOKEN)
  1194. return cd->cache_show(m, cd, NULL);
  1195. ifdebug(CACHE)
  1196. seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
  1197. convert_to_wallclock(cp->expiry_time),
  1198. atomic_read(&cp->ref.refcount), cp->flags);
  1199. cache_get(cp);
  1200. if (cache_check(cd, cp, NULL))
  1201. /* cache_check does a cache_put on failure */
  1202. seq_printf(m, "# ");
  1203. else {
  1204. if (cache_is_expired(cd, cp))
  1205. seq_printf(m, "# ");
  1206. cache_put(cp, cd);
  1207. }
  1208. return cd->cache_show(m, cd, cp);
  1209. }
  1210. static const struct seq_operations cache_content_op = {
  1211. .start = cache_seq_start,
  1212. .next = cache_seq_next,
  1213. .stop = cache_seq_stop,
  1214. .show = c_show,
  1215. };
  1216. static int content_open(struct inode *inode, struct file *file,
  1217. struct cache_detail *cd)
  1218. {
  1219. struct seq_file *seq;
  1220. int err;
  1221. if (!cd || !try_module_get(cd->owner))
  1222. return -EACCES;
  1223. err = seq_open(file, &cache_content_op);
  1224. if (err) {
  1225. module_put(cd->owner);
  1226. return err;
  1227. }
  1228. seq = file->private_data;
  1229. seq->private = cd;
  1230. return 0;
  1231. }
  1232. static int content_release(struct inode *inode, struct file *file,
  1233. struct cache_detail *cd)
  1234. {
  1235. int ret = seq_release(inode, file);
  1236. module_put(cd->owner);
  1237. return ret;
  1238. }
  1239. static int open_flush(struct inode *inode, struct file *file,
  1240. struct cache_detail *cd)
  1241. {
  1242. if (!cd || !try_module_get(cd->owner))
  1243. return -EACCES;
  1244. return nonseekable_open(inode, file);
  1245. }
  1246. static int release_flush(struct inode *inode, struct file *file,
  1247. struct cache_detail *cd)
  1248. {
  1249. module_put(cd->owner);
  1250. return 0;
  1251. }
  1252. static ssize_t read_flush(struct file *file, char __user *buf,
  1253. size_t count, loff_t *ppos,
  1254. struct cache_detail *cd)
  1255. {
  1256. char tbuf[22];
  1257. unsigned long p = *ppos;
  1258. size_t len;
  1259. snprintf(tbuf, sizeof(tbuf), "%lu\n", convert_to_wallclock(cd->flush_time));
  1260. len = strlen(tbuf);
  1261. if (p >= len)
  1262. return 0;
  1263. len -= p;
  1264. if (len > count)
  1265. len = count;
  1266. if (copy_to_user(buf, (void*)(tbuf+p), len))
  1267. return -EFAULT;
  1268. *ppos += len;
  1269. return len;
  1270. }
  1271. static ssize_t write_flush(struct file *file, const char __user *buf,
  1272. size_t count, loff_t *ppos,
  1273. struct cache_detail *cd)
  1274. {
  1275. char tbuf[20];
  1276. char *bp, *ep;
  1277. time_t then, now;
  1278. if (*ppos || count > sizeof(tbuf)-1)
  1279. return -EINVAL;
  1280. if (copy_from_user(tbuf, buf, count))
  1281. return -EFAULT;
  1282. tbuf[count] = 0;
  1283. simple_strtoul(tbuf, &ep, 0);
  1284. if (*ep && *ep != '\n')
  1285. return -EINVAL;
  1286. bp = tbuf;
  1287. then = get_expiry(&bp);
  1288. now = seconds_since_boot();
  1289. cd->nextcheck = now;
  1290. /* Can only set flush_time to 1 second beyond "now", or
  1291. * possibly 1 second beyond flushtime. This is because
  1292. * flush_time never goes backwards so it mustn't get too far
  1293. * ahead of time.
  1294. */
  1295. if (then >= now) {
  1296. /* Want to flush everything, so behave like cache_purge() */
  1297. if (cd->flush_time >= now)
  1298. now = cd->flush_time + 1;
  1299. then = now;
  1300. }
  1301. cd->flush_time = then;
  1302. cache_flush();
  1303. *ppos += count;
  1304. return count;
  1305. }
  1306. static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
  1307. size_t count, loff_t *ppos)
  1308. {
  1309. struct cache_detail *cd = PDE_DATA(file_inode(filp));
  1310. return cache_read(filp, buf, count, ppos, cd);
  1311. }
  1312. static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
  1313. size_t count, loff_t *ppos)
  1314. {
  1315. struct cache_detail *cd = PDE_DATA(file_inode(filp));
  1316. return cache_write(filp, buf, count, ppos, cd);
  1317. }
  1318. static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
  1319. {
  1320. struct cache_detail *cd = PDE_DATA(file_inode(filp));
  1321. return cache_poll(filp, wait, cd);
  1322. }
  1323. static long cache_ioctl_procfs(struct file *filp,
  1324. unsigned int cmd, unsigned long arg)
  1325. {
  1326. struct inode *inode = file_inode(filp);
  1327. struct cache_detail *cd = PDE_DATA(inode);
  1328. return cache_ioctl(inode, filp, cmd, arg, cd);
  1329. }
  1330. static int cache_open_procfs(struct inode *inode, struct file *filp)
  1331. {
  1332. struct cache_detail *cd = PDE_DATA(inode);
  1333. return cache_open(inode, filp, cd);
  1334. }
  1335. static int cache_release_procfs(struct inode *inode, struct file *filp)
  1336. {
  1337. struct cache_detail *cd = PDE_DATA(inode);
  1338. return cache_release(inode, filp, cd);
  1339. }
  1340. static const struct file_operations cache_file_operations_procfs = {
  1341. .owner = THIS_MODULE,
  1342. .llseek = no_llseek,
  1343. .read = cache_read_procfs,
  1344. .write = cache_write_procfs,
  1345. .poll = cache_poll_procfs,
  1346. .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
  1347. .open = cache_open_procfs,
  1348. .release = cache_release_procfs,
  1349. };
  1350. static int content_open_procfs(struct inode *inode, struct file *filp)
  1351. {
  1352. struct cache_detail *cd = PDE_DATA(inode);
  1353. return content_open(inode, filp, cd);
  1354. }
  1355. static int content_release_procfs(struct inode *inode, struct file *filp)
  1356. {
  1357. struct cache_detail *cd = PDE_DATA(inode);
  1358. return content_release(inode, filp, cd);
  1359. }
  1360. static const struct file_operations content_file_operations_procfs = {
  1361. .open = content_open_procfs,
  1362. .read = seq_read,
  1363. .llseek = seq_lseek,
  1364. .release = content_release_procfs,
  1365. };
  1366. static int open_flush_procfs(struct inode *inode, struct file *filp)
  1367. {
  1368. struct cache_detail *cd = PDE_DATA(inode);
  1369. return open_flush(inode, filp, cd);
  1370. }
  1371. static int release_flush_procfs(struct inode *inode, struct file *filp)
  1372. {
  1373. struct cache_detail *cd = PDE_DATA(inode);
  1374. return release_flush(inode, filp, cd);
  1375. }
  1376. static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
  1377. size_t count, loff_t *ppos)
  1378. {
  1379. struct cache_detail *cd = PDE_DATA(file_inode(filp));
  1380. return read_flush(filp, buf, count, ppos, cd);
  1381. }
  1382. static ssize_t write_flush_procfs(struct file *filp,
  1383. const char __user *buf,
  1384. size_t count, loff_t *ppos)
  1385. {
  1386. struct cache_detail *cd = PDE_DATA(file_inode(filp));
  1387. return write_flush(filp, buf, count, ppos, cd);
  1388. }
  1389. static const struct file_operations cache_flush_operations_procfs = {
  1390. .open = open_flush_procfs,
  1391. .read = read_flush_procfs,
  1392. .write = write_flush_procfs,
  1393. .release = release_flush_procfs,
  1394. .llseek = no_llseek,
  1395. };
  1396. static void remove_cache_proc_entries(struct cache_detail *cd, struct net *net)
  1397. {
  1398. struct sunrpc_net *sn;
  1399. if (cd->u.procfs.proc_ent == NULL)
  1400. return;
  1401. if (cd->u.procfs.flush_ent)
  1402. remove_proc_entry("flush", cd->u.procfs.proc_ent);
  1403. if (cd->u.procfs.channel_ent)
  1404. remove_proc_entry("channel", cd->u.procfs.proc_ent);
  1405. if (cd->u.procfs.content_ent)
  1406. remove_proc_entry("content", cd->u.procfs.proc_ent);
  1407. cd->u.procfs.proc_ent = NULL;
  1408. sn = net_generic(net, sunrpc_net_id);
  1409. remove_proc_entry(cd->name, sn->proc_net_rpc);
  1410. }
  1411. #ifdef CONFIG_PROC_FS
  1412. static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
  1413. {
  1414. struct proc_dir_entry *p;
  1415. struct sunrpc_net *sn;
  1416. sn = net_generic(net, sunrpc_net_id);
  1417. cd->u.procfs.proc_ent = proc_mkdir(cd->name, sn->proc_net_rpc);
  1418. if (cd->u.procfs.proc_ent == NULL)
  1419. goto out_nomem;
  1420. cd->u.procfs.channel_ent = NULL;
  1421. cd->u.procfs.content_ent = NULL;
  1422. p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
  1423. cd->u.procfs.proc_ent,
  1424. &cache_flush_operations_procfs, cd);
  1425. cd->u.procfs.flush_ent = p;
  1426. if (p == NULL)
  1427. goto out_nomem;
  1428. if (cd->cache_request || cd->cache_parse) {
  1429. p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
  1430. cd->u.procfs.proc_ent,
  1431. &cache_file_operations_procfs, cd);
  1432. cd->u.procfs.channel_ent = p;
  1433. if (p == NULL)
  1434. goto out_nomem;
  1435. }
  1436. if (cd->cache_show) {
  1437. p = proc_create_data("content", S_IFREG|S_IRUSR,
  1438. cd->u.procfs.proc_ent,
  1439. &content_file_operations_procfs, cd);
  1440. cd->u.procfs.content_ent = p;
  1441. if (p == NULL)
  1442. goto out_nomem;
  1443. }
  1444. return 0;
  1445. out_nomem:
  1446. remove_cache_proc_entries(cd, net);
  1447. return -ENOMEM;
  1448. }
  1449. #else /* CONFIG_PROC_FS */
  1450. static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
  1451. {
  1452. return 0;
  1453. }
  1454. #endif
  1455. void __init cache_initialize(void)
  1456. {
  1457. INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean);
  1458. }
  1459. int cache_register_net(struct cache_detail *cd, struct net *net)
  1460. {
  1461. int ret;
  1462. sunrpc_init_cache_detail(cd);
  1463. ret = create_cache_proc_entries(cd, net);
  1464. if (ret)
  1465. sunrpc_destroy_cache_detail(cd);
  1466. return ret;
  1467. }
  1468. EXPORT_SYMBOL_GPL(cache_register_net);
  1469. void cache_unregister_net(struct cache_detail *cd, struct net *net)
  1470. {
  1471. remove_cache_proc_entries(cd, net);
  1472. sunrpc_destroy_cache_detail(cd);
  1473. }
  1474. EXPORT_SYMBOL_GPL(cache_unregister_net);
  1475. struct cache_detail *cache_create_net(struct cache_detail *tmpl, struct net *net)
  1476. {
  1477. struct cache_detail *cd;
  1478. int i;
  1479. cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL);
  1480. if (cd == NULL)
  1481. return ERR_PTR(-ENOMEM);
  1482. cd->hash_table = kzalloc(cd->hash_size * sizeof(struct hlist_head),
  1483. GFP_KERNEL);
  1484. if (cd->hash_table == NULL) {
  1485. kfree(cd);
  1486. return ERR_PTR(-ENOMEM);
  1487. }
  1488. for (i = 0; i < cd->hash_size; i++)
  1489. INIT_HLIST_HEAD(&cd->hash_table[i]);
  1490. cd->net = net;
  1491. return cd;
  1492. }
  1493. EXPORT_SYMBOL_GPL(cache_create_net);
  1494. void cache_destroy_net(struct cache_detail *cd, struct net *net)
  1495. {
  1496. kfree(cd->hash_table);
  1497. kfree(cd);
  1498. }
  1499. EXPORT_SYMBOL_GPL(cache_destroy_net);
  1500. static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
  1501. size_t count, loff_t *ppos)
  1502. {
  1503. struct cache_detail *cd = RPC_I(file_inode(filp))->private;
  1504. return cache_read(filp, buf, count, ppos, cd);
  1505. }
  1506. static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
  1507. size_t count, loff_t *ppos)
  1508. {
  1509. struct cache_detail *cd = RPC_I(file_inode(filp))->private;
  1510. return cache_write(filp, buf, count, ppos, cd);
  1511. }
  1512. static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
  1513. {
  1514. struct cache_detail *cd = RPC_I(file_inode(filp))->private;
  1515. return cache_poll(filp, wait, cd);
  1516. }
  1517. static long cache_ioctl_pipefs(struct file *filp,
  1518. unsigned int cmd, unsigned long arg)
  1519. {
  1520. struct inode *inode = file_inode(filp);
  1521. struct cache_detail *cd = RPC_I(inode)->private;
  1522. return cache_ioctl(inode, filp, cmd, arg, cd);
  1523. }
  1524. static int cache_open_pipefs(struct inode *inode, struct file *filp)
  1525. {
  1526. struct cache_detail *cd = RPC_I(inode)->private;
  1527. return cache_open(inode, filp, cd);
  1528. }
  1529. static int cache_release_pipefs(struct inode *inode, struct file *filp)
  1530. {
  1531. struct cache_detail *cd = RPC_I(inode)->private;
  1532. return cache_release(inode, filp, cd);
  1533. }
  1534. const struct file_operations cache_file_operations_pipefs = {
  1535. .owner = THIS_MODULE,
  1536. .llseek = no_llseek,
  1537. .read = cache_read_pipefs,
  1538. .write = cache_write_pipefs,
  1539. .poll = cache_poll_pipefs,
  1540. .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
  1541. .open = cache_open_pipefs,
  1542. .release = cache_release_pipefs,
  1543. };
  1544. static int content_open_pipefs(struct inode *inode, struct file *filp)
  1545. {
  1546. struct cache_detail *cd = RPC_I(inode)->private;
  1547. return content_open(inode, filp, cd);
  1548. }
  1549. static int content_release_pipefs(struct inode *inode, struct file *filp)
  1550. {
  1551. struct cache_detail *cd = RPC_I(inode)->private;
  1552. return content_release(inode, filp, cd);
  1553. }
  1554. const struct file_operations content_file_operations_pipefs = {
  1555. .open = content_open_pipefs,
  1556. .read = seq_read,
  1557. .llseek = seq_lseek,
  1558. .release = content_release_pipefs,
  1559. };
  1560. static int open_flush_pipefs(struct inode *inode, struct file *filp)
  1561. {
  1562. struct cache_detail *cd = RPC_I(inode)->private;
  1563. return open_flush(inode, filp, cd);
  1564. }
  1565. static int release_flush_pipefs(struct inode *inode, struct file *filp)
  1566. {
  1567. struct cache_detail *cd = RPC_I(inode)->private;
  1568. return release_flush(inode, filp, cd);
  1569. }
  1570. static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
  1571. size_t count, loff_t *ppos)
  1572. {
  1573. struct cache_detail *cd = RPC_I(file_inode(filp))->private;
  1574. return read_flush(filp, buf, count, ppos, cd);
  1575. }
  1576. static ssize_t write_flush_pipefs(struct file *filp,
  1577. const char __user *buf,
  1578. size_t count, loff_t *ppos)
  1579. {
  1580. struct cache_detail *cd = RPC_I(file_inode(filp))->private;
  1581. return write_flush(filp, buf, count, ppos, cd);
  1582. }
  1583. const struct file_operations cache_flush_operations_pipefs = {
  1584. .open = open_flush_pipefs,
  1585. .read = read_flush_pipefs,
  1586. .write = write_flush_pipefs,
  1587. .release = release_flush_pipefs,
  1588. .llseek = no_llseek,
  1589. };
  1590. int sunrpc_cache_register_pipefs(struct dentry *parent,
  1591. const char *name, umode_t umode,
  1592. struct cache_detail *cd)
  1593. {
  1594. struct dentry *dir = rpc_create_cache_dir(parent, name, umode, cd);
  1595. if (IS_ERR(dir))
  1596. return PTR_ERR(dir);
  1597. cd->u.pipefs.dir = dir;
  1598. return 0;
  1599. }
  1600. EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
  1601. void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
  1602. {
  1603. rpc_remove_cache_dir(cd->u.pipefs.dir);
  1604. cd->u.pipefs.dir = NULL;
  1605. }
  1606. EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);