export.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593
  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. static struct workqueue_struct *nfsd_export_wq;
  35. #define EXPKEY_HASHBITS 8
  36. #define EXPKEY_HASHMAX (1 << EXPKEY_HASHBITS)
  37. #define EXPKEY_HASHMASK (EXPKEY_HASHMAX -1)
  38. static void expkey_release(struct work_struct *work)
  39. {
  40. struct svc_expkey *key = container_of(to_rcu_work(work),
  41. struct svc_expkey, ek_rwork);
  42. if (test_bit(CACHE_VALID, &key->h.flags) &&
  43. !test_bit(CACHE_NEGATIVE, &key->h.flags))
  44. path_put(&key->ek_path);
  45. auth_domain_put(key->ek_client);
  46. kfree(key);
  47. }
  48. static void expkey_put(struct kref *ref)
  49. {
  50. struct svc_expkey *key = container_of(ref, struct svc_expkey, h.ref);
  51. INIT_RCU_WORK(&key->ek_rwork, expkey_release);
  52. queue_rcu_work(nfsd_export_wq, &key->ek_rwork);
  53. }
  54. static int expkey_upcall(struct cache_detail *cd, struct cache_head *h)
  55. {
  56. return sunrpc_cache_pipe_upcall(cd, h);
  57. }
  58. static void expkey_request(struct cache_detail *cd,
  59. struct cache_head *h,
  60. char **bpp, int *blen)
  61. {
  62. /* client fsidtype \xfsid */
  63. struct svc_expkey *ek = container_of(h, struct svc_expkey, h);
  64. char type[5];
  65. qword_add(bpp, blen, ek->ek_client->name);
  66. snprintf(type, 5, "%d", ek->ek_fsidtype);
  67. qword_add(bpp, blen, type);
  68. qword_addhex(bpp, blen, (char*)ek->ek_fsid, key_len(ek->ek_fsidtype));
  69. (*bpp)[-1] = '\n';
  70. }
  71. static struct svc_expkey *svc_expkey_update(struct cache_detail *cd, struct svc_expkey *new,
  72. struct svc_expkey *old);
  73. static struct svc_expkey *svc_expkey_lookup(struct cache_detail *cd, struct svc_expkey *);
  74. static int expkey_parse(struct cache_detail *cd, char *mesg, int mlen)
  75. {
  76. /* client fsidtype fsid expiry [path] */
  77. char *buf;
  78. int len;
  79. struct auth_domain *dom = NULL;
  80. int err;
  81. u8 fsidtype;
  82. struct svc_expkey key;
  83. struct svc_expkey *ek = NULL;
  84. if (mesg[mlen - 1] != '\n')
  85. return -EINVAL;
  86. mesg[mlen-1] = 0;
  87. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  88. err = -ENOMEM;
  89. if (!buf)
  90. goto out;
  91. err = -EINVAL;
  92. if (qword_get(&mesg, buf, PAGE_SIZE) <= 0)
  93. goto out;
  94. err = -ENOENT;
  95. dom = auth_domain_find(buf);
  96. if (!dom)
  97. goto out;
  98. dprintk("found domain %s\n", buf);
  99. err = -EINVAL;
  100. if (qword_get(&mesg, buf, PAGE_SIZE) <= 0)
  101. goto out;
  102. if (kstrtou8(buf, 10, &fsidtype))
  103. goto out;
  104. dprintk("found fsidtype %u\n", fsidtype);
  105. if (key_len(fsidtype)==0) /* invalid type */
  106. goto out;
  107. if ((len=qword_get(&mesg, buf, PAGE_SIZE)) <= 0)
  108. goto out;
  109. dprintk("found fsid length %d\n", len);
  110. if (len != key_len(fsidtype))
  111. goto out;
  112. /* OK, we seem to have a valid key */
  113. key.h.flags = 0;
  114. err = get_expiry(&mesg, &key.h.expiry_time);
  115. if (err)
  116. goto out;
  117. key.ek_client = dom;
  118. key.ek_fsidtype = fsidtype;
  119. memcpy(key.ek_fsid, buf, len);
  120. ek = svc_expkey_lookup(cd, &key);
  121. err = -ENOMEM;
  122. if (!ek)
  123. goto out;
  124. /* now we want a pathname, or empty meaning NEGATIVE */
  125. err = -EINVAL;
  126. len = qword_get(&mesg, buf, PAGE_SIZE);
  127. if (len < 0)
  128. goto out;
  129. dprintk("Path seems to be <%s>\n", buf);
  130. err = 0;
  131. if (len == 0) {
  132. set_bit(CACHE_NEGATIVE, &key.h.flags);
  133. ek = svc_expkey_update(cd, &key, ek);
  134. if (ek)
  135. trace_nfsd_expkey_update(ek, NULL);
  136. else
  137. err = -ENOMEM;
  138. } else {
  139. err = kern_path(buf, 0, &key.ek_path);
  140. if (err)
  141. goto out;
  142. dprintk("Found the path %s\n", buf);
  143. ek = svc_expkey_update(cd, &key, ek);
  144. if (ek)
  145. trace_nfsd_expkey_update(ek, buf);
  146. else
  147. err = -ENOMEM;
  148. path_put(&key.ek_path);
  149. }
  150. cache_flush();
  151. out:
  152. if (ek)
  153. cache_put(&ek->h, cd);
  154. if (dom)
  155. auth_domain_put(dom);
  156. kfree(buf);
  157. return err;
  158. }
  159. static int expkey_show(struct seq_file *m,
  160. struct cache_detail *cd,
  161. struct cache_head *h)
  162. {
  163. struct svc_expkey *ek ;
  164. int i;
  165. if (h ==NULL) {
  166. seq_puts(m, "#domain fsidtype fsid [path]\n");
  167. return 0;
  168. }
  169. ek = container_of(h, struct svc_expkey, h);
  170. seq_printf(m, "%s %d 0x", ek->ek_client->name,
  171. ek->ek_fsidtype);
  172. for (i=0; i < key_len(ek->ek_fsidtype)/4; i++)
  173. seq_printf(m, "%08x", ek->ek_fsid[i]);
  174. if (test_bit(CACHE_VALID, &h->flags) &&
  175. !test_bit(CACHE_NEGATIVE, &h->flags)) {
  176. seq_printf(m, " ");
  177. seq_path(m, &ek->ek_path, "\\ \t\n");
  178. }
  179. seq_printf(m, "\n");
  180. return 0;
  181. }
  182. static inline int expkey_match (struct cache_head *a, struct cache_head *b)
  183. {
  184. struct svc_expkey *orig = container_of(a, struct svc_expkey, h);
  185. struct svc_expkey *new = container_of(b, struct svc_expkey, h);
  186. if (orig->ek_fsidtype != new->ek_fsidtype ||
  187. orig->ek_client != new->ek_client ||
  188. memcmp(orig->ek_fsid, new->ek_fsid, key_len(orig->ek_fsidtype)) != 0)
  189. return 0;
  190. return 1;
  191. }
  192. static inline void expkey_init(struct cache_head *cnew,
  193. struct cache_head *citem)
  194. {
  195. struct svc_expkey *new = container_of(cnew, struct svc_expkey, h);
  196. struct svc_expkey *item = container_of(citem, struct svc_expkey, h);
  197. kref_get(&item->ek_client->ref);
  198. new->ek_client = item->ek_client;
  199. new->ek_fsidtype = item->ek_fsidtype;
  200. memcpy(new->ek_fsid, item->ek_fsid, sizeof(new->ek_fsid));
  201. }
  202. static inline void expkey_update(struct cache_head *cnew,
  203. struct cache_head *citem)
  204. {
  205. struct svc_expkey *new = container_of(cnew, struct svc_expkey, h);
  206. struct svc_expkey *item = container_of(citem, struct svc_expkey, h);
  207. new->ek_path = item->ek_path;
  208. path_get(&item->ek_path);
  209. }
  210. static struct cache_head *expkey_alloc(void)
  211. {
  212. struct svc_expkey *i = kmalloc_obj(*i);
  213. if (i)
  214. return &i->h;
  215. else
  216. return NULL;
  217. }
  218. static void expkey_flush(void)
  219. {
  220. /*
  221. * Take the nfsd_mutex here to ensure that the file cache is not
  222. * destroyed while we're in the middle of flushing.
  223. */
  224. mutex_lock(&nfsd_mutex);
  225. nfsd_file_cache_purge(current->nsproxy->net_ns);
  226. mutex_unlock(&nfsd_mutex);
  227. }
  228. static const struct cache_detail svc_expkey_cache_template = {
  229. .owner = THIS_MODULE,
  230. .hash_size = EXPKEY_HASHMAX,
  231. .name = "nfsd.fh",
  232. .cache_put = expkey_put,
  233. .cache_upcall = expkey_upcall,
  234. .cache_request = expkey_request,
  235. .cache_parse = expkey_parse,
  236. .cache_show = expkey_show,
  237. .match = expkey_match,
  238. .init = expkey_init,
  239. .update = expkey_update,
  240. .alloc = expkey_alloc,
  241. .flush = expkey_flush,
  242. };
  243. static int
  244. svc_expkey_hash(struct svc_expkey *item)
  245. {
  246. int hash = item->ek_fsidtype;
  247. char * cp = (char*)item->ek_fsid;
  248. int len = key_len(item->ek_fsidtype);
  249. hash ^= hash_mem(cp, len, EXPKEY_HASHBITS);
  250. hash ^= hash_ptr(item->ek_client, EXPKEY_HASHBITS);
  251. hash &= EXPKEY_HASHMASK;
  252. return hash;
  253. }
  254. static struct svc_expkey *
  255. svc_expkey_lookup(struct cache_detail *cd, struct svc_expkey *item)
  256. {
  257. struct cache_head *ch;
  258. int hash = svc_expkey_hash(item);
  259. ch = sunrpc_cache_lookup_rcu(cd, &item->h, hash);
  260. if (ch)
  261. return container_of(ch, struct svc_expkey, h);
  262. else
  263. return NULL;
  264. }
  265. static struct svc_expkey *
  266. svc_expkey_update(struct cache_detail *cd, struct svc_expkey *new,
  267. struct svc_expkey *old)
  268. {
  269. struct cache_head *ch;
  270. int hash = svc_expkey_hash(new);
  271. ch = sunrpc_cache_update(cd, &new->h, &old->h, hash);
  272. if (ch)
  273. return container_of(ch, struct svc_expkey, h);
  274. else
  275. return NULL;
  276. }
  277. #define EXPORT_HASHBITS 8
  278. #define EXPORT_HASHMAX (1<< EXPORT_HASHBITS)
  279. static void nfsd4_fslocs_free(struct nfsd4_fs_locations *fsloc)
  280. {
  281. struct nfsd4_fs_location *locations = fsloc->locations;
  282. int i;
  283. if (!locations)
  284. return;
  285. for (i = 0; i < fsloc->locations_count; i++) {
  286. kfree(locations[i].path);
  287. kfree(locations[i].hosts);
  288. }
  289. kfree(locations);
  290. fsloc->locations = NULL;
  291. }
  292. static int export_stats_init(struct export_stats *stats)
  293. {
  294. stats->start_time = ktime_get_seconds();
  295. return percpu_counter_init_many(stats->counter, 0, GFP_KERNEL,
  296. EXP_STATS_COUNTERS_NUM);
  297. }
  298. static void export_stats_reset(struct export_stats *stats)
  299. {
  300. if (stats) {
  301. int i;
  302. for (i = 0; i < EXP_STATS_COUNTERS_NUM; i++)
  303. percpu_counter_set(&stats->counter[i], 0);
  304. }
  305. }
  306. static void export_stats_destroy(struct export_stats *stats)
  307. {
  308. if (stats)
  309. percpu_counter_destroy_many(stats->counter,
  310. EXP_STATS_COUNTERS_NUM);
  311. }
  312. static void svc_export_release(struct work_struct *work)
  313. {
  314. struct svc_export *exp = container_of(to_rcu_work(work),
  315. struct svc_export, ex_rwork);
  316. path_put(&exp->ex_path);
  317. auth_domain_put(exp->ex_client);
  318. nfsd4_fslocs_free(&exp->ex_fslocs);
  319. export_stats_destroy(exp->ex_stats);
  320. kfree(exp->ex_stats);
  321. kfree(exp->ex_uuid);
  322. kfree(exp);
  323. }
  324. static void svc_export_put(struct kref *ref)
  325. {
  326. struct svc_export *exp = container_of(ref, struct svc_export, h.ref);
  327. INIT_RCU_WORK(&exp->ex_rwork, svc_export_release);
  328. queue_rcu_work(nfsd_export_wq, &exp->ex_rwork);
  329. }
  330. static int svc_export_upcall(struct cache_detail *cd, struct cache_head *h)
  331. {
  332. return sunrpc_cache_pipe_upcall(cd, h);
  333. }
  334. static void svc_export_request(struct cache_detail *cd,
  335. struct cache_head *h,
  336. char **bpp, int *blen)
  337. {
  338. /* client path */
  339. struct svc_export *exp = container_of(h, struct svc_export, h);
  340. char *pth;
  341. qword_add(bpp, blen, exp->ex_client->name);
  342. pth = d_path(&exp->ex_path, *bpp, *blen);
  343. if (IS_ERR(pth)) {
  344. /* is this correct? */
  345. (*bpp)[0] = '\n';
  346. return;
  347. }
  348. qword_add(bpp, blen, pth);
  349. (*bpp)[-1] = '\n';
  350. }
  351. static struct svc_export *svc_export_update(struct svc_export *new,
  352. struct svc_export *old);
  353. static struct svc_export *svc_export_lookup(struct svc_export *);
  354. static int check_export(const struct path *path, int *flags, unsigned char *uuid)
  355. {
  356. struct inode *inode = d_inode(path->dentry);
  357. /*
  358. * We currently export only dirs, regular files, and (for v4
  359. * pseudoroot) symlinks.
  360. */
  361. if (!S_ISDIR(inode->i_mode) &&
  362. !S_ISLNK(inode->i_mode) &&
  363. !S_ISREG(inode->i_mode))
  364. return -ENOTDIR;
  365. /*
  366. * Mountd should never pass down a writeable V4ROOT export, but,
  367. * just to make sure:
  368. */
  369. if (*flags & NFSEXP_V4ROOT)
  370. *flags |= NFSEXP_READONLY;
  371. /* There are two requirements on a filesystem to be exportable.
  372. * 1: We must be able to identify the filesystem from a number.
  373. * either a device number (so FS_REQUIRES_DEV needed)
  374. * or an FSID number (so NFSEXP_FSID or ->uuid is needed).
  375. * 2: We must be able to find an inode from a filehandle.
  376. * This means that s_export_op must be set and comply with
  377. * the requirements for remote filesystem export.
  378. * 3: We must not currently be on an idmapped mount.
  379. */
  380. if (!(inode->i_sb->s_type->fs_flags & FS_REQUIRES_DEV) &&
  381. !(*flags & NFSEXP_FSID) &&
  382. uuid == NULL) {
  383. dprintk("exp_export: export of non-dev fs without fsid\n");
  384. return -EINVAL;
  385. }
  386. if (!exportfs_may_export(inode->i_sb->s_export_op)) {
  387. dprintk("exp_export: export of invalid fs type (%s).\n",
  388. inode->i_sb->s_type->name);
  389. return -EINVAL;
  390. }
  391. if (is_idmapped_mnt(path->mnt)) {
  392. dprintk("exp_export: export of idmapped mounts not yet supported.\n");
  393. return -EINVAL;
  394. }
  395. if (inode->i_sb->s_export_op->flags & EXPORT_OP_NOSUBTREECHK &&
  396. !(*flags & NFSEXP_NOSUBTREECHECK)) {
  397. dprintk("%s: %s does not support subtree checking!\n",
  398. __func__, inode->i_sb->s_type->name);
  399. return -EINVAL;
  400. }
  401. return 0;
  402. }
  403. #ifdef CONFIG_NFSD_V4
  404. static int
  405. fsloc_parse(char **mesg, char *buf, struct nfsd4_fs_locations *fsloc)
  406. {
  407. int len;
  408. int migrated, i, err;
  409. /* more than one fsloc */
  410. if (fsloc->locations)
  411. return -EINVAL;
  412. /* listsize */
  413. err = get_uint(mesg, &fsloc->locations_count);
  414. if (err)
  415. return err;
  416. if (fsloc->locations_count > MAX_FS_LOCATIONS)
  417. return -EINVAL;
  418. if (fsloc->locations_count == 0)
  419. return 0;
  420. fsloc->locations = kzalloc_objs(struct nfsd4_fs_location,
  421. fsloc->locations_count);
  422. if (!fsloc->locations)
  423. return -ENOMEM;
  424. for (i=0; i < fsloc->locations_count; i++) {
  425. /* colon separated host list */
  426. err = -EINVAL;
  427. len = qword_get(mesg, buf, PAGE_SIZE);
  428. if (len <= 0)
  429. goto out_free_all;
  430. err = -ENOMEM;
  431. fsloc->locations[i].hosts = kstrdup(buf, GFP_KERNEL);
  432. if (!fsloc->locations[i].hosts)
  433. goto out_free_all;
  434. err = -EINVAL;
  435. /* slash separated path component list */
  436. len = qword_get(mesg, buf, PAGE_SIZE);
  437. if (len <= 0)
  438. goto out_free_all;
  439. err = -ENOMEM;
  440. fsloc->locations[i].path = kstrdup(buf, GFP_KERNEL);
  441. if (!fsloc->locations[i].path)
  442. goto out_free_all;
  443. }
  444. /* migrated */
  445. err = get_int(mesg, &migrated);
  446. if (err)
  447. goto out_free_all;
  448. err = -EINVAL;
  449. if (migrated < 0 || migrated > 1)
  450. goto out_free_all;
  451. fsloc->migrated = migrated;
  452. return 0;
  453. out_free_all:
  454. nfsd4_fslocs_free(fsloc);
  455. return err;
  456. }
  457. static int secinfo_parse(char **mesg, char *buf, struct svc_export *exp)
  458. {
  459. struct exp_flavor_info *f;
  460. u32 listsize;
  461. int err;
  462. /* more than one secinfo */
  463. if (exp->ex_nflavors)
  464. return -EINVAL;
  465. err = get_uint(mesg, &listsize);
  466. if (err)
  467. return err;
  468. if (listsize > MAX_SECINFO_LIST)
  469. return -EINVAL;
  470. for (f = exp->ex_flavors; f < exp->ex_flavors + listsize; f++) {
  471. err = get_uint(mesg, &f->pseudoflavor);
  472. if (err)
  473. return err;
  474. /*
  475. * XXX: It would be nice to also check whether this
  476. * pseudoflavor is supported, so we can discover the
  477. * problem at export time instead of when a client fails
  478. * to authenticate.
  479. */
  480. err = get_uint(mesg, &f->flags);
  481. if (err)
  482. return err;
  483. /* Only some flags are allowed to differ between flavors: */
  484. if (~NFSEXP_SECINFO_FLAGS & (f->flags ^ exp->ex_flags))
  485. return -EINVAL;
  486. }
  487. exp->ex_nflavors = listsize;
  488. return 0;
  489. }
  490. #else /* CONFIG_NFSD_V4 */
  491. static inline int
  492. fsloc_parse(char **mesg, char *buf, struct nfsd4_fs_locations *fsloc){return 0;}
  493. static inline int
  494. secinfo_parse(char **mesg, char *buf, struct svc_export *exp) { return 0; }
  495. #endif
  496. static int xprtsec_parse(char **mesg, char *buf, struct svc_export *exp)
  497. {
  498. unsigned int i, mode, listsize;
  499. int err;
  500. err = get_uint(mesg, &listsize);
  501. if (err)
  502. return err;
  503. if (listsize > NFSEXP_XPRTSEC_NUM)
  504. return -EINVAL;
  505. exp->ex_xprtsec_modes = 0;
  506. for (i = 0; i < listsize; i++) {
  507. err = get_uint(mesg, &mode);
  508. if (err)
  509. return err;
  510. if (mode > NFSEXP_XPRTSEC_MTLS)
  511. return -EINVAL;
  512. exp->ex_xprtsec_modes |= mode;
  513. }
  514. return 0;
  515. }
  516. static inline int
  517. nfsd_uuid_parse(char **mesg, char *buf, unsigned char **puuid)
  518. {
  519. int len;
  520. /* more than one uuid */
  521. if (*puuid)
  522. return -EINVAL;
  523. /* expect a 16 byte uuid encoded as \xXXXX... */
  524. len = qword_get(mesg, buf, PAGE_SIZE);
  525. if (len != EX_UUID_LEN)
  526. return -EINVAL;
  527. *puuid = kmemdup(buf, EX_UUID_LEN, GFP_KERNEL);
  528. if (*puuid == NULL)
  529. return -ENOMEM;
  530. return 0;
  531. }
  532. static int svc_export_parse(struct cache_detail *cd, char *mesg, int mlen)
  533. {
  534. /* client path expiry [flags anonuid anongid fsid] */
  535. char *buf;
  536. int err;
  537. struct auth_domain *dom = NULL;
  538. struct svc_export exp = {}, *expp;
  539. int an_int;
  540. if (mesg[mlen-1] != '\n')
  541. return -EINVAL;
  542. mesg[mlen-1] = 0;
  543. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  544. if (!buf)
  545. return -ENOMEM;
  546. /* client */
  547. err = -EINVAL;
  548. if (qword_get(&mesg, buf, PAGE_SIZE) <= 0)
  549. goto out;
  550. err = -ENOENT;
  551. dom = auth_domain_find(buf);
  552. if (!dom)
  553. goto out;
  554. /* path */
  555. err = -EINVAL;
  556. if (qword_get(&mesg, buf, PAGE_SIZE) <= 0)
  557. goto out1;
  558. err = kern_path(buf, 0, &exp.ex_path);
  559. if (err)
  560. goto out1;
  561. exp.ex_client = dom;
  562. exp.cd = cd;
  563. exp.ex_devid_map = NULL;
  564. exp.ex_xprtsec_modes = NFSEXP_XPRTSEC_ALL;
  565. /* expiry */
  566. err = get_expiry(&mesg, &exp.h.expiry_time);
  567. if (err)
  568. goto out3;
  569. /* flags */
  570. err = get_int(&mesg, &an_int);
  571. if (err == -ENOENT) {
  572. err = 0;
  573. set_bit(CACHE_NEGATIVE, &exp.h.flags);
  574. } else {
  575. if (err || an_int < 0)
  576. goto out3;
  577. exp.ex_flags= an_int;
  578. /* anon uid */
  579. err = get_int(&mesg, &an_int);
  580. if (err)
  581. goto out3;
  582. exp.ex_anon_uid= make_kuid(current_user_ns(), an_int);
  583. /* anon gid */
  584. err = get_int(&mesg, &an_int);
  585. if (err)
  586. goto out3;
  587. exp.ex_anon_gid= make_kgid(current_user_ns(), an_int);
  588. /* fsid */
  589. err = get_int(&mesg, &an_int);
  590. if (err)
  591. goto out3;
  592. exp.ex_fsid = an_int;
  593. while (qword_get(&mesg, buf, PAGE_SIZE) > 0) {
  594. if (strcmp(buf, "fsloc") == 0)
  595. err = fsloc_parse(&mesg, buf, &exp.ex_fslocs);
  596. else if (strcmp(buf, "uuid") == 0)
  597. err = nfsd_uuid_parse(&mesg, buf, &exp.ex_uuid);
  598. else if (strcmp(buf, "secinfo") == 0)
  599. err = secinfo_parse(&mesg, buf, &exp);
  600. else if (strcmp(buf, "xprtsec") == 0)
  601. err = xprtsec_parse(&mesg, buf, &exp);
  602. else
  603. /* quietly ignore unknown words and anything
  604. * following. Newer user-space can try to set
  605. * new values, then see what the result was.
  606. */
  607. break;
  608. if (err)
  609. goto out4;
  610. }
  611. err = check_export(&exp.ex_path, &exp.ex_flags, exp.ex_uuid);
  612. if (err)
  613. goto out4;
  614. /*
  615. * No point caching this if it would immediately expire.
  616. * Also, this protects exportfs's dummy export from the
  617. * anon_uid/anon_gid checks:
  618. */
  619. if (exp.h.expiry_time < seconds_since_boot())
  620. goto out4;
  621. /*
  622. * For some reason exportfs has been passing down an
  623. * invalid (-1) uid & gid on the "dummy" export which it
  624. * uses to test export support. To make sure exportfs
  625. * sees errors from check_export we therefore need to
  626. * delay these checks till after check_export:
  627. */
  628. err = -EINVAL;
  629. if (!uid_valid(exp.ex_anon_uid))
  630. goto out4;
  631. if (!gid_valid(exp.ex_anon_gid))
  632. goto out4;
  633. err = 0;
  634. nfsd4_setup_layout_type(&exp);
  635. }
  636. expp = svc_export_lookup(&exp);
  637. if (!expp) {
  638. err = -ENOMEM;
  639. goto out4;
  640. }
  641. expp = svc_export_update(&exp, expp);
  642. if (expp) {
  643. trace_nfsd_export_update(expp);
  644. cache_flush();
  645. exp_put(expp);
  646. } else
  647. err = -ENOMEM;
  648. out4:
  649. nfsd4_fslocs_free(&exp.ex_fslocs);
  650. kfree(exp.ex_uuid);
  651. out3:
  652. path_put(&exp.ex_path);
  653. out1:
  654. auth_domain_put(dom);
  655. out:
  656. kfree(buf);
  657. return err;
  658. }
  659. static void exp_flags(struct seq_file *m, int flag, int fsid,
  660. kuid_t anonu, kgid_t anong, struct nfsd4_fs_locations *fslocs);
  661. static void show_secinfo(struct seq_file *m, struct svc_export *exp);
  662. static int is_export_stats_file(struct seq_file *m)
  663. {
  664. /*
  665. * The export_stats file uses the same ops as the exports file.
  666. * We use the file's name to determine the reported info per export.
  667. * There is no rename in nsfdfs, so d_name.name is stable.
  668. */
  669. return !strcmp(m->file->f_path.dentry->d_name.name, "export_stats");
  670. }
  671. static int svc_export_show(struct seq_file *m,
  672. struct cache_detail *cd,
  673. struct cache_head *h)
  674. {
  675. struct svc_export *exp;
  676. bool export_stats = is_export_stats_file(m);
  677. if (h == NULL) {
  678. if (export_stats)
  679. seq_puts(m, "#path domain start-time\n#\tstats\n");
  680. else
  681. seq_puts(m, "#path domain(flags)\n");
  682. return 0;
  683. }
  684. exp = container_of(h, struct svc_export, h);
  685. seq_path(m, &exp->ex_path, " \t\n\\");
  686. seq_putc(m, '\t');
  687. seq_escape(m, exp->ex_client->name, " \t\n\\");
  688. if (export_stats) {
  689. struct percpu_counter *counter = exp->ex_stats->counter;
  690. seq_printf(m, "\t%lld\n", exp->ex_stats->start_time);
  691. seq_printf(m, "\tfh_stale: %lld\n",
  692. percpu_counter_sum_positive(&counter[EXP_STATS_FH_STALE]));
  693. seq_printf(m, "\tio_read: %lld\n",
  694. percpu_counter_sum_positive(&counter[EXP_STATS_IO_READ]));
  695. seq_printf(m, "\tio_write: %lld\n",
  696. percpu_counter_sum_positive(&counter[EXP_STATS_IO_WRITE]));
  697. seq_putc(m, '\n');
  698. return 0;
  699. }
  700. seq_putc(m, '(');
  701. if (test_bit(CACHE_VALID, &h->flags) &&
  702. !test_bit(CACHE_NEGATIVE, &h->flags)) {
  703. exp_flags(m, exp->ex_flags, exp->ex_fsid,
  704. exp->ex_anon_uid, exp->ex_anon_gid, &exp->ex_fslocs);
  705. if (exp->ex_uuid) {
  706. int i;
  707. seq_puts(m, ",uuid=");
  708. for (i = 0; i < EX_UUID_LEN; i++) {
  709. if ((i&3) == 0 && i)
  710. seq_putc(m, ':');
  711. seq_printf(m, "%02x", exp->ex_uuid[i]);
  712. }
  713. }
  714. show_secinfo(m, exp);
  715. }
  716. seq_puts(m, ")\n");
  717. return 0;
  718. }
  719. static int svc_export_match(struct cache_head *a, struct cache_head *b)
  720. {
  721. struct svc_export *orig = container_of(a, struct svc_export, h);
  722. struct svc_export *new = container_of(b, struct svc_export, h);
  723. return orig->ex_client == new->ex_client &&
  724. path_equal(&orig->ex_path, &new->ex_path);
  725. }
  726. static void svc_export_init(struct cache_head *cnew, struct cache_head *citem)
  727. {
  728. struct svc_export *new = container_of(cnew, struct svc_export, h);
  729. struct svc_export *item = container_of(citem, struct svc_export, h);
  730. kref_get(&item->ex_client->ref);
  731. new->ex_client = item->ex_client;
  732. new->ex_path = item->ex_path;
  733. path_get(&item->ex_path);
  734. new->ex_fslocs.locations = NULL;
  735. new->ex_fslocs.locations_count = 0;
  736. new->ex_fslocs.migrated = 0;
  737. new->ex_layout_types = 0;
  738. new->ex_uuid = NULL;
  739. new->cd = item->cd;
  740. export_stats_reset(new->ex_stats);
  741. }
  742. static void export_update(struct cache_head *cnew, struct cache_head *citem)
  743. {
  744. struct svc_export *new = container_of(cnew, struct svc_export, h);
  745. struct svc_export *item = container_of(citem, struct svc_export, h);
  746. int i;
  747. new->ex_flags = item->ex_flags;
  748. new->ex_anon_uid = item->ex_anon_uid;
  749. new->ex_anon_gid = item->ex_anon_gid;
  750. new->ex_fsid = item->ex_fsid;
  751. new->ex_devid_map = item->ex_devid_map;
  752. item->ex_devid_map = NULL;
  753. new->ex_uuid = item->ex_uuid;
  754. item->ex_uuid = NULL;
  755. new->ex_fslocs.locations = item->ex_fslocs.locations;
  756. item->ex_fslocs.locations = NULL;
  757. new->ex_fslocs.locations_count = item->ex_fslocs.locations_count;
  758. item->ex_fslocs.locations_count = 0;
  759. new->ex_fslocs.migrated = item->ex_fslocs.migrated;
  760. item->ex_fslocs.migrated = 0;
  761. new->ex_layout_types = item->ex_layout_types;
  762. new->ex_nflavors = item->ex_nflavors;
  763. for (i = 0; i < MAX_SECINFO_LIST; i++) {
  764. new->ex_flavors[i] = item->ex_flavors[i];
  765. }
  766. new->ex_xprtsec_modes = item->ex_xprtsec_modes;
  767. }
  768. static struct cache_head *svc_export_alloc(void)
  769. {
  770. struct svc_export *i = kmalloc_obj(*i);
  771. if (!i)
  772. return NULL;
  773. i->ex_stats = kmalloc_obj(*(i->ex_stats));
  774. if (!i->ex_stats) {
  775. kfree(i);
  776. return NULL;
  777. }
  778. if (export_stats_init(i->ex_stats)) {
  779. kfree(i->ex_stats);
  780. kfree(i);
  781. return NULL;
  782. }
  783. return &i->h;
  784. }
  785. static const struct cache_detail svc_export_cache_template = {
  786. .owner = THIS_MODULE,
  787. .hash_size = EXPORT_HASHMAX,
  788. .name = "nfsd.export",
  789. .cache_put = svc_export_put,
  790. .cache_upcall = svc_export_upcall,
  791. .cache_request = svc_export_request,
  792. .cache_parse = svc_export_parse,
  793. .cache_show = svc_export_show,
  794. .match = svc_export_match,
  795. .init = svc_export_init,
  796. .update = export_update,
  797. .alloc = svc_export_alloc,
  798. };
  799. static int
  800. svc_export_hash(struct svc_export *exp)
  801. {
  802. int hash;
  803. hash = hash_ptr(exp->ex_client, EXPORT_HASHBITS);
  804. hash ^= hash_ptr(exp->ex_path.dentry, EXPORT_HASHBITS);
  805. hash ^= hash_ptr(exp->ex_path.mnt, EXPORT_HASHBITS);
  806. return hash;
  807. }
  808. static struct svc_export *
  809. svc_export_lookup(struct svc_export *exp)
  810. {
  811. struct cache_head *ch;
  812. int hash = svc_export_hash(exp);
  813. ch = sunrpc_cache_lookup_rcu(exp->cd, &exp->h, hash);
  814. if (ch)
  815. return container_of(ch, struct svc_export, h);
  816. else
  817. return NULL;
  818. }
  819. static struct svc_export *
  820. svc_export_update(struct svc_export *new, struct svc_export *old)
  821. {
  822. struct cache_head *ch;
  823. int hash = svc_export_hash(old);
  824. ch = sunrpc_cache_update(old->cd, &new->h, &old->h, hash);
  825. if (ch)
  826. return container_of(ch, struct svc_export, h);
  827. else
  828. return NULL;
  829. }
  830. static struct svc_expkey *
  831. exp_find_key(struct cache_detail *cd, struct auth_domain *clp, int fsid_type,
  832. u32 *fsidv, struct cache_req *reqp)
  833. {
  834. struct svc_expkey key, *ek;
  835. int err;
  836. if (!clp)
  837. return ERR_PTR(-ENOENT);
  838. key.ek_client = clp;
  839. key.ek_fsidtype = fsid_type;
  840. memcpy(key.ek_fsid, fsidv, key_len(fsid_type));
  841. ek = svc_expkey_lookup(cd, &key);
  842. if (ek == NULL)
  843. return ERR_PTR(-ENOMEM);
  844. err = cache_check(cd, &ek->h, reqp);
  845. if (err) {
  846. trace_nfsd_exp_find_key(&key, err);
  847. return ERR_PTR(err);
  848. }
  849. return ek;
  850. }
  851. static struct svc_export *
  852. exp_get_by_name(struct cache_detail *cd, struct auth_domain *clp,
  853. const struct path *path, struct cache_req *reqp)
  854. {
  855. struct svc_export *exp, key;
  856. int err;
  857. if (!clp)
  858. return ERR_PTR(-ENOENT);
  859. key.ex_client = clp;
  860. key.ex_path = *path;
  861. key.cd = cd;
  862. exp = svc_export_lookup(&key);
  863. if (exp == NULL)
  864. return ERR_PTR(-ENOMEM);
  865. err = cache_check(cd, &exp->h, reqp);
  866. if (err) {
  867. trace_nfsd_exp_get_by_name(&key, err);
  868. return ERR_PTR(err);
  869. }
  870. return exp;
  871. }
  872. /*
  873. * Find the export entry for a given dentry.
  874. */
  875. static struct svc_export *
  876. exp_parent(struct cache_detail *cd, struct auth_domain *clp, struct path *path)
  877. {
  878. struct dentry *saved = dget(path->dentry);
  879. struct svc_export *exp = exp_get_by_name(cd, clp, path, NULL);
  880. while (PTR_ERR(exp) == -ENOENT && !IS_ROOT(path->dentry)) {
  881. struct dentry *parent = dget_parent(path->dentry);
  882. dput(path->dentry);
  883. path->dentry = parent;
  884. exp = exp_get_by_name(cd, clp, path, NULL);
  885. }
  886. dput(path->dentry);
  887. path->dentry = saved;
  888. return exp;
  889. }
  890. /*
  891. * Obtain the root fh on behalf of a client.
  892. * This could be done in user space, but I feel that it adds some safety
  893. * since its harder to fool a kernel module than a user space program.
  894. */
  895. int
  896. exp_rootfh(struct net *net, struct auth_domain *clp, char *name,
  897. struct knfsd_fh *f, int maxsize)
  898. {
  899. struct svc_export *exp;
  900. struct path path;
  901. struct inode *inode __maybe_unused;
  902. struct svc_fh fh;
  903. int err;
  904. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  905. struct cache_detail *cd = nn->svc_export_cache;
  906. err = -EPERM;
  907. /* NB: we probably ought to check that it's NUL-terminated */
  908. if (kern_path(name, 0, &path)) {
  909. printk("nfsd: exp_rootfh path not found %s", name);
  910. return err;
  911. }
  912. inode = d_inode(path.dentry);
  913. dprintk("nfsd: exp_rootfh(%s [%p] %s:%s/%ld)\n",
  914. name, path.dentry, clp->name,
  915. inode->i_sb->s_id, inode->i_ino);
  916. exp = exp_parent(cd, clp, &path);
  917. if (IS_ERR(exp)) {
  918. err = PTR_ERR(exp);
  919. goto out;
  920. }
  921. /*
  922. * fh must be initialized before calling fh_compose
  923. */
  924. fh_init(&fh, maxsize);
  925. if (fh_compose(&fh, exp, path.dentry, NULL))
  926. err = -EINVAL;
  927. else
  928. err = 0;
  929. memcpy(f, &fh.fh_handle, sizeof(struct knfsd_fh));
  930. fh_put(&fh);
  931. exp_put(exp);
  932. out:
  933. path_put(&path);
  934. return err;
  935. }
  936. static struct svc_export *exp_find(struct cache_detail *cd,
  937. struct auth_domain *clp, int fsid_type,
  938. u32 *fsidv, struct cache_req *reqp)
  939. {
  940. struct svc_export *exp;
  941. struct nfsd_net *nn = net_generic(cd->net, nfsd_net_id);
  942. struct svc_expkey *ek = exp_find_key(nn->svc_expkey_cache, clp, fsid_type, fsidv, reqp);
  943. if (IS_ERR(ek))
  944. return ERR_CAST(ek);
  945. exp = exp_get_by_name(cd, clp, &ek->ek_path, reqp);
  946. cache_put(&ek->h, nn->svc_expkey_cache);
  947. if (IS_ERR(exp))
  948. return ERR_CAST(exp);
  949. return exp;
  950. }
  951. /**
  952. * check_xprtsec_policy - check if access to export is allowed by the
  953. * xprtsec policy
  954. * @exp: svc_export that is being accessed.
  955. * @rqstp: svc_rqst attempting to access @exp.
  956. *
  957. * Helper function for check_nfsd_access(). Note that callers should be
  958. * using check_nfsd_access() instead of calling this function directly. The
  959. * one exception is __fh_verify() since it has logic that may result in one
  960. * or both of the helpers being skipped.
  961. *
  962. * Return values:
  963. * %nfs_ok if access is granted, or
  964. * %nfserr_wrongsec if access is denied
  965. */
  966. __be32 check_xprtsec_policy(struct svc_export *exp, struct svc_rqst *rqstp)
  967. {
  968. struct svc_xprt *xprt = rqstp->rq_xprt;
  969. if (exp->ex_xprtsec_modes & NFSEXP_XPRTSEC_NONE) {
  970. if (!test_bit(XPT_TLS_SESSION, &xprt->xpt_flags))
  971. return nfs_ok;
  972. }
  973. if (exp->ex_xprtsec_modes & NFSEXP_XPRTSEC_TLS) {
  974. if (test_bit(XPT_TLS_SESSION, &xprt->xpt_flags) &&
  975. !test_bit(XPT_PEER_AUTH, &xprt->xpt_flags))
  976. return nfs_ok;
  977. }
  978. if (exp->ex_xprtsec_modes & NFSEXP_XPRTSEC_MTLS) {
  979. if (test_bit(XPT_TLS_SESSION, &xprt->xpt_flags) &&
  980. test_bit(XPT_PEER_AUTH, &xprt->xpt_flags))
  981. return nfs_ok;
  982. }
  983. return nfserr_wrongsec;
  984. }
  985. /**
  986. * check_security_flavor - check if access to export is allowed by the
  987. * security flavor
  988. * @exp: svc_export that is being accessed.
  989. * @rqstp: svc_rqst attempting to access @exp.
  990. * @may_bypass_gss: reduce strictness of authorization check
  991. *
  992. * Helper function for check_nfsd_access(). Note that callers should be
  993. * using check_nfsd_access() instead of calling this function directly. The
  994. * one exception is __fh_verify() since it has logic that may result in one
  995. * or both of the helpers being skipped.
  996. *
  997. * Return values:
  998. * %nfs_ok if access is granted, or
  999. * %nfserr_wrongsec if access is denied
  1000. */
  1001. __be32 check_security_flavor(struct svc_export *exp, struct svc_rqst *rqstp,
  1002. bool may_bypass_gss)
  1003. {
  1004. struct exp_flavor_info *f, *end = exp->ex_flavors + exp->ex_nflavors;
  1005. /* legacy gss-only clients are always OK: */
  1006. if (exp->ex_client == rqstp->rq_gssclient)
  1007. return nfs_ok;
  1008. /* ip-address based client; check sec= export option: */
  1009. for (f = exp->ex_flavors; f < end; f++) {
  1010. if (f->pseudoflavor == rqstp->rq_cred.cr_flavor)
  1011. return nfs_ok;
  1012. }
  1013. /* defaults in absence of sec= options: */
  1014. if (exp->ex_nflavors == 0) {
  1015. if (rqstp->rq_cred.cr_flavor == RPC_AUTH_NULL ||
  1016. rqstp->rq_cred.cr_flavor == RPC_AUTH_UNIX)
  1017. return nfs_ok;
  1018. }
  1019. /* If the compound op contains a spo_must_allowed op,
  1020. * it will be sent with integrity/protection which
  1021. * will have to be expressly allowed on mounts that
  1022. * don't support it
  1023. */
  1024. if (nfsd4_spo_must_allow(rqstp))
  1025. return nfs_ok;
  1026. /* Some calls may be processed without authentication
  1027. * on GSS exports. For example NFS2/3 calls on root
  1028. * directory, see section 2.3.2 of rfc 2623.
  1029. * For "may_bypass_gss" check that export has really
  1030. * enabled some flavor with authentication (GSS or any
  1031. * other) and also check that the used auth flavor is
  1032. * without authentication (none or sys).
  1033. */
  1034. if (may_bypass_gss && (
  1035. rqstp->rq_cred.cr_flavor == RPC_AUTH_NULL ||
  1036. rqstp->rq_cred.cr_flavor == RPC_AUTH_UNIX)) {
  1037. for (f = exp->ex_flavors; f < end; f++) {
  1038. if (f->pseudoflavor >= RPC_AUTH_DES)
  1039. return 0;
  1040. }
  1041. }
  1042. return nfserr_wrongsec;
  1043. }
  1044. /**
  1045. * check_nfsd_access - check if access to export is allowed.
  1046. * @exp: svc_export that is being accessed.
  1047. * @rqstp: svc_rqst attempting to access @exp.
  1048. * @may_bypass_gss: reduce strictness of authorization check
  1049. *
  1050. * Return values:
  1051. * %nfs_ok if access is granted, or
  1052. * %nfserr_wrongsec if access is denied
  1053. */
  1054. __be32 check_nfsd_access(struct svc_export *exp, struct svc_rqst *rqstp,
  1055. bool may_bypass_gss)
  1056. {
  1057. __be32 status;
  1058. status = check_xprtsec_policy(exp, rqstp);
  1059. if (status != nfs_ok)
  1060. return status;
  1061. return check_security_flavor(exp, rqstp, may_bypass_gss);
  1062. }
  1063. /*
  1064. * Uses rq_client and rq_gssclient to find an export; uses rq_client (an
  1065. * auth_unix client) if it's available and has secinfo information;
  1066. * otherwise, will try to use rq_gssclient.
  1067. *
  1068. * Called from functions that handle requests; functions that do work on
  1069. * behalf of mountd are passed a single client name to use, and should
  1070. * use exp_get_by_name() or exp_find().
  1071. */
  1072. struct svc_export *
  1073. rqst_exp_get_by_name(struct svc_rqst *rqstp, const struct path *path)
  1074. {
  1075. struct svc_export *gssexp, *exp = ERR_PTR(-ENOENT);
  1076. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1077. struct cache_detail *cd = nn->svc_export_cache;
  1078. if (rqstp->rq_client == NULL)
  1079. goto gss;
  1080. /* First try the auth_unix client: */
  1081. exp = exp_get_by_name(cd, rqstp->rq_client, path, &rqstp->rq_chandle);
  1082. if (PTR_ERR(exp) == -ENOENT)
  1083. goto gss;
  1084. if (IS_ERR(exp))
  1085. return exp;
  1086. /* If it has secinfo, assume there are no gss/... clients */
  1087. if (exp->ex_nflavors > 0)
  1088. return exp;
  1089. gss:
  1090. /* Otherwise, try falling back on gss client */
  1091. if (rqstp->rq_gssclient == NULL)
  1092. return exp;
  1093. gssexp = exp_get_by_name(cd, rqstp->rq_gssclient, path, &rqstp->rq_chandle);
  1094. if (PTR_ERR(gssexp) == -ENOENT)
  1095. return exp;
  1096. if (!IS_ERR(exp))
  1097. exp_put(exp);
  1098. return gssexp;
  1099. }
  1100. /**
  1101. * rqst_exp_find - Find an svc_export in the context of a rqst or similar
  1102. * @reqp: The handle to be used to suspend the request if a cache-upcall is needed
  1103. * If NULL, missing in-cache information will result in failure.
  1104. * @net: The network namespace in which the request exists
  1105. * @cl: default auth_domain to use for looking up the export
  1106. * @gsscl: an alternate auth_domain defined using deprecated gss/krb5 format.
  1107. * @fsid_type: The type of fsid to look for
  1108. * @fsidv: The actual fsid to look up in the context of either client.
  1109. *
  1110. * Perform a lookup for @cl/@fsidv in the given @net for an export. If
  1111. * none found and @gsscl specified, repeat the lookup.
  1112. *
  1113. * Returns an export, or an error pointer.
  1114. */
  1115. struct svc_export *
  1116. rqst_exp_find(struct cache_req *reqp, struct net *net,
  1117. struct auth_domain *cl, struct auth_domain *gsscl,
  1118. int fsid_type, u32 *fsidv)
  1119. {
  1120. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1121. struct svc_export *gssexp, *exp = ERR_PTR(-ENOENT);
  1122. struct cache_detail *cd = nn->svc_export_cache;
  1123. if (!cl)
  1124. goto gss;
  1125. /* First try the auth_unix client: */
  1126. exp = exp_find(cd, cl, fsid_type, fsidv, reqp);
  1127. if (PTR_ERR(exp) == -ENOENT)
  1128. goto gss;
  1129. if (IS_ERR(exp))
  1130. return exp;
  1131. /* If it has secinfo, assume there are no gss/... clients */
  1132. if (exp->ex_nflavors > 0)
  1133. return exp;
  1134. gss:
  1135. /* Otherwise, try falling back on gss client */
  1136. if (!gsscl)
  1137. return exp;
  1138. gssexp = exp_find(cd, gsscl, fsid_type, fsidv, reqp);
  1139. if (PTR_ERR(gssexp) == -ENOENT)
  1140. return exp;
  1141. if (!IS_ERR(exp))
  1142. exp_put(exp);
  1143. return gssexp;
  1144. }
  1145. struct svc_export *
  1146. rqst_exp_parent(struct svc_rqst *rqstp, struct path *path)
  1147. {
  1148. struct dentry *saved = dget(path->dentry);
  1149. struct svc_export *exp = rqst_exp_get_by_name(rqstp, path);
  1150. while (PTR_ERR(exp) == -ENOENT && !IS_ROOT(path->dentry)) {
  1151. struct dentry *parent = dget_parent(path->dentry);
  1152. dput(path->dentry);
  1153. path->dentry = parent;
  1154. exp = rqst_exp_get_by_name(rqstp, path);
  1155. }
  1156. dput(path->dentry);
  1157. path->dentry = saved;
  1158. return exp;
  1159. }
  1160. struct svc_export *rqst_find_fsidzero_export(struct svc_rqst *rqstp)
  1161. {
  1162. u32 fsidv[2];
  1163. mk_fsid(FSID_NUM, fsidv, 0, 0, 0, NULL);
  1164. return rqst_exp_find(&rqstp->rq_chandle, SVC_NET(rqstp),
  1165. rqstp->rq_client, rqstp->rq_gssclient,
  1166. FSID_NUM, fsidv);
  1167. }
  1168. /*
  1169. * Called when we need the filehandle for the root of the pseudofs,
  1170. * for a given NFSv4 client. The root is defined to be the
  1171. * export point with fsid==0
  1172. */
  1173. __be32
  1174. exp_pseudoroot(struct svc_rqst *rqstp, struct svc_fh *fhp)
  1175. {
  1176. struct svc_export *exp;
  1177. __be32 rv;
  1178. exp = rqst_find_fsidzero_export(rqstp);
  1179. if (IS_ERR(exp))
  1180. return nfserrno(PTR_ERR(exp));
  1181. rv = fh_compose(fhp, exp, exp->ex_path.dentry, NULL);
  1182. exp_put(exp);
  1183. return rv;
  1184. }
  1185. static struct flags {
  1186. int flag;
  1187. char *name[2];
  1188. } expflags[] = {
  1189. { NFSEXP_READONLY, {"ro", "rw"}},
  1190. { NFSEXP_INSECURE_PORT, {"insecure", ""}},
  1191. { NFSEXP_ROOTSQUASH, {"root_squash", "no_root_squash"}},
  1192. { NFSEXP_ALLSQUASH, {"all_squash", ""}},
  1193. { NFSEXP_ASYNC, {"async", "sync"}},
  1194. { NFSEXP_GATHERED_WRITES, {"wdelay", "no_wdelay"}},
  1195. { NFSEXP_NOREADDIRPLUS, {"nordirplus", ""}},
  1196. { NFSEXP_NOHIDE, {"nohide", ""}},
  1197. { NFSEXP_CROSSMOUNT, {"crossmnt", ""}},
  1198. { NFSEXP_NOSUBTREECHECK, {"no_subtree_check", ""}},
  1199. { NFSEXP_NOAUTHNLM, {"insecure_locks", ""}},
  1200. { NFSEXP_V4ROOT, {"v4root", ""}},
  1201. { NFSEXP_PNFS, {"pnfs", ""}},
  1202. { NFSEXP_SECURITY_LABEL, {"security_label", ""}},
  1203. { 0, {"", ""}}
  1204. };
  1205. static void show_expflags(struct seq_file *m, int flags, int mask)
  1206. {
  1207. struct flags *flg;
  1208. int state, first = 0;
  1209. for (flg = expflags; flg->flag; flg++) {
  1210. if (flg->flag & ~mask)
  1211. continue;
  1212. state = (flg->flag & flags) ? 0 : 1;
  1213. if (*flg->name[state])
  1214. seq_printf(m, "%s%s", first++?",":"", flg->name[state]);
  1215. }
  1216. }
  1217. static void show_secinfo_flags(struct seq_file *m, int flags)
  1218. {
  1219. seq_printf(m, ",");
  1220. show_expflags(m, flags, NFSEXP_SECINFO_FLAGS);
  1221. }
  1222. static bool secinfo_flags_equal(int f, int g)
  1223. {
  1224. f &= NFSEXP_SECINFO_FLAGS;
  1225. g &= NFSEXP_SECINFO_FLAGS;
  1226. return f == g;
  1227. }
  1228. static int show_secinfo_run(struct seq_file *m, struct exp_flavor_info **fp, struct exp_flavor_info *end)
  1229. {
  1230. int flags;
  1231. flags = (*fp)->flags;
  1232. seq_printf(m, ",sec=%d", (*fp)->pseudoflavor);
  1233. (*fp)++;
  1234. while (*fp != end && secinfo_flags_equal(flags, (*fp)->flags)) {
  1235. seq_printf(m, ":%d", (*fp)->pseudoflavor);
  1236. (*fp)++;
  1237. }
  1238. return flags;
  1239. }
  1240. static void show_secinfo(struct seq_file *m, struct svc_export *exp)
  1241. {
  1242. struct exp_flavor_info *f;
  1243. struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;
  1244. int flags;
  1245. if (exp->ex_nflavors == 0)
  1246. return;
  1247. f = exp->ex_flavors;
  1248. flags = show_secinfo_run(m, &f, end);
  1249. if (!secinfo_flags_equal(flags, exp->ex_flags))
  1250. show_secinfo_flags(m, flags);
  1251. while (f != end) {
  1252. flags = show_secinfo_run(m, &f, end);
  1253. show_secinfo_flags(m, flags);
  1254. }
  1255. }
  1256. static void exp_flags(struct seq_file *m, int flag, int fsid,
  1257. kuid_t anonu, kgid_t anong, struct nfsd4_fs_locations *fsloc)
  1258. {
  1259. struct user_namespace *userns = m->file->f_cred->user_ns;
  1260. show_expflags(m, flag, NFSEXP_ALLFLAGS);
  1261. if (flag & NFSEXP_FSID)
  1262. seq_printf(m, ",fsid=%d", fsid);
  1263. if (!uid_eq(anonu, make_kuid(userns, (uid_t)-2)) &&
  1264. !uid_eq(anonu, make_kuid(userns, 0x10000-2)))
  1265. seq_printf(m, ",anonuid=%u", from_kuid_munged(userns, anonu));
  1266. if (!gid_eq(anong, make_kgid(userns, (gid_t)-2)) &&
  1267. !gid_eq(anong, make_kgid(userns, 0x10000-2)))
  1268. seq_printf(m, ",anongid=%u", from_kgid_munged(userns, anong));
  1269. if (fsloc && fsloc->locations_count > 0) {
  1270. char *loctype = (fsloc->migrated) ? "refer" : "replicas";
  1271. int i;
  1272. seq_printf(m, ",%s=", loctype);
  1273. seq_escape(m, fsloc->locations[0].path, ",;@ \t\n\\");
  1274. seq_putc(m, '@');
  1275. seq_escape(m, fsloc->locations[0].hosts, ",;@ \t\n\\");
  1276. for (i = 1; i < fsloc->locations_count; i++) {
  1277. seq_putc(m, ';');
  1278. seq_escape(m, fsloc->locations[i].path, ",;@ \t\n\\");
  1279. seq_putc(m, '@');
  1280. seq_escape(m, fsloc->locations[i].hosts, ",;@ \t\n\\");
  1281. }
  1282. }
  1283. }
  1284. static int e_show(struct seq_file *m, void *p)
  1285. {
  1286. struct cache_head *cp = p;
  1287. struct svc_export *exp = container_of(cp, struct svc_export, h);
  1288. struct cache_detail *cd = m->private;
  1289. bool export_stats = is_export_stats_file(m);
  1290. if (p == SEQ_START_TOKEN) {
  1291. seq_puts(m, "# Version 1.1\n");
  1292. if (export_stats)
  1293. seq_puts(m, "# Path Client Start-time\n#\tStats\n");
  1294. else
  1295. seq_puts(m, "# Path Client(Flags) # IPs\n");
  1296. return 0;
  1297. }
  1298. if (cache_check_rcu(cd, &exp->h, NULL))
  1299. return 0;
  1300. return svc_export_show(m, cd, cp);
  1301. }
  1302. const struct seq_operations nfs_exports_op = {
  1303. .start = cache_seq_start_rcu,
  1304. .next = cache_seq_next_rcu,
  1305. .stop = cache_seq_stop_rcu,
  1306. .show = e_show,
  1307. };
  1308. /**
  1309. * nfsd_export_wq_init - allocate the export release workqueue
  1310. *
  1311. * Called once at module load. The workqueue runs deferred svc_export and
  1312. * svc_expkey release work scheduled by queue_rcu_work() in the cache put
  1313. * callbacks.
  1314. *
  1315. * Return values:
  1316. * %0: workqueue allocated
  1317. * %-ENOMEM: allocation failed
  1318. */
  1319. int nfsd_export_wq_init(void)
  1320. {
  1321. nfsd_export_wq = alloc_workqueue("nfsd_export", WQ_UNBOUND, 0);
  1322. if (!nfsd_export_wq)
  1323. return -ENOMEM;
  1324. return 0;
  1325. }
  1326. /**
  1327. * nfsd_export_wq_shutdown - drain and free the export release workqueue
  1328. *
  1329. * Called once at module unload. Per-namespace teardown in
  1330. * nfsd_export_shutdown() has already drained all deferred work.
  1331. */
  1332. void nfsd_export_wq_shutdown(void)
  1333. {
  1334. destroy_workqueue(nfsd_export_wq);
  1335. }
  1336. /*
  1337. * Initialize the exports module.
  1338. */
  1339. int
  1340. nfsd_export_init(struct net *net)
  1341. {
  1342. int rv;
  1343. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1344. dprintk("nfsd: initializing export module (net: %x).\n", net->ns.inum);
  1345. nn->svc_export_cache = cache_create_net(&svc_export_cache_template, net);
  1346. if (IS_ERR(nn->svc_export_cache))
  1347. return PTR_ERR(nn->svc_export_cache);
  1348. rv = cache_register_net(nn->svc_export_cache, net);
  1349. if (rv)
  1350. goto destroy_export_cache;
  1351. nn->svc_expkey_cache = cache_create_net(&svc_expkey_cache_template, net);
  1352. if (IS_ERR(nn->svc_expkey_cache)) {
  1353. rv = PTR_ERR(nn->svc_expkey_cache);
  1354. goto unregister_export_cache;
  1355. }
  1356. rv = cache_register_net(nn->svc_expkey_cache, net);
  1357. if (rv)
  1358. goto destroy_expkey_cache;
  1359. return 0;
  1360. destroy_expkey_cache:
  1361. cache_destroy_net(nn->svc_expkey_cache, net);
  1362. unregister_export_cache:
  1363. cache_unregister_net(nn->svc_export_cache, net);
  1364. destroy_export_cache:
  1365. cache_destroy_net(nn->svc_export_cache, net);
  1366. return rv;
  1367. }
  1368. /*
  1369. * Flush exports table - called when last nfsd thread is killed
  1370. */
  1371. void
  1372. nfsd_export_flush(struct net *net)
  1373. {
  1374. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1375. cache_purge(nn->svc_expkey_cache);
  1376. cache_purge(nn->svc_export_cache);
  1377. }
  1378. /*
  1379. * Shutdown the exports module.
  1380. */
  1381. void
  1382. nfsd_export_shutdown(struct net *net)
  1383. {
  1384. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1385. dprintk("nfsd: shutting down export module (net: %x).\n", net->ns.inum);
  1386. cache_unregister_net(nn->svc_expkey_cache, net);
  1387. cache_unregister_net(nn->svc_export_cache, net);
  1388. /* Drain deferred export and expkey release work. */
  1389. rcu_barrier();
  1390. flush_workqueue(nfsd_export_wq);
  1391. cache_destroy_net(nn->svc_expkey_cache, net);
  1392. cache_destroy_net(nn->svc_export_cache, net);
  1393. svcauth_unix_purge(net);
  1394. dprintk("nfsd: export shutdown complete (net: %x).\n", net->ns.inum);
  1395. }