namespace.c 35 KB

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