super.c 79 KB

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