export.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NFS exporting and validation.
  4. *
  5. * We maintain a list of clients, each of which has a list of
  6. * exports. To export an fs to a given client, you first have
  7. * to create the client entry with NFSCTL_ADDCLIENT, which
  8. * creates a client control block and adds it to the hash
  9. * table. Then, you call NFSCTL_EXPORT for each fs.
  10. *
  11. *
  12. * Copyright (C) 1995, 1996 Olaf Kirch, <okir@monad.swb.de>
  13. */
  14. #include <linux/slab.h>
  15. #include <linux/namei.h>
  16. #include <linux/module.h>
  17. #include <linux/exportfs.h>
  18. #include <linux/sunrpc/svc_xprt.h>
  19. #include "nfsd.h"
  20. #include "nfsfh.h"
  21. #include "netns.h"
  22. #include "pnfs.h"
  23. #define NFSDDBG_FACILITY NFSDDBG_EXPORT
  24. /*
  25. * We have two caches.
  26. * One maps client+vfsmnt+dentry to export options - the export map
  27. * The other maps client+filehandle-fragment to export options. - the expkey map
  28. *
  29. * The export options are actually stored in the first map, and the
  30. * second map contains a reference to the entry in the first map.
  31. */
  32. #define EXPKEY_HASHBITS 8
  33. #define EXPKEY_HASHMAX (1 << EXPKEY_HASHBITS)
  34. #define EXPKEY_HASHMASK (EXPKEY_HASHMAX -1)
  35. static void expkey_put(struct kref *ref)
  36. {
  37. struct svc_expkey *key = container_of(ref, struct svc_expkey, h.ref);
  38. if (test_bit(CACHE_VALID, &key->h.flags) &&
  39. !test_bit(CACHE_NEGATIVE, &key->h.flags))
  40. path_put(&key->ek_path);
  41. auth_domain_put(key->ek_client);
  42. kfree(key);
  43. }
  44. static void expkey_request(struct cache_detail *cd,
  45. struct cache_head *h,
  46. char **bpp, int *blen)
  47. {
  48. /* client fsidtype \xfsid */
  49. struct svc_expkey *ek = container_of(h, struct svc_expkey, h);
  50. char type[5];
  51. qword_add(bpp, blen, ek->ek_client->name);
  52. snprintf(type, 5, "%d", ek->ek_fsidtype);
  53. qword_add(bpp, blen, type);
  54. qword_addhex(bpp, blen, (char*)ek->ek_fsid, key_len(ek->ek_fsidtype));
  55. (*bpp)[-1] = '\n';
  56. }
  57. static struct svc_expkey *svc_expkey_update(struct cache_detail *cd, struct svc_expkey *new,
  58. struct svc_expkey *old);
  59. static struct svc_expkey *svc_expkey_lookup(struct cache_detail *cd, struct svc_expkey *);
  60. static int expkey_parse(struct cache_detail *cd, char *mesg, int mlen)
  61. {
  62. /* client fsidtype fsid expiry [path] */
  63. char *buf;
  64. int len;
  65. struct auth_domain *dom = NULL;
  66. int err;
  67. int fsidtype;
  68. char *ep;
  69. struct svc_expkey key;
  70. struct svc_expkey *ek = NULL;
  71. if (mesg[mlen - 1] != '\n')
  72. return -EINVAL;
  73. mesg[mlen-1] = 0;
  74. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  75. err = -ENOMEM;
  76. if (!buf)
  77. goto out;
  78. err = -EINVAL;
  79. if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
  80. goto out;
  81. err = -ENOENT;
  82. dom = auth_domain_find(buf);
  83. if (!dom)
  84. goto out;
  85. dprintk("found domain %s\n", buf);
  86. err = -EINVAL;
  87. if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
  88. goto out;
  89. fsidtype = simple_strtoul(buf, &ep, 10);
  90. if (*ep)
  91. goto out;
  92. dprintk("found fsidtype %d\n", fsidtype);
  93. if (key_len(fsidtype)==0) /* invalid type */
  94. goto out;
  95. if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
  96. goto out;
  97. dprintk("found fsid length %d\n", len);
  98. if (len != key_len(fsidtype))
  99. goto out;
  100. /* OK, we seem to have a valid key */
  101. key.h.flags = 0;
  102. key.h.expiry_time = get_expiry(&mesg);
  103. if (key.h.expiry_time == 0)
  104. goto out;
  105. key.ek_client = dom;
  106. key.ek_fsidtype = fsidtype;
  107. memcpy(key.ek_fsid, buf, len);
  108. ek = svc_expkey_lookup(cd, &key);
  109. err = -ENOMEM;
  110. if (!ek)
  111. goto out;
  112. /* now we want a pathname, or empty meaning NEGATIVE */
  113. err = -EINVAL;
  114. len = qword_get(&mesg, buf, PAGE_SIZE);
  115. if (len < 0)
  116. goto out;
  117. dprintk("Path seems to be <%s>\n", buf);
  118. err = 0;
  119. if (len == 0) {
  120. set_bit(CACHE_NEGATIVE, &key.h.flags);
  121. ek = svc_expkey_update(cd, &key, ek);
  122. if (!ek)
  123. err = -ENOMEM;
  124. } else {
  125. err = kern_path(buf, 0, &key.ek_path);
  126. if (err)
  127. goto out;
  128. dprintk("Found the path %s\n", buf);
  129. ek = svc_expkey_update(cd, &key, ek);
  130. if (!ek)
  131. err = -ENOMEM;
  132. path_put(&key.ek_path);
  133. }
  134. cache_flush();
  135. out:
  136. if (ek)
  137. cache_put(&ek->h, cd);
  138. if (dom)
  139. auth_domain_put(dom);
  140. kfree(buf);
  141. return err;
  142. }
  143. static int expkey_show(struct seq_file *m,
  144. struct cache_detail *cd,
  145. struct cache_head *h)
  146. {
  147. struct svc_expkey *ek ;
  148. int i;
  149. if (h ==NULL) {
  150. seq_puts(m, "#domain fsidtype fsid [path]\n");
  151. return 0;
  152. }
  153. ek = container_of(h, struct svc_expkey, h);
  154. seq_printf(m, "%s %d 0x", ek->ek_client->name,
  155. ek->ek_fsidtype);
  156. for (i=0; i < key_len(ek->ek_fsidtype)/4; i++)
  157. seq_printf(m, "%08x", ek->ek_fsid[i]);
  158. if (test_bit(CACHE_VALID, &h->flags) &&
  159. !test_bit(CACHE_NEGATIVE, &h->flags)) {
  160. seq_printf(m, " ");
  161. seq_path(m, &ek->ek_path, "\\ \t\n");
  162. }
  163. seq_printf(m, "\n");
  164. return 0;
  165. }
  166. static inline int expkey_match (struct cache_head *a, struct cache_head *b)
  167. {
  168. struct svc_expkey *orig = container_of(a, struct svc_expkey, h);
  169. struct svc_expkey *new = container_of(b, struct svc_expkey, h);
  170. if (orig->ek_fsidtype != new->ek_fsidtype ||
  171. orig->ek_client != new->ek_client ||
  172. memcmp(orig->ek_fsid, new->ek_fsid, key_len(orig->ek_fsidtype)) != 0)
  173. return 0;
  174. return 1;
  175. }
  176. static inline void expkey_init(struct cache_head *cnew,
  177. struct cache_head *citem)
  178. {
  179. struct svc_expkey *new = container_of(cnew, struct svc_expkey, h);
  180. struct svc_expkey *item = container_of(citem, struct svc_expkey, h);
  181. kref_get(&item->ek_client->ref);
  182. new->ek_client = item->ek_client;
  183. new->ek_fsidtype = item->ek_fsidtype;
  184. memcpy(new->ek_fsid, item->ek_fsid, sizeof(new->ek_fsid));
  185. }
  186. static inline void expkey_update(struct cache_head *cnew,
  187. struct cache_head *citem)
  188. {
  189. struct svc_expkey *new = container_of(cnew, struct svc_expkey, h);
  190. struct svc_expkey *item = container_of(citem, struct svc_expkey, h);
  191. new->ek_path = item->ek_path;
  192. path_get(&item->ek_path);
  193. }
  194. static struct cache_head *expkey_alloc(void)
  195. {
  196. struct svc_expkey *i = kmalloc(sizeof(*i), GFP_KERNEL);
  197. if (i)
  198. return &i->h;
  199. else
  200. return NULL;
  201. }
  202. static const struct cache_detail svc_expkey_cache_template = {
  203. .owner = THIS_MODULE,
  204. .hash_size = EXPKEY_HASHMAX,
  205. .name = "nfsd.fh",
  206. .cache_put = expkey_put,
  207. .cache_request = expkey_request,
  208. .cache_parse = expkey_parse,
  209. .cache_show = expkey_show,
  210. .match = expkey_match,
  211. .init = expkey_init,
  212. .update = expkey_update,
  213. .alloc = expkey_alloc,
  214. };
  215. static int
  216. svc_expkey_hash(struct svc_expkey *item)
  217. {
  218. int hash = item->ek_fsidtype;
  219. char * cp = (char*)item->ek_fsid;
  220. int len = key_len(item->ek_fsidtype);
  221. hash ^= hash_mem(cp, len, EXPKEY_HASHBITS);
  222. hash ^= hash_ptr(item->ek_client, EXPKEY_HASHBITS);
  223. hash &= EXPKEY_HASHMASK;
  224. return hash;
  225. }
  226. static struct svc_expkey *
  227. svc_expkey_lookup(struct cache_detail *cd, struct svc_expkey *item)
  228. {
  229. struct cache_head *ch;
  230. int hash = svc_expkey_hash(item);
  231. ch = sunrpc_cache_lookup(cd, &item->h, hash);
  232. if (ch)
  233. return container_of(ch, struct svc_expkey, h);
  234. else
  235. return NULL;
  236. }
  237. static struct svc_expkey *
  238. svc_expkey_update(struct cache_detail *cd, struct svc_expkey *new,
  239. struct svc_expkey *old)
  240. {
  241. struct cache_head *ch;
  242. int hash = svc_expkey_hash(new);
  243. ch = sunrpc_cache_update(cd, &new->h, &old->h, hash);
  244. if (ch)
  245. return container_of(ch, struct svc_expkey, h);
  246. else
  247. return NULL;
  248. }
  249. #define EXPORT_HASHBITS 8
  250. #define EXPORT_HASHMAX (1<< EXPORT_HASHBITS)
  251. static void nfsd4_fslocs_free(struct nfsd4_fs_locations *fsloc)
  252. {
  253. struct nfsd4_fs_location *locations = fsloc->locations;
  254. int i;
  255. if (!locations)
  256. return;
  257. for (i = 0; i < fsloc->locations_count; i++) {
  258. kfree(locations[i].path);
  259. kfree(locations[i].hosts);
  260. }
  261. kfree(locations);
  262. fsloc->locations = NULL;
  263. }
  264. static void svc_export_put(struct kref *ref)
  265. {
  266. struct svc_export *exp = container_of(ref, struct svc_export, h.ref);
  267. path_put(&exp->ex_path);
  268. auth_domain_put(exp->ex_client);
  269. nfsd4_fslocs_free(&exp->ex_fslocs);
  270. kfree(exp->ex_uuid);
  271. kfree(exp);
  272. }
  273. static void svc_export_request(struct cache_detail *cd,
  274. struct cache_head *h,
  275. char **bpp, int *blen)
  276. {
  277. /* client path */
  278. struct svc_export *exp = container_of(h, struct svc_export, h);
  279. char *pth;
  280. qword_add(bpp, blen, exp->ex_client->name);
  281. pth = d_path(&exp->ex_path, *bpp, *blen);
  282. if (IS_ERR(pth)) {
  283. /* is this correct? */
  284. (*bpp)[0] = '\n';
  285. return;
  286. }
  287. qword_add(bpp, blen, pth);
  288. (*bpp)[-1] = '\n';
  289. }
  290. static struct svc_export *svc_export_update(struct svc_export *new,
  291. struct svc_export *old);
  292. static struct svc_export *svc_export_lookup(struct svc_export *);
  293. static int check_export(struct inode *inode, int *flags, unsigned char *uuid)
  294. {
  295. /*
  296. * We currently export only dirs, regular files, and (for v4
  297. * pseudoroot) symlinks.
  298. */
  299. if (!S_ISDIR(inode->i_mode) &&
  300. !S_ISLNK(inode->i_mode) &&
  301. !S_ISREG(inode->i_mode))
  302. return -ENOTDIR;
  303. /*
  304. * Mountd should never pass down a writeable V4ROOT export, but,
  305. * just to make sure:
  306. */
  307. if (*flags & NFSEXP_V4ROOT)
  308. *flags |= NFSEXP_READONLY;
  309. /* There are two requirements on a filesystem to be exportable.
  310. * 1: We must be able to identify the filesystem from a number.
  311. * either a device number (so FS_REQUIRES_DEV needed)
  312. * or an FSID number (so NFSEXP_FSID or ->uuid is needed).
  313. * 2: We must be able to find an inode from a filehandle.
  314. * This means that s_export_op must be set.
  315. */
  316. if (!(inode->i_sb->s_type->fs_flags & FS_REQUIRES_DEV) &&
  317. !(*flags & NFSEXP_FSID) &&
  318. uuid == NULL) {
  319. dprintk("exp_export: export of non-dev fs without fsid\n");
  320. return -EINVAL;
  321. }
  322. if (!inode->i_sb->s_export_op ||
  323. !inode->i_sb->s_export_op->fh_to_dentry) {
  324. dprintk("exp_export: export of invalid fs type.\n");
  325. return -EINVAL;
  326. }
  327. return 0;
  328. }
  329. #ifdef CONFIG_NFSD_V4
  330. static int
  331. fsloc_parse(char **mesg, char *buf, struct nfsd4_fs_locations *fsloc)
  332. {
  333. int len;
  334. int migrated, i, err;
  335. /* more than one fsloc */
  336. if (fsloc->locations)
  337. return -EINVAL;
  338. /* listsize */
  339. err = get_uint(mesg, &fsloc->locations_count);
  340. if (err)
  341. return err;
  342. if (fsloc->locations_count > MAX_FS_LOCATIONS)
  343. return -EINVAL;
  344. if (fsloc->locations_count == 0)
  345. return 0;
  346. fsloc->locations = kcalloc(fsloc->locations_count,
  347. sizeof(struct nfsd4_fs_location),
  348. GFP_KERNEL);
  349. if (!fsloc->locations)
  350. return -ENOMEM;
  351. for (i=0; i < fsloc->locations_count; i++) {
  352. /* colon separated host list */
  353. err = -EINVAL;
  354. len = qword_get(mesg, buf, PAGE_SIZE);
  355. if (len <= 0)
  356. goto out_free_all;
  357. err = -ENOMEM;
  358. fsloc->locations[i].hosts = kstrdup(buf, GFP_KERNEL);
  359. if (!fsloc->locations[i].hosts)
  360. goto out_free_all;
  361. err = -EINVAL;
  362. /* slash separated path component list */
  363. len = qword_get(mesg, buf, PAGE_SIZE);
  364. if (len <= 0)
  365. goto out_free_all;
  366. err = -ENOMEM;
  367. fsloc->locations[i].path = kstrdup(buf, GFP_KERNEL);
  368. if (!fsloc->locations[i].path)
  369. goto out_free_all;
  370. }
  371. /* migrated */
  372. err = get_int(mesg, &migrated);
  373. if (err)
  374. goto out_free_all;
  375. err = -EINVAL;
  376. if (migrated < 0 || migrated > 1)
  377. goto out_free_all;
  378. fsloc->migrated = migrated;
  379. return 0;
  380. out_free_all:
  381. nfsd4_fslocs_free(fsloc);
  382. return err;
  383. }
  384. static int secinfo_parse(char **mesg, char *buf, struct svc_export *exp)
  385. {
  386. struct exp_flavor_info *f;
  387. u32 listsize;
  388. int err;
  389. /* more than one secinfo */
  390. if (exp->ex_nflavors)
  391. return -EINVAL;
  392. err = get_uint(mesg, &listsize);
  393. if (err)
  394. return err;
  395. if (listsize > MAX_SECINFO_LIST)
  396. return -EINVAL;
  397. for (f = exp->ex_flavors; f < exp->ex_flavors + listsize; f++) {
  398. err = get_uint(mesg, &f->pseudoflavor);
  399. if (err)
  400. return err;
  401. /*
  402. * XXX: It would be nice to also check whether this
  403. * pseudoflavor is supported, so we can discover the
  404. * problem at export time instead of when a client fails
  405. * to authenticate.
  406. */
  407. err = get_uint(mesg, &f->flags);
  408. if (err)
  409. return err;
  410. /* Only some flags are allowed to differ between flavors: */
  411. if (~NFSEXP_SECINFO_FLAGS & (f->flags ^ exp->ex_flags))
  412. return -EINVAL;
  413. }
  414. exp->ex_nflavors = listsize;
  415. return 0;
  416. }
  417. #else /* CONFIG_NFSD_V4 */
  418. static inline int
  419. fsloc_parse(char **mesg, char *buf, struct nfsd4_fs_locations *fsloc){return 0;}
  420. static inline int
  421. secinfo_parse(char **mesg, char *buf, struct svc_export *exp) { return 0; }
  422. #endif
  423. static inline int
  424. nfsd_uuid_parse(char **mesg, char *buf, unsigned char **puuid)
  425. {
  426. int len;
  427. /* more than one uuid */
  428. if (*puuid)
  429. return -EINVAL;
  430. /* expect a 16 byte uuid encoded as \xXXXX... */
  431. len = qword_get(mesg, buf, PAGE_SIZE);
  432. if (len != EX_UUID_LEN)
  433. return -EINVAL;
  434. *puuid = kmemdup(buf, EX_UUID_LEN, GFP_KERNEL);
  435. if (*puuid == NULL)
  436. return -ENOMEM;
  437. return 0;
  438. }
  439. static int svc_export_parse(struct cache_detail *cd, char *mesg, int mlen)
  440. {
  441. /* client path expiry [flags anonuid anongid fsid] */
  442. char *buf;
  443. int len;
  444. int err;
  445. struct auth_domain *dom = NULL;
  446. struct svc_export exp = {}, *expp;
  447. int an_int;
  448. if (mesg[mlen-1] != '\n')
  449. return -EINVAL;
  450. mesg[mlen-1] = 0;
  451. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  452. if (!buf)
  453. return -ENOMEM;
  454. /* client */
  455. err = -EINVAL;
  456. len = qword_get(&mesg, buf, PAGE_SIZE);
  457. if (len <= 0)
  458. goto out;
  459. err = -ENOENT;
  460. dom = auth_domain_find(buf);
  461. if (!dom)
  462. goto out;
  463. /* path */
  464. err = -EINVAL;
  465. if ((len = qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
  466. goto out1;
  467. err = kern_path(buf, 0, &exp.ex_path);
  468. if (err)
  469. goto out1;
  470. exp.ex_client = dom;
  471. exp.cd = cd;
  472. exp.ex_devid_map = NULL;
  473. /* expiry */
  474. err = -EINVAL;
  475. exp.h.expiry_time = get_expiry(&mesg);
  476. if (exp.h.expiry_time == 0)
  477. goto out3;
  478. /* flags */
  479. err = get_int(&mesg, &an_int);
  480. if (err == -ENOENT) {
  481. err = 0;
  482. set_bit(CACHE_NEGATIVE, &exp.h.flags);
  483. } else {
  484. if (err || an_int < 0)
  485. goto out3;
  486. exp.ex_flags= an_int;
  487. /* anon uid */
  488. err = get_int(&mesg, &an_int);
  489. if (err)
  490. goto out3;
  491. exp.ex_anon_uid= make_kuid(&init_user_ns, an_int);
  492. /* anon gid */
  493. err = get_int(&mesg, &an_int);
  494. if (err)
  495. goto out3;
  496. exp.ex_anon_gid= make_kgid(&init_user_ns, an_int);
  497. /* fsid */
  498. err = get_int(&mesg, &an_int);
  499. if (err)
  500. goto out3;
  501. exp.ex_fsid = an_int;
  502. while ((len = qword_get(&mesg, buf, PAGE_SIZE)) > 0) {
  503. if (strcmp(buf, "fsloc") == 0)
  504. err = fsloc_parse(&mesg, buf, &exp.ex_fslocs);
  505. else if (strcmp(buf, "uuid") == 0)
  506. err = nfsd_uuid_parse(&mesg, buf, &exp.ex_uuid);
  507. else if (strcmp(buf, "secinfo") == 0)
  508. err = secinfo_parse(&mesg, buf, &exp);
  509. else
  510. /* quietly ignore unknown words and anything
  511. * following. Newer user-space can try to set
  512. * new values, then see what the result was.
  513. */
  514. break;
  515. if (err)
  516. goto out4;
  517. }
  518. err = check_export(d_inode(exp.ex_path.dentry), &exp.ex_flags,
  519. exp.ex_uuid);
  520. if (err)
  521. goto out4;
  522. /*
  523. * No point caching this if it would immediately expire.
  524. * Also, this protects exportfs's dummy export from the
  525. * anon_uid/anon_gid checks:
  526. */
  527. if (exp.h.expiry_time < seconds_since_boot())
  528. goto out4;
  529. /*
  530. * For some reason exportfs has been passing down an
  531. * invalid (-1) uid & gid on the "dummy" export which it
  532. * uses to test export support. To make sure exportfs
  533. * sees errors from check_export we therefore need to
  534. * delay these checks till after check_export:
  535. */
  536. err = -EINVAL;
  537. if (!uid_valid(exp.ex_anon_uid))
  538. goto out4;
  539. if (!gid_valid(exp.ex_anon_gid))
  540. goto out4;
  541. err = 0;
  542. nfsd4_setup_layout_type(&exp);
  543. }
  544. expp = svc_export_lookup(&exp);
  545. if (expp)
  546. expp = svc_export_update(&exp, expp);
  547. else
  548. err = -ENOMEM;
  549. cache_flush();
  550. if (expp == NULL)
  551. err = -ENOMEM;
  552. else
  553. exp_put(expp);
  554. out4:
  555. nfsd4_fslocs_free(&exp.ex_fslocs);
  556. kfree(exp.ex_uuid);
  557. out3:
  558. path_put(&exp.ex_path);
  559. out1:
  560. auth_domain_put(dom);
  561. out:
  562. kfree(buf);
  563. return err;
  564. }
  565. static void exp_flags(struct seq_file *m, int flag, int fsid,
  566. kuid_t anonu, kgid_t anong, struct nfsd4_fs_locations *fslocs);
  567. static void show_secinfo(struct seq_file *m, struct svc_export *exp);
  568. static int svc_export_show(struct seq_file *m,
  569. struct cache_detail *cd,
  570. struct cache_head *h)
  571. {
  572. struct svc_export *exp ;
  573. if (h ==NULL) {
  574. seq_puts(m, "#path domain(flags)\n");
  575. return 0;
  576. }
  577. exp = container_of(h, struct svc_export, h);
  578. seq_path(m, &exp->ex_path, " \t\n\\");
  579. seq_putc(m, '\t');
  580. seq_escape(m, exp->ex_client->name, " \t\n\\");
  581. seq_putc(m, '(');
  582. if (test_bit(CACHE_VALID, &h->flags) &&
  583. !test_bit(CACHE_NEGATIVE, &h->flags)) {
  584. exp_flags(m, exp->ex_flags, exp->ex_fsid,
  585. exp->ex_anon_uid, exp->ex_anon_gid, &exp->ex_fslocs);
  586. if (exp->ex_uuid) {
  587. int i;
  588. seq_puts(m, ",uuid=");
  589. for (i = 0; i < EX_UUID_LEN; i++) {
  590. if ((i&3) == 0 && i)
  591. seq_putc(m, ':');
  592. seq_printf(m, "%02x", exp->ex_uuid[i]);
  593. }
  594. }
  595. show_secinfo(m, exp);
  596. }
  597. seq_puts(m, ")\n");
  598. return 0;
  599. }
  600. static int svc_export_match(struct cache_head *a, struct cache_head *b)
  601. {
  602. struct svc_export *orig = container_of(a, struct svc_export, h);
  603. struct svc_export *new = container_of(b, struct svc_export, h);
  604. return orig->ex_client == new->ex_client &&
  605. path_equal(&orig->ex_path, &new->ex_path);
  606. }
  607. static void svc_export_init(struct cache_head *cnew, struct cache_head *citem)
  608. {
  609. struct svc_export *new = container_of(cnew, struct svc_export, h);
  610. struct svc_export *item = container_of(citem, struct svc_export, h);
  611. kref_get(&item->ex_client->ref);
  612. new->ex_client = item->ex_client;
  613. new->ex_path = item->ex_path;
  614. path_get(&item->ex_path);
  615. new->ex_fslocs.locations = NULL;
  616. new->ex_fslocs.locations_count = 0;
  617. new->ex_fslocs.migrated = 0;
  618. new->ex_layout_types = 0;
  619. new->ex_uuid = NULL;
  620. new->cd = item->cd;
  621. }
  622. static void export_update(struct cache_head *cnew, struct cache_head *citem)
  623. {
  624. struct svc_export *new = container_of(cnew, struct svc_export, h);
  625. struct svc_export *item = container_of(citem, struct svc_export, h);
  626. int i;
  627. new->ex_flags = item->ex_flags;
  628. new->ex_anon_uid = item->ex_anon_uid;
  629. new->ex_anon_gid = item->ex_anon_gid;
  630. new->ex_fsid = item->ex_fsid;
  631. new->ex_devid_map = item->ex_devid_map;
  632. item->ex_devid_map = NULL;
  633. new->ex_uuid = item->ex_uuid;
  634. item->ex_uuid = NULL;
  635. new->ex_fslocs.locations = item->ex_fslocs.locations;
  636. item->ex_fslocs.locations = NULL;
  637. new->ex_fslocs.locations_count = item->ex_fslocs.locations_count;
  638. item->ex_fslocs.locations_count = 0;
  639. new->ex_fslocs.migrated = item->ex_fslocs.migrated;
  640. item->ex_fslocs.migrated = 0;
  641. new->ex_layout_types = item->ex_layout_types;
  642. new->ex_nflavors = item->ex_nflavors;
  643. for (i = 0; i < MAX_SECINFO_LIST; i++) {
  644. new->ex_flavors[i] = item->ex_flavors[i];
  645. }
  646. }
  647. static struct cache_head *svc_export_alloc(void)
  648. {
  649. struct svc_export *i = kmalloc(sizeof(*i), GFP_KERNEL);
  650. if (i)
  651. return &i->h;
  652. else
  653. return NULL;
  654. }
  655. static const struct cache_detail svc_export_cache_template = {
  656. .owner = THIS_MODULE,
  657. .hash_size = EXPORT_HASHMAX,
  658. .name = "nfsd.export",
  659. .cache_put = svc_export_put,
  660. .cache_request = svc_export_request,
  661. .cache_parse = svc_export_parse,
  662. .cache_show = svc_export_show,
  663. .match = svc_export_match,
  664. .init = svc_export_init,
  665. .update = export_update,
  666. .alloc = svc_export_alloc,
  667. };
  668. static int
  669. svc_export_hash(struct svc_export *exp)
  670. {
  671. int hash;
  672. hash = hash_ptr(exp->ex_client, EXPORT_HASHBITS);
  673. hash ^= hash_ptr(exp->ex_path.dentry, EXPORT_HASHBITS);
  674. hash ^= hash_ptr(exp->ex_path.mnt, EXPORT_HASHBITS);
  675. return hash;
  676. }
  677. static struct svc_export *
  678. svc_export_lookup(struct svc_export *exp)
  679. {
  680. struct cache_head *ch;
  681. int hash = svc_export_hash(exp);
  682. ch = sunrpc_cache_lookup(exp->cd, &exp->h, hash);
  683. if (ch)
  684. return container_of(ch, struct svc_export, h);
  685. else
  686. return NULL;
  687. }
  688. static struct svc_export *
  689. svc_export_update(struct svc_export *new, struct svc_export *old)
  690. {
  691. struct cache_head *ch;
  692. int hash = svc_export_hash(old);
  693. ch = sunrpc_cache_update(old->cd, &new->h, &old->h, hash);
  694. if (ch)
  695. return container_of(ch, struct svc_export, h);
  696. else
  697. return NULL;
  698. }
  699. static struct svc_expkey *
  700. exp_find_key(struct cache_detail *cd, struct auth_domain *clp, int fsid_type,
  701. u32 *fsidv, struct cache_req *reqp)
  702. {
  703. struct svc_expkey key, *ek;
  704. int err;
  705. if (!clp)
  706. return ERR_PTR(-ENOENT);
  707. key.ek_client = clp;
  708. key.ek_fsidtype = fsid_type;
  709. memcpy(key.ek_fsid, fsidv, key_len(fsid_type));
  710. ek = svc_expkey_lookup(cd, &key);
  711. if (ek == NULL)
  712. return ERR_PTR(-ENOMEM);
  713. err = cache_check(cd, &ek->h, reqp);
  714. if (err)
  715. return ERR_PTR(err);
  716. return ek;
  717. }
  718. static struct svc_export *
  719. exp_get_by_name(struct cache_detail *cd, struct auth_domain *clp,
  720. const struct path *path, struct cache_req *reqp)
  721. {
  722. struct svc_export *exp, key;
  723. int err;
  724. if (!clp)
  725. return ERR_PTR(-ENOENT);
  726. key.ex_client = clp;
  727. key.ex_path = *path;
  728. key.cd = cd;
  729. exp = svc_export_lookup(&key);
  730. if (exp == NULL)
  731. return ERR_PTR(-ENOMEM);
  732. err = cache_check(cd, &exp->h, reqp);
  733. if (err)
  734. return ERR_PTR(err);
  735. return exp;
  736. }
  737. /*
  738. * Find the export entry for a given dentry.
  739. */
  740. static struct svc_export *
  741. exp_parent(struct cache_detail *cd, struct auth_domain *clp, struct path *path)
  742. {
  743. struct dentry *saved = dget(path->dentry);
  744. struct svc_export *exp = exp_get_by_name(cd, clp, path, NULL);
  745. while (PTR_ERR(exp) == -ENOENT && !IS_ROOT(path->dentry)) {
  746. struct dentry *parent = dget_parent(path->dentry);
  747. dput(path->dentry);
  748. path->dentry = parent;
  749. exp = exp_get_by_name(cd, clp, path, NULL);
  750. }
  751. dput(path->dentry);
  752. path->dentry = saved;
  753. return exp;
  754. }
  755. /*
  756. * Obtain the root fh on behalf of a client.
  757. * This could be done in user space, but I feel that it adds some safety
  758. * since its harder to fool a kernel module than a user space program.
  759. */
  760. int
  761. exp_rootfh(struct net *net, struct auth_domain *clp, char *name,
  762. struct knfsd_fh *f, int maxsize)
  763. {
  764. struct svc_export *exp;
  765. struct path path;
  766. struct inode *inode;
  767. struct svc_fh fh;
  768. int err;
  769. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  770. struct cache_detail *cd = nn->svc_export_cache;
  771. err = -EPERM;
  772. /* NB: we probably ought to check that it's NUL-terminated */
  773. if (kern_path(name, 0, &path)) {
  774. printk("nfsd: exp_rootfh path not found %s", name);
  775. return err;
  776. }
  777. inode = d_inode(path.dentry);
  778. dprintk("nfsd: exp_rootfh(%s [%p] %s:%s/%ld)\n",
  779. name, path.dentry, clp->name,
  780. inode->i_sb->s_id, inode->i_ino);
  781. exp = exp_parent(cd, clp, &path);
  782. if (IS_ERR(exp)) {
  783. err = PTR_ERR(exp);
  784. goto out;
  785. }
  786. /*
  787. * fh must be initialized before calling fh_compose
  788. */
  789. fh_init(&fh, maxsize);
  790. if (fh_compose(&fh, exp, path.dentry, NULL))
  791. err = -EINVAL;
  792. else
  793. err = 0;
  794. memcpy(f, &fh.fh_handle, sizeof(struct knfsd_fh));
  795. fh_put(&fh);
  796. exp_put(exp);
  797. out:
  798. path_put(&path);
  799. return err;
  800. }
  801. static struct svc_export *exp_find(struct cache_detail *cd,
  802. struct auth_domain *clp, int fsid_type,
  803. u32 *fsidv, struct cache_req *reqp)
  804. {
  805. struct svc_export *exp;
  806. struct nfsd_net *nn = net_generic(cd->net, nfsd_net_id);
  807. struct svc_expkey *ek = exp_find_key(nn->svc_expkey_cache, clp, fsid_type, fsidv, reqp);
  808. if (IS_ERR(ek))
  809. return ERR_CAST(ek);
  810. exp = exp_get_by_name(cd, clp, &ek->ek_path, reqp);
  811. cache_put(&ek->h, nn->svc_expkey_cache);
  812. if (IS_ERR(exp))
  813. return ERR_CAST(exp);
  814. return exp;
  815. }
  816. __be32 check_nfsd_access(struct svc_export *exp, struct svc_rqst *rqstp)
  817. {
  818. struct exp_flavor_info *f;
  819. struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;
  820. /* legacy gss-only clients are always OK: */
  821. if (exp->ex_client == rqstp->rq_gssclient)
  822. return 0;
  823. /* ip-address based client; check sec= export option: */
  824. for (f = exp->ex_flavors; f < end; f++) {
  825. if (f->pseudoflavor == rqstp->rq_cred.cr_flavor)
  826. return 0;
  827. }
  828. /* defaults in absence of sec= options: */
  829. if (exp->ex_nflavors == 0) {
  830. if (rqstp->rq_cred.cr_flavor == RPC_AUTH_NULL ||
  831. rqstp->rq_cred.cr_flavor == RPC_AUTH_UNIX)
  832. return 0;
  833. }
  834. /* If the compound op contains a spo_must_allowed op,
  835. * it will be sent with integrity/protection which
  836. * will have to be expressly allowed on mounts that
  837. * don't support it
  838. */
  839. if (nfsd4_spo_must_allow(rqstp))
  840. return 0;
  841. return nfserr_wrongsec;
  842. }
  843. /*
  844. * Uses rq_client and rq_gssclient to find an export; uses rq_client (an
  845. * auth_unix client) if it's available and has secinfo information;
  846. * otherwise, will try to use rq_gssclient.
  847. *
  848. * Called from functions that handle requests; functions that do work on
  849. * behalf of mountd are passed a single client name to use, and should
  850. * use exp_get_by_name() or exp_find().
  851. */
  852. struct svc_export *
  853. rqst_exp_get_by_name(struct svc_rqst *rqstp, struct path *path)
  854. {
  855. struct svc_export *gssexp, *exp = ERR_PTR(-ENOENT);
  856. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  857. struct cache_detail *cd = nn->svc_export_cache;
  858. if (rqstp->rq_client == NULL)
  859. goto gss;
  860. /* First try the auth_unix client: */
  861. exp = exp_get_by_name(cd, rqstp->rq_client, path, &rqstp->rq_chandle);
  862. if (PTR_ERR(exp) == -ENOENT)
  863. goto gss;
  864. if (IS_ERR(exp))
  865. return exp;
  866. /* If it has secinfo, assume there are no gss/... clients */
  867. if (exp->ex_nflavors > 0)
  868. return exp;
  869. gss:
  870. /* Otherwise, try falling back on gss client */
  871. if (rqstp->rq_gssclient == NULL)
  872. return exp;
  873. gssexp = exp_get_by_name(cd, rqstp->rq_gssclient, path, &rqstp->rq_chandle);
  874. if (PTR_ERR(gssexp) == -ENOENT)
  875. return exp;
  876. if (!IS_ERR(exp))
  877. exp_put(exp);
  878. return gssexp;
  879. }
  880. struct svc_export *
  881. rqst_exp_find(struct svc_rqst *rqstp, int fsid_type, u32 *fsidv)
  882. {
  883. struct svc_export *gssexp, *exp = ERR_PTR(-ENOENT);
  884. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  885. struct cache_detail *cd = nn->svc_export_cache;
  886. if (rqstp->rq_client == NULL)
  887. goto gss;
  888. /* First try the auth_unix client: */
  889. exp = exp_find(cd, rqstp->rq_client, fsid_type,
  890. fsidv, &rqstp->rq_chandle);
  891. if (PTR_ERR(exp) == -ENOENT)
  892. goto gss;
  893. if (IS_ERR(exp))
  894. return exp;
  895. /* If it has secinfo, assume there are no gss/... clients */
  896. if (exp->ex_nflavors > 0)
  897. return exp;
  898. gss:
  899. /* Otherwise, try falling back on gss client */
  900. if (rqstp->rq_gssclient == NULL)
  901. return exp;
  902. gssexp = exp_find(cd, rqstp->rq_gssclient, fsid_type, fsidv,
  903. &rqstp->rq_chandle);
  904. if (PTR_ERR(gssexp) == -ENOENT)
  905. return exp;
  906. if (!IS_ERR(exp))
  907. exp_put(exp);
  908. return gssexp;
  909. }
  910. struct svc_export *
  911. rqst_exp_parent(struct svc_rqst *rqstp, struct path *path)
  912. {
  913. struct dentry *saved = dget(path->dentry);
  914. struct svc_export *exp = rqst_exp_get_by_name(rqstp, path);
  915. while (PTR_ERR(exp) == -ENOENT && !IS_ROOT(path->dentry)) {
  916. struct dentry *parent = dget_parent(path->dentry);
  917. dput(path->dentry);
  918. path->dentry = parent;
  919. exp = rqst_exp_get_by_name(rqstp, path);
  920. }
  921. dput(path->dentry);
  922. path->dentry = saved;
  923. return exp;
  924. }
  925. struct svc_export *rqst_find_fsidzero_export(struct svc_rqst *rqstp)
  926. {
  927. u32 fsidv[2];
  928. mk_fsid(FSID_NUM, fsidv, 0, 0, 0, NULL);
  929. return rqst_exp_find(rqstp, FSID_NUM, fsidv);
  930. }
  931. /*
  932. * Called when we need the filehandle for the root of the pseudofs,
  933. * for a given NFSv4 client. The root is defined to be the
  934. * export point with fsid==0
  935. */
  936. __be32
  937. exp_pseudoroot(struct svc_rqst *rqstp, struct svc_fh *fhp)
  938. {
  939. struct svc_export *exp;
  940. __be32 rv;
  941. exp = rqst_find_fsidzero_export(rqstp);
  942. if (IS_ERR(exp))
  943. return nfserrno(PTR_ERR(exp));
  944. rv = fh_compose(fhp, exp, exp->ex_path.dentry, NULL);
  945. exp_put(exp);
  946. return rv;
  947. }
  948. static struct flags {
  949. int flag;
  950. char *name[2];
  951. } expflags[] = {
  952. { NFSEXP_READONLY, {"ro", "rw"}},
  953. { NFSEXP_INSECURE_PORT, {"insecure", ""}},
  954. { NFSEXP_ROOTSQUASH, {"root_squash", "no_root_squash"}},
  955. { NFSEXP_ALLSQUASH, {"all_squash", ""}},
  956. { NFSEXP_ASYNC, {"async", "sync"}},
  957. { NFSEXP_GATHERED_WRITES, {"wdelay", "no_wdelay"}},
  958. { NFSEXP_NOREADDIRPLUS, {"nordirplus", ""}},
  959. { NFSEXP_NOHIDE, {"nohide", ""}},
  960. { NFSEXP_CROSSMOUNT, {"crossmnt", ""}},
  961. { NFSEXP_NOSUBTREECHECK, {"no_subtree_check", ""}},
  962. { NFSEXP_NOAUTHNLM, {"insecure_locks", ""}},
  963. { NFSEXP_V4ROOT, {"v4root", ""}},
  964. { NFSEXP_PNFS, {"pnfs", ""}},
  965. { NFSEXP_SECURITY_LABEL, {"security_label", ""}},
  966. { 0, {"", ""}}
  967. };
  968. static void show_expflags(struct seq_file *m, int flags, int mask)
  969. {
  970. struct flags *flg;
  971. int state, first = 0;
  972. for (flg = expflags; flg->flag; flg++) {
  973. if (flg->flag & ~mask)
  974. continue;
  975. state = (flg->flag & flags) ? 0 : 1;
  976. if (*flg->name[state])
  977. seq_printf(m, "%s%s", first++?",":"", flg->name[state]);
  978. }
  979. }
  980. static void show_secinfo_flags(struct seq_file *m, int flags)
  981. {
  982. seq_printf(m, ",");
  983. show_expflags(m, flags, NFSEXP_SECINFO_FLAGS);
  984. }
  985. static bool secinfo_flags_equal(int f, int g)
  986. {
  987. f &= NFSEXP_SECINFO_FLAGS;
  988. g &= NFSEXP_SECINFO_FLAGS;
  989. return f == g;
  990. }
  991. static int show_secinfo_run(struct seq_file *m, struct exp_flavor_info **fp, struct exp_flavor_info *end)
  992. {
  993. int flags;
  994. flags = (*fp)->flags;
  995. seq_printf(m, ",sec=%d", (*fp)->pseudoflavor);
  996. (*fp)++;
  997. while (*fp != end && secinfo_flags_equal(flags, (*fp)->flags)) {
  998. seq_printf(m, ":%d", (*fp)->pseudoflavor);
  999. (*fp)++;
  1000. }
  1001. return flags;
  1002. }
  1003. static void show_secinfo(struct seq_file *m, struct svc_export *exp)
  1004. {
  1005. struct exp_flavor_info *f;
  1006. struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;
  1007. int flags;
  1008. if (exp->ex_nflavors == 0)
  1009. return;
  1010. f = exp->ex_flavors;
  1011. flags = show_secinfo_run(m, &f, end);
  1012. if (!secinfo_flags_equal(flags, exp->ex_flags))
  1013. show_secinfo_flags(m, flags);
  1014. while (f != end) {
  1015. flags = show_secinfo_run(m, &f, end);
  1016. show_secinfo_flags(m, flags);
  1017. }
  1018. }
  1019. static void exp_flags(struct seq_file *m, int flag, int fsid,
  1020. kuid_t anonu, kgid_t anong, struct nfsd4_fs_locations *fsloc)
  1021. {
  1022. show_expflags(m, flag, NFSEXP_ALLFLAGS);
  1023. if (flag & NFSEXP_FSID)
  1024. seq_printf(m, ",fsid=%d", fsid);
  1025. if (!uid_eq(anonu, make_kuid(&init_user_ns, (uid_t)-2)) &&
  1026. !uid_eq(anonu, make_kuid(&init_user_ns, 0x10000-2)))
  1027. seq_printf(m, ",anonuid=%u", from_kuid(&init_user_ns, anonu));
  1028. if (!gid_eq(anong, make_kgid(&init_user_ns, (gid_t)-2)) &&
  1029. !gid_eq(anong, make_kgid(&init_user_ns, 0x10000-2)))
  1030. seq_printf(m, ",anongid=%u", from_kgid(&init_user_ns, anong));
  1031. if (fsloc && fsloc->locations_count > 0) {
  1032. char *loctype = (fsloc->migrated) ? "refer" : "replicas";
  1033. int i;
  1034. seq_printf(m, ",%s=", loctype);
  1035. seq_escape(m, fsloc->locations[0].path, ",;@ \t\n\\");
  1036. seq_putc(m, '@');
  1037. seq_escape(m, fsloc->locations[0].hosts, ",;@ \t\n\\");
  1038. for (i = 1; i < fsloc->locations_count; i++) {
  1039. seq_putc(m, ';');
  1040. seq_escape(m, fsloc->locations[i].path, ",;@ \t\n\\");
  1041. seq_putc(m, '@');
  1042. seq_escape(m, fsloc->locations[i].hosts, ",;@ \t\n\\");
  1043. }
  1044. }
  1045. }
  1046. static int e_show(struct seq_file *m, void *p)
  1047. {
  1048. struct cache_head *cp = p;
  1049. struct svc_export *exp = container_of(cp, struct svc_export, h);
  1050. struct cache_detail *cd = m->private;
  1051. if (p == SEQ_START_TOKEN) {
  1052. seq_puts(m, "# Version 1.1\n");
  1053. seq_puts(m, "# Path Client(Flags) # IPs\n");
  1054. return 0;
  1055. }
  1056. exp_get(exp);
  1057. if (cache_check(cd, &exp->h, NULL))
  1058. return 0;
  1059. exp_put(exp);
  1060. return svc_export_show(m, cd, cp);
  1061. }
  1062. const struct seq_operations nfs_exports_op = {
  1063. .start = cache_seq_start,
  1064. .next = cache_seq_next,
  1065. .stop = cache_seq_stop,
  1066. .show = e_show,
  1067. };
  1068. /*
  1069. * Initialize the exports module.
  1070. */
  1071. int
  1072. nfsd_export_init(struct net *net)
  1073. {
  1074. int rv;
  1075. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1076. dprintk("nfsd: initializing export module (net: %x).\n", net->ns.inum);
  1077. nn->svc_export_cache = cache_create_net(&svc_export_cache_template, net);
  1078. if (IS_ERR(nn->svc_export_cache))
  1079. return PTR_ERR(nn->svc_export_cache);
  1080. rv = cache_register_net(nn->svc_export_cache, net);
  1081. if (rv)
  1082. goto destroy_export_cache;
  1083. nn->svc_expkey_cache = cache_create_net(&svc_expkey_cache_template, net);
  1084. if (IS_ERR(nn->svc_expkey_cache)) {
  1085. rv = PTR_ERR(nn->svc_expkey_cache);
  1086. goto unregister_export_cache;
  1087. }
  1088. rv = cache_register_net(nn->svc_expkey_cache, net);
  1089. if (rv)
  1090. goto destroy_expkey_cache;
  1091. return 0;
  1092. destroy_expkey_cache:
  1093. cache_destroy_net(nn->svc_expkey_cache, net);
  1094. unregister_export_cache:
  1095. cache_unregister_net(nn->svc_export_cache, net);
  1096. destroy_export_cache:
  1097. cache_destroy_net(nn->svc_export_cache, net);
  1098. return rv;
  1099. }
  1100. /*
  1101. * Flush exports table - called when last nfsd thread is killed
  1102. */
  1103. void
  1104. nfsd_export_flush(struct net *net)
  1105. {
  1106. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1107. cache_purge(nn->svc_expkey_cache);
  1108. cache_purge(nn->svc_export_cache);
  1109. }
  1110. /*
  1111. * Shutdown the exports module.
  1112. */
  1113. void
  1114. nfsd_export_shutdown(struct net *net)
  1115. {
  1116. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1117. dprintk("nfsd: shutting down export module (net: %x).\n", net->ns.inum);
  1118. cache_unregister_net(nn->svc_expkey_cache, net);
  1119. cache_unregister_net(nn->svc_export_cache, net);
  1120. cache_destroy_net(nn->svc_expkey_cache, net);
  1121. cache_destroy_net(nn->svc_export_cache, net);
  1122. svcauth_unix_purge(net);
  1123. dprintk("nfsd: export shutdown complete (net: %x).\n", net->ns.inum);
  1124. }