super.c 69 KB

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