super.c 67 KB

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