cache.c 40 KB

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