super.c 26 KB

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