super.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. /*
  2. * linux/fs/nfs/super.c
  3. *
  4. * Copyright (C) 1992 Rick Sladkey
  5. *
  6. * nfs superblock handling functions
  7. *
  8. * Modularised by Alan Cox <Alan.Cox@linux.org>, while hacking some
  9. * experimental NFS changes. Modularisation taken straight from SYS5 fs.
  10. *
  11. * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
  12. * J.S.Peatfield@damtp.cam.ac.uk
  13. *
  14. * Split from inode.c by David Howells <dhowells@redhat.com>
  15. *
  16. * - superblocks are indexed on server only - all inodes, dentries, etc. associated with a
  17. * particular server are held in the same superblock
  18. * - NFS superblocks can have several effective roots to the dentry tree
  19. * - directory type roots are spliced into the tree when a path from one root reaches the root
  20. * of another (see nfs_lookup())
  21. */
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/time.h>
  25. #include <linux/kernel.h>
  26. #include <linux/mm.h>
  27. #include <linux/string.h>
  28. #include <linux/stat.h>
  29. #include <linux/errno.h>
  30. #include <linux/unistd.h>
  31. #include <linux/sunrpc/clnt.h>
  32. #include <linux/sunrpc/stats.h>
  33. #include <linux/sunrpc/metrics.h>
  34. #include <linux/nfs_fs.h>
  35. #include <linux/nfs_mount.h>
  36. #include <linux/nfs4_mount.h>
  37. #include <linux/lockd/bind.h>
  38. #include <linux/smp_lock.h>
  39. #include <linux/seq_file.h>
  40. #include <linux/mount.h>
  41. #include <linux/nfs_idmap.h>
  42. #include <linux/vfs.h>
  43. #include <linux/inet.h>
  44. #include <linux/nfs_xdr.h>
  45. #include <linux/magic.h>
  46. #include <asm/system.h>
  47. #include <asm/uaccess.h>
  48. #include "nfs4_fs.h"
  49. #include "callback.h"
  50. #include "delegation.h"
  51. #include "iostat.h"
  52. #include "internal.h"
  53. #define NFSDBG_FACILITY NFSDBG_VFS
  54. static void nfs_umount_begin(struct vfsmount *, int);
  55. static int nfs_statfs(struct dentry *, struct kstatfs *);
  56. static int nfs_show_options(struct seq_file *, struct vfsmount *);
  57. static int nfs_show_stats(struct seq_file *, struct vfsmount *);
  58. static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
  59. static int nfs_xdev_get_sb(struct file_system_type *fs_type,
  60. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
  61. static void nfs_kill_super(struct super_block *);
  62. static struct file_system_type nfs_fs_type = {
  63. .owner = THIS_MODULE,
  64. .name = "nfs",
  65. .get_sb = nfs_get_sb,
  66. .kill_sb = nfs_kill_super,
  67. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  68. };
  69. struct file_system_type nfs_xdev_fs_type = {
  70. .owner = THIS_MODULE,
  71. .name = "nfs",
  72. .get_sb = nfs_xdev_get_sb,
  73. .kill_sb = nfs_kill_super,
  74. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  75. };
  76. static const struct super_operations nfs_sops = {
  77. .alloc_inode = nfs_alloc_inode,
  78. .destroy_inode = nfs_destroy_inode,
  79. .write_inode = nfs_write_inode,
  80. .statfs = nfs_statfs,
  81. .clear_inode = nfs_clear_inode,
  82. .umount_begin = nfs_umount_begin,
  83. .show_options = nfs_show_options,
  84. .show_stats = nfs_show_stats,
  85. };
  86. #ifdef CONFIG_NFS_V4
  87. static int nfs4_get_sb(struct file_system_type *fs_type,
  88. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
  89. static int nfs4_xdev_get_sb(struct file_system_type *fs_type,
  90. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
  91. static int nfs4_referral_get_sb(struct file_system_type *fs_type,
  92. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
  93. static void nfs4_kill_super(struct super_block *sb);
  94. static struct file_system_type nfs4_fs_type = {
  95. .owner = THIS_MODULE,
  96. .name = "nfs4",
  97. .get_sb = nfs4_get_sb,
  98. .kill_sb = nfs4_kill_super,
  99. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  100. };
  101. struct file_system_type nfs4_xdev_fs_type = {
  102. .owner = THIS_MODULE,
  103. .name = "nfs4",
  104. .get_sb = nfs4_xdev_get_sb,
  105. .kill_sb = nfs4_kill_super,
  106. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  107. };
  108. struct file_system_type nfs4_referral_fs_type = {
  109. .owner = THIS_MODULE,
  110. .name = "nfs4",
  111. .get_sb = nfs4_referral_get_sb,
  112. .kill_sb = nfs4_kill_super,
  113. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  114. };
  115. static const struct super_operations nfs4_sops = {
  116. .alloc_inode = nfs_alloc_inode,
  117. .destroy_inode = nfs_destroy_inode,
  118. .write_inode = nfs_write_inode,
  119. .statfs = nfs_statfs,
  120. .clear_inode = nfs4_clear_inode,
  121. .umount_begin = nfs_umount_begin,
  122. .show_options = nfs_show_options,
  123. .show_stats = nfs_show_stats,
  124. };
  125. #endif
  126. static struct shrinker *acl_shrinker;
  127. /*
  128. * Register the NFS filesystems
  129. */
  130. int __init register_nfs_fs(void)
  131. {
  132. int ret;
  133. ret = register_filesystem(&nfs_fs_type);
  134. if (ret < 0)
  135. goto error_0;
  136. ret = nfs_register_sysctl();
  137. if (ret < 0)
  138. goto error_1;
  139. #ifdef CONFIG_NFS_V4
  140. ret = register_filesystem(&nfs4_fs_type);
  141. if (ret < 0)
  142. goto error_2;
  143. #endif
  144. acl_shrinker = set_shrinker(DEFAULT_SEEKS, nfs_access_cache_shrinker);
  145. return 0;
  146. #ifdef CONFIG_NFS_V4
  147. error_2:
  148. nfs_unregister_sysctl();
  149. #endif
  150. error_1:
  151. unregister_filesystem(&nfs_fs_type);
  152. error_0:
  153. return ret;
  154. }
  155. /*
  156. * Unregister the NFS filesystems
  157. */
  158. void __exit unregister_nfs_fs(void)
  159. {
  160. if (acl_shrinker != NULL)
  161. remove_shrinker(acl_shrinker);
  162. #ifdef CONFIG_NFS_V4
  163. unregister_filesystem(&nfs4_fs_type);
  164. nfs_unregister_sysctl();
  165. #endif
  166. unregister_filesystem(&nfs_fs_type);
  167. }
  168. /*
  169. * Deliver file system statistics to userspace
  170. */
  171. static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  172. {
  173. struct nfs_server *server = NFS_SB(dentry->d_sb);
  174. unsigned char blockbits;
  175. unsigned long blockres;
  176. struct nfs_fh *fh = NFS_FH(dentry->d_inode);
  177. struct nfs_fattr fattr;
  178. struct nfs_fsstat res = {
  179. .fattr = &fattr,
  180. };
  181. int error;
  182. lock_kernel();
  183. error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
  184. if (error < 0)
  185. goto out_err;
  186. buf->f_type = NFS_SUPER_MAGIC;
  187. /*
  188. * Current versions of glibc do not correctly handle the
  189. * case where f_frsize != f_bsize. Eventually we want to
  190. * report the value of wtmult in this field.
  191. */
  192. buf->f_frsize = dentry->d_sb->s_blocksize;
  193. /*
  194. * On most *nix systems, f_blocks, f_bfree, and f_bavail
  195. * are reported in units of f_frsize. Linux hasn't had
  196. * an f_frsize field in its statfs struct until recently,
  197. * thus historically Linux's sys_statfs reports these
  198. * fields in units of f_bsize.
  199. */
  200. buf->f_bsize = dentry->d_sb->s_blocksize;
  201. blockbits = dentry->d_sb->s_blocksize_bits;
  202. blockres = (1 << blockbits) - 1;
  203. buf->f_blocks = (res.tbytes + blockres) >> blockbits;
  204. buf->f_bfree = (res.fbytes + blockres) >> blockbits;
  205. buf->f_bavail = (res.abytes + blockres) >> blockbits;
  206. buf->f_files = res.tfiles;
  207. buf->f_ffree = res.afiles;
  208. buf->f_namelen = server->namelen;
  209. unlock_kernel();
  210. return 0;
  211. out_err:
  212. dprintk("%s: statfs error = %d\n", __FUNCTION__, -error);
  213. unlock_kernel();
  214. return error;
  215. }
  216. /*
  217. * Map the security flavour number to a name
  218. */
  219. static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
  220. {
  221. static const struct {
  222. rpc_authflavor_t flavour;
  223. const char *str;
  224. } sec_flavours[] = {
  225. { RPC_AUTH_NULL, "null" },
  226. { RPC_AUTH_UNIX, "sys" },
  227. { RPC_AUTH_GSS_KRB5, "krb5" },
  228. { RPC_AUTH_GSS_KRB5I, "krb5i" },
  229. { RPC_AUTH_GSS_KRB5P, "krb5p" },
  230. { RPC_AUTH_GSS_LKEY, "lkey" },
  231. { RPC_AUTH_GSS_LKEYI, "lkeyi" },
  232. { RPC_AUTH_GSS_LKEYP, "lkeyp" },
  233. { RPC_AUTH_GSS_SPKM, "spkm" },
  234. { RPC_AUTH_GSS_SPKMI, "spkmi" },
  235. { RPC_AUTH_GSS_SPKMP, "spkmp" },
  236. { UINT_MAX, "unknown" }
  237. };
  238. int i;
  239. for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
  240. if (sec_flavours[i].flavour == flavour)
  241. break;
  242. }
  243. return sec_flavours[i].str;
  244. }
  245. /*
  246. * Describe the mount options in force on this server representation
  247. */
  248. static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss, int showdefaults)
  249. {
  250. static const struct proc_nfs_info {
  251. int flag;
  252. const char *str;
  253. const char *nostr;
  254. } nfs_info[] = {
  255. { NFS_MOUNT_SOFT, ",soft", ",hard" },
  256. { NFS_MOUNT_INTR, ",intr", "" },
  257. { NFS_MOUNT_NOCTO, ",nocto", "" },
  258. { NFS_MOUNT_NOAC, ",noac", "" },
  259. { NFS_MOUNT_NONLM, ",nolock", "" },
  260. { NFS_MOUNT_NOACL, ",noacl", "" },
  261. { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
  262. { 0, NULL, NULL }
  263. };
  264. const struct proc_nfs_info *nfs_infop;
  265. struct nfs_client *clp = nfss->nfs_client;
  266. char buf[12];
  267. const char *proto;
  268. seq_printf(m, ",vers=%d", clp->rpc_ops->version);
  269. seq_printf(m, ",rsize=%d", nfss->rsize);
  270. seq_printf(m, ",wsize=%d", nfss->wsize);
  271. if (nfss->acregmin != 3*HZ || showdefaults)
  272. seq_printf(m, ",acregmin=%d", nfss->acregmin/HZ);
  273. if (nfss->acregmax != 60*HZ || showdefaults)
  274. seq_printf(m, ",acregmax=%d", nfss->acregmax/HZ);
  275. if (nfss->acdirmin != 30*HZ || showdefaults)
  276. seq_printf(m, ",acdirmin=%d", nfss->acdirmin/HZ);
  277. if (nfss->acdirmax != 60*HZ || showdefaults)
  278. seq_printf(m, ",acdirmax=%d", nfss->acdirmax/HZ);
  279. for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
  280. if (nfss->flags & nfs_infop->flag)
  281. seq_puts(m, nfs_infop->str);
  282. else
  283. seq_puts(m, nfs_infop->nostr);
  284. }
  285. switch (nfss->client->cl_xprt->prot) {
  286. case IPPROTO_TCP:
  287. proto = "tcp";
  288. break;
  289. case IPPROTO_UDP:
  290. proto = "udp";
  291. break;
  292. default:
  293. snprintf(buf, sizeof(buf), "%u", nfss->client->cl_xprt->prot);
  294. proto = buf;
  295. }
  296. seq_printf(m, ",proto=%s", proto);
  297. seq_printf(m, ",timeo=%lu", 10U * clp->retrans_timeo / HZ);
  298. seq_printf(m, ",retrans=%u", clp->retrans_count);
  299. seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
  300. }
  301. /*
  302. * Describe the mount options on this VFS mountpoint
  303. */
  304. static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
  305. {
  306. struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
  307. nfs_show_mount_options(m, nfss, 0);
  308. seq_puts(m, ",addr=");
  309. seq_escape(m, nfss->nfs_client->cl_hostname, " \t\n\\");
  310. return 0;
  311. }
  312. /*
  313. * Present statistical information for this VFS mountpoint
  314. */
  315. static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
  316. {
  317. int i, cpu;
  318. struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
  319. struct rpc_auth *auth = nfss->client->cl_auth;
  320. struct nfs_iostats totals = { };
  321. seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
  322. /*
  323. * Display all mount option settings
  324. */
  325. seq_printf(m, "\n\topts:\t");
  326. seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
  327. seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
  328. seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
  329. seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
  330. nfs_show_mount_options(m, nfss, 1);
  331. seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
  332. seq_printf(m, "\n\tcaps:\t");
  333. seq_printf(m, "caps=0x%x", nfss->caps);
  334. seq_printf(m, ",wtmult=%d", nfss->wtmult);
  335. seq_printf(m, ",dtsize=%d", nfss->dtsize);
  336. seq_printf(m, ",bsize=%d", nfss->bsize);
  337. seq_printf(m, ",namelen=%d", nfss->namelen);
  338. #ifdef CONFIG_NFS_V4
  339. if (nfss->nfs_client->cl_nfsversion == 4) {
  340. seq_printf(m, "\n\tnfsv4:\t");
  341. seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
  342. seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
  343. seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
  344. }
  345. #endif
  346. /*
  347. * Display security flavor in effect for this mount
  348. */
  349. seq_printf(m, "\n\tsec:\tflavor=%d", auth->au_ops->au_flavor);
  350. if (auth->au_flavor)
  351. seq_printf(m, ",pseudoflavor=%d", auth->au_flavor);
  352. /*
  353. * Display superblock I/O counters
  354. */
  355. for_each_possible_cpu(cpu) {
  356. struct nfs_iostats *stats;
  357. preempt_disable();
  358. stats = per_cpu_ptr(nfss->io_stats, cpu);
  359. for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
  360. totals.events[i] += stats->events[i];
  361. for (i = 0; i < __NFSIOS_BYTESMAX; i++)
  362. totals.bytes[i] += stats->bytes[i];
  363. preempt_enable();
  364. }
  365. seq_printf(m, "\n\tevents:\t");
  366. for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
  367. seq_printf(m, "%lu ", totals.events[i]);
  368. seq_printf(m, "\n\tbytes:\t");
  369. for (i = 0; i < __NFSIOS_BYTESMAX; i++)
  370. seq_printf(m, "%Lu ", totals.bytes[i]);
  371. seq_printf(m, "\n");
  372. rpc_print_iostats(m, nfss->client);
  373. return 0;
  374. }
  375. /*
  376. * Begin unmount by attempting to remove all automounted mountpoints we added
  377. * in response to xdev traversals and referrals
  378. */
  379. static void nfs_umount_begin(struct vfsmount *vfsmnt, int flags)
  380. {
  381. struct nfs_server *server = NFS_SB(vfsmnt->mnt_sb);
  382. struct rpc_clnt *rpc;
  383. shrink_submounts(vfsmnt, &nfs_automount_list);
  384. if (!(flags & MNT_FORCE))
  385. return;
  386. /* -EIO all pending I/O */
  387. rpc = server->client_acl;
  388. if (!IS_ERR(rpc))
  389. rpc_killall_tasks(rpc);
  390. rpc = server->client;
  391. if (!IS_ERR(rpc))
  392. rpc_killall_tasks(rpc);
  393. }
  394. /*
  395. * Sanity-check a server address provided by the mount command
  396. */
  397. static int nfs_verify_server_address(struct sockaddr *addr)
  398. {
  399. switch (addr->sa_family) {
  400. case AF_INET: {
  401. struct sockaddr_in *sa = (struct sockaddr_in *) addr;
  402. if (sa->sin_addr.s_addr != INADDR_ANY)
  403. return 1;
  404. break;
  405. }
  406. }
  407. return 0;
  408. }
  409. /*
  410. * Validate the NFS2/NFS3 mount data
  411. * - fills in the mount root filehandle
  412. */
  413. static int nfs_validate_mount_data(struct nfs_mount_data *data,
  414. struct nfs_fh *mntfh)
  415. {
  416. if (data == NULL)
  417. goto out_no_data;
  418. switch (data->version) {
  419. case 1:
  420. data->namlen = 0;
  421. case 2:
  422. data->bsize = 0;
  423. case 3:
  424. if (data->flags & NFS_MOUNT_VER3)
  425. goto out_no_v3;
  426. data->root.size = NFS2_FHSIZE;
  427. memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
  428. case 4:
  429. if (data->flags & NFS_MOUNT_SECFLAVOUR)
  430. goto out_no_sec;
  431. case 5:
  432. memset(data->context, 0, sizeof(data->context));
  433. case 6:
  434. if (data->flags & NFS_MOUNT_VER3)
  435. mntfh->size = data->root.size;
  436. else
  437. mntfh->size = NFS2_FHSIZE;
  438. if (mntfh->size > sizeof(mntfh->data))
  439. goto out_invalid_fh;
  440. memcpy(mntfh->data, data->root.data, mntfh->size);
  441. if (mntfh->size < sizeof(mntfh->data))
  442. memset(mntfh->data + mntfh->size, 0,
  443. sizeof(mntfh->data) - mntfh->size);
  444. break;
  445. default:
  446. goto out_bad_version;
  447. }
  448. if (!(data->flags & NFS_MOUNT_SECFLAVOUR))
  449. data->pseudoflavor = RPC_AUTH_UNIX;
  450. #ifndef CONFIG_NFS_V3
  451. if (data->flags & NFS_MOUNT_VER3)
  452. goto out_v3_not_compiled;
  453. #endif /* !CONFIG_NFS_V3 */
  454. if (!nfs_verify_server_address((struct sockaddr *) &data->addr))
  455. goto out_no_address;
  456. return 0;
  457. out_no_data:
  458. dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
  459. return -EINVAL;
  460. out_no_v3:
  461. dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
  462. data->version);
  463. return -EINVAL;
  464. out_no_sec:
  465. dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
  466. return -EINVAL;
  467. out_bad_version:
  468. dfprintk(MOUNT, "NFS: bad nfs_mount_data version %d\n",
  469. data->version);
  470. return -EINVAL;
  471. #ifndef CONFIG_NFS_V3
  472. out_v3_not_compiled:
  473. dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
  474. return -EPROTONOSUPPORT;
  475. #endif /* !CONFIG_NFS_V3 */
  476. out_no_address:
  477. dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
  478. return -EINVAL;
  479. out_invalid_fh:
  480. dfprintk(MOUNT, "NFS: invalid root filehandle\n");
  481. return -EINVAL;
  482. }
  483. /*
  484. * Initialise the common bits of the superblock
  485. */
  486. static inline void nfs_initialise_sb(struct super_block *sb)
  487. {
  488. struct nfs_server *server = NFS_SB(sb);
  489. sb->s_magic = NFS_SUPER_MAGIC;
  490. /* We probably want something more informative here */
  491. snprintf(sb->s_id, sizeof(sb->s_id),
  492. "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
  493. if (sb->s_blocksize == 0)
  494. sb->s_blocksize = nfs_block_bits(server->wsize,
  495. &sb->s_blocksize_bits);
  496. if (server->flags & NFS_MOUNT_NOAC)
  497. sb->s_flags |= MS_SYNCHRONOUS;
  498. nfs_super_set_maxbytes(sb, server->maxfilesize);
  499. }
  500. /*
  501. * Finish setting up an NFS2/3 superblock
  502. */
  503. static void nfs_fill_super(struct super_block *sb, struct nfs_mount_data *data)
  504. {
  505. struct nfs_server *server = NFS_SB(sb);
  506. sb->s_blocksize_bits = 0;
  507. sb->s_blocksize = 0;
  508. if (data->bsize)
  509. sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
  510. if (server->flags & NFS_MOUNT_VER3) {
  511. /* The VFS shouldn't apply the umask to mode bits. We will do
  512. * so ourselves when necessary.
  513. */
  514. sb->s_flags |= MS_POSIXACL;
  515. sb->s_time_gran = 1;
  516. }
  517. sb->s_op = &nfs_sops;
  518. nfs_initialise_sb(sb);
  519. }
  520. /*
  521. * Finish setting up a cloned NFS2/3 superblock
  522. */
  523. static void nfs_clone_super(struct super_block *sb,
  524. const struct super_block *old_sb)
  525. {
  526. struct nfs_server *server = NFS_SB(sb);
  527. sb->s_blocksize_bits = old_sb->s_blocksize_bits;
  528. sb->s_blocksize = old_sb->s_blocksize;
  529. sb->s_maxbytes = old_sb->s_maxbytes;
  530. if (server->flags & NFS_MOUNT_VER3) {
  531. /* The VFS shouldn't apply the umask to mode bits. We will do
  532. * so ourselves when necessary.
  533. */
  534. sb->s_flags |= MS_POSIXACL;
  535. sb->s_time_gran = 1;
  536. }
  537. sb->s_op = old_sb->s_op;
  538. nfs_initialise_sb(sb);
  539. }
  540. static int nfs_set_super(struct super_block *s, void *_server)
  541. {
  542. struct nfs_server *server = _server;
  543. int ret;
  544. s->s_fs_info = server;
  545. ret = set_anon_super(s, server);
  546. if (ret == 0)
  547. server->s_dev = s->s_dev;
  548. return ret;
  549. }
  550. static int nfs_compare_super(struct super_block *sb, void *data)
  551. {
  552. struct nfs_server *server = data, *old = NFS_SB(sb);
  553. if (old->nfs_client != server->nfs_client)
  554. return 0;
  555. if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
  556. return 0;
  557. return 1;
  558. }
  559. static int nfs_get_sb(struct file_system_type *fs_type,
  560. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
  561. {
  562. struct nfs_server *server = NULL;
  563. struct super_block *s;
  564. struct nfs_fh mntfh;
  565. struct nfs_mount_data *data = raw_data;
  566. struct dentry *mntroot;
  567. int error;
  568. /* Validate the mount data */
  569. error = nfs_validate_mount_data(data, &mntfh);
  570. if (error < 0)
  571. goto out;
  572. /* Get a volume representation */
  573. server = nfs_create_server(data, &mntfh);
  574. if (IS_ERR(server)) {
  575. error = PTR_ERR(server);
  576. goto out;
  577. }
  578. /* Get a superblock - note that we may end up sharing one that already exists */
  579. s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
  580. if (IS_ERR(s)) {
  581. error = PTR_ERR(s);
  582. goto out_err_nosb;
  583. }
  584. if (s->s_fs_info != server) {
  585. nfs_free_server(server);
  586. server = NULL;
  587. }
  588. if (!s->s_root) {
  589. /* initial superblock/root creation */
  590. s->s_flags = flags;
  591. nfs_fill_super(s, data);
  592. }
  593. mntroot = nfs_get_root(s, &mntfh);
  594. if (IS_ERR(mntroot)) {
  595. error = PTR_ERR(mntroot);
  596. goto error_splat_super;
  597. }
  598. s->s_flags |= MS_ACTIVE;
  599. mnt->mnt_sb = s;
  600. mnt->mnt_root = mntroot;
  601. error = 0;
  602. out:
  603. return error;
  604. out_err_nosb:
  605. nfs_free_server(server);
  606. goto out;
  607. error_splat_super:
  608. up_write(&s->s_umount);
  609. deactivate_super(s);
  610. goto out;
  611. }
  612. /*
  613. * Destroy an NFS2/3 superblock
  614. */
  615. static void nfs_kill_super(struct super_block *s)
  616. {
  617. struct nfs_server *server = NFS_SB(s);
  618. kill_anon_super(s);
  619. nfs_free_server(server);
  620. }
  621. /*
  622. * Clone an NFS2/3 server record on xdev traversal (FSID-change)
  623. */
  624. static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
  625. const char *dev_name, void *raw_data,
  626. struct vfsmount *mnt)
  627. {
  628. struct nfs_clone_mount *data = raw_data;
  629. struct super_block *s;
  630. struct nfs_server *server;
  631. struct dentry *mntroot;
  632. int error;
  633. dprintk("--> nfs_xdev_get_sb()\n");
  634. /* create a new volume representation */
  635. server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
  636. if (IS_ERR(server)) {
  637. error = PTR_ERR(server);
  638. goto out_err_noserver;
  639. }
  640. /* Get a superblock - note that we may end up sharing one that already exists */
  641. s = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
  642. if (IS_ERR(s)) {
  643. error = PTR_ERR(s);
  644. goto out_err_nosb;
  645. }
  646. if (s->s_fs_info != server) {
  647. nfs_free_server(server);
  648. server = NULL;
  649. }
  650. if (!s->s_root) {
  651. /* initial superblock/root creation */
  652. s->s_flags = flags;
  653. nfs_clone_super(s, data->sb);
  654. }
  655. mntroot = nfs_get_root(s, data->fh);
  656. if (IS_ERR(mntroot)) {
  657. error = PTR_ERR(mntroot);
  658. goto error_splat_super;
  659. }
  660. s->s_flags |= MS_ACTIVE;
  661. mnt->mnt_sb = s;
  662. mnt->mnt_root = mntroot;
  663. dprintk("<-- nfs_xdev_get_sb() = 0\n");
  664. return 0;
  665. out_err_nosb:
  666. nfs_free_server(server);
  667. out_err_noserver:
  668. dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
  669. return error;
  670. error_splat_super:
  671. up_write(&s->s_umount);
  672. deactivate_super(s);
  673. dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
  674. return error;
  675. }
  676. #ifdef CONFIG_NFS_V4
  677. /*
  678. * Finish setting up a cloned NFS4 superblock
  679. */
  680. static void nfs4_clone_super(struct super_block *sb,
  681. const struct super_block *old_sb)
  682. {
  683. sb->s_blocksize_bits = old_sb->s_blocksize_bits;
  684. sb->s_blocksize = old_sb->s_blocksize;
  685. sb->s_maxbytes = old_sb->s_maxbytes;
  686. sb->s_time_gran = 1;
  687. sb->s_op = old_sb->s_op;
  688. nfs_initialise_sb(sb);
  689. }
  690. /*
  691. * Set up an NFS4 superblock
  692. */
  693. static void nfs4_fill_super(struct super_block *sb)
  694. {
  695. sb->s_time_gran = 1;
  696. sb->s_op = &nfs4_sops;
  697. nfs_initialise_sb(sb);
  698. }
  699. /*
  700. * Get the superblock for an NFS4 mountpoint
  701. */
  702. static int nfs4_get_sb(struct file_system_type *fs_type,
  703. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
  704. {
  705. struct nfs4_mount_data *data = raw_data;
  706. struct super_block *s;
  707. struct nfs_server *server;
  708. struct sockaddr_in addr;
  709. rpc_authflavor_t authflavour;
  710. struct nfs_fh mntfh;
  711. struct dentry *mntroot;
  712. char *p, *mntpath = NULL, *hostname = NULL, *ip_addr = NULL;
  713. int error;
  714. if (data == NULL) {
  715. dprintk("%s: missing data argument\n", __FUNCTION__);
  716. return -EINVAL;
  717. }
  718. if (data->version <= 0 || data->version > NFS4_MOUNT_VERSION) {
  719. dprintk("%s: bad mount version\n", __FUNCTION__);
  720. return -EINVAL;
  721. }
  722. /* We now require that the mount process passes the remote address */
  723. if (data->host_addrlen != sizeof(addr))
  724. return -EINVAL;
  725. if (copy_from_user(&addr, data->host_addr, sizeof(addr)))
  726. return -EFAULT;
  727. if (!nfs_verify_server_address((struct sockaddr *) &addr)) {
  728. dprintk("%s: mount program didn't pass remote IP address!\n",
  729. __FUNCTION__);
  730. return -EINVAL;
  731. }
  732. /* RFC3530: The default port for NFS is 2049 */
  733. if (addr.sin_port == 0)
  734. addr.sin_port = htons(NFS_PORT);
  735. /* Grab the authentication type */
  736. authflavour = RPC_AUTH_UNIX;
  737. if (data->auth_flavourlen != 0) {
  738. if (data->auth_flavourlen != 1) {
  739. dprintk("%s: Invalid number of RPC auth flavours %d.\n",
  740. __FUNCTION__, data->auth_flavourlen);
  741. error = -EINVAL;
  742. goto out;
  743. }
  744. if (copy_from_user(&authflavour, data->auth_flavours,
  745. sizeof(authflavour))) {
  746. error = -EFAULT;
  747. goto out;
  748. }
  749. }
  750. p = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
  751. if (IS_ERR(p))
  752. goto out_err;
  753. hostname = p;
  754. p = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
  755. if (IS_ERR(p))
  756. goto out_err;
  757. mntpath = p;
  758. dprintk("MNTPATH: %s\n", mntpath);
  759. p = strndup_user(data->client_addr.data, 16);
  760. if (IS_ERR(p))
  761. goto out_err;
  762. ip_addr = p;
  763. /* Get a volume representation */
  764. server = nfs4_create_server(data, hostname, &addr, mntpath, ip_addr,
  765. authflavour, &mntfh);
  766. if (IS_ERR(server)) {
  767. error = PTR_ERR(server);
  768. goto out;
  769. }
  770. /* Get a superblock - note that we may end up sharing one that already exists */
  771. s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
  772. if (IS_ERR(s)) {
  773. error = PTR_ERR(s);
  774. goto out_free;
  775. }
  776. if (s->s_fs_info != server) {
  777. nfs_free_server(server);
  778. server = NULL;
  779. }
  780. if (!s->s_root) {
  781. /* initial superblock/root creation */
  782. s->s_flags = flags;
  783. nfs4_fill_super(s);
  784. }
  785. mntroot = nfs4_get_root(s, &mntfh);
  786. if (IS_ERR(mntroot)) {
  787. error = PTR_ERR(mntroot);
  788. goto error_splat_super;
  789. }
  790. s->s_flags |= MS_ACTIVE;
  791. mnt->mnt_sb = s;
  792. mnt->mnt_root = mntroot;
  793. error = 0;
  794. out:
  795. kfree(ip_addr);
  796. kfree(mntpath);
  797. kfree(hostname);
  798. return error;
  799. out_err:
  800. error = PTR_ERR(p);
  801. goto out;
  802. out_free:
  803. nfs_free_server(server);
  804. goto out;
  805. error_splat_super:
  806. up_write(&s->s_umount);
  807. deactivate_super(s);
  808. goto out;
  809. }
  810. static void nfs4_kill_super(struct super_block *sb)
  811. {
  812. struct nfs_server *server = NFS_SB(sb);
  813. nfs_return_all_delegations(sb);
  814. kill_anon_super(sb);
  815. nfs4_renewd_prepare_shutdown(server);
  816. nfs_free_server(server);
  817. }
  818. /*
  819. * Clone an NFS4 server record on xdev traversal (FSID-change)
  820. */
  821. static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
  822. const char *dev_name, void *raw_data,
  823. struct vfsmount *mnt)
  824. {
  825. struct nfs_clone_mount *data = raw_data;
  826. struct super_block *s;
  827. struct nfs_server *server;
  828. struct dentry *mntroot;
  829. int error;
  830. dprintk("--> nfs4_xdev_get_sb()\n");
  831. /* create a new volume representation */
  832. server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
  833. if (IS_ERR(server)) {
  834. error = PTR_ERR(server);
  835. goto out_err_noserver;
  836. }
  837. /* Get a superblock - note that we may end up sharing one that already exists */
  838. s = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
  839. if (IS_ERR(s)) {
  840. error = PTR_ERR(s);
  841. goto out_err_nosb;
  842. }
  843. if (s->s_fs_info != server) {
  844. nfs_free_server(server);
  845. server = NULL;
  846. }
  847. if (!s->s_root) {
  848. /* initial superblock/root creation */
  849. s->s_flags = flags;
  850. nfs4_clone_super(s, data->sb);
  851. }
  852. mntroot = nfs4_get_root(s, data->fh);
  853. if (IS_ERR(mntroot)) {
  854. error = PTR_ERR(mntroot);
  855. goto error_splat_super;
  856. }
  857. s->s_flags |= MS_ACTIVE;
  858. mnt->mnt_sb = s;
  859. mnt->mnt_root = mntroot;
  860. dprintk("<-- nfs4_xdev_get_sb() = 0\n");
  861. return 0;
  862. out_err_nosb:
  863. nfs_free_server(server);
  864. out_err_noserver:
  865. dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
  866. return error;
  867. error_splat_super:
  868. up_write(&s->s_umount);
  869. deactivate_super(s);
  870. dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
  871. return error;
  872. }
  873. /*
  874. * Create an NFS4 server record on referral traversal
  875. */
  876. static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
  877. const char *dev_name, void *raw_data,
  878. struct vfsmount *mnt)
  879. {
  880. struct nfs_clone_mount *data = raw_data;
  881. struct super_block *s;
  882. struct nfs_server *server;
  883. struct dentry *mntroot;
  884. struct nfs_fh mntfh;
  885. int error;
  886. dprintk("--> nfs4_referral_get_sb()\n");
  887. /* create a new volume representation */
  888. server = nfs4_create_referral_server(data, &mntfh);
  889. if (IS_ERR(server)) {
  890. error = PTR_ERR(server);
  891. goto out_err_noserver;
  892. }
  893. /* Get a superblock - note that we may end up sharing one that already exists */
  894. s = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
  895. if (IS_ERR(s)) {
  896. error = PTR_ERR(s);
  897. goto out_err_nosb;
  898. }
  899. if (s->s_fs_info != server) {
  900. nfs_free_server(server);
  901. server = NULL;
  902. }
  903. if (!s->s_root) {
  904. /* initial superblock/root creation */
  905. s->s_flags = flags;
  906. nfs4_fill_super(s);
  907. }
  908. mntroot = nfs4_get_root(s, &mntfh);
  909. if (IS_ERR(mntroot)) {
  910. error = PTR_ERR(mntroot);
  911. goto error_splat_super;
  912. }
  913. s->s_flags |= MS_ACTIVE;
  914. mnt->mnt_sb = s;
  915. mnt->mnt_root = mntroot;
  916. dprintk("<-- nfs4_referral_get_sb() = 0\n");
  917. return 0;
  918. out_err_nosb:
  919. nfs_free_server(server);
  920. out_err_noserver:
  921. dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
  922. return error;
  923. error_splat_super:
  924. up_write(&s->s_umount);
  925. deactivate_super(s);
  926. dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
  927. return error;
  928. }
  929. #endif /* CONFIG_NFS_V4 */