namespace.c 62 KB

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