super.c 79 KB

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