super.c 71 KB

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