namespace.c 57 KB

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