export.c 27 KB

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