export.c 38 KB

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