super.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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. { -1, "unknown" }
  237. };
  238. int i;
  239. for (i=0; sec_flavours[i].flavour != -1; 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. * Validate the NFS2/NFS3 mount data
  396. * - fills in the mount root filehandle
  397. */
  398. static int nfs_validate_mount_data(struct nfs_mount_data *data,
  399. struct nfs_fh *mntfh)
  400. {
  401. if (data == NULL) {
  402. dprintk("%s: missing data argument\n", __FUNCTION__);
  403. return -EINVAL;
  404. }
  405. if (data->version <= 0 || data->version > NFS_MOUNT_VERSION) {
  406. dprintk("%s: bad mount version\n", __FUNCTION__);
  407. return -EINVAL;
  408. }
  409. switch (data->version) {
  410. case 1:
  411. data->namlen = 0;
  412. case 2:
  413. data->bsize = 0;
  414. case 3:
  415. if (data->flags & NFS_MOUNT_VER3) {
  416. dprintk("%s: mount structure version %d does not support NFSv3\n",
  417. __FUNCTION__,
  418. data->version);
  419. return -EINVAL;
  420. }
  421. data->root.size = NFS2_FHSIZE;
  422. memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
  423. case 4:
  424. if (data->flags & NFS_MOUNT_SECFLAVOUR) {
  425. dprintk("%s: mount structure version %d does not support strong security\n",
  426. __FUNCTION__,
  427. data->version);
  428. return -EINVAL;
  429. }
  430. case 5:
  431. memset(data->context, 0, sizeof(data->context));
  432. }
  433. /* Set the pseudoflavor */
  434. if (!(data->flags & NFS_MOUNT_SECFLAVOUR))
  435. data->pseudoflavor = RPC_AUTH_UNIX;
  436. #ifndef CONFIG_NFS_V3
  437. /* If NFSv3 is not compiled in, return -EPROTONOSUPPORT */
  438. if (data->flags & NFS_MOUNT_VER3) {
  439. dprintk("%s: NFSv3 not compiled into kernel\n", __FUNCTION__);
  440. return -EPROTONOSUPPORT;
  441. }
  442. #endif /* CONFIG_NFS_V3 */
  443. /* We now require that the mount process passes the remote address */
  444. if (data->addr.sin_addr.s_addr == INADDR_ANY) {
  445. dprintk("%s: mount program didn't pass remote address!\n",
  446. __FUNCTION__);
  447. return -EINVAL;
  448. }
  449. /* Prepare the root filehandle */
  450. if (data->flags & NFS_MOUNT_VER3)
  451. mntfh->size = data->root.size;
  452. else
  453. mntfh->size = NFS2_FHSIZE;
  454. if (mntfh->size > sizeof(mntfh->data)) {
  455. dprintk("%s: invalid root filehandle\n", __FUNCTION__);
  456. return -EINVAL;
  457. }
  458. memcpy(mntfh->data, data->root.data, mntfh->size);
  459. if (mntfh->size < sizeof(mntfh->data))
  460. memset(mntfh->data + mntfh->size, 0,
  461. sizeof(mntfh->data) - mntfh->size);
  462. return 0;
  463. }
  464. /*
  465. * Initialise the common bits of the superblock
  466. */
  467. static inline void nfs_initialise_sb(struct super_block *sb)
  468. {
  469. struct nfs_server *server = NFS_SB(sb);
  470. sb->s_magic = NFS_SUPER_MAGIC;
  471. /* We probably want something more informative here */
  472. snprintf(sb->s_id, sizeof(sb->s_id),
  473. "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
  474. if (sb->s_blocksize == 0)
  475. sb->s_blocksize = nfs_block_bits(server->wsize,
  476. &sb->s_blocksize_bits);
  477. if (server->flags & NFS_MOUNT_NOAC)
  478. sb->s_flags |= MS_SYNCHRONOUS;
  479. nfs_super_set_maxbytes(sb, server->maxfilesize);
  480. }
  481. /*
  482. * Finish setting up an NFS2/3 superblock
  483. */
  484. static void nfs_fill_super(struct super_block *sb, struct nfs_mount_data *data)
  485. {
  486. struct nfs_server *server = NFS_SB(sb);
  487. sb->s_blocksize_bits = 0;
  488. sb->s_blocksize = 0;
  489. if (data->bsize)
  490. sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
  491. if (server->flags & NFS_MOUNT_VER3) {
  492. /* The VFS shouldn't apply the umask to mode bits. We will do
  493. * so ourselves when necessary.
  494. */
  495. sb->s_flags |= MS_POSIXACL;
  496. sb->s_time_gran = 1;
  497. }
  498. sb->s_op = &nfs_sops;
  499. nfs_initialise_sb(sb);
  500. }
  501. /*
  502. * Finish setting up a cloned NFS2/3 superblock
  503. */
  504. static void nfs_clone_super(struct super_block *sb,
  505. const struct super_block *old_sb)
  506. {
  507. struct nfs_server *server = NFS_SB(sb);
  508. sb->s_blocksize_bits = old_sb->s_blocksize_bits;
  509. sb->s_blocksize = old_sb->s_blocksize;
  510. sb->s_maxbytes = old_sb->s_maxbytes;
  511. if (server->flags & NFS_MOUNT_VER3) {
  512. /* The VFS shouldn't apply the umask to mode bits. We will do
  513. * so ourselves when necessary.
  514. */
  515. sb->s_flags |= MS_POSIXACL;
  516. sb->s_time_gran = 1;
  517. }
  518. sb->s_op = old_sb->s_op;
  519. nfs_initialise_sb(sb);
  520. }
  521. static int nfs_set_super(struct super_block *s, void *_server)
  522. {
  523. struct nfs_server *server = _server;
  524. int ret;
  525. s->s_fs_info = server;
  526. ret = set_anon_super(s, server);
  527. if (ret == 0)
  528. server->s_dev = s->s_dev;
  529. return ret;
  530. }
  531. static int nfs_compare_super(struct super_block *sb, void *data)
  532. {
  533. struct nfs_server *server = data, *old = NFS_SB(sb);
  534. if (old->nfs_client != server->nfs_client)
  535. return 0;
  536. if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
  537. return 0;
  538. return 1;
  539. }
  540. static int nfs_get_sb(struct file_system_type *fs_type,
  541. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
  542. {
  543. struct nfs_server *server = NULL;
  544. struct super_block *s;
  545. struct nfs_fh mntfh;
  546. struct nfs_mount_data *data = raw_data;
  547. struct dentry *mntroot;
  548. int error;
  549. /* Validate the mount data */
  550. error = nfs_validate_mount_data(data, &mntfh);
  551. if (error < 0)
  552. goto out;
  553. /* Get a volume representation */
  554. server = nfs_create_server(data, &mntfh);
  555. if (IS_ERR(server)) {
  556. error = PTR_ERR(server);
  557. goto out;
  558. }
  559. /* Get a superblock - note that we may end up sharing one that already exists */
  560. s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
  561. if (IS_ERR(s)) {
  562. error = PTR_ERR(s);
  563. goto out_err_nosb;
  564. }
  565. if (s->s_fs_info != server) {
  566. nfs_free_server(server);
  567. server = NULL;
  568. }
  569. if (!s->s_root) {
  570. /* initial superblock/root creation */
  571. s->s_flags = flags;
  572. nfs_fill_super(s, data);
  573. }
  574. mntroot = nfs_get_root(s, &mntfh);
  575. if (IS_ERR(mntroot)) {
  576. error = PTR_ERR(mntroot);
  577. goto error_splat_super;
  578. }
  579. s->s_flags |= MS_ACTIVE;
  580. mnt->mnt_sb = s;
  581. mnt->mnt_root = mntroot;
  582. error = 0;
  583. out:
  584. return error;
  585. out_err_nosb:
  586. nfs_free_server(server);
  587. goto out;
  588. error_splat_super:
  589. up_write(&s->s_umount);
  590. deactivate_super(s);
  591. goto out;
  592. }
  593. /*
  594. * Destroy an NFS2/3 superblock
  595. */
  596. static void nfs_kill_super(struct super_block *s)
  597. {
  598. struct nfs_server *server = NFS_SB(s);
  599. kill_anon_super(s);
  600. nfs_free_server(server);
  601. }
  602. /*
  603. * Clone an NFS2/3 server record on xdev traversal (FSID-change)
  604. */
  605. static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
  606. const char *dev_name, void *raw_data,
  607. struct vfsmount *mnt)
  608. {
  609. struct nfs_clone_mount *data = raw_data;
  610. struct super_block *s;
  611. struct nfs_server *server;
  612. struct dentry *mntroot;
  613. int error;
  614. dprintk("--> nfs_xdev_get_sb()\n");
  615. /* create a new volume representation */
  616. server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
  617. if (IS_ERR(server)) {
  618. error = PTR_ERR(server);
  619. goto out_err_noserver;
  620. }
  621. /* Get a superblock - note that we may end up sharing one that already exists */
  622. s = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
  623. if (IS_ERR(s)) {
  624. error = PTR_ERR(s);
  625. goto out_err_nosb;
  626. }
  627. if (s->s_fs_info != server) {
  628. nfs_free_server(server);
  629. server = NULL;
  630. }
  631. if (!s->s_root) {
  632. /* initial superblock/root creation */
  633. s->s_flags = flags;
  634. nfs_clone_super(s, data->sb);
  635. }
  636. mntroot = nfs_get_root(s, data->fh);
  637. if (IS_ERR(mntroot)) {
  638. error = PTR_ERR(mntroot);
  639. goto error_splat_super;
  640. }
  641. s->s_flags |= MS_ACTIVE;
  642. mnt->mnt_sb = s;
  643. mnt->mnt_root = mntroot;
  644. dprintk("<-- nfs_xdev_get_sb() = 0\n");
  645. return 0;
  646. out_err_nosb:
  647. nfs_free_server(server);
  648. out_err_noserver:
  649. dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
  650. return error;
  651. error_splat_super:
  652. up_write(&s->s_umount);
  653. deactivate_super(s);
  654. dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
  655. return error;
  656. }
  657. #ifdef CONFIG_NFS_V4
  658. /*
  659. * Finish setting up a cloned NFS4 superblock
  660. */
  661. static void nfs4_clone_super(struct super_block *sb,
  662. const struct super_block *old_sb)
  663. {
  664. sb->s_blocksize_bits = old_sb->s_blocksize_bits;
  665. sb->s_blocksize = old_sb->s_blocksize;
  666. sb->s_maxbytes = old_sb->s_maxbytes;
  667. sb->s_time_gran = 1;
  668. sb->s_op = old_sb->s_op;
  669. nfs_initialise_sb(sb);
  670. }
  671. /*
  672. * Set up an NFS4 superblock
  673. */
  674. static void nfs4_fill_super(struct super_block *sb)
  675. {
  676. sb->s_time_gran = 1;
  677. sb->s_op = &nfs4_sops;
  678. nfs_initialise_sb(sb);
  679. }
  680. /*
  681. * Get the superblock for an NFS4 mountpoint
  682. */
  683. static int nfs4_get_sb(struct file_system_type *fs_type,
  684. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
  685. {
  686. struct nfs4_mount_data *data = raw_data;
  687. struct super_block *s;
  688. struct nfs_server *server;
  689. struct sockaddr_in addr;
  690. rpc_authflavor_t authflavour;
  691. struct nfs_fh mntfh;
  692. struct dentry *mntroot;
  693. char *p, *mntpath = NULL, *hostname = NULL, *ip_addr = NULL;
  694. int error;
  695. if (data == NULL) {
  696. dprintk("%s: missing data argument\n", __FUNCTION__);
  697. return -EINVAL;
  698. }
  699. if (data->version <= 0 || data->version > NFS4_MOUNT_VERSION) {
  700. dprintk("%s: bad mount version\n", __FUNCTION__);
  701. return -EINVAL;
  702. }
  703. /* We now require that the mount process passes the remote address */
  704. if (data->host_addrlen != sizeof(addr))
  705. return -EINVAL;
  706. if (copy_from_user(&addr, data->host_addr, sizeof(addr)))
  707. return -EFAULT;
  708. if (addr.sin_family != AF_INET ||
  709. addr.sin_addr.s_addr == INADDR_ANY
  710. ) {
  711. dprintk("%s: mount program didn't pass remote IP address!\n",
  712. __FUNCTION__);
  713. return -EINVAL;
  714. }
  715. /* RFC3530: The default port for NFS is 2049 */
  716. if (addr.sin_port == 0)
  717. addr.sin_port = htons(NFS_PORT);
  718. /* Grab the authentication type */
  719. authflavour = RPC_AUTH_UNIX;
  720. if (data->auth_flavourlen != 0) {
  721. if (data->auth_flavourlen != 1) {
  722. dprintk("%s: Invalid number of RPC auth flavours %d.\n",
  723. __FUNCTION__, data->auth_flavourlen);
  724. error = -EINVAL;
  725. goto out;
  726. }
  727. if (copy_from_user(&authflavour, data->auth_flavours,
  728. sizeof(authflavour))) {
  729. error = -EFAULT;
  730. goto out;
  731. }
  732. }
  733. p = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
  734. if (IS_ERR(p))
  735. goto out_err;
  736. hostname = p;
  737. p = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
  738. if (IS_ERR(p))
  739. goto out_err;
  740. mntpath = p;
  741. dprintk("MNTPATH: %s\n", mntpath);
  742. p = strndup_user(data->client_addr.data, 16);
  743. if (IS_ERR(p))
  744. goto out_err;
  745. ip_addr = p;
  746. /* Get a volume representation */
  747. server = nfs4_create_server(data, hostname, &addr, mntpath, ip_addr,
  748. authflavour, &mntfh);
  749. if (IS_ERR(server)) {
  750. error = PTR_ERR(server);
  751. goto out;
  752. }
  753. /* Get a superblock - note that we may end up sharing one that already exists */
  754. s = sget(fs_type, nfs_compare_super, nfs_set_super, server);
  755. if (IS_ERR(s)) {
  756. error = PTR_ERR(s);
  757. goto out_free;
  758. }
  759. if (s->s_fs_info != server) {
  760. nfs_free_server(server);
  761. server = NULL;
  762. }
  763. if (!s->s_root) {
  764. /* initial superblock/root creation */
  765. s->s_flags = flags;
  766. nfs4_fill_super(s);
  767. }
  768. mntroot = nfs4_get_root(s, &mntfh);
  769. if (IS_ERR(mntroot)) {
  770. error = PTR_ERR(mntroot);
  771. goto error_splat_super;
  772. }
  773. s->s_flags |= MS_ACTIVE;
  774. mnt->mnt_sb = s;
  775. mnt->mnt_root = mntroot;
  776. error = 0;
  777. out:
  778. kfree(ip_addr);
  779. kfree(mntpath);
  780. kfree(hostname);
  781. return error;
  782. out_err:
  783. error = PTR_ERR(p);
  784. goto out;
  785. out_free:
  786. nfs_free_server(server);
  787. goto out;
  788. error_splat_super:
  789. up_write(&s->s_umount);
  790. deactivate_super(s);
  791. goto out;
  792. }
  793. static void nfs4_kill_super(struct super_block *sb)
  794. {
  795. struct nfs_server *server = NFS_SB(sb);
  796. nfs_return_all_delegations(sb);
  797. kill_anon_super(sb);
  798. nfs4_renewd_prepare_shutdown(server);
  799. nfs_free_server(server);
  800. }
  801. /*
  802. * Clone an NFS4 server record on xdev traversal (FSID-change)
  803. */
  804. static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
  805. const char *dev_name, void *raw_data,
  806. struct vfsmount *mnt)
  807. {
  808. struct nfs_clone_mount *data = raw_data;
  809. struct super_block *s;
  810. struct nfs_server *server;
  811. struct dentry *mntroot;
  812. int error;
  813. dprintk("--> nfs4_xdev_get_sb()\n");
  814. /* create a new volume representation */
  815. server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
  816. if (IS_ERR(server)) {
  817. error = PTR_ERR(server);
  818. goto out_err_noserver;
  819. }
  820. /* Get a superblock - note that we may end up sharing one that already exists */
  821. s = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
  822. if (IS_ERR(s)) {
  823. error = PTR_ERR(s);
  824. goto out_err_nosb;
  825. }
  826. if (s->s_fs_info != server) {
  827. nfs_free_server(server);
  828. server = NULL;
  829. }
  830. if (!s->s_root) {
  831. /* initial superblock/root creation */
  832. s->s_flags = flags;
  833. nfs4_clone_super(s, data->sb);
  834. }
  835. mntroot = nfs4_get_root(s, data->fh);
  836. if (IS_ERR(mntroot)) {
  837. error = PTR_ERR(mntroot);
  838. goto error_splat_super;
  839. }
  840. s->s_flags |= MS_ACTIVE;
  841. mnt->mnt_sb = s;
  842. mnt->mnt_root = mntroot;
  843. dprintk("<-- nfs4_xdev_get_sb() = 0\n");
  844. return 0;
  845. out_err_nosb:
  846. nfs_free_server(server);
  847. out_err_noserver:
  848. dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
  849. return error;
  850. error_splat_super:
  851. up_write(&s->s_umount);
  852. deactivate_super(s);
  853. dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
  854. return error;
  855. }
  856. /*
  857. * Create an NFS4 server record on referral traversal
  858. */
  859. static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
  860. const char *dev_name, void *raw_data,
  861. struct vfsmount *mnt)
  862. {
  863. struct nfs_clone_mount *data = raw_data;
  864. struct super_block *s;
  865. struct nfs_server *server;
  866. struct dentry *mntroot;
  867. struct nfs_fh mntfh;
  868. int error;
  869. dprintk("--> nfs4_referral_get_sb()\n");
  870. /* create a new volume representation */
  871. server = nfs4_create_referral_server(data, &mntfh);
  872. if (IS_ERR(server)) {
  873. error = PTR_ERR(server);
  874. goto out_err_noserver;
  875. }
  876. /* Get a superblock - note that we may end up sharing one that already exists */
  877. s = sget(&nfs_fs_type, nfs_compare_super, nfs_set_super, server);
  878. if (IS_ERR(s)) {
  879. error = PTR_ERR(s);
  880. goto out_err_nosb;
  881. }
  882. if (s->s_fs_info != server) {
  883. nfs_free_server(server);
  884. server = NULL;
  885. }
  886. if (!s->s_root) {
  887. /* initial superblock/root creation */
  888. s->s_flags = flags;
  889. nfs4_fill_super(s);
  890. }
  891. mntroot = nfs4_get_root(s, &mntfh);
  892. if (IS_ERR(mntroot)) {
  893. error = PTR_ERR(mntroot);
  894. goto error_splat_super;
  895. }
  896. s->s_flags |= MS_ACTIVE;
  897. mnt->mnt_sb = s;
  898. mnt->mnt_root = mntroot;
  899. dprintk("<-- nfs4_referral_get_sb() = 0\n");
  900. return 0;
  901. out_err_nosb:
  902. nfs_free_server(server);
  903. out_err_noserver:
  904. dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
  905. return error;
  906. error_splat_super:
  907. up_write(&s->s_umount);
  908. deactivate_super(s);
  909. dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
  910. return error;
  911. }
  912. #endif /* CONFIG_NFS_V4 */