super.c 74 KB

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