super.c 70 KB

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