super.c 77 KB

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