cache.c 39 KB

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