super.c 79 KB

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