super.c 38 KB

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