cache.c 39 KB

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