export.c 39 KB

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