namespace.c 41 KB

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