super.c 71 KB

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