super.c 68 KB

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