namespace.c 58 KB

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