namespace.c 49 KB

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