export.c 32 KB

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