super.c 79 KB

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