super.c 77 KB

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