namespace.c 55 KB

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