export.c 38 KB

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