export.c 39 KB

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