export.c 38 KB

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