namespace.c 41 KB

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