super.c 66 KB

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