namespace.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041
  1. /*
  2. * linux/fs/namespace.c
  3. *
  4. * (C) Copyright Al Viro 2000, 2001
  5. * Released under GPL v2.
  6. *
  7. * Based on code from fs/super.c, copyright Linus Torvalds and others.
  8. * Heavily rewritten.
  9. */
  10. #include <linux/syscalls.h>
  11. #include <linux/export.h>
  12. #include <linux/capability.h>
  13. #include <linux/mnt_namespace.h>
  14. #include <linux/user_namespace.h>
  15. #include <linux/namei.h>
  16. #include <linux/security.h>
  17. #include <linux/idr.h>
  18. #include <linux/acct.h> /* acct_auto_close_mnt */
  19. #include <linux/init.h> /* init_rootfs */
  20. #include <linux/fs_struct.h> /* get_fs_root et.al. */
  21. #include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */
  22. #include <linux/uaccess.h>
  23. #include <linux/proc_ns.h>
  24. #include <linux/magic.h>
  25. #include <linux/bootmem.h>
  26. #include "pnode.h"
  27. #include "internal.h"
  28. static unsigned int m_hash_mask __read_mostly;
  29. static unsigned int m_hash_shift __read_mostly;
  30. static unsigned int mp_hash_mask __read_mostly;
  31. static unsigned int mp_hash_shift __read_mostly;
  32. static __initdata unsigned long mhash_entries;
  33. static int __init set_mhash_entries(char *str)
  34. {
  35. if (!str)
  36. return 0;
  37. mhash_entries = simple_strtoul(str, &str, 0);
  38. return 1;
  39. }
  40. __setup("mhash_entries=", set_mhash_entries);
  41. static __initdata unsigned long mphash_entries;
  42. static int __init set_mphash_entries(char *str)
  43. {
  44. if (!str)
  45. return 0;
  46. mphash_entries = simple_strtoul(str, &str, 0);
  47. return 1;
  48. }
  49. __setup("mphash_entries=", set_mphash_entries);
  50. static u64 event;
  51. static DEFINE_IDA(mnt_id_ida);
  52. static DEFINE_IDA(mnt_group_ida);
  53. static DEFINE_SPINLOCK(mnt_id_lock);
  54. static int mnt_id_start = 0;
  55. static int mnt_group_start = 1;
  56. static struct hlist_head *mount_hashtable __read_mostly;
  57. static struct hlist_head *mountpoint_hashtable __read_mostly;
  58. static struct kmem_cache *mnt_cache __read_mostly;
  59. static DECLARE_RWSEM(namespace_sem);
  60. /* /sys/fs */
  61. struct kobject *fs_kobj;
  62. EXPORT_SYMBOL_GPL(fs_kobj);
  63. /*
  64. * vfsmount lock may be taken for read to prevent changes to the
  65. * vfsmount hash, ie. during mountpoint lookups or walking back
  66. * up the tree.
  67. *
  68. * It should be taken for write in all cases where the vfsmount
  69. * tree or hash is modified or when a vfsmount structure is modified.
  70. */
  71. __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
  72. static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
  73. {
  74. unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
  75. tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
  76. tmp = tmp + (tmp >> m_hash_shift);
  77. return &mount_hashtable[tmp & m_hash_mask];
  78. }
  79. static inline struct hlist_head *mp_hash(struct dentry *dentry)
  80. {
  81. unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
  82. tmp = tmp + (tmp >> mp_hash_shift);
  83. return &mountpoint_hashtable[tmp & mp_hash_mask];
  84. }
  85. /*
  86. * allocation is serialized by namespace_sem, but we need the spinlock to
  87. * serialize with freeing.
  88. */
  89. static int mnt_alloc_id(struct mount *mnt)
  90. {
  91. int res;
  92. retry:
  93. ida_pre_get(&mnt_id_ida, GFP_KERNEL);
  94. spin_lock(&mnt_id_lock);
  95. res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
  96. if (!res)
  97. mnt_id_start = mnt->mnt_id + 1;
  98. spin_unlock(&mnt_id_lock);
  99. if (res == -EAGAIN)
  100. goto retry;
  101. return res;
  102. }
  103. static void mnt_free_id(struct mount *mnt)
  104. {
  105. int id = mnt->mnt_id;
  106. spin_lock(&mnt_id_lock);
  107. ida_remove(&mnt_id_ida, id);
  108. if (mnt_id_start > id)
  109. mnt_id_start = id;
  110. spin_unlock(&mnt_id_lock);
  111. }
  112. /*
  113. * Allocate a new peer group ID
  114. *
  115. * mnt_group_ida is protected by namespace_sem
  116. */
  117. static int mnt_alloc_group_id(struct mount *mnt)
  118. {
  119. int res;
  120. if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
  121. return -ENOMEM;
  122. res = ida_get_new_above(&mnt_group_ida,
  123. mnt_group_start,
  124. &mnt->mnt_group_id);
  125. if (!res)
  126. mnt_group_start = mnt->mnt_group_id + 1;
  127. return res;
  128. }
  129. /*
  130. * Release a peer group ID
  131. */
  132. void mnt_release_group_id(struct mount *mnt)
  133. {
  134. int id = mnt->mnt_group_id;
  135. ida_remove(&mnt_group_ida, id);
  136. if (mnt_group_start > id)
  137. mnt_group_start = id;
  138. mnt->mnt_group_id = 0;
  139. }
  140. /*
  141. * vfsmount lock must be held for read
  142. */
  143. static inline void mnt_add_count(struct mount *mnt, int n)
  144. {
  145. #ifdef CONFIG_SMP
  146. this_cpu_add(mnt->mnt_pcp->mnt_count, n);
  147. #else
  148. preempt_disable();
  149. mnt->mnt_count += n;
  150. preempt_enable();
  151. #endif
  152. }
  153. /*
  154. * vfsmount lock must be held for write
  155. */
  156. unsigned int mnt_get_count(struct mount *mnt)
  157. {
  158. #ifdef CONFIG_SMP
  159. unsigned int count = 0;
  160. int cpu;
  161. for_each_possible_cpu(cpu) {
  162. count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
  163. }
  164. return count;
  165. #else
  166. return mnt->mnt_count;
  167. #endif
  168. }
  169. static struct mount *alloc_vfsmnt(const char *name)
  170. {
  171. struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
  172. if (mnt) {
  173. int err;
  174. err = mnt_alloc_id(mnt);
  175. if (err)
  176. goto out_free_cache;
  177. if (name) {
  178. mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
  179. if (!mnt->mnt_devname)
  180. goto out_free_id;
  181. }
  182. #ifdef CONFIG_SMP
  183. mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
  184. if (!mnt->mnt_pcp)
  185. goto out_free_devname;
  186. this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
  187. #else
  188. mnt->mnt_count = 1;
  189. mnt->mnt_writers = 0;
  190. #endif
  191. INIT_HLIST_NODE(&mnt->mnt_hash);
  192. INIT_LIST_HEAD(&mnt->mnt_child);
  193. INIT_LIST_HEAD(&mnt->mnt_mounts);
  194. INIT_LIST_HEAD(&mnt->mnt_list);
  195. INIT_LIST_HEAD(&mnt->mnt_expire);
  196. INIT_LIST_HEAD(&mnt->mnt_share);
  197. INIT_LIST_HEAD(&mnt->mnt_slave_list);
  198. INIT_LIST_HEAD(&mnt->mnt_slave);
  199. #ifdef CONFIG_FSNOTIFY
  200. INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
  201. #endif
  202. }
  203. return mnt;
  204. #ifdef CONFIG_SMP
  205. out_free_devname:
  206. kfree(mnt->mnt_devname);
  207. #endif
  208. out_free_id:
  209. mnt_free_id(mnt);
  210. out_free_cache:
  211. kmem_cache_free(mnt_cache, mnt);
  212. return NULL;
  213. }
  214. /*
  215. * Most r/o checks on a fs are for operations that take
  216. * discrete amounts of time, like a write() or unlink().
  217. * We must keep track of when those operations start
  218. * (for permission checks) and when they end, so that
  219. * we can determine when writes are able to occur to
  220. * a filesystem.
  221. */
  222. /*
  223. * __mnt_is_readonly: check whether a mount is read-only
  224. * @mnt: the mount to check for its write status
  225. *
  226. * This shouldn't be used directly ouside of the VFS.
  227. * It does not guarantee that the filesystem will stay
  228. * r/w, just that it is right *now*. This can not and
  229. * should not be used in place of IS_RDONLY(inode).
  230. * mnt_want/drop_write() will _keep_ the filesystem
  231. * r/w.
  232. */
  233. int __mnt_is_readonly(struct vfsmount *mnt)
  234. {
  235. if (mnt->mnt_flags & MNT_READONLY)
  236. return 1;
  237. if (mnt->mnt_sb->s_flags & MS_RDONLY)
  238. return 1;
  239. return 0;
  240. }
  241. EXPORT_SYMBOL_GPL(__mnt_is_readonly);
  242. static inline void mnt_inc_writers(struct mount *mnt)
  243. {
  244. #ifdef CONFIG_SMP
  245. this_cpu_inc(mnt->mnt_pcp->mnt_writers);
  246. #else
  247. mnt->mnt_writers++;
  248. #endif
  249. }
  250. static inline void mnt_dec_writers(struct mount *mnt)
  251. {
  252. #ifdef CONFIG_SMP
  253. this_cpu_dec(mnt->mnt_pcp->mnt_writers);
  254. #else
  255. mnt->mnt_writers--;
  256. #endif
  257. }
  258. static unsigned int mnt_get_writers(struct mount *mnt)
  259. {
  260. #ifdef CONFIG_SMP
  261. unsigned int count = 0;
  262. int cpu;
  263. for_each_possible_cpu(cpu) {
  264. count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
  265. }
  266. return count;
  267. #else
  268. return mnt->mnt_writers;
  269. #endif
  270. }
  271. static int mnt_is_readonly(struct vfsmount *mnt)
  272. {
  273. if (mnt->mnt_sb->s_readonly_remount)
  274. return 1;
  275. /* Order wrt setting s_flags/s_readonly_remount in do_remount() */
  276. smp_rmb();
  277. return __mnt_is_readonly(mnt);
  278. }
  279. /*
  280. * Most r/o & frozen checks on a fs are for operations that take discrete
  281. * amounts of time, like a write() or unlink(). We must keep track of when
  282. * those operations start (for permission checks) and when they end, so that we
  283. * can determine when writes are able to occur to a filesystem.
  284. */
  285. /**
  286. * __mnt_want_write - get write access to a mount without freeze protection
  287. * @m: the mount on which to take a write
  288. *
  289. * This tells the low-level filesystem that a write is about to be performed to
  290. * it, and makes sure that writes are allowed (mnt it read-write) before
  291. * returning success. This operation does not protect against filesystem being
  292. * frozen. When the write operation is finished, __mnt_drop_write() must be
  293. * called. This is effectively a refcount.
  294. */
  295. int __mnt_want_write(struct vfsmount *m)
  296. {
  297. struct mount *mnt = real_mount(m);
  298. int ret = 0;
  299. preempt_disable();
  300. mnt_inc_writers(mnt);
  301. /*
  302. * The store to mnt_inc_writers must be visible before we pass
  303. * MNT_WRITE_HOLD loop below, so that the slowpath can see our
  304. * incremented count after it has set MNT_WRITE_HOLD.
  305. */
  306. smp_mb();
  307. while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
  308. cpu_relax();
  309. /*
  310. * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
  311. * be set to match its requirements. So we must not load that until
  312. * MNT_WRITE_HOLD is cleared.
  313. */
  314. smp_rmb();
  315. if (mnt_is_readonly(m)) {
  316. mnt_dec_writers(mnt);
  317. ret = -EROFS;
  318. }
  319. preempt_enable();
  320. return ret;
  321. }
  322. /**
  323. * mnt_want_write - get write access to a mount
  324. * @m: the mount on which to take a write
  325. *
  326. * This tells the low-level filesystem that a write is about to be performed to
  327. * it, and makes sure that writes are allowed (mount is read-write, filesystem
  328. * is not frozen) before returning success. When the write operation is
  329. * finished, mnt_drop_write() must be called. This is effectively a refcount.
  330. */
  331. int mnt_want_write(struct vfsmount *m)
  332. {
  333. int ret;
  334. sb_start_write(m->mnt_sb);
  335. ret = __mnt_want_write(m);
  336. if (ret)
  337. sb_end_write(m->mnt_sb);
  338. return ret;
  339. }
  340. EXPORT_SYMBOL_GPL(mnt_want_write);
  341. /**
  342. * mnt_clone_write - get write access to a mount
  343. * @mnt: the mount on which to take a write
  344. *
  345. * This is effectively like mnt_want_write, except
  346. * it must only be used to take an extra write reference
  347. * on a mountpoint that we already know has a write reference
  348. * on it. This allows some optimisation.
  349. *
  350. * After finished, mnt_drop_write must be called as usual to
  351. * drop the reference.
  352. */
  353. int mnt_clone_write(struct vfsmount *mnt)
  354. {
  355. /* superblock may be r/o */
  356. if (__mnt_is_readonly(mnt))
  357. return -EROFS;
  358. preempt_disable();
  359. mnt_inc_writers(real_mount(mnt));
  360. preempt_enable();
  361. return 0;
  362. }
  363. EXPORT_SYMBOL_GPL(mnt_clone_write);
  364. /**
  365. * __mnt_want_write_file - get write access to a file's mount
  366. * @file: the file who's mount on which to take a write
  367. *
  368. * This is like __mnt_want_write, but it takes a file and can
  369. * do some optimisations if the file is open for write already
  370. */
  371. int __mnt_want_write_file(struct file *file)
  372. {
  373. if (!(file->f_mode & FMODE_WRITER))
  374. return __mnt_want_write(file->f_path.mnt);
  375. else
  376. return mnt_clone_write(file->f_path.mnt);
  377. }
  378. /**
  379. * mnt_want_write_file - get write access to a file's mount
  380. * @file: the file who's mount on which to take a write
  381. *
  382. * This is like mnt_want_write, but it takes a file and can
  383. * do some optimisations if the file is open for write already
  384. */
  385. int mnt_want_write_file(struct file *file)
  386. {
  387. int ret;
  388. sb_start_write(file->f_path.mnt->mnt_sb);
  389. ret = __mnt_want_write_file(file);
  390. if (ret)
  391. sb_end_write(file->f_path.mnt->mnt_sb);
  392. return ret;
  393. }
  394. EXPORT_SYMBOL_GPL(mnt_want_write_file);
  395. /**
  396. * __mnt_drop_write - give up write access to a mount
  397. * @mnt: the mount on which to give up write access
  398. *
  399. * Tells the low-level filesystem that we are done
  400. * performing writes to it. Must be matched with
  401. * __mnt_want_write() call above.
  402. */
  403. void __mnt_drop_write(struct vfsmount *mnt)
  404. {
  405. preempt_disable();
  406. mnt_dec_writers(real_mount(mnt));
  407. preempt_enable();
  408. }
  409. /**
  410. * mnt_drop_write - give up write access to a mount
  411. * @mnt: the mount on which to give up write access
  412. *
  413. * Tells the low-level filesystem that we are done performing writes to it and
  414. * also allows filesystem to be frozen again. Must be matched with
  415. * mnt_want_write() call above.
  416. */
  417. void mnt_drop_write(struct vfsmount *mnt)
  418. {
  419. __mnt_drop_write(mnt);
  420. sb_end_write(mnt->mnt_sb);
  421. }
  422. EXPORT_SYMBOL_GPL(mnt_drop_write);
  423. void __mnt_drop_write_file(struct file *file)
  424. {
  425. __mnt_drop_write(file->f_path.mnt);
  426. }
  427. void mnt_drop_write_file(struct file *file)
  428. {
  429. mnt_drop_write(file->f_path.mnt);
  430. }
  431. EXPORT_SYMBOL(mnt_drop_write_file);
  432. static int mnt_make_readonly(struct mount *mnt)
  433. {
  434. int ret = 0;
  435. lock_mount_hash();
  436. mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
  437. /*
  438. * After storing MNT_WRITE_HOLD, we'll read the counters. This store
  439. * should be visible before we do.
  440. */
  441. smp_mb();
  442. /*
  443. * With writers on hold, if this value is zero, then there are
  444. * definitely no active writers (although held writers may subsequently
  445. * increment the count, they'll have to wait, and decrement it after
  446. * seeing MNT_READONLY).
  447. *
  448. * It is OK to have counter incremented on one CPU and decremented on
  449. * another: the sum will add up correctly. The danger would be when we
  450. * sum up each counter, if we read a counter before it is incremented,
  451. * but then read another CPU's count which it has been subsequently
  452. * decremented from -- we would see more decrements than we should.
  453. * MNT_WRITE_HOLD protects against this scenario, because
  454. * mnt_want_write first increments count, then smp_mb, then spins on
  455. * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
  456. * we're counting up here.
  457. */
  458. if (mnt_get_writers(mnt) > 0)
  459. ret = -EBUSY;
  460. else
  461. mnt->mnt.mnt_flags |= MNT_READONLY;
  462. /*
  463. * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
  464. * that become unheld will see MNT_READONLY.
  465. */
  466. smp_wmb();
  467. mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
  468. unlock_mount_hash();
  469. return ret;
  470. }
  471. static void __mnt_unmake_readonly(struct mount *mnt)
  472. {
  473. lock_mount_hash();
  474. mnt->mnt.mnt_flags &= ~MNT_READONLY;
  475. unlock_mount_hash();
  476. }
  477. int sb_prepare_remount_readonly(struct super_block *sb)
  478. {
  479. struct mount *mnt;
  480. int err = 0;
  481. /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */
  482. if (atomic_long_read(&sb->s_remove_count))
  483. return -EBUSY;
  484. lock_mount_hash();
  485. list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
  486. if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
  487. mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
  488. smp_mb();
  489. if (mnt_get_writers(mnt) > 0) {
  490. err = -EBUSY;
  491. break;
  492. }
  493. }
  494. }
  495. if (!err && atomic_long_read(&sb->s_remove_count))
  496. err = -EBUSY;
  497. if (!err) {
  498. sb->s_readonly_remount = 1;
  499. smp_wmb();
  500. }
  501. list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
  502. if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
  503. mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
  504. }
  505. unlock_mount_hash();
  506. return err;
  507. }
  508. static void free_vfsmnt(struct mount *mnt)
  509. {
  510. kfree(mnt->mnt_devname);
  511. mnt_free_id(mnt);
  512. #ifdef CONFIG_SMP
  513. free_percpu(mnt->mnt_pcp);
  514. #endif
  515. kmem_cache_free(mnt_cache, mnt);
  516. }
  517. /* call under rcu_read_lock */
  518. bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
  519. {
  520. struct mount *mnt;
  521. if (read_seqretry(&mount_lock, seq))
  522. return false;
  523. if (bastard == NULL)
  524. return true;
  525. mnt = real_mount(bastard);
  526. mnt_add_count(mnt, 1);
  527. if (likely(!read_seqretry(&mount_lock, seq)))
  528. return true;
  529. if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
  530. mnt_add_count(mnt, -1);
  531. return false;
  532. }
  533. rcu_read_unlock();
  534. mntput(bastard);
  535. rcu_read_lock();
  536. return false;
  537. }
  538. /*
  539. * find the first mount at @dentry on vfsmount @mnt.
  540. * call under rcu_read_lock()
  541. */
  542. struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
  543. {
  544. struct hlist_head *head = m_hash(mnt, dentry);
  545. struct mount *p;
  546. hlist_for_each_entry_rcu(p, head, mnt_hash)
  547. if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
  548. return p;
  549. return NULL;
  550. }
  551. /*
  552. * find the last mount at @dentry on vfsmount @mnt.
  553. * mount_lock must be held.
  554. */
  555. struct mount *__lookup_mnt_last(struct vfsmount *mnt, struct dentry *dentry)
  556. {
  557. struct mount *p, *res;
  558. res = p = __lookup_mnt(mnt, dentry);
  559. if (!p)
  560. goto out;
  561. hlist_for_each_entry_continue(p, mnt_hash) {
  562. if (&p->mnt_parent->mnt != mnt || p->mnt_mountpoint != dentry)
  563. break;
  564. res = p;
  565. }
  566. out:
  567. return res;
  568. }
  569. /*
  570. * lookup_mnt - Return the first child mount mounted at path
  571. *
  572. * "First" means first mounted chronologically. If you create the
  573. * following mounts:
  574. *
  575. * mount /dev/sda1 /mnt
  576. * mount /dev/sda2 /mnt
  577. * mount /dev/sda3 /mnt
  578. *
  579. * Then lookup_mnt() on the base /mnt dentry in the root mount will
  580. * return successively the root dentry and vfsmount of /dev/sda1, then
  581. * /dev/sda2, then /dev/sda3, then NULL.
  582. *
  583. * lookup_mnt takes a reference to the found vfsmount.
  584. */
  585. struct vfsmount *lookup_mnt(struct path *path)
  586. {
  587. struct mount *child_mnt;
  588. struct vfsmount *m;
  589. unsigned seq;
  590. rcu_read_lock();
  591. do {
  592. seq = read_seqbegin(&mount_lock);
  593. child_mnt = __lookup_mnt(path->mnt, path->dentry);
  594. m = child_mnt ? &child_mnt->mnt : NULL;
  595. } while (!legitimize_mnt(m, seq));
  596. rcu_read_unlock();
  597. return m;
  598. }
  599. static struct mountpoint *new_mountpoint(struct dentry *dentry)
  600. {
  601. struct hlist_head *chain = mp_hash(dentry);
  602. struct mountpoint *mp;
  603. int ret;
  604. hlist_for_each_entry(mp, chain, m_hash) {
  605. if (mp->m_dentry == dentry) {
  606. /* might be worth a WARN_ON() */
  607. if (d_unlinked(dentry))
  608. return ERR_PTR(-ENOENT);
  609. mp->m_count++;
  610. return mp;
  611. }
  612. }
  613. mp = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
  614. if (!mp)
  615. return ERR_PTR(-ENOMEM);
  616. ret = d_set_mounted(dentry);
  617. if (ret) {
  618. kfree(mp);
  619. return ERR_PTR(ret);
  620. }
  621. mp->m_dentry = dentry;
  622. mp->m_count = 1;
  623. hlist_add_head(&mp->m_hash, chain);
  624. return mp;
  625. }
  626. static void put_mountpoint(struct mountpoint *mp)
  627. {
  628. if (!--mp->m_count) {
  629. struct dentry *dentry = mp->m_dentry;
  630. spin_lock(&dentry->d_lock);
  631. dentry->d_flags &= ~DCACHE_MOUNTED;
  632. spin_unlock(&dentry->d_lock);
  633. hlist_del(&mp->m_hash);
  634. kfree(mp);
  635. }
  636. }
  637. static inline int check_mnt(struct mount *mnt)
  638. {
  639. return mnt->mnt_ns == current->nsproxy->mnt_ns;
  640. }
  641. /*
  642. * vfsmount lock must be held for write
  643. */
  644. static void touch_mnt_namespace(struct mnt_namespace *ns)
  645. {
  646. if (ns) {
  647. ns->event = ++event;
  648. wake_up_interruptible(&ns->poll);
  649. }
  650. }
  651. /*
  652. * vfsmount lock must be held for write
  653. */
  654. static void __touch_mnt_namespace(struct mnt_namespace *ns)
  655. {
  656. if (ns && ns->event != event) {
  657. ns->event = event;
  658. wake_up_interruptible(&ns->poll);
  659. }
  660. }
  661. /*
  662. * vfsmount lock must be held for write
  663. */
  664. static void detach_mnt(struct mount *mnt, struct path *old_path)
  665. {
  666. old_path->dentry = mnt->mnt_mountpoint;
  667. old_path->mnt = &mnt->mnt_parent->mnt;
  668. mnt->mnt_parent = mnt;
  669. mnt->mnt_mountpoint = mnt->mnt.mnt_root;
  670. list_del_init(&mnt->mnt_child);
  671. hlist_del_init_rcu(&mnt->mnt_hash);
  672. put_mountpoint(mnt->mnt_mp);
  673. mnt->mnt_mp = NULL;
  674. }
  675. /*
  676. * vfsmount lock must be held for write
  677. */
  678. void mnt_set_mountpoint(struct mount *mnt,
  679. struct mountpoint *mp,
  680. struct mount *child_mnt)
  681. {
  682. mp->m_count++;
  683. mnt_add_count(mnt, 1); /* essentially, that's mntget */
  684. child_mnt->mnt_mountpoint = dget(mp->m_dentry);
  685. child_mnt->mnt_parent = mnt;
  686. child_mnt->mnt_mp = mp;
  687. }
  688. /*
  689. * vfsmount lock must be held for write
  690. */
  691. static void attach_mnt(struct mount *mnt,
  692. struct mount *parent,
  693. struct mountpoint *mp)
  694. {
  695. mnt_set_mountpoint(parent, mp, mnt);
  696. hlist_add_head_rcu(&mnt->mnt_hash, m_hash(&parent->mnt, mp->m_dentry));
  697. list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
  698. }
  699. /*
  700. * vfsmount lock must be held for write
  701. */
  702. static void commit_tree(struct mount *mnt, struct mount *shadows)
  703. {
  704. struct mount *parent = mnt->mnt_parent;
  705. struct mount *m;
  706. LIST_HEAD(head);
  707. struct mnt_namespace *n = parent->mnt_ns;
  708. BUG_ON(parent == mnt);
  709. list_add_tail(&head, &mnt->mnt_list);
  710. list_for_each_entry(m, &head, mnt_list)
  711. m->mnt_ns = n;
  712. list_splice(&head, n->list.prev);
  713. if (shadows)
  714. hlist_add_after_rcu(&shadows->mnt_hash, &mnt->mnt_hash);
  715. else
  716. hlist_add_head_rcu(&mnt->mnt_hash,
  717. m_hash(&parent->mnt, mnt->mnt_mountpoint));
  718. list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
  719. touch_mnt_namespace(n);
  720. }
  721. static struct mount *next_mnt(struct mount *p, struct mount *root)
  722. {
  723. struct list_head *next = p->mnt_mounts.next;
  724. if (next == &p->mnt_mounts) {
  725. while (1) {
  726. if (p == root)
  727. return NULL;
  728. next = p->mnt_child.next;
  729. if (next != &p->mnt_parent->mnt_mounts)
  730. break;
  731. p = p->mnt_parent;
  732. }
  733. }
  734. return list_entry(next, struct mount, mnt_child);
  735. }
  736. static struct mount *skip_mnt_tree(struct mount *p)
  737. {
  738. struct list_head *prev = p->mnt_mounts.prev;
  739. while (prev != &p->mnt_mounts) {
  740. p = list_entry(prev, struct mount, mnt_child);
  741. prev = p->mnt_mounts.prev;
  742. }
  743. return p;
  744. }
  745. struct vfsmount *
  746. vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
  747. {
  748. struct mount *mnt;
  749. struct dentry *root;
  750. if (!type)
  751. return ERR_PTR(-ENODEV);
  752. mnt = alloc_vfsmnt(name);
  753. if (!mnt)
  754. return ERR_PTR(-ENOMEM);
  755. if (flags & MS_KERNMOUNT)
  756. mnt->mnt.mnt_flags = MNT_INTERNAL;
  757. root = mount_fs(type, flags, name, data);
  758. if (IS_ERR(root)) {
  759. free_vfsmnt(mnt);
  760. return ERR_CAST(root);
  761. }
  762. mnt->mnt.mnt_root = root;
  763. mnt->mnt.mnt_sb = root->d_sb;
  764. mnt->mnt_mountpoint = mnt->mnt.mnt_root;
  765. mnt->mnt_parent = mnt;
  766. lock_mount_hash();
  767. list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
  768. unlock_mount_hash();
  769. return &mnt->mnt;
  770. }
  771. EXPORT_SYMBOL_GPL(vfs_kern_mount);
  772. static struct mount *clone_mnt(struct mount *old, struct dentry *root,
  773. int flag)
  774. {
  775. struct super_block *sb = old->mnt.mnt_sb;
  776. struct mount *mnt;
  777. int err;
  778. mnt = alloc_vfsmnt(old->mnt_devname);
  779. if (!mnt)
  780. return ERR_PTR(-ENOMEM);
  781. if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
  782. mnt->mnt_group_id = 0; /* not a peer of original */
  783. else
  784. mnt->mnt_group_id = old->mnt_group_id;
  785. if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
  786. err = mnt_alloc_group_id(mnt);
  787. if (err)
  788. goto out_free;
  789. }
  790. mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~(MNT_WRITE_HOLD|MNT_MARKED);
  791. /* Don't allow unprivileged users to change mount flags */
  792. if ((flag & CL_UNPRIVILEGED) && (mnt->mnt.mnt_flags & MNT_READONLY))
  793. mnt->mnt.mnt_flags |= MNT_LOCK_READONLY;
  794. /* Don't allow unprivileged users to reveal what is under a mount */
  795. if ((flag & CL_UNPRIVILEGED) && list_empty(&old->mnt_expire))
  796. mnt->mnt.mnt_flags |= MNT_LOCKED;
  797. atomic_inc(&sb->s_active);
  798. mnt->mnt.mnt_sb = sb;
  799. mnt->mnt.mnt_root = dget(root);
  800. mnt->mnt_mountpoint = mnt->mnt.mnt_root;
  801. mnt->mnt_parent = mnt;
  802. lock_mount_hash();
  803. list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
  804. unlock_mount_hash();
  805. if ((flag & CL_SLAVE) ||
  806. ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
  807. list_add(&mnt->mnt_slave, &old->mnt_slave_list);
  808. mnt->mnt_master = old;
  809. CLEAR_MNT_SHARED(mnt);
  810. } else if (!(flag & CL_PRIVATE)) {
  811. if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
  812. list_add(&mnt->mnt_share, &old->mnt_share);
  813. if (IS_MNT_SLAVE(old))
  814. list_add(&mnt->mnt_slave, &old->mnt_slave);
  815. mnt->mnt_master = old->mnt_master;
  816. }
  817. if (flag & CL_MAKE_SHARED)
  818. set_mnt_shared(mnt);
  819. /* stick the duplicate mount on the same expiry list
  820. * as the original if that was on one */
  821. if (flag & CL_EXPIRE) {
  822. if (!list_empty(&old->mnt_expire))
  823. list_add(&mnt->mnt_expire, &old->mnt_expire);
  824. }
  825. return mnt;
  826. out_free:
  827. free_vfsmnt(mnt);
  828. return ERR_PTR(err);
  829. }
  830. static void delayed_free(struct rcu_head *head)
  831. {
  832. struct mount *mnt = container_of(head, struct mount, mnt_rcu);
  833. kfree(mnt->mnt_devname);
  834. #ifdef CONFIG_SMP
  835. free_percpu(mnt->mnt_pcp);
  836. #endif
  837. kmem_cache_free(mnt_cache, mnt);
  838. }
  839. static void mntput_no_expire(struct mount *mnt)
  840. {
  841. put_again:
  842. rcu_read_lock();
  843. mnt_add_count(mnt, -1);
  844. if (likely(mnt->mnt_ns)) { /* shouldn't be the last one */
  845. rcu_read_unlock();
  846. return;
  847. }
  848. lock_mount_hash();
  849. if (mnt_get_count(mnt)) {
  850. rcu_read_unlock();
  851. unlock_mount_hash();
  852. return;
  853. }
  854. if (unlikely(mnt->mnt_pinned)) {
  855. mnt_add_count(mnt, mnt->mnt_pinned + 1);
  856. mnt->mnt_pinned = 0;
  857. rcu_read_unlock();
  858. unlock_mount_hash();
  859. acct_auto_close_mnt(&mnt->mnt);
  860. goto put_again;
  861. }
  862. if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
  863. rcu_read_unlock();
  864. unlock_mount_hash();
  865. return;
  866. }
  867. mnt->mnt.mnt_flags |= MNT_DOOMED;
  868. rcu_read_unlock();
  869. list_del(&mnt->mnt_instance);
  870. unlock_mount_hash();
  871. /*
  872. * This probably indicates that somebody messed
  873. * up a mnt_want/drop_write() pair. If this
  874. * happens, the filesystem was probably unable
  875. * to make r/w->r/o transitions.
  876. */
  877. /*
  878. * The locking used to deal with mnt_count decrement provides barriers,
  879. * so mnt_get_writers() below is safe.
  880. */
  881. WARN_ON(mnt_get_writers(mnt));
  882. fsnotify_vfsmount_delete(&mnt->mnt);
  883. dput(mnt->mnt.mnt_root);
  884. deactivate_super(mnt->mnt.mnt_sb);
  885. mnt_free_id(mnt);
  886. call_rcu(&mnt->mnt_rcu, delayed_free);
  887. }
  888. void mntput(struct vfsmount *mnt)
  889. {
  890. if (mnt) {
  891. struct mount *m = real_mount(mnt);
  892. /* avoid cacheline pingpong, hope gcc doesn't get "smart" */
  893. if (unlikely(m->mnt_expiry_mark))
  894. m->mnt_expiry_mark = 0;
  895. mntput_no_expire(m);
  896. }
  897. }
  898. EXPORT_SYMBOL(mntput);
  899. struct vfsmount *mntget(struct vfsmount *mnt)
  900. {
  901. if (mnt)
  902. mnt_add_count(real_mount(mnt), 1);
  903. return mnt;
  904. }
  905. EXPORT_SYMBOL(mntget);
  906. void mnt_pin(struct vfsmount *mnt)
  907. {
  908. lock_mount_hash();
  909. real_mount(mnt)->mnt_pinned++;
  910. unlock_mount_hash();
  911. }
  912. EXPORT_SYMBOL(mnt_pin);
  913. void mnt_unpin(struct vfsmount *m)
  914. {
  915. struct mount *mnt = real_mount(m);
  916. lock_mount_hash();
  917. if (mnt->mnt_pinned) {
  918. mnt_add_count(mnt, 1);
  919. mnt->mnt_pinned--;
  920. }
  921. unlock_mount_hash();
  922. }
  923. EXPORT_SYMBOL(mnt_unpin);
  924. static inline void mangle(struct seq_file *m, const char *s)
  925. {
  926. seq_escape(m, s, " \t\n\\");
  927. }
  928. /*
  929. * Simple .show_options callback for filesystems which don't want to
  930. * implement more complex mount option showing.
  931. *
  932. * See also save_mount_options().
  933. */
  934. int generic_show_options(struct seq_file *m, struct dentry *root)
  935. {
  936. const char *options;
  937. rcu_read_lock();
  938. options = rcu_dereference(root->d_sb->s_options);
  939. if (options != NULL && options[0]) {
  940. seq_putc(m, ',');
  941. mangle(m, options);
  942. }
  943. rcu_read_unlock();
  944. return 0;
  945. }
  946. EXPORT_SYMBOL(generic_show_options);
  947. /*
  948. * If filesystem uses generic_show_options(), this function should be
  949. * called from the fill_super() callback.
  950. *
  951. * The .remount_fs callback usually needs to be handled in a special
  952. * way, to make sure, that previous options are not overwritten if the
  953. * remount fails.
  954. *
  955. * Also note, that if the filesystem's .remount_fs function doesn't
  956. * reset all options to their default value, but changes only newly
  957. * given options, then the displayed options will not reflect reality
  958. * any more.
  959. */
  960. void save_mount_options(struct super_block *sb, char *options)
  961. {
  962. BUG_ON(sb->s_options);
  963. rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
  964. }
  965. EXPORT_SYMBOL(save_mount_options);
  966. void replace_mount_options(struct super_block *sb, char *options)
  967. {
  968. char *old = sb->s_options;
  969. rcu_assign_pointer(sb->s_options, options);
  970. if (old) {
  971. synchronize_rcu();
  972. kfree(old);
  973. }
  974. }
  975. EXPORT_SYMBOL(replace_mount_options);
  976. #ifdef CONFIG_PROC_FS
  977. /* iterator; we want it to have access to namespace_sem, thus here... */
  978. static void *m_start(struct seq_file *m, loff_t *pos)
  979. {
  980. struct proc_mounts *p = proc_mounts(m);
  981. down_read(&namespace_sem);
  982. if (p->cached_event == p->ns->event) {
  983. void *v = p->cached_mount;
  984. if (*pos == p->cached_index)
  985. return v;
  986. if (*pos == p->cached_index + 1) {
  987. v = seq_list_next(v, &p->ns->list, &p->cached_index);
  988. return p->cached_mount = v;
  989. }
  990. }
  991. p->cached_event = p->ns->event;
  992. p->cached_mount = seq_list_start(&p->ns->list, *pos);
  993. p->cached_index = *pos;
  994. return p->cached_mount;
  995. }
  996. static void *m_next(struct seq_file *m, void *v, loff_t *pos)
  997. {
  998. struct proc_mounts *p = proc_mounts(m);
  999. p->cached_mount = seq_list_next(v, &p->ns->list, pos);
  1000. p->cached_index = *pos;
  1001. return p->cached_mount;
  1002. }
  1003. static void m_stop(struct seq_file *m, void *v)
  1004. {
  1005. up_read(&namespace_sem);
  1006. }
  1007. static int m_show(struct seq_file *m, void *v)
  1008. {
  1009. struct proc_mounts *p = proc_mounts(m);
  1010. struct mount *r = list_entry(v, struct mount, mnt_list);
  1011. return p->show(m, &r->mnt);
  1012. }
  1013. const struct seq_operations mounts_op = {
  1014. .start = m_start,
  1015. .next = m_next,
  1016. .stop = m_stop,
  1017. .show = m_show,
  1018. };
  1019. #endif /* CONFIG_PROC_FS */
  1020. /**
  1021. * may_umount_tree - check if a mount tree is busy
  1022. * @mnt: root of mount tree
  1023. *
  1024. * This is called to check if a tree of mounts has any
  1025. * open files, pwds, chroots or sub mounts that are
  1026. * busy.
  1027. */
  1028. int may_umount_tree(struct vfsmount *m)
  1029. {
  1030. struct mount *mnt = real_mount(m);
  1031. int actual_refs = 0;
  1032. int minimum_refs = 0;
  1033. struct mount *p;
  1034. BUG_ON(!m);
  1035. /* write lock needed for mnt_get_count */
  1036. lock_mount_hash();
  1037. for (p = mnt; p; p = next_mnt(p, mnt)) {
  1038. actual_refs += mnt_get_count(p);
  1039. minimum_refs += 2;
  1040. }
  1041. unlock_mount_hash();
  1042. if (actual_refs > minimum_refs)
  1043. return 0;
  1044. return 1;
  1045. }
  1046. EXPORT_SYMBOL(may_umount_tree);
  1047. /**
  1048. * may_umount - check if a mount point is busy
  1049. * @mnt: root of mount
  1050. *
  1051. * This is called to check if a mount point has any
  1052. * open files, pwds, chroots or sub mounts. If the
  1053. * mount has sub mounts this will return busy
  1054. * regardless of whether the sub mounts are busy.
  1055. *
  1056. * Doesn't take quota and stuff into account. IOW, in some cases it will
  1057. * give false negatives. The main reason why it's here is that we need
  1058. * a non-destructive way to look for easily umountable filesystems.
  1059. */
  1060. int may_umount(struct vfsmount *mnt)
  1061. {
  1062. int ret = 1;
  1063. down_read(&namespace_sem);
  1064. lock_mount_hash();
  1065. if (propagate_mount_busy(real_mount(mnt), 2))
  1066. ret = 0;
  1067. unlock_mount_hash();
  1068. up_read(&namespace_sem);
  1069. return ret;
  1070. }
  1071. EXPORT_SYMBOL(may_umount);
  1072. static HLIST_HEAD(unmounted); /* protected by namespace_sem */
  1073. static void namespace_unlock(void)
  1074. {
  1075. struct mount *mnt;
  1076. struct hlist_head head = unmounted;
  1077. if (likely(hlist_empty(&head))) {
  1078. up_write(&namespace_sem);
  1079. return;
  1080. }
  1081. head.first->pprev = &head.first;
  1082. INIT_HLIST_HEAD(&unmounted);
  1083. up_write(&namespace_sem);
  1084. synchronize_rcu();
  1085. while (!hlist_empty(&head)) {
  1086. mnt = hlist_entry(head.first, struct mount, mnt_hash);
  1087. hlist_del_init(&mnt->mnt_hash);
  1088. if (mnt->mnt_ex_mountpoint.mnt)
  1089. path_put(&mnt->mnt_ex_mountpoint);
  1090. mntput(&mnt->mnt);
  1091. }
  1092. }
  1093. static inline void namespace_lock(void)
  1094. {
  1095. down_write(&namespace_sem);
  1096. }
  1097. /*
  1098. * mount_lock must be held
  1099. * namespace_sem must be held for write
  1100. * how = 0 => just this tree, don't propagate
  1101. * how = 1 => propagate; we know that nobody else has reference to any victims
  1102. * how = 2 => lazy umount
  1103. */
  1104. void umount_tree(struct mount *mnt, int how)
  1105. {
  1106. HLIST_HEAD(tmp_list);
  1107. struct mount *p;
  1108. struct mount *last = NULL;
  1109. for (p = mnt; p; p = next_mnt(p, mnt)) {
  1110. hlist_del_init_rcu(&p->mnt_hash);
  1111. hlist_add_head(&p->mnt_hash, &tmp_list);
  1112. }
  1113. if (how)
  1114. propagate_umount(&tmp_list);
  1115. hlist_for_each_entry(p, &tmp_list, mnt_hash) {
  1116. list_del_init(&p->mnt_expire);
  1117. list_del_init(&p->mnt_list);
  1118. __touch_mnt_namespace(p->mnt_ns);
  1119. p->mnt_ns = NULL;
  1120. if (how < 2)
  1121. p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
  1122. list_del_init(&p->mnt_child);
  1123. if (mnt_has_parent(p)) {
  1124. put_mountpoint(p->mnt_mp);
  1125. /* move the reference to mountpoint into ->mnt_ex_mountpoint */
  1126. p->mnt_ex_mountpoint.dentry = p->mnt_mountpoint;
  1127. p->mnt_ex_mountpoint.mnt = &p->mnt_parent->mnt;
  1128. p->mnt_mountpoint = p->mnt.mnt_root;
  1129. p->mnt_parent = p;
  1130. p->mnt_mp = NULL;
  1131. }
  1132. change_mnt_propagation(p, MS_PRIVATE);
  1133. last = p;
  1134. }
  1135. if (last) {
  1136. last->mnt_hash.next = unmounted.first;
  1137. unmounted.first = tmp_list.first;
  1138. unmounted.first->pprev = &unmounted.first;
  1139. }
  1140. }
  1141. static void shrink_submounts(struct mount *mnt);
  1142. static int do_umount(struct mount *mnt, int flags)
  1143. {
  1144. struct super_block *sb = mnt->mnt.mnt_sb;
  1145. int retval;
  1146. retval = security_sb_umount(&mnt->mnt, flags);
  1147. if (retval)
  1148. return retval;
  1149. /*
  1150. * Allow userspace to request a mountpoint be expired rather than
  1151. * unmounting unconditionally. Unmount only happens if:
  1152. * (1) the mark is already set (the mark is cleared by mntput())
  1153. * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
  1154. */
  1155. if (flags & MNT_EXPIRE) {
  1156. if (&mnt->mnt == current->fs->root.mnt ||
  1157. flags & (MNT_FORCE | MNT_DETACH))
  1158. return -EINVAL;
  1159. /*
  1160. * probably don't strictly need the lock here if we examined
  1161. * all race cases, but it's a slowpath.
  1162. */
  1163. lock_mount_hash();
  1164. if (mnt_get_count(mnt) != 2) {
  1165. unlock_mount_hash();
  1166. return -EBUSY;
  1167. }
  1168. unlock_mount_hash();
  1169. if (!xchg(&mnt->mnt_expiry_mark, 1))
  1170. return -EAGAIN;
  1171. }
  1172. /*
  1173. * If we may have to abort operations to get out of this
  1174. * mount, and they will themselves hold resources we must
  1175. * allow the fs to do things. In the Unix tradition of
  1176. * 'Gee thats tricky lets do it in userspace' the umount_begin
  1177. * might fail to complete on the first run through as other tasks
  1178. * must return, and the like. Thats for the mount program to worry
  1179. * about for the moment.
  1180. */
  1181. if (flags & MNT_FORCE && sb->s_op->umount_begin) {
  1182. sb->s_op->umount_begin(sb);
  1183. }
  1184. /*
  1185. * No sense to grab the lock for this test, but test itself looks
  1186. * somewhat bogus. Suggestions for better replacement?
  1187. * Ho-hum... In principle, we might treat that as umount + switch
  1188. * to rootfs. GC would eventually take care of the old vfsmount.
  1189. * Actually it makes sense, especially if rootfs would contain a
  1190. * /reboot - static binary that would close all descriptors and
  1191. * call reboot(9). Then init(8) could umount root and exec /reboot.
  1192. */
  1193. if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
  1194. /*
  1195. * Special case for "unmounting" root ...
  1196. * we just try to remount it readonly.
  1197. */
  1198. down_write(&sb->s_umount);
  1199. if (!(sb->s_flags & MS_RDONLY))
  1200. retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
  1201. up_write(&sb->s_umount);
  1202. return retval;
  1203. }
  1204. namespace_lock();
  1205. lock_mount_hash();
  1206. event++;
  1207. if (flags & MNT_DETACH) {
  1208. if (!list_empty(&mnt->mnt_list))
  1209. umount_tree(mnt, 2);
  1210. retval = 0;
  1211. } else {
  1212. shrink_submounts(mnt);
  1213. retval = -EBUSY;
  1214. if (!propagate_mount_busy(mnt, 2)) {
  1215. if (!list_empty(&mnt->mnt_list))
  1216. umount_tree(mnt, 1);
  1217. retval = 0;
  1218. }
  1219. }
  1220. unlock_mount_hash();
  1221. namespace_unlock();
  1222. return retval;
  1223. }
  1224. /*
  1225. * Is the caller allowed to modify his namespace?
  1226. */
  1227. static inline bool may_mount(void)
  1228. {
  1229. return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
  1230. }
  1231. /*
  1232. * Now umount can handle mount points as well as block devices.
  1233. * This is important for filesystems which use unnamed block devices.
  1234. *
  1235. * We now support a flag for forced unmount like the other 'big iron'
  1236. * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
  1237. */
  1238. SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
  1239. {
  1240. struct path path;
  1241. struct mount *mnt;
  1242. int retval;
  1243. int lookup_flags = 0;
  1244. if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
  1245. return -EINVAL;
  1246. if (!may_mount())
  1247. return -EPERM;
  1248. if (!(flags & UMOUNT_NOFOLLOW))
  1249. lookup_flags |= LOOKUP_FOLLOW;
  1250. retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path);
  1251. if (retval)
  1252. goto out;
  1253. mnt = real_mount(path.mnt);
  1254. retval = -EINVAL;
  1255. if (path.dentry != path.mnt->mnt_root)
  1256. goto dput_and_out;
  1257. if (!check_mnt(mnt))
  1258. goto dput_and_out;
  1259. if (mnt->mnt.mnt_flags & MNT_LOCKED)
  1260. goto dput_and_out;
  1261. retval = do_umount(mnt, flags);
  1262. dput_and_out:
  1263. /* we mustn't call path_put() as that would clear mnt_expiry_mark */
  1264. dput(path.dentry);
  1265. mntput_no_expire(mnt);
  1266. out:
  1267. return retval;
  1268. }
  1269. #ifdef __ARCH_WANT_SYS_OLDUMOUNT
  1270. /*
  1271. * The 2.0 compatible umount. No flags.
  1272. */
  1273. SYSCALL_DEFINE1(oldumount, char __user *, name)
  1274. {
  1275. return sys_umount(name, 0);
  1276. }
  1277. #endif
  1278. static bool is_mnt_ns_file(struct dentry *dentry)
  1279. {
  1280. /* Is this a proxy for a mount namespace? */
  1281. struct inode *inode = dentry->d_inode;
  1282. struct proc_ns *ei;
  1283. if (!proc_ns_inode(inode))
  1284. return false;
  1285. ei = get_proc_ns(inode);
  1286. if (ei->ns_ops != &mntns_operations)
  1287. return false;
  1288. return true;
  1289. }
  1290. static bool mnt_ns_loop(struct dentry *dentry)
  1291. {
  1292. /* Could bind mounting the mount namespace inode cause a
  1293. * mount namespace loop?
  1294. */
  1295. struct mnt_namespace *mnt_ns;
  1296. if (!is_mnt_ns_file(dentry))
  1297. return false;
  1298. mnt_ns = get_proc_ns(dentry->d_inode)->ns;
  1299. return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
  1300. }
  1301. struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
  1302. int flag)
  1303. {
  1304. struct mount *res, *p, *q, *r, *parent;
  1305. if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
  1306. return ERR_PTR(-EINVAL);
  1307. if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
  1308. return ERR_PTR(-EINVAL);
  1309. res = q = clone_mnt(mnt, dentry, flag);
  1310. if (IS_ERR(q))
  1311. return q;
  1312. q->mnt.mnt_flags &= ~MNT_LOCKED;
  1313. q->mnt_mountpoint = mnt->mnt_mountpoint;
  1314. p = mnt;
  1315. list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
  1316. struct mount *s;
  1317. if (!is_subdir(r->mnt_mountpoint, dentry))
  1318. continue;
  1319. for (s = r; s; s = next_mnt(s, r)) {
  1320. if (!(flag & CL_COPY_UNBINDABLE) &&
  1321. IS_MNT_UNBINDABLE(s)) {
  1322. s = skip_mnt_tree(s);
  1323. continue;
  1324. }
  1325. if (!(flag & CL_COPY_MNT_NS_FILE) &&
  1326. is_mnt_ns_file(s->mnt.mnt_root)) {
  1327. s = skip_mnt_tree(s);
  1328. continue;
  1329. }
  1330. while (p != s->mnt_parent) {
  1331. p = p->mnt_parent;
  1332. q = q->mnt_parent;
  1333. }
  1334. p = s;
  1335. parent = q;
  1336. q = clone_mnt(p, p->mnt.mnt_root, flag);
  1337. if (IS_ERR(q))
  1338. goto out;
  1339. lock_mount_hash();
  1340. list_add_tail(&q->mnt_list, &res->mnt_list);
  1341. attach_mnt(q, parent, p->mnt_mp);
  1342. unlock_mount_hash();
  1343. }
  1344. }
  1345. return res;
  1346. out:
  1347. if (res) {
  1348. lock_mount_hash();
  1349. umount_tree(res, 0);
  1350. unlock_mount_hash();
  1351. }
  1352. return q;
  1353. }
  1354. /* Caller should check returned pointer for errors */
  1355. struct vfsmount *collect_mounts(struct path *path)
  1356. {
  1357. struct mount *tree;
  1358. namespace_lock();
  1359. tree = copy_tree(real_mount(path->mnt), path->dentry,
  1360. CL_COPY_ALL | CL_PRIVATE);
  1361. namespace_unlock();
  1362. if (IS_ERR(tree))
  1363. return ERR_CAST(tree);
  1364. return &tree->mnt;
  1365. }
  1366. void drop_collected_mounts(struct vfsmount *mnt)
  1367. {
  1368. namespace_lock();
  1369. lock_mount_hash();
  1370. umount_tree(real_mount(mnt), 0);
  1371. unlock_mount_hash();
  1372. namespace_unlock();
  1373. }
  1374. int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
  1375. struct vfsmount *root)
  1376. {
  1377. struct mount *mnt;
  1378. int res = f(root, arg);
  1379. if (res)
  1380. return res;
  1381. list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
  1382. res = f(&mnt->mnt, arg);
  1383. if (res)
  1384. return res;
  1385. }
  1386. return 0;
  1387. }
  1388. static void cleanup_group_ids(struct mount *mnt, struct mount *end)
  1389. {
  1390. struct mount *p;
  1391. for (p = mnt; p != end; p = next_mnt(p, mnt)) {
  1392. if (p->mnt_group_id && !IS_MNT_SHARED(p))
  1393. mnt_release_group_id(p);
  1394. }
  1395. }
  1396. static int invent_group_ids(struct mount *mnt, bool recurse)
  1397. {
  1398. struct mount *p;
  1399. for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
  1400. if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
  1401. int err = mnt_alloc_group_id(p);
  1402. if (err) {
  1403. cleanup_group_ids(mnt, p);
  1404. return err;
  1405. }
  1406. }
  1407. }
  1408. return 0;
  1409. }
  1410. /*
  1411. * @source_mnt : mount tree to be attached
  1412. * @nd : place the mount tree @source_mnt is attached
  1413. * @parent_nd : if non-null, detach the source_mnt from its parent and
  1414. * store the parent mount and mountpoint dentry.
  1415. * (done when source_mnt is moved)
  1416. *
  1417. * NOTE: in the table below explains the semantics when a source mount
  1418. * of a given type is attached to a destination mount of a given type.
  1419. * ---------------------------------------------------------------------------
  1420. * | BIND MOUNT OPERATION |
  1421. * |**************************************************************************
  1422. * | source-->| shared | private | slave | unbindable |
  1423. * | dest | | | | |
  1424. * | | | | | | |
  1425. * | v | | | | |
  1426. * |**************************************************************************
  1427. * | shared | shared (++) | shared (+) | shared(+++)| invalid |
  1428. * | | | | | |
  1429. * |non-shared| shared (+) | private | slave (*) | invalid |
  1430. * ***************************************************************************
  1431. * A bind operation clones the source mount and mounts the clone on the
  1432. * destination mount.
  1433. *
  1434. * (++) the cloned mount is propagated to all the mounts in the propagation
  1435. * tree of the destination mount and the cloned mount is added to
  1436. * the peer group of the source mount.
  1437. * (+) the cloned mount is created under the destination mount and is marked
  1438. * as shared. The cloned mount is added to the peer group of the source
  1439. * mount.
  1440. * (+++) the mount is propagated to all the mounts in the propagation tree
  1441. * of the destination mount and the cloned mount is made slave
  1442. * of the same master as that of the source mount. The cloned mount
  1443. * is marked as 'shared and slave'.
  1444. * (*) the cloned mount is made a slave of the same master as that of the
  1445. * source mount.
  1446. *
  1447. * ---------------------------------------------------------------------------
  1448. * | MOVE MOUNT OPERATION |
  1449. * |**************************************************************************
  1450. * | source-->| shared | private | slave | unbindable |
  1451. * | dest | | | | |
  1452. * | | | | | | |
  1453. * | v | | | | |
  1454. * |**************************************************************************
  1455. * | shared | shared (+) | shared (+) | shared(+++) | invalid |
  1456. * | | | | | |
  1457. * |non-shared| shared (+*) | private | slave (*) | unbindable |
  1458. * ***************************************************************************
  1459. *
  1460. * (+) the mount is moved to the destination. And is then propagated to
  1461. * all the mounts in the propagation tree of the destination mount.
  1462. * (+*) the mount is moved to the destination.
  1463. * (+++) the mount is moved to the destination and is then propagated to
  1464. * all the mounts belonging to the destination mount's propagation tree.
  1465. * the mount is marked as 'shared and slave'.
  1466. * (*) the mount continues to be a slave at the new location.
  1467. *
  1468. * if the source mount is a tree, the operations explained above is
  1469. * applied to each mount in the tree.
  1470. * Must be called without spinlocks held, since this function can sleep
  1471. * in allocations.
  1472. */
  1473. static int attach_recursive_mnt(struct mount *source_mnt,
  1474. struct mount *dest_mnt,
  1475. struct mountpoint *dest_mp,
  1476. struct path *parent_path)
  1477. {
  1478. HLIST_HEAD(tree_list);
  1479. struct mount *child, *p;
  1480. struct hlist_node *n;
  1481. int err;
  1482. if (IS_MNT_SHARED(dest_mnt)) {
  1483. err = invent_group_ids(source_mnt, true);
  1484. if (err)
  1485. goto out;
  1486. err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
  1487. lock_mount_hash();
  1488. if (err)
  1489. goto out_cleanup_ids;
  1490. for (p = source_mnt; p; p = next_mnt(p, source_mnt))
  1491. set_mnt_shared(p);
  1492. } else {
  1493. lock_mount_hash();
  1494. }
  1495. if (parent_path) {
  1496. detach_mnt(source_mnt, parent_path);
  1497. attach_mnt(source_mnt, dest_mnt, dest_mp);
  1498. touch_mnt_namespace(source_mnt->mnt_ns);
  1499. } else {
  1500. mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
  1501. commit_tree(source_mnt, NULL);
  1502. }
  1503. hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
  1504. struct mount *q;
  1505. hlist_del_init(&child->mnt_hash);
  1506. q = __lookup_mnt_last(&child->mnt_parent->mnt,
  1507. child->mnt_mountpoint);
  1508. commit_tree(child, q);
  1509. }
  1510. unlock_mount_hash();
  1511. return 0;
  1512. out_cleanup_ids:
  1513. while (!hlist_empty(&tree_list)) {
  1514. child = hlist_entry(tree_list.first, struct mount, mnt_hash);
  1515. umount_tree(child, 0);
  1516. }
  1517. unlock_mount_hash();
  1518. cleanup_group_ids(source_mnt, NULL);
  1519. out:
  1520. return err;
  1521. }
  1522. static struct mountpoint *lock_mount(struct path *path)
  1523. {
  1524. struct vfsmount *mnt;
  1525. struct dentry *dentry = path->dentry;
  1526. retry:
  1527. mutex_lock(&dentry->d_inode->i_mutex);
  1528. if (unlikely(cant_mount(dentry))) {
  1529. mutex_unlock(&dentry->d_inode->i_mutex);
  1530. return ERR_PTR(-ENOENT);
  1531. }
  1532. namespace_lock();
  1533. mnt = lookup_mnt(path);
  1534. if (likely(!mnt)) {
  1535. struct mountpoint *mp = new_mountpoint(dentry);
  1536. if (IS_ERR(mp)) {
  1537. namespace_unlock();
  1538. mutex_unlock(&dentry->d_inode->i_mutex);
  1539. return mp;
  1540. }
  1541. return mp;
  1542. }
  1543. namespace_unlock();
  1544. mutex_unlock(&path->dentry->d_inode->i_mutex);
  1545. path_put(path);
  1546. path->mnt = mnt;
  1547. dentry = path->dentry = dget(mnt->mnt_root);
  1548. goto retry;
  1549. }
  1550. static void unlock_mount(struct mountpoint *where)
  1551. {
  1552. struct dentry *dentry = where->m_dentry;
  1553. put_mountpoint(where);
  1554. namespace_unlock();
  1555. mutex_unlock(&dentry->d_inode->i_mutex);
  1556. }
  1557. static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
  1558. {
  1559. if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER)
  1560. return -EINVAL;
  1561. if (S_ISDIR(mp->m_dentry->d_inode->i_mode) !=
  1562. S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode))
  1563. return -ENOTDIR;
  1564. return attach_recursive_mnt(mnt, p, mp, NULL);
  1565. }
  1566. /*
  1567. * Sanity check the flags to change_mnt_propagation.
  1568. */
  1569. static int flags_to_propagation_type(int flags)
  1570. {
  1571. int type = flags & ~(MS_REC | MS_SILENT);
  1572. /* Fail if any non-propagation flags are set */
  1573. if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
  1574. return 0;
  1575. /* Only one propagation flag should be set */
  1576. if (!is_power_of_2(type))
  1577. return 0;
  1578. return type;
  1579. }
  1580. /*
  1581. * recursively change the type of the mountpoint.
  1582. */
  1583. static int do_change_type(struct path *path, int flag)
  1584. {
  1585. struct mount *m;
  1586. struct mount *mnt = real_mount(path->mnt);
  1587. int recurse = flag & MS_REC;
  1588. int type;
  1589. int err = 0;
  1590. if (path->dentry != path->mnt->mnt_root)
  1591. return -EINVAL;
  1592. type = flags_to_propagation_type(flag);
  1593. if (!type)
  1594. return -EINVAL;
  1595. namespace_lock();
  1596. if (type == MS_SHARED) {
  1597. err = invent_group_ids(mnt, recurse);
  1598. if (err)
  1599. goto out_unlock;
  1600. }
  1601. lock_mount_hash();
  1602. for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
  1603. change_mnt_propagation(m, type);
  1604. unlock_mount_hash();
  1605. out_unlock:
  1606. namespace_unlock();
  1607. return err;
  1608. }
  1609. static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
  1610. {
  1611. struct mount *child;
  1612. list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
  1613. if (!is_subdir(child->mnt_mountpoint, dentry))
  1614. continue;
  1615. if (child->mnt.mnt_flags & MNT_LOCKED)
  1616. return true;
  1617. }
  1618. return false;
  1619. }
  1620. /*
  1621. * do loopback mount.
  1622. */
  1623. static int do_loopback(struct path *path, const char *old_name,
  1624. int recurse)
  1625. {
  1626. struct path old_path;
  1627. struct mount *mnt = NULL, *old, *parent;
  1628. struct mountpoint *mp;
  1629. int err;
  1630. if (!old_name || !*old_name)
  1631. return -EINVAL;
  1632. err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
  1633. if (err)
  1634. return err;
  1635. err = -EINVAL;
  1636. if (mnt_ns_loop(old_path.dentry))
  1637. goto out;
  1638. mp = lock_mount(path);
  1639. err = PTR_ERR(mp);
  1640. if (IS_ERR(mp))
  1641. goto out;
  1642. old = real_mount(old_path.mnt);
  1643. parent = real_mount(path->mnt);
  1644. err = -EINVAL;
  1645. if (IS_MNT_UNBINDABLE(old))
  1646. goto out2;
  1647. if (!check_mnt(parent) || !check_mnt(old))
  1648. goto out2;
  1649. if (!recurse && has_locked_children(old, old_path.dentry))
  1650. goto out2;
  1651. if (recurse)
  1652. mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE);
  1653. else
  1654. mnt = clone_mnt(old, old_path.dentry, 0);
  1655. if (IS_ERR(mnt)) {
  1656. err = PTR_ERR(mnt);
  1657. goto out2;
  1658. }
  1659. mnt->mnt.mnt_flags &= ~MNT_LOCKED;
  1660. err = graft_tree(mnt, parent, mp);
  1661. if (err) {
  1662. lock_mount_hash();
  1663. umount_tree(mnt, 0);
  1664. unlock_mount_hash();
  1665. }
  1666. out2:
  1667. unlock_mount(mp);
  1668. out:
  1669. path_put(&old_path);
  1670. return err;
  1671. }
  1672. static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
  1673. {
  1674. int error = 0;
  1675. int readonly_request = 0;
  1676. if (ms_flags & MS_RDONLY)
  1677. readonly_request = 1;
  1678. if (readonly_request == __mnt_is_readonly(mnt))
  1679. return 0;
  1680. if (mnt->mnt_flags & MNT_LOCK_READONLY)
  1681. return -EPERM;
  1682. if (readonly_request)
  1683. error = mnt_make_readonly(real_mount(mnt));
  1684. else
  1685. __mnt_unmake_readonly(real_mount(mnt));
  1686. return error;
  1687. }
  1688. /*
  1689. * change filesystem flags. dir should be a physical root of filesystem.
  1690. * If you've mounted a non-root directory somewhere and want to do remount
  1691. * on it - tough luck.
  1692. */
  1693. static int do_remount(struct path *path, int flags, int mnt_flags,
  1694. void *data)
  1695. {
  1696. int err;
  1697. struct super_block *sb = path->mnt->mnt_sb;
  1698. struct mount *mnt = real_mount(path->mnt);
  1699. if (!check_mnt(mnt))
  1700. return -EINVAL;
  1701. if (path->dentry != path->mnt->mnt_root)
  1702. return -EINVAL;
  1703. err = security_sb_remount(sb, data);
  1704. if (err)
  1705. return err;
  1706. down_write(&sb->s_umount);
  1707. if (flags & MS_BIND)
  1708. err = change_mount_flags(path->mnt, flags);
  1709. else if (!capable(CAP_SYS_ADMIN))
  1710. err = -EPERM;
  1711. else
  1712. err = do_remount_sb(sb, flags, data, 0);
  1713. if (!err) {
  1714. lock_mount_hash();
  1715. mnt_flags |= mnt->mnt.mnt_flags & MNT_PROPAGATION_MASK;
  1716. mnt->mnt.mnt_flags = mnt_flags;
  1717. touch_mnt_namespace(mnt->mnt_ns);
  1718. unlock_mount_hash();
  1719. }
  1720. up_write(&sb->s_umount);
  1721. return err;
  1722. }
  1723. static inline int tree_contains_unbindable(struct mount *mnt)
  1724. {
  1725. struct mount *p;
  1726. for (p = mnt; p; p = next_mnt(p, mnt)) {
  1727. if (IS_MNT_UNBINDABLE(p))
  1728. return 1;
  1729. }
  1730. return 0;
  1731. }
  1732. static int do_move_mount(struct path *path, const char *old_name)
  1733. {
  1734. struct path old_path, parent_path;
  1735. struct mount *p;
  1736. struct mount *old;
  1737. struct mountpoint *mp;
  1738. int err;
  1739. if (!old_name || !*old_name)
  1740. return -EINVAL;
  1741. err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
  1742. if (err)
  1743. return err;
  1744. mp = lock_mount(path);
  1745. err = PTR_ERR(mp);
  1746. if (IS_ERR(mp))
  1747. goto out;
  1748. old = real_mount(old_path.mnt);
  1749. p = real_mount(path->mnt);
  1750. err = -EINVAL;
  1751. if (!check_mnt(p) || !check_mnt(old))
  1752. goto out1;
  1753. if (old->mnt.mnt_flags & MNT_LOCKED)
  1754. goto out1;
  1755. err = -EINVAL;
  1756. if (old_path.dentry != old_path.mnt->mnt_root)
  1757. goto out1;
  1758. if (!mnt_has_parent(old))
  1759. goto out1;
  1760. if (S_ISDIR(path->dentry->d_inode->i_mode) !=
  1761. S_ISDIR(old_path.dentry->d_inode->i_mode))
  1762. goto out1;
  1763. /*
  1764. * Don't move a mount residing in a shared parent.
  1765. */
  1766. if (IS_MNT_SHARED(old->mnt_parent))
  1767. goto out1;
  1768. /*
  1769. * Don't move a mount tree containing unbindable mounts to a destination
  1770. * mount which is shared.
  1771. */
  1772. if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
  1773. goto out1;
  1774. err = -ELOOP;
  1775. for (; mnt_has_parent(p); p = p->mnt_parent)
  1776. if (p == old)
  1777. goto out1;
  1778. err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path);
  1779. if (err)
  1780. goto out1;
  1781. /* if the mount is moved, it should no longer be expire
  1782. * automatically */
  1783. list_del_init(&old->mnt_expire);
  1784. out1:
  1785. unlock_mount(mp);
  1786. out:
  1787. if (!err)
  1788. path_put(&parent_path);
  1789. path_put(&old_path);
  1790. return err;
  1791. }
  1792. static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
  1793. {
  1794. int err;
  1795. const char *subtype = strchr(fstype, '.');
  1796. if (subtype) {
  1797. subtype++;
  1798. err = -EINVAL;
  1799. if (!subtype[0])
  1800. goto err;
  1801. } else
  1802. subtype = "";
  1803. mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
  1804. err = -ENOMEM;
  1805. if (!mnt->mnt_sb->s_subtype)
  1806. goto err;
  1807. return mnt;
  1808. err:
  1809. mntput(mnt);
  1810. return ERR_PTR(err);
  1811. }
  1812. /*
  1813. * add a mount into a namespace's mount tree
  1814. */
  1815. static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
  1816. {
  1817. struct mountpoint *mp;
  1818. struct mount *parent;
  1819. int err;
  1820. mnt_flags &= ~MNT_INTERNAL_FLAGS;
  1821. mp = lock_mount(path);
  1822. if (IS_ERR(mp))
  1823. return PTR_ERR(mp);
  1824. parent = real_mount(path->mnt);
  1825. err = -EINVAL;
  1826. if (unlikely(!check_mnt(parent))) {
  1827. /* that's acceptable only for automounts done in private ns */
  1828. if (!(mnt_flags & MNT_SHRINKABLE))
  1829. goto unlock;
  1830. /* ... and for those we'd better have mountpoint still alive */
  1831. if (!parent->mnt_ns)
  1832. goto unlock;
  1833. }
  1834. /* Refuse the same filesystem on the same mount point */
  1835. err = -EBUSY;
  1836. if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
  1837. path->mnt->mnt_root == path->dentry)
  1838. goto unlock;
  1839. err = -EINVAL;
  1840. if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode))
  1841. goto unlock;
  1842. newmnt->mnt.mnt_flags = mnt_flags;
  1843. err = graft_tree(newmnt, parent, mp);
  1844. unlock:
  1845. unlock_mount(mp);
  1846. return err;
  1847. }
  1848. /*
  1849. * create a new mount for userspace and request it to be added into the
  1850. * namespace's tree
  1851. */
  1852. static int do_new_mount(struct path *path, const char *fstype, int flags,
  1853. int mnt_flags, const char *name, void *data)
  1854. {
  1855. struct file_system_type *type;
  1856. struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
  1857. struct vfsmount *mnt;
  1858. int err;
  1859. if (!fstype)
  1860. return -EINVAL;
  1861. type = get_fs_type(fstype);
  1862. if (!type)
  1863. return -ENODEV;
  1864. if (user_ns != &init_user_ns) {
  1865. if (!(type->fs_flags & FS_USERNS_MOUNT)) {
  1866. put_filesystem(type);
  1867. return -EPERM;
  1868. }
  1869. /* Only in special cases allow devices from mounts
  1870. * created outside the initial user namespace.
  1871. */
  1872. if (!(type->fs_flags & FS_USERNS_DEV_MOUNT)) {
  1873. flags |= MS_NODEV;
  1874. mnt_flags |= MNT_NODEV;
  1875. }
  1876. }
  1877. mnt = vfs_kern_mount(type, flags, name, data);
  1878. if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
  1879. !mnt->mnt_sb->s_subtype)
  1880. mnt = fs_set_subtype(mnt, fstype);
  1881. put_filesystem(type);
  1882. if (IS_ERR(mnt))
  1883. return PTR_ERR(mnt);
  1884. err = do_add_mount(real_mount(mnt), path, mnt_flags);
  1885. if (err)
  1886. mntput(mnt);
  1887. return err;
  1888. }
  1889. int finish_automount(struct vfsmount *m, struct path *path)
  1890. {
  1891. struct mount *mnt = real_mount(m);
  1892. int err;
  1893. /* The new mount record should have at least 2 refs to prevent it being
  1894. * expired before we get a chance to add it
  1895. */
  1896. BUG_ON(mnt_get_count(mnt) < 2);
  1897. if (m->mnt_sb == path->mnt->mnt_sb &&
  1898. m->mnt_root == path->dentry) {
  1899. err = -ELOOP;
  1900. goto fail;
  1901. }
  1902. err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
  1903. if (!err)
  1904. return 0;
  1905. fail:
  1906. /* remove m from any expiration list it may be on */
  1907. if (!list_empty(&mnt->mnt_expire)) {
  1908. namespace_lock();
  1909. list_del_init(&mnt->mnt_expire);
  1910. namespace_unlock();
  1911. }
  1912. mntput(m);
  1913. mntput(m);
  1914. return err;
  1915. }
  1916. /**
  1917. * mnt_set_expiry - Put a mount on an expiration list
  1918. * @mnt: The mount to list.
  1919. * @expiry_list: The list to add the mount to.
  1920. */
  1921. void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
  1922. {
  1923. namespace_lock();
  1924. list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
  1925. namespace_unlock();
  1926. }
  1927. EXPORT_SYMBOL(mnt_set_expiry);
  1928. /*
  1929. * process a list of expirable mountpoints with the intent of discarding any
  1930. * mountpoints that aren't in use and haven't been touched since last we came
  1931. * here
  1932. */
  1933. void mark_mounts_for_expiry(struct list_head *mounts)
  1934. {
  1935. struct mount *mnt, *next;
  1936. LIST_HEAD(graveyard);
  1937. if (list_empty(mounts))
  1938. return;
  1939. namespace_lock();
  1940. lock_mount_hash();
  1941. /* extract from the expiration list every vfsmount that matches the
  1942. * following criteria:
  1943. * - only referenced by its parent vfsmount
  1944. * - still marked for expiry (marked on the last call here; marks are
  1945. * cleared by mntput())
  1946. */
  1947. list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
  1948. if (!xchg(&mnt->mnt_expiry_mark, 1) ||
  1949. propagate_mount_busy(mnt, 1))
  1950. continue;
  1951. list_move(&mnt->mnt_expire, &graveyard);
  1952. }
  1953. while (!list_empty(&graveyard)) {
  1954. mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
  1955. touch_mnt_namespace(mnt->mnt_ns);
  1956. umount_tree(mnt, 1);
  1957. }
  1958. unlock_mount_hash();
  1959. namespace_unlock();
  1960. }
  1961. EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
  1962. /*
  1963. * Ripoff of 'select_parent()'
  1964. *
  1965. * search the list of submounts for a given mountpoint, and move any
  1966. * shrinkable submounts to the 'graveyard' list.
  1967. */
  1968. static int select_submounts(struct mount *parent, struct list_head *graveyard)
  1969. {
  1970. struct mount *this_parent = parent;
  1971. struct list_head *next;
  1972. int found = 0;
  1973. repeat:
  1974. next = this_parent->mnt_mounts.next;
  1975. resume:
  1976. while (next != &this_parent->mnt_mounts) {
  1977. struct list_head *tmp = next;
  1978. struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
  1979. next = tmp->next;
  1980. if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
  1981. continue;
  1982. /*
  1983. * Descend a level if the d_mounts list is non-empty.
  1984. */
  1985. if (!list_empty(&mnt->mnt_mounts)) {
  1986. this_parent = mnt;
  1987. goto repeat;
  1988. }
  1989. if (!propagate_mount_busy(mnt, 1)) {
  1990. list_move_tail(&mnt->mnt_expire, graveyard);
  1991. found++;
  1992. }
  1993. }
  1994. /*
  1995. * All done at this level ... ascend and resume the search
  1996. */
  1997. if (this_parent != parent) {
  1998. next = this_parent->mnt_child.next;
  1999. this_parent = this_parent->mnt_parent;
  2000. goto resume;
  2001. }
  2002. return found;
  2003. }
  2004. /*
  2005. * process a list of expirable mountpoints with the intent of discarding any
  2006. * submounts of a specific parent mountpoint
  2007. *
  2008. * mount_lock must be held for write
  2009. */
  2010. static void shrink_submounts(struct mount *mnt)
  2011. {
  2012. LIST_HEAD(graveyard);
  2013. struct mount *m;
  2014. /* extract submounts of 'mountpoint' from the expiration list */
  2015. while (select_submounts(mnt, &graveyard)) {
  2016. while (!list_empty(&graveyard)) {
  2017. m = list_first_entry(&graveyard, struct mount,
  2018. mnt_expire);
  2019. touch_mnt_namespace(m->mnt_ns);
  2020. umount_tree(m, 1);
  2021. }
  2022. }
  2023. }
  2024. /*
  2025. * Some copy_from_user() implementations do not return the exact number of
  2026. * bytes remaining to copy on a fault. But copy_mount_options() requires that.
  2027. * Note that this function differs from copy_from_user() in that it will oops
  2028. * on bad values of `to', rather than returning a short copy.
  2029. */
  2030. static long exact_copy_from_user(void *to, const void __user * from,
  2031. unsigned long n)
  2032. {
  2033. char *t = to;
  2034. const char __user *f = from;
  2035. char c;
  2036. if (!access_ok(VERIFY_READ, from, n))
  2037. return n;
  2038. while (n) {
  2039. if (__get_user(c, f)) {
  2040. memset(t, 0, n);
  2041. break;
  2042. }
  2043. *t++ = c;
  2044. f++;
  2045. n--;
  2046. }
  2047. return n;
  2048. }
  2049. int copy_mount_options(const void __user * data, unsigned long *where)
  2050. {
  2051. int i;
  2052. unsigned long page;
  2053. unsigned long size;
  2054. *where = 0;
  2055. if (!data)
  2056. return 0;
  2057. if (!(page = __get_free_page(GFP_KERNEL)))
  2058. return -ENOMEM;
  2059. /* We only care that *some* data at the address the user
  2060. * gave us is valid. Just in case, we'll zero
  2061. * the remainder of the page.
  2062. */
  2063. /* copy_from_user cannot cross TASK_SIZE ! */
  2064. size = TASK_SIZE - (unsigned long)data;
  2065. if (size > PAGE_SIZE)
  2066. size = PAGE_SIZE;
  2067. i = size - exact_copy_from_user((void *)page, data, size);
  2068. if (!i) {
  2069. free_page(page);
  2070. return -EFAULT;
  2071. }
  2072. if (i != PAGE_SIZE)
  2073. memset((char *)page + i, 0, PAGE_SIZE - i);
  2074. *where = page;
  2075. return 0;
  2076. }
  2077. int copy_mount_string(const void __user *data, char **where)
  2078. {
  2079. char *tmp;
  2080. if (!data) {
  2081. *where = NULL;
  2082. return 0;
  2083. }
  2084. tmp = strndup_user(data, PAGE_SIZE);
  2085. if (IS_ERR(tmp))
  2086. return PTR_ERR(tmp);
  2087. *where = tmp;
  2088. return 0;
  2089. }
  2090. /*
  2091. * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
  2092. * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
  2093. *
  2094. * data is a (void *) that can point to any structure up to
  2095. * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
  2096. * information (or be NULL).
  2097. *
  2098. * Pre-0.97 versions of mount() didn't have a flags word.
  2099. * When the flags word was introduced its top half was required
  2100. * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
  2101. * Therefore, if this magic number is present, it carries no information
  2102. * and must be discarded.
  2103. */
  2104. long do_mount(const char *dev_name, const char *dir_name,
  2105. const char *type_page, unsigned long flags, void *data_page)
  2106. {
  2107. struct path path;
  2108. int retval = 0;
  2109. int mnt_flags = 0;
  2110. /* Discard magic */
  2111. if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
  2112. flags &= ~MS_MGC_MSK;
  2113. /* Basic sanity checks */
  2114. if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
  2115. return -EINVAL;
  2116. if (data_page)
  2117. ((char *)data_page)[PAGE_SIZE - 1] = 0;
  2118. /* ... and get the mountpoint */
  2119. retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);
  2120. if (retval)
  2121. return retval;
  2122. retval = security_sb_mount(dev_name, &path,
  2123. type_page, flags, data_page);
  2124. if (!retval && !may_mount())
  2125. retval = -EPERM;
  2126. if (retval)
  2127. goto dput_out;
  2128. /* Default to relatime unless overriden */
  2129. if (!(flags & MS_NOATIME))
  2130. mnt_flags |= MNT_RELATIME;
  2131. /* Separate the per-mountpoint flags */
  2132. if (flags & MS_NOSUID)
  2133. mnt_flags |= MNT_NOSUID;
  2134. if (flags & MS_NODEV)
  2135. mnt_flags |= MNT_NODEV;
  2136. if (flags & MS_NOEXEC)
  2137. mnt_flags |= MNT_NOEXEC;
  2138. if (flags & MS_NOATIME)
  2139. mnt_flags |= MNT_NOATIME;
  2140. if (flags & MS_NODIRATIME)
  2141. mnt_flags |= MNT_NODIRATIME;
  2142. if (flags & MS_STRICTATIME)
  2143. mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
  2144. if (flags & MS_RDONLY)
  2145. mnt_flags |= MNT_READONLY;
  2146. flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
  2147. MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
  2148. MS_STRICTATIME);
  2149. if (flags & MS_REMOUNT)
  2150. retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
  2151. data_page);
  2152. else if (flags & MS_BIND)
  2153. retval = do_loopback(&path, dev_name, flags & MS_REC);
  2154. else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
  2155. retval = do_change_type(&path, flags);
  2156. else if (flags & MS_MOVE)
  2157. retval = do_move_mount(&path, dev_name);
  2158. else
  2159. retval = do_new_mount(&path, type_page, flags, mnt_flags,
  2160. dev_name, data_page);
  2161. dput_out:
  2162. path_put(&path);
  2163. return retval;
  2164. }
  2165. static void free_mnt_ns(struct mnt_namespace *ns)
  2166. {
  2167. proc_free_inum(ns->proc_inum);
  2168. put_user_ns(ns->user_ns);
  2169. kfree(ns);
  2170. }
  2171. /*
  2172. * Assign a sequence number so we can detect when we attempt to bind
  2173. * mount a reference to an older mount namespace into the current
  2174. * mount namespace, preventing reference counting loops. A 64bit
  2175. * number incrementing at 10Ghz will take 12,427 years to wrap which
  2176. * is effectively never, so we can ignore the possibility.
  2177. */
  2178. static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
  2179. static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns)
  2180. {
  2181. struct mnt_namespace *new_ns;
  2182. int ret;
  2183. new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
  2184. if (!new_ns)
  2185. return ERR_PTR(-ENOMEM);
  2186. ret = proc_alloc_inum(&new_ns->proc_inum);
  2187. if (ret) {
  2188. kfree(new_ns);
  2189. return ERR_PTR(ret);
  2190. }
  2191. new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
  2192. atomic_set(&new_ns->count, 1);
  2193. new_ns->root = NULL;
  2194. INIT_LIST_HEAD(&new_ns->list);
  2195. init_waitqueue_head(&new_ns->poll);
  2196. new_ns->event = 0;
  2197. new_ns->user_ns = get_user_ns(user_ns);
  2198. return new_ns;
  2199. }
  2200. struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
  2201. struct user_namespace *user_ns, struct fs_struct *new_fs)
  2202. {
  2203. struct mnt_namespace *new_ns;
  2204. struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
  2205. struct mount *p, *q;
  2206. struct mount *old;
  2207. struct mount *new;
  2208. int copy_flags;
  2209. BUG_ON(!ns);
  2210. if (likely(!(flags & CLONE_NEWNS))) {
  2211. get_mnt_ns(ns);
  2212. return ns;
  2213. }
  2214. old = ns->root;
  2215. new_ns = alloc_mnt_ns(user_ns);
  2216. if (IS_ERR(new_ns))
  2217. return new_ns;
  2218. namespace_lock();
  2219. /* First pass: copy the tree topology */
  2220. copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
  2221. if (user_ns != ns->user_ns)
  2222. copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED;
  2223. new = copy_tree(old, old->mnt.mnt_root, copy_flags);
  2224. if (IS_ERR(new)) {
  2225. namespace_unlock();
  2226. free_mnt_ns(new_ns);
  2227. return ERR_CAST(new);
  2228. }
  2229. new_ns->root = new;
  2230. list_add_tail(&new_ns->list, &new->mnt_list);
  2231. /*
  2232. * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
  2233. * as belonging to new namespace. We have already acquired a private
  2234. * fs_struct, so tsk->fs->lock is not needed.
  2235. */
  2236. p = old;
  2237. q = new;
  2238. while (p) {
  2239. q->mnt_ns = new_ns;
  2240. if (new_fs) {
  2241. if (&p->mnt == new_fs->root.mnt) {
  2242. new_fs->root.mnt = mntget(&q->mnt);
  2243. rootmnt = &p->mnt;
  2244. }
  2245. if (&p->mnt == new_fs->pwd.mnt) {
  2246. new_fs->pwd.mnt = mntget(&q->mnt);
  2247. pwdmnt = &p->mnt;
  2248. }
  2249. }
  2250. p = next_mnt(p, old);
  2251. q = next_mnt(q, new);
  2252. if (!q)
  2253. break;
  2254. while (p->mnt.mnt_root != q->mnt.mnt_root)
  2255. p = next_mnt(p, old);
  2256. }
  2257. namespace_unlock();
  2258. if (rootmnt)
  2259. mntput(rootmnt);
  2260. if (pwdmnt)
  2261. mntput(pwdmnt);
  2262. return new_ns;
  2263. }
  2264. /**
  2265. * create_mnt_ns - creates a private namespace and adds a root filesystem
  2266. * @mnt: pointer to the new root filesystem mountpoint
  2267. */
  2268. static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
  2269. {
  2270. struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns);
  2271. if (!IS_ERR(new_ns)) {
  2272. struct mount *mnt = real_mount(m);
  2273. mnt->mnt_ns = new_ns;
  2274. new_ns->root = mnt;
  2275. list_add(&mnt->mnt_list, &new_ns->list);
  2276. } else {
  2277. mntput(m);
  2278. }
  2279. return new_ns;
  2280. }
  2281. struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
  2282. {
  2283. struct mnt_namespace *ns;
  2284. struct super_block *s;
  2285. struct path path;
  2286. int err;
  2287. ns = create_mnt_ns(mnt);
  2288. if (IS_ERR(ns))
  2289. return ERR_CAST(ns);
  2290. err = vfs_path_lookup(mnt->mnt_root, mnt,
  2291. name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
  2292. put_mnt_ns(ns);
  2293. if (err)
  2294. return ERR_PTR(err);
  2295. /* trade a vfsmount reference for active sb one */
  2296. s = path.mnt->mnt_sb;
  2297. atomic_inc(&s->s_active);
  2298. mntput(path.mnt);
  2299. /* lock the sucker */
  2300. down_write(&s->s_umount);
  2301. /* ... and return the root of (sub)tree on it */
  2302. return path.dentry;
  2303. }
  2304. EXPORT_SYMBOL(mount_subtree);
  2305. SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
  2306. char __user *, type, unsigned long, flags, void __user *, data)
  2307. {
  2308. int ret;
  2309. char *kernel_type;
  2310. struct filename *kernel_dir;
  2311. char *kernel_dev;
  2312. unsigned long data_page;
  2313. ret = copy_mount_string(type, &kernel_type);
  2314. if (ret < 0)
  2315. goto out_type;
  2316. kernel_dir = getname(dir_name);
  2317. if (IS_ERR(kernel_dir)) {
  2318. ret = PTR_ERR(kernel_dir);
  2319. goto out_dir;
  2320. }
  2321. ret = copy_mount_string(dev_name, &kernel_dev);
  2322. if (ret < 0)
  2323. goto out_dev;
  2324. ret = copy_mount_options(data, &data_page);
  2325. if (ret < 0)
  2326. goto out_data;
  2327. ret = do_mount(kernel_dev, kernel_dir->name, kernel_type, flags,
  2328. (void *) data_page);
  2329. free_page(data_page);
  2330. out_data:
  2331. kfree(kernel_dev);
  2332. out_dev:
  2333. putname(kernel_dir);
  2334. out_dir:
  2335. kfree(kernel_type);
  2336. out_type:
  2337. return ret;
  2338. }
  2339. /*
  2340. * Return true if path is reachable from root
  2341. *
  2342. * namespace_sem or mount_lock is held
  2343. */
  2344. bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
  2345. const struct path *root)
  2346. {
  2347. while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
  2348. dentry = mnt->mnt_mountpoint;
  2349. mnt = mnt->mnt_parent;
  2350. }
  2351. return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
  2352. }
  2353. int path_is_under(struct path *path1, struct path *path2)
  2354. {
  2355. int res;
  2356. read_seqlock_excl(&mount_lock);
  2357. res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
  2358. read_sequnlock_excl(&mount_lock);
  2359. return res;
  2360. }
  2361. EXPORT_SYMBOL(path_is_under);
  2362. /*
  2363. * pivot_root Semantics:
  2364. * Moves the root file system of the current process to the directory put_old,
  2365. * makes new_root as the new root file system of the current process, and sets
  2366. * root/cwd of all processes which had them on the current root to new_root.
  2367. *
  2368. * Restrictions:
  2369. * The new_root and put_old must be directories, and must not be on the
  2370. * same file system as the current process root. The put_old must be
  2371. * underneath new_root, i.e. adding a non-zero number of /.. to the string
  2372. * pointed to by put_old must yield the same directory as new_root. No other
  2373. * file system may be mounted on put_old. After all, new_root is a mountpoint.
  2374. *
  2375. * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
  2376. * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
  2377. * in this situation.
  2378. *
  2379. * Notes:
  2380. * - we don't move root/cwd if they are not at the root (reason: if something
  2381. * cared enough to change them, it's probably wrong to force them elsewhere)
  2382. * - it's okay to pick a root that isn't the root of a file system, e.g.
  2383. * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
  2384. * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
  2385. * first.
  2386. */
  2387. SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
  2388. const char __user *, put_old)
  2389. {
  2390. struct path new, old, parent_path, root_parent, root;
  2391. struct mount *new_mnt, *root_mnt, *old_mnt;
  2392. struct mountpoint *old_mp, *root_mp;
  2393. int error;
  2394. if (!may_mount())
  2395. return -EPERM;
  2396. error = user_path_dir(new_root, &new);
  2397. if (error)
  2398. goto out0;
  2399. error = user_path_dir(put_old, &old);
  2400. if (error)
  2401. goto out1;
  2402. error = security_sb_pivotroot(&old, &new);
  2403. if (error)
  2404. goto out2;
  2405. get_fs_root(current->fs, &root);
  2406. old_mp = lock_mount(&old);
  2407. error = PTR_ERR(old_mp);
  2408. if (IS_ERR(old_mp))
  2409. goto out3;
  2410. error = -EINVAL;
  2411. new_mnt = real_mount(new.mnt);
  2412. root_mnt = real_mount(root.mnt);
  2413. old_mnt = real_mount(old.mnt);
  2414. if (IS_MNT_SHARED(old_mnt) ||
  2415. IS_MNT_SHARED(new_mnt->mnt_parent) ||
  2416. IS_MNT_SHARED(root_mnt->mnt_parent))
  2417. goto out4;
  2418. if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
  2419. goto out4;
  2420. if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
  2421. goto out4;
  2422. error = -ENOENT;
  2423. if (d_unlinked(new.dentry))
  2424. goto out4;
  2425. error = -EBUSY;
  2426. if (new_mnt == root_mnt || old_mnt == root_mnt)
  2427. goto out4; /* loop, on the same file system */
  2428. error = -EINVAL;
  2429. if (root.mnt->mnt_root != root.dentry)
  2430. goto out4; /* not a mountpoint */
  2431. if (!mnt_has_parent(root_mnt))
  2432. goto out4; /* not attached */
  2433. root_mp = root_mnt->mnt_mp;
  2434. if (new.mnt->mnt_root != new.dentry)
  2435. goto out4; /* not a mountpoint */
  2436. if (!mnt_has_parent(new_mnt))
  2437. goto out4; /* not attached */
  2438. /* make sure we can reach put_old from new_root */
  2439. if (!is_path_reachable(old_mnt, old.dentry, &new))
  2440. goto out4;
  2441. root_mp->m_count++; /* pin it so it won't go away */
  2442. lock_mount_hash();
  2443. detach_mnt(new_mnt, &parent_path);
  2444. detach_mnt(root_mnt, &root_parent);
  2445. if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
  2446. new_mnt->mnt.mnt_flags |= MNT_LOCKED;
  2447. root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
  2448. }
  2449. /* mount old root on put_old */
  2450. attach_mnt(root_mnt, old_mnt, old_mp);
  2451. /* mount new_root on / */
  2452. attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp);
  2453. touch_mnt_namespace(current->nsproxy->mnt_ns);
  2454. unlock_mount_hash();
  2455. chroot_fs_refs(&root, &new);
  2456. put_mountpoint(root_mp);
  2457. error = 0;
  2458. out4:
  2459. unlock_mount(old_mp);
  2460. if (!error) {
  2461. path_put(&root_parent);
  2462. path_put(&parent_path);
  2463. }
  2464. out3:
  2465. path_put(&root);
  2466. out2:
  2467. path_put(&old);
  2468. out1:
  2469. path_put(&new);
  2470. out0:
  2471. return error;
  2472. }
  2473. static void __init init_mount_tree(void)
  2474. {
  2475. struct vfsmount *mnt;
  2476. struct mnt_namespace *ns;
  2477. struct path root;
  2478. struct file_system_type *type;
  2479. type = get_fs_type("rootfs");
  2480. if (!type)
  2481. panic("Can't find rootfs type");
  2482. mnt = vfs_kern_mount(type, 0, "rootfs", NULL);
  2483. put_filesystem(type);
  2484. if (IS_ERR(mnt))
  2485. panic("Can't create rootfs");
  2486. ns = create_mnt_ns(mnt);
  2487. if (IS_ERR(ns))
  2488. panic("Can't allocate initial namespace");
  2489. init_task.nsproxy->mnt_ns = ns;
  2490. get_mnt_ns(ns);
  2491. root.mnt = mnt;
  2492. root.dentry = mnt->mnt_root;
  2493. set_fs_pwd(current->fs, &root);
  2494. set_fs_root(current->fs, &root);
  2495. }
  2496. void __init mnt_init(void)
  2497. {
  2498. unsigned u;
  2499. int err;
  2500. mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
  2501. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  2502. mount_hashtable = alloc_large_system_hash("Mount-cache",
  2503. sizeof(struct hlist_head),
  2504. mhash_entries, 19,
  2505. 0,
  2506. &m_hash_shift, &m_hash_mask, 0, 0);
  2507. mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
  2508. sizeof(struct hlist_head),
  2509. mphash_entries, 19,
  2510. 0,
  2511. &mp_hash_shift, &mp_hash_mask, 0, 0);
  2512. if (!mount_hashtable || !mountpoint_hashtable)
  2513. panic("Failed to allocate mount hash table\n");
  2514. for (u = 0; u <= m_hash_mask; u++)
  2515. INIT_HLIST_HEAD(&mount_hashtable[u]);
  2516. for (u = 0; u <= mp_hash_mask; u++)
  2517. INIT_HLIST_HEAD(&mountpoint_hashtable[u]);
  2518. kernfs_init();
  2519. err = sysfs_init();
  2520. if (err)
  2521. printk(KERN_WARNING "%s: sysfs_init error: %d\n",
  2522. __func__, err);
  2523. fs_kobj = kobject_create_and_add("fs", NULL);
  2524. if (!fs_kobj)
  2525. printk(KERN_WARNING "%s: kobj create error\n", __func__);
  2526. init_rootfs();
  2527. init_mount_tree();
  2528. }
  2529. void put_mnt_ns(struct mnt_namespace *ns)
  2530. {
  2531. if (!atomic_dec_and_test(&ns->count))
  2532. return;
  2533. drop_collected_mounts(&ns->root->mnt);
  2534. free_mnt_ns(ns);
  2535. }
  2536. struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
  2537. {
  2538. struct vfsmount *mnt;
  2539. mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
  2540. if (!IS_ERR(mnt)) {
  2541. /*
  2542. * it is a longterm mount, don't release mnt until
  2543. * we unmount before file sys is unregistered
  2544. */
  2545. real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
  2546. }
  2547. return mnt;
  2548. }
  2549. EXPORT_SYMBOL_GPL(kern_mount_data);
  2550. void kern_unmount(struct vfsmount *mnt)
  2551. {
  2552. /* release long term mount so mount point can be released */
  2553. if (!IS_ERR_OR_NULL(mnt)) {
  2554. real_mount(mnt)->mnt_ns = NULL;
  2555. synchronize_rcu(); /* yecchhh... */
  2556. mntput(mnt);
  2557. }
  2558. }
  2559. EXPORT_SYMBOL(kern_unmount);
  2560. bool our_mnt(struct vfsmount *mnt)
  2561. {
  2562. return check_mnt(real_mount(mnt));
  2563. }
  2564. bool current_chrooted(void)
  2565. {
  2566. /* Does the current process have a non-standard root */
  2567. struct path ns_root;
  2568. struct path fs_root;
  2569. bool chrooted;
  2570. /* Find the namespace root */
  2571. ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
  2572. ns_root.dentry = ns_root.mnt->mnt_root;
  2573. path_get(&ns_root);
  2574. while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
  2575. ;
  2576. get_fs_root(current->fs, &fs_root);
  2577. chrooted = !path_equal(&fs_root, &ns_root);
  2578. path_put(&fs_root);
  2579. path_put(&ns_root);
  2580. return chrooted;
  2581. }
  2582. bool fs_fully_visible(struct file_system_type *type)
  2583. {
  2584. struct mnt_namespace *ns = current->nsproxy->mnt_ns;
  2585. struct mount *mnt;
  2586. bool visible = false;
  2587. if (unlikely(!ns))
  2588. return false;
  2589. down_read(&namespace_sem);
  2590. list_for_each_entry(mnt, &ns->list, mnt_list) {
  2591. struct mount *child;
  2592. if (mnt->mnt.mnt_sb->s_type != type)
  2593. continue;
  2594. /* This mount is not fully visible if there are any child mounts
  2595. * that cover anything except for empty directories.
  2596. */
  2597. list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
  2598. struct inode *inode = child->mnt_mountpoint->d_inode;
  2599. if (!S_ISDIR(inode->i_mode))
  2600. goto next;
  2601. if (inode->i_nlink > 2)
  2602. goto next;
  2603. }
  2604. visible = true;
  2605. goto found;
  2606. next: ;
  2607. }
  2608. found:
  2609. up_read(&namespace_sem);
  2610. return visible;
  2611. }
  2612. static void *mntns_get(struct task_struct *task)
  2613. {
  2614. struct mnt_namespace *ns = NULL;
  2615. struct nsproxy *nsproxy;
  2616. rcu_read_lock();
  2617. nsproxy = task_nsproxy(task);
  2618. if (nsproxy) {
  2619. ns = nsproxy->mnt_ns;
  2620. get_mnt_ns(ns);
  2621. }
  2622. rcu_read_unlock();
  2623. return ns;
  2624. }
  2625. static void mntns_put(void *ns)
  2626. {
  2627. put_mnt_ns(ns);
  2628. }
  2629. static int mntns_install(struct nsproxy *nsproxy, void *ns)
  2630. {
  2631. struct fs_struct *fs = current->fs;
  2632. struct mnt_namespace *mnt_ns = ns;
  2633. struct path root;
  2634. if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
  2635. !ns_capable(current_user_ns(), CAP_SYS_CHROOT) ||
  2636. !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
  2637. return -EPERM;
  2638. if (fs->users != 1)
  2639. return -EINVAL;
  2640. get_mnt_ns(mnt_ns);
  2641. put_mnt_ns(nsproxy->mnt_ns);
  2642. nsproxy->mnt_ns = mnt_ns;
  2643. /* Find the root */
  2644. root.mnt = &mnt_ns->root->mnt;
  2645. root.dentry = mnt_ns->root->mnt.mnt_root;
  2646. path_get(&root);
  2647. while(d_mountpoint(root.dentry) && follow_down_one(&root))
  2648. ;
  2649. /* Update the pwd and root */
  2650. set_fs_pwd(fs, &root);
  2651. set_fs_root(fs, &root);
  2652. path_put(&root);
  2653. return 0;
  2654. }
  2655. static unsigned int mntns_inum(void *ns)
  2656. {
  2657. struct mnt_namespace *mnt_ns = ns;
  2658. return mnt_ns->proc_inum;
  2659. }
  2660. const struct proc_ns_operations mntns_operations = {
  2661. .name = "mnt",
  2662. .type = CLONE_NEWNS,
  2663. .get = mntns_get,
  2664. .put = mntns_put,
  2665. .install = mntns_install,
  2666. .inum = mntns_inum,
  2667. };