export.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  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 expiry [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. /* more than one fsloc */
  334. if (fsloc->locations)
  335. return -EINVAL;
  336. /* listsize */
  337. err = get_uint(mesg, &fsloc->locations_count);
  338. if (err)
  339. return err;
  340. if (fsloc->locations_count > MAX_FS_LOCATIONS)
  341. return -EINVAL;
  342. if (fsloc->locations_count == 0)
  343. return 0;
  344. fsloc->locations = kzalloc(fsloc->locations_count
  345. * sizeof(struct nfsd4_fs_location), GFP_KERNEL);
  346. if (!fsloc->locations)
  347. return -ENOMEM;
  348. for (i=0; i < fsloc->locations_count; i++) {
  349. /* colon separated host list */
  350. err = -EINVAL;
  351. len = qword_get(mesg, buf, PAGE_SIZE);
  352. if (len <= 0)
  353. goto out_free_all;
  354. err = -ENOMEM;
  355. fsloc->locations[i].hosts = kstrdup(buf, GFP_KERNEL);
  356. if (!fsloc->locations[i].hosts)
  357. goto out_free_all;
  358. err = -EINVAL;
  359. /* slash separated path component list */
  360. len = qword_get(mesg, buf, PAGE_SIZE);
  361. if (len <= 0)
  362. goto out_free_all;
  363. err = -ENOMEM;
  364. fsloc->locations[i].path = kstrdup(buf, GFP_KERNEL);
  365. if (!fsloc->locations[i].path)
  366. goto out_free_all;
  367. }
  368. /* migrated */
  369. err = get_int(mesg, &migrated);
  370. if (err)
  371. goto out_free_all;
  372. err = -EINVAL;
  373. if (migrated < 0 || migrated > 1)
  374. goto out_free_all;
  375. fsloc->migrated = migrated;
  376. return 0;
  377. out_free_all:
  378. nfsd4_fslocs_free(fsloc);
  379. return err;
  380. }
  381. static int secinfo_parse(char **mesg, char *buf, struct svc_export *exp)
  382. {
  383. struct exp_flavor_info *f;
  384. u32 listsize;
  385. int err;
  386. /* more than one secinfo */
  387. if (exp->ex_nflavors)
  388. return -EINVAL;
  389. err = get_uint(mesg, &listsize);
  390. if (err)
  391. return err;
  392. if (listsize > MAX_SECINFO_LIST)
  393. return -EINVAL;
  394. for (f = exp->ex_flavors; f < exp->ex_flavors + listsize; f++) {
  395. err = get_uint(mesg, &f->pseudoflavor);
  396. if (err)
  397. return err;
  398. /*
  399. * XXX: It would be nice to also check whether this
  400. * pseudoflavor is supported, so we can discover the
  401. * problem at export time instead of when a client fails
  402. * to authenticate.
  403. */
  404. err = get_uint(mesg, &f->flags);
  405. if (err)
  406. return err;
  407. /* Only some flags are allowed to differ between flavors: */
  408. if (~NFSEXP_SECINFO_FLAGS & (f->flags ^ exp->ex_flags))
  409. return -EINVAL;
  410. }
  411. exp->ex_nflavors = listsize;
  412. return 0;
  413. }
  414. #else /* CONFIG_NFSD_V4 */
  415. static inline int
  416. fsloc_parse(char **mesg, char *buf, struct nfsd4_fs_locations *fsloc){return 0;}
  417. static inline int
  418. secinfo_parse(char **mesg, char *buf, struct svc_export *exp) { return 0; }
  419. #endif
  420. static inline int
  421. uuid_parse(char **mesg, char *buf, unsigned char **puuid)
  422. {
  423. int len;
  424. /* more than one uuid */
  425. if (*puuid)
  426. return -EINVAL;
  427. /* expect a 16 byte uuid encoded as \xXXXX... */
  428. len = qword_get(mesg, buf, PAGE_SIZE);
  429. if (len != EX_UUID_LEN)
  430. return -EINVAL;
  431. *puuid = kmemdup(buf, EX_UUID_LEN, GFP_KERNEL);
  432. if (*puuid == NULL)
  433. return -ENOMEM;
  434. return 0;
  435. }
  436. static int svc_export_parse(struct cache_detail *cd, char *mesg, int mlen)
  437. {
  438. /* client path expiry [flags anonuid anongid fsid] */
  439. char *buf;
  440. int len;
  441. int err;
  442. struct auth_domain *dom = NULL;
  443. struct svc_export exp = {}, *expp;
  444. int an_int;
  445. if (mesg[mlen-1] != '\n')
  446. return -EINVAL;
  447. mesg[mlen-1] = 0;
  448. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  449. if (!buf)
  450. return -ENOMEM;
  451. /* client */
  452. err = -EINVAL;
  453. len = qword_get(&mesg, buf, PAGE_SIZE);
  454. if (len <= 0)
  455. goto out;
  456. err = -ENOENT;
  457. dom = auth_domain_find(buf);
  458. if (!dom)
  459. goto out;
  460. /* path */
  461. err = -EINVAL;
  462. if ((len = qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
  463. goto out1;
  464. err = kern_path(buf, 0, &exp.ex_path);
  465. if (err)
  466. goto out1;
  467. exp.ex_client = dom;
  468. exp.cd = cd;
  469. /* expiry */
  470. err = -EINVAL;
  471. exp.h.expiry_time = get_expiry(&mesg);
  472. if (exp.h.expiry_time == 0)
  473. goto out3;
  474. /* flags */
  475. err = get_int(&mesg, &an_int);
  476. if (err == -ENOENT) {
  477. err = 0;
  478. set_bit(CACHE_NEGATIVE, &exp.h.flags);
  479. } else {
  480. if (err || an_int < 0)
  481. goto out3;
  482. exp.ex_flags= an_int;
  483. /* anon uid */
  484. err = get_int(&mesg, &an_int);
  485. if (err)
  486. goto out3;
  487. exp.ex_anon_uid= make_kuid(&init_user_ns, an_int);
  488. /* anon gid */
  489. err = get_int(&mesg, &an_int);
  490. if (err)
  491. goto out3;
  492. exp.ex_anon_gid= make_kgid(&init_user_ns, an_int);
  493. /* fsid */
  494. err = get_int(&mesg, &an_int);
  495. if (err)
  496. goto out3;
  497. exp.ex_fsid = an_int;
  498. while ((len = qword_get(&mesg, buf, PAGE_SIZE)) > 0) {
  499. if (strcmp(buf, "fsloc") == 0)
  500. err = fsloc_parse(&mesg, buf, &exp.ex_fslocs);
  501. else if (strcmp(buf, "uuid") == 0)
  502. err = uuid_parse(&mesg, buf, &exp.ex_uuid);
  503. else if (strcmp(buf, "secinfo") == 0)
  504. err = secinfo_parse(&mesg, buf, &exp);
  505. else
  506. /* quietly ignore unknown words and anything
  507. * following. Newer user-space can try to set
  508. * new values, then see what the result was.
  509. */
  510. break;
  511. if (err)
  512. goto out4;
  513. }
  514. err = check_export(exp.ex_path.dentry->d_inode, &exp.ex_flags,
  515. exp.ex_uuid);
  516. if (err)
  517. goto out4;
  518. /*
  519. * No point caching this if it would immediately expire.
  520. * Also, this protects exportfs's dummy export from the
  521. * anon_uid/anon_gid checks:
  522. */
  523. if (exp.h.expiry_time < seconds_since_boot())
  524. goto out4;
  525. /*
  526. * For some reason exportfs has been passing down an
  527. * invalid (-1) uid & gid on the "dummy" export which it
  528. * uses to test export support. To make sure exportfs
  529. * sees errors from check_export we therefore need to
  530. * delay these checks till after check_export:
  531. */
  532. err = -EINVAL;
  533. if (!uid_valid(exp.ex_anon_uid))
  534. goto out4;
  535. if (!gid_valid(exp.ex_anon_gid))
  536. goto out4;
  537. err = 0;
  538. }
  539. expp = svc_export_lookup(&exp);
  540. if (expp)
  541. expp = svc_export_update(&exp, expp);
  542. else
  543. err = -ENOMEM;
  544. cache_flush();
  545. if (expp == NULL)
  546. err = -ENOMEM;
  547. else
  548. exp_put(expp);
  549. out4:
  550. nfsd4_fslocs_free(&exp.ex_fslocs);
  551. kfree(exp.ex_uuid);
  552. out3:
  553. path_put(&exp.ex_path);
  554. out1:
  555. auth_domain_put(dom);
  556. out:
  557. kfree(buf);
  558. return err;
  559. }
  560. static void exp_flags(struct seq_file *m, int flag, int fsid,
  561. kuid_t anonu, kgid_t anong, struct nfsd4_fs_locations *fslocs);
  562. static void show_secinfo(struct seq_file *m, struct svc_export *exp);
  563. static int svc_export_show(struct seq_file *m,
  564. struct cache_detail *cd,
  565. struct cache_head *h)
  566. {
  567. struct svc_export *exp ;
  568. if (h ==NULL) {
  569. seq_puts(m, "#path domain(flags)\n");
  570. return 0;
  571. }
  572. exp = container_of(h, struct svc_export, h);
  573. seq_path(m, &exp->ex_path, " \t\n\\");
  574. seq_putc(m, '\t');
  575. seq_escape(m, exp->ex_client->name, " \t\n\\");
  576. seq_putc(m, '(');
  577. if (test_bit(CACHE_VALID, &h->flags) &&
  578. !test_bit(CACHE_NEGATIVE, &h->flags)) {
  579. exp_flags(m, exp->ex_flags, exp->ex_fsid,
  580. exp->ex_anon_uid, exp->ex_anon_gid, &exp->ex_fslocs);
  581. if (exp->ex_uuid) {
  582. int i;
  583. seq_puts(m, ",uuid=");
  584. for (i = 0; i < EX_UUID_LEN; i++) {
  585. if ((i&3) == 0 && i)
  586. seq_putc(m, ':');
  587. seq_printf(m, "%02x", exp->ex_uuid[i]);
  588. }
  589. }
  590. show_secinfo(m, exp);
  591. }
  592. seq_puts(m, ")\n");
  593. return 0;
  594. }
  595. static int svc_export_match(struct cache_head *a, struct cache_head *b)
  596. {
  597. struct svc_export *orig = container_of(a, struct svc_export, h);
  598. struct svc_export *new = container_of(b, struct svc_export, h);
  599. return orig->ex_client == new->ex_client &&
  600. orig->ex_path.dentry == new->ex_path.dentry &&
  601. orig->ex_path.mnt == new->ex_path.mnt;
  602. }
  603. static void svc_export_init(struct cache_head *cnew, struct cache_head *citem)
  604. {
  605. struct svc_export *new = container_of(cnew, struct svc_export, h);
  606. struct svc_export *item = container_of(citem, struct svc_export, h);
  607. kref_get(&item->ex_client->ref);
  608. new->ex_client = item->ex_client;
  609. new->ex_path.dentry = dget(item->ex_path.dentry);
  610. new->ex_path.mnt = mntget(item->ex_path.mnt);
  611. new->ex_fslocs.locations = NULL;
  612. new->ex_fslocs.locations_count = 0;
  613. new->ex_fslocs.migrated = 0;
  614. new->ex_uuid = NULL;
  615. new->cd = item->cd;
  616. }
  617. static void export_update(struct cache_head *cnew, struct cache_head *citem)
  618. {
  619. struct svc_export *new = container_of(cnew, struct svc_export, h);
  620. struct svc_export *item = container_of(citem, struct svc_export, h);
  621. int i;
  622. new->ex_flags = item->ex_flags;
  623. new->ex_anon_uid = item->ex_anon_uid;
  624. new->ex_anon_gid = item->ex_anon_gid;
  625. new->ex_fsid = item->ex_fsid;
  626. new->ex_uuid = item->ex_uuid;
  627. item->ex_uuid = NULL;
  628. new->ex_fslocs.locations = item->ex_fslocs.locations;
  629. item->ex_fslocs.locations = NULL;
  630. new->ex_fslocs.locations_count = item->ex_fslocs.locations_count;
  631. item->ex_fslocs.locations_count = 0;
  632. new->ex_fslocs.migrated = item->ex_fslocs.migrated;
  633. item->ex_fslocs.migrated = 0;
  634. new->ex_nflavors = item->ex_nflavors;
  635. for (i = 0; i < MAX_SECINFO_LIST; i++) {
  636. new->ex_flavors[i] = item->ex_flavors[i];
  637. }
  638. }
  639. static struct cache_head *svc_export_alloc(void)
  640. {
  641. struct svc_export *i = kmalloc(sizeof(*i), GFP_KERNEL);
  642. if (i)
  643. return &i->h;
  644. else
  645. return NULL;
  646. }
  647. static struct cache_detail svc_export_cache_template = {
  648. .owner = THIS_MODULE,
  649. .hash_size = EXPORT_HASHMAX,
  650. .name = "nfsd.export",
  651. .cache_put = svc_export_put,
  652. .cache_request = svc_export_request,
  653. .cache_parse = svc_export_parse,
  654. .cache_show = svc_export_show,
  655. .match = svc_export_match,
  656. .init = svc_export_init,
  657. .update = export_update,
  658. .alloc = svc_export_alloc,
  659. };
  660. static int
  661. svc_export_hash(struct svc_export *exp)
  662. {
  663. int hash;
  664. hash = hash_ptr(exp->ex_client, EXPORT_HASHBITS);
  665. hash ^= hash_ptr(exp->ex_path.dentry, EXPORT_HASHBITS);
  666. hash ^= hash_ptr(exp->ex_path.mnt, EXPORT_HASHBITS);
  667. return hash;
  668. }
  669. static struct svc_export *
  670. svc_export_lookup(struct svc_export *exp)
  671. {
  672. struct cache_head *ch;
  673. int hash = svc_export_hash(exp);
  674. ch = sunrpc_cache_lookup(exp->cd, &exp->h, hash);
  675. if (ch)
  676. return container_of(ch, struct svc_export, h);
  677. else
  678. return NULL;
  679. }
  680. static struct svc_export *
  681. svc_export_update(struct svc_export *new, struct svc_export *old)
  682. {
  683. struct cache_head *ch;
  684. int hash = svc_export_hash(old);
  685. ch = sunrpc_cache_update(old->cd, &new->h, &old->h, hash);
  686. if (ch)
  687. return container_of(ch, struct svc_export, h);
  688. else
  689. return NULL;
  690. }
  691. static struct svc_expkey *
  692. exp_find_key(struct cache_detail *cd, struct auth_domain *clp, int fsid_type,
  693. u32 *fsidv, struct cache_req *reqp)
  694. {
  695. struct svc_expkey key, *ek;
  696. int err;
  697. if (!clp)
  698. return ERR_PTR(-ENOENT);
  699. key.ek_client = clp;
  700. key.ek_fsidtype = fsid_type;
  701. memcpy(key.ek_fsid, fsidv, key_len(fsid_type));
  702. ek = svc_expkey_lookup(cd, &key);
  703. if (ek == NULL)
  704. return ERR_PTR(-ENOMEM);
  705. err = cache_check(cd, &ek->h, reqp);
  706. if (err)
  707. return ERR_PTR(err);
  708. return ek;
  709. }
  710. static struct svc_export *
  711. exp_get_by_name(struct cache_detail *cd, struct auth_domain *clp,
  712. const struct path *path, struct cache_req *reqp)
  713. {
  714. struct svc_export *exp, key;
  715. int err;
  716. if (!clp)
  717. return ERR_PTR(-ENOENT);
  718. key.ex_client = clp;
  719. key.ex_path = *path;
  720. key.cd = cd;
  721. exp = svc_export_lookup(&key);
  722. if (exp == NULL)
  723. return ERR_PTR(-ENOMEM);
  724. err = cache_check(cd, &exp->h, reqp);
  725. if (err)
  726. return ERR_PTR(err);
  727. return exp;
  728. }
  729. /*
  730. * Find the export entry for a given dentry.
  731. */
  732. static struct svc_export *
  733. exp_parent(struct cache_detail *cd, struct auth_domain *clp, struct path *path)
  734. {
  735. struct dentry *saved = dget(path->dentry);
  736. struct svc_export *exp = exp_get_by_name(cd, clp, path, NULL);
  737. while (PTR_ERR(exp) == -ENOENT && !IS_ROOT(path->dentry)) {
  738. struct dentry *parent = dget_parent(path->dentry);
  739. dput(path->dentry);
  740. path->dentry = parent;
  741. exp = exp_get_by_name(cd, clp, path, NULL);
  742. }
  743. dput(path->dentry);
  744. path->dentry = saved;
  745. return exp;
  746. }
  747. /*
  748. * Obtain the root fh on behalf of a client.
  749. * This could be done in user space, but I feel that it adds some safety
  750. * since its harder to fool a kernel module than a user space program.
  751. */
  752. int
  753. exp_rootfh(struct net *net, struct auth_domain *clp, char *name,
  754. struct knfsd_fh *f, int maxsize)
  755. {
  756. struct svc_export *exp;
  757. struct path path;
  758. struct inode *inode;
  759. struct svc_fh fh;
  760. int err;
  761. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  762. struct cache_detail *cd = nn->svc_export_cache;
  763. err = -EPERM;
  764. /* NB: we probably ought to check that it's NUL-terminated */
  765. if (kern_path(name, 0, &path)) {
  766. printk("nfsd: exp_rootfh path not found %s", name);
  767. return err;
  768. }
  769. inode = path.dentry->d_inode;
  770. dprintk("nfsd: exp_rootfh(%s [%p] %s:%s/%ld)\n",
  771. name, path.dentry, clp->name,
  772. inode->i_sb->s_id, inode->i_ino);
  773. exp = exp_parent(cd, clp, &path);
  774. if (IS_ERR(exp)) {
  775. err = PTR_ERR(exp);
  776. goto out;
  777. }
  778. /*
  779. * fh must be initialized before calling fh_compose
  780. */
  781. fh_init(&fh, maxsize);
  782. if (fh_compose(&fh, exp, path.dentry, NULL))
  783. err = -EINVAL;
  784. else
  785. err = 0;
  786. memcpy(f, &fh.fh_handle, sizeof(struct knfsd_fh));
  787. fh_put(&fh);
  788. exp_put(exp);
  789. out:
  790. path_put(&path);
  791. return err;
  792. }
  793. static struct svc_export *exp_find(struct cache_detail *cd,
  794. struct auth_domain *clp, int fsid_type,
  795. u32 *fsidv, struct cache_req *reqp)
  796. {
  797. struct svc_export *exp;
  798. struct nfsd_net *nn = net_generic(cd->net, nfsd_net_id);
  799. struct svc_expkey *ek = exp_find_key(nn->svc_expkey_cache, clp, fsid_type, fsidv, reqp);
  800. if (IS_ERR(ek))
  801. return ERR_CAST(ek);
  802. exp = exp_get_by_name(cd, clp, &ek->ek_path, reqp);
  803. cache_put(&ek->h, nn->svc_expkey_cache);
  804. if (IS_ERR(exp))
  805. return ERR_CAST(exp);
  806. return exp;
  807. }
  808. __be32 check_nfsd_access(struct svc_export *exp, struct svc_rqst *rqstp)
  809. {
  810. struct exp_flavor_info *f;
  811. struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;
  812. /* legacy gss-only clients are always OK: */
  813. if (exp->ex_client == rqstp->rq_gssclient)
  814. return 0;
  815. /* ip-address based client; check sec= export option: */
  816. for (f = exp->ex_flavors; f < end; f++) {
  817. if (f->pseudoflavor == rqstp->rq_cred.cr_flavor)
  818. return 0;
  819. }
  820. /* defaults in absence of sec= options: */
  821. if (exp->ex_nflavors == 0) {
  822. if (rqstp->rq_cred.cr_flavor == RPC_AUTH_NULL ||
  823. rqstp->rq_cred.cr_flavor == RPC_AUTH_UNIX)
  824. return 0;
  825. }
  826. return nfserr_wrongsec;
  827. }
  828. /*
  829. * Uses rq_client and rq_gssclient to find an export; uses rq_client (an
  830. * auth_unix client) if it's available and has secinfo information;
  831. * otherwise, will try to use rq_gssclient.
  832. *
  833. * Called from functions that handle requests; functions that do work on
  834. * behalf of mountd are passed a single client name to use, and should
  835. * use exp_get_by_name() or exp_find().
  836. */
  837. struct svc_export *
  838. rqst_exp_get_by_name(struct svc_rqst *rqstp, struct path *path)
  839. {
  840. struct svc_export *gssexp, *exp = ERR_PTR(-ENOENT);
  841. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  842. struct cache_detail *cd = nn->svc_export_cache;
  843. if (rqstp->rq_client == NULL)
  844. goto gss;
  845. /* First try the auth_unix client: */
  846. exp = exp_get_by_name(cd, rqstp->rq_client, path, &rqstp->rq_chandle);
  847. if (PTR_ERR(exp) == -ENOENT)
  848. goto gss;
  849. if (IS_ERR(exp))
  850. return exp;
  851. /* If it has secinfo, assume there are no gss/... clients */
  852. if (exp->ex_nflavors > 0)
  853. return exp;
  854. gss:
  855. /* Otherwise, try falling back on gss client */
  856. if (rqstp->rq_gssclient == NULL)
  857. return exp;
  858. gssexp = exp_get_by_name(cd, rqstp->rq_gssclient, path, &rqstp->rq_chandle);
  859. if (PTR_ERR(gssexp) == -ENOENT)
  860. return exp;
  861. if (!IS_ERR(exp))
  862. exp_put(exp);
  863. return gssexp;
  864. }
  865. struct svc_export *
  866. rqst_exp_find(struct svc_rqst *rqstp, int fsid_type, u32 *fsidv)
  867. {
  868. struct svc_export *gssexp, *exp = ERR_PTR(-ENOENT);
  869. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  870. struct cache_detail *cd = nn->svc_export_cache;
  871. if (rqstp->rq_client == NULL)
  872. goto gss;
  873. /* First try the auth_unix client: */
  874. exp = exp_find(cd, rqstp->rq_client, fsid_type,
  875. fsidv, &rqstp->rq_chandle);
  876. if (PTR_ERR(exp) == -ENOENT)
  877. goto gss;
  878. if (IS_ERR(exp))
  879. return exp;
  880. /* If it has secinfo, assume there are no gss/... clients */
  881. if (exp->ex_nflavors > 0)
  882. return exp;
  883. gss:
  884. /* Otherwise, try falling back on gss client */
  885. if (rqstp->rq_gssclient == NULL)
  886. return exp;
  887. gssexp = exp_find(cd, rqstp->rq_gssclient, fsid_type, fsidv,
  888. &rqstp->rq_chandle);
  889. if (PTR_ERR(gssexp) == -ENOENT)
  890. return exp;
  891. if (!IS_ERR(exp))
  892. exp_put(exp);
  893. return gssexp;
  894. }
  895. struct svc_export *
  896. rqst_exp_parent(struct svc_rqst *rqstp, struct path *path)
  897. {
  898. struct dentry *saved = dget(path->dentry);
  899. struct svc_export *exp = rqst_exp_get_by_name(rqstp, path);
  900. while (PTR_ERR(exp) == -ENOENT && !IS_ROOT(path->dentry)) {
  901. struct dentry *parent = dget_parent(path->dentry);
  902. dput(path->dentry);
  903. path->dentry = parent;
  904. exp = rqst_exp_get_by_name(rqstp, path);
  905. }
  906. dput(path->dentry);
  907. path->dentry = saved;
  908. return exp;
  909. }
  910. struct svc_export *rqst_find_fsidzero_export(struct svc_rqst *rqstp)
  911. {
  912. u32 fsidv[2];
  913. mk_fsid(FSID_NUM, fsidv, 0, 0, 0, NULL);
  914. return rqst_exp_find(rqstp, FSID_NUM, fsidv);
  915. }
  916. /*
  917. * Called when we need the filehandle for the root of the pseudofs,
  918. * for a given NFSv4 client. The root is defined to be the
  919. * export point with fsid==0
  920. */
  921. __be32
  922. exp_pseudoroot(struct svc_rqst *rqstp, struct svc_fh *fhp)
  923. {
  924. struct svc_export *exp;
  925. __be32 rv;
  926. exp = rqst_find_fsidzero_export(rqstp);
  927. if (IS_ERR(exp))
  928. return nfserrno(PTR_ERR(exp));
  929. rv = fh_compose(fhp, exp, exp->ex_path.dentry, NULL);
  930. exp_put(exp);
  931. return rv;
  932. }
  933. /* Iterator */
  934. static void *e_start(struct seq_file *m, loff_t *pos)
  935. __acquires(((struct cache_detail *)m->private)->hash_lock)
  936. {
  937. loff_t n = *pos;
  938. unsigned hash, export;
  939. struct cache_head *ch;
  940. struct cache_detail *cd = m->private;
  941. struct cache_head **export_table = cd->hash_table;
  942. read_lock(&cd->hash_lock);
  943. if (!n--)
  944. return SEQ_START_TOKEN;
  945. hash = n >> 32;
  946. export = n & ((1LL<<32) - 1);
  947. for (ch=export_table[hash]; ch; ch=ch->next)
  948. if (!export--)
  949. return ch;
  950. n &= ~((1LL<<32) - 1);
  951. do {
  952. hash++;
  953. n += 1LL<<32;
  954. } while(hash < EXPORT_HASHMAX && export_table[hash]==NULL);
  955. if (hash >= EXPORT_HASHMAX)
  956. return NULL;
  957. *pos = n+1;
  958. return export_table[hash];
  959. }
  960. static void *e_next(struct seq_file *m, void *p, loff_t *pos)
  961. {
  962. struct cache_head *ch = p;
  963. int hash = (*pos >> 32);
  964. struct cache_detail *cd = m->private;
  965. struct cache_head **export_table = cd->hash_table;
  966. if (p == SEQ_START_TOKEN)
  967. hash = 0;
  968. else if (ch->next == NULL) {
  969. hash++;
  970. *pos += 1LL<<32;
  971. } else {
  972. ++*pos;
  973. return ch->next;
  974. }
  975. *pos &= ~((1LL<<32) - 1);
  976. while (hash < EXPORT_HASHMAX && export_table[hash] == NULL) {
  977. hash++;
  978. *pos += 1LL<<32;
  979. }
  980. if (hash >= EXPORT_HASHMAX)
  981. return NULL;
  982. ++*pos;
  983. return export_table[hash];
  984. }
  985. static void e_stop(struct seq_file *m, void *p)
  986. __releases(((struct cache_detail *)m->private)->hash_lock)
  987. {
  988. struct cache_detail *cd = m->private;
  989. read_unlock(&cd->hash_lock);
  990. }
  991. static struct flags {
  992. int flag;
  993. char *name[2];
  994. } expflags[] = {
  995. { NFSEXP_READONLY, {"ro", "rw"}},
  996. { NFSEXP_INSECURE_PORT, {"insecure", ""}},
  997. { NFSEXP_ROOTSQUASH, {"root_squash", "no_root_squash"}},
  998. { NFSEXP_ALLSQUASH, {"all_squash", ""}},
  999. { NFSEXP_ASYNC, {"async", "sync"}},
  1000. { NFSEXP_GATHERED_WRITES, {"wdelay", "no_wdelay"}},
  1001. { NFSEXP_NOHIDE, {"nohide", ""}},
  1002. { NFSEXP_CROSSMOUNT, {"crossmnt", ""}},
  1003. { NFSEXP_NOSUBTREECHECK, {"no_subtree_check", ""}},
  1004. { NFSEXP_NOAUTHNLM, {"insecure_locks", ""}},
  1005. { NFSEXP_V4ROOT, {"v4root", ""}},
  1006. { 0, {"", ""}}
  1007. };
  1008. static void show_expflags(struct seq_file *m, int flags, int mask)
  1009. {
  1010. struct flags *flg;
  1011. int state, first = 0;
  1012. for (flg = expflags; flg->flag; flg++) {
  1013. if (flg->flag & ~mask)
  1014. continue;
  1015. state = (flg->flag & flags) ? 0 : 1;
  1016. if (*flg->name[state])
  1017. seq_printf(m, "%s%s", first++?",":"", flg->name[state]);
  1018. }
  1019. }
  1020. static void show_secinfo_flags(struct seq_file *m, int flags)
  1021. {
  1022. seq_printf(m, ",");
  1023. show_expflags(m, flags, NFSEXP_SECINFO_FLAGS);
  1024. }
  1025. static bool secinfo_flags_equal(int f, int g)
  1026. {
  1027. f &= NFSEXP_SECINFO_FLAGS;
  1028. g &= NFSEXP_SECINFO_FLAGS;
  1029. return f == g;
  1030. }
  1031. static int show_secinfo_run(struct seq_file *m, struct exp_flavor_info **fp, struct exp_flavor_info *end)
  1032. {
  1033. int flags;
  1034. flags = (*fp)->flags;
  1035. seq_printf(m, ",sec=%d", (*fp)->pseudoflavor);
  1036. (*fp)++;
  1037. while (*fp != end && secinfo_flags_equal(flags, (*fp)->flags)) {
  1038. seq_printf(m, ":%d", (*fp)->pseudoflavor);
  1039. (*fp)++;
  1040. }
  1041. return flags;
  1042. }
  1043. static void show_secinfo(struct seq_file *m, struct svc_export *exp)
  1044. {
  1045. struct exp_flavor_info *f;
  1046. struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;
  1047. int flags;
  1048. if (exp->ex_nflavors == 0)
  1049. return;
  1050. f = exp->ex_flavors;
  1051. flags = show_secinfo_run(m, &f, end);
  1052. if (!secinfo_flags_equal(flags, exp->ex_flags))
  1053. show_secinfo_flags(m, flags);
  1054. while (f != end) {
  1055. flags = show_secinfo_run(m, &f, end);
  1056. show_secinfo_flags(m, flags);
  1057. }
  1058. }
  1059. static void exp_flags(struct seq_file *m, int flag, int fsid,
  1060. kuid_t anonu, kgid_t anong, struct nfsd4_fs_locations *fsloc)
  1061. {
  1062. show_expflags(m, flag, NFSEXP_ALLFLAGS);
  1063. if (flag & NFSEXP_FSID)
  1064. seq_printf(m, ",fsid=%d", fsid);
  1065. if (!uid_eq(anonu, make_kuid(&init_user_ns, (uid_t)-2)) &&
  1066. !uid_eq(anonu, make_kuid(&init_user_ns, 0x10000-2)))
  1067. seq_printf(m, ",anonuid=%u", from_kuid(&init_user_ns, anonu));
  1068. if (!gid_eq(anong, make_kgid(&init_user_ns, (gid_t)-2)) &&
  1069. !gid_eq(anong, make_kgid(&init_user_ns, 0x10000-2)))
  1070. seq_printf(m, ",anongid=%u", from_kgid(&init_user_ns, anong));
  1071. if (fsloc && fsloc->locations_count > 0) {
  1072. char *loctype = (fsloc->migrated) ? "refer" : "replicas";
  1073. int i;
  1074. seq_printf(m, ",%s=", loctype);
  1075. seq_escape(m, fsloc->locations[0].path, ",;@ \t\n\\");
  1076. seq_putc(m, '@');
  1077. seq_escape(m, fsloc->locations[0].hosts, ",;@ \t\n\\");
  1078. for (i = 1; i < fsloc->locations_count; i++) {
  1079. seq_putc(m, ';');
  1080. seq_escape(m, fsloc->locations[i].path, ",;@ \t\n\\");
  1081. seq_putc(m, '@');
  1082. seq_escape(m, fsloc->locations[i].hosts, ",;@ \t\n\\");
  1083. }
  1084. }
  1085. }
  1086. static int e_show(struct seq_file *m, void *p)
  1087. {
  1088. struct cache_head *cp = p;
  1089. struct svc_export *exp = container_of(cp, struct svc_export, h);
  1090. struct cache_detail *cd = m->private;
  1091. if (p == SEQ_START_TOKEN) {
  1092. seq_puts(m, "# Version 1.1\n");
  1093. seq_puts(m, "# Path Client(Flags) # IPs\n");
  1094. return 0;
  1095. }
  1096. cache_get(&exp->h);
  1097. if (cache_check(cd, &exp->h, NULL))
  1098. return 0;
  1099. exp_put(exp);
  1100. return svc_export_show(m, cd, cp);
  1101. }
  1102. const struct seq_operations nfs_exports_op = {
  1103. .start = e_start,
  1104. .next = e_next,
  1105. .stop = e_stop,
  1106. .show = e_show,
  1107. };
  1108. /*
  1109. * Initialize the exports module.
  1110. */
  1111. int
  1112. nfsd_export_init(struct net *net)
  1113. {
  1114. int rv;
  1115. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1116. dprintk("nfsd: initializing export module (net: %p).\n", net);
  1117. nn->svc_export_cache = cache_create_net(&svc_export_cache_template, net);
  1118. if (IS_ERR(nn->svc_export_cache))
  1119. return PTR_ERR(nn->svc_export_cache);
  1120. rv = cache_register_net(nn->svc_export_cache, net);
  1121. if (rv)
  1122. goto destroy_export_cache;
  1123. nn->svc_expkey_cache = cache_create_net(&svc_expkey_cache_template, net);
  1124. if (IS_ERR(nn->svc_expkey_cache)) {
  1125. rv = PTR_ERR(nn->svc_expkey_cache);
  1126. goto unregister_export_cache;
  1127. }
  1128. rv = cache_register_net(nn->svc_expkey_cache, net);
  1129. if (rv)
  1130. goto destroy_expkey_cache;
  1131. return 0;
  1132. destroy_expkey_cache:
  1133. cache_destroy_net(nn->svc_expkey_cache, net);
  1134. unregister_export_cache:
  1135. cache_unregister_net(nn->svc_export_cache, net);
  1136. destroy_export_cache:
  1137. cache_destroy_net(nn->svc_export_cache, net);
  1138. return rv;
  1139. }
  1140. /*
  1141. * Flush exports table - called when last nfsd thread is killed
  1142. */
  1143. void
  1144. nfsd_export_flush(struct net *net)
  1145. {
  1146. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1147. cache_purge(nn->svc_expkey_cache);
  1148. cache_purge(nn->svc_export_cache);
  1149. }
  1150. /*
  1151. * Shutdown the exports module.
  1152. */
  1153. void
  1154. nfsd_export_shutdown(struct net *net)
  1155. {
  1156. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1157. dprintk("nfsd: shutting down export module (net: %p).\n", net);
  1158. cache_unregister_net(nn->svc_expkey_cache, net);
  1159. cache_unregister_net(nn->svc_export_cache, net);
  1160. cache_destroy_net(nn->svc_expkey_cache, net);
  1161. cache_destroy_net(nn->svc_export_cache, net);
  1162. svcauth_unix_purge(net);
  1163. dprintk("nfsd: export shutdown complete (net: %p).\n", net);
  1164. }