cache.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647
  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. for (ch = *cp ; ch ; cp = & ch->next, ch = *cp) {
  359. if (current_detail->nextcheck > ch->expiry_time)
  360. current_detail->nextcheck = ch->expiry_time+1;
  361. if (ch->expiry_time >= get_seconds() &&
  362. ch->last_refresh >= current_detail->flush_time)
  363. continue;
  364. *cp = ch->next;
  365. ch->next = NULL;
  366. current_detail->entries--;
  367. rv = 1;
  368. break;
  369. }
  370. write_unlock(&current_detail->hash_lock);
  371. d = current_detail;
  372. if (!ch)
  373. current_index ++;
  374. spin_unlock(&cache_list_lock);
  375. if (ch) {
  376. if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
  377. cache_dequeue(current_detail, ch);
  378. cache_revisit_request(ch);
  379. cache_put(ch, d);
  380. }
  381. } else
  382. spin_unlock(&cache_list_lock);
  383. return rv;
  384. }
  385. /*
  386. * We want to regularly clean the cache, so we need to schedule some work ...
  387. */
  388. static void do_cache_clean(struct work_struct *work)
  389. {
  390. int delay = 5;
  391. if (cache_clean() == -1)
  392. delay = round_jiffies_relative(30*HZ);
  393. if (list_empty(&cache_list))
  394. delay = 0;
  395. if (delay)
  396. schedule_delayed_work(&cache_cleaner, delay);
  397. }
  398. /*
  399. * Clean all caches promptly. This just calls cache_clean
  400. * repeatedly until we are sure that every cache has had a chance to
  401. * be fully cleaned
  402. */
  403. void cache_flush(void)
  404. {
  405. while (cache_clean() != -1)
  406. cond_resched();
  407. while (cache_clean() != -1)
  408. cond_resched();
  409. }
  410. EXPORT_SYMBOL_GPL(cache_flush);
  411. void cache_purge(struct cache_detail *detail)
  412. {
  413. detail->flush_time = LONG_MAX;
  414. detail->nextcheck = get_seconds();
  415. cache_flush();
  416. detail->flush_time = 1;
  417. }
  418. EXPORT_SYMBOL_GPL(cache_purge);
  419. /*
  420. * Deferral and Revisiting of Requests.
  421. *
  422. * If a cache lookup finds a pending entry, we
  423. * need to defer the request and revisit it later.
  424. * All deferred requests are stored in a hash table,
  425. * indexed by "struct cache_head *".
  426. * As it may be wasteful to store a whole request
  427. * structure, we allow the request to provide a
  428. * deferred form, which must contain a
  429. * 'struct cache_deferred_req'
  430. * This cache_deferred_req contains a method to allow
  431. * it to be revisited when cache info is available
  432. */
  433. #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
  434. #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
  435. #define DFR_MAX 300 /* ??? */
  436. static DEFINE_SPINLOCK(cache_defer_lock);
  437. static LIST_HEAD(cache_defer_list);
  438. static struct list_head cache_defer_hash[DFR_HASHSIZE];
  439. static int cache_defer_cnt;
  440. static int cache_defer_req(struct cache_req *req, struct cache_head *item)
  441. {
  442. struct cache_deferred_req *dreq, *discard;
  443. int hash = DFR_HASH(item);
  444. if (cache_defer_cnt >= DFR_MAX) {
  445. /* too much in the cache, randomly drop this one,
  446. * or continue and drop the oldest below
  447. */
  448. if (net_random()&1)
  449. return -ENOMEM;
  450. }
  451. dreq = req->defer(req);
  452. if (dreq == NULL)
  453. return -ENOMEM;
  454. dreq->item = item;
  455. spin_lock(&cache_defer_lock);
  456. list_add(&dreq->recent, &cache_defer_list);
  457. if (cache_defer_hash[hash].next == NULL)
  458. INIT_LIST_HEAD(&cache_defer_hash[hash]);
  459. list_add(&dreq->hash, &cache_defer_hash[hash]);
  460. /* it is in, now maybe clean up */
  461. discard = NULL;
  462. if (++cache_defer_cnt > DFR_MAX) {
  463. discard = list_entry(cache_defer_list.prev,
  464. struct cache_deferred_req, recent);
  465. list_del_init(&discard->recent);
  466. list_del_init(&discard->hash);
  467. cache_defer_cnt--;
  468. }
  469. spin_unlock(&cache_defer_lock);
  470. if (discard)
  471. /* there was one too many */
  472. discard->revisit(discard, 1);
  473. if (!test_bit(CACHE_PENDING, &item->flags)) {
  474. /* must have just been validated... */
  475. cache_revisit_request(item);
  476. return -EAGAIN;
  477. }
  478. return 0;
  479. }
  480. static void cache_revisit_request(struct cache_head *item)
  481. {
  482. struct cache_deferred_req *dreq;
  483. struct list_head pending;
  484. struct list_head *lp;
  485. int hash = DFR_HASH(item);
  486. INIT_LIST_HEAD(&pending);
  487. spin_lock(&cache_defer_lock);
  488. lp = cache_defer_hash[hash].next;
  489. if (lp) {
  490. while (lp != &cache_defer_hash[hash]) {
  491. dreq = list_entry(lp, struct cache_deferred_req, hash);
  492. lp = lp->next;
  493. if (dreq->item == item) {
  494. list_del_init(&dreq->hash);
  495. list_move(&dreq->recent, &pending);
  496. cache_defer_cnt--;
  497. }
  498. }
  499. }
  500. spin_unlock(&cache_defer_lock);
  501. while (!list_empty(&pending)) {
  502. dreq = list_entry(pending.next, struct cache_deferred_req, recent);
  503. list_del_init(&dreq->recent);
  504. dreq->revisit(dreq, 0);
  505. }
  506. }
  507. void cache_clean_deferred(void *owner)
  508. {
  509. struct cache_deferred_req *dreq, *tmp;
  510. struct list_head pending;
  511. INIT_LIST_HEAD(&pending);
  512. spin_lock(&cache_defer_lock);
  513. list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
  514. if (dreq->owner == owner) {
  515. list_del_init(&dreq->hash);
  516. list_move(&dreq->recent, &pending);
  517. cache_defer_cnt--;
  518. }
  519. }
  520. spin_unlock(&cache_defer_lock);
  521. while (!list_empty(&pending)) {
  522. dreq = list_entry(pending.next, struct cache_deferred_req, recent);
  523. list_del_init(&dreq->recent);
  524. dreq->revisit(dreq, 1);
  525. }
  526. }
  527. /*
  528. * communicate with user-space
  529. *
  530. * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
  531. * On read, you get a full request, or block.
  532. * On write, an update request is processed.
  533. * Poll works if anything to read, and always allows write.
  534. *
  535. * Implemented by linked list of requests. Each open file has
  536. * a ->private that also exists in this list. New requests are added
  537. * to the end and may wakeup and preceding readers.
  538. * New readers are added to the head. If, on read, an item is found with
  539. * CACHE_UPCALLING clear, we free it from the list.
  540. *
  541. */
  542. static DEFINE_SPINLOCK(queue_lock);
  543. static DEFINE_MUTEX(queue_io_mutex);
  544. struct cache_queue {
  545. struct list_head list;
  546. int reader; /* if 0, then request */
  547. };
  548. struct cache_request {
  549. struct cache_queue q;
  550. struct cache_head *item;
  551. char * buf;
  552. int len;
  553. int readers;
  554. };
  555. struct cache_reader {
  556. struct cache_queue q;
  557. int offset; /* if non-0, we have a refcnt on next request */
  558. };
  559. static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
  560. loff_t *ppos, struct cache_detail *cd)
  561. {
  562. struct cache_reader *rp = filp->private_data;
  563. struct cache_request *rq;
  564. struct inode *inode = filp->f_path.dentry->d_inode;
  565. int err;
  566. if (count == 0)
  567. return 0;
  568. mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
  569. * readers on this file */
  570. again:
  571. spin_lock(&queue_lock);
  572. /* need to find next request */
  573. while (rp->q.list.next != &cd->queue &&
  574. list_entry(rp->q.list.next, struct cache_queue, list)
  575. ->reader) {
  576. struct list_head *next = rp->q.list.next;
  577. list_move(&rp->q.list, next);
  578. }
  579. if (rp->q.list.next == &cd->queue) {
  580. spin_unlock(&queue_lock);
  581. mutex_unlock(&inode->i_mutex);
  582. BUG_ON(rp->offset);
  583. return 0;
  584. }
  585. rq = container_of(rp->q.list.next, struct cache_request, q.list);
  586. BUG_ON(rq->q.reader);
  587. if (rp->offset == 0)
  588. rq->readers++;
  589. spin_unlock(&queue_lock);
  590. if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
  591. err = -EAGAIN;
  592. spin_lock(&queue_lock);
  593. list_move(&rp->q.list, &rq->q.list);
  594. spin_unlock(&queue_lock);
  595. } else {
  596. if (rp->offset + count > rq->len)
  597. count = rq->len - rp->offset;
  598. err = -EFAULT;
  599. if (copy_to_user(buf, rq->buf + rp->offset, count))
  600. goto out;
  601. rp->offset += count;
  602. if (rp->offset >= rq->len) {
  603. rp->offset = 0;
  604. spin_lock(&queue_lock);
  605. list_move(&rp->q.list, &rq->q.list);
  606. spin_unlock(&queue_lock);
  607. }
  608. err = 0;
  609. }
  610. out:
  611. if (rp->offset == 0) {
  612. /* need to release rq */
  613. spin_lock(&queue_lock);
  614. rq->readers--;
  615. if (rq->readers == 0 &&
  616. !test_bit(CACHE_PENDING, &rq->item->flags)) {
  617. list_del(&rq->q.list);
  618. spin_unlock(&queue_lock);
  619. cache_put(rq->item, cd);
  620. kfree(rq->buf);
  621. kfree(rq);
  622. } else
  623. spin_unlock(&queue_lock);
  624. }
  625. if (err == -EAGAIN)
  626. goto again;
  627. mutex_unlock(&inode->i_mutex);
  628. return err ? err : count;
  629. }
  630. static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
  631. size_t count, struct cache_detail *cd)
  632. {
  633. ssize_t ret;
  634. if (copy_from_user(kaddr, buf, count))
  635. return -EFAULT;
  636. kaddr[count] = '\0';
  637. ret = cd->cache_parse(cd, kaddr, count);
  638. if (!ret)
  639. ret = count;
  640. return ret;
  641. }
  642. static ssize_t cache_slow_downcall(const char __user *buf,
  643. size_t count, struct cache_detail *cd)
  644. {
  645. static char write_buf[8192]; /* protected by queue_io_mutex */
  646. ssize_t ret = -EINVAL;
  647. if (count >= sizeof(write_buf))
  648. goto out;
  649. mutex_lock(&queue_io_mutex);
  650. ret = cache_do_downcall(write_buf, buf, count, cd);
  651. mutex_unlock(&queue_io_mutex);
  652. out:
  653. return ret;
  654. }
  655. static ssize_t cache_downcall(struct address_space *mapping,
  656. const char __user *buf,
  657. size_t count, struct cache_detail *cd)
  658. {
  659. struct page *page;
  660. char *kaddr;
  661. ssize_t ret = -ENOMEM;
  662. if (count >= PAGE_CACHE_SIZE)
  663. goto out_slow;
  664. page = find_or_create_page(mapping, 0, GFP_KERNEL);
  665. if (!page)
  666. goto out_slow;
  667. kaddr = kmap(page);
  668. ret = cache_do_downcall(kaddr, buf, count, cd);
  669. kunmap(page);
  670. unlock_page(page);
  671. page_cache_release(page);
  672. return ret;
  673. out_slow:
  674. return cache_slow_downcall(buf, count, cd);
  675. }
  676. static ssize_t cache_write(struct file *filp, const char __user *buf,
  677. size_t count, loff_t *ppos,
  678. struct cache_detail *cd)
  679. {
  680. struct address_space *mapping = filp->f_mapping;
  681. struct inode *inode = filp->f_path.dentry->d_inode;
  682. ssize_t ret = -EINVAL;
  683. if (!cd->cache_parse)
  684. goto out;
  685. mutex_lock(&inode->i_mutex);
  686. ret = cache_downcall(mapping, buf, count, cd);
  687. mutex_unlock(&inode->i_mutex);
  688. out:
  689. return ret;
  690. }
  691. static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
  692. static unsigned int cache_poll(struct file *filp, poll_table *wait,
  693. struct cache_detail *cd)
  694. {
  695. unsigned int mask;
  696. struct cache_reader *rp = filp->private_data;
  697. struct cache_queue *cq;
  698. poll_wait(filp, &queue_wait, wait);
  699. /* alway allow write */
  700. mask = POLL_OUT | POLLWRNORM;
  701. if (!rp)
  702. return mask;
  703. spin_lock(&queue_lock);
  704. for (cq= &rp->q; &cq->list != &cd->queue;
  705. cq = list_entry(cq->list.next, struct cache_queue, list))
  706. if (!cq->reader) {
  707. mask |= POLLIN | POLLRDNORM;
  708. break;
  709. }
  710. spin_unlock(&queue_lock);
  711. return mask;
  712. }
  713. static int cache_ioctl(struct inode *ino, struct file *filp,
  714. unsigned int cmd, unsigned long arg,
  715. struct cache_detail *cd)
  716. {
  717. int len = 0;
  718. struct cache_reader *rp = filp->private_data;
  719. struct cache_queue *cq;
  720. if (cmd != FIONREAD || !rp)
  721. return -EINVAL;
  722. spin_lock(&queue_lock);
  723. /* only find the length remaining in current request,
  724. * or the length of the next request
  725. */
  726. for (cq= &rp->q; &cq->list != &cd->queue;
  727. cq = list_entry(cq->list.next, struct cache_queue, list))
  728. if (!cq->reader) {
  729. struct cache_request *cr =
  730. container_of(cq, struct cache_request, q);
  731. len = cr->len - rp->offset;
  732. break;
  733. }
  734. spin_unlock(&queue_lock);
  735. return put_user(len, (int __user *)arg);
  736. }
  737. static int cache_open(struct inode *inode, struct file *filp,
  738. struct cache_detail *cd)
  739. {
  740. struct cache_reader *rp = NULL;
  741. if (!cd || !try_module_get(cd->owner))
  742. return -EACCES;
  743. nonseekable_open(inode, filp);
  744. if (filp->f_mode & FMODE_READ) {
  745. rp = kmalloc(sizeof(*rp), GFP_KERNEL);
  746. if (!rp)
  747. return -ENOMEM;
  748. rp->offset = 0;
  749. rp->q.reader = 1;
  750. atomic_inc(&cd->readers);
  751. spin_lock(&queue_lock);
  752. list_add(&rp->q.list, &cd->queue);
  753. spin_unlock(&queue_lock);
  754. }
  755. filp->private_data = rp;
  756. return 0;
  757. }
  758. static int cache_release(struct inode *inode, struct file *filp,
  759. struct cache_detail *cd)
  760. {
  761. struct cache_reader *rp = filp->private_data;
  762. if (rp) {
  763. spin_lock(&queue_lock);
  764. if (rp->offset) {
  765. struct cache_queue *cq;
  766. for (cq= &rp->q; &cq->list != &cd->queue;
  767. cq = list_entry(cq->list.next, struct cache_queue, list))
  768. if (!cq->reader) {
  769. container_of(cq, struct cache_request, q)
  770. ->readers--;
  771. break;
  772. }
  773. rp->offset = 0;
  774. }
  775. list_del(&rp->q.list);
  776. spin_unlock(&queue_lock);
  777. filp->private_data = NULL;
  778. kfree(rp);
  779. cd->last_close = get_seconds();
  780. atomic_dec(&cd->readers);
  781. }
  782. module_put(cd->owner);
  783. return 0;
  784. }
  785. static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
  786. {
  787. struct cache_queue *cq;
  788. spin_lock(&queue_lock);
  789. list_for_each_entry(cq, &detail->queue, list)
  790. if (!cq->reader) {
  791. struct cache_request *cr = container_of(cq, struct cache_request, q);
  792. if (cr->item != ch)
  793. continue;
  794. if (cr->readers != 0)
  795. continue;
  796. list_del(&cr->q.list);
  797. spin_unlock(&queue_lock);
  798. cache_put(cr->item, detail);
  799. kfree(cr->buf);
  800. kfree(cr);
  801. return;
  802. }
  803. spin_unlock(&queue_lock);
  804. }
  805. /*
  806. * Support routines for text-based upcalls.
  807. * Fields are separated by spaces.
  808. * Fields are either mangled to quote space tab newline slosh with slosh
  809. * or a hexified with a leading \x
  810. * Record is terminated with newline.
  811. *
  812. */
  813. void qword_add(char **bpp, int *lp, char *str)
  814. {
  815. char *bp = *bpp;
  816. int len = *lp;
  817. char c;
  818. if (len < 0) return;
  819. while ((c=*str++) && len)
  820. switch(c) {
  821. case ' ':
  822. case '\t':
  823. case '\n':
  824. case '\\':
  825. if (len >= 4) {
  826. *bp++ = '\\';
  827. *bp++ = '0' + ((c & 0300)>>6);
  828. *bp++ = '0' + ((c & 0070)>>3);
  829. *bp++ = '0' + ((c & 0007)>>0);
  830. }
  831. len -= 4;
  832. break;
  833. default:
  834. *bp++ = c;
  835. len--;
  836. }
  837. if (c || len <1) len = -1;
  838. else {
  839. *bp++ = ' ';
  840. len--;
  841. }
  842. *bpp = bp;
  843. *lp = len;
  844. }
  845. EXPORT_SYMBOL_GPL(qword_add);
  846. void qword_addhex(char **bpp, int *lp, char *buf, int blen)
  847. {
  848. char *bp = *bpp;
  849. int len = *lp;
  850. if (len < 0) return;
  851. if (len > 2) {
  852. *bp++ = '\\';
  853. *bp++ = 'x';
  854. len -= 2;
  855. while (blen && len >= 2) {
  856. unsigned char c = *buf++;
  857. *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
  858. *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
  859. len -= 2;
  860. blen--;
  861. }
  862. }
  863. if (blen || len<1) len = -1;
  864. else {
  865. *bp++ = ' ';
  866. len--;
  867. }
  868. *bpp = bp;
  869. *lp = len;
  870. }
  871. EXPORT_SYMBOL_GPL(qword_addhex);
  872. static void warn_no_listener(struct cache_detail *detail)
  873. {
  874. if (detail->last_warn != detail->last_close) {
  875. detail->last_warn = detail->last_close;
  876. if (detail->warn_no_listener)
  877. detail->warn_no_listener(detail, detail->last_close != 0);
  878. }
  879. }
  880. /*
  881. * register an upcall request to user-space and queue it up for read() by the
  882. * upcall daemon.
  883. *
  884. * Each request is at most one page long.
  885. */
  886. int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h,
  887. void (*cache_request)(struct cache_detail *,
  888. struct cache_head *,
  889. char **,
  890. int *))
  891. {
  892. char *buf;
  893. struct cache_request *crq;
  894. char *bp;
  895. int len;
  896. if (atomic_read(&detail->readers) == 0 &&
  897. detail->last_close < get_seconds() - 30) {
  898. warn_no_listener(detail);
  899. return -EINVAL;
  900. }
  901. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  902. if (!buf)
  903. return -EAGAIN;
  904. crq = kmalloc(sizeof (*crq), GFP_KERNEL);
  905. if (!crq) {
  906. kfree(buf);
  907. return -EAGAIN;
  908. }
  909. bp = buf; len = PAGE_SIZE;
  910. cache_request(detail, h, &bp, &len);
  911. if (len < 0) {
  912. kfree(buf);
  913. kfree(crq);
  914. return -EAGAIN;
  915. }
  916. crq->q.reader = 0;
  917. crq->item = cache_get(h);
  918. crq->buf = buf;
  919. crq->len = PAGE_SIZE - len;
  920. crq->readers = 0;
  921. spin_lock(&queue_lock);
  922. list_add_tail(&crq->q.list, &detail->queue);
  923. spin_unlock(&queue_lock);
  924. wake_up(&queue_wait);
  925. return 0;
  926. }
  927. EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
  928. /*
  929. * parse a message from user-space and pass it
  930. * to an appropriate cache
  931. * Messages are, like requests, separated into fields by
  932. * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
  933. *
  934. * Message is
  935. * reply cachename expiry key ... content....
  936. *
  937. * key and content are both parsed by cache
  938. */
  939. #define isodigit(c) (isdigit(c) && c <= '7')
  940. int qword_get(char **bpp, char *dest, int bufsize)
  941. {
  942. /* return bytes copied, or -1 on error */
  943. char *bp = *bpp;
  944. int len = 0;
  945. while (*bp == ' ') bp++;
  946. if (bp[0] == '\\' && bp[1] == 'x') {
  947. /* HEX STRING */
  948. bp += 2;
  949. while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
  950. int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
  951. bp++;
  952. byte <<= 4;
  953. byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
  954. *dest++ = byte;
  955. bp++;
  956. len++;
  957. }
  958. } else {
  959. /* text with \nnn octal quoting */
  960. while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
  961. if (*bp == '\\' &&
  962. isodigit(bp[1]) && (bp[1] <= '3') &&
  963. isodigit(bp[2]) &&
  964. isodigit(bp[3])) {
  965. int byte = (*++bp -'0');
  966. bp++;
  967. byte = (byte << 3) | (*bp++ - '0');
  968. byte = (byte << 3) | (*bp++ - '0');
  969. *dest++ = byte;
  970. len++;
  971. } else {
  972. *dest++ = *bp++;
  973. len++;
  974. }
  975. }
  976. }
  977. if (*bp != ' ' && *bp != '\n' && *bp != '\0')
  978. return -1;
  979. while (*bp == ' ') bp++;
  980. *bpp = bp;
  981. *dest = '\0';
  982. return len;
  983. }
  984. EXPORT_SYMBOL_GPL(qword_get);
  985. /*
  986. * support /proc/sunrpc/cache/$CACHENAME/content
  987. * as a seqfile.
  988. * We call ->cache_show passing NULL for the item to
  989. * get a header, then pass each real item in the cache
  990. */
  991. struct handle {
  992. struct cache_detail *cd;
  993. };
  994. static void *c_start(struct seq_file *m, loff_t *pos)
  995. __acquires(cd->hash_lock)
  996. {
  997. loff_t n = *pos;
  998. unsigned hash, entry;
  999. struct cache_head *ch;
  1000. struct cache_detail *cd = ((struct handle*)m->private)->cd;
  1001. read_lock(&cd->hash_lock);
  1002. if (!n--)
  1003. return SEQ_START_TOKEN;
  1004. hash = n >> 32;
  1005. entry = n & ((1LL<<32) - 1);
  1006. for (ch=cd->hash_table[hash]; ch; ch=ch->next)
  1007. if (!entry--)
  1008. return ch;
  1009. n &= ~((1LL<<32) - 1);
  1010. do {
  1011. hash++;
  1012. n += 1LL<<32;
  1013. } while(hash < cd->hash_size &&
  1014. cd->hash_table[hash]==NULL);
  1015. if (hash >= cd->hash_size)
  1016. return NULL;
  1017. *pos = n+1;
  1018. return cd->hash_table[hash];
  1019. }
  1020. static void *c_next(struct seq_file *m, void *p, loff_t *pos)
  1021. {
  1022. struct cache_head *ch = p;
  1023. int hash = (*pos >> 32);
  1024. struct cache_detail *cd = ((struct handle*)m->private)->cd;
  1025. if (p == SEQ_START_TOKEN)
  1026. hash = 0;
  1027. else if (ch->next == NULL) {
  1028. hash++;
  1029. *pos += 1LL<<32;
  1030. } else {
  1031. ++*pos;
  1032. return ch->next;
  1033. }
  1034. *pos &= ~((1LL<<32) - 1);
  1035. while (hash < cd->hash_size &&
  1036. cd->hash_table[hash] == NULL) {
  1037. hash++;
  1038. *pos += 1LL<<32;
  1039. }
  1040. if (hash >= cd->hash_size)
  1041. return NULL;
  1042. ++*pos;
  1043. return cd->hash_table[hash];
  1044. }
  1045. static void c_stop(struct seq_file *m, void *p)
  1046. __releases(cd->hash_lock)
  1047. {
  1048. struct cache_detail *cd = ((struct handle*)m->private)->cd;
  1049. read_unlock(&cd->hash_lock);
  1050. }
  1051. static int c_show(struct seq_file *m, void *p)
  1052. {
  1053. struct cache_head *cp = p;
  1054. struct cache_detail *cd = ((struct handle*)m->private)->cd;
  1055. if (p == SEQ_START_TOKEN)
  1056. return cd->cache_show(m, cd, NULL);
  1057. ifdebug(CACHE)
  1058. seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
  1059. cp->expiry_time, atomic_read(&cp->ref.refcount), cp->flags);
  1060. cache_get(cp);
  1061. if (cache_check(cd, cp, NULL))
  1062. /* cache_check does a cache_put on failure */
  1063. seq_printf(m, "# ");
  1064. else
  1065. cache_put(cp, cd);
  1066. return cd->cache_show(m, cd, cp);
  1067. }
  1068. static const struct seq_operations cache_content_op = {
  1069. .start = c_start,
  1070. .next = c_next,
  1071. .stop = c_stop,
  1072. .show = c_show,
  1073. };
  1074. static int content_open(struct inode *inode, struct file *file,
  1075. struct cache_detail *cd)
  1076. {
  1077. struct handle *han;
  1078. if (!cd || !try_module_get(cd->owner))
  1079. return -EACCES;
  1080. han = __seq_open_private(file, &cache_content_op, sizeof(*han));
  1081. if (han == NULL)
  1082. return -ENOMEM;
  1083. han->cd = cd;
  1084. return 0;
  1085. }
  1086. static int content_release(struct inode *inode, struct file *file,
  1087. struct cache_detail *cd)
  1088. {
  1089. int ret = seq_release_private(inode, file);
  1090. module_put(cd->owner);
  1091. return ret;
  1092. }
  1093. static int open_flush(struct inode *inode, struct file *file,
  1094. struct cache_detail *cd)
  1095. {
  1096. if (!cd || !try_module_get(cd->owner))
  1097. return -EACCES;
  1098. return nonseekable_open(inode, file);
  1099. }
  1100. static int release_flush(struct inode *inode, struct file *file,
  1101. struct cache_detail *cd)
  1102. {
  1103. module_put(cd->owner);
  1104. return 0;
  1105. }
  1106. static ssize_t read_flush(struct file *file, char __user *buf,
  1107. size_t count, loff_t *ppos,
  1108. struct cache_detail *cd)
  1109. {
  1110. char tbuf[20];
  1111. unsigned long p = *ppos;
  1112. size_t len;
  1113. sprintf(tbuf, "%lu\n", cd->flush_time);
  1114. len = strlen(tbuf);
  1115. if (p >= len)
  1116. return 0;
  1117. len -= p;
  1118. if (len > count)
  1119. len = count;
  1120. if (copy_to_user(buf, (void*)(tbuf+p), len))
  1121. return -EFAULT;
  1122. *ppos += len;
  1123. return len;
  1124. }
  1125. static ssize_t write_flush(struct file *file, const char __user *buf,
  1126. size_t count, loff_t *ppos,
  1127. struct cache_detail *cd)
  1128. {
  1129. char tbuf[20];
  1130. char *ep;
  1131. long flushtime;
  1132. if (*ppos || count > sizeof(tbuf)-1)
  1133. return -EINVAL;
  1134. if (copy_from_user(tbuf, buf, count))
  1135. return -EFAULT;
  1136. tbuf[count] = 0;
  1137. flushtime = simple_strtoul(tbuf, &ep, 0);
  1138. if (*ep && *ep != '\n')
  1139. return -EINVAL;
  1140. cd->flush_time = flushtime;
  1141. cd->nextcheck = get_seconds();
  1142. cache_flush();
  1143. *ppos += count;
  1144. return count;
  1145. }
  1146. static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
  1147. size_t count, loff_t *ppos)
  1148. {
  1149. struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
  1150. return cache_read(filp, buf, count, ppos, cd);
  1151. }
  1152. static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
  1153. size_t count, loff_t *ppos)
  1154. {
  1155. struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
  1156. return cache_write(filp, buf, count, ppos, cd);
  1157. }
  1158. static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
  1159. {
  1160. struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
  1161. return cache_poll(filp, wait, cd);
  1162. }
  1163. static int cache_ioctl_procfs(struct inode *inode, struct file *filp,
  1164. unsigned int cmd, unsigned long arg)
  1165. {
  1166. struct cache_detail *cd = PDE(inode)->data;
  1167. return cache_ioctl(inode, filp, cmd, arg, cd);
  1168. }
  1169. static int cache_open_procfs(struct inode *inode, struct file *filp)
  1170. {
  1171. struct cache_detail *cd = PDE(inode)->data;
  1172. return cache_open(inode, filp, cd);
  1173. }
  1174. static int cache_release_procfs(struct inode *inode, struct file *filp)
  1175. {
  1176. struct cache_detail *cd = PDE(inode)->data;
  1177. return cache_release(inode, filp, cd);
  1178. }
  1179. static const struct file_operations cache_file_operations_procfs = {
  1180. .owner = THIS_MODULE,
  1181. .llseek = no_llseek,
  1182. .read = cache_read_procfs,
  1183. .write = cache_write_procfs,
  1184. .poll = cache_poll_procfs,
  1185. .ioctl = cache_ioctl_procfs, /* for FIONREAD */
  1186. .open = cache_open_procfs,
  1187. .release = cache_release_procfs,
  1188. };
  1189. static int content_open_procfs(struct inode *inode, struct file *filp)
  1190. {
  1191. struct cache_detail *cd = PDE(inode)->data;
  1192. return content_open(inode, filp, cd);
  1193. }
  1194. static int content_release_procfs(struct inode *inode, struct file *filp)
  1195. {
  1196. struct cache_detail *cd = PDE(inode)->data;
  1197. return content_release(inode, filp, cd);
  1198. }
  1199. static const struct file_operations content_file_operations_procfs = {
  1200. .open = content_open_procfs,
  1201. .read = seq_read,
  1202. .llseek = seq_lseek,
  1203. .release = content_release_procfs,
  1204. };
  1205. static int open_flush_procfs(struct inode *inode, struct file *filp)
  1206. {
  1207. struct cache_detail *cd = PDE(inode)->data;
  1208. return open_flush(inode, filp, cd);
  1209. }
  1210. static int release_flush_procfs(struct inode *inode, struct file *filp)
  1211. {
  1212. struct cache_detail *cd = PDE(inode)->data;
  1213. return release_flush(inode, filp, cd);
  1214. }
  1215. static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
  1216. size_t count, loff_t *ppos)
  1217. {
  1218. struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
  1219. return read_flush(filp, buf, count, ppos, cd);
  1220. }
  1221. static ssize_t write_flush_procfs(struct file *filp,
  1222. const char __user *buf,
  1223. size_t count, loff_t *ppos)
  1224. {
  1225. struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
  1226. return write_flush(filp, buf, count, ppos, cd);
  1227. }
  1228. static const struct file_operations cache_flush_operations_procfs = {
  1229. .open = open_flush_procfs,
  1230. .read = read_flush_procfs,
  1231. .write = write_flush_procfs,
  1232. .release = release_flush_procfs,
  1233. };
  1234. static void remove_cache_proc_entries(struct cache_detail *cd)
  1235. {
  1236. if (cd->u.procfs.proc_ent == NULL)
  1237. return;
  1238. if (cd->u.procfs.flush_ent)
  1239. remove_proc_entry("flush", cd->u.procfs.proc_ent);
  1240. if (cd->u.procfs.channel_ent)
  1241. remove_proc_entry("channel", cd->u.procfs.proc_ent);
  1242. if (cd->u.procfs.content_ent)
  1243. remove_proc_entry("content", cd->u.procfs.proc_ent);
  1244. cd->u.procfs.proc_ent = NULL;
  1245. remove_proc_entry(cd->name, proc_net_rpc);
  1246. }
  1247. #ifdef CONFIG_PROC_FS
  1248. static int create_cache_proc_entries(struct cache_detail *cd)
  1249. {
  1250. struct proc_dir_entry *p;
  1251. cd->u.procfs.proc_ent = proc_mkdir(cd->name, proc_net_rpc);
  1252. if (cd->u.procfs.proc_ent == NULL)
  1253. goto out_nomem;
  1254. cd->u.procfs.channel_ent = NULL;
  1255. cd->u.procfs.content_ent = NULL;
  1256. p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
  1257. cd->u.procfs.proc_ent,
  1258. &cache_flush_operations_procfs, cd);
  1259. cd->u.procfs.flush_ent = p;
  1260. if (p == NULL)
  1261. goto out_nomem;
  1262. if (cd->cache_upcall || cd->cache_parse) {
  1263. p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
  1264. cd->u.procfs.proc_ent,
  1265. &cache_file_operations_procfs, cd);
  1266. cd->u.procfs.channel_ent = p;
  1267. if (p == NULL)
  1268. goto out_nomem;
  1269. }
  1270. if (cd->cache_show) {
  1271. p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR,
  1272. cd->u.procfs.proc_ent,
  1273. &content_file_operations_procfs, cd);
  1274. cd->u.procfs.content_ent = p;
  1275. if (p == NULL)
  1276. goto out_nomem;
  1277. }
  1278. return 0;
  1279. out_nomem:
  1280. remove_cache_proc_entries(cd);
  1281. return -ENOMEM;
  1282. }
  1283. #else /* CONFIG_PROC_FS */
  1284. static int create_cache_proc_entries(struct cache_detail *cd)
  1285. {
  1286. return 0;
  1287. }
  1288. #endif
  1289. int cache_register(struct cache_detail *cd)
  1290. {
  1291. int ret;
  1292. sunrpc_init_cache_detail(cd);
  1293. ret = create_cache_proc_entries(cd);
  1294. if (ret)
  1295. sunrpc_destroy_cache_detail(cd);
  1296. return ret;
  1297. }
  1298. EXPORT_SYMBOL_GPL(cache_register);
  1299. void cache_unregister(struct cache_detail *cd)
  1300. {
  1301. remove_cache_proc_entries(cd);
  1302. sunrpc_destroy_cache_detail(cd);
  1303. }
  1304. EXPORT_SYMBOL_GPL(cache_unregister);
  1305. static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
  1306. size_t count, loff_t *ppos)
  1307. {
  1308. struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
  1309. return cache_read(filp, buf, count, ppos, cd);
  1310. }
  1311. static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
  1312. size_t count, loff_t *ppos)
  1313. {
  1314. struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
  1315. return cache_write(filp, buf, count, ppos, cd);
  1316. }
  1317. static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
  1318. {
  1319. struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
  1320. return cache_poll(filp, wait, cd);
  1321. }
  1322. static int cache_ioctl_pipefs(struct inode *inode, struct file *filp,
  1323. unsigned int cmd, unsigned long arg)
  1324. {
  1325. struct cache_detail *cd = RPC_I(inode)->private;
  1326. return cache_ioctl(inode, filp, cmd, arg, cd);
  1327. }
  1328. static int cache_open_pipefs(struct inode *inode, struct file *filp)
  1329. {
  1330. struct cache_detail *cd = RPC_I(inode)->private;
  1331. return cache_open(inode, filp, cd);
  1332. }
  1333. static int cache_release_pipefs(struct inode *inode, struct file *filp)
  1334. {
  1335. struct cache_detail *cd = RPC_I(inode)->private;
  1336. return cache_release(inode, filp, cd);
  1337. }
  1338. const struct file_operations cache_file_operations_pipefs = {
  1339. .owner = THIS_MODULE,
  1340. .llseek = no_llseek,
  1341. .read = cache_read_pipefs,
  1342. .write = cache_write_pipefs,
  1343. .poll = cache_poll_pipefs,
  1344. .ioctl = cache_ioctl_pipefs, /* for FIONREAD */
  1345. .open = cache_open_pipefs,
  1346. .release = cache_release_pipefs,
  1347. };
  1348. static int content_open_pipefs(struct inode *inode, struct file *filp)
  1349. {
  1350. struct cache_detail *cd = RPC_I(inode)->private;
  1351. return content_open(inode, filp, cd);
  1352. }
  1353. static int content_release_pipefs(struct inode *inode, struct file *filp)
  1354. {
  1355. struct cache_detail *cd = RPC_I(inode)->private;
  1356. return content_release(inode, filp, cd);
  1357. }
  1358. const struct file_operations content_file_operations_pipefs = {
  1359. .open = content_open_pipefs,
  1360. .read = seq_read,
  1361. .llseek = seq_lseek,
  1362. .release = content_release_pipefs,
  1363. };
  1364. static int open_flush_pipefs(struct inode *inode, struct file *filp)
  1365. {
  1366. struct cache_detail *cd = RPC_I(inode)->private;
  1367. return open_flush(inode, filp, cd);
  1368. }
  1369. static int release_flush_pipefs(struct inode *inode, struct file *filp)
  1370. {
  1371. struct cache_detail *cd = RPC_I(inode)->private;
  1372. return release_flush(inode, filp, cd);
  1373. }
  1374. static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
  1375. size_t count, loff_t *ppos)
  1376. {
  1377. struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
  1378. return read_flush(filp, buf, count, ppos, cd);
  1379. }
  1380. static ssize_t write_flush_pipefs(struct file *filp,
  1381. const char __user *buf,
  1382. size_t count, loff_t *ppos)
  1383. {
  1384. struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
  1385. return write_flush(filp, buf, count, ppos, cd);
  1386. }
  1387. const struct file_operations cache_flush_operations_pipefs = {
  1388. .open = open_flush_pipefs,
  1389. .read = read_flush_pipefs,
  1390. .write = write_flush_pipefs,
  1391. .release = release_flush_pipefs,
  1392. };
  1393. int sunrpc_cache_register_pipefs(struct dentry *parent,
  1394. const char *name, mode_t umode,
  1395. struct cache_detail *cd)
  1396. {
  1397. struct qstr q;
  1398. struct dentry *dir;
  1399. int ret = 0;
  1400. sunrpc_init_cache_detail(cd);
  1401. q.name = name;
  1402. q.len = strlen(name);
  1403. q.hash = full_name_hash(q.name, q.len);
  1404. dir = rpc_create_cache_dir(parent, &q, umode, cd);
  1405. if (!IS_ERR(dir))
  1406. cd->u.pipefs.dir = dir;
  1407. else {
  1408. sunrpc_destroy_cache_detail(cd);
  1409. ret = PTR_ERR(dir);
  1410. }
  1411. return ret;
  1412. }
  1413. EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
  1414. void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
  1415. {
  1416. rpc_remove_cache_dir(cd->u.pipefs.dir);
  1417. cd->u.pipefs.dir = NULL;
  1418. sunrpc_destroy_cache_detail(cd);
  1419. }
  1420. EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);