super.c 73 KB

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