namespace.c 47 KB

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