export.c 34 KB

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