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