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