super.c 74 KB

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