super.c 72 KB

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