super.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/nfs/super.c
  4. *
  5. * Copyright (C) 1992 Rick Sladkey
  6. *
  7. * nfs superblock handling functions
  8. *
  9. * Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
  10. * experimental NFS changes. Modularisation taken straight from SYS5 fs.
  11. *
  12. * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
  13. * J.S.Peatfield@damtp.cam.ac.uk
  14. *
  15. * Split from inode.c by David Howells <dhowells@redhat.com>
  16. *
  17. * - superblocks are indexed on server only - all inodes, dentries, etc. associated with a
  18. * particular server are held in the same superblock
  19. * - NFS superblocks can have several effective roots to the dentry tree
  20. * - directory type roots are spliced into the tree when a path from one root reaches the root
  21. * of another (see nfs_lookup())
  22. */
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/time.h>
  26. #include <linux/kernel.h>
  27. #include <linux/mm.h>
  28. #include <linux/string.h>
  29. #include <linux/stat.h>
  30. #include <linux/errno.h>
  31. #include <linux/unistd.h>
  32. #include <linux/sunrpc/clnt.h>
  33. #include <linux/sunrpc/addr.h>
  34. #include <linux/sunrpc/stats.h>
  35. #include <linux/sunrpc/metrics.h>
  36. #include <linux/sunrpc/xprtsock.h>
  37. #include <linux/sunrpc/xprtrdma.h>
  38. #include <linux/nfs_fs.h>
  39. #include <linux/nfs_mount.h>
  40. #include <linux/nfs4_mount.h>
  41. #include <linux/lockd/bind.h>
  42. #include <linux/seq_file.h>
  43. #include <linux/mount.h>
  44. #include <linux/namei.h>
  45. #include <linux/vfs.h>
  46. #include <linux/inet.h>
  47. #include <linux/in6.h>
  48. #include <linux/slab.h>
  49. #include <net/ipv6.h>
  50. #include <linux/netdevice.h>
  51. #include <linux/nfs_xdr.h>
  52. #include <linux/magic.h>
  53. #include <linux/parser.h>
  54. #include <linux/nsproxy.h>
  55. #include <linux/rcupdate.h>
  56. #include <linux/uaccess.h>
  57. #include <linux/nfs_ssc.h>
  58. #include "nfs4_fs.h"
  59. #include "callback.h"
  60. #include "delegation.h"
  61. #include "iostat.h"
  62. #include "internal.h"
  63. #include "fscache.h"
  64. #include "nfs4session.h"
  65. #include "pnfs.h"
  66. #include "nfs.h"
  67. #define NFSDBG_FACILITY NFSDBG_VFS
  68. const struct super_operations nfs_sops = {
  69. .alloc_inode = nfs_alloc_inode,
  70. .free_inode = nfs_free_inode,
  71. .write_inode = nfs_write_inode,
  72. .drop_inode = nfs_drop_inode,
  73. .statfs = nfs_statfs,
  74. .evict_inode = nfs_evict_inode,
  75. .umount_begin = nfs_umount_begin,
  76. .show_options = nfs_show_options,
  77. .show_devname = nfs_show_devname,
  78. .show_path = nfs_show_path,
  79. .show_stats = nfs_show_stats,
  80. };
  81. EXPORT_SYMBOL_GPL(nfs_sops);
  82. static const struct nfs_ssc_client_ops nfs_ssc_clnt_ops_tbl = {
  83. .sco_sb_deactive = nfs_sb_deactive,
  84. };
  85. #if IS_ENABLED(CONFIG_NFS_V4)
  86. static int __init register_nfs4_fs(void)
  87. {
  88. return register_filesystem(&nfs4_fs_type);
  89. }
  90. static void unregister_nfs4_fs(void)
  91. {
  92. unregister_filesystem(&nfs4_fs_type);
  93. }
  94. #else
  95. static int __init register_nfs4_fs(void)
  96. {
  97. return 0;
  98. }
  99. static void unregister_nfs4_fs(void)
  100. {
  101. }
  102. #endif
  103. static void nfs_ssc_register_ops(void)
  104. {
  105. nfs_ssc_register(&nfs_ssc_clnt_ops_tbl);
  106. }
  107. static void nfs_ssc_unregister_ops(void)
  108. {
  109. nfs_ssc_unregister(&nfs_ssc_clnt_ops_tbl);
  110. }
  111. static struct shrinker acl_shrinker = {
  112. .count_objects = nfs_access_cache_count,
  113. .scan_objects = nfs_access_cache_scan,
  114. .seeks = DEFAULT_SEEKS,
  115. };
  116. /*
  117. * Register the NFS filesystems
  118. */
  119. int __init register_nfs_fs(void)
  120. {
  121. int ret;
  122. ret = register_filesystem(&nfs_fs_type);
  123. if (ret < 0)
  124. goto error_0;
  125. ret = register_nfs4_fs();
  126. if (ret < 0)
  127. goto error_1;
  128. ret = nfs_register_sysctl();
  129. if (ret < 0)
  130. goto error_2;
  131. ret = register_shrinker(&acl_shrinker);
  132. if (ret < 0)
  133. goto error_3;
  134. nfs_ssc_register_ops();
  135. return 0;
  136. error_3:
  137. nfs_unregister_sysctl();
  138. error_2:
  139. unregister_nfs4_fs();
  140. error_1:
  141. unregister_filesystem(&nfs_fs_type);
  142. error_0:
  143. return ret;
  144. }
  145. /*
  146. * Unregister the NFS filesystems
  147. */
  148. void __exit unregister_nfs_fs(void)
  149. {
  150. unregister_shrinker(&acl_shrinker);
  151. nfs_unregister_sysctl();
  152. unregister_nfs4_fs();
  153. nfs_ssc_unregister_ops();
  154. unregister_filesystem(&nfs_fs_type);
  155. }
  156. bool nfs_sb_active(struct super_block *sb)
  157. {
  158. struct nfs_server *server = NFS_SB(sb);
  159. if (!atomic_inc_not_zero(&sb->s_active))
  160. return false;
  161. if (atomic_inc_return(&server->active) != 1)
  162. atomic_dec(&sb->s_active);
  163. return true;
  164. }
  165. EXPORT_SYMBOL_GPL(nfs_sb_active);
  166. void nfs_sb_deactive(struct super_block *sb)
  167. {
  168. struct nfs_server *server = NFS_SB(sb);
  169. if (atomic_dec_and_test(&server->active))
  170. deactivate_super(sb);
  171. }
  172. EXPORT_SYMBOL_GPL(nfs_sb_deactive);
  173. static int __nfs_list_for_each_server(struct list_head *head,
  174. int (*fn)(struct nfs_server *, void *),
  175. void *data)
  176. {
  177. struct nfs_server *server, *last = NULL;
  178. int ret = 0;
  179. rcu_read_lock();
  180. list_for_each_entry_rcu(server, head, client_link) {
  181. if (!(server->super && nfs_sb_active(server->super)))
  182. continue;
  183. rcu_read_unlock();
  184. if (last)
  185. nfs_sb_deactive(last->super);
  186. last = server;
  187. ret = fn(server, data);
  188. if (ret)
  189. goto out;
  190. rcu_read_lock();
  191. }
  192. rcu_read_unlock();
  193. out:
  194. if (last)
  195. nfs_sb_deactive(last->super);
  196. return ret;
  197. }
  198. int nfs_client_for_each_server(struct nfs_client *clp,
  199. int (*fn)(struct nfs_server *, void *),
  200. void *data)
  201. {
  202. return __nfs_list_for_each_server(&clp->cl_superblocks, fn, data);
  203. }
  204. EXPORT_SYMBOL_GPL(nfs_client_for_each_server);
  205. /*
  206. * Deliver file system statistics to userspace
  207. */
  208. int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  209. {
  210. struct nfs_server *server = NFS_SB(dentry->d_sb);
  211. unsigned char blockbits;
  212. unsigned long blockres;
  213. struct nfs_fh *fh = NFS_FH(d_inode(dentry));
  214. struct nfs_fsstat res;
  215. int error = -ENOMEM;
  216. res.fattr = nfs_alloc_fattr();
  217. if (res.fattr == NULL)
  218. goto out_err;
  219. error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
  220. if (unlikely(error == -ESTALE)) {
  221. struct dentry *pd_dentry;
  222. pd_dentry = dget_parent(dentry);
  223. nfs_zap_caches(d_inode(pd_dentry));
  224. dput(pd_dentry);
  225. }
  226. nfs_free_fattr(res.fattr);
  227. if (error < 0)
  228. goto out_err;
  229. buf->f_type = NFS_SUPER_MAGIC;
  230. /*
  231. * Current versions of glibc do not correctly handle the
  232. * case where f_frsize != f_bsize. Eventually we want to
  233. * report the value of wtmult in this field.
  234. */
  235. buf->f_frsize = dentry->d_sb->s_blocksize;
  236. /*
  237. * On most *nix systems, f_blocks, f_bfree, and f_bavail
  238. * are reported in units of f_frsize. Linux hasn't had
  239. * an f_frsize field in its statfs struct until recently,
  240. * thus historically Linux's sys_statfs reports these
  241. * fields in units of f_bsize.
  242. */
  243. buf->f_bsize = dentry->d_sb->s_blocksize;
  244. blockbits = dentry->d_sb->s_blocksize_bits;
  245. blockres = (1 << blockbits) - 1;
  246. buf->f_blocks = (res.tbytes + blockres) >> blockbits;
  247. buf->f_bfree = (res.fbytes + blockres) >> blockbits;
  248. buf->f_bavail = (res.abytes + blockres) >> blockbits;
  249. buf->f_files = res.tfiles;
  250. buf->f_ffree = res.afiles;
  251. buf->f_namelen = server->namelen;
  252. return 0;
  253. out_err:
  254. dprintk("%s: statfs error = %d\n", __func__, -error);
  255. return error;
  256. }
  257. EXPORT_SYMBOL_GPL(nfs_statfs);
  258. /*
  259. * Map the security flavour number to a name
  260. */
  261. static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
  262. {
  263. static const struct {
  264. rpc_authflavor_t flavour;
  265. const char *str;
  266. } sec_flavours[NFS_AUTH_INFO_MAX_FLAVORS] = {
  267. /* update NFS_AUTH_INFO_MAX_FLAVORS when this list changes! */
  268. { RPC_AUTH_NULL, "null" },
  269. { RPC_AUTH_UNIX, "sys" },
  270. { RPC_AUTH_GSS_KRB5, "krb5" },
  271. { RPC_AUTH_GSS_KRB5I, "krb5i" },
  272. { RPC_AUTH_GSS_KRB5P, "krb5p" },
  273. { RPC_AUTH_GSS_LKEY, "lkey" },
  274. { RPC_AUTH_GSS_LKEYI, "lkeyi" },
  275. { RPC_AUTH_GSS_LKEYP, "lkeyp" },
  276. { RPC_AUTH_GSS_SPKM, "spkm" },
  277. { RPC_AUTH_GSS_SPKMI, "spkmi" },
  278. { RPC_AUTH_GSS_SPKMP, "spkmp" },
  279. { UINT_MAX, "unknown" }
  280. };
  281. int i;
  282. for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
  283. if (sec_flavours[i].flavour == flavour)
  284. break;
  285. }
  286. return sec_flavours[i].str;
  287. }
  288. static void nfs_show_mountd_netid(struct seq_file *m, struct nfs_server *nfss,
  289. int showdefaults)
  290. {
  291. struct sockaddr *sap = (struct sockaddr *) &nfss->mountd_address;
  292. char *proto = NULL;
  293. switch (sap->sa_family) {
  294. case AF_INET:
  295. switch (nfss->mountd_protocol) {
  296. case IPPROTO_UDP:
  297. proto = RPCBIND_NETID_UDP;
  298. break;
  299. case IPPROTO_TCP:
  300. proto = RPCBIND_NETID_TCP;
  301. break;
  302. }
  303. break;
  304. case AF_INET6:
  305. switch (nfss->mountd_protocol) {
  306. case IPPROTO_UDP:
  307. proto = RPCBIND_NETID_UDP6;
  308. break;
  309. case IPPROTO_TCP:
  310. proto = RPCBIND_NETID_TCP6;
  311. break;
  312. }
  313. break;
  314. }
  315. if (proto || showdefaults)
  316. seq_printf(m, ",mountproto=%s", proto ?: "auto");
  317. }
  318. static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
  319. int showdefaults)
  320. {
  321. struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
  322. if (nfss->flags & NFS_MOUNT_LEGACY_INTERFACE)
  323. return;
  324. switch (sap->sa_family) {
  325. case AF_INET: {
  326. struct sockaddr_in *sin = (struct sockaddr_in *)sap;
  327. seq_printf(m, ",mountaddr=%pI4", &sin->sin_addr.s_addr);
  328. break;
  329. }
  330. case AF_INET6: {
  331. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
  332. seq_printf(m, ",mountaddr=%pI6c", &sin6->sin6_addr);
  333. break;
  334. }
  335. default:
  336. if (showdefaults)
  337. seq_puts(m, ",mountaddr=unspecified");
  338. }
  339. if (nfss->mountd_version || showdefaults)
  340. seq_printf(m, ",mountvers=%u", nfss->mountd_version);
  341. if ((nfss->mountd_port &&
  342. nfss->mountd_port != (unsigned short)NFS_UNSPEC_PORT) ||
  343. showdefaults)
  344. seq_printf(m, ",mountport=%u", nfss->mountd_port);
  345. nfs_show_mountd_netid(m, nfss, showdefaults);
  346. }
  347. #if IS_ENABLED(CONFIG_NFS_V4)
  348. static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
  349. int showdefaults)
  350. {
  351. struct nfs_client *clp = nfss->nfs_client;
  352. seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
  353. }
  354. #else
  355. static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
  356. int showdefaults)
  357. {
  358. }
  359. #endif
  360. static void nfs_show_nfs_version(struct seq_file *m,
  361. unsigned int version,
  362. unsigned int minorversion)
  363. {
  364. seq_printf(m, ",vers=%u", version);
  365. if (version == 4)
  366. seq_printf(m, ".%u", minorversion);
  367. }
  368. /*
  369. * Describe the mount options in force on this server representation
  370. */
  371. static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
  372. int showdefaults)
  373. {
  374. static const struct proc_nfs_info {
  375. int flag;
  376. const char *str;
  377. const char *nostr;
  378. } nfs_info[] = {
  379. { NFS_MOUNT_SOFT, ",soft", "" },
  380. { NFS_MOUNT_SOFTERR, ",softerr", "" },
  381. { NFS_MOUNT_SOFTREVAL, ",softreval", "" },
  382. { NFS_MOUNT_POSIX, ",posix", "" },
  383. { NFS_MOUNT_NOCTO, ",nocto", "" },
  384. { NFS_MOUNT_NOAC, ",noac", "" },
  385. { NFS_MOUNT_NONLM, ",nolock", "" },
  386. { NFS_MOUNT_NOACL, ",noacl", "" },
  387. { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
  388. { NFS_MOUNT_UNSHARED, ",nosharecache", "" },
  389. { NFS_MOUNT_NORESVPORT, ",noresvport", "" },
  390. { 0, NULL, NULL }
  391. };
  392. const struct proc_nfs_info *nfs_infop;
  393. struct nfs_client *clp = nfss->nfs_client;
  394. u32 version = clp->rpc_ops->version;
  395. int local_flock, local_fcntl;
  396. nfs_show_nfs_version(m, version, clp->cl_minorversion);
  397. seq_printf(m, ",rsize=%u", nfss->rsize);
  398. seq_printf(m, ",wsize=%u", nfss->wsize);
  399. if (nfss->bsize != 0)
  400. seq_printf(m, ",bsize=%u", nfss->bsize);
  401. seq_printf(m, ",namlen=%u", nfss->namelen);
  402. if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
  403. seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
  404. if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
  405. seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
  406. if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
  407. seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
  408. if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
  409. seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
  410. if (!(nfss->flags & (NFS_MOUNT_SOFT|NFS_MOUNT_SOFTERR)))
  411. seq_puts(m, ",hard");
  412. for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
  413. if (nfss->flags & nfs_infop->flag)
  414. seq_puts(m, nfs_infop->str);
  415. else
  416. seq_puts(m, nfs_infop->nostr);
  417. }
  418. rcu_read_lock();
  419. seq_printf(m, ",proto=%s",
  420. rpc_peeraddr2str(nfss->client, RPC_DISPLAY_NETID));
  421. rcu_read_unlock();
  422. if (clp->cl_nconnect > 0)
  423. seq_printf(m, ",nconnect=%u", clp->cl_nconnect);
  424. if (version == 4) {
  425. if (nfss->port != NFS_PORT)
  426. seq_printf(m, ",port=%u", nfss->port);
  427. } else
  428. if (nfss->port)
  429. seq_printf(m, ",port=%u", nfss->port);
  430. seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
  431. seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
  432. seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
  433. if (version != 4)
  434. nfs_show_mountd_options(m, nfss, showdefaults);
  435. else
  436. nfs_show_nfsv4_options(m, nfss, showdefaults);
  437. if (nfss->options & NFS_OPTION_FSCACHE)
  438. seq_puts(m, ",fsc");
  439. if (nfss->options & NFS_OPTION_MIGRATION)
  440. seq_puts(m, ",migration");
  441. if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) {
  442. if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
  443. seq_puts(m, ",lookupcache=none");
  444. else
  445. seq_puts(m, ",lookupcache=pos");
  446. }
  447. local_flock = nfss->flags & NFS_MOUNT_LOCAL_FLOCK;
  448. local_fcntl = nfss->flags & NFS_MOUNT_LOCAL_FCNTL;
  449. if (!local_flock && !local_fcntl)
  450. seq_puts(m, ",local_lock=none");
  451. else if (local_flock && local_fcntl)
  452. seq_puts(m, ",local_lock=all");
  453. else if (local_flock)
  454. seq_puts(m, ",local_lock=flock");
  455. else
  456. seq_puts(m, ",local_lock=posix");
  457. }
  458. /*
  459. * Describe the mount options on this VFS mountpoint
  460. */
  461. int nfs_show_options(struct seq_file *m, struct dentry *root)
  462. {
  463. struct nfs_server *nfss = NFS_SB(root->d_sb);
  464. nfs_show_mount_options(m, nfss, 0);
  465. rcu_read_lock();
  466. seq_printf(m, ",addr=%s",
  467. rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
  468. RPC_DISPLAY_ADDR));
  469. rcu_read_unlock();
  470. return 0;
  471. }
  472. EXPORT_SYMBOL_GPL(nfs_show_options);
  473. #if IS_ENABLED(CONFIG_NFS_V4)
  474. static void show_lease(struct seq_file *m, struct nfs_server *server)
  475. {
  476. struct nfs_client *clp = server->nfs_client;
  477. unsigned long expire;
  478. seq_printf(m, ",lease_time=%ld", clp->cl_lease_time / HZ);
  479. expire = clp->cl_last_renewal + clp->cl_lease_time;
  480. seq_printf(m, ",lease_expired=%ld",
  481. time_after(expire, jiffies) ? 0 : (jiffies - expire) / HZ);
  482. }
  483. #ifdef CONFIG_NFS_V4_1
  484. static void show_sessions(struct seq_file *m, struct nfs_server *server)
  485. {
  486. if (nfs4_has_session(server->nfs_client))
  487. seq_puts(m, ",sessions");
  488. }
  489. #else
  490. static void show_sessions(struct seq_file *m, struct nfs_server *server) {}
  491. #endif
  492. #endif
  493. #ifdef CONFIG_NFS_V4_1
  494. static void show_pnfs(struct seq_file *m, struct nfs_server *server)
  495. {
  496. seq_printf(m, ",pnfs=");
  497. if (server->pnfs_curr_ld)
  498. seq_printf(m, "%s", server->pnfs_curr_ld->name);
  499. else
  500. seq_printf(m, "not configured");
  501. }
  502. static void show_implementation_id(struct seq_file *m, struct nfs_server *nfss)
  503. {
  504. if (nfss->nfs_client && nfss->nfs_client->cl_implid) {
  505. struct nfs41_impl_id *impl_id = nfss->nfs_client->cl_implid;
  506. seq_printf(m, "\n\timpl_id:\tname='%s',domain='%s',"
  507. "date='%llu,%u'",
  508. impl_id->name, impl_id->domain,
  509. impl_id->date.seconds, impl_id->date.nseconds);
  510. }
  511. }
  512. #else
  513. #if IS_ENABLED(CONFIG_NFS_V4)
  514. static void show_pnfs(struct seq_file *m, struct nfs_server *server)
  515. {
  516. }
  517. #endif
  518. static void show_implementation_id(struct seq_file *m, struct nfs_server *nfss)
  519. {
  520. }
  521. #endif
  522. int nfs_show_devname(struct seq_file *m, struct dentry *root)
  523. {
  524. char *page = (char *) __get_free_page(GFP_KERNEL);
  525. char *devname, *dummy;
  526. int err = 0;
  527. if (!page)
  528. return -ENOMEM;
  529. devname = nfs_path(&dummy, root, page, PAGE_SIZE, 0);
  530. if (IS_ERR(devname))
  531. err = PTR_ERR(devname);
  532. else
  533. seq_escape(m, devname, " \t\n\\");
  534. free_page((unsigned long)page);
  535. return err;
  536. }
  537. EXPORT_SYMBOL_GPL(nfs_show_devname);
  538. int nfs_show_path(struct seq_file *m, struct dentry *dentry)
  539. {
  540. seq_puts(m, "/");
  541. return 0;
  542. }
  543. EXPORT_SYMBOL_GPL(nfs_show_path);
  544. /*
  545. * Present statistical information for this VFS mountpoint
  546. */
  547. int nfs_show_stats(struct seq_file *m, struct dentry *root)
  548. {
  549. int i, cpu;
  550. struct nfs_server *nfss = NFS_SB(root->d_sb);
  551. struct rpc_auth *auth = nfss->client->cl_auth;
  552. struct nfs_iostats totals = { };
  553. seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
  554. /*
  555. * Display all mount option settings
  556. */
  557. seq_puts(m, "\n\topts:\t");
  558. seq_puts(m, sb_rdonly(root->d_sb) ? "ro" : "rw");
  559. seq_puts(m, root->d_sb->s_flags & SB_SYNCHRONOUS ? ",sync" : "");
  560. seq_puts(m, root->d_sb->s_flags & SB_NOATIME ? ",noatime" : "");
  561. seq_puts(m, root->d_sb->s_flags & SB_NODIRATIME ? ",nodiratime" : "");
  562. nfs_show_mount_options(m, nfss, 1);
  563. seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
  564. show_implementation_id(m, nfss);
  565. seq_puts(m, "\n\tcaps:\t");
  566. seq_printf(m, "caps=0x%x", nfss->caps);
  567. seq_printf(m, ",wtmult=%u", nfss->wtmult);
  568. seq_printf(m, ",dtsize=%u", nfss->dtsize);
  569. seq_printf(m, ",bsize=%u", nfss->bsize);
  570. seq_printf(m, ",namlen=%u", nfss->namelen);
  571. #if IS_ENABLED(CONFIG_NFS_V4)
  572. if (nfss->nfs_client->rpc_ops->version == 4) {
  573. seq_puts(m, "\n\tnfsv4:\t");
  574. seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
  575. seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
  576. seq_printf(m, ",bm2=0x%x", nfss->attr_bitmask[2]);
  577. seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
  578. show_sessions(m, nfss);
  579. show_pnfs(m, nfss);
  580. show_lease(m, nfss);
  581. }
  582. #endif
  583. /*
  584. * Display security flavor in effect for this mount
  585. */
  586. seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
  587. if (auth->au_flavor)
  588. seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
  589. /*
  590. * Display superblock I/O counters
  591. */
  592. for_each_possible_cpu(cpu) {
  593. struct nfs_iostats *stats;
  594. preempt_disable();
  595. stats = per_cpu_ptr(nfss->io_stats, cpu);
  596. for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
  597. totals.events[i] += stats->events[i];
  598. for (i = 0; i < __NFSIOS_BYTESMAX; i++)
  599. totals.bytes[i] += stats->bytes[i];
  600. #ifdef CONFIG_NFS_FSCACHE
  601. for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
  602. totals.fscache[i] += stats->fscache[i];
  603. #endif
  604. preempt_enable();
  605. }
  606. seq_puts(m, "\n\tevents:\t");
  607. for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
  608. seq_printf(m, "%lu ", totals.events[i]);
  609. seq_puts(m, "\n\tbytes:\t");
  610. for (i = 0; i < __NFSIOS_BYTESMAX; i++)
  611. seq_printf(m, "%Lu ", totals.bytes[i]);
  612. #ifdef CONFIG_NFS_FSCACHE
  613. if (nfss->options & NFS_OPTION_FSCACHE) {
  614. seq_puts(m, "\n\tfsc:\t");
  615. for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
  616. seq_printf(m, "%Lu ", totals.fscache[i]);
  617. }
  618. #endif
  619. seq_putc(m, '\n');
  620. rpc_clnt_show_stats(m, nfss->client);
  621. return 0;
  622. }
  623. EXPORT_SYMBOL_GPL(nfs_show_stats);
  624. /*
  625. * Begin unmount by attempting to remove all automounted mountpoints we added
  626. * in response to xdev traversals and referrals
  627. */
  628. void nfs_umount_begin(struct super_block *sb)
  629. {
  630. struct nfs_server *server;
  631. struct rpc_clnt *rpc;
  632. server = NFS_SB(sb);
  633. /* -EIO all pending I/O */
  634. rpc = server->client_acl;
  635. if (!IS_ERR(rpc))
  636. rpc_killall_tasks(rpc);
  637. rpc = server->client;
  638. if (!IS_ERR(rpc))
  639. rpc_killall_tasks(rpc);
  640. }
  641. EXPORT_SYMBOL_GPL(nfs_umount_begin);
  642. /*
  643. * Return true if 'match' is in auth_info or auth_info is empty.
  644. * Return false otherwise.
  645. */
  646. bool nfs_auth_info_match(const struct nfs_auth_info *auth_info,
  647. rpc_authflavor_t match)
  648. {
  649. int i;
  650. if (!auth_info->flavor_len)
  651. return true;
  652. for (i = 0; i < auth_info->flavor_len; i++) {
  653. if (auth_info->flavors[i] == match)
  654. return true;
  655. }
  656. return false;
  657. }
  658. EXPORT_SYMBOL_GPL(nfs_auth_info_match);
  659. /*
  660. * Ensure that a specified authtype in ctx->auth_info is supported by
  661. * the server. Returns 0 and sets ctx->selected_flavor if it's ok, and
  662. * -EACCES if not.
  663. */
  664. static int nfs_verify_authflavors(struct nfs_fs_context *ctx,
  665. rpc_authflavor_t *server_authlist,
  666. unsigned int count)
  667. {
  668. rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
  669. bool found_auth_null = false;
  670. unsigned int i;
  671. /*
  672. * If the sec= mount option is used, the specified flavor or AUTH_NULL
  673. * must be in the list returned by the server.
  674. *
  675. * AUTH_NULL has a special meaning when it's in the server list - it
  676. * means that the server will ignore the rpc creds, so any flavor
  677. * can be used but still use the sec= that was specified.
  678. *
  679. * Note also that the MNT procedure in MNTv1 does not return a list
  680. * of supported security flavors. In this case, nfs_mount() fabricates
  681. * a security flavor list containing just AUTH_NULL.
  682. */
  683. for (i = 0; i < count; i++) {
  684. flavor = server_authlist[i];
  685. if (nfs_auth_info_match(&ctx->auth_info, flavor))
  686. goto out;
  687. if (flavor == RPC_AUTH_NULL)
  688. found_auth_null = true;
  689. }
  690. if (found_auth_null) {
  691. flavor = ctx->auth_info.flavors[0];
  692. goto out;
  693. }
  694. dfprintk(MOUNT,
  695. "NFS: specified auth flavors not supported by server\n");
  696. return -EACCES;
  697. out:
  698. ctx->selected_flavor = flavor;
  699. dfprintk(MOUNT, "NFS: using auth flavor %u\n", ctx->selected_flavor);
  700. return 0;
  701. }
  702. /*
  703. * Use the remote server's MOUNT service to request the NFS file handle
  704. * corresponding to the provided path.
  705. */
  706. static int nfs_request_mount(struct fs_context *fc,
  707. struct nfs_fh *root_fh,
  708. rpc_authflavor_t *server_authlist,
  709. unsigned int *server_authlist_len)
  710. {
  711. struct nfs_fs_context *ctx = nfs_fc2context(fc);
  712. struct nfs_mount_request request = {
  713. .sap = (struct sockaddr *)
  714. &ctx->mount_server.address,
  715. .dirpath = ctx->nfs_server.export_path,
  716. .protocol = ctx->mount_server.protocol,
  717. .fh = root_fh,
  718. .noresvport = ctx->flags & NFS_MOUNT_NORESVPORT,
  719. .auth_flav_len = server_authlist_len,
  720. .auth_flavs = server_authlist,
  721. .net = fc->net_ns,
  722. };
  723. int status;
  724. if (ctx->mount_server.version == 0) {
  725. switch (ctx->version) {
  726. default:
  727. ctx->mount_server.version = NFS_MNT3_VERSION;
  728. break;
  729. case 2:
  730. ctx->mount_server.version = NFS_MNT_VERSION;
  731. }
  732. }
  733. request.version = ctx->mount_server.version;
  734. if (ctx->mount_server.hostname)
  735. request.hostname = ctx->mount_server.hostname;
  736. else
  737. request.hostname = ctx->nfs_server.hostname;
  738. /*
  739. * Construct the mount server's address.
  740. */
  741. if (ctx->mount_server.address.sa_family == AF_UNSPEC) {
  742. memcpy(request.sap, &ctx->nfs_server.address,
  743. ctx->nfs_server.addrlen);
  744. ctx->mount_server.addrlen = ctx->nfs_server.addrlen;
  745. }
  746. request.salen = ctx->mount_server.addrlen;
  747. nfs_set_port(request.sap, &ctx->mount_server.port, 0);
  748. /*
  749. * Now ask the mount server to map our export path
  750. * to a file handle.
  751. */
  752. status = nfs_mount(&request);
  753. if (status != 0) {
  754. dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
  755. request.hostname, status);
  756. return status;
  757. }
  758. return 0;
  759. }
  760. static struct nfs_server *nfs_try_mount_request(struct fs_context *fc)
  761. {
  762. struct nfs_fs_context *ctx = nfs_fc2context(fc);
  763. int status;
  764. unsigned int i;
  765. bool tried_auth_unix = false;
  766. bool auth_null_in_list = false;
  767. struct nfs_server *server = ERR_PTR(-EACCES);
  768. rpc_authflavor_t authlist[NFS_MAX_SECFLAVORS];
  769. unsigned int authlist_len = ARRAY_SIZE(authlist);
  770. status = nfs_request_mount(fc, ctx->mntfh, authlist, &authlist_len);
  771. if (status)
  772. return ERR_PTR(status);
  773. /*
  774. * Was a sec= authflavor specified in the options? First, verify
  775. * whether the server supports it, and then just try to use it if so.
  776. */
  777. if (ctx->auth_info.flavor_len > 0) {
  778. status = nfs_verify_authflavors(ctx, authlist, authlist_len);
  779. dfprintk(MOUNT, "NFS: using auth flavor %u\n",
  780. ctx->selected_flavor);
  781. if (status)
  782. return ERR_PTR(status);
  783. return ctx->nfs_mod->rpc_ops->create_server(fc);
  784. }
  785. /*
  786. * No sec= option was provided. RFC 2623, section 2.7 suggests we
  787. * SHOULD prefer the flavor listed first. However, some servers list
  788. * AUTH_NULL first. Avoid ever choosing AUTH_NULL.
  789. */
  790. for (i = 0; i < authlist_len; ++i) {
  791. rpc_authflavor_t flavor;
  792. struct rpcsec_gss_info info;
  793. flavor = authlist[i];
  794. switch (flavor) {
  795. case RPC_AUTH_UNIX:
  796. tried_auth_unix = true;
  797. break;
  798. case RPC_AUTH_NULL:
  799. auth_null_in_list = true;
  800. continue;
  801. default:
  802. if (rpcauth_get_gssinfo(flavor, &info) != 0)
  803. continue;
  804. break;
  805. }
  806. dfprintk(MOUNT, "NFS: attempting to use auth flavor %u\n", flavor);
  807. ctx->selected_flavor = flavor;
  808. server = ctx->nfs_mod->rpc_ops->create_server(fc);
  809. if (!IS_ERR(server))
  810. return server;
  811. }
  812. /*
  813. * Nothing we tried so far worked. At this point, give up if we've
  814. * already tried AUTH_UNIX or if the server's list doesn't contain
  815. * AUTH_NULL
  816. */
  817. if (tried_auth_unix || !auth_null_in_list)
  818. return server;
  819. /* Last chance! Try AUTH_UNIX */
  820. dfprintk(MOUNT, "NFS: attempting to use auth flavor %u\n", RPC_AUTH_UNIX);
  821. ctx->selected_flavor = RPC_AUTH_UNIX;
  822. return ctx->nfs_mod->rpc_ops->create_server(fc);
  823. }
  824. int nfs_try_get_tree(struct fs_context *fc)
  825. {
  826. struct nfs_fs_context *ctx = nfs_fc2context(fc);
  827. if (ctx->need_mount)
  828. ctx->server = nfs_try_mount_request(fc);
  829. else
  830. ctx->server = ctx->nfs_mod->rpc_ops->create_server(fc);
  831. return nfs_get_tree_common(fc);
  832. }
  833. EXPORT_SYMBOL_GPL(nfs_try_get_tree);
  834. #define NFS_REMOUNT_CMP_FLAGMASK ~(NFS_MOUNT_INTR \
  835. | NFS_MOUNT_SECURE \
  836. | NFS_MOUNT_TCP \
  837. | NFS_MOUNT_VER3 \
  838. | NFS_MOUNT_KERBEROS \
  839. | NFS_MOUNT_NONLM \
  840. | NFS_MOUNT_BROKEN_SUID \
  841. | NFS_MOUNT_STRICTLOCK \
  842. | NFS_MOUNT_LEGACY_INTERFACE)
  843. #define NFS_MOUNT_CMP_FLAGMASK (NFS_REMOUNT_CMP_FLAGMASK & \
  844. ~(NFS_MOUNT_UNSHARED | NFS_MOUNT_NORESVPORT))
  845. static int
  846. nfs_compare_remount_data(struct nfs_server *nfss,
  847. struct nfs_fs_context *ctx)
  848. {
  849. if ((ctx->flags ^ nfss->flags) & NFS_REMOUNT_CMP_FLAGMASK ||
  850. ctx->rsize != nfss->rsize ||
  851. ctx->wsize != nfss->wsize ||
  852. ctx->version != nfss->nfs_client->rpc_ops->version ||
  853. ctx->minorversion != nfss->nfs_client->cl_minorversion ||
  854. ctx->retrans != nfss->client->cl_timeout->to_retries ||
  855. !nfs_auth_info_match(&ctx->auth_info, nfss->client->cl_auth->au_flavor) ||
  856. ctx->acregmin != nfss->acregmin / HZ ||
  857. ctx->acregmax != nfss->acregmax / HZ ||
  858. ctx->acdirmin != nfss->acdirmin / HZ ||
  859. ctx->acdirmax != nfss->acdirmax / HZ ||
  860. ctx->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
  861. (ctx->options & NFS_OPTION_FSCACHE) != (nfss->options & NFS_OPTION_FSCACHE) ||
  862. ctx->nfs_server.port != nfss->port ||
  863. ctx->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
  864. !rpc_cmp_addr((struct sockaddr *)&ctx->nfs_server.address,
  865. (struct sockaddr *)&nfss->nfs_client->cl_addr))
  866. return -EINVAL;
  867. return 0;
  868. }
  869. int nfs_reconfigure(struct fs_context *fc)
  870. {
  871. struct nfs_fs_context *ctx = nfs_fc2context(fc);
  872. struct super_block *sb = fc->root->d_sb;
  873. struct nfs_server *nfss = sb->s_fs_info;
  874. sync_filesystem(sb);
  875. /*
  876. * Userspace mount programs that send binary options generally send
  877. * them populated with default values. We have no way to know which
  878. * ones were explicitly specified. Fall back to legacy behavior and
  879. * just return success.
  880. */
  881. if (ctx->skip_reconfig_option_check)
  882. return 0;
  883. /*
  884. * noac is a special case. It implies -o sync, but that's not
  885. * necessarily reflected in the mtab options. reconfigure_super
  886. * will clear SB_SYNCHRONOUS if -o sync wasn't specified in the
  887. * remount options, so we have to explicitly reset it.
  888. */
  889. if (ctx->flags & NFS_MOUNT_NOAC) {
  890. fc->sb_flags |= SB_SYNCHRONOUS;
  891. fc->sb_flags_mask |= SB_SYNCHRONOUS;
  892. }
  893. /* compare new mount options with old ones */
  894. return nfs_compare_remount_data(nfss, ctx);
  895. }
  896. EXPORT_SYMBOL_GPL(nfs_reconfigure);
  897. /*
  898. * Finish setting up an NFS superblock
  899. */
  900. static void nfs_fill_super(struct super_block *sb, struct nfs_fs_context *ctx)
  901. {
  902. struct nfs_server *server = NFS_SB(sb);
  903. sb->s_blocksize_bits = 0;
  904. sb->s_blocksize = 0;
  905. sb->s_xattr = server->nfs_client->cl_nfs_mod->xattr;
  906. sb->s_op = server->nfs_client->cl_nfs_mod->sops;
  907. if (ctx && ctx->bsize)
  908. sb->s_blocksize = nfs_block_size(ctx->bsize, &sb->s_blocksize_bits);
  909. if (server->nfs_client->rpc_ops->version != 2) {
  910. /* The VFS shouldn't apply the umask to mode bits. We will do
  911. * so ourselves when necessary.
  912. */
  913. sb->s_flags |= SB_POSIXACL;
  914. sb->s_time_gran = 1;
  915. sb->s_export_op = &nfs_export_ops;
  916. } else
  917. sb->s_time_gran = 1000;
  918. if (server->nfs_client->rpc_ops->version != 4) {
  919. sb->s_time_min = 0;
  920. sb->s_time_max = U32_MAX;
  921. } else {
  922. sb->s_time_min = S64_MIN;
  923. sb->s_time_max = S64_MAX;
  924. }
  925. sb->s_magic = NFS_SUPER_MAGIC;
  926. /* We probably want something more informative here */
  927. snprintf(sb->s_id, sizeof(sb->s_id),
  928. "%u:%u", MAJOR(sb->s_dev), MINOR(sb->s_dev));
  929. if (sb->s_blocksize == 0)
  930. sb->s_blocksize = nfs_block_bits(server->wsize,
  931. &sb->s_blocksize_bits);
  932. nfs_super_set_maxbytes(sb, server->maxfilesize);
  933. }
  934. static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b,
  935. const struct fs_context *fc)
  936. {
  937. const struct nfs_server *a = s->s_fs_info;
  938. const struct rpc_clnt *clnt_a = a->client;
  939. const struct rpc_clnt *clnt_b = b->client;
  940. if ((s->s_flags & NFS_SB_MASK) != (fc->sb_flags & NFS_SB_MASK))
  941. goto Ebusy;
  942. if (a->nfs_client != b->nfs_client)
  943. goto Ebusy;
  944. if ((a->flags ^ b->flags) & NFS_MOUNT_CMP_FLAGMASK)
  945. goto Ebusy;
  946. if (a->wsize != b->wsize)
  947. goto Ebusy;
  948. if (a->rsize != b->rsize)
  949. goto Ebusy;
  950. if (a->acregmin != b->acregmin)
  951. goto Ebusy;
  952. if (a->acregmax != b->acregmax)
  953. goto Ebusy;
  954. if (a->acdirmin != b->acdirmin)
  955. goto Ebusy;
  956. if (a->acdirmax != b->acdirmax)
  957. goto Ebusy;
  958. if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
  959. goto Ebusy;
  960. return 1;
  961. Ebusy:
  962. return 0;
  963. }
  964. static int nfs_set_super(struct super_block *s, struct fs_context *fc)
  965. {
  966. struct nfs_server *server = fc->s_fs_info;
  967. int ret;
  968. s->s_d_op = server->nfs_client->rpc_ops->dentry_ops;
  969. ret = set_anon_super(s, server);
  970. if (ret == 0)
  971. server->s_dev = s->s_dev;
  972. return ret;
  973. }
  974. static int nfs_compare_super_address(struct nfs_server *server1,
  975. struct nfs_server *server2)
  976. {
  977. struct sockaddr *sap1, *sap2;
  978. struct rpc_xprt *xprt1 = server1->client->cl_xprt;
  979. struct rpc_xprt *xprt2 = server2->client->cl_xprt;
  980. if (!net_eq(xprt1->xprt_net, xprt2->xprt_net))
  981. return 0;
  982. sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
  983. sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
  984. if (sap1->sa_family != sap2->sa_family)
  985. return 0;
  986. switch (sap1->sa_family) {
  987. case AF_INET: {
  988. struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
  989. struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
  990. if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
  991. return 0;
  992. if (sin1->sin_port != sin2->sin_port)
  993. return 0;
  994. break;
  995. }
  996. case AF_INET6: {
  997. struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
  998. struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
  999. if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
  1000. return 0;
  1001. if (sin1->sin6_port != sin2->sin6_port)
  1002. return 0;
  1003. break;
  1004. }
  1005. default:
  1006. return 0;
  1007. }
  1008. return 1;
  1009. }
  1010. static int nfs_compare_userns(const struct nfs_server *old,
  1011. const struct nfs_server *new)
  1012. {
  1013. const struct user_namespace *oldns = &init_user_ns;
  1014. const struct user_namespace *newns = &init_user_ns;
  1015. if (old->client && old->client->cl_cred)
  1016. oldns = old->client->cl_cred->user_ns;
  1017. if (new->client && new->client->cl_cred)
  1018. newns = new->client->cl_cred->user_ns;
  1019. if (oldns != newns)
  1020. return 0;
  1021. return 1;
  1022. }
  1023. static int nfs_compare_super(struct super_block *sb, struct fs_context *fc)
  1024. {
  1025. struct nfs_server *server = fc->s_fs_info, *old = NFS_SB(sb);
  1026. if (!nfs_compare_super_address(old, server))
  1027. return 0;
  1028. /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
  1029. if (old->flags & NFS_MOUNT_UNSHARED)
  1030. return 0;
  1031. if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
  1032. return 0;
  1033. if (!nfs_compare_userns(old, server))
  1034. return 0;
  1035. return nfs_compare_mount_options(sb, server, fc);
  1036. }
  1037. #ifdef CONFIG_NFS_FSCACHE
  1038. static void nfs_get_cache_cookie(struct super_block *sb,
  1039. struct nfs_fs_context *ctx)
  1040. {
  1041. struct nfs_server *nfss = NFS_SB(sb);
  1042. char *uniq = NULL;
  1043. int ulen = 0;
  1044. nfss->fscache_key = NULL;
  1045. nfss->fscache = NULL;
  1046. if (!ctx)
  1047. return;
  1048. if (ctx->clone_data.sb) {
  1049. struct nfs_server *mnt_s = NFS_SB(ctx->clone_data.sb);
  1050. if (!(mnt_s->options & NFS_OPTION_FSCACHE))
  1051. return;
  1052. if (mnt_s->fscache_key) {
  1053. uniq = mnt_s->fscache_key->key.uniquifier;
  1054. ulen = mnt_s->fscache_key->key.uniq_len;
  1055. }
  1056. } else {
  1057. if (!(ctx->options & NFS_OPTION_FSCACHE))
  1058. return;
  1059. if (ctx->fscache_uniq) {
  1060. uniq = ctx->fscache_uniq;
  1061. ulen = strlen(ctx->fscache_uniq);
  1062. }
  1063. }
  1064. nfs_fscache_get_super_cookie(sb, uniq, ulen);
  1065. }
  1066. #else
  1067. static void nfs_get_cache_cookie(struct super_block *sb,
  1068. struct nfs_fs_context *ctx)
  1069. {
  1070. }
  1071. #endif
  1072. int nfs_get_tree_common(struct fs_context *fc)
  1073. {
  1074. struct nfs_fs_context *ctx = nfs_fc2context(fc);
  1075. struct super_block *s;
  1076. int (*compare_super)(struct super_block *, struct fs_context *) = nfs_compare_super;
  1077. struct nfs_server *server = ctx->server;
  1078. int error;
  1079. ctx->server = NULL;
  1080. if (IS_ERR(server))
  1081. return PTR_ERR(server);
  1082. if (server->flags & NFS_MOUNT_UNSHARED)
  1083. compare_super = NULL;
  1084. /* -o noac implies -o sync */
  1085. if (server->flags & NFS_MOUNT_NOAC)
  1086. fc->sb_flags |= SB_SYNCHRONOUS;
  1087. if (ctx->clone_data.sb)
  1088. if (ctx->clone_data.sb->s_flags & SB_SYNCHRONOUS)
  1089. fc->sb_flags |= SB_SYNCHRONOUS;
  1090. if (server->caps & NFS_CAP_SECURITY_LABEL)
  1091. fc->lsm_flags |= SECURITY_LSM_NATIVE_LABELS;
  1092. /* Get a superblock - note that we may end up sharing one that already exists */
  1093. fc->s_fs_info = server;
  1094. s = sget_fc(fc, compare_super, nfs_set_super);
  1095. fc->s_fs_info = NULL;
  1096. if (IS_ERR(s)) {
  1097. error = PTR_ERR(s);
  1098. nfs_errorf(fc, "NFS: Couldn't get superblock");
  1099. goto out_err_nosb;
  1100. }
  1101. if (s->s_fs_info != server) {
  1102. nfs_free_server(server);
  1103. server = NULL;
  1104. } else {
  1105. error = super_setup_bdi_name(s, "%u:%u", MAJOR(server->s_dev),
  1106. MINOR(server->s_dev));
  1107. if (error)
  1108. goto error_splat_super;
  1109. s->s_bdi->io_pages = server->rpages;
  1110. server->super = s;
  1111. }
  1112. if (!s->s_root) {
  1113. unsigned bsize = ctx->clone_data.inherited_bsize;
  1114. /* initial superblock/root creation */
  1115. nfs_fill_super(s, ctx);
  1116. if (bsize) {
  1117. s->s_blocksize_bits = bsize;
  1118. s->s_blocksize = 1U << bsize;
  1119. }
  1120. nfs_get_cache_cookie(s, ctx);
  1121. }
  1122. error = nfs_get_root(s, fc);
  1123. if (error < 0) {
  1124. nfs_errorf(fc, "NFS: Couldn't get root dentry");
  1125. goto error_splat_super;
  1126. }
  1127. s->s_flags |= SB_ACTIVE;
  1128. error = 0;
  1129. out:
  1130. return error;
  1131. out_err_nosb:
  1132. nfs_free_server(server);
  1133. goto out;
  1134. error_splat_super:
  1135. deactivate_locked_super(s);
  1136. goto out;
  1137. }
  1138. /*
  1139. * Destroy an NFS2/3 superblock
  1140. */
  1141. void nfs_kill_super(struct super_block *s)
  1142. {
  1143. struct nfs_server *server = NFS_SB(s);
  1144. dev_t dev = s->s_dev;
  1145. generic_shutdown_super(s);
  1146. nfs_fscache_release_super_cookie(s);
  1147. nfs_free_server(server);
  1148. free_anon_bdev(dev);
  1149. }
  1150. EXPORT_SYMBOL_GPL(nfs_kill_super);
  1151. #if IS_ENABLED(CONFIG_NFS_V4)
  1152. /*
  1153. * NFS v4 module parameters need to stay in the
  1154. * NFS client for backwards compatibility
  1155. */
  1156. unsigned int nfs_callback_set_tcpport;
  1157. unsigned short nfs_callback_nr_threads;
  1158. /* Default cache timeout is 10 minutes */
  1159. unsigned int nfs_idmap_cache_timeout = 600;
  1160. /* Turn off NFSv4 uid/gid mapping when using AUTH_SYS */
  1161. bool nfs4_disable_idmapping = true;
  1162. unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
  1163. unsigned short max_session_cb_slots = NFS4_DEF_CB_SLOT_TABLE_SIZE;
  1164. unsigned short send_implementation_id = 1;
  1165. char nfs4_client_id_uniquifier[NFS4_CLIENT_ID_UNIQ_LEN] = "";
  1166. bool recover_lost_locks = false;
  1167. EXPORT_SYMBOL_GPL(nfs_callback_nr_threads);
  1168. EXPORT_SYMBOL_GPL(nfs_callback_set_tcpport);
  1169. EXPORT_SYMBOL_GPL(nfs_idmap_cache_timeout);
  1170. EXPORT_SYMBOL_GPL(nfs4_disable_idmapping);
  1171. EXPORT_SYMBOL_GPL(max_session_slots);
  1172. EXPORT_SYMBOL_GPL(max_session_cb_slots);
  1173. EXPORT_SYMBOL_GPL(send_implementation_id);
  1174. EXPORT_SYMBOL_GPL(nfs4_client_id_uniquifier);
  1175. EXPORT_SYMBOL_GPL(recover_lost_locks);
  1176. #define NFS_CALLBACK_MAXPORTNR (65535U)
  1177. static int param_set_portnr(const char *val, const struct kernel_param *kp)
  1178. {
  1179. unsigned long num;
  1180. int ret;
  1181. if (!val)
  1182. return -EINVAL;
  1183. ret = kstrtoul(val, 0, &num);
  1184. if (ret || num > NFS_CALLBACK_MAXPORTNR)
  1185. return -EINVAL;
  1186. *((unsigned int *)kp->arg) = num;
  1187. return 0;
  1188. }
  1189. static const struct kernel_param_ops param_ops_portnr = {
  1190. .set = param_set_portnr,
  1191. .get = param_get_uint,
  1192. };
  1193. #define param_check_portnr(name, p) __param_check(name, p, unsigned int);
  1194. module_param_named(callback_tcpport, nfs_callback_set_tcpport, portnr, 0644);
  1195. module_param_named(callback_nr_threads, nfs_callback_nr_threads, ushort, 0644);
  1196. MODULE_PARM_DESC(callback_nr_threads, "Number of threads that will be "
  1197. "assigned to the NFSv4 callback channels.");
  1198. module_param(nfs_idmap_cache_timeout, int, 0644);
  1199. module_param(nfs4_disable_idmapping, bool, 0644);
  1200. module_param_string(nfs4_unique_id, nfs4_client_id_uniquifier,
  1201. NFS4_CLIENT_ID_UNIQ_LEN, 0600);
  1202. MODULE_PARM_DESC(nfs4_disable_idmapping,
  1203. "Turn off NFSv4 idmapping when using 'sec=sys'");
  1204. module_param(max_session_slots, ushort, 0644);
  1205. MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
  1206. "requests the client will negotiate");
  1207. module_param(max_session_cb_slots, ushort, 0644);
  1208. MODULE_PARM_DESC(max_session_cb_slots, "Maximum number of parallel NFSv4.1 "
  1209. "callbacks the client will process for a given server");
  1210. module_param(send_implementation_id, ushort, 0644);
  1211. MODULE_PARM_DESC(send_implementation_id,
  1212. "Send implementation ID with NFSv4.1 exchange_id");
  1213. MODULE_PARM_DESC(nfs4_unique_id, "nfs_client_id4 uniquifier string");
  1214. module_param(recover_lost_locks, bool, 0644);
  1215. MODULE_PARM_DESC(recover_lost_locks,
  1216. "If the server reports that a lock might be lost, "
  1217. "try to recover it risking data corruption.");
  1218. #endif /* CONFIG_NFS_V4 */