export.c 39 KB

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