super.c 39 KB

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