namespace.c 58 KB

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