super.c 68 KB

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