cache.c 43 KB

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