export.c 28 KB

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