super.c 79 KB

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