cache.c 43 KB

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