super.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897
  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. static struct nfs_parsed_mount_data *nfs_alloc_parsed_mount_data(int flags)
  636. {
  637. struct nfs_parsed_mount_data *data;
  638. data = kzalloc(sizeof(*data), GFP_KERNEL);
  639. if (data) {
  640. data->flags = flags;
  641. data->rsize = NFS_MAX_FILE_IO_SIZE;
  642. data->wsize = NFS_MAX_FILE_IO_SIZE;
  643. data->acregmin = NFS_DEF_ACREGMIN;
  644. data->acregmax = NFS_DEF_ACREGMAX;
  645. data->acdirmin = NFS_DEF_ACDIRMIN;
  646. data->acdirmax = NFS_DEF_ACDIRMAX;
  647. data->nfs_server.port = NFS_UNSPEC_PORT;
  648. data->auth_flavors[0] = RPC_AUTH_UNIX;
  649. data->auth_flavor_len = 1;
  650. data->minorversion = 0;
  651. }
  652. return data;
  653. }
  654. /*
  655. * Sanity-check a server address provided by the mount command.
  656. *
  657. * Address family must be initialized, and address must not be
  658. * the ANY address for that family.
  659. */
  660. static int nfs_verify_server_address(struct sockaddr *addr)
  661. {
  662. switch (addr->sa_family) {
  663. case AF_INET: {
  664. struct sockaddr_in *sa = (struct sockaddr_in *)addr;
  665. return sa->sin_addr.s_addr != htonl(INADDR_ANY);
  666. }
  667. case AF_INET6: {
  668. struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
  669. return !ipv6_addr_any(sa);
  670. }
  671. }
  672. dfprintk(MOUNT, "NFS: Invalid IP address specified\n");
  673. return 0;
  674. }
  675. /*
  676. * Select between a default port value and a user-specified port value.
  677. * If a zero value is set, then autobind will be used.
  678. */
  679. static void nfs_set_default_port(struct sockaddr *sap, const int parsed_port,
  680. const unsigned short default_port)
  681. {
  682. unsigned short port = default_port;
  683. if (parsed_port != NFS_UNSPEC_PORT)
  684. port = parsed_port;
  685. rpc_set_port(sap, port);
  686. }
  687. /*
  688. * Sanity check the NFS transport protocol.
  689. *
  690. */
  691. static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
  692. {
  693. switch (mnt->nfs_server.protocol) {
  694. case XPRT_TRANSPORT_UDP:
  695. case XPRT_TRANSPORT_TCP:
  696. case XPRT_TRANSPORT_RDMA:
  697. break;
  698. default:
  699. mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
  700. }
  701. }
  702. /*
  703. * For text based NFSv2/v3 mounts, the mount protocol transport default
  704. * settings should depend upon the specified NFS transport.
  705. */
  706. static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
  707. {
  708. nfs_validate_transport_protocol(mnt);
  709. if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
  710. mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
  711. return;
  712. switch (mnt->nfs_server.protocol) {
  713. case XPRT_TRANSPORT_UDP:
  714. mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
  715. break;
  716. case XPRT_TRANSPORT_TCP:
  717. case XPRT_TRANSPORT_RDMA:
  718. mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
  719. }
  720. }
  721. /*
  722. * Parse the value of the 'sec=' option.
  723. */
  724. static int nfs_parse_security_flavors(char *value,
  725. struct nfs_parsed_mount_data *mnt)
  726. {
  727. substring_t args[MAX_OPT_ARGS];
  728. dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
  729. switch (match_token(value, nfs_secflavor_tokens, args)) {
  730. case Opt_sec_none:
  731. mnt->auth_flavors[0] = RPC_AUTH_NULL;
  732. break;
  733. case Opt_sec_sys:
  734. mnt->auth_flavors[0] = RPC_AUTH_UNIX;
  735. break;
  736. case Opt_sec_krb5:
  737. mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
  738. break;
  739. case Opt_sec_krb5i:
  740. mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
  741. break;
  742. case Opt_sec_krb5p:
  743. mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
  744. break;
  745. case Opt_sec_lkey:
  746. mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
  747. break;
  748. case Opt_sec_lkeyi:
  749. mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
  750. break;
  751. case Opt_sec_lkeyp:
  752. mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
  753. break;
  754. case Opt_sec_spkm:
  755. mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
  756. break;
  757. case Opt_sec_spkmi:
  758. mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
  759. break;
  760. case Opt_sec_spkmp:
  761. mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
  762. break;
  763. default:
  764. return 0;
  765. }
  766. mnt->auth_flavor_len = 1;
  767. return 1;
  768. }
  769. /*
  770. * Error-check and convert a string of mount options from user space into
  771. * a data structure. The whole mount string is processed; bad options are
  772. * skipped as they are encountered. If there were no errors, return 1;
  773. * otherwise return 0 (zero).
  774. */
  775. static int nfs_parse_mount_options(char *raw,
  776. struct nfs_parsed_mount_data *mnt)
  777. {
  778. char *p, *string, *secdata;
  779. int rc, sloppy = 0, invalid_option = 0;
  780. if (!raw) {
  781. dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
  782. return 1;
  783. }
  784. dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
  785. secdata = alloc_secdata();
  786. if (!secdata)
  787. goto out_nomem;
  788. rc = security_sb_copy_data(raw, secdata);
  789. if (rc)
  790. goto out_security_failure;
  791. rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
  792. if (rc)
  793. goto out_security_failure;
  794. free_secdata(secdata);
  795. while ((p = strsep(&raw, ",")) != NULL) {
  796. substring_t args[MAX_OPT_ARGS];
  797. unsigned long option;
  798. int token;
  799. if (!*p)
  800. continue;
  801. dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", p);
  802. token = match_token(p, nfs_mount_option_tokens, args);
  803. switch (token) {
  804. /*
  805. * boolean options: foo/nofoo
  806. */
  807. case Opt_soft:
  808. mnt->flags |= NFS_MOUNT_SOFT;
  809. break;
  810. case Opt_hard:
  811. mnt->flags &= ~NFS_MOUNT_SOFT;
  812. break;
  813. case Opt_posix:
  814. mnt->flags |= NFS_MOUNT_POSIX;
  815. break;
  816. case Opt_noposix:
  817. mnt->flags &= ~NFS_MOUNT_POSIX;
  818. break;
  819. case Opt_cto:
  820. mnt->flags &= ~NFS_MOUNT_NOCTO;
  821. break;
  822. case Opt_nocto:
  823. mnt->flags |= NFS_MOUNT_NOCTO;
  824. break;
  825. case Opt_ac:
  826. mnt->flags &= ~NFS_MOUNT_NOAC;
  827. break;
  828. case Opt_noac:
  829. mnt->flags |= NFS_MOUNT_NOAC;
  830. break;
  831. case Opt_lock:
  832. mnt->flags &= ~NFS_MOUNT_NONLM;
  833. break;
  834. case Opt_nolock:
  835. mnt->flags |= NFS_MOUNT_NONLM;
  836. break;
  837. case Opt_v2:
  838. mnt->flags &= ~NFS_MOUNT_VER3;
  839. mnt->version = 2;
  840. break;
  841. case Opt_v3:
  842. mnt->flags |= NFS_MOUNT_VER3;
  843. mnt->version = 3;
  844. break;
  845. #ifdef CONFIG_NFS_V4
  846. case Opt_v4:
  847. mnt->flags &= ~NFS_MOUNT_VER3;
  848. mnt->version = 4;
  849. break;
  850. #endif
  851. case Opt_udp:
  852. mnt->flags &= ~NFS_MOUNT_TCP;
  853. mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
  854. break;
  855. case Opt_tcp:
  856. mnt->flags |= NFS_MOUNT_TCP;
  857. mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
  858. break;
  859. case Opt_rdma:
  860. mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
  861. mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
  862. xprt_load_transport(p);
  863. break;
  864. case Opt_acl:
  865. mnt->flags &= ~NFS_MOUNT_NOACL;
  866. break;
  867. case Opt_noacl:
  868. mnt->flags |= NFS_MOUNT_NOACL;
  869. break;
  870. case Opt_rdirplus:
  871. mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
  872. break;
  873. case Opt_nordirplus:
  874. mnt->flags |= NFS_MOUNT_NORDIRPLUS;
  875. break;
  876. case Opt_sharecache:
  877. mnt->flags &= ~NFS_MOUNT_UNSHARED;
  878. break;
  879. case Opt_nosharecache:
  880. mnt->flags |= NFS_MOUNT_UNSHARED;
  881. break;
  882. case Opt_resvport:
  883. mnt->flags &= ~NFS_MOUNT_NORESVPORT;
  884. break;
  885. case Opt_noresvport:
  886. mnt->flags |= NFS_MOUNT_NORESVPORT;
  887. break;
  888. case Opt_fscache:
  889. mnt->options |= NFS_OPTION_FSCACHE;
  890. kfree(mnt->fscache_uniq);
  891. mnt->fscache_uniq = NULL;
  892. break;
  893. case Opt_nofscache:
  894. mnt->options &= ~NFS_OPTION_FSCACHE;
  895. kfree(mnt->fscache_uniq);
  896. mnt->fscache_uniq = NULL;
  897. break;
  898. case Opt_fscache_uniq:
  899. string = match_strdup(args);
  900. if (!string)
  901. goto out_nomem;
  902. kfree(mnt->fscache_uniq);
  903. mnt->fscache_uniq = string;
  904. mnt->options |= NFS_OPTION_FSCACHE;
  905. break;
  906. /*
  907. * options that take numeric values
  908. */
  909. case Opt_port:
  910. string = match_strdup(args);
  911. if (string == NULL)
  912. goto out_nomem;
  913. rc = strict_strtoul(string, 10, &option);
  914. kfree(string);
  915. if (rc != 0 || option > USHORT_MAX)
  916. goto out_invalid_value;
  917. mnt->nfs_server.port = option;
  918. break;
  919. case Opt_rsize:
  920. string = match_strdup(args);
  921. if (string == NULL)
  922. goto out_nomem;
  923. rc = strict_strtoul(string, 10, &option);
  924. kfree(string);
  925. if (rc != 0)
  926. goto out_invalid_value;
  927. mnt->rsize = option;
  928. break;
  929. case Opt_wsize:
  930. string = match_strdup(args);
  931. if (string == NULL)
  932. goto out_nomem;
  933. rc = strict_strtoul(string, 10, &option);
  934. kfree(string);
  935. if (rc != 0)
  936. goto out_invalid_value;
  937. mnt->wsize = option;
  938. break;
  939. case Opt_bsize:
  940. string = match_strdup(args);
  941. if (string == NULL)
  942. goto out_nomem;
  943. rc = strict_strtoul(string, 10, &option);
  944. kfree(string);
  945. if (rc != 0)
  946. goto out_invalid_value;
  947. mnt->bsize = option;
  948. break;
  949. case Opt_timeo:
  950. string = match_strdup(args);
  951. if (string == NULL)
  952. goto out_nomem;
  953. rc = strict_strtoul(string, 10, &option);
  954. kfree(string);
  955. if (rc != 0 || option == 0)
  956. goto out_invalid_value;
  957. mnt->timeo = option;
  958. break;
  959. case Opt_retrans:
  960. string = match_strdup(args);
  961. if (string == NULL)
  962. goto out_nomem;
  963. rc = strict_strtoul(string, 10, &option);
  964. kfree(string);
  965. if (rc != 0 || option == 0)
  966. goto out_invalid_value;
  967. mnt->retrans = option;
  968. break;
  969. case Opt_acregmin:
  970. string = match_strdup(args);
  971. if (string == NULL)
  972. goto out_nomem;
  973. rc = strict_strtoul(string, 10, &option);
  974. kfree(string);
  975. if (rc != 0)
  976. goto out_invalid_value;
  977. mnt->acregmin = option;
  978. break;
  979. case Opt_acregmax:
  980. string = match_strdup(args);
  981. if (string == NULL)
  982. goto out_nomem;
  983. rc = strict_strtoul(string, 10, &option);
  984. kfree(string);
  985. if (rc != 0)
  986. goto out_invalid_value;
  987. mnt->acregmax = option;
  988. break;
  989. case Opt_acdirmin:
  990. string = match_strdup(args);
  991. if (string == NULL)
  992. goto out_nomem;
  993. rc = strict_strtoul(string, 10, &option);
  994. kfree(string);
  995. if (rc != 0)
  996. goto out_invalid_value;
  997. mnt->acdirmin = option;
  998. break;
  999. case Opt_acdirmax:
  1000. string = match_strdup(args);
  1001. if (string == NULL)
  1002. goto out_nomem;
  1003. rc = strict_strtoul(string, 10, &option);
  1004. kfree(string);
  1005. if (rc != 0)
  1006. goto out_invalid_value;
  1007. mnt->acdirmax = option;
  1008. break;
  1009. case Opt_actimeo:
  1010. string = match_strdup(args);
  1011. if (string == NULL)
  1012. goto out_nomem;
  1013. rc = strict_strtoul(string, 10, &option);
  1014. kfree(string);
  1015. if (rc != 0)
  1016. goto out_invalid_value;
  1017. mnt->acregmin = mnt->acregmax =
  1018. mnt->acdirmin = mnt->acdirmax = option;
  1019. break;
  1020. case Opt_namelen:
  1021. string = match_strdup(args);
  1022. if (string == NULL)
  1023. goto out_nomem;
  1024. rc = strict_strtoul(string, 10, &option);
  1025. kfree(string);
  1026. if (rc != 0)
  1027. goto out_invalid_value;
  1028. mnt->namlen = option;
  1029. break;
  1030. case Opt_mountport:
  1031. string = match_strdup(args);
  1032. if (string == NULL)
  1033. goto out_nomem;
  1034. rc = strict_strtoul(string, 10, &option);
  1035. kfree(string);
  1036. if (rc != 0 || option > USHORT_MAX)
  1037. goto out_invalid_value;
  1038. mnt->mount_server.port = option;
  1039. break;
  1040. case Opt_mountvers:
  1041. string = match_strdup(args);
  1042. if (string == NULL)
  1043. goto out_nomem;
  1044. rc = strict_strtoul(string, 10, &option);
  1045. kfree(string);
  1046. if (rc != 0 ||
  1047. option < NFS_MNT_VERSION ||
  1048. option > NFS_MNT3_VERSION)
  1049. goto out_invalid_value;
  1050. mnt->mount_server.version = option;
  1051. break;
  1052. case Opt_nfsvers:
  1053. string = match_strdup(args);
  1054. if (string == NULL)
  1055. goto out_nomem;
  1056. rc = strict_strtoul(string, 10, &option);
  1057. kfree(string);
  1058. if (rc != 0)
  1059. goto out_invalid_value;
  1060. switch (option) {
  1061. case NFS2_VERSION:
  1062. mnt->flags &= ~NFS_MOUNT_VER3;
  1063. mnt->version = 2;
  1064. break;
  1065. case NFS3_VERSION:
  1066. mnt->flags |= NFS_MOUNT_VER3;
  1067. mnt->version = 3;
  1068. break;
  1069. #ifdef CONFIG_NFS_V4
  1070. case NFS4_VERSION:
  1071. mnt->flags &= ~NFS_MOUNT_VER3;
  1072. mnt->version = 4;
  1073. break;
  1074. #endif
  1075. default:
  1076. goto out_invalid_value;
  1077. }
  1078. break;
  1079. case Opt_minorversion:
  1080. string = match_strdup(args);
  1081. if (string == NULL)
  1082. goto out_nomem;
  1083. rc = strict_strtoul(string, 10, &option);
  1084. kfree(string);
  1085. if (rc != 0)
  1086. goto out_invalid_value;
  1087. if (option > NFS4_MAX_MINOR_VERSION)
  1088. goto out_invalid_value;
  1089. mnt->minorversion = option;
  1090. break;
  1091. /*
  1092. * options that take text values
  1093. */
  1094. case Opt_sec:
  1095. string = match_strdup(args);
  1096. if (string == NULL)
  1097. goto out_nomem;
  1098. rc = nfs_parse_security_flavors(string, mnt);
  1099. kfree(string);
  1100. if (!rc) {
  1101. dfprintk(MOUNT, "NFS: unrecognized "
  1102. "security flavor\n");
  1103. return 0;
  1104. }
  1105. break;
  1106. case Opt_proto:
  1107. string = match_strdup(args);
  1108. if (string == NULL)
  1109. goto out_nomem;
  1110. token = match_token(string,
  1111. nfs_xprt_protocol_tokens, args);
  1112. switch (token) {
  1113. case Opt_xprt_udp:
  1114. mnt->flags &= ~NFS_MOUNT_TCP;
  1115. mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
  1116. kfree(string);
  1117. break;
  1118. case Opt_xprt_tcp:
  1119. mnt->flags |= NFS_MOUNT_TCP;
  1120. mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
  1121. kfree(string);
  1122. break;
  1123. case Opt_xprt_rdma:
  1124. /* vector side protocols to TCP */
  1125. mnt->flags |= NFS_MOUNT_TCP;
  1126. mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
  1127. xprt_load_transport(string);
  1128. kfree(string);
  1129. break;
  1130. default:
  1131. dfprintk(MOUNT, "NFS: unrecognized "
  1132. "transport protocol\n");
  1133. return 0;
  1134. }
  1135. break;
  1136. case Opt_mountproto:
  1137. string = match_strdup(args);
  1138. if (string == NULL)
  1139. goto out_nomem;
  1140. token = match_token(string,
  1141. nfs_xprt_protocol_tokens, args);
  1142. kfree(string);
  1143. switch (token) {
  1144. case Opt_xprt_udp:
  1145. mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
  1146. break;
  1147. case Opt_xprt_tcp:
  1148. mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
  1149. break;
  1150. case Opt_xprt_rdma: /* not used for side protocols */
  1151. default:
  1152. dfprintk(MOUNT, "NFS: unrecognized "
  1153. "transport protocol\n");
  1154. return 0;
  1155. }
  1156. break;
  1157. case Opt_addr:
  1158. string = match_strdup(args);
  1159. if (string == NULL)
  1160. goto out_nomem;
  1161. mnt->nfs_server.addrlen =
  1162. rpc_pton(string, strlen(string),
  1163. (struct sockaddr *)
  1164. &mnt->nfs_server.address,
  1165. sizeof(mnt->nfs_server.address));
  1166. kfree(string);
  1167. if (mnt->nfs_server.addrlen == 0)
  1168. goto out_invalid_address;
  1169. break;
  1170. case Opt_clientaddr:
  1171. string = match_strdup(args);
  1172. if (string == NULL)
  1173. goto out_nomem;
  1174. kfree(mnt->client_address);
  1175. mnt->client_address = string;
  1176. break;
  1177. case Opt_mounthost:
  1178. string = match_strdup(args);
  1179. if (string == NULL)
  1180. goto out_nomem;
  1181. kfree(mnt->mount_server.hostname);
  1182. mnt->mount_server.hostname = string;
  1183. break;
  1184. case Opt_mountaddr:
  1185. string = match_strdup(args);
  1186. if (string == NULL)
  1187. goto out_nomem;
  1188. mnt->mount_server.addrlen =
  1189. rpc_pton(string, strlen(string),
  1190. (struct sockaddr *)
  1191. &mnt->mount_server.address,
  1192. sizeof(mnt->mount_server.address));
  1193. kfree(string);
  1194. if (mnt->mount_server.addrlen == 0)
  1195. goto out_invalid_address;
  1196. break;
  1197. case Opt_lookupcache:
  1198. string = match_strdup(args);
  1199. if (string == NULL)
  1200. goto out_nomem;
  1201. token = match_token(string,
  1202. nfs_lookupcache_tokens, args);
  1203. kfree(string);
  1204. switch (token) {
  1205. case Opt_lookupcache_all:
  1206. mnt->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
  1207. break;
  1208. case Opt_lookupcache_positive:
  1209. mnt->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
  1210. mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
  1211. break;
  1212. case Opt_lookupcache_none:
  1213. mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
  1214. break;
  1215. default:
  1216. dfprintk(MOUNT, "NFS: invalid "
  1217. "lookupcache argument\n");
  1218. return 0;
  1219. };
  1220. break;
  1221. /*
  1222. * Special options
  1223. */
  1224. case Opt_sloppy:
  1225. sloppy = 1;
  1226. dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
  1227. break;
  1228. case Opt_userspace:
  1229. case Opt_deprecated:
  1230. dfprintk(MOUNT, "NFS: ignoring mount option "
  1231. "'%s'\n", p);
  1232. break;
  1233. default:
  1234. invalid_option = 1;
  1235. dfprintk(MOUNT, "NFS: unrecognized mount option "
  1236. "'%s'\n", p);
  1237. }
  1238. }
  1239. if (!sloppy && invalid_option)
  1240. return 0;
  1241. return 1;
  1242. out_invalid_address:
  1243. printk(KERN_INFO "NFS: bad IP address specified: %s\n", p);
  1244. return 0;
  1245. out_invalid_value:
  1246. printk(KERN_INFO "NFS: bad mount option value specified: %s\n", p);
  1247. return 0;
  1248. out_nomem:
  1249. printk(KERN_INFO "NFS: not enough memory to parse option\n");
  1250. return 0;
  1251. out_security_failure:
  1252. free_secdata(secdata);
  1253. printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
  1254. return 0;
  1255. }
  1256. /*
  1257. * Match the requested auth flavors with the list returned by
  1258. * the server. Returns zero and sets the mount's authentication
  1259. * flavor on success; returns -EACCES if server does not support
  1260. * the requested flavor.
  1261. */
  1262. static int nfs_walk_authlist(struct nfs_parsed_mount_data *args,
  1263. struct nfs_mount_request *request)
  1264. {
  1265. unsigned int i, j, server_authlist_len = *(request->auth_flav_len);
  1266. /*
  1267. * Certain releases of Linux's mountd return an empty
  1268. * flavor list. To prevent behavioral regression with
  1269. * these servers (ie. rejecting mounts that used to
  1270. * succeed), revert to pre-2.6.32 behavior (no checking)
  1271. * if the returned flavor list is empty.
  1272. */
  1273. if (server_authlist_len == 0)
  1274. return 0;
  1275. /*
  1276. * We avoid sophisticated negotiating here, as there are
  1277. * plenty of cases where we can get it wrong, providing
  1278. * either too little or too much security.
  1279. *
  1280. * RFC 2623, section 2.7 suggests we SHOULD prefer the
  1281. * flavor listed first. However, some servers list
  1282. * AUTH_NULL first. Our caller plants AUTH_SYS, the
  1283. * preferred default, in args->auth_flavors[0] if user
  1284. * didn't specify sec= mount option.
  1285. */
  1286. for (i = 0; i < args->auth_flavor_len; i++)
  1287. for (j = 0; j < server_authlist_len; j++)
  1288. if (args->auth_flavors[i] == request->auth_flavs[j]) {
  1289. dfprintk(MOUNT, "NFS: using auth flavor %d\n",
  1290. request->auth_flavs[j]);
  1291. args->auth_flavors[0] = request->auth_flavs[j];
  1292. return 0;
  1293. }
  1294. dfprintk(MOUNT, "NFS: server does not support requested auth flavor\n");
  1295. nfs_umount(request);
  1296. return -EACCES;
  1297. }
  1298. /*
  1299. * Use the remote server's MOUNT service to request the NFS file handle
  1300. * corresponding to the provided path.
  1301. */
  1302. static int nfs_try_mount(struct nfs_parsed_mount_data *args,
  1303. struct nfs_fh *root_fh)
  1304. {
  1305. rpc_authflavor_t server_authlist[NFS_MAX_SECFLAVORS];
  1306. unsigned int server_authlist_len = ARRAY_SIZE(server_authlist);
  1307. struct nfs_mount_request request = {
  1308. .sap = (struct sockaddr *)
  1309. &args->mount_server.address,
  1310. .dirpath = args->nfs_server.export_path,
  1311. .protocol = args->mount_server.protocol,
  1312. .fh = root_fh,
  1313. .noresvport = args->flags & NFS_MOUNT_NORESVPORT,
  1314. .auth_flav_len = &server_authlist_len,
  1315. .auth_flavs = server_authlist,
  1316. };
  1317. int status;
  1318. if (args->mount_server.version == 0) {
  1319. switch (args->version) {
  1320. default:
  1321. args->mount_server.version = NFS_MNT3_VERSION;
  1322. break;
  1323. case 2:
  1324. args->mount_server.version = NFS_MNT_VERSION;
  1325. }
  1326. }
  1327. request.version = args->mount_server.version;
  1328. if (args->mount_server.hostname)
  1329. request.hostname = args->mount_server.hostname;
  1330. else
  1331. request.hostname = args->nfs_server.hostname;
  1332. /*
  1333. * Construct the mount server's address.
  1334. */
  1335. if (args->mount_server.address.ss_family == AF_UNSPEC) {
  1336. memcpy(request.sap, &args->nfs_server.address,
  1337. args->nfs_server.addrlen);
  1338. args->mount_server.addrlen = args->nfs_server.addrlen;
  1339. }
  1340. request.salen = args->mount_server.addrlen;
  1341. nfs_set_default_port(request.sap, args->mount_server.port, 0);
  1342. /*
  1343. * Now ask the mount server to map our export path
  1344. * to a file handle.
  1345. */
  1346. status = nfs_mount(&request);
  1347. if (status != 0) {
  1348. dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
  1349. request.hostname, status);
  1350. return status;
  1351. }
  1352. /*
  1353. * MNTv1 (NFSv2) does not support auth flavor negotiation.
  1354. */
  1355. if (args->mount_server.version != NFS_MNT3_VERSION)
  1356. return 0;
  1357. return nfs_walk_authlist(args, &request);
  1358. }
  1359. static int nfs_parse_simple_hostname(const char *dev_name,
  1360. char **hostname, size_t maxnamlen,
  1361. char **export_path, size_t maxpathlen)
  1362. {
  1363. size_t len;
  1364. char *colon, *comma;
  1365. colon = strchr(dev_name, ':');
  1366. if (colon == NULL)
  1367. goto out_bad_devname;
  1368. len = colon - dev_name;
  1369. if (len > maxnamlen)
  1370. goto out_hostname;
  1371. /* N.B. caller will free nfs_server.hostname in all cases */
  1372. *hostname = kstrndup(dev_name, len, GFP_KERNEL);
  1373. if (!*hostname)
  1374. goto out_nomem;
  1375. /* kill possible hostname list: not supported */
  1376. comma = strchr(*hostname, ',');
  1377. if (comma != NULL) {
  1378. if (comma == *hostname)
  1379. goto out_bad_devname;
  1380. *comma = '\0';
  1381. }
  1382. colon++;
  1383. len = strlen(colon);
  1384. if (len > maxpathlen)
  1385. goto out_path;
  1386. *export_path = kstrndup(colon, len, GFP_KERNEL);
  1387. if (!*export_path)
  1388. goto out_nomem;
  1389. dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
  1390. return 0;
  1391. out_bad_devname:
  1392. dfprintk(MOUNT, "NFS: device name not in host:path format\n");
  1393. return -EINVAL;
  1394. out_nomem:
  1395. dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
  1396. return -ENOMEM;
  1397. out_hostname:
  1398. dfprintk(MOUNT, "NFS: server hostname too long\n");
  1399. return -ENAMETOOLONG;
  1400. out_path:
  1401. dfprintk(MOUNT, "NFS: export pathname too long\n");
  1402. return -ENAMETOOLONG;
  1403. }
  1404. /*
  1405. * Hostname has square brackets around it because it contains one or
  1406. * more colons. We look for the first closing square bracket, and a
  1407. * colon must follow it.
  1408. */
  1409. static int nfs_parse_protected_hostname(const char *dev_name,
  1410. char **hostname, size_t maxnamlen,
  1411. char **export_path, size_t maxpathlen)
  1412. {
  1413. size_t len;
  1414. char *start, *end;
  1415. start = (char *)(dev_name + 1);
  1416. end = strchr(start, ']');
  1417. if (end == NULL)
  1418. goto out_bad_devname;
  1419. if (*(end + 1) != ':')
  1420. goto out_bad_devname;
  1421. len = end - start;
  1422. if (len > maxnamlen)
  1423. goto out_hostname;
  1424. /* N.B. caller will free nfs_server.hostname in all cases */
  1425. *hostname = kstrndup(start, len, GFP_KERNEL);
  1426. if (*hostname == NULL)
  1427. goto out_nomem;
  1428. end += 2;
  1429. len = strlen(end);
  1430. if (len > maxpathlen)
  1431. goto out_path;
  1432. *export_path = kstrndup(end, len, GFP_KERNEL);
  1433. if (!*export_path)
  1434. goto out_nomem;
  1435. return 0;
  1436. out_bad_devname:
  1437. dfprintk(MOUNT, "NFS: device name not in host:path format\n");
  1438. return -EINVAL;
  1439. out_nomem:
  1440. dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
  1441. return -ENOMEM;
  1442. out_hostname:
  1443. dfprintk(MOUNT, "NFS: server hostname too long\n");
  1444. return -ENAMETOOLONG;
  1445. out_path:
  1446. dfprintk(MOUNT, "NFS: export pathname too long\n");
  1447. return -ENAMETOOLONG;
  1448. }
  1449. /*
  1450. * Split "dev_name" into "hostname:export_path".
  1451. *
  1452. * The leftmost colon demarks the split between the server's hostname
  1453. * and the export path. If the hostname starts with a left square
  1454. * bracket, then it may contain colons.
  1455. *
  1456. * Note: caller frees hostname and export path, even on error.
  1457. */
  1458. static int nfs_parse_devname(const char *dev_name,
  1459. char **hostname, size_t maxnamlen,
  1460. char **export_path, size_t maxpathlen)
  1461. {
  1462. if (*dev_name == '[')
  1463. return nfs_parse_protected_hostname(dev_name,
  1464. hostname, maxnamlen,
  1465. export_path, maxpathlen);
  1466. return nfs_parse_simple_hostname(dev_name,
  1467. hostname, maxnamlen,
  1468. export_path, maxpathlen);
  1469. }
  1470. /*
  1471. * Validate the NFS2/NFS3 mount data
  1472. * - fills in the mount root filehandle
  1473. *
  1474. * For option strings, user space handles the following behaviors:
  1475. *
  1476. * + DNS: mapping server host name to IP address ("addr=" option)
  1477. *
  1478. * + failure mode: how to behave if a mount request can't be handled
  1479. * immediately ("fg/bg" option)
  1480. *
  1481. * + retry: how often to retry a mount request ("retry=" option)
  1482. *
  1483. * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
  1484. * mountproto=tcp after mountproto=udp, and so on
  1485. */
  1486. static int nfs_validate_mount_data(void *options,
  1487. struct nfs_parsed_mount_data *args,
  1488. struct nfs_fh *mntfh,
  1489. const char *dev_name)
  1490. {
  1491. struct nfs_mount_data *data = (struct nfs_mount_data *)options;
  1492. struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
  1493. if (data == NULL)
  1494. goto out_no_data;
  1495. switch (data->version) {
  1496. case 1:
  1497. data->namlen = 0;
  1498. case 2:
  1499. data->bsize = 0;
  1500. case 3:
  1501. if (data->flags & NFS_MOUNT_VER3)
  1502. goto out_no_v3;
  1503. data->root.size = NFS2_FHSIZE;
  1504. memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
  1505. case 4:
  1506. if (data->flags & NFS_MOUNT_SECFLAVOUR)
  1507. goto out_no_sec;
  1508. case 5:
  1509. memset(data->context, 0, sizeof(data->context));
  1510. case 6:
  1511. if (data->flags & NFS_MOUNT_VER3) {
  1512. if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
  1513. goto out_invalid_fh;
  1514. mntfh->size = data->root.size;
  1515. args->version = 3;
  1516. } else {
  1517. mntfh->size = NFS2_FHSIZE;
  1518. args->version = 2;
  1519. }
  1520. memcpy(mntfh->data, data->root.data, mntfh->size);
  1521. if (mntfh->size < sizeof(mntfh->data))
  1522. memset(mntfh->data + mntfh->size, 0,
  1523. sizeof(mntfh->data) - mntfh->size);
  1524. /*
  1525. * Translate to nfs_parsed_mount_data, which nfs_fill_super
  1526. * can deal with.
  1527. */
  1528. args->flags = data->flags & NFS_MOUNT_FLAGMASK;
  1529. args->rsize = data->rsize;
  1530. args->wsize = data->wsize;
  1531. args->timeo = data->timeo;
  1532. args->retrans = data->retrans;
  1533. args->acregmin = data->acregmin;
  1534. args->acregmax = data->acregmax;
  1535. args->acdirmin = data->acdirmin;
  1536. args->acdirmax = data->acdirmax;
  1537. memcpy(sap, &data->addr, sizeof(data->addr));
  1538. args->nfs_server.addrlen = sizeof(data->addr);
  1539. if (!nfs_verify_server_address(sap))
  1540. goto out_no_address;
  1541. if (!(data->flags & NFS_MOUNT_TCP))
  1542. args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
  1543. else
  1544. args->nfs_server.protocol = XPRT_TRANSPORT_TCP;
  1545. /* N.B. caller will free nfs_server.hostname in all cases */
  1546. args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
  1547. args->namlen = data->namlen;
  1548. args->bsize = data->bsize;
  1549. if (data->flags & NFS_MOUNT_SECFLAVOUR)
  1550. args->auth_flavors[0] = data->pseudoflavor;
  1551. if (!args->nfs_server.hostname)
  1552. goto out_nomem;
  1553. /*
  1554. * The legacy version 6 binary mount data from userspace has a
  1555. * field used only to transport selinux information into the
  1556. * the kernel. To continue to support that functionality we
  1557. * have a touch of selinux knowledge here in the NFS code. The
  1558. * userspace code converted context=blah to just blah so we are
  1559. * converting back to the full string selinux understands.
  1560. */
  1561. if (data->context[0]){
  1562. #ifdef CONFIG_SECURITY_SELINUX
  1563. int rc;
  1564. char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
  1565. if (!opts_str)
  1566. return -ENOMEM;
  1567. strcpy(opts_str, "context=");
  1568. data->context[NFS_MAX_CONTEXT_LEN] = '\0';
  1569. strcat(opts_str, &data->context[0]);
  1570. rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
  1571. kfree(opts_str);
  1572. if (rc)
  1573. return rc;
  1574. #else
  1575. return -EINVAL;
  1576. #endif
  1577. }
  1578. break;
  1579. default: {
  1580. int status;
  1581. if (nfs_parse_mount_options((char *)options, args) == 0)
  1582. return -EINVAL;
  1583. if (!nfs_verify_server_address(sap))
  1584. goto out_no_address;
  1585. if (args->version == 4)
  1586. #ifdef CONFIG_NFS_V4
  1587. return nfs4_validate_text_mount_data(options,
  1588. args, dev_name);
  1589. #else
  1590. goto out_v4_not_compiled;
  1591. #endif
  1592. nfs_set_default_port(sap, args->nfs_server.port, 0);
  1593. nfs_set_mount_transport_protocol(args);
  1594. status = nfs_parse_devname(dev_name,
  1595. &args->nfs_server.hostname,
  1596. PAGE_SIZE,
  1597. &args->nfs_server.export_path,
  1598. NFS_MAXPATHLEN);
  1599. if (!status)
  1600. status = nfs_try_mount(args, mntfh);
  1601. kfree(args->nfs_server.export_path);
  1602. args->nfs_server.export_path = NULL;
  1603. if (status)
  1604. return status;
  1605. break;
  1606. }
  1607. }
  1608. #ifndef CONFIG_NFS_V3
  1609. if (args->version == 3)
  1610. goto out_v3_not_compiled;
  1611. #endif /* !CONFIG_NFS_V3 */
  1612. return 0;
  1613. out_no_data:
  1614. dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
  1615. return -EINVAL;
  1616. out_no_v3:
  1617. dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
  1618. data->version);
  1619. return -EINVAL;
  1620. out_no_sec:
  1621. dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
  1622. return -EINVAL;
  1623. #ifndef CONFIG_NFS_V3
  1624. out_v3_not_compiled:
  1625. dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
  1626. return -EPROTONOSUPPORT;
  1627. #endif /* !CONFIG_NFS_V3 */
  1628. #ifndef CONFIG_NFS_V4
  1629. out_v4_not_compiled:
  1630. dfprintk(MOUNT, "NFS: NFSv4 is not compiled into kernel\n");
  1631. return -EPROTONOSUPPORT;
  1632. #endif /* !CONFIG_NFS_V4 */
  1633. out_nomem:
  1634. dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
  1635. return -ENOMEM;
  1636. out_no_address:
  1637. dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
  1638. return -EINVAL;
  1639. out_invalid_fh:
  1640. dfprintk(MOUNT, "NFS: invalid root filehandle\n");
  1641. return -EINVAL;
  1642. }
  1643. static int
  1644. nfs_compare_remount_data(struct nfs_server *nfss,
  1645. struct nfs_parsed_mount_data *data)
  1646. {
  1647. if (data->flags != nfss->flags ||
  1648. data->rsize != nfss->rsize ||
  1649. data->wsize != nfss->wsize ||
  1650. data->retrans != nfss->client->cl_timeout->to_retries ||
  1651. data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
  1652. data->acregmin != nfss->acregmin / HZ ||
  1653. data->acregmax != nfss->acregmax / HZ ||
  1654. data->acdirmin != nfss->acdirmin / HZ ||
  1655. data->acdirmax != nfss->acdirmax / HZ ||
  1656. data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
  1657. data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
  1658. memcmp(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
  1659. data->nfs_server.addrlen) != 0)
  1660. return -EINVAL;
  1661. return 0;
  1662. }
  1663. static int
  1664. nfs_remount(struct super_block *sb, int *flags, char *raw_data)
  1665. {
  1666. int error;
  1667. struct nfs_server *nfss = sb->s_fs_info;
  1668. struct nfs_parsed_mount_data *data;
  1669. struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
  1670. struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
  1671. u32 nfsvers = nfss->nfs_client->rpc_ops->version;
  1672. /*
  1673. * Userspace mount programs that send binary options generally send
  1674. * them populated with default values. We have no way to know which
  1675. * ones were explicitly specified. Fall back to legacy behavior and
  1676. * just return success.
  1677. */
  1678. if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
  1679. (nfsvers <= 3 && (!options || (options->version >= 1 &&
  1680. options->version <= 6))))
  1681. return 0;
  1682. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1683. if (data == NULL)
  1684. return -ENOMEM;
  1685. lock_kernel();
  1686. /* fill out struct with values from existing mount */
  1687. data->flags = nfss->flags;
  1688. data->rsize = nfss->rsize;
  1689. data->wsize = nfss->wsize;
  1690. data->retrans = nfss->client->cl_timeout->to_retries;
  1691. data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
  1692. data->acregmin = nfss->acregmin / HZ;
  1693. data->acregmax = nfss->acregmax / HZ;
  1694. data->acdirmin = nfss->acdirmin / HZ;
  1695. data->acdirmax = nfss->acdirmax / HZ;
  1696. data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
  1697. data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
  1698. memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
  1699. data->nfs_server.addrlen);
  1700. /* overwrite those values with any that were specified */
  1701. error = nfs_parse_mount_options((char *)options, data);
  1702. if (error < 0)
  1703. goto out;
  1704. /* compare new mount options with old ones */
  1705. error = nfs_compare_remount_data(nfss, data);
  1706. out:
  1707. kfree(data);
  1708. unlock_kernel();
  1709. return error;
  1710. }
  1711. /*
  1712. * Initialise the common bits of the superblock
  1713. */
  1714. static inline void nfs_initialise_sb(struct super_block *sb)
  1715. {
  1716. struct nfs_server *server = NFS_SB(sb);
  1717. sb->s_magic = NFS_SUPER_MAGIC;
  1718. /* We probably want something more informative here */
  1719. snprintf(sb->s_id, sizeof(sb->s_id),
  1720. "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
  1721. if (sb->s_blocksize == 0)
  1722. sb->s_blocksize = nfs_block_bits(server->wsize,
  1723. &sb->s_blocksize_bits);
  1724. if (server->flags & NFS_MOUNT_NOAC)
  1725. sb->s_flags |= MS_SYNCHRONOUS;
  1726. sb->s_bdi = &server->backing_dev_info;
  1727. nfs_super_set_maxbytes(sb, server->maxfilesize);
  1728. }
  1729. /*
  1730. * Finish setting up an NFS2/3 superblock
  1731. */
  1732. static void nfs_fill_super(struct super_block *sb,
  1733. struct nfs_parsed_mount_data *data)
  1734. {
  1735. struct nfs_server *server = NFS_SB(sb);
  1736. sb->s_blocksize_bits = 0;
  1737. sb->s_blocksize = 0;
  1738. if (data->bsize)
  1739. sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
  1740. if (server->nfs_client->rpc_ops->version == 3) {
  1741. /* The VFS shouldn't apply the umask to mode bits. We will do
  1742. * so ourselves when necessary.
  1743. */
  1744. sb->s_flags |= MS_POSIXACL;
  1745. sb->s_time_gran = 1;
  1746. }
  1747. sb->s_op = &nfs_sops;
  1748. nfs_initialise_sb(sb);
  1749. }
  1750. /*
  1751. * Finish setting up a cloned NFS2/3 superblock
  1752. */
  1753. static void nfs_clone_super(struct super_block *sb,
  1754. const struct super_block *old_sb)
  1755. {
  1756. struct nfs_server *server = NFS_SB(sb);
  1757. sb->s_blocksize_bits = old_sb->s_blocksize_bits;
  1758. sb->s_blocksize = old_sb->s_blocksize;
  1759. sb->s_maxbytes = old_sb->s_maxbytes;
  1760. if (server->nfs_client->rpc_ops->version == 3) {
  1761. /* The VFS shouldn't apply the umask to mode bits. We will do
  1762. * so ourselves when necessary.
  1763. */
  1764. sb->s_flags |= MS_POSIXACL;
  1765. sb->s_time_gran = 1;
  1766. }
  1767. sb->s_op = old_sb->s_op;
  1768. nfs_initialise_sb(sb);
  1769. }
  1770. static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
  1771. {
  1772. const struct nfs_server *a = s->s_fs_info;
  1773. const struct rpc_clnt *clnt_a = a->client;
  1774. const struct rpc_clnt *clnt_b = b->client;
  1775. if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
  1776. goto Ebusy;
  1777. if (a->nfs_client != b->nfs_client)
  1778. goto Ebusy;
  1779. if (a->flags != b->flags)
  1780. goto Ebusy;
  1781. if (a->wsize != b->wsize)
  1782. goto Ebusy;
  1783. if (a->rsize != b->rsize)
  1784. goto Ebusy;
  1785. if (a->acregmin != b->acregmin)
  1786. goto Ebusy;
  1787. if (a->acregmax != b->acregmax)
  1788. goto Ebusy;
  1789. if (a->acdirmin != b->acdirmin)
  1790. goto Ebusy;
  1791. if (a->acdirmax != b->acdirmax)
  1792. goto Ebusy;
  1793. if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
  1794. goto Ebusy;
  1795. return 1;
  1796. Ebusy:
  1797. return 0;
  1798. }
  1799. struct nfs_sb_mountdata {
  1800. struct nfs_server *server;
  1801. int mntflags;
  1802. };
  1803. static int nfs_set_super(struct super_block *s, void *data)
  1804. {
  1805. struct nfs_sb_mountdata *sb_mntdata = data;
  1806. struct nfs_server *server = sb_mntdata->server;
  1807. int ret;
  1808. s->s_flags = sb_mntdata->mntflags;
  1809. s->s_fs_info = server;
  1810. ret = set_anon_super(s, server);
  1811. if (ret == 0)
  1812. server->s_dev = s->s_dev;
  1813. return ret;
  1814. }
  1815. static int nfs_compare_super_address(struct nfs_server *server1,
  1816. struct nfs_server *server2)
  1817. {
  1818. struct sockaddr *sap1, *sap2;
  1819. sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
  1820. sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
  1821. if (sap1->sa_family != sap2->sa_family)
  1822. return 0;
  1823. switch (sap1->sa_family) {
  1824. case AF_INET: {
  1825. struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
  1826. struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
  1827. if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
  1828. return 0;
  1829. if (sin1->sin_port != sin2->sin_port)
  1830. return 0;
  1831. break;
  1832. }
  1833. case AF_INET6: {
  1834. struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
  1835. struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
  1836. if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
  1837. return 0;
  1838. if (sin1->sin6_port != sin2->sin6_port)
  1839. return 0;
  1840. break;
  1841. }
  1842. default:
  1843. return 0;
  1844. }
  1845. return 1;
  1846. }
  1847. static int nfs_compare_super(struct super_block *sb, void *data)
  1848. {
  1849. struct nfs_sb_mountdata *sb_mntdata = data;
  1850. struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
  1851. int mntflags = sb_mntdata->mntflags;
  1852. if (!nfs_compare_super_address(old, server))
  1853. return 0;
  1854. /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
  1855. if (old->flags & NFS_MOUNT_UNSHARED)
  1856. return 0;
  1857. if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
  1858. return 0;
  1859. return nfs_compare_mount_options(sb, server, mntflags);
  1860. }
  1861. static int nfs_bdi_register(struct nfs_server *server)
  1862. {
  1863. return bdi_register_dev(&server->backing_dev_info, server->s_dev);
  1864. }
  1865. static int nfs_get_sb(struct file_system_type *fs_type,
  1866. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
  1867. {
  1868. struct nfs_server *server = NULL;
  1869. struct super_block *s;
  1870. struct nfs_parsed_mount_data *data;
  1871. struct nfs_fh *mntfh;
  1872. struct dentry *mntroot;
  1873. int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
  1874. struct nfs_sb_mountdata sb_mntdata = {
  1875. .mntflags = flags,
  1876. };
  1877. int error = -ENOMEM;
  1878. data = nfs_alloc_parsed_mount_data(NFS_MOUNT_VER3 | NFS_MOUNT_TCP);
  1879. mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
  1880. if (data == NULL || mntfh == NULL)
  1881. goto out_free_fh;
  1882. security_init_mnt_opts(&data->lsm_opts);
  1883. /* Validate the mount data */
  1884. error = nfs_validate_mount_data(raw_data, data, mntfh, dev_name);
  1885. if (error < 0)
  1886. goto out;
  1887. #ifdef CONFIG_NFS_V4
  1888. if (data->version == 4) {
  1889. error = nfs4_try_mount(flags, dev_name, data, mnt);
  1890. kfree(data->client_address);
  1891. goto out;
  1892. }
  1893. #endif /* CONFIG_NFS_V4 */
  1894. /* Get a volume representation */
  1895. server = nfs_create_server(data, mntfh);
  1896. if (IS_ERR(server)) {
  1897. error = PTR_ERR(server);
  1898. goto out;
  1899. }
  1900. sb_mntdata.server = server;
  1901. if (server->flags & NFS_MOUNT_UNSHARED)
  1902. compare_super = NULL;
  1903. /* Get a superblock - note that we may end up sharing one that already exists */
  1904. s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
  1905. if (IS_ERR(s)) {
  1906. error = PTR_ERR(s);
  1907. goto out_err_nosb;
  1908. }
  1909. if (s->s_fs_info != server) {
  1910. nfs_free_server(server);
  1911. server = NULL;
  1912. } else {
  1913. error = nfs_bdi_register(server);
  1914. if (error)
  1915. goto error_splat_super;
  1916. }
  1917. if (!s->s_root) {
  1918. /* initial superblock/root creation */
  1919. nfs_fill_super(s, data);
  1920. nfs_fscache_get_super_cookie(
  1921. s, data ? data->fscache_uniq : NULL, NULL);
  1922. }
  1923. mntroot = nfs_get_root(s, mntfh);
  1924. if (IS_ERR(mntroot)) {
  1925. error = PTR_ERR(mntroot);
  1926. goto error_splat_super;
  1927. }
  1928. error = security_sb_set_mnt_opts(s, &data->lsm_opts);
  1929. if (error)
  1930. goto error_splat_root;
  1931. s->s_flags |= MS_ACTIVE;
  1932. mnt->mnt_sb = s;
  1933. mnt->mnt_root = mntroot;
  1934. error = 0;
  1935. out:
  1936. kfree(data->nfs_server.hostname);
  1937. kfree(data->mount_server.hostname);
  1938. kfree(data->fscache_uniq);
  1939. security_free_mnt_opts(&data->lsm_opts);
  1940. out_free_fh:
  1941. kfree(mntfh);
  1942. kfree(data);
  1943. return error;
  1944. out_err_nosb:
  1945. nfs_free_server(server);
  1946. goto out;
  1947. error_splat_root:
  1948. dput(mntroot);
  1949. error_splat_super:
  1950. deactivate_locked_super(s);
  1951. goto out;
  1952. }
  1953. /*
  1954. * Destroy an NFS2/3 superblock
  1955. */
  1956. static void nfs_kill_super(struct super_block *s)
  1957. {
  1958. struct nfs_server *server = NFS_SB(s);
  1959. kill_anon_super(s);
  1960. bdi_unregister(&server->backing_dev_info);
  1961. nfs_fscache_release_super_cookie(s);
  1962. nfs_free_server(server);
  1963. }
  1964. /*
  1965. * Clone an NFS2/3 server record on xdev traversal (FSID-change)
  1966. */
  1967. static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
  1968. const char *dev_name, void *raw_data,
  1969. struct vfsmount *mnt)
  1970. {
  1971. struct nfs_clone_mount *data = raw_data;
  1972. struct super_block *s;
  1973. struct nfs_server *server;
  1974. struct dentry *mntroot;
  1975. int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
  1976. struct nfs_sb_mountdata sb_mntdata = {
  1977. .mntflags = flags,
  1978. };
  1979. int error;
  1980. dprintk("--> nfs_xdev_get_sb()\n");
  1981. /* create a new volume representation */
  1982. server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
  1983. if (IS_ERR(server)) {
  1984. error = PTR_ERR(server);
  1985. goto out_err_noserver;
  1986. }
  1987. sb_mntdata.server = server;
  1988. if (server->flags & NFS_MOUNT_UNSHARED)
  1989. compare_super = NULL;
  1990. /* Get a superblock - note that we may end up sharing one that already exists */
  1991. s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
  1992. if (IS_ERR(s)) {
  1993. error = PTR_ERR(s);
  1994. goto out_err_nosb;
  1995. }
  1996. if (s->s_fs_info != server) {
  1997. nfs_free_server(server);
  1998. server = NULL;
  1999. } else {
  2000. error = nfs_bdi_register(server);
  2001. if (error)
  2002. goto error_splat_super;
  2003. }
  2004. if (!s->s_root) {
  2005. /* initial superblock/root creation */
  2006. nfs_clone_super(s, data->sb);
  2007. nfs_fscache_get_super_cookie(s, NULL, data);
  2008. }
  2009. mntroot = nfs_get_root(s, data->fh);
  2010. if (IS_ERR(mntroot)) {
  2011. error = PTR_ERR(mntroot);
  2012. goto error_splat_super;
  2013. }
  2014. if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
  2015. dput(mntroot);
  2016. error = -ESTALE;
  2017. goto error_splat_super;
  2018. }
  2019. s->s_flags |= MS_ACTIVE;
  2020. mnt->mnt_sb = s;
  2021. mnt->mnt_root = mntroot;
  2022. /* clone any lsm security options from the parent to the new sb */
  2023. security_sb_clone_mnt_opts(data->sb, s);
  2024. dprintk("<-- nfs_xdev_get_sb() = 0\n");
  2025. return 0;
  2026. out_err_nosb:
  2027. nfs_free_server(server);
  2028. out_err_noserver:
  2029. dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
  2030. return error;
  2031. error_splat_super:
  2032. deactivate_locked_super(s);
  2033. dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
  2034. return error;
  2035. }
  2036. #ifdef CONFIG_NFS_V4
  2037. /*
  2038. * Finish setting up a cloned NFS4 superblock
  2039. */
  2040. static void nfs4_clone_super(struct super_block *sb,
  2041. const struct super_block *old_sb)
  2042. {
  2043. sb->s_blocksize_bits = old_sb->s_blocksize_bits;
  2044. sb->s_blocksize = old_sb->s_blocksize;
  2045. sb->s_maxbytes = old_sb->s_maxbytes;
  2046. sb->s_time_gran = 1;
  2047. sb->s_op = old_sb->s_op;
  2048. nfs_initialise_sb(sb);
  2049. }
  2050. /*
  2051. * Set up an NFS4 superblock
  2052. */
  2053. static void nfs4_fill_super(struct super_block *sb)
  2054. {
  2055. sb->s_time_gran = 1;
  2056. sb->s_op = &nfs4_sops;
  2057. nfs_initialise_sb(sb);
  2058. }
  2059. static void nfs4_validate_mount_flags(struct nfs_parsed_mount_data *args)
  2060. {
  2061. args->flags &= ~(NFS_MOUNT_NONLM|NFS_MOUNT_NOACL|NFS_MOUNT_VER3);
  2062. }
  2063. static int nfs4_validate_text_mount_data(void *options,
  2064. struct nfs_parsed_mount_data *args,
  2065. const char *dev_name)
  2066. {
  2067. struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
  2068. nfs_set_default_port(sap, args->nfs_server.port, NFS_PORT);
  2069. nfs_validate_transport_protocol(args);
  2070. nfs4_validate_mount_flags(args);
  2071. if (args->version != 4) {
  2072. dfprintk(MOUNT,
  2073. "NFS4: Illegal mount version\n");
  2074. return -EINVAL;
  2075. }
  2076. if (args->auth_flavor_len > 1) {
  2077. dfprintk(MOUNT,
  2078. "NFS4: Too many RPC auth flavours specified\n");
  2079. return -EINVAL;
  2080. }
  2081. if (args->client_address == NULL) {
  2082. dfprintk(MOUNT,
  2083. "NFS4: mount program didn't pass callback address\n");
  2084. return -EINVAL;
  2085. }
  2086. return nfs_parse_devname(dev_name,
  2087. &args->nfs_server.hostname,
  2088. NFS4_MAXNAMLEN,
  2089. &args->nfs_server.export_path,
  2090. NFS4_MAXPATHLEN);
  2091. }
  2092. /*
  2093. * Validate NFSv4 mount options
  2094. */
  2095. static int nfs4_validate_mount_data(void *options,
  2096. struct nfs_parsed_mount_data *args,
  2097. const char *dev_name)
  2098. {
  2099. struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
  2100. struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
  2101. char *c;
  2102. if (data == NULL)
  2103. goto out_no_data;
  2104. args->version = 4;
  2105. switch (data->version) {
  2106. case 1:
  2107. if (data->host_addrlen > sizeof(args->nfs_server.address))
  2108. goto out_no_address;
  2109. if (data->host_addrlen == 0)
  2110. goto out_no_address;
  2111. args->nfs_server.addrlen = data->host_addrlen;
  2112. if (copy_from_user(sap, data->host_addr, data->host_addrlen))
  2113. return -EFAULT;
  2114. if (!nfs_verify_server_address(sap))
  2115. goto out_no_address;
  2116. if (data->auth_flavourlen) {
  2117. if (data->auth_flavourlen > 1)
  2118. goto out_inval_auth;
  2119. if (copy_from_user(&args->auth_flavors[0],
  2120. data->auth_flavours,
  2121. sizeof(args->auth_flavors[0])))
  2122. return -EFAULT;
  2123. }
  2124. c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
  2125. if (IS_ERR(c))
  2126. return PTR_ERR(c);
  2127. args->nfs_server.hostname = c;
  2128. c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
  2129. if (IS_ERR(c))
  2130. return PTR_ERR(c);
  2131. args->nfs_server.export_path = c;
  2132. dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
  2133. c = strndup_user(data->client_addr.data, 16);
  2134. if (IS_ERR(c))
  2135. return PTR_ERR(c);
  2136. args->client_address = c;
  2137. /*
  2138. * Translate to nfs_parsed_mount_data, which nfs4_fill_super
  2139. * can deal with.
  2140. */
  2141. args->flags = data->flags & NFS4_MOUNT_FLAGMASK;
  2142. args->rsize = data->rsize;
  2143. args->wsize = data->wsize;
  2144. args->timeo = data->timeo;
  2145. args->retrans = data->retrans;
  2146. args->acregmin = data->acregmin;
  2147. args->acregmax = data->acregmax;
  2148. args->acdirmin = data->acdirmin;
  2149. args->acdirmax = data->acdirmax;
  2150. args->nfs_server.protocol = data->proto;
  2151. nfs_validate_transport_protocol(args);
  2152. break;
  2153. default:
  2154. if (nfs_parse_mount_options((char *)options, args) == 0)
  2155. return -EINVAL;
  2156. if (!nfs_verify_server_address(sap))
  2157. return -EINVAL;
  2158. return nfs4_validate_text_mount_data(options, args, dev_name);
  2159. }
  2160. return 0;
  2161. out_no_data:
  2162. dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
  2163. return -EINVAL;
  2164. out_inval_auth:
  2165. dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
  2166. data->auth_flavourlen);
  2167. return -EINVAL;
  2168. out_no_address:
  2169. dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
  2170. return -EINVAL;
  2171. }
  2172. /*
  2173. * Get the superblock for the NFS4 root partition
  2174. */
  2175. static int nfs4_remote_get_sb(struct file_system_type *fs_type,
  2176. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
  2177. {
  2178. struct nfs_parsed_mount_data *data = raw_data;
  2179. struct super_block *s;
  2180. struct nfs_server *server;
  2181. struct nfs_fh *mntfh;
  2182. struct dentry *mntroot;
  2183. int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
  2184. struct nfs_sb_mountdata sb_mntdata = {
  2185. .mntflags = flags,
  2186. };
  2187. int error = -ENOMEM;
  2188. mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
  2189. if (data == NULL || mntfh == NULL)
  2190. goto out_free_fh;
  2191. security_init_mnt_opts(&data->lsm_opts);
  2192. /* Get a volume representation */
  2193. server = nfs4_create_server(data, mntfh);
  2194. if (IS_ERR(server)) {
  2195. error = PTR_ERR(server);
  2196. goto out;
  2197. }
  2198. sb_mntdata.server = server;
  2199. if (server->flags & NFS4_MOUNT_UNSHARED)
  2200. compare_super = NULL;
  2201. /* Get a superblock - note that we may end up sharing one that already exists */
  2202. s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
  2203. if (IS_ERR(s)) {
  2204. error = PTR_ERR(s);
  2205. goto out_free;
  2206. }
  2207. if (s->s_fs_info != server) {
  2208. nfs_free_server(server);
  2209. server = NULL;
  2210. } else {
  2211. error = nfs_bdi_register(server);
  2212. if (error)
  2213. goto error_splat_super;
  2214. }
  2215. if (!s->s_root) {
  2216. /* initial superblock/root creation */
  2217. nfs4_fill_super(s);
  2218. nfs_fscache_get_super_cookie(
  2219. s, data ? data->fscache_uniq : NULL, NULL);
  2220. }
  2221. mntroot = nfs4_get_root(s, mntfh);
  2222. if (IS_ERR(mntroot)) {
  2223. error = PTR_ERR(mntroot);
  2224. goto error_splat_super;
  2225. }
  2226. error = security_sb_set_mnt_opts(s, &data->lsm_opts);
  2227. if (error)
  2228. goto error_splat_root;
  2229. s->s_flags |= MS_ACTIVE;
  2230. mnt->mnt_sb = s;
  2231. mnt->mnt_root = mntroot;
  2232. error = 0;
  2233. out:
  2234. security_free_mnt_opts(&data->lsm_opts);
  2235. out_free_fh:
  2236. kfree(mntfh);
  2237. return error;
  2238. out_free:
  2239. nfs_free_server(server);
  2240. goto out;
  2241. error_splat_root:
  2242. dput(mntroot);
  2243. error_splat_super:
  2244. deactivate_locked_super(s);
  2245. goto out;
  2246. }
  2247. static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type,
  2248. int flags, void *data, const char *hostname)
  2249. {
  2250. struct vfsmount *root_mnt;
  2251. char *root_devname;
  2252. size_t len;
  2253. len = strlen(hostname) + 3;
  2254. root_devname = kmalloc(len, GFP_KERNEL);
  2255. if (root_devname == NULL)
  2256. return ERR_PTR(-ENOMEM);
  2257. snprintf(root_devname, len, "%s:/", hostname);
  2258. root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data);
  2259. kfree(root_devname);
  2260. return root_mnt;
  2261. }
  2262. static void nfs_fix_devname(const struct path *path, struct vfsmount *mnt)
  2263. {
  2264. char *page = (char *) __get_free_page(GFP_KERNEL);
  2265. char *devname, *tmp;
  2266. if (page == NULL)
  2267. return;
  2268. devname = nfs_path(path->mnt->mnt_devname,
  2269. path->mnt->mnt_root, path->dentry,
  2270. page, PAGE_SIZE);
  2271. if (devname == NULL)
  2272. goto out_freepage;
  2273. tmp = kstrdup(devname, GFP_KERNEL);
  2274. if (tmp == NULL)
  2275. goto out_freepage;
  2276. kfree(mnt->mnt_devname);
  2277. mnt->mnt_devname = tmp;
  2278. out_freepage:
  2279. free_page((unsigned long)page);
  2280. }
  2281. static int nfs_follow_remote_path(struct vfsmount *root_mnt,
  2282. const char *export_path, struct vfsmount *mnt_target)
  2283. {
  2284. struct mnt_namespace *ns_private;
  2285. struct nameidata nd;
  2286. struct super_block *s;
  2287. int ret;
  2288. ns_private = create_mnt_ns(root_mnt);
  2289. ret = PTR_ERR(ns_private);
  2290. if (IS_ERR(ns_private))
  2291. goto out_mntput;
  2292. ret = vfs_path_lookup(root_mnt->mnt_root, root_mnt,
  2293. export_path, LOOKUP_FOLLOW, &nd);
  2294. put_mnt_ns(ns_private);
  2295. if (ret != 0)
  2296. goto out_err;
  2297. s = nd.path.mnt->mnt_sb;
  2298. atomic_inc(&s->s_active);
  2299. mnt_target->mnt_sb = s;
  2300. mnt_target->mnt_root = dget(nd.path.dentry);
  2301. /* Correct the device pathname */
  2302. nfs_fix_devname(&nd.path, mnt_target);
  2303. path_put(&nd.path);
  2304. down_write(&s->s_umount);
  2305. return 0;
  2306. out_mntput:
  2307. mntput(root_mnt);
  2308. out_err:
  2309. return ret;
  2310. }
  2311. static int nfs4_try_mount(int flags, const char *dev_name,
  2312. struct nfs_parsed_mount_data *data,
  2313. struct vfsmount *mnt)
  2314. {
  2315. char *export_path;
  2316. struct vfsmount *root_mnt;
  2317. int error;
  2318. dfprintk(MOUNT, "--> nfs4_try_mount()\n");
  2319. export_path = data->nfs_server.export_path;
  2320. data->nfs_server.export_path = "/";
  2321. root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, data,
  2322. data->nfs_server.hostname);
  2323. data->nfs_server.export_path = export_path;
  2324. error = PTR_ERR(root_mnt);
  2325. if (IS_ERR(root_mnt))
  2326. goto out;
  2327. error = nfs_follow_remote_path(root_mnt, export_path, mnt);
  2328. out:
  2329. dfprintk(MOUNT, "<-- nfs4_try_mount() = %d%s\n", error,
  2330. error != 0 ? " [error]" : "");
  2331. return error;
  2332. }
  2333. /*
  2334. * Get the superblock for an NFS4 mountpoint
  2335. */
  2336. static int nfs4_get_sb(struct file_system_type *fs_type,
  2337. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
  2338. {
  2339. struct nfs_parsed_mount_data *data;
  2340. int error = -ENOMEM;
  2341. data = nfs_alloc_parsed_mount_data(0);
  2342. if (data == NULL)
  2343. goto out_free_data;
  2344. /* Validate the mount data */
  2345. error = nfs4_validate_mount_data(raw_data, data, dev_name);
  2346. if (error < 0)
  2347. goto out;
  2348. error = nfs4_try_mount(flags, dev_name, data, mnt);
  2349. out:
  2350. kfree(data->client_address);
  2351. kfree(data->nfs_server.export_path);
  2352. kfree(data->nfs_server.hostname);
  2353. kfree(data->fscache_uniq);
  2354. out_free_data:
  2355. kfree(data);
  2356. dprintk("<-- nfs4_get_sb() = %d%s\n", error,
  2357. error != 0 ? " [error]" : "");
  2358. return error;
  2359. }
  2360. static void nfs4_kill_super(struct super_block *sb)
  2361. {
  2362. struct nfs_server *server = NFS_SB(sb);
  2363. dprintk("--> %s\n", __func__);
  2364. nfs_super_return_all_delegations(sb);
  2365. kill_anon_super(sb);
  2366. nfs4_renewd_prepare_shutdown(server);
  2367. nfs_fscache_release_super_cookie(sb);
  2368. nfs_free_server(server);
  2369. dprintk("<-- %s\n", __func__);
  2370. }
  2371. /*
  2372. * Clone an NFS4 server record on xdev traversal (FSID-change)
  2373. */
  2374. static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
  2375. const char *dev_name, void *raw_data,
  2376. struct vfsmount *mnt)
  2377. {
  2378. struct nfs_clone_mount *data = raw_data;
  2379. struct super_block *s;
  2380. struct nfs_server *server;
  2381. struct dentry *mntroot;
  2382. int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
  2383. struct nfs_sb_mountdata sb_mntdata = {
  2384. .mntflags = flags,
  2385. };
  2386. int error;
  2387. dprintk("--> nfs4_xdev_get_sb()\n");
  2388. /* create a new volume representation */
  2389. server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
  2390. if (IS_ERR(server)) {
  2391. error = PTR_ERR(server);
  2392. goto out_err_noserver;
  2393. }
  2394. sb_mntdata.server = server;
  2395. if (server->flags & NFS4_MOUNT_UNSHARED)
  2396. compare_super = NULL;
  2397. /* Get a superblock - note that we may end up sharing one that already exists */
  2398. s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
  2399. if (IS_ERR(s)) {
  2400. error = PTR_ERR(s);
  2401. goto out_err_nosb;
  2402. }
  2403. if (s->s_fs_info != server) {
  2404. nfs_free_server(server);
  2405. server = NULL;
  2406. } else {
  2407. error = nfs_bdi_register(server);
  2408. if (error)
  2409. goto error_splat_super;
  2410. }
  2411. if (!s->s_root) {
  2412. /* initial superblock/root creation */
  2413. nfs4_clone_super(s, data->sb);
  2414. nfs_fscache_get_super_cookie(s, NULL, data);
  2415. }
  2416. mntroot = nfs4_get_root(s, data->fh);
  2417. if (IS_ERR(mntroot)) {
  2418. error = PTR_ERR(mntroot);
  2419. goto error_splat_super;
  2420. }
  2421. if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
  2422. dput(mntroot);
  2423. error = -ESTALE;
  2424. goto error_splat_super;
  2425. }
  2426. s->s_flags |= MS_ACTIVE;
  2427. mnt->mnt_sb = s;
  2428. mnt->mnt_root = mntroot;
  2429. security_sb_clone_mnt_opts(data->sb, s);
  2430. dprintk("<-- nfs4_xdev_get_sb() = 0\n");
  2431. return 0;
  2432. out_err_nosb:
  2433. nfs_free_server(server);
  2434. out_err_noserver:
  2435. dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
  2436. return error;
  2437. error_splat_super:
  2438. deactivate_locked_super(s);
  2439. dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
  2440. return error;
  2441. }
  2442. static int nfs4_remote_referral_get_sb(struct file_system_type *fs_type,
  2443. int flags, const char *dev_name, void *raw_data,
  2444. struct vfsmount *mnt)
  2445. {
  2446. struct nfs_clone_mount *data = raw_data;
  2447. struct super_block *s;
  2448. struct nfs_server *server;
  2449. struct dentry *mntroot;
  2450. struct nfs_fh mntfh;
  2451. int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
  2452. struct nfs_sb_mountdata sb_mntdata = {
  2453. .mntflags = flags,
  2454. };
  2455. int error;
  2456. dprintk("--> nfs4_referral_get_sb()\n");
  2457. /* create a new volume representation */
  2458. server = nfs4_create_referral_server(data, &mntfh);
  2459. if (IS_ERR(server)) {
  2460. error = PTR_ERR(server);
  2461. goto out_err_noserver;
  2462. }
  2463. sb_mntdata.server = server;
  2464. if (server->flags & NFS4_MOUNT_UNSHARED)
  2465. compare_super = NULL;
  2466. /* Get a superblock - note that we may end up sharing one that already exists */
  2467. s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
  2468. if (IS_ERR(s)) {
  2469. error = PTR_ERR(s);
  2470. goto out_err_nosb;
  2471. }
  2472. if (s->s_fs_info != server) {
  2473. nfs_free_server(server);
  2474. server = NULL;
  2475. } else {
  2476. error = nfs_bdi_register(server);
  2477. if (error)
  2478. goto error_splat_super;
  2479. }
  2480. if (!s->s_root) {
  2481. /* initial superblock/root creation */
  2482. nfs4_fill_super(s);
  2483. nfs_fscache_get_super_cookie(s, NULL, data);
  2484. }
  2485. mntroot = nfs4_get_root(s, &mntfh);
  2486. if (IS_ERR(mntroot)) {
  2487. error = PTR_ERR(mntroot);
  2488. goto error_splat_super;
  2489. }
  2490. if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
  2491. dput(mntroot);
  2492. error = -ESTALE;
  2493. goto error_splat_super;
  2494. }
  2495. s->s_flags |= MS_ACTIVE;
  2496. mnt->mnt_sb = s;
  2497. mnt->mnt_root = mntroot;
  2498. security_sb_clone_mnt_opts(data->sb, s);
  2499. dprintk("<-- nfs4_referral_get_sb() = 0\n");
  2500. return 0;
  2501. out_err_nosb:
  2502. nfs_free_server(server);
  2503. out_err_noserver:
  2504. dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
  2505. return error;
  2506. error_splat_super:
  2507. deactivate_locked_super(s);
  2508. dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
  2509. return error;
  2510. }
  2511. /*
  2512. * Create an NFS4 server record on referral traversal
  2513. */
  2514. static int nfs4_referral_get_sb(struct file_system_type *fs_type,
  2515. int flags, const char *dev_name, void *raw_data,
  2516. struct vfsmount *mnt)
  2517. {
  2518. struct nfs_clone_mount *data = raw_data;
  2519. char *export_path;
  2520. struct vfsmount *root_mnt;
  2521. int error;
  2522. dprintk("--> nfs4_referral_get_sb()\n");
  2523. export_path = data->mnt_path;
  2524. data->mnt_path = "/";
  2525. root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
  2526. flags, data, data->hostname);
  2527. data->mnt_path = export_path;
  2528. error = PTR_ERR(root_mnt);
  2529. if (IS_ERR(root_mnt))
  2530. goto out;
  2531. error = nfs_follow_remote_path(root_mnt, export_path, mnt);
  2532. out:
  2533. dprintk("<-- nfs4_referral_get_sb() = %d%s\n", error,
  2534. error != 0 ? " [error]" : "");
  2535. return error;
  2536. }
  2537. #endif /* CONFIG_NFS_V4 */