super.c 64 KB

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