export.c 39 KB

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