cache.c 32 KB

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