super.c 70 KB

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