cache.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810
  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 (copy_from_user(kaddr, buf, count))
  732. return -EFAULT;
  733. kaddr[count] = '\0';
  734. ret = cd->cache_parse(cd, kaddr, count);
  735. if (!ret)
  736. ret = count;
  737. return ret;
  738. }
  739. static ssize_t cache_slow_downcall(const char __user *buf,
  740. size_t count, struct cache_detail *cd)
  741. {
  742. static char write_buf[8192]; /* protected by queue_io_mutex */
  743. ssize_t ret = -EINVAL;
  744. if (count >= sizeof(write_buf))
  745. goto out;
  746. mutex_lock(&queue_io_mutex);
  747. ret = cache_do_downcall(write_buf, buf, count, cd);
  748. mutex_unlock(&queue_io_mutex);
  749. out:
  750. return ret;
  751. }
  752. static ssize_t cache_downcall(struct address_space *mapping,
  753. const char __user *buf,
  754. size_t count, struct cache_detail *cd)
  755. {
  756. struct page *page;
  757. char *kaddr;
  758. ssize_t ret = -ENOMEM;
  759. if (count >= PAGE_CACHE_SIZE)
  760. goto out_slow;
  761. page = find_or_create_page(mapping, 0, GFP_KERNEL);
  762. if (!page)
  763. goto out_slow;
  764. kaddr = kmap(page);
  765. ret = cache_do_downcall(kaddr, buf, count, cd);
  766. kunmap(page);
  767. unlock_page(page);
  768. page_cache_release(page);
  769. return ret;
  770. out_slow:
  771. return cache_slow_downcall(buf, count, cd);
  772. }
  773. static ssize_t cache_write(struct file *filp, const char __user *buf,
  774. size_t count, loff_t *ppos,
  775. struct cache_detail *cd)
  776. {
  777. struct address_space *mapping = filp->f_mapping;
  778. struct inode *inode = filp->f_path.dentry->d_inode;
  779. ssize_t ret = -EINVAL;
  780. if (!cd->cache_parse)
  781. goto out;
  782. mutex_lock(&inode->i_mutex);
  783. ret = cache_downcall(mapping, buf, count, cd);
  784. mutex_unlock(&inode->i_mutex);
  785. out:
  786. return ret;
  787. }
  788. static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
  789. static unsigned int cache_poll(struct file *filp, poll_table *wait,
  790. struct cache_detail *cd)
  791. {
  792. unsigned int mask;
  793. struct cache_reader *rp = filp->private_data;
  794. struct cache_queue *cq;
  795. poll_wait(filp, &queue_wait, wait);
  796. /* alway allow write */
  797. mask = POLL_OUT | POLLWRNORM;
  798. if (!rp)
  799. return mask;
  800. spin_lock(&queue_lock);
  801. for (cq= &rp->q; &cq->list != &cd->queue;
  802. cq = list_entry(cq->list.next, struct cache_queue, list))
  803. if (!cq->reader) {
  804. mask |= POLLIN | POLLRDNORM;
  805. break;
  806. }
  807. spin_unlock(&queue_lock);
  808. return mask;
  809. }
  810. static int cache_ioctl(struct inode *ino, struct file *filp,
  811. unsigned int cmd, unsigned long arg,
  812. struct cache_detail *cd)
  813. {
  814. int len = 0;
  815. struct cache_reader *rp = filp->private_data;
  816. struct cache_queue *cq;
  817. if (cmd != FIONREAD || !rp)
  818. return -EINVAL;
  819. spin_lock(&queue_lock);
  820. /* only find the length remaining in current request,
  821. * or the length of the next request
  822. */
  823. for (cq= &rp->q; &cq->list != &cd->queue;
  824. cq = list_entry(cq->list.next, struct cache_queue, list))
  825. if (!cq->reader) {
  826. struct cache_request *cr =
  827. container_of(cq, struct cache_request, q);
  828. len = cr->len - rp->offset;
  829. break;
  830. }
  831. spin_unlock(&queue_lock);
  832. return put_user(len, (int __user *)arg);
  833. }
  834. static int cache_open(struct inode *inode, struct file *filp,
  835. struct cache_detail *cd)
  836. {
  837. struct cache_reader *rp = NULL;
  838. if (!cd || !try_module_get(cd->owner))
  839. return -EACCES;
  840. nonseekable_open(inode, filp);
  841. if (filp->f_mode & FMODE_READ) {
  842. rp = kmalloc(sizeof(*rp), GFP_KERNEL);
  843. if (!rp)
  844. return -ENOMEM;
  845. rp->offset = 0;
  846. rp->q.reader = 1;
  847. atomic_inc(&cd->readers);
  848. spin_lock(&queue_lock);
  849. list_add(&rp->q.list, &cd->queue);
  850. spin_unlock(&queue_lock);
  851. }
  852. filp->private_data = rp;
  853. return 0;
  854. }
  855. static int cache_release(struct inode *inode, struct file *filp,
  856. struct cache_detail *cd)
  857. {
  858. struct cache_reader *rp = filp->private_data;
  859. if (rp) {
  860. spin_lock(&queue_lock);
  861. if (rp->offset) {
  862. struct cache_queue *cq;
  863. for (cq= &rp->q; &cq->list != &cd->queue;
  864. cq = list_entry(cq->list.next, struct cache_queue, list))
  865. if (!cq->reader) {
  866. container_of(cq, struct cache_request, q)
  867. ->readers--;
  868. break;
  869. }
  870. rp->offset = 0;
  871. }
  872. list_del(&rp->q.list);
  873. spin_unlock(&queue_lock);
  874. filp->private_data = NULL;
  875. kfree(rp);
  876. cd->last_close = seconds_since_boot();
  877. atomic_dec(&cd->readers);
  878. }
  879. module_put(cd->owner);
  880. return 0;
  881. }
  882. static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
  883. {
  884. struct cache_queue *cq;
  885. spin_lock(&queue_lock);
  886. list_for_each_entry(cq, &detail->queue, list)
  887. if (!cq->reader) {
  888. struct cache_request *cr = container_of(cq, struct cache_request, q);
  889. if (cr->item != ch)
  890. continue;
  891. if (cr->readers != 0)
  892. continue;
  893. list_del(&cr->q.list);
  894. spin_unlock(&queue_lock);
  895. cache_put(cr->item, detail);
  896. kfree(cr->buf);
  897. kfree(cr);
  898. return;
  899. }
  900. spin_unlock(&queue_lock);
  901. }
  902. /*
  903. * Support routines for text-based upcalls.
  904. * Fields are separated by spaces.
  905. * Fields are either mangled to quote space tab newline slosh with slosh
  906. * or a hexified with a leading \x
  907. * Record is terminated with newline.
  908. *
  909. */
  910. void qword_add(char **bpp, int *lp, char *str)
  911. {
  912. char *bp = *bpp;
  913. int len = *lp;
  914. char c;
  915. if (len < 0) return;
  916. while ((c=*str++) && len)
  917. switch(c) {
  918. case ' ':
  919. case '\t':
  920. case '\n':
  921. case '\\':
  922. if (len >= 4) {
  923. *bp++ = '\\';
  924. *bp++ = '0' + ((c & 0300)>>6);
  925. *bp++ = '0' + ((c & 0070)>>3);
  926. *bp++ = '0' + ((c & 0007)>>0);
  927. }
  928. len -= 4;
  929. break;
  930. default:
  931. *bp++ = c;
  932. len--;
  933. }
  934. if (c || len <1) len = -1;
  935. else {
  936. *bp++ = ' ';
  937. len--;
  938. }
  939. *bpp = bp;
  940. *lp = len;
  941. }
  942. EXPORT_SYMBOL_GPL(qword_add);
  943. void qword_addhex(char **bpp, int *lp, char *buf, int blen)
  944. {
  945. char *bp = *bpp;
  946. int len = *lp;
  947. if (len < 0) return;
  948. if (len > 2) {
  949. *bp++ = '\\';
  950. *bp++ = 'x';
  951. len -= 2;
  952. while (blen && len >= 2) {
  953. unsigned char c = *buf++;
  954. *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
  955. *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
  956. len -= 2;
  957. blen--;
  958. }
  959. }
  960. if (blen || len<1) len = -1;
  961. else {
  962. *bp++ = ' ';
  963. len--;
  964. }
  965. *bpp = bp;
  966. *lp = len;
  967. }
  968. EXPORT_SYMBOL_GPL(qword_addhex);
  969. static void warn_no_listener(struct cache_detail *detail)
  970. {
  971. if (detail->last_warn != detail->last_close) {
  972. detail->last_warn = detail->last_close;
  973. if (detail->warn_no_listener)
  974. detail->warn_no_listener(detail, detail->last_close != 0);
  975. }
  976. }
  977. static bool cache_listeners_exist(struct cache_detail *detail)
  978. {
  979. if (atomic_read(&detail->readers))
  980. return true;
  981. if (detail->last_close == 0)
  982. /* This cache was never opened */
  983. return false;
  984. if (detail->last_close < seconds_since_boot() - 30)
  985. /*
  986. * We allow for the possibility that someone might
  987. * restart a userspace daemon without restarting the
  988. * server; but after 30 seconds, we give up.
  989. */
  990. return false;
  991. return true;
  992. }
  993. /*
  994. * register an upcall request to user-space and queue it up for read() by the
  995. * upcall daemon.
  996. *
  997. * Each request is at most one page long.
  998. */
  999. int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h,
  1000. void (*cache_request)(struct cache_detail *,
  1001. struct cache_head *,
  1002. char **,
  1003. int *))
  1004. {
  1005. char *buf;
  1006. struct cache_request *crq;
  1007. char *bp;
  1008. int len;
  1009. if (!cache_listeners_exist(detail)) {
  1010. warn_no_listener(detail);
  1011. return -EINVAL;
  1012. }
  1013. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1014. if (!buf)
  1015. return -EAGAIN;
  1016. crq = kmalloc(sizeof (*crq), GFP_KERNEL);
  1017. if (!crq) {
  1018. kfree(buf);
  1019. return -EAGAIN;
  1020. }
  1021. bp = buf; len = PAGE_SIZE;
  1022. cache_request(detail, h, &bp, &len);
  1023. if (len < 0) {
  1024. kfree(buf);
  1025. kfree(crq);
  1026. return -EAGAIN;
  1027. }
  1028. crq->q.reader = 0;
  1029. crq->item = cache_get(h);
  1030. crq->buf = buf;
  1031. crq->len = PAGE_SIZE - len;
  1032. crq->readers = 0;
  1033. spin_lock(&queue_lock);
  1034. list_add_tail(&crq->q.list, &detail->queue);
  1035. spin_unlock(&queue_lock);
  1036. wake_up(&queue_wait);
  1037. return 0;
  1038. }
  1039. EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
  1040. /*
  1041. * parse a message from user-space and pass it
  1042. * to an appropriate cache
  1043. * Messages are, like requests, separated into fields by
  1044. * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
  1045. *
  1046. * Message is
  1047. * reply cachename expiry key ... content....
  1048. *
  1049. * key and content are both parsed by cache
  1050. */
  1051. #define isodigit(c) (isdigit(c) && c <= '7')
  1052. int qword_get(char **bpp, char *dest, int bufsize)
  1053. {
  1054. /* return bytes copied, or -1 on error */
  1055. char *bp = *bpp;
  1056. int len = 0;
  1057. while (*bp == ' ') bp++;
  1058. if (bp[0] == '\\' && bp[1] == 'x') {
  1059. /* HEX STRING */
  1060. bp += 2;
  1061. while (len < bufsize) {
  1062. int h, l;
  1063. h = hex_to_bin(bp[0]);
  1064. if (h < 0)
  1065. break;
  1066. l = hex_to_bin(bp[1]);
  1067. if (l < 0)
  1068. break;
  1069. *dest++ = (h << 4) | l;
  1070. bp += 2;
  1071. len++;
  1072. }
  1073. } else {
  1074. /* text with \nnn octal quoting */
  1075. while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
  1076. if (*bp == '\\' &&
  1077. isodigit(bp[1]) && (bp[1] <= '3') &&
  1078. isodigit(bp[2]) &&
  1079. isodigit(bp[3])) {
  1080. int byte = (*++bp -'0');
  1081. bp++;
  1082. byte = (byte << 3) | (*bp++ - '0');
  1083. byte = (byte << 3) | (*bp++ - '0');
  1084. *dest++ = byte;
  1085. len++;
  1086. } else {
  1087. *dest++ = *bp++;
  1088. len++;
  1089. }
  1090. }
  1091. }
  1092. if (*bp != ' ' && *bp != '\n' && *bp != '\0')
  1093. return -1;
  1094. while (*bp == ' ') bp++;
  1095. *bpp = bp;
  1096. *dest = '\0';
  1097. return len;
  1098. }
  1099. EXPORT_SYMBOL_GPL(qword_get);
  1100. /*
  1101. * support /proc/sunrpc/cache/$CACHENAME/content
  1102. * as a seqfile.
  1103. * We call ->cache_show passing NULL for the item to
  1104. * get a header, then pass each real item in the cache
  1105. */
  1106. struct handle {
  1107. struct cache_detail *cd;
  1108. };
  1109. static void *c_start(struct seq_file *m, loff_t *pos)
  1110. __acquires(cd->hash_lock)
  1111. {
  1112. loff_t n = *pos;
  1113. unsigned hash, entry;
  1114. struct cache_head *ch;
  1115. struct cache_detail *cd = ((struct handle*)m->private)->cd;
  1116. read_lock(&cd->hash_lock);
  1117. if (!n--)
  1118. return SEQ_START_TOKEN;
  1119. hash = n >> 32;
  1120. entry = n & ((1LL<<32) - 1);
  1121. for (ch=cd->hash_table[hash]; ch; ch=ch->next)
  1122. if (!entry--)
  1123. return ch;
  1124. n &= ~((1LL<<32) - 1);
  1125. do {
  1126. hash++;
  1127. n += 1LL<<32;
  1128. } while(hash < cd->hash_size &&
  1129. cd->hash_table[hash]==NULL);
  1130. if (hash >= cd->hash_size)
  1131. return NULL;
  1132. *pos = n+1;
  1133. return cd->hash_table[hash];
  1134. }
  1135. static void *c_next(struct seq_file *m, void *p, loff_t *pos)
  1136. {
  1137. struct cache_head *ch = p;
  1138. int hash = (*pos >> 32);
  1139. struct cache_detail *cd = ((struct handle*)m->private)->cd;
  1140. if (p == SEQ_START_TOKEN)
  1141. hash = 0;
  1142. else if (ch->next == NULL) {
  1143. hash++;
  1144. *pos += 1LL<<32;
  1145. } else {
  1146. ++*pos;
  1147. return ch->next;
  1148. }
  1149. *pos &= ~((1LL<<32) - 1);
  1150. while (hash < cd->hash_size &&
  1151. cd->hash_table[hash] == NULL) {
  1152. hash++;
  1153. *pos += 1LL<<32;
  1154. }
  1155. if (hash >= cd->hash_size)
  1156. return NULL;
  1157. ++*pos;
  1158. return cd->hash_table[hash];
  1159. }
  1160. static void c_stop(struct seq_file *m, void *p)
  1161. __releases(cd->hash_lock)
  1162. {
  1163. struct cache_detail *cd = ((struct handle*)m->private)->cd;
  1164. read_unlock(&cd->hash_lock);
  1165. }
  1166. static int c_show(struct seq_file *m, void *p)
  1167. {
  1168. struct cache_head *cp = p;
  1169. struct cache_detail *cd = ((struct handle*)m->private)->cd;
  1170. if (p == SEQ_START_TOKEN)
  1171. return cd->cache_show(m, cd, NULL);
  1172. ifdebug(CACHE)
  1173. seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
  1174. convert_to_wallclock(cp->expiry_time),
  1175. atomic_read(&cp->ref.refcount), cp->flags);
  1176. cache_get(cp);
  1177. if (cache_check(cd, cp, NULL))
  1178. /* cache_check does a cache_put on failure */
  1179. seq_printf(m, "# ");
  1180. else
  1181. cache_put(cp, cd);
  1182. return cd->cache_show(m, cd, cp);
  1183. }
  1184. static const struct seq_operations cache_content_op = {
  1185. .start = c_start,
  1186. .next = c_next,
  1187. .stop = c_stop,
  1188. .show = c_show,
  1189. };
  1190. static int content_open(struct inode *inode, struct file *file,
  1191. struct cache_detail *cd)
  1192. {
  1193. struct handle *han;
  1194. if (!cd || !try_module_get(cd->owner))
  1195. return -EACCES;
  1196. han = __seq_open_private(file, &cache_content_op, sizeof(*han));
  1197. if (han == NULL) {
  1198. module_put(cd->owner);
  1199. return -ENOMEM;
  1200. }
  1201. han->cd = cd;
  1202. return 0;
  1203. }
  1204. static int content_release(struct inode *inode, struct file *file,
  1205. struct cache_detail *cd)
  1206. {
  1207. int ret = seq_release_private(inode, file);
  1208. module_put(cd->owner);
  1209. return ret;
  1210. }
  1211. static int open_flush(struct inode *inode, struct file *file,
  1212. struct cache_detail *cd)
  1213. {
  1214. if (!cd || !try_module_get(cd->owner))
  1215. return -EACCES;
  1216. return nonseekable_open(inode, file);
  1217. }
  1218. static int release_flush(struct inode *inode, struct file *file,
  1219. struct cache_detail *cd)
  1220. {
  1221. module_put(cd->owner);
  1222. return 0;
  1223. }
  1224. static ssize_t read_flush(struct file *file, char __user *buf,
  1225. size_t count, loff_t *ppos,
  1226. struct cache_detail *cd)
  1227. {
  1228. char tbuf[20];
  1229. unsigned long p = *ppos;
  1230. size_t len;
  1231. sprintf(tbuf, "%lu\n", convert_to_wallclock(cd->flush_time));
  1232. len = strlen(tbuf);
  1233. if (p >= len)
  1234. return 0;
  1235. len -= p;
  1236. if (len > count)
  1237. len = count;
  1238. if (copy_to_user(buf, (void*)(tbuf+p), len))
  1239. return -EFAULT;
  1240. *ppos += len;
  1241. return len;
  1242. }
  1243. static ssize_t write_flush(struct file *file, const char __user *buf,
  1244. size_t count, loff_t *ppos,
  1245. struct cache_detail *cd)
  1246. {
  1247. char tbuf[20];
  1248. char *bp, *ep;
  1249. if (*ppos || count > sizeof(tbuf)-1)
  1250. return -EINVAL;
  1251. if (copy_from_user(tbuf, buf, count))
  1252. return -EFAULT;
  1253. tbuf[count] = 0;
  1254. simple_strtoul(tbuf, &ep, 0);
  1255. if (*ep && *ep != '\n')
  1256. return -EINVAL;
  1257. bp = tbuf;
  1258. cd->flush_time = get_expiry(&bp);
  1259. cd->nextcheck = seconds_since_boot();
  1260. cache_flush();
  1261. *ppos += count;
  1262. return count;
  1263. }
  1264. static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
  1265. size_t count, loff_t *ppos)
  1266. {
  1267. struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
  1268. return cache_read(filp, buf, count, ppos, cd);
  1269. }
  1270. static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
  1271. size_t count, loff_t *ppos)
  1272. {
  1273. struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
  1274. return cache_write(filp, buf, count, ppos, cd);
  1275. }
  1276. static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
  1277. {
  1278. struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
  1279. return cache_poll(filp, wait, cd);
  1280. }
  1281. static long cache_ioctl_procfs(struct file *filp,
  1282. unsigned int cmd, unsigned long arg)
  1283. {
  1284. struct inode *inode = filp->f_path.dentry->d_inode;
  1285. struct cache_detail *cd = PDE(inode)->data;
  1286. return cache_ioctl(inode, filp, cmd, arg, cd);
  1287. }
  1288. static int cache_open_procfs(struct inode *inode, struct file *filp)
  1289. {
  1290. struct cache_detail *cd = PDE(inode)->data;
  1291. return cache_open(inode, filp, cd);
  1292. }
  1293. static int cache_release_procfs(struct inode *inode, struct file *filp)
  1294. {
  1295. struct cache_detail *cd = PDE(inode)->data;
  1296. return cache_release(inode, filp, cd);
  1297. }
  1298. static const struct file_operations cache_file_operations_procfs = {
  1299. .owner = THIS_MODULE,
  1300. .llseek = no_llseek,
  1301. .read = cache_read_procfs,
  1302. .write = cache_write_procfs,
  1303. .poll = cache_poll_procfs,
  1304. .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
  1305. .open = cache_open_procfs,
  1306. .release = cache_release_procfs,
  1307. };
  1308. static int content_open_procfs(struct inode *inode, struct file *filp)
  1309. {
  1310. struct cache_detail *cd = PDE(inode)->data;
  1311. return content_open(inode, filp, cd);
  1312. }
  1313. static int content_release_procfs(struct inode *inode, struct file *filp)
  1314. {
  1315. struct cache_detail *cd = PDE(inode)->data;
  1316. return content_release(inode, filp, cd);
  1317. }
  1318. static const struct file_operations content_file_operations_procfs = {
  1319. .open = content_open_procfs,
  1320. .read = seq_read,
  1321. .llseek = seq_lseek,
  1322. .release = content_release_procfs,
  1323. };
  1324. static int open_flush_procfs(struct inode *inode, struct file *filp)
  1325. {
  1326. struct cache_detail *cd = PDE(inode)->data;
  1327. return open_flush(inode, filp, cd);
  1328. }
  1329. static int release_flush_procfs(struct inode *inode, struct file *filp)
  1330. {
  1331. struct cache_detail *cd = PDE(inode)->data;
  1332. return release_flush(inode, filp, cd);
  1333. }
  1334. static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
  1335. size_t count, loff_t *ppos)
  1336. {
  1337. struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
  1338. return read_flush(filp, buf, count, ppos, cd);
  1339. }
  1340. static ssize_t write_flush_procfs(struct file *filp,
  1341. const char __user *buf,
  1342. size_t count, loff_t *ppos)
  1343. {
  1344. struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
  1345. return write_flush(filp, buf, count, ppos, cd);
  1346. }
  1347. static const struct file_operations cache_flush_operations_procfs = {
  1348. .open = open_flush_procfs,
  1349. .read = read_flush_procfs,
  1350. .write = write_flush_procfs,
  1351. .release = release_flush_procfs,
  1352. .llseek = no_llseek,
  1353. };
  1354. static void remove_cache_proc_entries(struct cache_detail *cd, struct net *net)
  1355. {
  1356. struct sunrpc_net *sn;
  1357. if (cd->u.procfs.proc_ent == NULL)
  1358. return;
  1359. if (cd->u.procfs.flush_ent)
  1360. remove_proc_entry("flush", cd->u.procfs.proc_ent);
  1361. if (cd->u.procfs.channel_ent)
  1362. remove_proc_entry("channel", cd->u.procfs.proc_ent);
  1363. if (cd->u.procfs.content_ent)
  1364. remove_proc_entry("content", cd->u.procfs.proc_ent);
  1365. cd->u.procfs.proc_ent = NULL;
  1366. sn = net_generic(net, sunrpc_net_id);
  1367. remove_proc_entry(cd->name, sn->proc_net_rpc);
  1368. }
  1369. #ifdef CONFIG_PROC_FS
  1370. static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
  1371. {
  1372. struct proc_dir_entry *p;
  1373. struct sunrpc_net *sn;
  1374. sn = net_generic(net, sunrpc_net_id);
  1375. cd->u.procfs.proc_ent = proc_mkdir(cd->name, sn->proc_net_rpc);
  1376. if (cd->u.procfs.proc_ent == NULL)
  1377. goto out_nomem;
  1378. cd->u.procfs.channel_ent = NULL;
  1379. cd->u.procfs.content_ent = NULL;
  1380. p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
  1381. cd->u.procfs.proc_ent,
  1382. &cache_flush_operations_procfs, cd);
  1383. cd->u.procfs.flush_ent = p;
  1384. if (p == NULL)
  1385. goto out_nomem;
  1386. if (cd->cache_upcall || cd->cache_parse) {
  1387. p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
  1388. cd->u.procfs.proc_ent,
  1389. &cache_file_operations_procfs, cd);
  1390. cd->u.procfs.channel_ent = p;
  1391. if (p == NULL)
  1392. goto out_nomem;
  1393. }
  1394. if (cd->cache_show) {
  1395. p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR,
  1396. cd->u.procfs.proc_ent,
  1397. &content_file_operations_procfs, cd);
  1398. cd->u.procfs.content_ent = p;
  1399. if (p == NULL)
  1400. goto out_nomem;
  1401. }
  1402. return 0;
  1403. out_nomem:
  1404. remove_cache_proc_entries(cd, net);
  1405. return -ENOMEM;
  1406. }
  1407. #else /* CONFIG_PROC_FS */
  1408. static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
  1409. {
  1410. return 0;
  1411. }
  1412. #endif
  1413. void __init cache_initialize(void)
  1414. {
  1415. INIT_DELAYED_WORK_DEFERRABLE(&cache_cleaner, do_cache_clean);
  1416. }
  1417. int cache_register_net(struct cache_detail *cd, struct net *net)
  1418. {
  1419. int ret;
  1420. sunrpc_init_cache_detail(cd);
  1421. ret = create_cache_proc_entries(cd, net);
  1422. if (ret)
  1423. sunrpc_destroy_cache_detail(cd);
  1424. return ret;
  1425. }
  1426. int cache_register(struct cache_detail *cd)
  1427. {
  1428. return cache_register_net(cd, &init_net);
  1429. }
  1430. EXPORT_SYMBOL_GPL(cache_register);
  1431. void cache_unregister_net(struct cache_detail *cd, struct net *net)
  1432. {
  1433. remove_cache_proc_entries(cd, net);
  1434. sunrpc_destroy_cache_detail(cd);
  1435. }
  1436. void cache_unregister(struct cache_detail *cd)
  1437. {
  1438. cache_unregister_net(cd, &init_net);
  1439. }
  1440. EXPORT_SYMBOL_GPL(cache_unregister);
  1441. static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
  1442. size_t count, loff_t *ppos)
  1443. {
  1444. struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
  1445. return cache_read(filp, buf, count, ppos, cd);
  1446. }
  1447. static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
  1448. size_t count, loff_t *ppos)
  1449. {
  1450. struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
  1451. return cache_write(filp, buf, count, ppos, cd);
  1452. }
  1453. static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
  1454. {
  1455. struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
  1456. return cache_poll(filp, wait, cd);
  1457. }
  1458. static long cache_ioctl_pipefs(struct file *filp,
  1459. unsigned int cmd, unsigned long arg)
  1460. {
  1461. struct inode *inode = filp->f_dentry->d_inode;
  1462. struct cache_detail *cd = RPC_I(inode)->private;
  1463. return cache_ioctl(inode, filp, cmd, arg, cd);
  1464. }
  1465. static int cache_open_pipefs(struct inode *inode, struct file *filp)
  1466. {
  1467. struct cache_detail *cd = RPC_I(inode)->private;
  1468. return cache_open(inode, filp, cd);
  1469. }
  1470. static int cache_release_pipefs(struct inode *inode, struct file *filp)
  1471. {
  1472. struct cache_detail *cd = RPC_I(inode)->private;
  1473. return cache_release(inode, filp, cd);
  1474. }
  1475. const struct file_operations cache_file_operations_pipefs = {
  1476. .owner = THIS_MODULE,
  1477. .llseek = no_llseek,
  1478. .read = cache_read_pipefs,
  1479. .write = cache_write_pipefs,
  1480. .poll = cache_poll_pipefs,
  1481. .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
  1482. .open = cache_open_pipefs,
  1483. .release = cache_release_pipefs,
  1484. };
  1485. static int content_open_pipefs(struct inode *inode, struct file *filp)
  1486. {
  1487. struct cache_detail *cd = RPC_I(inode)->private;
  1488. return content_open(inode, filp, cd);
  1489. }
  1490. static int content_release_pipefs(struct inode *inode, struct file *filp)
  1491. {
  1492. struct cache_detail *cd = RPC_I(inode)->private;
  1493. return content_release(inode, filp, cd);
  1494. }
  1495. const struct file_operations content_file_operations_pipefs = {
  1496. .open = content_open_pipefs,
  1497. .read = seq_read,
  1498. .llseek = seq_lseek,
  1499. .release = content_release_pipefs,
  1500. };
  1501. static int open_flush_pipefs(struct inode *inode, struct file *filp)
  1502. {
  1503. struct cache_detail *cd = RPC_I(inode)->private;
  1504. return open_flush(inode, filp, cd);
  1505. }
  1506. static int release_flush_pipefs(struct inode *inode, struct file *filp)
  1507. {
  1508. struct cache_detail *cd = RPC_I(inode)->private;
  1509. return release_flush(inode, filp, cd);
  1510. }
  1511. static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
  1512. size_t count, loff_t *ppos)
  1513. {
  1514. struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
  1515. return read_flush(filp, buf, count, ppos, cd);
  1516. }
  1517. static ssize_t write_flush_pipefs(struct file *filp,
  1518. const char __user *buf,
  1519. size_t count, loff_t *ppos)
  1520. {
  1521. struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
  1522. return write_flush(filp, buf, count, ppos, cd);
  1523. }
  1524. const struct file_operations cache_flush_operations_pipefs = {
  1525. .open = open_flush_pipefs,
  1526. .read = read_flush_pipefs,
  1527. .write = write_flush_pipefs,
  1528. .release = release_flush_pipefs,
  1529. .llseek = no_llseek,
  1530. };
  1531. int sunrpc_cache_register_pipefs(struct dentry *parent,
  1532. const char *name, mode_t umode,
  1533. struct cache_detail *cd)
  1534. {
  1535. struct qstr q;
  1536. struct dentry *dir;
  1537. int ret = 0;
  1538. sunrpc_init_cache_detail(cd);
  1539. q.name = name;
  1540. q.len = strlen(name);
  1541. q.hash = full_name_hash(q.name, q.len);
  1542. dir = rpc_create_cache_dir(parent, &q, umode, cd);
  1543. if (!IS_ERR(dir))
  1544. cd->u.pipefs.dir = dir;
  1545. else {
  1546. sunrpc_destroy_cache_detail(cd);
  1547. ret = PTR_ERR(dir);
  1548. }
  1549. return ret;
  1550. }
  1551. EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
  1552. void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
  1553. {
  1554. rpc_remove_cache_dir(cd->u.pipefs.dir);
  1555. cd->u.pipefs.dir = NULL;
  1556. sunrpc_destroy_cache_detail(cd);
  1557. }
  1558. EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);