namespace.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  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_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 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_expire);
  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_expire);
  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. newmnt->mnt_namespace = current->namespace;
  709. err = graft_tree(newmnt, nd);
  710. if (err == 0 && fslist) {
  711. /* add to the specified expiration list */
  712. spin_lock(&vfsmount_lock);
  713. list_add_tail(&newmnt->mnt_expire, fslist);
  714. spin_unlock(&vfsmount_lock);
  715. }
  716. unlock:
  717. up_write(&current->namespace->sem);
  718. mntput(newmnt);
  719. return err;
  720. }
  721. EXPORT_SYMBOL_GPL(do_add_mount);
  722. static void expire_mount(struct vfsmount *mnt, struct list_head *mounts)
  723. {
  724. spin_lock(&vfsmount_lock);
  725. /*
  726. * Check if mount is still attached, if not, let whoever holds it deal
  727. * with the sucker
  728. */
  729. if (mnt->mnt_parent == mnt) {
  730. spin_unlock(&vfsmount_lock);
  731. return;
  732. }
  733. /*
  734. * Check that it is still dead: the count should now be 2 - as
  735. * contributed by the vfsmount parent and the mntget above
  736. */
  737. if (atomic_read(&mnt->mnt_count) == 2) {
  738. struct nameidata old_nd;
  739. /* delete from the namespace */
  740. list_del_init(&mnt->mnt_list);
  741. mnt->mnt_namespace = NULL;
  742. detach_mnt(mnt, &old_nd);
  743. spin_unlock(&vfsmount_lock);
  744. path_release(&old_nd);
  745. /*
  746. * Now lay it to rest if this was the last ref on the superblock
  747. */
  748. if (atomic_read(&mnt->mnt_sb->s_active) == 1) {
  749. /* last instance - try to be smart */
  750. lock_kernel();
  751. DQUOT_OFF(mnt->mnt_sb);
  752. acct_auto_close(mnt->mnt_sb);
  753. unlock_kernel();
  754. }
  755. mntput(mnt);
  756. } else {
  757. /*
  758. * Someone brought it back to life whilst we didn't have any
  759. * locks held so return it to the expiration list
  760. */
  761. list_add_tail(&mnt->mnt_expire, mounts);
  762. spin_unlock(&vfsmount_lock);
  763. }
  764. }
  765. /*
  766. * process a list of expirable mountpoints with the intent of discarding any
  767. * mountpoints that aren't in use and haven't been touched since last we came
  768. * here
  769. */
  770. void mark_mounts_for_expiry(struct list_head *mounts)
  771. {
  772. struct namespace *namespace;
  773. struct vfsmount *mnt, *next;
  774. LIST_HEAD(graveyard);
  775. if (list_empty(mounts))
  776. return;
  777. spin_lock(&vfsmount_lock);
  778. /* extract from the expiration list every vfsmount that matches the
  779. * following criteria:
  780. * - only referenced by its parent vfsmount
  781. * - still marked for expiry (marked on the last call here; marks are
  782. * cleared by mntput())
  783. */
  784. list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
  785. if (!xchg(&mnt->mnt_expiry_mark, 1) ||
  786. atomic_read(&mnt->mnt_count) != 1)
  787. continue;
  788. mntget(mnt);
  789. list_move(&mnt->mnt_expire, &graveyard);
  790. }
  791. /*
  792. * go through the vfsmounts we've just consigned to the graveyard to
  793. * - check that they're still dead
  794. * - delete the vfsmount from the appropriate namespace under lock
  795. * - dispose of the corpse
  796. */
  797. while (!list_empty(&graveyard)) {
  798. mnt = list_entry(graveyard.next, struct vfsmount, mnt_expire);
  799. list_del_init(&mnt->mnt_expire);
  800. /* don't do anything if the namespace is dead - all the
  801. * vfsmounts from it are going away anyway */
  802. namespace = mnt->mnt_namespace;
  803. if (!namespace || !namespace->root)
  804. continue;
  805. get_namespace(namespace);
  806. spin_unlock(&vfsmount_lock);
  807. down_write(&namespace->sem);
  808. expire_mount(mnt, mounts);
  809. up_write(&namespace->sem);
  810. mntput(mnt);
  811. put_namespace(namespace);
  812. spin_lock(&vfsmount_lock);
  813. }
  814. spin_unlock(&vfsmount_lock);
  815. }
  816. EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
  817. /*
  818. * Some copy_from_user() implementations do not return the exact number of
  819. * bytes remaining to copy on a fault. But copy_mount_options() requires that.
  820. * Note that this function differs from copy_from_user() in that it will oops
  821. * on bad values of `to', rather than returning a short copy.
  822. */
  823. static long
  824. exact_copy_from_user(void *to, const void __user *from, unsigned long n)
  825. {
  826. char *t = to;
  827. const char __user *f = from;
  828. char c;
  829. if (!access_ok(VERIFY_READ, from, n))
  830. return n;
  831. while (n) {
  832. if (__get_user(c, f)) {
  833. memset(t, 0, n);
  834. break;
  835. }
  836. *t++ = c;
  837. f++;
  838. n--;
  839. }
  840. return n;
  841. }
  842. int copy_mount_options(const void __user *data, unsigned long *where)
  843. {
  844. int i;
  845. unsigned long page;
  846. unsigned long size;
  847. *where = 0;
  848. if (!data)
  849. return 0;
  850. if (!(page = __get_free_page(GFP_KERNEL)))
  851. return -ENOMEM;
  852. /* We only care that *some* data at the address the user
  853. * gave us is valid. Just in case, we'll zero
  854. * the remainder of the page.
  855. */
  856. /* copy_from_user cannot cross TASK_SIZE ! */
  857. size = TASK_SIZE - (unsigned long)data;
  858. if (size > PAGE_SIZE)
  859. size = PAGE_SIZE;
  860. i = size - exact_copy_from_user((void *)page, data, size);
  861. if (!i) {
  862. free_page(page);
  863. return -EFAULT;
  864. }
  865. if (i != PAGE_SIZE)
  866. memset((char *)page + i, 0, PAGE_SIZE - i);
  867. *where = page;
  868. return 0;
  869. }
  870. /*
  871. * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
  872. * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
  873. *
  874. * data is a (void *) that can point to any structure up to
  875. * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
  876. * information (or be NULL).
  877. *
  878. * Pre-0.97 versions of mount() didn't have a flags word.
  879. * When the flags word was introduced its top half was required
  880. * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
  881. * Therefore, if this magic number is present, it carries no information
  882. * and must be discarded.
  883. */
  884. long do_mount(char * dev_name, char * dir_name, char *type_page,
  885. unsigned long flags, void *data_page)
  886. {
  887. struct nameidata nd;
  888. int retval = 0;
  889. int mnt_flags = 0;
  890. /* Discard magic */
  891. if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
  892. flags &= ~MS_MGC_MSK;
  893. /* Basic sanity checks */
  894. if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
  895. return -EINVAL;
  896. if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
  897. return -EINVAL;
  898. if (data_page)
  899. ((char *)data_page)[PAGE_SIZE - 1] = 0;
  900. /* Separate the per-mountpoint flags */
  901. if (flags & MS_NOSUID)
  902. mnt_flags |= MNT_NOSUID;
  903. if (flags & MS_NODEV)
  904. mnt_flags |= MNT_NODEV;
  905. if (flags & MS_NOEXEC)
  906. mnt_flags |= MNT_NOEXEC;
  907. flags &= ~(MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_ACTIVE);
  908. /* ... and get the mountpoint */
  909. retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
  910. if (retval)
  911. return retval;
  912. retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
  913. if (retval)
  914. goto dput_out;
  915. if (flags & MS_REMOUNT)
  916. retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
  917. data_page);
  918. else if (flags & MS_BIND)
  919. retval = do_loopback(&nd, dev_name, flags & MS_REC);
  920. else if (flags & MS_MOVE)
  921. retval = do_move_mount(&nd, dev_name);
  922. else
  923. retval = do_new_mount(&nd, type_page, flags, mnt_flags,
  924. dev_name, data_page);
  925. dput_out:
  926. path_release(&nd);
  927. return retval;
  928. }
  929. int copy_namespace(int flags, struct task_struct *tsk)
  930. {
  931. struct namespace *namespace = tsk->namespace;
  932. struct namespace *new_ns;
  933. struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
  934. struct fs_struct *fs = tsk->fs;
  935. struct vfsmount *p, *q;
  936. if (!namespace)
  937. return 0;
  938. get_namespace(namespace);
  939. if (!(flags & CLONE_NEWNS))
  940. return 0;
  941. if (!capable(CAP_SYS_ADMIN)) {
  942. put_namespace(namespace);
  943. return -EPERM;
  944. }
  945. new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
  946. if (!new_ns)
  947. goto out;
  948. atomic_set(&new_ns->count, 1);
  949. init_rwsem(&new_ns->sem);
  950. INIT_LIST_HEAD(&new_ns->list);
  951. down_write(&tsk->namespace->sem);
  952. /* First pass: copy the tree topology */
  953. new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root);
  954. if (!new_ns->root) {
  955. up_write(&tsk->namespace->sem);
  956. kfree(new_ns);
  957. goto out;
  958. }
  959. spin_lock(&vfsmount_lock);
  960. list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
  961. spin_unlock(&vfsmount_lock);
  962. /*
  963. * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
  964. * as belonging to new namespace. We have already acquired a private
  965. * fs_struct, so tsk->fs->lock is not needed.
  966. */
  967. p = namespace->root;
  968. q = new_ns->root;
  969. while (p) {
  970. q->mnt_namespace = new_ns;
  971. if (fs) {
  972. if (p == fs->rootmnt) {
  973. rootmnt = p;
  974. fs->rootmnt = mntget(q);
  975. }
  976. if (p == fs->pwdmnt) {
  977. pwdmnt = p;
  978. fs->pwdmnt = mntget(q);
  979. }
  980. if (p == fs->altrootmnt) {
  981. altrootmnt = p;
  982. fs->altrootmnt = mntget(q);
  983. }
  984. }
  985. p = next_mnt(p, namespace->root);
  986. q = next_mnt(q, new_ns->root);
  987. }
  988. up_write(&tsk->namespace->sem);
  989. tsk->namespace = new_ns;
  990. if (rootmnt)
  991. mntput(rootmnt);
  992. if (pwdmnt)
  993. mntput(pwdmnt);
  994. if (altrootmnt)
  995. mntput(altrootmnt);
  996. put_namespace(namespace);
  997. return 0;
  998. out:
  999. put_namespace(namespace);
  1000. return -ENOMEM;
  1001. }
  1002. asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
  1003. char __user * type, unsigned long flags,
  1004. void __user * data)
  1005. {
  1006. int retval;
  1007. unsigned long data_page;
  1008. unsigned long type_page;
  1009. unsigned long dev_page;
  1010. char *dir_page;
  1011. retval = copy_mount_options (type, &type_page);
  1012. if (retval < 0)
  1013. return retval;
  1014. dir_page = getname(dir_name);
  1015. retval = PTR_ERR(dir_page);
  1016. if (IS_ERR(dir_page))
  1017. goto out1;
  1018. retval = copy_mount_options (dev_name, &dev_page);
  1019. if (retval < 0)
  1020. goto out2;
  1021. retval = copy_mount_options (data, &data_page);
  1022. if (retval < 0)
  1023. goto out3;
  1024. lock_kernel();
  1025. retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
  1026. flags, (void*)data_page);
  1027. unlock_kernel();
  1028. free_page(data_page);
  1029. out3:
  1030. free_page(dev_page);
  1031. out2:
  1032. putname(dir_page);
  1033. out1:
  1034. free_page(type_page);
  1035. return retval;
  1036. }
  1037. /*
  1038. * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
  1039. * It can block. Requires the big lock held.
  1040. */
  1041. void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
  1042. struct dentry *dentry)
  1043. {
  1044. struct dentry *old_root;
  1045. struct vfsmount *old_rootmnt;
  1046. write_lock(&fs->lock);
  1047. old_root = fs->root;
  1048. old_rootmnt = fs->rootmnt;
  1049. fs->rootmnt = mntget(mnt);
  1050. fs->root = dget(dentry);
  1051. write_unlock(&fs->lock);
  1052. if (old_root) {
  1053. dput(old_root);
  1054. mntput(old_rootmnt);
  1055. }
  1056. }
  1057. /*
  1058. * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
  1059. * It can block. Requires the big lock held.
  1060. */
  1061. void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
  1062. struct dentry *dentry)
  1063. {
  1064. struct dentry *old_pwd;
  1065. struct vfsmount *old_pwdmnt;
  1066. write_lock(&fs->lock);
  1067. old_pwd = fs->pwd;
  1068. old_pwdmnt = fs->pwdmnt;
  1069. fs->pwdmnt = mntget(mnt);
  1070. fs->pwd = dget(dentry);
  1071. write_unlock(&fs->lock);
  1072. if (old_pwd) {
  1073. dput(old_pwd);
  1074. mntput(old_pwdmnt);
  1075. }
  1076. }
  1077. static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
  1078. {
  1079. struct task_struct *g, *p;
  1080. struct fs_struct *fs;
  1081. read_lock(&tasklist_lock);
  1082. do_each_thread(g, p) {
  1083. task_lock(p);
  1084. fs = p->fs;
  1085. if (fs) {
  1086. atomic_inc(&fs->count);
  1087. task_unlock(p);
  1088. if (fs->root==old_nd->dentry&&fs->rootmnt==old_nd->mnt)
  1089. set_fs_root(fs, new_nd->mnt, new_nd->dentry);
  1090. if (fs->pwd==old_nd->dentry&&fs->pwdmnt==old_nd->mnt)
  1091. set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
  1092. put_fs_struct(fs);
  1093. } else
  1094. task_unlock(p);
  1095. } while_each_thread(g, p);
  1096. read_unlock(&tasklist_lock);
  1097. }
  1098. /*
  1099. * pivot_root Semantics:
  1100. * Moves the root file system of the current process to the directory put_old,
  1101. * makes new_root as the new root file system of the current process, and sets
  1102. * root/cwd of all processes which had them on the current root to new_root.
  1103. *
  1104. * Restrictions:
  1105. * The new_root and put_old must be directories, and must not be on the
  1106. * same file system as the current process root. The put_old must be
  1107. * underneath new_root, i.e. adding a non-zero number of /.. to the string
  1108. * pointed to by put_old must yield the same directory as new_root. No other
  1109. * file system may be mounted on put_old. After all, new_root is a mountpoint.
  1110. *
  1111. * Notes:
  1112. * - we don't move root/cwd if they are not at the root (reason: if something
  1113. * cared enough to change them, it's probably wrong to force them elsewhere)
  1114. * - it's okay to pick a root that isn't the root of a file system, e.g.
  1115. * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
  1116. * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
  1117. * first.
  1118. */
  1119. asmlinkage long sys_pivot_root(const char __user *new_root, const char __user *put_old)
  1120. {
  1121. struct vfsmount *tmp;
  1122. struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
  1123. int error;
  1124. if (!capable(CAP_SYS_ADMIN))
  1125. return -EPERM;
  1126. lock_kernel();
  1127. error = __user_walk(new_root, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &new_nd);
  1128. if (error)
  1129. goto out0;
  1130. error = -EINVAL;
  1131. if (!check_mnt(new_nd.mnt))
  1132. goto out1;
  1133. error = __user_walk(put_old, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &old_nd);
  1134. if (error)
  1135. goto out1;
  1136. error = security_sb_pivotroot(&old_nd, &new_nd);
  1137. if (error) {
  1138. path_release(&old_nd);
  1139. goto out1;
  1140. }
  1141. read_lock(&current->fs->lock);
  1142. user_nd.mnt = mntget(current->fs->rootmnt);
  1143. user_nd.dentry = dget(current->fs->root);
  1144. read_unlock(&current->fs->lock);
  1145. down_write(&current->namespace->sem);
  1146. down(&old_nd.dentry->d_inode->i_sem);
  1147. error = -EINVAL;
  1148. if (!check_mnt(user_nd.mnt))
  1149. goto out2;
  1150. error = -ENOENT;
  1151. if (IS_DEADDIR(new_nd.dentry->d_inode))
  1152. goto out2;
  1153. if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
  1154. goto out2;
  1155. if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
  1156. goto out2;
  1157. error = -EBUSY;
  1158. if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
  1159. goto out2; /* loop, on the same file system */
  1160. error = -EINVAL;
  1161. if (user_nd.mnt->mnt_root != user_nd.dentry)
  1162. goto out2; /* not a mountpoint */
  1163. if (new_nd.mnt->mnt_root != new_nd.dentry)
  1164. goto out2; /* not a mountpoint */
  1165. tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
  1166. spin_lock(&vfsmount_lock);
  1167. if (tmp != new_nd.mnt) {
  1168. for (;;) {
  1169. if (tmp->mnt_parent == tmp)
  1170. goto out3; /* already mounted on put_old */
  1171. if (tmp->mnt_parent == new_nd.mnt)
  1172. break;
  1173. tmp = tmp->mnt_parent;
  1174. }
  1175. if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
  1176. goto out3;
  1177. } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
  1178. goto out3;
  1179. detach_mnt(new_nd.mnt, &parent_nd);
  1180. detach_mnt(user_nd.mnt, &root_parent);
  1181. attach_mnt(user_nd.mnt, &old_nd); /* mount old root on put_old */
  1182. attach_mnt(new_nd.mnt, &root_parent); /* mount new_root on / */
  1183. spin_unlock(&vfsmount_lock);
  1184. chroot_fs_refs(&user_nd, &new_nd);
  1185. security_sb_post_pivotroot(&user_nd, &new_nd);
  1186. error = 0;
  1187. path_release(&root_parent);
  1188. path_release(&parent_nd);
  1189. out2:
  1190. up(&old_nd.dentry->d_inode->i_sem);
  1191. up_write(&current->namespace->sem);
  1192. path_release(&user_nd);
  1193. path_release(&old_nd);
  1194. out1:
  1195. path_release(&new_nd);
  1196. out0:
  1197. unlock_kernel();
  1198. return error;
  1199. out3:
  1200. spin_unlock(&vfsmount_lock);
  1201. goto out2;
  1202. }
  1203. static void __init init_mount_tree(void)
  1204. {
  1205. struct vfsmount *mnt;
  1206. struct namespace *namespace;
  1207. struct task_struct *g, *p;
  1208. mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
  1209. if (IS_ERR(mnt))
  1210. panic("Can't create rootfs");
  1211. namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
  1212. if (!namespace)
  1213. panic("Can't allocate initial namespace");
  1214. atomic_set(&namespace->count, 1);
  1215. INIT_LIST_HEAD(&namespace->list);
  1216. init_rwsem(&namespace->sem);
  1217. list_add(&mnt->mnt_list, &namespace->list);
  1218. namespace->root = mnt;
  1219. mnt->mnt_namespace = namespace;
  1220. init_task.namespace = namespace;
  1221. read_lock(&tasklist_lock);
  1222. do_each_thread(g, p) {
  1223. get_namespace(namespace);
  1224. p->namespace = namespace;
  1225. } while_each_thread(g, p);
  1226. read_unlock(&tasklist_lock);
  1227. set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
  1228. set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
  1229. }
  1230. void __init mnt_init(unsigned long mempages)
  1231. {
  1232. struct list_head *d;
  1233. unsigned int nr_hash;
  1234. int i;
  1235. mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
  1236. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  1237. mount_hashtable = (struct list_head *)
  1238. __get_free_page(GFP_ATOMIC);
  1239. if (!mount_hashtable)
  1240. panic("Failed to allocate mount hash table\n");
  1241. /*
  1242. * Find the power-of-two list-heads that can fit into the allocation..
  1243. * We don't guarantee that "sizeof(struct list_head)" is necessarily
  1244. * a power-of-two.
  1245. */
  1246. nr_hash = PAGE_SIZE / sizeof(struct list_head);
  1247. hash_bits = 0;
  1248. do {
  1249. hash_bits++;
  1250. } while ((nr_hash >> hash_bits) != 0);
  1251. hash_bits--;
  1252. /*
  1253. * Re-calculate the actual number of entries and the mask
  1254. * from the number of bits we can fit.
  1255. */
  1256. nr_hash = 1UL << hash_bits;
  1257. hash_mask = nr_hash-1;
  1258. printk("Mount-cache hash table entries: %d\n", nr_hash);
  1259. /* And initialize the newly allocated array */
  1260. d = mount_hashtable;
  1261. i = nr_hash;
  1262. do {
  1263. INIT_LIST_HEAD(d);
  1264. d++;
  1265. i--;
  1266. } while (i);
  1267. sysfs_init();
  1268. init_rootfs();
  1269. init_mount_tree();
  1270. }
  1271. void __put_namespace(struct namespace *namespace)
  1272. {
  1273. struct vfsmount *root = namespace->root;
  1274. namespace->root = NULL;
  1275. spin_unlock(&vfsmount_lock);
  1276. down_write(&namespace->sem);
  1277. spin_lock(&vfsmount_lock);
  1278. umount_tree(root);
  1279. spin_unlock(&vfsmount_lock);
  1280. up_write(&namespace->sem);
  1281. kfree(namespace);
  1282. }