export.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  1. #define MSNFS /* HACK HACK */
  2. /*
  3. * linux/fs/nfsd/export.c
  4. *
  5. * NFS exporting and validation.
  6. *
  7. * We maintain a list of clients, each of which has a list of
  8. * exports. To export an fs to a given client, you first have
  9. * to create the client entry with NFSCTL_ADDCLIENT, which
  10. * creates a client control block and adds it to the hash
  11. * table. Then, you call NFSCTL_EXPORT for each fs.
  12. *
  13. *
  14. * Copyright (C) 1995, 1996 Olaf Kirch, <okir@monad.swb.de>
  15. */
  16. #include <linux/unistd.h>
  17. #include <linux/slab.h>
  18. #include <linux/stat.h>
  19. #include <linux/in.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/syscalls.h>
  22. #include <linux/rwsem.h>
  23. #include <linux/dcache.h>
  24. #include <linux/namei.h>
  25. #include <linux/mount.h>
  26. #include <linux/hash.h>
  27. #include <linux/module.h>
  28. #include <linux/exportfs.h>
  29. #include <linux/sunrpc/svc.h>
  30. #include <linux/nfsd/nfsd.h>
  31. #include <linux/nfsd/nfsfh.h>
  32. #include <linux/nfsd/syscall.h>
  33. #include <linux/lockd/bind.h>
  34. #include <linux/sunrpc/msg_prot.h>
  35. #include <linux/sunrpc/gss_api.h>
  36. #include <net/ipv6.h>
  37. #define NFSDDBG_FACILITY NFSDDBG_EXPORT
  38. typedef struct auth_domain svc_client;
  39. typedef struct svc_export svc_export;
  40. static void exp_do_unexport(svc_export *unexp);
  41. static int exp_verify_string(char *cp, int max);
  42. /*
  43. * We have two caches.
  44. * One maps client+vfsmnt+dentry to export options - the export map
  45. * The other maps client+filehandle-fragment to export options. - the expkey map
  46. *
  47. * The export options are actually stored in the first map, and the
  48. * second map contains a reference to the entry in the first map.
  49. */
  50. #define EXPKEY_HASHBITS 8
  51. #define EXPKEY_HASHMAX (1 << EXPKEY_HASHBITS)
  52. #define EXPKEY_HASHMASK (EXPKEY_HASHMAX -1)
  53. static struct cache_head *expkey_table[EXPKEY_HASHMAX];
  54. static void expkey_put(struct kref *ref)
  55. {
  56. struct svc_expkey *key = container_of(ref, struct svc_expkey, h.ref);
  57. if (test_bit(CACHE_VALID, &key->h.flags) &&
  58. !test_bit(CACHE_NEGATIVE, &key->h.flags))
  59. path_put(&key->ek_path);
  60. auth_domain_put(key->ek_client);
  61. kfree(key);
  62. }
  63. static void expkey_request(struct cache_detail *cd,
  64. struct cache_head *h,
  65. char **bpp, int *blen)
  66. {
  67. /* client fsidtype \xfsid */
  68. struct svc_expkey *ek = container_of(h, struct svc_expkey, h);
  69. char type[5];
  70. qword_add(bpp, blen, ek->ek_client->name);
  71. snprintf(type, 5, "%d", ek->ek_fsidtype);
  72. qword_add(bpp, blen, type);
  73. qword_addhex(bpp, blen, (char*)ek->ek_fsid, key_len(ek->ek_fsidtype));
  74. (*bpp)[-1] = '\n';
  75. }
  76. static struct svc_expkey *svc_expkey_update(struct svc_expkey *new, struct svc_expkey *old);
  77. static struct svc_expkey *svc_expkey_lookup(struct svc_expkey *);
  78. static struct cache_detail svc_expkey_cache;
  79. static int expkey_parse(struct cache_detail *cd, char *mesg, int mlen)
  80. {
  81. /* client fsidtype fsid [path] */
  82. char *buf;
  83. int len;
  84. struct auth_domain *dom = NULL;
  85. int err;
  86. int fsidtype;
  87. char *ep;
  88. struct svc_expkey key;
  89. struct svc_expkey *ek = NULL;
  90. if (mesg[mlen-1] != '\n')
  91. return -EINVAL;
  92. mesg[mlen-1] = 0;
  93. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  94. err = -ENOMEM;
  95. if (!buf)
  96. goto out;
  97. err = -EINVAL;
  98. if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
  99. goto out;
  100. err = -ENOENT;
  101. dom = auth_domain_find(buf);
  102. if (!dom)
  103. goto out;
  104. dprintk("found domain %s\n", buf);
  105. err = -EINVAL;
  106. if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
  107. goto out;
  108. fsidtype = simple_strtoul(buf, &ep, 10);
  109. if (*ep)
  110. goto out;
  111. dprintk("found fsidtype %d\n", fsidtype);
  112. if (key_len(fsidtype)==0) /* invalid type */
  113. goto out;
  114. if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
  115. goto out;
  116. dprintk("found fsid length %d\n", len);
  117. if (len != key_len(fsidtype))
  118. goto out;
  119. /* OK, we seem to have a valid key */
  120. key.h.flags = 0;
  121. key.h.expiry_time = get_expiry(&mesg);
  122. if (key.h.expiry_time == 0)
  123. goto out;
  124. key.ek_client = dom;
  125. key.ek_fsidtype = fsidtype;
  126. memcpy(key.ek_fsid, buf, len);
  127. ek = svc_expkey_lookup(&key);
  128. err = -ENOMEM;
  129. if (!ek)
  130. goto out;
  131. /* now we want a pathname, or empty meaning NEGATIVE */
  132. err = -EINVAL;
  133. len = qword_get(&mesg, buf, PAGE_SIZE);
  134. if (len < 0)
  135. goto out;
  136. dprintk("Path seems to be <%s>\n", buf);
  137. err = 0;
  138. if (len == 0) {
  139. set_bit(CACHE_NEGATIVE, &key.h.flags);
  140. ek = svc_expkey_update(&key, ek);
  141. if (!ek)
  142. err = -ENOMEM;
  143. } else {
  144. struct nameidata nd;
  145. err = path_lookup(buf, 0, &nd);
  146. if (err)
  147. goto out;
  148. dprintk("Found the path %s\n", buf);
  149. key.ek_path = nd.path;
  150. ek = svc_expkey_update(&key, ek);
  151. if (!ek)
  152. err = -ENOMEM;
  153. path_put(&nd.path);
  154. }
  155. cache_flush();
  156. out:
  157. if (ek)
  158. cache_put(&ek->h, &svc_expkey_cache);
  159. if (dom)
  160. auth_domain_put(dom);
  161. kfree(buf);
  162. return err;
  163. }
  164. static int expkey_show(struct seq_file *m,
  165. struct cache_detail *cd,
  166. struct cache_head *h)
  167. {
  168. struct svc_expkey *ek ;
  169. int i;
  170. if (h ==NULL) {
  171. seq_puts(m, "#domain fsidtype fsid [path]\n");
  172. return 0;
  173. }
  174. ek = container_of(h, struct svc_expkey, h);
  175. seq_printf(m, "%s %d 0x", ek->ek_client->name,
  176. ek->ek_fsidtype);
  177. for (i=0; i < key_len(ek->ek_fsidtype)/4; i++)
  178. seq_printf(m, "%08x", ek->ek_fsid[i]);
  179. if (test_bit(CACHE_VALID, &h->flags) &&
  180. !test_bit(CACHE_NEGATIVE, &h->flags)) {
  181. seq_printf(m, " ");
  182. seq_path(m, &ek->ek_path, "\\ \t\n");
  183. }
  184. seq_printf(m, "\n");
  185. return 0;
  186. }
  187. static inline int expkey_match (struct cache_head *a, struct cache_head *b)
  188. {
  189. struct svc_expkey *orig = container_of(a, struct svc_expkey, h);
  190. struct svc_expkey *new = container_of(b, struct svc_expkey, h);
  191. if (orig->ek_fsidtype != new->ek_fsidtype ||
  192. orig->ek_client != new->ek_client ||
  193. memcmp(orig->ek_fsid, new->ek_fsid, key_len(orig->ek_fsidtype)) != 0)
  194. return 0;
  195. return 1;
  196. }
  197. static inline void expkey_init(struct cache_head *cnew,
  198. struct cache_head *citem)
  199. {
  200. struct svc_expkey *new = container_of(cnew, struct svc_expkey, h);
  201. struct svc_expkey *item = container_of(citem, struct svc_expkey, h);
  202. kref_get(&item->ek_client->ref);
  203. new->ek_client = item->ek_client;
  204. new->ek_fsidtype = item->ek_fsidtype;
  205. memcpy(new->ek_fsid, item->ek_fsid, sizeof(new->ek_fsid));
  206. }
  207. static inline void expkey_update(struct cache_head *cnew,
  208. struct cache_head *citem)
  209. {
  210. struct svc_expkey *new = container_of(cnew, struct svc_expkey, h);
  211. struct svc_expkey *item = container_of(citem, struct svc_expkey, h);
  212. new->ek_path = item->ek_path;
  213. path_get(&item->ek_path);
  214. }
  215. static struct cache_head *expkey_alloc(void)
  216. {
  217. struct svc_expkey *i = kmalloc(sizeof(*i), GFP_KERNEL);
  218. if (i)
  219. return &i->h;
  220. else
  221. return NULL;
  222. }
  223. static struct cache_detail svc_expkey_cache = {
  224. .owner = THIS_MODULE,
  225. .hash_size = EXPKEY_HASHMAX,
  226. .hash_table = expkey_table,
  227. .name = "nfsd.fh",
  228. .cache_put = expkey_put,
  229. .cache_request = expkey_request,
  230. .cache_parse = expkey_parse,
  231. .cache_show = expkey_show,
  232. .match = expkey_match,
  233. .init = expkey_init,
  234. .update = expkey_update,
  235. .alloc = expkey_alloc,
  236. };
  237. static struct svc_expkey *
  238. svc_expkey_lookup(struct svc_expkey *item)
  239. {
  240. struct cache_head *ch;
  241. int hash = item->ek_fsidtype;
  242. char * cp = (char*)item->ek_fsid;
  243. int len = key_len(item->ek_fsidtype);
  244. hash ^= hash_mem(cp, len, EXPKEY_HASHBITS);
  245. hash ^= hash_ptr(item->ek_client, EXPKEY_HASHBITS);
  246. hash &= EXPKEY_HASHMASK;
  247. ch = sunrpc_cache_lookup(&svc_expkey_cache, &item->h,
  248. hash);
  249. if (ch)
  250. return container_of(ch, struct svc_expkey, h);
  251. else
  252. return NULL;
  253. }
  254. static struct svc_expkey *
  255. svc_expkey_update(struct svc_expkey *new, struct svc_expkey *old)
  256. {
  257. struct cache_head *ch;
  258. int hash = new->ek_fsidtype;
  259. char * cp = (char*)new->ek_fsid;
  260. int len = key_len(new->ek_fsidtype);
  261. hash ^= hash_mem(cp, len, EXPKEY_HASHBITS);
  262. hash ^= hash_ptr(new->ek_client, EXPKEY_HASHBITS);
  263. hash &= EXPKEY_HASHMASK;
  264. ch = sunrpc_cache_update(&svc_expkey_cache, &new->h,
  265. &old->h, hash);
  266. if (ch)
  267. return container_of(ch, struct svc_expkey, h);
  268. else
  269. return NULL;
  270. }
  271. #define EXPORT_HASHBITS 8
  272. #define EXPORT_HASHMAX (1<< EXPORT_HASHBITS)
  273. #define EXPORT_HASHMASK (EXPORT_HASHMAX -1)
  274. static struct cache_head *export_table[EXPORT_HASHMAX];
  275. static void nfsd4_fslocs_free(struct nfsd4_fs_locations *fsloc)
  276. {
  277. int i;
  278. for (i = 0; i < fsloc->locations_count; i++) {
  279. kfree(fsloc->locations[i].path);
  280. kfree(fsloc->locations[i].hosts);
  281. }
  282. kfree(fsloc->locations);
  283. }
  284. static void svc_export_put(struct kref *ref)
  285. {
  286. struct svc_export *exp = container_of(ref, struct svc_export, h.ref);
  287. path_put(&exp->ex_path);
  288. auth_domain_put(exp->ex_client);
  289. kfree(exp->ex_pathname);
  290. nfsd4_fslocs_free(&exp->ex_fslocs);
  291. kfree(exp);
  292. }
  293. static void svc_export_request(struct cache_detail *cd,
  294. struct cache_head *h,
  295. char **bpp, int *blen)
  296. {
  297. /* client path */
  298. struct svc_export *exp = container_of(h, struct svc_export, h);
  299. char *pth;
  300. qword_add(bpp, blen, exp->ex_client->name);
  301. pth = d_path(&exp->ex_path, *bpp, *blen);
  302. if (IS_ERR(pth)) {
  303. /* is this correct? */
  304. (*bpp)[0] = '\n';
  305. return;
  306. }
  307. qword_add(bpp, blen, pth);
  308. (*bpp)[-1] = '\n';
  309. }
  310. static struct svc_export *svc_export_update(struct svc_export *new,
  311. struct svc_export *old);
  312. static struct svc_export *svc_export_lookup(struct svc_export *);
  313. static int check_export(struct inode *inode, int flags, unsigned char *uuid)
  314. {
  315. /* We currently export only dirs and regular files.
  316. * This is what umountd does.
  317. */
  318. if (!S_ISDIR(inode->i_mode) &&
  319. !S_ISREG(inode->i_mode))
  320. return -ENOTDIR;
  321. /* There are two requirements on a filesystem to be exportable.
  322. * 1: We must be able to identify the filesystem from a number.
  323. * either a device number (so FS_REQUIRES_DEV needed)
  324. * or an FSID number (so NFSEXP_FSID or ->uuid is needed).
  325. * 2: We must be able to find an inode from a filehandle.
  326. * This means that s_export_op must be set.
  327. */
  328. if (!(inode->i_sb->s_type->fs_flags & FS_REQUIRES_DEV) &&
  329. !(flags & NFSEXP_FSID) &&
  330. uuid == NULL) {
  331. dprintk("exp_export: export of non-dev fs without fsid\n");
  332. return -EINVAL;
  333. }
  334. if (!inode->i_sb->s_export_op ||
  335. !inode->i_sb->s_export_op->fh_to_dentry) {
  336. dprintk("exp_export: export of invalid fs type.\n");
  337. return -EINVAL;
  338. }
  339. return 0;
  340. }
  341. #ifdef CONFIG_NFSD_V4
  342. static int
  343. fsloc_parse(char **mesg, char *buf, struct nfsd4_fs_locations *fsloc)
  344. {
  345. int len;
  346. int migrated, i, err;
  347. /* listsize */
  348. err = get_int(mesg, &fsloc->locations_count);
  349. if (err)
  350. return err;
  351. if (fsloc->locations_count > MAX_FS_LOCATIONS)
  352. return -EINVAL;
  353. if (fsloc->locations_count == 0)
  354. return 0;
  355. fsloc->locations = kzalloc(fsloc->locations_count
  356. * sizeof(struct nfsd4_fs_location), GFP_KERNEL);
  357. if (!fsloc->locations)
  358. return -ENOMEM;
  359. for (i=0; i < fsloc->locations_count; i++) {
  360. /* colon separated host list */
  361. err = -EINVAL;
  362. len = qword_get(mesg, buf, PAGE_SIZE);
  363. if (len <= 0)
  364. goto out_free_all;
  365. err = -ENOMEM;
  366. fsloc->locations[i].hosts = kstrdup(buf, GFP_KERNEL);
  367. if (!fsloc->locations[i].hosts)
  368. goto out_free_all;
  369. err = -EINVAL;
  370. /* slash separated path component list */
  371. len = qword_get(mesg, buf, PAGE_SIZE);
  372. if (len <= 0)
  373. goto out_free_all;
  374. err = -ENOMEM;
  375. fsloc->locations[i].path = kstrdup(buf, GFP_KERNEL);
  376. if (!fsloc->locations[i].path)
  377. goto out_free_all;
  378. }
  379. /* migrated */
  380. err = get_int(mesg, &migrated);
  381. if (err)
  382. goto out_free_all;
  383. err = -EINVAL;
  384. if (migrated < 0 || migrated > 1)
  385. goto out_free_all;
  386. fsloc->migrated = migrated;
  387. return 0;
  388. out_free_all:
  389. nfsd4_fslocs_free(fsloc);
  390. return err;
  391. }
  392. static int secinfo_parse(char **mesg, char *buf, struct svc_export *exp)
  393. {
  394. int listsize, err;
  395. struct exp_flavor_info *f;
  396. err = get_int(mesg, &listsize);
  397. if (err)
  398. return err;
  399. if (listsize < 0 || listsize > MAX_SECINFO_LIST)
  400. return -EINVAL;
  401. for (f = exp->ex_flavors; f < exp->ex_flavors + listsize; f++) {
  402. err = get_int(mesg, &f->pseudoflavor);
  403. if (err)
  404. return err;
  405. /*
  406. * Just a quick sanity check; we could also try to check
  407. * whether this pseudoflavor is supported, but at worst
  408. * an unsupported pseudoflavor on the export would just
  409. * be a pseudoflavor that won't match the flavor of any
  410. * authenticated request. The administrator will
  411. * probably discover the problem when someone fails to
  412. * authenticate.
  413. */
  414. if (f->pseudoflavor < 0)
  415. return -EINVAL;
  416. err = get_int(mesg, &f->flags);
  417. if (err)
  418. return err;
  419. /* Only some flags are allowed to differ between flavors: */
  420. if (~NFSEXP_SECINFO_FLAGS & (f->flags ^ exp->ex_flags))
  421. return -EINVAL;
  422. }
  423. exp->ex_nflavors = listsize;
  424. return 0;
  425. }
  426. #else /* CONFIG_NFSD_V4 */
  427. static inline int
  428. fsloc_parse(char **mesg, char *buf, struct nfsd4_fs_locations *fsloc){return 0;}
  429. static inline int
  430. secinfo_parse(char **mesg, char *buf, struct svc_export *exp) { return 0; }
  431. #endif
  432. static int svc_export_parse(struct cache_detail *cd, char *mesg, int mlen)
  433. {
  434. /* client path expiry [flags anonuid anongid fsid] */
  435. char *buf;
  436. int len;
  437. int err;
  438. struct auth_domain *dom = NULL;
  439. struct nameidata nd;
  440. struct svc_export exp, *expp;
  441. int an_int;
  442. nd.path.dentry = NULL;
  443. exp.ex_pathname = NULL;
  444. /* fs locations */
  445. exp.ex_fslocs.locations = NULL;
  446. exp.ex_fslocs.locations_count = 0;
  447. exp.ex_fslocs.migrated = 0;
  448. exp.ex_uuid = NULL;
  449. /* secinfo */
  450. exp.ex_nflavors = 0;
  451. if (mesg[mlen-1] != '\n')
  452. return -EINVAL;
  453. mesg[mlen-1] = 0;
  454. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  455. err = -ENOMEM;
  456. if (!buf) goto out;
  457. /* client */
  458. len = qword_get(&mesg, buf, PAGE_SIZE);
  459. err = -EINVAL;
  460. if (len <= 0) goto out;
  461. err = -ENOENT;
  462. dom = auth_domain_find(buf);
  463. if (!dom)
  464. goto out;
  465. /* path */
  466. err = -EINVAL;
  467. if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
  468. goto out;
  469. err = path_lookup(buf, 0, &nd);
  470. if (err) goto out_no_path;
  471. exp.h.flags = 0;
  472. exp.ex_client = dom;
  473. exp.ex_path.mnt = nd.path.mnt;
  474. exp.ex_path.dentry = nd.path.dentry;
  475. exp.ex_pathname = kstrdup(buf, GFP_KERNEL);
  476. err = -ENOMEM;
  477. if (!exp.ex_pathname)
  478. goto out;
  479. /* expiry */
  480. err = -EINVAL;
  481. exp.h.expiry_time = get_expiry(&mesg);
  482. if (exp.h.expiry_time == 0)
  483. goto out;
  484. /* flags */
  485. err = get_int(&mesg, &an_int);
  486. if (err == -ENOENT) {
  487. err = 0;
  488. set_bit(CACHE_NEGATIVE, &exp.h.flags);
  489. } else {
  490. if (err || an_int < 0) goto out;
  491. exp.ex_flags= an_int;
  492. /* anon uid */
  493. err = get_int(&mesg, &an_int);
  494. if (err) goto out;
  495. exp.ex_anon_uid= an_int;
  496. /* anon gid */
  497. err = get_int(&mesg, &an_int);
  498. if (err) goto out;
  499. exp.ex_anon_gid= an_int;
  500. /* fsid */
  501. err = get_int(&mesg, &an_int);
  502. if (err) goto out;
  503. exp.ex_fsid = an_int;
  504. while ((len = qword_get(&mesg, buf, PAGE_SIZE)) > 0) {
  505. if (strcmp(buf, "fsloc") == 0)
  506. err = fsloc_parse(&mesg, buf, &exp.ex_fslocs);
  507. else if (strcmp(buf, "uuid") == 0) {
  508. /* expect a 16 byte uuid encoded as \xXXXX... */
  509. len = qword_get(&mesg, buf, PAGE_SIZE);
  510. if (len != 16)
  511. err = -EINVAL;
  512. else {
  513. exp.ex_uuid =
  514. kmemdup(buf, 16, GFP_KERNEL);
  515. if (exp.ex_uuid == NULL)
  516. err = -ENOMEM;
  517. }
  518. } else if (strcmp(buf, "secinfo") == 0)
  519. err = secinfo_parse(&mesg, buf, &exp);
  520. else
  521. /* quietly ignore unknown words and anything
  522. * following. Newer user-space can try to set
  523. * new values, then see what the result was.
  524. */
  525. break;
  526. if (err)
  527. goto out;
  528. }
  529. err = check_export(nd.path.dentry->d_inode, exp.ex_flags,
  530. exp.ex_uuid);
  531. if (err) goto out;
  532. }
  533. expp = svc_export_lookup(&exp);
  534. if (expp)
  535. expp = svc_export_update(&exp, expp);
  536. else
  537. err = -ENOMEM;
  538. cache_flush();
  539. if (expp == NULL)
  540. err = -ENOMEM;
  541. else
  542. exp_put(expp);
  543. out:
  544. nfsd4_fslocs_free(&exp.ex_fslocs);
  545. kfree(exp.ex_uuid);
  546. kfree(exp.ex_pathname);
  547. if (nd.path.dentry)
  548. path_put(&nd.path);
  549. out_no_path:
  550. if (dom)
  551. auth_domain_put(dom);
  552. kfree(buf);
  553. return err;
  554. }
  555. static void exp_flags(struct seq_file *m, int flag, int fsid,
  556. uid_t anonu, uid_t anong, struct nfsd4_fs_locations *fslocs);
  557. static void show_secinfo(struct seq_file *m, struct svc_export *exp);
  558. static int svc_export_show(struct seq_file *m,
  559. struct cache_detail *cd,
  560. struct cache_head *h)
  561. {
  562. struct svc_export *exp ;
  563. if (h ==NULL) {
  564. seq_puts(m, "#path domain(flags)\n");
  565. return 0;
  566. }
  567. exp = container_of(h, struct svc_export, h);
  568. seq_path(m, &exp->ex_path, " \t\n\\");
  569. seq_putc(m, '\t');
  570. seq_escape(m, exp->ex_client->name, " \t\n\\");
  571. seq_putc(m, '(');
  572. if (test_bit(CACHE_VALID, &h->flags) &&
  573. !test_bit(CACHE_NEGATIVE, &h->flags)) {
  574. exp_flags(m, exp->ex_flags, exp->ex_fsid,
  575. exp->ex_anon_uid, exp->ex_anon_gid, &exp->ex_fslocs);
  576. if (exp->ex_uuid) {
  577. int i;
  578. seq_puts(m, ",uuid=");
  579. for (i=0; i<16; i++) {
  580. if ((i&3) == 0 && i)
  581. seq_putc(m, ':');
  582. seq_printf(m, "%02x", exp->ex_uuid[i]);
  583. }
  584. }
  585. show_secinfo(m, exp);
  586. }
  587. seq_puts(m, ")\n");
  588. return 0;
  589. }
  590. static int svc_export_match(struct cache_head *a, struct cache_head *b)
  591. {
  592. struct svc_export *orig = container_of(a, struct svc_export, h);
  593. struct svc_export *new = container_of(b, struct svc_export, h);
  594. return orig->ex_client == new->ex_client &&
  595. orig->ex_path.dentry == new->ex_path.dentry &&
  596. orig->ex_path.mnt == new->ex_path.mnt;
  597. }
  598. static void svc_export_init(struct cache_head *cnew, struct cache_head *citem)
  599. {
  600. struct svc_export *new = container_of(cnew, struct svc_export, h);
  601. struct svc_export *item = container_of(citem, struct svc_export, h);
  602. kref_get(&item->ex_client->ref);
  603. new->ex_client = item->ex_client;
  604. new->ex_path.dentry = dget(item->ex_path.dentry);
  605. new->ex_path.mnt = mntget(item->ex_path.mnt);
  606. new->ex_pathname = NULL;
  607. new->ex_fslocs.locations = NULL;
  608. new->ex_fslocs.locations_count = 0;
  609. new->ex_fslocs.migrated = 0;
  610. }
  611. static void export_update(struct cache_head *cnew, struct cache_head *citem)
  612. {
  613. struct svc_export *new = container_of(cnew, struct svc_export, h);
  614. struct svc_export *item = container_of(citem, struct svc_export, h);
  615. int i;
  616. new->ex_flags = item->ex_flags;
  617. new->ex_anon_uid = item->ex_anon_uid;
  618. new->ex_anon_gid = item->ex_anon_gid;
  619. new->ex_fsid = item->ex_fsid;
  620. new->ex_uuid = item->ex_uuid;
  621. item->ex_uuid = NULL;
  622. new->ex_pathname = item->ex_pathname;
  623. item->ex_pathname = NULL;
  624. new->ex_fslocs.locations = item->ex_fslocs.locations;
  625. item->ex_fslocs.locations = NULL;
  626. new->ex_fslocs.locations_count = item->ex_fslocs.locations_count;
  627. item->ex_fslocs.locations_count = 0;
  628. new->ex_fslocs.migrated = item->ex_fslocs.migrated;
  629. item->ex_fslocs.migrated = 0;
  630. new->ex_nflavors = item->ex_nflavors;
  631. for (i = 0; i < MAX_SECINFO_LIST; i++) {
  632. new->ex_flavors[i] = item->ex_flavors[i];
  633. }
  634. }
  635. static struct cache_head *svc_export_alloc(void)
  636. {
  637. struct svc_export *i = kmalloc(sizeof(*i), GFP_KERNEL);
  638. if (i)
  639. return &i->h;
  640. else
  641. return NULL;
  642. }
  643. struct cache_detail svc_export_cache = {
  644. .owner = THIS_MODULE,
  645. .hash_size = EXPORT_HASHMAX,
  646. .hash_table = export_table,
  647. .name = "nfsd.export",
  648. .cache_put = svc_export_put,
  649. .cache_request = svc_export_request,
  650. .cache_parse = svc_export_parse,
  651. .cache_show = svc_export_show,
  652. .match = svc_export_match,
  653. .init = svc_export_init,
  654. .update = export_update,
  655. .alloc = svc_export_alloc,
  656. };
  657. static struct svc_export *
  658. svc_export_lookup(struct svc_export *exp)
  659. {
  660. struct cache_head *ch;
  661. int hash;
  662. hash = hash_ptr(exp->ex_client, EXPORT_HASHBITS);
  663. hash ^= hash_ptr(exp->ex_path.dentry, EXPORT_HASHBITS);
  664. hash ^= hash_ptr(exp->ex_path.mnt, EXPORT_HASHBITS);
  665. ch = sunrpc_cache_lookup(&svc_export_cache, &exp->h,
  666. hash);
  667. if (ch)
  668. return container_of(ch, struct svc_export, h);
  669. else
  670. return NULL;
  671. }
  672. static struct svc_export *
  673. svc_export_update(struct svc_export *new, struct svc_export *old)
  674. {
  675. struct cache_head *ch;
  676. int hash;
  677. hash = hash_ptr(old->ex_client, EXPORT_HASHBITS);
  678. hash ^= hash_ptr(old->ex_path.dentry, EXPORT_HASHBITS);
  679. hash ^= hash_ptr(old->ex_path.mnt, EXPORT_HASHBITS);
  680. ch = sunrpc_cache_update(&svc_export_cache, &new->h,
  681. &old->h,
  682. hash);
  683. if (ch)
  684. return container_of(ch, struct svc_export, h);
  685. else
  686. return NULL;
  687. }
  688. static struct svc_expkey *
  689. exp_find_key(svc_client *clp, int fsid_type, u32 *fsidv, struct cache_req *reqp)
  690. {
  691. struct svc_expkey key, *ek;
  692. int err;
  693. if (!clp)
  694. return ERR_PTR(-ENOENT);
  695. key.ek_client = clp;
  696. key.ek_fsidtype = fsid_type;
  697. memcpy(key.ek_fsid, fsidv, key_len(fsid_type));
  698. ek = svc_expkey_lookup(&key);
  699. if (ek == NULL)
  700. return ERR_PTR(-ENOMEM);
  701. err = cache_check(&svc_expkey_cache, &ek->h, reqp);
  702. if (err)
  703. return ERR_PTR(err);
  704. return ek;
  705. }
  706. static int exp_set_key(svc_client *clp, int fsid_type, u32 *fsidv,
  707. struct svc_export *exp)
  708. {
  709. struct svc_expkey key, *ek;
  710. key.ek_client = clp;
  711. key.ek_fsidtype = fsid_type;
  712. memcpy(key.ek_fsid, fsidv, key_len(fsid_type));
  713. key.ek_path = exp->ex_path;
  714. key.h.expiry_time = NEVER;
  715. key.h.flags = 0;
  716. ek = svc_expkey_lookup(&key);
  717. if (ek)
  718. ek = svc_expkey_update(&key,ek);
  719. if (ek) {
  720. cache_put(&ek->h, &svc_expkey_cache);
  721. return 0;
  722. }
  723. return -ENOMEM;
  724. }
  725. /*
  726. * Find the client's export entry matching xdev/xino.
  727. */
  728. static inline struct svc_expkey *
  729. exp_get_key(svc_client *clp, dev_t dev, ino_t ino)
  730. {
  731. u32 fsidv[3];
  732. if (old_valid_dev(dev)) {
  733. mk_fsid(FSID_DEV, fsidv, dev, ino, 0, NULL);
  734. return exp_find_key(clp, FSID_DEV, fsidv, NULL);
  735. }
  736. mk_fsid(FSID_ENCODE_DEV, fsidv, dev, ino, 0, NULL);
  737. return exp_find_key(clp, FSID_ENCODE_DEV, fsidv, NULL);
  738. }
  739. /*
  740. * Find the client's export entry matching fsid
  741. */
  742. static inline struct svc_expkey *
  743. exp_get_fsid_key(svc_client *clp, int fsid)
  744. {
  745. u32 fsidv[2];
  746. mk_fsid(FSID_NUM, fsidv, 0, 0, fsid, NULL);
  747. return exp_find_key(clp, FSID_NUM, fsidv, NULL);
  748. }
  749. static svc_export *exp_get_by_name(svc_client *clp, struct vfsmount *mnt,
  750. struct dentry *dentry,
  751. struct cache_req *reqp)
  752. {
  753. struct svc_export *exp, key;
  754. int err;
  755. if (!clp)
  756. return ERR_PTR(-ENOENT);
  757. key.ex_client = clp;
  758. key.ex_path.mnt = mnt;
  759. key.ex_path.dentry = dentry;
  760. exp = svc_export_lookup(&key);
  761. if (exp == NULL)
  762. return ERR_PTR(-ENOMEM);
  763. err = cache_check(&svc_export_cache, &exp->h, reqp);
  764. if (err)
  765. return ERR_PTR(err);
  766. return exp;
  767. }
  768. /*
  769. * Find the export entry for a given dentry.
  770. */
  771. static struct svc_export *exp_parent(svc_client *clp, struct vfsmount *mnt,
  772. struct dentry *dentry,
  773. struct cache_req *reqp)
  774. {
  775. svc_export *exp;
  776. dget(dentry);
  777. exp = exp_get_by_name(clp, mnt, dentry, reqp);
  778. while (PTR_ERR(exp) == -ENOENT && !IS_ROOT(dentry)) {
  779. struct dentry *parent;
  780. parent = dget_parent(dentry);
  781. dput(dentry);
  782. dentry = parent;
  783. exp = exp_get_by_name(clp, mnt, dentry, reqp);
  784. }
  785. dput(dentry);
  786. return exp;
  787. }
  788. /*
  789. * Hashtable locking. Write locks are placed only by user processes
  790. * wanting to modify export information.
  791. * Write locking only done in this file. Read locking
  792. * needed externally.
  793. */
  794. static DECLARE_RWSEM(hash_sem);
  795. void
  796. exp_readlock(void)
  797. {
  798. down_read(&hash_sem);
  799. }
  800. static inline void
  801. exp_writelock(void)
  802. {
  803. down_write(&hash_sem);
  804. }
  805. void
  806. exp_readunlock(void)
  807. {
  808. up_read(&hash_sem);
  809. }
  810. static inline void
  811. exp_writeunlock(void)
  812. {
  813. up_write(&hash_sem);
  814. }
  815. static void exp_fsid_unhash(struct svc_export *exp)
  816. {
  817. struct svc_expkey *ek;
  818. if ((exp->ex_flags & NFSEXP_FSID) == 0)
  819. return;
  820. ek = exp_get_fsid_key(exp->ex_client, exp->ex_fsid);
  821. if (!IS_ERR(ek)) {
  822. ek->h.expiry_time = get_seconds()-1;
  823. cache_put(&ek->h, &svc_expkey_cache);
  824. }
  825. svc_expkey_cache.nextcheck = get_seconds();
  826. }
  827. static int exp_fsid_hash(svc_client *clp, struct svc_export *exp)
  828. {
  829. u32 fsid[2];
  830. if ((exp->ex_flags & NFSEXP_FSID) == 0)
  831. return 0;
  832. mk_fsid(FSID_NUM, fsid, 0, 0, exp->ex_fsid, NULL);
  833. return exp_set_key(clp, FSID_NUM, fsid, exp);
  834. }
  835. static int exp_hash(struct auth_domain *clp, struct svc_export *exp)
  836. {
  837. u32 fsid[2];
  838. struct inode *inode = exp->ex_path.dentry->d_inode;
  839. dev_t dev = inode->i_sb->s_dev;
  840. if (old_valid_dev(dev)) {
  841. mk_fsid(FSID_DEV, fsid, dev, inode->i_ino, 0, NULL);
  842. return exp_set_key(clp, FSID_DEV, fsid, exp);
  843. }
  844. mk_fsid(FSID_ENCODE_DEV, fsid, dev, inode->i_ino, 0, NULL);
  845. return exp_set_key(clp, FSID_ENCODE_DEV, fsid, exp);
  846. }
  847. static void exp_unhash(struct svc_export *exp)
  848. {
  849. struct svc_expkey *ek;
  850. struct inode *inode = exp->ex_path.dentry->d_inode;
  851. ek = exp_get_key(exp->ex_client, inode->i_sb->s_dev, inode->i_ino);
  852. if (!IS_ERR(ek)) {
  853. ek->h.expiry_time = get_seconds()-1;
  854. cache_put(&ek->h, &svc_expkey_cache);
  855. }
  856. svc_expkey_cache.nextcheck = get_seconds();
  857. }
  858. /*
  859. * Export a file system.
  860. */
  861. int
  862. exp_export(struct nfsctl_export *nxp)
  863. {
  864. svc_client *clp;
  865. struct svc_export *exp = NULL;
  866. struct svc_export new;
  867. struct svc_expkey *fsid_key = NULL;
  868. struct nameidata nd;
  869. int err;
  870. /* Consistency check */
  871. err = -EINVAL;
  872. if (!exp_verify_string(nxp->ex_path, NFS_MAXPATHLEN) ||
  873. !exp_verify_string(nxp->ex_client, NFSCLNT_IDMAX))
  874. goto out;
  875. dprintk("exp_export called for %s:%s (%x/%ld fl %x).\n",
  876. nxp->ex_client, nxp->ex_path,
  877. (unsigned)nxp->ex_dev, (long)nxp->ex_ino,
  878. nxp->ex_flags);
  879. /* Try to lock the export table for update */
  880. exp_writelock();
  881. /* Look up client info */
  882. if (!(clp = auth_domain_find(nxp->ex_client)))
  883. goto out_unlock;
  884. /* Look up the dentry */
  885. err = path_lookup(nxp->ex_path, 0, &nd);
  886. if (err)
  887. goto out_put_clp;
  888. err = -EINVAL;
  889. exp = exp_get_by_name(clp, nd.path.mnt, nd.path.dentry, NULL);
  890. memset(&new, 0, sizeof(new));
  891. /* must make sure there won't be an ex_fsid clash */
  892. if ((nxp->ex_flags & NFSEXP_FSID) &&
  893. (!IS_ERR(fsid_key = exp_get_fsid_key(clp, nxp->ex_dev))) &&
  894. fsid_key->ek_path.mnt &&
  895. (fsid_key->ek_path.mnt != nd.path.mnt ||
  896. fsid_key->ek_path.dentry != nd.path.dentry))
  897. goto finish;
  898. if (!IS_ERR(exp)) {
  899. /* just a flags/id/fsid update */
  900. exp_fsid_unhash(exp);
  901. exp->ex_flags = nxp->ex_flags;
  902. exp->ex_anon_uid = nxp->ex_anon_uid;
  903. exp->ex_anon_gid = nxp->ex_anon_gid;
  904. exp->ex_fsid = nxp->ex_dev;
  905. err = exp_fsid_hash(clp, exp);
  906. goto finish;
  907. }
  908. err = check_export(nd.path.dentry->d_inode, nxp->ex_flags, NULL);
  909. if (err) goto finish;
  910. err = -ENOMEM;
  911. dprintk("nfsd: creating export entry %p for client %p\n", exp, clp);
  912. new.h.expiry_time = NEVER;
  913. new.h.flags = 0;
  914. new.ex_pathname = kstrdup(nxp->ex_path, GFP_KERNEL);
  915. if (!new.ex_pathname)
  916. goto finish;
  917. new.ex_client = clp;
  918. new.ex_path = nd.path;
  919. new.ex_flags = nxp->ex_flags;
  920. new.ex_anon_uid = nxp->ex_anon_uid;
  921. new.ex_anon_gid = nxp->ex_anon_gid;
  922. new.ex_fsid = nxp->ex_dev;
  923. exp = svc_export_lookup(&new);
  924. if (exp)
  925. exp = svc_export_update(&new, exp);
  926. if (!exp)
  927. goto finish;
  928. if (exp_hash(clp, exp) ||
  929. exp_fsid_hash(clp, exp)) {
  930. /* failed to create at least one index */
  931. exp_do_unexport(exp);
  932. cache_flush();
  933. } else
  934. err = 0;
  935. finish:
  936. kfree(new.ex_pathname);
  937. if (exp)
  938. exp_put(exp);
  939. if (fsid_key && !IS_ERR(fsid_key))
  940. cache_put(&fsid_key->h, &svc_expkey_cache);
  941. path_put(&nd.path);
  942. out_put_clp:
  943. auth_domain_put(clp);
  944. out_unlock:
  945. exp_writeunlock();
  946. out:
  947. return err;
  948. }
  949. /*
  950. * Unexport a file system. The export entry has already
  951. * been removed from the client's list of exported fs's.
  952. */
  953. static void
  954. exp_do_unexport(svc_export *unexp)
  955. {
  956. unexp->h.expiry_time = get_seconds()-1;
  957. svc_export_cache.nextcheck = get_seconds();
  958. exp_unhash(unexp);
  959. exp_fsid_unhash(unexp);
  960. }
  961. /*
  962. * unexport syscall.
  963. */
  964. int
  965. exp_unexport(struct nfsctl_export *nxp)
  966. {
  967. struct auth_domain *dom;
  968. svc_export *exp;
  969. struct nameidata nd;
  970. int err;
  971. /* Consistency check */
  972. if (!exp_verify_string(nxp->ex_path, NFS_MAXPATHLEN) ||
  973. !exp_verify_string(nxp->ex_client, NFSCLNT_IDMAX))
  974. return -EINVAL;
  975. exp_writelock();
  976. err = -EINVAL;
  977. dom = auth_domain_find(nxp->ex_client);
  978. if (!dom) {
  979. dprintk("nfsd: unexport couldn't find %s\n", nxp->ex_client);
  980. goto out_unlock;
  981. }
  982. err = path_lookup(nxp->ex_path, 0, &nd);
  983. if (err)
  984. goto out_domain;
  985. err = -EINVAL;
  986. exp = exp_get_by_name(dom, nd.path.mnt, nd.path.dentry, NULL);
  987. path_put(&nd.path);
  988. if (IS_ERR(exp))
  989. goto out_domain;
  990. exp_do_unexport(exp);
  991. exp_put(exp);
  992. err = 0;
  993. out_domain:
  994. auth_domain_put(dom);
  995. cache_flush();
  996. out_unlock:
  997. exp_writeunlock();
  998. return err;
  999. }
  1000. /*
  1001. * Obtain the root fh on behalf of a client.
  1002. * This could be done in user space, but I feel that it adds some safety
  1003. * since its harder to fool a kernel module than a user space program.
  1004. */
  1005. int
  1006. exp_rootfh(svc_client *clp, char *path, struct knfsd_fh *f, int maxsize)
  1007. {
  1008. struct svc_export *exp;
  1009. struct nameidata nd;
  1010. struct inode *inode;
  1011. struct svc_fh fh;
  1012. int err;
  1013. err = -EPERM;
  1014. /* NB: we probably ought to check that it's NUL-terminated */
  1015. if (path_lookup(path, 0, &nd)) {
  1016. printk("nfsd: exp_rootfh path not found %s", path);
  1017. return err;
  1018. }
  1019. inode = nd.path.dentry->d_inode;
  1020. dprintk("nfsd: exp_rootfh(%s [%p] %s:%s/%ld)\n",
  1021. path, nd.path.dentry, clp->name,
  1022. inode->i_sb->s_id, inode->i_ino);
  1023. exp = exp_parent(clp, nd.path.mnt, nd.path.dentry, NULL);
  1024. if (IS_ERR(exp)) {
  1025. err = PTR_ERR(exp);
  1026. goto out;
  1027. }
  1028. /*
  1029. * fh must be initialized before calling fh_compose
  1030. */
  1031. fh_init(&fh, maxsize);
  1032. if (fh_compose(&fh, exp, nd.path.dentry, NULL))
  1033. err = -EINVAL;
  1034. else
  1035. err = 0;
  1036. memcpy(f, &fh.fh_handle, sizeof(struct knfsd_fh));
  1037. fh_put(&fh);
  1038. exp_put(exp);
  1039. out:
  1040. path_put(&nd.path);
  1041. return err;
  1042. }
  1043. static struct svc_export *exp_find(struct auth_domain *clp, int fsid_type,
  1044. u32 *fsidv, struct cache_req *reqp)
  1045. {
  1046. struct svc_export *exp;
  1047. struct svc_expkey *ek = exp_find_key(clp, fsid_type, fsidv, reqp);
  1048. if (IS_ERR(ek))
  1049. return ERR_CAST(ek);
  1050. exp = exp_get_by_name(clp, ek->ek_path.mnt, ek->ek_path.dentry, reqp);
  1051. cache_put(&ek->h, &svc_expkey_cache);
  1052. if (IS_ERR(exp))
  1053. return ERR_CAST(exp);
  1054. return exp;
  1055. }
  1056. __be32 check_nfsd_access(struct svc_export *exp, struct svc_rqst *rqstp)
  1057. {
  1058. struct exp_flavor_info *f;
  1059. struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;
  1060. /* legacy gss-only clients are always OK: */
  1061. if (exp->ex_client == rqstp->rq_gssclient)
  1062. return 0;
  1063. /* ip-address based client; check sec= export option: */
  1064. for (f = exp->ex_flavors; f < end; f++) {
  1065. if (f->pseudoflavor == rqstp->rq_flavor)
  1066. return 0;
  1067. }
  1068. /* defaults in absence of sec= options: */
  1069. if (exp->ex_nflavors == 0) {
  1070. if (rqstp->rq_flavor == RPC_AUTH_NULL ||
  1071. rqstp->rq_flavor == RPC_AUTH_UNIX)
  1072. return 0;
  1073. }
  1074. return nfserr_wrongsec;
  1075. }
  1076. /*
  1077. * Uses rq_client and rq_gssclient to find an export; uses rq_client (an
  1078. * auth_unix client) if it's available and has secinfo information;
  1079. * otherwise, will try to use rq_gssclient.
  1080. *
  1081. * Called from functions that handle requests; functions that do work on
  1082. * behalf of mountd are passed a single client name to use, and should
  1083. * use exp_get_by_name() or exp_find().
  1084. */
  1085. struct svc_export *
  1086. rqst_exp_get_by_name(struct svc_rqst *rqstp, struct vfsmount *mnt,
  1087. struct dentry *dentry)
  1088. {
  1089. struct svc_export *gssexp, *exp = ERR_PTR(-ENOENT);
  1090. if (rqstp->rq_client == NULL)
  1091. goto gss;
  1092. /* First try the auth_unix client: */
  1093. exp = exp_get_by_name(rqstp->rq_client, mnt, dentry,
  1094. &rqstp->rq_chandle);
  1095. if (PTR_ERR(exp) == -ENOENT)
  1096. goto gss;
  1097. if (IS_ERR(exp))
  1098. return exp;
  1099. /* If it has secinfo, assume there are no gss/... clients */
  1100. if (exp->ex_nflavors > 0)
  1101. return exp;
  1102. gss:
  1103. /* Otherwise, try falling back on gss client */
  1104. if (rqstp->rq_gssclient == NULL)
  1105. return exp;
  1106. gssexp = exp_get_by_name(rqstp->rq_gssclient, mnt, dentry,
  1107. &rqstp->rq_chandle);
  1108. if (PTR_ERR(gssexp) == -ENOENT)
  1109. return exp;
  1110. if (!IS_ERR(exp))
  1111. exp_put(exp);
  1112. return gssexp;
  1113. }
  1114. struct svc_export *
  1115. rqst_exp_find(struct svc_rqst *rqstp, int fsid_type, u32 *fsidv)
  1116. {
  1117. struct svc_export *gssexp, *exp = ERR_PTR(-ENOENT);
  1118. if (rqstp->rq_client == NULL)
  1119. goto gss;
  1120. /* First try the auth_unix client: */
  1121. exp = exp_find(rqstp->rq_client, fsid_type, fsidv, &rqstp->rq_chandle);
  1122. if (PTR_ERR(exp) == -ENOENT)
  1123. goto gss;
  1124. if (IS_ERR(exp))
  1125. return exp;
  1126. /* If it has secinfo, assume there are no gss/... clients */
  1127. if (exp->ex_nflavors > 0)
  1128. return exp;
  1129. gss:
  1130. /* Otherwise, try falling back on gss client */
  1131. if (rqstp->rq_gssclient == NULL)
  1132. return exp;
  1133. gssexp = exp_find(rqstp->rq_gssclient, fsid_type, fsidv,
  1134. &rqstp->rq_chandle);
  1135. if (PTR_ERR(gssexp) == -ENOENT)
  1136. return exp;
  1137. if (!IS_ERR(exp))
  1138. exp_put(exp);
  1139. return gssexp;
  1140. }
  1141. struct svc_export *
  1142. rqst_exp_parent(struct svc_rqst *rqstp, struct vfsmount *mnt,
  1143. struct dentry *dentry)
  1144. {
  1145. struct svc_export *exp;
  1146. dget(dentry);
  1147. exp = rqst_exp_get_by_name(rqstp, mnt, dentry);
  1148. while (PTR_ERR(exp) == -ENOENT && !IS_ROOT(dentry)) {
  1149. struct dentry *parent;
  1150. parent = dget_parent(dentry);
  1151. dput(dentry);
  1152. dentry = parent;
  1153. exp = rqst_exp_get_by_name(rqstp, mnt, dentry);
  1154. }
  1155. dput(dentry);
  1156. return exp;
  1157. }
  1158. /*
  1159. * Called when we need the filehandle for the root of the pseudofs,
  1160. * for a given NFSv4 client. The root is defined to be the
  1161. * export point with fsid==0
  1162. */
  1163. __be32
  1164. exp_pseudoroot(struct svc_rqst *rqstp, struct svc_fh *fhp)
  1165. {
  1166. struct svc_export *exp;
  1167. __be32 rv;
  1168. u32 fsidv[2];
  1169. mk_fsid(FSID_NUM, fsidv, 0, 0, 0, NULL);
  1170. exp = rqst_exp_find(rqstp, FSID_NUM, fsidv);
  1171. if (IS_ERR(exp))
  1172. return nfserrno(PTR_ERR(exp));
  1173. rv = fh_compose(fhp, exp, exp->ex_path.dentry, NULL);
  1174. if (rv)
  1175. goto out;
  1176. rv = check_nfsd_access(exp, rqstp);
  1177. out:
  1178. exp_put(exp);
  1179. return rv;
  1180. }
  1181. /* Iterator */
  1182. static void *e_start(struct seq_file *m, loff_t *pos)
  1183. __acquires(svc_export_cache.hash_lock)
  1184. {
  1185. loff_t n = *pos;
  1186. unsigned hash, export;
  1187. struct cache_head *ch;
  1188. exp_readlock();
  1189. read_lock(&svc_export_cache.hash_lock);
  1190. if (!n--)
  1191. return SEQ_START_TOKEN;
  1192. hash = n >> 32;
  1193. export = n & ((1LL<<32) - 1);
  1194. for (ch=export_table[hash]; ch; ch=ch->next)
  1195. if (!export--)
  1196. return ch;
  1197. n &= ~((1LL<<32) - 1);
  1198. do {
  1199. hash++;
  1200. n += 1LL<<32;
  1201. } while(hash < EXPORT_HASHMAX && export_table[hash]==NULL);
  1202. if (hash >= EXPORT_HASHMAX)
  1203. return NULL;
  1204. *pos = n+1;
  1205. return export_table[hash];
  1206. }
  1207. static void *e_next(struct seq_file *m, void *p, loff_t *pos)
  1208. {
  1209. struct cache_head *ch = p;
  1210. int hash = (*pos >> 32);
  1211. if (p == SEQ_START_TOKEN)
  1212. hash = 0;
  1213. else if (ch->next == NULL) {
  1214. hash++;
  1215. *pos += 1LL<<32;
  1216. } else {
  1217. ++*pos;
  1218. return ch->next;
  1219. }
  1220. *pos &= ~((1LL<<32) - 1);
  1221. while (hash < EXPORT_HASHMAX && export_table[hash] == NULL) {
  1222. hash++;
  1223. *pos += 1LL<<32;
  1224. }
  1225. if (hash >= EXPORT_HASHMAX)
  1226. return NULL;
  1227. ++*pos;
  1228. return export_table[hash];
  1229. }
  1230. static void e_stop(struct seq_file *m, void *p)
  1231. __releases(svc_export_cache.hash_lock)
  1232. {
  1233. read_unlock(&svc_export_cache.hash_lock);
  1234. exp_readunlock();
  1235. }
  1236. static struct flags {
  1237. int flag;
  1238. char *name[2];
  1239. } expflags[] = {
  1240. { NFSEXP_READONLY, {"ro", "rw"}},
  1241. { NFSEXP_INSECURE_PORT, {"insecure", ""}},
  1242. { NFSEXP_ROOTSQUASH, {"root_squash", "no_root_squash"}},
  1243. { NFSEXP_ALLSQUASH, {"all_squash", ""}},
  1244. { NFSEXP_ASYNC, {"async", "sync"}},
  1245. { NFSEXP_GATHERED_WRITES, {"wdelay", "no_wdelay"}},
  1246. { NFSEXP_NOHIDE, {"nohide", ""}},
  1247. { NFSEXP_CROSSMOUNT, {"crossmnt", ""}},
  1248. { NFSEXP_NOSUBTREECHECK, {"no_subtree_check", ""}},
  1249. { NFSEXP_NOAUTHNLM, {"insecure_locks", ""}},
  1250. #ifdef MSNFS
  1251. { NFSEXP_MSNFS, {"msnfs", ""}},
  1252. #endif
  1253. { 0, {"", ""}}
  1254. };
  1255. static void show_expflags(struct seq_file *m, int flags, int mask)
  1256. {
  1257. struct flags *flg;
  1258. int state, first = 0;
  1259. for (flg = expflags; flg->flag; flg++) {
  1260. if (flg->flag & ~mask)
  1261. continue;
  1262. state = (flg->flag & flags) ? 0 : 1;
  1263. if (*flg->name[state])
  1264. seq_printf(m, "%s%s", first++?",":"", flg->name[state]);
  1265. }
  1266. }
  1267. static void show_secinfo_flags(struct seq_file *m, int flags)
  1268. {
  1269. seq_printf(m, ",");
  1270. show_expflags(m, flags, NFSEXP_SECINFO_FLAGS);
  1271. }
  1272. static void show_secinfo(struct seq_file *m, struct svc_export *exp)
  1273. {
  1274. struct exp_flavor_info *f;
  1275. struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;
  1276. int lastflags = 0, first = 0;
  1277. if (exp->ex_nflavors == 0)
  1278. return;
  1279. for (f = exp->ex_flavors; f < end; f++) {
  1280. if (first || f->flags != lastflags) {
  1281. if (!first)
  1282. show_secinfo_flags(m, lastflags);
  1283. seq_printf(m, ",sec=%d", f->pseudoflavor);
  1284. lastflags = f->flags;
  1285. } else {
  1286. seq_printf(m, ":%d", f->pseudoflavor);
  1287. }
  1288. }
  1289. show_secinfo_flags(m, lastflags);
  1290. }
  1291. static void exp_flags(struct seq_file *m, int flag, int fsid,
  1292. uid_t anonu, uid_t anong, struct nfsd4_fs_locations *fsloc)
  1293. {
  1294. show_expflags(m, flag, NFSEXP_ALLFLAGS);
  1295. if (flag & NFSEXP_FSID)
  1296. seq_printf(m, ",fsid=%d", fsid);
  1297. if (anonu != (uid_t)-2 && anonu != (0x10000-2))
  1298. seq_printf(m, ",anonuid=%u", anonu);
  1299. if (anong != (gid_t)-2 && anong != (0x10000-2))
  1300. seq_printf(m, ",anongid=%u", anong);
  1301. if (fsloc && fsloc->locations_count > 0) {
  1302. char *loctype = (fsloc->migrated) ? "refer" : "replicas";
  1303. int i;
  1304. seq_printf(m, ",%s=", loctype);
  1305. seq_escape(m, fsloc->locations[0].path, ",;@ \t\n\\");
  1306. seq_putc(m, '@');
  1307. seq_escape(m, fsloc->locations[0].hosts, ",;@ \t\n\\");
  1308. for (i = 1; i < fsloc->locations_count; i++) {
  1309. seq_putc(m, ';');
  1310. seq_escape(m, fsloc->locations[i].path, ",;@ \t\n\\");
  1311. seq_putc(m, '@');
  1312. seq_escape(m, fsloc->locations[i].hosts, ",;@ \t\n\\");
  1313. }
  1314. }
  1315. }
  1316. static int e_show(struct seq_file *m, void *p)
  1317. {
  1318. struct cache_head *cp = p;
  1319. struct svc_export *exp = container_of(cp, struct svc_export, h);
  1320. if (p == SEQ_START_TOKEN) {
  1321. seq_puts(m, "# Version 1.1\n");
  1322. seq_puts(m, "# Path Client(Flags) # IPs\n");
  1323. return 0;
  1324. }
  1325. cache_get(&exp->h);
  1326. if (cache_check(&svc_export_cache, &exp->h, NULL))
  1327. return 0;
  1328. cache_put(&exp->h, &svc_export_cache);
  1329. return svc_export_show(m, &svc_export_cache, cp);
  1330. }
  1331. struct seq_operations nfs_exports_op = {
  1332. .start = e_start,
  1333. .next = e_next,
  1334. .stop = e_stop,
  1335. .show = e_show,
  1336. };
  1337. /*
  1338. * Add or modify a client.
  1339. * Change requests may involve the list of host addresses. The list of
  1340. * exports and possibly existing uid maps are left untouched.
  1341. */
  1342. int
  1343. exp_addclient(struct nfsctl_client *ncp)
  1344. {
  1345. struct auth_domain *dom;
  1346. int i, err;
  1347. struct in6_addr addr6;
  1348. /* First, consistency check. */
  1349. err = -EINVAL;
  1350. if (! exp_verify_string(ncp->cl_ident, NFSCLNT_IDMAX))
  1351. goto out;
  1352. if (ncp->cl_naddr > NFSCLNT_ADDRMAX)
  1353. goto out;
  1354. /* Lock the hashtable */
  1355. exp_writelock();
  1356. dom = unix_domain_find(ncp->cl_ident);
  1357. err = -ENOMEM;
  1358. if (!dom)
  1359. goto out_unlock;
  1360. /* Insert client into hashtable. */
  1361. for (i = 0; i < ncp->cl_naddr; i++) {
  1362. ipv6_addr_set_v4mapped(ncp->cl_addrlist[i].s_addr, &addr6);
  1363. auth_unix_add_addr(&addr6, dom);
  1364. }
  1365. auth_unix_forget_old(dom);
  1366. auth_domain_put(dom);
  1367. err = 0;
  1368. out_unlock:
  1369. exp_writeunlock();
  1370. out:
  1371. return err;
  1372. }
  1373. /*
  1374. * Delete a client given an identifier.
  1375. */
  1376. int
  1377. exp_delclient(struct nfsctl_client *ncp)
  1378. {
  1379. int err;
  1380. struct auth_domain *dom;
  1381. err = -EINVAL;
  1382. if (!exp_verify_string(ncp->cl_ident, NFSCLNT_IDMAX))
  1383. goto out;
  1384. /* Lock the hashtable */
  1385. exp_writelock();
  1386. dom = auth_domain_find(ncp->cl_ident);
  1387. /* just make sure that no addresses work
  1388. * and that it will expire soon
  1389. */
  1390. if (dom) {
  1391. err = auth_unix_forget_old(dom);
  1392. auth_domain_put(dom);
  1393. }
  1394. exp_writeunlock();
  1395. out:
  1396. return err;
  1397. }
  1398. /*
  1399. * Verify that string is non-empty and does not exceed max length.
  1400. */
  1401. static int
  1402. exp_verify_string(char *cp, int max)
  1403. {
  1404. int i;
  1405. for (i = 0; i < max; i++)
  1406. if (!cp[i])
  1407. return i;
  1408. cp[i] = 0;
  1409. printk(KERN_NOTICE "nfsd: couldn't validate string %s\n", cp);
  1410. return 0;
  1411. }
  1412. /*
  1413. * Initialize the exports module.
  1414. */
  1415. int
  1416. nfsd_export_init(void)
  1417. {
  1418. int rv;
  1419. dprintk("nfsd: initializing export module.\n");
  1420. rv = cache_register(&svc_export_cache);
  1421. if (rv)
  1422. return rv;
  1423. rv = cache_register(&svc_expkey_cache);
  1424. if (rv)
  1425. cache_unregister(&svc_export_cache);
  1426. return rv;
  1427. }
  1428. /*
  1429. * Flush exports table - called when last nfsd thread is killed
  1430. */
  1431. void
  1432. nfsd_export_flush(void)
  1433. {
  1434. exp_writelock();
  1435. cache_purge(&svc_expkey_cache);
  1436. cache_purge(&svc_export_cache);
  1437. exp_writeunlock();
  1438. }
  1439. /*
  1440. * Shutdown the exports module.
  1441. */
  1442. void
  1443. nfsd_export_shutdown(void)
  1444. {
  1445. dprintk("nfsd: shutting down export module.\n");
  1446. exp_writelock();
  1447. cache_unregister(&svc_expkey_cache);
  1448. cache_unregister(&svc_export_cache);
  1449. svcauth_unix_purge();
  1450. exp_writeunlock();
  1451. dprintk("nfsd: export shutdown complete.\n");
  1452. }