super.c 72 KB

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