super.c 74 KB

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