cache.c 40 KB

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