super.c 73 KB

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