super.c 69 KB

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