super.c 71 KB

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