export.c 32 KB

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