export.c 39 KB

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