super.c 64 KB

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