namespace.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225
  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/slab.h>
  12. #include <linux/sched.h>
  13. #include <linux/smp_lock.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/acct.h>
  17. #include <linux/capability.h>
  18. #include <linux/cpumask.h>
  19. #include <linux/module.h>
  20. #include <linux/sysfs.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/mnt_namespace.h>
  23. #include <linux/namei.h>
  24. #include <linux/security.h>
  25. #include <linux/mount.h>
  26. #include <linux/ramfs.h>
  27. #include <linux/log2.h>
  28. #include <linux/idr.h>
  29. #include <linux/fs_struct.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/unistd.h>
  32. #include "pnode.h"
  33. #include "internal.h"
  34. #define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head))
  35. #define HASH_SIZE (1UL << HASH_SHIFT)
  36. /* spinlock for vfsmount related operations, inplace of dcache_lock */
  37. __cacheline_aligned_in_smp DEFINE_SPINLOCK(vfsmount_lock);
  38. static int event;
  39. static DEFINE_IDA(mnt_id_ida);
  40. static DEFINE_IDA(mnt_group_ida);
  41. static struct list_head *mount_hashtable __read_mostly;
  42. static struct kmem_cache *mnt_cache __read_mostly;
  43. static struct rw_semaphore namespace_sem;
  44. /* /sys/fs */
  45. struct kobject *fs_kobj;
  46. EXPORT_SYMBOL_GPL(fs_kobj);
  47. static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
  48. {
  49. unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
  50. tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
  51. tmp = tmp + (tmp >> HASH_SHIFT);
  52. return tmp & (HASH_SIZE - 1);
  53. }
  54. #define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16)
  55. /* allocation is serialized by namespace_sem */
  56. static int mnt_alloc_id(struct vfsmount *mnt)
  57. {
  58. int res;
  59. retry:
  60. ida_pre_get(&mnt_id_ida, GFP_KERNEL);
  61. spin_lock(&vfsmount_lock);
  62. res = ida_get_new(&mnt_id_ida, &mnt->mnt_id);
  63. spin_unlock(&vfsmount_lock);
  64. if (res == -EAGAIN)
  65. goto retry;
  66. return res;
  67. }
  68. static void mnt_free_id(struct vfsmount *mnt)
  69. {
  70. spin_lock(&vfsmount_lock);
  71. ida_remove(&mnt_id_ida, mnt->mnt_id);
  72. spin_unlock(&vfsmount_lock);
  73. }
  74. /*
  75. * Allocate a new peer group ID
  76. *
  77. * mnt_group_ida is protected by namespace_sem
  78. */
  79. static int mnt_alloc_group_id(struct vfsmount *mnt)
  80. {
  81. if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
  82. return -ENOMEM;
  83. return ida_get_new_above(&mnt_group_ida, 1, &mnt->mnt_group_id);
  84. }
  85. /*
  86. * Release a peer group ID
  87. */
  88. void mnt_release_group_id(struct vfsmount *mnt)
  89. {
  90. ida_remove(&mnt_group_ida, mnt->mnt_group_id);
  91. mnt->mnt_group_id = 0;
  92. }
  93. struct vfsmount *alloc_vfsmnt(const char *name)
  94. {
  95. struct vfsmount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
  96. if (mnt) {
  97. int err;
  98. err = mnt_alloc_id(mnt);
  99. if (err)
  100. goto out_free_cache;
  101. if (name) {
  102. mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
  103. if (!mnt->mnt_devname)
  104. goto out_free_id;
  105. }
  106. atomic_set(&mnt->mnt_count, 1);
  107. INIT_LIST_HEAD(&mnt->mnt_hash);
  108. INIT_LIST_HEAD(&mnt->mnt_child);
  109. INIT_LIST_HEAD(&mnt->mnt_mounts);
  110. INIT_LIST_HEAD(&mnt->mnt_list);
  111. INIT_LIST_HEAD(&mnt->mnt_expire);
  112. INIT_LIST_HEAD(&mnt->mnt_share);
  113. INIT_LIST_HEAD(&mnt->mnt_slave_list);
  114. INIT_LIST_HEAD(&mnt->mnt_slave);
  115. #ifdef CONFIG_SMP
  116. mnt->mnt_writers = alloc_percpu(int);
  117. if (!mnt->mnt_writers)
  118. goto out_free_devname;
  119. #else
  120. mnt->mnt_writers = 0;
  121. #endif
  122. }
  123. return mnt;
  124. #ifdef CONFIG_SMP
  125. out_free_devname:
  126. kfree(mnt->mnt_devname);
  127. #endif
  128. out_free_id:
  129. mnt_free_id(mnt);
  130. out_free_cache:
  131. kmem_cache_free(mnt_cache, mnt);
  132. return NULL;
  133. }
  134. /*
  135. * Most r/o checks on a fs are for operations that take
  136. * discrete amounts of time, like a write() or unlink().
  137. * We must keep track of when those operations start
  138. * (for permission checks) and when they end, so that
  139. * we can determine when writes are able to occur to
  140. * a filesystem.
  141. */
  142. /*
  143. * __mnt_is_readonly: check whether a mount is read-only
  144. * @mnt: the mount to check for its write status
  145. *
  146. * This shouldn't be used directly ouside of the VFS.
  147. * It does not guarantee that the filesystem will stay
  148. * r/w, just that it is right *now*. This can not and
  149. * should not be used in place of IS_RDONLY(inode).
  150. * mnt_want/drop_write() will _keep_ the filesystem
  151. * r/w.
  152. */
  153. int __mnt_is_readonly(struct vfsmount *mnt)
  154. {
  155. if (mnt->mnt_flags & MNT_READONLY)
  156. return 1;
  157. if (mnt->mnt_sb->s_flags & MS_RDONLY)
  158. return 1;
  159. return 0;
  160. }
  161. EXPORT_SYMBOL_GPL(__mnt_is_readonly);
  162. static inline void inc_mnt_writers(struct vfsmount *mnt)
  163. {
  164. #ifdef CONFIG_SMP
  165. (*per_cpu_ptr(mnt->mnt_writers, smp_processor_id()))++;
  166. #else
  167. mnt->mnt_writers++;
  168. #endif
  169. }
  170. static inline void dec_mnt_writers(struct vfsmount *mnt)
  171. {
  172. #ifdef CONFIG_SMP
  173. (*per_cpu_ptr(mnt->mnt_writers, smp_processor_id()))--;
  174. #else
  175. mnt->mnt_writers--;
  176. #endif
  177. }
  178. static unsigned int count_mnt_writers(struct vfsmount *mnt)
  179. {
  180. #ifdef CONFIG_SMP
  181. unsigned int count = 0;
  182. int cpu;
  183. for_each_possible_cpu(cpu) {
  184. count += *per_cpu_ptr(mnt->mnt_writers, cpu);
  185. }
  186. return count;
  187. #else
  188. return mnt->mnt_writers;
  189. #endif
  190. }
  191. /*
  192. * Most r/o checks on a fs are for operations that take
  193. * discrete amounts of time, like a write() or unlink().
  194. * We must keep track of when those operations start
  195. * (for permission checks) and when they end, so that
  196. * we can determine when writes are able to occur to
  197. * a filesystem.
  198. */
  199. /**
  200. * mnt_want_write - get write access to a mount
  201. * @mnt: the mount on which to take a write
  202. *
  203. * This tells the low-level filesystem that a write is
  204. * about to be performed to it, and makes sure that
  205. * writes are allowed before returning success. When
  206. * the write operation is finished, mnt_drop_write()
  207. * must be called. This is effectively a refcount.
  208. */
  209. int mnt_want_write(struct vfsmount *mnt)
  210. {
  211. int ret = 0;
  212. preempt_disable();
  213. inc_mnt_writers(mnt);
  214. /*
  215. * The store to inc_mnt_writers must be visible before we pass
  216. * MNT_WRITE_HOLD loop below, so that the slowpath can see our
  217. * incremented count after it has set MNT_WRITE_HOLD.
  218. */
  219. smp_mb();
  220. while (mnt->mnt_flags & MNT_WRITE_HOLD)
  221. cpu_relax();
  222. /*
  223. * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
  224. * be set to match its requirements. So we must not load that until
  225. * MNT_WRITE_HOLD is cleared.
  226. */
  227. smp_rmb();
  228. if (__mnt_is_readonly(mnt)) {
  229. dec_mnt_writers(mnt);
  230. ret = -EROFS;
  231. goto out;
  232. }
  233. out:
  234. preempt_enable();
  235. return ret;
  236. }
  237. EXPORT_SYMBOL_GPL(mnt_want_write);
  238. /**
  239. * mnt_drop_write - give up write access to a mount
  240. * @mnt: the mount on which to give up write access
  241. *
  242. * Tells the low-level filesystem that we are done
  243. * performing writes to it. Must be matched with
  244. * mnt_want_write() call above.
  245. */
  246. void mnt_drop_write(struct vfsmount *mnt)
  247. {
  248. preempt_disable();
  249. dec_mnt_writers(mnt);
  250. preempt_enable();
  251. }
  252. EXPORT_SYMBOL_GPL(mnt_drop_write);
  253. static int mnt_make_readonly(struct vfsmount *mnt)
  254. {
  255. int ret = 0;
  256. spin_lock(&vfsmount_lock);
  257. mnt->mnt_flags |= MNT_WRITE_HOLD;
  258. /*
  259. * After storing MNT_WRITE_HOLD, we'll read the counters. This store
  260. * should be visible before we do.
  261. */
  262. smp_mb();
  263. /*
  264. * With writers on hold, if this value is zero, then there are
  265. * definitely no active writers (although held writers may subsequently
  266. * increment the count, they'll have to wait, and decrement it after
  267. * seeing MNT_READONLY).
  268. *
  269. * It is OK to have counter incremented on one CPU and decremented on
  270. * another: the sum will add up correctly. The danger would be when we
  271. * sum up each counter, if we read a counter before it is incremented,
  272. * but then read another CPU's count which it has been subsequently
  273. * decremented from -- we would see more decrements than we should.
  274. * MNT_WRITE_HOLD protects against this scenario, because
  275. * mnt_want_write first increments count, then smp_mb, then spins on
  276. * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
  277. * we're counting up here.
  278. */
  279. if (count_mnt_writers(mnt) > 0)
  280. ret = -EBUSY;
  281. else
  282. mnt->mnt_flags |= MNT_READONLY;
  283. /*
  284. * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
  285. * that become unheld will see MNT_READONLY.
  286. */
  287. smp_wmb();
  288. mnt->mnt_flags &= ~MNT_WRITE_HOLD;
  289. spin_unlock(&vfsmount_lock);
  290. return ret;
  291. }
  292. static void __mnt_unmake_readonly(struct vfsmount *mnt)
  293. {
  294. spin_lock(&vfsmount_lock);
  295. mnt->mnt_flags &= ~MNT_READONLY;
  296. spin_unlock(&vfsmount_lock);
  297. }
  298. void simple_set_mnt(struct vfsmount *mnt, struct super_block *sb)
  299. {
  300. mnt->mnt_sb = sb;
  301. mnt->mnt_root = dget(sb->s_root);
  302. }
  303. EXPORT_SYMBOL(simple_set_mnt);
  304. void free_vfsmnt(struct vfsmount *mnt)
  305. {
  306. kfree(mnt->mnt_devname);
  307. mnt_free_id(mnt);
  308. #ifdef CONFIG_SMP
  309. free_percpu(mnt->mnt_writers);
  310. #endif
  311. kmem_cache_free(mnt_cache, mnt);
  312. }
  313. /*
  314. * find the first or last mount at @dentry on vfsmount @mnt depending on
  315. * @dir. If @dir is set return the first mount else return the last mount.
  316. */
  317. struct vfsmount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
  318. int dir)
  319. {
  320. struct list_head *head = mount_hashtable + hash(mnt, dentry);
  321. struct list_head *tmp = head;
  322. struct vfsmount *p, *found = NULL;
  323. for (;;) {
  324. tmp = dir ? tmp->next : tmp->prev;
  325. p = NULL;
  326. if (tmp == head)
  327. break;
  328. p = list_entry(tmp, struct vfsmount, mnt_hash);
  329. if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
  330. found = p;
  331. break;
  332. }
  333. }
  334. return found;
  335. }
  336. /*
  337. * lookup_mnt increments the ref count before returning
  338. * the vfsmount struct.
  339. */
  340. struct vfsmount *lookup_mnt(struct path *path)
  341. {
  342. struct vfsmount *child_mnt;
  343. spin_lock(&vfsmount_lock);
  344. if ((child_mnt = __lookup_mnt(path->mnt, path->dentry, 1)))
  345. mntget(child_mnt);
  346. spin_unlock(&vfsmount_lock);
  347. return child_mnt;
  348. }
  349. static inline int check_mnt(struct vfsmount *mnt)
  350. {
  351. return mnt->mnt_ns == current->nsproxy->mnt_ns;
  352. }
  353. static void touch_mnt_namespace(struct mnt_namespace *ns)
  354. {
  355. if (ns) {
  356. ns->event = ++event;
  357. wake_up_interruptible(&ns->poll);
  358. }
  359. }
  360. static void __touch_mnt_namespace(struct mnt_namespace *ns)
  361. {
  362. if (ns && ns->event != event) {
  363. ns->event = event;
  364. wake_up_interruptible(&ns->poll);
  365. }
  366. }
  367. static void detach_mnt(struct vfsmount *mnt, struct path *old_path)
  368. {
  369. old_path->dentry = mnt->mnt_mountpoint;
  370. old_path->mnt = mnt->mnt_parent;
  371. mnt->mnt_parent = mnt;
  372. mnt->mnt_mountpoint = mnt->mnt_root;
  373. list_del_init(&mnt->mnt_child);
  374. list_del_init(&mnt->mnt_hash);
  375. old_path->dentry->d_mounted--;
  376. }
  377. void mnt_set_mountpoint(struct vfsmount *mnt, struct dentry *dentry,
  378. struct vfsmount *child_mnt)
  379. {
  380. child_mnt->mnt_parent = mntget(mnt);
  381. child_mnt->mnt_mountpoint = dget(dentry);
  382. dentry->d_mounted++;
  383. }
  384. static void attach_mnt(struct vfsmount *mnt, struct path *path)
  385. {
  386. mnt_set_mountpoint(path->mnt, path->dentry, mnt);
  387. list_add_tail(&mnt->mnt_hash, mount_hashtable +
  388. hash(path->mnt, path->dentry));
  389. list_add_tail(&mnt->mnt_child, &path->mnt->mnt_mounts);
  390. }
  391. /*
  392. * the caller must hold vfsmount_lock
  393. */
  394. static void commit_tree(struct vfsmount *mnt)
  395. {
  396. struct vfsmount *parent = mnt->mnt_parent;
  397. struct vfsmount *m;
  398. LIST_HEAD(head);
  399. struct mnt_namespace *n = parent->mnt_ns;
  400. BUG_ON(parent == mnt);
  401. list_add_tail(&head, &mnt->mnt_list);
  402. list_for_each_entry(m, &head, mnt_list)
  403. m->mnt_ns = n;
  404. list_splice(&head, n->list.prev);
  405. list_add_tail(&mnt->mnt_hash, mount_hashtable +
  406. hash(parent, mnt->mnt_mountpoint));
  407. list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
  408. touch_mnt_namespace(n);
  409. }
  410. static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
  411. {
  412. struct list_head *next = p->mnt_mounts.next;
  413. if (next == &p->mnt_mounts) {
  414. while (1) {
  415. if (p == root)
  416. return NULL;
  417. next = p->mnt_child.next;
  418. if (next != &p->mnt_parent->mnt_mounts)
  419. break;
  420. p = p->mnt_parent;
  421. }
  422. }
  423. return list_entry(next, struct vfsmount, mnt_child);
  424. }
  425. static struct vfsmount *skip_mnt_tree(struct vfsmount *p)
  426. {
  427. struct list_head *prev = p->mnt_mounts.prev;
  428. while (prev != &p->mnt_mounts) {
  429. p = list_entry(prev, struct vfsmount, mnt_child);
  430. prev = p->mnt_mounts.prev;
  431. }
  432. return p;
  433. }
  434. static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root,
  435. int flag)
  436. {
  437. struct super_block *sb = old->mnt_sb;
  438. struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
  439. if (mnt) {
  440. if (flag & (CL_SLAVE | CL_PRIVATE))
  441. mnt->mnt_group_id = 0; /* not a peer of original */
  442. else
  443. mnt->mnt_group_id = old->mnt_group_id;
  444. if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
  445. int err = mnt_alloc_group_id(mnt);
  446. if (err)
  447. goto out_free;
  448. }
  449. mnt->mnt_flags = old->mnt_flags;
  450. atomic_inc(&sb->s_active);
  451. mnt->mnt_sb = sb;
  452. mnt->mnt_root = dget(root);
  453. mnt->mnt_mountpoint = mnt->mnt_root;
  454. mnt->mnt_parent = mnt;
  455. if (flag & CL_SLAVE) {
  456. list_add(&mnt->mnt_slave, &old->mnt_slave_list);
  457. mnt->mnt_master = old;
  458. CLEAR_MNT_SHARED(mnt);
  459. } else if (!(flag & CL_PRIVATE)) {
  460. if ((flag & CL_PROPAGATION) || IS_MNT_SHARED(old))
  461. list_add(&mnt->mnt_share, &old->mnt_share);
  462. if (IS_MNT_SLAVE(old))
  463. list_add(&mnt->mnt_slave, &old->mnt_slave);
  464. mnt->mnt_master = old->mnt_master;
  465. }
  466. if (flag & CL_MAKE_SHARED)
  467. set_mnt_shared(mnt);
  468. /* stick the duplicate mount on the same expiry list
  469. * as the original if that was on one */
  470. if (flag & CL_EXPIRE) {
  471. if (!list_empty(&old->mnt_expire))
  472. list_add(&mnt->mnt_expire, &old->mnt_expire);
  473. }
  474. }
  475. return mnt;
  476. out_free:
  477. free_vfsmnt(mnt);
  478. return NULL;
  479. }
  480. static inline void __mntput(struct vfsmount *mnt)
  481. {
  482. struct super_block *sb = mnt->mnt_sb;
  483. /*
  484. * This probably indicates that somebody messed
  485. * up a mnt_want/drop_write() pair. If this
  486. * happens, the filesystem was probably unable
  487. * to make r/w->r/o transitions.
  488. */
  489. /*
  490. * atomic_dec_and_lock() used to deal with ->mnt_count decrements
  491. * provides barriers, so count_mnt_writers() below is safe. AV
  492. */
  493. WARN_ON(count_mnt_writers(mnt));
  494. dput(mnt->mnt_root);
  495. free_vfsmnt(mnt);
  496. deactivate_super(sb);
  497. }
  498. void mntput_no_expire(struct vfsmount *mnt)
  499. {
  500. repeat:
  501. if (atomic_dec_and_lock(&mnt->mnt_count, &vfsmount_lock)) {
  502. if (likely(!mnt->mnt_pinned)) {
  503. spin_unlock(&vfsmount_lock);
  504. __mntput(mnt);
  505. return;
  506. }
  507. atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count);
  508. mnt->mnt_pinned = 0;
  509. spin_unlock(&vfsmount_lock);
  510. acct_auto_close_mnt(mnt);
  511. security_sb_umount_close(mnt);
  512. goto repeat;
  513. }
  514. }
  515. EXPORT_SYMBOL(mntput_no_expire);
  516. void mnt_pin(struct vfsmount *mnt)
  517. {
  518. spin_lock(&vfsmount_lock);
  519. mnt->mnt_pinned++;
  520. spin_unlock(&vfsmount_lock);
  521. }
  522. EXPORT_SYMBOL(mnt_pin);
  523. void mnt_unpin(struct vfsmount *mnt)
  524. {
  525. spin_lock(&vfsmount_lock);
  526. if (mnt->mnt_pinned) {
  527. atomic_inc(&mnt->mnt_count);
  528. mnt->mnt_pinned--;
  529. }
  530. spin_unlock(&vfsmount_lock);
  531. }
  532. EXPORT_SYMBOL(mnt_unpin);
  533. static inline void mangle(struct seq_file *m, const char *s)
  534. {
  535. seq_escape(m, s, " \t\n\\");
  536. }
  537. /*
  538. * Simple .show_options callback for filesystems which don't want to
  539. * implement more complex mount option showing.
  540. *
  541. * See also save_mount_options().
  542. */
  543. int generic_show_options(struct seq_file *m, struct vfsmount *mnt)
  544. {
  545. const char *options;
  546. rcu_read_lock();
  547. options = rcu_dereference(mnt->mnt_sb->s_options);
  548. if (options != NULL && options[0]) {
  549. seq_putc(m, ',');
  550. mangle(m, options);
  551. }
  552. rcu_read_unlock();
  553. return 0;
  554. }
  555. EXPORT_SYMBOL(generic_show_options);
  556. /*
  557. * If filesystem uses generic_show_options(), this function should be
  558. * called from the fill_super() callback.
  559. *
  560. * The .remount_fs callback usually needs to be handled in a special
  561. * way, to make sure, that previous options are not overwritten if the
  562. * remount fails.
  563. *
  564. * Also note, that if the filesystem's .remount_fs function doesn't
  565. * reset all options to their default value, but changes only newly
  566. * given options, then the displayed options will not reflect reality
  567. * any more.
  568. */
  569. void save_mount_options(struct super_block *sb, char *options)
  570. {
  571. BUG_ON(sb->s_options);
  572. rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
  573. }
  574. EXPORT_SYMBOL(save_mount_options);
  575. void replace_mount_options(struct super_block *sb, char *options)
  576. {
  577. char *old = sb->s_options;
  578. rcu_assign_pointer(sb->s_options, options);
  579. if (old) {
  580. synchronize_rcu();
  581. kfree(old);
  582. }
  583. }
  584. EXPORT_SYMBOL(replace_mount_options);
  585. #ifdef CONFIG_PROC_FS
  586. /* iterator */
  587. static void *m_start(struct seq_file *m, loff_t *pos)
  588. {
  589. struct proc_mounts *p = m->private;
  590. down_read(&namespace_sem);
  591. return seq_list_start(&p->ns->list, *pos);
  592. }
  593. static void *m_next(struct seq_file *m, void *v, loff_t *pos)
  594. {
  595. struct proc_mounts *p = m->private;
  596. return seq_list_next(v, &p->ns->list, pos);
  597. }
  598. static void m_stop(struct seq_file *m, void *v)
  599. {
  600. up_read(&namespace_sem);
  601. }
  602. struct proc_fs_info {
  603. int flag;
  604. const char *str;
  605. };
  606. static int show_sb_opts(struct seq_file *m, struct super_block *sb)
  607. {
  608. static const struct proc_fs_info fs_info[] = {
  609. { MS_SYNCHRONOUS, ",sync" },
  610. { MS_DIRSYNC, ",dirsync" },
  611. { MS_MANDLOCK, ",mand" },
  612. { 0, NULL }
  613. };
  614. const struct proc_fs_info *fs_infop;
  615. for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
  616. if (sb->s_flags & fs_infop->flag)
  617. seq_puts(m, fs_infop->str);
  618. }
  619. return security_sb_show_options(m, sb);
  620. }
  621. static void show_mnt_opts(struct seq_file *m, struct vfsmount *mnt)
  622. {
  623. static const struct proc_fs_info mnt_info[] = {
  624. { MNT_NOSUID, ",nosuid" },
  625. { MNT_NODEV, ",nodev" },
  626. { MNT_NOEXEC, ",noexec" },
  627. { MNT_NOATIME, ",noatime" },
  628. { MNT_NODIRATIME, ",nodiratime" },
  629. { MNT_RELATIME, ",relatime" },
  630. { MNT_STRICTATIME, ",strictatime" },
  631. { 0, NULL }
  632. };
  633. const struct proc_fs_info *fs_infop;
  634. for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
  635. if (mnt->mnt_flags & fs_infop->flag)
  636. seq_puts(m, fs_infop->str);
  637. }
  638. }
  639. static void show_type(struct seq_file *m, struct super_block *sb)
  640. {
  641. mangle(m, sb->s_type->name);
  642. if (sb->s_subtype && sb->s_subtype[0]) {
  643. seq_putc(m, '.');
  644. mangle(m, sb->s_subtype);
  645. }
  646. }
  647. static int show_vfsmnt(struct seq_file *m, void *v)
  648. {
  649. struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
  650. int err = 0;
  651. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  652. mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
  653. seq_putc(m, ' ');
  654. seq_path(m, &mnt_path, " \t\n\\");
  655. seq_putc(m, ' ');
  656. show_type(m, mnt->mnt_sb);
  657. seq_puts(m, __mnt_is_readonly(mnt) ? " ro" : " rw");
  658. err = show_sb_opts(m, mnt->mnt_sb);
  659. if (err)
  660. goto out;
  661. show_mnt_opts(m, mnt);
  662. if (mnt->mnt_sb->s_op->show_options)
  663. err = mnt->mnt_sb->s_op->show_options(m, mnt);
  664. seq_puts(m, " 0 0\n");
  665. out:
  666. return err;
  667. }
  668. const struct seq_operations mounts_op = {
  669. .start = m_start,
  670. .next = m_next,
  671. .stop = m_stop,
  672. .show = show_vfsmnt
  673. };
  674. static int show_mountinfo(struct seq_file *m, void *v)
  675. {
  676. struct proc_mounts *p = m->private;
  677. struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
  678. struct super_block *sb = mnt->mnt_sb;
  679. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  680. struct path root = p->root;
  681. int err = 0;
  682. seq_printf(m, "%i %i %u:%u ", mnt->mnt_id, mnt->mnt_parent->mnt_id,
  683. MAJOR(sb->s_dev), MINOR(sb->s_dev));
  684. seq_dentry(m, mnt->mnt_root, " \t\n\\");
  685. seq_putc(m, ' ');
  686. seq_path_root(m, &mnt_path, &root, " \t\n\\");
  687. if (root.mnt != p->root.mnt || root.dentry != p->root.dentry) {
  688. /*
  689. * Mountpoint is outside root, discard that one. Ugly,
  690. * but less so than trying to do that in iterator in a
  691. * race-free way (due to renames).
  692. */
  693. return SEQ_SKIP;
  694. }
  695. seq_puts(m, mnt->mnt_flags & MNT_READONLY ? " ro" : " rw");
  696. show_mnt_opts(m, mnt);
  697. /* Tagged fields ("foo:X" or "bar") */
  698. if (IS_MNT_SHARED(mnt))
  699. seq_printf(m, " shared:%i", mnt->mnt_group_id);
  700. if (IS_MNT_SLAVE(mnt)) {
  701. int master = mnt->mnt_master->mnt_group_id;
  702. int dom = get_dominating_id(mnt, &p->root);
  703. seq_printf(m, " master:%i", master);
  704. if (dom && dom != master)
  705. seq_printf(m, " propagate_from:%i", dom);
  706. }
  707. if (IS_MNT_UNBINDABLE(mnt))
  708. seq_puts(m, " unbindable");
  709. /* Filesystem specific data */
  710. seq_puts(m, " - ");
  711. show_type(m, sb);
  712. seq_putc(m, ' ');
  713. mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
  714. seq_puts(m, sb->s_flags & MS_RDONLY ? " ro" : " rw");
  715. err = show_sb_opts(m, sb);
  716. if (err)
  717. goto out;
  718. if (sb->s_op->show_options)
  719. err = sb->s_op->show_options(m, mnt);
  720. seq_putc(m, '\n');
  721. out:
  722. return err;
  723. }
  724. const struct seq_operations mountinfo_op = {
  725. .start = m_start,
  726. .next = m_next,
  727. .stop = m_stop,
  728. .show = show_mountinfo,
  729. };
  730. static int show_vfsstat(struct seq_file *m, void *v)
  731. {
  732. struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
  733. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  734. int err = 0;
  735. /* device */
  736. if (mnt->mnt_devname) {
  737. seq_puts(m, "device ");
  738. mangle(m, mnt->mnt_devname);
  739. } else
  740. seq_puts(m, "no device");
  741. /* mount point */
  742. seq_puts(m, " mounted on ");
  743. seq_path(m, &mnt_path, " \t\n\\");
  744. seq_putc(m, ' ');
  745. /* file system type */
  746. seq_puts(m, "with fstype ");
  747. show_type(m, mnt->mnt_sb);
  748. /* optional statistics */
  749. if (mnt->mnt_sb->s_op->show_stats) {
  750. seq_putc(m, ' ');
  751. err = mnt->mnt_sb->s_op->show_stats(m, mnt);
  752. }
  753. seq_putc(m, '\n');
  754. return err;
  755. }
  756. const struct seq_operations mountstats_op = {
  757. .start = m_start,
  758. .next = m_next,
  759. .stop = m_stop,
  760. .show = show_vfsstat,
  761. };
  762. #endif /* CONFIG_PROC_FS */
  763. /**
  764. * may_umount_tree - check if a mount tree is busy
  765. * @mnt: root of mount tree
  766. *
  767. * This is called to check if a tree of mounts has any
  768. * open files, pwds, chroots or sub mounts that are
  769. * busy.
  770. */
  771. int may_umount_tree(struct vfsmount *mnt)
  772. {
  773. int actual_refs = 0;
  774. int minimum_refs = 0;
  775. struct vfsmount *p;
  776. spin_lock(&vfsmount_lock);
  777. for (p = mnt; p; p = next_mnt(p, mnt)) {
  778. actual_refs += atomic_read(&p->mnt_count);
  779. minimum_refs += 2;
  780. }
  781. spin_unlock(&vfsmount_lock);
  782. if (actual_refs > minimum_refs)
  783. return 0;
  784. return 1;
  785. }
  786. EXPORT_SYMBOL(may_umount_tree);
  787. /**
  788. * may_umount - check if a mount point is busy
  789. * @mnt: root of mount
  790. *
  791. * This is called to check if a mount point has any
  792. * open files, pwds, chroots or sub mounts. If the
  793. * mount has sub mounts this will return busy
  794. * regardless of whether the sub mounts are busy.
  795. *
  796. * Doesn't take quota and stuff into account. IOW, in some cases it will
  797. * give false negatives. The main reason why it's here is that we need
  798. * a non-destructive way to look for easily umountable filesystems.
  799. */
  800. int may_umount(struct vfsmount *mnt)
  801. {
  802. int ret = 1;
  803. spin_lock(&vfsmount_lock);
  804. if (propagate_mount_busy(mnt, 2))
  805. ret = 0;
  806. spin_unlock(&vfsmount_lock);
  807. return ret;
  808. }
  809. EXPORT_SYMBOL(may_umount);
  810. void release_mounts(struct list_head *head)
  811. {
  812. struct vfsmount *mnt;
  813. while (!list_empty(head)) {
  814. mnt = list_first_entry(head, struct vfsmount, mnt_hash);
  815. list_del_init(&mnt->mnt_hash);
  816. if (mnt->mnt_parent != mnt) {
  817. struct dentry *dentry;
  818. struct vfsmount *m;
  819. spin_lock(&vfsmount_lock);
  820. dentry = mnt->mnt_mountpoint;
  821. m = mnt->mnt_parent;
  822. mnt->mnt_mountpoint = mnt->mnt_root;
  823. mnt->mnt_parent = mnt;
  824. m->mnt_ghosts--;
  825. spin_unlock(&vfsmount_lock);
  826. dput(dentry);
  827. mntput(m);
  828. }
  829. mntput(mnt);
  830. }
  831. }
  832. void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill)
  833. {
  834. struct vfsmount *p;
  835. for (p = mnt; p; p = next_mnt(p, mnt))
  836. list_move(&p->mnt_hash, kill);
  837. if (propagate)
  838. propagate_umount(kill);
  839. list_for_each_entry(p, kill, mnt_hash) {
  840. list_del_init(&p->mnt_expire);
  841. list_del_init(&p->mnt_list);
  842. __touch_mnt_namespace(p->mnt_ns);
  843. p->mnt_ns = NULL;
  844. list_del_init(&p->mnt_child);
  845. if (p->mnt_parent != p) {
  846. p->mnt_parent->mnt_ghosts++;
  847. p->mnt_mountpoint->d_mounted--;
  848. }
  849. change_mnt_propagation(p, MS_PRIVATE);
  850. }
  851. }
  852. static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts);
  853. static int do_umount(struct vfsmount *mnt, int flags)
  854. {
  855. struct super_block *sb = mnt->mnt_sb;
  856. int retval;
  857. LIST_HEAD(umount_list);
  858. retval = security_sb_umount(mnt, flags);
  859. if (retval)
  860. return retval;
  861. /*
  862. * Allow userspace to request a mountpoint be expired rather than
  863. * unmounting unconditionally. Unmount only happens if:
  864. * (1) the mark is already set (the mark is cleared by mntput())
  865. * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
  866. */
  867. if (flags & MNT_EXPIRE) {
  868. if (mnt == current->fs->root.mnt ||
  869. flags & (MNT_FORCE | MNT_DETACH))
  870. return -EINVAL;
  871. if (atomic_read(&mnt->mnt_count) != 2)
  872. return -EBUSY;
  873. if (!xchg(&mnt->mnt_expiry_mark, 1))
  874. return -EAGAIN;
  875. }
  876. /*
  877. * If we may have to abort operations to get out of this
  878. * mount, and they will themselves hold resources we must
  879. * allow the fs to do things. In the Unix tradition of
  880. * 'Gee thats tricky lets do it in userspace' the umount_begin
  881. * might fail to complete on the first run through as other tasks
  882. * must return, and the like. Thats for the mount program to worry
  883. * about for the moment.
  884. */
  885. if (flags & MNT_FORCE && sb->s_op->umount_begin) {
  886. sb->s_op->umount_begin(sb);
  887. }
  888. /*
  889. * No sense to grab the lock for this test, but test itself looks
  890. * somewhat bogus. Suggestions for better replacement?
  891. * Ho-hum... In principle, we might treat that as umount + switch
  892. * to rootfs. GC would eventually take care of the old vfsmount.
  893. * Actually it makes sense, especially if rootfs would contain a
  894. * /reboot - static binary that would close all descriptors and
  895. * call reboot(9). Then init(8) could umount root and exec /reboot.
  896. */
  897. if (mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
  898. /*
  899. * Special case for "unmounting" root ...
  900. * we just try to remount it readonly.
  901. */
  902. down_write(&sb->s_umount);
  903. if (!(sb->s_flags & MS_RDONLY)) {
  904. lock_kernel();
  905. retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
  906. unlock_kernel();
  907. }
  908. up_write(&sb->s_umount);
  909. return retval;
  910. }
  911. down_write(&namespace_sem);
  912. spin_lock(&vfsmount_lock);
  913. event++;
  914. if (!(flags & MNT_DETACH))
  915. shrink_submounts(mnt, &umount_list);
  916. retval = -EBUSY;
  917. if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
  918. if (!list_empty(&mnt->mnt_list))
  919. umount_tree(mnt, 1, &umount_list);
  920. retval = 0;
  921. }
  922. spin_unlock(&vfsmount_lock);
  923. if (retval)
  924. security_sb_umount_busy(mnt);
  925. up_write(&namespace_sem);
  926. release_mounts(&umount_list);
  927. return retval;
  928. }
  929. /*
  930. * Now umount can handle mount points as well as block devices.
  931. * This is important for filesystems which use unnamed block devices.
  932. *
  933. * We now support a flag for forced unmount like the other 'big iron'
  934. * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
  935. */
  936. SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
  937. {
  938. struct path path;
  939. int retval;
  940. retval = user_path(name, &path);
  941. if (retval)
  942. goto out;
  943. retval = -EINVAL;
  944. if (path.dentry != path.mnt->mnt_root)
  945. goto dput_and_out;
  946. if (!check_mnt(path.mnt))
  947. goto dput_and_out;
  948. retval = -EPERM;
  949. if (!capable(CAP_SYS_ADMIN))
  950. goto dput_and_out;
  951. retval = do_umount(path.mnt, flags);
  952. dput_and_out:
  953. /* we mustn't call path_put() as that would clear mnt_expiry_mark */
  954. dput(path.dentry);
  955. mntput_no_expire(path.mnt);
  956. out:
  957. return retval;
  958. }
  959. #ifdef __ARCH_WANT_SYS_OLDUMOUNT
  960. /*
  961. * The 2.0 compatible umount. No flags.
  962. */
  963. SYSCALL_DEFINE1(oldumount, char __user *, name)
  964. {
  965. return sys_umount(name, 0);
  966. }
  967. #endif
  968. static int mount_is_safe(struct path *path)
  969. {
  970. if (capable(CAP_SYS_ADMIN))
  971. return 0;
  972. return -EPERM;
  973. #ifdef notyet
  974. if (S_ISLNK(path->dentry->d_inode->i_mode))
  975. return -EPERM;
  976. if (path->dentry->d_inode->i_mode & S_ISVTX) {
  977. if (current_uid() != path->dentry->d_inode->i_uid)
  978. return -EPERM;
  979. }
  980. if (inode_permission(path->dentry->d_inode, MAY_WRITE))
  981. return -EPERM;
  982. return 0;
  983. #endif
  984. }
  985. struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
  986. int flag)
  987. {
  988. struct vfsmount *res, *p, *q, *r, *s;
  989. struct path path;
  990. if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
  991. return NULL;
  992. res = q = clone_mnt(mnt, dentry, flag);
  993. if (!q)
  994. goto Enomem;
  995. q->mnt_mountpoint = mnt->mnt_mountpoint;
  996. p = mnt;
  997. list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
  998. if (!is_subdir(r->mnt_mountpoint, dentry))
  999. continue;
  1000. for (s = r; s; s = next_mnt(s, r)) {
  1001. if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
  1002. s = skip_mnt_tree(s);
  1003. continue;
  1004. }
  1005. while (p != s->mnt_parent) {
  1006. p = p->mnt_parent;
  1007. q = q->mnt_parent;
  1008. }
  1009. p = s;
  1010. path.mnt = q;
  1011. path.dentry = p->mnt_mountpoint;
  1012. q = clone_mnt(p, p->mnt_root, flag);
  1013. if (!q)
  1014. goto Enomem;
  1015. spin_lock(&vfsmount_lock);
  1016. list_add_tail(&q->mnt_list, &res->mnt_list);
  1017. attach_mnt(q, &path);
  1018. spin_unlock(&vfsmount_lock);
  1019. }
  1020. }
  1021. return res;
  1022. Enomem:
  1023. if (res) {
  1024. LIST_HEAD(umount_list);
  1025. spin_lock(&vfsmount_lock);
  1026. umount_tree(res, 0, &umount_list);
  1027. spin_unlock(&vfsmount_lock);
  1028. release_mounts(&umount_list);
  1029. }
  1030. return NULL;
  1031. }
  1032. struct vfsmount *collect_mounts(struct path *path)
  1033. {
  1034. struct vfsmount *tree;
  1035. down_write(&namespace_sem);
  1036. tree = copy_tree(path->mnt, path->dentry, CL_COPY_ALL | CL_PRIVATE);
  1037. up_write(&namespace_sem);
  1038. return tree;
  1039. }
  1040. void drop_collected_mounts(struct vfsmount *mnt)
  1041. {
  1042. LIST_HEAD(umount_list);
  1043. down_write(&namespace_sem);
  1044. spin_lock(&vfsmount_lock);
  1045. umount_tree(mnt, 0, &umount_list);
  1046. spin_unlock(&vfsmount_lock);
  1047. up_write(&namespace_sem);
  1048. release_mounts(&umount_list);
  1049. }
  1050. static void cleanup_group_ids(struct vfsmount *mnt, struct vfsmount *end)
  1051. {
  1052. struct vfsmount *p;
  1053. for (p = mnt; p != end; p = next_mnt(p, mnt)) {
  1054. if (p->mnt_group_id && !IS_MNT_SHARED(p))
  1055. mnt_release_group_id(p);
  1056. }
  1057. }
  1058. static int invent_group_ids(struct vfsmount *mnt, bool recurse)
  1059. {
  1060. struct vfsmount *p;
  1061. for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
  1062. if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
  1063. int err = mnt_alloc_group_id(p);
  1064. if (err) {
  1065. cleanup_group_ids(mnt, p);
  1066. return err;
  1067. }
  1068. }
  1069. }
  1070. return 0;
  1071. }
  1072. /*
  1073. * @source_mnt : mount tree to be attached
  1074. * @nd : place the mount tree @source_mnt is attached
  1075. * @parent_nd : if non-null, detach the source_mnt from its parent and
  1076. * store the parent mount and mountpoint dentry.
  1077. * (done when source_mnt is moved)
  1078. *
  1079. * NOTE: in the table below explains the semantics when a source mount
  1080. * of a given type is attached to a destination mount of a given type.
  1081. * ---------------------------------------------------------------------------
  1082. * | BIND MOUNT OPERATION |
  1083. * |**************************************************************************
  1084. * | source-->| shared | private | slave | unbindable |
  1085. * | dest | | | | |
  1086. * | | | | | | |
  1087. * | v | | | | |
  1088. * |**************************************************************************
  1089. * | shared | shared (++) | shared (+) | shared(+++)| invalid |
  1090. * | | | | | |
  1091. * |non-shared| shared (+) | private | slave (*) | invalid |
  1092. * ***************************************************************************
  1093. * A bind operation clones the source mount and mounts the clone on the
  1094. * destination mount.
  1095. *
  1096. * (++) the cloned mount is propagated to all the mounts in the propagation
  1097. * tree of the destination mount and the cloned mount is added to
  1098. * the peer group of the source mount.
  1099. * (+) the cloned mount is created under the destination mount and is marked
  1100. * as shared. The cloned mount is added to the peer group of the source
  1101. * mount.
  1102. * (+++) the mount is propagated to all the mounts in the propagation tree
  1103. * of the destination mount and the cloned mount is made slave
  1104. * of the same master as that of the source mount. The cloned mount
  1105. * is marked as 'shared and slave'.
  1106. * (*) the cloned mount is made a slave of the same master as that of the
  1107. * source mount.
  1108. *
  1109. * ---------------------------------------------------------------------------
  1110. * | MOVE MOUNT OPERATION |
  1111. * |**************************************************************************
  1112. * | source-->| shared | private | slave | unbindable |
  1113. * | dest | | | | |
  1114. * | | | | | | |
  1115. * | v | | | | |
  1116. * |**************************************************************************
  1117. * | shared | shared (+) | shared (+) | shared(+++) | invalid |
  1118. * | | | | | |
  1119. * |non-shared| shared (+*) | private | slave (*) | unbindable |
  1120. * ***************************************************************************
  1121. *
  1122. * (+) the mount is moved to the destination. And is then propagated to
  1123. * all the mounts in the propagation tree of the destination mount.
  1124. * (+*) the mount is moved to the destination.
  1125. * (+++) the mount is moved to the destination and is then propagated to
  1126. * all the mounts belonging to the destination mount's propagation tree.
  1127. * the mount is marked as 'shared and slave'.
  1128. * (*) the mount continues to be a slave at the new location.
  1129. *
  1130. * if the source mount is a tree, the operations explained above is
  1131. * applied to each mount in the tree.
  1132. * Must be called without spinlocks held, since this function can sleep
  1133. * in allocations.
  1134. */
  1135. static int attach_recursive_mnt(struct vfsmount *source_mnt,
  1136. struct path *path, struct path *parent_path)
  1137. {
  1138. LIST_HEAD(tree_list);
  1139. struct vfsmount *dest_mnt = path->mnt;
  1140. struct dentry *dest_dentry = path->dentry;
  1141. struct vfsmount *child, *p;
  1142. int err;
  1143. if (IS_MNT_SHARED(dest_mnt)) {
  1144. err = invent_group_ids(source_mnt, true);
  1145. if (err)
  1146. goto out;
  1147. }
  1148. err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list);
  1149. if (err)
  1150. goto out_cleanup_ids;
  1151. if (IS_MNT_SHARED(dest_mnt)) {
  1152. for (p = source_mnt; p; p = next_mnt(p, source_mnt))
  1153. set_mnt_shared(p);
  1154. }
  1155. spin_lock(&vfsmount_lock);
  1156. if (parent_path) {
  1157. detach_mnt(source_mnt, parent_path);
  1158. attach_mnt(source_mnt, path);
  1159. touch_mnt_namespace(parent_path->mnt->mnt_ns);
  1160. } else {
  1161. mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
  1162. commit_tree(source_mnt);
  1163. }
  1164. list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
  1165. list_del_init(&child->mnt_hash);
  1166. commit_tree(child);
  1167. }
  1168. spin_unlock(&vfsmount_lock);
  1169. return 0;
  1170. out_cleanup_ids:
  1171. if (IS_MNT_SHARED(dest_mnt))
  1172. cleanup_group_ids(source_mnt, NULL);
  1173. out:
  1174. return err;
  1175. }
  1176. static int graft_tree(struct vfsmount *mnt, struct path *path)
  1177. {
  1178. int err;
  1179. if (mnt->mnt_sb->s_flags & MS_NOUSER)
  1180. return -EINVAL;
  1181. if (S_ISDIR(path->dentry->d_inode->i_mode) !=
  1182. S_ISDIR(mnt->mnt_root->d_inode->i_mode))
  1183. return -ENOTDIR;
  1184. err = -ENOENT;
  1185. mutex_lock(&path->dentry->d_inode->i_mutex);
  1186. if (IS_DEADDIR(path->dentry->d_inode))
  1187. goto out_unlock;
  1188. err = security_sb_check_sb(mnt, path);
  1189. if (err)
  1190. goto out_unlock;
  1191. err = -ENOENT;
  1192. if (IS_ROOT(path->dentry) || !d_unhashed(path->dentry))
  1193. err = attach_recursive_mnt(mnt, path, NULL);
  1194. out_unlock:
  1195. mutex_unlock(&path->dentry->d_inode->i_mutex);
  1196. if (!err)
  1197. security_sb_post_addmount(mnt, path);
  1198. return err;
  1199. }
  1200. /*
  1201. * recursively change the type of the mountpoint.
  1202. */
  1203. static int do_change_type(struct path *path, int flag)
  1204. {
  1205. struct vfsmount *m, *mnt = path->mnt;
  1206. int recurse = flag & MS_REC;
  1207. int type = flag & ~MS_REC;
  1208. int err = 0;
  1209. if (!capable(CAP_SYS_ADMIN))
  1210. return -EPERM;
  1211. if (path->dentry != path->mnt->mnt_root)
  1212. return -EINVAL;
  1213. down_write(&namespace_sem);
  1214. if (type == MS_SHARED) {
  1215. err = invent_group_ids(mnt, recurse);
  1216. if (err)
  1217. goto out_unlock;
  1218. }
  1219. spin_lock(&vfsmount_lock);
  1220. for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
  1221. change_mnt_propagation(m, type);
  1222. spin_unlock(&vfsmount_lock);
  1223. out_unlock:
  1224. up_write(&namespace_sem);
  1225. return err;
  1226. }
  1227. /*
  1228. * do loopback mount.
  1229. */
  1230. static int do_loopback(struct path *path, char *old_name,
  1231. int recurse)
  1232. {
  1233. struct path old_path;
  1234. struct vfsmount *mnt = NULL;
  1235. int err = mount_is_safe(path);
  1236. if (err)
  1237. return err;
  1238. if (!old_name || !*old_name)
  1239. return -EINVAL;
  1240. err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
  1241. if (err)
  1242. return err;
  1243. down_write(&namespace_sem);
  1244. err = -EINVAL;
  1245. if (IS_MNT_UNBINDABLE(old_path.mnt))
  1246. goto out;
  1247. if (!check_mnt(path->mnt) || !check_mnt(old_path.mnt))
  1248. goto out;
  1249. err = -ENOMEM;
  1250. if (recurse)
  1251. mnt = copy_tree(old_path.mnt, old_path.dentry, 0);
  1252. else
  1253. mnt = clone_mnt(old_path.mnt, old_path.dentry, 0);
  1254. if (!mnt)
  1255. goto out;
  1256. err = graft_tree(mnt, path);
  1257. if (err) {
  1258. LIST_HEAD(umount_list);
  1259. spin_lock(&vfsmount_lock);
  1260. umount_tree(mnt, 0, &umount_list);
  1261. spin_unlock(&vfsmount_lock);
  1262. release_mounts(&umount_list);
  1263. }
  1264. out:
  1265. up_write(&namespace_sem);
  1266. path_put(&old_path);
  1267. return err;
  1268. }
  1269. static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
  1270. {
  1271. int error = 0;
  1272. int readonly_request = 0;
  1273. if (ms_flags & MS_RDONLY)
  1274. readonly_request = 1;
  1275. if (readonly_request == __mnt_is_readonly(mnt))
  1276. return 0;
  1277. if (readonly_request)
  1278. error = mnt_make_readonly(mnt);
  1279. else
  1280. __mnt_unmake_readonly(mnt);
  1281. return error;
  1282. }
  1283. /*
  1284. * change filesystem flags. dir should be a physical root of filesystem.
  1285. * If you've mounted a non-root directory somewhere and want to do remount
  1286. * on it - tough luck.
  1287. */
  1288. static int do_remount(struct path *path, int flags, int mnt_flags,
  1289. void *data)
  1290. {
  1291. int err;
  1292. struct super_block *sb = path->mnt->mnt_sb;
  1293. if (!capable(CAP_SYS_ADMIN))
  1294. return -EPERM;
  1295. if (!check_mnt(path->mnt))
  1296. return -EINVAL;
  1297. if (path->dentry != path->mnt->mnt_root)
  1298. return -EINVAL;
  1299. down_write(&sb->s_umount);
  1300. if (flags & MS_BIND)
  1301. err = change_mount_flags(path->mnt, flags);
  1302. else
  1303. err = do_remount_sb(sb, flags, data, 0);
  1304. if (!err)
  1305. path->mnt->mnt_flags = mnt_flags;
  1306. up_write(&sb->s_umount);
  1307. if (!err) {
  1308. security_sb_post_remount(path->mnt, flags, data);
  1309. spin_lock(&vfsmount_lock);
  1310. touch_mnt_namespace(path->mnt->mnt_ns);
  1311. spin_unlock(&vfsmount_lock);
  1312. }
  1313. return err;
  1314. }
  1315. static inline int tree_contains_unbindable(struct vfsmount *mnt)
  1316. {
  1317. struct vfsmount *p;
  1318. for (p = mnt; p; p = next_mnt(p, mnt)) {
  1319. if (IS_MNT_UNBINDABLE(p))
  1320. return 1;
  1321. }
  1322. return 0;
  1323. }
  1324. static int do_move_mount(struct path *path, char *old_name)
  1325. {
  1326. struct path old_path, parent_path;
  1327. struct vfsmount *p;
  1328. int err = 0;
  1329. if (!capable(CAP_SYS_ADMIN))
  1330. return -EPERM;
  1331. if (!old_name || !*old_name)
  1332. return -EINVAL;
  1333. err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
  1334. if (err)
  1335. return err;
  1336. down_write(&namespace_sem);
  1337. while (d_mountpoint(path->dentry) &&
  1338. follow_down(path))
  1339. ;
  1340. err = -EINVAL;
  1341. if (!check_mnt(path->mnt) || !check_mnt(old_path.mnt))
  1342. goto out;
  1343. err = -ENOENT;
  1344. mutex_lock(&path->dentry->d_inode->i_mutex);
  1345. if (IS_DEADDIR(path->dentry->d_inode))
  1346. goto out1;
  1347. if (!IS_ROOT(path->dentry) && d_unhashed(path->dentry))
  1348. goto out1;
  1349. err = -EINVAL;
  1350. if (old_path.dentry != old_path.mnt->mnt_root)
  1351. goto out1;
  1352. if (old_path.mnt == old_path.mnt->mnt_parent)
  1353. goto out1;
  1354. if (S_ISDIR(path->dentry->d_inode->i_mode) !=
  1355. S_ISDIR(old_path.dentry->d_inode->i_mode))
  1356. goto out1;
  1357. /*
  1358. * Don't move a mount residing in a shared parent.
  1359. */
  1360. if (old_path.mnt->mnt_parent &&
  1361. IS_MNT_SHARED(old_path.mnt->mnt_parent))
  1362. goto out1;
  1363. /*
  1364. * Don't move a mount tree containing unbindable mounts to a destination
  1365. * mount which is shared.
  1366. */
  1367. if (IS_MNT_SHARED(path->mnt) &&
  1368. tree_contains_unbindable(old_path.mnt))
  1369. goto out1;
  1370. err = -ELOOP;
  1371. for (p = path->mnt; p->mnt_parent != p; p = p->mnt_parent)
  1372. if (p == old_path.mnt)
  1373. goto out1;
  1374. err = attach_recursive_mnt(old_path.mnt, path, &parent_path);
  1375. if (err)
  1376. goto out1;
  1377. /* if the mount is moved, it should no longer be expire
  1378. * automatically */
  1379. list_del_init(&old_path.mnt->mnt_expire);
  1380. out1:
  1381. mutex_unlock(&path->dentry->d_inode->i_mutex);
  1382. out:
  1383. up_write(&namespace_sem);
  1384. if (!err)
  1385. path_put(&parent_path);
  1386. path_put(&old_path);
  1387. return err;
  1388. }
  1389. /*
  1390. * create a new mount for userspace and request it to be added into the
  1391. * namespace's tree
  1392. */
  1393. static int do_new_mount(struct path *path, char *type, int flags,
  1394. int mnt_flags, char *name, void *data)
  1395. {
  1396. struct vfsmount *mnt;
  1397. if (!type || !memchr(type, 0, PAGE_SIZE))
  1398. return -EINVAL;
  1399. /* we need capabilities... */
  1400. if (!capable(CAP_SYS_ADMIN))
  1401. return -EPERM;
  1402. mnt = do_kern_mount(type, flags, name, data);
  1403. if (IS_ERR(mnt))
  1404. return PTR_ERR(mnt);
  1405. return do_add_mount(mnt, path, mnt_flags, NULL);
  1406. }
  1407. /*
  1408. * add a mount into a namespace's mount tree
  1409. * - provide the option of adding the new mount to an expiration list
  1410. */
  1411. int do_add_mount(struct vfsmount *newmnt, struct path *path,
  1412. int mnt_flags, struct list_head *fslist)
  1413. {
  1414. int err;
  1415. down_write(&namespace_sem);
  1416. /* Something was mounted here while we slept */
  1417. while (d_mountpoint(path->dentry) &&
  1418. follow_down(path))
  1419. ;
  1420. err = -EINVAL;
  1421. if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(path->mnt))
  1422. goto unlock;
  1423. /* Refuse the same filesystem on the same mount point */
  1424. err = -EBUSY;
  1425. if (path->mnt->mnt_sb == newmnt->mnt_sb &&
  1426. path->mnt->mnt_root == path->dentry)
  1427. goto unlock;
  1428. err = -EINVAL;
  1429. if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
  1430. goto unlock;
  1431. newmnt->mnt_flags = mnt_flags;
  1432. if ((err = graft_tree(newmnt, path)))
  1433. goto unlock;
  1434. if (fslist) /* add to the specified expiration list */
  1435. list_add_tail(&newmnt->mnt_expire, fslist);
  1436. up_write(&namespace_sem);
  1437. return 0;
  1438. unlock:
  1439. up_write(&namespace_sem);
  1440. mntput(newmnt);
  1441. return err;
  1442. }
  1443. EXPORT_SYMBOL_GPL(do_add_mount);
  1444. /*
  1445. * process a list of expirable mountpoints with the intent of discarding any
  1446. * mountpoints that aren't in use and haven't been touched since last we came
  1447. * here
  1448. */
  1449. void mark_mounts_for_expiry(struct list_head *mounts)
  1450. {
  1451. struct vfsmount *mnt, *next;
  1452. LIST_HEAD(graveyard);
  1453. LIST_HEAD(umounts);
  1454. if (list_empty(mounts))
  1455. return;
  1456. down_write(&namespace_sem);
  1457. spin_lock(&vfsmount_lock);
  1458. /* extract from the expiration list every vfsmount that matches the
  1459. * following criteria:
  1460. * - only referenced by its parent vfsmount
  1461. * - still marked for expiry (marked on the last call here; marks are
  1462. * cleared by mntput())
  1463. */
  1464. list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
  1465. if (!xchg(&mnt->mnt_expiry_mark, 1) ||
  1466. propagate_mount_busy(mnt, 1))
  1467. continue;
  1468. list_move(&mnt->mnt_expire, &graveyard);
  1469. }
  1470. while (!list_empty(&graveyard)) {
  1471. mnt = list_first_entry(&graveyard, struct vfsmount, mnt_expire);
  1472. touch_mnt_namespace(mnt->mnt_ns);
  1473. umount_tree(mnt, 1, &umounts);
  1474. }
  1475. spin_unlock(&vfsmount_lock);
  1476. up_write(&namespace_sem);
  1477. release_mounts(&umounts);
  1478. }
  1479. EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
  1480. /*
  1481. * Ripoff of 'select_parent()'
  1482. *
  1483. * search the list of submounts for a given mountpoint, and move any
  1484. * shrinkable submounts to the 'graveyard' list.
  1485. */
  1486. static int select_submounts(struct vfsmount *parent, struct list_head *graveyard)
  1487. {
  1488. struct vfsmount *this_parent = parent;
  1489. struct list_head *next;
  1490. int found = 0;
  1491. repeat:
  1492. next = this_parent->mnt_mounts.next;
  1493. resume:
  1494. while (next != &this_parent->mnt_mounts) {
  1495. struct list_head *tmp = next;
  1496. struct vfsmount *mnt = list_entry(tmp, struct vfsmount, mnt_child);
  1497. next = tmp->next;
  1498. if (!(mnt->mnt_flags & MNT_SHRINKABLE))
  1499. continue;
  1500. /*
  1501. * Descend a level if the d_mounts list is non-empty.
  1502. */
  1503. if (!list_empty(&mnt->mnt_mounts)) {
  1504. this_parent = mnt;
  1505. goto repeat;
  1506. }
  1507. if (!propagate_mount_busy(mnt, 1)) {
  1508. list_move_tail(&mnt->mnt_expire, graveyard);
  1509. found++;
  1510. }
  1511. }
  1512. /*
  1513. * All done at this level ... ascend and resume the search
  1514. */
  1515. if (this_parent != parent) {
  1516. next = this_parent->mnt_child.next;
  1517. this_parent = this_parent->mnt_parent;
  1518. goto resume;
  1519. }
  1520. return found;
  1521. }
  1522. /*
  1523. * process a list of expirable mountpoints with the intent of discarding any
  1524. * submounts of a specific parent mountpoint
  1525. */
  1526. static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts)
  1527. {
  1528. LIST_HEAD(graveyard);
  1529. struct vfsmount *m;
  1530. /* extract submounts of 'mountpoint' from the expiration list */
  1531. while (select_submounts(mnt, &graveyard)) {
  1532. while (!list_empty(&graveyard)) {
  1533. m = list_first_entry(&graveyard, struct vfsmount,
  1534. mnt_expire);
  1535. touch_mnt_namespace(m->mnt_ns);
  1536. umount_tree(m, 1, umounts);
  1537. }
  1538. }
  1539. }
  1540. /*
  1541. * Some copy_from_user() implementations do not return the exact number of
  1542. * bytes remaining to copy on a fault. But copy_mount_options() requires that.
  1543. * Note that this function differs from copy_from_user() in that it will oops
  1544. * on bad values of `to', rather than returning a short copy.
  1545. */
  1546. static long exact_copy_from_user(void *to, const void __user * from,
  1547. unsigned long n)
  1548. {
  1549. char *t = to;
  1550. const char __user *f = from;
  1551. char c;
  1552. if (!access_ok(VERIFY_READ, from, n))
  1553. return n;
  1554. while (n) {
  1555. if (__get_user(c, f)) {
  1556. memset(t, 0, n);
  1557. break;
  1558. }
  1559. *t++ = c;
  1560. f++;
  1561. n--;
  1562. }
  1563. return n;
  1564. }
  1565. int copy_mount_options(const void __user * data, unsigned long *where)
  1566. {
  1567. int i;
  1568. unsigned long page;
  1569. unsigned long size;
  1570. *where = 0;
  1571. if (!data)
  1572. return 0;
  1573. if (!(page = __get_free_page(GFP_KERNEL)))
  1574. return -ENOMEM;
  1575. /* We only care that *some* data at the address the user
  1576. * gave us is valid. Just in case, we'll zero
  1577. * the remainder of the page.
  1578. */
  1579. /* copy_from_user cannot cross TASK_SIZE ! */
  1580. size = TASK_SIZE - (unsigned long)data;
  1581. if (size > PAGE_SIZE)
  1582. size = PAGE_SIZE;
  1583. i = size - exact_copy_from_user((void *)page, data, size);
  1584. if (!i) {
  1585. free_page(page);
  1586. return -EFAULT;
  1587. }
  1588. if (i != PAGE_SIZE)
  1589. memset((char *)page + i, 0, PAGE_SIZE - i);
  1590. *where = page;
  1591. return 0;
  1592. }
  1593. /*
  1594. * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
  1595. * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
  1596. *
  1597. * data is a (void *) that can point to any structure up to
  1598. * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
  1599. * information (or be NULL).
  1600. *
  1601. * Pre-0.97 versions of mount() didn't have a flags word.
  1602. * When the flags word was introduced its top half was required
  1603. * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
  1604. * Therefore, if this magic number is present, it carries no information
  1605. * and must be discarded.
  1606. */
  1607. long do_mount(char *dev_name, char *dir_name, char *type_page,
  1608. unsigned long flags, void *data_page)
  1609. {
  1610. struct path path;
  1611. int retval = 0;
  1612. int mnt_flags = 0;
  1613. /* Discard magic */
  1614. if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
  1615. flags &= ~MS_MGC_MSK;
  1616. /* Basic sanity checks */
  1617. if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
  1618. return -EINVAL;
  1619. if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
  1620. return -EINVAL;
  1621. if (data_page)
  1622. ((char *)data_page)[PAGE_SIZE - 1] = 0;
  1623. /* Default to relatime unless overriden */
  1624. if (!(flags & MS_NOATIME))
  1625. mnt_flags |= MNT_RELATIME;
  1626. /* Separate the per-mountpoint flags */
  1627. if (flags & MS_NOSUID)
  1628. mnt_flags |= MNT_NOSUID;
  1629. if (flags & MS_NODEV)
  1630. mnt_flags |= MNT_NODEV;
  1631. if (flags & MS_NOEXEC)
  1632. mnt_flags |= MNT_NOEXEC;
  1633. if (flags & MS_NOATIME)
  1634. mnt_flags |= MNT_NOATIME;
  1635. if (flags & MS_NODIRATIME)
  1636. mnt_flags |= MNT_NODIRATIME;
  1637. if (flags & MS_STRICTATIME)
  1638. mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
  1639. if (flags & MS_RDONLY)
  1640. mnt_flags |= MNT_READONLY;
  1641. flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE |
  1642. MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
  1643. MS_STRICTATIME);
  1644. /* ... and get the mountpoint */
  1645. retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);
  1646. if (retval)
  1647. return retval;
  1648. retval = security_sb_mount(dev_name, &path,
  1649. type_page, flags, data_page);
  1650. if (retval)
  1651. goto dput_out;
  1652. if (flags & MS_REMOUNT)
  1653. retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
  1654. data_page);
  1655. else if (flags & MS_BIND)
  1656. retval = do_loopback(&path, dev_name, flags & MS_REC);
  1657. else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
  1658. retval = do_change_type(&path, flags);
  1659. else if (flags & MS_MOVE)
  1660. retval = do_move_mount(&path, dev_name);
  1661. else
  1662. retval = do_new_mount(&path, type_page, flags, mnt_flags,
  1663. dev_name, data_page);
  1664. dput_out:
  1665. path_put(&path);
  1666. return retval;
  1667. }
  1668. /*
  1669. * Allocate a new namespace structure and populate it with contents
  1670. * copied from the namespace of the passed in task structure.
  1671. */
  1672. static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
  1673. struct fs_struct *fs)
  1674. {
  1675. struct mnt_namespace *new_ns;
  1676. struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
  1677. struct vfsmount *p, *q;
  1678. new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
  1679. if (!new_ns)
  1680. return ERR_PTR(-ENOMEM);
  1681. atomic_set(&new_ns->count, 1);
  1682. INIT_LIST_HEAD(&new_ns->list);
  1683. init_waitqueue_head(&new_ns->poll);
  1684. new_ns->event = 0;
  1685. down_write(&namespace_sem);
  1686. /* First pass: copy the tree topology */
  1687. new_ns->root = copy_tree(mnt_ns->root, mnt_ns->root->mnt_root,
  1688. CL_COPY_ALL | CL_EXPIRE);
  1689. if (!new_ns->root) {
  1690. up_write(&namespace_sem);
  1691. kfree(new_ns);
  1692. return ERR_PTR(-ENOMEM);
  1693. }
  1694. spin_lock(&vfsmount_lock);
  1695. list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
  1696. spin_unlock(&vfsmount_lock);
  1697. /*
  1698. * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
  1699. * as belonging to new namespace. We have already acquired a private
  1700. * fs_struct, so tsk->fs->lock is not needed.
  1701. */
  1702. p = mnt_ns->root;
  1703. q = new_ns->root;
  1704. while (p) {
  1705. q->mnt_ns = new_ns;
  1706. if (fs) {
  1707. if (p == fs->root.mnt) {
  1708. rootmnt = p;
  1709. fs->root.mnt = mntget(q);
  1710. }
  1711. if (p == fs->pwd.mnt) {
  1712. pwdmnt = p;
  1713. fs->pwd.mnt = mntget(q);
  1714. }
  1715. }
  1716. p = next_mnt(p, mnt_ns->root);
  1717. q = next_mnt(q, new_ns->root);
  1718. }
  1719. up_write(&namespace_sem);
  1720. if (rootmnt)
  1721. mntput(rootmnt);
  1722. if (pwdmnt)
  1723. mntput(pwdmnt);
  1724. return new_ns;
  1725. }
  1726. struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
  1727. struct fs_struct *new_fs)
  1728. {
  1729. struct mnt_namespace *new_ns;
  1730. BUG_ON(!ns);
  1731. get_mnt_ns(ns);
  1732. if (!(flags & CLONE_NEWNS))
  1733. return ns;
  1734. new_ns = dup_mnt_ns(ns, new_fs);
  1735. put_mnt_ns(ns);
  1736. return new_ns;
  1737. }
  1738. SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
  1739. char __user *, type, unsigned long, flags, void __user *, data)
  1740. {
  1741. int retval;
  1742. unsigned long data_page;
  1743. unsigned long type_page;
  1744. unsigned long dev_page;
  1745. char *dir_page;
  1746. retval = copy_mount_options(type, &type_page);
  1747. if (retval < 0)
  1748. return retval;
  1749. dir_page = getname(dir_name);
  1750. retval = PTR_ERR(dir_page);
  1751. if (IS_ERR(dir_page))
  1752. goto out1;
  1753. retval = copy_mount_options(dev_name, &dev_page);
  1754. if (retval < 0)
  1755. goto out2;
  1756. retval = copy_mount_options(data, &data_page);
  1757. if (retval < 0)
  1758. goto out3;
  1759. lock_kernel();
  1760. retval = do_mount((char *)dev_page, dir_page, (char *)type_page,
  1761. flags, (void *)data_page);
  1762. unlock_kernel();
  1763. free_page(data_page);
  1764. out3:
  1765. free_page(dev_page);
  1766. out2:
  1767. putname(dir_page);
  1768. out1:
  1769. free_page(type_page);
  1770. return retval;
  1771. }
  1772. /*
  1773. * pivot_root Semantics:
  1774. * Moves the root file system of the current process to the directory put_old,
  1775. * makes new_root as the new root file system of the current process, and sets
  1776. * root/cwd of all processes which had them on the current root to new_root.
  1777. *
  1778. * Restrictions:
  1779. * The new_root and put_old must be directories, and must not be on the
  1780. * same file system as the current process root. The put_old must be
  1781. * underneath new_root, i.e. adding a non-zero number of /.. to the string
  1782. * pointed to by put_old must yield the same directory as new_root. No other
  1783. * file system may be mounted on put_old. After all, new_root is a mountpoint.
  1784. *
  1785. * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
  1786. * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
  1787. * in this situation.
  1788. *
  1789. * Notes:
  1790. * - we don't move root/cwd if they are not at the root (reason: if something
  1791. * cared enough to change them, it's probably wrong to force them elsewhere)
  1792. * - it's okay to pick a root that isn't the root of a file system, e.g.
  1793. * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
  1794. * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
  1795. * first.
  1796. */
  1797. SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
  1798. const char __user *, put_old)
  1799. {
  1800. struct vfsmount *tmp;
  1801. struct path new, old, parent_path, root_parent, root;
  1802. int error;
  1803. if (!capable(CAP_SYS_ADMIN))
  1804. return -EPERM;
  1805. error = user_path_dir(new_root, &new);
  1806. if (error)
  1807. goto out0;
  1808. error = -EINVAL;
  1809. if (!check_mnt(new.mnt))
  1810. goto out1;
  1811. error = user_path_dir(put_old, &old);
  1812. if (error)
  1813. goto out1;
  1814. error = security_sb_pivotroot(&old, &new);
  1815. if (error) {
  1816. path_put(&old);
  1817. goto out1;
  1818. }
  1819. read_lock(&current->fs->lock);
  1820. root = current->fs->root;
  1821. path_get(&current->fs->root);
  1822. read_unlock(&current->fs->lock);
  1823. down_write(&namespace_sem);
  1824. mutex_lock(&old.dentry->d_inode->i_mutex);
  1825. error = -EINVAL;
  1826. if (IS_MNT_SHARED(old.mnt) ||
  1827. IS_MNT_SHARED(new.mnt->mnt_parent) ||
  1828. IS_MNT_SHARED(root.mnt->mnt_parent))
  1829. goto out2;
  1830. if (!check_mnt(root.mnt))
  1831. goto out2;
  1832. error = -ENOENT;
  1833. if (IS_DEADDIR(new.dentry->d_inode))
  1834. goto out2;
  1835. if (d_unhashed(new.dentry) && !IS_ROOT(new.dentry))
  1836. goto out2;
  1837. if (d_unhashed(old.dentry) && !IS_ROOT(old.dentry))
  1838. goto out2;
  1839. error = -EBUSY;
  1840. if (new.mnt == root.mnt ||
  1841. old.mnt == root.mnt)
  1842. goto out2; /* loop, on the same file system */
  1843. error = -EINVAL;
  1844. if (root.mnt->mnt_root != root.dentry)
  1845. goto out2; /* not a mountpoint */
  1846. if (root.mnt->mnt_parent == root.mnt)
  1847. goto out2; /* not attached */
  1848. if (new.mnt->mnt_root != new.dentry)
  1849. goto out2; /* not a mountpoint */
  1850. if (new.mnt->mnt_parent == new.mnt)
  1851. goto out2; /* not attached */
  1852. /* make sure we can reach put_old from new_root */
  1853. tmp = old.mnt;
  1854. spin_lock(&vfsmount_lock);
  1855. if (tmp != new.mnt) {
  1856. for (;;) {
  1857. if (tmp->mnt_parent == tmp)
  1858. goto out3; /* already mounted on put_old */
  1859. if (tmp->mnt_parent == new.mnt)
  1860. break;
  1861. tmp = tmp->mnt_parent;
  1862. }
  1863. if (!is_subdir(tmp->mnt_mountpoint, new.dentry))
  1864. goto out3;
  1865. } else if (!is_subdir(old.dentry, new.dentry))
  1866. goto out3;
  1867. detach_mnt(new.mnt, &parent_path);
  1868. detach_mnt(root.mnt, &root_parent);
  1869. /* mount old root on put_old */
  1870. attach_mnt(root.mnt, &old);
  1871. /* mount new_root on / */
  1872. attach_mnt(new.mnt, &root_parent);
  1873. touch_mnt_namespace(current->nsproxy->mnt_ns);
  1874. spin_unlock(&vfsmount_lock);
  1875. chroot_fs_refs(&root, &new);
  1876. security_sb_post_pivotroot(&root, &new);
  1877. error = 0;
  1878. path_put(&root_parent);
  1879. path_put(&parent_path);
  1880. out2:
  1881. mutex_unlock(&old.dentry->d_inode->i_mutex);
  1882. up_write(&namespace_sem);
  1883. path_put(&root);
  1884. path_put(&old);
  1885. out1:
  1886. path_put(&new);
  1887. out0:
  1888. return error;
  1889. out3:
  1890. spin_unlock(&vfsmount_lock);
  1891. goto out2;
  1892. }
  1893. static void __init init_mount_tree(void)
  1894. {
  1895. struct vfsmount *mnt;
  1896. struct mnt_namespace *ns;
  1897. struct path root;
  1898. mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
  1899. if (IS_ERR(mnt))
  1900. panic("Can't create rootfs");
  1901. ns = kmalloc(sizeof(*ns), GFP_KERNEL);
  1902. if (!ns)
  1903. panic("Can't allocate initial namespace");
  1904. atomic_set(&ns->count, 1);
  1905. INIT_LIST_HEAD(&ns->list);
  1906. init_waitqueue_head(&ns->poll);
  1907. ns->event = 0;
  1908. list_add(&mnt->mnt_list, &ns->list);
  1909. ns->root = mnt;
  1910. mnt->mnt_ns = ns;
  1911. init_task.nsproxy->mnt_ns = ns;
  1912. get_mnt_ns(ns);
  1913. root.mnt = ns->root;
  1914. root.dentry = ns->root->mnt_root;
  1915. set_fs_pwd(current->fs, &root);
  1916. set_fs_root(current->fs, &root);
  1917. }
  1918. void __init mnt_init(void)
  1919. {
  1920. unsigned u;
  1921. int err;
  1922. init_rwsem(&namespace_sem);
  1923. mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
  1924. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  1925. mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
  1926. if (!mount_hashtable)
  1927. panic("Failed to allocate mount hash table\n");
  1928. printk("Mount-cache hash table entries: %lu\n", HASH_SIZE);
  1929. for (u = 0; u < HASH_SIZE; u++)
  1930. INIT_LIST_HEAD(&mount_hashtable[u]);
  1931. err = sysfs_init();
  1932. if (err)
  1933. printk(KERN_WARNING "%s: sysfs_init error: %d\n",
  1934. __func__, err);
  1935. fs_kobj = kobject_create_and_add("fs", NULL);
  1936. if (!fs_kobj)
  1937. printk(KERN_WARNING "%s: kobj create error\n", __func__);
  1938. init_rootfs();
  1939. init_mount_tree();
  1940. }
  1941. void __put_mnt_ns(struct mnt_namespace *ns)
  1942. {
  1943. struct vfsmount *root = ns->root;
  1944. LIST_HEAD(umount_list);
  1945. ns->root = NULL;
  1946. spin_unlock(&vfsmount_lock);
  1947. down_write(&namespace_sem);
  1948. spin_lock(&vfsmount_lock);
  1949. umount_tree(root, 0, &umount_list);
  1950. spin_unlock(&vfsmount_lock);
  1951. up_write(&namespace_sem);
  1952. release_mounts(&umount_list);
  1953. kfree(ns);
  1954. }