namespace.c 36 KB

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