namespace.c 44 KB

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