super.c 69 KB

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