super.c 70 KB

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