namespace.c 49 KB

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