export.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NFS exporting and validation.
  4. *
  5. * We maintain a list of clients, each of which has a list of
  6. * exports. To export an fs to a given client, you first have
  7. * to create the client entry with NFSCTL_ADDCLIENT, which
  8. * creates a client control block and adds it to the hash
  9. * table. Then, you call NFSCTL_EXPORT for each fs.
  10. *
  11. *
  12. * Copyright (C) 1995, 1996 Olaf Kirch, <okir@monad.swb.de>
  13. */
  14. #include <linux/slab.h>
  15. #include <linux/namei.h>
  16. #include <linux/module.h>
  17. #include <linux/exportfs.h>
  18. #include <linux/sunrpc/svc_xprt.h>
  19. #include "nfsd.h"
  20. #include "nfsfh.h"
  21. #include "netns.h"
  22. #include "pnfs.h"
  23. #include "filecache.h"
  24. #include "trace.h"
  25. #define NFSDDBG_FACILITY NFSDDBG_EXPORT
  26. /*
  27. * We have two caches.
  28. * One maps client+vfsmnt+dentry to export options - the export map
  29. * The other maps client+filehandle-fragment to export options. - the expkey map
  30. *
  31. * The export options are actually stored in the first map, and the
  32. * second map contains a reference to the entry in the first map.
  33. */
  34. #define EXPKEY_HASHBITS 8
  35. #define EXPKEY_HASHMAX (1 << EXPKEY_HASHBITS)
  36. #define EXPKEY_HASHMASK (EXPKEY_HASHMAX -1)
  37. static void expkey_put(struct kref *ref)
  38. {
  39. struct svc_expkey *key = container_of(ref, struct svc_expkey, h.ref);
  40. if (test_bit(CACHE_VALID, &key->h.flags) &&
  41. !test_bit(CACHE_NEGATIVE, &key->h.flags))
  42. path_put(&key->ek_path);
  43. auth_domain_put(key->ek_client);
  44. kfree_rcu(key, ek_rcu);
  45. }
  46. static int expkey_upcall(struct cache_detail *cd, struct cache_head *h)
  47. {
  48. return sunrpc_cache_pipe_upcall(cd, h);
  49. }
  50. static void expkey_request(struct cache_detail *cd,
  51. struct cache_head *h,
  52. char **bpp, int *blen)
  53. {
  54. /* client fsidtype \xfsid */
  55. struct svc_expkey *ek = container_of(h, struct svc_expkey, h);
  56. char type[5];
  57. qword_add(bpp, blen, ek->ek_client->name);
  58. snprintf(type, 5, "%d", ek->ek_fsidtype);
  59. qword_add(bpp, blen, type);
  60. qword_addhex(bpp, blen, (char*)ek->ek_fsid, key_len(ek->ek_fsidtype));
  61. (*bpp)[-1] = '\n';
  62. }
  63. static struct svc_expkey *svc_expkey_update(struct cache_detail *cd, struct svc_expkey *new,
  64. struct svc_expkey *old);
  65. static struct svc_expkey *svc_expkey_lookup(struct cache_detail *cd, struct svc_expkey *);
  66. static int expkey_parse(struct cache_detail *cd, char *mesg, int mlen)
  67. {
  68. /* client fsidtype fsid expiry [path] */
  69. char *buf;
  70. int len;
  71. struct auth_domain *dom = NULL;
  72. int err;
  73. int fsidtype;
  74. char *ep;
  75. struct svc_expkey key;
  76. struct svc_expkey *ek = NULL;
  77. if (mesg[mlen - 1] != '\n')
  78. return -EINVAL;
  79. mesg[mlen-1] = 0;
  80. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  81. err = -ENOMEM;
  82. if (!buf)
  83. goto out;
  84. err = -EINVAL;
  85. if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
  86. goto out;
  87. err = -ENOENT;
  88. dom = auth_domain_find(buf);
  89. if (!dom)
  90. goto out;
  91. dprintk("found domain %s\n", buf);
  92. err = -EINVAL;
  93. if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
  94. goto out;
  95. fsidtype = simple_strtoul(buf, &ep, 10);
  96. if (*ep)
  97. goto out;
  98. dprintk("found fsidtype %d\n", fsidtype);
  99. if (key_len(fsidtype)==0) /* invalid type */
  100. goto out;
  101. if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
  102. goto out;
  103. dprintk("found fsid length %d\n", len);
  104. if (len != key_len(fsidtype))
  105. goto out;
  106. /* OK, we seem to have a valid key */
  107. key.h.flags = 0;
  108. key.h.expiry_time = get_expiry(&mesg);
  109. if (key.h.expiry_time == 0)
  110. goto out;
  111. key.ek_client = dom;
  112. key.ek_fsidtype = fsidtype;
  113. memcpy(key.ek_fsid, buf, len);
  114. ek = svc_expkey_lookup(cd, &key);
  115. err = -ENOMEM;
  116. if (!ek)
  117. goto out;
  118. /* now we want a pathname, or empty meaning NEGATIVE */
  119. err = -EINVAL;
  120. len = qword_get(&mesg, buf, PAGE_SIZE);
  121. if (len < 0)
  122. goto out;
  123. dprintk("Path seems to be <%s>\n", buf);
  124. err = 0;
  125. if (len == 0) {
  126. set_bit(CACHE_NEGATIVE, &key.h.flags);
  127. ek = svc_expkey_update(cd, &key, ek);
  128. if (ek)
  129. trace_nfsd_expkey_update(ek, NULL);
  130. else
  131. err = -ENOMEM;
  132. } else {
  133. err = kern_path(buf, 0, &key.ek_path);
  134. if (err)
  135. goto out;
  136. dprintk("Found the path %s\n", buf);
  137. ek = svc_expkey_update(cd, &key, ek);
  138. if (ek)
  139. trace_nfsd_expkey_update(ek, buf);
  140. else
  141. err = -ENOMEM;
  142. path_put(&key.ek_path);
  143. }
  144. cache_flush();
  145. out:
  146. if (ek)
  147. cache_put(&ek->h, cd);
  148. if (dom)
  149. auth_domain_put(dom);
  150. kfree(buf);
  151. return err;
  152. }
  153. static int expkey_show(struct seq_file *m,
  154. struct cache_detail *cd,
  155. struct cache_head *h)
  156. {
  157. struct svc_expkey *ek ;
  158. int i;
  159. if (h ==NULL) {
  160. seq_puts(m, "#domain fsidtype fsid [path]\n");
  161. return 0;
  162. }
  163. ek = container_of(h, struct svc_expkey, h);
  164. seq_printf(m, "%s %d 0x", ek->ek_client->name,
  165. ek->ek_fsidtype);
  166. for (i=0; i < key_len(ek->ek_fsidtype)/4; i++)
  167. seq_printf(m, "%08x", ek->ek_fsid[i]);
  168. if (test_bit(CACHE_VALID, &h->flags) &&
  169. !test_bit(CACHE_NEGATIVE, &h->flags)) {
  170. seq_printf(m, " ");
  171. seq_path(m, &ek->ek_path, "\\ \t\n");
  172. }
  173. seq_printf(m, "\n");
  174. return 0;
  175. }
  176. static inline int expkey_match (struct cache_head *a, struct cache_head *b)
  177. {
  178. struct svc_expkey *orig = container_of(a, struct svc_expkey, h);
  179. struct svc_expkey *new = container_of(b, struct svc_expkey, h);
  180. if (orig->ek_fsidtype != new->ek_fsidtype ||
  181. orig->ek_client != new->ek_client ||
  182. memcmp(orig->ek_fsid, new->ek_fsid, key_len(orig->ek_fsidtype)) != 0)
  183. return 0;
  184. return 1;
  185. }
  186. static inline void expkey_init(struct cache_head *cnew,
  187. struct cache_head *citem)
  188. {
  189. struct svc_expkey *new = container_of(cnew, struct svc_expkey, h);
  190. struct svc_expkey *item = container_of(citem, struct svc_expkey, h);
  191. kref_get(&item->ek_client->ref);
  192. new->ek_client = item->ek_client;
  193. new->ek_fsidtype = item->ek_fsidtype;
  194. memcpy(new->ek_fsid, item->ek_fsid, sizeof(new->ek_fsid));
  195. }
  196. static inline void expkey_update(struct cache_head *cnew,
  197. struct cache_head *citem)
  198. {
  199. struct svc_expkey *new = container_of(cnew, struct svc_expkey, h);
  200. struct svc_expkey *item = container_of(citem, struct svc_expkey, h);
  201. new->ek_path = item->ek_path;
  202. path_get(&item->ek_path);
  203. }
  204. static struct cache_head *expkey_alloc(void)
  205. {
  206. struct svc_expkey *i = kmalloc(sizeof(*i), GFP_KERNEL);
  207. if (i)
  208. return &i->h;
  209. else
  210. return NULL;
  211. }
  212. static void expkey_flush(void)
  213. {
  214. /*
  215. * Take the nfsd_mutex here to ensure that the file cache is not
  216. * destroyed while we're in the middle of flushing.
  217. */
  218. mutex_lock(&nfsd_mutex);
  219. nfsd_file_cache_purge(current->nsproxy->net_ns);
  220. mutex_unlock(&nfsd_mutex);
  221. }
  222. static const struct cache_detail svc_expkey_cache_template = {
  223. .owner = THIS_MODULE,
  224. .hash_size = EXPKEY_HASHMAX,
  225. .name = "nfsd.fh",
  226. .cache_put = expkey_put,
  227. .cache_upcall = expkey_upcall,
  228. .cache_request = expkey_request,
  229. .cache_parse = expkey_parse,
  230. .cache_show = expkey_show,
  231. .match = expkey_match,
  232. .init = expkey_init,
  233. .update = expkey_update,
  234. .alloc = expkey_alloc,
  235. .flush = expkey_flush,
  236. };
  237. static int
  238. svc_expkey_hash(struct svc_expkey *item)
  239. {
  240. int hash = item->ek_fsidtype;
  241. char * cp = (char*)item->ek_fsid;
  242. int len = key_len(item->ek_fsidtype);
  243. hash ^= hash_mem(cp, len, EXPKEY_HASHBITS);
  244. hash ^= hash_ptr(item->ek_client, EXPKEY_HASHBITS);
  245. hash &= EXPKEY_HASHMASK;
  246. return hash;
  247. }
  248. static struct svc_expkey *
  249. svc_expkey_lookup(struct cache_detail *cd, struct svc_expkey *item)
  250. {
  251. struct cache_head *ch;
  252. int hash = svc_expkey_hash(item);
  253. ch = sunrpc_cache_lookup_rcu(cd, &item->h, hash);
  254. if (ch)
  255. return container_of(ch, struct svc_expkey, h);
  256. else
  257. return NULL;
  258. }
  259. static struct svc_expkey *
  260. svc_expkey_update(struct cache_detail *cd, struct svc_expkey *new,
  261. struct svc_expkey *old)
  262. {
  263. struct cache_head *ch;
  264. int hash = svc_expkey_hash(new);
  265. ch = sunrpc_cache_update(cd, &new->h, &old->h, hash);
  266. if (ch)
  267. return container_of(ch, struct svc_expkey, h);
  268. else
  269. return NULL;
  270. }
  271. #define EXPORT_HASHBITS 8
  272. #define EXPORT_HASHMAX (1<< EXPORT_HASHBITS)
  273. static void nfsd4_fslocs_free(struct nfsd4_fs_locations *fsloc)
  274. {
  275. struct nfsd4_fs_location *locations = fsloc->locations;
  276. int i;
  277. if (!locations)
  278. return;
  279. for (i = 0; i < fsloc->locations_count; i++) {
  280. kfree(locations[i].path);
  281. kfree(locations[i].hosts);
  282. }
  283. kfree(locations);
  284. fsloc->locations = NULL;
  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. nfsd4_fslocs_free(&exp->ex_fslocs);
  292. kfree(exp->ex_uuid);
  293. kfree_rcu(exp, ex_rcu);
  294. }
  295. static int svc_export_upcall(struct cache_detail *cd, struct cache_head *h)
  296. {
  297. return sunrpc_cache_pipe_upcall(cd, h);
  298. }
  299. static void svc_export_request(struct cache_detail *cd,
  300. struct cache_head *h,
  301. char **bpp, int *blen)
  302. {
  303. /* client path */
  304. struct svc_export *exp = container_of(h, struct svc_export, h);
  305. char *pth;
  306. qword_add(bpp, blen, exp->ex_client->name);
  307. pth = d_path(&exp->ex_path, *bpp, *blen);
  308. if (IS_ERR(pth)) {
  309. /* is this correct? */
  310. (*bpp)[0] = '\n';
  311. return;
  312. }
  313. qword_add(bpp, blen, pth);
  314. (*bpp)[-1] = '\n';
  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. /*
  322. * We currently export only dirs, regular files, and (for v4
  323. * pseudoroot) symlinks.
  324. */
  325. if (!S_ISDIR(inode->i_mode) &&
  326. !S_ISLNK(inode->i_mode) &&
  327. !S_ISREG(inode->i_mode))
  328. return -ENOTDIR;
  329. /*
  330. * Mountd should never pass down a writeable V4ROOT export, but,
  331. * just to make sure:
  332. */
  333. if (*flags & NFSEXP_V4ROOT)
  334. *flags |= NFSEXP_READONLY;
  335. /* There are two requirements on a filesystem to be exportable.
  336. * 1: We must be able to identify the filesystem from a number.
  337. * either a device number (so FS_REQUIRES_DEV needed)
  338. * or an FSID number (so NFSEXP_FSID or ->uuid is needed).
  339. * 2: We must be able to find an inode from a filehandle.
  340. * This means that s_export_op must be set.
  341. */
  342. if (!(inode->i_sb->s_type->fs_flags & FS_REQUIRES_DEV) &&
  343. !(*flags & NFSEXP_FSID) &&
  344. uuid == NULL) {
  345. dprintk("exp_export: export of non-dev fs without fsid\n");
  346. return -EINVAL;
  347. }
  348. if (!inode->i_sb->s_export_op ||
  349. !inode->i_sb->s_export_op->fh_to_dentry) {
  350. dprintk("exp_export: export of invalid fs type.\n");
  351. return -EINVAL;
  352. }
  353. return 0;
  354. }
  355. #ifdef CONFIG_NFSD_V4
  356. static int
  357. fsloc_parse(char **mesg, char *buf, struct nfsd4_fs_locations *fsloc)
  358. {
  359. int len;
  360. int migrated, i, err;
  361. /* more than one fsloc */
  362. if (fsloc->locations)
  363. return -EINVAL;
  364. /* listsize */
  365. err = get_uint(mesg, &fsloc->locations_count);
  366. if (err)
  367. return err;
  368. if (fsloc->locations_count > MAX_FS_LOCATIONS)
  369. return -EINVAL;
  370. if (fsloc->locations_count == 0)
  371. return 0;
  372. fsloc->locations = kcalloc(fsloc->locations_count,
  373. sizeof(struct nfsd4_fs_location),
  374. GFP_KERNEL);
  375. if (!fsloc->locations)
  376. return -ENOMEM;
  377. for (i=0; i < fsloc->locations_count; i++) {
  378. /* colon separated host list */
  379. err = -EINVAL;
  380. len = qword_get(mesg, buf, PAGE_SIZE);
  381. if (len <= 0)
  382. goto out_free_all;
  383. err = -ENOMEM;
  384. fsloc->locations[i].hosts = kstrdup(buf, GFP_KERNEL);
  385. if (!fsloc->locations[i].hosts)
  386. goto out_free_all;
  387. err = -EINVAL;
  388. /* slash separated path component list */
  389. len = qword_get(mesg, buf, PAGE_SIZE);
  390. if (len <= 0)
  391. goto out_free_all;
  392. err = -ENOMEM;
  393. fsloc->locations[i].path = kstrdup(buf, GFP_KERNEL);
  394. if (!fsloc->locations[i].path)
  395. goto out_free_all;
  396. }
  397. /* migrated */
  398. err = get_int(mesg, &migrated);
  399. if (err)
  400. goto out_free_all;
  401. err = -EINVAL;
  402. if (migrated < 0 || migrated > 1)
  403. goto out_free_all;
  404. fsloc->migrated = migrated;
  405. return 0;
  406. out_free_all:
  407. nfsd4_fslocs_free(fsloc);
  408. return err;
  409. }
  410. static int secinfo_parse(char **mesg, char *buf, struct svc_export *exp)
  411. {
  412. struct exp_flavor_info *f;
  413. u32 listsize;
  414. int err;
  415. /* more than one secinfo */
  416. if (exp->ex_nflavors)
  417. return -EINVAL;
  418. err = get_uint(mesg, &listsize);
  419. if (err)
  420. return err;
  421. if (listsize > MAX_SECINFO_LIST)
  422. return -EINVAL;
  423. for (f = exp->ex_flavors; f < exp->ex_flavors + listsize; f++) {
  424. err = get_uint(mesg, &f->pseudoflavor);
  425. if (err)
  426. return err;
  427. /*
  428. * XXX: It would be nice to also check whether this
  429. * pseudoflavor is supported, so we can discover the
  430. * problem at export time instead of when a client fails
  431. * to authenticate.
  432. */
  433. err = get_uint(mesg, &f->flags);
  434. if (err)
  435. return err;
  436. /* Only some flags are allowed to differ between flavors: */
  437. if (~NFSEXP_SECINFO_FLAGS & (f->flags ^ exp->ex_flags))
  438. return -EINVAL;
  439. }
  440. exp->ex_nflavors = listsize;
  441. return 0;
  442. }
  443. #else /* CONFIG_NFSD_V4 */
  444. static inline int
  445. fsloc_parse(char **mesg, char *buf, struct nfsd4_fs_locations *fsloc){return 0;}
  446. static inline int
  447. secinfo_parse(char **mesg, char *buf, struct svc_export *exp) { return 0; }
  448. #endif
  449. static inline int
  450. nfsd_uuid_parse(char **mesg, char *buf, unsigned char **puuid)
  451. {
  452. int len;
  453. /* more than one uuid */
  454. if (*puuid)
  455. return -EINVAL;
  456. /* expect a 16 byte uuid encoded as \xXXXX... */
  457. len = qword_get(mesg, buf, PAGE_SIZE);
  458. if (len != EX_UUID_LEN)
  459. return -EINVAL;
  460. *puuid = kmemdup(buf, EX_UUID_LEN, GFP_KERNEL);
  461. if (*puuid == NULL)
  462. return -ENOMEM;
  463. return 0;
  464. }
  465. static int svc_export_parse(struct cache_detail *cd, char *mesg, int mlen)
  466. {
  467. /* client path expiry [flags anonuid anongid fsid] */
  468. char *buf;
  469. int len;
  470. int err;
  471. struct auth_domain *dom = NULL;
  472. struct svc_export exp = {}, *expp;
  473. int an_int;
  474. if (mesg[mlen-1] != '\n')
  475. return -EINVAL;
  476. mesg[mlen-1] = 0;
  477. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  478. if (!buf)
  479. return -ENOMEM;
  480. /* client */
  481. err = -EINVAL;
  482. len = qword_get(&mesg, buf, PAGE_SIZE);
  483. if (len <= 0)
  484. goto out;
  485. err = -ENOENT;
  486. dom = auth_domain_find(buf);
  487. if (!dom)
  488. goto out;
  489. /* path */
  490. err = -EINVAL;
  491. if ((len = qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
  492. goto out1;
  493. err = kern_path(buf, 0, &exp.ex_path);
  494. if (err)
  495. goto out1;
  496. exp.ex_client = dom;
  497. exp.cd = cd;
  498. exp.ex_devid_map = NULL;
  499. /* expiry */
  500. err = -EINVAL;
  501. exp.h.expiry_time = get_expiry(&mesg);
  502. if (exp.h.expiry_time == 0)
  503. goto out3;
  504. /* flags */
  505. err = get_int(&mesg, &an_int);
  506. if (err == -ENOENT) {
  507. err = 0;
  508. set_bit(CACHE_NEGATIVE, &exp.h.flags);
  509. } else {
  510. if (err || an_int < 0)
  511. goto out3;
  512. exp.ex_flags= an_int;
  513. /* anon uid */
  514. err = get_int(&mesg, &an_int);
  515. if (err)
  516. goto out3;
  517. exp.ex_anon_uid= make_kuid(current_user_ns(), an_int);
  518. /* anon gid */
  519. err = get_int(&mesg, &an_int);
  520. if (err)
  521. goto out3;
  522. exp.ex_anon_gid= make_kgid(current_user_ns(), an_int);
  523. /* fsid */
  524. err = get_int(&mesg, &an_int);
  525. if (err)
  526. goto out3;
  527. exp.ex_fsid = an_int;
  528. while ((len = qword_get(&mesg, buf, PAGE_SIZE)) > 0) {
  529. if (strcmp(buf, "fsloc") == 0)
  530. err = fsloc_parse(&mesg, buf, &exp.ex_fslocs);
  531. else if (strcmp(buf, "uuid") == 0)
  532. err = nfsd_uuid_parse(&mesg, buf, &exp.ex_uuid);
  533. else if (strcmp(buf, "secinfo") == 0)
  534. err = secinfo_parse(&mesg, buf, &exp);
  535. else
  536. /* quietly ignore unknown words and anything
  537. * following. Newer user-space can try to set
  538. * new values, then see what the result was.
  539. */
  540. break;
  541. if (err)
  542. goto out4;
  543. }
  544. err = check_export(d_inode(exp.ex_path.dentry), &exp.ex_flags,
  545. exp.ex_uuid);
  546. if (err)
  547. goto out4;
  548. /*
  549. * No point caching this if it would immediately expire.
  550. * Also, this protects exportfs's dummy export from the
  551. * anon_uid/anon_gid checks:
  552. */
  553. if (exp.h.expiry_time < seconds_since_boot())
  554. goto out4;
  555. /*
  556. * For some reason exportfs has been passing down an
  557. * invalid (-1) uid & gid on the "dummy" export which it
  558. * uses to test export support. To make sure exportfs
  559. * sees errors from check_export we therefore need to
  560. * delay these checks till after check_export:
  561. */
  562. err = -EINVAL;
  563. if (!uid_valid(exp.ex_anon_uid))
  564. goto out4;
  565. if (!gid_valid(exp.ex_anon_gid))
  566. goto out4;
  567. err = 0;
  568. nfsd4_setup_layout_type(&exp);
  569. }
  570. expp = svc_export_lookup(&exp);
  571. if (!expp) {
  572. err = -ENOMEM;
  573. goto out4;
  574. }
  575. expp = svc_export_update(&exp, expp);
  576. if (expp) {
  577. trace_nfsd_export_update(expp);
  578. cache_flush();
  579. exp_put(expp);
  580. } else
  581. err = -ENOMEM;
  582. out4:
  583. nfsd4_fslocs_free(&exp.ex_fslocs);
  584. kfree(exp.ex_uuid);
  585. out3:
  586. path_put(&exp.ex_path);
  587. out1:
  588. auth_domain_put(dom);
  589. out:
  590. kfree(buf);
  591. return err;
  592. }
  593. static void exp_flags(struct seq_file *m, int flag, int fsid,
  594. kuid_t anonu, kgid_t anong, struct nfsd4_fs_locations *fslocs);
  595. static void show_secinfo(struct seq_file *m, struct svc_export *exp);
  596. static int svc_export_show(struct seq_file *m,
  597. struct cache_detail *cd,
  598. struct cache_head *h)
  599. {
  600. struct svc_export *exp ;
  601. if (h ==NULL) {
  602. seq_puts(m, "#path domain(flags)\n");
  603. return 0;
  604. }
  605. exp = container_of(h, struct svc_export, h);
  606. seq_path(m, &exp->ex_path, " \t\n\\");
  607. seq_putc(m, '\t');
  608. seq_escape(m, exp->ex_client->name, " \t\n\\");
  609. seq_putc(m, '(');
  610. if (test_bit(CACHE_VALID, &h->flags) &&
  611. !test_bit(CACHE_NEGATIVE, &h->flags)) {
  612. exp_flags(m, exp->ex_flags, exp->ex_fsid,
  613. exp->ex_anon_uid, exp->ex_anon_gid, &exp->ex_fslocs);
  614. if (exp->ex_uuid) {
  615. int i;
  616. seq_puts(m, ",uuid=");
  617. for (i = 0; i < EX_UUID_LEN; i++) {
  618. if ((i&3) == 0 && i)
  619. seq_putc(m, ':');
  620. seq_printf(m, "%02x", exp->ex_uuid[i]);
  621. }
  622. }
  623. show_secinfo(m, exp);
  624. }
  625. seq_puts(m, ")\n");
  626. return 0;
  627. }
  628. static int svc_export_match(struct cache_head *a, struct cache_head *b)
  629. {
  630. struct svc_export *orig = container_of(a, struct svc_export, h);
  631. struct svc_export *new = container_of(b, struct svc_export, h);
  632. return orig->ex_client == new->ex_client &&
  633. path_equal(&orig->ex_path, &new->ex_path);
  634. }
  635. static void svc_export_init(struct cache_head *cnew, struct cache_head *citem)
  636. {
  637. struct svc_export *new = container_of(cnew, struct svc_export, h);
  638. struct svc_export *item = container_of(citem, struct svc_export, h);
  639. kref_get(&item->ex_client->ref);
  640. new->ex_client = item->ex_client;
  641. new->ex_path = item->ex_path;
  642. path_get(&item->ex_path);
  643. new->ex_fslocs.locations = NULL;
  644. new->ex_fslocs.locations_count = 0;
  645. new->ex_fslocs.migrated = 0;
  646. new->ex_layout_types = 0;
  647. new->ex_uuid = NULL;
  648. new->cd = item->cd;
  649. }
  650. static void export_update(struct cache_head *cnew, struct cache_head *citem)
  651. {
  652. struct svc_export *new = container_of(cnew, struct svc_export, h);
  653. struct svc_export *item = container_of(citem, struct svc_export, h);
  654. int i;
  655. new->ex_flags = item->ex_flags;
  656. new->ex_anon_uid = item->ex_anon_uid;
  657. new->ex_anon_gid = item->ex_anon_gid;
  658. new->ex_fsid = item->ex_fsid;
  659. new->ex_devid_map = item->ex_devid_map;
  660. item->ex_devid_map = NULL;
  661. new->ex_uuid = item->ex_uuid;
  662. item->ex_uuid = NULL;
  663. new->ex_fslocs.locations = item->ex_fslocs.locations;
  664. item->ex_fslocs.locations = NULL;
  665. new->ex_fslocs.locations_count = item->ex_fslocs.locations_count;
  666. item->ex_fslocs.locations_count = 0;
  667. new->ex_fslocs.migrated = item->ex_fslocs.migrated;
  668. item->ex_fslocs.migrated = 0;
  669. new->ex_layout_types = item->ex_layout_types;
  670. new->ex_nflavors = item->ex_nflavors;
  671. for (i = 0; i < MAX_SECINFO_LIST; i++) {
  672. new->ex_flavors[i] = item->ex_flavors[i];
  673. }
  674. }
  675. static struct cache_head *svc_export_alloc(void)
  676. {
  677. struct svc_export *i = kmalloc(sizeof(*i), GFP_KERNEL);
  678. if (i)
  679. return &i->h;
  680. else
  681. return NULL;
  682. }
  683. static const struct cache_detail svc_export_cache_template = {
  684. .owner = THIS_MODULE,
  685. .hash_size = EXPORT_HASHMAX,
  686. .name = "nfsd.export",
  687. .cache_put = svc_export_put,
  688. .cache_upcall = svc_export_upcall,
  689. .cache_request = svc_export_request,
  690. .cache_parse = svc_export_parse,
  691. .cache_show = svc_export_show,
  692. .match = svc_export_match,
  693. .init = svc_export_init,
  694. .update = export_update,
  695. .alloc = svc_export_alloc,
  696. };
  697. static int
  698. svc_export_hash(struct svc_export *exp)
  699. {
  700. int hash;
  701. hash = hash_ptr(exp->ex_client, EXPORT_HASHBITS);
  702. hash ^= hash_ptr(exp->ex_path.dentry, EXPORT_HASHBITS);
  703. hash ^= hash_ptr(exp->ex_path.mnt, EXPORT_HASHBITS);
  704. return hash;
  705. }
  706. static struct svc_export *
  707. svc_export_lookup(struct svc_export *exp)
  708. {
  709. struct cache_head *ch;
  710. int hash = svc_export_hash(exp);
  711. ch = sunrpc_cache_lookup_rcu(exp->cd, &exp->h, hash);
  712. if (ch)
  713. return container_of(ch, struct svc_export, h);
  714. else
  715. return NULL;
  716. }
  717. static struct svc_export *
  718. svc_export_update(struct svc_export *new, struct svc_export *old)
  719. {
  720. struct cache_head *ch;
  721. int hash = svc_export_hash(old);
  722. ch = sunrpc_cache_update(old->cd, &new->h, &old->h, hash);
  723. if (ch)
  724. return container_of(ch, struct svc_export, h);
  725. else
  726. return NULL;
  727. }
  728. static struct svc_expkey *
  729. exp_find_key(struct cache_detail *cd, struct auth_domain *clp, int fsid_type,
  730. u32 *fsidv, struct cache_req *reqp)
  731. {
  732. struct svc_expkey key, *ek;
  733. int err;
  734. if (!clp)
  735. return ERR_PTR(-ENOENT);
  736. key.ek_client = clp;
  737. key.ek_fsidtype = fsid_type;
  738. memcpy(key.ek_fsid, fsidv, key_len(fsid_type));
  739. ek = svc_expkey_lookup(cd, &key);
  740. if (ek == NULL)
  741. return ERR_PTR(-ENOMEM);
  742. err = cache_check(cd, &ek->h, reqp);
  743. if (err) {
  744. trace_nfsd_exp_find_key(&key, err);
  745. return ERR_PTR(err);
  746. }
  747. return ek;
  748. }
  749. static struct svc_export *
  750. exp_get_by_name(struct cache_detail *cd, struct auth_domain *clp,
  751. const struct path *path, struct cache_req *reqp)
  752. {
  753. struct svc_export *exp, key;
  754. int err;
  755. if (!clp)
  756. return ERR_PTR(-ENOENT);
  757. key.ex_client = clp;
  758. key.ex_path = *path;
  759. key.cd = cd;
  760. exp = svc_export_lookup(&key);
  761. if (exp == NULL)
  762. return ERR_PTR(-ENOMEM);
  763. err = cache_check(cd, &exp->h, reqp);
  764. if (err) {
  765. trace_nfsd_exp_get_by_name(&key, err);
  766. return ERR_PTR(err);
  767. }
  768. return exp;
  769. }
  770. /*
  771. * Find the export entry for a given dentry.
  772. */
  773. static struct svc_export *
  774. exp_parent(struct cache_detail *cd, struct auth_domain *clp, struct path *path)
  775. {
  776. struct dentry *saved = dget(path->dentry);
  777. struct svc_export *exp = exp_get_by_name(cd, clp, path, NULL);
  778. while (PTR_ERR(exp) == -ENOENT && !IS_ROOT(path->dentry)) {
  779. struct dentry *parent = dget_parent(path->dentry);
  780. dput(path->dentry);
  781. path->dentry = parent;
  782. exp = exp_get_by_name(cd, clp, path, NULL);
  783. }
  784. dput(path->dentry);
  785. path->dentry = saved;
  786. return exp;
  787. }
  788. /*
  789. * Obtain the root fh on behalf of a client.
  790. * This could be done in user space, but I feel that it adds some safety
  791. * since its harder to fool a kernel module than a user space program.
  792. */
  793. int
  794. exp_rootfh(struct net *net, struct auth_domain *clp, char *name,
  795. struct knfsd_fh *f, int maxsize)
  796. {
  797. struct svc_export *exp;
  798. struct path path;
  799. struct inode *inode;
  800. struct svc_fh fh;
  801. int err;
  802. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  803. struct cache_detail *cd = nn->svc_export_cache;
  804. err = -EPERM;
  805. /* NB: we probably ought to check that it's NUL-terminated */
  806. if (kern_path(name, 0, &path)) {
  807. printk("nfsd: exp_rootfh path not found %s", name);
  808. return err;
  809. }
  810. inode = d_inode(path.dentry);
  811. dprintk("nfsd: exp_rootfh(%s [%p] %s:%s/%ld)\n",
  812. name, path.dentry, clp->name,
  813. inode->i_sb->s_id, inode->i_ino);
  814. exp = exp_parent(cd, clp, &path);
  815. if (IS_ERR(exp)) {
  816. err = PTR_ERR(exp);
  817. goto out;
  818. }
  819. /*
  820. * fh must be initialized before calling fh_compose
  821. */
  822. fh_init(&fh, maxsize);
  823. if (fh_compose(&fh, exp, path.dentry, NULL))
  824. err = -EINVAL;
  825. else
  826. err = 0;
  827. memcpy(f, &fh.fh_handle, sizeof(struct knfsd_fh));
  828. fh_put(&fh);
  829. exp_put(exp);
  830. out:
  831. path_put(&path);
  832. return err;
  833. }
  834. static struct svc_export *exp_find(struct cache_detail *cd,
  835. struct auth_domain *clp, int fsid_type,
  836. u32 *fsidv, struct cache_req *reqp)
  837. {
  838. struct svc_export *exp;
  839. struct nfsd_net *nn = net_generic(cd->net, nfsd_net_id);
  840. struct svc_expkey *ek = exp_find_key(nn->svc_expkey_cache, clp, fsid_type, fsidv, reqp);
  841. if (IS_ERR(ek))
  842. return ERR_CAST(ek);
  843. exp = exp_get_by_name(cd, clp, &ek->ek_path, reqp);
  844. cache_put(&ek->h, nn->svc_expkey_cache);
  845. if (IS_ERR(exp))
  846. return ERR_CAST(exp);
  847. return exp;
  848. }
  849. __be32 check_nfsd_access(struct svc_export *exp, struct svc_rqst *rqstp)
  850. {
  851. struct exp_flavor_info *f;
  852. struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;
  853. /* legacy gss-only clients are always OK: */
  854. if (exp->ex_client == rqstp->rq_gssclient)
  855. return 0;
  856. /* ip-address based client; check sec= export option: */
  857. for (f = exp->ex_flavors; f < end; f++) {
  858. if (f->pseudoflavor == rqstp->rq_cred.cr_flavor)
  859. return 0;
  860. }
  861. /* defaults in absence of sec= options: */
  862. if (exp->ex_nflavors == 0) {
  863. if (rqstp->rq_cred.cr_flavor == RPC_AUTH_NULL ||
  864. rqstp->rq_cred.cr_flavor == RPC_AUTH_UNIX)
  865. return 0;
  866. }
  867. /* If the compound op contains a spo_must_allowed op,
  868. * it will be sent with integrity/protection which
  869. * will have to be expressly allowed on mounts that
  870. * don't support it
  871. */
  872. if (nfsd4_spo_must_allow(rqstp))
  873. return 0;
  874. return rqstp->rq_vers < 4 ? nfserr_acces : nfserr_wrongsec;
  875. }
  876. /*
  877. * Uses rq_client and rq_gssclient to find an export; uses rq_client (an
  878. * auth_unix client) if it's available and has secinfo information;
  879. * otherwise, will try to use rq_gssclient.
  880. *
  881. * Called from functions that handle requests; functions that do work on
  882. * behalf of mountd are passed a single client name to use, and should
  883. * use exp_get_by_name() or exp_find().
  884. */
  885. struct svc_export *
  886. rqst_exp_get_by_name(struct svc_rqst *rqstp, struct path *path)
  887. {
  888. struct svc_export *gssexp, *exp = ERR_PTR(-ENOENT);
  889. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  890. struct cache_detail *cd = nn->svc_export_cache;
  891. if (rqstp->rq_client == NULL)
  892. goto gss;
  893. /* First try the auth_unix client: */
  894. exp = exp_get_by_name(cd, rqstp->rq_client, path, &rqstp->rq_chandle);
  895. if (PTR_ERR(exp) == -ENOENT)
  896. goto gss;
  897. if (IS_ERR(exp))
  898. return exp;
  899. /* If it has secinfo, assume there are no gss/... clients */
  900. if (exp->ex_nflavors > 0)
  901. return exp;
  902. gss:
  903. /* Otherwise, try falling back on gss client */
  904. if (rqstp->rq_gssclient == NULL)
  905. return exp;
  906. gssexp = exp_get_by_name(cd, rqstp->rq_gssclient, path, &rqstp->rq_chandle);
  907. if (PTR_ERR(gssexp) == -ENOENT)
  908. return exp;
  909. if (!IS_ERR(exp))
  910. exp_put(exp);
  911. return gssexp;
  912. }
  913. struct svc_export *
  914. rqst_exp_find(struct svc_rqst *rqstp, int fsid_type, u32 *fsidv)
  915. {
  916. struct svc_export *gssexp, *exp = ERR_PTR(-ENOENT);
  917. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  918. struct cache_detail *cd = nn->svc_export_cache;
  919. if (rqstp->rq_client == NULL)
  920. goto gss;
  921. /* First try the auth_unix client: */
  922. exp = exp_find(cd, rqstp->rq_client, fsid_type,
  923. fsidv, &rqstp->rq_chandle);
  924. if (PTR_ERR(exp) == -ENOENT)
  925. goto gss;
  926. if (IS_ERR(exp))
  927. return exp;
  928. /* If it has secinfo, assume there are no gss/... clients */
  929. if (exp->ex_nflavors > 0)
  930. return exp;
  931. gss:
  932. /* Otherwise, try falling back on gss client */
  933. if (rqstp->rq_gssclient == NULL)
  934. return exp;
  935. gssexp = exp_find(cd, rqstp->rq_gssclient, fsid_type, fsidv,
  936. &rqstp->rq_chandle);
  937. if (PTR_ERR(gssexp) == -ENOENT)
  938. return exp;
  939. if (!IS_ERR(exp))
  940. exp_put(exp);
  941. return gssexp;
  942. }
  943. struct svc_export *
  944. rqst_exp_parent(struct svc_rqst *rqstp, struct path *path)
  945. {
  946. struct dentry *saved = dget(path->dentry);
  947. struct svc_export *exp = rqst_exp_get_by_name(rqstp, path);
  948. while (PTR_ERR(exp) == -ENOENT && !IS_ROOT(path->dentry)) {
  949. struct dentry *parent = dget_parent(path->dentry);
  950. dput(path->dentry);
  951. path->dentry = parent;
  952. exp = rqst_exp_get_by_name(rqstp, path);
  953. }
  954. dput(path->dentry);
  955. path->dentry = saved;
  956. return exp;
  957. }
  958. struct svc_export *rqst_find_fsidzero_export(struct svc_rqst *rqstp)
  959. {
  960. u32 fsidv[2];
  961. mk_fsid(FSID_NUM, fsidv, 0, 0, 0, NULL);
  962. return rqst_exp_find(rqstp, FSID_NUM, fsidv);
  963. }
  964. /*
  965. * Called when we need the filehandle for the root of the pseudofs,
  966. * for a given NFSv4 client. The root is defined to be the
  967. * export point with fsid==0
  968. */
  969. __be32
  970. exp_pseudoroot(struct svc_rqst *rqstp, struct svc_fh *fhp)
  971. {
  972. struct svc_export *exp;
  973. __be32 rv;
  974. exp = rqst_find_fsidzero_export(rqstp);
  975. if (IS_ERR(exp))
  976. return nfserrno(PTR_ERR(exp));
  977. rv = fh_compose(fhp, exp, exp->ex_path.dentry, NULL);
  978. exp_put(exp);
  979. return rv;
  980. }
  981. static struct flags {
  982. int flag;
  983. char *name[2];
  984. } expflags[] = {
  985. { NFSEXP_READONLY, {"ro", "rw"}},
  986. { NFSEXP_INSECURE_PORT, {"insecure", ""}},
  987. { NFSEXP_ROOTSQUASH, {"root_squash", "no_root_squash"}},
  988. { NFSEXP_ALLSQUASH, {"all_squash", ""}},
  989. { NFSEXP_ASYNC, {"async", "sync"}},
  990. { NFSEXP_GATHERED_WRITES, {"wdelay", "no_wdelay"}},
  991. { NFSEXP_NOREADDIRPLUS, {"nordirplus", ""}},
  992. { NFSEXP_NOHIDE, {"nohide", ""}},
  993. { NFSEXP_CROSSMOUNT, {"crossmnt", ""}},
  994. { NFSEXP_NOSUBTREECHECK, {"no_subtree_check", ""}},
  995. { NFSEXP_NOAUTHNLM, {"insecure_locks", ""}},
  996. { NFSEXP_V4ROOT, {"v4root", ""}},
  997. { NFSEXP_PNFS, {"pnfs", ""}},
  998. { NFSEXP_SECURITY_LABEL, {"security_label", ""}},
  999. { 0, {"", ""}}
  1000. };
  1001. static void show_expflags(struct seq_file *m, int flags, int mask)
  1002. {
  1003. struct flags *flg;
  1004. int state, first = 0;
  1005. for (flg = expflags; flg->flag; flg++) {
  1006. if (flg->flag & ~mask)
  1007. continue;
  1008. state = (flg->flag & flags) ? 0 : 1;
  1009. if (*flg->name[state])
  1010. seq_printf(m, "%s%s", first++?",":"", flg->name[state]);
  1011. }
  1012. }
  1013. static void show_secinfo_flags(struct seq_file *m, int flags)
  1014. {
  1015. seq_printf(m, ",");
  1016. show_expflags(m, flags, NFSEXP_SECINFO_FLAGS);
  1017. }
  1018. static bool secinfo_flags_equal(int f, int g)
  1019. {
  1020. f &= NFSEXP_SECINFO_FLAGS;
  1021. g &= NFSEXP_SECINFO_FLAGS;
  1022. return f == g;
  1023. }
  1024. static int show_secinfo_run(struct seq_file *m, struct exp_flavor_info **fp, struct exp_flavor_info *end)
  1025. {
  1026. int flags;
  1027. flags = (*fp)->flags;
  1028. seq_printf(m, ",sec=%d", (*fp)->pseudoflavor);
  1029. (*fp)++;
  1030. while (*fp != end && secinfo_flags_equal(flags, (*fp)->flags)) {
  1031. seq_printf(m, ":%d", (*fp)->pseudoflavor);
  1032. (*fp)++;
  1033. }
  1034. return flags;
  1035. }
  1036. static void show_secinfo(struct seq_file *m, struct svc_export *exp)
  1037. {
  1038. struct exp_flavor_info *f;
  1039. struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;
  1040. int flags;
  1041. if (exp->ex_nflavors == 0)
  1042. return;
  1043. f = exp->ex_flavors;
  1044. flags = show_secinfo_run(m, &f, end);
  1045. if (!secinfo_flags_equal(flags, exp->ex_flags))
  1046. show_secinfo_flags(m, flags);
  1047. while (f != end) {
  1048. flags = show_secinfo_run(m, &f, end);
  1049. show_secinfo_flags(m, flags);
  1050. }
  1051. }
  1052. static void exp_flags(struct seq_file *m, int flag, int fsid,
  1053. kuid_t anonu, kgid_t anong, struct nfsd4_fs_locations *fsloc)
  1054. {
  1055. struct user_namespace *userns = m->file->f_cred->user_ns;
  1056. show_expflags(m, flag, NFSEXP_ALLFLAGS);
  1057. if (flag & NFSEXP_FSID)
  1058. seq_printf(m, ",fsid=%d", fsid);
  1059. if (!uid_eq(anonu, make_kuid(userns, (uid_t)-2)) &&
  1060. !uid_eq(anonu, make_kuid(userns, 0x10000-2)))
  1061. seq_printf(m, ",anonuid=%u", from_kuid_munged(userns, anonu));
  1062. if (!gid_eq(anong, make_kgid(userns, (gid_t)-2)) &&
  1063. !gid_eq(anong, make_kgid(userns, 0x10000-2)))
  1064. seq_printf(m, ",anongid=%u", from_kgid_munged(userns, anong));
  1065. if (fsloc && fsloc->locations_count > 0) {
  1066. char *loctype = (fsloc->migrated) ? "refer" : "replicas";
  1067. int i;
  1068. seq_printf(m, ",%s=", loctype);
  1069. seq_escape(m, fsloc->locations[0].path, ",;@ \t\n\\");
  1070. seq_putc(m, '@');
  1071. seq_escape(m, fsloc->locations[0].hosts, ",;@ \t\n\\");
  1072. for (i = 1; i < fsloc->locations_count; i++) {
  1073. seq_putc(m, ';');
  1074. seq_escape(m, fsloc->locations[i].path, ",;@ \t\n\\");
  1075. seq_putc(m, '@');
  1076. seq_escape(m, fsloc->locations[i].hosts, ",;@ \t\n\\");
  1077. }
  1078. }
  1079. }
  1080. static int e_show(struct seq_file *m, void *p)
  1081. {
  1082. struct cache_head *cp = p;
  1083. struct svc_export *exp = container_of(cp, struct svc_export, h);
  1084. struct cache_detail *cd = m->private;
  1085. if (p == SEQ_START_TOKEN) {
  1086. seq_puts(m, "# Version 1.1\n");
  1087. seq_puts(m, "# Path Client(Flags) # IPs\n");
  1088. return 0;
  1089. }
  1090. exp_get(exp);
  1091. if (cache_check(cd, &exp->h, NULL))
  1092. return 0;
  1093. exp_put(exp);
  1094. return svc_export_show(m, cd, cp);
  1095. }
  1096. const struct seq_operations nfs_exports_op = {
  1097. .start = cache_seq_start_rcu,
  1098. .next = cache_seq_next_rcu,
  1099. .stop = cache_seq_stop_rcu,
  1100. .show = e_show,
  1101. };
  1102. /*
  1103. * Initialize the exports module.
  1104. */
  1105. int
  1106. nfsd_export_init(struct net *net)
  1107. {
  1108. int rv;
  1109. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1110. dprintk("nfsd: initializing export module (net: %x).\n", net->ns.inum);
  1111. nn->svc_export_cache = cache_create_net(&svc_export_cache_template, net);
  1112. if (IS_ERR(nn->svc_export_cache))
  1113. return PTR_ERR(nn->svc_export_cache);
  1114. rv = cache_register_net(nn->svc_export_cache, net);
  1115. if (rv)
  1116. goto destroy_export_cache;
  1117. nn->svc_expkey_cache = cache_create_net(&svc_expkey_cache_template, net);
  1118. if (IS_ERR(nn->svc_expkey_cache)) {
  1119. rv = PTR_ERR(nn->svc_expkey_cache);
  1120. goto unregister_export_cache;
  1121. }
  1122. rv = cache_register_net(nn->svc_expkey_cache, net);
  1123. if (rv)
  1124. goto destroy_expkey_cache;
  1125. return 0;
  1126. destroy_expkey_cache:
  1127. cache_destroy_net(nn->svc_expkey_cache, net);
  1128. unregister_export_cache:
  1129. cache_unregister_net(nn->svc_export_cache, net);
  1130. destroy_export_cache:
  1131. cache_destroy_net(nn->svc_export_cache, net);
  1132. return rv;
  1133. }
  1134. /*
  1135. * Flush exports table - called when last nfsd thread is killed
  1136. */
  1137. void
  1138. nfsd_export_flush(struct net *net)
  1139. {
  1140. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1141. cache_purge(nn->svc_expkey_cache);
  1142. cache_purge(nn->svc_export_cache);
  1143. }
  1144. /*
  1145. * Shutdown the exports module.
  1146. */
  1147. void
  1148. nfsd_export_shutdown(struct net *net)
  1149. {
  1150. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1151. dprintk("nfsd: shutting down export module (net: %x).\n", net->ns.inum);
  1152. cache_unregister_net(nn->svc_expkey_cache, net);
  1153. cache_unregister_net(nn->svc_export_cache, net);
  1154. cache_destroy_net(nn->svc_expkey_cache, net);
  1155. cache_destroy_net(nn->svc_export_cache, net);
  1156. svcauth_unix_purge(net);
  1157. dprintk("nfsd: export shutdown complete (net: %x).\n", net->ns.inum);
  1158. }