export.c 33 KB

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