super.c 74 KB

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