namespace.c 71 KB

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