super.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895
  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. static struct nfs_parsed_mount_data *nfs_alloc_parsed_mount_data(int flags)
  636. {
  637. struct nfs_parsed_mount_data *data;
  638. data = kzalloc(sizeof(*data), GFP_KERNEL);
  639. if (data) {
  640. data->flags = flags;
  641. data->rsize = NFS_MAX_FILE_IO_SIZE;
  642. data->wsize = NFS_MAX_FILE_IO_SIZE;
  643. data->acregmin = NFS_DEF_ACREGMIN;
  644. data->acregmax = NFS_DEF_ACREGMAX;
  645. data->acdirmin = NFS_DEF_ACDIRMIN;
  646. data->acdirmax = NFS_DEF_ACDIRMAX;
  647. data->nfs_server.port = NFS_UNSPEC_PORT;
  648. data->auth_flavors[0] = RPC_AUTH_UNIX;
  649. data->auth_flavor_len = 1;
  650. data->minorversion = 0;
  651. }
  652. return data;
  653. }
  654. /*
  655. * Sanity-check a server address provided by the mount command.
  656. *
  657. * Address family must be initialized, and address must not be
  658. * the ANY address for that family.
  659. */
  660. static int nfs_verify_server_address(struct sockaddr *addr)
  661. {
  662. switch (addr->sa_family) {
  663. case AF_INET: {
  664. struct sockaddr_in *sa = (struct sockaddr_in *)addr;
  665. return sa->sin_addr.s_addr != htonl(INADDR_ANY);
  666. }
  667. case AF_INET6: {
  668. struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
  669. return !ipv6_addr_any(sa);
  670. }
  671. }
  672. dfprintk(MOUNT, "NFS: Invalid IP address specified\n");
  673. return 0;
  674. }
  675. /*
  676. * Select between a default port value and a user-specified port value.
  677. * If a zero value is set, then autobind will be used.
  678. */
  679. static void nfs_set_default_port(struct sockaddr *sap, const int parsed_port,
  680. const unsigned short default_port)
  681. {
  682. unsigned short port = default_port;
  683. if (parsed_port != NFS_UNSPEC_PORT)
  684. port = parsed_port;
  685. rpc_set_port(sap, port);
  686. }
  687. /*
  688. * Sanity check the NFS transport protocol.
  689. *
  690. */
  691. static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
  692. {
  693. switch (mnt->nfs_server.protocol) {
  694. case XPRT_TRANSPORT_UDP:
  695. case XPRT_TRANSPORT_TCP:
  696. case XPRT_TRANSPORT_RDMA:
  697. break;
  698. default:
  699. mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
  700. }
  701. }
  702. /*
  703. * For text based NFSv2/v3 mounts, the mount protocol transport default
  704. * settings should depend upon the specified NFS transport.
  705. */
  706. static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
  707. {
  708. nfs_validate_transport_protocol(mnt);
  709. if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
  710. mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
  711. return;
  712. switch (mnt->nfs_server.protocol) {
  713. case XPRT_TRANSPORT_UDP:
  714. mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
  715. break;
  716. case XPRT_TRANSPORT_TCP:
  717. case XPRT_TRANSPORT_RDMA:
  718. mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
  719. }
  720. }
  721. /*
  722. * Parse the value of the 'sec=' option.
  723. */
  724. static int nfs_parse_security_flavors(char *value,
  725. struct nfs_parsed_mount_data *mnt)
  726. {
  727. substring_t args[MAX_OPT_ARGS];
  728. dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
  729. switch (match_token(value, nfs_secflavor_tokens, args)) {
  730. case Opt_sec_none:
  731. mnt->auth_flavors[0] = RPC_AUTH_NULL;
  732. break;
  733. case Opt_sec_sys:
  734. mnt->auth_flavors[0] = RPC_AUTH_UNIX;
  735. break;
  736. case Opt_sec_krb5:
  737. mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
  738. break;
  739. case Opt_sec_krb5i:
  740. mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
  741. break;
  742. case Opt_sec_krb5p:
  743. mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
  744. break;
  745. case Opt_sec_lkey:
  746. mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
  747. break;
  748. case Opt_sec_lkeyi:
  749. mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
  750. break;
  751. case Opt_sec_lkeyp:
  752. mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
  753. break;
  754. case Opt_sec_spkm:
  755. mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
  756. break;
  757. case Opt_sec_spkmi:
  758. mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
  759. break;
  760. case Opt_sec_spkmp:
  761. mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
  762. break;
  763. default:
  764. return 0;
  765. }
  766. mnt->auth_flavor_len = 1;
  767. return 1;
  768. }
  769. /*
  770. * Error-check and convert a string of mount options from user space into
  771. * a data structure. The whole mount string is processed; bad options are
  772. * skipped as they are encountered. If there were no errors, return 1;
  773. * otherwise return 0 (zero).
  774. */
  775. static int nfs_parse_mount_options(char *raw,
  776. struct nfs_parsed_mount_data *mnt)
  777. {
  778. char *p, *string, *secdata;
  779. int rc, sloppy = 0, invalid_option = 0;
  780. if (!raw) {
  781. dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
  782. return 1;
  783. }
  784. dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
  785. secdata = alloc_secdata();
  786. if (!secdata)
  787. goto out_nomem;
  788. rc = security_sb_copy_data(raw, secdata);
  789. if (rc)
  790. goto out_security_failure;
  791. rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
  792. if (rc)
  793. goto out_security_failure;
  794. free_secdata(secdata);
  795. while ((p = strsep(&raw, ",")) != NULL) {
  796. substring_t args[MAX_OPT_ARGS];
  797. unsigned long option;
  798. int token;
  799. if (!*p)
  800. continue;
  801. dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", p);
  802. token = match_token(p, nfs_mount_option_tokens, args);
  803. switch (token) {
  804. /*
  805. * boolean options: foo/nofoo
  806. */
  807. case Opt_soft:
  808. mnt->flags |= NFS_MOUNT_SOFT;
  809. break;
  810. case Opt_hard:
  811. mnt->flags &= ~NFS_MOUNT_SOFT;
  812. break;
  813. case Opt_posix:
  814. mnt->flags |= NFS_MOUNT_POSIX;
  815. break;
  816. case Opt_noposix:
  817. mnt->flags &= ~NFS_MOUNT_POSIX;
  818. break;
  819. case Opt_cto:
  820. mnt->flags &= ~NFS_MOUNT_NOCTO;
  821. break;
  822. case Opt_nocto:
  823. mnt->flags |= NFS_MOUNT_NOCTO;
  824. break;
  825. case Opt_ac:
  826. mnt->flags &= ~NFS_MOUNT_NOAC;
  827. break;
  828. case Opt_noac:
  829. mnt->flags |= NFS_MOUNT_NOAC;
  830. break;
  831. case Opt_lock:
  832. mnt->flags &= ~NFS_MOUNT_NONLM;
  833. break;
  834. case Opt_nolock:
  835. mnt->flags |= NFS_MOUNT_NONLM;
  836. break;
  837. case Opt_v2:
  838. mnt->flags &= ~NFS_MOUNT_VER3;
  839. mnt->version = 2;
  840. break;
  841. case Opt_v3:
  842. mnt->flags |= NFS_MOUNT_VER3;
  843. mnt->version = 3;
  844. break;
  845. #ifdef CONFIG_NFS_V4
  846. case Opt_v4:
  847. mnt->flags &= ~NFS_MOUNT_VER3;
  848. mnt->version = 4;
  849. break;
  850. #endif
  851. case Opt_udp:
  852. mnt->flags &= ~NFS_MOUNT_TCP;
  853. mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
  854. break;
  855. case Opt_tcp:
  856. mnt->flags |= NFS_MOUNT_TCP;
  857. mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
  858. break;
  859. case Opt_rdma:
  860. mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
  861. mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
  862. xprt_load_transport(p);
  863. break;
  864. case Opt_acl:
  865. mnt->flags &= ~NFS_MOUNT_NOACL;
  866. break;
  867. case Opt_noacl:
  868. mnt->flags |= NFS_MOUNT_NOACL;
  869. break;
  870. case Opt_rdirplus:
  871. mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
  872. break;
  873. case Opt_nordirplus:
  874. mnt->flags |= NFS_MOUNT_NORDIRPLUS;
  875. break;
  876. case Opt_sharecache:
  877. mnt->flags &= ~NFS_MOUNT_UNSHARED;
  878. break;
  879. case Opt_nosharecache:
  880. mnt->flags |= NFS_MOUNT_UNSHARED;
  881. break;
  882. case Opt_resvport:
  883. mnt->flags &= ~NFS_MOUNT_NORESVPORT;
  884. break;
  885. case Opt_noresvport:
  886. mnt->flags |= NFS_MOUNT_NORESVPORT;
  887. break;
  888. case Opt_fscache:
  889. mnt->options |= NFS_OPTION_FSCACHE;
  890. kfree(mnt->fscache_uniq);
  891. mnt->fscache_uniq = NULL;
  892. break;
  893. case Opt_nofscache:
  894. mnt->options &= ~NFS_OPTION_FSCACHE;
  895. kfree(mnt->fscache_uniq);
  896. mnt->fscache_uniq = NULL;
  897. break;
  898. case Opt_fscache_uniq:
  899. string = match_strdup(args);
  900. if (!string)
  901. goto out_nomem;
  902. kfree(mnt->fscache_uniq);
  903. mnt->fscache_uniq = string;
  904. mnt->options |= NFS_OPTION_FSCACHE;
  905. break;
  906. /*
  907. * options that take numeric values
  908. */
  909. case Opt_port:
  910. string = match_strdup(args);
  911. if (string == NULL)
  912. goto out_nomem;
  913. rc = strict_strtoul(string, 10, &option);
  914. kfree(string);
  915. if (rc != 0 || option > USHORT_MAX)
  916. goto out_invalid_value;
  917. mnt->nfs_server.port = option;
  918. break;
  919. case Opt_rsize:
  920. string = match_strdup(args);
  921. if (string == NULL)
  922. goto out_nomem;
  923. rc = strict_strtoul(string, 10, &option);
  924. kfree(string);
  925. if (rc != 0)
  926. goto out_invalid_value;
  927. mnt->rsize = option;
  928. break;
  929. case Opt_wsize:
  930. string = match_strdup(args);
  931. if (string == NULL)
  932. goto out_nomem;
  933. rc = strict_strtoul(string, 10, &option);
  934. kfree(string);
  935. if (rc != 0)
  936. goto out_invalid_value;
  937. mnt->wsize = option;
  938. break;
  939. case Opt_bsize:
  940. string = match_strdup(args);
  941. if (string == NULL)
  942. goto out_nomem;
  943. rc = strict_strtoul(string, 10, &option);
  944. kfree(string);
  945. if (rc != 0)
  946. goto out_invalid_value;
  947. mnt->bsize = option;
  948. break;
  949. case Opt_timeo:
  950. string = match_strdup(args);
  951. if (string == NULL)
  952. goto out_nomem;
  953. rc = strict_strtoul(string, 10, &option);
  954. kfree(string);
  955. if (rc != 0 || option == 0)
  956. goto out_invalid_value;
  957. mnt->timeo = option;
  958. break;
  959. case Opt_retrans:
  960. string = match_strdup(args);
  961. if (string == NULL)
  962. goto out_nomem;
  963. rc = strict_strtoul(string, 10, &option);
  964. kfree(string);
  965. if (rc != 0 || option == 0)
  966. goto out_invalid_value;
  967. mnt->retrans = option;
  968. break;
  969. case Opt_acregmin:
  970. string = match_strdup(args);
  971. if (string == NULL)
  972. goto out_nomem;
  973. rc = strict_strtoul(string, 10, &option);
  974. kfree(string);
  975. if (rc != 0)
  976. goto out_invalid_value;
  977. mnt->acregmin = option;
  978. break;
  979. case Opt_acregmax:
  980. string = match_strdup(args);
  981. if (string == NULL)
  982. goto out_nomem;
  983. rc = strict_strtoul(string, 10, &option);
  984. kfree(string);
  985. if (rc != 0)
  986. goto out_invalid_value;
  987. mnt->acregmax = option;
  988. break;
  989. case Opt_acdirmin:
  990. string = match_strdup(args);
  991. if (string == NULL)
  992. goto out_nomem;
  993. rc = strict_strtoul(string, 10, &option);
  994. kfree(string);
  995. if (rc != 0)
  996. goto out_invalid_value;
  997. mnt->acdirmin = option;
  998. break;
  999. case Opt_acdirmax:
  1000. string = match_strdup(args);
  1001. if (string == NULL)
  1002. goto out_nomem;
  1003. rc = strict_strtoul(string, 10, &option);
  1004. kfree(string);
  1005. if (rc != 0)
  1006. goto out_invalid_value;
  1007. mnt->acdirmax = option;
  1008. break;
  1009. case Opt_actimeo:
  1010. string = match_strdup(args);
  1011. if (string == NULL)
  1012. goto out_nomem;
  1013. rc = strict_strtoul(string, 10, &option);
  1014. kfree(string);
  1015. if (rc != 0)
  1016. goto out_invalid_value;
  1017. mnt->acregmin = mnt->acregmax =
  1018. mnt->acdirmin = mnt->acdirmax = option;
  1019. break;
  1020. case Opt_namelen:
  1021. string = match_strdup(args);
  1022. if (string == NULL)
  1023. goto out_nomem;
  1024. rc = strict_strtoul(string, 10, &option);
  1025. kfree(string);
  1026. if (rc != 0)
  1027. goto out_invalid_value;
  1028. mnt->namlen = option;
  1029. break;
  1030. case Opt_mountport:
  1031. string = match_strdup(args);
  1032. if (string == NULL)
  1033. goto out_nomem;
  1034. rc = strict_strtoul(string, 10, &option);
  1035. kfree(string);
  1036. if (rc != 0 || option > USHORT_MAX)
  1037. goto out_invalid_value;
  1038. mnt->mount_server.port = option;
  1039. break;
  1040. case Opt_mountvers:
  1041. string = match_strdup(args);
  1042. if (string == NULL)
  1043. goto out_nomem;
  1044. rc = strict_strtoul(string, 10, &option);
  1045. kfree(string);
  1046. if (rc != 0 ||
  1047. option < NFS_MNT_VERSION ||
  1048. option > NFS_MNT3_VERSION)
  1049. goto out_invalid_value;
  1050. mnt->mount_server.version = option;
  1051. break;
  1052. case Opt_nfsvers:
  1053. string = match_strdup(args);
  1054. if (string == NULL)
  1055. goto out_nomem;
  1056. rc = strict_strtoul(string, 10, &option);
  1057. kfree(string);
  1058. if (rc != 0)
  1059. goto out_invalid_value;
  1060. switch (option) {
  1061. case NFS2_VERSION:
  1062. mnt->flags &= ~NFS_MOUNT_VER3;
  1063. mnt->version = 2;
  1064. break;
  1065. case NFS3_VERSION:
  1066. mnt->flags |= NFS_MOUNT_VER3;
  1067. mnt->version = 3;
  1068. break;
  1069. #ifdef CONFIG_NFS_V4
  1070. case NFS4_VERSION:
  1071. mnt->flags &= ~NFS_MOUNT_VER3;
  1072. mnt->version = 4;
  1073. break;
  1074. #endif
  1075. default:
  1076. goto out_invalid_value;
  1077. }
  1078. break;
  1079. case Opt_minorversion:
  1080. string = match_strdup(args);
  1081. if (string == NULL)
  1082. goto out_nomem;
  1083. rc = strict_strtoul(string, 10, &option);
  1084. kfree(string);
  1085. if (rc != 0)
  1086. goto out_invalid_value;
  1087. if (option > NFS4_MAX_MINOR_VERSION)
  1088. goto out_invalid_value;
  1089. mnt->minorversion = option;
  1090. break;
  1091. /*
  1092. * options that take text values
  1093. */
  1094. case Opt_sec:
  1095. string = match_strdup(args);
  1096. if (string == NULL)
  1097. goto out_nomem;
  1098. rc = nfs_parse_security_flavors(string, mnt);
  1099. kfree(string);
  1100. if (!rc) {
  1101. dfprintk(MOUNT, "NFS: unrecognized "
  1102. "security flavor\n");
  1103. return 0;
  1104. }
  1105. break;
  1106. case Opt_proto:
  1107. string = match_strdup(args);
  1108. if (string == NULL)
  1109. goto out_nomem;
  1110. token = match_token(string,
  1111. nfs_xprt_protocol_tokens, args);
  1112. switch (token) {
  1113. case Opt_xprt_udp:
  1114. mnt->flags &= ~NFS_MOUNT_TCP;
  1115. mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
  1116. kfree(string);
  1117. break;
  1118. case Opt_xprt_tcp:
  1119. mnt->flags |= NFS_MOUNT_TCP;
  1120. mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
  1121. kfree(string);
  1122. break;
  1123. case Opt_xprt_rdma:
  1124. /* vector side protocols to TCP */
  1125. mnt->flags |= NFS_MOUNT_TCP;
  1126. mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
  1127. xprt_load_transport(string);
  1128. kfree(string);
  1129. break;
  1130. default:
  1131. dfprintk(MOUNT, "NFS: unrecognized "
  1132. "transport protocol\n");
  1133. return 0;
  1134. }
  1135. break;
  1136. case Opt_mountproto:
  1137. string = match_strdup(args);
  1138. if (string == NULL)
  1139. goto out_nomem;
  1140. token = match_token(string,
  1141. nfs_xprt_protocol_tokens, args);
  1142. kfree(string);
  1143. switch (token) {
  1144. case Opt_xprt_udp:
  1145. mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
  1146. break;
  1147. case Opt_xprt_tcp:
  1148. mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
  1149. break;
  1150. case Opt_xprt_rdma: /* not used for side protocols */
  1151. default:
  1152. dfprintk(MOUNT, "NFS: unrecognized "
  1153. "transport protocol\n");
  1154. return 0;
  1155. }
  1156. break;
  1157. case Opt_addr:
  1158. string = match_strdup(args);
  1159. if (string == NULL)
  1160. goto out_nomem;
  1161. mnt->nfs_server.addrlen =
  1162. rpc_pton(string, strlen(string),
  1163. (struct sockaddr *)
  1164. &mnt->nfs_server.address,
  1165. sizeof(mnt->nfs_server.address));
  1166. kfree(string);
  1167. if (mnt->nfs_server.addrlen == 0)
  1168. goto out_invalid_address;
  1169. break;
  1170. case Opt_clientaddr:
  1171. string = match_strdup(args);
  1172. if (string == NULL)
  1173. goto out_nomem;
  1174. kfree(mnt->client_address);
  1175. mnt->client_address = string;
  1176. break;
  1177. case Opt_mounthost:
  1178. string = match_strdup(args);
  1179. if (string == NULL)
  1180. goto out_nomem;
  1181. kfree(mnt->mount_server.hostname);
  1182. mnt->mount_server.hostname = string;
  1183. break;
  1184. case Opt_mountaddr:
  1185. string = match_strdup(args);
  1186. if (string == NULL)
  1187. goto out_nomem;
  1188. mnt->mount_server.addrlen =
  1189. rpc_pton(string, strlen(string),
  1190. (struct sockaddr *)
  1191. &mnt->mount_server.address,
  1192. sizeof(mnt->mount_server.address));
  1193. kfree(string);
  1194. if (mnt->mount_server.addrlen == 0)
  1195. goto out_invalid_address;
  1196. break;
  1197. case Opt_lookupcache:
  1198. string = match_strdup(args);
  1199. if (string == NULL)
  1200. goto out_nomem;
  1201. token = match_token(string,
  1202. nfs_lookupcache_tokens, args);
  1203. kfree(string);
  1204. switch (token) {
  1205. case Opt_lookupcache_all:
  1206. mnt->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
  1207. break;
  1208. case Opt_lookupcache_positive:
  1209. mnt->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
  1210. mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
  1211. break;
  1212. case Opt_lookupcache_none:
  1213. mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
  1214. break;
  1215. default:
  1216. dfprintk(MOUNT, "NFS: invalid "
  1217. "lookupcache argument\n");
  1218. return 0;
  1219. };
  1220. break;
  1221. /*
  1222. * Special options
  1223. */
  1224. case Opt_sloppy:
  1225. sloppy = 1;
  1226. dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
  1227. break;
  1228. case Opt_userspace:
  1229. case Opt_deprecated:
  1230. dfprintk(MOUNT, "NFS: ignoring mount option "
  1231. "'%s'\n", p);
  1232. break;
  1233. default:
  1234. invalid_option = 1;
  1235. dfprintk(MOUNT, "NFS: unrecognized mount option "
  1236. "'%s'\n", p);
  1237. }
  1238. }
  1239. if (!sloppy && invalid_option)
  1240. return 0;
  1241. return 1;
  1242. out_invalid_address:
  1243. printk(KERN_INFO "NFS: bad IP address specified: %s\n", p);
  1244. return 0;
  1245. out_invalid_value:
  1246. printk(KERN_INFO "NFS: bad mount option value specified: %s\n", p);
  1247. return 0;
  1248. out_nomem:
  1249. printk(KERN_INFO "NFS: not enough memory to parse option\n");
  1250. return 0;
  1251. out_security_failure:
  1252. free_secdata(secdata);
  1253. printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
  1254. return 0;
  1255. }
  1256. /*
  1257. * Match the requested auth flavors with the list returned by
  1258. * the server. Returns zero and sets the mount's authentication
  1259. * flavor on success; returns -EACCES if server does not support
  1260. * the requested flavor.
  1261. */
  1262. static int nfs_walk_authlist(struct nfs_parsed_mount_data *args,
  1263. struct nfs_mount_request *request)
  1264. {
  1265. unsigned int i, j, server_authlist_len = *(request->auth_flav_len);
  1266. /*
  1267. * Certain releases of Linux's mountd return an empty
  1268. * flavor list. To prevent behavioral regression with
  1269. * these servers (ie. rejecting mounts that used to
  1270. * succeed), revert to pre-2.6.32 behavior (no checking)
  1271. * if the returned flavor list is empty.
  1272. */
  1273. if (server_authlist_len == 0)
  1274. return 0;
  1275. /*
  1276. * We avoid sophisticated negotiating here, as there are
  1277. * plenty of cases where we can get it wrong, providing
  1278. * either too little or too much security.
  1279. *
  1280. * RFC 2623, section 2.7 suggests we SHOULD prefer the
  1281. * flavor listed first. However, some servers list
  1282. * AUTH_NULL first. Our caller plants AUTH_SYS, the
  1283. * preferred default, in args->auth_flavors[0] if user
  1284. * didn't specify sec= mount option.
  1285. */
  1286. for (i = 0; i < args->auth_flavor_len; i++)
  1287. for (j = 0; j < server_authlist_len; j++)
  1288. if (args->auth_flavors[i] == request->auth_flavs[j]) {
  1289. dfprintk(MOUNT, "NFS: using auth flavor %d\n",
  1290. request->auth_flavs[j]);
  1291. args->auth_flavors[0] = request->auth_flavs[j];
  1292. return 0;
  1293. }
  1294. dfprintk(MOUNT, "NFS: server does not support requested auth flavor\n");
  1295. nfs_umount(request);
  1296. return -EACCES;
  1297. }
  1298. /*
  1299. * Use the remote server's MOUNT service to request the NFS file handle
  1300. * corresponding to the provided path.
  1301. */
  1302. static int nfs_try_mount(struct nfs_parsed_mount_data *args,
  1303. struct nfs_fh *root_fh)
  1304. {
  1305. rpc_authflavor_t server_authlist[NFS_MAX_SECFLAVORS];
  1306. unsigned int server_authlist_len = ARRAY_SIZE(server_authlist);
  1307. struct nfs_mount_request request = {
  1308. .sap = (struct sockaddr *)
  1309. &args->mount_server.address,
  1310. .dirpath = args->nfs_server.export_path,
  1311. .protocol = args->mount_server.protocol,
  1312. .fh = root_fh,
  1313. .noresvport = args->flags & NFS_MOUNT_NORESVPORT,
  1314. .auth_flav_len = &server_authlist_len,
  1315. .auth_flavs = server_authlist,
  1316. };
  1317. int status;
  1318. if (args->mount_server.version == 0) {
  1319. switch (args->version) {
  1320. default:
  1321. args->mount_server.version = NFS_MNT3_VERSION;
  1322. break;
  1323. case 2:
  1324. args->mount_server.version = NFS_MNT_VERSION;
  1325. }
  1326. }
  1327. request.version = args->mount_server.version;
  1328. if (args->mount_server.hostname)
  1329. request.hostname = args->mount_server.hostname;
  1330. else
  1331. request.hostname = args->nfs_server.hostname;
  1332. /*
  1333. * Construct the mount server's address.
  1334. */
  1335. if (args->mount_server.address.ss_family == AF_UNSPEC) {
  1336. memcpy(request.sap, &args->nfs_server.address,
  1337. args->nfs_server.addrlen);
  1338. args->mount_server.addrlen = args->nfs_server.addrlen;
  1339. }
  1340. request.salen = args->mount_server.addrlen;
  1341. nfs_set_default_port(request.sap, args->mount_server.port, 0);
  1342. /*
  1343. * Now ask the mount server to map our export path
  1344. * to a file handle.
  1345. */
  1346. status = nfs_mount(&request);
  1347. if (status != 0) {
  1348. dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
  1349. request.hostname, status);
  1350. return status;
  1351. }
  1352. /*
  1353. * MNTv1 (NFSv2) does not support auth flavor negotiation.
  1354. */
  1355. if (args->mount_server.version != NFS_MNT3_VERSION)
  1356. return 0;
  1357. return nfs_walk_authlist(args, &request);
  1358. }
  1359. static int nfs_parse_simple_hostname(const char *dev_name,
  1360. char **hostname, size_t maxnamlen,
  1361. char **export_path, size_t maxpathlen)
  1362. {
  1363. size_t len;
  1364. char *colon, *comma;
  1365. colon = strchr(dev_name, ':');
  1366. if (colon == NULL)
  1367. goto out_bad_devname;
  1368. len = colon - dev_name;
  1369. if (len > maxnamlen)
  1370. goto out_hostname;
  1371. /* N.B. caller will free nfs_server.hostname in all cases */
  1372. *hostname = kstrndup(dev_name, len, GFP_KERNEL);
  1373. if (!*hostname)
  1374. goto out_nomem;
  1375. /* kill possible hostname list: not supported */
  1376. comma = strchr(*hostname, ',');
  1377. if (comma != NULL) {
  1378. if (comma == *hostname)
  1379. goto out_bad_devname;
  1380. *comma = '\0';
  1381. }
  1382. colon++;
  1383. len = strlen(colon);
  1384. if (len > maxpathlen)
  1385. goto out_path;
  1386. *export_path = kstrndup(colon, len, GFP_KERNEL);
  1387. if (!*export_path)
  1388. goto out_nomem;
  1389. dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
  1390. return 0;
  1391. out_bad_devname:
  1392. dfprintk(MOUNT, "NFS: device name not in host:path format\n");
  1393. return -EINVAL;
  1394. out_nomem:
  1395. dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
  1396. return -ENOMEM;
  1397. out_hostname:
  1398. dfprintk(MOUNT, "NFS: server hostname too long\n");
  1399. return -ENAMETOOLONG;
  1400. out_path:
  1401. dfprintk(MOUNT, "NFS: export pathname too long\n");
  1402. return -ENAMETOOLONG;
  1403. }
  1404. /*
  1405. * Hostname has square brackets around it because it contains one or
  1406. * more colons. We look for the first closing square bracket, and a
  1407. * colon must follow it.
  1408. */
  1409. static int nfs_parse_protected_hostname(const char *dev_name,
  1410. char **hostname, size_t maxnamlen,
  1411. char **export_path, size_t maxpathlen)
  1412. {
  1413. size_t len;
  1414. char *start, *end;
  1415. start = (char *)(dev_name + 1);
  1416. end = strchr(start, ']');
  1417. if (end == NULL)
  1418. goto out_bad_devname;
  1419. if (*(end + 1) != ':')
  1420. goto out_bad_devname;
  1421. len = end - start;
  1422. if (len > maxnamlen)
  1423. goto out_hostname;
  1424. /* N.B. caller will free nfs_server.hostname in all cases */
  1425. *hostname = kstrndup(start, len, GFP_KERNEL);
  1426. if (*hostname == NULL)
  1427. goto out_nomem;
  1428. end += 2;
  1429. len = strlen(end);
  1430. if (len > maxpathlen)
  1431. goto out_path;
  1432. *export_path = kstrndup(end, len, GFP_KERNEL);
  1433. if (!*export_path)
  1434. goto out_nomem;
  1435. return 0;
  1436. out_bad_devname:
  1437. dfprintk(MOUNT, "NFS: device name not in host:path format\n");
  1438. return -EINVAL;
  1439. out_nomem:
  1440. dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
  1441. return -ENOMEM;
  1442. out_hostname:
  1443. dfprintk(MOUNT, "NFS: server hostname too long\n");
  1444. return -ENAMETOOLONG;
  1445. out_path:
  1446. dfprintk(MOUNT, "NFS: export pathname too long\n");
  1447. return -ENAMETOOLONG;
  1448. }
  1449. /*
  1450. * Split "dev_name" into "hostname:export_path".
  1451. *
  1452. * The leftmost colon demarks the split between the server's hostname
  1453. * and the export path. If the hostname starts with a left square
  1454. * bracket, then it may contain colons.
  1455. *
  1456. * Note: caller frees hostname and export path, even on error.
  1457. */
  1458. static int nfs_parse_devname(const char *dev_name,
  1459. char **hostname, size_t maxnamlen,
  1460. char **export_path, size_t maxpathlen)
  1461. {
  1462. if (*dev_name == '[')
  1463. return nfs_parse_protected_hostname(dev_name,
  1464. hostname, maxnamlen,
  1465. export_path, maxpathlen);
  1466. return nfs_parse_simple_hostname(dev_name,
  1467. hostname, maxnamlen,
  1468. export_path, maxpathlen);
  1469. }
  1470. /*
  1471. * Validate the NFS2/NFS3 mount data
  1472. * - fills in the mount root filehandle
  1473. *
  1474. * For option strings, user space handles the following behaviors:
  1475. *
  1476. * + DNS: mapping server host name to IP address ("addr=" option)
  1477. *
  1478. * + failure mode: how to behave if a mount request can't be handled
  1479. * immediately ("fg/bg" option)
  1480. *
  1481. * + retry: how often to retry a mount request ("retry=" option)
  1482. *
  1483. * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
  1484. * mountproto=tcp after mountproto=udp, and so on
  1485. */
  1486. static int nfs_validate_mount_data(void *options,
  1487. struct nfs_parsed_mount_data *args,
  1488. struct nfs_fh *mntfh,
  1489. const char *dev_name)
  1490. {
  1491. struct nfs_mount_data *data = (struct nfs_mount_data *)options;
  1492. struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
  1493. if (data == NULL)
  1494. goto out_no_data;
  1495. switch (data->version) {
  1496. case 1:
  1497. data->namlen = 0;
  1498. case 2:
  1499. data->bsize = 0;
  1500. case 3:
  1501. if (data->flags & NFS_MOUNT_VER3)
  1502. goto out_no_v3;
  1503. data->root.size = NFS2_FHSIZE;
  1504. memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
  1505. case 4:
  1506. if (data->flags & NFS_MOUNT_SECFLAVOUR)
  1507. goto out_no_sec;
  1508. case 5:
  1509. memset(data->context, 0, sizeof(data->context));
  1510. case 6:
  1511. if (data->flags & NFS_MOUNT_VER3) {
  1512. if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
  1513. goto out_invalid_fh;
  1514. mntfh->size = data->root.size;
  1515. args->version = 3;
  1516. } else {
  1517. mntfh->size = NFS2_FHSIZE;
  1518. args->version = 2;
  1519. }
  1520. memcpy(mntfh->data, data->root.data, mntfh->size);
  1521. if (mntfh->size < sizeof(mntfh->data))
  1522. memset(mntfh->data + mntfh->size, 0,
  1523. sizeof(mntfh->data) - mntfh->size);
  1524. /*
  1525. * Translate to nfs_parsed_mount_data, which nfs_fill_super
  1526. * can deal with.
  1527. */
  1528. args->flags = data->flags & NFS_MOUNT_FLAGMASK;
  1529. args->rsize = data->rsize;
  1530. args->wsize = data->wsize;
  1531. args->timeo = data->timeo;
  1532. args->retrans = data->retrans;
  1533. args->acregmin = data->acregmin;
  1534. args->acregmax = data->acregmax;
  1535. args->acdirmin = data->acdirmin;
  1536. args->acdirmax = data->acdirmax;
  1537. memcpy(sap, &data->addr, sizeof(data->addr));
  1538. args->nfs_server.addrlen = sizeof(data->addr);
  1539. if (!nfs_verify_server_address(sap))
  1540. goto out_no_address;
  1541. if (!(data->flags & NFS_MOUNT_TCP))
  1542. args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
  1543. /* N.B. caller will free nfs_server.hostname in all cases */
  1544. args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
  1545. args->namlen = data->namlen;
  1546. args->bsize = data->bsize;
  1547. if (data->flags & NFS_MOUNT_SECFLAVOUR)
  1548. args->auth_flavors[0] = data->pseudoflavor;
  1549. if (!args->nfs_server.hostname)
  1550. goto out_nomem;
  1551. /*
  1552. * The legacy version 6 binary mount data from userspace has a
  1553. * field used only to transport selinux information into the
  1554. * the kernel. To continue to support that functionality we
  1555. * have a touch of selinux knowledge here in the NFS code. The
  1556. * userspace code converted context=blah to just blah so we are
  1557. * converting back to the full string selinux understands.
  1558. */
  1559. if (data->context[0]){
  1560. #ifdef CONFIG_SECURITY_SELINUX
  1561. int rc;
  1562. char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
  1563. if (!opts_str)
  1564. return -ENOMEM;
  1565. strcpy(opts_str, "context=");
  1566. data->context[NFS_MAX_CONTEXT_LEN] = '\0';
  1567. strcat(opts_str, &data->context[0]);
  1568. rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
  1569. kfree(opts_str);
  1570. if (rc)
  1571. return rc;
  1572. #else
  1573. return -EINVAL;
  1574. #endif
  1575. }
  1576. break;
  1577. default: {
  1578. int status;
  1579. if (nfs_parse_mount_options((char *)options, args) == 0)
  1580. return -EINVAL;
  1581. if (!nfs_verify_server_address(sap))
  1582. goto out_no_address;
  1583. if (args->version == 4)
  1584. #ifdef CONFIG_NFS_V4
  1585. return nfs4_validate_text_mount_data(options,
  1586. args, dev_name);
  1587. #else
  1588. goto out_v4_not_compiled;
  1589. #endif
  1590. nfs_set_default_port(sap, args->nfs_server.port, 0);
  1591. nfs_set_mount_transport_protocol(args);
  1592. status = nfs_parse_devname(dev_name,
  1593. &args->nfs_server.hostname,
  1594. PAGE_SIZE,
  1595. &args->nfs_server.export_path,
  1596. NFS_MAXPATHLEN);
  1597. if (!status)
  1598. status = nfs_try_mount(args, mntfh);
  1599. kfree(args->nfs_server.export_path);
  1600. args->nfs_server.export_path = NULL;
  1601. if (status)
  1602. return status;
  1603. break;
  1604. }
  1605. }
  1606. #ifndef CONFIG_NFS_V3
  1607. if (args->version == 3)
  1608. goto out_v3_not_compiled;
  1609. #endif /* !CONFIG_NFS_V3 */
  1610. return 0;
  1611. out_no_data:
  1612. dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
  1613. return -EINVAL;
  1614. out_no_v3:
  1615. dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
  1616. data->version);
  1617. return -EINVAL;
  1618. out_no_sec:
  1619. dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
  1620. return -EINVAL;
  1621. #ifndef CONFIG_NFS_V3
  1622. out_v3_not_compiled:
  1623. dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
  1624. return -EPROTONOSUPPORT;
  1625. #endif /* !CONFIG_NFS_V3 */
  1626. #ifndef CONFIG_NFS_V4
  1627. out_v4_not_compiled:
  1628. dfprintk(MOUNT, "NFS: NFSv4 is not compiled into kernel\n");
  1629. return -EPROTONOSUPPORT;
  1630. #endif /* !CONFIG_NFS_V4 */
  1631. out_nomem:
  1632. dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
  1633. return -ENOMEM;
  1634. out_no_address:
  1635. dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
  1636. return -EINVAL;
  1637. out_invalid_fh:
  1638. dfprintk(MOUNT, "NFS: invalid root filehandle\n");
  1639. return -EINVAL;
  1640. }
  1641. static int
  1642. nfs_compare_remount_data(struct nfs_server *nfss,
  1643. struct nfs_parsed_mount_data *data)
  1644. {
  1645. if (data->flags != nfss->flags ||
  1646. data->rsize != nfss->rsize ||
  1647. data->wsize != nfss->wsize ||
  1648. data->retrans != nfss->client->cl_timeout->to_retries ||
  1649. data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
  1650. data->acregmin != nfss->acregmin / HZ ||
  1651. data->acregmax != nfss->acregmax / HZ ||
  1652. data->acdirmin != nfss->acdirmin / HZ ||
  1653. data->acdirmax != nfss->acdirmax / HZ ||
  1654. data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
  1655. data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
  1656. memcmp(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
  1657. data->nfs_server.addrlen) != 0)
  1658. return -EINVAL;
  1659. return 0;
  1660. }
  1661. static int
  1662. nfs_remount(struct super_block *sb, int *flags, char *raw_data)
  1663. {
  1664. int error;
  1665. struct nfs_server *nfss = sb->s_fs_info;
  1666. struct nfs_parsed_mount_data *data;
  1667. struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
  1668. struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
  1669. u32 nfsvers = nfss->nfs_client->rpc_ops->version;
  1670. /*
  1671. * Userspace mount programs that send binary options generally send
  1672. * them populated with default values. We have no way to know which
  1673. * ones were explicitly specified. Fall back to legacy behavior and
  1674. * just return success.
  1675. */
  1676. if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
  1677. (nfsvers <= 3 && (!options || (options->version >= 1 &&
  1678. options->version <= 6))))
  1679. return 0;
  1680. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1681. if (data == NULL)
  1682. return -ENOMEM;
  1683. lock_kernel();
  1684. /* fill out struct with values from existing mount */
  1685. data->flags = nfss->flags;
  1686. data->rsize = nfss->rsize;
  1687. data->wsize = nfss->wsize;
  1688. data->retrans = nfss->client->cl_timeout->to_retries;
  1689. data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
  1690. data->acregmin = nfss->acregmin / HZ;
  1691. data->acregmax = nfss->acregmax / HZ;
  1692. data->acdirmin = nfss->acdirmin / HZ;
  1693. data->acdirmax = nfss->acdirmax / HZ;
  1694. data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
  1695. data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
  1696. memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
  1697. data->nfs_server.addrlen);
  1698. /* overwrite those values with any that were specified */
  1699. error = nfs_parse_mount_options((char *)options, data);
  1700. if (error < 0)
  1701. goto out;
  1702. /* compare new mount options with old ones */
  1703. error = nfs_compare_remount_data(nfss, data);
  1704. out:
  1705. kfree(data);
  1706. unlock_kernel();
  1707. return error;
  1708. }
  1709. /*
  1710. * Initialise the common bits of the superblock
  1711. */
  1712. static inline void nfs_initialise_sb(struct super_block *sb)
  1713. {
  1714. struct nfs_server *server = NFS_SB(sb);
  1715. sb->s_magic = NFS_SUPER_MAGIC;
  1716. /* We probably want something more informative here */
  1717. snprintf(sb->s_id, sizeof(sb->s_id),
  1718. "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
  1719. if (sb->s_blocksize == 0)
  1720. sb->s_blocksize = nfs_block_bits(server->wsize,
  1721. &sb->s_blocksize_bits);
  1722. if (server->flags & NFS_MOUNT_NOAC)
  1723. sb->s_flags |= MS_SYNCHRONOUS;
  1724. sb->s_bdi = &server->backing_dev_info;
  1725. nfs_super_set_maxbytes(sb, server->maxfilesize);
  1726. }
  1727. /*
  1728. * Finish setting up an NFS2/3 superblock
  1729. */
  1730. static void nfs_fill_super(struct super_block *sb,
  1731. struct nfs_parsed_mount_data *data)
  1732. {
  1733. struct nfs_server *server = NFS_SB(sb);
  1734. sb->s_blocksize_bits = 0;
  1735. sb->s_blocksize = 0;
  1736. if (data->bsize)
  1737. sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
  1738. if (server->nfs_client->rpc_ops->version == 3) {
  1739. /* The VFS shouldn't apply the umask to mode bits. We will do
  1740. * so ourselves when necessary.
  1741. */
  1742. sb->s_flags |= MS_POSIXACL;
  1743. sb->s_time_gran = 1;
  1744. }
  1745. sb->s_op = &nfs_sops;
  1746. nfs_initialise_sb(sb);
  1747. }
  1748. /*
  1749. * Finish setting up a cloned NFS2/3 superblock
  1750. */
  1751. static void nfs_clone_super(struct super_block *sb,
  1752. const struct super_block *old_sb)
  1753. {
  1754. struct nfs_server *server = NFS_SB(sb);
  1755. sb->s_blocksize_bits = old_sb->s_blocksize_bits;
  1756. sb->s_blocksize = old_sb->s_blocksize;
  1757. sb->s_maxbytes = old_sb->s_maxbytes;
  1758. if (server->nfs_client->rpc_ops->version == 3) {
  1759. /* The VFS shouldn't apply the umask to mode bits. We will do
  1760. * so ourselves when necessary.
  1761. */
  1762. sb->s_flags |= MS_POSIXACL;
  1763. sb->s_time_gran = 1;
  1764. }
  1765. sb->s_op = old_sb->s_op;
  1766. nfs_initialise_sb(sb);
  1767. }
  1768. static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
  1769. {
  1770. const struct nfs_server *a = s->s_fs_info;
  1771. const struct rpc_clnt *clnt_a = a->client;
  1772. const struct rpc_clnt *clnt_b = b->client;
  1773. if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
  1774. goto Ebusy;
  1775. if (a->nfs_client != b->nfs_client)
  1776. goto Ebusy;
  1777. if (a->flags != b->flags)
  1778. goto Ebusy;
  1779. if (a->wsize != b->wsize)
  1780. goto Ebusy;
  1781. if (a->rsize != b->rsize)
  1782. goto Ebusy;
  1783. if (a->acregmin != b->acregmin)
  1784. goto Ebusy;
  1785. if (a->acregmax != b->acregmax)
  1786. goto Ebusy;
  1787. if (a->acdirmin != b->acdirmin)
  1788. goto Ebusy;
  1789. if (a->acdirmax != b->acdirmax)
  1790. goto Ebusy;
  1791. if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
  1792. goto Ebusy;
  1793. return 1;
  1794. Ebusy:
  1795. return 0;
  1796. }
  1797. struct nfs_sb_mountdata {
  1798. struct nfs_server *server;
  1799. int mntflags;
  1800. };
  1801. static int nfs_set_super(struct super_block *s, void *data)
  1802. {
  1803. struct nfs_sb_mountdata *sb_mntdata = data;
  1804. struct nfs_server *server = sb_mntdata->server;
  1805. int ret;
  1806. s->s_flags = sb_mntdata->mntflags;
  1807. s->s_fs_info = server;
  1808. ret = set_anon_super(s, server);
  1809. if (ret == 0)
  1810. server->s_dev = s->s_dev;
  1811. return ret;
  1812. }
  1813. static int nfs_compare_super_address(struct nfs_server *server1,
  1814. struct nfs_server *server2)
  1815. {
  1816. struct sockaddr *sap1, *sap2;
  1817. sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
  1818. sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
  1819. if (sap1->sa_family != sap2->sa_family)
  1820. return 0;
  1821. switch (sap1->sa_family) {
  1822. case AF_INET: {
  1823. struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
  1824. struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
  1825. if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
  1826. return 0;
  1827. if (sin1->sin_port != sin2->sin_port)
  1828. return 0;
  1829. break;
  1830. }
  1831. case AF_INET6: {
  1832. struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
  1833. struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
  1834. if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
  1835. return 0;
  1836. if (sin1->sin6_port != sin2->sin6_port)
  1837. return 0;
  1838. break;
  1839. }
  1840. default:
  1841. return 0;
  1842. }
  1843. return 1;
  1844. }
  1845. static int nfs_compare_super(struct super_block *sb, void *data)
  1846. {
  1847. struct nfs_sb_mountdata *sb_mntdata = data;
  1848. struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
  1849. int mntflags = sb_mntdata->mntflags;
  1850. if (!nfs_compare_super_address(old, server))
  1851. return 0;
  1852. /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
  1853. if (old->flags & NFS_MOUNT_UNSHARED)
  1854. return 0;
  1855. if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
  1856. return 0;
  1857. return nfs_compare_mount_options(sb, server, mntflags);
  1858. }
  1859. static int nfs_bdi_register(struct nfs_server *server)
  1860. {
  1861. return bdi_register_dev(&server->backing_dev_info, server->s_dev);
  1862. }
  1863. static int nfs_get_sb(struct file_system_type *fs_type,
  1864. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
  1865. {
  1866. struct nfs_server *server = NULL;
  1867. struct super_block *s;
  1868. struct nfs_parsed_mount_data *data;
  1869. struct nfs_fh *mntfh;
  1870. struct dentry *mntroot;
  1871. int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
  1872. struct nfs_sb_mountdata sb_mntdata = {
  1873. .mntflags = flags,
  1874. };
  1875. int error = -ENOMEM;
  1876. data = nfs_alloc_parsed_mount_data(NFS_MOUNT_VER3 | NFS_MOUNT_TCP);
  1877. mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
  1878. if (data == NULL || mntfh == NULL)
  1879. goto out_free_fh;
  1880. security_init_mnt_opts(&data->lsm_opts);
  1881. /* Validate the mount data */
  1882. error = nfs_validate_mount_data(raw_data, data, mntfh, dev_name);
  1883. if (error < 0)
  1884. goto out;
  1885. #ifdef CONFIG_NFS_V4
  1886. if (data->version == 4) {
  1887. error = nfs4_try_mount(flags, dev_name, data, mnt);
  1888. kfree(data->client_address);
  1889. goto out;
  1890. }
  1891. #endif /* CONFIG_NFS_V4 */
  1892. /* Get a volume representation */
  1893. server = nfs_create_server(data, mntfh);
  1894. if (IS_ERR(server)) {
  1895. error = PTR_ERR(server);
  1896. goto out;
  1897. }
  1898. sb_mntdata.server = server;
  1899. if (server->flags & NFS_MOUNT_UNSHARED)
  1900. compare_super = NULL;
  1901. /* Get a superblock - note that we may end up sharing one that already exists */
  1902. s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
  1903. if (IS_ERR(s)) {
  1904. error = PTR_ERR(s);
  1905. goto out_err_nosb;
  1906. }
  1907. if (s->s_fs_info != server) {
  1908. nfs_free_server(server);
  1909. server = NULL;
  1910. } else {
  1911. error = nfs_bdi_register(server);
  1912. if (error)
  1913. goto error_splat_super;
  1914. }
  1915. if (!s->s_root) {
  1916. /* initial superblock/root creation */
  1917. nfs_fill_super(s, data);
  1918. nfs_fscache_get_super_cookie(
  1919. s, data ? data->fscache_uniq : NULL, NULL);
  1920. }
  1921. mntroot = nfs_get_root(s, mntfh);
  1922. if (IS_ERR(mntroot)) {
  1923. error = PTR_ERR(mntroot);
  1924. goto error_splat_super;
  1925. }
  1926. error = security_sb_set_mnt_opts(s, &data->lsm_opts);
  1927. if (error)
  1928. goto error_splat_root;
  1929. s->s_flags |= MS_ACTIVE;
  1930. mnt->mnt_sb = s;
  1931. mnt->mnt_root = mntroot;
  1932. error = 0;
  1933. out:
  1934. kfree(data->nfs_server.hostname);
  1935. kfree(data->mount_server.hostname);
  1936. kfree(data->fscache_uniq);
  1937. security_free_mnt_opts(&data->lsm_opts);
  1938. out_free_fh:
  1939. kfree(mntfh);
  1940. kfree(data);
  1941. return error;
  1942. out_err_nosb:
  1943. nfs_free_server(server);
  1944. goto out;
  1945. error_splat_root:
  1946. dput(mntroot);
  1947. error_splat_super:
  1948. deactivate_locked_super(s);
  1949. goto out;
  1950. }
  1951. /*
  1952. * Destroy an NFS2/3 superblock
  1953. */
  1954. static void nfs_kill_super(struct super_block *s)
  1955. {
  1956. struct nfs_server *server = NFS_SB(s);
  1957. kill_anon_super(s);
  1958. bdi_unregister(&server->backing_dev_info);
  1959. nfs_fscache_release_super_cookie(s);
  1960. nfs_free_server(server);
  1961. }
  1962. /*
  1963. * Clone an NFS2/3 server record on xdev traversal (FSID-change)
  1964. */
  1965. static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
  1966. const char *dev_name, void *raw_data,
  1967. struct vfsmount *mnt)
  1968. {
  1969. struct nfs_clone_mount *data = raw_data;
  1970. struct super_block *s;
  1971. struct nfs_server *server;
  1972. struct dentry *mntroot;
  1973. int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
  1974. struct nfs_sb_mountdata sb_mntdata = {
  1975. .mntflags = flags,
  1976. };
  1977. int error;
  1978. dprintk("--> nfs_xdev_get_sb()\n");
  1979. /* create a new volume representation */
  1980. server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
  1981. if (IS_ERR(server)) {
  1982. error = PTR_ERR(server);
  1983. goto out_err_noserver;
  1984. }
  1985. sb_mntdata.server = server;
  1986. if (server->flags & NFS_MOUNT_UNSHARED)
  1987. compare_super = NULL;
  1988. /* Get a superblock - note that we may end up sharing one that already exists */
  1989. s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
  1990. if (IS_ERR(s)) {
  1991. error = PTR_ERR(s);
  1992. goto out_err_nosb;
  1993. }
  1994. if (s->s_fs_info != server) {
  1995. nfs_free_server(server);
  1996. server = NULL;
  1997. } else {
  1998. error = nfs_bdi_register(server);
  1999. if (error)
  2000. goto error_splat_super;
  2001. }
  2002. if (!s->s_root) {
  2003. /* initial superblock/root creation */
  2004. nfs_clone_super(s, data->sb);
  2005. nfs_fscache_get_super_cookie(s, NULL, data);
  2006. }
  2007. mntroot = nfs_get_root(s, data->fh);
  2008. if (IS_ERR(mntroot)) {
  2009. error = PTR_ERR(mntroot);
  2010. goto error_splat_super;
  2011. }
  2012. if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
  2013. dput(mntroot);
  2014. error = -ESTALE;
  2015. goto error_splat_super;
  2016. }
  2017. s->s_flags |= MS_ACTIVE;
  2018. mnt->mnt_sb = s;
  2019. mnt->mnt_root = mntroot;
  2020. /* clone any lsm security options from the parent to the new sb */
  2021. security_sb_clone_mnt_opts(data->sb, s);
  2022. dprintk("<-- nfs_xdev_get_sb() = 0\n");
  2023. return 0;
  2024. out_err_nosb:
  2025. nfs_free_server(server);
  2026. out_err_noserver:
  2027. dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
  2028. return error;
  2029. error_splat_super:
  2030. deactivate_locked_super(s);
  2031. dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
  2032. return error;
  2033. }
  2034. #ifdef CONFIG_NFS_V4
  2035. /*
  2036. * Finish setting up a cloned NFS4 superblock
  2037. */
  2038. static void nfs4_clone_super(struct super_block *sb,
  2039. const struct super_block *old_sb)
  2040. {
  2041. sb->s_blocksize_bits = old_sb->s_blocksize_bits;
  2042. sb->s_blocksize = old_sb->s_blocksize;
  2043. sb->s_maxbytes = old_sb->s_maxbytes;
  2044. sb->s_time_gran = 1;
  2045. sb->s_op = old_sb->s_op;
  2046. nfs_initialise_sb(sb);
  2047. }
  2048. /*
  2049. * Set up an NFS4 superblock
  2050. */
  2051. static void nfs4_fill_super(struct super_block *sb)
  2052. {
  2053. sb->s_time_gran = 1;
  2054. sb->s_op = &nfs4_sops;
  2055. nfs_initialise_sb(sb);
  2056. }
  2057. static void nfs4_validate_mount_flags(struct nfs_parsed_mount_data *args)
  2058. {
  2059. args->flags &= ~(NFS_MOUNT_NONLM|NFS_MOUNT_NOACL|NFS_MOUNT_VER3);
  2060. }
  2061. static int nfs4_validate_text_mount_data(void *options,
  2062. struct nfs_parsed_mount_data *args,
  2063. const char *dev_name)
  2064. {
  2065. struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
  2066. nfs_set_default_port(sap, args->nfs_server.port, NFS_PORT);
  2067. nfs_validate_transport_protocol(args);
  2068. nfs4_validate_mount_flags(args);
  2069. if (args->version != 4) {
  2070. dfprintk(MOUNT,
  2071. "NFS4: Illegal mount version\n");
  2072. return -EINVAL;
  2073. }
  2074. if (args->auth_flavor_len > 1) {
  2075. dfprintk(MOUNT,
  2076. "NFS4: Too many RPC auth flavours specified\n");
  2077. return -EINVAL;
  2078. }
  2079. if (args->client_address == NULL) {
  2080. dfprintk(MOUNT,
  2081. "NFS4: mount program didn't pass callback address\n");
  2082. return -EINVAL;
  2083. }
  2084. return nfs_parse_devname(dev_name,
  2085. &args->nfs_server.hostname,
  2086. NFS4_MAXNAMLEN,
  2087. &args->nfs_server.export_path,
  2088. NFS4_MAXPATHLEN);
  2089. }
  2090. /*
  2091. * Validate NFSv4 mount options
  2092. */
  2093. static int nfs4_validate_mount_data(void *options,
  2094. struct nfs_parsed_mount_data *args,
  2095. const char *dev_name)
  2096. {
  2097. struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
  2098. struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
  2099. char *c;
  2100. if (data == NULL)
  2101. goto out_no_data;
  2102. args->version = 4;
  2103. switch (data->version) {
  2104. case 1:
  2105. if (data->host_addrlen > sizeof(args->nfs_server.address))
  2106. goto out_no_address;
  2107. if (data->host_addrlen == 0)
  2108. goto out_no_address;
  2109. args->nfs_server.addrlen = data->host_addrlen;
  2110. if (copy_from_user(sap, data->host_addr, data->host_addrlen))
  2111. return -EFAULT;
  2112. if (!nfs_verify_server_address(sap))
  2113. goto out_no_address;
  2114. if (data->auth_flavourlen) {
  2115. if (data->auth_flavourlen > 1)
  2116. goto out_inval_auth;
  2117. if (copy_from_user(&args->auth_flavors[0],
  2118. data->auth_flavours,
  2119. sizeof(args->auth_flavors[0])))
  2120. return -EFAULT;
  2121. }
  2122. c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
  2123. if (IS_ERR(c))
  2124. return PTR_ERR(c);
  2125. args->nfs_server.hostname = c;
  2126. c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
  2127. if (IS_ERR(c))
  2128. return PTR_ERR(c);
  2129. args->nfs_server.export_path = c;
  2130. dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
  2131. c = strndup_user(data->client_addr.data, 16);
  2132. if (IS_ERR(c))
  2133. return PTR_ERR(c);
  2134. args->client_address = c;
  2135. /*
  2136. * Translate to nfs_parsed_mount_data, which nfs4_fill_super
  2137. * can deal with.
  2138. */
  2139. args->flags = data->flags & NFS4_MOUNT_FLAGMASK;
  2140. args->rsize = data->rsize;
  2141. args->wsize = data->wsize;
  2142. args->timeo = data->timeo;
  2143. args->retrans = data->retrans;
  2144. args->acregmin = data->acregmin;
  2145. args->acregmax = data->acregmax;
  2146. args->acdirmin = data->acdirmin;
  2147. args->acdirmax = data->acdirmax;
  2148. args->nfs_server.protocol = data->proto;
  2149. nfs_validate_transport_protocol(args);
  2150. break;
  2151. default:
  2152. if (nfs_parse_mount_options((char *)options, args) == 0)
  2153. return -EINVAL;
  2154. if (!nfs_verify_server_address(sap))
  2155. return -EINVAL;
  2156. return nfs4_validate_text_mount_data(options, args, dev_name);
  2157. }
  2158. return 0;
  2159. out_no_data:
  2160. dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
  2161. return -EINVAL;
  2162. out_inval_auth:
  2163. dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
  2164. data->auth_flavourlen);
  2165. return -EINVAL;
  2166. out_no_address:
  2167. dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
  2168. return -EINVAL;
  2169. }
  2170. /*
  2171. * Get the superblock for the NFS4 root partition
  2172. */
  2173. static int nfs4_remote_get_sb(struct file_system_type *fs_type,
  2174. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
  2175. {
  2176. struct nfs_parsed_mount_data *data = raw_data;
  2177. struct super_block *s;
  2178. struct nfs_server *server;
  2179. struct nfs_fh *mntfh;
  2180. struct dentry *mntroot;
  2181. int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
  2182. struct nfs_sb_mountdata sb_mntdata = {
  2183. .mntflags = flags,
  2184. };
  2185. int error = -ENOMEM;
  2186. mntfh = kzalloc(sizeof(*mntfh), GFP_KERNEL);
  2187. if (data == NULL || mntfh == NULL)
  2188. goto out_free_fh;
  2189. security_init_mnt_opts(&data->lsm_opts);
  2190. /* Get a volume representation */
  2191. server = nfs4_create_server(data, mntfh);
  2192. if (IS_ERR(server)) {
  2193. error = PTR_ERR(server);
  2194. goto out;
  2195. }
  2196. sb_mntdata.server = server;
  2197. if (server->flags & NFS4_MOUNT_UNSHARED)
  2198. compare_super = NULL;
  2199. /* Get a superblock - note that we may end up sharing one that already exists */
  2200. s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
  2201. if (IS_ERR(s)) {
  2202. error = PTR_ERR(s);
  2203. goto out_free;
  2204. }
  2205. if (s->s_fs_info != server) {
  2206. nfs_free_server(server);
  2207. server = NULL;
  2208. } else {
  2209. error = nfs_bdi_register(server);
  2210. if (error)
  2211. goto error_splat_super;
  2212. }
  2213. if (!s->s_root) {
  2214. /* initial superblock/root creation */
  2215. nfs4_fill_super(s);
  2216. nfs_fscache_get_super_cookie(
  2217. s, data ? data->fscache_uniq : NULL, NULL);
  2218. }
  2219. mntroot = nfs4_get_root(s, mntfh);
  2220. if (IS_ERR(mntroot)) {
  2221. error = PTR_ERR(mntroot);
  2222. goto error_splat_super;
  2223. }
  2224. error = security_sb_set_mnt_opts(s, &data->lsm_opts);
  2225. if (error)
  2226. goto error_splat_root;
  2227. s->s_flags |= MS_ACTIVE;
  2228. mnt->mnt_sb = s;
  2229. mnt->mnt_root = mntroot;
  2230. error = 0;
  2231. out:
  2232. security_free_mnt_opts(&data->lsm_opts);
  2233. out_free_fh:
  2234. kfree(mntfh);
  2235. return error;
  2236. out_free:
  2237. nfs_free_server(server);
  2238. goto out;
  2239. error_splat_root:
  2240. dput(mntroot);
  2241. error_splat_super:
  2242. deactivate_locked_super(s);
  2243. goto out;
  2244. }
  2245. static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type,
  2246. int flags, void *data, const char *hostname)
  2247. {
  2248. struct vfsmount *root_mnt;
  2249. char *root_devname;
  2250. size_t len;
  2251. len = strlen(hostname) + 3;
  2252. root_devname = kmalloc(len, GFP_KERNEL);
  2253. if (root_devname == NULL)
  2254. return ERR_PTR(-ENOMEM);
  2255. snprintf(root_devname, len, "%s:/", hostname);
  2256. root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data);
  2257. kfree(root_devname);
  2258. return root_mnt;
  2259. }
  2260. static void nfs_fix_devname(const struct path *path, struct vfsmount *mnt)
  2261. {
  2262. char *page = (char *) __get_free_page(GFP_KERNEL);
  2263. char *devname, *tmp;
  2264. if (page == NULL)
  2265. return;
  2266. devname = nfs_path(path->mnt->mnt_devname,
  2267. path->mnt->mnt_root, path->dentry,
  2268. page, PAGE_SIZE);
  2269. if (devname == NULL)
  2270. goto out_freepage;
  2271. tmp = kstrdup(devname, GFP_KERNEL);
  2272. if (tmp == NULL)
  2273. goto out_freepage;
  2274. kfree(mnt->mnt_devname);
  2275. mnt->mnt_devname = tmp;
  2276. out_freepage:
  2277. free_page((unsigned long)page);
  2278. }
  2279. static int nfs_follow_remote_path(struct vfsmount *root_mnt,
  2280. const char *export_path, struct vfsmount *mnt_target)
  2281. {
  2282. struct mnt_namespace *ns_private;
  2283. struct nameidata nd;
  2284. struct super_block *s;
  2285. int ret;
  2286. ns_private = create_mnt_ns(root_mnt);
  2287. ret = PTR_ERR(ns_private);
  2288. if (IS_ERR(ns_private))
  2289. goto out_mntput;
  2290. ret = vfs_path_lookup(root_mnt->mnt_root, root_mnt,
  2291. export_path, LOOKUP_FOLLOW, &nd);
  2292. put_mnt_ns(ns_private);
  2293. if (ret != 0)
  2294. goto out_err;
  2295. s = nd.path.mnt->mnt_sb;
  2296. atomic_inc(&s->s_active);
  2297. mnt_target->mnt_sb = s;
  2298. mnt_target->mnt_root = dget(nd.path.dentry);
  2299. /* Correct the device pathname */
  2300. nfs_fix_devname(&nd.path, mnt_target);
  2301. path_put(&nd.path);
  2302. down_write(&s->s_umount);
  2303. return 0;
  2304. out_mntput:
  2305. mntput(root_mnt);
  2306. out_err:
  2307. return ret;
  2308. }
  2309. static int nfs4_try_mount(int flags, const char *dev_name,
  2310. struct nfs_parsed_mount_data *data,
  2311. struct vfsmount *mnt)
  2312. {
  2313. char *export_path;
  2314. struct vfsmount *root_mnt;
  2315. int error;
  2316. dfprintk(MOUNT, "--> nfs4_try_mount()\n");
  2317. export_path = data->nfs_server.export_path;
  2318. data->nfs_server.export_path = "/";
  2319. root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, data,
  2320. data->nfs_server.hostname);
  2321. data->nfs_server.export_path = export_path;
  2322. error = PTR_ERR(root_mnt);
  2323. if (IS_ERR(root_mnt))
  2324. goto out;
  2325. error = nfs_follow_remote_path(root_mnt, export_path, mnt);
  2326. out:
  2327. dfprintk(MOUNT, "<-- nfs4_try_mount() = %d%s\n", error,
  2328. error != 0 ? " [error]" : "");
  2329. return error;
  2330. }
  2331. /*
  2332. * Get the superblock for an NFS4 mountpoint
  2333. */
  2334. static int nfs4_get_sb(struct file_system_type *fs_type,
  2335. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
  2336. {
  2337. struct nfs_parsed_mount_data *data;
  2338. int error = -ENOMEM;
  2339. data = nfs_alloc_parsed_mount_data(0);
  2340. if (data == NULL)
  2341. goto out_free_data;
  2342. /* Validate the mount data */
  2343. error = nfs4_validate_mount_data(raw_data, data, dev_name);
  2344. if (error < 0)
  2345. goto out;
  2346. error = nfs4_try_mount(flags, dev_name, data, mnt);
  2347. out:
  2348. kfree(data->client_address);
  2349. kfree(data->nfs_server.export_path);
  2350. kfree(data->nfs_server.hostname);
  2351. kfree(data->fscache_uniq);
  2352. out_free_data:
  2353. kfree(data);
  2354. dprintk("<-- nfs4_get_sb() = %d%s\n", error,
  2355. error != 0 ? " [error]" : "");
  2356. return error;
  2357. }
  2358. static void nfs4_kill_super(struct super_block *sb)
  2359. {
  2360. struct nfs_server *server = NFS_SB(sb);
  2361. dprintk("--> %s\n", __func__);
  2362. nfs_super_return_all_delegations(sb);
  2363. kill_anon_super(sb);
  2364. nfs4_renewd_prepare_shutdown(server);
  2365. nfs_fscache_release_super_cookie(sb);
  2366. nfs_free_server(server);
  2367. dprintk("<-- %s\n", __func__);
  2368. }
  2369. /*
  2370. * Clone an NFS4 server record on xdev traversal (FSID-change)
  2371. */
  2372. static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
  2373. const char *dev_name, void *raw_data,
  2374. struct vfsmount *mnt)
  2375. {
  2376. struct nfs_clone_mount *data = raw_data;
  2377. struct super_block *s;
  2378. struct nfs_server *server;
  2379. struct dentry *mntroot;
  2380. int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
  2381. struct nfs_sb_mountdata sb_mntdata = {
  2382. .mntflags = flags,
  2383. };
  2384. int error;
  2385. dprintk("--> nfs4_xdev_get_sb()\n");
  2386. /* create a new volume representation */
  2387. server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
  2388. if (IS_ERR(server)) {
  2389. error = PTR_ERR(server);
  2390. goto out_err_noserver;
  2391. }
  2392. sb_mntdata.server = server;
  2393. if (server->flags & NFS4_MOUNT_UNSHARED)
  2394. compare_super = NULL;
  2395. /* Get a superblock - note that we may end up sharing one that already exists */
  2396. s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
  2397. if (IS_ERR(s)) {
  2398. error = PTR_ERR(s);
  2399. goto out_err_nosb;
  2400. }
  2401. if (s->s_fs_info != server) {
  2402. nfs_free_server(server);
  2403. server = NULL;
  2404. } else {
  2405. error = nfs_bdi_register(server);
  2406. if (error)
  2407. goto error_splat_super;
  2408. }
  2409. if (!s->s_root) {
  2410. /* initial superblock/root creation */
  2411. nfs4_clone_super(s, data->sb);
  2412. nfs_fscache_get_super_cookie(s, NULL, data);
  2413. }
  2414. mntroot = nfs4_get_root(s, data->fh);
  2415. if (IS_ERR(mntroot)) {
  2416. error = PTR_ERR(mntroot);
  2417. goto error_splat_super;
  2418. }
  2419. if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
  2420. dput(mntroot);
  2421. error = -ESTALE;
  2422. goto error_splat_super;
  2423. }
  2424. s->s_flags |= MS_ACTIVE;
  2425. mnt->mnt_sb = s;
  2426. mnt->mnt_root = mntroot;
  2427. security_sb_clone_mnt_opts(data->sb, s);
  2428. dprintk("<-- nfs4_xdev_get_sb() = 0\n");
  2429. return 0;
  2430. out_err_nosb:
  2431. nfs_free_server(server);
  2432. out_err_noserver:
  2433. dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
  2434. return error;
  2435. error_splat_super:
  2436. deactivate_locked_super(s);
  2437. dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
  2438. return error;
  2439. }
  2440. static int nfs4_remote_referral_get_sb(struct file_system_type *fs_type,
  2441. int flags, const char *dev_name, void *raw_data,
  2442. struct vfsmount *mnt)
  2443. {
  2444. struct nfs_clone_mount *data = raw_data;
  2445. struct super_block *s;
  2446. struct nfs_server *server;
  2447. struct dentry *mntroot;
  2448. struct nfs_fh mntfh;
  2449. int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
  2450. struct nfs_sb_mountdata sb_mntdata = {
  2451. .mntflags = flags,
  2452. };
  2453. int error;
  2454. dprintk("--> nfs4_referral_get_sb()\n");
  2455. /* create a new volume representation */
  2456. server = nfs4_create_referral_server(data, &mntfh);
  2457. if (IS_ERR(server)) {
  2458. error = PTR_ERR(server);
  2459. goto out_err_noserver;
  2460. }
  2461. sb_mntdata.server = server;
  2462. if (server->flags & NFS4_MOUNT_UNSHARED)
  2463. compare_super = NULL;
  2464. /* Get a superblock - note that we may end up sharing one that already exists */
  2465. s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
  2466. if (IS_ERR(s)) {
  2467. error = PTR_ERR(s);
  2468. goto out_err_nosb;
  2469. }
  2470. if (s->s_fs_info != server) {
  2471. nfs_free_server(server);
  2472. server = NULL;
  2473. } else {
  2474. error = nfs_bdi_register(server);
  2475. if (error)
  2476. goto error_splat_super;
  2477. }
  2478. if (!s->s_root) {
  2479. /* initial superblock/root creation */
  2480. nfs4_fill_super(s);
  2481. nfs_fscache_get_super_cookie(s, NULL, data);
  2482. }
  2483. mntroot = nfs4_get_root(s, &mntfh);
  2484. if (IS_ERR(mntroot)) {
  2485. error = PTR_ERR(mntroot);
  2486. goto error_splat_super;
  2487. }
  2488. if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
  2489. dput(mntroot);
  2490. error = -ESTALE;
  2491. goto error_splat_super;
  2492. }
  2493. s->s_flags |= MS_ACTIVE;
  2494. mnt->mnt_sb = s;
  2495. mnt->mnt_root = mntroot;
  2496. security_sb_clone_mnt_opts(data->sb, s);
  2497. dprintk("<-- nfs4_referral_get_sb() = 0\n");
  2498. return 0;
  2499. out_err_nosb:
  2500. nfs_free_server(server);
  2501. out_err_noserver:
  2502. dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
  2503. return error;
  2504. error_splat_super:
  2505. deactivate_locked_super(s);
  2506. dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
  2507. return error;
  2508. }
  2509. /*
  2510. * Create an NFS4 server record on referral traversal
  2511. */
  2512. static int nfs4_referral_get_sb(struct file_system_type *fs_type,
  2513. int flags, const char *dev_name, void *raw_data,
  2514. struct vfsmount *mnt)
  2515. {
  2516. struct nfs_clone_mount *data = raw_data;
  2517. char *export_path;
  2518. struct vfsmount *root_mnt;
  2519. int error;
  2520. dprintk("--> nfs4_referral_get_sb()\n");
  2521. export_path = data->mnt_path;
  2522. data->mnt_path = "/";
  2523. root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
  2524. flags, data, data->hostname);
  2525. data->mnt_path = export_path;
  2526. error = PTR_ERR(root_mnt);
  2527. if (IS_ERR(root_mnt))
  2528. goto out;
  2529. error = nfs_follow_remote_path(root_mnt, export_path, mnt);
  2530. out:
  2531. dprintk("<-- nfs4_referral_get_sb() = %d%s\n", error,
  2532. error != 0 ? " [error]" : "");
  2533. return error;
  2534. }
  2535. #endif /* CONFIG_NFS_V4 */