super.c 79 KB

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