super.c 65 KB

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