export.c 39 KB

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