cache.c 42 KB

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