export.c 30 KB

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