cache.c 37 KB

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