namespace.c 59 KB

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