super.c 72 KB

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