cache.c 40 KB

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