namespace.c 35 KB

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