super.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180
  1. /*
  2. * linux/fs/nfs/super.c
  3. *
  4. * Copyright (C) 1992 Rick Sladkey
  5. *
  6. * nfs superblock handling functions
  7. *
  8. * Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
  9. * experimental NFS changes. Modularisation taken straight from SYS5 fs.
  10. *
  11. * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
  12. * J.S.Peatfield@damtp.cam.ac.uk
  13. *
  14. * Split from inode.c by David Howells <dhowells@redhat.com>
  15. *
  16. * - superblocks are indexed on server only - all inodes, dentries, etc. associated with a
  17. * particular server are held in the same superblock
  18. * - NFS superblocks can have several effective roots to the dentry tree
  19. * - directory type roots are spliced into the tree when a path from one root reaches the root
  20. * of another (see nfs_lookup())
  21. */
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/time.h>
  25. #include <linux/kernel.h>
  26. #include <linux/mm.h>
  27. #include <linux/string.h>
  28. #include <linux/stat.h>
  29. #include <linux/errno.h>
  30. #include <linux/unistd.h>
  31. #include <linux/sunrpc/clnt.h>
  32. #include <linux/sunrpc/stats.h>
  33. #include <linux/sunrpc/metrics.h>
  34. #include <linux/sunrpc/xprtsock.h>
  35. #include <linux/sunrpc/xprtrdma.h>
  36. #include <linux/nfs_fs.h>
  37. #include <linux/nfs_mount.h>
  38. #include <linux/nfs4_mount.h>
  39. #include <linux/lockd/bind.h>
  40. #include <linux/smp_lock.h>
  41. #include <linux/seq_file.h>
  42. #include <linux/mount.h>
  43. #include <linux/mnt_namespace.h>
  44. #include <linux/namei.h>
  45. #include <linux/nfs_idmap.h>
  46. #include <linux/vfs.h>
  47. #include <linux/inet.h>
  48. #include <linux/in6.h>
  49. #include <linux/slab.h>
  50. #include <net/ipv6.h>
  51. #include <linux/netdevice.h>
  52. #include <linux/nfs_xdr.h>
  53. #include <linux/magic.h>
  54. #include <linux/parser.h>
  55. #include <asm/system.h>
  56. #include <asm/uaccess.h>
  57. #include "nfs4_fs.h"
  58. #include "callback.h"
  59. #include "delegation.h"
  60. #include "iostat.h"
  61. #include "internal.h"
  62. #include "fscache.h"
  63. #define NFSDBG_FACILITY NFSDBG_VFS
  64. enum {
  65. /* Mount options that take no arguments */
  66. Opt_soft, Opt_hard,
  67. Opt_posix, Opt_noposix,
  68. Opt_cto, Opt_nocto,
  69. Opt_ac, Opt_noac,
  70. Opt_lock, Opt_nolock,
  71. Opt_v2, Opt_v3, Opt_v4,
  72. Opt_udp, Opt_tcp, Opt_rdma,
  73. Opt_acl, Opt_noacl,
  74. Opt_rdirplus, Opt_nordirplus,
  75. Opt_sharecache, Opt_nosharecache,
  76. Opt_resvport, Opt_noresvport,
  77. Opt_fscache, Opt_nofscache,
  78. /* Mount options that take integer arguments */
  79. Opt_port,
  80. Opt_rsize, Opt_wsize, Opt_bsize,
  81. Opt_timeo, Opt_retrans,
  82. Opt_acregmin, Opt_acregmax,
  83. Opt_acdirmin, Opt_acdirmax,
  84. Opt_actimeo,
  85. Opt_namelen,
  86. Opt_mountport,
  87. Opt_mountvers,
  88. Opt_nfsvers,
  89. Opt_minorversion,
  90. /* Mount options that take string arguments */
  91. Opt_sec, Opt_proto, Opt_mountproto, Opt_mounthost,
  92. Opt_addr, Opt_mountaddr, Opt_clientaddr,
  93. Opt_lookupcache,
  94. Opt_fscache_uniq,
  95. Opt_local_lock,
  96. /* Special mount options */
  97. Opt_userspace, Opt_deprecated, Opt_sloppy,
  98. Opt_err
  99. };
  100. static const match_table_t nfs_mount_option_tokens = {
  101. { Opt_userspace, "bg" },
  102. { Opt_userspace, "fg" },
  103. { Opt_userspace, "retry=%s" },
  104. { Opt_sloppy, "sloppy" },
  105. { Opt_soft, "soft" },
  106. { Opt_hard, "hard" },
  107. { Opt_deprecated, "intr" },
  108. { Opt_deprecated, "nointr" },
  109. { Opt_posix, "posix" },
  110. { Opt_noposix, "noposix" },
  111. { Opt_cto, "cto" },
  112. { Opt_nocto, "nocto" },
  113. { Opt_ac, "ac" },
  114. { Opt_noac, "noac" },
  115. { Opt_lock, "lock" },
  116. { Opt_nolock, "nolock" },
  117. { Opt_v2, "v2" },
  118. { Opt_v3, "v3" },
  119. { Opt_v4, "v4" },
  120. { Opt_udp, "udp" },
  121. { Opt_tcp, "tcp" },
  122. { Opt_rdma, "rdma" },
  123. { Opt_acl, "acl" },
  124. { Opt_noacl, "noacl" },
  125. { Opt_rdirplus, "rdirplus" },
  126. { Opt_nordirplus, "nordirplus" },
  127. { Opt_sharecache, "sharecache" },
  128. { Opt_nosharecache, "nosharecache" },
  129. { Opt_resvport, "resvport" },
  130. { Opt_noresvport, "noresvport" },
  131. { Opt_fscache, "fsc" },
  132. { Opt_nofscache, "nofsc" },
  133. { Opt_port, "port=%s" },
  134. { Opt_rsize, "rsize=%s" },
  135. { Opt_wsize, "wsize=%s" },
  136. { Opt_bsize, "bsize=%s" },
  137. { Opt_timeo, "timeo=%s" },
  138. { Opt_retrans, "retrans=%s" },
  139. { Opt_acregmin, "acregmin=%s" },
  140. { Opt_acregmax, "acregmax=%s" },
  141. { Opt_acdirmin, "acdirmin=%s" },
  142. { Opt_acdirmax, "acdirmax=%s" },
  143. { Opt_actimeo, "actimeo=%s" },
  144. { Opt_namelen, "namlen=%s" },
  145. { Opt_mountport, "mountport=%s" },
  146. { Opt_mountvers, "mountvers=%s" },
  147. { Opt_nfsvers, "nfsvers=%s" },
  148. { Opt_nfsvers, "vers=%s" },
  149. { Opt_minorversion, "minorversion=%s" },
  150. { Opt_sec, "sec=%s" },
  151. { Opt_proto, "proto=%s" },
  152. { Opt_mountproto, "mountproto=%s" },
  153. { Opt_addr, "addr=%s" },
  154. { Opt_clientaddr, "clientaddr=%s" },
  155. { Opt_mounthost, "mounthost=%s" },
  156. { Opt_mountaddr, "mountaddr=%s" },
  157. { Opt_lookupcache, "lookupcache=%s" },
  158. { Opt_fscache_uniq, "fsc=%s" },
  159. { Opt_local_lock, "local_lock=%s" },
  160. { Opt_err, NULL }
  161. };
  162. enum {
  163. Opt_xprt_udp, Opt_xprt_udp6, Opt_xprt_tcp, Opt_xprt_tcp6, Opt_xprt_rdma,
  164. Opt_xprt_err
  165. };
  166. static const match_table_t nfs_xprt_protocol_tokens = {
  167. { Opt_xprt_udp, "udp" },
  168. { Opt_xprt_udp6, "udp6" },
  169. { Opt_xprt_tcp, "tcp" },
  170. { Opt_xprt_tcp6, "tcp6" },
  171. { Opt_xprt_rdma, "rdma" },
  172. { Opt_xprt_err, NULL }
  173. };
  174. enum {
  175. Opt_sec_none, Opt_sec_sys,
  176. Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
  177. Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
  178. Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,
  179. Opt_sec_err
  180. };
  181. static const match_table_t nfs_secflavor_tokens = {
  182. { Opt_sec_none, "none" },
  183. { Opt_sec_none, "null" },
  184. { Opt_sec_sys, "sys" },
  185. { Opt_sec_krb5, "krb5" },
  186. { Opt_sec_krb5i, "krb5i" },
  187. { Opt_sec_krb5p, "krb5p" },
  188. { Opt_sec_lkey, "lkey" },
  189. { Opt_sec_lkeyi, "lkeyi" },
  190. { Opt_sec_lkeyp, "lkeyp" },
  191. { Opt_sec_spkm, "spkm3" },
  192. { Opt_sec_spkmi, "spkm3i" },
  193. { Opt_sec_spkmp, "spkm3p" },
  194. { Opt_sec_err, NULL }
  195. };
  196. enum {
  197. Opt_lookupcache_all, Opt_lookupcache_positive,
  198. Opt_lookupcache_none,
  199. Opt_lookupcache_err
  200. };
  201. static match_table_t nfs_lookupcache_tokens = {
  202. { Opt_lookupcache_all, "all" },
  203. { Opt_lookupcache_positive, "pos" },
  204. { Opt_lookupcache_positive, "positive" },
  205. { Opt_lookupcache_none, "none" },
  206. { Opt_lookupcache_err, NULL }
  207. };
  208. enum {
  209. Opt_local_lock_all, Opt_local_lock_flock, Opt_local_lock_posix,
  210. Opt_local_lock_none,
  211. Opt_local_lock_err
  212. };
  213. static match_table_t nfs_local_lock_tokens = {
  214. { Opt_local_lock_all, "all" },
  215. { Opt_local_lock_flock, "flock" },
  216. { Opt_local_lock_posix, "posix" },
  217. { Opt_local_lock_none, "none" },
  218. { Opt_local_lock_err, NULL }
  219. };
  220. static void nfs_umount_begin(struct super_block *);
  221. static int nfs_statfs(struct dentry *, struct kstatfs *);
  222. static int nfs_show_options(struct seq_file *, struct vfsmount *);
  223. static int nfs_show_stats(struct seq_file *, struct vfsmount *);
  224. static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
  225. static int nfs_xdev_get_sb(struct file_system_type *fs_type,
  226. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
  227. static void nfs_put_super(struct super_block *);
  228. static void nfs_kill_super(struct super_block *);
  229. static int nfs_remount(struct super_block *sb, int *flags, char *raw_data);
  230. static struct file_system_type nfs_fs_type = {
  231. .owner = THIS_MODULE,
  232. .name = "nfs",
  233. .get_sb = nfs_get_sb,
  234. .kill_sb = nfs_kill_super,
  235. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  236. };
  237. struct file_system_type nfs_xdev_fs_type = {
  238. .owner = THIS_MODULE,
  239. .name = "nfs",
  240. .get_sb = nfs_xdev_get_sb,
  241. .kill_sb = nfs_kill_super,
  242. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  243. };
  244. static const struct super_operations nfs_sops = {
  245. .alloc_inode = nfs_alloc_inode,
  246. .destroy_inode = nfs_destroy_inode,
  247. .write_inode = nfs_write_inode,
  248. .put_super = nfs_put_super,
  249. .statfs = nfs_statfs,
  250. .evict_inode = nfs_evict_inode,
  251. .umount_begin = nfs_umount_begin,
  252. .show_options = nfs_show_options,
  253. .show_stats = nfs_show_stats,
  254. .remount_fs = nfs_remount,
  255. };
  256. #ifdef CONFIG_NFS_V4
  257. static int nfs4_validate_text_mount_data(void *options,
  258. struct nfs_parsed_mount_data *args, const char *dev_name);
  259. static int nfs4_try_mount(int flags, const char *dev_name,
  260. struct nfs_parsed_mount_data *data, struct vfsmount *mnt);
  261. static int nfs4_get_sb(struct file_system_type *fs_type,
  262. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
  263. static int nfs4_remote_get_sb(struct file_system_type *fs_type,
  264. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
  265. static int nfs4_xdev_get_sb(struct file_system_type *fs_type,
  266. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
  267. static int nfs4_referral_get_sb(struct file_system_type *fs_type,
  268. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
  269. static int nfs4_remote_referral_get_sb(struct file_system_type *fs_type,
  270. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
  271. static void nfs4_kill_super(struct super_block *sb);
  272. static struct file_system_type nfs4_fs_type = {
  273. .owner = THIS_MODULE,
  274. .name = "nfs4",
  275. .get_sb = nfs4_get_sb,
  276. .kill_sb = nfs4_kill_super,
  277. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  278. };
  279. static struct file_system_type nfs4_remote_fs_type = {
  280. .owner = THIS_MODULE,
  281. .name = "nfs4",
  282. .get_sb = nfs4_remote_get_sb,
  283. .kill_sb = nfs4_kill_super,
  284. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  285. };
  286. struct file_system_type nfs4_xdev_fs_type = {
  287. .owner = THIS_MODULE,
  288. .name = "nfs4",
  289. .get_sb = nfs4_xdev_get_sb,
  290. .kill_sb = nfs4_kill_super,
  291. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  292. };
  293. static struct file_system_type nfs4_remote_referral_fs_type = {
  294. .owner = THIS_MODULE,
  295. .name = "nfs4",
  296. .get_sb = nfs4_remote_referral_get_sb,
  297. .kill_sb = nfs4_kill_super,
  298. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  299. };
  300. struct file_system_type nfs4_referral_fs_type = {
  301. .owner = THIS_MODULE,
  302. .name = "nfs4",
  303. .get_sb = nfs4_referral_get_sb,
  304. .kill_sb = nfs4_kill_super,
  305. .fs_flags = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
  306. };
  307. static const struct super_operations nfs4_sops = {
  308. .alloc_inode = nfs_alloc_inode,
  309. .destroy_inode = nfs_destroy_inode,
  310. .write_inode = nfs_write_inode,
  311. .put_super = nfs_put_super,
  312. .statfs = nfs_statfs,
  313. .evict_inode = nfs4_evict_inode,
  314. .umount_begin = nfs_umount_begin,
  315. .show_options = nfs_show_options,
  316. .show_stats = nfs_show_stats,
  317. .remount_fs = nfs_remount,
  318. };
  319. #endif
  320. static struct shrinker acl_shrinker = {
  321. .shrink = nfs_access_cache_shrinker,
  322. .seeks = DEFAULT_SEEKS,
  323. };
  324. /*
  325. * Register the NFS filesystems
  326. */
  327. int __init register_nfs_fs(void)
  328. {
  329. int ret;
  330. ret = register_filesystem(&nfs_fs_type);
  331. if (ret < 0)
  332. goto error_0;
  333. ret = nfs_register_sysctl();
  334. if (ret < 0)
  335. goto error_1;
  336. #ifdef CONFIG_NFS_V4
  337. ret = register_filesystem(&nfs4_fs_type);
  338. if (ret < 0)
  339. goto error_2;
  340. #endif
  341. register_shrinker(&acl_shrinker);
  342. return 0;
  343. #ifdef CONFIG_NFS_V4
  344. error_2:
  345. nfs_unregister_sysctl();
  346. #endif
  347. error_1:
  348. unregister_filesystem(&nfs_fs_type);
  349. error_0:
  350. return ret;
  351. }
  352. /*
  353. * Unregister the NFS filesystems
  354. */
  355. void __exit unregister_nfs_fs(void)
  356. {
  357. unregister_shrinker(&acl_shrinker);
  358. #ifdef CONFIG_NFS_V4
  359. unregister_filesystem(&nfs4_fs_type);
  360. #endif
  361. nfs_unregister_sysctl();
  362. unregister_filesystem(&nfs_fs_type);
  363. }
  364. void nfs_sb_active(struct super_block *sb)
  365. {
  366. struct nfs_server *server = NFS_SB(sb);
  367. if (atomic_inc_return(&server->active) == 1)
  368. atomic_inc(&sb->s_active);
  369. }
  370. void nfs_sb_deactive(struct super_block *sb)
  371. {
  372. struct nfs_server *server = NFS_SB(sb);
  373. if (atomic_dec_and_test(&server->active))
  374. deactivate_super(sb);
  375. }
  376. /*
  377. * Deliver file system statistics to userspace
  378. */
  379. static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  380. {
  381. struct nfs_server *server = NFS_SB(dentry->d_sb);
  382. unsigned char blockbits;
  383. unsigned long blockres;
  384. struct nfs_fh *fh = NFS_FH(dentry->d_inode);
  385. struct nfs_fsstat res;
  386. int error = -ENOMEM;
  387. res.fattr = nfs_alloc_fattr();
  388. if (res.fattr == NULL)
  389. goto out_err;
  390. error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
  391. if (unlikely(error == -ESTALE)) {
  392. struct dentry *pd_dentry;
  393. pd_dentry = dget_parent(dentry);
  394. if (pd_dentry != NULL) {
  395. nfs_zap_caches(pd_dentry->d_inode);
  396. dput(pd_dentry);
  397. }
  398. }
  399. nfs_free_fattr(res.fattr);
  400. if (error < 0)
  401. goto out_err;
  402. buf->f_type = NFS_SUPER_MAGIC;
  403. /*
  404. * Current versions of glibc do not correctly handle the
  405. * case where f_frsize != f_bsize. Eventually we want to
  406. * report the value of wtmult in this field.
  407. */
  408. buf->f_frsize = dentry->d_sb->s_blocksize;
  409. /*
  410. * On most *nix systems, f_blocks, f_bfree, and f_bavail
  411. * are reported in units of f_frsize. Linux hasn't had
  412. * an f_frsize field in its statfs struct until recently,
  413. * thus historically Linux's sys_statfs reports these
  414. * fields in units of f_bsize.
  415. */
  416. buf->f_bsize = dentry->d_sb->s_blocksize;
  417. blockbits = dentry->d_sb->s_blocksize_bits;
  418. blockres = (1 << blockbits) - 1;
  419. buf->f_blocks = (res.tbytes + blockres) >> blockbits;
  420. buf->f_bfree = (res.fbytes + blockres) >> blockbits;
  421. buf->f_bavail = (res.abytes + blockres) >> blockbits;
  422. buf->f_files = res.tfiles;
  423. buf->f_ffree = res.afiles;
  424. buf->f_namelen = server->namelen;
  425. return 0;
  426. out_err:
  427. dprintk("%s: statfs error = %d\n", __func__, -error);
  428. return error;
  429. }
  430. /*
  431. * Map the security flavour number to a name
  432. */
  433. static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
  434. {
  435. static const struct {
  436. rpc_authflavor_t flavour;
  437. const char *str;
  438. } sec_flavours[] = {
  439. { RPC_AUTH_NULL, "null" },
  440. { RPC_AUTH_UNIX, "sys" },
  441. { RPC_AUTH_GSS_KRB5, "krb5" },
  442. { RPC_AUTH_GSS_KRB5I, "krb5i" },
  443. { RPC_AUTH_GSS_KRB5P, "krb5p" },
  444. { RPC_AUTH_GSS_LKEY, "lkey" },
  445. { RPC_AUTH_GSS_LKEYI, "lkeyi" },
  446. { RPC_AUTH_GSS_LKEYP, "lkeyp" },
  447. { RPC_AUTH_GSS_SPKM, "spkm" },
  448. { RPC_AUTH_GSS_SPKMI, "spkmi" },
  449. { RPC_AUTH_GSS_SPKMP, "spkmp" },
  450. { UINT_MAX, "unknown" }
  451. };
  452. int i;
  453. for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
  454. if (sec_flavours[i].flavour == flavour)
  455. break;
  456. }
  457. return sec_flavours[i].str;
  458. }
  459. static void nfs_show_mountd_netid(struct seq_file *m, struct nfs_server *nfss,
  460. int showdefaults)
  461. {
  462. struct sockaddr *sap = (struct sockaddr *) &nfss->mountd_address;
  463. seq_printf(m, ",mountproto=");
  464. switch (sap->sa_family) {
  465. case AF_INET:
  466. switch (nfss->mountd_protocol) {
  467. case IPPROTO_UDP:
  468. seq_printf(m, RPCBIND_NETID_UDP);
  469. break;
  470. case IPPROTO_TCP:
  471. seq_printf(m, RPCBIND_NETID_TCP);
  472. break;
  473. default:
  474. if (showdefaults)
  475. seq_printf(m, "auto");
  476. }
  477. break;
  478. case AF_INET6:
  479. switch (nfss->mountd_protocol) {
  480. case IPPROTO_UDP:
  481. seq_printf(m, RPCBIND_NETID_UDP6);
  482. break;
  483. case IPPROTO_TCP:
  484. seq_printf(m, RPCBIND_NETID_TCP6);
  485. break;
  486. default:
  487. if (showdefaults)
  488. seq_printf(m, "auto");
  489. }
  490. break;
  491. default:
  492. if (showdefaults)
  493. seq_printf(m, "auto");
  494. }
  495. }
  496. static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
  497. int showdefaults)
  498. {
  499. struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
  500. if (nfss->flags & NFS_MOUNT_LEGACY_INTERFACE)
  501. return;
  502. switch (sap->sa_family) {
  503. case AF_INET: {
  504. struct sockaddr_in *sin = (struct sockaddr_in *)sap;
  505. seq_printf(m, ",mountaddr=%pI4", &sin->sin_addr.s_addr);
  506. break;
  507. }
  508. case AF_INET6: {
  509. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
  510. seq_printf(m, ",mountaddr=%pI6c", &sin6->sin6_addr);
  511. break;
  512. }
  513. default:
  514. if (showdefaults)
  515. seq_printf(m, ",mountaddr=unspecified");
  516. }
  517. if (nfss->mountd_version || showdefaults)
  518. seq_printf(m, ",mountvers=%u", nfss->mountd_version);
  519. if (nfss->mountd_port || showdefaults)
  520. seq_printf(m, ",mountport=%u", nfss->mountd_port);
  521. nfs_show_mountd_netid(m, nfss, showdefaults);
  522. }
  523. #ifdef CONFIG_NFS_V4
  524. static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
  525. int showdefaults)
  526. {
  527. struct nfs_client *clp = nfss->nfs_client;
  528. seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
  529. seq_printf(m, ",minorversion=%u", clp->cl_minorversion);
  530. }
  531. #else
  532. static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
  533. int showdefaults)
  534. {
  535. }
  536. #endif
  537. /*
  538. * Describe the mount options in force on this server representation
  539. */
  540. static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
  541. int showdefaults)
  542. {
  543. static const struct proc_nfs_info {
  544. int flag;
  545. const char *str;
  546. const char *nostr;
  547. } nfs_info[] = {
  548. { NFS_MOUNT_SOFT, ",soft", ",hard" },
  549. { NFS_MOUNT_POSIX, ",posix", "" },
  550. { NFS_MOUNT_NOCTO, ",nocto", "" },
  551. { NFS_MOUNT_NOAC, ",noac", "" },
  552. { NFS_MOUNT_NONLM, ",nolock", "" },
  553. { NFS_MOUNT_NOACL, ",noacl", "" },
  554. { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
  555. { NFS_MOUNT_UNSHARED, ",nosharecache", "" },
  556. { NFS_MOUNT_NORESVPORT, ",noresvport", "" },
  557. { 0, NULL, NULL }
  558. };
  559. const struct proc_nfs_info *nfs_infop;
  560. struct nfs_client *clp = nfss->nfs_client;
  561. u32 version = clp->rpc_ops->version;
  562. int local_flock, local_fcntl;
  563. seq_printf(m, ",vers=%u", version);
  564. seq_printf(m, ",rsize=%u", nfss->rsize);
  565. seq_printf(m, ",wsize=%u", nfss->wsize);
  566. if (nfss->bsize != 0)
  567. seq_printf(m, ",bsize=%u", nfss->bsize);
  568. seq_printf(m, ",namlen=%u", nfss->namelen);
  569. if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
  570. seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
  571. if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
  572. seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
  573. if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
  574. seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
  575. if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
  576. seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
  577. for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
  578. if (nfss->flags & nfs_infop->flag)
  579. seq_puts(m, nfs_infop->str);
  580. else
  581. seq_puts(m, nfs_infop->nostr);
  582. }
  583. seq_printf(m, ",proto=%s",
  584. rpc_peeraddr2str(nfss->client, RPC_DISPLAY_NETID));
  585. if (version == 4) {
  586. if (nfss->port != NFS_PORT)
  587. seq_printf(m, ",port=%u", nfss->port);
  588. } else
  589. if (nfss->port)
  590. seq_printf(m, ",port=%u", nfss->port);
  591. seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
  592. seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
  593. seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
  594. if (version != 4)
  595. nfs_show_mountd_options(m, nfss, showdefaults);
  596. else
  597. nfs_show_nfsv4_options(m, nfss, showdefaults);
  598. if (nfss->options & NFS_OPTION_FSCACHE)
  599. seq_printf(m, ",fsc");
  600. if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) {
  601. if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
  602. seq_printf(m, ",lookupcache=none");
  603. else
  604. seq_printf(m, ",lookupcache=pos");
  605. }
  606. local_flock = nfss->flags & NFS_MOUNT_LOCAL_FLOCK;
  607. local_fcntl = nfss->flags & NFS_MOUNT_LOCAL_FCNTL;
  608. if (!local_flock && !local_fcntl)
  609. seq_printf(m, ",local_lock=none");
  610. else if (local_flock && local_fcntl)
  611. seq_printf(m, ",local_lock=all");
  612. else if (local_flock)
  613. seq_printf(m, ",local_lock=flock");
  614. else
  615. seq_printf(m, ",local_lock=posix");
  616. }
  617. /*
  618. * Describe the mount options on this VFS mountpoint
  619. */
  620. static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
  621. {
  622. struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
  623. nfs_show_mount_options(m, nfss, 0);
  624. seq_printf(m, ",addr=%s",
  625. rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
  626. RPC_DISPLAY_ADDR));
  627. return 0;
  628. }
  629. /*
  630. * Present statistical information for this VFS mountpoint
  631. */
  632. static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
  633. {
  634. int i, cpu;
  635. struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
  636. struct rpc_auth *auth = nfss->client->cl_auth;
  637. struct nfs_iostats totals = { };
  638. seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
  639. /*
  640. * Display all mount option settings
  641. */
  642. seq_printf(m, "\n\topts:\t");
  643. seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
  644. seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
  645. seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
  646. seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
  647. nfs_show_mount_options(m, nfss, 1);
  648. seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
  649. seq_printf(m, "\n\tcaps:\t");
  650. seq_printf(m, "caps=0x%x", nfss->caps);
  651. seq_printf(m, ",wtmult=%u", nfss->wtmult);
  652. seq_printf(m, ",dtsize=%u", nfss->dtsize);
  653. seq_printf(m, ",bsize=%u", nfss->bsize);
  654. seq_printf(m, ",namlen=%u", nfss->namelen);
  655. #ifdef CONFIG_NFS_V4
  656. if (nfss->nfs_client->rpc_ops->version == 4) {
  657. seq_printf(m, "\n\tnfsv4:\t");
  658. seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
  659. seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
  660. seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
  661. }
  662. #endif
  663. /*
  664. * Display security flavor in effect for this mount
  665. */
  666. seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
  667. if (auth->au_flavor)
  668. seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
  669. /*
  670. * Display superblock I/O counters
  671. */
  672. for_each_possible_cpu(cpu) {
  673. struct nfs_iostats *stats;
  674. preempt_disable();
  675. stats = per_cpu_ptr(nfss->io_stats, cpu);
  676. for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
  677. totals.events[i] += stats->events[i];
  678. for (i = 0; i < __NFSIOS_BYTESMAX; i++)
  679. totals.bytes[i] += stats->bytes[i];
  680. #ifdef CONFIG_NFS_FSCACHE
  681. for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
  682. totals.fscache[i] += stats->fscache[i];
  683. #endif
  684. preempt_enable();
  685. }
  686. seq_printf(m, "\n\tevents:\t");
  687. for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
  688. seq_printf(m, "%lu ", totals.events[i]);
  689. seq_printf(m, "\n\tbytes:\t");
  690. for (i = 0; i < __NFSIOS_BYTESMAX; i++)
  691. seq_printf(m, "%Lu ", totals.bytes[i]);
  692. #ifdef CONFIG_NFS_FSCACHE
  693. if (nfss->options & NFS_OPTION_FSCACHE) {
  694. seq_printf(m, "\n\tfsc:\t");
  695. for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
  696. seq_printf(m, "%Lu ", totals.bytes[i]);
  697. }
  698. #endif
  699. seq_printf(m, "\n");
  700. rpc_print_iostats(m, nfss->client);
  701. return 0;
  702. }
  703. /*
  704. * Begin unmount by attempting to remove all automounted mountpoints we added
  705. * in response to xdev traversals and referrals
  706. */
  707. static void nfs_umount_begin(struct super_block *sb)
  708. {
  709. struct nfs_server *server;
  710. struct rpc_clnt *rpc;
  711. server = NFS_SB(sb);
  712. /* -EIO all pending I/O */
  713. rpc = server->client_acl;
  714. if (!IS_ERR(rpc))
  715. rpc_killall_tasks(rpc);
  716. rpc = server->client;
  717. if (!IS_ERR(rpc))
  718. rpc_killall_tasks(rpc);
  719. }
  720. static struct nfs_parsed_mount_data *nfs_alloc_parsed_mount_data(unsigned int version)
  721. {
  722. struct nfs_parsed_mount_data *data;
  723. data = kzalloc(sizeof(*data), GFP_KERNEL);
  724. if (data) {
  725. data->acregmin = NFS_DEF_ACREGMIN;
  726. data->acregmax = NFS_DEF_ACREGMAX;
  727. data->acdirmin = NFS_DEF_ACDIRMIN;
  728. data->acdirmax = NFS_DEF_ACDIRMAX;
  729. data->mount_server.port = NFS_UNSPEC_PORT;
  730. data->nfs_server.port = NFS_UNSPEC_PORT;
  731. data->nfs_server.protocol = XPRT_TRANSPORT_TCP;
  732. data->auth_flavors[0] = RPC_AUTH_UNIX;
  733. data->auth_flavor_len = 1;
  734. data->version = version;
  735. data->minorversion = 0;
  736. }
  737. return data;
  738. }
  739. /*
  740. * Sanity-check a server address provided by the mount command.
  741. *
  742. * Address family must be initialized, and address must not be
  743. * the ANY address for that family.
  744. */
  745. static int nfs_verify_server_address(struct sockaddr *addr)
  746. {
  747. switch (addr->sa_family) {
  748. case AF_INET: {
  749. struct sockaddr_in *sa = (struct sockaddr_in *)addr;
  750. return sa->sin_addr.s_addr != htonl(INADDR_ANY);
  751. }
  752. case AF_INET6: {
  753. struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
  754. return !ipv6_addr_any(sa);
  755. }
  756. }
  757. dfprintk(MOUNT, "NFS: Invalid IP address specified\n");
  758. return 0;
  759. }
  760. /*
  761. * Select between a default port value and a user-specified port value.
  762. * If a zero value is set, then autobind will be used.
  763. */
  764. static void nfs_set_port(struct sockaddr *sap, int *port,
  765. const unsigned short default_port)
  766. {
  767. if (*port == NFS_UNSPEC_PORT)
  768. *port = default_port;
  769. rpc_set_port(sap, *port);
  770. }
  771. /*
  772. * Sanity check the NFS transport protocol.
  773. *
  774. */
  775. static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
  776. {
  777. switch (mnt->nfs_server.protocol) {
  778. case XPRT_TRANSPORT_UDP:
  779. case XPRT_TRANSPORT_TCP:
  780. case XPRT_TRANSPORT_RDMA:
  781. break;
  782. default:
  783. mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
  784. }
  785. }
  786. /*
  787. * For text based NFSv2/v3 mounts, the mount protocol transport default
  788. * settings should depend upon the specified NFS transport.
  789. */
  790. static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
  791. {
  792. nfs_validate_transport_protocol(mnt);
  793. if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
  794. mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
  795. return;
  796. switch (mnt->nfs_server.protocol) {
  797. case XPRT_TRANSPORT_UDP:
  798. mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
  799. break;
  800. case XPRT_TRANSPORT_TCP:
  801. case XPRT_TRANSPORT_RDMA:
  802. mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
  803. }
  804. }
  805. /*
  806. * Parse the value of the 'sec=' option.
  807. */
  808. static int nfs_parse_security_flavors(char *value,
  809. struct nfs_parsed_mount_data *mnt)
  810. {
  811. substring_t args[MAX_OPT_ARGS];
  812. dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
  813. switch (match_token(value, nfs_secflavor_tokens, args)) {
  814. case Opt_sec_none:
  815. mnt->auth_flavors[0] = RPC_AUTH_NULL;
  816. break;
  817. case Opt_sec_sys:
  818. mnt->auth_flavors[0] = RPC_AUTH_UNIX;
  819. break;
  820. case Opt_sec_krb5:
  821. mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
  822. break;
  823. case Opt_sec_krb5i:
  824. mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
  825. break;
  826. case Opt_sec_krb5p:
  827. mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
  828. break;
  829. case Opt_sec_lkey:
  830. mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
  831. break;
  832. case Opt_sec_lkeyi:
  833. mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
  834. break;
  835. case Opt_sec_lkeyp:
  836. mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
  837. break;
  838. case Opt_sec_spkm:
  839. mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
  840. break;
  841. case Opt_sec_spkmi:
  842. mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
  843. break;
  844. case Opt_sec_spkmp:
  845. mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
  846. break;
  847. default:
  848. return 0;
  849. }
  850. mnt->auth_flavor_len = 1;
  851. return 1;
  852. }
  853. /*
  854. * Error-check and convert a string of mount options from user space into
  855. * a data structure. The whole mount string is processed; bad options are
  856. * skipped as they are encountered. If there were no errors, return 1;
  857. * otherwise return 0 (zero).
  858. */
  859. static int nfs_parse_mount_options(char *raw,
  860. struct nfs_parsed_mount_data *mnt)
  861. {
  862. char *p, *string, *secdata;
  863. int rc, sloppy = 0, invalid_option = 0;
  864. unsigned short protofamily = AF_UNSPEC;
  865. unsigned short mountfamily = AF_UNSPEC;
  866. if (!raw) {
  867. dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
  868. return 1;
  869. }
  870. dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
  871. secdata = alloc_secdata();
  872. if (!secdata)
  873. goto out_nomem;
  874. rc = security_sb_copy_data(raw, secdata);
  875. if (rc)
  876. goto out_security_failure;
  877. rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
  878. if (rc)
  879. goto out_security_failure;
  880. free_secdata(secdata);
  881. while ((p = strsep(&raw, ",")) != NULL) {
  882. substring_t args[MAX_OPT_ARGS];
  883. unsigned long option;
  884. int token;
  885. if (!*p)
  886. continue;
  887. dfprintk(MOUNT, "NFS: parsing nfs mount option '%s'\n", p);
  888. token = match_token(p, nfs_mount_option_tokens, args);
  889. switch (token) {
  890. /*
  891. * boolean options: foo/nofoo
  892. */
  893. case Opt_soft:
  894. mnt->flags |= NFS_MOUNT_SOFT;
  895. break;
  896. case Opt_hard:
  897. mnt->flags &= ~NFS_MOUNT_SOFT;
  898. break;
  899. case Opt_posix:
  900. mnt->flags |= NFS_MOUNT_POSIX;
  901. break;
  902. case Opt_noposix:
  903. mnt->flags &= ~NFS_MOUNT_POSIX;
  904. break;
  905. case Opt_cto:
  906. mnt->flags &= ~NFS_MOUNT_NOCTO;
  907. break;
  908. case Opt_nocto:
  909. mnt->flags |= NFS_MOUNT_NOCTO;
  910. break;
  911. case Opt_ac:
  912. mnt->flags &= ~NFS_MOUNT_NOAC;
  913. break;
  914. case Opt_noac:
  915. mnt->flags |= NFS_MOUNT_NOAC;
  916. break;
  917. case Opt_lock:
  918. mnt->flags &= ~NFS_MOUNT_NONLM;
  919. mnt->flags &= ~(NFS_MOUNT_LOCAL_FLOCK |
  920. NFS_MOUNT_LOCAL_FCNTL);
  921. break;
  922. case Opt_nolock:
  923. mnt->flags |= NFS_MOUNT_NONLM;
  924. mnt->flags |= (NFS_MOUNT_LOCAL_FLOCK |
  925. NFS_MOUNT_LOCAL_FCNTL);
  926. break;
  927. case Opt_v2:
  928. mnt->flags &= ~NFS_MOUNT_VER3;
  929. mnt->version = 2;
  930. break;
  931. case Opt_v3:
  932. mnt->flags |= NFS_MOUNT_VER3;
  933. mnt->version = 3;
  934. break;
  935. #ifdef CONFIG_NFS_V4
  936. case Opt_v4:
  937. mnt->flags &= ~NFS_MOUNT_VER3;
  938. mnt->version = 4;
  939. break;
  940. #endif
  941. case Opt_udp:
  942. mnt->flags &= ~NFS_MOUNT_TCP;
  943. mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
  944. break;
  945. case Opt_tcp:
  946. mnt->flags |= NFS_MOUNT_TCP;
  947. mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
  948. break;
  949. case Opt_rdma:
  950. mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
  951. mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
  952. xprt_load_transport(p);
  953. break;
  954. case Opt_acl:
  955. mnt->flags &= ~NFS_MOUNT_NOACL;
  956. break;
  957. case Opt_noacl:
  958. mnt->flags |= NFS_MOUNT_NOACL;
  959. break;
  960. case Opt_rdirplus:
  961. mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
  962. break;
  963. case Opt_nordirplus:
  964. mnt->flags |= NFS_MOUNT_NORDIRPLUS;
  965. break;
  966. case Opt_sharecache:
  967. mnt->flags &= ~NFS_MOUNT_UNSHARED;
  968. break;
  969. case Opt_nosharecache:
  970. mnt->flags |= NFS_MOUNT_UNSHARED;
  971. break;
  972. case Opt_resvport:
  973. mnt->flags &= ~NFS_MOUNT_NORESVPORT;
  974. break;
  975. case Opt_noresvport:
  976. mnt->flags |= NFS_MOUNT_NORESVPORT;
  977. break;
  978. case Opt_fscache:
  979. mnt->options |= NFS_OPTION_FSCACHE;
  980. kfree(mnt->fscache_uniq);
  981. mnt->fscache_uniq = NULL;
  982. break;
  983. case Opt_nofscache:
  984. mnt->options &= ~NFS_OPTION_FSCACHE;
  985. kfree(mnt->fscache_uniq);
  986. mnt->fscache_uniq = NULL;
  987. break;
  988. /*
  989. * options that take numeric values
  990. */
  991. case Opt_port:
  992. string = match_strdup(args);
  993. if (string == NULL)
  994. goto out_nomem;
  995. rc = strict_strtoul(string, 10, &option);
  996. kfree(string);
  997. if (rc != 0 || option > USHRT_MAX)
  998. goto out_invalid_value;
  999. mnt->nfs_server.port = option;
  1000. break;
  1001. case Opt_rsize:
  1002. string = match_strdup(args);
  1003. if (string == NULL)
  1004. goto out_nomem;
  1005. rc = strict_strtoul(string, 10, &option);
  1006. kfree(string);
  1007. if (rc != 0)
  1008. goto out_invalid_value;
  1009. mnt->rsize = option;
  1010. break;
  1011. case Opt_wsize:
  1012. string = match_strdup(args);
  1013. if (string == NULL)
  1014. goto out_nomem;
  1015. rc = strict_strtoul(string, 10, &option);
  1016. kfree(string);
  1017. if (rc != 0)
  1018. goto out_invalid_value;
  1019. mnt->wsize = option;
  1020. break;
  1021. case Opt_bsize:
  1022. string = match_strdup(args);
  1023. if (string == NULL)
  1024. goto out_nomem;
  1025. rc = strict_strtoul(string, 10, &option);
  1026. kfree(string);
  1027. if (rc != 0)
  1028. goto out_invalid_value;
  1029. mnt->bsize = option;
  1030. break;
  1031. case Opt_timeo:
  1032. string = match_strdup(args);
  1033. if (string == NULL)
  1034. goto out_nomem;
  1035. rc = strict_strtoul(string, 10, &option);
  1036. kfree(string);
  1037. if (rc != 0 || option == 0)
  1038. goto out_invalid_value;
  1039. mnt->timeo = option;
  1040. break;
  1041. case Opt_retrans:
  1042. string = match_strdup(args);
  1043. if (string == NULL)
  1044. goto out_nomem;
  1045. rc = strict_strtoul(string, 10, &option);
  1046. kfree(string);
  1047. if (rc != 0 || option == 0)
  1048. goto out_invalid_value;
  1049. mnt->retrans = option;
  1050. break;
  1051. case Opt_acregmin:
  1052. string = match_strdup(args);
  1053. if (string == NULL)
  1054. goto out_nomem;
  1055. rc = strict_strtoul(string, 10, &option);
  1056. kfree(string);
  1057. if (rc != 0)
  1058. goto out_invalid_value;
  1059. mnt->acregmin = option;
  1060. break;
  1061. case Opt_acregmax:
  1062. string = match_strdup(args);
  1063. if (string == NULL)
  1064. goto out_nomem;
  1065. rc = strict_strtoul(string, 10, &option);
  1066. kfree(string);
  1067. if (rc != 0)
  1068. goto out_invalid_value;
  1069. mnt->acregmax = option;
  1070. break;
  1071. case Opt_acdirmin:
  1072. string = match_strdup(args);
  1073. if (string == NULL)
  1074. goto out_nomem;
  1075. rc = strict_strtoul(string, 10, &option);
  1076. kfree(string);
  1077. if (rc != 0)
  1078. goto out_invalid_value;
  1079. mnt->acdirmin = option;
  1080. break;
  1081. case Opt_acdirmax:
  1082. string = match_strdup(args);
  1083. if (string == NULL)
  1084. goto out_nomem;
  1085. rc = strict_strtoul(string, 10, &option);
  1086. kfree(string);
  1087. if (rc != 0)
  1088. goto out_invalid_value;
  1089. mnt->acdirmax = option;
  1090. break;
  1091. case Opt_actimeo:
  1092. string = match_strdup(args);
  1093. if (string == NULL)
  1094. goto out_nomem;
  1095. rc = strict_strtoul(string, 10, &option);
  1096. kfree(string);
  1097. if (rc != 0)
  1098. goto out_invalid_value;
  1099. mnt->acregmin = mnt->acregmax =
  1100. mnt->acdirmin = mnt->acdirmax = option;
  1101. break;
  1102. case Opt_namelen:
  1103. string = match_strdup(args);
  1104. if (string == NULL)
  1105. goto out_nomem;
  1106. rc = strict_strtoul(string, 10, &option);
  1107. kfree(string);
  1108. if (rc != 0)
  1109. goto out_invalid_value;
  1110. mnt->namlen = option;
  1111. break;
  1112. case Opt_mountport:
  1113. string = match_strdup(args);
  1114. if (string == NULL)
  1115. goto out_nomem;
  1116. rc = strict_strtoul(string, 10, &option);
  1117. kfree(string);
  1118. if (rc != 0 || option > USHRT_MAX)
  1119. goto out_invalid_value;
  1120. mnt->mount_server.port = option;
  1121. break;
  1122. case Opt_mountvers:
  1123. string = match_strdup(args);
  1124. if (string == NULL)
  1125. goto out_nomem;
  1126. rc = strict_strtoul(string, 10, &option);
  1127. kfree(string);
  1128. if (rc != 0 ||
  1129. option < NFS_MNT_VERSION ||
  1130. option > NFS_MNT3_VERSION)
  1131. goto out_invalid_value;
  1132. mnt->mount_server.version = option;
  1133. break;
  1134. case Opt_nfsvers:
  1135. string = match_strdup(args);
  1136. if (string == NULL)
  1137. goto out_nomem;
  1138. rc = strict_strtoul(string, 10, &option);
  1139. kfree(string);
  1140. if (rc != 0)
  1141. goto out_invalid_value;
  1142. switch (option) {
  1143. case NFS2_VERSION:
  1144. mnt->flags &= ~NFS_MOUNT_VER3;
  1145. mnt->version = 2;
  1146. break;
  1147. case NFS3_VERSION:
  1148. mnt->flags |= NFS_MOUNT_VER3;
  1149. mnt->version = 3;
  1150. break;
  1151. #ifdef CONFIG_NFS_V4
  1152. case NFS4_VERSION:
  1153. mnt->flags &= ~NFS_MOUNT_VER3;
  1154. mnt->version = 4;
  1155. break;
  1156. #endif
  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(3);
  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 int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
  2137. const char *dev_name, void *raw_data,
  2138. struct vfsmount *mnt)
  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_get_sb()\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. mnt->mnt_sb = s;
  2190. mnt->mnt_root = mntroot;
  2191. /* clone any lsm security options from the parent to the new sb */
  2192. security_sb_clone_mnt_opts(data->sb, s);
  2193. dprintk("<-- nfs_xdev_get_sb() = 0\n");
  2194. return 0;
  2195. out_err_nosb:
  2196. nfs_free_server(server);
  2197. out_err_noserver:
  2198. dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
  2199. return error;
  2200. error_splat_super:
  2201. if (server && !s->s_root)
  2202. bdi_unregister(&server->backing_dev_info);
  2203. error_splat_bdi:
  2204. deactivate_locked_super(s);
  2205. dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
  2206. return error;
  2207. }
  2208. #ifdef CONFIG_NFS_V4
  2209. /*
  2210. * Finish setting up a cloned NFS4 superblock
  2211. */
  2212. static void nfs4_clone_super(struct super_block *sb,
  2213. const struct super_block *old_sb)
  2214. {
  2215. sb->s_blocksize_bits = old_sb->s_blocksize_bits;
  2216. sb->s_blocksize = old_sb->s_blocksize;
  2217. sb->s_maxbytes = old_sb->s_maxbytes;
  2218. sb->s_time_gran = 1;
  2219. sb->s_op = old_sb->s_op;
  2220. nfs_initialise_sb(sb);
  2221. }
  2222. /*
  2223. * Set up an NFS4 superblock
  2224. */
  2225. static void nfs4_fill_super(struct super_block *sb)
  2226. {
  2227. sb->s_time_gran = 1;
  2228. sb->s_op = &nfs4_sops;
  2229. nfs_initialise_sb(sb);
  2230. }
  2231. static void nfs4_validate_mount_flags(struct nfs_parsed_mount_data *args)
  2232. {
  2233. args->flags &= ~(NFS_MOUNT_NONLM|NFS_MOUNT_NOACL|NFS_MOUNT_VER3|
  2234. NFS_MOUNT_LOCAL_FLOCK|NFS_MOUNT_LOCAL_FCNTL);
  2235. }
  2236. static int nfs4_validate_text_mount_data(void *options,
  2237. struct nfs_parsed_mount_data *args,
  2238. const char *dev_name)
  2239. {
  2240. struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
  2241. nfs_set_port(sap, &args->nfs_server.port, NFS_PORT);
  2242. nfs_validate_transport_protocol(args);
  2243. nfs4_validate_mount_flags(args);
  2244. if (args->version != 4) {
  2245. dfprintk(MOUNT,
  2246. "NFS4: Illegal mount version\n");
  2247. return -EINVAL;
  2248. }
  2249. if (args->auth_flavor_len > 1) {
  2250. dfprintk(MOUNT,
  2251. "NFS4: Too many RPC auth flavours specified\n");
  2252. return -EINVAL;
  2253. }
  2254. if (args->client_address == NULL) {
  2255. dfprintk(MOUNT,
  2256. "NFS4: mount program didn't pass callback address\n");
  2257. return -EINVAL;
  2258. }
  2259. return nfs_parse_devname(dev_name,
  2260. &args->nfs_server.hostname,
  2261. NFS4_MAXNAMLEN,
  2262. &args->nfs_server.export_path,
  2263. NFS4_MAXPATHLEN);
  2264. }
  2265. /*
  2266. * Validate NFSv4 mount options
  2267. */
  2268. static int nfs4_validate_mount_data(void *options,
  2269. struct nfs_parsed_mount_data *args,
  2270. const char *dev_name)
  2271. {
  2272. struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
  2273. struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
  2274. char *c;
  2275. if (data == NULL)
  2276. goto out_no_data;
  2277. switch (data->version) {
  2278. case 1:
  2279. if (data->host_addrlen > sizeof(args->nfs_server.address))
  2280. goto out_no_address;
  2281. if (data->host_addrlen == 0)
  2282. goto out_no_address;
  2283. args->nfs_server.addrlen = data->host_addrlen;
  2284. if (copy_from_user(sap, data->host_addr, data->host_addrlen))
  2285. return -EFAULT;
  2286. if (!nfs_verify_server_address(sap))
  2287. goto out_no_address;
  2288. if (data->auth_flavourlen) {
  2289. if (data->auth_flavourlen > 1)
  2290. goto out_inval_auth;
  2291. if (copy_from_user(&args->auth_flavors[0],
  2292. data->auth_flavours,
  2293. sizeof(args->auth_flavors[0])))
  2294. return -EFAULT;
  2295. }
  2296. c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
  2297. if (IS_ERR(c))
  2298. return PTR_ERR(c);
  2299. args->nfs_server.hostname = c;
  2300. c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
  2301. if (IS_ERR(c))
  2302. return PTR_ERR(c);
  2303. args->nfs_server.export_path = c;
  2304. dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
  2305. c = strndup_user(data->client_addr.data, 16);
  2306. if (IS_ERR(c))
  2307. return PTR_ERR(c);
  2308. args->client_address = c;
  2309. /*
  2310. * Translate to nfs_parsed_mount_data, which nfs4_fill_super
  2311. * can deal with.
  2312. */
  2313. args->flags = data->flags & NFS4_MOUNT_FLAGMASK;
  2314. args->rsize = data->rsize;
  2315. args->wsize = data->wsize;
  2316. args->timeo = data->timeo;
  2317. args->retrans = data->retrans;
  2318. args->acregmin = data->acregmin;
  2319. args->acregmax = data->acregmax;
  2320. args->acdirmin = data->acdirmin;
  2321. args->acdirmax = data->acdirmax;
  2322. args->nfs_server.protocol = data->proto;
  2323. nfs_validate_transport_protocol(args);
  2324. break;
  2325. default:
  2326. if (nfs_parse_mount_options((char *)options, args) == 0)
  2327. return -EINVAL;
  2328. if (!nfs_verify_server_address(sap))
  2329. return -EINVAL;
  2330. return nfs4_validate_text_mount_data(options, args, dev_name);
  2331. }
  2332. return 0;
  2333. out_no_data:
  2334. dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
  2335. return -EINVAL;
  2336. out_inval_auth:
  2337. dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
  2338. data->auth_flavourlen);
  2339. return -EINVAL;
  2340. out_no_address:
  2341. dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
  2342. return -EINVAL;
  2343. }
  2344. /*
  2345. * Get the superblock for the NFS4 root partition
  2346. */
  2347. static int nfs4_remote_get_sb(struct file_system_type *fs_type,
  2348. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
  2349. {
  2350. struct nfs_parsed_mount_data *data = raw_data;
  2351. struct super_block *s;
  2352. struct nfs_server *server;
  2353. struct nfs_fh *mntfh;
  2354. struct dentry *mntroot;
  2355. int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
  2356. struct nfs_sb_mountdata sb_mntdata = {
  2357. .mntflags = flags,
  2358. };
  2359. int error = -ENOMEM;
  2360. mntfh = nfs_alloc_fhandle();
  2361. if (data == NULL || mntfh == NULL)
  2362. goto out_free_fh;
  2363. security_init_mnt_opts(&data->lsm_opts);
  2364. /* Get a volume representation */
  2365. server = nfs4_create_server(data, mntfh);
  2366. if (IS_ERR(server)) {
  2367. error = PTR_ERR(server);
  2368. goto out;
  2369. }
  2370. sb_mntdata.server = server;
  2371. if (server->flags & NFS4_MOUNT_UNSHARED)
  2372. compare_super = NULL;
  2373. /* Get a superblock - note that we may end up sharing one that already exists */
  2374. s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
  2375. if (IS_ERR(s)) {
  2376. error = PTR_ERR(s);
  2377. goto out_free;
  2378. }
  2379. if (s->s_fs_info != server) {
  2380. nfs_free_server(server);
  2381. server = NULL;
  2382. } else {
  2383. error = nfs_bdi_register(server);
  2384. if (error)
  2385. goto error_splat_bdi;
  2386. }
  2387. if (!s->s_root) {
  2388. /* initial superblock/root creation */
  2389. nfs4_fill_super(s);
  2390. nfs_fscache_get_super_cookie(
  2391. s, data ? data->fscache_uniq : NULL, NULL);
  2392. }
  2393. mntroot = nfs4_get_root(s, mntfh);
  2394. if (IS_ERR(mntroot)) {
  2395. error = PTR_ERR(mntroot);
  2396. goto error_splat_super;
  2397. }
  2398. error = security_sb_set_mnt_opts(s, &data->lsm_opts);
  2399. if (error)
  2400. goto error_splat_root;
  2401. s->s_flags |= MS_ACTIVE;
  2402. mnt->mnt_sb = s;
  2403. mnt->mnt_root = mntroot;
  2404. error = 0;
  2405. out:
  2406. security_free_mnt_opts(&data->lsm_opts);
  2407. out_free_fh:
  2408. nfs_free_fhandle(mntfh);
  2409. return error;
  2410. out_free:
  2411. nfs_free_server(server);
  2412. goto out;
  2413. error_splat_root:
  2414. dput(mntroot);
  2415. error_splat_super:
  2416. if (server && !s->s_root)
  2417. bdi_unregister(&server->backing_dev_info);
  2418. error_splat_bdi:
  2419. deactivate_locked_super(s);
  2420. goto out;
  2421. }
  2422. static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type,
  2423. int flags, void *data, const char *hostname)
  2424. {
  2425. struct vfsmount *root_mnt;
  2426. char *root_devname;
  2427. size_t len;
  2428. len = strlen(hostname) + 3;
  2429. root_devname = kmalloc(len, GFP_KERNEL);
  2430. if (root_devname == NULL)
  2431. return ERR_PTR(-ENOMEM);
  2432. snprintf(root_devname, len, "%s:/", hostname);
  2433. root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data);
  2434. kfree(root_devname);
  2435. return root_mnt;
  2436. }
  2437. static void nfs_fix_devname(const struct path *path, struct vfsmount *mnt)
  2438. {
  2439. char *page = (char *) __get_free_page(GFP_KERNEL);
  2440. char *devname, *tmp;
  2441. if (page == NULL)
  2442. return;
  2443. devname = nfs_path(path->mnt->mnt_devname,
  2444. path->mnt->mnt_root, path->dentry,
  2445. page, PAGE_SIZE);
  2446. if (IS_ERR(devname))
  2447. goto out_freepage;
  2448. tmp = kstrdup(devname, GFP_KERNEL);
  2449. if (tmp == NULL)
  2450. goto out_freepage;
  2451. kfree(mnt->mnt_devname);
  2452. mnt->mnt_devname = tmp;
  2453. out_freepage:
  2454. free_page((unsigned long)page);
  2455. }
  2456. struct nfs_referral_count {
  2457. struct list_head list;
  2458. const struct task_struct *task;
  2459. unsigned int referral_count;
  2460. };
  2461. static LIST_HEAD(nfs_referral_count_list);
  2462. static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
  2463. static struct nfs_referral_count *nfs_find_referral_count(void)
  2464. {
  2465. struct nfs_referral_count *p;
  2466. list_for_each_entry(p, &nfs_referral_count_list, list) {
  2467. if (p->task == current)
  2468. return p;
  2469. }
  2470. return NULL;
  2471. }
  2472. #define NFS_MAX_NESTED_REFERRALS 2
  2473. static int nfs_referral_loop_protect(void)
  2474. {
  2475. struct nfs_referral_count *p, *new;
  2476. int ret = -ENOMEM;
  2477. new = kmalloc(sizeof(*new), GFP_KERNEL);
  2478. if (!new)
  2479. goto out;
  2480. new->task = current;
  2481. new->referral_count = 1;
  2482. ret = 0;
  2483. spin_lock(&nfs_referral_count_list_lock);
  2484. p = nfs_find_referral_count();
  2485. if (p != NULL) {
  2486. if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
  2487. ret = -ELOOP;
  2488. else
  2489. p->referral_count++;
  2490. } else {
  2491. list_add(&new->list, &nfs_referral_count_list);
  2492. new = NULL;
  2493. }
  2494. spin_unlock(&nfs_referral_count_list_lock);
  2495. kfree(new);
  2496. out:
  2497. return ret;
  2498. }
  2499. static void nfs_referral_loop_unprotect(void)
  2500. {
  2501. struct nfs_referral_count *p;
  2502. spin_lock(&nfs_referral_count_list_lock);
  2503. p = nfs_find_referral_count();
  2504. p->referral_count--;
  2505. if (p->referral_count == 0)
  2506. list_del(&p->list);
  2507. else
  2508. p = NULL;
  2509. spin_unlock(&nfs_referral_count_list_lock);
  2510. kfree(p);
  2511. }
  2512. static int nfs_follow_remote_path(struct vfsmount *root_mnt,
  2513. const char *export_path, struct vfsmount *mnt_target)
  2514. {
  2515. struct nameidata *nd = NULL;
  2516. struct mnt_namespace *ns_private;
  2517. struct super_block *s;
  2518. int ret;
  2519. nd = kmalloc(sizeof(*nd), GFP_KERNEL);
  2520. if (nd == NULL)
  2521. return -ENOMEM;
  2522. ns_private = create_mnt_ns(root_mnt);
  2523. ret = PTR_ERR(ns_private);
  2524. if (IS_ERR(ns_private))
  2525. goto out_mntput;
  2526. ret = nfs_referral_loop_protect();
  2527. if (ret != 0)
  2528. goto out_put_mnt_ns;
  2529. ret = vfs_path_lookup(root_mnt->mnt_root, root_mnt,
  2530. export_path, LOOKUP_FOLLOW, nd);
  2531. nfs_referral_loop_unprotect();
  2532. put_mnt_ns(ns_private);
  2533. if (ret != 0)
  2534. goto out_err;
  2535. s = nd->path.mnt->mnt_sb;
  2536. atomic_inc(&s->s_active);
  2537. mnt_target->mnt_sb = s;
  2538. mnt_target->mnt_root = dget(nd->path.dentry);
  2539. /* Correct the device pathname */
  2540. nfs_fix_devname(&nd->path, mnt_target);
  2541. path_put(&nd->path);
  2542. kfree(nd);
  2543. down_write(&s->s_umount);
  2544. return 0;
  2545. out_put_mnt_ns:
  2546. put_mnt_ns(ns_private);
  2547. out_mntput:
  2548. mntput(root_mnt);
  2549. out_err:
  2550. kfree(nd);
  2551. return ret;
  2552. }
  2553. static int nfs4_try_mount(int flags, const char *dev_name,
  2554. struct nfs_parsed_mount_data *data,
  2555. struct vfsmount *mnt)
  2556. {
  2557. char *export_path;
  2558. struct vfsmount *root_mnt;
  2559. int error;
  2560. dfprintk(MOUNT, "--> nfs4_try_mount()\n");
  2561. export_path = data->nfs_server.export_path;
  2562. data->nfs_server.export_path = "/";
  2563. root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, data,
  2564. data->nfs_server.hostname);
  2565. data->nfs_server.export_path = export_path;
  2566. error = PTR_ERR(root_mnt);
  2567. if (IS_ERR(root_mnt))
  2568. goto out;
  2569. error = nfs_follow_remote_path(root_mnt, export_path, mnt);
  2570. out:
  2571. dfprintk(MOUNT, "<-- nfs4_try_mount() = %d%s\n", error,
  2572. error != 0 ? " [error]" : "");
  2573. return error;
  2574. }
  2575. /*
  2576. * Get the superblock for an NFS4 mountpoint
  2577. */
  2578. static int nfs4_get_sb(struct file_system_type *fs_type,
  2579. int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
  2580. {
  2581. struct nfs_parsed_mount_data *data;
  2582. int error = -ENOMEM;
  2583. data = nfs_alloc_parsed_mount_data(4);
  2584. if (data == NULL)
  2585. goto out_free_data;
  2586. /* Validate the mount data */
  2587. error = nfs4_validate_mount_data(raw_data, data, dev_name);
  2588. if (error < 0)
  2589. goto out;
  2590. error = nfs4_try_mount(flags, dev_name, data, mnt);
  2591. out:
  2592. kfree(data->client_address);
  2593. kfree(data->nfs_server.export_path);
  2594. kfree(data->nfs_server.hostname);
  2595. kfree(data->fscache_uniq);
  2596. out_free_data:
  2597. kfree(data);
  2598. dprintk("<-- nfs4_get_sb() = %d%s\n", error,
  2599. error != 0 ? " [error]" : "");
  2600. return error;
  2601. }
  2602. static void nfs4_kill_super(struct super_block *sb)
  2603. {
  2604. struct nfs_server *server = NFS_SB(sb);
  2605. dprintk("--> %s\n", __func__);
  2606. nfs_super_return_all_delegations(sb);
  2607. kill_anon_super(sb);
  2608. nfs_fscache_release_super_cookie(sb);
  2609. nfs_free_server(server);
  2610. dprintk("<-- %s\n", __func__);
  2611. }
  2612. /*
  2613. * Clone an NFS4 server record on xdev traversal (FSID-change)
  2614. */
  2615. static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
  2616. const char *dev_name, void *raw_data,
  2617. struct vfsmount *mnt)
  2618. {
  2619. struct nfs_clone_mount *data = raw_data;
  2620. struct super_block *s;
  2621. struct nfs_server *server;
  2622. struct dentry *mntroot;
  2623. int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
  2624. struct nfs_sb_mountdata sb_mntdata = {
  2625. .mntflags = flags,
  2626. };
  2627. int error;
  2628. dprintk("--> nfs4_xdev_get_sb()\n");
  2629. /* create a new volume representation */
  2630. server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
  2631. if (IS_ERR(server)) {
  2632. error = PTR_ERR(server);
  2633. goto out_err_noserver;
  2634. }
  2635. sb_mntdata.server = server;
  2636. if (server->flags & NFS4_MOUNT_UNSHARED)
  2637. compare_super = NULL;
  2638. /* Get a superblock - note that we may end up sharing one that already exists */
  2639. s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
  2640. if (IS_ERR(s)) {
  2641. error = PTR_ERR(s);
  2642. goto out_err_nosb;
  2643. }
  2644. if (s->s_fs_info != server) {
  2645. nfs_free_server(server);
  2646. server = NULL;
  2647. } else {
  2648. error = nfs_bdi_register(server);
  2649. if (error)
  2650. goto error_splat_bdi;
  2651. }
  2652. if (!s->s_root) {
  2653. /* initial superblock/root creation */
  2654. nfs4_clone_super(s, data->sb);
  2655. nfs_fscache_get_super_cookie(s, NULL, data);
  2656. }
  2657. mntroot = nfs4_get_root(s, data->fh);
  2658. if (IS_ERR(mntroot)) {
  2659. error = PTR_ERR(mntroot);
  2660. goto error_splat_super;
  2661. }
  2662. if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
  2663. dput(mntroot);
  2664. error = -ESTALE;
  2665. goto error_splat_super;
  2666. }
  2667. s->s_flags |= MS_ACTIVE;
  2668. mnt->mnt_sb = s;
  2669. mnt->mnt_root = mntroot;
  2670. security_sb_clone_mnt_opts(data->sb, s);
  2671. dprintk("<-- nfs4_xdev_get_sb() = 0\n");
  2672. return 0;
  2673. out_err_nosb:
  2674. nfs_free_server(server);
  2675. out_err_noserver:
  2676. dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
  2677. return error;
  2678. error_splat_super:
  2679. if (server && !s->s_root)
  2680. bdi_unregister(&server->backing_dev_info);
  2681. error_splat_bdi:
  2682. deactivate_locked_super(s);
  2683. dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
  2684. return error;
  2685. }
  2686. static int nfs4_remote_referral_get_sb(struct file_system_type *fs_type,
  2687. int flags, const char *dev_name, void *raw_data,
  2688. struct vfsmount *mnt)
  2689. {
  2690. struct nfs_clone_mount *data = raw_data;
  2691. struct super_block *s;
  2692. struct nfs_server *server;
  2693. struct dentry *mntroot;
  2694. struct nfs_fh *mntfh;
  2695. int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
  2696. struct nfs_sb_mountdata sb_mntdata = {
  2697. .mntflags = flags,
  2698. };
  2699. int error = -ENOMEM;
  2700. dprintk("--> nfs4_referral_get_sb()\n");
  2701. mntfh = nfs_alloc_fhandle();
  2702. if (mntfh == NULL)
  2703. goto out_err_nofh;
  2704. /* create a new volume representation */
  2705. server = nfs4_create_referral_server(data, mntfh);
  2706. if (IS_ERR(server)) {
  2707. error = PTR_ERR(server);
  2708. goto out_err_noserver;
  2709. }
  2710. sb_mntdata.server = server;
  2711. if (server->flags & NFS4_MOUNT_UNSHARED)
  2712. compare_super = NULL;
  2713. /* Get a superblock - note that we may end up sharing one that already exists */
  2714. s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
  2715. if (IS_ERR(s)) {
  2716. error = PTR_ERR(s);
  2717. goto out_err_nosb;
  2718. }
  2719. if (s->s_fs_info != server) {
  2720. nfs_free_server(server);
  2721. server = NULL;
  2722. } else {
  2723. error = nfs_bdi_register(server);
  2724. if (error)
  2725. goto error_splat_bdi;
  2726. }
  2727. if (!s->s_root) {
  2728. /* initial superblock/root creation */
  2729. nfs4_fill_super(s);
  2730. nfs_fscache_get_super_cookie(s, NULL, data);
  2731. }
  2732. mntroot = nfs4_get_root(s, mntfh);
  2733. if (IS_ERR(mntroot)) {
  2734. error = PTR_ERR(mntroot);
  2735. goto error_splat_super;
  2736. }
  2737. if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
  2738. dput(mntroot);
  2739. error = -ESTALE;
  2740. goto error_splat_super;
  2741. }
  2742. s->s_flags |= MS_ACTIVE;
  2743. mnt->mnt_sb = s;
  2744. mnt->mnt_root = mntroot;
  2745. security_sb_clone_mnt_opts(data->sb, s);
  2746. nfs_free_fhandle(mntfh);
  2747. dprintk("<-- nfs4_referral_get_sb() = 0\n");
  2748. return 0;
  2749. out_err_nosb:
  2750. nfs_free_server(server);
  2751. out_err_noserver:
  2752. nfs_free_fhandle(mntfh);
  2753. out_err_nofh:
  2754. dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
  2755. return error;
  2756. error_splat_super:
  2757. if (server && !s->s_root)
  2758. bdi_unregister(&server->backing_dev_info);
  2759. error_splat_bdi:
  2760. deactivate_locked_super(s);
  2761. nfs_free_fhandle(mntfh);
  2762. dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
  2763. return error;
  2764. }
  2765. /*
  2766. * Create an NFS4 server record on referral traversal
  2767. */
  2768. static int nfs4_referral_get_sb(struct file_system_type *fs_type,
  2769. int flags, const char *dev_name, void *raw_data,
  2770. struct vfsmount *mnt)
  2771. {
  2772. struct nfs_clone_mount *data = raw_data;
  2773. char *export_path;
  2774. struct vfsmount *root_mnt;
  2775. int error;
  2776. dprintk("--> nfs4_referral_get_sb()\n");
  2777. export_path = data->mnt_path;
  2778. data->mnt_path = "/";
  2779. root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
  2780. flags, data, data->hostname);
  2781. data->mnt_path = export_path;
  2782. error = PTR_ERR(root_mnt);
  2783. if (IS_ERR(root_mnt))
  2784. goto out;
  2785. error = nfs_follow_remote_path(root_mnt, export_path, mnt);
  2786. out:
  2787. dprintk("<-- nfs4_referral_get_sb() = %d%s\n", error,
  2788. error != 0 ? " [error]" : "");
  2789. return error;
  2790. }
  2791. #endif /* CONFIG_NFS_V4 */