namespace.c 45 KB

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