export.c 39 KB

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