cache.c 39 KB

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