cache.c 42 KB

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