export.c 30 KB

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