filecache.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * The NFSD open file cache.
  4. *
  5. * (c) 2015 - Jeff Layton <jeff.layton@primarydata.com>
  6. *
  7. * An nfsd_file object is a per-file collection of open state that binds
  8. * together:
  9. * - a struct file *
  10. * - a user credential
  11. * - a network namespace
  12. * - a read-ahead context
  13. * - monitoring for writeback errors
  14. *
  15. * nfsd_file objects are reference-counted. Consumers acquire a new
  16. * object via the nfsd_file_acquire API. They manage their interest in
  17. * the acquired object, and hence the object's reference count, via
  18. * nfsd_file_get and nfsd_file_put. There are two varieties of nfsd_file
  19. * object:
  20. *
  21. * * non-garbage-collected: When a consumer wants to precisely control
  22. * the lifetime of a file's open state, it acquires a non-garbage-
  23. * collected nfsd_file. The final nfsd_file_put releases the open
  24. * state immediately.
  25. *
  26. * * garbage-collected: When a consumer does not control the lifetime
  27. * of open state, it acquires a garbage-collected nfsd_file. The
  28. * final nfsd_file_put allows the open state to linger for a period
  29. * during which it may be re-used.
  30. */
  31. #include <linux/hash.h>
  32. #include <linux/slab.h>
  33. #include <linux/file.h>
  34. #include <linux/pagemap.h>
  35. #include <linux/sched.h>
  36. #include <linux/list_lru.h>
  37. #include <linux/fsnotify_backend.h>
  38. #include <linux/fsnotify.h>
  39. #include <linux/seq_file.h>
  40. #include <linux/rhashtable.h>
  41. #include "vfs.h"
  42. #include "nfsd.h"
  43. #include "nfsfh.h"
  44. #include "netns.h"
  45. #include "filecache.h"
  46. #include "trace.h"
  47. #define NFSD_LAUNDRETTE_DELAY (2 * HZ)
  48. #define NFSD_FILE_CACHE_UP (0)
  49. /* We only care about NFSD_MAY_READ/WRITE for this cache */
  50. #define NFSD_FILE_MAY_MASK (NFSD_MAY_READ|NFSD_MAY_WRITE|NFSD_MAY_LOCALIO)
  51. static DEFINE_PER_CPU(unsigned long, nfsd_file_cache_hits);
  52. static DEFINE_PER_CPU(unsigned long, nfsd_file_acquisitions);
  53. static DEFINE_PER_CPU(unsigned long, nfsd_file_allocations);
  54. static DEFINE_PER_CPU(unsigned long, nfsd_file_releases);
  55. static DEFINE_PER_CPU(unsigned long, nfsd_file_total_age);
  56. static DEFINE_PER_CPU(unsigned long, nfsd_file_evictions);
  57. struct nfsd_fcache_disposal {
  58. spinlock_t lock;
  59. struct list_head freeme;
  60. };
  61. static struct kmem_cache *nfsd_file_slab;
  62. static struct kmem_cache *nfsd_file_mark_slab;
  63. static struct list_lru nfsd_file_lru;
  64. static unsigned long nfsd_file_flags;
  65. static struct fsnotify_group *nfsd_file_fsnotify_group;
  66. static struct delayed_work nfsd_filecache_laundrette;
  67. static struct rhltable nfsd_file_rhltable
  68. ____cacheline_aligned_in_smp;
  69. static bool
  70. nfsd_match_cred(const struct cred *c1, const struct cred *c2)
  71. {
  72. int i;
  73. if (!uid_eq(c1->fsuid, c2->fsuid))
  74. return false;
  75. if (!gid_eq(c1->fsgid, c2->fsgid))
  76. return false;
  77. if (c1->group_info == NULL || c2->group_info == NULL)
  78. return c1->group_info == c2->group_info;
  79. if (c1->group_info->ngroups != c2->group_info->ngroups)
  80. return false;
  81. for (i = 0; i < c1->group_info->ngroups; i++) {
  82. if (!gid_eq(c1->group_info->gid[i], c2->group_info->gid[i]))
  83. return false;
  84. }
  85. return true;
  86. }
  87. static const struct rhashtable_params nfsd_file_rhash_params = {
  88. .key_len = sizeof_field(struct nfsd_file, nf_inode),
  89. .key_offset = offsetof(struct nfsd_file, nf_inode),
  90. .head_offset = offsetof(struct nfsd_file, nf_rlist),
  91. /*
  92. * Start with a single page hash table to reduce resizing churn
  93. * on light workloads.
  94. */
  95. .min_size = 256,
  96. .automatic_shrinking = true,
  97. };
  98. static void
  99. nfsd_file_schedule_laundrette(void)
  100. {
  101. if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags))
  102. queue_delayed_work(system_unbound_wq, &nfsd_filecache_laundrette,
  103. NFSD_LAUNDRETTE_DELAY);
  104. }
  105. static void
  106. nfsd_file_slab_free(struct rcu_head *rcu)
  107. {
  108. struct nfsd_file *nf = container_of(rcu, struct nfsd_file, nf_rcu);
  109. put_cred(nf->nf_cred);
  110. kmem_cache_free(nfsd_file_slab, nf);
  111. }
  112. static void
  113. nfsd_file_mark_free(struct fsnotify_mark *mark)
  114. {
  115. struct nfsd_file_mark *nfm = container_of(mark, struct nfsd_file_mark,
  116. nfm_mark);
  117. kmem_cache_free(nfsd_file_mark_slab, nfm);
  118. }
  119. static struct nfsd_file_mark *
  120. nfsd_file_mark_get(struct nfsd_file_mark *nfm)
  121. {
  122. if (!refcount_inc_not_zero(&nfm->nfm_ref))
  123. return NULL;
  124. return nfm;
  125. }
  126. static void
  127. nfsd_file_mark_put(struct nfsd_file_mark *nfm)
  128. {
  129. if (refcount_dec_and_test(&nfm->nfm_ref)) {
  130. fsnotify_destroy_mark(&nfm->nfm_mark, nfsd_file_fsnotify_group);
  131. fsnotify_put_mark(&nfm->nfm_mark);
  132. }
  133. }
  134. static struct nfsd_file_mark *
  135. nfsd_file_mark_find_or_create(struct inode *inode)
  136. {
  137. int err;
  138. struct fsnotify_mark *mark;
  139. struct nfsd_file_mark *nfm = NULL, *new;
  140. do {
  141. fsnotify_group_lock(nfsd_file_fsnotify_group);
  142. mark = fsnotify_find_inode_mark(inode,
  143. nfsd_file_fsnotify_group);
  144. if (mark) {
  145. nfm = nfsd_file_mark_get(container_of(mark,
  146. struct nfsd_file_mark,
  147. nfm_mark));
  148. fsnotify_group_unlock(nfsd_file_fsnotify_group);
  149. if (nfm) {
  150. fsnotify_put_mark(mark);
  151. break;
  152. }
  153. /* Avoid soft lockup race with nfsd_file_mark_put() */
  154. fsnotify_destroy_mark(mark, nfsd_file_fsnotify_group);
  155. fsnotify_put_mark(mark);
  156. } else {
  157. fsnotify_group_unlock(nfsd_file_fsnotify_group);
  158. }
  159. /* allocate a new nfm */
  160. new = kmem_cache_alloc(nfsd_file_mark_slab, GFP_KERNEL);
  161. if (!new)
  162. return NULL;
  163. fsnotify_init_mark(&new->nfm_mark, nfsd_file_fsnotify_group);
  164. new->nfm_mark.mask = FS_ATTRIB|FS_DELETE_SELF;
  165. refcount_set(&new->nfm_ref, 1);
  166. err = fsnotify_add_inode_mark(&new->nfm_mark, inode, 0);
  167. /*
  168. * If the add was successful, then return the object.
  169. * Otherwise, we need to put the reference we hold on the
  170. * nfm_mark. The fsnotify code will take a reference and put
  171. * it on failure, so we can't just free it directly. It's also
  172. * not safe to call fsnotify_destroy_mark on it as the
  173. * mark->group will be NULL. Thus, we can't let the nfm_ref
  174. * counter drive the destruction at this point.
  175. */
  176. if (likely(!err))
  177. nfm = new;
  178. else
  179. fsnotify_put_mark(&new->nfm_mark);
  180. } while (unlikely(err == -EEXIST));
  181. return nfm;
  182. }
  183. static struct nfsd_file *
  184. nfsd_file_alloc(struct net *net, struct inode *inode, unsigned char need,
  185. bool want_gc)
  186. {
  187. struct nfsd_file *nf;
  188. nf = kmem_cache_alloc(nfsd_file_slab, GFP_KERNEL);
  189. if (unlikely(!nf))
  190. return NULL;
  191. this_cpu_inc(nfsd_file_allocations);
  192. INIT_LIST_HEAD(&nf->nf_lru);
  193. INIT_LIST_HEAD(&nf->nf_gc);
  194. nf->nf_birthtime = ktime_get();
  195. nf->nf_file = NULL;
  196. nf->nf_cred = get_current_cred();
  197. nf->nf_net = net;
  198. nf->nf_flags = want_gc ?
  199. BIT(NFSD_FILE_HASHED) | BIT(NFSD_FILE_PENDING) | BIT(NFSD_FILE_GC) :
  200. BIT(NFSD_FILE_HASHED) | BIT(NFSD_FILE_PENDING);
  201. nf->nf_inode = inode;
  202. refcount_set(&nf->nf_ref, 1);
  203. nf->nf_may = need;
  204. nf->nf_mark = NULL;
  205. return nf;
  206. }
  207. /**
  208. * nfsd_file_check_write_error - check for writeback errors on a file
  209. * @nf: nfsd_file to check for writeback errors
  210. *
  211. * Check whether a nfsd_file has an unseen error. Reset the write
  212. * verifier if so.
  213. */
  214. static void
  215. nfsd_file_check_write_error(struct nfsd_file *nf)
  216. {
  217. struct file *file = nf->nf_file;
  218. if ((file->f_mode & FMODE_WRITE) &&
  219. filemap_check_wb_err(file->f_mapping, READ_ONCE(file->f_wb_err)))
  220. nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id));
  221. }
  222. static void
  223. nfsd_file_hash_remove(struct nfsd_file *nf)
  224. {
  225. trace_nfsd_file_unhash(nf);
  226. rhltable_remove(&nfsd_file_rhltable, &nf->nf_rlist,
  227. nfsd_file_rhash_params);
  228. }
  229. static bool
  230. nfsd_file_unhash(struct nfsd_file *nf)
  231. {
  232. if (test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
  233. nfsd_file_hash_remove(nf);
  234. return true;
  235. }
  236. return false;
  237. }
  238. static void
  239. nfsd_file_free(struct nfsd_file *nf)
  240. {
  241. s64 age = ktime_to_ms(ktime_sub(ktime_get(), nf->nf_birthtime));
  242. trace_nfsd_file_free(nf);
  243. this_cpu_inc(nfsd_file_releases);
  244. this_cpu_add(nfsd_file_total_age, age);
  245. nfsd_file_unhash(nf);
  246. if (nf->nf_mark)
  247. nfsd_file_mark_put(nf->nf_mark);
  248. if (nf->nf_file) {
  249. nfsd_file_check_write_error(nf);
  250. nfsd_filp_close(nf->nf_file);
  251. }
  252. /*
  253. * If this item is still linked via nf_lru, that's a bug.
  254. * WARN and leak it to preserve system stability.
  255. */
  256. if (WARN_ON_ONCE(!list_empty(&nf->nf_lru)))
  257. return;
  258. call_rcu(&nf->nf_rcu, nfsd_file_slab_free);
  259. }
  260. static bool
  261. nfsd_file_check_writeback(struct nfsd_file *nf)
  262. {
  263. struct file *file = nf->nf_file;
  264. struct address_space *mapping;
  265. /* File not open for write? */
  266. if (!(file->f_mode & FMODE_WRITE))
  267. return false;
  268. /*
  269. * Some filesystems (e.g. NFS) flush all dirty data on close.
  270. * On others, there is no need to wait for writeback.
  271. */
  272. if (!(file_inode(file)->i_sb->s_export_op->flags & EXPORT_OP_FLUSH_ON_CLOSE))
  273. return false;
  274. mapping = file->f_mapping;
  275. return mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) ||
  276. mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK);
  277. }
  278. static bool nfsd_file_lru_add(struct nfsd_file *nf)
  279. {
  280. set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags);
  281. if (list_lru_add_obj(&nfsd_file_lru, &nf->nf_lru)) {
  282. trace_nfsd_file_lru_add(nf);
  283. return true;
  284. }
  285. return false;
  286. }
  287. static bool nfsd_file_lru_remove(struct nfsd_file *nf)
  288. {
  289. if (list_lru_del_obj(&nfsd_file_lru, &nf->nf_lru)) {
  290. trace_nfsd_file_lru_del(nf);
  291. return true;
  292. }
  293. return false;
  294. }
  295. struct nfsd_file *
  296. nfsd_file_get(struct nfsd_file *nf)
  297. {
  298. if (nf && refcount_inc_not_zero(&nf->nf_ref))
  299. return nf;
  300. return NULL;
  301. }
  302. /**
  303. * nfsd_file_put - put the reference to a nfsd_file
  304. * @nf: nfsd_file of which to put the reference
  305. *
  306. * Put a reference to a nfsd_file. In the non-GC case, we just put the
  307. * reference immediately. In the GC case, if the reference would be
  308. * the last one, the put it on the LRU instead to be cleaned up later.
  309. */
  310. void
  311. nfsd_file_put(struct nfsd_file *nf)
  312. {
  313. might_sleep();
  314. trace_nfsd_file_put(nf);
  315. if (test_bit(NFSD_FILE_GC, &nf->nf_flags) &&
  316. test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
  317. /*
  318. * If this is the last reference (nf_ref == 1), then try to
  319. * transfer it to the LRU.
  320. */
  321. if (refcount_dec_not_one(&nf->nf_ref))
  322. return;
  323. /* Try to add it to the LRU. If that fails, decrement. */
  324. if (nfsd_file_lru_add(nf)) {
  325. /* If it's still hashed, we're done */
  326. if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
  327. nfsd_file_schedule_laundrette();
  328. return;
  329. }
  330. /*
  331. * We're racing with unhashing, so try to remove it from
  332. * the LRU. If removal fails, then someone else already
  333. * has our reference.
  334. */
  335. if (!nfsd_file_lru_remove(nf))
  336. return;
  337. }
  338. }
  339. if (refcount_dec_and_test(&nf->nf_ref))
  340. nfsd_file_free(nf);
  341. }
  342. /**
  343. * nfsd_file_put_local - put nfsd_file reference and arm nfsd_serv_put in caller
  344. * @nf: nfsd_file of which to put the reference
  345. *
  346. * First save the associated net to return to caller, then put
  347. * the reference of the nfsd_file.
  348. */
  349. struct net *
  350. nfsd_file_put_local(struct nfsd_file *nf)
  351. {
  352. struct net *net = nf->nf_net;
  353. nfsd_file_put(nf);
  354. return net;
  355. }
  356. /**
  357. * nfsd_file_file - get the backing file of an nfsd_file
  358. * @nf: nfsd_file of which to access the backing file.
  359. *
  360. * Return backing file for @nf.
  361. */
  362. struct file *
  363. nfsd_file_file(struct nfsd_file *nf)
  364. {
  365. return nf->nf_file;
  366. }
  367. static void
  368. nfsd_file_dispose_list(struct list_head *dispose)
  369. {
  370. struct nfsd_file *nf;
  371. while (!list_empty(dispose)) {
  372. nf = list_first_entry(dispose, struct nfsd_file, nf_gc);
  373. list_del_init(&nf->nf_gc);
  374. nfsd_file_free(nf);
  375. }
  376. }
  377. /**
  378. * nfsd_file_dispose_list_delayed - move list of dead files to net's freeme list
  379. * @dispose: list of nfsd_files to be disposed
  380. *
  381. * Transfers each file to the "freeme" list for its nfsd_net, to eventually
  382. * be disposed of by the per-net garbage collector.
  383. */
  384. static void
  385. nfsd_file_dispose_list_delayed(struct list_head *dispose)
  386. {
  387. while(!list_empty(dispose)) {
  388. struct nfsd_file *nf = list_first_entry(dispose,
  389. struct nfsd_file, nf_gc);
  390. struct nfsd_net *nn = net_generic(nf->nf_net, nfsd_net_id);
  391. struct nfsd_fcache_disposal *l = nn->fcache_disposal;
  392. struct svc_serv *serv;
  393. spin_lock(&l->lock);
  394. list_move_tail(&nf->nf_gc, &l->freeme);
  395. spin_unlock(&l->lock);
  396. /*
  397. * The filecache laundrette is shut down after the
  398. * nn->nfsd_serv pointer is cleared, but before the
  399. * svc_serv is freed.
  400. */
  401. serv = nn->nfsd_serv;
  402. if (serv)
  403. svc_wake_up(serv);
  404. }
  405. }
  406. /**
  407. * nfsd_file_net_dispose - deal with nfsd_files waiting to be disposed.
  408. * @nn: nfsd_net in which to find files to be disposed.
  409. *
  410. * When files held open for nfsv3 are removed from the filecache, whether
  411. * due to memory pressure or garbage collection, they are queued to
  412. * a per-net-ns queue. This function completes the disposal, either
  413. * directly or by waking another nfsd thread to help with the work.
  414. */
  415. void nfsd_file_net_dispose(struct nfsd_net *nn)
  416. {
  417. struct nfsd_fcache_disposal *l = nn->fcache_disposal;
  418. if (!list_empty(&l->freeme)) {
  419. LIST_HEAD(dispose);
  420. int i;
  421. spin_lock(&l->lock);
  422. for (i = 0; i < 8 && !list_empty(&l->freeme); i++)
  423. list_move(l->freeme.next, &dispose);
  424. spin_unlock(&l->lock);
  425. if (!list_empty(&l->freeme))
  426. /* Wake up another thread to share the work
  427. * *before* doing any actual disposing.
  428. */
  429. svc_wake_up(nn->nfsd_serv);
  430. nfsd_file_dispose_list(&dispose);
  431. }
  432. }
  433. /**
  434. * nfsd_file_lru_cb - Examine an entry on the LRU list
  435. * @item: LRU entry to examine
  436. * @lru: controlling LRU
  437. * @lock: LRU list lock (unused)
  438. * @arg: dispose list
  439. *
  440. * Return values:
  441. * %LRU_REMOVED: @item was removed from the LRU
  442. * %LRU_ROTATE: @item is to be moved to the LRU tail
  443. * %LRU_SKIP: @item cannot be evicted
  444. */
  445. static enum lru_status
  446. nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru,
  447. spinlock_t *lock, void *arg)
  448. __releases(lock)
  449. __acquires(lock)
  450. {
  451. struct list_head *head = arg;
  452. struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru);
  453. /* We should only be dealing with GC entries here */
  454. WARN_ON_ONCE(!test_bit(NFSD_FILE_GC, &nf->nf_flags));
  455. /*
  456. * Don't throw out files that are still undergoing I/O or
  457. * that have uncleared errors pending.
  458. */
  459. if (nfsd_file_check_writeback(nf)) {
  460. trace_nfsd_file_gc_writeback(nf);
  461. return LRU_SKIP;
  462. }
  463. /* If it was recently added to the list, skip it */
  464. if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags)) {
  465. trace_nfsd_file_gc_referenced(nf);
  466. return LRU_ROTATE;
  467. }
  468. /*
  469. * Put the reference held on behalf of the LRU. If it wasn't the last
  470. * one, then just remove it from the LRU and ignore it.
  471. */
  472. if (!refcount_dec_and_test(&nf->nf_ref)) {
  473. trace_nfsd_file_gc_in_use(nf);
  474. list_lru_isolate(lru, &nf->nf_lru);
  475. return LRU_REMOVED;
  476. }
  477. /* Refcount went to zero. Unhash it and queue it to the dispose list */
  478. nfsd_file_unhash(nf);
  479. list_lru_isolate(lru, &nf->nf_lru);
  480. list_add(&nf->nf_gc, head);
  481. this_cpu_inc(nfsd_file_evictions);
  482. trace_nfsd_file_gc_disposed(nf);
  483. return LRU_REMOVED;
  484. }
  485. static void
  486. nfsd_file_gc(void)
  487. {
  488. LIST_HEAD(dispose);
  489. unsigned long ret;
  490. ret = list_lru_walk(&nfsd_file_lru, nfsd_file_lru_cb,
  491. &dispose, list_lru_count(&nfsd_file_lru));
  492. trace_nfsd_file_gc_removed(ret, list_lru_count(&nfsd_file_lru));
  493. nfsd_file_dispose_list_delayed(&dispose);
  494. }
  495. static void
  496. nfsd_file_gc_worker(struct work_struct *work)
  497. {
  498. nfsd_file_gc();
  499. if (list_lru_count(&nfsd_file_lru))
  500. nfsd_file_schedule_laundrette();
  501. }
  502. static unsigned long
  503. nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc)
  504. {
  505. return list_lru_count(&nfsd_file_lru);
  506. }
  507. static unsigned long
  508. nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc)
  509. {
  510. LIST_HEAD(dispose);
  511. unsigned long ret;
  512. ret = list_lru_shrink_walk(&nfsd_file_lru, sc,
  513. nfsd_file_lru_cb, &dispose);
  514. trace_nfsd_file_shrinker_removed(ret, list_lru_count(&nfsd_file_lru));
  515. nfsd_file_dispose_list_delayed(&dispose);
  516. return ret;
  517. }
  518. static struct shrinker *nfsd_file_shrinker;
  519. /**
  520. * nfsd_file_cond_queue - conditionally unhash and queue a nfsd_file
  521. * @nf: nfsd_file to attempt to queue
  522. * @dispose: private list to queue successfully-put objects
  523. *
  524. * Unhash an nfsd_file, try to get a reference to it, and then put that
  525. * reference. If it's the last reference, queue it to the dispose list.
  526. */
  527. static void
  528. nfsd_file_cond_queue(struct nfsd_file *nf, struct list_head *dispose)
  529. __must_hold(RCU)
  530. {
  531. int decrement = 1;
  532. /* If we raced with someone else unhashing, ignore it */
  533. if (!nfsd_file_unhash(nf))
  534. return;
  535. /* If we can't get a reference, ignore it */
  536. if (!nfsd_file_get(nf))
  537. return;
  538. /* Extra decrement if we remove from the LRU */
  539. if (nfsd_file_lru_remove(nf))
  540. ++decrement;
  541. /* If refcount goes to 0, then put on the dispose list */
  542. if (refcount_sub_and_test(decrement, &nf->nf_ref)) {
  543. list_add(&nf->nf_gc, dispose);
  544. trace_nfsd_file_closing(nf);
  545. }
  546. }
  547. /**
  548. * nfsd_file_queue_for_close: try to close out any open nfsd_files for an inode
  549. * @inode: inode on which to close out nfsd_files
  550. * @dispose: list on which to gather nfsd_files to close out
  551. *
  552. * An nfsd_file represents a struct file being held open on behalf of nfsd.
  553. * An open file however can block other activity (such as leases), or cause
  554. * undesirable behavior (e.g. spurious silly-renames when reexporting NFS).
  555. *
  556. * This function is intended to find open nfsd_files when this sort of
  557. * conflicting access occurs and then attempt to close those files out.
  558. *
  559. * Populates the dispose list with entries that have already had their
  560. * refcounts go to zero. The actual free of an nfsd_file can be expensive,
  561. * so we leave it up to the caller whether it wants to wait or not.
  562. */
  563. static void
  564. nfsd_file_queue_for_close(struct inode *inode, struct list_head *dispose)
  565. {
  566. struct rhlist_head *tmp, *list;
  567. struct nfsd_file *nf;
  568. rcu_read_lock();
  569. list = rhltable_lookup(&nfsd_file_rhltable, &inode,
  570. nfsd_file_rhash_params);
  571. rhl_for_each_entry_rcu(nf, tmp, list, nf_rlist) {
  572. if (!test_bit(NFSD_FILE_GC, &nf->nf_flags))
  573. continue;
  574. nfsd_file_cond_queue(nf, dispose);
  575. }
  576. rcu_read_unlock();
  577. }
  578. /**
  579. * nfsd_file_close_inode - attempt a delayed close of a nfsd_file
  580. * @inode: inode of the file to attempt to remove
  581. *
  582. * Close out any open nfsd_files that can be reaped for @inode. The
  583. * actual freeing is deferred to the dispose_list_delayed infrastructure.
  584. *
  585. * This is used by the fsnotify callbacks and setlease notifier.
  586. */
  587. static void
  588. nfsd_file_close_inode(struct inode *inode)
  589. {
  590. LIST_HEAD(dispose);
  591. nfsd_file_queue_for_close(inode, &dispose);
  592. nfsd_file_dispose_list_delayed(&dispose);
  593. }
  594. /**
  595. * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file
  596. * @inode: inode of the file to attempt to remove
  597. *
  598. * Close out any open nfsd_files that can be reaped for @inode. The
  599. * nfsd_files are closed out synchronously.
  600. *
  601. * This is called from nfsd_rename and nfsd_unlink to avoid silly-renames
  602. * when reexporting NFS.
  603. */
  604. void
  605. nfsd_file_close_inode_sync(struct inode *inode)
  606. {
  607. struct nfsd_file *nf;
  608. LIST_HEAD(dispose);
  609. trace_nfsd_file_close(inode);
  610. nfsd_file_queue_for_close(inode, &dispose);
  611. while (!list_empty(&dispose)) {
  612. nf = list_first_entry(&dispose, struct nfsd_file, nf_gc);
  613. list_del_init(&nf->nf_gc);
  614. nfsd_file_free(nf);
  615. }
  616. }
  617. static int
  618. nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg,
  619. void *data)
  620. {
  621. struct file_lease *fl = data;
  622. /* Only close files for F_SETLEASE leases */
  623. if (fl->c.flc_flags & FL_LEASE)
  624. nfsd_file_close_inode(file_inode(fl->c.flc_file));
  625. return 0;
  626. }
  627. static struct notifier_block nfsd_file_lease_notifier = {
  628. .notifier_call = nfsd_file_lease_notifier_call,
  629. };
  630. static int
  631. nfsd_file_fsnotify_handle_event(struct fsnotify_mark *mark, u32 mask,
  632. struct inode *inode, struct inode *dir,
  633. const struct qstr *name, u32 cookie)
  634. {
  635. if (WARN_ON_ONCE(!inode))
  636. return 0;
  637. trace_nfsd_file_fsnotify_handle_event(inode, mask);
  638. /* Should be no marks on non-regular files */
  639. if (!S_ISREG(inode->i_mode)) {
  640. WARN_ON_ONCE(1);
  641. return 0;
  642. }
  643. /* don't close files if this was not the last link */
  644. if (mask & FS_ATTRIB) {
  645. if (inode->i_nlink)
  646. return 0;
  647. }
  648. nfsd_file_close_inode(inode);
  649. return 0;
  650. }
  651. static const struct fsnotify_ops nfsd_file_fsnotify_ops = {
  652. .handle_inode_event = nfsd_file_fsnotify_handle_event,
  653. .free_mark = nfsd_file_mark_free,
  654. };
  655. int
  656. nfsd_file_cache_init(void)
  657. {
  658. int ret;
  659. lockdep_assert_held(&nfsd_mutex);
  660. if (test_and_set_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1)
  661. return 0;
  662. ret = rhltable_init(&nfsd_file_rhltable, &nfsd_file_rhash_params);
  663. if (ret)
  664. goto out;
  665. ret = -ENOMEM;
  666. nfsd_file_slab = KMEM_CACHE(nfsd_file, 0);
  667. if (!nfsd_file_slab) {
  668. pr_err("nfsd: unable to create nfsd_file_slab\n");
  669. goto out_err;
  670. }
  671. nfsd_file_mark_slab = KMEM_CACHE(nfsd_file_mark, 0);
  672. if (!nfsd_file_mark_slab) {
  673. pr_err("nfsd: unable to create nfsd_file_mark_slab\n");
  674. goto out_err;
  675. }
  676. ret = list_lru_init(&nfsd_file_lru);
  677. if (ret) {
  678. pr_err("nfsd: failed to init nfsd_file_lru: %d\n", ret);
  679. goto out_err;
  680. }
  681. nfsd_file_shrinker = shrinker_alloc(0, "nfsd-filecache");
  682. if (!nfsd_file_shrinker) {
  683. ret = -ENOMEM;
  684. pr_err("nfsd: failed to allocate nfsd_file_shrinker\n");
  685. goto out_lru;
  686. }
  687. nfsd_file_shrinker->count_objects = nfsd_file_lru_count;
  688. nfsd_file_shrinker->scan_objects = nfsd_file_lru_scan;
  689. nfsd_file_shrinker->seeks = 1;
  690. shrinker_register(nfsd_file_shrinker);
  691. ret = lease_register_notifier(&nfsd_file_lease_notifier);
  692. if (ret) {
  693. pr_err("nfsd: unable to register lease notifier: %d\n", ret);
  694. goto out_shrinker;
  695. }
  696. nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops,
  697. 0);
  698. if (IS_ERR(nfsd_file_fsnotify_group)) {
  699. pr_err("nfsd: unable to create fsnotify group: %ld\n",
  700. PTR_ERR(nfsd_file_fsnotify_group));
  701. ret = PTR_ERR(nfsd_file_fsnotify_group);
  702. nfsd_file_fsnotify_group = NULL;
  703. goto out_notifier;
  704. }
  705. INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_gc_worker);
  706. out:
  707. if (ret)
  708. clear_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags);
  709. return ret;
  710. out_notifier:
  711. lease_unregister_notifier(&nfsd_file_lease_notifier);
  712. out_shrinker:
  713. shrinker_free(nfsd_file_shrinker);
  714. out_lru:
  715. list_lru_destroy(&nfsd_file_lru);
  716. out_err:
  717. kmem_cache_destroy(nfsd_file_slab);
  718. nfsd_file_slab = NULL;
  719. kmem_cache_destroy(nfsd_file_mark_slab);
  720. nfsd_file_mark_slab = NULL;
  721. rhltable_destroy(&nfsd_file_rhltable);
  722. goto out;
  723. }
  724. /**
  725. * __nfsd_file_cache_purge: clean out the cache for shutdown
  726. * @net: net-namespace to shut down the cache (may be NULL)
  727. *
  728. * Walk the nfsd_file cache and close out any that match @net. If @net is NULL,
  729. * then close out everything. Called when an nfsd instance is being shut down,
  730. * and when the exports table is flushed.
  731. */
  732. static void
  733. __nfsd_file_cache_purge(struct net *net)
  734. {
  735. struct rhashtable_iter iter;
  736. struct nfsd_file *nf;
  737. LIST_HEAD(dispose);
  738. rhltable_walk_enter(&nfsd_file_rhltable, &iter);
  739. do {
  740. rhashtable_walk_start(&iter);
  741. nf = rhashtable_walk_next(&iter);
  742. while (!IS_ERR_OR_NULL(nf)) {
  743. if (!net || nf->nf_net == net)
  744. nfsd_file_cond_queue(nf, &dispose);
  745. nf = rhashtable_walk_next(&iter);
  746. }
  747. rhashtable_walk_stop(&iter);
  748. } while (nf == ERR_PTR(-EAGAIN));
  749. rhashtable_walk_exit(&iter);
  750. nfsd_file_dispose_list(&dispose);
  751. }
  752. static struct nfsd_fcache_disposal *
  753. nfsd_alloc_fcache_disposal(void)
  754. {
  755. struct nfsd_fcache_disposal *l;
  756. l = kmalloc(sizeof(*l), GFP_KERNEL);
  757. if (!l)
  758. return NULL;
  759. spin_lock_init(&l->lock);
  760. INIT_LIST_HEAD(&l->freeme);
  761. return l;
  762. }
  763. static void
  764. nfsd_free_fcache_disposal(struct nfsd_fcache_disposal *l)
  765. {
  766. nfsd_file_dispose_list(&l->freeme);
  767. kfree(l);
  768. }
  769. static void
  770. nfsd_free_fcache_disposal_net(struct net *net)
  771. {
  772. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  773. struct nfsd_fcache_disposal *l = nn->fcache_disposal;
  774. nfsd_free_fcache_disposal(l);
  775. }
  776. int
  777. nfsd_file_cache_start_net(struct net *net)
  778. {
  779. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  780. nn->fcache_disposal = nfsd_alloc_fcache_disposal();
  781. return nn->fcache_disposal ? 0 : -ENOMEM;
  782. }
  783. /**
  784. * nfsd_file_cache_purge - Remove all cache items associated with @net
  785. * @net: target net namespace
  786. *
  787. */
  788. void
  789. nfsd_file_cache_purge(struct net *net)
  790. {
  791. lockdep_assert_held(&nfsd_mutex);
  792. if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1)
  793. __nfsd_file_cache_purge(net);
  794. }
  795. void
  796. nfsd_file_cache_shutdown_net(struct net *net)
  797. {
  798. nfsd_file_cache_purge(net);
  799. nfsd_free_fcache_disposal_net(net);
  800. }
  801. void
  802. nfsd_file_cache_shutdown(void)
  803. {
  804. int i;
  805. lockdep_assert_held(&nfsd_mutex);
  806. if (test_and_clear_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 0)
  807. return;
  808. lease_unregister_notifier(&nfsd_file_lease_notifier);
  809. shrinker_free(nfsd_file_shrinker);
  810. /*
  811. * make sure all callers of nfsd_file_lru_cb are done before
  812. * calling nfsd_file_cache_purge
  813. */
  814. cancel_delayed_work_sync(&nfsd_filecache_laundrette);
  815. __nfsd_file_cache_purge(NULL);
  816. list_lru_destroy(&nfsd_file_lru);
  817. rcu_barrier();
  818. fsnotify_put_group(nfsd_file_fsnotify_group);
  819. nfsd_file_fsnotify_group = NULL;
  820. kmem_cache_destroy(nfsd_file_slab);
  821. nfsd_file_slab = NULL;
  822. fsnotify_wait_marks_destroyed();
  823. kmem_cache_destroy(nfsd_file_mark_slab);
  824. nfsd_file_mark_slab = NULL;
  825. rhltable_destroy(&nfsd_file_rhltable);
  826. for_each_possible_cpu(i) {
  827. per_cpu(nfsd_file_cache_hits, i) = 0;
  828. per_cpu(nfsd_file_acquisitions, i) = 0;
  829. per_cpu(nfsd_file_allocations, i) = 0;
  830. per_cpu(nfsd_file_releases, i) = 0;
  831. per_cpu(nfsd_file_total_age, i) = 0;
  832. per_cpu(nfsd_file_evictions, i) = 0;
  833. }
  834. }
  835. static struct nfsd_file *
  836. nfsd_file_lookup_locked(const struct net *net, const struct cred *cred,
  837. struct inode *inode, unsigned char need,
  838. bool want_gc)
  839. {
  840. struct rhlist_head *tmp, *list;
  841. struct nfsd_file *nf;
  842. list = rhltable_lookup(&nfsd_file_rhltable, &inode,
  843. nfsd_file_rhash_params);
  844. rhl_for_each_entry_rcu(nf, tmp, list, nf_rlist) {
  845. if (nf->nf_may != need)
  846. continue;
  847. if (nf->nf_net != net)
  848. continue;
  849. if (!nfsd_match_cred(nf->nf_cred, cred))
  850. continue;
  851. if (test_bit(NFSD_FILE_GC, &nf->nf_flags) != want_gc)
  852. continue;
  853. if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0)
  854. continue;
  855. if (!nfsd_file_get(nf))
  856. continue;
  857. return nf;
  858. }
  859. return NULL;
  860. }
  861. /**
  862. * nfsd_file_is_cached - are there any cached open files for this inode?
  863. * @inode: inode to check
  864. *
  865. * The lookup matches inodes in all net namespaces and is atomic wrt
  866. * nfsd_file_acquire().
  867. *
  868. * Return values:
  869. * %true: filecache contains at least one file matching this inode
  870. * %false: filecache contains no files matching this inode
  871. */
  872. bool
  873. nfsd_file_is_cached(struct inode *inode)
  874. {
  875. struct rhlist_head *tmp, *list;
  876. struct nfsd_file *nf;
  877. bool ret = false;
  878. rcu_read_lock();
  879. list = rhltable_lookup(&nfsd_file_rhltable, &inode,
  880. nfsd_file_rhash_params);
  881. rhl_for_each_entry_rcu(nf, tmp, list, nf_rlist)
  882. if (test_bit(NFSD_FILE_GC, &nf->nf_flags)) {
  883. ret = true;
  884. break;
  885. }
  886. rcu_read_unlock();
  887. trace_nfsd_file_is_cached(inode, (int)ret);
  888. return ret;
  889. }
  890. static __be32
  891. nfsd_file_do_acquire(struct svc_rqst *rqstp, struct net *net,
  892. struct svc_cred *cred,
  893. struct auth_domain *client,
  894. struct svc_fh *fhp,
  895. unsigned int may_flags, struct file *file,
  896. struct nfsd_file **pnf, bool want_gc)
  897. {
  898. unsigned char need = may_flags & NFSD_FILE_MAY_MASK;
  899. struct nfsd_file *new, *nf;
  900. bool stale_retry = true;
  901. bool open_retry = true;
  902. struct inode *inode;
  903. __be32 status;
  904. int ret;
  905. retry:
  906. if (rqstp) {
  907. status = fh_verify(rqstp, fhp, S_IFREG,
  908. may_flags|NFSD_MAY_OWNER_OVERRIDE);
  909. } else {
  910. status = fh_verify_local(net, cred, client, fhp, S_IFREG,
  911. may_flags|NFSD_MAY_OWNER_OVERRIDE);
  912. }
  913. if (status != nfs_ok)
  914. return status;
  915. inode = d_inode(fhp->fh_dentry);
  916. rcu_read_lock();
  917. nf = nfsd_file_lookup_locked(net, current_cred(), inode, need, want_gc);
  918. rcu_read_unlock();
  919. if (nf) {
  920. /*
  921. * If the nf is on the LRU then it holds an extra reference
  922. * that must be put if it's removed. It had better not be
  923. * the last one however, since we should hold another.
  924. */
  925. if (nfsd_file_lru_remove(nf))
  926. WARN_ON_ONCE(refcount_dec_and_test(&nf->nf_ref));
  927. goto wait_for_construction;
  928. }
  929. new = nfsd_file_alloc(net, inode, need, want_gc);
  930. if (!new) {
  931. status = nfserr_jukebox;
  932. goto out;
  933. }
  934. rcu_read_lock();
  935. spin_lock(&inode->i_lock);
  936. nf = nfsd_file_lookup_locked(net, current_cred(), inode, need, want_gc);
  937. if (unlikely(nf)) {
  938. spin_unlock(&inode->i_lock);
  939. rcu_read_unlock();
  940. nfsd_file_free(new);
  941. goto wait_for_construction;
  942. }
  943. nf = new;
  944. ret = rhltable_insert(&nfsd_file_rhltable, &nf->nf_rlist,
  945. nfsd_file_rhash_params);
  946. spin_unlock(&inode->i_lock);
  947. rcu_read_unlock();
  948. if (likely(ret == 0))
  949. goto open_file;
  950. trace_nfsd_file_insert_err(rqstp, inode, may_flags, ret);
  951. status = nfserr_jukebox;
  952. goto construction_err;
  953. wait_for_construction:
  954. wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE);
  955. /* Did construction of this file fail? */
  956. if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
  957. trace_nfsd_file_cons_err(rqstp, inode, may_flags, nf);
  958. if (!open_retry) {
  959. status = nfserr_jukebox;
  960. goto construction_err;
  961. }
  962. nfsd_file_put(nf);
  963. open_retry = false;
  964. fh_put(fhp);
  965. goto retry;
  966. }
  967. this_cpu_inc(nfsd_file_cache_hits);
  968. status = nfserrno(nfsd_open_break_lease(file_inode(nf->nf_file), may_flags));
  969. if (status != nfs_ok) {
  970. nfsd_file_put(nf);
  971. nf = NULL;
  972. }
  973. out:
  974. if (status == nfs_ok) {
  975. this_cpu_inc(nfsd_file_acquisitions);
  976. nfsd_file_check_write_error(nf);
  977. *pnf = nf;
  978. }
  979. trace_nfsd_file_acquire(rqstp, inode, may_flags, nf, status);
  980. return status;
  981. open_file:
  982. trace_nfsd_file_alloc(nf);
  983. nf->nf_mark = nfsd_file_mark_find_or_create(inode);
  984. if (nf->nf_mark) {
  985. if (file) {
  986. get_file(file);
  987. nf->nf_file = file;
  988. status = nfs_ok;
  989. trace_nfsd_file_opened(nf, status);
  990. } else {
  991. ret = nfsd_open_verified(rqstp, fhp, may_flags,
  992. &nf->nf_file);
  993. if (ret == -EOPENSTALE && stale_retry) {
  994. stale_retry = false;
  995. nfsd_file_unhash(nf);
  996. clear_and_wake_up_bit(NFSD_FILE_PENDING,
  997. &nf->nf_flags);
  998. if (refcount_dec_and_test(&nf->nf_ref))
  999. nfsd_file_free(nf);
  1000. nf = NULL;
  1001. fh_put(fhp);
  1002. goto retry;
  1003. }
  1004. status = nfserrno(ret);
  1005. trace_nfsd_file_open(nf, status);
  1006. }
  1007. } else
  1008. status = nfserr_jukebox;
  1009. /*
  1010. * If construction failed, or we raced with a call to unlink()
  1011. * then unhash.
  1012. */
  1013. if (status != nfs_ok || inode->i_nlink == 0)
  1014. nfsd_file_unhash(nf);
  1015. clear_and_wake_up_bit(NFSD_FILE_PENDING, &nf->nf_flags);
  1016. if (status == nfs_ok)
  1017. goto out;
  1018. construction_err:
  1019. if (refcount_dec_and_test(&nf->nf_ref))
  1020. nfsd_file_free(nf);
  1021. nf = NULL;
  1022. goto out;
  1023. }
  1024. /**
  1025. * nfsd_file_acquire_gc - Get a struct nfsd_file with an open file
  1026. * @rqstp: the RPC transaction being executed
  1027. * @fhp: the NFS filehandle of the file to be opened
  1028. * @may_flags: NFSD_MAY_ settings for the file
  1029. * @pnf: OUT: new or found "struct nfsd_file" object
  1030. *
  1031. * The nfsd_file object returned by this API is reference-counted
  1032. * and garbage-collected. The object is retained for a few
  1033. * seconds after the final nfsd_file_put() in case the caller
  1034. * wants to re-use it.
  1035. *
  1036. * Return values:
  1037. * %nfs_ok - @pnf points to an nfsd_file with its reference
  1038. * count boosted.
  1039. *
  1040. * On error, an nfsstat value in network byte order is returned.
  1041. */
  1042. __be32
  1043. nfsd_file_acquire_gc(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1044. unsigned int may_flags, struct nfsd_file **pnf)
  1045. {
  1046. return nfsd_file_do_acquire(rqstp, SVC_NET(rqstp), NULL, NULL,
  1047. fhp, may_flags, NULL, pnf, true);
  1048. }
  1049. /**
  1050. * nfsd_file_acquire - Get a struct nfsd_file with an open file
  1051. * @rqstp: the RPC transaction being executed
  1052. * @fhp: the NFS filehandle of the file to be opened
  1053. * @may_flags: NFSD_MAY_ settings for the file
  1054. * @pnf: OUT: new or found "struct nfsd_file" object
  1055. *
  1056. * The nfsd_file_object returned by this API is reference-counted
  1057. * but not garbage-collected. The object is unhashed after the
  1058. * final nfsd_file_put().
  1059. *
  1060. * Return values:
  1061. * %nfs_ok - @pnf points to an nfsd_file with its reference
  1062. * count boosted.
  1063. *
  1064. * On error, an nfsstat value in network byte order is returned.
  1065. */
  1066. __be32
  1067. nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1068. unsigned int may_flags, struct nfsd_file **pnf)
  1069. {
  1070. return nfsd_file_do_acquire(rqstp, SVC_NET(rqstp), NULL, NULL,
  1071. fhp, may_flags, NULL, pnf, false);
  1072. }
  1073. /**
  1074. * nfsd_file_acquire_local - Get a struct nfsd_file with an open file for localio
  1075. * @net: The network namespace in which to perform a lookup
  1076. * @cred: the user credential with which to validate access
  1077. * @client: the auth_domain for LOCALIO lookup
  1078. * @fhp: the NFS filehandle of the file to be opened
  1079. * @may_flags: NFSD_MAY_ settings for the file
  1080. * @pnf: OUT: new or found "struct nfsd_file" object
  1081. *
  1082. * This file lookup interface provide access to a file given the
  1083. * filehandle and credential. No connection-based authorisation
  1084. * is performed and in that way it is quite different to other
  1085. * file access mediated by nfsd. It allows a kernel module such as the NFS
  1086. * client to reach across network and filesystem namespaces to access
  1087. * a file. The security implications of this should be carefully
  1088. * considered before use.
  1089. *
  1090. * The nfsd_file object returned by this API is reference-counted
  1091. * and garbage-collected. The object is retained for a few
  1092. * seconds after the final nfsd_file_put() in case the caller
  1093. * wants to re-use it.
  1094. *
  1095. * Return values:
  1096. * %nfs_ok - @pnf points to an nfsd_file with its reference
  1097. * count boosted.
  1098. *
  1099. * On error, an nfsstat value in network byte order is returned.
  1100. */
  1101. __be32
  1102. nfsd_file_acquire_local(struct net *net, struct svc_cred *cred,
  1103. struct auth_domain *client, struct svc_fh *fhp,
  1104. unsigned int may_flags, struct nfsd_file **pnf)
  1105. {
  1106. /*
  1107. * Save creds before calling nfsd_file_do_acquire() (which calls
  1108. * nfsd_setuser). Important because caller (LOCALIO) is from
  1109. * client context.
  1110. */
  1111. const struct cred *save_cred = get_current_cred();
  1112. __be32 beres;
  1113. beres = nfsd_file_do_acquire(NULL, net, cred, client,
  1114. fhp, may_flags, NULL, pnf, true);
  1115. revert_creds(save_cred);
  1116. return beres;
  1117. }
  1118. /**
  1119. * nfsd_file_acquire_opened - Get a struct nfsd_file using existing open file
  1120. * @rqstp: the RPC transaction being executed
  1121. * @fhp: the NFS filehandle of the file just created
  1122. * @may_flags: NFSD_MAY_ settings for the file
  1123. * @file: cached, already-open file (may be NULL)
  1124. * @pnf: OUT: new or found "struct nfsd_file" object
  1125. *
  1126. * Acquire a nfsd_file object that is not GC'ed. If one doesn't already exist,
  1127. * and @file is non-NULL, use it to instantiate a new nfsd_file instead of
  1128. * opening a new one.
  1129. *
  1130. * Return values:
  1131. * %nfs_ok - @pnf points to an nfsd_file with its reference
  1132. * count boosted.
  1133. *
  1134. * On error, an nfsstat value in network byte order is returned.
  1135. */
  1136. __be32
  1137. nfsd_file_acquire_opened(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1138. unsigned int may_flags, struct file *file,
  1139. struct nfsd_file **pnf)
  1140. {
  1141. return nfsd_file_do_acquire(rqstp, SVC_NET(rqstp), NULL, NULL,
  1142. fhp, may_flags, file, pnf, false);
  1143. }
  1144. /*
  1145. * Note that fields may be added, removed or reordered in the future. Programs
  1146. * scraping this file for info should test the labels to ensure they're
  1147. * getting the correct field.
  1148. */
  1149. int nfsd_file_cache_stats_show(struct seq_file *m, void *v)
  1150. {
  1151. unsigned long allocations = 0, releases = 0, evictions = 0;
  1152. unsigned long hits = 0, acquisitions = 0;
  1153. unsigned int i, count = 0, buckets = 0;
  1154. unsigned long lru = 0, total_age = 0;
  1155. /* Serialize with server shutdown */
  1156. mutex_lock(&nfsd_mutex);
  1157. if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1) {
  1158. struct bucket_table *tbl;
  1159. struct rhashtable *ht;
  1160. lru = list_lru_count(&nfsd_file_lru);
  1161. rcu_read_lock();
  1162. ht = &nfsd_file_rhltable.ht;
  1163. count = atomic_read(&ht->nelems);
  1164. tbl = rht_dereference_rcu(ht->tbl, ht);
  1165. buckets = tbl->size;
  1166. rcu_read_unlock();
  1167. }
  1168. mutex_unlock(&nfsd_mutex);
  1169. for_each_possible_cpu(i) {
  1170. hits += per_cpu(nfsd_file_cache_hits, i);
  1171. acquisitions += per_cpu(nfsd_file_acquisitions, i);
  1172. allocations += per_cpu(nfsd_file_allocations, i);
  1173. releases += per_cpu(nfsd_file_releases, i);
  1174. total_age += per_cpu(nfsd_file_total_age, i);
  1175. evictions += per_cpu(nfsd_file_evictions, i);
  1176. }
  1177. seq_printf(m, "total inodes: %u\n", count);
  1178. seq_printf(m, "hash buckets: %u\n", buckets);
  1179. seq_printf(m, "lru entries: %lu\n", lru);
  1180. seq_printf(m, "cache hits: %lu\n", hits);
  1181. seq_printf(m, "acquisitions: %lu\n", acquisitions);
  1182. seq_printf(m, "allocations: %lu\n", allocations);
  1183. seq_printf(m, "releases: %lu\n", releases);
  1184. seq_printf(m, "evictions: %lu\n", evictions);
  1185. if (releases)
  1186. seq_printf(m, "mean age (ms): %ld\n", total_age / releases);
  1187. else
  1188. seq_printf(m, "mean age (ms): -\n");
  1189. return 0;
  1190. }