export.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  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/sched.h>
  19. #include <linux/stat.h>
  20. #include <linux/in.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/syscalls.h>
  23. #include <linux/rwsem.h>
  24. #include <linux/dcache.h>
  25. #include <linux/namei.h>
  26. #include <linux/mount.h>
  27. #include <linux/hash.h>
  28. #include <linux/module.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. #define NFSDDBG_FACILITY NFSDDBG_EXPORT
  35. typedef struct auth_domain svc_client;
  36. typedef struct svc_export svc_export;
  37. static void exp_do_unexport(svc_export *unexp);
  38. static int exp_verify_string(char *cp, int max);
  39. /*
  40. * We have two caches.
  41. * One maps client+vfsmnt+dentry to export options - the export map
  42. * The other maps client+filehandle-fragment to export options. - the expkey map
  43. *
  44. * The export options are actually stored in the first map, and the
  45. * second map contains a reference to the entry in the first map.
  46. */
  47. #define EXPKEY_HASHBITS 8
  48. #define EXPKEY_HASHMAX (1 << EXPKEY_HASHBITS)
  49. #define EXPKEY_HASHMASK (EXPKEY_HASHMAX -1)
  50. static struct cache_head *expkey_table[EXPKEY_HASHMAX];
  51. static void expkey_put(struct kref *ref)
  52. {
  53. struct svc_expkey *key = container_of(ref, struct svc_expkey, h.ref);
  54. if (test_bit(CACHE_VALID, &key->h.flags) &&
  55. !test_bit(CACHE_NEGATIVE, &key->h.flags)) {
  56. dput(key->ek_dentry);
  57. mntput(key->ek_mnt);
  58. }
  59. auth_domain_put(key->ek_client);
  60. kfree(key);
  61. }
  62. static void expkey_request(struct cache_detail *cd,
  63. struct cache_head *h,
  64. char **bpp, int *blen)
  65. {
  66. /* client fsidtype \xfsid */
  67. struct svc_expkey *ek = container_of(h, struct svc_expkey, h);
  68. char type[5];
  69. qword_add(bpp, blen, ek->ek_client->name);
  70. snprintf(type, 5, "%d", ek->ek_fsidtype);
  71. qword_add(bpp, blen, type);
  72. qword_addhex(bpp, blen, (char*)ek->ek_fsid, key_len(ek->ek_fsidtype));
  73. (*bpp)[-1] = '\n';
  74. }
  75. static struct svc_expkey *svc_expkey_update(struct svc_expkey *new, struct svc_expkey *old);
  76. static struct svc_expkey *svc_expkey_lookup(struct svc_expkey *);
  77. static struct cache_detail svc_expkey_cache;
  78. static int expkey_parse(struct cache_detail *cd, char *mesg, int mlen)
  79. {
  80. /* client fsidtype fsid [path] */
  81. char *buf;
  82. int len;
  83. struct auth_domain *dom = NULL;
  84. int err;
  85. int fsidtype;
  86. char *ep;
  87. struct svc_expkey key;
  88. struct svc_expkey *ek;
  89. if (mesg[mlen-1] != '\n')
  90. return -EINVAL;
  91. mesg[mlen-1] = 0;
  92. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  93. err = -ENOMEM;
  94. if (!buf) goto out;
  95. err = -EINVAL;
  96. if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
  97. goto out;
  98. err = -ENOENT;
  99. dom = auth_domain_find(buf);
  100. if (!dom)
  101. goto out;
  102. dprintk("found domain %s\n", buf);
  103. err = -EINVAL;
  104. if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
  105. goto out;
  106. fsidtype = simple_strtoul(buf, &ep, 10);
  107. if (*ep)
  108. goto out;
  109. dprintk("found fsidtype %d\n", fsidtype);
  110. if (key_len(fsidtype)==0) /* invalid type */
  111. goto out;
  112. if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
  113. goto out;
  114. dprintk("found fsid length %d\n", len);
  115. if (len != key_len(fsidtype))
  116. goto out;
  117. /* OK, we seem to have a valid key */
  118. key.h.flags = 0;
  119. key.h.expiry_time = get_expiry(&mesg);
  120. if (key.h.expiry_time == 0)
  121. goto out;
  122. key.ek_client = dom;
  123. key.ek_fsidtype = fsidtype;
  124. memcpy(key.ek_fsid, buf, len);
  125. ek = svc_expkey_lookup(&key);
  126. err = -ENOMEM;
  127. if (!ek)
  128. goto out;
  129. /* now we want a pathname, or empty meaning NEGATIVE */
  130. err = -EINVAL;
  131. if ((len=qword_get(&mesg, buf, PAGE_SIZE)) < 0)
  132. goto out;
  133. dprintk("Path seems to be <%s>\n", buf);
  134. err = 0;
  135. if (len == 0) {
  136. set_bit(CACHE_NEGATIVE, &key.h.flags);
  137. ek = svc_expkey_update(&key, ek);
  138. if (ek)
  139. cache_put(&ek->h, &svc_expkey_cache);
  140. else err = -ENOMEM;
  141. } else {
  142. struct nameidata nd;
  143. err = path_lookup(buf, 0, &nd);
  144. if (err)
  145. goto out;
  146. dprintk("Found the path %s\n", buf);
  147. key.ek_mnt = nd.mnt;
  148. key.ek_dentry = nd.dentry;
  149. ek = svc_expkey_update(&key, ek);
  150. if (ek)
  151. cache_put(&ek->h, &svc_expkey_cache);
  152. else
  153. err = -ENOMEM;
  154. path_release(&nd);
  155. }
  156. cache_flush();
  157. out:
  158. if (dom)
  159. auth_domain_put(dom);
  160. kfree(buf);
  161. return err;
  162. }
  163. static int expkey_show(struct seq_file *m,
  164. struct cache_detail *cd,
  165. struct cache_head *h)
  166. {
  167. struct svc_expkey *ek ;
  168. if (h ==NULL) {
  169. seq_puts(m, "#domain fsidtype fsid [path]\n");
  170. return 0;
  171. }
  172. ek = container_of(h, struct svc_expkey, h);
  173. seq_printf(m, "%s %d 0x%08x", ek->ek_client->name,
  174. ek->ek_fsidtype, ek->ek_fsid[0]);
  175. if (ek->ek_fsidtype != 1)
  176. seq_printf(m, "%08x", ek->ek_fsid[1]);
  177. if (ek->ek_fsidtype == 2)
  178. seq_printf(m, "%08x", ek->ek_fsid[2]);
  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_mnt, ek->ek_dentry, "\\ \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. new->ek_fsid[0] = item->ek_fsid[0];
  206. new->ek_fsid[1] = item->ek_fsid[1];
  207. new->ek_fsid[2] = item->ek_fsid[2];
  208. }
  209. static inline void expkey_update(struct cache_head *cnew,
  210. struct cache_head *citem)
  211. {
  212. struct svc_expkey *new = container_of(cnew, struct svc_expkey, h);
  213. struct svc_expkey *item = container_of(citem, struct svc_expkey, h);
  214. new->ek_mnt = mntget(item->ek_mnt);
  215. new->ek_dentry = dget(item->ek_dentry);
  216. }
  217. static struct cache_head *expkey_alloc(void)
  218. {
  219. struct svc_expkey *i = kmalloc(sizeof(*i), GFP_KERNEL);
  220. if (i)
  221. return &i->h;
  222. else
  223. return NULL;
  224. }
  225. static struct cache_detail svc_expkey_cache = {
  226. .owner = THIS_MODULE,
  227. .hash_size = EXPKEY_HASHMAX,
  228. .hash_table = expkey_table,
  229. .name = "nfsd.fh",
  230. .cache_put = expkey_put,
  231. .cache_request = expkey_request,
  232. .cache_parse = expkey_parse,
  233. .cache_show = expkey_show,
  234. .match = expkey_match,
  235. .init = expkey_init,
  236. .update = expkey_update,
  237. .alloc = expkey_alloc,
  238. };
  239. static struct svc_expkey *
  240. svc_expkey_lookup(struct svc_expkey *item)
  241. {
  242. struct cache_head *ch;
  243. int hash = item->ek_fsidtype;
  244. char * cp = (char*)item->ek_fsid;
  245. int len = key_len(item->ek_fsidtype);
  246. hash ^= hash_mem(cp, len, EXPKEY_HASHBITS);
  247. hash ^= hash_ptr(item->ek_client, EXPKEY_HASHBITS);
  248. hash &= EXPKEY_HASHMASK;
  249. ch = sunrpc_cache_lookup(&svc_expkey_cache, &item->h,
  250. hash);
  251. if (ch)
  252. return container_of(ch, struct svc_expkey, h);
  253. else
  254. return NULL;
  255. }
  256. static struct svc_expkey *
  257. svc_expkey_update(struct svc_expkey *new, struct svc_expkey *old)
  258. {
  259. struct cache_head *ch;
  260. int hash = new->ek_fsidtype;
  261. char * cp = (char*)new->ek_fsid;
  262. int len = key_len(new->ek_fsidtype);
  263. hash ^= hash_mem(cp, len, EXPKEY_HASHBITS);
  264. hash ^= hash_ptr(new->ek_client, EXPKEY_HASHBITS);
  265. hash &= EXPKEY_HASHMASK;
  266. ch = sunrpc_cache_update(&svc_expkey_cache, &new->h,
  267. &old->h, hash);
  268. if (ch)
  269. return container_of(ch, struct svc_expkey, h);
  270. else
  271. return NULL;
  272. }
  273. #define EXPORT_HASHBITS 8
  274. #define EXPORT_HASHMAX (1<< EXPORT_HASHBITS)
  275. #define EXPORT_HASHMASK (EXPORT_HASHMAX -1)
  276. static struct cache_head *export_table[EXPORT_HASHMAX];
  277. static void nfsd4_fslocs_free(struct nfsd4_fs_locations *fsloc)
  278. {
  279. int i;
  280. for (i = 0; i < fsloc->locations_count; i++) {
  281. kfree(fsloc->locations[i].path);
  282. kfree(fsloc->locations[i].hosts);
  283. }
  284. kfree(fsloc->locations);
  285. }
  286. static void svc_export_put(struct kref *ref)
  287. {
  288. struct svc_export *exp = container_of(ref, struct svc_export, h.ref);
  289. dput(exp->ex_dentry);
  290. mntput(exp->ex_mnt);
  291. auth_domain_put(exp->ex_client);
  292. kfree(exp->ex_path);
  293. nfsd4_fslocs_free(&exp->ex_fslocs);
  294. kfree(exp);
  295. }
  296. static void svc_export_request(struct cache_detail *cd,
  297. struct cache_head *h,
  298. char **bpp, int *blen)
  299. {
  300. /* client path */
  301. struct svc_export *exp = container_of(h, struct svc_export, h);
  302. char *pth;
  303. qword_add(bpp, blen, exp->ex_client->name);
  304. pth = d_path(exp->ex_dentry, exp->ex_mnt, *bpp, *blen);
  305. if (IS_ERR(pth)) {
  306. /* is this correct? */
  307. (*bpp)[0] = '\n';
  308. return;
  309. }
  310. qword_add(bpp, blen, pth);
  311. (*bpp)[-1] = '\n';
  312. }
  313. static struct svc_export *svc_export_update(struct svc_export *new,
  314. struct svc_export *old);
  315. static struct svc_export *svc_export_lookup(struct svc_export *);
  316. static int check_export(struct inode *inode, int flags)
  317. {
  318. /* We currently export only dirs and regular files.
  319. * This is what umountd does.
  320. */
  321. if (!S_ISDIR(inode->i_mode) &&
  322. !S_ISREG(inode->i_mode))
  323. return -ENOTDIR;
  324. /* There are two requirements on a filesystem to be exportable.
  325. * 1: We must be able to identify the filesystem from a number.
  326. * either a device number (so FS_REQUIRES_DEV needed)
  327. * or an FSID number (so NFSEXP_FSID needed).
  328. * 2: We must be able to find an inode from a filehandle.
  329. * This means that s_export_op must be set.
  330. */
  331. if (!(inode->i_sb->s_type->fs_flags & FS_REQUIRES_DEV) &&
  332. !(flags & NFSEXP_FSID)) {
  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. len = qword_get(mesg, buf, PAGE_SIZE);
  353. if (len != 5 || memcmp(buf, "fsloc", 5))
  354. return 0;
  355. /* listsize */
  356. err = get_int(mesg, &fsloc->locations_count);
  357. if (err)
  358. return err;
  359. if (fsloc->locations_count > MAX_FS_LOCATIONS)
  360. return -EINVAL;
  361. if (fsloc->locations_count == 0)
  362. return 0;
  363. fsloc->locations = kzalloc(fsloc->locations_count
  364. * sizeof(struct nfsd4_fs_location), GFP_KERNEL);
  365. if (!fsloc->locations)
  366. return -ENOMEM;
  367. for (i=0; i < fsloc->locations_count; i++) {
  368. /* colon separated host list */
  369. err = -EINVAL;
  370. len = qword_get(mesg, buf, PAGE_SIZE);
  371. if (len <= 0)
  372. goto out_free_all;
  373. err = -ENOMEM;
  374. fsloc->locations[i].hosts = kstrdup(buf, GFP_KERNEL);
  375. if (!fsloc->locations[i].hosts)
  376. goto out_free_all;
  377. err = -EINVAL;
  378. /* slash separated path component list */
  379. len = qword_get(mesg, buf, PAGE_SIZE);
  380. if (len <= 0)
  381. goto out_free_all;
  382. err = -ENOMEM;
  383. fsloc->locations[i].path = kstrdup(buf, GFP_KERNEL);
  384. if (!fsloc->locations[i].path)
  385. goto out_free_all;
  386. }
  387. /* migrated */
  388. err = get_int(mesg, &migrated);
  389. if (err)
  390. goto out_free_all;
  391. err = -EINVAL;
  392. if (migrated < 0 || migrated > 1)
  393. goto out_free_all;
  394. fsloc->migrated = migrated;
  395. return 0;
  396. out_free_all:
  397. nfsd4_fslocs_free(fsloc);
  398. return err;
  399. }
  400. #else /* CONFIG_NFSD_V4 */
  401. static inline int fsloc_parse(char **mesg, char *buf, struct nfsd4_fs_locations *fsloc) { return 0; }
  402. #endif
  403. static int svc_export_parse(struct cache_detail *cd, char *mesg, int mlen)
  404. {
  405. /* client path expiry [flags anonuid anongid fsid] */
  406. char *buf;
  407. int len;
  408. int err;
  409. struct auth_domain *dom = NULL;
  410. struct nameidata nd;
  411. struct svc_export exp, *expp;
  412. int an_int;
  413. nd.dentry = NULL;
  414. exp.ex_path = NULL;
  415. if (mesg[mlen-1] != '\n')
  416. return -EINVAL;
  417. mesg[mlen-1] = 0;
  418. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  419. err = -ENOMEM;
  420. if (!buf) goto out;
  421. /* client */
  422. len = qword_get(&mesg, buf, PAGE_SIZE);
  423. err = -EINVAL;
  424. if (len <= 0) goto out;
  425. err = -ENOENT;
  426. dom = auth_domain_find(buf);
  427. if (!dom)
  428. goto out;
  429. /* path */
  430. err = -EINVAL;
  431. if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
  432. goto out;
  433. err = path_lookup(buf, 0, &nd);
  434. if (err) goto out_no_path;
  435. exp.h.flags = 0;
  436. exp.ex_client = dom;
  437. exp.ex_mnt = nd.mnt;
  438. exp.ex_dentry = nd.dentry;
  439. exp.ex_path = kstrdup(buf, GFP_KERNEL);
  440. err = -ENOMEM;
  441. if (!exp.ex_path)
  442. goto out;
  443. /* expiry */
  444. err = -EINVAL;
  445. exp.h.expiry_time = get_expiry(&mesg);
  446. if (exp.h.expiry_time == 0)
  447. goto out;
  448. /* fs locations */
  449. exp.ex_fslocs.locations = NULL;
  450. exp.ex_fslocs.locations_count = 0;
  451. exp.ex_fslocs.migrated = 0;
  452. /* flags */
  453. err = get_int(&mesg, &an_int);
  454. if (err == -ENOENT)
  455. set_bit(CACHE_NEGATIVE, &exp.h.flags);
  456. else {
  457. if (err || an_int < 0) goto out;
  458. exp.ex_flags= an_int;
  459. /* anon uid */
  460. err = get_int(&mesg, &an_int);
  461. if (err) goto out;
  462. exp.ex_anon_uid= an_int;
  463. /* anon gid */
  464. err = get_int(&mesg, &an_int);
  465. if (err) goto out;
  466. exp.ex_anon_gid= an_int;
  467. /* fsid */
  468. err = get_int(&mesg, &an_int);
  469. if (err) goto out;
  470. exp.ex_fsid = an_int;
  471. err = check_export(nd.dentry->d_inode, exp.ex_flags);
  472. if (err) goto out;
  473. err = fsloc_parse(&mesg, buf, &exp.ex_fslocs);
  474. if (err)
  475. goto out;
  476. }
  477. expp = svc_export_lookup(&exp);
  478. if (expp)
  479. expp = svc_export_update(&exp, expp);
  480. else
  481. err = -ENOMEM;
  482. cache_flush();
  483. if (expp == NULL)
  484. err = -ENOMEM;
  485. else
  486. exp_put(expp);
  487. out:
  488. kfree(exp.ex_path);
  489. if (nd.dentry)
  490. path_release(&nd);
  491. out_no_path:
  492. if (dom)
  493. auth_domain_put(dom);
  494. kfree(buf);
  495. return err;
  496. }
  497. static void exp_flags(struct seq_file *m, int flag, int fsid,
  498. uid_t anonu, uid_t anong, struct nfsd4_fs_locations *fslocs);
  499. static int svc_export_show(struct seq_file *m,
  500. struct cache_detail *cd,
  501. struct cache_head *h)
  502. {
  503. struct svc_export *exp ;
  504. if (h ==NULL) {
  505. seq_puts(m, "#path domain(flags)\n");
  506. return 0;
  507. }
  508. exp = container_of(h, struct svc_export, h);
  509. seq_path(m, exp->ex_mnt, exp->ex_dentry, " \t\n\\");
  510. seq_putc(m, '\t');
  511. seq_escape(m, exp->ex_client->name, " \t\n\\");
  512. seq_putc(m, '(');
  513. if (test_bit(CACHE_VALID, &h->flags) &&
  514. !test_bit(CACHE_NEGATIVE, &h->flags))
  515. exp_flags(m, exp->ex_flags, exp->ex_fsid,
  516. exp->ex_anon_uid, exp->ex_anon_gid, &exp->ex_fslocs);
  517. seq_puts(m, ")\n");
  518. return 0;
  519. }
  520. static int svc_export_match(struct cache_head *a, struct cache_head *b)
  521. {
  522. struct svc_export *orig = container_of(a, struct svc_export, h);
  523. struct svc_export *new = container_of(b, struct svc_export, h);
  524. return orig->ex_client == new->ex_client &&
  525. orig->ex_dentry == new->ex_dentry &&
  526. orig->ex_mnt == new->ex_mnt;
  527. }
  528. static void svc_export_init(struct cache_head *cnew, struct cache_head *citem)
  529. {
  530. struct svc_export *new = container_of(cnew, struct svc_export, h);
  531. struct svc_export *item = container_of(citem, struct svc_export, h);
  532. kref_get(&item->ex_client->ref);
  533. new->ex_client = item->ex_client;
  534. new->ex_dentry = dget(item->ex_dentry);
  535. new->ex_mnt = mntget(item->ex_mnt);
  536. new->ex_path = NULL;
  537. new->ex_fslocs.locations = NULL;
  538. new->ex_fslocs.locations_count = 0;
  539. new->ex_fslocs.migrated = 0;
  540. }
  541. static void export_update(struct cache_head *cnew, struct cache_head *citem)
  542. {
  543. struct svc_export *new = container_of(cnew, struct svc_export, h);
  544. struct svc_export *item = container_of(citem, struct svc_export, h);
  545. new->ex_flags = item->ex_flags;
  546. new->ex_anon_uid = item->ex_anon_uid;
  547. new->ex_anon_gid = item->ex_anon_gid;
  548. new->ex_fsid = item->ex_fsid;
  549. new->ex_path = item->ex_path;
  550. item->ex_path = NULL;
  551. new->ex_fslocs.locations = item->ex_fslocs.locations;
  552. item->ex_fslocs.locations = NULL;
  553. new->ex_fslocs.locations_count = item->ex_fslocs.locations_count;
  554. item->ex_fslocs.locations_count = 0;
  555. new->ex_fslocs.migrated = item->ex_fslocs.migrated;
  556. item->ex_fslocs.migrated = 0;
  557. }
  558. static struct cache_head *svc_export_alloc(void)
  559. {
  560. struct svc_export *i = kmalloc(sizeof(*i), GFP_KERNEL);
  561. if (i)
  562. return &i->h;
  563. else
  564. return NULL;
  565. }
  566. struct cache_detail svc_export_cache = {
  567. .owner = THIS_MODULE,
  568. .hash_size = EXPORT_HASHMAX,
  569. .hash_table = export_table,
  570. .name = "nfsd.export",
  571. .cache_put = svc_export_put,
  572. .cache_request = svc_export_request,
  573. .cache_parse = svc_export_parse,
  574. .cache_show = svc_export_show,
  575. .match = svc_export_match,
  576. .init = svc_export_init,
  577. .update = export_update,
  578. .alloc = svc_export_alloc,
  579. };
  580. static struct svc_export *
  581. svc_export_lookup(struct svc_export *exp)
  582. {
  583. struct cache_head *ch;
  584. int hash;
  585. hash = hash_ptr(exp->ex_client, EXPORT_HASHBITS);
  586. hash ^= hash_ptr(exp->ex_dentry, EXPORT_HASHBITS);
  587. hash ^= hash_ptr(exp->ex_mnt, EXPORT_HASHBITS);
  588. ch = sunrpc_cache_lookup(&svc_export_cache, &exp->h,
  589. hash);
  590. if (ch)
  591. return container_of(ch, struct svc_export, h);
  592. else
  593. return NULL;
  594. }
  595. static struct svc_export *
  596. svc_export_update(struct svc_export *new, struct svc_export *old)
  597. {
  598. struct cache_head *ch;
  599. int hash;
  600. hash = hash_ptr(old->ex_client, EXPORT_HASHBITS);
  601. hash ^= hash_ptr(old->ex_dentry, EXPORT_HASHBITS);
  602. hash ^= hash_ptr(old->ex_mnt, EXPORT_HASHBITS);
  603. ch = sunrpc_cache_update(&svc_export_cache, &new->h,
  604. &old->h,
  605. hash);
  606. if (ch)
  607. return container_of(ch, struct svc_export, h);
  608. else
  609. return NULL;
  610. }
  611. static struct svc_expkey *
  612. exp_find_key(svc_client *clp, int fsid_type, u32 *fsidv, struct cache_req *reqp)
  613. {
  614. struct svc_expkey key, *ek;
  615. int err;
  616. if (!clp)
  617. return NULL;
  618. key.ek_client = clp;
  619. key.ek_fsidtype = fsid_type;
  620. memcpy(key.ek_fsid, fsidv, key_len(fsid_type));
  621. ek = svc_expkey_lookup(&key);
  622. if (ek != NULL)
  623. if ((err = cache_check(&svc_expkey_cache, &ek->h, reqp)))
  624. ek = ERR_PTR(err);
  625. return ek;
  626. }
  627. static int exp_set_key(svc_client *clp, int fsid_type, u32 *fsidv,
  628. struct svc_export *exp)
  629. {
  630. struct svc_expkey key, *ek;
  631. key.ek_client = clp;
  632. key.ek_fsidtype = fsid_type;
  633. memcpy(key.ek_fsid, fsidv, key_len(fsid_type));
  634. key.ek_mnt = exp->ex_mnt;
  635. key.ek_dentry = exp->ex_dentry;
  636. key.h.expiry_time = NEVER;
  637. key.h.flags = 0;
  638. ek = svc_expkey_lookup(&key);
  639. if (ek)
  640. ek = svc_expkey_update(&key,ek);
  641. if (ek) {
  642. cache_put(&ek->h, &svc_expkey_cache);
  643. return 0;
  644. }
  645. return -ENOMEM;
  646. }
  647. /*
  648. * Find the client's export entry matching xdev/xino.
  649. */
  650. static inline struct svc_expkey *
  651. exp_get_key(svc_client *clp, dev_t dev, ino_t ino)
  652. {
  653. u32 fsidv[3];
  654. if (old_valid_dev(dev)) {
  655. mk_fsid_v0(fsidv, dev, ino);
  656. return exp_find_key(clp, 0, fsidv, NULL);
  657. }
  658. mk_fsid_v3(fsidv, dev, ino);
  659. return exp_find_key(clp, 3, fsidv, NULL);
  660. }
  661. /*
  662. * Find the client's export entry matching fsid
  663. */
  664. static inline struct svc_expkey *
  665. exp_get_fsid_key(svc_client *clp, int fsid)
  666. {
  667. u32 fsidv[2];
  668. mk_fsid_v1(fsidv, fsid);
  669. return exp_find_key(clp, 1, fsidv, NULL);
  670. }
  671. svc_export *
  672. exp_get_by_name(svc_client *clp, struct vfsmount *mnt, struct dentry *dentry,
  673. struct cache_req *reqp)
  674. {
  675. struct svc_export *exp, key;
  676. if (!clp)
  677. return NULL;
  678. key.ex_client = clp;
  679. key.ex_mnt = mnt;
  680. key.ex_dentry = dentry;
  681. exp = svc_export_lookup(&key);
  682. if (exp != NULL) {
  683. int err;
  684. err = cache_check(&svc_export_cache, &exp->h, reqp);
  685. switch (err) {
  686. case 0: break;
  687. case -EAGAIN:
  688. case -ETIMEDOUT:
  689. exp = ERR_PTR(err);
  690. break;
  691. default:
  692. exp = NULL;
  693. }
  694. }
  695. return exp;
  696. }
  697. /*
  698. * Find the export entry for a given dentry.
  699. */
  700. struct svc_export *
  701. exp_parent(svc_client *clp, struct vfsmount *mnt, struct dentry *dentry,
  702. struct cache_req *reqp)
  703. {
  704. svc_export *exp;
  705. dget(dentry);
  706. exp = exp_get_by_name(clp, mnt, dentry, reqp);
  707. while (exp == NULL && !IS_ROOT(dentry)) {
  708. struct dentry *parent;
  709. parent = dget_parent(dentry);
  710. dput(dentry);
  711. dentry = parent;
  712. exp = exp_get_by_name(clp, mnt, dentry, reqp);
  713. }
  714. dput(dentry);
  715. return exp;
  716. }
  717. /*
  718. * Hashtable locking. Write locks are placed only by user processes
  719. * wanting to modify export information.
  720. * Write locking only done in this file. Read locking
  721. * needed externally.
  722. */
  723. static DECLARE_RWSEM(hash_sem);
  724. void
  725. exp_readlock(void)
  726. {
  727. down_read(&hash_sem);
  728. }
  729. static inline void
  730. exp_writelock(void)
  731. {
  732. down_write(&hash_sem);
  733. }
  734. void
  735. exp_readunlock(void)
  736. {
  737. up_read(&hash_sem);
  738. }
  739. static inline void
  740. exp_writeunlock(void)
  741. {
  742. up_write(&hash_sem);
  743. }
  744. static void exp_fsid_unhash(struct svc_export *exp)
  745. {
  746. struct svc_expkey *ek;
  747. if ((exp->ex_flags & NFSEXP_FSID) == 0)
  748. return;
  749. ek = exp_get_fsid_key(exp->ex_client, exp->ex_fsid);
  750. if (ek && !IS_ERR(ek)) {
  751. ek->h.expiry_time = get_seconds()-1;
  752. cache_put(&ek->h, &svc_expkey_cache);
  753. }
  754. svc_expkey_cache.nextcheck = get_seconds();
  755. }
  756. static int exp_fsid_hash(svc_client *clp, struct svc_export *exp)
  757. {
  758. u32 fsid[2];
  759. if ((exp->ex_flags & NFSEXP_FSID) == 0)
  760. return 0;
  761. mk_fsid_v1(fsid, exp->ex_fsid);
  762. return exp_set_key(clp, 1, fsid, exp);
  763. }
  764. static int exp_hash(struct auth_domain *clp, struct svc_export *exp)
  765. {
  766. u32 fsid[2];
  767. struct inode *inode = exp->ex_dentry->d_inode;
  768. dev_t dev = inode->i_sb->s_dev;
  769. if (old_valid_dev(dev)) {
  770. mk_fsid_v0(fsid, dev, inode->i_ino);
  771. return exp_set_key(clp, 0, fsid, exp);
  772. }
  773. mk_fsid_v3(fsid, dev, inode->i_ino);
  774. return exp_set_key(clp, 3, fsid, exp);
  775. }
  776. static void exp_unhash(struct svc_export *exp)
  777. {
  778. struct svc_expkey *ek;
  779. struct inode *inode = exp->ex_dentry->d_inode;
  780. ek = exp_get_key(exp->ex_client, inode->i_sb->s_dev, inode->i_ino);
  781. if (ek && !IS_ERR(ek)) {
  782. ek->h.expiry_time = get_seconds()-1;
  783. cache_put(&ek->h, &svc_expkey_cache);
  784. }
  785. svc_expkey_cache.nextcheck = get_seconds();
  786. }
  787. /*
  788. * Export a file system.
  789. */
  790. int
  791. exp_export(struct nfsctl_export *nxp)
  792. {
  793. svc_client *clp;
  794. struct svc_export *exp = NULL;
  795. struct svc_export new;
  796. struct svc_expkey *fsid_key = NULL;
  797. struct nameidata nd;
  798. int err;
  799. /* Consistency check */
  800. err = -EINVAL;
  801. if (!exp_verify_string(nxp->ex_path, NFS_MAXPATHLEN) ||
  802. !exp_verify_string(nxp->ex_client, NFSCLNT_IDMAX))
  803. goto out;
  804. dprintk("exp_export called for %s:%s (%x/%ld fl %x).\n",
  805. nxp->ex_client, nxp->ex_path,
  806. (unsigned)nxp->ex_dev, (long)nxp->ex_ino,
  807. nxp->ex_flags);
  808. /* Try to lock the export table for update */
  809. exp_writelock();
  810. /* Look up client info */
  811. if (!(clp = auth_domain_find(nxp->ex_client)))
  812. goto out_unlock;
  813. /* Look up the dentry */
  814. err = path_lookup(nxp->ex_path, 0, &nd);
  815. if (err)
  816. goto out_unlock;
  817. err = -EINVAL;
  818. exp = exp_get_by_name(clp, nd.mnt, nd.dentry, NULL);
  819. memset(&new, 0, sizeof(new));
  820. /* must make sure there won't be an ex_fsid clash */
  821. if ((nxp->ex_flags & NFSEXP_FSID) &&
  822. (fsid_key = exp_get_fsid_key(clp, nxp->ex_dev)) &&
  823. !IS_ERR(fsid_key) &&
  824. fsid_key->ek_mnt &&
  825. (fsid_key->ek_mnt != nd.mnt || fsid_key->ek_dentry != nd.dentry) )
  826. goto finish;
  827. if (exp) {
  828. /* just a flags/id/fsid update */
  829. exp_fsid_unhash(exp);
  830. exp->ex_flags = nxp->ex_flags;
  831. exp->ex_anon_uid = nxp->ex_anon_uid;
  832. exp->ex_anon_gid = nxp->ex_anon_gid;
  833. exp->ex_fsid = nxp->ex_dev;
  834. err = exp_fsid_hash(clp, exp);
  835. goto finish;
  836. }
  837. err = check_export(nd.dentry->d_inode, nxp->ex_flags);
  838. if (err) goto finish;
  839. err = -ENOMEM;
  840. dprintk("nfsd: creating export entry %p for client %p\n", exp, clp);
  841. new.h.expiry_time = NEVER;
  842. new.h.flags = 0;
  843. new.ex_path = kstrdup(nxp->ex_path, GFP_KERNEL);
  844. if (!new.ex_path)
  845. goto finish;
  846. new.ex_client = clp;
  847. new.ex_mnt = nd.mnt;
  848. new.ex_dentry = nd.dentry;
  849. new.ex_flags = nxp->ex_flags;
  850. new.ex_anon_uid = nxp->ex_anon_uid;
  851. new.ex_anon_gid = nxp->ex_anon_gid;
  852. new.ex_fsid = nxp->ex_dev;
  853. exp = svc_export_lookup(&new);
  854. if (exp)
  855. exp = svc_export_update(&new, exp);
  856. if (!exp)
  857. goto finish;
  858. if (exp_hash(clp, exp) ||
  859. exp_fsid_hash(clp, exp)) {
  860. /* failed to create at least one index */
  861. exp_do_unexport(exp);
  862. cache_flush();
  863. } else
  864. err = 0;
  865. finish:
  866. if (new.ex_path)
  867. kfree(new.ex_path);
  868. if (exp)
  869. exp_put(exp);
  870. if (fsid_key && !IS_ERR(fsid_key))
  871. cache_put(&fsid_key->h, &svc_expkey_cache);
  872. if (clp)
  873. auth_domain_put(clp);
  874. path_release(&nd);
  875. out_unlock:
  876. exp_writeunlock();
  877. out:
  878. return err;
  879. }
  880. /*
  881. * Unexport a file system. The export entry has already
  882. * been removed from the client's list of exported fs's.
  883. */
  884. static void
  885. exp_do_unexport(svc_export *unexp)
  886. {
  887. unexp->h.expiry_time = get_seconds()-1;
  888. svc_export_cache.nextcheck = get_seconds();
  889. exp_unhash(unexp);
  890. exp_fsid_unhash(unexp);
  891. }
  892. /*
  893. * unexport syscall.
  894. */
  895. int
  896. exp_unexport(struct nfsctl_export *nxp)
  897. {
  898. struct auth_domain *dom;
  899. svc_export *exp;
  900. struct nameidata nd;
  901. int err;
  902. /* Consistency check */
  903. if (!exp_verify_string(nxp->ex_path, NFS_MAXPATHLEN) ||
  904. !exp_verify_string(nxp->ex_client, NFSCLNT_IDMAX))
  905. return -EINVAL;
  906. exp_writelock();
  907. err = -EINVAL;
  908. dom = auth_domain_find(nxp->ex_client);
  909. if (!dom) {
  910. dprintk("nfsd: unexport couldn't find %s\n", nxp->ex_client);
  911. goto out_unlock;
  912. }
  913. err = path_lookup(nxp->ex_path, 0, &nd);
  914. if (err)
  915. goto out_domain;
  916. err = -EINVAL;
  917. exp = exp_get_by_name(dom, nd.mnt, nd.dentry, NULL);
  918. path_release(&nd);
  919. if (!exp)
  920. goto out_domain;
  921. exp_do_unexport(exp);
  922. exp_put(exp);
  923. err = 0;
  924. out_domain:
  925. auth_domain_put(dom);
  926. cache_flush();
  927. out_unlock:
  928. exp_writeunlock();
  929. return err;
  930. }
  931. /*
  932. * Obtain the root fh on behalf of a client.
  933. * This could be done in user space, but I feel that it adds some safety
  934. * since its harder to fool a kernel module than a user space program.
  935. */
  936. int
  937. exp_rootfh(svc_client *clp, char *path, struct knfsd_fh *f, int maxsize)
  938. {
  939. struct svc_export *exp;
  940. struct nameidata nd;
  941. struct inode *inode;
  942. struct svc_fh fh;
  943. int err;
  944. err = -EPERM;
  945. /* NB: we probably ought to check that it's NUL-terminated */
  946. if (path_lookup(path, 0, &nd)) {
  947. printk("nfsd: exp_rootfh path not found %s", path);
  948. return err;
  949. }
  950. inode = nd.dentry->d_inode;
  951. dprintk("nfsd: exp_rootfh(%s [%p] %s:%s/%ld)\n",
  952. path, nd.dentry, clp->name,
  953. inode->i_sb->s_id, inode->i_ino);
  954. exp = exp_parent(clp, nd.mnt, nd.dentry, NULL);
  955. if (IS_ERR(exp)) {
  956. err = PTR_ERR(exp);
  957. goto out;
  958. }
  959. if (!exp) {
  960. dprintk("nfsd: exp_rootfh export not found.\n");
  961. goto out;
  962. }
  963. /*
  964. * fh must be initialized before calling fh_compose
  965. */
  966. fh_init(&fh, maxsize);
  967. if (fh_compose(&fh, exp, nd.dentry, NULL))
  968. err = -EINVAL;
  969. else
  970. err = 0;
  971. memcpy(f, &fh.fh_handle, sizeof(struct knfsd_fh));
  972. fh_put(&fh);
  973. exp_put(exp);
  974. out:
  975. path_release(&nd);
  976. return err;
  977. }
  978. struct svc_export *
  979. exp_find(struct auth_domain *clp, int fsid_type, u32 *fsidv,
  980. struct cache_req *reqp)
  981. {
  982. struct svc_export *exp;
  983. struct svc_expkey *ek = exp_find_key(clp, fsid_type, fsidv, reqp);
  984. if (!ek || IS_ERR(ek))
  985. return ERR_PTR(PTR_ERR(ek));
  986. exp = exp_get_by_name(clp, ek->ek_mnt, ek->ek_dentry, reqp);
  987. cache_put(&ek->h, &svc_expkey_cache);
  988. if (!exp || IS_ERR(exp))
  989. return ERR_PTR(PTR_ERR(exp));
  990. return exp;
  991. }
  992. /*
  993. * Called when we need the filehandle for the root of the pseudofs,
  994. * for a given NFSv4 client. The root is defined to be the
  995. * export point with fsid==0
  996. */
  997. __be32
  998. exp_pseudoroot(struct auth_domain *clp, struct svc_fh *fhp,
  999. struct cache_req *creq)
  1000. {
  1001. struct svc_export *exp;
  1002. __be32 rv;
  1003. u32 fsidv[2];
  1004. mk_fsid_v1(fsidv, 0);
  1005. exp = exp_find(clp, 1, fsidv, creq);
  1006. if (IS_ERR(exp))
  1007. return nfserrno(PTR_ERR(exp));
  1008. if (exp == NULL)
  1009. return nfserr_perm;
  1010. rv = fh_compose(fhp, exp, exp->ex_dentry, NULL);
  1011. exp_put(exp);
  1012. return rv;
  1013. }
  1014. /* Iterator */
  1015. static void *e_start(struct seq_file *m, loff_t *pos)
  1016. __acquires(svc_export_cache.hash_lock)
  1017. {
  1018. loff_t n = *pos;
  1019. unsigned hash, export;
  1020. struct cache_head *ch;
  1021. exp_readlock();
  1022. read_lock(&svc_export_cache.hash_lock);
  1023. if (!n--)
  1024. return SEQ_START_TOKEN;
  1025. hash = n >> 32;
  1026. export = n & ((1LL<<32) - 1);
  1027. for (ch=export_table[hash]; ch; ch=ch->next)
  1028. if (!export--)
  1029. return ch;
  1030. n &= ~((1LL<<32) - 1);
  1031. do {
  1032. hash++;
  1033. n += 1LL<<32;
  1034. } while(hash < EXPORT_HASHMAX && export_table[hash]==NULL);
  1035. if (hash >= EXPORT_HASHMAX)
  1036. return NULL;
  1037. *pos = n+1;
  1038. return export_table[hash];
  1039. }
  1040. static void *e_next(struct seq_file *m, void *p, loff_t *pos)
  1041. {
  1042. struct cache_head *ch = p;
  1043. int hash = (*pos >> 32);
  1044. if (p == SEQ_START_TOKEN)
  1045. hash = 0;
  1046. else if (ch->next == NULL) {
  1047. hash++;
  1048. *pos += 1LL<<32;
  1049. } else {
  1050. ++*pos;
  1051. return ch->next;
  1052. }
  1053. *pos &= ~((1LL<<32) - 1);
  1054. while (hash < EXPORT_HASHMAX && export_table[hash] == NULL) {
  1055. hash++;
  1056. *pos += 1LL<<32;
  1057. }
  1058. if (hash >= EXPORT_HASHMAX)
  1059. return NULL;
  1060. ++*pos;
  1061. return export_table[hash];
  1062. }
  1063. static void e_stop(struct seq_file *m, void *p)
  1064. __releases(svc_export_cache.hash_lock)
  1065. {
  1066. read_unlock(&svc_export_cache.hash_lock);
  1067. exp_readunlock();
  1068. }
  1069. static struct flags {
  1070. int flag;
  1071. char *name[2];
  1072. } expflags[] = {
  1073. { NFSEXP_READONLY, {"ro", "rw"}},
  1074. { NFSEXP_INSECURE_PORT, {"insecure", ""}},
  1075. { NFSEXP_ROOTSQUASH, {"root_squash", "no_root_squash"}},
  1076. { NFSEXP_ALLSQUASH, {"all_squash", ""}},
  1077. { NFSEXP_ASYNC, {"async", "sync"}},
  1078. { NFSEXP_GATHERED_WRITES, {"wdelay", "no_wdelay"}},
  1079. { NFSEXP_NOHIDE, {"nohide", ""}},
  1080. { NFSEXP_CROSSMOUNT, {"crossmnt", ""}},
  1081. { NFSEXP_NOSUBTREECHECK, {"no_subtree_check", ""}},
  1082. { NFSEXP_NOAUTHNLM, {"insecure_locks", ""}},
  1083. #ifdef MSNFS
  1084. { NFSEXP_MSNFS, {"msnfs", ""}},
  1085. #endif
  1086. { 0, {"", ""}}
  1087. };
  1088. static void exp_flags(struct seq_file *m, int flag, int fsid,
  1089. uid_t anonu, uid_t anong, struct nfsd4_fs_locations *fsloc)
  1090. {
  1091. int first = 0;
  1092. struct flags *flg;
  1093. for (flg = expflags; flg->flag; flg++) {
  1094. int state = (flg->flag & flag)?0:1;
  1095. if (*flg->name[state])
  1096. seq_printf(m, "%s%s", first++?",":"", flg->name[state]);
  1097. }
  1098. if (flag & NFSEXP_FSID)
  1099. seq_printf(m, "%sfsid=%d", first++?",":"", fsid);
  1100. if (anonu != (uid_t)-2 && anonu != (0x10000-2))
  1101. seq_printf(m, "%sanonuid=%d", first++?",":"", anonu);
  1102. if (anong != (gid_t)-2 && anong != (0x10000-2))
  1103. seq_printf(m, "%sanongid=%d", first++?",":"", anong);
  1104. if (fsloc && fsloc->locations_count > 0) {
  1105. char *loctype = (fsloc->migrated) ? "refer" : "replicas";
  1106. int i;
  1107. seq_printf(m, "%s%s=", first++?",":"", loctype);
  1108. seq_escape(m, fsloc->locations[0].path, ",;@ \t\n\\");
  1109. seq_putc(m, '@');
  1110. seq_escape(m, fsloc->locations[0].hosts, ",;@ \t\n\\");
  1111. for (i = 1; i < fsloc->locations_count; i++) {
  1112. seq_putc(m, ';');
  1113. seq_escape(m, fsloc->locations[i].path, ",;@ \t\n\\");
  1114. seq_putc(m, '@');
  1115. seq_escape(m, fsloc->locations[i].hosts, ",;@ \t\n\\");
  1116. }
  1117. }
  1118. }
  1119. static int e_show(struct seq_file *m, void *p)
  1120. {
  1121. struct cache_head *cp = p;
  1122. struct svc_export *exp = container_of(cp, struct svc_export, h);
  1123. if (p == SEQ_START_TOKEN) {
  1124. seq_puts(m, "# Version 1.1\n");
  1125. seq_puts(m, "# Path Client(Flags) # IPs\n");
  1126. return 0;
  1127. }
  1128. cache_get(&exp->h);
  1129. if (cache_check(&svc_export_cache, &exp->h, NULL))
  1130. return 0;
  1131. cache_put(&exp->h, &svc_export_cache);
  1132. return svc_export_show(m, &svc_export_cache, cp);
  1133. }
  1134. struct seq_operations nfs_exports_op = {
  1135. .start = e_start,
  1136. .next = e_next,
  1137. .stop = e_stop,
  1138. .show = e_show,
  1139. };
  1140. /*
  1141. * Add or modify a client.
  1142. * Change requests may involve the list of host addresses. The list of
  1143. * exports and possibly existing uid maps are left untouched.
  1144. */
  1145. int
  1146. exp_addclient(struct nfsctl_client *ncp)
  1147. {
  1148. struct auth_domain *dom;
  1149. int i, err;
  1150. /* First, consistency check. */
  1151. err = -EINVAL;
  1152. if (! exp_verify_string(ncp->cl_ident, NFSCLNT_IDMAX))
  1153. goto out;
  1154. if (ncp->cl_naddr > NFSCLNT_ADDRMAX)
  1155. goto out;
  1156. /* Lock the hashtable */
  1157. exp_writelock();
  1158. dom = unix_domain_find(ncp->cl_ident);
  1159. err = -ENOMEM;
  1160. if (!dom)
  1161. goto out_unlock;
  1162. /* Insert client into hashtable. */
  1163. for (i = 0; i < ncp->cl_naddr; i++)
  1164. auth_unix_add_addr(ncp->cl_addrlist[i], dom);
  1165. auth_unix_forget_old(dom);
  1166. auth_domain_put(dom);
  1167. err = 0;
  1168. out_unlock:
  1169. exp_writeunlock();
  1170. out:
  1171. return err;
  1172. }
  1173. /*
  1174. * Delete a client given an identifier.
  1175. */
  1176. int
  1177. exp_delclient(struct nfsctl_client *ncp)
  1178. {
  1179. int err;
  1180. struct auth_domain *dom;
  1181. err = -EINVAL;
  1182. if (!exp_verify_string(ncp->cl_ident, NFSCLNT_IDMAX))
  1183. goto out;
  1184. /* Lock the hashtable */
  1185. exp_writelock();
  1186. dom = auth_domain_find(ncp->cl_ident);
  1187. /* just make sure that no addresses work
  1188. * and that it will expire soon
  1189. */
  1190. if (dom) {
  1191. err = auth_unix_forget_old(dom);
  1192. auth_domain_put(dom);
  1193. }
  1194. exp_writeunlock();
  1195. out:
  1196. return err;
  1197. }
  1198. /*
  1199. * Verify that string is non-empty and does not exceed max length.
  1200. */
  1201. static int
  1202. exp_verify_string(char *cp, int max)
  1203. {
  1204. int i;
  1205. for (i = 0; i < max; i++)
  1206. if (!cp[i])
  1207. return i;
  1208. cp[i] = 0;
  1209. printk(KERN_NOTICE "nfsd: couldn't validate string %s\n", cp);
  1210. return 0;
  1211. }
  1212. /*
  1213. * Initialize the exports module.
  1214. */
  1215. void
  1216. nfsd_export_init(void)
  1217. {
  1218. dprintk("nfsd: initializing export module.\n");
  1219. cache_register(&svc_export_cache);
  1220. cache_register(&svc_expkey_cache);
  1221. }
  1222. /*
  1223. * Flush exports table - called when last nfsd thread is killed
  1224. */
  1225. void
  1226. nfsd_export_flush(void)
  1227. {
  1228. exp_writelock();
  1229. cache_purge(&svc_expkey_cache);
  1230. cache_purge(&svc_export_cache);
  1231. exp_writeunlock();
  1232. }
  1233. /*
  1234. * Shutdown the exports module.
  1235. */
  1236. void
  1237. nfsd_export_shutdown(void)
  1238. {
  1239. dprintk("nfsd: shutting down export module.\n");
  1240. exp_writelock();
  1241. if (cache_unregister(&svc_expkey_cache))
  1242. printk(KERN_ERR "nfsd: failed to unregister expkey cache\n");
  1243. if (cache_unregister(&svc_export_cache))
  1244. printk(KERN_ERR "nfsd: failed to unregister export cache\n");
  1245. svcauth_unix_purge();
  1246. exp_writeunlock();
  1247. dprintk("nfsd: export shutdown complete.\n");
  1248. }