namespace.c 44 KB

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