super.c 26 KB

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