super.c 72 KB

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