super.c 77 KB

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