namespace.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921
  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 path *old_path)
  141. {
  142. old_path->dentry = mnt->mnt_mountpoint;
  143. old_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_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 path *path)
  158. {
  159. mnt_set_mountpoint(path->mnt, path->dentry, mnt);
  160. list_add_tail(&mnt->mnt_hash, mount_hashtable +
  161. hash(path->mnt, path->dentry));
  162. list_add_tail(&mnt->mnt_child, &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. m->mnt_ghosts--;
  487. spin_unlock(&vfsmount_lock);
  488. dput(dentry);
  489. mntput(m);
  490. }
  491. mntput(mnt);
  492. }
  493. }
  494. void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill)
  495. {
  496. struct vfsmount *p;
  497. for (p = mnt; p; p = next_mnt(p, mnt))
  498. list_move(&p->mnt_hash, kill);
  499. if (propagate)
  500. propagate_umount(kill);
  501. list_for_each_entry(p, kill, mnt_hash) {
  502. list_del_init(&p->mnt_expire);
  503. list_del_init(&p->mnt_list);
  504. __touch_mnt_namespace(p->mnt_ns);
  505. p->mnt_ns = NULL;
  506. list_del_init(&p->mnt_child);
  507. if (p->mnt_parent != p) {
  508. p->mnt_parent->mnt_ghosts++;
  509. p->mnt_mountpoint->d_mounted--;
  510. }
  511. change_mnt_propagation(p, MS_PRIVATE);
  512. }
  513. }
  514. static int do_umount(struct vfsmount *mnt, int flags)
  515. {
  516. struct super_block *sb = mnt->mnt_sb;
  517. int retval;
  518. LIST_HEAD(umount_list);
  519. retval = security_sb_umount(mnt, flags);
  520. if (retval)
  521. return retval;
  522. /*
  523. * Allow userspace to request a mountpoint be expired rather than
  524. * unmounting unconditionally. Unmount only happens if:
  525. * (1) the mark is already set (the mark is cleared by mntput())
  526. * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
  527. */
  528. if (flags & MNT_EXPIRE) {
  529. if (mnt == current->fs->root.mnt ||
  530. flags & (MNT_FORCE | MNT_DETACH))
  531. return -EINVAL;
  532. if (atomic_read(&mnt->mnt_count) != 2)
  533. return -EBUSY;
  534. if (!xchg(&mnt->mnt_expiry_mark, 1))
  535. return -EAGAIN;
  536. }
  537. /*
  538. * If we may have to abort operations to get out of this
  539. * mount, and they will themselves hold resources we must
  540. * allow the fs to do things. In the Unix tradition of
  541. * 'Gee thats tricky lets do it in userspace' the umount_begin
  542. * might fail to complete on the first run through as other tasks
  543. * must return, and the like. Thats for the mount program to worry
  544. * about for the moment.
  545. */
  546. lock_kernel();
  547. if (sb->s_op->umount_begin)
  548. sb->s_op->umount_begin(mnt, flags);
  549. unlock_kernel();
  550. /*
  551. * No sense to grab the lock for this test, but test itself looks
  552. * somewhat bogus. Suggestions for better replacement?
  553. * Ho-hum... In principle, we might treat that as umount + switch
  554. * to rootfs. GC would eventually take care of the old vfsmount.
  555. * Actually it makes sense, especially if rootfs would contain a
  556. * /reboot - static binary that would close all descriptors and
  557. * call reboot(9). Then init(8) could umount root and exec /reboot.
  558. */
  559. if (mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
  560. /*
  561. * Special case for "unmounting" root ...
  562. * we just try to remount it readonly.
  563. */
  564. down_write(&sb->s_umount);
  565. if (!(sb->s_flags & MS_RDONLY)) {
  566. lock_kernel();
  567. DQUOT_OFF(sb);
  568. retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
  569. unlock_kernel();
  570. }
  571. up_write(&sb->s_umount);
  572. return retval;
  573. }
  574. down_write(&namespace_sem);
  575. spin_lock(&vfsmount_lock);
  576. event++;
  577. retval = -EBUSY;
  578. if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
  579. if (!list_empty(&mnt->mnt_list))
  580. umount_tree(mnt, 1, &umount_list);
  581. retval = 0;
  582. }
  583. spin_unlock(&vfsmount_lock);
  584. if (retval)
  585. security_sb_umount_busy(mnt);
  586. up_write(&namespace_sem);
  587. release_mounts(&umount_list);
  588. return retval;
  589. }
  590. /*
  591. * Now umount can handle mount points as well as block devices.
  592. * This is important for filesystems which use unnamed block devices.
  593. *
  594. * We now support a flag for forced unmount like the other 'big iron'
  595. * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
  596. */
  597. asmlinkage long sys_umount(char __user * name, int flags)
  598. {
  599. struct nameidata nd;
  600. int retval;
  601. retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
  602. if (retval)
  603. goto out;
  604. retval = -EINVAL;
  605. if (nd.path.dentry != nd.path.mnt->mnt_root)
  606. goto dput_and_out;
  607. if (!check_mnt(nd.path.mnt))
  608. goto dput_and_out;
  609. retval = -EPERM;
  610. if (!capable(CAP_SYS_ADMIN))
  611. goto dput_and_out;
  612. retval = do_umount(nd.path.mnt, flags);
  613. dput_and_out:
  614. /* we mustn't call path_put() as that would clear mnt_expiry_mark */
  615. dput(nd.path.dentry);
  616. mntput_no_expire(nd.path.mnt);
  617. out:
  618. return retval;
  619. }
  620. #ifdef __ARCH_WANT_SYS_OLDUMOUNT
  621. /*
  622. * The 2.0 compatible umount. No flags.
  623. */
  624. asmlinkage long sys_oldumount(char __user * name)
  625. {
  626. return sys_umount(name, 0);
  627. }
  628. #endif
  629. static int mount_is_safe(struct nameidata *nd)
  630. {
  631. if (capable(CAP_SYS_ADMIN))
  632. return 0;
  633. return -EPERM;
  634. #ifdef notyet
  635. if (S_ISLNK(nd->path.dentry->d_inode->i_mode))
  636. return -EPERM;
  637. if (nd->path.dentry->d_inode->i_mode & S_ISVTX) {
  638. if (current->uid != nd->path.dentry->d_inode->i_uid)
  639. return -EPERM;
  640. }
  641. if (vfs_permission(nd, MAY_WRITE))
  642. return -EPERM;
  643. return 0;
  644. #endif
  645. }
  646. static int lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
  647. {
  648. while (1) {
  649. if (d == dentry)
  650. return 1;
  651. if (d == NULL || d == d->d_parent)
  652. return 0;
  653. d = d->d_parent;
  654. }
  655. }
  656. struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
  657. int flag)
  658. {
  659. struct vfsmount *res, *p, *q, *r, *s;
  660. struct path path;
  661. if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
  662. return NULL;
  663. res = q = clone_mnt(mnt, dentry, flag);
  664. if (!q)
  665. goto Enomem;
  666. q->mnt_mountpoint = mnt->mnt_mountpoint;
  667. p = mnt;
  668. list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
  669. if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
  670. continue;
  671. for (s = r; s; s = next_mnt(s, r)) {
  672. if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
  673. s = skip_mnt_tree(s);
  674. continue;
  675. }
  676. while (p != s->mnt_parent) {
  677. p = p->mnt_parent;
  678. q = q->mnt_parent;
  679. }
  680. p = s;
  681. path.mnt = q;
  682. path.dentry = p->mnt_mountpoint;
  683. q = clone_mnt(p, p->mnt_root, flag);
  684. if (!q)
  685. goto Enomem;
  686. spin_lock(&vfsmount_lock);
  687. list_add_tail(&q->mnt_list, &res->mnt_list);
  688. attach_mnt(q, &path);
  689. spin_unlock(&vfsmount_lock);
  690. }
  691. }
  692. return res;
  693. Enomem:
  694. if (res) {
  695. LIST_HEAD(umount_list);
  696. spin_lock(&vfsmount_lock);
  697. umount_tree(res, 0, &umount_list);
  698. spin_unlock(&vfsmount_lock);
  699. release_mounts(&umount_list);
  700. }
  701. return NULL;
  702. }
  703. struct vfsmount *collect_mounts(struct vfsmount *mnt, struct dentry *dentry)
  704. {
  705. struct vfsmount *tree;
  706. down_read(&namespace_sem);
  707. tree = copy_tree(mnt, dentry, CL_COPY_ALL | CL_PRIVATE);
  708. up_read(&namespace_sem);
  709. return tree;
  710. }
  711. void drop_collected_mounts(struct vfsmount *mnt)
  712. {
  713. LIST_HEAD(umount_list);
  714. down_read(&namespace_sem);
  715. spin_lock(&vfsmount_lock);
  716. umount_tree(mnt, 0, &umount_list);
  717. spin_unlock(&vfsmount_lock);
  718. up_read(&namespace_sem);
  719. release_mounts(&umount_list);
  720. }
  721. /*
  722. * @source_mnt : mount tree to be attached
  723. * @nd : place the mount tree @source_mnt is attached
  724. * @parent_nd : if non-null, detach the source_mnt from its parent and
  725. * store the parent mount and mountpoint dentry.
  726. * (done when source_mnt is moved)
  727. *
  728. * NOTE: in the table below explains the semantics when a source mount
  729. * of a given type is attached to a destination mount of a given type.
  730. * ---------------------------------------------------------------------------
  731. * | BIND MOUNT OPERATION |
  732. * |**************************************************************************
  733. * | source-->| shared | private | slave | unbindable |
  734. * | dest | | | | |
  735. * | | | | | | |
  736. * | v | | | | |
  737. * |**************************************************************************
  738. * | shared | shared (++) | shared (+) | shared(+++)| invalid |
  739. * | | | | | |
  740. * |non-shared| shared (+) | private | slave (*) | invalid |
  741. * ***************************************************************************
  742. * A bind operation clones the source mount and mounts the clone on the
  743. * destination mount.
  744. *
  745. * (++) the cloned mount is propagated to all the mounts in the propagation
  746. * tree of the destination mount and the cloned mount is added to
  747. * the peer group of the source mount.
  748. * (+) the cloned mount is created under the destination mount and is marked
  749. * as shared. The cloned mount is added to the peer group of the source
  750. * mount.
  751. * (+++) the mount is propagated to all the mounts in the propagation tree
  752. * of the destination mount and the cloned mount is made slave
  753. * of the same master as that of the source mount. The cloned mount
  754. * is marked as 'shared and slave'.
  755. * (*) the cloned mount is made a slave of the same master as that of the
  756. * source mount.
  757. *
  758. * ---------------------------------------------------------------------------
  759. * | MOVE MOUNT OPERATION |
  760. * |**************************************************************************
  761. * | source-->| shared | private | slave | unbindable |
  762. * | dest | | | | |
  763. * | | | | | | |
  764. * | v | | | | |
  765. * |**************************************************************************
  766. * | shared | shared (+) | shared (+) | shared(+++) | invalid |
  767. * | | | | | |
  768. * |non-shared| shared (+*) | private | slave (*) | unbindable |
  769. * ***************************************************************************
  770. *
  771. * (+) the mount is moved to the destination. And is then propagated to
  772. * all the mounts in the propagation tree of the destination mount.
  773. * (+*) the mount is moved to the destination.
  774. * (+++) the mount is moved to the destination and is then propagated to
  775. * all the mounts belonging to the destination mount's propagation tree.
  776. * the mount is marked as 'shared and slave'.
  777. * (*) the mount continues to be a slave at the new location.
  778. *
  779. * if the source mount is a tree, the operations explained above is
  780. * applied to each mount in the tree.
  781. * Must be called without spinlocks held, since this function can sleep
  782. * in allocations.
  783. */
  784. static int attach_recursive_mnt(struct vfsmount *source_mnt,
  785. struct path *path, struct path *parent_path)
  786. {
  787. LIST_HEAD(tree_list);
  788. struct vfsmount *dest_mnt = path->mnt;
  789. struct dentry *dest_dentry = path->dentry;
  790. struct vfsmount *child, *p;
  791. if (propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list))
  792. return -EINVAL;
  793. if (IS_MNT_SHARED(dest_mnt)) {
  794. for (p = source_mnt; p; p = next_mnt(p, source_mnt))
  795. set_mnt_shared(p);
  796. }
  797. spin_lock(&vfsmount_lock);
  798. if (parent_path) {
  799. detach_mnt(source_mnt, parent_path);
  800. attach_mnt(source_mnt, path);
  801. touch_mnt_namespace(current->nsproxy->mnt_ns);
  802. } else {
  803. mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
  804. commit_tree(source_mnt);
  805. }
  806. list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
  807. list_del_init(&child->mnt_hash);
  808. commit_tree(child);
  809. }
  810. spin_unlock(&vfsmount_lock);
  811. return 0;
  812. }
  813. static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
  814. {
  815. int err;
  816. if (mnt->mnt_sb->s_flags & MS_NOUSER)
  817. return -EINVAL;
  818. if (S_ISDIR(nd->path.dentry->d_inode->i_mode) !=
  819. S_ISDIR(mnt->mnt_root->d_inode->i_mode))
  820. return -ENOTDIR;
  821. err = -ENOENT;
  822. mutex_lock(&nd->path.dentry->d_inode->i_mutex);
  823. if (IS_DEADDIR(nd->path.dentry->d_inode))
  824. goto out_unlock;
  825. err = security_sb_check_sb(mnt, nd);
  826. if (err)
  827. goto out_unlock;
  828. err = -ENOENT;
  829. if (IS_ROOT(nd->path.dentry) || !d_unhashed(nd->path.dentry))
  830. err = attach_recursive_mnt(mnt, &nd->path, NULL);
  831. out_unlock:
  832. mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
  833. if (!err)
  834. security_sb_post_addmount(mnt, nd);
  835. return err;
  836. }
  837. /*
  838. * recursively change the type of the mountpoint.
  839. * noinline this do_mount helper to save do_mount stack space.
  840. */
  841. static noinline int do_change_type(struct nameidata *nd, int flag)
  842. {
  843. struct vfsmount *m, *mnt = nd->path.mnt;
  844. int recurse = flag & MS_REC;
  845. int type = flag & ~MS_REC;
  846. if (!capable(CAP_SYS_ADMIN))
  847. return -EPERM;
  848. if (nd->path.dentry != nd->path.mnt->mnt_root)
  849. return -EINVAL;
  850. down_write(&namespace_sem);
  851. spin_lock(&vfsmount_lock);
  852. for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
  853. change_mnt_propagation(m, type);
  854. spin_unlock(&vfsmount_lock);
  855. up_write(&namespace_sem);
  856. return 0;
  857. }
  858. /*
  859. * do loopback mount.
  860. * noinline this do_mount helper to save do_mount stack space.
  861. */
  862. static noinline int do_loopback(struct nameidata *nd, char *old_name,
  863. int recurse)
  864. {
  865. struct nameidata old_nd;
  866. struct vfsmount *mnt = NULL;
  867. int err = mount_is_safe(nd);
  868. if (err)
  869. return err;
  870. if (!old_name || !*old_name)
  871. return -EINVAL;
  872. err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
  873. if (err)
  874. return err;
  875. down_write(&namespace_sem);
  876. err = -EINVAL;
  877. if (IS_MNT_UNBINDABLE(old_nd.path.mnt))
  878. goto out;
  879. if (!check_mnt(nd->path.mnt) || !check_mnt(old_nd.path.mnt))
  880. goto out;
  881. err = -ENOMEM;
  882. if (recurse)
  883. mnt = copy_tree(old_nd.path.mnt, old_nd.path.dentry, 0);
  884. else
  885. mnt = clone_mnt(old_nd.path.mnt, old_nd.path.dentry, 0);
  886. if (!mnt)
  887. goto out;
  888. err = graft_tree(mnt, nd);
  889. if (err) {
  890. LIST_HEAD(umount_list);
  891. spin_lock(&vfsmount_lock);
  892. umount_tree(mnt, 0, &umount_list);
  893. spin_unlock(&vfsmount_lock);
  894. release_mounts(&umount_list);
  895. }
  896. out:
  897. up_write(&namespace_sem);
  898. path_put(&old_nd.path);
  899. return err;
  900. }
  901. /*
  902. * change filesystem flags. dir should be a physical root of filesystem.
  903. * If you've mounted a non-root directory somewhere and want to do remount
  904. * on it - tough luck.
  905. * noinline this do_mount helper to save do_mount stack space.
  906. */
  907. static noinline int do_remount(struct nameidata *nd, int flags, int mnt_flags,
  908. void *data)
  909. {
  910. int err;
  911. struct super_block *sb = nd->path.mnt->mnt_sb;
  912. if (!capable(CAP_SYS_ADMIN))
  913. return -EPERM;
  914. if (!check_mnt(nd->path.mnt))
  915. return -EINVAL;
  916. if (nd->path.dentry != nd->path.mnt->mnt_root)
  917. return -EINVAL;
  918. down_write(&sb->s_umount);
  919. err = do_remount_sb(sb, flags, data, 0);
  920. if (!err)
  921. nd->path.mnt->mnt_flags = mnt_flags;
  922. up_write(&sb->s_umount);
  923. if (!err)
  924. security_sb_post_remount(nd->path.mnt, flags, data);
  925. return err;
  926. }
  927. static inline int tree_contains_unbindable(struct vfsmount *mnt)
  928. {
  929. struct vfsmount *p;
  930. for (p = mnt; p; p = next_mnt(p, mnt)) {
  931. if (IS_MNT_UNBINDABLE(p))
  932. return 1;
  933. }
  934. return 0;
  935. }
  936. /*
  937. * noinline this do_mount helper to save do_mount stack space.
  938. */
  939. static noinline int do_move_mount(struct nameidata *nd, char *old_name)
  940. {
  941. struct nameidata old_nd;
  942. struct path parent_path;
  943. struct vfsmount *p;
  944. int err = 0;
  945. if (!capable(CAP_SYS_ADMIN))
  946. return -EPERM;
  947. if (!old_name || !*old_name)
  948. return -EINVAL;
  949. err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
  950. if (err)
  951. return err;
  952. down_write(&namespace_sem);
  953. while (d_mountpoint(nd->path.dentry) &&
  954. follow_down(&nd->path.mnt, &nd->path.dentry))
  955. ;
  956. err = -EINVAL;
  957. if (!check_mnt(nd->path.mnt) || !check_mnt(old_nd.path.mnt))
  958. goto out;
  959. err = -ENOENT;
  960. mutex_lock(&nd->path.dentry->d_inode->i_mutex);
  961. if (IS_DEADDIR(nd->path.dentry->d_inode))
  962. goto out1;
  963. if (!IS_ROOT(nd->path.dentry) && d_unhashed(nd->path.dentry))
  964. goto out1;
  965. err = -EINVAL;
  966. if (old_nd.path.dentry != old_nd.path.mnt->mnt_root)
  967. goto out1;
  968. if (old_nd.path.mnt == old_nd.path.mnt->mnt_parent)
  969. goto out1;
  970. if (S_ISDIR(nd->path.dentry->d_inode->i_mode) !=
  971. S_ISDIR(old_nd.path.dentry->d_inode->i_mode))
  972. goto out1;
  973. /*
  974. * Don't move a mount residing in a shared parent.
  975. */
  976. if (old_nd.path.mnt->mnt_parent &&
  977. IS_MNT_SHARED(old_nd.path.mnt->mnt_parent))
  978. goto out1;
  979. /*
  980. * Don't move a mount tree containing unbindable mounts to a destination
  981. * mount which is shared.
  982. */
  983. if (IS_MNT_SHARED(nd->path.mnt) &&
  984. tree_contains_unbindable(old_nd.path.mnt))
  985. goto out1;
  986. err = -ELOOP;
  987. for (p = nd->path.mnt; p->mnt_parent != p; p = p->mnt_parent)
  988. if (p == old_nd.path.mnt)
  989. goto out1;
  990. err = attach_recursive_mnt(old_nd.path.mnt, &nd->path, &parent_path);
  991. if (err)
  992. goto out1;
  993. spin_lock(&vfsmount_lock);
  994. /* if the mount is moved, it should no longer be expire
  995. * automatically */
  996. list_del_init(&old_nd.path.mnt->mnt_expire);
  997. spin_unlock(&vfsmount_lock);
  998. out1:
  999. mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
  1000. out:
  1001. up_write(&namespace_sem);
  1002. if (!err)
  1003. path_put(&parent_path);
  1004. path_put(&old_nd.path);
  1005. return err;
  1006. }
  1007. /*
  1008. * create a new mount for userspace and request it to be added into the
  1009. * namespace's tree
  1010. * noinline this do_mount helper to save do_mount stack space.
  1011. */
  1012. static noinline int do_new_mount(struct nameidata *nd, char *type, int flags,
  1013. int mnt_flags, char *name, void *data)
  1014. {
  1015. struct vfsmount *mnt;
  1016. if (!type || !memchr(type, 0, PAGE_SIZE))
  1017. return -EINVAL;
  1018. /* we need capabilities... */
  1019. if (!capable(CAP_SYS_ADMIN))
  1020. return -EPERM;
  1021. mnt = do_kern_mount(type, flags, name, data);
  1022. if (IS_ERR(mnt))
  1023. return PTR_ERR(mnt);
  1024. return do_add_mount(mnt, nd, mnt_flags, NULL);
  1025. }
  1026. /*
  1027. * add a mount into a namespace's mount tree
  1028. * - provide the option of adding the new mount to an expiration list
  1029. */
  1030. int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
  1031. int mnt_flags, struct list_head *fslist)
  1032. {
  1033. int err;
  1034. down_write(&namespace_sem);
  1035. /* Something was mounted here while we slept */
  1036. while (d_mountpoint(nd->path.dentry) &&
  1037. follow_down(&nd->path.mnt, &nd->path.dentry))
  1038. ;
  1039. err = -EINVAL;
  1040. if (!check_mnt(nd->path.mnt))
  1041. goto unlock;
  1042. /* Refuse the same filesystem on the same mount point */
  1043. err = -EBUSY;
  1044. if (nd->path.mnt->mnt_sb == newmnt->mnt_sb &&
  1045. nd->path.mnt->mnt_root == nd->path.dentry)
  1046. goto unlock;
  1047. err = -EINVAL;
  1048. if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
  1049. goto unlock;
  1050. newmnt->mnt_flags = mnt_flags;
  1051. if ((err = graft_tree(newmnt, nd)))
  1052. goto unlock;
  1053. if (fslist) {
  1054. /* add to the specified expiration list */
  1055. spin_lock(&vfsmount_lock);
  1056. list_add_tail(&newmnt->mnt_expire, fslist);
  1057. spin_unlock(&vfsmount_lock);
  1058. }
  1059. up_write(&namespace_sem);
  1060. return 0;
  1061. unlock:
  1062. up_write(&namespace_sem);
  1063. mntput(newmnt);
  1064. return err;
  1065. }
  1066. EXPORT_SYMBOL_GPL(do_add_mount);
  1067. static void expire_mount(struct vfsmount *mnt, struct list_head *mounts,
  1068. struct list_head *umounts)
  1069. {
  1070. spin_lock(&vfsmount_lock);
  1071. /*
  1072. * Check if mount is still attached, if not, let whoever holds it deal
  1073. * with the sucker
  1074. */
  1075. if (mnt->mnt_parent == mnt) {
  1076. spin_unlock(&vfsmount_lock);
  1077. return;
  1078. }
  1079. /*
  1080. * Check that it is still dead: the count should now be 2 - as
  1081. * contributed by the vfsmount parent and the mntget above
  1082. */
  1083. if (!propagate_mount_busy(mnt, 2)) {
  1084. /* delete from the namespace */
  1085. touch_mnt_namespace(mnt->mnt_ns);
  1086. list_del_init(&mnt->mnt_list);
  1087. mnt->mnt_ns = NULL;
  1088. umount_tree(mnt, 1, umounts);
  1089. spin_unlock(&vfsmount_lock);
  1090. } else {
  1091. /*
  1092. * Someone brought it back to life whilst we didn't have any
  1093. * locks held so return it to the expiration list
  1094. */
  1095. list_add_tail(&mnt->mnt_expire, mounts);
  1096. spin_unlock(&vfsmount_lock);
  1097. }
  1098. }
  1099. /*
  1100. * go through the vfsmounts we've just consigned to the graveyard to
  1101. * - check that they're still dead
  1102. * - delete the vfsmount from the appropriate namespace under lock
  1103. * - dispose of the corpse
  1104. */
  1105. static void expire_mount_list(struct list_head *graveyard, struct list_head *mounts)
  1106. {
  1107. struct mnt_namespace *ns;
  1108. struct vfsmount *mnt;
  1109. while (!list_empty(graveyard)) {
  1110. LIST_HEAD(umounts);
  1111. mnt = list_first_entry(graveyard, struct vfsmount, mnt_expire);
  1112. list_del_init(&mnt->mnt_expire);
  1113. /* don't do anything if the namespace is dead - all the
  1114. * vfsmounts from it are going away anyway */
  1115. ns = mnt->mnt_ns;
  1116. if (!ns || !ns->root)
  1117. continue;
  1118. get_mnt_ns(ns);
  1119. spin_unlock(&vfsmount_lock);
  1120. down_write(&namespace_sem);
  1121. expire_mount(mnt, mounts, &umounts);
  1122. up_write(&namespace_sem);
  1123. release_mounts(&umounts);
  1124. mntput(mnt);
  1125. put_mnt_ns(ns);
  1126. spin_lock(&vfsmount_lock);
  1127. }
  1128. }
  1129. /*
  1130. * process a list of expirable mountpoints with the intent of discarding any
  1131. * mountpoints that aren't in use and haven't been touched since last we came
  1132. * here
  1133. */
  1134. void mark_mounts_for_expiry(struct list_head *mounts)
  1135. {
  1136. struct vfsmount *mnt, *next;
  1137. LIST_HEAD(graveyard);
  1138. if (list_empty(mounts))
  1139. return;
  1140. spin_lock(&vfsmount_lock);
  1141. /* extract from the expiration list every vfsmount that matches the
  1142. * following criteria:
  1143. * - only referenced by its parent vfsmount
  1144. * - still marked for expiry (marked on the last call here; marks are
  1145. * cleared by mntput())
  1146. */
  1147. list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
  1148. if (!xchg(&mnt->mnt_expiry_mark, 1) ||
  1149. atomic_read(&mnt->mnt_count) != 1)
  1150. continue;
  1151. mntget(mnt);
  1152. list_move(&mnt->mnt_expire, &graveyard);
  1153. }
  1154. expire_mount_list(&graveyard, mounts);
  1155. spin_unlock(&vfsmount_lock);
  1156. }
  1157. EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
  1158. /*
  1159. * Ripoff of 'select_parent()'
  1160. *
  1161. * search the list of submounts for a given mountpoint, and move any
  1162. * shrinkable submounts to the 'graveyard' list.
  1163. */
  1164. static int select_submounts(struct vfsmount *parent, struct list_head *graveyard)
  1165. {
  1166. struct vfsmount *this_parent = parent;
  1167. struct list_head *next;
  1168. int found = 0;
  1169. repeat:
  1170. next = this_parent->mnt_mounts.next;
  1171. resume:
  1172. while (next != &this_parent->mnt_mounts) {
  1173. struct list_head *tmp = next;
  1174. struct vfsmount *mnt = list_entry(tmp, struct vfsmount, mnt_child);
  1175. next = tmp->next;
  1176. if (!(mnt->mnt_flags & MNT_SHRINKABLE))
  1177. continue;
  1178. /*
  1179. * Descend a level if the d_mounts list is non-empty.
  1180. */
  1181. if (!list_empty(&mnt->mnt_mounts)) {
  1182. this_parent = mnt;
  1183. goto repeat;
  1184. }
  1185. if (!propagate_mount_busy(mnt, 1)) {
  1186. mntget(mnt);
  1187. list_move_tail(&mnt->mnt_expire, graveyard);
  1188. found++;
  1189. }
  1190. }
  1191. /*
  1192. * All done at this level ... ascend and resume the search
  1193. */
  1194. if (this_parent != parent) {
  1195. next = this_parent->mnt_child.next;
  1196. this_parent = this_parent->mnt_parent;
  1197. goto resume;
  1198. }
  1199. return found;
  1200. }
  1201. /*
  1202. * process a list of expirable mountpoints with the intent of discarding any
  1203. * submounts of a specific parent mountpoint
  1204. */
  1205. void shrink_submounts(struct vfsmount *mountpoint, struct list_head *mounts)
  1206. {
  1207. LIST_HEAD(graveyard);
  1208. int found;
  1209. spin_lock(&vfsmount_lock);
  1210. /* extract submounts of 'mountpoint' from the expiration list */
  1211. while ((found = select_submounts(mountpoint, &graveyard)) != 0)
  1212. expire_mount_list(&graveyard, mounts);
  1213. spin_unlock(&vfsmount_lock);
  1214. }
  1215. EXPORT_SYMBOL_GPL(shrink_submounts);
  1216. /*
  1217. * Some copy_from_user() implementations do not return the exact number of
  1218. * bytes remaining to copy on a fault. But copy_mount_options() requires that.
  1219. * Note that this function differs from copy_from_user() in that it will oops
  1220. * on bad values of `to', rather than returning a short copy.
  1221. */
  1222. static long exact_copy_from_user(void *to, const void __user * from,
  1223. unsigned long n)
  1224. {
  1225. char *t = to;
  1226. const char __user *f = from;
  1227. char c;
  1228. if (!access_ok(VERIFY_READ, from, n))
  1229. return n;
  1230. while (n) {
  1231. if (__get_user(c, f)) {
  1232. memset(t, 0, n);
  1233. break;
  1234. }
  1235. *t++ = c;
  1236. f++;
  1237. n--;
  1238. }
  1239. return n;
  1240. }
  1241. int copy_mount_options(const void __user * data, unsigned long *where)
  1242. {
  1243. int i;
  1244. unsigned long page;
  1245. unsigned long size;
  1246. *where = 0;
  1247. if (!data)
  1248. return 0;
  1249. if (!(page = __get_free_page(GFP_KERNEL)))
  1250. return -ENOMEM;
  1251. /* We only care that *some* data at the address the user
  1252. * gave us is valid. Just in case, we'll zero
  1253. * the remainder of the page.
  1254. */
  1255. /* copy_from_user cannot cross TASK_SIZE ! */
  1256. size = TASK_SIZE - (unsigned long)data;
  1257. if (size > PAGE_SIZE)
  1258. size = PAGE_SIZE;
  1259. i = size - exact_copy_from_user((void *)page, data, size);
  1260. if (!i) {
  1261. free_page(page);
  1262. return -EFAULT;
  1263. }
  1264. if (i != PAGE_SIZE)
  1265. memset((char *)page + i, 0, PAGE_SIZE - i);
  1266. *where = page;
  1267. return 0;
  1268. }
  1269. /*
  1270. * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
  1271. * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
  1272. *
  1273. * data is a (void *) that can point to any structure up to
  1274. * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
  1275. * information (or be NULL).
  1276. *
  1277. * Pre-0.97 versions of mount() didn't have a flags word.
  1278. * When the flags word was introduced its top half was required
  1279. * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
  1280. * Therefore, if this magic number is present, it carries no information
  1281. * and must be discarded.
  1282. */
  1283. long do_mount(char *dev_name, char *dir_name, char *type_page,
  1284. unsigned long flags, void *data_page)
  1285. {
  1286. struct nameidata nd;
  1287. int retval = 0;
  1288. int mnt_flags = 0;
  1289. /* Discard magic */
  1290. if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
  1291. flags &= ~MS_MGC_MSK;
  1292. /* Basic sanity checks */
  1293. if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
  1294. return -EINVAL;
  1295. if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
  1296. return -EINVAL;
  1297. if (data_page)
  1298. ((char *)data_page)[PAGE_SIZE - 1] = 0;
  1299. /* Separate the per-mountpoint flags */
  1300. if (flags & MS_NOSUID)
  1301. mnt_flags |= MNT_NOSUID;
  1302. if (flags & MS_NODEV)
  1303. mnt_flags |= MNT_NODEV;
  1304. if (flags & MS_NOEXEC)
  1305. mnt_flags |= MNT_NOEXEC;
  1306. if (flags & MS_NOATIME)
  1307. mnt_flags |= MNT_NOATIME;
  1308. if (flags & MS_NODIRATIME)
  1309. mnt_flags |= MNT_NODIRATIME;
  1310. if (flags & MS_RELATIME)
  1311. mnt_flags |= MNT_RELATIME;
  1312. flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE |
  1313. MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT);
  1314. /* ... and get the mountpoint */
  1315. retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
  1316. if (retval)
  1317. return retval;
  1318. retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
  1319. if (retval)
  1320. goto dput_out;
  1321. if (flags & MS_REMOUNT)
  1322. retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
  1323. data_page);
  1324. else if (flags & MS_BIND)
  1325. retval = do_loopback(&nd, dev_name, flags & MS_REC);
  1326. else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
  1327. retval = do_change_type(&nd, flags);
  1328. else if (flags & MS_MOVE)
  1329. retval = do_move_mount(&nd, dev_name);
  1330. else
  1331. retval = do_new_mount(&nd, type_page, flags, mnt_flags,
  1332. dev_name, data_page);
  1333. dput_out:
  1334. path_put(&nd.path);
  1335. return retval;
  1336. }
  1337. /*
  1338. * Allocate a new namespace structure and populate it with contents
  1339. * copied from the namespace of the passed in task structure.
  1340. */
  1341. static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
  1342. struct fs_struct *fs)
  1343. {
  1344. struct mnt_namespace *new_ns;
  1345. struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
  1346. struct vfsmount *p, *q;
  1347. new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
  1348. if (!new_ns)
  1349. return ERR_PTR(-ENOMEM);
  1350. atomic_set(&new_ns->count, 1);
  1351. INIT_LIST_HEAD(&new_ns->list);
  1352. init_waitqueue_head(&new_ns->poll);
  1353. new_ns->event = 0;
  1354. down_write(&namespace_sem);
  1355. /* First pass: copy the tree topology */
  1356. new_ns->root = copy_tree(mnt_ns->root, mnt_ns->root->mnt_root,
  1357. CL_COPY_ALL | CL_EXPIRE);
  1358. if (!new_ns->root) {
  1359. up_write(&namespace_sem);
  1360. kfree(new_ns);
  1361. return ERR_PTR(-ENOMEM);;
  1362. }
  1363. spin_lock(&vfsmount_lock);
  1364. list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
  1365. spin_unlock(&vfsmount_lock);
  1366. /*
  1367. * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
  1368. * as belonging to new namespace. We have already acquired a private
  1369. * fs_struct, so tsk->fs->lock is not needed.
  1370. */
  1371. p = mnt_ns->root;
  1372. q = new_ns->root;
  1373. while (p) {
  1374. q->mnt_ns = new_ns;
  1375. if (fs) {
  1376. if (p == fs->root.mnt) {
  1377. rootmnt = p;
  1378. fs->root.mnt = mntget(q);
  1379. }
  1380. if (p == fs->pwd.mnt) {
  1381. pwdmnt = p;
  1382. fs->pwd.mnt = mntget(q);
  1383. }
  1384. if (p == fs->altroot.mnt) {
  1385. altrootmnt = p;
  1386. fs->altroot.mnt = mntget(q);
  1387. }
  1388. }
  1389. p = next_mnt(p, mnt_ns->root);
  1390. q = next_mnt(q, new_ns->root);
  1391. }
  1392. up_write(&namespace_sem);
  1393. if (rootmnt)
  1394. mntput(rootmnt);
  1395. if (pwdmnt)
  1396. mntput(pwdmnt);
  1397. if (altrootmnt)
  1398. mntput(altrootmnt);
  1399. return new_ns;
  1400. }
  1401. struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
  1402. struct fs_struct *new_fs)
  1403. {
  1404. struct mnt_namespace *new_ns;
  1405. BUG_ON(!ns);
  1406. get_mnt_ns(ns);
  1407. if (!(flags & CLONE_NEWNS))
  1408. return ns;
  1409. new_ns = dup_mnt_ns(ns, new_fs);
  1410. put_mnt_ns(ns);
  1411. return new_ns;
  1412. }
  1413. asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
  1414. char __user * type, unsigned long flags,
  1415. void __user * data)
  1416. {
  1417. int retval;
  1418. unsigned long data_page;
  1419. unsigned long type_page;
  1420. unsigned long dev_page;
  1421. char *dir_page;
  1422. retval = copy_mount_options(type, &type_page);
  1423. if (retval < 0)
  1424. return retval;
  1425. dir_page = getname(dir_name);
  1426. retval = PTR_ERR(dir_page);
  1427. if (IS_ERR(dir_page))
  1428. goto out1;
  1429. retval = copy_mount_options(dev_name, &dev_page);
  1430. if (retval < 0)
  1431. goto out2;
  1432. retval = copy_mount_options(data, &data_page);
  1433. if (retval < 0)
  1434. goto out3;
  1435. lock_kernel();
  1436. retval = do_mount((char *)dev_page, dir_page, (char *)type_page,
  1437. flags, (void *)data_page);
  1438. unlock_kernel();
  1439. free_page(data_page);
  1440. out3:
  1441. free_page(dev_page);
  1442. out2:
  1443. putname(dir_page);
  1444. out1:
  1445. free_page(type_page);
  1446. return retval;
  1447. }
  1448. /*
  1449. * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
  1450. * It can block. Requires the big lock held.
  1451. */
  1452. void set_fs_root(struct fs_struct *fs, struct path *path)
  1453. {
  1454. struct path old_root;
  1455. write_lock(&fs->lock);
  1456. old_root = fs->root;
  1457. fs->root = *path;
  1458. path_get(path);
  1459. write_unlock(&fs->lock);
  1460. if (old_root.dentry)
  1461. path_put(&old_root);
  1462. }
  1463. /*
  1464. * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
  1465. * It can block. Requires the big lock held.
  1466. */
  1467. void set_fs_pwd(struct fs_struct *fs, struct path *path)
  1468. {
  1469. struct path old_pwd;
  1470. write_lock(&fs->lock);
  1471. old_pwd = fs->pwd;
  1472. fs->pwd = *path;
  1473. path_get(path);
  1474. write_unlock(&fs->lock);
  1475. if (old_pwd.dentry)
  1476. path_put(&old_pwd);
  1477. }
  1478. static void chroot_fs_refs(struct path *old_root, struct path *new_root)
  1479. {
  1480. struct task_struct *g, *p;
  1481. struct fs_struct *fs;
  1482. read_lock(&tasklist_lock);
  1483. do_each_thread(g, p) {
  1484. task_lock(p);
  1485. fs = p->fs;
  1486. if (fs) {
  1487. atomic_inc(&fs->count);
  1488. task_unlock(p);
  1489. if (fs->root.dentry == old_root->dentry
  1490. && fs->root.mnt == old_root->mnt)
  1491. set_fs_root(fs, new_root);
  1492. if (fs->pwd.dentry == old_root->dentry
  1493. && fs->pwd.mnt == old_root->mnt)
  1494. set_fs_pwd(fs, new_root);
  1495. put_fs_struct(fs);
  1496. } else
  1497. task_unlock(p);
  1498. } while_each_thread(g, p);
  1499. read_unlock(&tasklist_lock);
  1500. }
  1501. /*
  1502. * pivot_root Semantics:
  1503. * Moves the root file system of the current process to the directory put_old,
  1504. * makes new_root as the new root file system of the current process, and sets
  1505. * root/cwd of all processes which had them on the current root to new_root.
  1506. *
  1507. * Restrictions:
  1508. * The new_root and put_old must be directories, and must not be on the
  1509. * same file system as the current process root. The put_old must be
  1510. * underneath new_root, i.e. adding a non-zero number of /.. to the string
  1511. * pointed to by put_old must yield the same directory as new_root. No other
  1512. * file system may be mounted on put_old. After all, new_root is a mountpoint.
  1513. *
  1514. * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
  1515. * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
  1516. * in this situation.
  1517. *
  1518. * Notes:
  1519. * - we don't move root/cwd if they are not at the root (reason: if something
  1520. * cared enough to change them, it's probably wrong to force them elsewhere)
  1521. * - it's okay to pick a root that isn't the root of a file system, e.g.
  1522. * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
  1523. * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
  1524. * first.
  1525. */
  1526. asmlinkage long sys_pivot_root(const char __user * new_root,
  1527. const char __user * put_old)
  1528. {
  1529. struct vfsmount *tmp;
  1530. struct nameidata new_nd, old_nd, user_nd;
  1531. struct path parent_path, root_parent;
  1532. int error;
  1533. if (!capable(CAP_SYS_ADMIN))
  1534. return -EPERM;
  1535. lock_kernel();
  1536. error = __user_walk(new_root, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
  1537. &new_nd);
  1538. if (error)
  1539. goto out0;
  1540. error = -EINVAL;
  1541. if (!check_mnt(new_nd.path.mnt))
  1542. goto out1;
  1543. error = __user_walk(put_old, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old_nd);
  1544. if (error)
  1545. goto out1;
  1546. error = security_sb_pivotroot(&old_nd, &new_nd);
  1547. if (error) {
  1548. path_put(&old_nd.path);
  1549. goto out1;
  1550. }
  1551. read_lock(&current->fs->lock);
  1552. user_nd.path = current->fs->root;
  1553. path_get(&current->fs->root);
  1554. read_unlock(&current->fs->lock);
  1555. down_write(&namespace_sem);
  1556. mutex_lock(&old_nd.path.dentry->d_inode->i_mutex);
  1557. error = -EINVAL;
  1558. if (IS_MNT_SHARED(old_nd.path.mnt) ||
  1559. IS_MNT_SHARED(new_nd.path.mnt->mnt_parent) ||
  1560. IS_MNT_SHARED(user_nd.path.mnt->mnt_parent))
  1561. goto out2;
  1562. if (!check_mnt(user_nd.path.mnt))
  1563. goto out2;
  1564. error = -ENOENT;
  1565. if (IS_DEADDIR(new_nd.path.dentry->d_inode))
  1566. goto out2;
  1567. if (d_unhashed(new_nd.path.dentry) && !IS_ROOT(new_nd.path.dentry))
  1568. goto out2;
  1569. if (d_unhashed(old_nd.path.dentry) && !IS_ROOT(old_nd.path.dentry))
  1570. goto out2;
  1571. error = -EBUSY;
  1572. if (new_nd.path.mnt == user_nd.path.mnt ||
  1573. old_nd.path.mnt == user_nd.path.mnt)
  1574. goto out2; /* loop, on the same file system */
  1575. error = -EINVAL;
  1576. if (user_nd.path.mnt->mnt_root != user_nd.path.dentry)
  1577. goto out2; /* not a mountpoint */
  1578. if (user_nd.path.mnt->mnt_parent == user_nd.path.mnt)
  1579. goto out2; /* not attached */
  1580. if (new_nd.path.mnt->mnt_root != new_nd.path.dentry)
  1581. goto out2; /* not a mountpoint */
  1582. if (new_nd.path.mnt->mnt_parent == new_nd.path.mnt)
  1583. goto out2; /* not attached */
  1584. /* make sure we can reach put_old from new_root */
  1585. tmp = old_nd.path.mnt;
  1586. spin_lock(&vfsmount_lock);
  1587. if (tmp != new_nd.path.mnt) {
  1588. for (;;) {
  1589. if (tmp->mnt_parent == tmp)
  1590. goto out3; /* already mounted on put_old */
  1591. if (tmp->mnt_parent == new_nd.path.mnt)
  1592. break;
  1593. tmp = tmp->mnt_parent;
  1594. }
  1595. if (!is_subdir(tmp->mnt_mountpoint, new_nd.path.dentry))
  1596. goto out3;
  1597. } else if (!is_subdir(old_nd.path.dentry, new_nd.path.dentry))
  1598. goto out3;
  1599. detach_mnt(new_nd.path.mnt, &parent_path);
  1600. detach_mnt(user_nd.path.mnt, &root_parent);
  1601. /* mount old root on put_old */
  1602. attach_mnt(user_nd.path.mnt, &old_nd.path);
  1603. /* mount new_root on / */
  1604. attach_mnt(new_nd.path.mnt, &root_parent);
  1605. touch_mnt_namespace(current->nsproxy->mnt_ns);
  1606. spin_unlock(&vfsmount_lock);
  1607. chroot_fs_refs(&user_nd.path, &new_nd.path);
  1608. security_sb_post_pivotroot(&user_nd, &new_nd);
  1609. error = 0;
  1610. path_put(&root_parent);
  1611. path_put(&parent_path);
  1612. out2:
  1613. mutex_unlock(&old_nd.path.dentry->d_inode->i_mutex);
  1614. up_write(&namespace_sem);
  1615. path_put(&user_nd.path);
  1616. path_put(&old_nd.path);
  1617. out1:
  1618. path_put(&new_nd.path);
  1619. out0:
  1620. unlock_kernel();
  1621. return error;
  1622. out3:
  1623. spin_unlock(&vfsmount_lock);
  1624. goto out2;
  1625. }
  1626. static void __init init_mount_tree(void)
  1627. {
  1628. struct vfsmount *mnt;
  1629. struct mnt_namespace *ns;
  1630. struct path root;
  1631. mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
  1632. if (IS_ERR(mnt))
  1633. panic("Can't create rootfs");
  1634. ns = kmalloc(sizeof(*ns), GFP_KERNEL);
  1635. if (!ns)
  1636. panic("Can't allocate initial namespace");
  1637. atomic_set(&ns->count, 1);
  1638. INIT_LIST_HEAD(&ns->list);
  1639. init_waitqueue_head(&ns->poll);
  1640. ns->event = 0;
  1641. list_add(&mnt->mnt_list, &ns->list);
  1642. ns->root = mnt;
  1643. mnt->mnt_ns = ns;
  1644. init_task.nsproxy->mnt_ns = ns;
  1645. get_mnt_ns(ns);
  1646. root.mnt = ns->root;
  1647. root.dentry = ns->root->mnt_root;
  1648. set_fs_pwd(current->fs, &root);
  1649. set_fs_root(current->fs, &root);
  1650. }
  1651. void __init mnt_init(void)
  1652. {
  1653. unsigned u;
  1654. int err;
  1655. init_rwsem(&namespace_sem);
  1656. mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
  1657. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  1658. mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
  1659. if (!mount_hashtable)
  1660. panic("Failed to allocate mount hash table\n");
  1661. printk("Mount-cache hash table entries: %lu\n", HASH_SIZE);
  1662. for (u = 0; u < HASH_SIZE; u++)
  1663. INIT_LIST_HEAD(&mount_hashtable[u]);
  1664. err = sysfs_init();
  1665. if (err)
  1666. printk(KERN_WARNING "%s: sysfs_init error: %d\n",
  1667. __FUNCTION__, err);
  1668. fs_kobj = kobject_create_and_add("fs", NULL);
  1669. if (!fs_kobj)
  1670. printk(KERN_WARNING "%s: kobj create error\n", __FUNCTION__);
  1671. init_rootfs();
  1672. init_mount_tree();
  1673. }
  1674. void __put_mnt_ns(struct mnt_namespace *ns)
  1675. {
  1676. struct vfsmount *root = ns->root;
  1677. LIST_HEAD(umount_list);
  1678. ns->root = NULL;
  1679. spin_unlock(&vfsmount_lock);
  1680. down_write(&namespace_sem);
  1681. spin_lock(&vfsmount_lock);
  1682. umount_tree(root, 0, &umount_list);
  1683. spin_unlock(&vfsmount_lock);
  1684. up_write(&namespace_sem);
  1685. release_mounts(&umount_list);
  1686. kfree(ns);
  1687. }