super.c 73 KB

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