cache.c 43 KB

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