cache.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638
  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. if (!cd || !try_module_get(cd->owner))
  735. return -EACCES;
  736. nonseekable_open(inode, filp);
  737. if (filp->f_mode & FMODE_READ) {
  738. rp = kmalloc(sizeof(*rp), GFP_KERNEL);
  739. if (!rp)
  740. return -ENOMEM;
  741. rp->offset = 0;
  742. rp->q.reader = 1;
  743. atomic_inc(&cd->readers);
  744. spin_lock(&queue_lock);
  745. list_add(&rp->q.list, &cd->queue);
  746. spin_unlock(&queue_lock);
  747. }
  748. filp->private_data = rp;
  749. return 0;
  750. }
  751. static int cache_release(struct inode *inode, struct file *filp,
  752. struct cache_detail *cd)
  753. {
  754. struct cache_reader *rp = filp->private_data;
  755. if (rp) {
  756. spin_lock(&queue_lock);
  757. if (rp->offset) {
  758. struct cache_queue *cq;
  759. for (cq= &rp->q; &cq->list != &cd->queue;
  760. cq = list_entry(cq->list.next, struct cache_queue, list))
  761. if (!cq->reader) {
  762. container_of(cq, struct cache_request, q)
  763. ->readers--;
  764. break;
  765. }
  766. rp->offset = 0;
  767. }
  768. list_del(&rp->q.list);
  769. spin_unlock(&queue_lock);
  770. filp->private_data = NULL;
  771. kfree(rp);
  772. cd->last_close = get_seconds();
  773. atomic_dec(&cd->readers);
  774. }
  775. module_put(cd->owner);
  776. return 0;
  777. }
  778. static void queue_loose(struct cache_detail *detail, struct cache_head *ch)
  779. {
  780. struct cache_queue *cq;
  781. spin_lock(&queue_lock);
  782. list_for_each_entry(cq, &detail->queue, list)
  783. if (!cq->reader) {
  784. struct cache_request *cr = container_of(cq, struct cache_request, q);
  785. if (cr->item != ch)
  786. continue;
  787. if (cr->readers != 0)
  788. continue;
  789. list_del(&cr->q.list);
  790. spin_unlock(&queue_lock);
  791. cache_put(cr->item, detail);
  792. kfree(cr->buf);
  793. kfree(cr);
  794. return;
  795. }
  796. spin_unlock(&queue_lock);
  797. }
  798. /*
  799. * Support routines for text-based upcalls.
  800. * Fields are separated by spaces.
  801. * Fields are either mangled to quote space tab newline slosh with slosh
  802. * or a hexified with a leading \x
  803. * Record is terminated with newline.
  804. *
  805. */
  806. void qword_add(char **bpp, int *lp, char *str)
  807. {
  808. char *bp = *bpp;
  809. int len = *lp;
  810. char c;
  811. if (len < 0) return;
  812. while ((c=*str++) && len)
  813. switch(c) {
  814. case ' ':
  815. case '\t':
  816. case '\n':
  817. case '\\':
  818. if (len >= 4) {
  819. *bp++ = '\\';
  820. *bp++ = '0' + ((c & 0300)>>6);
  821. *bp++ = '0' + ((c & 0070)>>3);
  822. *bp++ = '0' + ((c & 0007)>>0);
  823. }
  824. len -= 4;
  825. break;
  826. default:
  827. *bp++ = c;
  828. len--;
  829. }
  830. if (c || len <1) len = -1;
  831. else {
  832. *bp++ = ' ';
  833. len--;
  834. }
  835. *bpp = bp;
  836. *lp = len;
  837. }
  838. EXPORT_SYMBOL_GPL(qword_add);
  839. void qword_addhex(char **bpp, int *lp, char *buf, int blen)
  840. {
  841. char *bp = *bpp;
  842. int len = *lp;
  843. if (len < 0) return;
  844. if (len > 2) {
  845. *bp++ = '\\';
  846. *bp++ = 'x';
  847. len -= 2;
  848. while (blen && len >= 2) {
  849. unsigned char c = *buf++;
  850. *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
  851. *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
  852. len -= 2;
  853. blen--;
  854. }
  855. }
  856. if (blen || len<1) len = -1;
  857. else {
  858. *bp++ = ' ';
  859. len--;
  860. }
  861. *bpp = bp;
  862. *lp = len;
  863. }
  864. EXPORT_SYMBOL_GPL(qword_addhex);
  865. static void warn_no_listener(struct cache_detail *detail)
  866. {
  867. if (detail->last_warn != detail->last_close) {
  868. detail->last_warn = detail->last_close;
  869. if (detail->warn_no_listener)
  870. detail->warn_no_listener(detail, detail->last_close != 0);
  871. }
  872. }
  873. /*
  874. * register an upcall request to user-space and queue it up for read() by the
  875. * upcall daemon.
  876. *
  877. * Each request is at most one page long.
  878. */
  879. int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h,
  880. void (*cache_request)(struct cache_detail *,
  881. struct cache_head *,
  882. char **,
  883. int *))
  884. {
  885. char *buf;
  886. struct cache_request *crq;
  887. char *bp;
  888. int len;
  889. if (atomic_read(&detail->readers) == 0 &&
  890. detail->last_close < get_seconds() - 30) {
  891. warn_no_listener(detail);
  892. return -EINVAL;
  893. }
  894. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  895. if (!buf)
  896. return -EAGAIN;
  897. crq = kmalloc(sizeof (*crq), GFP_KERNEL);
  898. if (!crq) {
  899. kfree(buf);
  900. return -EAGAIN;
  901. }
  902. bp = buf; len = PAGE_SIZE;
  903. cache_request(detail, h, &bp, &len);
  904. if (len < 0) {
  905. kfree(buf);
  906. kfree(crq);
  907. return -EAGAIN;
  908. }
  909. crq->q.reader = 0;
  910. crq->item = cache_get(h);
  911. crq->buf = buf;
  912. crq->len = PAGE_SIZE - len;
  913. crq->readers = 0;
  914. spin_lock(&queue_lock);
  915. list_add_tail(&crq->q.list, &detail->queue);
  916. spin_unlock(&queue_lock);
  917. wake_up(&queue_wait);
  918. return 0;
  919. }
  920. EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
  921. /*
  922. * parse a message from user-space and pass it
  923. * to an appropriate cache
  924. * Messages are, like requests, separated into fields by
  925. * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
  926. *
  927. * Message is
  928. * reply cachename expiry key ... content....
  929. *
  930. * key and content are both parsed by cache
  931. */
  932. #define isodigit(c) (isdigit(c) && c <= '7')
  933. int qword_get(char **bpp, char *dest, int bufsize)
  934. {
  935. /* return bytes copied, or -1 on error */
  936. char *bp = *bpp;
  937. int len = 0;
  938. while (*bp == ' ') bp++;
  939. if (bp[0] == '\\' && bp[1] == 'x') {
  940. /* HEX STRING */
  941. bp += 2;
  942. while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
  943. int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
  944. bp++;
  945. byte <<= 4;
  946. byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
  947. *dest++ = byte;
  948. bp++;
  949. len++;
  950. }
  951. } else {
  952. /* text with \nnn octal quoting */
  953. while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
  954. if (*bp == '\\' &&
  955. isodigit(bp[1]) && (bp[1] <= '3') &&
  956. isodigit(bp[2]) &&
  957. isodigit(bp[3])) {
  958. int byte = (*++bp -'0');
  959. bp++;
  960. byte = (byte << 3) | (*bp++ - '0');
  961. byte = (byte << 3) | (*bp++ - '0');
  962. *dest++ = byte;
  963. len++;
  964. } else {
  965. *dest++ = *bp++;
  966. len++;
  967. }
  968. }
  969. }
  970. if (*bp != ' ' && *bp != '\n' && *bp != '\0')
  971. return -1;
  972. while (*bp == ' ') bp++;
  973. *bpp = bp;
  974. *dest = '\0';
  975. return len;
  976. }
  977. EXPORT_SYMBOL_GPL(qword_get);
  978. /*
  979. * support /proc/sunrpc/cache/$CACHENAME/content
  980. * as a seqfile.
  981. * We call ->cache_show passing NULL for the item to
  982. * get a header, then pass each real item in the cache
  983. */
  984. struct handle {
  985. struct cache_detail *cd;
  986. };
  987. static void *c_start(struct seq_file *m, loff_t *pos)
  988. __acquires(cd->hash_lock)
  989. {
  990. loff_t n = *pos;
  991. unsigned hash, entry;
  992. struct cache_head *ch;
  993. struct cache_detail *cd = ((struct handle*)m->private)->cd;
  994. read_lock(&cd->hash_lock);
  995. if (!n--)
  996. return SEQ_START_TOKEN;
  997. hash = n >> 32;
  998. entry = n & ((1LL<<32) - 1);
  999. for (ch=cd->hash_table[hash]; ch; ch=ch->next)
  1000. if (!entry--)
  1001. return ch;
  1002. n &= ~((1LL<<32) - 1);
  1003. do {
  1004. hash++;
  1005. n += 1LL<<32;
  1006. } while(hash < cd->hash_size &&
  1007. cd->hash_table[hash]==NULL);
  1008. if (hash >= cd->hash_size)
  1009. return NULL;
  1010. *pos = n+1;
  1011. return cd->hash_table[hash];
  1012. }
  1013. static void *c_next(struct seq_file *m, void *p, loff_t *pos)
  1014. {
  1015. struct cache_head *ch = p;
  1016. int hash = (*pos >> 32);
  1017. struct cache_detail *cd = ((struct handle*)m->private)->cd;
  1018. if (p == SEQ_START_TOKEN)
  1019. hash = 0;
  1020. else if (ch->next == NULL) {
  1021. hash++;
  1022. *pos += 1LL<<32;
  1023. } else {
  1024. ++*pos;
  1025. return ch->next;
  1026. }
  1027. *pos &= ~((1LL<<32) - 1);
  1028. while (hash < cd->hash_size &&
  1029. cd->hash_table[hash] == NULL) {
  1030. hash++;
  1031. *pos += 1LL<<32;
  1032. }
  1033. if (hash >= cd->hash_size)
  1034. return NULL;
  1035. ++*pos;
  1036. return cd->hash_table[hash];
  1037. }
  1038. static void c_stop(struct seq_file *m, void *p)
  1039. __releases(cd->hash_lock)
  1040. {
  1041. struct cache_detail *cd = ((struct handle*)m->private)->cd;
  1042. read_unlock(&cd->hash_lock);
  1043. }
  1044. static int c_show(struct seq_file *m, void *p)
  1045. {
  1046. struct cache_head *cp = p;
  1047. struct cache_detail *cd = ((struct handle*)m->private)->cd;
  1048. if (p == SEQ_START_TOKEN)
  1049. return cd->cache_show(m, cd, NULL);
  1050. ifdebug(CACHE)
  1051. seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
  1052. cp->expiry_time, atomic_read(&cp->ref.refcount), cp->flags);
  1053. cache_get(cp);
  1054. if (cache_check(cd, cp, NULL))
  1055. /* cache_check does a cache_put on failure */
  1056. seq_printf(m, "# ");
  1057. else
  1058. cache_put(cp, cd);
  1059. return cd->cache_show(m, cd, cp);
  1060. }
  1061. static const struct seq_operations cache_content_op = {
  1062. .start = c_start,
  1063. .next = c_next,
  1064. .stop = c_stop,
  1065. .show = c_show,
  1066. };
  1067. static int content_open(struct inode *inode, struct file *file,
  1068. struct cache_detail *cd)
  1069. {
  1070. struct handle *han;
  1071. if (!cd || !try_module_get(cd->owner))
  1072. return -EACCES;
  1073. han = __seq_open_private(file, &cache_content_op, sizeof(*han));
  1074. if (han == NULL)
  1075. return -ENOMEM;
  1076. han->cd = cd;
  1077. return 0;
  1078. }
  1079. static int content_release(struct inode *inode, struct file *file,
  1080. struct cache_detail *cd)
  1081. {
  1082. int ret = seq_release_private(inode, file);
  1083. module_put(cd->owner);
  1084. return ret;
  1085. }
  1086. static int open_flush(struct inode *inode, struct file *file,
  1087. struct cache_detail *cd)
  1088. {
  1089. if (!cd || !try_module_get(cd->owner))
  1090. return -EACCES;
  1091. return nonseekable_open(inode, file);
  1092. }
  1093. static int release_flush(struct inode *inode, struct file *file,
  1094. struct cache_detail *cd)
  1095. {
  1096. module_put(cd->owner);
  1097. return 0;
  1098. }
  1099. static ssize_t read_flush(struct file *file, char __user *buf,
  1100. size_t count, loff_t *ppos,
  1101. struct cache_detail *cd)
  1102. {
  1103. char tbuf[20];
  1104. unsigned long p = *ppos;
  1105. size_t len;
  1106. sprintf(tbuf, "%lu\n", cd->flush_time);
  1107. len = strlen(tbuf);
  1108. if (p >= len)
  1109. return 0;
  1110. len -= p;
  1111. if (len > count)
  1112. len = count;
  1113. if (copy_to_user(buf, (void*)(tbuf+p), len))
  1114. return -EFAULT;
  1115. *ppos += len;
  1116. return len;
  1117. }
  1118. static ssize_t write_flush(struct file *file, const char __user *buf,
  1119. size_t count, loff_t *ppos,
  1120. struct cache_detail *cd)
  1121. {
  1122. char tbuf[20];
  1123. char *ep;
  1124. long flushtime;
  1125. if (*ppos || count > sizeof(tbuf)-1)
  1126. return -EINVAL;
  1127. if (copy_from_user(tbuf, buf, count))
  1128. return -EFAULT;
  1129. tbuf[count] = 0;
  1130. flushtime = simple_strtoul(tbuf, &ep, 0);
  1131. if (*ep && *ep != '\n')
  1132. return -EINVAL;
  1133. cd->flush_time = flushtime;
  1134. cd->nextcheck = get_seconds();
  1135. cache_flush();
  1136. *ppos += count;
  1137. return count;
  1138. }
  1139. static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
  1140. size_t count, loff_t *ppos)
  1141. {
  1142. struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
  1143. return cache_read(filp, buf, count, ppos, cd);
  1144. }
  1145. static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
  1146. size_t count, loff_t *ppos)
  1147. {
  1148. struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
  1149. return cache_write(filp, buf, count, ppos, cd);
  1150. }
  1151. static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
  1152. {
  1153. struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
  1154. return cache_poll(filp, wait, cd);
  1155. }
  1156. static int cache_ioctl_procfs(struct inode *inode, struct file *filp,
  1157. unsigned int cmd, unsigned long arg)
  1158. {
  1159. struct cache_detail *cd = PDE(inode)->data;
  1160. return cache_ioctl(inode, filp, cmd, arg, cd);
  1161. }
  1162. static int cache_open_procfs(struct inode *inode, struct file *filp)
  1163. {
  1164. struct cache_detail *cd = PDE(inode)->data;
  1165. return cache_open(inode, filp, cd);
  1166. }
  1167. static int cache_release_procfs(struct inode *inode, struct file *filp)
  1168. {
  1169. struct cache_detail *cd = PDE(inode)->data;
  1170. return cache_release(inode, filp, cd);
  1171. }
  1172. static const struct file_operations cache_file_operations_procfs = {
  1173. .owner = THIS_MODULE,
  1174. .llseek = no_llseek,
  1175. .read = cache_read_procfs,
  1176. .write = cache_write_procfs,
  1177. .poll = cache_poll_procfs,
  1178. .ioctl = cache_ioctl_procfs, /* for FIONREAD */
  1179. .open = cache_open_procfs,
  1180. .release = cache_release_procfs,
  1181. };
  1182. static int content_open_procfs(struct inode *inode, struct file *filp)
  1183. {
  1184. struct cache_detail *cd = PDE(inode)->data;
  1185. return content_open(inode, filp, cd);
  1186. }
  1187. static int content_release_procfs(struct inode *inode, struct file *filp)
  1188. {
  1189. struct cache_detail *cd = PDE(inode)->data;
  1190. return content_release(inode, filp, cd);
  1191. }
  1192. static const struct file_operations content_file_operations_procfs = {
  1193. .open = content_open_procfs,
  1194. .read = seq_read,
  1195. .llseek = seq_lseek,
  1196. .release = content_release_procfs,
  1197. };
  1198. static int open_flush_procfs(struct inode *inode, struct file *filp)
  1199. {
  1200. struct cache_detail *cd = PDE(inode)->data;
  1201. return open_flush(inode, filp, cd);
  1202. }
  1203. static int release_flush_procfs(struct inode *inode, struct file *filp)
  1204. {
  1205. struct cache_detail *cd = PDE(inode)->data;
  1206. return release_flush(inode, filp, cd);
  1207. }
  1208. static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
  1209. size_t count, loff_t *ppos)
  1210. {
  1211. struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
  1212. return read_flush(filp, buf, count, ppos, cd);
  1213. }
  1214. static ssize_t write_flush_procfs(struct file *filp,
  1215. const 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 write_flush(filp, buf, count, ppos, cd);
  1220. }
  1221. static const struct file_operations cache_flush_operations_procfs = {
  1222. .open = open_flush_procfs,
  1223. .read = read_flush_procfs,
  1224. .write = write_flush_procfs,
  1225. .release = release_flush_procfs,
  1226. };
  1227. static void remove_cache_proc_entries(struct cache_detail *cd)
  1228. {
  1229. if (cd->u.procfs.proc_ent == NULL)
  1230. return;
  1231. if (cd->u.procfs.flush_ent)
  1232. remove_proc_entry("flush", cd->u.procfs.proc_ent);
  1233. if (cd->u.procfs.channel_ent)
  1234. remove_proc_entry("channel", cd->u.procfs.proc_ent);
  1235. if (cd->u.procfs.content_ent)
  1236. remove_proc_entry("content", cd->u.procfs.proc_ent);
  1237. cd->u.procfs.proc_ent = NULL;
  1238. remove_proc_entry(cd->name, proc_net_rpc);
  1239. }
  1240. #ifdef CONFIG_PROC_FS
  1241. static int create_cache_proc_entries(struct cache_detail *cd)
  1242. {
  1243. struct proc_dir_entry *p;
  1244. cd->u.procfs.proc_ent = proc_mkdir(cd->name, proc_net_rpc);
  1245. if (cd->u.procfs.proc_ent == NULL)
  1246. goto out_nomem;
  1247. cd->u.procfs.channel_ent = NULL;
  1248. cd->u.procfs.content_ent = NULL;
  1249. p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
  1250. cd->u.procfs.proc_ent,
  1251. &cache_flush_operations_procfs, cd);
  1252. cd->u.procfs.flush_ent = p;
  1253. if (p == NULL)
  1254. goto out_nomem;
  1255. if (cd->cache_upcall || cd->cache_parse) {
  1256. p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
  1257. cd->u.procfs.proc_ent,
  1258. &cache_file_operations_procfs, cd);
  1259. cd->u.procfs.channel_ent = p;
  1260. if (p == NULL)
  1261. goto out_nomem;
  1262. }
  1263. if (cd->cache_show) {
  1264. p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR,
  1265. cd->u.procfs.proc_ent,
  1266. &content_file_operations_procfs, cd);
  1267. cd->u.procfs.content_ent = p;
  1268. if (p == NULL)
  1269. goto out_nomem;
  1270. }
  1271. return 0;
  1272. out_nomem:
  1273. remove_cache_proc_entries(cd);
  1274. return -ENOMEM;
  1275. }
  1276. #else /* CONFIG_PROC_FS */
  1277. static int create_cache_proc_entries(struct cache_detail *cd)
  1278. {
  1279. return 0;
  1280. }
  1281. #endif
  1282. int cache_register(struct cache_detail *cd)
  1283. {
  1284. int ret;
  1285. sunrpc_init_cache_detail(cd);
  1286. ret = create_cache_proc_entries(cd);
  1287. if (ret)
  1288. sunrpc_destroy_cache_detail(cd);
  1289. return ret;
  1290. }
  1291. EXPORT_SYMBOL_GPL(cache_register);
  1292. void cache_unregister(struct cache_detail *cd)
  1293. {
  1294. remove_cache_proc_entries(cd);
  1295. sunrpc_destroy_cache_detail(cd);
  1296. }
  1297. EXPORT_SYMBOL_GPL(cache_unregister);
  1298. static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
  1299. size_t count, loff_t *ppos)
  1300. {
  1301. struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
  1302. return cache_read(filp, buf, count, ppos, cd);
  1303. }
  1304. static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
  1305. size_t count, loff_t *ppos)
  1306. {
  1307. struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
  1308. return cache_write(filp, buf, count, ppos, cd);
  1309. }
  1310. static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
  1311. {
  1312. struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
  1313. return cache_poll(filp, wait, cd);
  1314. }
  1315. static int cache_ioctl_pipefs(struct inode *inode, struct file *filp,
  1316. unsigned int cmd, unsigned long arg)
  1317. {
  1318. struct cache_detail *cd = RPC_I(inode)->private;
  1319. return cache_ioctl(inode, filp, cmd, arg, cd);
  1320. }
  1321. static int cache_open_pipefs(struct inode *inode, struct file *filp)
  1322. {
  1323. struct cache_detail *cd = RPC_I(inode)->private;
  1324. return cache_open(inode, filp, cd);
  1325. }
  1326. static int cache_release_pipefs(struct inode *inode, struct file *filp)
  1327. {
  1328. struct cache_detail *cd = RPC_I(inode)->private;
  1329. return cache_release(inode, filp, cd);
  1330. }
  1331. const struct file_operations cache_file_operations_pipefs = {
  1332. .owner = THIS_MODULE,
  1333. .llseek = no_llseek,
  1334. .read = cache_read_pipefs,
  1335. .write = cache_write_pipefs,
  1336. .poll = cache_poll_pipefs,
  1337. .ioctl = cache_ioctl_pipefs, /* for FIONREAD */
  1338. .open = cache_open_pipefs,
  1339. .release = cache_release_pipefs,
  1340. };
  1341. static int content_open_pipefs(struct inode *inode, struct file *filp)
  1342. {
  1343. struct cache_detail *cd = RPC_I(inode)->private;
  1344. return content_open(inode, filp, cd);
  1345. }
  1346. static int content_release_pipefs(struct inode *inode, struct file *filp)
  1347. {
  1348. struct cache_detail *cd = RPC_I(inode)->private;
  1349. return content_release(inode, filp, cd);
  1350. }
  1351. const struct file_operations content_file_operations_pipefs = {
  1352. .open = content_open_pipefs,
  1353. .read = seq_read,
  1354. .llseek = seq_lseek,
  1355. .release = content_release_pipefs,
  1356. };
  1357. static int open_flush_pipefs(struct inode *inode, struct file *filp)
  1358. {
  1359. struct cache_detail *cd = RPC_I(inode)->private;
  1360. return open_flush(inode, filp, cd);
  1361. }
  1362. static int release_flush_pipefs(struct inode *inode, struct file *filp)
  1363. {
  1364. struct cache_detail *cd = RPC_I(inode)->private;
  1365. return release_flush(inode, filp, cd);
  1366. }
  1367. static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
  1368. size_t count, loff_t *ppos)
  1369. {
  1370. struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
  1371. return read_flush(filp, buf, count, ppos, cd);
  1372. }
  1373. static ssize_t write_flush_pipefs(struct file *filp,
  1374. const 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 write_flush(filp, buf, count, ppos, cd);
  1379. }
  1380. const struct file_operations cache_flush_operations_pipefs = {
  1381. .open = open_flush_pipefs,
  1382. .read = read_flush_pipefs,
  1383. .write = write_flush_pipefs,
  1384. .release = release_flush_pipefs,
  1385. };
  1386. int sunrpc_cache_register_pipefs(struct dentry *parent,
  1387. const char *name, mode_t umode,
  1388. struct cache_detail *cd)
  1389. {
  1390. struct qstr q;
  1391. struct dentry *dir;
  1392. int ret = 0;
  1393. sunrpc_init_cache_detail(cd);
  1394. q.name = name;
  1395. q.len = strlen(name);
  1396. q.hash = full_name_hash(q.name, q.len);
  1397. dir = rpc_create_cache_dir(parent, &q, umode, cd);
  1398. if (!IS_ERR(dir))
  1399. cd->u.pipefs.dir = dir;
  1400. else {
  1401. sunrpc_destroy_cache_detail(cd);
  1402. ret = PTR_ERR(dir);
  1403. }
  1404. return ret;
  1405. }
  1406. EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
  1407. void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
  1408. {
  1409. rpc_remove_cache_dir(cd->u.pipefs.dir);
  1410. cd->u.pipefs.dir = NULL;
  1411. sunrpc_destroy_cache_detail(cd);
  1412. }
  1413. EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);