filecache.c 38 KB

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