super.c 72 KB

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