namespace.c 49 KB

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