super.c 79 KB

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