super.c 78 KB

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