namespace.c 48 KB

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