super.c 74 KB

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