cache.c 41 KB

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