super.c 71 KB

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