namespace.c 35 KB

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