export.c 39 KB

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