super.c 77 KB

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