super.c 74 KB

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