super.c 70 KB

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