super.c 71 KB

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