super.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611
  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@lxorguk.ukuu.org.uk>, 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/sunrpc/xprtsock.h>
  35. #include <linux/sunrpc/xprtrdma.h>
  36. #include <linux/nfs_fs.h>
  37. #include <linux/nfs_mount.h>
  38. #include <linux/nfs4_mount.h>
  39. #include <linux/lockd/bind.h>
  40. #include <linux/smp_lock.h>
  41. #include <linux/seq_file.h>
  42. #include <linux/mount.h>
  43. #include <linux/nfs_idmap.h>
  44. #include <linux/vfs.h>
  45. #include <linux/inet.h>
  46. #include <linux/in6.h>
  47. #include <net/ipv6.h>
  48. #include <linux/netdevice.h>
  49. #include <linux/nfs_xdr.h>
  50. #include <linux/magic.h>
  51. #include <linux/parser.h>
  52. #include <asm/system.h>
  53. #include <asm/uaccess.h>
  54. #include "nfs4_fs.h"
  55. #include "callback.h"
  56. #include "delegation.h"
  57. #include "iostat.h"
  58. #include "internal.h"
  59. #define NFSDBG_FACILITY NFSDBG_VFS
  60. enum {
  61. /* Mount options that take no arguments */
  62. Opt_soft, Opt_hard,
  63. Opt_posix, Opt_noposix,
  64. Opt_cto, Opt_nocto,
  65. Opt_ac, Opt_noac,
  66. Opt_lock, Opt_nolock,
  67. Opt_v2, Opt_v3,
  68. Opt_udp, Opt_tcp, Opt_rdma,
  69. Opt_acl, Opt_noacl,
  70. Opt_rdirplus, Opt_nordirplus,
  71. Opt_sharecache, Opt_nosharecache,
  72. Opt_resvport, Opt_noresvport,
  73. /* Mount options that take integer arguments */
  74. Opt_port,
  75. Opt_rsize, Opt_wsize, Opt_bsize,
  76. Opt_timeo, Opt_retrans,
  77. Opt_acregmin, Opt_acregmax,
  78. Opt_acdirmin, Opt_acdirmax,
  79. Opt_actimeo,
  80. Opt_namelen,
  81. Opt_mountport,
  82. Opt_mountvers,
  83. Opt_nfsvers,
  84. /* Mount options that take string arguments */
  85. Opt_sec, Opt_proto, Opt_mountproto, Opt_mounthost,
  86. Opt_addr, Opt_mountaddr, Opt_clientaddr,
  87. Opt_lookupcache,
  88. /* Special mount options */
  89. Opt_userspace, Opt_deprecated, Opt_sloppy,
  90. Opt_err
  91. };
  92. static const match_table_t nfs_mount_option_tokens = {
  93. { Opt_userspace, "bg" },
  94. { Opt_userspace, "fg" },
  95. { Opt_userspace, "retry=%s" },
  96. { Opt_sloppy, "sloppy" },
  97. { Opt_soft, "soft" },
  98. { Opt_hard, "hard" },
  99. { Opt_deprecated, "intr" },
  100. { Opt_deprecated, "nointr" },
  101. { Opt_posix, "posix" },
  102. { Opt_noposix, "noposix" },
  103. { Opt_cto, "cto" },
  104. { Opt_nocto, "nocto" },
  105. { Opt_ac, "ac" },
  106. { Opt_noac, "noac" },
  107. { Opt_lock, "lock" },
  108. { Opt_nolock, "nolock" },
  109. { Opt_v2, "v2" },
  110. { Opt_v3, "v3" },
  111. { Opt_udp, "udp" },
  112. { Opt_tcp, "tcp" },
  113. { Opt_rdma, "rdma" },
  114. { Opt_acl, "acl" },
  115. { Opt_noacl, "noacl" },
  116. { Opt_rdirplus, "rdirplus" },
  117. { Opt_nordirplus, "nordirplus" },
  118. { Opt_sharecache, "sharecache" },
  119. { Opt_nosharecache, "nosharecache" },
  120. { Opt_resvport, "resvport" },
  121. { Opt_noresvport, "noresvport" },
  122. { Opt_port, "port=%u" },
  123. { Opt_rsize, "rsize=%u" },
  124. { Opt_wsize, "wsize=%u" },
  125. { Opt_bsize, "bsize=%u" },
  126. { Opt_timeo, "timeo=%u" },
  127. { Opt_retrans, "retrans=%u" },
  128. { Opt_acregmin, "acregmin=%u" },
  129. { Opt_acregmax, "acregmax=%u" },
  130. { Opt_acdirmin, "acdirmin=%u" },
  131. { Opt_acdirmax, "acdirmax=%u" },
  132. { Opt_actimeo, "actimeo=%u" },
  133. { Opt_namelen, "namlen=%u" },
  134. { Opt_mountport, "mountport=%u" },
  135. { Opt_mountvers, "mountvers=%u" },
  136. { Opt_nfsvers, "nfsvers=%u" },
  137. { Opt_nfsvers, "vers=%u" },
  138. { Opt_sec, "sec=%s" },
  139. { Opt_proto, "proto=%s" },
  140. { Opt_mountproto, "mountproto=%s" },
  141. { Opt_addr, "addr=%s" },
  142. { Opt_clientaddr, "clientaddr=%s" },
  143. { Opt_mounthost, "mounthost=%s" },
  144. { Opt_mountaddr, "mountaddr=%s" },
  145. { Opt_lookupcache, "lookupcache=%s" },
  146. { Opt_err, NULL }
  147. };
  148. enum {
  149. Opt_xprt_udp, Opt_xprt_tcp, Opt_xprt_rdma,
  150. Opt_xprt_err
  151. };
  152. static const match_table_t nfs_xprt_protocol_tokens = {
  153. { Opt_xprt_udp, "udp" },
  154. { Opt_xprt_tcp, "tcp" },
  155. { Opt_xprt_rdma, "rdma" },
  156. { Opt_xprt_err, NULL }
  157. };
  158. enum {
  159. Opt_sec_none, Opt_sec_sys,
  160. Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
  161. Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
  162. Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,
  163. Opt_sec_err
  164. };
  165. static const match_table_t nfs_secflavor_tokens = {
  166. { Opt_sec_none, "none" },
  167. { Opt_sec_none, "null" },
  168. { Opt_sec_sys, "sys" },
  169. { Opt_sec_krb5, "krb5" },
  170. { Opt_sec_krb5i, "krb5i" },
  171. { Opt_sec_krb5p, "krb5p" },
  172. { Opt_sec_lkey, "lkey" },
  173. { Opt_sec_lkeyi, "lkeyi" },
  174. { Opt_sec_lkeyp, "lkeyp" },
  175. { Opt_sec_spkm, "spkm3" },
  176. { Opt_sec_spkmi, "spkm3i" },
  177. { Opt_sec_spkmp, "spkm3p" },
  178. { Opt_sec_err, NULL }
  179. };
  180. enum {
  181. Opt_lookupcache_all, Opt_lookupcache_positive,
  182. Opt_lookupcache_none,
  183. Opt_lookupcache_err
  184. };
  185. static match_table_t nfs_lookupcache_tokens = {
  186. { Opt_lookupcache_all, "all" },
  187. { Opt_lookupcache_positive, "pos" },
  188. { Opt_lookupcache_positive, "positive" },
  189. { Opt_lookupcache_none, "none" },
  190. { Opt_lookupcache_err, NULL }
  191. };
  192. static void nfs_umount_begin(struct super_block *);
  193. static int nfs_statfs(struct dentry *, struct kstatfs *);
  194. static int nfs_show_options(struct seq_file *, struct vfsmount *);
  195. static int nfs_show_stats(struct seq_file *, struct vfsmount *);
  196. static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
  197. static int nfs_xdev_get_sb(struct file_system_type *fs_type,
  198. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
  199. static void nfs_kill_super(struct super_block *);
  200. static int nfs_remount(struct super_block *sb, int *flags, char *raw_data);
  201. static struct file_system_type nfs_fs_type = {
  202. .owner = THIS_MODULE,
  203. .name = "nfs",
  204. .get_sb = nfs_get_sb,
  205. .kill_sb = nfs_kill_super,
  206. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  207. };
  208. struct file_system_type nfs_xdev_fs_type = {
  209. .owner = THIS_MODULE,
  210. .name = "nfs",
  211. .get_sb = nfs_xdev_get_sb,
  212. .kill_sb = nfs_kill_super,
  213. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  214. };
  215. static const struct super_operations nfs_sops = {
  216. .alloc_inode = nfs_alloc_inode,
  217. .destroy_inode = nfs_destroy_inode,
  218. .write_inode = nfs_write_inode,
  219. .statfs = nfs_statfs,
  220. .clear_inode = nfs_clear_inode,
  221. .umount_begin = nfs_umount_begin,
  222. .show_options = nfs_show_options,
  223. .show_stats = nfs_show_stats,
  224. .remount_fs = nfs_remount,
  225. };
  226. #ifdef CONFIG_NFS_V4
  227. static int nfs4_get_sb(struct file_system_type *fs_type,
  228. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
  229. static int nfs4_xdev_get_sb(struct file_system_type *fs_type,
  230. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
  231. static int nfs4_referral_get_sb(struct file_system_type *fs_type,
  232. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
  233. static void nfs4_kill_super(struct super_block *sb);
  234. static struct file_system_type nfs4_fs_type = {
  235. .owner = THIS_MODULE,
  236. .name = "nfs4",
  237. .get_sb = nfs4_get_sb,
  238. .kill_sb = nfs4_kill_super,
  239. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  240. };
  241. struct file_system_type nfs4_xdev_fs_type = {
  242. .owner = THIS_MODULE,
  243. .name = "nfs4",
  244. .get_sb = nfs4_xdev_get_sb,
  245. .kill_sb = nfs4_kill_super,
  246. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  247. };
  248. struct file_system_type nfs4_referral_fs_type = {
  249. .owner = THIS_MODULE,
  250. .name = "nfs4",
  251. .get_sb = nfs4_referral_get_sb,
  252. .kill_sb = nfs4_kill_super,
  253. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  254. };
  255. static const struct super_operations nfs4_sops = {
  256. .alloc_inode = nfs_alloc_inode,
  257. .destroy_inode = nfs_destroy_inode,
  258. .write_inode = nfs_write_inode,
  259. .statfs = nfs_statfs,
  260. .clear_inode = nfs4_clear_inode,
  261. .umount_begin = nfs_umount_begin,
  262. .show_options = nfs_show_options,
  263. .show_stats = nfs_show_stats,
  264. .remount_fs = nfs_remount,
  265. };
  266. #endif
  267. static struct shrinker acl_shrinker = {
  268. .shrink = nfs_access_cache_shrinker,
  269. .seeks = DEFAULT_SEEKS,
  270. };
  271. /*
  272. * Register the NFS filesystems
  273. */
  274. int __init register_nfs_fs(void)
  275. {
  276. int ret;
  277. ret = register_filesystem(&nfs_fs_type);
  278. if (ret < 0)
  279. goto error_0;
  280. ret = nfs_register_sysctl();
  281. if (ret < 0)
  282. goto error_1;
  283. #ifdef CONFIG_NFS_V4
  284. ret = register_filesystem(&nfs4_fs_type);
  285. if (ret < 0)
  286. goto error_2;
  287. #endif
  288. register_shrinker(&acl_shrinker);
  289. return 0;
  290. #ifdef CONFIG_NFS_V4
  291. error_2:
  292. nfs_unregister_sysctl();
  293. #endif
  294. error_1:
  295. unregister_filesystem(&nfs_fs_type);
  296. error_0:
  297. return ret;
  298. }
  299. /*
  300. * Unregister the NFS filesystems
  301. */
  302. void __exit unregister_nfs_fs(void)
  303. {
  304. unregister_shrinker(&acl_shrinker);
  305. #ifdef CONFIG_NFS_V4
  306. unregister_filesystem(&nfs4_fs_type);
  307. #endif
  308. nfs_unregister_sysctl();
  309. unregister_filesystem(&nfs_fs_type);
  310. }
  311. void nfs_sb_active(struct super_block *sb)
  312. {
  313. struct nfs_server *server = NFS_SB(sb);
  314. if (atomic_inc_return(&server->active) == 1)
  315. atomic_inc(&sb->s_active);
  316. }
  317. void nfs_sb_deactive(struct super_block *sb)
  318. {
  319. struct nfs_server *server = NFS_SB(sb);
  320. if (atomic_dec_and_test(&server->active))
  321. deactivate_super(sb);
  322. }
  323. /*
  324. * Deliver file system statistics to userspace
  325. */
  326. static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  327. {
  328. struct nfs_server *server = NFS_SB(dentry->d_sb);
  329. unsigned char blockbits;
  330. unsigned long blockres;
  331. struct nfs_fh *fh = NFS_FH(dentry->d_inode);
  332. struct nfs_fattr fattr;
  333. struct nfs_fsstat res = {
  334. .fattr = &fattr,
  335. };
  336. int error;
  337. error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
  338. if (error < 0)
  339. goto out_err;
  340. buf->f_type = NFS_SUPER_MAGIC;
  341. /*
  342. * Current versions of glibc do not correctly handle the
  343. * case where f_frsize != f_bsize. Eventually we want to
  344. * report the value of wtmult in this field.
  345. */
  346. buf->f_frsize = dentry->d_sb->s_blocksize;
  347. /*
  348. * On most *nix systems, f_blocks, f_bfree, and f_bavail
  349. * are reported in units of f_frsize. Linux hasn't had
  350. * an f_frsize field in its statfs struct until recently,
  351. * thus historically Linux's sys_statfs reports these
  352. * fields in units of f_bsize.
  353. */
  354. buf->f_bsize = dentry->d_sb->s_blocksize;
  355. blockbits = dentry->d_sb->s_blocksize_bits;
  356. blockres = (1 << blockbits) - 1;
  357. buf->f_blocks = (res.tbytes + blockres) >> blockbits;
  358. buf->f_bfree = (res.fbytes + blockres) >> blockbits;
  359. buf->f_bavail = (res.abytes + blockres) >> blockbits;
  360. buf->f_files = res.tfiles;
  361. buf->f_ffree = res.afiles;
  362. buf->f_namelen = server->namelen;
  363. return 0;
  364. out_err:
  365. dprintk("%s: statfs error = %d\n", __func__, -error);
  366. return error;
  367. }
  368. /*
  369. * Map the security flavour number to a name
  370. */
  371. static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
  372. {
  373. static const struct {
  374. rpc_authflavor_t flavour;
  375. const char *str;
  376. } sec_flavours[] = {
  377. { RPC_AUTH_NULL, "null" },
  378. { RPC_AUTH_UNIX, "sys" },
  379. { RPC_AUTH_GSS_KRB5, "krb5" },
  380. { RPC_AUTH_GSS_KRB5I, "krb5i" },
  381. { RPC_AUTH_GSS_KRB5P, "krb5p" },
  382. { RPC_AUTH_GSS_LKEY, "lkey" },
  383. { RPC_AUTH_GSS_LKEYI, "lkeyi" },
  384. { RPC_AUTH_GSS_LKEYP, "lkeyp" },
  385. { RPC_AUTH_GSS_SPKM, "spkm" },
  386. { RPC_AUTH_GSS_SPKMI, "spkmi" },
  387. { RPC_AUTH_GSS_SPKMP, "spkmp" },
  388. { UINT_MAX, "unknown" }
  389. };
  390. int i;
  391. for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
  392. if (sec_flavours[i].flavour == flavour)
  393. break;
  394. }
  395. return sec_flavours[i].str;
  396. }
  397. static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
  398. int showdefaults)
  399. {
  400. struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
  401. switch (sap->sa_family) {
  402. case AF_INET: {
  403. struct sockaddr_in *sin = (struct sockaddr_in *)sap;
  404. seq_printf(m, ",mountaddr=" NIPQUAD_FMT,
  405. NIPQUAD(sin->sin_addr.s_addr));
  406. break;
  407. }
  408. case AF_INET6: {
  409. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
  410. seq_printf(m, ",mountaddr=" NIP6_FMT,
  411. NIP6(sin6->sin6_addr));
  412. break;
  413. }
  414. default:
  415. if (showdefaults)
  416. seq_printf(m, ",mountaddr=unspecified");
  417. }
  418. if (nfss->mountd_version || showdefaults)
  419. seq_printf(m, ",mountvers=%u", nfss->mountd_version);
  420. if (nfss->mountd_port || showdefaults)
  421. seq_printf(m, ",mountport=%u", nfss->mountd_port);
  422. switch (nfss->mountd_protocol) {
  423. case IPPROTO_UDP:
  424. seq_printf(m, ",mountproto=udp");
  425. break;
  426. case IPPROTO_TCP:
  427. seq_printf(m, ",mountproto=tcp");
  428. break;
  429. default:
  430. if (showdefaults)
  431. seq_printf(m, ",mountproto=auto");
  432. }
  433. }
  434. /*
  435. * Describe the mount options in force on this server representation
  436. */
  437. static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
  438. int showdefaults)
  439. {
  440. static const struct proc_nfs_info {
  441. int flag;
  442. const char *str;
  443. const char *nostr;
  444. } nfs_info[] = {
  445. { NFS_MOUNT_SOFT, ",soft", ",hard" },
  446. { NFS_MOUNT_INTR, ",intr", ",nointr" },
  447. { NFS_MOUNT_POSIX, ",posix", "" },
  448. { NFS_MOUNT_NOCTO, ",nocto", "" },
  449. { NFS_MOUNT_NOAC, ",noac", "" },
  450. { NFS_MOUNT_NONLM, ",nolock", "" },
  451. { NFS_MOUNT_NOACL, ",noacl", "" },
  452. { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
  453. { NFS_MOUNT_UNSHARED, ",nosharecache", "" },
  454. { NFS_MOUNT_NORESVPORT, ",noresvport", "" },
  455. { 0, NULL, NULL }
  456. };
  457. const struct proc_nfs_info *nfs_infop;
  458. struct nfs_client *clp = nfss->nfs_client;
  459. u32 version = clp->rpc_ops->version;
  460. seq_printf(m, ",vers=%u", version);
  461. seq_printf(m, ",rsize=%u", nfss->rsize);
  462. seq_printf(m, ",wsize=%u", nfss->wsize);
  463. if (nfss->bsize != 0)
  464. seq_printf(m, ",bsize=%u", nfss->bsize);
  465. seq_printf(m, ",namlen=%u", nfss->namelen);
  466. if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
  467. seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
  468. if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
  469. seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
  470. if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
  471. seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
  472. if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
  473. seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
  474. for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
  475. if (nfss->flags & nfs_infop->flag)
  476. seq_puts(m, nfs_infop->str);
  477. else
  478. seq_puts(m, nfs_infop->nostr);
  479. }
  480. seq_printf(m, ",proto=%s",
  481. rpc_peeraddr2str(nfss->client, RPC_DISPLAY_PROTO));
  482. if (version == 4) {
  483. if (nfss->port != NFS_PORT)
  484. seq_printf(m, ",port=%u", nfss->port);
  485. } else
  486. if (nfss->port)
  487. seq_printf(m, ",port=%u", nfss->port);
  488. seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
  489. seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
  490. seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
  491. if (version != 4)
  492. nfs_show_mountd_options(m, nfss, showdefaults);
  493. #ifdef CONFIG_NFS_V4
  494. if (clp->rpc_ops->version == 4)
  495. seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
  496. #endif
  497. }
  498. /*
  499. * Describe the mount options on this VFS mountpoint
  500. */
  501. static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
  502. {
  503. struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
  504. nfs_show_mount_options(m, nfss, 0);
  505. seq_printf(m, ",addr=%s",
  506. rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
  507. RPC_DISPLAY_ADDR));
  508. return 0;
  509. }
  510. /*
  511. * Present statistical information for this VFS mountpoint
  512. */
  513. static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
  514. {
  515. int i, cpu;
  516. struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
  517. struct rpc_auth *auth = nfss->client->cl_auth;
  518. struct nfs_iostats totals = { };
  519. seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
  520. /*
  521. * Display all mount option settings
  522. */
  523. seq_printf(m, "\n\topts:\t");
  524. seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
  525. seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
  526. seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
  527. seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
  528. nfs_show_mount_options(m, nfss, 1);
  529. seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
  530. seq_printf(m, "\n\tcaps:\t");
  531. seq_printf(m, "caps=0x%x", nfss->caps);
  532. seq_printf(m, ",wtmult=%u", nfss->wtmult);
  533. seq_printf(m, ",dtsize=%u", nfss->dtsize);
  534. seq_printf(m, ",bsize=%u", nfss->bsize);
  535. seq_printf(m, ",namlen=%u", nfss->namelen);
  536. #ifdef CONFIG_NFS_V4
  537. if (nfss->nfs_client->rpc_ops->version == 4) {
  538. seq_printf(m, "\n\tnfsv4:\t");
  539. seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
  540. seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
  541. seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
  542. }
  543. #endif
  544. /*
  545. * Display security flavor in effect for this mount
  546. */
  547. seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
  548. if (auth->au_flavor)
  549. seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
  550. /*
  551. * Display superblock I/O counters
  552. */
  553. for_each_possible_cpu(cpu) {
  554. struct nfs_iostats *stats;
  555. preempt_disable();
  556. stats = per_cpu_ptr(nfss->io_stats, cpu);
  557. for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
  558. totals.events[i] += stats->events[i];
  559. for (i = 0; i < __NFSIOS_BYTESMAX; i++)
  560. totals.bytes[i] += stats->bytes[i];
  561. preempt_enable();
  562. }
  563. seq_printf(m, "\n\tevents:\t");
  564. for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
  565. seq_printf(m, "%lu ", totals.events[i]);
  566. seq_printf(m, "\n\tbytes:\t");
  567. for (i = 0; i < __NFSIOS_BYTESMAX; i++)
  568. seq_printf(m, "%Lu ", totals.bytes[i]);
  569. seq_printf(m, "\n");
  570. rpc_print_iostats(m, nfss->client);
  571. return 0;
  572. }
  573. /*
  574. * Begin unmount by attempting to remove all automounted mountpoints we added
  575. * in response to xdev traversals and referrals
  576. */
  577. static void nfs_umount_begin(struct super_block *sb)
  578. {
  579. struct nfs_server *server = NFS_SB(sb);
  580. struct rpc_clnt *rpc;
  581. /* -EIO all pending I/O */
  582. rpc = server->client_acl;
  583. if (!IS_ERR(rpc))
  584. rpc_killall_tasks(rpc);
  585. rpc = server->client;
  586. if (!IS_ERR(rpc))
  587. rpc_killall_tasks(rpc);
  588. }
  589. /*
  590. * Sanity-check a server address provided by the mount command.
  591. *
  592. * Address family must be initialized, and address must not be
  593. * the ANY address for that family.
  594. */
  595. static int nfs_verify_server_address(struct sockaddr *addr)
  596. {
  597. switch (addr->sa_family) {
  598. case AF_INET: {
  599. struct sockaddr_in *sa = (struct sockaddr_in *)addr;
  600. return sa->sin_addr.s_addr != htonl(INADDR_ANY);
  601. }
  602. case AF_INET6: {
  603. struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
  604. return !ipv6_addr_any(sa);
  605. }
  606. }
  607. return 0;
  608. }
  609. static void nfs_parse_ipv4_address(char *string, size_t str_len,
  610. struct sockaddr *sap, size_t *addr_len)
  611. {
  612. struct sockaddr_in *sin = (struct sockaddr_in *)sap;
  613. u8 *addr = (u8 *)&sin->sin_addr.s_addr;
  614. if (str_len <= INET_ADDRSTRLEN) {
  615. dfprintk(MOUNT, "NFS: parsing IPv4 address %*s\n",
  616. (int)str_len, string);
  617. sin->sin_family = AF_INET;
  618. *addr_len = sizeof(*sin);
  619. if (in4_pton(string, str_len, addr, '\0', NULL))
  620. return;
  621. }
  622. sap->sa_family = AF_UNSPEC;
  623. *addr_len = 0;
  624. }
  625. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  626. static int nfs_parse_ipv6_scope_id(const char *string, const size_t str_len,
  627. const char *delim,
  628. struct sockaddr_in6 *sin6)
  629. {
  630. char *p;
  631. size_t len;
  632. if ((string + str_len) == delim)
  633. return 1;
  634. if (*delim != IPV6_SCOPE_DELIMITER)
  635. return 0;
  636. if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
  637. return 0;
  638. len = (string + str_len) - delim - 1;
  639. p = kstrndup(delim + 1, len, GFP_KERNEL);
  640. if (p) {
  641. unsigned long scope_id = 0;
  642. struct net_device *dev;
  643. dev = dev_get_by_name(&init_net, p);
  644. if (dev != NULL) {
  645. scope_id = dev->ifindex;
  646. dev_put(dev);
  647. } else {
  648. if (strict_strtoul(p, 10, &scope_id) == 0) {
  649. kfree(p);
  650. return 0;
  651. }
  652. }
  653. kfree(p);
  654. sin6->sin6_scope_id = scope_id;
  655. dfprintk(MOUNT, "NFS: IPv6 scope ID = %lu\n", scope_id);
  656. return 1;
  657. }
  658. return 0;
  659. }
  660. static void nfs_parse_ipv6_address(char *string, size_t str_len,
  661. struct sockaddr *sap, size_t *addr_len)
  662. {
  663. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
  664. u8 *addr = (u8 *)&sin6->sin6_addr.in6_u;
  665. const char *delim;
  666. if (str_len <= INET6_ADDRSTRLEN) {
  667. dfprintk(MOUNT, "NFS: parsing IPv6 address %*s\n",
  668. (int)str_len, string);
  669. sin6->sin6_family = AF_INET6;
  670. *addr_len = sizeof(*sin6);
  671. if (in6_pton(string, str_len, addr,
  672. IPV6_SCOPE_DELIMITER, &delim) != 0) {
  673. if (nfs_parse_ipv6_scope_id(string, str_len,
  674. delim, sin6) != 0)
  675. return;
  676. }
  677. }
  678. sap->sa_family = AF_UNSPEC;
  679. *addr_len = 0;
  680. }
  681. #else
  682. static void nfs_parse_ipv6_address(char *string, size_t str_len,
  683. struct sockaddr *sap, size_t *addr_len)
  684. {
  685. sap->sa_family = AF_UNSPEC;
  686. *addr_len = 0;
  687. }
  688. #endif
  689. /*
  690. * Construct a sockaddr based on the contents of a string that contains
  691. * an IP address in presentation format.
  692. *
  693. * If there is a problem constructing the new sockaddr, set the address
  694. * family to AF_UNSPEC.
  695. */
  696. void nfs_parse_ip_address(char *string, size_t str_len,
  697. struct sockaddr *sap, size_t *addr_len)
  698. {
  699. unsigned int i, colons;
  700. colons = 0;
  701. for (i = 0; i < str_len; i++)
  702. if (string[i] == ':')
  703. colons++;
  704. if (colons >= 2)
  705. nfs_parse_ipv6_address(string, str_len, sap, addr_len);
  706. else
  707. nfs_parse_ipv4_address(string, str_len, sap, addr_len);
  708. }
  709. /*
  710. * Sanity check the NFS transport protocol.
  711. *
  712. */
  713. static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
  714. {
  715. switch (mnt->nfs_server.protocol) {
  716. case XPRT_TRANSPORT_UDP:
  717. case XPRT_TRANSPORT_TCP:
  718. case XPRT_TRANSPORT_RDMA:
  719. break;
  720. default:
  721. mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
  722. }
  723. }
  724. /*
  725. * For text based NFSv2/v3 mounts, the mount protocol transport default
  726. * settings should depend upon the specified NFS transport.
  727. */
  728. static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
  729. {
  730. nfs_validate_transport_protocol(mnt);
  731. if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
  732. mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
  733. return;
  734. switch (mnt->nfs_server.protocol) {
  735. case XPRT_TRANSPORT_UDP:
  736. mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
  737. break;
  738. case XPRT_TRANSPORT_TCP:
  739. case XPRT_TRANSPORT_RDMA:
  740. mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
  741. }
  742. }
  743. /*
  744. * Parse the value of the 'sec=' option.
  745. *
  746. * The flavor_len setting is for v4 mounts.
  747. */
  748. static int nfs_parse_security_flavors(char *value,
  749. struct nfs_parsed_mount_data *mnt)
  750. {
  751. substring_t args[MAX_OPT_ARGS];
  752. dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
  753. switch (match_token(value, nfs_secflavor_tokens, args)) {
  754. case Opt_sec_none:
  755. mnt->auth_flavor_len = 0;
  756. mnt->auth_flavors[0] = RPC_AUTH_NULL;
  757. break;
  758. case Opt_sec_sys:
  759. mnt->auth_flavor_len = 0;
  760. mnt->auth_flavors[0] = RPC_AUTH_UNIX;
  761. break;
  762. case Opt_sec_krb5:
  763. mnt->auth_flavor_len = 1;
  764. mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
  765. break;
  766. case Opt_sec_krb5i:
  767. mnt->auth_flavor_len = 1;
  768. mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
  769. break;
  770. case Opt_sec_krb5p:
  771. mnt->auth_flavor_len = 1;
  772. mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
  773. break;
  774. case Opt_sec_lkey:
  775. mnt->auth_flavor_len = 1;
  776. mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
  777. break;
  778. case Opt_sec_lkeyi:
  779. mnt->auth_flavor_len = 1;
  780. mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
  781. break;
  782. case Opt_sec_lkeyp:
  783. mnt->auth_flavor_len = 1;
  784. mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
  785. break;
  786. case Opt_sec_spkm:
  787. mnt->auth_flavor_len = 1;
  788. mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
  789. break;
  790. case Opt_sec_spkmi:
  791. mnt->auth_flavor_len = 1;
  792. mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
  793. break;
  794. case Opt_sec_spkmp:
  795. mnt->auth_flavor_len = 1;
  796. mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
  797. break;
  798. default:
  799. return 0;
  800. }
  801. return 1;
  802. }
  803. static void nfs_parse_invalid_value(const char *option)
  804. {
  805. dfprintk(MOUNT, "NFS: bad value specified for %s option\n", option);
  806. }
  807. /*
  808. * Error-check and convert a string of mount options from user space into
  809. * a data structure. The whole mount string is processed; bad options are
  810. * skipped as they are encountered. If there were no errors, return 1;
  811. * otherwise return 0 (zero).
  812. */
  813. static int nfs_parse_mount_options(char *raw,
  814. struct nfs_parsed_mount_data *mnt)
  815. {
  816. char *p, *string, *secdata;
  817. int rc, sloppy = 0, errors = 0;
  818. if (!raw) {
  819. dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
  820. return 1;
  821. }
  822. dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
  823. secdata = alloc_secdata();
  824. if (!secdata)
  825. goto out_nomem;
  826. rc = security_sb_copy_data(raw, secdata);
  827. if (rc)
  828. goto out_security_failure;
  829. rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
  830. if (rc)
  831. goto out_security_failure;
  832. free_secdata(secdata);
  833. while ((p = strsep(&raw, ",")) != NULL) {
  834. substring_t args[MAX_OPT_ARGS];
  835. int option, token;
  836. if (!*p)
  837. continue;
  838. dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", p);
  839. token = match_token(p, nfs_mount_option_tokens, args);
  840. switch (token) {
  841. /*
  842. * boolean options: foo/nofoo
  843. */
  844. case Opt_soft:
  845. mnt->flags |= NFS_MOUNT_SOFT;
  846. break;
  847. case Opt_hard:
  848. mnt->flags &= ~NFS_MOUNT_SOFT;
  849. break;
  850. case Opt_posix:
  851. mnt->flags |= NFS_MOUNT_POSIX;
  852. break;
  853. case Opt_noposix:
  854. mnt->flags &= ~NFS_MOUNT_POSIX;
  855. break;
  856. case Opt_cto:
  857. mnt->flags &= ~NFS_MOUNT_NOCTO;
  858. break;
  859. case Opt_nocto:
  860. mnt->flags |= NFS_MOUNT_NOCTO;
  861. break;
  862. case Opt_ac:
  863. mnt->flags &= ~NFS_MOUNT_NOAC;
  864. break;
  865. case Opt_noac:
  866. mnt->flags |= NFS_MOUNT_NOAC;
  867. break;
  868. case Opt_lock:
  869. mnt->flags &= ~NFS_MOUNT_NONLM;
  870. break;
  871. case Opt_nolock:
  872. mnt->flags |= NFS_MOUNT_NONLM;
  873. break;
  874. case Opt_v2:
  875. mnt->flags &= ~NFS_MOUNT_VER3;
  876. break;
  877. case Opt_v3:
  878. mnt->flags |= NFS_MOUNT_VER3;
  879. break;
  880. case Opt_udp:
  881. mnt->flags &= ~NFS_MOUNT_TCP;
  882. mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
  883. break;
  884. case Opt_tcp:
  885. mnt->flags |= NFS_MOUNT_TCP;
  886. mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
  887. break;
  888. case Opt_rdma:
  889. mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
  890. mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
  891. break;
  892. case Opt_acl:
  893. mnt->flags &= ~NFS_MOUNT_NOACL;
  894. break;
  895. case Opt_noacl:
  896. mnt->flags |= NFS_MOUNT_NOACL;
  897. break;
  898. case Opt_rdirplus:
  899. mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
  900. break;
  901. case Opt_nordirplus:
  902. mnt->flags |= NFS_MOUNT_NORDIRPLUS;
  903. break;
  904. case Opt_sharecache:
  905. mnt->flags &= ~NFS_MOUNT_UNSHARED;
  906. break;
  907. case Opt_nosharecache:
  908. mnt->flags |= NFS_MOUNT_UNSHARED;
  909. break;
  910. case Opt_resvport:
  911. mnt->flags &= ~NFS_MOUNT_NORESVPORT;
  912. break;
  913. case Opt_noresvport:
  914. mnt->flags |= NFS_MOUNT_NORESVPORT;
  915. break;
  916. /*
  917. * options that take numeric values
  918. */
  919. case Opt_port:
  920. if (match_int(args, &option) ||
  921. option < 0 || option > USHORT_MAX) {
  922. errors++;
  923. nfs_parse_invalid_value("port");
  924. } else
  925. mnt->nfs_server.port = option;
  926. break;
  927. case Opt_rsize:
  928. if (match_int(args, &option) || option < 0) {
  929. errors++;
  930. nfs_parse_invalid_value("rsize");
  931. } else
  932. mnt->rsize = option;
  933. break;
  934. case Opt_wsize:
  935. if (match_int(args, &option) || option < 0) {
  936. errors++;
  937. nfs_parse_invalid_value("wsize");
  938. } else
  939. mnt->wsize = option;
  940. break;
  941. case Opt_bsize:
  942. if (match_int(args, &option) || option < 0) {
  943. errors++;
  944. nfs_parse_invalid_value("bsize");
  945. } else
  946. mnt->bsize = option;
  947. break;
  948. case Opt_timeo:
  949. if (match_int(args, &option) || option <= 0) {
  950. errors++;
  951. nfs_parse_invalid_value("timeo");
  952. } else
  953. mnt->timeo = option;
  954. break;
  955. case Opt_retrans:
  956. if (match_int(args, &option) || option <= 0) {
  957. errors++;
  958. nfs_parse_invalid_value("retrans");
  959. } else
  960. mnt->retrans = option;
  961. break;
  962. case Opt_acregmin:
  963. if (match_int(args, &option) || option < 0) {
  964. errors++;
  965. nfs_parse_invalid_value("acregmin");
  966. } else
  967. mnt->acregmin = option;
  968. break;
  969. case Opt_acregmax:
  970. if (match_int(args, &option) || option < 0) {
  971. errors++;
  972. nfs_parse_invalid_value("acregmax");
  973. } else
  974. mnt->acregmax = option;
  975. break;
  976. case Opt_acdirmin:
  977. if (match_int(args, &option) || option < 0) {
  978. errors++;
  979. nfs_parse_invalid_value("acdirmin");
  980. } else
  981. mnt->acdirmin = option;
  982. break;
  983. case Opt_acdirmax:
  984. if (match_int(args, &option) || option < 0) {
  985. errors++;
  986. nfs_parse_invalid_value("acdirmax");
  987. } else
  988. mnt->acdirmax = option;
  989. break;
  990. case Opt_actimeo:
  991. if (match_int(args, &option) || option < 0) {
  992. errors++;
  993. nfs_parse_invalid_value("actimeo");
  994. } else
  995. mnt->acregmin = mnt->acregmax =
  996. mnt->acdirmin = mnt->acdirmax = option;
  997. break;
  998. case Opt_namelen:
  999. if (match_int(args, &option) || option < 0) {
  1000. errors++;
  1001. nfs_parse_invalid_value("namlen");
  1002. } else
  1003. mnt->namlen = option;
  1004. break;
  1005. case Opt_mountport:
  1006. if (match_int(args, &option) ||
  1007. option < 0 || option > USHORT_MAX) {
  1008. errors++;
  1009. nfs_parse_invalid_value("mountport");
  1010. } else
  1011. mnt->mount_server.port = option;
  1012. break;
  1013. case Opt_mountvers:
  1014. if (match_int(args, &option) ||
  1015. option < NFS_MNT_VERSION ||
  1016. option > NFS_MNT3_VERSION) {
  1017. errors++;
  1018. nfs_parse_invalid_value("mountvers");
  1019. } else
  1020. mnt->mount_server.version = option;
  1021. break;
  1022. case Opt_nfsvers:
  1023. if (match_int(args, &option)) {
  1024. errors++;
  1025. nfs_parse_invalid_value("nfsvers");
  1026. break;
  1027. }
  1028. switch (option) {
  1029. case NFS2_VERSION:
  1030. mnt->flags &= ~NFS_MOUNT_VER3;
  1031. break;
  1032. case NFS3_VERSION:
  1033. mnt->flags |= NFS_MOUNT_VER3;
  1034. break;
  1035. default:
  1036. errors++;
  1037. nfs_parse_invalid_value("nfsvers");
  1038. }
  1039. break;
  1040. /*
  1041. * options that take text values
  1042. */
  1043. case Opt_sec:
  1044. string = match_strdup(args);
  1045. if (string == NULL)
  1046. goto out_nomem;
  1047. rc = nfs_parse_security_flavors(string, mnt);
  1048. kfree(string);
  1049. if (!rc) {
  1050. errors++;
  1051. dfprintk(MOUNT, "NFS: unrecognized "
  1052. "security flavor\n");
  1053. }
  1054. break;
  1055. case Opt_proto:
  1056. string = match_strdup(args);
  1057. if (string == NULL)
  1058. goto out_nomem;
  1059. token = match_token(string,
  1060. nfs_xprt_protocol_tokens, args);
  1061. kfree(string);
  1062. switch (token) {
  1063. case Opt_xprt_udp:
  1064. mnt->flags &= ~NFS_MOUNT_TCP;
  1065. mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
  1066. break;
  1067. case Opt_xprt_tcp:
  1068. mnt->flags |= NFS_MOUNT_TCP;
  1069. mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
  1070. break;
  1071. case Opt_xprt_rdma:
  1072. /* vector side protocols to TCP */
  1073. mnt->flags |= NFS_MOUNT_TCP;
  1074. mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
  1075. break;
  1076. default:
  1077. errors++;
  1078. dfprintk(MOUNT, "NFS: unrecognized "
  1079. "transport protocol\n");
  1080. }
  1081. break;
  1082. case Opt_mountproto:
  1083. string = match_strdup(args);
  1084. if (string == NULL)
  1085. goto out_nomem;
  1086. token = match_token(string,
  1087. nfs_xprt_protocol_tokens, args);
  1088. kfree(string);
  1089. switch (token) {
  1090. case Opt_xprt_udp:
  1091. mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
  1092. break;
  1093. case Opt_xprt_tcp:
  1094. mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
  1095. break;
  1096. case Opt_xprt_rdma: /* not used for side protocols */
  1097. default:
  1098. errors++;
  1099. dfprintk(MOUNT, "NFS: unrecognized "
  1100. "transport protocol\n");
  1101. }
  1102. break;
  1103. case Opt_addr:
  1104. string = match_strdup(args);
  1105. if (string == NULL)
  1106. goto out_nomem;
  1107. nfs_parse_ip_address(string, strlen(string),
  1108. (struct sockaddr *)
  1109. &mnt->nfs_server.address,
  1110. &mnt->nfs_server.addrlen);
  1111. kfree(string);
  1112. break;
  1113. case Opt_clientaddr:
  1114. string = match_strdup(args);
  1115. if (string == NULL)
  1116. goto out_nomem;
  1117. kfree(mnt->client_address);
  1118. mnt->client_address = string;
  1119. break;
  1120. case Opt_mounthost:
  1121. string = match_strdup(args);
  1122. if (string == NULL)
  1123. goto out_nomem;
  1124. kfree(mnt->mount_server.hostname);
  1125. mnt->mount_server.hostname = string;
  1126. break;
  1127. case Opt_mountaddr:
  1128. string = match_strdup(args);
  1129. if (string == NULL)
  1130. goto out_nomem;
  1131. nfs_parse_ip_address(string, strlen(string),
  1132. (struct sockaddr *)
  1133. &mnt->mount_server.address,
  1134. &mnt->mount_server.addrlen);
  1135. kfree(string);
  1136. break;
  1137. case Opt_lookupcache:
  1138. string = match_strdup(args);
  1139. if (string == NULL)
  1140. goto out_nomem;
  1141. token = match_token(string,
  1142. nfs_lookupcache_tokens, args);
  1143. kfree(string);
  1144. switch (token) {
  1145. case Opt_lookupcache_all:
  1146. mnt->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
  1147. break;
  1148. case Opt_lookupcache_positive:
  1149. mnt->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
  1150. mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
  1151. break;
  1152. case Opt_lookupcache_none:
  1153. mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
  1154. break;
  1155. default:
  1156. errors++;
  1157. dfprintk(MOUNT, "NFS: invalid "
  1158. "lookupcache argument\n");
  1159. };
  1160. break;
  1161. /*
  1162. * Special options
  1163. */
  1164. case Opt_sloppy:
  1165. sloppy = 1;
  1166. dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
  1167. break;
  1168. case Opt_userspace:
  1169. case Opt_deprecated:
  1170. dfprintk(MOUNT, "NFS: ignoring mount option "
  1171. "'%s'\n", p);
  1172. break;
  1173. default:
  1174. errors++;
  1175. dfprintk(MOUNT, "NFS: unrecognized mount option "
  1176. "'%s'\n", p);
  1177. }
  1178. }
  1179. if (errors > 0) {
  1180. dfprintk(MOUNT, "NFS: parsing encountered %d error%s\n",
  1181. errors, (errors == 1 ? "" : "s"));
  1182. if (!sloppy)
  1183. return 0;
  1184. }
  1185. return 1;
  1186. out_nomem:
  1187. printk(KERN_INFO "NFS: not enough memory to parse option\n");
  1188. return 0;
  1189. out_security_failure:
  1190. free_secdata(secdata);
  1191. printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
  1192. return 0;
  1193. }
  1194. /*
  1195. * Use the remote server's MOUNT service to request the NFS file handle
  1196. * corresponding to the provided path.
  1197. */
  1198. static int nfs_try_mount(struct nfs_parsed_mount_data *args,
  1199. struct nfs_fh *root_fh)
  1200. {
  1201. struct nfs_mount_request request = {
  1202. .sap = (struct sockaddr *)
  1203. &args->mount_server.address,
  1204. .dirpath = args->nfs_server.export_path,
  1205. .protocol = args->mount_server.protocol,
  1206. .fh = root_fh,
  1207. };
  1208. int status;
  1209. if (args->mount_server.version == 0) {
  1210. if (args->flags & NFS_MOUNT_VER3)
  1211. args->mount_server.version = NFS_MNT3_VERSION;
  1212. else
  1213. args->mount_server.version = NFS_MNT_VERSION;
  1214. }
  1215. request.version = args->mount_server.version;
  1216. if (args->mount_server.hostname)
  1217. request.hostname = args->mount_server.hostname;
  1218. else
  1219. request.hostname = args->nfs_server.hostname;
  1220. /*
  1221. * Construct the mount server's address.
  1222. */
  1223. if (args->mount_server.address.ss_family == AF_UNSPEC) {
  1224. memcpy(request.sap, &args->nfs_server.address,
  1225. args->nfs_server.addrlen);
  1226. args->mount_server.addrlen = args->nfs_server.addrlen;
  1227. }
  1228. request.salen = args->mount_server.addrlen;
  1229. /*
  1230. * autobind will be used if mount_server.port == 0
  1231. */
  1232. nfs_set_port(request.sap, args->mount_server.port);
  1233. /*
  1234. * Now ask the mount server to map our export path
  1235. * to a file handle.
  1236. */
  1237. status = nfs_mount(&request);
  1238. if (status == 0)
  1239. return 0;
  1240. dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
  1241. request.hostname, status);
  1242. return status;
  1243. }
  1244. static int nfs_parse_simple_hostname(const char *dev_name,
  1245. char **hostname, size_t maxnamlen,
  1246. char **export_path, size_t maxpathlen)
  1247. {
  1248. size_t len;
  1249. char *colon, *comma;
  1250. colon = strchr(dev_name, ':');
  1251. if (colon == NULL)
  1252. goto out_bad_devname;
  1253. len = colon - dev_name;
  1254. if (len > maxnamlen)
  1255. goto out_hostname;
  1256. /* N.B. caller will free nfs_server.hostname in all cases */
  1257. *hostname = kstrndup(dev_name, len, GFP_KERNEL);
  1258. if (!*hostname)
  1259. goto out_nomem;
  1260. /* kill possible hostname list: not supported */
  1261. comma = strchr(*hostname, ',');
  1262. if (comma != NULL) {
  1263. if (comma == *hostname)
  1264. goto out_bad_devname;
  1265. *comma = '\0';
  1266. }
  1267. colon++;
  1268. len = strlen(colon);
  1269. if (len > maxpathlen)
  1270. goto out_path;
  1271. *export_path = kstrndup(colon, len, GFP_KERNEL);
  1272. if (!*export_path)
  1273. goto out_nomem;
  1274. dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
  1275. return 0;
  1276. out_bad_devname:
  1277. dfprintk(MOUNT, "NFS: device name not in host:path format\n");
  1278. return -EINVAL;
  1279. out_nomem:
  1280. dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
  1281. return -ENOMEM;
  1282. out_hostname:
  1283. dfprintk(MOUNT, "NFS: server hostname too long\n");
  1284. return -ENAMETOOLONG;
  1285. out_path:
  1286. dfprintk(MOUNT, "NFS: export pathname too long\n");
  1287. return -ENAMETOOLONG;
  1288. }
  1289. /*
  1290. * Hostname has square brackets around it because it contains one or
  1291. * more colons. We look for the first closing square bracket, and a
  1292. * colon must follow it.
  1293. */
  1294. static int nfs_parse_protected_hostname(const char *dev_name,
  1295. char **hostname, size_t maxnamlen,
  1296. char **export_path, size_t maxpathlen)
  1297. {
  1298. size_t len;
  1299. char *start, *end;
  1300. start = (char *)(dev_name + 1);
  1301. end = strchr(start, ']');
  1302. if (end == NULL)
  1303. goto out_bad_devname;
  1304. if (*(end + 1) != ':')
  1305. goto out_bad_devname;
  1306. len = end - start;
  1307. if (len > maxnamlen)
  1308. goto out_hostname;
  1309. /* N.B. caller will free nfs_server.hostname in all cases */
  1310. *hostname = kstrndup(start, len, GFP_KERNEL);
  1311. if (*hostname == NULL)
  1312. goto out_nomem;
  1313. end += 2;
  1314. len = strlen(end);
  1315. if (len > maxpathlen)
  1316. goto out_path;
  1317. *export_path = kstrndup(end, len, GFP_KERNEL);
  1318. if (!*export_path)
  1319. goto out_nomem;
  1320. return 0;
  1321. out_bad_devname:
  1322. dfprintk(MOUNT, "NFS: device name not in host:path format\n");
  1323. return -EINVAL;
  1324. out_nomem:
  1325. dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
  1326. return -ENOMEM;
  1327. out_hostname:
  1328. dfprintk(MOUNT, "NFS: server hostname too long\n");
  1329. return -ENAMETOOLONG;
  1330. out_path:
  1331. dfprintk(MOUNT, "NFS: export pathname too long\n");
  1332. return -ENAMETOOLONG;
  1333. }
  1334. /*
  1335. * Split "dev_name" into "hostname:export_path".
  1336. *
  1337. * The leftmost colon demarks the split between the server's hostname
  1338. * and the export path. If the hostname starts with a left square
  1339. * bracket, then it may contain colons.
  1340. *
  1341. * Note: caller frees hostname and export path, even on error.
  1342. */
  1343. static int nfs_parse_devname(const char *dev_name,
  1344. char **hostname, size_t maxnamlen,
  1345. char **export_path, size_t maxpathlen)
  1346. {
  1347. if (*dev_name == '[')
  1348. return nfs_parse_protected_hostname(dev_name,
  1349. hostname, maxnamlen,
  1350. export_path, maxpathlen);
  1351. return nfs_parse_simple_hostname(dev_name,
  1352. hostname, maxnamlen,
  1353. export_path, maxpathlen);
  1354. }
  1355. /*
  1356. * Validate the NFS2/NFS3 mount data
  1357. * - fills in the mount root filehandle
  1358. *
  1359. * For option strings, user space handles the following behaviors:
  1360. *
  1361. * + DNS: mapping server host name to IP address ("addr=" option)
  1362. *
  1363. * + failure mode: how to behave if a mount request can't be handled
  1364. * immediately ("fg/bg" option)
  1365. *
  1366. * + retry: how often to retry a mount request ("retry=" option)
  1367. *
  1368. * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
  1369. * mountproto=tcp after mountproto=udp, and so on
  1370. */
  1371. static int nfs_validate_mount_data(void *options,
  1372. struct nfs_parsed_mount_data *args,
  1373. struct nfs_fh *mntfh,
  1374. const char *dev_name)
  1375. {
  1376. struct nfs_mount_data *data = (struct nfs_mount_data *)options;
  1377. if (data == NULL)
  1378. goto out_no_data;
  1379. args->flags = (NFS_MOUNT_VER3 | NFS_MOUNT_TCP);
  1380. args->rsize = NFS_MAX_FILE_IO_SIZE;
  1381. args->wsize = NFS_MAX_FILE_IO_SIZE;
  1382. args->acregmin = NFS_DEF_ACREGMIN;
  1383. args->acregmax = NFS_DEF_ACREGMAX;
  1384. args->acdirmin = NFS_DEF_ACDIRMIN;
  1385. args->acdirmax = NFS_DEF_ACDIRMAX;
  1386. args->mount_server.port = 0; /* autobind unless user sets port */
  1387. args->nfs_server.port = 0; /* autobind unless user sets port */
  1388. args->nfs_server.protocol = XPRT_TRANSPORT_TCP;
  1389. args->auth_flavors[0] = RPC_AUTH_UNIX;
  1390. switch (data->version) {
  1391. case 1:
  1392. data->namlen = 0;
  1393. case 2:
  1394. data->bsize = 0;
  1395. case 3:
  1396. if (data->flags & NFS_MOUNT_VER3)
  1397. goto out_no_v3;
  1398. data->root.size = NFS2_FHSIZE;
  1399. memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
  1400. case 4:
  1401. if (data->flags & NFS_MOUNT_SECFLAVOUR)
  1402. goto out_no_sec;
  1403. case 5:
  1404. memset(data->context, 0, sizeof(data->context));
  1405. case 6:
  1406. if (data->flags & NFS_MOUNT_VER3) {
  1407. if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
  1408. goto out_invalid_fh;
  1409. mntfh->size = data->root.size;
  1410. } else
  1411. mntfh->size = NFS2_FHSIZE;
  1412. memcpy(mntfh->data, data->root.data, mntfh->size);
  1413. if (mntfh->size < sizeof(mntfh->data))
  1414. memset(mntfh->data + mntfh->size, 0,
  1415. sizeof(mntfh->data) - mntfh->size);
  1416. /*
  1417. * Translate to nfs_parsed_mount_data, which nfs_fill_super
  1418. * can deal with.
  1419. */
  1420. args->flags = data->flags & NFS_MOUNT_FLAGMASK;
  1421. args->rsize = data->rsize;
  1422. args->wsize = data->wsize;
  1423. args->timeo = data->timeo;
  1424. args->retrans = data->retrans;
  1425. args->acregmin = data->acregmin;
  1426. args->acregmax = data->acregmax;
  1427. args->acdirmin = data->acdirmin;
  1428. args->acdirmax = data->acdirmax;
  1429. memcpy(&args->nfs_server.address, &data->addr,
  1430. sizeof(data->addr));
  1431. args->nfs_server.addrlen = sizeof(data->addr);
  1432. if (!nfs_verify_server_address((struct sockaddr *)
  1433. &args->nfs_server.address))
  1434. goto out_no_address;
  1435. if (!(data->flags & NFS_MOUNT_TCP))
  1436. args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
  1437. /* N.B. caller will free nfs_server.hostname in all cases */
  1438. args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
  1439. args->namlen = data->namlen;
  1440. args->bsize = data->bsize;
  1441. if (data->flags & NFS_MOUNT_SECFLAVOUR)
  1442. args->auth_flavors[0] = data->pseudoflavor;
  1443. if (!args->nfs_server.hostname)
  1444. goto out_nomem;
  1445. /*
  1446. * The legacy version 6 binary mount data from userspace has a
  1447. * field used only to transport selinux information into the
  1448. * the kernel. To continue to support that functionality we
  1449. * have a touch of selinux knowledge here in the NFS code. The
  1450. * userspace code converted context=blah to just blah so we are
  1451. * converting back to the full string selinux understands.
  1452. */
  1453. if (data->context[0]){
  1454. #ifdef CONFIG_SECURITY_SELINUX
  1455. int rc;
  1456. char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
  1457. if (!opts_str)
  1458. return -ENOMEM;
  1459. strcpy(opts_str, "context=");
  1460. data->context[NFS_MAX_CONTEXT_LEN] = '\0';
  1461. strcat(opts_str, &data->context[0]);
  1462. rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
  1463. kfree(opts_str);
  1464. if (rc)
  1465. return rc;
  1466. #else
  1467. return -EINVAL;
  1468. #endif
  1469. }
  1470. break;
  1471. default: {
  1472. int status;
  1473. if (nfs_parse_mount_options((char *)options, args) == 0)
  1474. return -EINVAL;
  1475. if (!nfs_verify_server_address((struct sockaddr *)
  1476. &args->nfs_server.address))
  1477. goto out_no_address;
  1478. nfs_set_port((struct sockaddr *)&args->nfs_server.address,
  1479. args->nfs_server.port);
  1480. nfs_set_mount_transport_protocol(args);
  1481. status = nfs_parse_devname(dev_name,
  1482. &args->nfs_server.hostname,
  1483. PAGE_SIZE,
  1484. &args->nfs_server.export_path,
  1485. NFS_MAXPATHLEN);
  1486. if (!status)
  1487. status = nfs_try_mount(args, mntfh);
  1488. kfree(args->nfs_server.export_path);
  1489. args->nfs_server.export_path = NULL;
  1490. if (status)
  1491. return status;
  1492. break;
  1493. }
  1494. }
  1495. #ifndef CONFIG_NFS_V3
  1496. if (args->flags & NFS_MOUNT_VER3)
  1497. goto out_v3_not_compiled;
  1498. #endif /* !CONFIG_NFS_V3 */
  1499. return 0;
  1500. out_no_data:
  1501. dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
  1502. return -EINVAL;
  1503. out_no_v3:
  1504. dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
  1505. data->version);
  1506. return -EINVAL;
  1507. out_no_sec:
  1508. dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
  1509. return -EINVAL;
  1510. #ifndef CONFIG_NFS_V3
  1511. out_v3_not_compiled:
  1512. dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
  1513. return -EPROTONOSUPPORT;
  1514. #endif /* !CONFIG_NFS_V3 */
  1515. out_nomem:
  1516. dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
  1517. return -ENOMEM;
  1518. out_no_address:
  1519. dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
  1520. return -EINVAL;
  1521. out_invalid_fh:
  1522. dfprintk(MOUNT, "NFS: invalid root filehandle\n");
  1523. return -EINVAL;
  1524. }
  1525. static int
  1526. nfs_compare_remount_data(struct nfs_server *nfss,
  1527. struct nfs_parsed_mount_data *data)
  1528. {
  1529. if (data->flags != nfss->flags ||
  1530. data->rsize != nfss->rsize ||
  1531. data->wsize != nfss->wsize ||
  1532. data->retrans != nfss->client->cl_timeout->to_retries ||
  1533. data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
  1534. data->acregmin != nfss->acregmin / HZ ||
  1535. data->acregmax != nfss->acregmax / HZ ||
  1536. data->acdirmin != nfss->acdirmin / HZ ||
  1537. data->acdirmax != nfss->acdirmax / HZ ||
  1538. data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
  1539. data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
  1540. memcmp(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
  1541. data->nfs_server.addrlen) != 0)
  1542. return -EINVAL;
  1543. return 0;
  1544. }
  1545. static int
  1546. nfs_remount(struct super_block *sb, int *flags, char *raw_data)
  1547. {
  1548. int error;
  1549. struct nfs_server *nfss = sb->s_fs_info;
  1550. struct nfs_parsed_mount_data *data;
  1551. struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
  1552. struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
  1553. u32 nfsvers = nfss->nfs_client->rpc_ops->version;
  1554. /*
  1555. * Userspace mount programs that send binary options generally send
  1556. * them populated with default values. We have no way to know which
  1557. * ones were explicitly specified. Fall back to legacy behavior and
  1558. * just return success.
  1559. */
  1560. if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
  1561. (nfsvers <= 3 && (!options || (options->version >= 1 &&
  1562. options->version <= 6))))
  1563. return 0;
  1564. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1565. if (data == NULL)
  1566. return -ENOMEM;
  1567. /* fill out struct with values from existing mount */
  1568. data->flags = nfss->flags;
  1569. data->rsize = nfss->rsize;
  1570. data->wsize = nfss->wsize;
  1571. data->retrans = nfss->client->cl_timeout->to_retries;
  1572. data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
  1573. data->acregmin = nfss->acregmin / HZ;
  1574. data->acregmax = nfss->acregmax / HZ;
  1575. data->acdirmin = nfss->acdirmin / HZ;
  1576. data->acdirmax = nfss->acdirmax / HZ;
  1577. data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
  1578. data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
  1579. memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
  1580. data->nfs_server.addrlen);
  1581. /* overwrite those values with any that were specified */
  1582. error = nfs_parse_mount_options((char *)options, data);
  1583. if (error < 0)
  1584. goto out;
  1585. /* compare new mount options with old ones */
  1586. error = nfs_compare_remount_data(nfss, data);
  1587. out:
  1588. kfree(data);
  1589. return error;
  1590. }
  1591. /*
  1592. * Initialise the common bits of the superblock
  1593. */
  1594. static inline void nfs_initialise_sb(struct super_block *sb)
  1595. {
  1596. struct nfs_server *server = NFS_SB(sb);
  1597. sb->s_magic = NFS_SUPER_MAGIC;
  1598. /* We probably want something more informative here */
  1599. snprintf(sb->s_id, sizeof(sb->s_id),
  1600. "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
  1601. if (sb->s_blocksize == 0)
  1602. sb->s_blocksize = nfs_block_bits(server->wsize,
  1603. &sb->s_blocksize_bits);
  1604. if (server->flags & NFS_MOUNT_NOAC)
  1605. sb->s_flags |= MS_SYNCHRONOUS;
  1606. nfs_super_set_maxbytes(sb, server->maxfilesize);
  1607. }
  1608. /*
  1609. * Finish setting up an NFS2/3 superblock
  1610. */
  1611. static void nfs_fill_super(struct super_block *sb,
  1612. struct nfs_parsed_mount_data *data)
  1613. {
  1614. struct nfs_server *server = NFS_SB(sb);
  1615. sb->s_blocksize_bits = 0;
  1616. sb->s_blocksize = 0;
  1617. if (data->bsize)
  1618. sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
  1619. if (server->flags & NFS_MOUNT_VER3) {
  1620. /* The VFS shouldn't apply the umask to mode bits. We will do
  1621. * so ourselves when necessary.
  1622. */
  1623. sb->s_flags |= MS_POSIXACL;
  1624. sb->s_time_gran = 1;
  1625. }
  1626. sb->s_op = &nfs_sops;
  1627. nfs_initialise_sb(sb);
  1628. }
  1629. /*
  1630. * Finish setting up a cloned NFS2/3 superblock
  1631. */
  1632. static void nfs_clone_super(struct super_block *sb,
  1633. const struct super_block *old_sb)
  1634. {
  1635. struct nfs_server *server = NFS_SB(sb);
  1636. sb->s_blocksize_bits = old_sb->s_blocksize_bits;
  1637. sb->s_blocksize = old_sb->s_blocksize;
  1638. sb->s_maxbytes = old_sb->s_maxbytes;
  1639. if (server->flags & NFS_MOUNT_VER3) {
  1640. /* The VFS shouldn't apply the umask to mode bits. We will do
  1641. * so ourselves when necessary.
  1642. */
  1643. sb->s_flags |= MS_POSIXACL;
  1644. sb->s_time_gran = 1;
  1645. }
  1646. sb->s_op = old_sb->s_op;
  1647. nfs_initialise_sb(sb);
  1648. }
  1649. #define NFS_MS_MASK (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_SYNCHRONOUS)
  1650. static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
  1651. {
  1652. const struct nfs_server *a = s->s_fs_info;
  1653. const struct rpc_clnt *clnt_a = a->client;
  1654. const struct rpc_clnt *clnt_b = b->client;
  1655. if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
  1656. goto Ebusy;
  1657. if (a->nfs_client != b->nfs_client)
  1658. goto Ebusy;
  1659. if (a->flags != b->flags)
  1660. goto Ebusy;
  1661. if (a->wsize != b->wsize)
  1662. goto Ebusy;
  1663. if (a->rsize != b->rsize)
  1664. goto Ebusy;
  1665. if (a->acregmin != b->acregmin)
  1666. goto Ebusy;
  1667. if (a->acregmax != b->acregmax)
  1668. goto Ebusy;
  1669. if (a->acdirmin != b->acdirmin)
  1670. goto Ebusy;
  1671. if (a->acdirmax != b->acdirmax)
  1672. goto Ebusy;
  1673. if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
  1674. goto Ebusy;
  1675. return 1;
  1676. Ebusy:
  1677. return 0;
  1678. }
  1679. struct nfs_sb_mountdata {
  1680. struct nfs_server *server;
  1681. int mntflags;
  1682. };
  1683. static int nfs_set_super(struct super_block *s, void *data)
  1684. {
  1685. struct nfs_sb_mountdata *sb_mntdata = data;
  1686. struct nfs_server *server = sb_mntdata->server;
  1687. int ret;
  1688. s->s_flags = sb_mntdata->mntflags;
  1689. s->s_fs_info = server;
  1690. ret = set_anon_super(s, server);
  1691. if (ret == 0)
  1692. server->s_dev = s->s_dev;
  1693. return ret;
  1694. }
  1695. static int nfs_compare_super_address(struct nfs_server *server1,
  1696. struct nfs_server *server2)
  1697. {
  1698. struct sockaddr *sap1, *sap2;
  1699. sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
  1700. sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
  1701. if (sap1->sa_family != sap2->sa_family)
  1702. return 0;
  1703. switch (sap1->sa_family) {
  1704. case AF_INET: {
  1705. struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
  1706. struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
  1707. if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
  1708. return 0;
  1709. if (sin1->sin_port != sin2->sin_port)
  1710. return 0;
  1711. break;
  1712. }
  1713. case AF_INET6: {
  1714. struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
  1715. struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
  1716. if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
  1717. return 0;
  1718. if (sin1->sin6_port != sin2->sin6_port)
  1719. return 0;
  1720. break;
  1721. }
  1722. default:
  1723. return 0;
  1724. }
  1725. return 1;
  1726. }
  1727. static int nfs_compare_super(struct super_block *sb, void *data)
  1728. {
  1729. struct nfs_sb_mountdata *sb_mntdata = data;
  1730. struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
  1731. int mntflags = sb_mntdata->mntflags;
  1732. if (!nfs_compare_super_address(old, server))
  1733. return 0;
  1734. /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
  1735. if (old->flags & NFS_MOUNT_UNSHARED)
  1736. return 0;
  1737. if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
  1738. return 0;
  1739. return nfs_compare_mount_options(sb, server, mntflags);
  1740. }
  1741. static int nfs_bdi_register(struct nfs_server *server)
  1742. {
  1743. return bdi_register_dev(&server->backing_dev_info, server->s_dev);
  1744. }
  1745. static int nfs_get_sb(struct file_system_type *fs_type,
  1746. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
  1747. {
  1748. struct nfs_server *server = NULL;
  1749. struct super_block *s;
  1750. struct nfs_parsed_mount_data *data;
  1751. struct nfs_fh *mntfh;
  1752. struct dentry *mntroot;
  1753. int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
  1754. struct nfs_sb_mountdata sb_mntdata = {
  1755. .mntflags = flags,
  1756. };
  1757. int error = -ENOMEM;
  1758. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1759. mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
  1760. if (data == NULL || mntfh == NULL)
  1761. goto out_free_fh;
  1762. security_init_mnt_opts(&data->lsm_opts);
  1763. /* Validate the mount data */
  1764. error = nfs_validate_mount_data(raw_data, data, mntfh, dev_name);
  1765. if (error < 0)
  1766. goto out;
  1767. /* Get a volume representation */
  1768. server = nfs_create_server(data, mntfh);
  1769. if (IS_ERR(server)) {
  1770. error = PTR_ERR(server);
  1771. goto out;
  1772. }
  1773. sb_mntdata.server = server;
  1774. if (server->flags & NFS_MOUNT_UNSHARED)
  1775. compare_super = NULL;
  1776. /* Get a superblock - note that we may end up sharing one that already exists */
  1777. s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
  1778. if (IS_ERR(s)) {
  1779. error = PTR_ERR(s);
  1780. goto out_err_nosb;
  1781. }
  1782. if (s->s_fs_info != server) {
  1783. nfs_free_server(server);
  1784. server = NULL;
  1785. } else {
  1786. error = nfs_bdi_register(server);
  1787. if (error)
  1788. goto error_splat_super;
  1789. }
  1790. if (!s->s_root) {
  1791. /* initial superblock/root creation */
  1792. nfs_fill_super(s, data);
  1793. }
  1794. mntroot = nfs_get_root(s, mntfh);
  1795. if (IS_ERR(mntroot)) {
  1796. error = PTR_ERR(mntroot);
  1797. goto error_splat_super;
  1798. }
  1799. error = security_sb_set_mnt_opts(s, &data->lsm_opts);
  1800. if (error)
  1801. goto error_splat_root;
  1802. s->s_flags |= MS_ACTIVE;
  1803. mnt->mnt_sb = s;
  1804. mnt->mnt_root = mntroot;
  1805. error = 0;
  1806. out:
  1807. kfree(data->nfs_server.hostname);
  1808. kfree(data->mount_server.hostname);
  1809. security_free_mnt_opts(&data->lsm_opts);
  1810. out_free_fh:
  1811. kfree(mntfh);
  1812. kfree(data);
  1813. return error;
  1814. out_err_nosb:
  1815. nfs_free_server(server);
  1816. goto out;
  1817. error_splat_root:
  1818. dput(mntroot);
  1819. error_splat_super:
  1820. up_write(&s->s_umount);
  1821. deactivate_super(s);
  1822. goto out;
  1823. }
  1824. /*
  1825. * Destroy an NFS2/3 superblock
  1826. */
  1827. static void nfs_kill_super(struct super_block *s)
  1828. {
  1829. struct nfs_server *server = NFS_SB(s);
  1830. bdi_unregister(&server->backing_dev_info);
  1831. kill_anon_super(s);
  1832. nfs_free_server(server);
  1833. }
  1834. /*
  1835. * Clone an NFS2/3 server record on xdev traversal (FSID-change)
  1836. */
  1837. static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
  1838. const char *dev_name, void *raw_data,
  1839. struct vfsmount *mnt)
  1840. {
  1841. struct nfs_clone_mount *data = raw_data;
  1842. struct super_block *s;
  1843. struct nfs_server *server;
  1844. struct dentry *mntroot;
  1845. int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
  1846. struct nfs_sb_mountdata sb_mntdata = {
  1847. .mntflags = flags,
  1848. };
  1849. int error;
  1850. dprintk("--> nfs_xdev_get_sb()\n");
  1851. /* create a new volume representation */
  1852. server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
  1853. if (IS_ERR(server)) {
  1854. error = PTR_ERR(server);
  1855. goto out_err_noserver;
  1856. }
  1857. sb_mntdata.server = server;
  1858. if (server->flags & NFS_MOUNT_UNSHARED)
  1859. compare_super = NULL;
  1860. /* Get a superblock - note that we may end up sharing one that already exists */
  1861. s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
  1862. if (IS_ERR(s)) {
  1863. error = PTR_ERR(s);
  1864. goto out_err_nosb;
  1865. }
  1866. if (s->s_fs_info != server) {
  1867. nfs_free_server(server);
  1868. server = NULL;
  1869. } else {
  1870. error = nfs_bdi_register(server);
  1871. if (error)
  1872. goto error_splat_super;
  1873. }
  1874. if (!s->s_root) {
  1875. /* initial superblock/root creation */
  1876. nfs_clone_super(s, data->sb);
  1877. }
  1878. mntroot = nfs_get_root(s, data->fh);
  1879. if (IS_ERR(mntroot)) {
  1880. error = PTR_ERR(mntroot);
  1881. goto error_splat_super;
  1882. }
  1883. if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
  1884. dput(mntroot);
  1885. error = -ESTALE;
  1886. goto error_splat_super;
  1887. }
  1888. s->s_flags |= MS_ACTIVE;
  1889. mnt->mnt_sb = s;
  1890. mnt->mnt_root = mntroot;
  1891. /* clone any lsm security options from the parent to the new sb */
  1892. security_sb_clone_mnt_opts(data->sb, s);
  1893. dprintk("<-- nfs_xdev_get_sb() = 0\n");
  1894. return 0;
  1895. out_err_nosb:
  1896. nfs_free_server(server);
  1897. out_err_noserver:
  1898. dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
  1899. return error;
  1900. error_splat_super:
  1901. up_write(&s->s_umount);
  1902. deactivate_super(s);
  1903. dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
  1904. return error;
  1905. }
  1906. #ifdef CONFIG_NFS_V4
  1907. /*
  1908. * Finish setting up a cloned NFS4 superblock
  1909. */
  1910. static void nfs4_clone_super(struct super_block *sb,
  1911. const struct super_block *old_sb)
  1912. {
  1913. sb->s_blocksize_bits = old_sb->s_blocksize_bits;
  1914. sb->s_blocksize = old_sb->s_blocksize;
  1915. sb->s_maxbytes = old_sb->s_maxbytes;
  1916. sb->s_time_gran = 1;
  1917. sb->s_op = old_sb->s_op;
  1918. nfs_initialise_sb(sb);
  1919. }
  1920. /*
  1921. * Set up an NFS4 superblock
  1922. */
  1923. static void nfs4_fill_super(struct super_block *sb)
  1924. {
  1925. sb->s_time_gran = 1;
  1926. sb->s_op = &nfs4_sops;
  1927. nfs_initialise_sb(sb);
  1928. }
  1929. /*
  1930. * Validate NFSv4 mount options
  1931. */
  1932. static int nfs4_validate_mount_data(void *options,
  1933. struct nfs_parsed_mount_data *args,
  1934. const char *dev_name)
  1935. {
  1936. struct sockaddr_in *ap;
  1937. struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
  1938. char *c;
  1939. if (data == NULL)
  1940. goto out_no_data;
  1941. args->rsize = NFS_MAX_FILE_IO_SIZE;
  1942. args->wsize = NFS_MAX_FILE_IO_SIZE;
  1943. args->acregmin = NFS_DEF_ACREGMIN;
  1944. args->acregmax = NFS_DEF_ACREGMAX;
  1945. args->acdirmin = NFS_DEF_ACDIRMIN;
  1946. args->acdirmax = NFS_DEF_ACDIRMAX;
  1947. args->nfs_server.port = NFS_PORT; /* 2049 unless user set port= */
  1948. args->auth_flavors[0] = RPC_AUTH_UNIX;
  1949. args->auth_flavor_len = 0;
  1950. switch (data->version) {
  1951. case 1:
  1952. ap = (struct sockaddr_in *)&args->nfs_server.address;
  1953. if (data->host_addrlen > sizeof(args->nfs_server.address))
  1954. goto out_no_address;
  1955. if (data->host_addrlen == 0)
  1956. goto out_no_address;
  1957. args->nfs_server.addrlen = data->host_addrlen;
  1958. if (copy_from_user(ap, data->host_addr, data->host_addrlen))
  1959. return -EFAULT;
  1960. if (!nfs_verify_server_address((struct sockaddr *)
  1961. &args->nfs_server.address))
  1962. goto out_no_address;
  1963. if (data->auth_flavourlen) {
  1964. if (data->auth_flavourlen > 1)
  1965. goto out_inval_auth;
  1966. if (copy_from_user(&args->auth_flavors[0],
  1967. data->auth_flavours,
  1968. sizeof(args->auth_flavors[0])))
  1969. return -EFAULT;
  1970. }
  1971. c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
  1972. if (IS_ERR(c))
  1973. return PTR_ERR(c);
  1974. args->nfs_server.hostname = c;
  1975. c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
  1976. if (IS_ERR(c))
  1977. return PTR_ERR(c);
  1978. args->nfs_server.export_path = c;
  1979. dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
  1980. c = strndup_user(data->client_addr.data, 16);
  1981. if (IS_ERR(c))
  1982. return PTR_ERR(c);
  1983. args->client_address = c;
  1984. /*
  1985. * Translate to nfs_parsed_mount_data, which nfs4_fill_super
  1986. * can deal with.
  1987. */
  1988. args->flags = data->flags & NFS4_MOUNT_FLAGMASK;
  1989. args->rsize = data->rsize;
  1990. args->wsize = data->wsize;
  1991. args->timeo = data->timeo;
  1992. args->retrans = data->retrans;
  1993. args->acregmin = data->acregmin;
  1994. args->acregmax = data->acregmax;
  1995. args->acdirmin = data->acdirmin;
  1996. args->acdirmax = data->acdirmax;
  1997. args->nfs_server.protocol = data->proto;
  1998. nfs_validate_transport_protocol(args);
  1999. break;
  2000. default: {
  2001. int status;
  2002. if (nfs_parse_mount_options((char *)options, args) == 0)
  2003. return -EINVAL;
  2004. if (!nfs_verify_server_address((struct sockaddr *)
  2005. &args->nfs_server.address))
  2006. return -EINVAL;
  2007. nfs_set_port((struct sockaddr *)&args->nfs_server.address,
  2008. args->nfs_server.port);
  2009. nfs_validate_transport_protocol(args);
  2010. if (args->auth_flavor_len > 1)
  2011. goto out_inval_auth;
  2012. if (args->client_address == NULL)
  2013. goto out_no_client_address;
  2014. status = nfs_parse_devname(dev_name,
  2015. &args->nfs_server.hostname,
  2016. NFS4_MAXNAMLEN,
  2017. &args->nfs_server.export_path,
  2018. NFS4_MAXPATHLEN);
  2019. if (status < 0)
  2020. return status;
  2021. break;
  2022. }
  2023. }
  2024. return 0;
  2025. out_no_data:
  2026. dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
  2027. return -EINVAL;
  2028. out_inval_auth:
  2029. dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
  2030. data->auth_flavourlen);
  2031. return -EINVAL;
  2032. out_no_address:
  2033. dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
  2034. return -EINVAL;
  2035. out_no_client_address:
  2036. dfprintk(MOUNT, "NFS4: mount program didn't pass callback address\n");
  2037. return -EINVAL;
  2038. }
  2039. /*
  2040. * Get the superblock for an NFS4 mountpoint
  2041. */
  2042. static int nfs4_get_sb(struct file_system_type *fs_type,
  2043. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
  2044. {
  2045. struct nfs_parsed_mount_data *data;
  2046. struct super_block *s;
  2047. struct nfs_server *server;
  2048. struct nfs_fh *mntfh;
  2049. struct dentry *mntroot;
  2050. int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
  2051. struct nfs_sb_mountdata sb_mntdata = {
  2052. .mntflags = flags,
  2053. };
  2054. int error = -ENOMEM;
  2055. data = kzalloc(sizeof(*data), GFP_KERNEL);
  2056. mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
  2057. if (data == NULL || mntfh == NULL)
  2058. goto out_free_fh;
  2059. security_init_mnt_opts(&data->lsm_opts);
  2060. /* Validate the mount data */
  2061. error = nfs4_validate_mount_data(raw_data, data, dev_name);
  2062. if (error < 0)
  2063. goto out;
  2064. /* Get a volume representation */
  2065. server = nfs4_create_server(data, mntfh);
  2066. if (IS_ERR(server)) {
  2067. error = PTR_ERR(server);
  2068. goto out;
  2069. }
  2070. sb_mntdata.server = server;
  2071. if (server->flags & NFS4_MOUNT_UNSHARED)
  2072. compare_super = NULL;
  2073. /* Get a superblock - note that we may end up sharing one that already exists */
  2074. s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
  2075. if (IS_ERR(s)) {
  2076. error = PTR_ERR(s);
  2077. goto out_free;
  2078. }
  2079. if (s->s_fs_info != server) {
  2080. nfs_free_server(server);
  2081. server = NULL;
  2082. } else {
  2083. error = nfs_bdi_register(server);
  2084. if (error)
  2085. goto error_splat_super;
  2086. }
  2087. if (!s->s_root) {
  2088. /* initial superblock/root creation */
  2089. nfs4_fill_super(s);
  2090. }
  2091. mntroot = nfs4_get_root(s, mntfh);
  2092. if (IS_ERR(mntroot)) {
  2093. error = PTR_ERR(mntroot);
  2094. goto error_splat_super;
  2095. }
  2096. error = security_sb_set_mnt_opts(s, &data->lsm_opts);
  2097. if (error)
  2098. goto error_splat_root;
  2099. s->s_flags |= MS_ACTIVE;
  2100. mnt->mnt_sb = s;
  2101. mnt->mnt_root = mntroot;
  2102. error = 0;
  2103. out:
  2104. kfree(data->client_address);
  2105. kfree(data->nfs_server.export_path);
  2106. kfree(data->nfs_server.hostname);
  2107. security_free_mnt_opts(&data->lsm_opts);
  2108. out_free_fh:
  2109. kfree(mntfh);
  2110. kfree(data);
  2111. return error;
  2112. out_free:
  2113. nfs_free_server(server);
  2114. goto out;
  2115. error_splat_root:
  2116. dput(mntroot);
  2117. error_splat_super:
  2118. up_write(&s->s_umount);
  2119. deactivate_super(s);
  2120. goto out;
  2121. }
  2122. static void nfs4_kill_super(struct super_block *sb)
  2123. {
  2124. struct nfs_server *server = NFS_SB(sb);
  2125. nfs_return_all_delegations(sb);
  2126. kill_anon_super(sb);
  2127. nfs4_renewd_prepare_shutdown(server);
  2128. nfs_free_server(server);
  2129. }
  2130. /*
  2131. * Clone an NFS4 server record on xdev traversal (FSID-change)
  2132. */
  2133. static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
  2134. const char *dev_name, void *raw_data,
  2135. struct vfsmount *mnt)
  2136. {
  2137. struct nfs_clone_mount *data = raw_data;
  2138. struct super_block *s;
  2139. struct nfs_server *server;
  2140. struct dentry *mntroot;
  2141. int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
  2142. struct nfs_sb_mountdata sb_mntdata = {
  2143. .mntflags = flags,
  2144. };
  2145. int error;
  2146. dprintk("--> nfs4_xdev_get_sb()\n");
  2147. /* create a new volume representation */
  2148. server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
  2149. if (IS_ERR(server)) {
  2150. error = PTR_ERR(server);
  2151. goto out_err_noserver;
  2152. }
  2153. sb_mntdata.server = server;
  2154. if (server->flags & NFS4_MOUNT_UNSHARED)
  2155. compare_super = NULL;
  2156. /* Get a superblock - note that we may end up sharing one that already exists */
  2157. s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
  2158. if (IS_ERR(s)) {
  2159. error = PTR_ERR(s);
  2160. goto out_err_nosb;
  2161. }
  2162. if (s->s_fs_info != server) {
  2163. nfs_free_server(server);
  2164. server = NULL;
  2165. } else {
  2166. error = nfs_bdi_register(server);
  2167. if (error)
  2168. goto error_splat_super;
  2169. }
  2170. if (!s->s_root) {
  2171. /* initial superblock/root creation */
  2172. nfs4_clone_super(s, data->sb);
  2173. }
  2174. mntroot = nfs4_get_root(s, data->fh);
  2175. if (IS_ERR(mntroot)) {
  2176. error = PTR_ERR(mntroot);
  2177. goto error_splat_super;
  2178. }
  2179. if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
  2180. dput(mntroot);
  2181. error = -ESTALE;
  2182. goto error_splat_super;
  2183. }
  2184. s->s_flags |= MS_ACTIVE;
  2185. mnt->mnt_sb = s;
  2186. mnt->mnt_root = mntroot;
  2187. security_sb_clone_mnt_opts(data->sb, s);
  2188. dprintk("<-- nfs4_xdev_get_sb() = 0\n");
  2189. return 0;
  2190. out_err_nosb:
  2191. nfs_free_server(server);
  2192. out_err_noserver:
  2193. dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
  2194. return error;
  2195. error_splat_super:
  2196. up_write(&s->s_umount);
  2197. deactivate_super(s);
  2198. dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
  2199. return error;
  2200. }
  2201. /*
  2202. * Create an NFS4 server record on referral traversal
  2203. */
  2204. static int nfs4_referral_get_sb(struct file_system_type *fs_type, int flags,
  2205. const char *dev_name, void *raw_data,
  2206. struct vfsmount *mnt)
  2207. {
  2208. struct nfs_clone_mount *data = raw_data;
  2209. struct super_block *s;
  2210. struct nfs_server *server;
  2211. struct dentry *mntroot;
  2212. struct nfs_fh mntfh;
  2213. int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
  2214. struct nfs_sb_mountdata sb_mntdata = {
  2215. .mntflags = flags,
  2216. };
  2217. int error;
  2218. dprintk("--> nfs4_referral_get_sb()\n");
  2219. /* create a new volume representation */
  2220. server = nfs4_create_referral_server(data, &mntfh);
  2221. if (IS_ERR(server)) {
  2222. error = PTR_ERR(server);
  2223. goto out_err_noserver;
  2224. }
  2225. sb_mntdata.server = server;
  2226. if (server->flags & NFS4_MOUNT_UNSHARED)
  2227. compare_super = NULL;
  2228. /* Get a superblock - note that we may end up sharing one that already exists */
  2229. s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
  2230. if (IS_ERR(s)) {
  2231. error = PTR_ERR(s);
  2232. goto out_err_nosb;
  2233. }
  2234. if (s->s_fs_info != server) {
  2235. nfs_free_server(server);
  2236. server = NULL;
  2237. } else {
  2238. error = nfs_bdi_register(server);
  2239. if (error)
  2240. goto error_splat_super;
  2241. }
  2242. if (!s->s_root) {
  2243. /* initial superblock/root creation */
  2244. nfs4_fill_super(s);
  2245. }
  2246. mntroot = nfs4_get_root(s, &mntfh);
  2247. if (IS_ERR(mntroot)) {
  2248. error = PTR_ERR(mntroot);
  2249. goto error_splat_super;
  2250. }
  2251. if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
  2252. dput(mntroot);
  2253. error = -ESTALE;
  2254. goto error_splat_super;
  2255. }
  2256. s->s_flags |= MS_ACTIVE;
  2257. mnt->mnt_sb = s;
  2258. mnt->mnt_root = mntroot;
  2259. security_sb_clone_mnt_opts(data->sb, s);
  2260. dprintk("<-- nfs4_referral_get_sb() = 0\n");
  2261. return 0;
  2262. out_err_nosb:
  2263. nfs_free_server(server);
  2264. out_err_noserver:
  2265. dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
  2266. return error;
  2267. error_splat_super:
  2268. up_write(&s->s_umount);
  2269. deactivate_super(s);
  2270. dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
  2271. return error;
  2272. }
  2273. #endif /* CONFIG_NFS_V4 */